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
33 #include "opt_ipfw.h" /* for ipfw_fwd */
35 #include "opt_inet6.h"
36 #include "opt_ipsec.h"
38 #include "opt_tcpdebug.h"
39 #include "opt_tcp_input.h"
40 #include "opt_tcp_sack.h"
42 #include <sys/param.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #include <sys/proc.h> /* for proc0 declaration */
48 #include <sys/protosw.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
54 #include <sys/systm.h>
56 #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */
61 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip.h>
68 #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */
69 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
70 #include <netinet/ip_var.h>
71 #include <netinet/ip6.h>
72 #include <netinet/icmp6.h>
73 #include <netinet6/in6_pcb.h>
74 #include <netinet6/ip6_var.h>
75 #include <netinet6/nd6.h>
76 #include <netinet/tcp.h>
77 #include <netinet/tcp_fsm.h>
78 #include <netinet/tcp_seq.h>
79 #include <netinet/tcp_timer.h>
80 #include <netinet/tcp_var.h>
81 #include <netinet6/tcp6_var.h>
82 #include <netinet/tcpip.h>
84 #include <netinet/tcp_debug.h>
88 #include <netipsec/ipsec.h>
89 #include <netipsec/ipsec6.h>
93 #include <netinet6/ipsec.h>
94 #include <netinet6/ipsec6.h>
95 #include <netkey/key.h>
98 #include <machine/in_cksum.h>
100 static const int tcprexmtthresh = 3;
102 struct tcpstat tcpstat;
103 SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW,
104 &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
106 static int log_in_vain = 0;
107 SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
108 &log_in_vain, 0, "Log all incoming TCP connections");
110 static int blackhole = 0;
111 SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
112 &blackhole, 0, "Do not send RST when dropping refused connections");
114 int tcp_delack_enabled = 1;
115 SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
116 &tcp_delack_enabled, 0,
117 "Delay ACK to try and piggyback it onto a data packet");
119 #ifdef TCP_DROP_SYNFIN
120 static int drop_synfin = 0;
121 SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
122 &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
125 static int tcp_do_rfc3042 = 1;
126 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW,
127 &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)");
129 static int tcp_do_rfc3390 = 1;
130 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW,
132 "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
134 static int tcp_insecure_rst = 0;
135 SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW,
136 &tcp_insecure_rst, 0,
137 "Follow the old (insecure) criteria for accepting RST packets.");
139 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
140 "TCP Segment Reassembly Queue");
142 static int tcp_reass_maxseg = 0;
143 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN,
144 &tcp_reass_maxseg, 0,
145 "Global maximum number of TCP Segments in Reassembly Queue");
147 int tcp_reass_qsize = 0;
148 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD,
150 "Global number of TCP Segments currently in Reassembly Queue");
152 static int tcp_reass_maxqlen = 48;
153 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxqlen, CTLFLAG_RW,
154 &tcp_reass_maxqlen, 0,
155 "Maximum number of TCP Segments per individual Reassembly Queue");
157 static int tcp_reass_overflows = 0;
158 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD,
159 &tcp_reass_overflows, 0,
160 "Global number of TCP Segment Reassembly Queue Overflows");
162 struct inpcbhead tcb;
163 #define tcb6 tcb /* for KAME src sync over BSD*'s */
164 struct inpcbinfo tcbinfo;
165 struct mtx *tcbinfo_mtx;
167 static void tcp_dooptions(struct tcpopt *, u_char *, int, int);
169 static void tcp_pulloutofband(struct socket *,
170 struct tcphdr *, struct mbuf *, int);
171 static int tcp_reass(struct tcpcb *, struct tcphdr *, int *,
173 static void tcp_xmit_timer(struct tcpcb *, int);
174 static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
175 static int tcp_timewait(struct tcptw *, struct tcpopt *,
176 struct tcphdr *, struct mbuf *, int);
178 /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
180 #define ND6_HINT(tp) \
182 if ((tp) && (tp)->t_inpcb && \
183 ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \
184 nd6_nud_hint(NULL, NULL, 0); \
191 * Indicate whether this ack should be delayed. We can delay the ack if
192 * - there is no delayed ack timer in progress and
193 * - our last ack wasn't a 0-sized window. We never want to delay
194 * the ack that opens up a 0-sized window and
195 * - delayed acks are enabled or
196 * - this is a half-synchronized T/TCP connection.
198 #define DELAY_ACK(tp) \
199 ((!callout_active(tp->tt_delack) && \
200 (tp->t_flags & TF_RXWIN0SENT) == 0) && \
201 (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
203 /* Initialize TCP reassembly queue */
204 uma_zone_t tcp_reass_zone;
208 tcp_reass_maxseg = nmbclusters / 16;
209 TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
211 tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent),
212 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
213 uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg);
217 tcp_reass(tp, th, tlenp, m)
218 register struct tcpcb *tp;
219 register struct tcphdr *th;
224 struct tseg_qent *p = NULL;
225 struct tseg_qent *nq;
226 struct tseg_qent *te = NULL;
227 struct socket *so = tp->t_inpcb->inp_socket;
230 INP_LOCK_ASSERT(tp->t_inpcb);
233 * XXX: tcp_reass() is rather inefficient with its data structures
234 * and should be rewritten (see NetBSD for optimizations). While
235 * doing that it should move to its own file tcp_reass.c.
239 * Call with th==NULL after become established to
240 * force pre-ESTABLISHED data up to user socket.
246 * Limit the number of segments in the reassembly queue to prevent
247 * holding on to too many segments (and thus running out of mbufs).
248 * Make sure to let the missing segment through which caused this
249 * queue. Always keep one global queue entry spare to be able to
250 * process the missing segment.
252 if (th->th_seq != tp->rcv_nxt &&
253 (tcp_reass_qsize + 1 >= tcp_reass_maxseg ||
254 tp->t_segqlen >= tcp_reass_maxqlen)) {
255 tcp_reass_overflows++;
256 tcpstat.tcps_rcvmemdrop++;
263 * Allocate a new queue entry. If we can't, or hit the zone limit
266 te = uma_zalloc(tcp_reass_zone, M_NOWAIT);
268 tcpstat.tcps_rcvmemdrop++;
277 * Find a segment which begins after this one does.
279 LIST_FOREACH(q, &tp->t_segq, tqe_q) {
280 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
286 * If there is a preceding segment, it may provide some of
287 * our data already. If so, drop the data from the incoming
288 * segment. If it provides all of our data, drop us.
292 /* conversion to int (in i) handles seq wraparound */
293 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
296 tcpstat.tcps_rcvduppack++;
297 tcpstat.tcps_rcvdupbyte += *tlenp;
299 uma_zfree(tcp_reass_zone, te);
303 * Try to present any queued data
304 * at the left window edge to the user.
305 * This is needed after the 3-WHS
308 goto present; /* ??? */
315 tcpstat.tcps_rcvoopack++;
316 tcpstat.tcps_rcvoobyte += *tlenp;
319 * While we overlap succeeding segments trim them or,
320 * if they are completely covered, dequeue them.
323 register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
326 if (i < q->tqe_len) {
327 q->tqe_th->th_seq += i;
333 nq = LIST_NEXT(q, tqe_q);
334 LIST_REMOVE(q, tqe_q);
336 uma_zfree(tcp_reass_zone, q);
342 /* Insert the new segment queue entry into place. */
345 te->tqe_len = *tlenp;
348 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
350 LIST_INSERT_AFTER(p, te, tqe_q);
355 * Present data to user, advancing rcv_nxt through
356 * completed sequence space.
358 if (!TCPS_HAVEESTABLISHED(tp->t_state))
360 q = LIST_FIRST(&tp->t_segq);
361 if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
363 SOCKBUF_LOCK(&so->so_rcv);
365 tp->rcv_nxt += q->tqe_len;
366 flags = q->tqe_th->th_flags & TH_FIN;
367 nq = LIST_NEXT(q, tqe_q);
368 LIST_REMOVE(q, tqe_q);
369 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
372 sbappendstream_locked(&so->so_rcv, q->tqe_m);
373 uma_zfree(tcp_reass_zone, q);
377 } while (q && q->tqe_th->th_seq == tp->rcv_nxt);
379 sorwakeup_locked(so);
384 * TCP input routine, follows pages 65-76 of the
385 * protocol specification dated September, 1981 very closely.
389 tcp6_input(mp, offp, proto)
393 register struct mbuf *m = *mp;
394 struct in6_ifaddr *ia6;
396 IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
399 * draft-itojun-ipv6-tcp-to-anycast
400 * better place to put this in?
402 ia6 = ip6_getdstifaddr(m);
403 if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
406 ip6 = mtod(m, struct ip6_hdr *);
407 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
408 (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
419 register struct mbuf *m;
422 register struct tcphdr *th;
423 register struct ip *ip = NULL;
424 register struct ipovly *ipov;
425 register struct inpcb *inp = NULL;
430 register struct tcpcb *tp = 0;
431 register int thflags;
432 struct socket *so = 0;
433 int todrop, acked, ourfinisacked, needoutput = 0;
435 struct tcpopt to; /* options in this segment */
437 #ifdef IPFIREWALL_FORWARD
438 struct m_tag *fwd_tag;
440 int rstreason; /* For badport_bandlim accounting purposes */
442 struct ip6_hdr *ip6 = NULL;
446 const int isipv6 = 0;
451 * The size of tcp_saveipgen must be the size of the max ip header,
454 u_char tcp_saveipgen[40];
455 struct tcphdr tcp_savetcp;
460 isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
462 bzero((char *)&to, sizeof(to));
464 tcpstat.tcps_rcvtotal++;
468 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
469 ip6 = mtod(m, struct ip6_hdr *);
470 tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
471 if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
472 tcpstat.tcps_rcvbadsum++;
475 th = (struct tcphdr *)((caddr_t)ip6 + off0);
478 * Be proactive about unspecified IPv6 address in source.
479 * As we use all-zero to indicate unbounded/unconnected pcb,
480 * unspecified IPv6 address can be used to confuse us.
482 * Note that packets with unspecified IPv6 destination is
483 * already dropped in ip6_input.
485 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
490 th = NULL; /* XXX: avoid compiler warning */
494 * Get IP and TCP header together in first mbuf.
495 * Note: IP leaves IP header in first mbuf.
497 if (off0 > sizeof (struct ip)) {
498 ip_stripoptions(m, (struct mbuf *)0);
499 off0 = sizeof(struct ip);
501 if (m->m_len < sizeof (struct tcpiphdr)) {
502 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
503 tcpstat.tcps_rcvshort++;
507 ip = mtod(m, struct ip *);
508 ipov = (struct ipovly *)ip;
509 th = (struct tcphdr *)((caddr_t)ip + off0);
512 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
513 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
514 th->th_sum = m->m_pkthdr.csum_data;
516 th->th_sum = in_pseudo(ip->ip_src.s_addr,
518 htonl(m->m_pkthdr.csum_data +
521 th->th_sum ^= 0xffff;
523 ipov->ih_len = (u_short)tlen;
524 ipov->ih_len = htons(ipov->ih_len);
528 * Checksum extended TCP header and data.
530 len = sizeof (struct ip) + tlen;
531 bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
532 ipov->ih_len = (u_short)tlen;
533 ipov->ih_len = htons(ipov->ih_len);
534 th->th_sum = in_cksum(m, len);
537 tcpstat.tcps_rcvbadsum++;
541 /* Re-initialization for later version check */
542 ip->ip_v = IPVERSION;
547 * Check that TCP offset makes sense,
548 * pull out TCP options and adjust length. XXX
550 off = th->th_off << 2;
551 if (off < sizeof (struct tcphdr) || off > tlen) {
552 tcpstat.tcps_rcvbadoff++;
555 tlen -= off; /* tlen is used instead of ti->ti_len */
556 if (off > sizeof (struct tcphdr)) {
559 IP6_EXTHDR_CHECK(m, off0, off, );
560 ip6 = mtod(m, struct ip6_hdr *);
561 th = (struct tcphdr *)((caddr_t)ip6 + off0);
564 if (m->m_len < sizeof(struct ip) + off) {
565 if ((m = m_pullup(m, sizeof (struct ip) + off))
567 tcpstat.tcps_rcvshort++;
570 ip = mtod(m, struct ip *);
571 ipov = (struct ipovly *)ip;
572 th = (struct tcphdr *)((caddr_t)ip + off0);
575 optlen = off - sizeof (struct tcphdr);
576 optp = (u_char *)(th + 1);
578 thflags = th->th_flags;
580 #ifdef TCP_DROP_SYNFIN
582 * If the drop_synfin option is enabled, drop all packets with
583 * both the SYN and FIN bits set. This prevents e.g. nmap from
584 * identifying the TCP/IP stack.
586 * This is a violation of the TCP specification.
588 if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
593 * Convert TCP protocol specific fields to host format.
595 th->th_seq = ntohl(th->th_seq);
596 th->th_ack = ntohl(th->th_ack);
597 th->th_win = ntohs(th->th_win);
598 th->th_urp = ntohs(th->th_urp);
601 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
602 * until after ip6_savecontrol() is called and before other functions
603 * which don't want those proto headers.
604 * Because ip6_savecontrol() is going to parse the mbuf to
605 * search for data to be passed up to user-land, it wants mbuf
606 * parameters to be unchanged.
607 * XXX: the call of ip6_savecontrol() has been obsoleted based on
608 * latest version of the advanced API (20020110).
610 drop_hdrlen = off0 + off;
613 * Locate pcb for segment.
615 INP_INFO_WLOCK(&tcbinfo);
618 KASSERT(headlocked, ("tcp_input: findpcb: head not locked"));
619 #ifdef IPFIREWALL_FORWARD
620 /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
621 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
623 if (fwd_tag != NULL && isipv6 == 0) { /* IPv6 support is not yet */
624 struct sockaddr_in *next_hop;
626 next_hop = (struct sockaddr_in *)(fwd_tag+1);
628 * Transparently forwarded. Pretend to be the destination.
629 * already got one like this?
631 inp = in_pcblookup_hash(&tcbinfo,
632 ip->ip_src, th->th_sport,
633 ip->ip_dst, th->th_dport,
634 0, m->m_pkthdr.rcvif);
636 /* It's new. Try to find the ambushing socket. */
637 inp = in_pcblookup_hash(&tcbinfo,
638 ip->ip_src, th->th_sport,
641 ntohs(next_hop->sin_port) :
643 1, m->m_pkthdr.rcvif);
645 /* Remove the tag from the packet. We don't need it anymore. */
646 m_tag_delete(m, fwd_tag);
648 #endif /* IPFIREWALL_FORWARD */
651 inp = in6_pcblookup_hash(&tcbinfo,
652 &ip6->ip6_src, th->th_sport,
653 &ip6->ip6_dst, th->th_dport,
654 1, m->m_pkthdr.rcvif);
657 inp = in_pcblookup_hash(&tcbinfo,
658 ip->ip_src, th->th_sport,
659 ip->ip_dst, th->th_dport,
660 1, m->m_pkthdr.rcvif);
661 #ifdef IPFIREWALL_FORWARD
663 #endif /* IPFIREWALL_FORWARD */
665 #if defined(IPSEC) || defined(FAST_IPSEC)
668 if (inp != NULL && ipsec6_in_reject(m, inp)) {
670 ipsec6stat.in_polvio++;
676 if (inp != NULL && ipsec4_in_reject(m, inp)) {
678 ipsecstat.in_polvio++;
682 #endif /*IPSEC || FAST_IPSEC*/
685 * If the state is CLOSED (i.e., TCB does not exist) then
686 * all data in the incoming segment is discarded.
687 * If the TCB exists but is in CLOSED state, it is embryonic,
688 * but should either do a listen or a connect soon.
693 char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2];
695 char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
702 strcat(dbuf, ip6_sprintf(&ip6->ip6_dst));
703 strcat(sbuf, ip6_sprintf(&ip6->ip6_src));
708 strcpy(dbuf, inet_ntoa(ip->ip_dst));
709 strcpy(sbuf, inet_ntoa(ip->ip_src));
711 switch (log_in_vain) {
713 if ((thflags & TH_SYN) == 0)
718 "Connection attempt to TCP %s:%d "
719 "from %s:%d flags:0x%02x\n",
720 dbuf, ntohs(th->th_dport), sbuf,
721 ntohs(th->th_sport), thflags);
730 if (thflags & TH_SYN)
739 rstreason = BANDLIM_RST_CLOSEDPORT;
744 /* Check the minimum TTL for socket. */
745 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl)
748 if (inp->inp_vflag & INP_TIMEWAIT) {
750 * The only option of relevance is TOF_CC, and only if
751 * present in a SYN segment. See tcp_timewait().
753 if (thflags & TH_SYN)
754 tcp_dooptions(&to, optp, optlen, 1);
755 if (tcp_timewait((struct tcptw *)inp->inp_ppcb,
759 * tcp_timewait unlocks inp.
761 INP_INFO_WUNLOCK(&tcbinfo);
767 rstreason = BANDLIM_RST_CLOSEDPORT;
770 if (tp->t_state == TCPS_CLOSED)
773 /* Unscale the window into a 32-bit value. */
774 if ((thflags & TH_SYN) == 0)
775 tiwin = th->th_win << tp->snd_scale;
780 INP_LOCK_ASSERT(inp);
781 if (mac_check_inpcb_deliver(inp, m))
784 so = inp->inp_socket;
786 if (so->so_options & SO_DEBUG) {
787 ostate = tp->t_state;
789 bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
791 bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
795 if (so->so_options & SO_ACCEPTCONN) {
796 struct in_conninfo inc;
799 inc.inc_isipv6 = isipv6;
802 inc.inc6_faddr = ip6->ip6_src;
803 inc.inc6_laddr = ip6->ip6_dst;
805 inc.inc_faddr = ip->ip_src;
806 inc.inc_laddr = ip->ip_dst;
808 inc.inc_fport = th->th_sport;
809 inc.inc_lport = th->th_dport;
812 * If the state is LISTEN then ignore segment if it contains
813 * a RST. If the segment contains an ACK then it is bad and
814 * send a RST. If it does not contain a SYN then it is not
815 * interesting; drop it.
817 * If the state is SYN_RECEIVED (syncache) and seg contains
818 * an ACK, but not for our SYN/ACK, send a RST. If the seg
819 * contains a RST, check the sequence number to see if it
820 * is a valid reset segment.
822 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
823 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
824 if (!syncache_expand(&inc, th, &so, m)) {
826 * No syncache entry, or ACK was not
827 * for our SYN/ACK. Send a RST.
829 tcpstat.tcps_badsyn++;
830 rstreason = BANDLIM_RST_OPENPORT;
835 * Could not complete 3-way handshake,
836 * connection is being closed down, and
837 * syncache will free mbuf.
840 INP_INFO_WUNLOCK(&tcbinfo);
844 * Socket is created in state SYN_RECEIVED.
845 * Continue processing segment.
852 * This is what would have happened in
853 * tcp_output() when the SYN,ACK was sent.
855 tp->snd_up = tp->snd_una;
856 tp->snd_max = tp->snd_nxt = tp->iss + 1;
857 tp->last_ack_sent = tp->rcv_nxt;
859 * RFC1323: The window in SYN & SYN/ACK
860 * segments is never scaled.
862 tp->snd_wnd = tiwin; /* unscaled */
865 if (thflags & TH_RST) {
866 syncache_chkrst(&inc, th);
869 if (thflags & TH_ACK) {
870 syncache_badack(&inc);
871 tcpstat.tcps_badsyn++;
872 rstreason = BANDLIM_RST_OPENPORT;
879 * Segment's flags are (SYN) or (SYN|FIN).
883 * If deprecated address is forbidden,
884 * we do not accept SYN to deprecated interface
885 * address to prevent any new inbound connection from
886 * getting established.
887 * When we do not accept SYN, we send a TCP RST,
888 * with deprecated source address (instead of dropping
889 * it). We compromise it as it is much better for peer
890 * to send a RST, and RST will be the final packet
893 * If we do not forbid deprecated addresses, we accept
894 * the SYN packet. RFC2462 does not suggest dropping
896 * If we decipher RFC2462 5.5.4, it says like this:
897 * 1. use of deprecated addr with existing
898 * communication is okay - "SHOULD continue to be
900 * 2. use of it with new communication:
901 * (2a) "SHOULD NOT be used if alternate address
902 * with sufficient scope is available"
903 * (2b) nothing mentioned otherwise.
904 * Here we fall into (2b) case as we have no choice in
905 * our source address selection - we must obey the peer.
907 * The wording in RFC2462 is confusing, and there are
908 * multiple description text for deprecated address
909 * handling - worse, they are not exactly the same.
910 * I believe 5.5.4 is the best one, so we follow 5.5.4.
912 if (isipv6 && !ip6_use_deprecated) {
913 struct in6_ifaddr *ia6;
915 if ((ia6 = ip6_getdstifaddr(m)) &&
916 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
919 rstreason = BANDLIM_RST_OPENPORT;
925 * If it is from this socket, drop it, it must be forged.
926 * Don't bother responding if the destination was a broadcast.
928 if (th->th_dport == th->th_sport) {
930 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
934 if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
939 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
941 * Note that it is quite possible to receive unicast
942 * link-layer packets with a broadcast IP address. Use
943 * in_broadcast() to find them.
945 if (m->m_flags & (M_BCAST|M_MCAST))
948 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
949 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
952 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
953 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
954 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
955 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
959 * SYN appears to be valid; create compressed TCP state
960 * for syncache, or perform t/tcp connection.
962 if (so->so_qlen <= so->so_qlimit) {
964 if (so->so_options & SO_DEBUG)
965 tcp_trace(TA_INPUT, ostate, tp,
966 (void *)tcp_saveipgen, &tcp_savetcp, 0);
968 tcp_dooptions(&to, optp, optlen, 1);
969 if (!syncache_add(&inc, &to, th, &so, m))
973 * Entry added to syncache, mbuf used to
974 * send SYN,ACK packet.
976 KASSERT(headlocked, ("headlocked"));
978 INP_INFO_WUNLOCK(&tcbinfo);
982 * Segment passed TAO tests.
989 tp->t_starttime = ticks;
990 tp->t_state = TCPS_ESTABLISHED;
994 * If there is a FIN or if there is data, then
995 * delay SYN,ACK(SYN) in the hope of piggy-backing
996 * it on a response segment. Otherwise must send
997 * ACK now in case the other side is slow starting.
999 if (thflags & TH_FIN || tlen != 0)
1000 tp->t_flags |= (TF_DELACK | TF_NEEDSYN);
1002 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
1003 tcpstat.tcps_connects++;
1010 KASSERT(headlocked, ("tcp_input: after_listen: head not locked"));
1011 INP_LOCK_ASSERT(inp);
1013 /* XXX temp debugging */
1014 /* should not happen - syncache should pick up these connections */
1015 if (tp->t_state == TCPS_LISTEN)
1016 panic("tcp_input: TCPS_LISTEN");
1019 * This is the second part of the MSS DoS prevention code (after
1020 * minmss on the sending side) and it deals with too many too small
1021 * tcp packets in a too short timeframe (1 second).
1023 * For every full second we count the number of received packets
1024 * and bytes. If we get a lot of packets per second for this connection
1025 * (tcp_minmssoverload) we take a closer look at it and compute the
1026 * average packet size for the past second. If that is less than
1027 * tcp_minmss we get too many packets with very small payload which
1028 * is not good and burdens our system (and every packet generates
1029 * a wakeup to the process connected to our socket). We can reasonable
1030 * expect this to be small packet DoS attack to exhaust our CPU
1033 * Care has to be taken for the minimum packet overload value. This
1034 * value defines the minimum number of packets per second before we
1035 * start to worry. This must not be too low to avoid killing for
1036 * example interactive connections with many small packets like
1039 * Setting either tcp_minmssoverload or tcp_minmss to "0" disables
1042 * Account for packet if payload packet, skip over ACK, etc.
1044 if (tcp_minmss && tcp_minmssoverload &&
1045 tp->t_state == TCPS_ESTABLISHED && tlen > 0) {
1046 if ((unsigned int)(tp->rcv_second - ticks) < hz) {
1048 tp->rcv_byps += tlen + off;
1049 if (tp->rcv_pps > tcp_minmssoverload) {
1050 if ((tp->rcv_byps / tp->rcv_pps) < tcp_minmss) {
1051 printf("too many small tcp packets from "
1052 "%s:%u, av. %lubyte/packet, "
1053 "dropping connection\n",
1056 ip6_sprintf(&inp->inp_inc.inc6_faddr) :
1058 inet_ntoa(inp->inp_inc.inc_faddr),
1059 inp->inp_inc.inc_fport,
1060 tp->rcv_byps / tp->rcv_pps);
1061 KASSERT(headlocked, ("tcp_input: "
1062 "after_listen: tcp_drop: head "
1064 tp = tcp_drop(tp, ECONNRESET);
1065 tcpstat.tcps_minmssdrops++;
1070 tp->rcv_second = ticks + hz;
1072 tp->rcv_byps = tlen + off;
1077 * Segment received on connection.
1078 * Reset idle time and keep-alive timer.
1080 tp->t_rcvtime = ticks;
1081 if (TCPS_HAVEESTABLISHED(tp->t_state))
1082 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
1085 * Process options only when we get SYN/ACK back. The SYN case
1086 * for incoming connections is handled in tcp_syncache.
1087 * XXX this is traditional behavior, may need to be cleaned up.
1089 tcp_dooptions(&to, optp, optlen, thflags & TH_SYN);
1090 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1091 if (to.to_flags & TOF_SCALE) {
1092 tp->t_flags |= TF_RCVD_SCALE;
1093 tp->requested_s_scale = to.to_requested_s_scale;
1095 if (to.to_flags & TOF_TS) {
1096 tp->t_flags |= TF_RCVD_TSTMP;
1097 tp->ts_recent = to.to_tsval;
1098 tp->ts_recent_age = ticks;
1100 if (to.to_flags & TOF_MSS)
1101 tcp_mss(tp, to.to_mss);
1102 if (tp->sack_enable) {
1103 if (!(to.to_flags & TOF_SACK))
1104 tp->sack_enable = 0;
1106 tp->t_flags |= TF_SACK_PERMIT;
1112 * Header prediction: check for the two common cases
1113 * of a uni-directional data xfer. If the packet has
1114 * no control flags, is in-sequence, the window didn't
1115 * change and we're not retransmitting, it's a
1116 * candidate. If the length is zero and the ack moved
1117 * forward, we're the sender side of the xfer. Just
1118 * free the data acked & wake any higher level process
1119 * that was blocked waiting for space. If the length
1120 * is non-zero and the ack didn't move, we're the
1121 * receiver side. If we're getting packets in-order
1122 * (the reassembly queue is empty), add the data to
1123 * the socket buffer and note that we need a delayed ack.
1124 * Make sure that the hidden state-flags are also off.
1125 * Since we check for TCPS_ESTABLISHED above, it can only
1128 if (tp->t_state == TCPS_ESTABLISHED &&
1129 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1130 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1131 ((to.to_flags & TOF_TS) == 0 ||
1132 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
1133 th->th_seq == tp->rcv_nxt && tiwin && tiwin == tp->snd_wnd &&
1134 tp->snd_nxt == tp->snd_max) {
1137 * If last ACK falls within this segment's sequence numbers,
1138 * record the timestamp.
1139 * NOTE that the test is modified according to the latest
1140 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1142 if ((to.to_flags & TOF_TS) != 0 &&
1143 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1144 tp->ts_recent_age = ticks;
1145 tp->ts_recent = to.to_tsval;
1149 if (SEQ_GT(th->th_ack, tp->snd_una) &&
1150 SEQ_LEQ(th->th_ack, tp->snd_max) &&
1151 tp->snd_cwnd >= tp->snd_wnd &&
1152 ((!tcp_do_newreno && !tp->sack_enable &&
1153 tp->t_dupacks < tcprexmtthresh) ||
1154 ((tcp_do_newreno || tp->sack_enable) &&
1155 !IN_FASTRECOVERY(tp) && to.to_nsacks == 0 &&
1156 TAILQ_EMPTY(&tp->snd_holes)))) {
1157 KASSERT(headlocked, ("headlocked"));
1158 INP_INFO_WUNLOCK(&tcbinfo);
1161 * this is a pure ack for outstanding data.
1163 ++tcpstat.tcps_predack;
1165 * "bad retransmit" recovery
1167 if (tp->t_rxtshift == 1 &&
1168 ticks < tp->t_badrxtwin) {
1169 ++tcpstat.tcps_sndrexmitbad;
1170 tp->snd_cwnd = tp->snd_cwnd_prev;
1172 tp->snd_ssthresh_prev;
1173 tp->snd_recover = tp->snd_recover_prev;
1174 if (tp->t_flags & TF_WASFRECOVERY)
1175 ENTER_FASTRECOVERY(tp);
1176 tp->snd_nxt = tp->snd_max;
1177 tp->t_badrxtwin = 0;
1181 * Recalculate the transmit timer / rtt.
1183 * Some boxes send broken timestamp replies
1184 * during the SYN+ACK phase, ignore
1185 * timestamps of 0 or we could calculate a
1186 * huge RTT and blow up the retransmit timer.
1188 if ((to.to_flags & TOF_TS) != 0 &&
1191 ticks - to.to_tsecr + 1);
1192 } else if (tp->t_rtttime &&
1193 SEQ_GT(th->th_ack, tp->t_rtseq)) {
1195 ticks - tp->t_rtttime);
1197 tcp_xmit_bandwidth_limit(tp, th->th_ack);
1198 acked = th->th_ack - tp->snd_una;
1199 tcpstat.tcps_rcvackpack++;
1200 tcpstat.tcps_rcvackbyte += acked;
1201 sbdrop(&so->so_snd, acked);
1202 if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
1203 SEQ_LEQ(th->th_ack, tp->snd_recover))
1204 tp->snd_recover = th->th_ack - 1;
1205 tp->snd_una = th->th_ack;
1207 * pull snd_wl2 up to prevent seq wrap relative
1210 tp->snd_wl2 = th->th_ack;
1213 ND6_HINT(tp); /* some progress has been done */
1216 * If all outstanding data are acked, stop
1217 * retransmit timer, otherwise restart timer
1218 * using current (possibly backed-off) value.
1219 * If process is waiting for space,
1220 * wakeup/selwakeup/signal. If data
1221 * are ready to send, let tcp_output
1222 * decide between more output or persist.
1225 if (so->so_options & SO_DEBUG)
1226 tcp_trace(TA_INPUT, ostate, tp,
1227 (void *)tcp_saveipgen,
1231 if (tp->snd_una == tp->snd_max)
1232 callout_stop(tp->tt_rexmt);
1233 else if (!callout_active(tp->tt_persist))
1234 callout_reset(tp->tt_rexmt,
1236 tcp_timer_rexmt, tp);
1239 if (so->so_snd.sb_cc)
1240 (void) tcp_output(tp);
1243 } else if (th->th_ack == tp->snd_una &&
1244 LIST_EMPTY(&tp->t_segq) &&
1245 tlen <= sbspace(&so->so_rcv)) {
1246 KASSERT(headlocked, ("headlocked"));
1247 INP_INFO_WUNLOCK(&tcbinfo);
1250 * this is a pure, in-sequence data packet
1251 * with nothing on the reassembly queue and
1252 * we have enough buffer space to take it.
1254 /* Clean receiver SACK report if present */
1255 if (tp->sack_enable && tp->rcv_numsacks)
1256 tcp_clean_sackreport(tp);
1257 ++tcpstat.tcps_preddat;
1258 tp->rcv_nxt += tlen;
1260 * Pull snd_wl1 up to prevent seq wrap relative to
1263 tp->snd_wl1 = th->th_seq;
1265 * Pull rcv_up up to prevent seq wrap relative to
1268 tp->rcv_up = tp->rcv_nxt;
1269 tcpstat.tcps_rcvpack++;
1270 tcpstat.tcps_rcvbyte += tlen;
1271 ND6_HINT(tp); /* some progress has been done */
1274 if (so->so_options & SO_DEBUG)
1275 tcp_trace(TA_INPUT, ostate, tp,
1276 (void *)tcp_saveipgen, &tcp_savetcp, 0);
1278 * Add data to socket buffer.
1280 SOCKBUF_LOCK(&so->so_rcv);
1281 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1284 m_adj(m, drop_hdrlen); /* delayed header drop */
1285 sbappendstream_locked(&so->so_rcv, m);
1287 sorwakeup_locked(so);
1288 if (DELAY_ACK(tp)) {
1289 tp->t_flags |= TF_DELACK;
1291 tp->t_flags |= TF_ACKNOW;
1299 * Calculate amount of space in receive window,
1300 * and then do TCP input processing.
1301 * Receive window is amount of space in rcv queue,
1302 * but not less than advertised window.
1306 win = sbspace(&so->so_rcv);
1309 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1312 switch (tp->t_state) {
1315 * If the state is SYN_RECEIVED:
1316 * if seg contains an ACK, but not for our SYN/ACK, send a RST.
1318 case TCPS_SYN_RECEIVED:
1319 if ((thflags & TH_ACK) &&
1320 (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1321 SEQ_GT(th->th_ack, tp->snd_max))) {
1322 rstreason = BANDLIM_RST_OPENPORT;
1328 * If the state is SYN_SENT:
1329 * if seg contains an ACK, but not for our SYN, drop the input.
1330 * if seg contains a RST, then drop the connection.
1331 * if seg does not contain SYN, then drop it.
1332 * Otherwise this is an acceptable SYN segment
1333 * initialize tp->rcv_nxt and tp->irs
1334 * if seg contains ack then advance tp->snd_una
1335 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1336 * arrange for segment to be acked (eventually)
1337 * continue processing rest of data/controls, beginning with URG
1340 if ((thflags & TH_ACK) &&
1341 (SEQ_LEQ(th->th_ack, tp->iss) ||
1342 SEQ_GT(th->th_ack, tp->snd_max))) {
1343 rstreason = BANDLIM_UNLIMITED;
1346 if (thflags & TH_RST) {
1347 if (thflags & TH_ACK) {
1348 KASSERT(headlocked, ("tcp_input: after_listen"
1349 ": tcp_drop.2: head not locked"));
1350 tp = tcp_drop(tp, ECONNREFUSED);
1354 if ((thflags & TH_SYN) == 0)
1356 tp->snd_wnd = th->th_win; /* initial send window */
1358 tp->irs = th->th_seq;
1360 if (thflags & TH_ACK) {
1361 tcpstat.tcps_connects++;
1365 mac_set_socket_peer_from_mbuf(m, so);
1368 /* Do window scaling on this connection? */
1369 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1370 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1371 tp->snd_scale = tp->requested_s_scale;
1372 tp->rcv_scale = tp->request_r_scale;
1374 tp->rcv_adv += tp->rcv_wnd;
1375 tp->snd_una++; /* SYN is acked */
1377 * If there's data, delay ACK; if there's also a FIN
1378 * ACKNOW will be turned on later.
1380 if (DELAY_ACK(tp) && tlen != 0)
1381 callout_reset(tp->tt_delack, tcp_delacktime,
1382 tcp_timer_delack, tp);
1384 tp->t_flags |= TF_ACKNOW;
1386 * Received <SYN,ACK> in SYN_SENT[*] state.
1388 * SYN_SENT --> ESTABLISHED
1389 * SYN_SENT* --> FIN_WAIT_1
1391 tp->t_starttime = ticks;
1392 if (tp->t_flags & TF_NEEDFIN) {
1393 tp->t_state = TCPS_FIN_WAIT_1;
1394 tp->t_flags &= ~TF_NEEDFIN;
1397 tp->t_state = TCPS_ESTABLISHED;
1398 callout_reset(tp->tt_keep, tcp_keepidle,
1399 tcp_timer_keep, tp);
1403 * Received initial SYN in SYN-SENT[*] state =>
1404 * simultaneous open. If segment contains CC option
1405 * and there is a cached CC, apply TAO test.
1406 * If it succeeds, connection is * half-synchronized.
1407 * Otherwise, do 3-way handshake:
1408 * SYN-SENT -> SYN-RECEIVED
1409 * SYN-SENT* -> SYN-RECEIVED*
1410 * If there was no CC option, clear cached CC value.
1412 tp->t_flags |= TF_ACKNOW;
1413 callout_stop(tp->tt_rexmt);
1414 tp->t_state = TCPS_SYN_RECEIVED;
1418 KASSERT(headlocked, ("tcp_input: trimthenstep6: head not "
1420 INP_LOCK_ASSERT(inp);
1423 * Advance th->th_seq to correspond to first data byte.
1424 * If data, trim to stay within window,
1425 * dropping FIN if necessary.
1428 if (tlen > tp->rcv_wnd) {
1429 todrop = tlen - tp->rcv_wnd;
1433 tcpstat.tcps_rcvpackafterwin++;
1434 tcpstat.tcps_rcvbyteafterwin += todrop;
1436 tp->snd_wl1 = th->th_seq - 1;
1437 tp->rcv_up = th->th_seq;
1439 * Client side of transaction: already sent SYN and data.
1440 * If the remote host used T/TCP to validate the SYN,
1441 * our data will be ACK'd; if so, enter normal data segment
1442 * processing in the middle of step 5, ack processing.
1443 * Otherwise, goto step 6.
1445 if (thflags & TH_ACK)
1451 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1452 * do normal processing.
1454 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later.
1458 case TCPS_TIME_WAIT:
1459 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
1460 break; /* continue normal processing */
1464 * States other than LISTEN or SYN_SENT.
1465 * First check the RST flag and sequence number since reset segments
1466 * are exempt from the timestamp and connection count tests. This
1467 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
1468 * below which allowed reset segments in half the sequence space
1469 * to fall though and be processed (which gives forged reset
1470 * segments with a random sequence number a 50 percent chance of
1471 * killing a connection).
1472 * Then check timestamp, if present.
1473 * Then check the connection count, if present.
1474 * Then check that at least some bytes of segment are within
1475 * receive window. If segment begins before rcv_nxt,
1476 * drop leading data (and SYN); if nothing left, just ack.
1479 * If the RST bit is set, check the sequence number to see
1480 * if this is a valid reset segment.
1482 * In all states except SYN-SENT, all reset (RST) segments
1483 * are validated by checking their SEQ-fields. A reset is
1484 * valid if its sequence number is in the window.
1485 * Note: this does not take into account delayed ACKs, so
1486 * we should test against last_ack_sent instead of rcv_nxt.
1487 * The sequence number in the reset segment is normally an
1488 * echo of our outgoing acknowlegement numbers, but some hosts
1489 * send a reset with the sequence number at the rightmost edge
1490 * of our receive window, and we have to handle this case.
1491 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
1492 * that brute force RST attacks are possible. To combat this,
1493 * we use a much stricter check while in the ESTABLISHED state,
1494 * only accepting RSTs where the sequence number is equal to
1495 * last_ack_sent. In all other states (the states in which a
1496 * RST is more likely), the more permissive check is used.
1497 * If we have multiple segments in flight, the intial reset
1498 * segment sequence numbers will be to the left of last_ack_sent,
1499 * but they will eventually catch up.
1500 * In any case, it never made sense to trim reset segments to
1501 * fit the receive window since RFC 1122 says:
1502 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
1504 * A TCP SHOULD allow a received RST segment to include data.
1507 * It has been suggested that a RST segment could contain
1508 * ASCII text that encoded and explained the cause of the
1509 * RST. No standard has yet been established for such
1512 * If the reset segment passes the sequence number test examine
1514 * SYN_RECEIVED STATE:
1515 * If passive open, return to LISTEN state.
1516 * If active open, inform user that connection was refused.
1517 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
1518 * Inform user that connection was reset, and close tcb.
1519 * CLOSING, LAST_ACK STATES:
1522 * Drop the segment - see Stevens, vol. 2, p. 964 and
1525 if (thflags & TH_RST) {
1526 if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
1527 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
1528 (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
1529 switch (tp->t_state) {
1531 case TCPS_SYN_RECEIVED:
1532 so->so_error = ECONNREFUSED;
1535 case TCPS_ESTABLISHED:
1536 if (tp->last_ack_sent != th->th_seq &&
1537 tcp_insecure_rst == 0) {
1538 tcpstat.tcps_badrst++;
1541 case TCPS_FIN_WAIT_1:
1542 case TCPS_FIN_WAIT_2:
1543 case TCPS_CLOSE_WAIT:
1544 so->so_error = ECONNRESET;
1546 tp->t_state = TCPS_CLOSED;
1547 tcpstat.tcps_drops++;
1548 KASSERT(headlocked, ("tcp_input: "
1549 "trimthenstep6: tcp_close: head not "
1556 KASSERT(headlocked, ("trimthenstep6: "
1557 "tcp_close.2: head not locked"));
1561 case TCPS_TIME_WAIT:
1562 KASSERT(tp->t_state != TCPS_TIME_WAIT,
1571 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1572 * and it's less than ts_recent, drop it.
1574 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
1575 TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1577 /* Check to see if ts_recent is over 24 days old. */
1578 if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1580 * Invalidate ts_recent. If this segment updates
1581 * ts_recent, the age will be reset later and ts_recent
1582 * will get a valid value. If it does not, setting
1583 * ts_recent to zero will at least satisfy the
1584 * requirement that zero be placed in the timestamp
1585 * echo reply when ts_recent isn't valid. The
1586 * age isn't reset until we get a valid ts_recent
1587 * because we don't want out-of-order segments to be
1588 * dropped when ts_recent is old.
1592 tcpstat.tcps_rcvduppack++;
1593 tcpstat.tcps_rcvdupbyte += tlen;
1594 tcpstat.tcps_pawsdrop++;
1602 * In the SYN-RECEIVED state, validate that the packet belongs to
1603 * this connection before trimming the data to fit the receive
1604 * window. Check the sequence number versus IRS since we know
1605 * the sequence numbers haven't wrapped. This is a partial fix
1606 * for the "LAND" DoS attack.
1608 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1609 rstreason = BANDLIM_RST_OPENPORT;
1613 todrop = tp->rcv_nxt - th->th_seq;
1615 if (thflags & TH_SYN) {
1625 * Following if statement from Stevens, vol. 2, p. 960.
1628 || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1630 * Any valid FIN must be to the left of the window.
1631 * At this point the FIN must be a duplicate or out
1632 * of sequence; drop it.
1637 * Send an ACK to resynchronize and drop any data.
1638 * But keep on processing for RST or ACK.
1640 tp->t_flags |= TF_ACKNOW;
1642 tcpstat.tcps_rcvduppack++;
1643 tcpstat.tcps_rcvdupbyte += todrop;
1645 tcpstat.tcps_rcvpartduppack++;
1646 tcpstat.tcps_rcvpartdupbyte += todrop;
1648 drop_hdrlen += todrop; /* drop from the top afterwards */
1649 th->th_seq += todrop;
1651 if (th->th_urp > todrop)
1652 th->th_urp -= todrop;
1660 * If new data are received on a connection after the
1661 * user processes are gone, then RST the other end.
1663 if ((so->so_state & SS_NOFDREF) &&
1664 tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1665 KASSERT(headlocked, ("trimthenstep6: tcp_close.3: head not "
1668 tcpstat.tcps_rcvafterclose++;
1669 rstreason = BANDLIM_UNLIMITED;
1674 * If segment ends after window, drop trailing data
1675 * (and PUSH and FIN); if nothing left, just ACK.
1677 todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1679 tcpstat.tcps_rcvpackafterwin++;
1680 if (todrop >= tlen) {
1681 tcpstat.tcps_rcvbyteafterwin += tlen;
1683 * If a new connection request is received
1684 * while in TIME_WAIT, drop the old connection
1685 * and start over if the sequence numbers
1686 * are above the previous ones.
1688 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
1689 if (thflags & TH_SYN &&
1690 tp->t_state == TCPS_TIME_WAIT &&
1691 SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1692 KASSERT(headlocked, ("trimthenstep6: "
1693 "tcp_close.4: head not locked"));
1698 * If window is closed can only take segments at
1699 * window edge, and have to drop data and PUSH from
1700 * incoming segments. Continue processing, but
1701 * remember to ack. Otherwise, drop segment
1704 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1705 tp->t_flags |= TF_ACKNOW;
1706 tcpstat.tcps_rcvwinprobe++;
1710 tcpstat.tcps_rcvbyteafterwin += todrop;
1713 thflags &= ~(TH_PUSH|TH_FIN);
1717 * If last ACK falls within this segment's sequence numbers,
1718 * record its timestamp.
1720 * 1) That the test incorporates suggestions from the latest
1721 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1722 * 2) That updating only on newer timestamps interferes with
1723 * our earlier PAWS tests, so this check should be solely
1724 * predicated on the sequence space of this segment.
1725 * 3) That we modify the segment boundary check to be
1726 * Last.ACK.Sent <= SEG.SEQ + SEG.Len
1727 * instead of RFC1323's
1728 * Last.ACK.Sent < SEG.SEQ + SEG.Len,
1729 * This modified check allows us to overcome RFC1323's
1730 * limitations as described in Stevens TCP/IP Illustrated
1731 * Vol. 2 p.869. In such cases, we can still calculate the
1732 * RTT correctly when RCV.NXT == Last.ACK.Sent.
1734 if ((to.to_flags & TOF_TS) != 0 &&
1735 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
1736 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
1737 ((thflags & (TH_SYN|TH_FIN)) != 0))) {
1738 tp->ts_recent_age = ticks;
1739 tp->ts_recent = to.to_tsval;
1743 * If a SYN is in the window, then this is an
1744 * error and we send an RST and drop the connection.
1746 if (thflags & TH_SYN) {
1747 KASSERT(headlocked, ("tcp_input: tcp_drop: trimthenstep6: "
1748 "head not locked"));
1749 tp = tcp_drop(tp, ECONNRESET);
1750 rstreason = BANDLIM_UNLIMITED;
1755 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
1756 * flag is on (half-synchronized state), then queue data for
1757 * later processing; else drop segment and return.
1759 if ((thflags & TH_ACK) == 0) {
1760 if (tp->t_state == TCPS_SYN_RECEIVED ||
1761 (tp->t_flags & TF_NEEDSYN))
1770 switch (tp->t_state) {
1773 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1774 * ESTABLISHED state and continue processing.
1775 * The ACK was checked above.
1777 case TCPS_SYN_RECEIVED:
1779 tcpstat.tcps_connects++;
1781 /* Do window scaling? */
1782 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1783 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1784 tp->snd_scale = tp->requested_s_scale;
1785 tp->rcv_scale = tp->request_r_scale;
1789 * SYN-RECEIVED -> ESTABLISHED
1790 * SYN-RECEIVED* -> FIN-WAIT-1
1792 tp->t_starttime = ticks;
1793 if (tp->t_flags & TF_NEEDFIN) {
1794 tp->t_state = TCPS_FIN_WAIT_1;
1795 tp->t_flags &= ~TF_NEEDFIN;
1797 tp->t_state = TCPS_ESTABLISHED;
1798 callout_reset(tp->tt_keep, tcp_keepidle,
1799 tcp_timer_keep, tp);
1802 * If segment contains data or ACK, will call tcp_reass()
1803 * later; if not, do so now to pass queued data to user.
1805 if (tlen == 0 && (thflags & TH_FIN) == 0)
1806 (void) tcp_reass(tp, (struct tcphdr *)0, 0,
1808 tp->snd_wl1 = th->th_seq - 1;
1812 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1813 * ACKs. If the ack is in the range
1814 * tp->snd_una < th->th_ack <= tp->snd_max
1815 * then advance tp->snd_una to th->th_ack and drop
1816 * data from the retransmission queue. If this ACK reflects
1817 * more up to date window information we update our window information.
1819 case TCPS_ESTABLISHED:
1820 case TCPS_FIN_WAIT_1:
1821 case TCPS_FIN_WAIT_2:
1822 case TCPS_CLOSE_WAIT:
1825 case TCPS_TIME_WAIT:
1826 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
1827 if (SEQ_GT(th->th_ack, tp->snd_max)) {
1828 tcpstat.tcps_rcvacktoomuch++;
1831 if (tp->sack_enable &&
1832 (to.to_nsacks > 0 || !TAILQ_EMPTY(&tp->snd_holes)))
1833 tcp_sack_doack(tp, &to, th->th_ack);
1834 if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1835 if (tlen == 0 && tiwin == tp->snd_wnd) {
1836 tcpstat.tcps_rcvdupack++;
1838 * If we have outstanding data (other than
1839 * a window probe), this is a completely
1840 * duplicate ack (ie, window info didn't
1841 * change), the ack is the biggest we've
1842 * seen and we've seen exactly our rexmt
1843 * threshhold of them, assume a packet
1844 * has been dropped and retransmit it.
1845 * Kludge snd_nxt & the congestion
1846 * window so we send only this one
1849 * We know we're losing at the current
1850 * window size so do congestion avoidance
1851 * (set ssthresh to half the current window
1852 * and pull our congestion window back to
1853 * the new ssthresh).
1855 * Dup acks mean that packets have left the
1856 * network (they're now cached at the receiver)
1857 * so bump cwnd by the amount in the receiver
1858 * to keep a constant cwnd packets in the
1861 if (!callout_active(tp->tt_rexmt) ||
1862 th->th_ack != tp->snd_una)
1864 else if (++tp->t_dupacks > tcprexmtthresh ||
1865 ((tcp_do_newreno || tp->sack_enable) &&
1866 IN_FASTRECOVERY(tp))) {
1867 if (tp->sack_enable && IN_FASTRECOVERY(tp)) {
1871 * Compute the amount of data in flight first.
1872 * We can inject new data into the pipe iff
1873 * we have less than 1/2 the original window's
1874 * worth of data in flight.
1876 awnd = (tp->snd_nxt - tp->snd_fack) +
1877 tp->sackhint.sack_bytes_rexmit;
1878 if (awnd < tp->snd_ssthresh) {
1879 tp->snd_cwnd += tp->t_maxseg;
1880 if (tp->snd_cwnd > tp->snd_ssthresh)
1881 tp->snd_cwnd = tp->snd_ssthresh;
1884 tp->snd_cwnd += tp->t_maxseg;
1885 (void) tcp_output(tp);
1887 } else if (tp->t_dupacks == tcprexmtthresh) {
1888 tcp_seq onxt = tp->snd_nxt;
1892 * If we're doing sack, check to
1893 * see if we're already in sack
1894 * recovery. If we're not doing sack,
1895 * check to see if we're in newreno
1898 if (tp->sack_enable) {
1899 if (IN_FASTRECOVERY(tp)) {
1903 } else if (tcp_do_newreno) {
1904 if (SEQ_LEQ(th->th_ack,
1910 win = min(tp->snd_wnd, tp->snd_cwnd) /
1914 tp->snd_ssthresh = win * tp->t_maxseg;
1915 ENTER_FASTRECOVERY(tp);
1916 tp->snd_recover = tp->snd_max;
1917 callout_stop(tp->tt_rexmt);
1919 if (tp->sack_enable) {
1920 tcpstat.tcps_sack_recovery_episode++;
1921 tp->sack_newdata = tp->snd_nxt;
1922 tp->snd_cwnd = tp->t_maxseg;
1923 (void) tcp_output(tp);
1926 tp->snd_nxt = th->th_ack;
1927 tp->snd_cwnd = tp->t_maxseg;
1928 (void) tcp_output(tp);
1929 KASSERT(tp->snd_limited <= 2,
1930 ("tp->snd_limited too big"));
1931 tp->snd_cwnd = tp->snd_ssthresh +
1933 (tp->t_dupacks - tp->snd_limited);
1934 if (SEQ_GT(onxt, tp->snd_nxt))
1937 } else if (tcp_do_rfc3042) {
1938 u_long oldcwnd = tp->snd_cwnd;
1939 tcp_seq oldsndmax = tp->snd_max;
1942 KASSERT(tp->t_dupacks == 1 ||
1944 ("dupacks not 1 or 2"));
1945 if (tp->t_dupacks == 1)
1946 tp->snd_limited = 0;
1948 (tp->snd_nxt - tp->snd_una) +
1949 (tp->t_dupacks - tp->snd_limited) *
1951 (void) tcp_output(tp);
1952 sent = tp->snd_max - oldsndmax;
1953 if (sent > tp->t_maxseg) {
1954 KASSERT((tp->t_dupacks == 2 &&
1955 tp->snd_limited == 0) ||
1956 (sent == tp->t_maxseg + 1 &&
1957 tp->t_flags & TF_SENTFIN),
1959 tp->snd_limited = 2;
1960 } else if (sent > 0)
1962 tp->snd_cwnd = oldcwnd;
1970 KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una"));
1973 * If the congestion window was inflated to account
1974 * for the other side's cached packets, retract it.
1976 if (tcp_do_newreno || tp->sack_enable) {
1977 if (IN_FASTRECOVERY(tp)) {
1978 if (SEQ_LT(th->th_ack, tp->snd_recover)) {
1979 if (tp->sack_enable)
1980 tcp_sack_partialack(tp, th);
1982 tcp_newreno_partial_ack(tp, th);
1985 * Out of fast recovery.
1986 * Window inflation should have left us
1987 * with approximately snd_ssthresh
1989 * But in case we would be inclined to
1990 * send a burst, better to do it via
1991 * the slow start mechanism.
1993 if (SEQ_GT(th->th_ack +
1996 tp->snd_cwnd = tp->snd_max -
2000 tp->snd_cwnd = tp->snd_ssthresh;
2004 if (tp->t_dupacks >= tcprexmtthresh &&
2005 tp->snd_cwnd > tp->snd_ssthresh)
2006 tp->snd_cwnd = tp->snd_ssthresh;
2010 * If we reach this point, ACK is not a duplicate,
2011 * i.e., it ACKs something we sent.
2013 if (tp->t_flags & TF_NEEDSYN) {
2015 * T/TCP: Connection was half-synchronized, and our
2016 * SYN has been ACK'd (so connection is now fully
2017 * synchronized). Go to non-starred state,
2018 * increment snd_una for ACK of SYN, and check if
2019 * we can do window scaling.
2021 tp->t_flags &= ~TF_NEEDSYN;
2023 /* Do window scaling? */
2024 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2025 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
2026 tp->snd_scale = tp->requested_s_scale;
2027 tp->rcv_scale = tp->request_r_scale;
2032 KASSERT(headlocked, ("tcp_input: process_ACK: head not "
2034 INP_LOCK_ASSERT(inp);
2036 acked = th->th_ack - tp->snd_una;
2037 tcpstat.tcps_rcvackpack++;
2038 tcpstat.tcps_rcvackbyte += acked;
2041 * If we just performed our first retransmit, and the ACK
2042 * arrives within our recovery window, then it was a mistake
2043 * to do the retransmit in the first place. Recover our
2044 * original cwnd and ssthresh, and proceed to transmit where
2047 if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
2048 ++tcpstat.tcps_sndrexmitbad;
2049 tp->snd_cwnd = tp->snd_cwnd_prev;
2050 tp->snd_ssthresh = tp->snd_ssthresh_prev;
2051 tp->snd_recover = tp->snd_recover_prev;
2052 if (tp->t_flags & TF_WASFRECOVERY)
2053 ENTER_FASTRECOVERY(tp);
2054 tp->snd_nxt = tp->snd_max;
2055 tp->t_badrxtwin = 0; /* XXX probably not required */
2059 * If we have a timestamp reply, update smoothed
2060 * round trip time. If no timestamp is present but
2061 * transmit timer is running and timed sequence
2062 * number was acked, update smoothed round trip time.
2063 * Since we now have an rtt measurement, cancel the
2064 * timer backoff (cf., Phil Karn's retransmit alg.).
2065 * Recompute the initial retransmit timer.
2067 * Some boxes send broken timestamp replies
2068 * during the SYN+ACK phase, ignore
2069 * timestamps of 0 or we could calculate a
2070 * huge RTT and blow up the retransmit timer.
2072 if ((to.to_flags & TOF_TS) != 0 &&
2074 tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
2075 } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2076 tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2078 tcp_xmit_bandwidth_limit(tp, th->th_ack);
2081 * If all outstanding data is acked, stop retransmit
2082 * timer and remember to restart (more output or persist).
2083 * If there is more data to be acked, restart retransmit
2084 * timer, using current (possibly backed-off) value.
2086 if (th->th_ack == tp->snd_max) {
2087 callout_stop(tp->tt_rexmt);
2089 } else if (!callout_active(tp->tt_persist))
2090 callout_reset(tp->tt_rexmt, tp->t_rxtcur,
2091 tcp_timer_rexmt, tp);
2094 * If no data (only SYN) was ACK'd,
2095 * skip rest of ACK processing.
2101 * When new data is acked, open the congestion window.
2102 * If the window gives us less than ssthresh packets
2103 * in flight, open exponentially (maxseg per packet).
2104 * Otherwise open linearly: maxseg per window
2105 * (maxseg^2 / cwnd per packet).
2107 if ((!tcp_do_newreno && !tp->sack_enable) ||
2108 !IN_FASTRECOVERY(tp)) {
2109 register u_int cw = tp->snd_cwnd;
2110 register u_int incr = tp->t_maxseg;
2111 if (cw > tp->snd_ssthresh)
2112 incr = incr * incr / cw;
2113 tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale);
2115 SOCKBUF_LOCK(&so->so_snd);
2116 if (acked > so->so_snd.sb_cc) {
2117 tp->snd_wnd -= so->so_snd.sb_cc;
2118 sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc);
2121 sbdrop_locked(&so->so_snd, acked);
2122 tp->snd_wnd -= acked;
2125 sowwakeup_locked(so);
2126 /* detect una wraparound */
2127 if ((tcp_do_newreno || tp->sack_enable) &&
2128 !IN_FASTRECOVERY(tp) &&
2129 SEQ_GT(tp->snd_una, tp->snd_recover) &&
2130 SEQ_LEQ(th->th_ack, tp->snd_recover))
2131 tp->snd_recover = th->th_ack - 1;
2132 if ((tcp_do_newreno || tp->sack_enable) &&
2133 IN_FASTRECOVERY(tp) &&
2134 SEQ_GEQ(th->th_ack, tp->snd_recover))
2135 EXIT_FASTRECOVERY(tp);
2136 tp->snd_una = th->th_ack;
2137 if (tp->sack_enable) {
2138 if (SEQ_GT(tp->snd_una, tp->snd_recover))
2139 tp->snd_recover = tp->snd_una;
2141 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2142 tp->snd_nxt = tp->snd_una;
2144 switch (tp->t_state) {
2147 * In FIN_WAIT_1 STATE in addition to the processing
2148 * for the ESTABLISHED state if our FIN is now acknowledged
2149 * then enter FIN_WAIT_2.
2151 case TCPS_FIN_WAIT_1:
2152 if (ourfinisacked) {
2154 * If we can't receive any more
2155 * data, then closing user can proceed.
2156 * Starting the timer is contrary to the
2157 * specification, but if we don't get a FIN
2158 * we'll hang forever.
2161 * we should release the tp also, and use a
2164 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
2165 soisdisconnected(so);
2166 callout_reset(tp->tt_2msl, tcp_maxidle,
2167 tcp_timer_2msl, tp);
2169 tp->t_state = TCPS_FIN_WAIT_2;
2174 * In CLOSING STATE in addition to the processing for
2175 * the ESTABLISHED state if the ACK acknowledges our FIN
2176 * then enter the TIME-WAIT state, otherwise ignore
2180 if (ourfinisacked) {
2181 KASSERT(headlocked, ("tcp_input: process_ACK: "
2182 "head not locked"));
2184 INP_INFO_WUNLOCK(&tcbinfo);
2191 * In LAST_ACK, we may still be waiting for data to drain
2192 * and/or to be acked, as well as for the ack of our FIN.
2193 * If our FIN is now acknowledged, delete the TCB,
2194 * enter the closed state and return.
2197 if (ourfinisacked) {
2198 KASSERT(headlocked, ("tcp_input: process_ACK:"
2199 " tcp_close: head not locked"));
2206 * In TIME_WAIT state the only thing that should arrive
2207 * is a retransmission of the remote FIN. Acknowledge
2208 * it and restart the finack timer.
2210 case TCPS_TIME_WAIT:
2211 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
2212 callout_reset(tp->tt_2msl, 2 * tcp_msl,
2213 tcp_timer_2msl, tp);
2219 KASSERT(headlocked, ("tcp_input: step6: head not locked"));
2220 INP_LOCK_ASSERT(inp);
2223 * Update window information.
2224 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2226 if ((thflags & TH_ACK) &&
2227 (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2228 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2229 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2230 /* keep track of pure window updates */
2232 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2233 tcpstat.tcps_rcvwinupd++;
2234 tp->snd_wnd = tiwin;
2235 tp->snd_wl1 = th->th_seq;
2236 tp->snd_wl2 = th->th_ack;
2237 if (tp->snd_wnd > tp->max_sndwnd)
2238 tp->max_sndwnd = tp->snd_wnd;
2243 * Process segments with URG.
2245 if ((thflags & TH_URG) && th->th_urp &&
2246 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2248 * This is a kludge, but if we receive and accept
2249 * random urgent pointers, we'll crash in
2250 * soreceive. It's hard to imagine someone
2251 * actually wanting to send this much urgent data.
2253 SOCKBUF_LOCK(&so->so_rcv);
2254 if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2255 th->th_urp = 0; /* XXX */
2256 thflags &= ~TH_URG; /* XXX */
2257 SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */
2258 goto dodata; /* XXX */
2261 * If this segment advances the known urgent pointer,
2262 * then mark the data stream. This should not happen
2263 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2264 * a FIN has been received from the remote side.
2265 * In these states we ignore the URG.
2267 * According to RFC961 (Assigned Protocols),
2268 * the urgent pointer points to the last octet
2269 * of urgent data. We continue, however,
2270 * to consider it to indicate the first octet
2271 * of data past the urgent section as the original
2272 * spec states (in one of two places).
2274 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2275 tp->rcv_up = th->th_seq + th->th_urp;
2276 so->so_oobmark = so->so_rcv.sb_cc +
2277 (tp->rcv_up - tp->rcv_nxt) - 1;
2278 if (so->so_oobmark == 0)
2279 so->so_rcv.sb_state |= SBS_RCVATMARK;
2281 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2283 SOCKBUF_UNLOCK(&so->so_rcv);
2285 * Remove out of band data so doesn't get presented to user.
2286 * This can happen independent of advancing the URG pointer,
2287 * but if two URG's are pending at once, some out-of-band
2288 * data may creep in... ick.
2290 if (th->th_urp <= (u_long)tlen &&
2291 !(so->so_options & SO_OOBINLINE)) {
2292 /* hdr drop is delayed */
2293 tcp_pulloutofband(so, th, m, drop_hdrlen);
2297 * If no out of band data is expected,
2298 * pull receive urgent pointer along
2299 * with the receive window.
2301 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2302 tp->rcv_up = tp->rcv_nxt;
2305 KASSERT(headlocked, ("tcp_input: dodata: head not locked"));
2306 INP_LOCK_ASSERT(inp);
2309 * Process the segment text, merging it into the TCP sequencing queue,
2310 * and arranging for acknowledgment of receipt if necessary.
2311 * This process logically involves adjusting tp->rcv_wnd as data
2312 * is presented to the user (this happens in tcp_usrreq.c,
2313 * case PRU_RCVD). If a FIN has already been received on this
2314 * connection then we just ignore the text.
2316 if ((tlen || (thflags & TH_FIN)) &&
2317 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2318 tcp_seq save_start = th->th_seq;
2319 tcp_seq save_end = th->th_seq + tlen;
2320 m_adj(m, drop_hdrlen); /* delayed header drop */
2322 * Insert segment which includes th into TCP reassembly queue
2323 * with control block tp. Set thflags to whether reassembly now
2324 * includes a segment with FIN. This handles the common case
2325 * inline (segment is the next to be received on an established
2326 * connection, and the queue is empty), avoiding linkage into
2327 * and removal from the queue and repetition of various
2329 * Set DELACK for segments received in order, but ack
2330 * immediately when segments are out of order (so
2331 * fast retransmit can work).
2333 if (th->th_seq == tp->rcv_nxt &&
2334 LIST_EMPTY(&tp->t_segq) &&
2335 TCPS_HAVEESTABLISHED(tp->t_state)) {
2337 tp->t_flags |= TF_DELACK;
2339 tp->t_flags |= TF_ACKNOW;
2340 tp->rcv_nxt += tlen;
2341 thflags = th->th_flags & TH_FIN;
2342 tcpstat.tcps_rcvpack++;
2343 tcpstat.tcps_rcvbyte += tlen;
2345 SOCKBUF_LOCK(&so->so_rcv);
2346 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
2349 sbappendstream_locked(&so->so_rcv, m);
2350 sorwakeup_locked(so);
2352 thflags = tcp_reass(tp, th, &tlen, m);
2353 tp->t_flags |= TF_ACKNOW;
2355 if (tlen > 0 && tp->sack_enable)
2356 tcp_update_sack_list(tp, save_start, save_end);
2358 * Note the amount of data that peer has sent into
2359 * our window, in order to estimate the sender's
2362 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2369 * If FIN is received ACK the FIN and let the user know
2370 * that the connection is closing.
2372 if (thflags & TH_FIN) {
2373 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2376 * If connection is half-synchronized
2377 * (ie NEEDSYN flag on) then delay ACK,
2378 * so it may be piggybacked when SYN is sent.
2379 * Otherwise, since we received a FIN then no
2380 * more input can be expected, send ACK now.
2382 if (tp->t_flags & TF_NEEDSYN)
2383 tp->t_flags |= TF_DELACK;
2385 tp->t_flags |= TF_ACKNOW;
2388 switch (tp->t_state) {
2391 * In SYN_RECEIVED and ESTABLISHED STATES
2392 * enter the CLOSE_WAIT state.
2394 case TCPS_SYN_RECEIVED:
2395 tp->t_starttime = ticks;
2397 case TCPS_ESTABLISHED:
2398 tp->t_state = TCPS_CLOSE_WAIT;
2402 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2403 * enter the CLOSING state.
2405 case TCPS_FIN_WAIT_1:
2406 tp->t_state = TCPS_CLOSING;
2410 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2411 * starting the time-wait timer, turning off the other
2414 case TCPS_FIN_WAIT_2:
2415 KASSERT(headlocked == 1, ("tcp_input: dodata: "
2416 "TCP_FIN_WAIT_2: head not locked"));
2418 INP_INFO_WUNLOCK(&tcbinfo);
2422 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2424 case TCPS_TIME_WAIT:
2425 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
2426 callout_reset(tp->tt_2msl, 2 * tcp_msl,
2427 tcp_timer_2msl, tp);
2431 INP_INFO_WUNLOCK(&tcbinfo);
2434 if (so->so_options & SO_DEBUG)
2435 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2440 * Return any desired output.
2442 if (needoutput || (tp->t_flags & TF_ACKNOW))
2443 (void) tcp_output(tp);
2446 KASSERT(headlocked == 0, ("tcp_input: check_delack: head locked"));
2447 INP_LOCK_ASSERT(inp);
2448 if (tp->t_flags & TF_DELACK) {
2449 tp->t_flags &= ~TF_DELACK;
2450 callout_reset(tp->tt_delack, tcp_delacktime,
2451 tcp_timer_delack, tp);
2457 KASSERT(headlocked, ("tcp_input: dropafterack: head not locked"));
2459 * Generate an ACK dropping incoming segment if it occupies
2460 * sequence space, where the ACK reflects our state.
2462 * We can now skip the test for the RST flag since all
2463 * paths to this code happen after packets containing
2464 * RST have been dropped.
2466 * In the SYN-RECEIVED state, don't send an ACK unless the
2467 * segment we received passes the SYN-RECEIVED ACK test.
2468 * If it fails send a RST. This breaks the loop in the
2469 * "LAND" DoS attack, and also prevents an ACK storm
2470 * between two listening ports that have been sent forged
2471 * SYN segments, each with the source address of the other.
2473 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2474 (SEQ_GT(tp->snd_una, th->th_ack) ||
2475 SEQ_GT(th->th_ack, tp->snd_max)) ) {
2476 rstreason = BANDLIM_RST_OPENPORT;
2480 if (so->so_options & SO_DEBUG)
2481 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2484 KASSERT(headlocked, ("headlocked should be 1"));
2485 INP_INFO_WUNLOCK(&tcbinfo);
2486 tp->t_flags |= TF_ACKNOW;
2487 (void) tcp_output(tp);
2493 KASSERT(headlocked, ("tcp_input: dropwithreset: head not locked"));
2495 * Generate a RST, dropping incoming segment.
2496 * Make ACK acceptable to originator of segment.
2497 * Don't bother to respond if destination was broadcast/multicast.
2499 if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2502 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2503 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2506 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2507 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
2508 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
2509 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
2512 /* IPv6 anycast check is done at tcp6_input() */
2515 * Perform bandwidth limiting.
2517 if (badport_bandlim(rstreason) < 0)
2521 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2522 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2526 if (thflags & TH_ACK)
2527 /* mtod() below is safe as long as hdr dropping is delayed */
2528 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
2531 if (thflags & TH_SYN)
2533 /* mtod() below is safe as long as hdr dropping is delayed */
2534 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2535 (tcp_seq)0, TH_RST|TH_ACK);
2541 INP_INFO_WUNLOCK(&tcbinfo);
2546 * Drop space held by incoming segment and return.
2549 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2550 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2556 INP_INFO_WUNLOCK(&tcbinfo);
2562 * Parse TCP options and place in tcpopt.
2565 tcp_dooptions(to, cp, cnt, is_syn)
2574 for (; cnt > 0; cnt -= optlen, cp += optlen) {
2576 if (opt == TCPOPT_EOL)
2578 if (opt == TCPOPT_NOP)
2584 if (optlen < 2 || optlen > cnt)
2589 if (optlen != TCPOLEN_MAXSEG)
2593 to->to_flags |= TOF_MSS;
2594 bcopy((char *)cp + 2,
2595 (char *)&to->to_mss, sizeof(to->to_mss));
2596 to->to_mss = ntohs(to->to_mss);
2599 if (optlen != TCPOLEN_WINDOW)
2603 to->to_flags |= TOF_SCALE;
2604 to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2606 case TCPOPT_TIMESTAMP:
2607 if (optlen != TCPOLEN_TIMESTAMP)
2609 to->to_flags |= TOF_TS;
2610 bcopy((char *)cp + 2,
2611 (char *)&to->to_tsval, sizeof(to->to_tsval));
2612 to->to_tsval = ntohl(to->to_tsval);
2613 bcopy((char *)cp + 6,
2614 (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2615 to->to_tsecr = ntohl(to->to_tsecr);
2617 * If echoed timestamp is later than the current time,
2618 * fall back to non RFC1323 RTT calculation.
2620 if ((to->to_tsecr != 0) && TSTMP_GT(to->to_tsecr, ticks))
2623 #ifdef TCP_SIGNATURE
2625 * XXX In order to reply to a host which has set the
2626 * TCP_SIGNATURE option in its initial SYN, we have to
2627 * record the fact that the option was observed here
2628 * for the syncache code to perform the correct response.
2630 case TCPOPT_SIGNATURE:
2631 if (optlen != TCPOLEN_SIGNATURE)
2633 to->to_flags |= (TOF_SIGNATURE | TOF_SIGLEN);
2636 case TCPOPT_SACK_PERMITTED:
2638 optlen != TCPOLEN_SACK_PERMITTED)
2641 /* MUST only be set on SYN */
2642 to->to_flags |= TOF_SACK;
2646 if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
2648 to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
2649 to->to_sacks = cp + 2;
2650 tcpstat.tcps_sack_rcv_blocks++;
2659 * Pull out of band byte out of a segment so
2660 * it doesn't appear in the user's data queue.
2661 * It is still reflected in the segment length for
2662 * sequencing purposes.
2665 tcp_pulloutofband(so, th, m, off)
2668 register struct mbuf *m;
2669 int off; /* delayed to be droped hdrlen */
2671 int cnt = off + th->th_urp - 1;
2674 if (m->m_len > cnt) {
2675 char *cp = mtod(m, caddr_t) + cnt;
2676 struct tcpcb *tp = sototcpcb(so);
2679 tp->t_oobflags |= TCPOOB_HAVEDATA;
2680 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2682 if (m->m_flags & M_PKTHDR)
2691 panic("tcp_pulloutofband");
2695 * Collect new round-trip time estimate
2696 * and update averages and current timeout.
2699 tcp_xmit_timer(tp, rtt)
2700 register struct tcpcb *tp;
2705 INP_LOCK_ASSERT(tp->t_inpcb);
2707 tcpstat.tcps_rttupdated++;
2709 if (tp->t_srtt != 0) {
2711 * srtt is stored as fixed point with 5 bits after the
2712 * binary point (i.e., scaled by 8). The following magic
2713 * is equivalent to the smoothing algorithm in rfc793 with
2714 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2715 * point). Adjust rtt to origin 0.
2717 delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2718 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2720 if ((tp->t_srtt += delta) <= 0)
2724 * We accumulate a smoothed rtt variance (actually, a
2725 * smoothed mean difference), then set the retransmit
2726 * timer to smoothed rtt + 4 times the smoothed variance.
2727 * rttvar is stored as fixed point with 4 bits after the
2728 * binary point (scaled by 16). The following is
2729 * equivalent to rfc793 smoothing with an alpha of .75
2730 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
2731 * rfc793's wired-in beta.
2735 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2736 if ((tp->t_rttvar += delta) <= 0)
2738 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
2739 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2742 * No rtt measurement yet - use the unsmoothed rtt.
2743 * Set the variance to half the rtt (so our first
2744 * retransmit happens at 3*rtt).
2746 tp->t_srtt = rtt << TCP_RTT_SHIFT;
2747 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
2748 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2754 * the retransmit should happen at rtt + 4 * rttvar.
2755 * Because of the way we do the smoothing, srtt and rttvar
2756 * will each average +1/2 tick of bias. When we compute
2757 * the retransmit timer, we want 1/2 tick of rounding and
2758 * 1 extra tick because of +-1/2 tick uncertainty in the
2759 * firing of the timer. The bias will give us exactly the
2760 * 1.5 tick we need. But, because the bias is
2761 * statistical, we have to test that we don't drop below
2762 * the minimum feasible timer (which is 2 ticks).
2764 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
2765 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2768 * We received an ack for a packet that wasn't retransmitted;
2769 * it is probably safe to discard any error indications we've
2770 * received recently. This isn't quite right, but close enough
2771 * for now (a route might have failed after we sent a segment,
2772 * and the return path might not be symmetrical).
2774 tp->t_softerror = 0;
2778 * Determine a reasonable value for maxseg size.
2779 * If the route is known, check route for mtu.
2780 * If none, use an mss that can be handled on the outgoing
2781 * interface without forcing IP to fragment; if bigger than
2782 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2783 * to utilize large mbufs. If no route is found, route has no mtu,
2784 * or the destination isn't local, use a default, hopefully conservative
2785 * size (usually 512 or the default IP max size, but no more than the mtu
2786 * of the interface), as we can't discover anything about intervening
2787 * gateways or networks. We also initialize the congestion/slow start
2788 * window to be a single segment if the destination isn't local.
2789 * While looking at the routing entry, we also initialize other path-dependent
2790 * parameters from pre-set or cached values in the routing entry.
2792 * Also take into account the space needed for options that we
2793 * send regularly. Make maxseg shorter by that amount to assure
2794 * that we can send maxseg amount of data even when the options
2795 * are present. Store the upper limit of the length of options plus
2799 * In case of T/TCP, we call this routine during implicit connection
2800 * setup as well (offer = -1), to initialize maxseg from the cached
2803 * NOTE that this routine is only called when we process an incoming
2804 * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt().
2814 struct inpcb *inp = tp->t_inpcb;
2816 struct hc_metrics_lite metrics;
2817 int origoffer = offer;
2819 int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2820 size_t min_protoh = isipv6 ?
2821 sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
2822 sizeof (struct tcpiphdr);
2824 const size_t min_protoh = sizeof(struct tcpiphdr);
2830 maxmtu = tcp_maxmtu6(&inp->inp_inc);
2831 tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt;
2835 maxmtu = tcp_maxmtu(&inp->inp_inc);
2836 tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
2838 so = inp->inp_socket;
2841 * no route to sender, stay with default mss and return
2846 /* what have we got? */
2850 * Offer == 0 means that there was no MSS on the SYN
2851 * segment, in this case we use tcp_mssdflt.
2855 isipv6 ? tcp_v6mssdflt :
2862 * Offer == -1 means that we didn't receive SYN yet.
2868 * Prevent DoS attack with too small MSS. Round up
2869 * to at least minmss.
2871 offer = max(offer, tcp_minmss);
2873 * Sanity check: make sure that maxopd will be large
2874 * enough to allow some data on segments even if the
2875 * all the option space is used (40bytes). Otherwise
2876 * funny things may happen in tcp_output.
2878 offer = max(offer, 64);
2882 * rmx information is now retrieved from tcp_hostcache
2884 tcp_hc_get(&inp->inp_inc, &metrics);
2887 * if there's a discovered mtu int tcp hostcache, use it
2888 * else, use the link mtu.
2890 if (metrics.rmx_mtu)
2891 mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
2895 mss = maxmtu - min_protoh;
2896 if (!path_mtu_discovery &&
2897 !in6_localaddr(&inp->in6p_faddr))
2898 mss = min(mss, tcp_v6mssdflt);
2902 mss = maxmtu - min_protoh;
2903 if (!path_mtu_discovery &&
2904 !in_localaddr(inp->inp_faddr))
2905 mss = min(mss, tcp_mssdflt);
2908 mss = min(mss, offer);
2911 * maxopd stores the maximum length of data AND options
2912 * in a segment; maxseg is the amount of data in a normal
2913 * segment. We need to store this value (maxopd) apart
2914 * from maxseg, because now every segment carries options
2915 * and thus we normally have somewhat less data in segments.
2920 * origoffer==-1 indicates, that no segments were received yet.
2921 * In this case we just guess.
2923 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2925 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2926 mss -= TCPOLEN_TSTAMP_APPA;
2929 #if (MCLBYTES & (MCLBYTES - 1)) == 0
2931 mss &= ~(MCLBYTES-1);
2934 mss = mss / MCLBYTES * MCLBYTES;
2939 * If there's a pipesize, change the socket buffer to that size,
2940 * don't change if sb_hiwat is different than default (then it
2941 * has been changed on purpose with setsockopt).
2942 * Make the socket buffers an integral number of mss units;
2943 * if the mss is larger than the socket buffer, decrease the mss.
2945 SOCKBUF_LOCK(&so->so_snd);
2946 if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe)
2947 bufsize = metrics.rmx_sendpipe;
2949 bufsize = so->so_snd.sb_hiwat;
2953 bufsize = roundup(bufsize, mss);
2954 if (bufsize > sb_max)
2956 if (bufsize > so->so_snd.sb_hiwat)
2957 (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
2959 SOCKBUF_UNLOCK(&so->so_snd);
2962 SOCKBUF_LOCK(&so->so_rcv);
2963 if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe)
2964 bufsize = metrics.rmx_recvpipe;
2966 bufsize = so->so_rcv.sb_hiwat;
2967 if (bufsize > mss) {
2968 bufsize = roundup(bufsize, mss);
2969 if (bufsize > sb_max)
2971 if (bufsize > so->so_rcv.sb_hiwat)
2972 (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
2974 SOCKBUF_UNLOCK(&so->so_rcv);
2976 * While we're here, check the others too
2978 if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
2980 tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
2981 tcpstat.tcps_usedrtt++;
2982 if (metrics.rmx_rttvar) {
2983 tp->t_rttvar = metrics.rmx_rttvar;
2984 tcpstat.tcps_usedrttvar++;
2986 /* default variation is +- 1 rtt */
2988 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
2990 TCPT_RANGESET(tp->t_rxtcur,
2991 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
2992 tp->t_rttmin, TCPTV_REXMTMAX);
2994 if (metrics.rmx_ssthresh) {
2996 * There's some sort of gateway or interface
2997 * buffer limit on the path. Use this to set
2998 * the slow start threshhold, but set the
2999 * threshold to no less than 2*mss.
3001 tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh);
3002 tcpstat.tcps_usedssthresh++;
3004 if (metrics.rmx_bandwidth)
3005 tp->snd_bandwidth = metrics.rmx_bandwidth;
3008 * Set the slow-start flight size depending on whether this
3009 * is a local network or not.
3011 * Extend this so we cache the cwnd too and retrieve it here.
3012 * Make cwnd even bigger than RFC3390 suggests but only if we
3013 * have previous experience with the remote host. Be careful
3014 * not make cwnd bigger than remote receive window or our own
3015 * send socket buffer. Maybe put some additional upper bound
3016 * on the retrieved cwnd. Should do incremental updates to
3017 * hostcache when cwnd collapses so next connection doesn't
3018 * overloads the path again.
3020 * RFC3390 says only do this if SYN or SYN/ACK didn't got lost.
3021 * We currently check only in syncache_socket for that.
3023 #define TCP_METRICS_CWND
3024 #ifdef TCP_METRICS_CWND
3025 if (metrics.rmx_cwnd)
3026 tp->snd_cwnd = max(mss,
3027 min(metrics.rmx_cwnd / 2,
3028 min(tp->snd_wnd, so->so_snd.sb_hiwat)));
3032 tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380));
3034 else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
3035 (!isipv6 && in_localaddr(inp->inp_faddr)))
3037 else if (in_localaddr(inp->inp_faddr))
3039 tp->snd_cwnd = mss * ss_fltsz_local;
3041 tp->snd_cwnd = mss * ss_fltsz;
3045 * Determine the MSS option to send on an outgoing SYN.
3049 struct in_conninfo *inc;
3056 int isipv6 = inc->inc_isipv6 ? 1 : 0;
3059 KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3063 mss = tcp_v6mssdflt;
3064 maxmtu = tcp_maxmtu6(inc);
3065 thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
3066 min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3071 maxmtu = tcp_maxmtu(inc);
3072 thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
3073 min_protoh = sizeof(struct tcpiphdr);
3075 if (maxmtu && thcmtu)
3076 mss = min(maxmtu, thcmtu) - min_protoh;
3077 else if (maxmtu || thcmtu)
3078 mss = max(maxmtu, thcmtu) - min_protoh;
3085 * On a partial ack arrives, force the retransmission of the
3086 * next unacknowledged segment. Do not clear tp->t_dupacks.
3087 * By setting snd_nxt to ti_ack, this forces retransmission timer to
3091 tcp_newreno_partial_ack(tp, th)
3095 tcp_seq onxt = tp->snd_nxt;
3096 u_long ocwnd = tp->snd_cwnd;
3098 callout_stop(tp->tt_rexmt);
3100 tp->snd_nxt = th->th_ack;
3102 * Set snd_cwnd to one segment beyond acknowledged offset.
3103 * (tp->snd_una has not yet been updated when this function is called.)
3105 tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
3106 tp->t_flags |= TF_ACKNOW;
3107 (void) tcp_output(tp);
3108 tp->snd_cwnd = ocwnd;
3109 if (SEQ_GT(onxt, tp->snd_nxt))
3112 * Partial window deflation. Relies on fact that tp->snd_una
3115 if (tp->snd_cwnd > th->th_ack - tp->snd_una)
3116 tp->snd_cwnd -= th->th_ack - tp->snd_una;
3119 tp->snd_cwnd += tp->t_maxseg;
3123 * Returns 1 if the TIME_WAIT state was killed and we should start over,
3124 * looking for a pcb in the listen state. Returns 0 otherwise.
3127 tcp_timewait(tw, to, th, m, tlen)
3137 int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
3139 const int isipv6 = 0;
3142 /* tcbinfo lock required for tcp_twclose(), tcp_2msl_reset. */
3143 INP_INFO_WLOCK_ASSERT(&tcbinfo);
3144 INP_LOCK_ASSERT(tw->tw_inpcb);
3146 thflags = th->th_flags;
3149 * NOTE: for FIN_WAIT_2 (to be added later),
3150 * must validate sequence number before accepting RST
3154 * If the segment contains RST:
3155 * Drop the segment - see Stevens, vol. 2, p. 964 and
3158 if (thflags & TH_RST)
3162 /* PAWS not needed at the moment */
3164 * RFC 1323 PAWS: If we have a timestamp reply on this segment
3165 * and it's less than ts_recent, drop it.
3167 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
3168 TSTMP_LT(to.to_tsval, tp->ts_recent)) {
3169 if ((thflags & TH_ACK) == 0)
3174 * ts_recent is never updated because we never accept new segments.
3179 * If a new connection request is received
3180 * while in TIME_WAIT, drop the old connection
3181 * and start over if the sequence numbers
3182 * are above the previous ones.
3184 if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) {
3185 (void) tcp_twclose(tw, 0);
3190 * Drop the the segment if it does not contain an ACK.
3192 if ((thflags & TH_ACK) == 0)
3196 * Reset the 2MSL timer if this is a duplicate FIN.
3198 if (thflags & TH_FIN) {
3199 seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0);
3200 if (seq + 1 == tw->rcv_nxt)
3201 tcp_timer_2msl_reset(tw, 2 * tcp_msl);
3205 * Acknowledge the segment if it has data or is not a duplicate ACK.
3207 if (thflags != TH_ACK || tlen != 0 ||
3208 th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt)
3209 tcp_twrespond(tw, TH_ACK);
3213 * Generate a RST, dropping incoming segment.
3214 * Make ACK acceptable to originator of segment.
3215 * Don't bother to respond if destination was broadcast/multicast.
3217 if (m->m_flags & (M_BCAST|M_MCAST))
3220 struct ip6_hdr *ip6;
3222 /* IPv6 anycast check is done at tcp6_input() */
3223 ip6 = mtod(m, struct ip6_hdr *);
3224 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3225 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3230 ip = mtod(m, struct ip *);
3231 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3232 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3233 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3234 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3237 if (thflags & TH_ACK) {
3239 mtod(m, void *), th, m, 0, th->th_ack, TH_RST);
3241 seq = th->th_seq + (thflags & TH_SYN ? 1 : 0);
3243 mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK);
3245 INP_UNLOCK(tw->tw_inpcb);
3249 INP_UNLOCK(tw->tw_inpcb);