2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_ipfw.h" /* for ipfw_fwd */
37 #include "opt_inet6.h"
38 #include "opt_ipsec.h"
39 #include "opt_tcpdebug.h"
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
45 #include <sys/proc.h> /* for proc0 declaration */
46 #include <sys/protosw.h>
47 #include <sys/signalvar.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
52 #include <sys/systm.h>
54 #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */
59 #include <net/route.h>
62 #define TCPSTATES /* for logging */
64 #include <netinet/in.h>
65 #include <netinet/in_pcb.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip.h>
69 #include <netinet/ip_icmp.h> /* required for icmp_var.h */
70 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
71 #include <netinet/ip_var.h>
72 #include <netinet/ip_options.h>
73 #include <netinet/ip6.h>
74 #include <netinet/icmp6.h>
75 #include <netinet6/in6_pcb.h>
76 #include <netinet6/ip6_var.h>
77 #include <netinet6/nd6.h>
78 #include <netinet/tcp.h>
79 #include <netinet/tcp_fsm.h>
80 #include <netinet/tcp_seq.h>
81 #include <netinet/tcp_timer.h>
82 #include <netinet/tcp_var.h>
83 #include <netinet6/tcp6_var.h>
84 #include <netinet/tcpip.h>
85 #include <netinet/tcp_syncache.h>
87 #include <netinet/tcp_debug.h>
91 #include <netipsec/ipsec.h>
92 #include <netipsec/ipsec6.h>
95 #include <machine/in_cksum.h>
97 #include <security/mac/mac_framework.h>
99 static const int tcprexmtthresh = 3;
101 VNET_DEFINE(struct tcpstat, tcpstat);
102 SYSCTL_VNET_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW,
103 &VNET_NAME(tcpstat), tcpstat,
104 "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
106 int tcp_log_in_vain = 0;
107 SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
109 "Log all incoming TCP segments to closed ports");
111 VNET_DEFINE(int, blackhole) = 0;
112 #define V_blackhole VNET(blackhole)
113 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
114 &VNET_NAME(blackhole), 0,
115 "Do not send RST on segments to closed ports");
117 VNET_DEFINE(int, tcp_delack_enabled) = 1;
118 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
119 &VNET_NAME(tcp_delack_enabled), 0,
120 "Delay ACK to try and piggyback it onto a data packet");
122 VNET_DEFINE(int, drop_synfin) = 0;
123 #define V_drop_synfin VNET(drop_synfin)
124 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
125 &VNET_NAME(drop_synfin), 0,
126 "Drop TCP packets with SYN+FIN set");
128 VNET_DEFINE(int, tcp_do_rfc3042) = 1;
129 #define V_tcp_do_rfc3042 VNET(tcp_do_rfc3042)
130 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW,
131 &VNET_NAME(tcp_do_rfc3042), 0,
132 "Enable RFC 3042 (Limited Transmit)");
134 VNET_DEFINE(int, tcp_do_rfc3390) = 1;
135 #define V_tcp_do_rfc3390 VNET(tcp_do_rfc3390)
136 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW,
137 &VNET_NAME(tcp_do_rfc3390), 0,
138 "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
140 VNET_DEFINE(int, tcp_do_rfc3465) = 1;
141 #define V_tcp_do_rfc3465 VNET(tcp_do_rfc3465)
142 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_RW,
143 &VNET_NAME(tcp_do_rfc3465), 0,
144 "Enable RFC 3465 (Appropriate Byte Counting)");
146 VNET_DEFINE(int, tcp_abc_l_var) = 2;
147 #define V_tcp_abc_l_var VNET(tcp_abc_l_var)
148 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_RW,
149 &VNET_NAME(tcp_abc_l_var), 2,
150 "Cap the max cwnd increment during slow-start to this number of segments");
152 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN");
154 VNET_DEFINE(int, tcp_do_ecn) = 0;
155 SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_RW,
156 &VNET_NAME(tcp_do_ecn), 0,
159 VNET_DEFINE(int, tcp_ecn_maxretries) = 1;
160 SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_RW,
161 &VNET_NAME(tcp_ecn_maxretries), 0,
162 "Max retries before giving up on ECN");
164 VNET_DEFINE(int, tcp_insecure_rst) = 0;
165 #define V_tcp_insecure_rst VNET(tcp_insecure_rst)
166 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW,
167 &VNET_NAME(tcp_insecure_rst), 0,
168 "Follow the old (insecure) criteria for accepting RST packets");
170 VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
171 #define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf)
172 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW,
173 &VNET_NAME(tcp_do_autorcvbuf), 0,
174 "Enable automatic receive buffer sizing");
176 VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024;
177 #define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc)
178 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW,
179 &VNET_NAME(tcp_autorcvbuf_inc), 0,
180 "Incrementor step size of automatic receive buffer");
182 VNET_DEFINE(int, tcp_autorcvbuf_max) = 256*1024;
183 #define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max)
184 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW,
185 &VNET_NAME(tcp_autorcvbuf_max), 0,
186 "Max size of automatic receive buffer");
188 int tcp_read_locking = 1;
189 SYSCTL_INT(_net_inet_tcp, OID_AUTO, read_locking, CTLFLAG_RW,
190 &tcp_read_locking, 0, "Enable read locking strategy");
192 VNET_DEFINE(struct inpcbhead, tcb);
193 #define tcb6 tcb /* for KAME src sync over BSD*'s */
194 VNET_DEFINE(struct inpcbinfo, tcbinfo);
196 static void tcp_dooptions(struct tcpopt *, u_char *, int, int);
197 static void tcp_do_segment(struct mbuf *, struct tcphdr *,
198 struct socket *, struct tcpcb *, int, int, uint8_t,
200 static void tcp_dropwithreset(struct mbuf *, struct tcphdr *,
201 struct tcpcb *, int, int);
202 static void tcp_pulloutofband(struct socket *,
203 struct tcphdr *, struct mbuf *, int);
204 static void tcp_xmit_timer(struct tcpcb *, int);
205 static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
207 tcp_congestion_exp(struct tcpcb *);
210 * Kernel module interface for updating tcpstat. The argument is an index
211 * into tcpstat treated as an array of u_long. While this encodes the
212 * general layout of tcpstat into the caller, it doesn't encode its location,
213 * so that future changes to add, for example, per-CPU stats support won't
214 * cause binary compatibility problems for kernel modules.
217 kmod_tcpstat_inc(int statnum)
220 (*((u_long *)&V_tcpstat + statnum))++;
224 tcp_congestion_exp(struct tcpcb *tp)
228 win = min(tp->snd_wnd, tp->snd_cwnd) /
232 tp->snd_ssthresh = win * tp->t_maxseg;
233 ENTER_FASTRECOVERY(tp);
234 tp->snd_recover = tp->snd_max;
235 if (tp->t_flags & TF_ECN_PERMIT)
236 tp->t_flags |= TF_ECN_SND_CWR;
239 /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
241 #define ND6_HINT(tp) \
243 if ((tp) && (tp)->t_inpcb && \
244 ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \
245 nd6_nud_hint(NULL, NULL, 0); \
252 * Indicate whether this ack should be delayed. We can delay the ack if
253 * - there is no delayed ack timer in progress and
254 * - our last ack wasn't a 0-sized window. We never want to delay
255 * the ack that opens up a 0-sized window and
256 * - delayed acks are enabled or
257 * - this is a half-synchronized T/TCP connection.
259 #define DELAY_ACK(tp) \
260 ((!tcp_timer_active(tp, TT_DELACK) && \
261 (tp->t_flags & TF_RXWIN0SENT) == 0) && \
262 (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
265 * TCP input handling is split into multiple parts:
266 * tcp6_input is a thin wrapper around tcp_input for the extended
267 * ip6_protox[] call format in ip6_input
268 * tcp_input handles primary segment validation, inpcb lookup and
269 * SYN processing on listen sockets
270 * tcp_do_segment processes the ACK and text of the segment for
271 * establishing, established and closing connections
275 tcp6_input(struct mbuf **mp, int *offp, int proto)
277 struct mbuf *m = *mp;
278 struct in6_ifaddr *ia6;
280 IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
283 * draft-itojun-ipv6-tcp-to-anycast
284 * better place to put this in?
286 ia6 = ip6_getdstifaddr(m);
287 if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
290 ifa_free(&ia6->ia_ifa);
291 ip6 = mtod(m, struct ip6_hdr *);
292 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
293 (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
297 ifa_free(&ia6->ia_ifa);
305 tcp_input(struct mbuf *m, int off0)
308 struct ip *ip = NULL;
310 struct inpcb *inp = NULL;
311 struct tcpcb *tp = NULL;
312 struct socket *so = NULL;
318 int rstreason = 0; /* For badport_bandlim accounting purposes */
320 #ifdef IPFIREWALL_FORWARD
321 struct m_tag *fwd_tag;
324 struct ip6_hdr *ip6 = NULL;
327 const void *ip6 = NULL;
328 const int isipv6 = 0;
330 struct tcpopt to; /* options in this segment */
331 char *s = NULL; /* address and port logging */
333 #define TI_UNLOCKED 1
339 * The size of tcp_saveipgen must be the size of the max ip header,
342 u_char tcp_saveipgen[IP6_HDR_LEN];
343 struct tcphdr tcp_savetcp;
348 isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
352 TCPSTAT_INC(tcps_rcvtotal);
356 /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */
357 ip6 = mtod(m, struct ip6_hdr *);
358 tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
359 if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
360 TCPSTAT_INC(tcps_rcvbadsum);
363 th = (struct tcphdr *)((caddr_t)ip6 + off0);
366 * Be proactive about unspecified IPv6 address in source.
367 * As we use all-zero to indicate unbounded/unconnected pcb,
368 * unspecified IPv6 address can be used to confuse us.
370 * Note that packets with unspecified IPv6 destination is
371 * already dropped in ip6_input.
373 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
378 th = NULL; /* XXX: Avoid compiler warning. */
382 * Get IP and TCP header together in first mbuf.
383 * Note: IP leaves IP header in first mbuf.
385 if (off0 > sizeof (struct ip)) {
386 ip_stripoptions(m, (struct mbuf *)0);
387 off0 = sizeof(struct ip);
389 if (m->m_len < sizeof (struct tcpiphdr)) {
390 if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
392 TCPSTAT_INC(tcps_rcvshort);
396 ip = mtod(m, struct ip *);
397 ipov = (struct ipovly *)ip;
398 th = (struct tcphdr *)((caddr_t)ip + off0);
401 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
402 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
403 th->th_sum = m->m_pkthdr.csum_data;
405 th->th_sum = in_pseudo(ip->ip_src.s_addr,
407 htonl(m->m_pkthdr.csum_data +
410 th->th_sum ^= 0xffff;
412 ipov->ih_len = (u_short)tlen;
413 ipov->ih_len = htons(ipov->ih_len);
417 * Checksum extended TCP header and data.
419 len = sizeof (struct ip) + tlen;
420 bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
421 ipov->ih_len = (u_short)tlen;
422 ipov->ih_len = htons(ipov->ih_len);
423 th->th_sum = in_cksum(m, len);
426 TCPSTAT_INC(tcps_rcvbadsum);
429 /* Re-initialization for later version check */
430 ip->ip_v = IPVERSION;
435 iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
441 * Check that TCP offset makes sense,
442 * pull out TCP options and adjust length. XXX
444 off = th->th_off << 2;
445 if (off < sizeof (struct tcphdr) || off > tlen) {
446 TCPSTAT_INC(tcps_rcvbadoff);
449 tlen -= off; /* tlen is used instead of ti->ti_len */
450 if (off > sizeof (struct tcphdr)) {
453 IP6_EXTHDR_CHECK(m, off0, off, );
454 ip6 = mtod(m, struct ip6_hdr *);
455 th = (struct tcphdr *)((caddr_t)ip6 + off0);
458 if (m->m_len < sizeof(struct ip) + off) {
459 if ((m = m_pullup(m, sizeof (struct ip) + off))
461 TCPSTAT_INC(tcps_rcvshort);
464 ip = mtod(m, struct ip *);
465 ipov = (struct ipovly *)ip;
466 th = (struct tcphdr *)((caddr_t)ip + off0);
469 optlen = off - sizeof (struct tcphdr);
470 optp = (u_char *)(th + 1);
472 thflags = th->th_flags;
475 * Convert TCP protocol specific fields to host format.
477 th->th_seq = ntohl(th->th_seq);
478 th->th_ack = ntohl(th->th_ack);
479 th->th_win = ntohs(th->th_win);
480 th->th_urp = ntohs(th->th_urp);
483 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
485 drop_hdrlen = off0 + off;
488 * Locate pcb for segment, which requires a lock on tcbinfo.
489 * Optimisticaly acquire a global read lock rather than a write lock
490 * unless header flags necessarily imply a state change. There are
491 * two cases where we might discover later we need a write lock
492 * despite the flags: ACKs moving a connection out of the syncache,
493 * and ACKs for a connection in TIMEWAIT.
495 if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
496 tcp_read_locking == 0) {
497 INP_INFO_WLOCK(&V_tcbinfo);
498 ti_locked = TI_WLOCKED;
500 INP_INFO_RLOCK(&V_tcbinfo);
501 ti_locked = TI_RLOCKED;
506 if (ti_locked == TI_RLOCKED)
507 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
508 else if (ti_locked == TI_WLOCKED)
509 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
511 panic("%s: findpcb ti_locked %d\n", __func__, ti_locked);
514 #ifdef IPFIREWALL_FORWARD
516 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
518 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
520 if (fwd_tag != NULL && isipv6 == 0) { /* IPv6 support is not yet */
521 struct sockaddr_in *next_hop;
523 next_hop = (struct sockaddr_in *)(fwd_tag+1);
525 * Transparently forwarded. Pretend to be the destination.
526 * already got one like this?
528 inp = in_pcblookup_hash(&V_tcbinfo,
529 ip->ip_src, th->th_sport,
530 ip->ip_dst, th->th_dport,
531 0, m->m_pkthdr.rcvif);
533 /* It's new. Try to find the ambushing socket. */
534 inp = in_pcblookup_hash(&V_tcbinfo,
535 ip->ip_src, th->th_sport,
538 ntohs(next_hop->sin_port) :
543 /* Remove the tag from the packet. We don't need it anymore. */
544 m_tag_delete(m, fwd_tag);
546 #endif /* IPFIREWALL_FORWARD */
550 inp = in6_pcblookup_hash(&V_tcbinfo,
551 &ip6->ip6_src, th->th_sport,
552 &ip6->ip6_dst, th->th_dport,
557 inp = in_pcblookup_hash(&V_tcbinfo,
558 ip->ip_src, th->th_sport,
559 ip->ip_dst, th->th_dport,
565 * If the INPCB does not exist then all data in the incoming
566 * segment is discarded and an appropriate RST is sent back.
567 * XXX MRT Send RST using which routing table?
571 * Log communication attempts to ports that are not
574 if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
575 tcp_log_in_vain == 2) {
576 if ((s = tcp_log_addrs(NULL, th, (void *)ip, ip6)))
577 log(LOG_INFO, "%s; %s: Connection attempt "
578 "to closed port\n", s, __func__);
581 * When blackholing do not respond with a RST but
582 * completely ignore the segment and drop it.
584 if ((V_blackhole == 1 && (thflags & TH_SYN)) ||
588 rstreason = BANDLIM_RST_CLOSEDPORT;
592 if (!(inp->inp_flags & INP_HW_FLOWID)
593 && (m->m_flags & M_FLOWID)
594 && ((inp->inp_socket == NULL)
595 || !(inp->inp_socket->so_options & SO_ACCEPTCONN))) {
596 inp->inp_flags |= INP_HW_FLOWID;
597 inp->inp_flags &= ~INP_SW_FLOWID;
598 inp->inp_flowid = m->m_pkthdr.flowid;
602 if (isipv6 && ipsec6_in_reject(m, inp)) {
603 V_ipsec6stat.in_polvio++;
607 if (ipsec4_in_reject(m, inp) != 0) {
608 V_ipsec4stat.in_polvio++;
614 * Check the minimum TTL for socket.
616 if (inp->inp_ip_minttl != 0) {
618 if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim)
622 if (inp->inp_ip_minttl > ip->ip_ttl)
627 * A previous connection in TIMEWAIT state is supposed to catch stray
628 * or duplicate segments arriving late. If this segment was a
629 * legitimate new connection attempt the old INPCB gets removed and
630 * we can try again to find a listening socket.
632 * At this point, due to earlier optimism, we may hold a read lock on
633 * the inpcbinfo, rather than a write lock. If so, we need to
634 * upgrade, or if that fails, acquire a reference on the inpcb, drop
635 * all locks, acquire a global write lock, and then re-acquire the
636 * inpcb lock. We may at that point discover that another thread has
637 * tried to free the inpcb, in which case we need to loop back and
638 * try to find a new inpcb to deliver to.
641 if (inp->inp_flags & INP_TIMEWAIT) {
642 KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED,
643 ("%s: INP_TIMEWAIT ti_locked %d", __func__, ti_locked));
645 if (ti_locked == TI_RLOCKED) {
646 if (INP_INFO_TRY_UPGRADE(&V_tcbinfo) == 0) {
649 INP_INFO_RUNLOCK(&V_tcbinfo);
650 INP_INFO_WLOCK(&V_tcbinfo);
651 ti_locked = TI_WLOCKED;
653 if (in_pcbrele(inp)) {
658 ti_locked = TI_WLOCKED;
660 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
662 if (thflags & TH_SYN)
663 tcp_dooptions(&to, optp, optlen, TO_SYN);
665 * NB: tcp_twcheck unlocks the INP and frees the mbuf.
667 if (tcp_twcheck(inp, &to, th, m, tlen))
669 INP_INFO_WUNLOCK(&V_tcbinfo);
673 * The TCPCB may no longer exist if the connection is winding
674 * down or it is in the CLOSED state. Either way we drop the
675 * segment and send an appropriate response.
678 if (tp == NULL || tp->t_state == TCPS_CLOSED) {
679 rstreason = BANDLIM_RST_CLOSEDPORT;
684 * We've identified a valid inpcb, but it could be that we need an
685 * inpcbinfo write lock and have only a read lock. In this case,
686 * attempt to upgrade/relock using the same strategy as the TIMEWAIT
687 * case above. If we relock, we have to jump back to 'relocked' as
688 * the connection might now be in TIMEWAIT.
690 if (tp->t_state != TCPS_ESTABLISHED ||
691 (thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
692 tcp_read_locking == 0) {
693 KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED,
694 ("%s: upgrade check ti_locked %d", __func__, ti_locked));
696 if (ti_locked == TI_RLOCKED) {
697 if (INP_INFO_TRY_UPGRADE(&V_tcbinfo) == 0) {
700 INP_INFO_RUNLOCK(&V_tcbinfo);
701 INP_INFO_WLOCK(&V_tcbinfo);
702 ti_locked = TI_WLOCKED;
704 if (in_pcbrele(inp)) {
710 ti_locked = TI_WLOCKED;
712 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
716 INP_WLOCK_ASSERT(inp);
717 if (mac_inpcb_check_deliver(inp, m))
720 so = inp->inp_socket;
721 KASSERT(so != NULL, ("%s: so == NULL", __func__));
723 if (so->so_options & SO_DEBUG) {
724 ostate = tp->t_state;
727 bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
730 bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
735 * When the socket is accepting connections (the INPCB is in LISTEN
736 * state) we look into the SYN cache if this is a new connection
737 * attempt or the completion of a previous one.
739 if (so->so_options & SO_ACCEPTCONN) {
740 struct in_conninfo inc;
742 KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but "
743 "tp not listening", __func__));
745 bzero(&inc, sizeof(inc));
748 inc.inc_flags |= INC_ISIPV6;
749 inc.inc6_faddr = ip6->ip6_src;
750 inc.inc6_laddr = ip6->ip6_dst;
754 inc.inc_faddr = ip->ip_src;
755 inc.inc_laddr = ip->ip_dst;
757 inc.inc_fport = th->th_sport;
758 inc.inc_lport = th->th_dport;
759 inc.inc_fibnum = so->so_fibnum;
762 * Check for an existing connection attempt in syncache if
763 * the flag is only ACK. A successful lookup creates a new
764 * socket appended to the listen queue in SYN_RECEIVED state.
766 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
768 * Parse the TCP options here because
769 * syncookies need access to the reflected
772 tcp_dooptions(&to, optp, optlen, 0);
774 * NB: syncache_expand() doesn't unlock
775 * inp and tcpinfo locks.
777 if (!syncache_expand(&inc, &to, th, &so, m)) {
779 * No syncache entry or ACK was not
780 * for our SYN/ACK. Send a RST.
781 * NB: syncache did its own logging
782 * of the failure cause.
784 rstreason = BANDLIM_RST_OPENPORT;
789 * We completed the 3-way handshake
790 * but could not allocate a socket
791 * either due to memory shortage,
792 * listen queue length limits or
793 * global socket limits. Send RST
794 * or wait and have the remote end
795 * retransmit the ACK for another
798 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
799 log(LOG_DEBUG, "%s; %s: Listen socket: "
800 "Socket allocation failed due to "
801 "limits or memory shortage, %s\n",
803 V_tcp_sc_rst_sock_fail ?
804 "sending RST" : "try again");
805 if (V_tcp_sc_rst_sock_fail) {
806 rstreason = BANDLIM_UNLIMITED;
812 * Socket is created in state SYN_RECEIVED.
813 * Unlock the listen socket, lock the newly
814 * created socket and update the tp variable.
816 INP_WUNLOCK(inp); /* listen socket */
818 INP_WLOCK(inp); /* new connection */
820 KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
823 * Process the segment and the data it
824 * contains. tcp_do_segment() consumes
825 * the mbuf chain and unlocks the inpcb.
827 tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
829 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
833 * Segment flag validation for new connection attempts:
835 * Our (SYN|ACK) response was rejected.
836 * Check with syncache and remove entry to prevent
839 * NB: syncache_chkrst does its own logging of failure
842 if (thflags & TH_RST) {
843 syncache_chkrst(&inc, th);
847 * We can't do anything without SYN.
849 if ((thflags & TH_SYN) == 0) {
850 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
851 log(LOG_DEBUG, "%s; %s: Listen socket: "
852 "SYN is missing, segment ignored\n",
854 TCPSTAT_INC(tcps_badsyn);
858 * (SYN|ACK) is bogus on a listen socket.
860 if (thflags & TH_ACK) {
861 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
862 log(LOG_DEBUG, "%s; %s: Listen socket: "
863 "SYN|ACK invalid, segment rejected\n",
865 syncache_badack(&inc); /* XXX: Not needed! */
866 TCPSTAT_INC(tcps_badsyn);
867 rstreason = BANDLIM_RST_OPENPORT;
871 * If the drop_synfin option is enabled, drop all
872 * segments with both the SYN and FIN bits set.
873 * This prevents e.g. nmap from identifying the
875 * XXX: Poor reasoning. nmap has other methods
876 * and is constantly refining its stack detection
878 * XXX: This is a violation of the TCP specification
879 * and was used by RFC1644.
881 if ((thflags & TH_FIN) && V_drop_synfin) {
882 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
883 log(LOG_DEBUG, "%s; %s: Listen socket: "
884 "SYN|FIN segment ignored (based on "
885 "sysctl setting)\n", s, __func__);
886 TCPSTAT_INC(tcps_badsyn);
890 * Segment's flags are (SYN) or (SYN|FIN).
892 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
893 * as they do not affect the state of the TCP FSM.
894 * The data pointed to by TH_URG and th_urp is ignored.
896 KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
897 ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
898 KASSERT(thflags & (TH_SYN),
899 ("%s: Listen socket: TH_SYN not set", __func__));
902 * If deprecated address is forbidden,
903 * we do not accept SYN to deprecated interface
904 * address to prevent any new inbound connection from
905 * getting established.
906 * When we do not accept SYN, we send a TCP RST,
907 * with deprecated source address (instead of dropping
908 * it). We compromise it as it is much better for peer
909 * to send a RST, and RST will be the final packet
912 * If we do not forbid deprecated addresses, we accept
913 * the SYN packet. RFC2462 does not suggest dropping
915 * If we decipher RFC2462 5.5.4, it says like this:
916 * 1. use of deprecated addr with existing
917 * communication is okay - "SHOULD continue to be
919 * 2. use of it with new communication:
920 * (2a) "SHOULD NOT be used if alternate address
921 * with sufficient scope is available"
922 * (2b) nothing mentioned otherwise.
923 * Here we fall into (2b) case as we have no choice in
924 * our source address selection - we must obey the peer.
926 * The wording in RFC2462 is confusing, and there are
927 * multiple description text for deprecated address
928 * handling - worse, they are not exactly the same.
929 * I believe 5.5.4 is the best one, so we follow 5.5.4.
931 if (isipv6 && !V_ip6_use_deprecated) {
932 struct in6_ifaddr *ia6;
934 ia6 = ip6_getdstifaddr(m);
936 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
937 ifa_free(&ia6->ia_ifa);
938 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
939 log(LOG_DEBUG, "%s; %s: Listen socket: "
940 "Connection attempt to deprecated "
941 "IPv6 address rejected\n",
943 rstreason = BANDLIM_RST_OPENPORT;
947 ifa_free(&ia6->ia_ifa);
951 * Basic sanity checks on incoming SYN requests:
952 * Don't respond if the destination is a link layer
953 * broadcast according to RFC1122 4.2.3.10, p. 104.
954 * If it is from this socket it must be forged.
955 * Don't respond if the source or destination is a
956 * global or subnet broad- or multicast address.
957 * Note that it is quite possible to receive unicast
958 * link-layer packets with a broadcast IP address. Use
959 * in_broadcast() to find them.
961 if (m->m_flags & (M_BCAST|M_MCAST)) {
962 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
963 log(LOG_DEBUG, "%s; %s: Listen socket: "
964 "Connection attempt from broad- or multicast "
965 "link layer address ignored\n", s, __func__);
970 if (th->th_dport == th->th_sport &&
971 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
972 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
973 log(LOG_DEBUG, "%s; %s: Listen socket: "
974 "Connection attempt to/from self "
975 "ignored\n", s, __func__);
978 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
979 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
980 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
981 log(LOG_DEBUG, "%s; %s: Listen socket: "
982 "Connection attempt from/to multicast "
983 "address ignored\n", s, __func__);
988 if (th->th_dport == th->th_sport &&
989 ip->ip_dst.s_addr == ip->ip_src.s_addr) {
990 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
991 log(LOG_DEBUG, "%s; %s: Listen socket: "
992 "Connection attempt from/to self "
993 "ignored\n", s, __func__);
996 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
997 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
998 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
999 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1000 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1001 log(LOG_DEBUG, "%s; %s: Listen socket: "
1002 "Connection attempt from/to broad- "
1003 "or multicast address ignored\n",
1009 * SYN appears to be valid. Create compressed TCP state
1013 if (so->so_options & SO_DEBUG)
1014 tcp_trace(TA_INPUT, ostate, tp,
1015 (void *)tcp_saveipgen, &tcp_savetcp, 0);
1017 tcp_dooptions(&to, optp, optlen, TO_SYN);
1018 syncache_add(&inc, &to, th, inp, &so, m);
1020 * Entry added to syncache and mbuf consumed.
1021 * Everything already unlocked by syncache_add().
1023 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1028 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
1029 * state. tcp_do_segment() always consumes the mbuf chain, unlocks
1030 * the inpcb, and unlocks pcbinfo.
1032 tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked);
1033 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1037 if (ti_locked == TI_RLOCKED)
1038 INP_INFO_RUNLOCK(&V_tcbinfo);
1039 else if (ti_locked == TI_WLOCKED)
1040 INP_INFO_WUNLOCK(&V_tcbinfo);
1042 panic("%s: dropwithreset ti_locked %d", __func__, ti_locked);
1043 ti_locked = TI_UNLOCKED;
1046 tcp_dropwithreset(m, th, tp, tlen, rstreason);
1049 tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1050 m = NULL; /* mbuf chain got consumed. */
1054 if (ti_locked == TI_RLOCKED)
1055 INP_INFO_RUNLOCK(&V_tcbinfo);
1056 else if (ti_locked == TI_WLOCKED)
1057 INP_INFO_WUNLOCK(&V_tcbinfo);
1059 panic("%s: dropunlock ti_locked %d", __func__, ti_locked);
1060 ti_locked = TI_UNLOCKED;
1066 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1074 tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
1075 struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos,
1078 int thflags, acked, ourfinisacked, needoutput = 0;
1079 int rstreason, todrop, win;
1085 * The size of tcp_saveipgen must be the size of the max ip header,
1088 u_char tcp_saveipgen[IP6_HDR_LEN];
1089 struct tcphdr tcp_savetcp;
1092 thflags = th->th_flags;
1095 * If this is either a state-changing packet or current state isn't
1096 * established, we require a write lock on tcbinfo. Otherwise, we
1097 * allow either a read lock or a write lock, as we may have acquired
1098 * a write lock due to a race.
1100 * Require a global write lock for SYN/FIN/RST segments or
1101 * non-established connections; otherwise accept either a read or
1102 * write lock, as we may have conservatively acquired a write lock in
1103 * certain cases in tcp_input() (is this still true?). Currently we
1104 * will never enter with no lock, so we try to drop it quickly in the
1105 * common pure ack/pure data cases.
1107 if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
1108 tp->t_state != TCPS_ESTABLISHED) {
1109 KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for "
1110 "SYN/FIN/RST/!EST", __func__, ti_locked));
1111 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1114 if (ti_locked == TI_RLOCKED)
1115 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1116 else if (ti_locked == TI_WLOCKED)
1117 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1119 panic("%s: ti_locked %d for EST", __func__,
1123 INP_WLOCK_ASSERT(tp->t_inpcb);
1124 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1126 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1130 * Segment received on connection.
1131 * Reset idle time and keep-alive timer.
1132 * XXX: This should be done after segment
1133 * validation to ignore broken/spoofed segs.
1135 tp->t_rcvtime = ticks;
1136 if (TCPS_HAVEESTABLISHED(tp->t_state))
1137 tcp_timer_activate(tp, TT_KEEP, tcp_keepidle);
1140 * Unscale the window into a 32-bit value.
1141 * For the SYN_SENT state the scale is zero.
1143 tiwin = th->th_win << tp->snd_scale;
1146 * TCP ECN processing.
1148 if (tp->t_flags & TF_ECN_PERMIT) {
1149 if (thflags & TH_CWR)
1150 tp->t_flags &= ~TF_ECN_SND_ECE;
1151 switch (iptos & IPTOS_ECN_MASK) {
1153 tp->t_flags |= TF_ECN_SND_ECE;
1154 TCPSTAT_INC(tcps_ecn_ce);
1156 case IPTOS_ECN_ECT0:
1157 TCPSTAT_INC(tcps_ecn_ect0);
1159 case IPTOS_ECN_ECT1:
1160 TCPSTAT_INC(tcps_ecn_ect1);
1164 * Congestion experienced.
1165 * Ignore if we are already trying to recover.
1167 if ((thflags & TH_ECE) &&
1168 SEQ_LEQ(th->th_ack, tp->snd_recover)) {
1169 TCPSTAT_INC(tcps_ecn_rcwnd);
1170 tcp_congestion_exp(tp);
1175 * Parse options on any incoming segment.
1177 tcp_dooptions(&to, (u_char *)(th + 1),
1178 (th->th_off << 2) - sizeof(struct tcphdr),
1179 (thflags & TH_SYN) ? TO_SYN : 0);
1182 * If echoed timestamp is later than the current time,
1183 * fall back to non RFC1323 RTT calculation. Normalize
1184 * timestamp if syncookies were used when this connection
1187 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1188 to.to_tsecr -= tp->ts_offset;
1189 if (TSTMP_GT(to.to_tsecr, ticks))
1194 * Process options only when we get SYN/ACK back. The SYN case
1195 * for incoming connections is handled in tcp_syncache.
1196 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
1197 * or <SYN,ACK>) segment itself is never scaled.
1198 * XXX this is traditional behavior, may need to be cleaned up.
1200 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1201 if ((to.to_flags & TOF_SCALE) &&
1202 (tp->t_flags & TF_REQ_SCALE)) {
1203 tp->t_flags |= TF_RCVD_SCALE;
1204 tp->snd_scale = to.to_wscale;
1207 * Initial send window. It will be updated with
1208 * the next incoming segment to the scaled value.
1210 tp->snd_wnd = th->th_win;
1211 if (to.to_flags & TOF_TS) {
1212 tp->t_flags |= TF_RCVD_TSTMP;
1213 tp->ts_recent = to.to_tsval;
1214 tp->ts_recent_age = ticks;
1216 if (to.to_flags & TOF_MSS)
1217 tcp_mss(tp, to.to_mss);
1218 if ((tp->t_flags & TF_SACK_PERMIT) &&
1219 (to.to_flags & TOF_SACKPERM) == 0)
1220 tp->t_flags &= ~TF_SACK_PERMIT;
1224 * Header prediction: check for the two common cases
1225 * of a uni-directional data xfer. If the packet has
1226 * no control flags, is in-sequence, the window didn't
1227 * change and we're not retransmitting, it's a
1228 * candidate. If the length is zero and the ack moved
1229 * forward, we're the sender side of the xfer. Just
1230 * free the data acked & wake any higher level process
1231 * that was blocked waiting for space. If the length
1232 * is non-zero and the ack didn't move, we're the
1233 * receiver side. If we're getting packets in-order
1234 * (the reassembly queue is empty), add the data to
1235 * the socket buffer and note that we need a delayed ack.
1236 * Make sure that the hidden state-flags are also off.
1237 * Since we check for TCPS_ESTABLISHED first, it can only
1240 if (tp->t_state == TCPS_ESTABLISHED &&
1241 th->th_seq == tp->rcv_nxt &&
1242 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1243 tp->snd_nxt == tp->snd_max &&
1244 tiwin && tiwin == tp->snd_wnd &&
1245 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1246 LIST_EMPTY(&tp->t_segq) &&
1247 ((to.to_flags & TOF_TS) == 0 ||
1248 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1251 * If last ACK falls within this segment's sequence numbers,
1252 * record the timestamp.
1253 * NOTE that the test is modified according to the latest
1254 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1256 if ((to.to_flags & TOF_TS) != 0 &&
1257 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1258 tp->ts_recent_age = ticks;
1259 tp->ts_recent = to.to_tsval;
1263 if (SEQ_GT(th->th_ack, tp->snd_una) &&
1264 SEQ_LEQ(th->th_ack, tp->snd_max) &&
1265 tp->snd_cwnd >= tp->snd_wnd &&
1266 ((!V_tcp_do_newreno &&
1267 !(tp->t_flags & TF_SACK_PERMIT) &&
1268 tp->t_dupacks < tcprexmtthresh) ||
1269 ((V_tcp_do_newreno ||
1270 (tp->t_flags & TF_SACK_PERMIT)) &&
1271 !IN_FASTRECOVERY(tp) &&
1272 (to.to_flags & TOF_SACK) == 0 &&
1273 TAILQ_EMPTY(&tp->snd_holes)))) {
1275 * This is a pure ack for outstanding data.
1277 if (ti_locked == TI_RLOCKED)
1278 INP_INFO_RUNLOCK(&V_tcbinfo);
1279 else if (ti_locked == TI_WLOCKED)
1280 INP_INFO_WUNLOCK(&V_tcbinfo);
1282 panic("%s: ti_locked %d on pure ACK",
1283 __func__, ti_locked);
1284 ti_locked = TI_UNLOCKED;
1286 TCPSTAT_INC(tcps_predack);
1289 * "bad retransmit" recovery.
1291 if (tp->t_rxtshift == 1 &&
1292 (int)(ticks - tp->t_badrxtwin) < 0) {
1293 TCPSTAT_INC(tcps_sndrexmitbad);
1294 tp->snd_cwnd = tp->snd_cwnd_prev;
1296 tp->snd_ssthresh_prev;
1297 tp->snd_recover = tp->snd_recover_prev;
1298 if (tp->t_flags & TF_WASFRECOVERY)
1299 ENTER_FASTRECOVERY(tp);
1300 tp->snd_nxt = tp->snd_max;
1301 tp->t_badrxtwin = 0;
1305 * Recalculate the transmit timer / rtt.
1307 * Some boxes send broken timestamp replies
1308 * during the SYN+ACK phase, ignore
1309 * timestamps of 0 or we could calculate a
1310 * huge RTT and blow up the retransmit timer.
1312 if ((to.to_flags & TOF_TS) != 0 &&
1314 if (!tp->t_rttlow ||
1315 tp->t_rttlow > ticks - to.to_tsecr)
1316 tp->t_rttlow = ticks - to.to_tsecr;
1318 ticks - to.to_tsecr + 1);
1319 } else if (tp->t_rtttime &&
1320 SEQ_GT(th->th_ack, tp->t_rtseq)) {
1321 if (!tp->t_rttlow ||
1322 tp->t_rttlow > ticks - tp->t_rtttime)
1323 tp->t_rttlow = ticks - tp->t_rtttime;
1325 ticks - tp->t_rtttime);
1327 tcp_xmit_bandwidth_limit(tp, th->th_ack);
1328 acked = th->th_ack - tp->snd_una;
1329 TCPSTAT_INC(tcps_rcvackpack);
1330 TCPSTAT_ADD(tcps_rcvackbyte, acked);
1331 sbdrop(&so->so_snd, acked);
1332 if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
1333 SEQ_LEQ(th->th_ack, tp->snd_recover))
1334 tp->snd_recover = th->th_ack - 1;
1335 tp->snd_una = th->th_ack;
1337 * Pull snd_wl2 up to prevent seq wrap relative
1340 tp->snd_wl2 = th->th_ack;
1343 ND6_HINT(tp); /* Some progress has been made. */
1346 * If all outstanding data are acked, stop
1347 * retransmit timer, otherwise restart timer
1348 * using current (possibly backed-off) value.
1349 * If process is waiting for space,
1350 * wakeup/selwakeup/signal. If data
1351 * are ready to send, let tcp_output
1352 * decide between more output or persist.
1355 if (so->so_options & SO_DEBUG)
1356 tcp_trace(TA_INPUT, ostate, tp,
1357 (void *)tcp_saveipgen,
1360 if (tp->snd_una == tp->snd_max)
1361 tcp_timer_activate(tp, TT_REXMT, 0);
1362 else if (!tcp_timer_active(tp, TT_PERSIST))
1363 tcp_timer_activate(tp, TT_REXMT,
1366 if (so->so_snd.sb_cc)
1367 (void) tcp_output(tp);
1370 } else if (th->th_ack == tp->snd_una &&
1371 tlen <= sbspace(&so->so_rcv)) {
1372 int newsize = 0; /* automatic sockbuf scaling */
1375 * This is a pure, in-sequence data packet with
1376 * nothing on the reassembly queue and we have enough
1377 * buffer space to take it.
1379 if (ti_locked == TI_RLOCKED)
1380 INP_INFO_RUNLOCK(&V_tcbinfo);
1381 else if (ti_locked == TI_WLOCKED)
1382 INP_INFO_WUNLOCK(&V_tcbinfo);
1384 panic("%s: ti_locked %d on pure data "
1385 "segment", __func__, ti_locked);
1386 ti_locked = TI_UNLOCKED;
1388 /* Clean receiver SACK report if present */
1389 if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
1390 tcp_clean_sackreport(tp);
1391 TCPSTAT_INC(tcps_preddat);
1392 tp->rcv_nxt += tlen;
1394 * Pull snd_wl1 up to prevent seq wrap relative to
1397 tp->snd_wl1 = th->th_seq;
1399 * Pull rcv_up up to prevent seq wrap relative to
1402 tp->rcv_up = tp->rcv_nxt;
1403 TCPSTAT_INC(tcps_rcvpack);
1404 TCPSTAT_ADD(tcps_rcvbyte, tlen);
1405 ND6_HINT(tp); /* Some progress has been made */
1407 if (so->so_options & SO_DEBUG)
1408 tcp_trace(TA_INPUT, ostate, tp,
1409 (void *)tcp_saveipgen, &tcp_savetcp, 0);
1412 * Automatic sizing of receive socket buffer. Often the send
1413 * buffer size is not optimally adjusted to the actual network
1414 * conditions at hand (delay bandwidth product). Setting the
1415 * buffer size too small limits throughput on links with high
1416 * bandwidth and high delay (eg. trans-continental/oceanic links).
1418 * On the receive side the socket buffer memory is only rarely
1419 * used to any significant extent. This allows us to be much
1420 * more aggressive in scaling the receive socket buffer. For
1421 * the case that the buffer space is actually used to a large
1422 * extent and we run out of kernel memory we can simply drop
1423 * the new segments; TCP on the sender will just retransmit it
1424 * later. Setting the buffer size too big may only consume too
1425 * much kernel memory if the application doesn't read() from
1426 * the socket or packet loss or reordering makes use of the
1429 * The criteria to step up the receive buffer one notch are:
1430 * 1. the number of bytes received during the time it takes
1431 * one timestamp to be reflected back to us (the RTT);
1432 * 2. received bytes per RTT is within seven eighth of the
1433 * current socket buffer size;
1434 * 3. receive buffer size has not hit maximal automatic size;
1436 * This algorithm does one step per RTT at most and only if
1437 * we receive a bulk stream w/o packet losses or reorderings.
1438 * Shrinking the buffer during idle times is not necessary as
1439 * it doesn't consume any memory when idle.
1441 * TODO: Only step up if the application is actually serving
1442 * the buffer to better manage the socket buffer resources.
1444 if (V_tcp_do_autorcvbuf &&
1446 (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
1447 if (to.to_tsecr > tp->rfbuf_ts &&
1448 to.to_tsecr - tp->rfbuf_ts < hz) {
1450 (so->so_rcv.sb_hiwat / 8 * 7) &&
1451 so->so_rcv.sb_hiwat <
1452 V_tcp_autorcvbuf_max) {
1454 min(so->so_rcv.sb_hiwat +
1455 V_tcp_autorcvbuf_inc,
1456 V_tcp_autorcvbuf_max);
1458 /* Start over with next RTT. */
1462 tp->rfbuf_cnt += tlen; /* add up */
1465 /* Add data to socket buffer. */
1466 SOCKBUF_LOCK(&so->so_rcv);
1467 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1471 * Set new socket buffer size.
1472 * Give up when limit is reached.
1475 if (!sbreserve_locked(&so->so_rcv,
1477 so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1478 m_adj(m, drop_hdrlen); /* delayed header drop */
1479 sbappendstream_locked(&so->so_rcv, m);
1481 /* NB: sorwakeup_locked() does an implicit unlock. */
1482 sorwakeup_locked(so);
1483 if (DELAY_ACK(tp)) {
1484 tp->t_flags |= TF_DELACK;
1486 tp->t_flags |= TF_ACKNOW;
1494 * Calculate amount of space in receive window,
1495 * and then do TCP input processing.
1496 * Receive window is amount of space in rcv queue,
1497 * but not less than advertised window.
1499 win = sbspace(&so->so_rcv);
1502 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1504 /* Reset receive buffer auto scaling when not in bulk receive mode. */
1508 switch (tp->t_state) {
1511 * If the state is SYN_RECEIVED:
1512 * if seg contains an ACK, but not for our SYN/ACK, send a RST.
1514 case TCPS_SYN_RECEIVED:
1515 if ((thflags & TH_ACK) &&
1516 (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1517 SEQ_GT(th->th_ack, tp->snd_max))) {
1518 rstreason = BANDLIM_RST_OPENPORT;
1524 * If the state is SYN_SENT:
1525 * if seg contains an ACK, but not for our SYN, drop the input.
1526 * if seg contains a RST, then drop the connection.
1527 * if seg does not contain SYN, then drop it.
1528 * Otherwise this is an acceptable SYN segment
1529 * initialize tp->rcv_nxt and tp->irs
1530 * if seg contains ack then advance tp->snd_una
1531 * if seg contains an ECE and ECN support is enabled, the stream
1533 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1534 * arrange for segment to be acked (eventually)
1535 * continue processing rest of data/controls, beginning with URG
1538 if ((thflags & TH_ACK) &&
1539 (SEQ_LEQ(th->th_ack, tp->iss) ||
1540 SEQ_GT(th->th_ack, tp->snd_max))) {
1541 rstreason = BANDLIM_UNLIMITED;
1544 if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST))
1545 tp = tcp_drop(tp, ECONNREFUSED);
1546 if (thflags & TH_RST)
1548 if (!(thflags & TH_SYN))
1551 tp->irs = th->th_seq;
1553 if (thflags & TH_ACK) {
1554 TCPSTAT_INC(tcps_connects);
1557 mac_socketpeer_set_from_mbuf(m, so);
1559 /* Do window scaling on this connection? */
1560 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1561 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1562 tp->rcv_scale = tp->request_r_scale;
1564 tp->rcv_adv += tp->rcv_wnd;
1565 tp->snd_una++; /* SYN is acked */
1567 * If there's data, delay ACK; if there's also a FIN
1568 * ACKNOW will be turned on later.
1570 if (DELAY_ACK(tp) && tlen != 0)
1571 tcp_timer_activate(tp, TT_DELACK,
1574 tp->t_flags |= TF_ACKNOW;
1576 if ((thflags & TH_ECE) && V_tcp_do_ecn) {
1577 tp->t_flags |= TF_ECN_PERMIT;
1578 TCPSTAT_INC(tcps_ecn_shs);
1582 * Received <SYN,ACK> in SYN_SENT[*] state.
1584 * SYN_SENT --> ESTABLISHED
1585 * SYN_SENT* --> FIN_WAIT_1
1587 tp->t_starttime = ticks;
1588 if (tp->t_flags & TF_NEEDFIN) {
1589 tp->t_state = TCPS_FIN_WAIT_1;
1590 tp->t_flags &= ~TF_NEEDFIN;
1593 tp->t_state = TCPS_ESTABLISHED;
1594 tcp_timer_activate(tp, TT_KEEP, tcp_keepidle);
1598 * Received initial SYN in SYN-SENT[*] state =>
1599 * simultaneous open. If segment contains CC option
1600 * and there is a cached CC, apply TAO test.
1601 * If it succeeds, connection is * half-synchronized.
1602 * Otherwise, do 3-way handshake:
1603 * SYN-SENT -> SYN-RECEIVED
1604 * SYN-SENT* -> SYN-RECEIVED*
1605 * If there was no CC option, clear cached CC value.
1607 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
1608 tcp_timer_activate(tp, TT_REXMT, 0);
1609 tp->t_state = TCPS_SYN_RECEIVED;
1612 KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: "
1613 "ti_locked %d", __func__, ti_locked));
1614 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1615 INP_WLOCK_ASSERT(tp->t_inpcb);
1618 * Advance th->th_seq to correspond to first data byte.
1619 * If data, trim to stay within window,
1620 * dropping FIN if necessary.
1623 if (tlen > tp->rcv_wnd) {
1624 todrop = tlen - tp->rcv_wnd;
1628 TCPSTAT_INC(tcps_rcvpackafterwin);
1629 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
1631 tp->snd_wl1 = th->th_seq - 1;
1632 tp->rcv_up = th->th_seq;
1634 * Client side of transaction: already sent SYN and data.
1635 * If the remote host used T/TCP to validate the SYN,
1636 * our data will be ACK'd; if so, enter normal data segment
1637 * processing in the middle of step 5, ack processing.
1638 * Otherwise, goto step 6.
1640 if (thflags & TH_ACK)
1646 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1647 * do normal processing.
1649 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later.
1653 break; /* continue normal processing */
1657 * States other than LISTEN or SYN_SENT.
1658 * First check the RST flag and sequence number since reset segments
1659 * are exempt from the timestamp and connection count tests. This
1660 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
1661 * below which allowed reset segments in half the sequence space
1662 * to fall though and be processed (which gives forged reset
1663 * segments with a random sequence number a 50 percent chance of
1664 * killing a connection).
1665 * Then check timestamp, if present.
1666 * Then check the connection count, if present.
1667 * Then check that at least some bytes of segment are within
1668 * receive window. If segment begins before rcv_nxt,
1669 * drop leading data (and SYN); if nothing left, just ack.
1672 * If the RST bit is set, check the sequence number to see
1673 * if this is a valid reset segment.
1675 * In all states except SYN-SENT, all reset (RST) segments
1676 * are validated by checking their SEQ-fields. A reset is
1677 * valid if its sequence number is in the window.
1678 * Note: this does not take into account delayed ACKs, so
1679 * we should test against last_ack_sent instead of rcv_nxt.
1680 * The sequence number in the reset segment is normally an
1681 * echo of our outgoing acknowlegement numbers, but some hosts
1682 * send a reset with the sequence number at the rightmost edge
1683 * of our receive window, and we have to handle this case.
1684 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
1685 * that brute force RST attacks are possible. To combat this,
1686 * we use a much stricter check while in the ESTABLISHED state,
1687 * only accepting RSTs where the sequence number is equal to
1688 * last_ack_sent. In all other states (the states in which a
1689 * RST is more likely), the more permissive check is used.
1690 * If we have multiple segments in flight, the initial reset
1691 * segment sequence numbers will be to the left of last_ack_sent,
1692 * but they will eventually catch up.
1693 * In any case, it never made sense to trim reset segments to
1694 * fit the receive window since RFC 1122 says:
1695 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
1697 * A TCP SHOULD allow a received RST segment to include data.
1700 * It has been suggested that a RST segment could contain
1701 * ASCII text that encoded and explained the cause of the
1702 * RST. No standard has yet been established for such
1705 * If the reset segment passes the sequence number test examine
1707 * SYN_RECEIVED STATE:
1708 * If passive open, return to LISTEN state.
1709 * If active open, inform user that connection was refused.
1710 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
1711 * Inform user that connection was reset, and close tcb.
1712 * CLOSING, LAST_ACK STATES:
1715 * Drop the segment - see Stevens, vol. 2, p. 964 and
1718 if (thflags & TH_RST) {
1719 if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) &&
1720 SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
1721 switch (tp->t_state) {
1723 case TCPS_SYN_RECEIVED:
1724 so->so_error = ECONNREFUSED;
1727 case TCPS_ESTABLISHED:
1728 if (V_tcp_insecure_rst == 0 &&
1729 !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) &&
1730 SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) &&
1731 !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) &&
1732 SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) {
1733 TCPSTAT_INC(tcps_badrst);
1737 case TCPS_FIN_WAIT_1:
1738 case TCPS_FIN_WAIT_2:
1739 case TCPS_CLOSE_WAIT:
1740 so->so_error = ECONNRESET;
1742 KASSERT(ti_locked == TI_WLOCKED,
1743 ("tcp_do_segment: TH_RST 1 ti_locked %d",
1745 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1747 tp->t_state = TCPS_CLOSED;
1748 TCPSTAT_INC(tcps_drops);
1754 KASSERT(ti_locked == TI_WLOCKED,
1755 ("tcp_do_segment: TH_RST 2 ti_locked %d",
1757 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1767 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1768 * and it's less than ts_recent, drop it.
1770 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
1771 TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1773 /* Check to see if ts_recent is over 24 days old. */
1774 if (ticks - tp->ts_recent_age > TCP_PAWS_IDLE) {
1776 * Invalidate ts_recent. If this segment updates
1777 * ts_recent, the age will be reset later and ts_recent
1778 * will get a valid value. If it does not, setting
1779 * ts_recent to zero will at least satisfy the
1780 * requirement that zero be placed in the timestamp
1781 * echo reply when ts_recent isn't valid. The
1782 * age isn't reset until we get a valid ts_recent
1783 * because we don't want out-of-order segments to be
1784 * dropped when ts_recent is old.
1788 TCPSTAT_INC(tcps_rcvduppack);
1789 TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
1790 TCPSTAT_INC(tcps_pawsdrop);
1798 * In the SYN-RECEIVED state, validate that the packet belongs to
1799 * this connection before trimming the data to fit the receive
1800 * window. Check the sequence number versus IRS since we know
1801 * the sequence numbers haven't wrapped. This is a partial fix
1802 * for the "LAND" DoS attack.
1804 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1805 rstreason = BANDLIM_RST_OPENPORT;
1809 todrop = tp->rcv_nxt - th->th_seq;
1812 * If this is a duplicate SYN for our current connection,
1813 * advance over it and pretend and it's not a SYN.
1815 if (thflags & TH_SYN && th->th_seq == tp->irs) {
1825 * Following if statement from Stevens, vol. 2, p. 960.
1828 || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1830 * Any valid FIN must be to the left of the window.
1831 * At this point the FIN must be a duplicate or out
1832 * of sequence; drop it.
1837 * Send an ACK to resynchronize and drop any data.
1838 * But keep on processing for RST or ACK.
1840 tp->t_flags |= TF_ACKNOW;
1842 TCPSTAT_INC(tcps_rcvduppack);
1843 TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
1845 TCPSTAT_INC(tcps_rcvpartduppack);
1846 TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
1848 drop_hdrlen += todrop; /* drop from the top afterwards */
1849 th->th_seq += todrop;
1851 if (th->th_urp > todrop)
1852 th->th_urp -= todrop;
1860 * If new data are received on a connection after the
1861 * user processes are gone, then RST the other end.
1863 if ((so->so_state & SS_NOFDREF) &&
1864 tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1867 KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && "
1868 "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked));
1869 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1871 if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) {
1872 log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket "
1873 "was closed, sending RST and removing tcpcb\n",
1874 s, __func__, tcpstates[tp->t_state], tlen);
1878 TCPSTAT_INC(tcps_rcvafterclose);
1879 rstreason = BANDLIM_UNLIMITED;
1884 * If segment ends after window, drop trailing data
1885 * (and PUSH and FIN); if nothing left, just ACK.
1887 todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
1889 TCPSTAT_INC(tcps_rcvpackafterwin);
1890 if (todrop >= tlen) {
1891 TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
1893 * If window is closed can only take segments at
1894 * window edge, and have to drop data and PUSH from
1895 * incoming segments. Continue processing, but
1896 * remember to ack. Otherwise, drop segment
1899 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1900 tp->t_flags |= TF_ACKNOW;
1901 TCPSTAT_INC(tcps_rcvwinprobe);
1905 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
1908 thflags &= ~(TH_PUSH|TH_FIN);
1912 * If last ACK falls within this segment's sequence numbers,
1913 * record its timestamp.
1915 * 1) That the test incorporates suggestions from the latest
1916 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1917 * 2) That updating only on newer timestamps interferes with
1918 * our earlier PAWS tests, so this check should be solely
1919 * predicated on the sequence space of this segment.
1920 * 3) That we modify the segment boundary check to be
1921 * Last.ACK.Sent <= SEG.SEQ + SEG.Len
1922 * instead of RFC1323's
1923 * Last.ACK.Sent < SEG.SEQ + SEG.Len,
1924 * This modified check allows us to overcome RFC1323's
1925 * limitations as described in Stevens TCP/IP Illustrated
1926 * Vol. 2 p.869. In such cases, we can still calculate the
1927 * RTT correctly when RCV.NXT == Last.ACK.Sent.
1929 if ((to.to_flags & TOF_TS) != 0 &&
1930 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
1931 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
1932 ((thflags & (TH_SYN|TH_FIN)) != 0))) {
1933 tp->ts_recent_age = ticks;
1934 tp->ts_recent = to.to_tsval;
1938 * If a SYN is in the window, then this is an
1939 * error and we send an RST and drop the connection.
1941 if (thflags & TH_SYN) {
1942 KASSERT(ti_locked == TI_WLOCKED,
1943 ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked));
1944 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1946 tp = tcp_drop(tp, ECONNRESET);
1947 rstreason = BANDLIM_UNLIMITED;
1952 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
1953 * flag is on (half-synchronized state), then queue data for
1954 * later processing; else drop segment and return.
1956 if ((thflags & TH_ACK) == 0) {
1957 if (tp->t_state == TCPS_SYN_RECEIVED ||
1958 (tp->t_flags & TF_NEEDSYN))
1960 else if (tp->t_flags & TF_ACKNOW)
1969 switch (tp->t_state) {
1972 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1973 * ESTABLISHED state and continue processing.
1974 * The ACK was checked above.
1976 case TCPS_SYN_RECEIVED:
1978 TCPSTAT_INC(tcps_connects);
1980 /* Do window scaling? */
1981 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1982 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1983 tp->rcv_scale = tp->request_r_scale;
1984 tp->snd_wnd = tiwin;
1988 * SYN-RECEIVED -> ESTABLISHED
1989 * SYN-RECEIVED* -> FIN-WAIT-1
1991 tp->t_starttime = ticks;
1992 if (tp->t_flags & TF_NEEDFIN) {
1993 tp->t_state = TCPS_FIN_WAIT_1;
1994 tp->t_flags &= ~TF_NEEDFIN;
1996 tp->t_state = TCPS_ESTABLISHED;
1997 tcp_timer_activate(tp, TT_KEEP, tcp_keepidle);
2000 * If segment contains data or ACK, will call tcp_reass()
2001 * later; if not, do so now to pass queued data to user.
2003 if (tlen == 0 && (thflags & TH_FIN) == 0)
2004 (void) tcp_reass(tp, (struct tcphdr *)0, 0,
2006 tp->snd_wl1 = th->th_seq - 1;
2010 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2011 * ACKs. If the ack is in the range
2012 * tp->snd_una < th->th_ack <= tp->snd_max
2013 * then advance tp->snd_una to th->th_ack and drop
2014 * data from the retransmission queue. If this ACK reflects
2015 * more up to date window information we update our window information.
2017 case TCPS_ESTABLISHED:
2018 case TCPS_FIN_WAIT_1:
2019 case TCPS_FIN_WAIT_2:
2020 case TCPS_CLOSE_WAIT:
2023 if (SEQ_GT(th->th_ack, tp->snd_max)) {
2024 TCPSTAT_INC(tcps_rcvacktoomuch);
2027 if ((tp->t_flags & TF_SACK_PERMIT) &&
2028 ((to.to_flags & TOF_SACK) ||
2029 !TAILQ_EMPTY(&tp->snd_holes)))
2030 tcp_sack_doack(tp, &to, th->th_ack);
2031 if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
2032 if (tlen == 0 && tiwin == tp->snd_wnd) {
2033 TCPSTAT_INC(tcps_rcvdupack);
2035 * If we have outstanding data (other than
2036 * a window probe), this is a completely
2037 * duplicate ack (ie, window info didn't
2038 * change), the ack is the biggest we've
2039 * seen and we've seen exactly our rexmt
2040 * threshhold of them, assume a packet
2041 * has been dropped and retransmit it.
2042 * Kludge snd_nxt & the congestion
2043 * window so we send only this one
2046 * We know we're losing at the current
2047 * window size so do congestion avoidance
2048 * (set ssthresh to half the current window
2049 * and pull our congestion window back to
2050 * the new ssthresh).
2052 * Dup acks mean that packets have left the
2053 * network (they're now cached at the receiver)
2054 * so bump cwnd by the amount in the receiver
2055 * to keep a constant cwnd packets in the
2058 * When using TCP ECN, notify the peer that
2059 * we reduced the cwnd.
2061 if (!tcp_timer_active(tp, TT_REXMT) ||
2062 th->th_ack != tp->snd_una)
2064 else if (++tp->t_dupacks > tcprexmtthresh ||
2065 ((V_tcp_do_newreno ||
2066 (tp->t_flags & TF_SACK_PERMIT)) &&
2067 IN_FASTRECOVERY(tp))) {
2068 if ((tp->t_flags & TF_SACK_PERMIT) &&
2069 IN_FASTRECOVERY(tp)) {
2073 * Compute the amount of data in flight first.
2074 * We can inject new data into the pipe iff
2075 * we have less than 1/2 the original window's
2076 * worth of data in flight.
2078 awnd = (tp->snd_nxt - tp->snd_fack) +
2079 tp->sackhint.sack_bytes_rexmit;
2080 if (awnd < tp->snd_ssthresh) {
2081 tp->snd_cwnd += tp->t_maxseg;
2082 if (tp->snd_cwnd > tp->snd_ssthresh)
2083 tp->snd_cwnd = tp->snd_ssthresh;
2086 tp->snd_cwnd += tp->t_maxseg;
2087 (void) tcp_output(tp);
2089 } else if (tp->t_dupacks == tcprexmtthresh) {
2090 tcp_seq onxt = tp->snd_nxt;
2093 * If we're doing sack, check to
2094 * see if we're already in sack
2095 * recovery. If we're not doing sack,
2096 * check to see if we're in newreno
2099 if (tp->t_flags & TF_SACK_PERMIT) {
2100 if (IN_FASTRECOVERY(tp)) {
2104 } else if (V_tcp_do_newreno ||
2106 if (SEQ_LEQ(th->th_ack,
2112 tcp_congestion_exp(tp);
2113 tcp_timer_activate(tp, TT_REXMT, 0);
2115 if (tp->t_flags & TF_SACK_PERMIT) {
2117 tcps_sack_recovery_episode);
2118 tp->sack_newdata = tp->snd_nxt;
2119 tp->snd_cwnd = tp->t_maxseg;
2120 (void) tcp_output(tp);
2123 tp->snd_nxt = th->th_ack;
2124 tp->snd_cwnd = tp->t_maxseg;
2125 (void) tcp_output(tp);
2126 KASSERT(tp->snd_limited <= 2,
2127 ("%s: tp->snd_limited too big",
2129 tp->snd_cwnd = tp->snd_ssthresh +
2131 (tp->t_dupacks - tp->snd_limited);
2132 if (SEQ_GT(onxt, tp->snd_nxt))
2135 } else if (V_tcp_do_rfc3042) {
2136 u_long oldcwnd = tp->snd_cwnd;
2137 tcp_seq oldsndmax = tp->snd_max;
2140 KASSERT(tp->t_dupacks == 1 ||
2142 ("%s: dupacks not 1 or 2",
2144 if (tp->t_dupacks == 1)
2145 tp->snd_limited = 0;
2147 (tp->snd_nxt - tp->snd_una) +
2148 (tp->t_dupacks - tp->snd_limited) *
2150 (void) tcp_output(tp);
2151 sent = tp->snd_max - oldsndmax;
2152 if (sent > tp->t_maxseg) {
2153 KASSERT((tp->t_dupacks == 2 &&
2154 tp->snd_limited == 0) ||
2155 (sent == tp->t_maxseg + 1 &&
2156 tp->t_flags & TF_SENTFIN),
2157 ("%s: sent too much",
2159 tp->snd_limited = 2;
2160 } else if (sent > 0)
2162 tp->snd_cwnd = oldcwnd;
2170 KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2171 ("%s: th_ack <= snd_una", __func__));
2174 * If the congestion window was inflated to account
2175 * for the other side's cached packets, retract it.
2177 if (V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) {
2178 if (IN_FASTRECOVERY(tp)) {
2179 if (SEQ_LT(th->th_ack, tp->snd_recover)) {
2180 if (tp->t_flags & TF_SACK_PERMIT)
2181 tcp_sack_partialack(tp, th);
2183 tcp_newreno_partial_ack(tp, th);
2186 * Out of fast recovery.
2187 * Window inflation should have left us
2188 * with approximately snd_ssthresh
2190 * But in case we would be inclined to
2191 * send a burst, better to do it via
2192 * the slow start mechanism.
2194 if (SEQ_GT(th->th_ack +
2197 tp->snd_cwnd = tp->snd_max -
2201 tp->snd_cwnd = tp->snd_ssthresh;
2205 if (tp->t_dupacks >= tcprexmtthresh &&
2206 tp->snd_cwnd > tp->snd_ssthresh)
2207 tp->snd_cwnd = tp->snd_ssthresh;
2211 * If we reach this point, ACK is not a duplicate,
2212 * i.e., it ACKs something we sent.
2214 if (tp->t_flags & TF_NEEDSYN) {
2216 * T/TCP: Connection was half-synchronized, and our
2217 * SYN has been ACK'd (so connection is now fully
2218 * synchronized). Go to non-starred state,
2219 * increment snd_una for ACK of SYN, and check if
2220 * we can do window scaling.
2222 tp->t_flags &= ~TF_NEEDSYN;
2224 /* Do window scaling? */
2225 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2226 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
2227 tp->rcv_scale = tp->request_r_scale;
2228 /* Send window already scaled. */
2233 INP_INFO_LOCK_ASSERT(&V_tcbinfo);
2234 KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED,
2235 ("tcp_input: process_ACK ti_locked %d", ti_locked));
2236 INP_WLOCK_ASSERT(tp->t_inpcb);
2238 acked = th->th_ack - tp->snd_una;
2239 TCPSTAT_INC(tcps_rcvackpack);
2240 TCPSTAT_ADD(tcps_rcvackbyte, acked);
2243 * If we just performed our first retransmit, and the ACK
2244 * arrives within our recovery window, then it was a mistake
2245 * to do the retransmit in the first place. Recover our
2246 * original cwnd and ssthresh, and proceed to transmit where
2249 if (tp->t_rxtshift == 1 && (int)(ticks - tp->t_badrxtwin) < 0) {
2250 TCPSTAT_INC(tcps_sndrexmitbad);
2251 tp->snd_cwnd = tp->snd_cwnd_prev;
2252 tp->snd_ssthresh = tp->snd_ssthresh_prev;
2253 tp->snd_recover = tp->snd_recover_prev;
2254 if (tp->t_flags & TF_WASFRECOVERY)
2255 ENTER_FASTRECOVERY(tp);
2256 tp->snd_nxt = tp->snd_max;
2257 tp->t_badrxtwin = 0; /* XXX probably not required */
2261 * If we have a timestamp reply, update smoothed
2262 * round trip time. If no timestamp is present but
2263 * transmit timer is running and timed sequence
2264 * number was acked, update smoothed round trip time.
2265 * Since we now have an rtt measurement, cancel the
2266 * timer backoff (cf., Phil Karn's retransmit alg.).
2267 * Recompute the initial retransmit timer.
2269 * Some boxes send broken timestamp replies
2270 * during the SYN+ACK phase, ignore
2271 * timestamps of 0 or we could calculate a
2272 * huge RTT and blow up the retransmit timer.
2274 if ((to.to_flags & TOF_TS) != 0 &&
2276 if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr)
2277 tp->t_rttlow = ticks - to.to_tsecr;
2278 tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
2279 } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2280 if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2281 tp->t_rttlow = ticks - tp->t_rtttime;
2282 tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2284 tcp_xmit_bandwidth_limit(tp, th->th_ack);
2287 * If all outstanding data is acked, stop retransmit
2288 * timer and remember to restart (more output or persist).
2289 * If there is more data to be acked, restart retransmit
2290 * timer, using current (possibly backed-off) value.
2292 if (th->th_ack == tp->snd_max) {
2293 tcp_timer_activate(tp, TT_REXMT, 0);
2295 } else if (!tcp_timer_active(tp, TT_PERSIST))
2296 tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2299 * If no data (only SYN) was ACK'd,
2300 * skip rest of ACK processing.
2306 * When new data is acked, open the congestion window.
2307 * Method depends on which congestion control state we're
2308 * in (slow start or cong avoid) and if ABC (RFC 3465) is
2311 * slow start: cwnd <= ssthresh
2312 * cong avoid: cwnd > ssthresh
2314 * slow start and ABC (RFC 3465):
2315 * Grow cwnd exponentially by the amount of data
2316 * ACKed capping the max increment per ACK to
2317 * (abc_l_var * maxseg) bytes.
2319 * slow start without ABC (RFC 2581):
2320 * Grow cwnd exponentially by maxseg per ACK.
2322 * cong avoid and ABC (RFC 3465):
2323 * Grow cwnd linearly by maxseg per RTT for each
2324 * cwnd worth of ACKed data.
2326 * cong avoid without ABC (RFC 2581):
2327 * Grow cwnd linearly by approximately maxseg per RTT using
2328 * maxseg^2 / cwnd per ACK as the increment.
2329 * If cwnd > maxseg^2, fix the cwnd increment at 1 byte to
2330 * avoid capping cwnd.
2332 if ((!V_tcp_do_newreno && !(tp->t_flags & TF_SACK_PERMIT)) ||
2333 !IN_FASTRECOVERY(tp)) {
2334 u_int cw = tp->snd_cwnd;
2335 u_int incr = tp->t_maxseg;
2336 /* In congestion avoidance? */
2337 if (cw > tp->snd_ssthresh) {
2338 if (V_tcp_do_rfc3465) {
2339 tp->t_bytes_acked += acked;
2340 if (tp->t_bytes_acked >= tp->snd_cwnd)
2341 tp->t_bytes_acked -= cw;
2346 incr = max((incr * incr / cw), 1);
2348 * In slow-start with ABC enabled and no RTO in sight?
2349 * (Must not use abc_l_var > 1 if slow starting after an
2350 * RTO. On RTO, snd_nxt = snd_una, so the snd_nxt ==
2351 * snd_max check is sufficient to handle this).
2353 } else if (V_tcp_do_rfc3465 &&
2354 tp->snd_nxt == tp->snd_max)
2356 V_tcp_abc_l_var * tp->t_maxseg);
2357 /* ABC is on by default, so (incr == 0) frequently. */
2359 tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale);
2361 SOCKBUF_LOCK(&so->so_snd);
2362 if (acked > so->so_snd.sb_cc) {
2363 tp->snd_wnd -= so->so_snd.sb_cc;
2364 sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc);
2367 sbdrop_locked(&so->so_snd, acked);
2368 tp->snd_wnd -= acked;
2371 /* NB: sowwakeup_locked() does an implicit unlock. */
2372 sowwakeup_locked(so);
2373 /* Detect una wraparound. */
2374 if ((V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) &&
2375 !IN_FASTRECOVERY(tp) &&
2376 SEQ_GT(tp->snd_una, tp->snd_recover) &&
2377 SEQ_LEQ(th->th_ack, tp->snd_recover))
2378 tp->snd_recover = th->th_ack - 1;
2379 if ((V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) &&
2380 IN_FASTRECOVERY(tp) &&
2381 SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2382 EXIT_FASTRECOVERY(tp);
2383 tp->t_bytes_acked = 0;
2385 tp->snd_una = th->th_ack;
2386 if (tp->t_flags & TF_SACK_PERMIT) {
2387 if (SEQ_GT(tp->snd_una, tp->snd_recover))
2388 tp->snd_recover = tp->snd_una;
2390 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2391 tp->snd_nxt = tp->snd_una;
2393 switch (tp->t_state) {
2396 * In FIN_WAIT_1 STATE in addition to the processing
2397 * for the ESTABLISHED state if our FIN is now acknowledged
2398 * then enter FIN_WAIT_2.
2400 case TCPS_FIN_WAIT_1:
2401 if (ourfinisacked) {
2403 * If we can't receive any more
2404 * data, then closing user can proceed.
2405 * Starting the timer is contrary to the
2406 * specification, but if we don't get a FIN
2407 * we'll hang forever.
2410 * we should release the tp also, and use a
2413 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
2416 soisdisconnected(so);
2417 timeout = (tcp_fast_finwait2_recycle) ?
2418 tcp_finwait2_timeout : tcp_maxidle;
2419 tcp_timer_activate(tp, TT_2MSL, timeout);
2421 tp->t_state = TCPS_FIN_WAIT_2;
2426 * In CLOSING STATE in addition to the processing for
2427 * the ESTABLISHED state if the ACK acknowledges our FIN
2428 * then enter the TIME-WAIT state, otherwise ignore
2432 if (ourfinisacked) {
2433 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2435 INP_INFO_WUNLOCK(&V_tcbinfo);
2442 * In LAST_ACK, we may still be waiting for data to drain
2443 * and/or to be acked, as well as for the ack of our FIN.
2444 * If our FIN is now acknowledged, delete the TCB,
2445 * enter the closed state and return.
2448 if (ourfinisacked) {
2449 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2458 INP_INFO_LOCK_ASSERT(&V_tcbinfo);
2459 KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED,
2460 ("tcp_do_segment: step6 ti_locked %d", ti_locked));
2461 INP_WLOCK_ASSERT(tp->t_inpcb);
2464 * Update window information.
2465 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2467 if ((thflags & TH_ACK) &&
2468 (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2469 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2470 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2471 /* keep track of pure window updates */
2473 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2474 TCPSTAT_INC(tcps_rcvwinupd);
2475 tp->snd_wnd = tiwin;
2476 tp->snd_wl1 = th->th_seq;
2477 tp->snd_wl2 = th->th_ack;
2478 if (tp->snd_wnd > tp->max_sndwnd)
2479 tp->max_sndwnd = tp->snd_wnd;
2484 * Process segments with URG.
2486 if ((thflags & TH_URG) && th->th_urp &&
2487 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2489 * This is a kludge, but if we receive and accept
2490 * random urgent pointers, we'll crash in
2491 * soreceive. It's hard to imagine someone
2492 * actually wanting to send this much urgent data.
2494 SOCKBUF_LOCK(&so->so_rcv);
2495 if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2496 th->th_urp = 0; /* XXX */
2497 thflags &= ~TH_URG; /* XXX */
2498 SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */
2499 goto dodata; /* XXX */
2502 * If this segment advances the known urgent pointer,
2503 * then mark the data stream. This should not happen
2504 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2505 * a FIN has been received from the remote side.
2506 * In these states we ignore the URG.
2508 * According to RFC961 (Assigned Protocols),
2509 * the urgent pointer points to the last octet
2510 * of urgent data. We continue, however,
2511 * to consider it to indicate the first octet
2512 * of data past the urgent section as the original
2513 * spec states (in one of two places).
2515 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2516 tp->rcv_up = th->th_seq + th->th_urp;
2517 so->so_oobmark = so->so_rcv.sb_cc +
2518 (tp->rcv_up - tp->rcv_nxt) - 1;
2519 if (so->so_oobmark == 0)
2520 so->so_rcv.sb_state |= SBS_RCVATMARK;
2522 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2524 SOCKBUF_UNLOCK(&so->so_rcv);
2526 * Remove out of band data so doesn't get presented to user.
2527 * This can happen independent of advancing the URG pointer,
2528 * but if two URG's are pending at once, some out-of-band
2529 * data may creep in... ick.
2531 if (th->th_urp <= (u_long)tlen &&
2532 !(so->so_options & SO_OOBINLINE)) {
2533 /* hdr drop is delayed */
2534 tcp_pulloutofband(so, th, m, drop_hdrlen);
2538 * If no out of band data is expected,
2539 * pull receive urgent pointer along
2540 * with the receive window.
2542 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2543 tp->rcv_up = tp->rcv_nxt;
2546 INP_INFO_LOCK_ASSERT(&V_tcbinfo);
2547 KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED,
2548 ("tcp_do_segment: dodata ti_locked %d", ti_locked));
2549 INP_WLOCK_ASSERT(tp->t_inpcb);
2552 * Process the segment text, merging it into the TCP sequencing queue,
2553 * and arranging for acknowledgment of receipt if necessary.
2554 * This process logically involves adjusting tp->rcv_wnd as data
2555 * is presented to the user (this happens in tcp_usrreq.c,
2556 * case PRU_RCVD). If a FIN has already been received on this
2557 * connection then we just ignore the text.
2559 if ((tlen || (thflags & TH_FIN)) &&
2560 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2561 tcp_seq save_start = th->th_seq;
2562 m_adj(m, drop_hdrlen); /* delayed header drop */
2564 * Insert segment which includes th into TCP reassembly queue
2565 * with control block tp. Set thflags to whether reassembly now
2566 * includes a segment with FIN. This handles the common case
2567 * inline (segment is the next to be received on an established
2568 * connection, and the queue is empty), avoiding linkage into
2569 * and removal from the queue and repetition of various
2571 * Set DELACK for segments received in order, but ack
2572 * immediately when segments are out of order (so
2573 * fast retransmit can work).
2575 if (th->th_seq == tp->rcv_nxt &&
2576 LIST_EMPTY(&tp->t_segq) &&
2577 TCPS_HAVEESTABLISHED(tp->t_state)) {
2579 tp->t_flags |= TF_DELACK;
2581 tp->t_flags |= TF_ACKNOW;
2582 tp->rcv_nxt += tlen;
2583 thflags = th->th_flags & TH_FIN;
2584 TCPSTAT_INC(tcps_rcvpack);
2585 TCPSTAT_ADD(tcps_rcvbyte, tlen);
2587 SOCKBUF_LOCK(&so->so_rcv);
2588 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
2591 sbappendstream_locked(&so->so_rcv, m);
2592 /* NB: sorwakeup_locked() does an implicit unlock. */
2593 sorwakeup_locked(so);
2596 * XXX: Due to the header drop above "th" is
2597 * theoretically invalid by now. Fortunately
2598 * m_adj() doesn't actually frees any mbufs
2599 * when trimming from the head.
2601 thflags = tcp_reass(tp, th, &tlen, m);
2602 tp->t_flags |= TF_ACKNOW;
2604 if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT))
2605 tcp_update_sack_list(tp, save_start, save_start + tlen);
2608 * Note the amount of data that peer has sent into
2609 * our window, in order to estimate the sender's
2613 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2621 * If FIN is received ACK the FIN and let the user know
2622 * that the connection is closing.
2624 if (thflags & TH_FIN) {
2625 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2628 * If connection is half-synchronized
2629 * (ie NEEDSYN flag on) then delay ACK,
2630 * so it may be piggybacked when SYN is sent.
2631 * Otherwise, since we received a FIN then no
2632 * more input can be expected, send ACK now.
2634 if (tp->t_flags & TF_NEEDSYN)
2635 tp->t_flags |= TF_DELACK;
2637 tp->t_flags |= TF_ACKNOW;
2640 switch (tp->t_state) {
2643 * In SYN_RECEIVED and ESTABLISHED STATES
2644 * enter the CLOSE_WAIT state.
2646 case TCPS_SYN_RECEIVED:
2647 tp->t_starttime = ticks;
2649 case TCPS_ESTABLISHED:
2650 tp->t_state = TCPS_CLOSE_WAIT;
2654 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2655 * enter the CLOSING state.
2657 case TCPS_FIN_WAIT_1:
2658 tp->t_state = TCPS_CLOSING;
2662 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2663 * starting the time-wait timer, turning off the other
2666 case TCPS_FIN_WAIT_2:
2667 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2668 KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata "
2669 "TCP_FIN_WAIT_2 ti_locked: %d", __func__,
2673 INP_INFO_WUNLOCK(&V_tcbinfo);
2677 if (ti_locked == TI_RLOCKED)
2678 INP_INFO_RUNLOCK(&V_tcbinfo);
2679 else if (ti_locked == TI_WLOCKED)
2680 INP_INFO_WUNLOCK(&V_tcbinfo);
2682 panic("%s: dodata epilogue ti_locked %d", __func__,
2684 ti_locked = TI_UNLOCKED;
2687 if (so->so_options & SO_DEBUG)
2688 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2693 * Return any desired output.
2695 if (needoutput || (tp->t_flags & TF_ACKNOW))
2696 (void) tcp_output(tp);
2699 KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d",
2700 __func__, ti_locked));
2701 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
2702 INP_WLOCK_ASSERT(tp->t_inpcb);
2704 if (tp->t_flags & TF_DELACK) {
2705 tp->t_flags &= ~TF_DELACK;
2706 tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
2708 INP_WUNLOCK(tp->t_inpcb);
2712 KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED,
2713 ("tcp_do_segment: dropafterack ti_locked %d", ti_locked));
2716 * Generate an ACK dropping incoming segment if it occupies
2717 * sequence space, where the ACK reflects our state.
2719 * We can now skip the test for the RST flag since all
2720 * paths to this code happen after packets containing
2721 * RST have been dropped.
2723 * In the SYN-RECEIVED state, don't send an ACK unless the
2724 * segment we received passes the SYN-RECEIVED ACK test.
2725 * If it fails send a RST. This breaks the loop in the
2726 * "LAND" DoS attack, and also prevents an ACK storm
2727 * between two listening ports that have been sent forged
2728 * SYN segments, each with the source address of the other.
2730 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2731 (SEQ_GT(tp->snd_una, th->th_ack) ||
2732 SEQ_GT(th->th_ack, tp->snd_max)) ) {
2733 rstreason = BANDLIM_RST_OPENPORT;
2737 if (so->so_options & SO_DEBUG)
2738 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2741 if (ti_locked == TI_RLOCKED)
2742 INP_INFO_RUNLOCK(&V_tcbinfo);
2743 else if (ti_locked == TI_WLOCKED)
2744 INP_INFO_WUNLOCK(&V_tcbinfo);
2746 panic("%s: dropafterack epilogue ti_locked %d", __func__,
2748 ti_locked = TI_UNLOCKED;
2750 tp->t_flags |= TF_ACKNOW;
2751 (void) tcp_output(tp);
2752 INP_WUNLOCK(tp->t_inpcb);
2757 if (ti_locked == TI_RLOCKED)
2758 INP_INFO_RUNLOCK(&V_tcbinfo);
2759 else if (ti_locked == TI_WLOCKED)
2760 INP_INFO_WUNLOCK(&V_tcbinfo);
2762 panic("%s: dropwithreset ti_locked %d", __func__, ti_locked);
2763 ti_locked = TI_UNLOCKED;
2766 tcp_dropwithreset(m, th, tp, tlen, rstreason);
2767 INP_WUNLOCK(tp->t_inpcb);
2769 tcp_dropwithreset(m, th, NULL, tlen, rstreason);
2773 if (ti_locked == TI_RLOCKED)
2774 INP_INFO_RUNLOCK(&V_tcbinfo);
2775 else if (ti_locked == TI_WLOCKED)
2776 INP_INFO_WUNLOCK(&V_tcbinfo);
2779 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
2781 ti_locked = TI_UNLOCKED;
2784 * Drop space held by incoming segment and return.
2787 if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2788 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2792 INP_WUNLOCK(tp->t_inpcb);
2797 * Issue RST and make ACK acceptable to originator of segment.
2798 * The mbuf must still include the original packet header.
2802 tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
2803 int tlen, int rstreason)
2807 struct ip6_hdr *ip6;
2811 INP_WLOCK_ASSERT(tp->t_inpcb);
2814 /* Don't bother if destination was broadcast/multicast. */
2815 if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2818 if (mtod(m, struct ip *)->ip_v == 6) {
2819 ip6 = mtod(m, struct ip6_hdr *);
2820 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2821 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2823 /* IPv6 anycast check is done at tcp6_input() */
2827 ip = mtod(m, struct ip *);
2828 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2829 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
2830 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
2831 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
2835 /* Perform bandwidth limiting. */
2836 if (badport_bandlim(rstreason) < 0)
2839 /* tcp_respond consumes the mbuf chain. */
2840 if (th->th_flags & TH_ACK) {
2841 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
2842 th->th_ack, TH_RST);
2844 if (th->th_flags & TH_SYN)
2846 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2847 (tcp_seq)0, TH_RST|TH_ACK);
2855 * Parse TCP options and place in tcpopt.
2858 tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
2863 for (; cnt > 0; cnt -= optlen, cp += optlen) {
2865 if (opt == TCPOPT_EOL)
2867 if (opt == TCPOPT_NOP)
2873 if (optlen < 2 || optlen > cnt)
2878 if (optlen != TCPOLEN_MAXSEG)
2880 if (!(flags & TO_SYN))
2882 to->to_flags |= TOF_MSS;
2883 bcopy((char *)cp + 2,
2884 (char *)&to->to_mss, sizeof(to->to_mss));
2885 to->to_mss = ntohs(to->to_mss);
2888 if (optlen != TCPOLEN_WINDOW)
2890 if (!(flags & TO_SYN))
2892 to->to_flags |= TOF_SCALE;
2893 to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
2895 case TCPOPT_TIMESTAMP:
2896 if (optlen != TCPOLEN_TIMESTAMP)
2898 to->to_flags |= TOF_TS;
2899 bcopy((char *)cp + 2,
2900 (char *)&to->to_tsval, sizeof(to->to_tsval));
2901 to->to_tsval = ntohl(to->to_tsval);
2902 bcopy((char *)cp + 6,
2903 (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2904 to->to_tsecr = ntohl(to->to_tsecr);
2906 #ifdef TCP_SIGNATURE
2908 * XXX In order to reply to a host which has set the
2909 * TCP_SIGNATURE option in its initial SYN, we have to
2910 * record the fact that the option was observed here
2911 * for the syncache code to perform the correct response.
2913 case TCPOPT_SIGNATURE:
2914 if (optlen != TCPOLEN_SIGNATURE)
2916 to->to_flags |= TOF_SIGNATURE;
2917 to->to_signature = cp + 2;
2920 case TCPOPT_SACK_PERMITTED:
2921 if (optlen != TCPOLEN_SACK_PERMITTED)
2923 if (!(flags & TO_SYN))
2927 to->to_flags |= TOF_SACKPERM;
2930 if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
2934 to->to_flags |= TOF_SACK;
2935 to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
2936 to->to_sacks = cp + 2;
2937 TCPSTAT_INC(tcps_sack_rcv_blocks);
2946 * Pull out of band byte out of a segment so
2947 * it doesn't appear in the user's data queue.
2948 * It is still reflected in the segment length for
2949 * sequencing purposes.
2952 tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
2955 int cnt = off + th->th_urp - 1;
2958 if (m->m_len > cnt) {
2959 char *cp = mtod(m, caddr_t) + cnt;
2960 struct tcpcb *tp = sototcpcb(so);
2962 INP_WLOCK_ASSERT(tp->t_inpcb);
2965 tp->t_oobflags |= TCPOOB_HAVEDATA;
2966 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2968 if (m->m_flags & M_PKTHDR)
2977 panic("tcp_pulloutofband");
2981 * Collect new round-trip time estimate
2982 * and update averages and current timeout.
2985 tcp_xmit_timer(struct tcpcb *tp, int rtt)
2989 INP_WLOCK_ASSERT(tp->t_inpcb);
2991 TCPSTAT_INC(tcps_rttupdated);
2993 if (tp->t_srtt != 0) {
2995 * srtt is stored as fixed point with 5 bits after the
2996 * binary point (i.e., scaled by 8). The following magic
2997 * is equivalent to the smoothing algorithm in rfc793 with
2998 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2999 * point). Adjust rtt to origin 0.
3001 delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3002 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3004 if ((tp->t_srtt += delta) <= 0)
3008 * We accumulate a smoothed rtt variance (actually, a
3009 * smoothed mean difference), then set the retransmit
3010 * timer to smoothed rtt + 4 times the smoothed variance.
3011 * rttvar is stored as fixed point with 4 bits after the
3012 * binary point (scaled by 16). The following is
3013 * equivalent to rfc793 smoothing with an alpha of .75
3014 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
3015 * rfc793's wired-in beta.
3019 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3020 if ((tp->t_rttvar += delta) <= 0)
3022 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
3023 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3026 * No rtt measurement yet - use the unsmoothed rtt.
3027 * Set the variance to half the rtt (so our first
3028 * retransmit happens at 3*rtt).
3030 tp->t_srtt = rtt << TCP_RTT_SHIFT;
3031 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
3032 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3038 * the retransmit should happen at rtt + 4 * rttvar.
3039 * Because of the way we do the smoothing, srtt and rttvar
3040 * will each average +1/2 tick of bias. When we compute
3041 * the retransmit timer, we want 1/2 tick of rounding and
3042 * 1 extra tick because of +-1/2 tick uncertainty in the
3043 * firing of the timer. The bias will give us exactly the
3044 * 1.5 tick we need. But, because the bias is
3045 * statistical, we have to test that we don't drop below
3046 * the minimum feasible timer (which is 2 ticks).
3048 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
3049 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3052 * We received an ack for a packet that wasn't retransmitted;
3053 * it is probably safe to discard any error indications we've
3054 * received recently. This isn't quite right, but close enough
3055 * for now (a route might have failed after we sent a segment,
3056 * and the return path might not be symmetrical).
3058 tp->t_softerror = 0;
3062 * Determine a reasonable value for maxseg size.
3063 * If the route is known, check route for mtu.
3064 * If none, use an mss that can be handled on the outgoing
3065 * interface without forcing IP to fragment; if bigger than
3066 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
3067 * to utilize large mbufs. If no route is found, route has no mtu,
3068 * or the destination isn't local, use a default, hopefully conservative
3069 * size (usually 512 or the default IP max size, but no more than the mtu
3070 * of the interface), as we can't discover anything about intervening
3071 * gateways or networks. We also initialize the congestion/slow start
3072 * window to be a single segment if the destination isn't local.
3073 * While looking at the routing entry, we also initialize other path-dependent
3074 * parameters from pre-set or cached values in the routing entry.
3076 * Also take into account the space needed for options that we
3077 * send regularly. Make maxseg shorter by that amount to assure
3078 * that we can send maxseg amount of data even when the options
3079 * are present. Store the upper limit of the length of options plus
3082 * In case of T/TCP, we call this routine during implicit connection
3083 * setup as well (offer = -1), to initialize maxseg from the cached
3086 * NOTE that this routine is only called when we process an incoming
3087 * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt().
3090 tcp_mss_update(struct tcpcb *tp, int offer,
3091 struct hc_metrics_lite *metricptr, int *mtuflags)
3095 struct inpcb *inp = tp->t_inpcb;
3096 struct hc_metrics_lite metrics;
3097 int origoffer = offer;
3099 int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3100 size_t min_protoh = isipv6 ?
3101 sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3102 sizeof (struct tcpiphdr);
3104 const size_t min_protoh = sizeof(struct tcpiphdr);
3107 INP_WLOCK_ASSERT(tp->t_inpcb);
3112 maxmtu = tcp_maxmtu6(&inp->inp_inc, mtuflags);
3113 tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt;
3117 maxmtu = tcp_maxmtu(&inp->inp_inc, mtuflags);
3118 tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt;
3122 * No route to sender, stay with default mss and return.
3126 * In case we return early we need to initialize metrics
3127 * to a defined state as tcp_hc_get() would do for us
3128 * if there was no cache hit.
3130 if (metricptr != NULL)
3131 bzero(metricptr, sizeof(struct hc_metrics_lite));
3135 /* What have we got? */
3139 * Offer == 0 means that there was no MSS on the SYN
3140 * segment, in this case we use tcp_mssdflt as
3141 * already assigned to t_maxopd above.
3143 offer = tp->t_maxopd;
3148 * Offer == -1 means that we didn't receive SYN yet.
3154 * Prevent DoS attack with too small MSS. Round up
3155 * to at least minmss.
3157 offer = max(offer, V_tcp_minmss);
3161 * rmx information is now retrieved from tcp_hostcache.
3163 tcp_hc_get(&inp->inp_inc, &metrics);
3164 if (metricptr != NULL)
3165 bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
3168 * If there's a discovered mtu int tcp hostcache, use it
3169 * else, use the link mtu.
3171 if (metrics.rmx_mtu)
3172 mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3176 mss = maxmtu - min_protoh;
3177 if (!V_path_mtu_discovery &&
3178 !in6_localaddr(&inp->in6p_faddr))
3179 mss = min(mss, V_tcp_v6mssdflt);
3183 mss = maxmtu - min_protoh;
3184 if (!V_path_mtu_discovery &&
3185 !in_localaddr(inp->inp_faddr))
3186 mss = min(mss, V_tcp_mssdflt);
3189 * XXX - The above conditional (mss = maxmtu - min_protoh)
3190 * probably violates the TCP spec.
3191 * The problem is that, since we don't know the
3192 * other end's MSS, we are supposed to use a conservative
3193 * default. But, if we do that, then MTU discovery will
3194 * never actually take place, because the conservative
3195 * default is much less than the MTUs typically seen
3196 * on the Internet today. For the moment, we'll sweep
3197 * this under the carpet.
3199 * The conservative default might not actually be a problem
3200 * if the only case this occurs is when sending an initial
3201 * SYN with options and data to a host we've never talked
3202 * to before. Then, they will reply with an MSS value which
3203 * will get recorded and the new parameters should get
3204 * recomputed. For Further Study.
3207 mss = min(mss, offer);
3210 * Sanity check: make sure that maxopd will be large
3211 * enough to allow some data on segments even if the
3212 * all the option space is used (40bytes). Otherwise
3213 * funny things may happen in tcp_output.
3218 * maxopd stores the maximum length of data AND options
3219 * in a segment; maxseg is the amount of data in a normal
3220 * segment. We need to store this value (maxopd) apart
3221 * from maxseg, because now every segment carries options
3222 * and thus we normally have somewhat less data in segments.
3227 * origoffer==-1 indicates that no segments were received yet.
3228 * In this case we just guess.
3230 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
3232 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
3233 mss -= TCPOLEN_TSTAMP_APPA;
3235 #if (MCLBYTES & (MCLBYTES - 1)) == 0
3237 mss &= ~(MCLBYTES-1);
3240 mss = mss / MCLBYTES * MCLBYTES;
3246 tcp_mss(struct tcpcb *tp, int offer)
3252 struct hc_metrics_lite metrics;
3257 KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
3259 tcp_mss_update(tp, offer, &metrics, &mtuflags);
3264 isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3268 * If there's a pipesize, change the socket buffer to that size,
3269 * don't change if sb_hiwat is different than default (then it
3270 * has been changed on purpose with setsockopt).
3271 * Make the socket buffers an integral number of mss units;
3272 * if the mss is larger than the socket buffer, decrease the mss.
3274 so = inp->inp_socket;
3275 SOCKBUF_LOCK(&so->so_snd);
3276 if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe)
3277 bufsize = metrics.rmx_sendpipe;
3279 bufsize = so->so_snd.sb_hiwat;
3283 bufsize = roundup(bufsize, mss);
3284 if (bufsize > sb_max)
3286 if (bufsize > so->so_snd.sb_hiwat)
3287 (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3289 SOCKBUF_UNLOCK(&so->so_snd);
3292 SOCKBUF_LOCK(&so->so_rcv);
3293 if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe)
3294 bufsize = metrics.rmx_recvpipe;
3296 bufsize = so->so_rcv.sb_hiwat;
3297 if (bufsize > mss) {
3298 bufsize = roundup(bufsize, mss);
3299 if (bufsize > sb_max)
3301 if (bufsize > so->so_rcv.sb_hiwat)
3302 (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3304 SOCKBUF_UNLOCK(&so->so_rcv);
3306 * While we're here, check the others too.
3308 if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
3310 tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
3311 TCPSTAT_INC(tcps_usedrtt);
3312 if (metrics.rmx_rttvar) {
3313 tp->t_rttvar = metrics.rmx_rttvar;
3314 TCPSTAT_INC(tcps_usedrttvar);
3316 /* default variation is +- 1 rtt */
3318 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
3320 TCPT_RANGESET(tp->t_rxtcur,
3321 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
3322 tp->t_rttmin, TCPTV_REXMTMAX);
3324 if (metrics.rmx_ssthresh) {
3326 * There's some sort of gateway or interface
3327 * buffer limit on the path. Use this to set
3328 * the slow start threshhold, but set the
3329 * threshold to no less than 2*mss.
3331 tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh);
3332 TCPSTAT_INC(tcps_usedssthresh);
3334 if (metrics.rmx_bandwidth)
3335 tp->snd_bandwidth = metrics.rmx_bandwidth;
3338 * Set the slow-start flight size depending on whether this
3339 * is a local network or not.
3341 * Extend this so we cache the cwnd too and retrieve it here.
3342 * Make cwnd even bigger than RFC3390 suggests but only if we
3343 * have previous experience with the remote host. Be careful
3344 * not make cwnd bigger than remote receive window or our own
3345 * send socket buffer. Maybe put some additional upper bound
3346 * on the retrieved cwnd. Should do incremental updates to
3347 * hostcache when cwnd collapses so next connection doesn't
3348 * overloads the path again.
3350 * RFC3390 says only do this if SYN or SYN/ACK didn't got lost.
3351 * We currently check only in syncache_socket for that.
3353 #define TCP_METRICS_CWND
3354 #ifdef TCP_METRICS_CWND
3355 if (metrics.rmx_cwnd)
3356 tp->snd_cwnd = max(mss,
3357 min(metrics.rmx_cwnd / 2,
3358 min(tp->snd_wnd, so->so_snd.sb_hiwat)));
3361 if (V_tcp_do_rfc3390)
3362 tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380));
3364 else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
3365 (!isipv6 && in_localaddr(inp->inp_faddr)))
3367 else if (in_localaddr(inp->inp_faddr))
3369 tp->snd_cwnd = mss * V_ss_fltsz_local;
3371 tp->snd_cwnd = mss * V_ss_fltsz;
3373 /* Check the interface for TSO capabilities. */
3374 if (mtuflags & CSUM_TSO)
3375 tp->t_flags |= TF_TSO;
3379 * Determine the MSS option to send on an outgoing SYN.
3382 tcp_mssopt(struct in_conninfo *inc)
3389 KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3392 if (inc->inc_flags & INC_ISIPV6) {
3393 mss = V_tcp_v6mssdflt;
3394 maxmtu = tcp_maxmtu6(inc, NULL);
3395 thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
3396 min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3400 mss = V_tcp_mssdflt;
3401 maxmtu = tcp_maxmtu(inc, NULL);
3402 thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
3403 min_protoh = sizeof(struct tcpiphdr);
3405 if (maxmtu && thcmtu)
3406 mss = min(maxmtu, thcmtu) - min_protoh;
3407 else if (maxmtu || thcmtu)
3408 mss = max(maxmtu, thcmtu) - min_protoh;
3415 * On a partial ack arrives, force the retransmission of the
3416 * next unacknowledged segment. Do not clear tp->t_dupacks.
3417 * By setting snd_nxt to ti_ack, this forces retransmission timer to
3421 tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
3423 tcp_seq onxt = tp->snd_nxt;
3424 u_long ocwnd = tp->snd_cwnd;
3426 INP_WLOCK_ASSERT(tp->t_inpcb);
3428 tcp_timer_activate(tp, TT_REXMT, 0);
3430 tp->snd_nxt = th->th_ack;
3432 * Set snd_cwnd to one segment beyond acknowledged offset.
3433 * (tp->snd_una has not yet been updated when this function is called.)
3435 tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
3436 tp->t_flags |= TF_ACKNOW;
3437 (void) tcp_output(tp);
3438 tp->snd_cwnd = ocwnd;
3439 if (SEQ_GT(onxt, tp->snd_nxt))
3442 * Partial window deflation. Relies on fact that tp->snd_una
3445 if (tp->snd_cwnd > th->th_ack - tp->snd_una)
3446 tp->snd_cwnd -= th->th_ack - tp->snd_una;
3449 tp->snd_cwnd += tp->t_maxseg;