xref: /freebsd/sys/netinet/tcp_input.c (revision f2512ba12a4287ab799825c7c4f4f260846cf596)
1c398230bSWarner Losh /*-
2e79adb8eSGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29e79adb8eSGarrett Wollman  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
324b421e2dSMike Silbersack #include <sys/cdefs.h>
334b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
344b421e2dSMike Silbersack 
35f9e354dfSJulian Elischer #include "opt_ipfw.h"		/* for ipfw_fwd	*/
361cfd4b53SBruce M Simpson #include "opt_inet.h"
37fb59c426SYoshinobu Inoue #include "opt_inet6.h"
383a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
39c488362eSRobert Watson #include "opt_mac.h"
400cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
410cc12cc5SJoerg Wunsch 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
4398163b98SPoul-Henning Kamp #include <sys/kernel.h>
44df8bae1dSRodney W. Grimes #include <sys/malloc.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
47df8bae1dSRodney W. Grimes #include <sys/protosw.h>
48960ed29cSSeigo Tanimura #include <sys/signalvar.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
51960ed29cSSeigo Tanimura #include <sys/sysctl.h>
52816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
53960ed29cSSeigo Tanimura #include <sys/systm.h>
54df8bae1dSRodney W. Grimes 
55e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
56e79adb8eSGarrett Wollman 
5712e2e970SAndre Oppermann #include <vm/uma.h>
5812e2e970SAndre Oppermann 
59df8bae1dSRodney W. Grimes #include <net/if.h>
60df8bae1dSRodney W. Grimes #include <net/route.h>
61df8bae1dSRodney W. Grimes 
62773673c1SAndre Oppermann #define TCPSTATES		/* for logging */
63773673c1SAndre Oppermann 
64df8bae1dSRodney W. Grimes #include <netinet/in.h>
65960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
66df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
67960ed29cSSeigo Tanimura #include <netinet/in_var.h>
68df8bae1dSRodney W. Grimes #include <netinet/ip.h>
698e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
70686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
71df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
72ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
73686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
74686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
75686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
76960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
77960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
78df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
79df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
80df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
81df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
82df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
83fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
84df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
85c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
86610ee2f9SDavid Greenman #ifdef TCPDEBUG
87df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
88fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
89fb59c426SYoshinobu Inoue 
90b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
91b9234fafSSam Leffler #include <netipsec/ipsec.h>
92b9234fafSSam Leffler #include <netipsec/ipsec6.h>
93b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
94b9234fafSSam Leffler 
95db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
96db4f9cc7SJonathan Lemon 
97aed55708SRobert Watson #include <security/mac/mac_framework.h>
98aed55708SRobert Watson 
99c068736aSJeffrey Hsu static const int tcprexmtthresh = 3;
1000312fbe9SPoul-Henning Kamp 
1012f96f1f4SGarrett Wollman struct	tcpstat tcpstat;
102c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW,
1033d177f46SBill Fumerola     &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
1040312fbe9SPoul-Henning Kamp 
105773673c1SAndre Oppermann int tcp_log_in_vain = 0;
106816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
107574b6964SAndre Oppermann     &tcp_log_in_vain, 0, "Log all incoming TCP segments to closed ports");
108816a3d83SPoul-Henning Kamp 
10916f7f31fSGeoff Rehmet static int blackhole = 0;
11016f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
111574b6964SAndre Oppermann     &blackhole, 0, "Do not send RST on segments to closed ports");
11216f7f31fSGeoff Rehmet 
113f498eeeeSDavid Greenman int tcp_delack_enabled = 1;
11484e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
1153d177f46SBill Fumerola     &tcp_delack_enabled, 0,
1163d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
117f498eeeeSDavid Greenman 
118f8613305SDag-Erling Smørgrav static int drop_synfin = 0;
119f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
120f8613305SDag-Erling Smørgrav     &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
121f8613305SDag-Erling Smørgrav 
122dba7bc6aSAndre Oppermann static int tcp_do_rfc3042 = 1;
123582a954bSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW,
124582a954bSJeffrey Hsu     &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)");
125582a954bSJeffrey Hsu 
126dba7bc6aSAndre Oppermann static int tcp_do_rfc3390 = 1;
127da3a8a1aSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW,
128da3a8a1aSJeffrey Hsu     &tcp_do_rfc3390, 0,
129da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
130da3a8a1aSJeffrey Hsu 
131f2512ba1SRui Paulo int	tcp_do_ecn = 0;
132f2512ba1SRui Paulo int	tcp_ecn_maxretries = 1;
133f2512ba1SRui Paulo SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN");
134f2512ba1SRui Paulo SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_RW,
135f2512ba1SRui Paulo     &tcp_do_ecn, 0, "TCP ECN support");
136f2512ba1SRui Paulo SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_RW,
137f2512ba1SRui Paulo     &tcp_ecn_maxretries, 0, "Max retries before giving up on ECN");
138f2512ba1SRui Paulo 
139a69968eeSMike Silbersack static int tcp_insecure_rst = 0;
140a69968eeSMike Silbersack SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW,
141a69968eeSMike Silbersack     &tcp_insecure_rst, 0,
1426489fe65SAndre Oppermann     "Follow the old (insecure) criteria for accepting RST packets");
143a69968eeSMike Silbersack 
1446741ecf5SAndre Oppermann int	tcp_do_autorcvbuf = 1;
1456741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW,
1466741ecf5SAndre Oppermann     &tcp_do_autorcvbuf, 0, "Enable automatic receive buffer sizing");
1476741ecf5SAndre Oppermann 
1486741ecf5SAndre Oppermann int	tcp_autorcvbuf_inc = 16*1024;
1496741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW,
1506489fe65SAndre Oppermann     &tcp_autorcvbuf_inc, 0,
1516489fe65SAndre Oppermann     "Incrementor step size of automatic receive buffer");
1526741ecf5SAndre Oppermann 
1536741ecf5SAndre Oppermann int	tcp_autorcvbuf_max = 256*1024;
1546741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW,
1556741ecf5SAndre Oppermann     &tcp_autorcvbuf_max, 0, "Max size of automatic receive buffer");
1566741ecf5SAndre Oppermann 
15715bd2b43SDavid Greenman struct inpcbhead tcb;
158fb59c426SYoshinobu Inoue #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
15915bd2b43SDavid Greenman struct inpcbinfo tcbinfo;
160df8bae1dSRodney W. Grimes 
1615a53ca16SPaul Saab static void	 tcp_dooptions(struct tcpopt *, u_char *, int, int);
162679d9708SAndre Oppermann static void	 tcp_do_segment(struct mbuf *, struct tcphdr *,
163f2512ba1SRui Paulo 		     struct socket *, struct tcpcb *, int, int, uint8_t);
164302ce8d6SAndre Oppermann static void	 tcp_dropwithreset(struct mbuf *, struct tcphdr *,
165302ce8d6SAndre Oppermann 		     struct tcpcb *, int, int);
1664d77a549SAlfred Perlstein static void	 tcp_pulloutofband(struct socket *,
1674d77a549SAlfred Perlstein 		     struct tcphdr *, struct mbuf *, int);
1684d77a549SAlfred Perlstein static void	 tcp_xmit_timer(struct tcpcb *, int);
169c068736aSJeffrey Hsu static void	 tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
170f2512ba1SRui Paulo static void inline
171f2512ba1SRui Paulo 		 tcp_congestion_exp(struct tcpcb *);
172f2512ba1SRui Paulo 
173f2512ba1SRui Paulo static void inline
174f2512ba1SRui Paulo tcp_congestion_exp(struct tcpcb *tp)
175f2512ba1SRui Paulo {
176f2512ba1SRui Paulo 	u_int win;
177f2512ba1SRui Paulo 
178f2512ba1SRui Paulo 	win = min(tp->snd_wnd, tp->snd_cwnd) /
179f2512ba1SRui Paulo 	    2 / tp->t_maxseg;
180f2512ba1SRui Paulo 	if (win < 2)
181f2512ba1SRui Paulo 		win = 2;
182f2512ba1SRui Paulo 	tp->snd_ssthresh = win * tp->t_maxseg;
183f2512ba1SRui Paulo 	ENTER_FASTRECOVERY(tp);
184f2512ba1SRui Paulo 	tp->snd_recover = tp->snd_max;
185f2512ba1SRui Paulo 	if (tp->t_flags & TF_ECN_PERMIT)
186f2512ba1SRui Paulo 		tp->t_flags |= TF_ECN_SND_CWR;
187f2512ba1SRui Paulo }
1880312fbe9SPoul-Henning Kamp 
189fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
190fb59c426SYoshinobu Inoue #ifdef INET6
191fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \
192fb59c426SYoshinobu Inoue do { \
193fb59c426SYoshinobu Inoue 	if ((tp) && (tp)->t_inpcb && \
19497d8d152SAndre Oppermann 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \
19597d8d152SAndre Oppermann 		nd6_nud_hint(NULL, NULL, 0); \
196fb59c426SYoshinobu Inoue } while (0)
197fb59c426SYoshinobu Inoue #else
198fb59c426SYoshinobu Inoue #define ND6_HINT(tp)
199fb59c426SYoshinobu Inoue #endif
200df8bae1dSRodney W. Grimes 
201df8bae1dSRodney W. Grimes /*
202262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
203262c1c1aSMatthew Dillon  *	- there is no delayed ack timer in progress and
204262c1c1aSMatthew Dillon  *	- our last ack wasn't a 0-sized window.  We never want to delay
2053bfd6421SJonathan Lemon  *	  the ack that opens up a 0-sized window and
2063bfd6421SJonathan Lemon  *		- delayed acks are enabled or
2073bfd6421SJonathan Lemon  *		- this is a half-synchronized T/TCP connection.
208d8c85a26SJonathan Lemon  */
209d8c85a26SJonathan Lemon #define DELAY_ACK(tp)							\
210b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
211a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
2123bfd6421SJonathan Lemon 	    (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
213d8c85a26SJonathan Lemon 
214df8bae1dSRodney W. Grimes 
215df8bae1dSRodney W. Grimes /*
2168d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
2178d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
2188d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
2198d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
2208d573cc1SAndre Oppermann  *	SYN processing on listen sockets
2218d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
2228d573cc1SAndre Oppermann  *	establishing, established and closing connections
223df8bae1dSRodney W. Grimes  */
224fb59c426SYoshinobu Inoue #ifdef INET6
225fb59c426SYoshinobu Inoue int
226ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto)
227fb59c426SYoshinobu Inoue {
228ad3f9ab3SAndre Oppermann 	struct mbuf *m = *mp;
22933841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
230fb59c426SYoshinobu Inoue 
231fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
232fb59c426SYoshinobu Inoue 
233fb59c426SYoshinobu Inoue 	/*
234fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
235fb59c426SYoshinobu Inoue 	 * better place to put this in?
236fb59c426SYoshinobu Inoue 	 */
23733841545SHajimu UMEMOTO 	ia6 = ip6_getdstifaddr(m);
23833841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
239fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
240fb59c426SYoshinobu Inoue 
241fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
242fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
243fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
244fb59c426SYoshinobu Inoue 		return IPPROTO_DONE;
245fb59c426SYoshinobu Inoue 	}
246fb59c426SYoshinobu Inoue 
247f0ffb944SJulian Elischer 	tcp_input(m, *offp);
248fb59c426SYoshinobu Inoue 	return IPPROTO_DONE;
249fb59c426SYoshinobu Inoue }
250fb59c426SYoshinobu Inoue #endif
251fb59c426SYoshinobu Inoue 
252df8bae1dSRodney W. Grimes void
253ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0)
254df8bae1dSRodney W. Grimes {
255ad3f9ab3SAndre Oppermann 	struct tcphdr *th;
256ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
257ad3f9ab3SAndre Oppermann 	struct ipovly *ipov;
258ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
259302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
260302ce8d6SAndre Oppermann 	struct socket *so = NULL;
261e79adb8eSGarrett Wollman 	u_char *optp = NULL;
26226f9a767SRodney W. Grimes 	int optlen = 0;
263a0194ef1SBruce M Simpson 	int len, tlen, off;
264fb59c426SYoshinobu Inoue 	int drop_hdrlen;
265ad3f9ab3SAndre Oppermann 	int thflags;
266302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
267f2512ba1SRui Paulo 	uint8_t iptos;
2689b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
2699b932e9eSAndre Oppermann 	struct m_tag *fwd_tag;
2709b932e9eSAndre Oppermann #endif
271c068736aSJeffrey Hsu #ifdef INET6
272302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
273c068736aSJeffrey Hsu 	int isipv6;
274c068736aSJeffrey Hsu #else
275a160e630SAndre Oppermann 	const void *ip6 = NULL;
276c068736aSJeffrey Hsu 	const int isipv6 = 0;
277c068736aSJeffrey Hsu #endif
278302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
279a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
280f76fcf6dSJeffrey Hsu 
281610ee2f9SDavid Greenman #ifdef TCPDEBUG
282c068736aSJeffrey Hsu 	/*
283c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
284c068736aSJeffrey Hsu 	 * now IPv6.
285c068736aSJeffrey Hsu 	 */
28614739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
287410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
288610ee2f9SDavid Greenman 	short ostate = 0;
289610ee2f9SDavid Greenman #endif
290c068736aSJeffrey Hsu 
291fb59c426SYoshinobu Inoue #ifdef INET6
292fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
293fb59c426SYoshinobu Inoue #endif
294a0292f23SGarrett Wollman 
295302ce8d6SAndre Oppermann 	to.to_flags = 0;
296df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvtotal++;
297fb59c426SYoshinobu Inoue 
298fb59c426SYoshinobu Inoue 	if (isipv6) {
29904d3a452SHajimu UMEMOTO #ifdef INET6
3008d573cc1SAndre Oppermann 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */
301fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
302fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
303fb59c426SYoshinobu Inoue 		if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
304fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbadsum++;
305fb59c426SYoshinobu Inoue 			goto drop;
306fb59c426SYoshinobu Inoue 		}
307fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
30833841545SHajimu UMEMOTO 
30933841545SHajimu UMEMOTO 		/*
31033841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
31133841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
31233841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
31333841545SHajimu UMEMOTO 		 *
31433841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
31533841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
31633841545SHajimu UMEMOTO 		 */
31733841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
31833841545SHajimu UMEMOTO 			/* XXX stat */
31933841545SHajimu UMEMOTO 			goto drop;
32033841545SHajimu UMEMOTO 		}
32104d3a452SHajimu UMEMOTO #else
3228d573cc1SAndre Oppermann 		th = NULL;		/* XXX: Avoid compiler warning. */
32304d3a452SHajimu UMEMOTO #endif
324c068736aSJeffrey Hsu 	} else {
325df8bae1dSRodney W. Grimes 		/*
326df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
327df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
328df8bae1dSRodney W. Grimes 		 */
329fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
330df8bae1dSRodney W. Grimes 			ip_stripoptions(m, (struct mbuf *)0);
331fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
332fb59c426SYoshinobu Inoue 		}
333df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
3344dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
3354dfdffe9SAndre Oppermann 			    == NULL) {
336df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvshort++;
337df8bae1dSRodney W. Grimes 				return;
338df8bae1dSRodney W. Grimes 			}
339df8bae1dSRodney W. Grimes 		}
340fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
341fb59c426SYoshinobu Inoue 		ipov = (struct ipovly *)ip;
342db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
343db4f9cc7SJonathan Lemon 		tlen = ip->ip_len;
344df8bae1dSRodney W. Grimes 
345db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
346db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
347db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
348db4f9cc7SJonathan Lemon 			else
349db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
350c068736aSJeffrey Hsu 						ip->ip_dst.s_addr,
351c068736aSJeffrey Hsu 						htonl(m->m_pkthdr.csum_data +
352c068736aSJeffrey Hsu 							ip->ip_len +
353c068736aSJeffrey Hsu 							IPPROTO_TCP));
354db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
3553c653157SHartmut Brandt #ifdef TCPDEBUG
3563c653157SHartmut Brandt 			ipov->ih_len = (u_short)tlen;
3573c653157SHartmut Brandt 			ipov->ih_len = htons(ipov->ih_len);
3583c653157SHartmut Brandt #endif
359db4f9cc7SJonathan Lemon 		} else {
360df8bae1dSRodney W. Grimes 			/*
361df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
362df8bae1dSRodney W. Grimes 			 */
363df8bae1dSRodney W. Grimes 			len = sizeof (struct ip) + tlen;
364fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
365fb59c426SYoshinobu Inoue 			ipov->ih_len = (u_short)tlen;
366fd8e4ebcSMike Barcroft 			ipov->ih_len = htons(ipov->ih_len);
367fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
368db4f9cc7SJonathan Lemon 		}
369fb59c426SYoshinobu Inoue 		if (th->th_sum) {
370df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbadsum++;
371df8bae1dSRodney W. Grimes 			goto drop;
372df8bae1dSRodney W. Grimes 		}
373fb59c426SYoshinobu Inoue 		/* Re-initialization for later version check */
374fb59c426SYoshinobu Inoue 		ip->ip_v = IPVERSION;
375fb59c426SYoshinobu Inoue 	}
376df8bae1dSRodney W. Grimes 
377f2512ba1SRui Paulo #ifdef INET6
378f2512ba1SRui Paulo 	if (isipv6)
379f2512ba1SRui Paulo 		iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
380f2512ba1SRui Paulo 	else
381f2512ba1SRui Paulo #endif
382f2512ba1SRui Paulo 		iptos = ip->ip_tos;
383f2512ba1SRui Paulo 
384df8bae1dSRodney W. Grimes 	/*
385df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
386df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
387df8bae1dSRodney W. Grimes 	 */
388fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
389df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
390df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadoff++;
391df8bae1dSRodney W. Grimes 		goto drop;
392df8bae1dSRodney W. Grimes 	}
393fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
394df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
395fb59c426SYoshinobu Inoue 		if (isipv6) {
39604d3a452SHajimu UMEMOTO #ifdef INET6
397fb59c426SYoshinobu Inoue 			IP6_EXTHDR_CHECK(m, off0, off, );
398fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
399fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
40004d3a452SHajimu UMEMOTO #endif
401c068736aSJeffrey Hsu 		} else {
402df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
403c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
4044dfdffe9SAndre Oppermann 				    == NULL) {
405df8bae1dSRodney W. Grimes 					tcpstat.tcps_rcvshort++;
406df8bae1dSRodney W. Grimes 					return;
407df8bae1dSRodney W. Grimes 				}
408fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
409fb59c426SYoshinobu Inoue 				ipov = (struct ipovly *)ip;
410fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
411fb59c426SYoshinobu Inoue 			}
412df8bae1dSRodney W. Grimes 		}
413df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
414fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
415df8bae1dSRodney W. Grimes 	}
416fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
417df8bae1dSRodney W. Grimes 
418e46cd3d4SDag-Erling Smørgrav 	/*
419df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
420df8bae1dSRodney W. Grimes 	 */
421fd8e4ebcSMike Barcroft 	th->th_seq = ntohl(th->th_seq);
422fd8e4ebcSMike Barcroft 	th->th_ack = ntohl(th->th_ack);
423fd8e4ebcSMike Barcroft 	th->th_win = ntohs(th->th_win);
424fd8e4ebcSMike Barcroft 	th->th_urp = ntohs(th->th_urp);
425df8bae1dSRodney W. Grimes 
426df8bae1dSRodney W. Grimes 	/*
427794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
428755c1f07SAndras Olah 	 */
429fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
430755c1f07SAndras Olah 
431755c1f07SAndras Olah 	/*
432df8bae1dSRodney W. Grimes 	 * Locate pcb for segment.
433df8bae1dSRodney W. Grimes 	 */
434f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
435df8bae1dSRodney W. Grimes findpcb:
436302ce8d6SAndre Oppermann 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
4379b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
4388d573cc1SAndre Oppermann 	/*
4398d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
4408d573cc1SAndre Oppermann 	 */
4419b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
4429b932e9eSAndre Oppermann 
4439b932e9eSAndre Oppermann 	if (fwd_tag != NULL && isipv6 == 0) {	/* IPv6 support is not yet */
4449b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
4459b932e9eSAndre Oppermann 
4469b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
447f9e354dfSJulian Elischer 		/*
4482b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
449f9e354dfSJulian Elischer 		 * already got one like this?
450f9e354dfSJulian Elischer 		 */
4519b932e9eSAndre Oppermann 		inp = in_pcblookup_hash(&tcbinfo,
4529b932e9eSAndre Oppermann 					ip->ip_src, th->th_sport,
453c068736aSJeffrey Hsu 					ip->ip_dst, th->th_dport,
454c068736aSJeffrey Hsu 					0, m->m_pkthdr.rcvif);
455f9e354dfSJulian Elischer 		if (!inp) {
4569b932e9eSAndre Oppermann 			/* It's new.  Try to find the ambushing socket. */
457f9e354dfSJulian Elischer 			inp = in_pcblookup_hash(&tcbinfo,
458fb59c426SYoshinobu Inoue 						ip->ip_src, th->th_sport,
4592b25acc1SLuigi Rizzo 						next_hop->sin_addr,
460c068736aSJeffrey Hsu 						next_hop->sin_port ?
461c068736aSJeffrey Hsu 						    ntohs(next_hop->sin_port) :
462c068736aSJeffrey Hsu 						    th->th_dport,
463421d8aa6SBjoern A. Zeeb 						INPLOOKUP_WILDCARD,
464421d8aa6SBjoern A. Zeeb 						m->m_pkthdr.rcvif);
465f9e354dfSJulian Elischer 		}
4669b932e9eSAndre Oppermann 		/* Remove the tag from the packet.  We don't need it anymore. */
4679b932e9eSAndre Oppermann 		m_tag_delete(m, fwd_tag);
468b10fbdeaSAndre Oppermann 	} else
4699b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
470b10fbdeaSAndre Oppermann 	{
47104d3a452SHajimu UMEMOTO 		if (isipv6) {
47204d3a452SHajimu UMEMOTO #ifdef INET6
473c068736aSJeffrey Hsu 			inp = in6_pcblookup_hash(&tcbinfo,
474c068736aSJeffrey Hsu 						 &ip6->ip6_src, th->th_sport,
475c068736aSJeffrey Hsu 						 &ip6->ip6_dst, th->th_dport,
476421d8aa6SBjoern A. Zeeb 						 INPLOOKUP_WILDCARD,
477421d8aa6SBjoern A. Zeeb 						 m->m_pkthdr.rcvif);
47804d3a452SHajimu UMEMOTO #endif
47904d3a452SHajimu UMEMOTO 		} else
480c068736aSJeffrey Hsu 			inp = in_pcblookup_hash(&tcbinfo,
481c068736aSJeffrey Hsu 						ip->ip_src, th->th_sport,
482c068736aSJeffrey Hsu 						ip->ip_dst, th->th_dport,
483421d8aa6SBjoern A. Zeeb 						INPLOOKUP_WILDCARD,
484421d8aa6SBjoern A. Zeeb 						m->m_pkthdr.rcvif);
485fb59c426SYoshinobu Inoue 	}
486f9e354dfSJulian Elischer 
487df8bae1dSRodney W. Grimes 	/*
488574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
489574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
4908b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
491df8bae1dSRodney W. Grimes 	 */
492816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
493574b6964SAndre Oppermann 		/*
494574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
495574b6964SAndre Oppermann 		 * in use.
496574b6964SAndre Oppermann 		 */
497574b6964SAndre Oppermann 		if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
498574b6964SAndre Oppermann 		    tcp_log_in_vain == 2) {
4999fb5d4c0SPeter Wemm 			if ((s = tcp_log_addrs(NULL, th, (void *)ip, ip6)))
500df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
501df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
5022e4e1b4cSGeoff Rehmet 		}
503574b6964SAndre Oppermann 		/*
504574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
505574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
506574b6964SAndre Oppermann 		 */
507574b6964SAndre Oppermann 		if ((blackhole == 1 && (thflags & TH_SYN)) ||
508574b6964SAndre Oppermann 		    blackhole == 2)
5091929eae1SAndre Oppermann 			goto dropunlock;
510574b6964SAndre Oppermann 
511a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
512a57815efSBosko Milekic 		goto dropwithreset;
513816a3d83SPoul-Henning Kamp 	}
5148501a69cSRobert Watson 	INP_WLOCK(inp);
515936cd18dSAndre Oppermann 
516a2589465SKen Smith #ifdef IPSEC
517a2589465SKen Smith #ifdef INET6
518a2589465SKen Smith 	if (isipv6 && ipsec6_in_reject(m, inp)) {
519a2589465SKen Smith 		ipsec6stat.in_polvio++;
520a2589465SKen Smith 		goto dropunlock;
521a2589465SKen Smith 	} else
522a2589465SKen Smith #endif /* INET6 */
523a2589465SKen Smith 	if (ipsec4_in_reject(m, inp) != 0) {
524a2589465SKen Smith 		ipsec4stat.in_polvio++;
525a2589465SKen Smith 		goto dropunlock;
526a2589465SKen Smith 	}
527a2589465SKen Smith #endif /* IPSEC */
528a2589465SKen Smith 
5298d573cc1SAndre Oppermann 	/*
5308d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
5318d573cc1SAndre Oppermann 	 */
53234f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
53334f83c52SGeorge V. Neville-Neil #ifdef INET6
53434f83c52SGeorge V. Neville-Neil 		if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim)
535302ce8d6SAndre Oppermann 			goto dropunlock;
53602707462SAndre Oppermann 		else
53734f83c52SGeorge V. Neville-Neil #endif
53834f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
539302ce8d6SAndre Oppermann 			goto dropunlock;
54034f83c52SGeorge V. Neville-Neil 	}
541936cd18dSAndre Oppermann 
542340c35deSJonathan Lemon 	/*
543794235b7SAndre Oppermann 	 * A previous connection in TIMEWAIT state is supposed to catch
544794235b7SAndre Oppermann 	 * stray or duplicate segments arriving late.  If this segment
545794235b7SAndre Oppermann 	 * was a legitimate new connection attempt the old INPCB gets
546794235b7SAndre Oppermann 	 * removed and we can try again to find a listening socket.
547340c35deSJonathan Lemon 	 */
548794235b7SAndre Oppermann 	if (inp->inp_vflag & INP_TIMEWAIT) {
549340c35deSJonathan Lemon 		if (thflags & TH_SYN)
550f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
5518d573cc1SAndre Oppermann 		/*
5528d573cc1SAndre Oppermann 		 * NB: tcp_twcheck unlocks the INP and frees the mbuf.
5538d573cc1SAndre Oppermann 		 */
5542104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
555340c35deSJonathan Lemon 			goto findpcb;
556340c35deSJonathan Lemon 		INP_INFO_WUNLOCK(&tcbinfo);
557340c35deSJonathan Lemon 		return;
558340c35deSJonathan Lemon 	}
559794235b7SAndre Oppermann 	/*
560794235b7SAndre Oppermann 	 * The TCPCB may no longer exist if the connection is winding
561794235b7SAndre Oppermann 	 * down or it is in the CLOSED state.  Either way we drop the
562794235b7SAndre Oppermann 	 * segment and send an appropriate response.
563794235b7SAndre Oppermann 	 */
564df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
565a160e630SAndre Oppermann 	if (tp == NULL || tp->t_state == TCPS_CLOSED) {
566a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
567a57815efSBosko Milekic 		goto dropwithreset;
56809f81a46SBosko Milekic 	}
569df8bae1dSRodney W. Grimes 
570c488362eSRobert Watson #ifdef MAC
5718501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
57230d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
573302ce8d6SAndre Oppermann 		goto dropunlock;
574c488362eSRobert Watson #endif
575a557af22SRobert Watson 	so = inp->inp_socket;
576302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
577610ee2f9SDavid Greenman #ifdef TCPDEBUG
578df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
579df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
580d30d90dcSMaxim Konovalov 		if (isipv6) {
58110fe523eSMaxim Konovalov #ifdef INET6
582540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
583d30d90dcSMaxim Konovalov #endif
584d30d90dcSMaxim Konovalov 		} else
585540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
586fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
587df8bae1dSRodney W. Grimes 	}
588610ee2f9SDavid Greenman #endif
589db33b3e6SAndre Oppermann 	/*
590db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
591db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
592db33b3e6SAndre Oppermann 	 * attempt or the completion of a previous one.
593db33b3e6SAndre Oppermann 	 */
594540e8b7eSJeffrey Hsu 	if (so->so_options & SO_ACCEPTCONN) {
595540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
596540e8b7eSJeffrey Hsu 
5977824d002SAndre Oppermann 		KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but "
5987824d002SAndre Oppermann 		    "tp not listening", __func__));
5997824d002SAndre Oppermann 
6008bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
601be2ac88cSJonathan Lemon 		inc.inc_isipv6 = isipv6;
602302ce8d6SAndre Oppermann #ifdef INET6
603be2ac88cSJonathan Lemon 		if (isipv6) {
604be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
605be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
606302ce8d6SAndre Oppermann 		} else
607302ce8d6SAndre Oppermann #endif
608302ce8d6SAndre Oppermann 		{
609be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
610be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
611be2ac88cSJonathan Lemon 		}
612be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
613be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
614be2ac88cSJonathan Lemon 
615fb59c426SYoshinobu Inoue 		/*
6168d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
6178d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
6188d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
619fb59c426SYoshinobu Inoue 		 */
620be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
621bf6d304aSAndre Oppermann 			/*
622bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
623bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
624bf6d304aSAndre Oppermann 			 * timestamp.
625bf6d304aSAndre Oppermann 			 */
626bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
6279fa198beSAndre Oppermann 			/*
6289fa198beSAndre Oppermann 			 * NB: syncache_expand() doesn't unlock
6299fa198beSAndre Oppermann 			 * inp and tcpinfo locks.
6309fa198beSAndre Oppermann 			 */
631bf6d304aSAndre Oppermann 			if (!syncache_expand(&inc, &to, th, &so, m)) {
6324195b4afSPaul Traina 				/*
633e207f800SAndre Oppermann 				 * No syncache entry or ACK was not
634be2ac88cSJonathan Lemon 				 * for our SYN/ACK.  Send a RST.
6358d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
6368d573cc1SAndre Oppermann 				 * of the failure cause.
6374195b4afSPaul Traina 				 */
638be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
639be2ac88cSJonathan Lemon 				goto dropwithreset;
640be2ac88cSJonathan Lemon 			}
641f76fcf6dSJeffrey Hsu 			if (so == NULL) {
642be2ac88cSJonathan Lemon 				/*
643e207f800SAndre Oppermann 				 * We completed the 3-way handshake
644e207f800SAndre Oppermann 				 * but could not allocate a socket
645e207f800SAndre Oppermann 				 * either due to memory shortage,
646e207f800SAndre Oppermann 				 * listen queue length limits or
647a160e630SAndre Oppermann 				 * global socket limits.  Send RST
648a160e630SAndre Oppermann 				 * or wait and have the remote end
649a160e630SAndre Oppermann 				 * retransmit the ACK for another
650a160e630SAndre Oppermann 				 * try.
651be2ac88cSJonathan Lemon 				 */
652a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
653a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
6548d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
6558d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
656a160e630SAndre Oppermann 					    s, __func__, (tcp_sc_rst_sock_fail ?
657a160e630SAndre Oppermann 					    "sending RST" : "try again"));
658a160e630SAndre Oppermann 				if (tcp_sc_rst_sock_fail) {
659e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
660e207f800SAndre Oppermann 					goto dropwithreset;
661a160e630SAndre Oppermann 				} else
662a160e630SAndre Oppermann 					goto dropunlock;
663f76fcf6dSJeffrey Hsu 			}
664be2ac88cSJonathan Lemon 			/*
665be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
6668d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
6678d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
668be2ac88cSJonathan Lemon 			 */
6698501a69cSRobert Watson 			INP_WUNLOCK(inp);	/* listen socket */
670be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
6718501a69cSRobert Watson 			INP_WLOCK(inp);		/* new connection */
672be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
6738d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
6748d573cc1SAndre Oppermann 			    ("%s: ", __func__));
675be2ac88cSJonathan Lemon 			/*
676302ce8d6SAndre Oppermann 			 * Process the segment and the data it
677302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
678302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
679302ce8d6SAndre Oppermann 			 */
680f2512ba1SRui Paulo 			tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
681f2512ba1SRui Paulo 			    iptos);
682679d9708SAndre Oppermann 			INP_INFO_UNLOCK_ASSERT(&tcbinfo);
683302ce8d6SAndre Oppermann 			return;
684be2ac88cSJonathan Lemon 		}
685a160e630SAndre Oppermann 		/*
6868d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
6878d573cc1SAndre Oppermann 		 *
688a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
689a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
690a160e630SAndre Oppermann 		 * retransmits.
69119bc77c5SAndre Oppermann 		 *
69219bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
69319bc77c5SAndre Oppermann 		 * causes.
694a160e630SAndre Oppermann 		 */
695a160e630SAndre Oppermann 		if (thflags & TH_RST) {
69619bc77c5SAndre Oppermann 			syncache_chkrst(&inc, th);
697a160e630SAndre Oppermann 			goto dropunlock;
698a160e630SAndre Oppermann 		}
699a160e630SAndre Oppermann 		/*
700a160e630SAndre Oppermann 		 * We can't do anything without SYN.
701a160e630SAndre Oppermann 		 */
702a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
703a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
704a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
705773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
7068d573cc1SAndre Oppermann 				    s, __func__);
707a160e630SAndre Oppermann 			tcpstat.tcps_badsyn++;
708a160e630SAndre Oppermann 			goto dropunlock;
709a160e630SAndre Oppermann 		}
710a160e630SAndre Oppermann 		/*
711a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
712a160e630SAndre Oppermann 		 */
713fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
714a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
715a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
7168d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
7178d573cc1SAndre Oppermann 				    s, __func__);
718a160e630SAndre Oppermann 			syncache_badack(&inc);	/* XXX: Not needed! */
719ebb0cbeaSPaul Traina 			tcpstat.tcps_badsyn++;
720a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
721a57815efSBosko Milekic 			goto dropwithreset;
7224195b4afSPaul Traina 		}
723a160e630SAndre Oppermann 		/*
724a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
725a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
726a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
727a160e630SAndre Oppermann 		 * TCP/IP stack.
728a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
729a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
730a160e630SAndre Oppermann 		 * strategies.
7318d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
732a160e630SAndre Oppermann 		 * and was used by RFC1644.
733a160e630SAndre Oppermann 		 */
734a160e630SAndre Oppermann 		if ((thflags & TH_FIN) && drop_synfin) {
735a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
736a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
737773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
7388d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
739a160e630SAndre Oppermann 			tcpstat.tcps_badsyn++;
740302ce8d6SAndre Oppermann                 	goto dropunlock;
741ebb0cbeaSPaul Traina 		}
742be2ac88cSJonathan Lemon 		/*
743be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
744a160e630SAndre Oppermann 		 *
745a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
746a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
747a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
748be2ac88cSJonathan Lemon 		 */
749a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
750a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
751a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
752a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
75333841545SHajimu UMEMOTO #ifdef INET6
75433841545SHajimu UMEMOTO 		/*
75533841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
75633841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
75733841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
75833841545SHajimu UMEMOTO 		 * getting established.
75933841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
76033841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
76133841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
76233841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
76333841545SHajimu UMEMOTO 		 * for the exchange.
76433841545SHajimu UMEMOTO 		 *
76533841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
76633841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
76733841545SHajimu UMEMOTO 		 * SYN in this case.
76833841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
76933841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
77033841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
77133841545SHajimu UMEMOTO 		 *    used"
77233841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
77333841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
77433841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
77533841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
77633841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
77733841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
77833841545SHajimu UMEMOTO 		 *
77933841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
78033841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
78133841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
78233841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
78333841545SHajimu UMEMOTO 		 */
78433841545SHajimu UMEMOTO 		if (isipv6 && !ip6_use_deprecated) {
78533841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
78633841545SHajimu UMEMOTO 
78733841545SHajimu UMEMOTO 			if ((ia6 = ip6_getdstifaddr(m)) &&
78833841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
789a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
790a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
7918d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
7928d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
793a160e630SAndre Oppermann 					s, __func__);
79433841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
79533841545SHajimu UMEMOTO 				goto dropwithreset;
79633841545SHajimu UMEMOTO 			}
79733841545SHajimu UMEMOTO 		}
79833841545SHajimu UMEMOTO #endif
79965f28919SJesper Skriver 		/*
800db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
8018d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
802db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
8038d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
8048d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
8058d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
80693ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
80793ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
80893ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
809be2ac88cSJonathan Lemon 		 */
8108d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
8118d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
8128d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
8138d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
814773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
815302ce8d6SAndre Oppermann 			goto dropunlock;
8168d573cc1SAndre Oppermann 		}
817be2ac88cSJonathan Lemon 		if (isipv6) {
818db33b3e6SAndre Oppermann #ifdef INET6
819db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
820a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
821a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
822a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
8238d573cc1SAndre Oppermann 					"Connection attempt to/from self "
824773673c1SAndre Oppermann 					"ignored\n", s, __func__);
825302ce8d6SAndre Oppermann 				goto dropunlock;
826a160e630SAndre Oppermann 			}
827be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
828a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
829a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
830a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
8318d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
832773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
833302ce8d6SAndre Oppermann 				goto dropunlock;
834a160e630SAndre Oppermann 			}
835db33b3e6SAndre Oppermann #endif
836c068736aSJeffrey Hsu 		} else {
837db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
838a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
839a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
840a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
8418d573cc1SAndre Oppermann 					"Connection attempt from/to self "
842773673c1SAndre Oppermann 					"ignored\n", s, __func__);
843302ce8d6SAndre Oppermann 				goto dropunlock;
844a160e630SAndre Oppermann 			}
845be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
846be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
8472ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
848a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
849a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
850a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
8518d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
852773673c1SAndre Oppermann 					"or multicast address ignored\n",
853a160e630SAndre Oppermann 					s, __func__);
854302ce8d6SAndre Oppermann 				goto dropunlock;
855c068736aSJeffrey Hsu 			}
856a160e630SAndre Oppermann 		}
857be2ac88cSJonathan Lemon 		/*
858db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
859db33b3e6SAndre Oppermann 		 * for syncache.
860be2ac88cSJonathan Lemon 		 */
8613c653157SHartmut Brandt #ifdef TCPDEBUG
8623c653157SHartmut Brandt 		if (so->so_options & SO_DEBUG)
8633c653157SHartmut Brandt 			tcp_trace(TA_INPUT, ostate, tp,
8643c653157SHartmut Brandt 			    (void *)tcp_saveipgen, &tcp_savetcp, 0);
8653c653157SHartmut Brandt #endif
866f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
8674d6e7130SAndre Oppermann 		syncache_add(&inc, &to, th, inp, &so, m);
868be2ac88cSJonathan Lemon 		/*
8694d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
870a160e630SAndre Oppermann 		 * Everything already unlocked by syncache_add().
871be2ac88cSJonathan Lemon 		 */
872a160e630SAndre Oppermann 		INP_INFO_UNLOCK_ASSERT(&tcbinfo);
873be2ac88cSJonathan Lemon 		return;
874f76fcf6dSJeffrey Hsu 	}
875db33b3e6SAndre Oppermann 
876302ce8d6SAndre Oppermann 	/*
8778d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
8788d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
8798d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
880302ce8d6SAndre Oppermann 	 */
881f2512ba1SRui Paulo 	tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos);
882679d9708SAndre Oppermann 	INP_INFO_UNLOCK_ASSERT(&tcbinfo);
883302ce8d6SAndre Oppermann 	return;
884be2ac88cSJonathan Lemon 
885302ce8d6SAndre Oppermann dropwithreset:
886995a7717SAndre Oppermann 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
887302ce8d6SAndre Oppermann 	tcp_dropwithreset(m, th, tp, tlen, rstreason);
888302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
889302ce8d6SAndre Oppermann dropunlock:
890995a7717SAndre Oppermann 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
8919fa198beSAndre Oppermann 	if (inp != NULL)
8928501a69cSRobert Watson 		INP_WUNLOCK(inp);
893302ce8d6SAndre Oppermann 	INP_INFO_WUNLOCK(&tcbinfo);
894302ce8d6SAndre Oppermann drop:
895995a7717SAndre Oppermann 	INP_INFO_UNLOCK_ASSERT(&tcbinfo);
896a160e630SAndre Oppermann 	if (s != NULL)
897a160e630SAndre Oppermann 		free(s, M_TCPLOG);
898302ce8d6SAndre Oppermann 	if (m != NULL)
899302ce8d6SAndre Oppermann 		m_freem(m);
900302ce8d6SAndre Oppermann 	return;
901302ce8d6SAndre Oppermann }
902302ce8d6SAndre Oppermann 
903679d9708SAndre Oppermann static void
904302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
905f2512ba1SRui Paulo     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos)
906302ce8d6SAndre Oppermann {
907302ce8d6SAndre Oppermann 	int thflags, acked, ourfinisacked, needoutput = 0;
908302ce8d6SAndre Oppermann 	int headlocked = 1;
909302ce8d6SAndre Oppermann 	int rstreason, todrop, win;
910302ce8d6SAndre Oppermann 	u_long tiwin;
911302ce8d6SAndre Oppermann 	struct tcpopt to;
912302ce8d6SAndre Oppermann 
91314739780SMaxim Konovalov #ifdef TCPDEBUG
91414739780SMaxim Konovalov 	/*
91514739780SMaxim Konovalov 	 * The size of tcp_saveipgen must be the size of the max ip header,
91614739780SMaxim Konovalov 	 * now IPv6.
91714739780SMaxim Konovalov 	 */
91814739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
91914739780SMaxim Konovalov 	struct tcphdr tcp_savetcp;
92014739780SMaxim Konovalov 	short ostate = 0;
92114739780SMaxim Konovalov #endif
922302ce8d6SAndre Oppermann 	thflags = th->th_flags;
923302ce8d6SAndre Oppermann 
924302ce8d6SAndre Oppermann 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
9258501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
926679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
927679d9708SAndre Oppermann 	    __func__));
928679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
929679d9708SAndre Oppermann 	    __func__));
930df8bae1dSRodney W. Grimes 
931df8bae1dSRodney W. Grimes 	/*
932df8bae1dSRodney W. Grimes 	 * Segment received on connection.
933df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
934e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
935e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
936df8bae1dSRodney W. Grimes 	 */
9379b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
9387ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
939b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_KEEP, tcp_keepidle);
940df8bae1dSRodney W. Grimes 
941df8bae1dSRodney W. Grimes 	/*
942464fcfbcSAndre Oppermann 	 * Unscale the window into a 32-bit value.
943e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
944464fcfbcSAndre Oppermann 	 */
945464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
946464fcfbcSAndre Oppermann 
947464fcfbcSAndre Oppermann 	/*
948f2512ba1SRui Paulo 	 * TCP ECN processing.
949f2512ba1SRui Paulo 	 */
950f2512ba1SRui Paulo 	if (tp->t_flags & TF_ECN_PERMIT) {
951f2512ba1SRui Paulo 		switch (iptos & IPTOS_ECN_MASK) {
952f2512ba1SRui Paulo 		case IPTOS_ECN_CE:
953f2512ba1SRui Paulo 			tp->t_flags |= TF_ECN_SND_ECE;
954f2512ba1SRui Paulo 			tcpstat.tcps_ecn_ce++;
955f2512ba1SRui Paulo 			break;
956f2512ba1SRui Paulo 		case IPTOS_ECN_ECT0:
957f2512ba1SRui Paulo 			tcpstat.tcps_ecn_ect0++;
958f2512ba1SRui Paulo 			break;
959f2512ba1SRui Paulo 		case IPTOS_ECN_ECT1:
960f2512ba1SRui Paulo 			tcpstat.tcps_ecn_ect1++;
961f2512ba1SRui Paulo 			break;
962f2512ba1SRui Paulo 		}
963f2512ba1SRui Paulo 
964f2512ba1SRui Paulo 		if (thflags & TH_CWR)
965f2512ba1SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_ECE;
966f2512ba1SRui Paulo 
967f2512ba1SRui Paulo 		/*
968f2512ba1SRui Paulo 		 * Congestion experienced.
969f2512ba1SRui Paulo 		 * Ignore if we are already trying to recover.
970f2512ba1SRui Paulo 		 */
971f2512ba1SRui Paulo 		if ((thflags & TH_ECE) &&
972f2512ba1SRui Paulo 		    SEQ_LEQ(th->th_ack, tp->snd_recover)) {
973f2512ba1SRui Paulo 			tcpstat.tcps_ecn_rcwnd++;
974f2512ba1SRui Paulo 			tcp_congestion_exp(tp);
975f2512ba1SRui Paulo 		}
976f2512ba1SRui Paulo 	}
977f2512ba1SRui Paulo 
978f2512ba1SRui Paulo 	/*
979f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
980f72167f4SAndre Oppermann 	 */
981302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
982302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
983302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
984f72167f4SAndre Oppermann 
985f72167f4SAndre Oppermann 	/*
986f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
987bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
988bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
989bf6d304aSAndre Oppermann 	 * was established.
990f72167f4SAndre Oppermann 	 */
991bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
992e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
993bf6d304aSAndre Oppermann 		if (TSTMP_GT(to.to_tsecr, ticks))
994f72167f4SAndre Oppermann 			to.to_tsecr = 0;
995bf6d304aSAndre Oppermann 	}
996f72167f4SAndre Oppermann 
997f72167f4SAndre Oppermann 	/*
99897d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
99997d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
10005396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
10015396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
100297d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1003df8bae1dSRodney W. Grimes 	 */
10040a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1005464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1006464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1007be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
100802a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
1009be2ac88cSJonathan Lemon 		}
10105396d0f8SAndre Oppermann 		/*
10115396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
10125396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
10135396d0f8SAndre Oppermann 		 */
10145396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
1015be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1016be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1017be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1018be2ac88cSJonathan Lemon 			tp->ts_recent_age = ticks;
1019be2ac88cSJonathan Lemon 		}
1020be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1021be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
10223529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
10233529149eSAndre Oppermann 		    (to.to_flags & TOF_SACKPERM) == 0)
10243529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
10256d90faf3SPaul Saab 	}
10266d90faf3SPaul Saab 
1027df8bae1dSRodney W. Grimes 	/*
1028df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1029df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1030df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1031df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1032df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1033df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1034df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1035df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1036df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1037df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1038df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1039df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1040a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1041c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1042a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1043df8bae1dSRodney W. Grimes 	 */
1044df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1045c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1046fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1047c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1048c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1049a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1050c5ad39b9SAndre Oppermann 	    LIST_EMPTY(&tp->t_segq) &&
1051be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_TS) == 0 ||
1052c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1053df8bae1dSRodney W. Grimes 
1054df8bae1dSRodney W. Grimes 		/*
1055df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1056df8bae1dSRodney W. Grimes 		 * record the timestamp.
1057a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1058a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1059df8bae1dSRodney W. Grimes 		 */
1060be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1061fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
10629b8b58e0SJonathan Lemon 			tp->ts_recent_age = ticks;
1063a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1064df8bae1dSRodney W. Grimes 		}
1065df8bae1dSRodney W. Grimes 
1066fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1067fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1068fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1069233e8c18SGarrett Wollman 			    tp->snd_cwnd >= tp->snd_wnd &&
10703529149eSAndre Oppermann 			    ((!tcp_do_newreno &&
10713529149eSAndre Oppermann 			      !(tp->t_flags & TF_SACK_PERMIT) &&
1072cb942153SJeffrey Hsu 			      tp->t_dupacks < tcprexmtthresh) ||
10733529149eSAndre Oppermann 			     ((tcp_do_newreno ||
10743529149eSAndre Oppermann 			       (tp->t_flags & TF_SACK_PERMIT)) &&
1075fc30a251SAndre Oppermann 			      !IN_FASTRECOVERY(tp) &&
1076fc30a251SAndre Oppermann 			      (to.to_flags & TOF_SACK) == 0 &&
1077482ac968SPaul Saab 			      TAILQ_EMPTY(&tp->snd_holes)))) {
1078302ce8d6SAndre Oppermann 				KASSERT(headlocked,
1079302ce8d6SAndre Oppermann 				    ("%s: headlocked", __func__));
1080e0bef1cbSRobert Watson 				INP_INFO_WUNLOCK(&tcbinfo);
1081e0bef1cbSRobert Watson 				headlocked = 0;
1082df8bae1dSRodney W. Grimes 				/*
1083e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1084df8bae1dSRodney W. Grimes 				 */
1085df8bae1dSRodney W. Grimes 				++tcpstat.tcps_predack;
10869b8b58e0SJonathan Lemon 				/*
1087e8949f74SAndre Oppermann 				 * "bad retransmit" recovery.
10889b8b58e0SJonathan Lemon 				 */
10899b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
10909b8b58e0SJonathan Lemon 				    ticks < tp->t_badrxtwin) {
1091cb942153SJeffrey Hsu 					++tcpstat.tcps_sndrexmitbad;
10929b8b58e0SJonathan Lemon 					tp->snd_cwnd = tp->snd_cwnd_prev;
10939b8b58e0SJonathan Lemon 					tp->snd_ssthresh =
10949b8b58e0SJonathan Lemon 					    tp->snd_ssthresh_prev;
10959d11646dSJeffrey Hsu 					tp->snd_recover = tp->snd_recover_prev;
10969d11646dSJeffrey Hsu 					if (tp->t_flags & TF_WASFRECOVERY)
10979d11646dSJeffrey Hsu 					    ENTER_FASTRECOVERY(tp);
10989b8b58e0SJonathan Lemon 					tp->snd_nxt = tp->snd_max;
10999b8b58e0SJonathan Lemon 					tp->t_badrxtwin = 0;
11009b8b58e0SJonathan Lemon 				}
1101fa55172bSMatthew Dillon 
1102fa55172bSMatthew Dillon 				/*
1103fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1104fa55172bSMatthew Dillon 				 *
1105fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1106fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1107fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1108fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1109fa55172bSMatthew Dillon 				 */
1110fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1111fa55172bSMatthew Dillon 				    to.to_tsecr) {
1112eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1113eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - to.to_tsecr)
1114eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - to.to_tsecr;
1115a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
11169b8b58e0SJonathan Lemon 					    ticks - to.to_tsecr + 1);
1117fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1118fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1119eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1120eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1121eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1122c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1123c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1124fa55172bSMatthew Dillon 				}
11251fcc99b5SMatthew Dillon 				tcp_xmit_bandwidth_limit(tp, th->th_ack);
1126fb59c426SYoshinobu Inoue 				acked = th->th_ack - tp->snd_una;
1127df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackpack++;
1128df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackbyte += acked;
1129df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
11309d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
11319d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
11329d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
11339d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
11341645d090SJeff Roberson 				/*
1135e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
11361645d090SJeff Roberson 				 * to th_ack.
11371645d090SJeff Roberson 				 */
1138cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1139b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1140df8bae1dSRodney W. Grimes 				m_freem(m);
1141e8949f74SAndre Oppermann 				ND6_HINT(tp); /* Some progress has been made. */
1142df8bae1dSRodney W. Grimes 
1143df8bae1dSRodney W. Grimes 				/*
1144df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1145df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1146df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1147df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1148df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1149df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1150df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1151e8949f74SAndre Oppermann 				 */
11523c653157SHartmut Brandt #ifdef TCPDEBUG
11533c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
11543c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
11553c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
11563c653157SHartmut Brandt 					    &tcp_savetcp, 0);
11573c653157SHartmut Brandt #endif
1158df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1159b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1160b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1161b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
1162b8152ba7SAndre Oppermann 						      tp->t_rxtcur);
1163df8bae1dSRodney W. Grimes 				sowwakeup(so);
1164df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
1165df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1166a14c749fSJonathan Lemon 				goto check_delack;
1167df8bae1dSRodney W. Grimes 			}
1168fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1169fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
11706741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
11716741ecf5SAndre Oppermann 
1172302ce8d6SAndre Oppermann 			KASSERT(headlocked, ("%s: headlocked", __func__));
1173e0bef1cbSRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
1174e0bef1cbSRobert Watson 			headlocked = 0;
1175df8bae1dSRodney W. Grimes 			/*
1176e8949f74SAndre Oppermann 			 * This is a pure, in-sequence data packet
1177df8bae1dSRodney W. Grimes 			 * with nothing on the reassembly queue and
1178df8bae1dSRodney W. Grimes 			 * we have enough buffer space to take it.
1179df8bae1dSRodney W. Grimes 			 */
11806d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
11813529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
11826d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
1183df8bae1dSRodney W. Grimes 			++tcpstat.tcps_preddat;
1184fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
11851645d090SJeff Roberson 			/*
11861645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
11871645d090SJeff Roberson 			 * th_seq.
11881645d090SJeff Roberson 			 */
11891645d090SJeff Roberson 			tp->snd_wl1 = th->th_seq;
11901645d090SJeff Roberson 			/*
11911645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
11921645d090SJeff Roberson 			 * rcv_nxt.
11931645d090SJeff Roberson 			 */
11941645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
1195df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpack++;
1196fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyte += tlen;
1197e8949f74SAndre Oppermann 			ND6_HINT(tp);	/* Some progress has been made */
11983c653157SHartmut Brandt #ifdef TCPDEBUG
11993c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
12003c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
12013c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
12023c653157SHartmut Brandt #endif
12036741ecf5SAndre Oppermann 		/*
12046741ecf5SAndre Oppermann 		 * Automatic sizing of receive socket buffer.  Often the send
12056741ecf5SAndre Oppermann 		 * buffer size is not optimally adjusted to the actual network
12066741ecf5SAndre Oppermann 		 * conditions at hand (delay bandwidth product).  Setting the
12076741ecf5SAndre Oppermann 		 * buffer size too small limits throughput on links with high
12086741ecf5SAndre Oppermann 		 * bandwidth and high delay (eg. trans-continental/oceanic links).
12096741ecf5SAndre Oppermann 		 *
12106741ecf5SAndre Oppermann 		 * On the receive side the socket buffer memory is only rarely
12116741ecf5SAndre Oppermann 		 * used to any significant extent.  This allows us to be much
12126741ecf5SAndre Oppermann 		 * more aggressive in scaling the receive socket buffer.  For
12136741ecf5SAndre Oppermann 		 * the case that the buffer space is actually used to a large
12146741ecf5SAndre Oppermann 		 * extent and we run out of kernel memory we can simply drop
12156741ecf5SAndre Oppermann 		 * the new segments; TCP on the sender will just retransmit it
12166741ecf5SAndre Oppermann 		 * later.  Setting the buffer size too big may only consume too
12176741ecf5SAndre Oppermann 		 * much kernel memory if the application doesn't read() from
12186741ecf5SAndre Oppermann 		 * the socket or packet loss or reordering makes use of the
12196741ecf5SAndre Oppermann 		 * reassembly queue.
12206741ecf5SAndre Oppermann 		 *
12216741ecf5SAndre Oppermann 		 * The criteria to step up the receive buffer one notch are:
12226741ecf5SAndre Oppermann 		 *  1. the number of bytes received during the time it takes
12236741ecf5SAndre Oppermann 		 *     one timestamp to be reflected back to us (the RTT);
12246741ecf5SAndre Oppermann 		 *  2. received bytes per RTT is within seven eighth of the
12256741ecf5SAndre Oppermann 		 *     current socket buffer size;
12266741ecf5SAndre Oppermann 		 *  3. receive buffer size has not hit maximal automatic size;
12276741ecf5SAndre Oppermann 		 *
12286741ecf5SAndre Oppermann 		 * This algorithm does one step per RTT at most and only if
12296741ecf5SAndre Oppermann 		 * we receive a bulk stream w/o packet losses or reorderings.
12306741ecf5SAndre Oppermann 		 * Shrinking the buffer during idle times is not necessary as
12316741ecf5SAndre Oppermann 		 * it doesn't consume any memory when idle.
12326741ecf5SAndre Oppermann 		 *
12336741ecf5SAndre Oppermann 		 * TODO: Only step up if the application is actually serving
12346741ecf5SAndre Oppermann 		 * the buffer to better manage the socket buffer resources.
1235df8bae1dSRodney W. Grimes 		 */
12366741ecf5SAndre Oppermann 			if (tcp_do_autorcvbuf &&
12376741ecf5SAndre Oppermann 			    to.to_tsecr &&
12386741ecf5SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
12396741ecf5SAndre Oppermann 				if (to.to_tsecr > tp->rfbuf_ts &&
12406741ecf5SAndre Oppermann 				    to.to_tsecr - tp->rfbuf_ts < hz) {
12416741ecf5SAndre Oppermann 					if (tp->rfbuf_cnt >
12426741ecf5SAndre Oppermann 					    (so->so_rcv.sb_hiwat / 8 * 7) &&
12436741ecf5SAndre Oppermann 					    so->so_rcv.sb_hiwat <
12446741ecf5SAndre Oppermann 					    tcp_autorcvbuf_max) {
12456741ecf5SAndre Oppermann 						newsize =
12466741ecf5SAndre Oppermann 						    min(so->so_rcv.sb_hiwat +
12476741ecf5SAndre Oppermann 						    tcp_autorcvbuf_inc,
12486741ecf5SAndre Oppermann 						    tcp_autorcvbuf_max);
12496741ecf5SAndre Oppermann 					}
12506741ecf5SAndre Oppermann 					/* Start over with next RTT. */
12516741ecf5SAndre Oppermann 					tp->rfbuf_ts = 0;
12526741ecf5SAndre Oppermann 					tp->rfbuf_cnt = 0;
12536741ecf5SAndre Oppermann 				} else
12546741ecf5SAndre Oppermann 					tp->rfbuf_cnt += tlen;	/* add up */
12556741ecf5SAndre Oppermann 			}
12566741ecf5SAndre Oppermann 
12576741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
12581e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1259c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1260c1c36a2cSMike Silbersack 				m_freem(m);
1261c1c36a2cSMike Silbersack 			} else {
12626741ecf5SAndre Oppermann 				/*
12636741ecf5SAndre Oppermann 				 * Set new socket buffer size.
12646741ecf5SAndre Oppermann 				 * Give up when limit is reached.
12656741ecf5SAndre Oppermann 				 */
12666741ecf5SAndre Oppermann 				if (newsize)
12676741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
12686741ecf5SAndre Oppermann 					    newsize, so, curthread))
12696741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1270fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
12711e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
1272c1c36a2cSMike Silbersack 			}
1273e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
12741e4d7da7SRobert Watson 			sorwakeup_locked(so);
1275d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp)) {
12763bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1277f498eeeeSDavid Greenman 			} else {
1278e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
1279e612a582SDavid Greenman 				tcp_output(tp);
1280e612a582SDavid Greenman 			}
1281a14c749fSJonathan Lemon 			goto check_delack;
1282df8bae1dSRodney W. Grimes 		}
1283df8bae1dSRodney W. Grimes 	}
1284df8bae1dSRodney W. Grimes 
1285df8bae1dSRodney W. Grimes 	/*
1286df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1287df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1288df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1289df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1290df8bae1dSRodney W. Grimes 	 */
1291df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1292df8bae1dSRodney W. Grimes 	if (win < 0)
1293df8bae1dSRodney W. Grimes 		win = 0;
129466e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1295df8bae1dSRodney W. Grimes 
12966741ecf5SAndre Oppermann 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
12976741ecf5SAndre Oppermann 	tp->rfbuf_ts = 0;
12986741ecf5SAndre Oppermann 	tp->rfbuf_cnt = 0;
12996741ecf5SAndre Oppermann 
1300df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1301df8bae1dSRodney W. Grimes 
1302df8bae1dSRodney W. Grimes 	/*
1303764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1304764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1305764d8cefSBill Fenner 	 */
1306764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1307fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1308fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1309a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1310a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1311a57815efSBosko Milekic 				goto dropwithreset;
1312a57815efSBosko Milekic 		}
1313764d8cefSBill Fenner 		break;
1314764d8cefSBill Fenner 
1315764d8cefSBill Fenner 	/*
1316df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1317df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1318df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1319df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1320df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1321df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1322df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1323f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
1324f2512ba1SRui Paulo 	 *	    is ECN capable.
1325df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1326df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1327df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1328df8bae1dSRodney W. Grimes 	 */
1329df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1330fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1331fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1332fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1333a57815efSBosko Milekic 			rstreason = BANDLIM_UNLIMITED;
1334a0292f23SGarrett Wollman 			goto dropwithreset;
1335a0292f23SGarrett Wollman 		}
13360ca3f933SAndre Oppermann 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST))
1337df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
13380ca3f933SAndre Oppermann 		if (thflags & TH_RST)
1339df8bae1dSRodney W. Grimes 			goto drop;
13400ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
1341df8bae1dSRodney W. Grimes 			goto drop;
1342464fcfbcSAndre Oppermann 
1343fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1344df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1345fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1346845799c1SAndras Olah 			tcpstat.tcps_connects++;
1347845799c1SAndras Olah 			soisconnected(so);
1348c488362eSRobert Watson #ifdef MAC
1349310e7cebSRobert Watson 			SOCK_LOCK(so);
135030d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
1351310e7cebSRobert Watson 			SOCK_UNLOCK(so);
1352c488362eSRobert Watson #endif
1353845799c1SAndras Olah 			/* Do window scaling on this connection? */
1354845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1355845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1356845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1357845799c1SAndras Olah 			}
1358a0292f23SGarrett Wollman 			tp->rcv_adv += tp->rcv_wnd;
1359a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1360a0292f23SGarrett Wollman 			/*
1361a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1362a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1363a0292f23SGarrett Wollman 			 */
1364d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && tlen != 0)
1365b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
1366b8152ba7SAndre Oppermann 				    tcp_delacktime);
1367a0292f23SGarrett Wollman 			else
1368a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1369f2512ba1SRui Paulo 
1370f2512ba1SRui Paulo 			if ((thflags & TH_ECE) && tcp_do_ecn) {
1371f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_PERMIT;
1372f2512ba1SRui Paulo 				tcpstat.tcps_ecn_shs++;
1373f2512ba1SRui Paulo 			}
1374f2512ba1SRui Paulo 
1375a0292f23SGarrett Wollman 			/*
1376a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1377a0292f23SGarrett Wollman 			 * Transitions:
1378a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1379a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1380a0292f23SGarrett Wollman 			 */
13819b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1382a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
1383a0292f23SGarrett Wollman 				tp->t_state = TCPS_FIN_WAIT_1;
1384a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1385fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
13867ff19458SPaul Traina 			} else {
1387a0292f23SGarrett Wollman 				tp->t_state = TCPS_ESTABLISHED;
1388b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_KEEP, tcp_keepidle);
13897ff19458SPaul Traina 			}
1390a0292f23SGarrett Wollman 		} else {
1391a0292f23SGarrett Wollman 			/*
1392c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
1393c068736aSJeffrey Hsu 			 * simultaneous open.  If segment contains CC option
1394c068736aSJeffrey Hsu 			 * and there is a cached CC, apply TAO test.
1395c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
1396c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
1397a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
1398a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
1399a0292f23SGarrett Wollman 			 * If there was no CC option, clear cached CC value.
1400a0292f23SGarrett Wollman 			 */
14014b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
1402b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
1403df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_SYN_RECEIVED;
1404a0292f23SGarrett Wollman 		}
1405df8bae1dSRodney W. Grimes 
1406302ce8d6SAndre Oppermann 		KASSERT(headlocked, ("%s: trimthenstep6: head not locked",
1407302ce8d6SAndre Oppermann 		    __func__));
14088501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
14097cfc6904SRobert Watson 
1410df8bae1dSRodney W. Grimes 		/*
1411fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
1412df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
1413df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
1414df8bae1dSRodney W. Grimes 		 */
1415fb59c426SYoshinobu Inoue 		th->th_seq++;
1416fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
1417fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
1418df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
1419fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
1420fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1421df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpackafterwin++;
1422df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1423df8bae1dSRodney W. Grimes 		}
1424fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1425fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
1426a0292f23SGarrett Wollman 		/*
1427a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
1428a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
1429a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
1430a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
1431a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
1432a0292f23SGarrett Wollman 		 */
1433fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
1434a0292f23SGarrett Wollman 			goto process_ACK;
1435c068736aSJeffrey Hsu 
1436df8bae1dSRodney W. Grimes 		goto step6;
1437c068736aSJeffrey Hsu 
1438a0292f23SGarrett Wollman 	/*
1439a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1440c94c54e4SAndre Oppermann 	 *      do normal processing.
1441a0292f23SGarrett Wollman 	 *
1442c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
1443a0292f23SGarrett Wollman 	 */
1444a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
1445a0292f23SGarrett Wollman 	case TCPS_CLOSING:
1446a0292f23SGarrett Wollman 		break;  /* continue normal processing */
1447df8bae1dSRodney W. Grimes 	}
1448df8bae1dSRodney W. Grimes 
1449df8bae1dSRodney W. Grimes 	/*
1450df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
145180ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
145280ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
145380ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
145480ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
145580ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
145680ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
145780ab7c0eSGarrett Wollman 	 * killing a connection).
145880ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
1459a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
1460df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
1461df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
1462df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
1463df8bae1dSRodney W. Grimes 	 *
146480ab7c0eSGarrett Wollman 	 *
146580ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
146680ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
146780ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
146880ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
146980ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
147080ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
147180ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
147280ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
14731a244a61SJonathan Lemon 	 *   The sequence number in the reset segment is normally an
14741a244a61SJonathan Lemon 	 *   echo of our outgoing acknowlegement numbers, but some hosts
14751a244a61SJonathan Lemon 	 *   send a reset with the sequence number at the rightmost edge
14761a244a61SJonathan Lemon 	 *   of our receive window, and we have to handle this case.
147780dd2a81SMike Silbersack 	 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
147880dd2a81SMike Silbersack 	 *   that brute force RST attacks are possible.  To combat this,
147980dd2a81SMike Silbersack 	 *   we use a much stricter check while in the ESTABLISHED state,
148080dd2a81SMike Silbersack 	 *   only accepting RSTs where the sequence number is equal to
148180dd2a81SMike Silbersack 	 *   last_ack_sent.  In all other states (the states in which a
148280dd2a81SMike Silbersack 	 *   RST is more likely), the more permissive check is used.
148380ab7c0eSGarrett Wollman 	 * If we have multiple segments in flight, the intial reset
148480ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
148580ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
148680ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
148780ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
148880ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
148980ab7c0eSGarrett Wollman 	 *
149080ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
149180ab7c0eSGarrett Wollman 	 *
149280ab7c0eSGarrett Wollman 	 *    DISCUSSION
149380ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
149480ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
149580ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
149680ab7c0eSGarrett Wollman 	 *         data.
149780ab7c0eSGarrett Wollman 	 *
149880ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
149980ab7c0eSGarrett Wollman 	 * the state:
150080ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
150180ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
150280ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
1503745bab7fSDima Dorfman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
150480ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
1505e9bd3a37SJonathan M. Bresler 	 *    CLOSING, LAST_ACK STATES:
150680ab7c0eSGarrett Wollman 	 *	Close the tcb.
1507e9bd3a37SJonathan M. Bresler 	 *    TIME_WAIT STATE:
150880ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
150980ab7c0eSGarrett Wollman 	 *      RFC 1337.
151080ab7c0eSGarrett Wollman 	 */
1511fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
151295ad8418SQing Li 		if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) &&
151395ad8418SQing Li 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
151480ab7c0eSGarrett Wollman 			switch (tp->t_state) {
151580ab7c0eSGarrett Wollman 
151680ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
151780ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
151880ab7c0eSGarrett Wollman 				goto close;
151980ab7c0eSGarrett Wollman 
152080ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
152195ad8418SQing Li 				if (tcp_insecure_rst == 0 &&
152295ad8418SQing Li 				    !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) &&
152395ad8418SQing Li 				    SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) &&
152495ad8418SQing Li 				    !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) &&
152595ad8418SQing Li 				    SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) {
152680dd2a81SMike Silbersack 					tcpstat.tcps_badrst++;
152780dd2a81SMike Silbersack 					goto drop;
152880dd2a81SMike Silbersack 				}
1529564aab1fSAndre Oppermann 				/* FALLTHROUGH */
153080ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
153180ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
153280ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
153380ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
153480ab7c0eSGarrett Wollman 			close:
153580ab7c0eSGarrett Wollman 				tp->t_state = TCPS_CLOSED;
153680ab7c0eSGarrett Wollman 				tcpstat.tcps_drops++;
1537302ce8d6SAndre Oppermann 				KASSERT(headlocked, ("%s: trimthenstep6: "
1538302ce8d6SAndre Oppermann 				    "tcp_close: head not locked", __func__));
153980ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
154080ab7c0eSGarrett Wollman 				break;
154180ab7c0eSGarrett Wollman 
154280ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
154380ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
1544302ce8d6SAndre Oppermann 				KASSERT(headlocked, ("%s: trimthenstep6: "
1545302ce8d6SAndre Oppermann 				    "tcp_close.2: head not locked", __func__));
154680ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
154780ab7c0eSGarrett Wollman 				break;
154880ab7c0eSGarrett Wollman 			}
154980ab7c0eSGarrett Wollman 		}
155080ab7c0eSGarrett Wollman 		goto drop;
155180ab7c0eSGarrett Wollman 	}
155280ab7c0eSGarrett Wollman 
155380ab7c0eSGarrett Wollman 	/*
1554df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1555df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
1556df8bae1dSRodney W. Grimes 	 */
1557be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
155880ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1559df8bae1dSRodney W. Grimes 
1560df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
15619b8b58e0SJonathan Lemon 		if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1562df8bae1dSRodney W. Grimes 			/*
1563df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
1564df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
1565df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
1566df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
1567df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
1568df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
1569df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
1570df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
1571df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
1572df8bae1dSRodney W. Grimes 			 */
1573df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
1574df8bae1dSRodney W. Grimes 		} else {
1575df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
1576fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvdupbyte += tlen;
1577df8bae1dSRodney W. Grimes 			tcpstat.tcps_pawsdrop++;
157807fd333dSMatthew Dillon 			if (tlen)
1579df8bae1dSRodney W. Grimes 				goto dropafterack;
15801ab4789dSMatthew Dillon 			goto drop;
15811ab4789dSMatthew Dillon 		}
1582df8bae1dSRodney W. Grimes 	}
1583df8bae1dSRodney W. Grimes 
1584a0292f23SGarrett Wollman 	/*
158580ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
158680ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
158780ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
158880ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
158980ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
159080ab7c0eSGarrett Wollman 	 */
1591a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1592a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
1593a57815efSBosko Milekic 		goto dropwithreset;
1594a57815efSBosko Milekic 	}
159580ab7c0eSGarrett Wollman 
1596fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
1597df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1598fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN) {
1599fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
1600fb59c426SYoshinobu Inoue 			th->th_seq++;
1601fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
1602fb59c426SYoshinobu Inoue 				th->th_urp--;
1603df8bae1dSRodney W. Grimes 			else
1604fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
1605df8bae1dSRodney W. Grimes 			todrop--;
1606df8bae1dSRodney W. Grimes 		}
1607df8bae1dSRodney W. Grimes 		/*
1608dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
1609df8bae1dSRodney W. Grimes 		 */
1610fb59c426SYoshinobu Inoue 		if (todrop > tlen
1611fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1612dac20301SGarrett Wollman 			/*
1613dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
1614dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
1615dac20301SGarrett Wollman 			 * of sequence; drop it.
1616dac20301SGarrett Wollman 			 */
1617fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1618dac20301SGarrett Wollman 
1619df8bae1dSRodney W. Grimes 			/*
1620dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
1621dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
1622df8bae1dSRodney W. Grimes 			 */
1623dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
1624fb59c426SYoshinobu Inoue 			todrop = tlen;
1625dac20301SGarrett Wollman 			tcpstat.tcps_rcvduppack++;
1626dac20301SGarrett Wollman 			tcpstat.tcps_rcvdupbyte += todrop;
1627df8bae1dSRodney W. Grimes 		} else {
1628df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartduppack++;
1629df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartdupbyte += todrop;
1630df8bae1dSRodney W. Grimes 		}
1631fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
1632fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
1633fb59c426SYoshinobu Inoue 		tlen -= todrop;
1634fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
1635fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
1636df8bae1dSRodney W. Grimes 		else {
1637fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
1638fb59c426SYoshinobu Inoue 			th->th_urp = 0;
1639df8bae1dSRodney W. Grimes 		}
1640df8bae1dSRodney W. Grimes 	}
1641df8bae1dSRodney W. Grimes 
1642df8bae1dSRodney W. Grimes 	/*
1643df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
1644df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
1645df8bae1dSRodney W. Grimes 	 */
1646df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
1647fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1648773673c1SAndre Oppermann 		char *s;
1649773673c1SAndre Oppermann 
1650302ce8d6SAndre Oppermann 		KASSERT(headlocked, ("%s: trimthenstep6: tcp_close.3: head "
1651302ce8d6SAndre Oppermann 		    "not locked", __func__));
1652773673c1SAndre Oppermann 		if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) {
1653e31d8aa3SMike Silbersack 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket "
1654773673c1SAndre Oppermann 			    "was closed, sending RST and removing tcpcb\n",
1655e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
1656773673c1SAndre Oppermann 			free(s, M_TCPLOG);
1657773673c1SAndre Oppermann 		}
1658df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1659df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvafterclose++;
1660a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1661df8bae1dSRodney W. Grimes 		goto dropwithreset;
1662df8bae1dSRodney W. Grimes 	}
1663df8bae1dSRodney W. Grimes 
1664df8bae1dSRodney W. Grimes 	/*
1665df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
1666df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
1667df8bae1dSRodney W. Grimes 	 */
1668fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
1669df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1670df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpackafterwin++;
1671fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
1672fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyteafterwin += tlen;
1673df8bae1dSRodney W. Grimes 			/*
1674df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
1675df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
1676df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
1677df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
1678df8bae1dSRodney W. Grimes 			 * and ack.
1679df8bae1dSRodney W. Grimes 			 */
1680fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1681df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1682df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvwinprobe++;
1683df8bae1dSRodney W. Grimes 			} else
1684df8bae1dSRodney W. Grimes 				goto dropafterack;
1685df8bae1dSRodney W. Grimes 		} else
1686df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1687df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
1688fb59c426SYoshinobu Inoue 		tlen -= todrop;
1689fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
1690df8bae1dSRodney W. Grimes 	}
1691df8bae1dSRodney W. Grimes 
1692df8bae1dSRodney W. Grimes 	/*
1693df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
1694df8bae1dSRodney W. Grimes 	 * record its timestamp.
1695cf09195bSPaul Saab 	 * NOTE:
1696cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
1697a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
1698cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
1699cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
1700cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
1701cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
1702cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
1703cf09195bSPaul Saab 	 *    instead of RFC1323's
1704cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
1705cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
1706cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
1707cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
1708cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
1709df8bae1dSRodney W. Grimes 	 */
1710be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
1711cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
1712cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
1713cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
17149b8b58e0SJonathan Lemon 		tp->ts_recent_age = ticks;
1715a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
1716df8bae1dSRodney W. Grimes 	}
1717df8bae1dSRodney W. Grimes 
1718df8bae1dSRodney W. Grimes 	/*
1719df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
1720df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
1721df8bae1dSRodney W. Grimes 	 */
1722fb59c426SYoshinobu Inoue 	if (thflags & TH_SYN) {
1723302ce8d6SAndre Oppermann 		KASSERT(headlocked, ("%s: tcp_drop: trimthenstep6: "
1724302ce8d6SAndre Oppermann 		    "head not locked", __func__));
1725df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
1726a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1727122aad88SAndre Oppermann 		goto drop;
1728df8bae1dSRodney W. Grimes 	}
1729df8bae1dSRodney W. Grimes 
1730a0292f23SGarrett Wollman 	/*
1731a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1732a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
1733a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
1734a0292f23SGarrett Wollman 	 */
1735fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
1736a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
1737a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
1738a0292f23SGarrett Wollman 			goto step6;
17395e1aa279SDavid Malone 		else if (tp->t_flags & TF_ACKNOW)
17405e1aa279SDavid Malone 			goto dropafterack;
1741a0292f23SGarrett Wollman 		else
1742a0292f23SGarrett Wollman 			goto drop;
1743a0292f23SGarrett Wollman 	}
1744df8bae1dSRodney W. Grimes 
1745df8bae1dSRodney W. Grimes 	/*
1746df8bae1dSRodney W. Grimes 	 * Ack processing.
1747df8bae1dSRodney W. Grimes 	 */
1748df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1749df8bae1dSRodney W. Grimes 
1750df8bae1dSRodney W. Grimes 	/*
1751764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1752764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
1753764d8cefSBill Fenner 	 * The ACK was checked above.
1754df8bae1dSRodney W. Grimes 	 */
1755df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1756a0292f23SGarrett Wollman 
1757df8bae1dSRodney W. Grimes 		tcpstat.tcps_connects++;
1758df8bae1dSRodney W. Grimes 		soisconnected(so);
1759df8bae1dSRodney W. Grimes 		/* Do window scaling? */
1760df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1761df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1762df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
1763464fcfbcSAndre Oppermann 			tp->snd_wnd = tiwin;
1764df8bae1dSRodney W. Grimes 		}
1765a0292f23SGarrett Wollman 		/*
1766a0292f23SGarrett Wollman 		 * Make transitions:
1767a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
1768a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
1769a0292f23SGarrett Wollman 		 */
17709b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
1771a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
1772a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
1773a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
17747ff19458SPaul Traina 		} else {
1775a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
1776b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_KEEP, tcp_keepidle);
17777ff19458SPaul Traina 		}
1778a0292f23SGarrett Wollman 		/*
1779a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
1780a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
1781a0292f23SGarrett Wollman 		 */
1782fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
1783fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
1784a0292f23SGarrett Wollman 			    (struct mbuf *)0);
1785fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
178693b0017fSPhilippe Charnier 		/* FALLTHROUGH */
1787df8bae1dSRodney W. Grimes 
1788df8bae1dSRodney W. Grimes 	/*
1789df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1790df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
1791fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
1792fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
1793df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
1794df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
1795df8bae1dSRodney W. Grimes 	 */
1796df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1797df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
1798df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
1799df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1800df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
1801df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
18025a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
18035a53ca16SPaul Saab 			tcpstat.tcps_rcvacktoomuch++;
18045a53ca16SPaul Saab 			goto dropafterack;
18055a53ca16SPaul Saab 		}
18063529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
1807fc30a251SAndre Oppermann 		    ((to.to_flags & TOF_SACK) ||
1808fc30a251SAndre Oppermann 		     !TAILQ_EMPTY(&tp->snd_holes)))
18095a53ca16SPaul Saab 			tcp_sack_doack(tp, &to, th->th_ack);
1810fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1811fb59c426SYoshinobu Inoue 			if (tlen == 0 && tiwin == tp->snd_wnd) {
1812df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupack++;
1813df8bae1dSRodney W. Grimes 				/*
1814df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
1815df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
1816df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
1817df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
1818df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
1819df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
1820df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
1821df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
1822df8bae1dSRodney W. Grimes 				 * window so we send only this one
1823df8bae1dSRodney W. Grimes 				 * packet.
1824df8bae1dSRodney W. Grimes 				 *
1825df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
1826df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
1827df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
1828df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
1829df8bae1dSRodney W. Grimes 				 * the new ssthresh).
1830df8bae1dSRodney W. Grimes 				 *
1831df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
1832df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
1833df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
1834df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
1835df8bae1dSRodney W. Grimes 				 * network.
1836f2512ba1SRui Paulo 				 *
1837f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
1838f2512ba1SRui Paulo 				 * we reduced the cwnd.
1839df8bae1dSRodney W. Grimes 				 */
1840b8152ba7SAndre Oppermann 				if (!tcp_timer_active(tp, TT_REXMT) ||
1841fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
1842df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
1843cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
18443529149eSAndre Oppermann 				    ((tcp_do_newreno ||
18453529149eSAndre Oppermann 				      (tp->t_flags & TF_SACK_PERMIT)) &&
18469d11646dSJeffrey Hsu 				     IN_FASTRECOVERY(tp))) {
18473529149eSAndre Oppermann 					if ((tp->t_flags & TF_SACK_PERMIT) &&
18483529149eSAndre Oppermann 					    IN_FASTRECOVERY(tp)) {
1849808f11b7SPaul Saab 						int awnd;
185025e6f9edSPaul Saab 
185125e6f9edSPaul Saab 						/*
185225e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
185325e6f9edSPaul Saab 						 * We can inject new data into the pipe iff
185425e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
185525e6f9edSPaul Saab 						 * worth of data in flight.
185625e6f9edSPaul Saab 						 */
1857808f11b7SPaul Saab 						awnd = (tp->snd_nxt - tp->snd_fack) +
18580077b016SPaul Saab 							tp->sackhint.sack_bytes_rexmit;
1859808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
186025e6f9edSPaul Saab 							tp->snd_cwnd += tp->t_maxseg;
186125e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
186225e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
186325e6f9edSPaul Saab 						}
186425e6f9edSPaul Saab 					} else
186546f58482SJonathan Lemon 						tp->snd_cwnd += tp->t_maxseg;
186646f58482SJonathan Lemon 					(void) tcp_output(tp);
186746f58482SJonathan Lemon 					goto drop;
1868cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
1869cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
1870a0445c2eSJayanth Vijayaraghavan 
1871a0445c2eSJayanth Vijayaraghavan 					/*
1872a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
1873a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
1874a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
1875a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
1876a0445c2eSJayanth Vijayaraghavan 					 * recovery.
1877a0445c2eSJayanth Vijayaraghavan 					 */
18783529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
1879a0445c2eSJayanth Vijayaraghavan 						if (IN_FASTRECOVERY(tp)) {
1880a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
1881a0445c2eSJayanth Vijayaraghavan 							break;
1882a0445c2eSJayanth Vijayaraghavan 						}
1883f2512ba1SRui Paulo 					} else if (tcp_do_newreno ||
1884f2512ba1SRui Paulo 					    tcp_do_ecn) {
1885a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
18869d11646dSJeffrey Hsu 						    tp->snd_recover)) {
1887cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
1888cb942153SJeffrey Hsu 							break;
188946f58482SJonathan Lemon 						}
1890a0445c2eSJayanth Vijayaraghavan 					}
1891f2512ba1SRui Paulo 					tcp_congestion_exp(tp);
1892b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
18939b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
18943529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
18956d90faf3SPaul Saab 						tcpstat.tcps_sack_recovery_episode++;
1896a55db2b6SPaul Saab 						tp->sack_newdata = tp->snd_nxt;
18978db456bfSPaul Saab 						tp->snd_cwnd = tp->t_maxseg;
18986d90faf3SPaul Saab 						(void) tcp_output(tp);
18996d90faf3SPaul Saab 						goto drop;
19006d90faf3SPaul Saab 					}
1901fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
1902df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
1903df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
190448d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
1905302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
1906302ce8d6SAndre Oppermann 					    __func__));
1907df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
190848d2549cSJeffrey Hsu 					     tp->t_maxseg *
190948d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
1910df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
1911df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
1912df8bae1dSRodney W. Grimes 					goto drop;
1913582a954bSJeffrey Hsu 				} else if (tcp_do_rfc3042) {
1914582a954bSJeffrey Hsu 					u_long oldcwnd = tp->snd_cwnd;
191548d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
191648d2549cSJeffrey Hsu 					u_int sent;
191789c02376SJeffrey Hsu 
1918582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
1919582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
1920302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
1921302ce8d6SAndre Oppermann 					    __func__));
192261a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
192348d2549cSJeffrey Hsu 						tp->snd_limited = 0;
192461a36e3dSJeffrey Hsu 					tp->snd_cwnd =
192561a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
192661a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
192761a36e3dSJeffrey Hsu 					    tp->t_maxseg;
1928582a954bSJeffrey Hsu 					(void) tcp_output(tp);
192948d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
193048d2549cSJeffrey Hsu 					if (sent > tp->t_maxseg) {
193189c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
193289c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
193389c02376SJeffrey Hsu 						   (sent == tp->t_maxseg + 1 &&
193489c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
1935302ce8d6SAndre Oppermann 						    ("%s: sent too much",
1936302ce8d6SAndre Oppermann 						    __func__));
193748d2549cSJeffrey Hsu 						tp->snd_limited = 2;
193848d2549cSJeffrey Hsu 					} else if (sent > 0)
193948d2549cSJeffrey Hsu 						++tp->snd_limited;
1940582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
1941582a954bSJeffrey Hsu 					goto drop;
1942df8bae1dSRodney W. Grimes 				}
1943df8bae1dSRodney W. Grimes 			} else
1944df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
1945df8bae1dSRodney W. Grimes 			break;
1946df8bae1dSRodney W. Grimes 		}
1947cb942153SJeffrey Hsu 
1948302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
1949302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
1950cb942153SJeffrey Hsu 
1951df8bae1dSRodney W. Grimes 		/*
1952df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
1953df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
1954df8bae1dSRodney W. Grimes 		 */
19553529149eSAndre Oppermann 		if (tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) {
19569d11646dSJeffrey Hsu 			if (IN_FASTRECOVERY(tp)) {
1957cb942153SJeffrey Hsu 				if (SEQ_LT(th->th_ack, tp->snd_recover)) {
19583529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT)
19596d90faf3SPaul Saab 						tcp_sack_partialack(tp, th);
19606d90faf3SPaul Saab 					else
1961c068736aSJeffrey Hsu 						tcp_newreno_partial_ack(tp, th);
1962c068736aSJeffrey Hsu 				} else {
1963c068736aSJeffrey Hsu 					/*
19646d90faf3SPaul Saab 					 * Out of fast recovery.
1965c068736aSJeffrey Hsu 					 * Window inflation should have left us
1966c068736aSJeffrey Hsu 					 * with approximately snd_ssthresh
1967c068736aSJeffrey Hsu 					 * outstanding data.
1968c068736aSJeffrey Hsu 					 * But in case we would be inclined to
1969c068736aSJeffrey Hsu 					 * send a burst, better to do it via
1970c068736aSJeffrey Hsu 					 * the slow start mechanism.
1971c068736aSJeffrey Hsu 					 */
1972c068736aSJeffrey Hsu 					if (SEQ_GT(th->th_ack +
1973c068736aSJeffrey Hsu 							tp->snd_ssthresh,
1974c068736aSJeffrey Hsu 						   tp->snd_max))
1975c068736aSJeffrey Hsu 						tp->snd_cwnd = tp->snd_max -
1976c068736aSJeffrey Hsu 								th->th_ack +
1977c068736aSJeffrey Hsu 								tp->t_maxseg;
1978c068736aSJeffrey Hsu 					else
1979c068736aSJeffrey Hsu 						tp->snd_cwnd = tp->snd_ssthresh;
1980c068736aSJeffrey Hsu 				}
1981c068736aSJeffrey Hsu 			}
1982c068736aSJeffrey Hsu 		} else {
1983233e8c18SGarrett Wollman 			if (tp->t_dupacks >= tcprexmtthresh &&
1984df8bae1dSRodney W. Grimes 			    tp->snd_cwnd > tp->snd_ssthresh)
1985df8bae1dSRodney W. Grimes 				tp->snd_cwnd = tp->snd_ssthresh;
198646f58482SJonathan Lemon 		}
1987cb942153SJeffrey Hsu 		tp->t_dupacks = 0;
1988a0292f23SGarrett Wollman 		/*
1989a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
1990a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
1991a0292f23SGarrett Wollman 		 */
1992a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
1993a0292f23SGarrett Wollman 			/*
1994a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
1995a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
199607e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
199707e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
199807e43e10SAndras Olah 			 * we can do window scaling.
1999a0292f23SGarrett Wollman 			 */
2000a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2001a0292f23SGarrett Wollman 			tp->snd_una++;
200207e43e10SAndras Olah 			/* Do window scaling? */
200307e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
200407e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
200507e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2006464fcfbcSAndre Oppermann 				/* Send window already scaled. */
200707e43e10SAndras Olah 			}
2008a0292f23SGarrett Wollman 		}
2009a0292f23SGarrett Wollman 
2010a0292f23SGarrett Wollman process_ACK:
2011302ce8d6SAndre Oppermann 		KASSERT(headlocked, ("%s: process_ACK: head not locked",
2012302ce8d6SAndre Oppermann 		    __func__));
20138501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
20147cfc6904SRobert Watson 
2015fb59c426SYoshinobu Inoue 		acked = th->th_ack - tp->snd_una;
2016df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackpack++;
2017df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackbyte += acked;
2018df8bae1dSRodney W. Grimes 
2019df8bae1dSRodney W. Grimes 		/*
20209b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
20219b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
20229b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
20239b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
20249b8b58e0SJonathan Lemon 		 * we left off.
20259b8b58e0SJonathan Lemon 		 */
20269b8b58e0SJonathan Lemon 		if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
2027d65bf08aSMatthew Dillon 			++tcpstat.tcps_sndrexmitbad;
20289b8b58e0SJonathan Lemon 			tp->snd_cwnd = tp->snd_cwnd_prev;
20299b8b58e0SJonathan Lemon 			tp->snd_ssthresh = tp->snd_ssthresh_prev;
20309d11646dSJeffrey Hsu 			tp->snd_recover = tp->snd_recover_prev;
20319d11646dSJeffrey Hsu 			if (tp->t_flags & TF_WASFRECOVERY)
20329d11646dSJeffrey Hsu 				ENTER_FASTRECOVERY(tp);
20339b8b58e0SJonathan Lemon 			tp->snd_nxt = tp->snd_max;
20349b8b58e0SJonathan Lemon 			tp->t_badrxtwin = 0;	/* XXX probably not required */
20359b8b58e0SJonathan Lemon 		}
20369b8b58e0SJonathan Lemon 
20379b8b58e0SJonathan Lemon 		/*
2038df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2039df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2040df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2041df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2042df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2043df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2044df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2045fa55172bSMatthew Dillon 		 *
2046fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2047fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2048fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2049fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2050df8bae1dSRodney W. Grimes 		 */
2051fa55172bSMatthew Dillon 		if ((to.to_flags & TOF_TS) != 0 &&
2052fa55172bSMatthew Dillon 		    to.to_tsecr) {
2053eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr)
2054eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - to.to_tsecr;
20559b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
2056fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2057eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2058eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
20599b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2060fa55172bSMatthew Dillon 		}
20611fcc99b5SMatthew Dillon 		tcp_xmit_bandwidth_limit(tp, th->th_ack);
2062df8bae1dSRodney W. Grimes 
2063df8bae1dSRodney W. Grimes 		/*
2064df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2065df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2066df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2067df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2068df8bae1dSRodney W. Grimes 		 */
2069fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2070b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2071df8bae1dSRodney W. Grimes 			needoutput = 1;
2072b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
2073b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2074a0292f23SGarrett Wollman 
2075a0292f23SGarrett Wollman 		/*
2076a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2077a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2078a0292f23SGarrett Wollman 		 */
2079a0292f23SGarrett Wollman 		if (acked == 0)
2080a0292f23SGarrett Wollman 			goto step6;
2081a0292f23SGarrett Wollman 
2082df8bae1dSRodney W. Grimes 		/*
2083df8bae1dSRodney W. Grimes 		 * When new data is acked, open the congestion window.
2084df8bae1dSRodney W. Grimes 		 * If the window gives us less than ssthresh packets
2085df8bae1dSRodney W. Grimes 		 * in flight, open exponentially (maxseg per packet).
2086df8bae1dSRodney W. Grimes 		 * Otherwise open linearly: maxseg per window
208710be5648SGarrett Wollman 		 * (maxseg^2 / cwnd per packet).
2088df8bae1dSRodney W. Grimes 		 */
20893529149eSAndre Oppermann 		if ((!tcp_do_newreno && !(tp->t_flags & TF_SACK_PERMIT)) ||
20906d90faf3SPaul Saab 		    !IN_FASTRECOVERY(tp)) {
2091ad3f9ab3SAndre Oppermann 			u_int cw = tp->snd_cwnd;
2092ad3f9ab3SAndre Oppermann 			u_int incr = tp->t_maxseg;
2093df8bae1dSRodney W. Grimes 			if (cw > tp->snd_ssthresh)
209410be5648SGarrett Wollman 				incr = incr * incr / cw;
2095df8bae1dSRodney W. Grimes 			tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale);
2096df8bae1dSRodney W. Grimes 		}
20975905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2098df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
2099df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
21005905999bSRobert Watson 			sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc);
2101df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2102df8bae1dSRodney W. Grimes 		} else {
21035905999bSRobert Watson 			sbdrop_locked(&so->so_snd, acked);
2104df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
2105df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2106df8bae1dSRodney W. Grimes 		}
2107564aab1fSAndre Oppermann 		/* NB: sowwakeup_locked() does an implicit unlock. */
21081e4d7da7SRobert Watson 		sowwakeup_locked(so);
2109e8949f74SAndre Oppermann 		/* Detect una wraparound. */
21103529149eSAndre Oppermann 		if ((tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) &&
21116d90faf3SPaul Saab 		    !IN_FASTRECOVERY(tp) &&
21129d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
21139d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
21149d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
21153529149eSAndre Oppermann 		if ((tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) &&
21166d90faf3SPaul Saab 		    IN_FASTRECOVERY(tp) &&
21179d11646dSJeffrey Hsu 		    SEQ_GEQ(th->th_ack, tp->snd_recover))
21189d11646dSJeffrey Hsu 			EXIT_FASTRECOVERY(tp);
2119fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
21203529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
21216d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
21226d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
21236d90faf3SPaul Saab 		}
2124df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2125df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2126df8bae1dSRodney W. Grimes 
2127df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2128df8bae1dSRodney W. Grimes 
2129df8bae1dSRodney W. Grimes 		/*
2130df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2131df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2132df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2133df8bae1dSRodney W. Grimes 		 */
2134df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2135df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2136df8bae1dSRodney W. Grimes 				/*
2137df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2138df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2139df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2140df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2141df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2142e8949f74SAndre Oppermann 				 *
2143e8949f74SAndre Oppermann 				 * XXXjl:
2144340c35deSJonathan Lemon 				 * we should release the tp also, and use a
2145340c35deSJonathan Lemon 				 * compressed state.
2146340c35deSJonathan Lemon 				 */
2147c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
21487c72af87SMohan Srinivasan 					int timeout;
21497c72af87SMohan Srinivasan 
215003e49181SSeigo Tanimura 					soisdisconnected(so);
21517c72af87SMohan Srinivasan 					timeout = (tcp_fast_finwait2_recycle) ?
21527c72af87SMohan Srinivasan 						tcp_finwait2_timeout : tcp_maxidle;
2153b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_2MSL, timeout);
21544cc20ab1SSeigo Tanimura 				}
2155df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
2156df8bae1dSRodney W. Grimes 			}
2157df8bae1dSRodney W. Grimes 			break;
2158df8bae1dSRodney W. Grimes 
2159df8bae1dSRodney W. Grimes 		/*
2160df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2161df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2162df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2163df8bae1dSRodney W. Grimes 		 * the segment.
2164df8bae1dSRodney W. Grimes 		 */
2165df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2166df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2167302ce8d6SAndre Oppermann 				KASSERT(headlocked, ("%s: process_ACK: "
2168302ce8d6SAndre Oppermann 				    "head not locked", __func__));
216911a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2170340c35deSJonathan Lemon 				INP_INFO_WUNLOCK(&tcbinfo);
2171302ce8d6SAndre Oppermann 				headlocked = 0;
2172340c35deSJonathan Lemon 				m_freem(m);
2173679d9708SAndre Oppermann 				return;
2174df8bae1dSRodney W. Grimes 			}
2175df8bae1dSRodney W. Grimes 			break;
2176df8bae1dSRodney W. Grimes 
2177df8bae1dSRodney W. Grimes 		/*
2178df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2179df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2180df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2181df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2182df8bae1dSRodney W. Grimes 		 */
2183df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2184df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2185302ce8d6SAndre Oppermann 				KASSERT(headlocked, ("%s: process_ACK: "
2186302ce8d6SAndre Oppermann 				    "tcp_close: head not locked", __func__));
2187df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2188df8bae1dSRodney W. Grimes 				goto drop;
2189df8bae1dSRodney W. Grimes 			}
2190df8bae1dSRodney W. Grimes 			break;
2191df8bae1dSRodney W. Grimes 		}
2192df8bae1dSRodney W. Grimes 	}
2193df8bae1dSRodney W. Grimes 
2194df8bae1dSRodney W. Grimes step6:
2195302ce8d6SAndre Oppermann 	KASSERT(headlocked, ("%s: step6: head not locked", __func__));
21968501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
21977cfc6904SRobert Watson 
2198df8bae1dSRodney W. Grimes 	/*
2199df8bae1dSRodney W. Grimes 	 * Update window information.
2200df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2201df8bae1dSRodney W. Grimes 	 */
2202fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) &&
2203fb59c426SYoshinobu Inoue 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2204fb59c426SYoshinobu Inoue 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2205fb59c426SYoshinobu Inoue 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2206df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
2207fb59c426SYoshinobu Inoue 		if (tlen == 0 &&
2208fb59c426SYoshinobu Inoue 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2209df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvwinupd++;
2210df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
2211fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq;
2212fb59c426SYoshinobu Inoue 		tp->snd_wl2 = th->th_ack;
2213df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2214df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
2215df8bae1dSRodney W. Grimes 		needoutput = 1;
2216df8bae1dSRodney W. Grimes 	}
2217df8bae1dSRodney W. Grimes 
2218df8bae1dSRodney W. Grimes 	/*
2219df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2220df8bae1dSRodney W. Grimes 	 */
2221fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2222df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2223df8bae1dSRodney W. Grimes 		/*
2224df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2225df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2226df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2227df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2228df8bae1dSRodney W. Grimes 		 */
222918ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2230fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2231fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2232fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
223318ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
2234df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2235df8bae1dSRodney W. Grimes 		}
2236df8bae1dSRodney W. Grimes 		/*
2237df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2238df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2239df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2240df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2241df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2242df8bae1dSRodney W. Grimes 		 *
2243df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2244df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2245df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2246df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2247df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2248df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2249df8bae1dSRodney W. Grimes 		 */
2250fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2251fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2252df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
2253df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2254927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2255c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
2256df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2257df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2258df8bae1dSRodney W. Grimes 		}
225918ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
2260df8bae1dSRodney W. Grimes 		/*
2261df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2262df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2263df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2264df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2265df8bae1dSRodney W. Grimes 		 */
226630613f56SJeffrey Hsu 		if (th->th_urp <= (u_long)tlen &&
226730613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
226830613f56SJeffrey Hsu 			/* hdr drop is delayed */
226930613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
227030613f56SJeffrey Hsu 		}
2271c068736aSJeffrey Hsu 	} else {
2272df8bae1dSRodney W. Grimes 		/*
2273df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2274df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2275df8bae1dSRodney W. Grimes 		 * with the receive window.
2276df8bae1dSRodney W. Grimes 		 */
2277df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2278df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2279c068736aSJeffrey Hsu 	}
2280df8bae1dSRodney W. Grimes dodata:							/* XXX */
2281302ce8d6SAndre Oppermann 	KASSERT(headlocked, ("%s: dodata: head not locked", __func__));
22828501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
22837cfc6904SRobert Watson 
2284df8bae1dSRodney W. Grimes 	/*
2285df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2286df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2287df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2288df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2289df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2290df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2291df8bae1dSRodney W. Grimes 	 */
2292fb59c426SYoshinobu Inoue 	if ((tlen || (thflags & TH_FIN)) &&
2293df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
229469e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
2295fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2296e4b64281SJesper Skriver 		/*
2297c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
2298c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
2299c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
2300c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
2301c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
2302c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
2303c068736aSJeffrey Hsu 		 * conversions.
2304c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
2305c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
2306c068736aSJeffrey Hsu 		 * fast retransmit can work).
2307e4b64281SJesper Skriver 		 */
2308e4b64281SJesper Skriver 		if (th->th_seq == tp->rcv_nxt &&
2309e4b64281SJesper Skriver 		    LIST_EMPTY(&tp->t_segq) &&
2310e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2311e4b64281SJesper Skriver 			if (DELAY_ACK(tp))
23123bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2313e4b64281SJesper Skriver 			else
2314e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2315e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2316e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
2317e4b64281SJesper Skriver 			tcpstat.tcps_rcvpack++;
2318e4b64281SJesper Skriver 			tcpstat.tcps_rcvbyte += tlen;
2319e4b64281SJesper Skriver 			ND6_HINT(tp);
23201e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
2321c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
2322c1c36a2cSMike Silbersack 				m_freem(m);
2323c1c36a2cSMike Silbersack 			else
23241e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
2325e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
23261e4d7da7SRobert Watson 			sorwakeup_locked(so);
2327e4b64281SJesper Skriver 		} else {
2328e8949f74SAndre Oppermann 			/*
2329e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
2330e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
2331e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
2332e8949f74SAndre Oppermann 			 * when trimming from the head.
2333e8949f74SAndre Oppermann 			 */
2334e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2335e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2336e4b64281SJesper Skriver 		}
23373529149eSAndre Oppermann 		if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT))
2338f194524fSAndre Oppermann 			tcp_update_sack_list(tp, save_start, save_start + tlen);
2339302ce8d6SAndre Oppermann #if 0
2340df8bae1dSRodney W. Grimes 		/*
2341df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2342df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2343df8bae1dSRodney W. Grimes 		 * buffer size.
2344302ce8d6SAndre Oppermann 		 * XXX: Unused.
2345df8bae1dSRodney W. Grimes 		 */
2346df8bae1dSRodney W. Grimes 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2347302ce8d6SAndre Oppermann #endif
2348df8bae1dSRodney W. Grimes 	} else {
2349df8bae1dSRodney W. Grimes 		m_freem(m);
2350fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2351df8bae1dSRodney W. Grimes 	}
2352df8bae1dSRodney W. Grimes 
2353df8bae1dSRodney W. Grimes 	/*
2354df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2355df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2356df8bae1dSRodney W. Grimes 	 */
2357fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2358df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2359df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2360a0292f23SGarrett Wollman 			/*
2361a0292f23SGarrett Wollman 			 * If connection is half-synchronized
2362d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
2363a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
2364a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
2365a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
2366a0292f23SGarrett Wollman 			 */
23673bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
23683bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2369a0292f23SGarrett Wollman 			else
2370df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2371df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2372df8bae1dSRodney W. Grimes 		}
2373df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2374df8bae1dSRodney W. Grimes 
2375df8bae1dSRodney W. Grimes 		/*
2376df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2377df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2378df8bae1dSRodney W. Grimes 		 */
2379df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
23809b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
23819b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
2382df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
2383df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
2384df8bae1dSRodney W. Grimes 			break;
2385df8bae1dSRodney W. Grimes 
2386df8bae1dSRodney W. Grimes 		/*
2387df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2388df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2389df8bae1dSRodney W. Grimes 		 */
2390df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2391df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
2392df8bae1dSRodney W. Grimes 			break;
2393df8bae1dSRodney W. Grimes 
2394df8bae1dSRodney W. Grimes 		/*
2395df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2396df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2397df8bae1dSRodney W. Grimes 		 * standard timers.
2398df8bae1dSRodney W. Grimes 		 */
2399df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
2400302ce8d6SAndre Oppermann 			KASSERT(headlocked == 1, ("%s: dodata: "
2401302ce8d6SAndre Oppermann 			    "TCP_FIN_WAIT_2: head not locked", __func__));
2402340c35deSJonathan Lemon 			tcp_twstart(tp);
240311a20fb8SJeffrey Hsu 			INP_INFO_WUNLOCK(&tcbinfo);
2404679d9708SAndre Oppermann 			return;
2405df8bae1dSRodney W. Grimes 		}
2406df8bae1dSRodney W. Grimes 	}
2407e0bef1cbSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
2408e0bef1cbSRobert Watson 	headlocked = 0;
2409610ee2f9SDavid Greenman #ifdef TCPDEBUG
24104cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2411fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2412fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2413610ee2f9SDavid Greenman #endif
2414df8bae1dSRodney W. Grimes 
2415df8bae1dSRodney W. Grimes 	/*
2416df8bae1dSRodney W. Grimes 	 * Return any desired output.
2417df8bae1dSRodney W. Grimes 	 */
2418df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2419df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
24207792ea27SJeffrey Hsu 
2421a14c749fSJonathan Lemon check_delack:
2422302ce8d6SAndre Oppermann 	KASSERT(headlocked == 0, ("%s: check_delack: head locked",
2423302ce8d6SAndre Oppermann 	    __func__));
24247824d002SAndre Oppermann 	INP_INFO_UNLOCK_ASSERT(&tcbinfo);
24258501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2426a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
24273bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
2428b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
24293bfd6421SJonathan Lemon 	}
24308501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
2431679d9708SAndre Oppermann 	return;
2432df8bae1dSRodney W. Grimes 
2433df8bae1dSRodney W. Grimes dropafterack:
2434302ce8d6SAndre Oppermann 	KASSERT(headlocked, ("%s: dropafterack: head not locked", __func__));
2435df8bae1dSRodney W. Grimes 	/*
2436df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
2437df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
243880ab7c0eSGarrett Wollman 	 *
243980ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
244080ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
244180ab7c0eSGarrett Wollman 	 * RST have been dropped.
244280ab7c0eSGarrett Wollman 	 *
244380ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
244480ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
244580ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
244680ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
244780ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
244880ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
2449df8bae1dSRodney W. Grimes 	 */
2450fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2451fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
2452a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
2453a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2454a57815efSBosko Milekic 		goto dropwithreset;
2455a57815efSBosko Milekic 	}
2456a0292f23SGarrett Wollman #ifdef TCPDEBUG
24574cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2458fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2459fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2460a0292f23SGarrett Wollman #endif
2461302ce8d6SAndre Oppermann 	KASSERT(headlocked, ("%s: headlocked should be 1", __func__));
246242cf3289SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
2463df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
2464df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
24658501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
2466d6915262SRobert Watson 	m_freem(m);
2467679d9708SAndre Oppermann 	return;
2468df8bae1dSRodney W. Grimes 
2469df8bae1dSRodney W. Grimes dropwithreset:
2470302ce8d6SAndre Oppermann 	KASSERT(headlocked, ("%s: dropwithreset: head not locked", __func__));
2471a57815efSBosko Milekic 
2472302ce8d6SAndre Oppermann 	tcp_dropwithreset(m, th, tp, tlen, rstreason);
2473c29afad6SSam Leffler 
24741c53f806SRobert Watson 	if (tp != NULL)
24758501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
2476f76fcf6dSJeffrey Hsu 	if (headlocked)
2477f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
2478679d9708SAndre Oppermann 	return;
2479df8bae1dSRodney W. Grimes 
2480df8bae1dSRodney W. Grimes drop:
2481df8bae1dSRodney W. Grimes 	/*
2482df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
2483df8bae1dSRodney W. Grimes 	 */
2484610ee2f9SDavid Greenman #ifdef TCPDEBUG
24851c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2486fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2487fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2488a0292f23SGarrett Wollman #endif
24891c53f806SRobert Watson 	if (tp != NULL)
24908501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
2491f76fcf6dSJeffrey Hsu 	if (headlocked)
2492f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
2493d6915262SRobert Watson 	m_freem(m);
2494679d9708SAndre Oppermann 	return;
2495302ce8d6SAndre Oppermann }
2496302ce8d6SAndre Oppermann 
2497302ce8d6SAndre Oppermann /*
24980ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
24990ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
25000ca3f933SAndre Oppermann  * tp may be NULL.
2501302ce8d6SAndre Oppermann  */
2502302ce8d6SAndre Oppermann static void
2503302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
2504302ce8d6SAndre Oppermann     int tlen, int rstreason)
2505302ce8d6SAndre Oppermann {
2506302ce8d6SAndre Oppermann 	struct ip *ip;
2507302ce8d6SAndre Oppermann #ifdef INET6
2508302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
2509302ce8d6SAndre Oppermann #endif
25107a3244ccSRobert Watson 
25117a3244ccSRobert Watson 	if (tp != NULL) {
25128501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
25137a3244ccSRobert Watson 	}
25147a3244ccSRobert Watson 
25150ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
2516302ce8d6SAndre Oppermann 	if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2517302ce8d6SAndre Oppermann 		goto drop;
2518302ce8d6SAndre Oppermann #ifdef INET6
2519302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
2520302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
2521302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2522302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2523302ce8d6SAndre Oppermann 			goto drop;
2524302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
2525302ce8d6SAndre Oppermann 	} else
2526302ce8d6SAndre Oppermann #endif
2527302ce8d6SAndre Oppermann 	{
2528302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
2529302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2530302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
2531302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
2532302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
2533302ce8d6SAndre Oppermann 			goto drop;
2534302ce8d6SAndre Oppermann 	}
2535302ce8d6SAndre Oppermann 
2536302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
2537302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
2538302ce8d6SAndre Oppermann 		goto drop;
2539302ce8d6SAndre Oppermann 
2540302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
2541302ce8d6SAndre Oppermann 	if (th->th_flags & TH_ACK) {
2542302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
2543302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
2544302ce8d6SAndre Oppermann 	} else {
2545302ce8d6SAndre Oppermann 		if (th->th_flags & TH_SYN)
2546302ce8d6SAndre Oppermann 			tlen++;
2547302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2548302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
2549302ce8d6SAndre Oppermann 	}
2550302ce8d6SAndre Oppermann 	return;
2551302ce8d6SAndre Oppermann drop:
2552302ce8d6SAndre Oppermann 	m_freem(m);
2553df8bae1dSRodney W. Grimes 	return;
2554df8bae1dSRodney W. Grimes }
2555df8bae1dSRodney W. Grimes 
2556be2ac88cSJonathan Lemon /*
2557be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
2558be2ac88cSJonathan Lemon  */
25590312fbe9SPoul-Henning Kamp static void
2560ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
2561df8bae1dSRodney W. Grimes {
2562df8bae1dSRodney W. Grimes 	int opt, optlen;
2563df8bae1dSRodney W. Grimes 
2564be2ac88cSJonathan Lemon 	to->to_flags = 0;
2565df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2566df8bae1dSRodney W. Grimes 		opt = cp[0];
2567df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
2568df8bae1dSRodney W. Grimes 			break;
2569df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
2570df8bae1dSRodney W. Grimes 			optlen = 1;
2571df8bae1dSRodney W. Grimes 		else {
2572b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
2573b474779fSJun-ichiro itojun Hagino 				break;
2574df8bae1dSRodney W. Grimes 			optlen = cp[1];
2575b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
2576df8bae1dSRodney W. Grimes 				break;
2577df8bae1dSRodney W. Grimes 		}
2578df8bae1dSRodney W. Grimes 		switch (opt) {
2579df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
2580df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
2581df8bae1dSRodney W. Grimes 				continue;
2582f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
2583df8bae1dSRodney W. Grimes 				continue;
2584be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
2585be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
2586be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
2587fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
2588df8bae1dSRodney W. Grimes 			break;
2589df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
2590df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
2591df8bae1dSRodney W. Grimes 				continue;
2592f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
2593df8bae1dSRodney W. Grimes 				continue;
2594be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
259502a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
2596df8bae1dSRodney W. Grimes 			break;
2597df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
2598df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
2599df8bae1dSRodney W. Grimes 				continue;
2600be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
2601a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2602a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
2603fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
2604a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
2605a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2606fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
2607df8bae1dSRodney W. Grimes 			break;
26081cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
26091cfd4b53SBruce M Simpson 		/*
26101cfd4b53SBruce M Simpson 		 * XXX In order to reply to a host which has set the
26111cfd4b53SBruce M Simpson 		 * TCP_SIGNATURE option in its initial SYN, we have to
26121cfd4b53SBruce M Simpson 		 * record the fact that the option was observed here
26131cfd4b53SBruce M Simpson 		 * for the syncache code to perform the correct response.
26141cfd4b53SBruce M Simpson 		 */
26151cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
26161cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
26171cfd4b53SBruce M Simpson 				continue;
2618df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
2619df47e437SAndre Oppermann 			to->to_signature = cp + 2;
26201cfd4b53SBruce M Simpson 			break;
2621265ed012SBruce M Simpson #endif
26226d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
2623f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
26246d90faf3SPaul Saab 				continue;
2625f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
2626f72167f4SAndre Oppermann 				continue;
2627f72167f4SAndre Oppermann 			if (!tcp_do_sack)
2628f72167f4SAndre Oppermann 				continue;
2629fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
26306d90faf3SPaul Saab 			break;
26316d90faf3SPaul Saab 		case TCPOPT_SACK:
26325a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
26336d90faf3SPaul Saab 				continue;
2634b728e902SAndre Oppermann 			if (flags & TO_SYN)
2635b728e902SAndre Oppermann 				continue;
2636fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
26375a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
26385a53ca16SPaul Saab 			to->to_sacks = cp + 2;
26395a53ca16SPaul Saab 			tcpstat.tcps_sack_rcv_blocks++;
26406d90faf3SPaul Saab 			break;
2641be2ac88cSJonathan Lemon 		default:
2642be2ac88cSJonathan Lemon 			continue;
2643df8bae1dSRodney W. Grimes 		}
2644df8bae1dSRodney W. Grimes 	}
2645df8bae1dSRodney W. Grimes }
2646df8bae1dSRodney W. Grimes 
2647df8bae1dSRodney W. Grimes /*
2648df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
2649df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
2650df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
2651df8bae1dSRodney W. Grimes  * sequencing purposes.
2652df8bae1dSRodney W. Grimes  */
26530312fbe9SPoul-Henning Kamp static void
2654ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
2655ad3f9ab3SAndre Oppermann     int off)
2656df8bae1dSRodney W. Grimes {
2657fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
2658df8bae1dSRodney W. Grimes 
2659df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
2660df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
2661df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
2662df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
2663df8bae1dSRodney W. Grimes 
26648501a69cSRobert Watson 			INP_WLOCK_ASSERT(tp->t_inpcb);
26657a3244ccSRobert Watson 
2666df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
2667df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
2668df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2669df8bae1dSRodney W. Grimes 			m->m_len--;
267069a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
267169a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
2672df8bae1dSRodney W. Grimes 			return;
2673df8bae1dSRodney W. Grimes 		}
2674df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
2675df8bae1dSRodney W. Grimes 		m = m->m_next;
26764dfdffe9SAndre Oppermann 		if (m == NULL)
2677df8bae1dSRodney W. Grimes 			break;
2678df8bae1dSRodney W. Grimes 	}
2679df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
2680df8bae1dSRodney W. Grimes }
2681df8bae1dSRodney W. Grimes 
2682df8bae1dSRodney W. Grimes /*
2683df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
2684df8bae1dSRodney W. Grimes  * and update averages and current timeout.
2685df8bae1dSRodney W. Grimes  */
26860312fbe9SPoul-Henning Kamp static void
2687ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
2688df8bae1dSRodney W. Grimes {
2689ad3f9ab3SAndre Oppermann 	int delta;
2690233e8c18SGarrett Wollman 
26918501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
26922be3bf22SRobert Watson 
2693233e8c18SGarrett Wollman 	tcpstat.tcps_rttupdated++;
2694233e8c18SGarrett Wollman 	tp->t_rttupdated++;
2695233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
2696233e8c18SGarrett Wollman 		/*
2697233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
2698233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
2699233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
2700233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2701233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
2702233e8c18SGarrett Wollman 		 */
2703233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2704233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2705233e8c18SGarrett Wollman 
2706233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
2707233e8c18SGarrett Wollman 			tp->t_srtt = 1;
2708233e8c18SGarrett Wollman 
2709233e8c18SGarrett Wollman 		/*
2710233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
2711233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
2712233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
2713233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
2714233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
2715233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
2716233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
2717233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
2718233e8c18SGarrett Wollman 		 */
2719233e8c18SGarrett Wollman 		if (delta < 0)
2720233e8c18SGarrett Wollman 			delta = -delta;
2721233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2722233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
2723233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
27241fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
27251fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2726233e8c18SGarrett Wollman 	} else {
2727233e8c18SGarrett Wollman 		/*
2728233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
2729233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
2730233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
2731233e8c18SGarrett Wollman 		 */
2732233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
2733233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
27341fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2735233e8c18SGarrett Wollman 	}
27369b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
2737df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
2738df8bae1dSRodney W. Grimes 
2739df8bae1dSRodney W. Grimes 	/*
2740df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
2741df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
2742df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
2743df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
2744df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
2745df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
2746df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
2747df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
2748df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
2749df8bae1dSRodney W. Grimes 	 */
2750233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
27519e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2752df8bae1dSRodney W. Grimes 
2753df8bae1dSRodney W. Grimes 	/*
2754df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
2755df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
2756df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
2757df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
2758df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
2759df8bae1dSRodney W. Grimes 	 */
2760df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
2761df8bae1dSRodney W. Grimes }
2762df8bae1dSRodney W. Grimes 
2763df8bae1dSRodney W. Grimes /*
2764df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
2765df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
2766df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
2767df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
2768df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2769df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
2770df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
2771df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
2772df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
2773df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
2774df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
2775df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
2776df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
2777a0292f23SGarrett Wollman  *
2778a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
2779a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
2780a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
2781a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
2782a0292f23SGarrett Wollman  * data in maxopd.
2783a0292f23SGarrett Wollman  *
2784a0292f23SGarrett Wollman  * In case of T/TCP, we call this routine during implicit connection
2785a0292f23SGarrett Wollman  * setup as well (offer = -1), to initialize maxseg from the cached
2786a0292f23SGarrett Wollman  * MSS of our peer.
278797d8d152SAndre Oppermann  *
278897d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
278997d8d152SAndre Oppermann  * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt().
2790df8bae1dSRodney W. Grimes  */
2791a0292f23SGarrett Wollman void
2792ad3f9ab3SAndre Oppermann tcp_mss(struct tcpcb *tp, int offer)
2793df8bae1dSRodney W. Grimes {
279497d8d152SAndre Oppermann 	int rtt, mss;
2795df8bae1dSRodney W. Grimes 	u_long bufsize;
279697d8d152SAndre Oppermann 	u_long maxmtu;
2797c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
2798df8bae1dSRodney W. Grimes 	struct socket *so;
279997d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
2800a0292f23SGarrett Wollman 	int origoffer = offer;
2801233dcce1SAndre Oppermann 	int mtuflags = 0;
2802fb59c426SYoshinobu Inoue #ifdef INET6
2803c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2804c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
2805c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
2806c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
2807c068736aSJeffrey Hsu #else
2808c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
2809fb59c426SYoshinobu Inoue #endif
2810df8bae1dSRodney W. Grimes 
28118501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
28127a3244ccSRobert Watson 
2813e8949f74SAndre Oppermann 	/* Initialize. */
281497d8d152SAndre Oppermann #ifdef INET6
281597d8d152SAndre Oppermann 	if (isipv6) {
2816233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, &mtuflags);
281797d8d152SAndre Oppermann 		tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt;
281897d8d152SAndre Oppermann 	} else
281997d8d152SAndre Oppermann #endif
282097d8d152SAndre Oppermann 	{
2821233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, &mtuflags);
282297d8d152SAndre Oppermann 		tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
2823df8bae1dSRodney W. Grimes 	}
2824df8bae1dSRodney W. Grimes 
282597d8d152SAndre Oppermann 	/*
2826e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
282797d8d152SAndre Oppermann 	 */
282897d8d152SAndre Oppermann 	if (maxmtu == 0)
282997d8d152SAndre Oppermann 		return;
283097d8d152SAndre Oppermann 
2831e8949f74SAndre Oppermann 	/* What have we got? */
283297d8d152SAndre Oppermann 	switch (offer) {
283397d8d152SAndre Oppermann 		case 0:
283497d8d152SAndre Oppermann 			/*
283597d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
2836c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
2837c3b02504SBjoern A. Zeeb 			 * already assigned to t_maxopd above.
283897d8d152SAndre Oppermann 			 */
2839c3b02504SBjoern A. Zeeb 			offer = tp->t_maxopd;
284097d8d152SAndre Oppermann 			break;
284197d8d152SAndre Oppermann 
284297d8d152SAndre Oppermann 		case -1:
2843a0292f23SGarrett Wollman 			/*
2844c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
2845a0292f23SGarrett Wollman 			 */
284697d8d152SAndre Oppermann 			/* FALLTHROUGH */
284797d8d152SAndre Oppermann 
284897d8d152SAndre Oppermann 		default:
2849a0292f23SGarrett Wollman 			/*
285053369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
285153369ac9SAndre Oppermann 			 * to at least minmss.
285253369ac9SAndre Oppermann 			 */
285353369ac9SAndre Oppermann 			offer = max(offer, tcp_minmss);
285453369ac9SAndre Oppermann 			/*
2855a0292f23SGarrett Wollman 			 * Sanity check: make sure that maxopd will be large
285697d8d152SAndre Oppermann 			 * enough to allow some data on segments even if the
2857a0292f23SGarrett Wollman 			 * all the option space is used (40bytes).  Otherwise
2858a0292f23SGarrett Wollman 			 * funny things may happen in tcp_output.
2859a0292f23SGarrett Wollman 			 */
2860a0292f23SGarrett Wollman 			offer = max(offer, 64);
286197d8d152SAndre Oppermann 	}
2862a0292f23SGarrett Wollman 
2863df8bae1dSRodney W. Grimes 	/*
2864e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
2865df8bae1dSRodney W. Grimes 	 */
286697d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
286797d8d152SAndre Oppermann 
2868df8bae1dSRodney W. Grimes 	/*
2869e8949f74SAndre Oppermann 	 * If there's a discovered mtu int tcp hostcache, use it
2870fb59c426SYoshinobu Inoue 	 * else, use the link mtu.
2871df8bae1dSRodney W. Grimes 	 */
287297d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
28732d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
2874c068736aSJeffrey Hsu 	else {
287531b3783cSHajimu UMEMOTO #ifdef INET6
2876fb59c426SYoshinobu Inoue 		if (isipv6) {
287797d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
287897d8d152SAndre Oppermann 			if (!path_mtu_discovery &&
287997d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
2880fb59c426SYoshinobu Inoue 				mss = min(mss, tcp_v6mssdflt);
2881b3399803SHajimu UMEMOTO 		} else
2882b3399803SHajimu UMEMOTO #endif
288397d8d152SAndre Oppermann 		{
288497d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
288597d8d152SAndre Oppermann 			if (!path_mtu_discovery &&
288697d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
2887a0292f23SGarrett Wollman 				mss = min(mss, tcp_mssdflt);
2888a0292f23SGarrett Wollman 		}
288997d8d152SAndre Oppermann 	}
2890a0292f23SGarrett Wollman 	mss = min(mss, offer);
289197d8d152SAndre Oppermann 
2892a0292f23SGarrett Wollman 	/*
2893a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
2894a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
2895a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
2896a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
2897a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
2898a0292f23SGarrett Wollman 	 */
2899a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
2900a0292f23SGarrett Wollman 
2901a0292f23SGarrett Wollman 	/*
2902e8949f74SAndre Oppermann 	 * origoffer==-1 indicates that no segments were received yet.
2903c94c54e4SAndre Oppermann 	 * In this case we just guess.
2904a0292f23SGarrett Wollman 	 */
2905a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2906a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2907a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2908a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
2909a0292f23SGarrett Wollman 
2910df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
2911df8bae1dSRodney W. Grimes 	if (mss > MCLBYTES)
2912df8bae1dSRodney W. Grimes 		mss &= ~(MCLBYTES-1);
2913df8bae1dSRodney W. Grimes #else
2914df8bae1dSRodney W. Grimes 	if (mss > MCLBYTES)
2915df8bae1dSRodney W. Grimes 		mss = mss / MCLBYTES * MCLBYTES;
2916df8bae1dSRodney W. Grimes #endif
291797d8d152SAndre Oppermann 	tp->t_maxseg = mss;
291897d8d152SAndre Oppermann 
2919df8bae1dSRodney W. Grimes 	/*
292097d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
292197d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
292297d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
292397d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
292497d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
2925df8bae1dSRodney W. Grimes 	 */
2926c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
29273f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
292897d8d152SAndre Oppermann 	if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe)
292997d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
293097d8d152SAndre Oppermann 	else
2931df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
2932df8bae1dSRodney W. Grimes 	if (bufsize < mss)
2933df8bae1dSRodney W. Grimes 		mss = bufsize;
2934df8bae1dSRodney W. Grimes 	else {
2935df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2936df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2937df8bae1dSRodney W. Grimes 			bufsize = sb_max;
293888c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
29393f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
2940df8bae1dSRodney W. Grimes 	}
29413f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
2942df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
2943df8bae1dSRodney W. Grimes 
29443f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
294597d8d152SAndre Oppermann 	if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe)
294697d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
294797d8d152SAndre Oppermann 	else
2948df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
2949df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
2950df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2951df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2952df8bae1dSRodney W. Grimes 			bufsize = sb_max;
295388c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
29543f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
2955df8bae1dSRodney W. Grimes 	}
29563f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
2957a0292f23SGarrett Wollman 	/*
2958e8949f74SAndre Oppermann 	 * While we're here, check the others too.
2959a0292f23SGarrett Wollman 	 */
296097d8d152SAndre Oppermann 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
296197d8d152SAndre Oppermann 		tp->t_srtt = rtt;
296297d8d152SAndre Oppermann 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
296397d8d152SAndre Oppermann 		tcpstat.tcps_usedrtt++;
296497d8d152SAndre Oppermann 		if (metrics.rmx_rttvar) {
296597d8d152SAndre Oppermann 			tp->t_rttvar = metrics.rmx_rttvar;
296697d8d152SAndre Oppermann 			tcpstat.tcps_usedrttvar++;
296797d8d152SAndre Oppermann 		} else {
296897d8d152SAndre Oppermann 			/* default variation is +- 1 rtt */
296997d8d152SAndre Oppermann 			tp->t_rttvar =
297097d8d152SAndre Oppermann 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
297197d8d152SAndre Oppermann 		}
297297d8d152SAndre Oppermann 		TCPT_RANGESET(tp->t_rxtcur,
297397d8d152SAndre Oppermann 			      ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
297497d8d152SAndre Oppermann 			      tp->t_rttmin, TCPTV_REXMTMAX);
297597d8d152SAndre Oppermann 	}
297697d8d152SAndre Oppermann 	if (metrics.rmx_ssthresh) {
2977df8bae1dSRodney W. Grimes 		/*
2978df8bae1dSRodney W. Grimes 		 * There's some sort of gateway or interface
2979df8bae1dSRodney W. Grimes 		 * buffer limit on the path.  Use this to set
2980df8bae1dSRodney W. Grimes 		 * the slow start threshhold, but set the
2981df8bae1dSRodney W. Grimes 		 * threshold to no less than 2*mss.
2982df8bae1dSRodney W. Grimes 		 */
298397d8d152SAndre Oppermann 		tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh);
2984dd224982SGarrett Wollman 		tcpstat.tcps_usedssthresh++;
2985df8bae1dSRodney W. Grimes 	}
298697d8d152SAndre Oppermann 	if (metrics.rmx_bandwidth)
298797d8d152SAndre Oppermann 		tp->snd_bandwidth = metrics.rmx_bandwidth;
298897d8d152SAndre Oppermann 
298997d8d152SAndre Oppermann 	/*
299097d8d152SAndre Oppermann 	 * Set the slow-start flight size depending on whether this
299197d8d152SAndre Oppermann 	 * is a local network or not.
299297d8d152SAndre Oppermann 	 *
299397d8d152SAndre Oppermann 	 * Extend this so we cache the cwnd too and retrieve it here.
299497d8d152SAndre Oppermann 	 * Make cwnd even bigger than RFC3390 suggests but only if we
299597d8d152SAndre Oppermann 	 * have previous experience with the remote host. Be careful
299697d8d152SAndre Oppermann 	 * not make cwnd bigger than remote receive window or our own
299797d8d152SAndre Oppermann 	 * send socket buffer. Maybe put some additional upper bound
299897d8d152SAndre Oppermann 	 * on the retrieved cwnd. Should do incremental updates to
299997d8d152SAndre Oppermann 	 * hostcache when cwnd collapses so next connection doesn't
300097d8d152SAndre Oppermann 	 * overloads the path again.
300197d8d152SAndre Oppermann 	 *
300297d8d152SAndre Oppermann 	 * RFC3390 says only do this if SYN or SYN/ACK didn't got lost.
300397d8d152SAndre Oppermann 	 * We currently check only in syncache_socket for that.
300497d8d152SAndre Oppermann 	 */
300597d8d152SAndre Oppermann #define TCP_METRICS_CWND
300697d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND
300797d8d152SAndre Oppermann 	if (metrics.rmx_cwnd)
300897d8d152SAndre Oppermann 		tp->snd_cwnd = max(mss,
300997d8d152SAndre Oppermann 				min(metrics.rmx_cwnd / 2,
301097d8d152SAndre Oppermann 				 min(tp->snd_wnd, so->so_snd.sb_hiwat)));
301197d8d152SAndre Oppermann 	else
301297d8d152SAndre Oppermann #endif
301397d8d152SAndre Oppermann 	if (tcp_do_rfc3390)
301497d8d152SAndre Oppermann 		tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380));
301597d8d152SAndre Oppermann #ifdef INET6
301697d8d152SAndre Oppermann 	else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
301797d8d152SAndre Oppermann 		 (!isipv6 && in_localaddr(inp->inp_faddr)))
3018943ae302SAndre Oppermann #else
3019943ae302SAndre Oppermann 	else if (in_localaddr(inp->inp_faddr))
302097d8d152SAndre Oppermann #endif
3021943ae302SAndre Oppermann 		tp->snd_cwnd = mss * ss_fltsz_local;
302297d8d152SAndre Oppermann 	else
302397d8d152SAndre Oppermann 		tp->snd_cwnd = mss * ss_fltsz;
3024233dcce1SAndre Oppermann 
3025233dcce1SAndre Oppermann 	/* Check the interface for TSO capabilities. */
3026233dcce1SAndre Oppermann 	if (mtuflags & CSUM_TSO)
3027233dcce1SAndre Oppermann 		tp->t_flags |= TF_TSO;
3028a0292f23SGarrett Wollman }
3029a0292f23SGarrett Wollman 
3030a0292f23SGarrett Wollman /*
3031a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3032a0292f23SGarrett Wollman  */
3033a0292f23SGarrett Wollman int
3034ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3035a0292f23SGarrett Wollman {
303697d8d152SAndre Oppermann 	int mss = 0;
303797d8d152SAndre Oppermann 	u_long maxmtu = 0;
303897d8d152SAndre Oppermann 	u_long thcmtu = 0;
303997d8d152SAndre Oppermann 	size_t min_protoh;
3040fb59c426SYoshinobu Inoue #ifdef INET6
304197d8d152SAndre Oppermann 	int isipv6 = inc->inc_isipv6 ? 1 : 0;
3042fb59c426SYoshinobu Inoue #endif
3043a0292f23SGarrett Wollman 
304497d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3045a0292f23SGarrett Wollman 
304631b3783cSHajimu UMEMOTO #ifdef INET6
304797d8d152SAndre Oppermann 	if (isipv6) {
304897d8d152SAndre Oppermann 		mss = tcp_v6mssdflt;
3049233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
305097d8d152SAndre Oppermann 		thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
305197d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
305297d8d152SAndre Oppermann 	} else
305331b3783cSHajimu UMEMOTO #endif
305497d8d152SAndre Oppermann 	{
305597d8d152SAndre Oppermann 		mss = tcp_mssdflt;
3056233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
305797d8d152SAndre Oppermann 		thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
305897d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
305997d8d152SAndre Oppermann 	}
306097d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
306197d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
306297d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
306397d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
306497d8d152SAndre Oppermann 
306597d8d152SAndre Oppermann 	return (mss);
3066df8bae1dSRodney W. Grimes }
306746f58482SJonathan Lemon 
306846f58482SJonathan Lemon 
306946f58482SJonathan Lemon /*
3070c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3071c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3072c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3073c068736aSJeffrey Hsu  * be started again.
307446f58482SJonathan Lemon  */
3075c068736aSJeffrey Hsu static void
3076ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
307746f58482SJonathan Lemon {
307846f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
307946f58482SJonathan Lemon 	u_long  ocwnd = tp->snd_cwnd;
308046f58482SJonathan Lemon 
30818501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
30827a3244ccSRobert Watson 
3083b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
308446f58482SJonathan Lemon 	tp->t_rtttime = 0;
308546f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
30866b2a5f92SJayanth Vijayaraghavan 	/*
3087c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3088c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
30896b2a5f92SJayanth Vijayaraghavan 	 */
30906b2a5f92SJayanth Vijayaraghavan 	tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
3091a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
30926b2a5f92SJayanth Vijayaraghavan 	(void) tcp_output(tp);
309346f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
309446f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
309546f58482SJonathan Lemon 		tp->snd_nxt = onxt;
309646f58482SJonathan Lemon 	/*
309746f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
309846f58482SJonathan Lemon 	 * not updated yet.
309946f58482SJonathan Lemon 	 */
3100d7587117SPaul Saab 	if (tp->snd_cwnd > th->th_ack - tp->snd_una)
3101d7587117SPaul Saab 		tp->snd_cwnd -= th->th_ack - tp->snd_una;
3102d7587117SPaul Saab 	else
3103d7587117SPaul Saab 		tp->snd_cwnd = 0;
3104d7587117SPaul Saab 	tp->snd_cwnd += tp->t_maxseg;
310546f58482SJonathan Lemon }
3106