Wed Sep 2 15:08:46 2020 UTC ()
Fix fast path for uni directional transfers
pure ACK case:

drag snd_wl2 along so only newer
ACKs can update the window size.
also avoids the state where snd_wl2
is eventually larger than th_ack and thus
blocking the window update mechanism and
the connection gets stuck for a loooong
time in the zero sized send window state.

see PR/kern 55567

ok thorpej@, also found in FreeBSD


(kardel)
diff -r1.418 -r1.419 src/sys/netinet/tcp_input.c

cvs diff -r1.418 -r1.419 src/sys/netinet/tcp_input.c (switch to unified diff)

--- src/sys/netinet/tcp_input.c 2020/07/06 18:49:12 1.418
+++ src/sys/netinet/tcp_input.c 2020/09/02 15:08:46 1.419
@@ -1,2898 +1,2911 @@ @@ -1,2898 +1,2911 @@
1/* $NetBSD: tcp_input.c,v 1.418 2020/07/06 18:49:12 christos Exp $ */ 1/* $NetBSD: tcp_input.c,v 1.419 2020/09/02 15:08:46 kardel Exp $ */
2 2
3/* 3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors 15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software 16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission. 17 * without specific prior written permission.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE. 29 * SUCH DAMAGE.
30 */ 30 */
31 31
32/* 32/*
33 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 33 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995
34 * 34 *
35 * NRL grants permission for redistribution and use in source and binary 35 * NRL grants permission for redistribution and use in source and binary
36 * forms, with or without modification, of the software and documentation 36 * forms, with or without modification, of the software and documentation
37 * created at NRL provided that the following conditions are met: 37 * created at NRL provided that the following conditions are met:
38 * 38 *
39 * 1. Redistributions of source code must retain the above copyright 39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer. 40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright 41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the 42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution. 43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software 44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgements: 45 * must display the following acknowledgements:
46 * This product includes software developed by the University of 46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors. 47 * California, Berkeley and its contributors.
48 * This product includes software developed at the Information 48 * This product includes software developed at the Information
49 * Technology Division, US Naval Research Laboratory. 49 * Technology Division, US Naval Research Laboratory.
50 * 4. Neither the name of the NRL nor the names of its contributors 50 * 4. Neither the name of the NRL nor the names of its contributors
51 * may be used to endorse or promote products derived from this software 51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission. 52 * without specific prior written permission.
53 * 53 *
54 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 54 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
55 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 55 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
57 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 57 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
58 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 58 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
59 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 59 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
60 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 60 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 61 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 62 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 63 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 * 65 *
66 * The views and conclusions contained in the software and documentation 66 * The views and conclusions contained in the software and documentation
67 * are those of the authors and should not be interpreted as representing 67 * are those of the authors and should not be interpreted as representing
68 * official policies, either expressed or implied, of the US Naval 68 * official policies, either expressed or implied, of the US Naval
69 * Research Laboratory (NRL). 69 * Research Laboratory (NRL).
70 */ 70 */
71 71
72/*- 72/*-
73 * Copyright (c) 1997, 1998, 1999, 2001, 2005, 2006, 73 * Copyright (c) 1997, 1998, 1999, 2001, 2005, 2006,
74 * 2011 The NetBSD Foundation, Inc. 74 * 2011 The NetBSD Foundation, Inc.
75 * All rights reserved. 75 * All rights reserved.
76 * 76 *
77 * This code is derived from software contributed to The NetBSD Foundation 77 * This code is derived from software contributed to The NetBSD Foundation
78 * by Coyote Point Systems, Inc. 78 * by Coyote Point Systems, Inc.
79 * This code is derived from software contributed to The NetBSD Foundation 79 * This code is derived from software contributed to The NetBSD Foundation
80 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation 80 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
81 * Facility, NASA Ames Research Center. 81 * Facility, NASA Ames Research Center.
82 * This code is derived from software contributed to The NetBSD Foundation 82 * This code is derived from software contributed to The NetBSD Foundation
83 * by Charles M. Hannum. 83 * by Charles M. Hannum.
84 * This code is derived from software contributed to The NetBSD Foundation 84 * This code is derived from software contributed to The NetBSD Foundation
85 * by Rui Paulo. 85 * by Rui Paulo.
86 * 86 *
87 * Redistribution and use in source and binary forms, with or without 87 * Redistribution and use in source and binary forms, with or without
88 * modification, are permitted provided that the following conditions 88 * modification, are permitted provided that the following conditions
89 * are met: 89 * are met:
90 * 1. Redistributions of source code must retain the above copyright 90 * 1. Redistributions of source code must retain the above copyright
91 * notice, this list of conditions and the following disclaimer. 91 * notice, this list of conditions and the following disclaimer.
92 * 2. Redistributions in binary form must reproduce the above copyright 92 * 2. Redistributions in binary form must reproduce the above copyright
93 * notice, this list of conditions and the following disclaimer in the 93 * notice, this list of conditions and the following disclaimer in the
94 * documentation and/or other materials provided with the distribution. 94 * documentation and/or other materials provided with the distribution.
95 * 95 *
96 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 96 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
97 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 97 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
98 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 98 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
99 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 99 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
100 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 100 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
101 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 101 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
102 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 102 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
103 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 103 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
104 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 104 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
105 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 105 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
106 * POSSIBILITY OF SUCH DAMAGE. 106 * POSSIBILITY OF SUCH DAMAGE.
107 */ 107 */
108 108
109/* 109/*
110 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 110 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
111 * The Regents of the University of California. All rights reserved. 111 * The Regents of the University of California. All rights reserved.
112 * 112 *
113 * Redistribution and use in source and binary forms, with or without 113 * Redistribution and use in source and binary forms, with or without
114 * modification, are permitted provided that the following conditions 114 * modification, are permitted provided that the following conditions
115 * are met: 115 * are met:
116 * 1. Redistributions of source code must retain the above copyright 116 * 1. Redistributions of source code must retain the above copyright
117 * notice, this list of conditions and the following disclaimer. 117 * notice, this list of conditions and the following disclaimer.
118 * 2. Redistributions in binary form must reproduce the above copyright 118 * 2. Redistributions in binary form must reproduce the above copyright
119 * notice, this list of conditions and the following disclaimer in the 119 * notice, this list of conditions and the following disclaimer in the
120 * documentation and/or other materials provided with the distribution. 120 * documentation and/or other materials provided with the distribution.
121 * 3. Neither the name of the University nor the names of its contributors 121 * 3. Neither the name of the University nor the names of its contributors
122 * may be used to endorse or promote products derived from this software 122 * may be used to endorse or promote products derived from this software
123 * without specific prior written permission. 123 * without specific prior written permission.
124 * 124 *
125 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 125 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
126 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 126 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
127 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 127 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
128 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 128 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
129 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 129 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
130 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 130 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
131 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 131 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
132 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 132 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
133 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 133 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
134 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 134 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
135 * SUCH DAMAGE. 135 * SUCH DAMAGE.
136 * 136 *
137 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 137 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
138 */ 138 */
139 139
140/* 140/*
141 * TODO list for SYN cache stuff: 141 * TODO list for SYN cache stuff:
142 * 142 *
143 * Find room for a "state" field, which is needed to keep a 143 * Find room for a "state" field, which is needed to keep a
144 * compressed state for TIME_WAIT TCBs. It's been noted already 144 * compressed state for TIME_WAIT TCBs. It's been noted already
145 * that this is fairly important for very high-volume web and 145 * that this is fairly important for very high-volume web and
146 * mail servers, which use a large number of short-lived 146 * mail servers, which use a large number of short-lived
147 * connections. 147 * connections.
148 */ 148 */
149 149
150#include <sys/cdefs.h> 150#include <sys/cdefs.h>
151__KERNEL_RCSID(0, "$NetBSD: tcp_input.c,v 1.418 2020/07/06 18:49:12 christos Exp $"); 151__KERNEL_RCSID(0, "$NetBSD: tcp_input.c,v 1.419 2020/09/02 15:08:46 kardel Exp $");
152 152
153#ifdef _KERNEL_OPT 153#ifdef _KERNEL_OPT
154#include "opt_inet.h" 154#include "opt_inet.h"
155#include "opt_ipsec.h" 155#include "opt_ipsec.h"
156#include "opt_inet_csum.h" 156#include "opt_inet_csum.h"
157#include "opt_tcp_debug.h" 157#include "opt_tcp_debug.h"
158#endif 158#endif
159 159
160#include <sys/param.h> 160#include <sys/param.h>
161#include <sys/systm.h> 161#include <sys/systm.h>
162#include <sys/malloc.h> 162#include <sys/malloc.h>
163#include <sys/mbuf.h> 163#include <sys/mbuf.h>
164#include <sys/protosw.h> 164#include <sys/protosw.h>
165#include <sys/socket.h> 165#include <sys/socket.h>
166#include <sys/socketvar.h> 166#include <sys/socketvar.h>
167#include <sys/errno.h> 167#include <sys/errno.h>
168#include <sys/syslog.h> 168#include <sys/syslog.h>
169#include <sys/pool.h> 169#include <sys/pool.h>
170#include <sys/domain.h> 170#include <sys/domain.h>
171#include <sys/kernel.h> 171#include <sys/kernel.h>
172#ifdef TCP_SIGNATURE 172#ifdef TCP_SIGNATURE
173#include <sys/md5.h> 173#include <sys/md5.h>
174#endif 174#endif
175#include <sys/lwp.h> /* for lwp0 */ 175#include <sys/lwp.h> /* for lwp0 */
176#include <sys/cprng.h> 176#include <sys/cprng.h>
177 177
178#include <net/if.h> 178#include <net/if.h>
179#include <net/if_types.h> 179#include <net/if_types.h>
180 180
181#include <netinet/in.h> 181#include <netinet/in.h>
182#include <netinet/in_systm.h> 182#include <netinet/in_systm.h>
183#include <netinet/ip.h> 183#include <netinet/ip.h>
184#include <netinet/in_pcb.h> 184#include <netinet/in_pcb.h>
185#include <netinet/in_var.h> 185#include <netinet/in_var.h>
186#include <netinet/ip_var.h> 186#include <netinet/ip_var.h>
187#include <netinet/in_offload.h> 187#include <netinet/in_offload.h>
188 188
189#ifdef INET6 189#ifdef INET6
190#include <netinet/ip6.h> 190#include <netinet/ip6.h>
191#include <netinet6/ip6_var.h> 191#include <netinet6/ip6_var.h>
192#include <netinet6/in6_pcb.h> 192#include <netinet6/in6_pcb.h>
193#include <netinet6/ip6_var.h> 193#include <netinet6/ip6_var.h>
194#include <netinet6/in6_var.h> 194#include <netinet6/in6_var.h>
195#include <netinet/icmp6.h> 195#include <netinet/icmp6.h>
196#include <netinet6/nd6.h> 196#include <netinet6/nd6.h>
197#ifdef TCP_SIGNATURE 197#ifdef TCP_SIGNATURE
198#include <netinet6/scope6_var.h> 198#include <netinet6/scope6_var.h>
199#endif 199#endif
200#endif 200#endif
201 201
202#ifndef INET6 202#ifndef INET6
203#include <netinet/ip6.h> 203#include <netinet/ip6.h>
204#endif 204#endif
205 205
206#include <netinet/tcp.h> 206#include <netinet/tcp.h>
207#include <netinet/tcp_fsm.h> 207#include <netinet/tcp_fsm.h>
208#include <netinet/tcp_seq.h> 208#include <netinet/tcp_seq.h>
209#include <netinet/tcp_timer.h> 209#include <netinet/tcp_timer.h>
210#include <netinet/tcp_var.h> 210#include <netinet/tcp_var.h>
211#include <netinet/tcp_private.h> 211#include <netinet/tcp_private.h>
212#include <netinet/tcp_congctl.h> 212#include <netinet/tcp_congctl.h>
213#include <netinet/tcp_debug.h> 213#include <netinet/tcp_debug.h>
214 214
215#ifdef INET6 215#ifdef INET6
216#include "faith.h" 216#include "faith.h"
217#if defined(NFAITH) && NFAITH > 0 217#if defined(NFAITH) && NFAITH > 0
218#include <net/if_faith.h> 218#include <net/if_faith.h>
219#endif 219#endif
220#endif 220#endif
221 221
222#ifdef IPSEC 222#ifdef IPSEC
223#include <netipsec/ipsec.h> 223#include <netipsec/ipsec.h>
224#include <netipsec/key.h> 224#include <netipsec/key.h>
225#ifdef INET6 225#ifdef INET6
226#include <netipsec/ipsec6.h> 226#include <netipsec/ipsec6.h>
227#endif 227#endif
228#endif /* IPSEC*/ 228#endif /* IPSEC*/
229 229
230#include <netinet/tcp_vtw.h> 230#include <netinet/tcp_vtw.h>
231 231
232int tcprexmtthresh = 3; 232int tcprexmtthresh = 3;
233int tcp_log_refused; 233int tcp_log_refused;
234 234
235int tcp_do_autorcvbuf = 1; 235int tcp_do_autorcvbuf = 1;
236int tcp_autorcvbuf_inc = 16 * 1024; 236int tcp_autorcvbuf_inc = 16 * 1024;
237int tcp_autorcvbuf_max = 256 * 1024; 237int tcp_autorcvbuf_max = 256 * 1024;
238int tcp_msl = (TCPTV_MSL / PR_SLOWHZ); 238int tcp_msl = (TCPTV_MSL / PR_SLOWHZ);
239 239
240static int tcp_rst_ppslim_count = 0; 240static int tcp_rst_ppslim_count = 0;
241static struct timeval tcp_rst_ppslim_last; 241static struct timeval tcp_rst_ppslim_last;
242static int tcp_ackdrop_ppslim_count = 0; 242static int tcp_ackdrop_ppslim_count = 0;
243static struct timeval tcp_ackdrop_ppslim_last; 243static struct timeval tcp_ackdrop_ppslim_last;
244 244
245static void syn_cache_timer(void *); 245static void syn_cache_timer(void *);
246 246
247#define TCP_PAWS_IDLE (24U * 24 * 60 * 60 * PR_SLOWHZ) 247#define TCP_PAWS_IDLE (24U * 24 * 60 * 60 * PR_SLOWHZ)
248 248
249/* for modulo comparisons of timestamps */ 249/* for modulo comparisons of timestamps */
250#define TSTMP_LT(a,b) ((int)((a)-(b)) < 0) 250#define TSTMP_LT(a,b) ((int)((a)-(b)) < 0)
251#define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0) 251#define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0)
252 252
253/* 253/*
254 * Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. 254 * Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint.
255 */ 255 */
256#ifdef INET6 256#ifdef INET6
257static inline void 257static inline void
258nd6_hint(struct tcpcb *tp) 258nd6_hint(struct tcpcb *tp)
259{ 259{
260 struct rtentry *rt = NULL; 260 struct rtentry *rt = NULL;
261 261
262 if (tp != NULL && tp->t_in6pcb != NULL && tp->t_family == AF_INET6 && 262 if (tp != NULL && tp->t_in6pcb != NULL && tp->t_family == AF_INET6 &&
263 (rt = rtcache_validate(&tp->t_in6pcb->in6p_route)) != NULL) { 263 (rt = rtcache_validate(&tp->t_in6pcb->in6p_route)) != NULL) {
264 nd6_nud_hint(rt); 264 nd6_nud_hint(rt);
265 rtcache_unref(rt, &tp->t_in6pcb->in6p_route); 265 rtcache_unref(rt, &tp->t_in6pcb->in6p_route);
266 } 266 }
267} 267}
268#else 268#else
269static inline void 269static inline void
270nd6_hint(struct tcpcb *tp) 270nd6_hint(struct tcpcb *tp)
271{ 271{
272} 272}
273#endif 273#endif
274 274
275/* 275/*
276 * Compute ACK transmission behavior. Delay the ACK unless 276 * Compute ACK transmission behavior. Delay the ACK unless
277 * we have already delayed an ACK (must send an ACK every two segments). 277 * we have already delayed an ACK (must send an ACK every two segments).
278 * We also ACK immediately if we received a PUSH and the ACK-on-PUSH 278 * We also ACK immediately if we received a PUSH and the ACK-on-PUSH
279 * option is enabled. 279 * option is enabled.
280 */ 280 */
281static void 281static void
282tcp_setup_ack(struct tcpcb *tp, const struct tcphdr *th) 282tcp_setup_ack(struct tcpcb *tp, const struct tcphdr *th)
283{ 283{
284 284
285 if (tp->t_flags & TF_DELACK || 285 if (tp->t_flags & TF_DELACK ||
286 (tcp_ack_on_push && th->th_flags & TH_PUSH)) 286 (tcp_ack_on_push && th->th_flags & TH_PUSH))
287 tp->t_flags |= TF_ACKNOW; 287 tp->t_flags |= TF_ACKNOW;
288 else 288 else
289 TCP_SET_DELACK(tp); 289 TCP_SET_DELACK(tp);
290} 290}
291 291
292static void 292static void
293icmp_check(struct tcpcb *tp, const struct tcphdr *th, int acked) 293icmp_check(struct tcpcb *tp, const struct tcphdr *th, int acked)
294{ 294{
295 295
296 /* 296 /*
297 * If we had a pending ICMP message that refers to data that have 297 * If we had a pending ICMP message that refers to data that have
298 * just been acknowledged, disregard the recorded ICMP message. 298 * just been acknowledged, disregard the recorded ICMP message.
299 */ 299 */
300 if ((tp->t_flags & TF_PMTUD_PEND) && 300 if ((tp->t_flags & TF_PMTUD_PEND) &&
301 SEQ_GT(th->th_ack, tp->t_pmtud_th_seq)) 301 SEQ_GT(th->th_ack, tp->t_pmtud_th_seq))
302 tp->t_flags &= ~TF_PMTUD_PEND; 302 tp->t_flags &= ~TF_PMTUD_PEND;
303 303
304 /* 304 /*
305 * Keep track of the largest chunk of data 305 * Keep track of the largest chunk of data
306 * acknowledged since last PMTU update 306 * acknowledged since last PMTU update
307 */ 307 */
308 if (tp->t_pmtud_mss_acked < acked) 308 if (tp->t_pmtud_mss_acked < acked)
309 tp->t_pmtud_mss_acked = acked; 309 tp->t_pmtud_mss_acked = acked;
310} 310}
311 311
312/* 312/*
313 * Convert TCP protocol fields to host order for easier processing. 313 * Convert TCP protocol fields to host order for easier processing.
314 */ 314 */
315static void 315static void
316tcp_fields_to_host(struct tcphdr *th) 316tcp_fields_to_host(struct tcphdr *th)
317{ 317{
318 318
319 NTOHL(th->th_seq); 319 NTOHL(th->th_seq);
320 NTOHL(th->th_ack); 320 NTOHL(th->th_ack);
321 NTOHS(th->th_win); 321 NTOHS(th->th_win);
322 NTOHS(th->th_urp); 322 NTOHS(th->th_urp);
323} 323}
324 324
325/* 325/*
326 * ... and reverse the above. 326 * ... and reverse the above.
327 */ 327 */
328static void 328static void
329tcp_fields_to_net(struct tcphdr *th) 329tcp_fields_to_net(struct tcphdr *th)
330{ 330{
331 331
332 HTONL(th->th_seq); 332 HTONL(th->th_seq);
333 HTONL(th->th_ack); 333 HTONL(th->th_ack);
334 HTONS(th->th_win); 334 HTONS(th->th_win);
335 HTONS(th->th_urp); 335 HTONS(th->th_urp);
336} 336}
337 337
338static void 338static void
339tcp_urp_drop(struct tcphdr *th, int todrop, int *tiflags) 339tcp_urp_drop(struct tcphdr *th, int todrop, int *tiflags)
340{ 340{
341 if (th->th_urp > todrop) { 341 if (th->th_urp > todrop) {
342 th->th_urp -= todrop; 342 th->th_urp -= todrop;
343 } else { 343 } else {
344 *tiflags &= ~TH_URG; 344 *tiflags &= ~TH_URG;
345 th->th_urp = 0; 345 th->th_urp = 0;
346 } 346 }
347} 347}
348 348
349#ifdef TCP_CSUM_COUNTERS 349#ifdef TCP_CSUM_COUNTERS
350#include <sys/device.h> 350#include <sys/device.h>
351 351
352extern struct evcnt tcp_hwcsum_ok; 352extern struct evcnt tcp_hwcsum_ok;
353extern struct evcnt tcp_hwcsum_bad; 353extern struct evcnt tcp_hwcsum_bad;
354extern struct evcnt tcp_hwcsum_data; 354extern struct evcnt tcp_hwcsum_data;
355extern struct evcnt tcp_swcsum; 355extern struct evcnt tcp_swcsum;
356#if defined(INET6) 356#if defined(INET6)
357extern struct evcnt tcp6_hwcsum_ok; 357extern struct evcnt tcp6_hwcsum_ok;
358extern struct evcnt tcp6_hwcsum_bad; 358extern struct evcnt tcp6_hwcsum_bad;
359extern struct evcnt tcp6_hwcsum_data; 359extern struct evcnt tcp6_hwcsum_data;
360extern struct evcnt tcp6_swcsum; 360extern struct evcnt tcp6_swcsum;
361#endif /* defined(INET6) */ 361#endif /* defined(INET6) */
362 362
363#define TCP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++ 363#define TCP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
364 364
365#else 365#else
366 366
367#define TCP_CSUM_COUNTER_INCR(ev) /* nothing */ 367#define TCP_CSUM_COUNTER_INCR(ev) /* nothing */
368 368
369#endif /* TCP_CSUM_COUNTERS */ 369#endif /* TCP_CSUM_COUNTERS */
370 370
371#ifdef TCP_REASS_COUNTERS 371#ifdef TCP_REASS_COUNTERS
372#include <sys/device.h> 372#include <sys/device.h>
373 373
374extern struct evcnt tcp_reass_; 374extern struct evcnt tcp_reass_;
375extern struct evcnt tcp_reass_empty; 375extern struct evcnt tcp_reass_empty;
376extern struct evcnt tcp_reass_iteration[8]; 376extern struct evcnt tcp_reass_iteration[8];
377extern struct evcnt tcp_reass_prependfirst; 377extern struct evcnt tcp_reass_prependfirst;
378extern struct evcnt tcp_reass_prepend; 378extern struct evcnt tcp_reass_prepend;
379extern struct evcnt tcp_reass_insert; 379extern struct evcnt tcp_reass_insert;
380extern struct evcnt tcp_reass_inserttail; 380extern struct evcnt tcp_reass_inserttail;
381extern struct evcnt tcp_reass_append; 381extern struct evcnt tcp_reass_append;
382extern struct evcnt tcp_reass_appendtail; 382extern struct evcnt tcp_reass_appendtail;
383extern struct evcnt tcp_reass_overlaptail; 383extern struct evcnt tcp_reass_overlaptail;
384extern struct evcnt tcp_reass_overlapfront; 384extern struct evcnt tcp_reass_overlapfront;
385extern struct evcnt tcp_reass_segdup; 385extern struct evcnt tcp_reass_segdup;
386extern struct evcnt tcp_reass_fragdup; 386extern struct evcnt tcp_reass_fragdup;
387 387
388#define TCP_REASS_COUNTER_INCR(ev) (ev)->ev_count++ 388#define TCP_REASS_COUNTER_INCR(ev) (ev)->ev_count++
389 389
390#else 390#else
391 391
392#define TCP_REASS_COUNTER_INCR(ev) /* nothing */ 392#define TCP_REASS_COUNTER_INCR(ev) /* nothing */
393 393
394#endif /* TCP_REASS_COUNTERS */ 394#endif /* TCP_REASS_COUNTERS */
395 395
396static int tcp_reass(struct tcpcb *, const struct tcphdr *, struct mbuf *, 396static int tcp_reass(struct tcpcb *, const struct tcphdr *, struct mbuf *,
397 int); 397 int);
398static int tcp_dooptions(struct tcpcb *, const u_char *, int, 398static int tcp_dooptions(struct tcpcb *, const u_char *, int,
399 struct tcphdr *, struct mbuf *, int, struct tcp_opt_info *); 399 struct tcphdr *, struct mbuf *, int, struct tcp_opt_info *);
400 400
401static void tcp4_log_refused(const struct ip *, const struct tcphdr *); 401static void tcp4_log_refused(const struct ip *, const struct tcphdr *);
402#ifdef INET6 402#ifdef INET6
403static void tcp6_log_refused(const struct ip6_hdr *, const struct tcphdr *); 403static void tcp6_log_refused(const struct ip6_hdr *, const struct tcphdr *);
404#endif 404#endif
405 405
406#if defined(MBUFTRACE) 406#if defined(MBUFTRACE)
407struct mowner tcp_reass_mowner = MOWNER_INIT("tcp", "reass"); 407struct mowner tcp_reass_mowner = MOWNER_INIT("tcp", "reass");
408#endif /* defined(MBUFTRACE) */ 408#endif /* defined(MBUFTRACE) */
409 409
410static struct pool tcpipqent_pool; 410static struct pool tcpipqent_pool;
411 411
412void 412void
413tcpipqent_init(void) 413tcpipqent_init(void)
414{ 414{
415 415
416 pool_init(&tcpipqent_pool, sizeof(struct ipqent), 0, 0, 0, "tcpipqepl", 416 pool_init(&tcpipqent_pool, sizeof(struct ipqent), 0, 0, 0, "tcpipqepl",
417 NULL, IPL_VM); 417 NULL, IPL_VM);
418} 418}
419 419
420struct ipqent * 420struct ipqent *
421tcpipqent_alloc(void) 421tcpipqent_alloc(void)
422{ 422{
423 struct ipqent *ipqe; 423 struct ipqent *ipqe;
424 int s; 424 int s;
425 425
426 s = splvm(); 426 s = splvm();
427 ipqe = pool_get(&tcpipqent_pool, PR_NOWAIT); 427 ipqe = pool_get(&tcpipqent_pool, PR_NOWAIT);
428 splx(s); 428 splx(s);
429 429
430 return ipqe; 430 return ipqe;
431} 431}
432 432
433void 433void
434tcpipqent_free(struct ipqent *ipqe) 434tcpipqent_free(struct ipqent *ipqe)
435{ 435{
436 int s; 436 int s;
437 437
438 s = splvm(); 438 s = splvm();
439 pool_put(&tcpipqent_pool, ipqe); 439 pool_put(&tcpipqent_pool, ipqe);
440 splx(s); 440 splx(s);
441} 441}
442 442
443/* 443/*
444 * Insert segment ti into reassembly queue of tcp with 444 * Insert segment ti into reassembly queue of tcp with
445 * control block tp. Return TH_FIN if reassembly now includes 445 * control block tp. Return TH_FIN if reassembly now includes
446 * a segment with FIN. 446 * a segment with FIN.
447 */ 447 */
448static int 448static int
449tcp_reass(struct tcpcb *tp, const struct tcphdr *th, struct mbuf *m, int tlen) 449tcp_reass(struct tcpcb *tp, const struct tcphdr *th, struct mbuf *m, int tlen)
450{ 450{
451 struct ipqent *p, *q, *nq, *tiqe = NULL; 451 struct ipqent *p, *q, *nq, *tiqe = NULL;
452 struct socket *so = NULL; 452 struct socket *so = NULL;
453 int pkt_flags; 453 int pkt_flags;
454 tcp_seq pkt_seq; 454 tcp_seq pkt_seq;
455 unsigned pkt_len; 455 unsigned pkt_len;
456 u_long rcvpartdupbyte = 0; 456 u_long rcvpartdupbyte = 0;
457 u_long rcvoobyte; 457 u_long rcvoobyte;
458#ifdef TCP_REASS_COUNTERS 458#ifdef TCP_REASS_COUNTERS
459 u_int count = 0; 459 u_int count = 0;
460#endif 460#endif
461 uint64_t *tcps; 461 uint64_t *tcps;
462 462
463 if (tp->t_inpcb) 463 if (tp->t_inpcb)
464 so = tp->t_inpcb->inp_socket; 464 so = tp->t_inpcb->inp_socket;
465#ifdef INET6 465#ifdef INET6
466 else if (tp->t_in6pcb) 466 else if (tp->t_in6pcb)
467 so = tp->t_in6pcb->in6p_socket; 467 so = tp->t_in6pcb->in6p_socket;
468#endif 468#endif
469 469
470 TCP_REASS_LOCK_CHECK(tp); 470 TCP_REASS_LOCK_CHECK(tp);
471 471
472 /* 472 /*
473 * Call with th==NULL after become established to 473 * Call with th==NULL after become established to
474 * force pre-ESTABLISHED data up to user socket. 474 * force pre-ESTABLISHED data up to user socket.
475 */ 475 */
476 if (th == NULL) 476 if (th == NULL)
477 goto present; 477 goto present;
478 478
479 m_claimm(m, &tcp_reass_mowner); 479 m_claimm(m, &tcp_reass_mowner);
480 480
481 rcvoobyte = tlen; 481 rcvoobyte = tlen;
482 /* 482 /*
483 * Copy these to local variables because the TCP header gets munged 483 * Copy these to local variables because the TCP header gets munged
484 * while we are collapsing mbufs. 484 * while we are collapsing mbufs.
485 */ 485 */
486 pkt_seq = th->th_seq; 486 pkt_seq = th->th_seq;
487 pkt_len = tlen; 487 pkt_len = tlen;
488 pkt_flags = th->th_flags; 488 pkt_flags = th->th_flags;
489 489
490 TCP_REASS_COUNTER_INCR(&tcp_reass_); 490 TCP_REASS_COUNTER_INCR(&tcp_reass_);
491 491
492 if ((p = TAILQ_LAST(&tp->segq, ipqehead)) != NULL) { 492 if ((p = TAILQ_LAST(&tp->segq, ipqehead)) != NULL) {
493 /* 493 /*
494 * When we miss a packet, the vast majority of time we get 494 * When we miss a packet, the vast majority of time we get
495 * packets that follow it in order. So optimize for that. 495 * packets that follow it in order. So optimize for that.
496 */ 496 */
497 if (pkt_seq == p->ipqe_seq + p->ipqe_len) { 497 if (pkt_seq == p->ipqe_seq + p->ipqe_len) {
498 p->ipqe_len += pkt_len; 498 p->ipqe_len += pkt_len;
499 p->ipqe_flags |= pkt_flags; 499 p->ipqe_flags |= pkt_flags;
500 m_cat(p->ipqe_m, m); 500 m_cat(p->ipqe_m, m);
501 m = NULL; 501 m = NULL;
502 tiqe = p; 502 tiqe = p;
503 TAILQ_REMOVE(&tp->timeq, p, ipqe_timeq); 503 TAILQ_REMOVE(&tp->timeq, p, ipqe_timeq);
504 TCP_REASS_COUNTER_INCR(&tcp_reass_appendtail); 504 TCP_REASS_COUNTER_INCR(&tcp_reass_appendtail);
505 goto skip_replacement; 505 goto skip_replacement;
506 } 506 }
507 /* 507 /*
508 * While we're here, if the pkt is completely beyond 508 * While we're here, if the pkt is completely beyond
509 * anything we have, just insert it at the tail. 509 * anything we have, just insert it at the tail.
510 */ 510 */
511 if (SEQ_GT(pkt_seq, p->ipqe_seq + p->ipqe_len)) { 511 if (SEQ_GT(pkt_seq, p->ipqe_seq + p->ipqe_len)) {
512 TCP_REASS_COUNTER_INCR(&tcp_reass_inserttail); 512 TCP_REASS_COUNTER_INCR(&tcp_reass_inserttail);
513 goto insert_it; 513 goto insert_it;
514 } 514 }
515 } 515 }
516 516
517 q = TAILQ_FIRST(&tp->segq); 517 q = TAILQ_FIRST(&tp->segq);
518 518
519 if (q != NULL) { 519 if (q != NULL) {
520 /* 520 /*
521 * If this segment immediately precedes the first out-of-order 521 * If this segment immediately precedes the first out-of-order
522 * block, simply slap the segment in front of it and (mostly) 522 * block, simply slap the segment in front of it and (mostly)
523 * skip the complicated logic. 523 * skip the complicated logic.
524 */ 524 */
525 if (pkt_seq + pkt_len == q->ipqe_seq) { 525 if (pkt_seq + pkt_len == q->ipqe_seq) {
526 q->ipqe_seq = pkt_seq; 526 q->ipqe_seq = pkt_seq;
527 q->ipqe_len += pkt_len; 527 q->ipqe_len += pkt_len;
528 q->ipqe_flags |= pkt_flags; 528 q->ipqe_flags |= pkt_flags;
529 m_cat(m, q->ipqe_m); 529 m_cat(m, q->ipqe_m);
530 q->ipqe_m = m; 530 q->ipqe_m = m;
531 tiqe = q; 531 tiqe = q;
532 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq); 532 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
533 TCP_REASS_COUNTER_INCR(&tcp_reass_prependfirst); 533 TCP_REASS_COUNTER_INCR(&tcp_reass_prependfirst);
534 goto skip_replacement; 534 goto skip_replacement;
535 } 535 }
536 } else { 536 } else {
537 TCP_REASS_COUNTER_INCR(&tcp_reass_empty); 537 TCP_REASS_COUNTER_INCR(&tcp_reass_empty);
538 } 538 }
539 539
540 /* 540 /*
541 * Find a segment which begins after this one does. 541 * Find a segment which begins after this one does.
542 */ 542 */
543 for (p = NULL; q != NULL; q = nq) { 543 for (p = NULL; q != NULL; q = nq) {
544 nq = TAILQ_NEXT(q, ipqe_q); 544 nq = TAILQ_NEXT(q, ipqe_q);
545#ifdef TCP_REASS_COUNTERS 545#ifdef TCP_REASS_COUNTERS
546 count++; 546 count++;
547#endif 547#endif
548 548
549 /* 549 /*
550 * If the received segment is just right after this 550 * If the received segment is just right after this
551 * fragment, merge the two together and then check 551 * fragment, merge the two together and then check
552 * for further overlaps. 552 * for further overlaps.
553 */ 553 */
554 if (q->ipqe_seq + q->ipqe_len == pkt_seq) { 554 if (q->ipqe_seq + q->ipqe_len == pkt_seq) {
555 pkt_len += q->ipqe_len; 555 pkt_len += q->ipqe_len;
556 pkt_flags |= q->ipqe_flags; 556 pkt_flags |= q->ipqe_flags;
557 pkt_seq = q->ipqe_seq; 557 pkt_seq = q->ipqe_seq;
558 m_cat(q->ipqe_m, m); 558 m_cat(q->ipqe_m, m);
559 m = q->ipqe_m; 559 m = q->ipqe_m;
560 TCP_REASS_COUNTER_INCR(&tcp_reass_append); 560 TCP_REASS_COUNTER_INCR(&tcp_reass_append);
561 goto free_ipqe; 561 goto free_ipqe;
562 } 562 }
563 563
564 /* 564 /*
565 * If the received segment is completely past this 565 * If the received segment is completely past this
566 * fragment, we need to go to the next fragment. 566 * fragment, we need to go to the next fragment.
567 */ 567 */
568 if (SEQ_LT(q->ipqe_seq + q->ipqe_len, pkt_seq)) { 568 if (SEQ_LT(q->ipqe_seq + q->ipqe_len, pkt_seq)) {
569 p = q; 569 p = q;
570 continue; 570 continue;
571 } 571 }
572 572
573 /* 573 /*
574 * If the fragment is past the received segment, 574 * If the fragment is past the received segment,
575 * it (or any following) can't be concatenated. 575 * it (or any following) can't be concatenated.
576 */ 576 */
577 if (SEQ_GT(q->ipqe_seq, pkt_seq + pkt_len)) { 577 if (SEQ_GT(q->ipqe_seq, pkt_seq + pkt_len)) {
578 TCP_REASS_COUNTER_INCR(&tcp_reass_insert); 578 TCP_REASS_COUNTER_INCR(&tcp_reass_insert);
579 break; 579 break;
580 } 580 }
581 581
582 /* 582 /*
583 * We've received all the data in this segment before. 583 * We've received all the data in this segment before.
584 * Mark it as a duplicate and return. 584 * Mark it as a duplicate and return.
585 */ 585 */
586 if (SEQ_LEQ(q->ipqe_seq, pkt_seq) && 586 if (SEQ_LEQ(q->ipqe_seq, pkt_seq) &&
587 SEQ_GEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) { 587 SEQ_GEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
588 tcps = TCP_STAT_GETREF(); 588 tcps = TCP_STAT_GETREF();
589 tcps[TCP_STAT_RCVDUPPACK]++; 589 tcps[TCP_STAT_RCVDUPPACK]++;
590 tcps[TCP_STAT_RCVDUPBYTE] += pkt_len; 590 tcps[TCP_STAT_RCVDUPBYTE] += pkt_len;
591 TCP_STAT_PUTREF(); 591 TCP_STAT_PUTREF();
592 tcp_new_dsack(tp, pkt_seq, pkt_len); 592 tcp_new_dsack(tp, pkt_seq, pkt_len);
593 m_freem(m); 593 m_freem(m);
594 if (tiqe != NULL) { 594 if (tiqe != NULL) {
595 tcpipqent_free(tiqe); 595 tcpipqent_free(tiqe);
596 } 596 }
597 TCP_REASS_COUNTER_INCR(&tcp_reass_segdup); 597 TCP_REASS_COUNTER_INCR(&tcp_reass_segdup);
598 goto out; 598 goto out;
599 } 599 }
600 600
601 /* 601 /*
602 * Received segment completely overlaps this fragment 602 * Received segment completely overlaps this fragment
603 * so we drop the fragment (this keeps the temporal 603 * so we drop the fragment (this keeps the temporal
604 * ordering of segments correct). 604 * ordering of segments correct).
605 */ 605 */
606 if (SEQ_GEQ(q->ipqe_seq, pkt_seq) && 606 if (SEQ_GEQ(q->ipqe_seq, pkt_seq) &&
607 SEQ_LEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) { 607 SEQ_LEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
608 rcvpartdupbyte += q->ipqe_len; 608 rcvpartdupbyte += q->ipqe_len;
609 m_freem(q->ipqe_m); 609 m_freem(q->ipqe_m);
610 TCP_REASS_COUNTER_INCR(&tcp_reass_fragdup); 610 TCP_REASS_COUNTER_INCR(&tcp_reass_fragdup);
611 goto free_ipqe; 611 goto free_ipqe;
612 } 612 }
613 613
614 /* 614 /*
615 * Received segment extends past the end of the fragment. 615 * Received segment extends past the end of the fragment.
616 * Drop the overlapping bytes, merge the fragment and 616 * Drop the overlapping bytes, merge the fragment and
617 * segment, and treat as a longer received packet. 617 * segment, and treat as a longer received packet.
618 */ 618 */
619 if (SEQ_LT(q->ipqe_seq, pkt_seq) && 619 if (SEQ_LT(q->ipqe_seq, pkt_seq) &&
620 SEQ_GT(q->ipqe_seq + q->ipqe_len, pkt_seq)) { 620 SEQ_GT(q->ipqe_seq + q->ipqe_len, pkt_seq)) {
621 int overlap = q->ipqe_seq + q->ipqe_len - pkt_seq; 621 int overlap = q->ipqe_seq + q->ipqe_len - pkt_seq;
622 m_adj(m, overlap); 622 m_adj(m, overlap);
623 rcvpartdupbyte += overlap; 623 rcvpartdupbyte += overlap;
624 m_cat(q->ipqe_m, m); 624 m_cat(q->ipqe_m, m);
625 m = q->ipqe_m; 625 m = q->ipqe_m;
626 pkt_seq = q->ipqe_seq; 626 pkt_seq = q->ipqe_seq;
627 pkt_len += q->ipqe_len - overlap; 627 pkt_len += q->ipqe_len - overlap;
628 rcvoobyte -= overlap; 628 rcvoobyte -= overlap;
629 TCP_REASS_COUNTER_INCR(&tcp_reass_overlaptail); 629 TCP_REASS_COUNTER_INCR(&tcp_reass_overlaptail);
630 goto free_ipqe; 630 goto free_ipqe;
631 } 631 }
632 632
633 /* 633 /*
634 * Received segment extends past the front of the fragment. 634 * Received segment extends past the front of the fragment.
635 * Drop the overlapping bytes on the received packet. The 635 * Drop the overlapping bytes on the received packet. The
636 * packet will then be concatenated with this fragment a 636 * packet will then be concatenated with this fragment a
637 * bit later. 637 * bit later.
638 */ 638 */
639 if (SEQ_GT(q->ipqe_seq, pkt_seq) && 639 if (SEQ_GT(q->ipqe_seq, pkt_seq) &&
640 SEQ_LT(q->ipqe_seq, pkt_seq + pkt_len)) { 640 SEQ_LT(q->ipqe_seq, pkt_seq + pkt_len)) {
641 int overlap = pkt_seq + pkt_len - q->ipqe_seq; 641 int overlap = pkt_seq + pkt_len - q->ipqe_seq;
642 m_adj(m, -overlap); 642 m_adj(m, -overlap);
643 pkt_len -= overlap; 643 pkt_len -= overlap;
644 rcvpartdupbyte += overlap; 644 rcvpartdupbyte += overlap;
645 TCP_REASS_COUNTER_INCR(&tcp_reass_overlapfront); 645 TCP_REASS_COUNTER_INCR(&tcp_reass_overlapfront);
646 rcvoobyte -= overlap; 646 rcvoobyte -= overlap;
647 } 647 }
648 648
649 /* 649 /*
650 * If the received segment immediately precedes this 650 * If the received segment immediately precedes this
651 * fragment then tack the fragment onto this segment 651 * fragment then tack the fragment onto this segment
652 * and reinsert the data. 652 * and reinsert the data.
653 */ 653 */
654 if (q->ipqe_seq == pkt_seq + pkt_len) { 654 if (q->ipqe_seq == pkt_seq + pkt_len) {
655 pkt_len += q->ipqe_len; 655 pkt_len += q->ipqe_len;
656 pkt_flags |= q->ipqe_flags; 656 pkt_flags |= q->ipqe_flags;
657 m_cat(m, q->ipqe_m); 657 m_cat(m, q->ipqe_m);
658 TAILQ_REMOVE(&tp->segq, q, ipqe_q); 658 TAILQ_REMOVE(&tp->segq, q, ipqe_q);
659 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq); 659 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
660 tp->t_segqlen--; 660 tp->t_segqlen--;
661 KASSERT(tp->t_segqlen >= 0); 661 KASSERT(tp->t_segqlen >= 0);
662 KASSERT(tp->t_segqlen != 0 || 662 KASSERT(tp->t_segqlen != 0 ||
663 (TAILQ_EMPTY(&tp->segq) && 663 (TAILQ_EMPTY(&tp->segq) &&
664 TAILQ_EMPTY(&tp->timeq))); 664 TAILQ_EMPTY(&tp->timeq)));
665 if (tiqe == NULL) { 665 if (tiqe == NULL) {
666 tiqe = q; 666 tiqe = q;
667 } else { 667 } else {
668 tcpipqent_free(q); 668 tcpipqent_free(q);
669 } 669 }
670 TCP_REASS_COUNTER_INCR(&tcp_reass_prepend); 670 TCP_REASS_COUNTER_INCR(&tcp_reass_prepend);
671 break; 671 break;
672 } 672 }
673 673
674 /* 674 /*
675 * If the fragment is before the segment, remember it. 675 * If the fragment is before the segment, remember it.
676 * When this loop is terminated, p will contain the 676 * When this loop is terminated, p will contain the
677 * pointer to the fragment that is right before the 677 * pointer to the fragment that is right before the
678 * received segment. 678 * received segment.
679 */ 679 */
680 if (SEQ_LEQ(q->ipqe_seq, pkt_seq)) 680 if (SEQ_LEQ(q->ipqe_seq, pkt_seq))
681 p = q; 681 p = q;
682 682
683 continue; 683 continue;
684 684
685 /* 685 /*
686 * This is a common operation. It also will allow 686 * This is a common operation. It also will allow
687 * to save doing a malloc/free in most instances. 687 * to save doing a malloc/free in most instances.
688 */ 688 */
689 free_ipqe: 689 free_ipqe:
690 TAILQ_REMOVE(&tp->segq, q, ipqe_q); 690 TAILQ_REMOVE(&tp->segq, q, ipqe_q);
691 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq); 691 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
692 tp->t_segqlen--; 692 tp->t_segqlen--;
693 KASSERT(tp->t_segqlen >= 0); 693 KASSERT(tp->t_segqlen >= 0);
694 KASSERT(tp->t_segqlen != 0 || 694 KASSERT(tp->t_segqlen != 0 ||
695 (TAILQ_EMPTY(&tp->segq) && TAILQ_EMPTY(&tp->timeq))); 695 (TAILQ_EMPTY(&tp->segq) && TAILQ_EMPTY(&tp->timeq)));
696 if (tiqe == NULL) { 696 if (tiqe == NULL) {
697 tiqe = q; 697 tiqe = q;
698 } else { 698 } else {
699 tcpipqent_free(q); 699 tcpipqent_free(q);
700 } 700 }
701 } 701 }
702 702
703#ifdef TCP_REASS_COUNTERS 703#ifdef TCP_REASS_COUNTERS
704 if (count > 7) 704 if (count > 7)
705 TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[0]); 705 TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[0]);
706 else if (count > 0) 706 else if (count > 0)
707 TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[count]); 707 TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[count]);
708#endif 708#endif
709 709
710insert_it: 710insert_it:
711 /* 711 /*
712 * Allocate a new queue entry (block) since the received segment 712 * Allocate a new queue entry (block) since the received segment
713 * did not collapse onto any other out-of-order block. If it had 713 * did not collapse onto any other out-of-order block. If it had
714 * collapsed, tiqe would not be NULL and we would be reusing it. 714 * collapsed, tiqe would not be NULL and we would be reusing it.
715 * 715 *
716 * If the allocation fails, drop the packet. 716 * If the allocation fails, drop the packet.
717 */ 717 */
718 if (tiqe == NULL) { 718 if (tiqe == NULL) {
719 tiqe = tcpipqent_alloc(); 719 tiqe = tcpipqent_alloc();
720 if (tiqe == NULL) { 720 if (tiqe == NULL) {
721 TCP_STATINC(TCP_STAT_RCVMEMDROP); 721 TCP_STATINC(TCP_STAT_RCVMEMDROP);
722 m_freem(m); 722 m_freem(m);
723 goto out; 723 goto out;
724 } 724 }
725 } 725 }
726 726
727 /* 727 /*
728 * Update the counters. 728 * Update the counters.
729 */ 729 */
730 tp->t_rcvoopack++; 730 tp->t_rcvoopack++;
731 tcps = TCP_STAT_GETREF(); 731 tcps = TCP_STAT_GETREF();
732 tcps[TCP_STAT_RCVOOPACK]++; 732 tcps[TCP_STAT_RCVOOPACK]++;
733 tcps[TCP_STAT_RCVOOBYTE] += rcvoobyte; 733 tcps[TCP_STAT_RCVOOBYTE] += rcvoobyte;
734 if (rcvpartdupbyte) { 734 if (rcvpartdupbyte) {
735 tcps[TCP_STAT_RCVPARTDUPPACK]++; 735 tcps[TCP_STAT_RCVPARTDUPPACK]++;
736 tcps[TCP_STAT_RCVPARTDUPBYTE] += rcvpartdupbyte; 736 tcps[TCP_STAT_RCVPARTDUPBYTE] += rcvpartdupbyte;
737 } 737 }
738 TCP_STAT_PUTREF(); 738 TCP_STAT_PUTREF();
739 739
740 /* 740 /*
741 * Insert the new fragment queue entry into both queues. 741 * Insert the new fragment queue entry into both queues.
742 */ 742 */
743 tiqe->ipqe_m = m; 743 tiqe->ipqe_m = m;
744 tiqe->ipqe_seq = pkt_seq; 744 tiqe->ipqe_seq = pkt_seq;
745 tiqe->ipqe_len = pkt_len; 745 tiqe->ipqe_len = pkt_len;
746 tiqe->ipqe_flags = pkt_flags; 746 tiqe->ipqe_flags = pkt_flags;
747 if (p == NULL) { 747 if (p == NULL) {
748 TAILQ_INSERT_HEAD(&tp->segq, tiqe, ipqe_q); 748 TAILQ_INSERT_HEAD(&tp->segq, tiqe, ipqe_q);
749 } else { 749 } else {
750 TAILQ_INSERT_AFTER(&tp->segq, p, tiqe, ipqe_q); 750 TAILQ_INSERT_AFTER(&tp->segq, p, tiqe, ipqe_q);
751 } 751 }
752 tp->t_segqlen++; 752 tp->t_segqlen++;
753 753
754skip_replacement: 754skip_replacement:
755 TAILQ_INSERT_HEAD(&tp->timeq, tiqe, ipqe_timeq); 755 TAILQ_INSERT_HEAD(&tp->timeq, tiqe, ipqe_timeq);
756 756
757present: 757present:
758 /* 758 /*
759 * Present data to user, advancing rcv_nxt through 759 * Present data to user, advancing rcv_nxt through
760 * completed sequence space. 760 * completed sequence space.
761 */ 761 */
762 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0) 762 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
763 goto out; 763 goto out;
764 q = TAILQ_FIRST(&tp->segq); 764 q = TAILQ_FIRST(&tp->segq);
765 if (q == NULL || q->ipqe_seq != tp->rcv_nxt) 765 if (q == NULL || q->ipqe_seq != tp->rcv_nxt)
766 goto out; 766 goto out;
767 if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_len) 767 if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_len)
768 goto out; 768 goto out;
769 769
770 tp->rcv_nxt += q->ipqe_len; 770 tp->rcv_nxt += q->ipqe_len;
771 pkt_flags = q->ipqe_flags & TH_FIN; 771 pkt_flags = q->ipqe_flags & TH_FIN;
772 nd6_hint(tp); 772 nd6_hint(tp);
773 773
774 TAILQ_REMOVE(&tp->segq, q, ipqe_q); 774 TAILQ_REMOVE(&tp->segq, q, ipqe_q);
775 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq); 775 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
776 tp->t_segqlen--; 776 tp->t_segqlen--;
777 KASSERT(tp->t_segqlen >= 0); 777 KASSERT(tp->t_segqlen >= 0);
778 KASSERT(tp->t_segqlen != 0 || 778 KASSERT(tp->t_segqlen != 0 ||
779 (TAILQ_EMPTY(&tp->segq) && TAILQ_EMPTY(&tp->timeq))); 779 (TAILQ_EMPTY(&tp->segq) && TAILQ_EMPTY(&tp->timeq)));
780 if (so->so_state & SS_CANTRCVMORE) 780 if (so->so_state & SS_CANTRCVMORE)
781 m_freem(q->ipqe_m); 781 m_freem(q->ipqe_m);
782 else 782 else
783 sbappendstream(&so->so_rcv, q->ipqe_m); 783 sbappendstream(&so->so_rcv, q->ipqe_m);
784 tcpipqent_free(q); 784 tcpipqent_free(q);
785 TCP_REASS_UNLOCK(tp); 785 TCP_REASS_UNLOCK(tp);
786 sorwakeup(so); 786 sorwakeup(so);
787 return pkt_flags; 787 return pkt_flags;
788 788
789out: 789out:
790 TCP_REASS_UNLOCK(tp); 790 TCP_REASS_UNLOCK(tp);
791 return 0; 791 return 0;
792} 792}
793 793
794#ifdef INET6 794#ifdef INET6
795int 795int
796tcp6_input(struct mbuf **mp, int *offp, int proto) 796tcp6_input(struct mbuf **mp, int *offp, int proto)
797{ 797{
798 struct mbuf *m = *mp; 798 struct mbuf *m = *mp;
799 799
800 /* 800 /*
801 * draft-itojun-ipv6-tcp-to-anycast 801 * draft-itojun-ipv6-tcp-to-anycast
802 * better place to put this in? 802 * better place to put this in?
803 */ 803 */
804 if (m->m_flags & M_ANYCAST6) { 804 if (m->m_flags & M_ANYCAST6) {
805 struct ip6_hdr *ip6; 805 struct ip6_hdr *ip6;
806 if (m->m_len < sizeof(struct ip6_hdr)) { 806 if (m->m_len < sizeof(struct ip6_hdr)) {
807 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 807 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
808 TCP_STATINC(TCP_STAT_RCVSHORT); 808 TCP_STATINC(TCP_STAT_RCVSHORT);
809 return IPPROTO_DONE; 809 return IPPROTO_DONE;
810 } 810 }
811 } 811 }
812 ip6 = mtod(m, struct ip6_hdr *); 812 ip6 = mtod(m, struct ip6_hdr *);
813 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 813 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
814 (char *)&ip6->ip6_dst - (char *)ip6); 814 (char *)&ip6->ip6_dst - (char *)ip6);
815 return IPPROTO_DONE; 815 return IPPROTO_DONE;
816 } 816 }
817 817
818 tcp_input(m, *offp, proto); 818 tcp_input(m, *offp, proto);
819 return IPPROTO_DONE; 819 return IPPROTO_DONE;
820} 820}
821#endif 821#endif
822 822
823static void 823static void
824tcp4_log_refused(const struct ip *ip, const struct tcphdr *th) 824tcp4_log_refused(const struct ip *ip, const struct tcphdr *th)
825{ 825{
826 char src[INET_ADDRSTRLEN]; 826 char src[INET_ADDRSTRLEN];
827 char dst[INET_ADDRSTRLEN]; 827 char dst[INET_ADDRSTRLEN];
828 828
829 if (ip) { 829 if (ip) {
830 in_print(src, sizeof(src), &ip->ip_src); 830 in_print(src, sizeof(src), &ip->ip_src);
831 in_print(dst, sizeof(dst), &ip->ip_dst); 831 in_print(dst, sizeof(dst), &ip->ip_dst);
832 } else { 832 } else {
833 strlcpy(src, "(unknown)", sizeof(src)); 833 strlcpy(src, "(unknown)", sizeof(src));
834 strlcpy(dst, "(unknown)", sizeof(dst)); 834 strlcpy(dst, "(unknown)", sizeof(dst));
835 } 835 }
836 log(LOG_INFO, 836 log(LOG_INFO,
837 "Connection attempt to TCP %s:%d from %s:%d\n", 837 "Connection attempt to TCP %s:%d from %s:%d\n",
838 dst, ntohs(th->th_dport), 838 dst, ntohs(th->th_dport),
839 src, ntohs(th->th_sport)); 839 src, ntohs(th->th_sport));
840} 840}
841 841
842#ifdef INET6 842#ifdef INET6
843static void 843static void
844tcp6_log_refused(const struct ip6_hdr *ip6, const struct tcphdr *th) 844tcp6_log_refused(const struct ip6_hdr *ip6, const struct tcphdr *th)
845{ 845{
846 char src[INET6_ADDRSTRLEN]; 846 char src[INET6_ADDRSTRLEN];
847 char dst[INET6_ADDRSTRLEN]; 847 char dst[INET6_ADDRSTRLEN];
848 848
849 if (ip6) { 849 if (ip6) {
850 in6_print(src, sizeof(src), &ip6->ip6_src); 850 in6_print(src, sizeof(src), &ip6->ip6_src);
851 in6_print(dst, sizeof(dst), &ip6->ip6_dst); 851 in6_print(dst, sizeof(dst), &ip6->ip6_dst);
852 } else { 852 } else {
853 strlcpy(src, "(unknown v6)", sizeof(src)); 853 strlcpy(src, "(unknown v6)", sizeof(src));
854 strlcpy(dst, "(unknown v6)", sizeof(dst)); 854 strlcpy(dst, "(unknown v6)", sizeof(dst));
855 } 855 }
856 log(LOG_INFO, 856 log(LOG_INFO,
857 "Connection attempt to TCP [%s]:%d from [%s]:%d\n", 857 "Connection attempt to TCP [%s]:%d from [%s]:%d\n",
858 dst, ntohs(th->th_dport), 858 dst, ntohs(th->th_dport),
859 src, ntohs(th->th_sport)); 859 src, ntohs(th->th_sport));
860} 860}
861#endif 861#endif
862 862
863/* 863/*
864 * Checksum extended TCP header and data. 864 * Checksum extended TCP header and data.
865 */ 865 */
866int 866int
867tcp_input_checksum(int af, struct mbuf *m, const struct tcphdr *th, 867tcp_input_checksum(int af, struct mbuf *m, const struct tcphdr *th,
868 int toff, int off, int tlen) 868 int toff, int off, int tlen)
869{ 869{
870 struct ifnet *rcvif; 870 struct ifnet *rcvif;
871 int s; 871 int s;
872 872
873 /* 873 /*
874 * XXX it's better to record and check if this mbuf is 874 * XXX it's better to record and check if this mbuf is
875 * already checked. 875 * already checked.
876 */ 876 */
877 877
878 rcvif = m_get_rcvif(m, &s); 878 rcvif = m_get_rcvif(m, &s);
879 if (__predict_false(rcvif == NULL)) 879 if (__predict_false(rcvif == NULL))
880 goto badcsum; /* XXX */ 880 goto badcsum; /* XXX */
881 881
882 switch (af) { 882 switch (af) {
883 case AF_INET: 883 case AF_INET:
884 switch (m->m_pkthdr.csum_flags & 884 switch (m->m_pkthdr.csum_flags &
885 ((rcvif->if_csum_flags_rx & M_CSUM_TCPv4) | 885 ((rcvif->if_csum_flags_rx & M_CSUM_TCPv4) |
886 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) { 886 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
887 case M_CSUM_TCPv4|M_CSUM_TCP_UDP_BAD: 887 case M_CSUM_TCPv4|M_CSUM_TCP_UDP_BAD:
888 TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_bad); 888 TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_bad);
889 goto badcsum; 889 goto badcsum;
890 890
891 case M_CSUM_TCPv4|M_CSUM_DATA: { 891 case M_CSUM_TCPv4|M_CSUM_DATA: {
892 u_int32_t hw_csum = m->m_pkthdr.csum_data; 892 u_int32_t hw_csum = m->m_pkthdr.csum_data;
893 893
894 TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_data); 894 TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_data);
895 if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) { 895 if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
896 const struct ip *ip = 896 const struct ip *ip =
897 mtod(m, const struct ip *); 897 mtod(m, const struct ip *);
898 898
899 hw_csum = in_cksum_phdr(ip->ip_src.s_addr, 899 hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
900 ip->ip_dst.s_addr, 900 ip->ip_dst.s_addr,
901 htons(hw_csum + tlen + off + IPPROTO_TCP)); 901 htons(hw_csum + tlen + off + IPPROTO_TCP));
902 } 902 }
903 if ((hw_csum ^ 0xffff) != 0) 903 if ((hw_csum ^ 0xffff) != 0)
904 goto badcsum; 904 goto badcsum;
905 break; 905 break;
906 } 906 }
907 907
908 case M_CSUM_TCPv4: 908 case M_CSUM_TCPv4:
909 /* Checksum was okay. */ 909 /* Checksum was okay. */
910 TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_ok); 910 TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_ok);
911 break; 911 break;
912 912
913 default: 913 default:
914 /* 914 /*
915 * Must compute it ourselves. Maybe skip checksum 915 * Must compute it ourselves. Maybe skip checksum
916 * on loopback interfaces. 916 * on loopback interfaces.
917 */ 917 */
918 if (__predict_true(!(rcvif->if_flags & IFF_LOOPBACK) || 918 if (__predict_true(!(rcvif->if_flags & IFF_LOOPBACK) ||
919 tcp_do_loopback_cksum)) { 919 tcp_do_loopback_cksum)) {
920 TCP_CSUM_COUNTER_INCR(&tcp_swcsum); 920 TCP_CSUM_COUNTER_INCR(&tcp_swcsum);
921 if (in4_cksum(m, IPPROTO_TCP, toff, 921 if (in4_cksum(m, IPPROTO_TCP, toff,
922 tlen + off) != 0) 922 tlen + off) != 0)
923 goto badcsum; 923 goto badcsum;
924 } 924 }
925 break; 925 break;
926 } 926 }
927 break; 927 break;
928 928
929#ifdef INET6 929#ifdef INET6
930 case AF_INET6: 930 case AF_INET6:
931 switch (m->m_pkthdr.csum_flags & 931 switch (m->m_pkthdr.csum_flags &
932 ((rcvif->if_csum_flags_rx & M_CSUM_TCPv6) | 932 ((rcvif->if_csum_flags_rx & M_CSUM_TCPv6) |
933 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) { 933 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
934 case M_CSUM_TCPv6|M_CSUM_TCP_UDP_BAD: 934 case M_CSUM_TCPv6|M_CSUM_TCP_UDP_BAD:
935 TCP_CSUM_COUNTER_INCR(&tcp6_hwcsum_bad); 935 TCP_CSUM_COUNTER_INCR(&tcp6_hwcsum_bad);
936 goto badcsum; 936 goto badcsum;
937 937
938#if 0 /* notyet */ 938#if 0 /* notyet */
939 case M_CSUM_TCPv6|M_CSUM_DATA: 939 case M_CSUM_TCPv6|M_CSUM_DATA:
940#endif 940#endif
941 941
942 case M_CSUM_TCPv6: 942 case M_CSUM_TCPv6:
943 /* Checksum was okay. */ 943 /* Checksum was okay. */
944 TCP_CSUM_COUNTER_INCR(&tcp6_hwcsum_ok); 944 TCP_CSUM_COUNTER_INCR(&tcp6_hwcsum_ok);
945 break; 945 break;
946 946
947 default: 947 default:
948 /* 948 /*
949 * Must compute it ourselves. Maybe skip checksum 949 * Must compute it ourselves. Maybe skip checksum
950 * on loopback interfaces. 950 * on loopback interfaces.
951 */ 951 */
952 if (__predict_true((m->m_flags & M_LOOP) == 0 || 952 if (__predict_true((m->m_flags & M_LOOP) == 0 ||
953 tcp_do_loopback_cksum)) { 953 tcp_do_loopback_cksum)) {
954 TCP_CSUM_COUNTER_INCR(&tcp6_swcsum); 954 TCP_CSUM_COUNTER_INCR(&tcp6_swcsum);
955 if (in6_cksum(m, IPPROTO_TCP, toff, 955 if (in6_cksum(m, IPPROTO_TCP, toff,
956 tlen + off) != 0) 956 tlen + off) != 0)
957 goto badcsum; 957 goto badcsum;
958 } 958 }
959 } 959 }
960 break; 960 break;
961#endif /* INET6 */ 961#endif /* INET6 */
962 } 962 }
963 m_put_rcvif(rcvif, &s); 963 m_put_rcvif(rcvif, &s);
964 964
965 return 0; 965 return 0;
966 966
967badcsum: 967badcsum:
968 m_put_rcvif(rcvif, &s); 968 m_put_rcvif(rcvif, &s);
969 TCP_STATINC(TCP_STAT_RCVBADSUM); 969 TCP_STATINC(TCP_STAT_RCVBADSUM);
970 return -1; 970 return -1;
971} 971}
972 972
973/* 973/*
974 * When a packet arrives addressed to a vestigial tcpbp, we 974 * When a packet arrives addressed to a vestigial tcpbp, we
975 * nevertheless have to respond to it per the spec. 975 * nevertheless have to respond to it per the spec.
976 * 976 *
977 * This code is duplicated from the one in tcp_input(). 977 * This code is duplicated from the one in tcp_input().
978 */ 978 */
979static void tcp_vtw_input(struct tcphdr *th, vestigial_inpcb_t *vp, 979static void tcp_vtw_input(struct tcphdr *th, vestigial_inpcb_t *vp,
980 struct mbuf *m, int tlen) 980 struct mbuf *m, int tlen)
981{ 981{
982 int tiflags; 982 int tiflags;
983 int todrop; 983 int todrop;
984 uint32_t t_flags = 0; 984 uint32_t t_flags = 0;
985 uint64_t *tcps; 985 uint64_t *tcps;
986 986
987 tiflags = th->th_flags; 987 tiflags = th->th_flags;
988 todrop = vp->rcv_nxt - th->th_seq; 988 todrop = vp->rcv_nxt - th->th_seq;
989 989
990 if (todrop > 0) { 990 if (todrop > 0) {
991 if (tiflags & TH_SYN) { 991 if (tiflags & TH_SYN) {
992 tiflags &= ~TH_SYN; 992 tiflags &= ~TH_SYN;
993 th->th_seq++; 993 th->th_seq++;
994 tcp_urp_drop(th, 1, &tiflags); 994 tcp_urp_drop(th, 1, &tiflags);
995 todrop--; 995 todrop--;
996 } 996 }
997 if (todrop > tlen || 997 if (todrop > tlen ||
998 (todrop == tlen && (tiflags & TH_FIN) == 0)) { 998 (todrop == tlen && (tiflags & TH_FIN) == 0)) {
999 /* 999 /*
1000 * Any valid FIN or RST must be to the left of the 1000 * Any valid FIN or RST must be to the left of the
1001 * window. At this point the FIN or RST must be a 1001 * window. At this point the FIN or RST must be a
1002 * duplicate or out of sequence; drop it. 1002 * duplicate or out of sequence; drop it.
1003 */ 1003 */
1004 if (tiflags & TH_RST) 1004 if (tiflags & TH_RST)
1005 goto drop; 1005 goto drop;
1006 tiflags &= ~(TH_FIN|TH_RST); 1006 tiflags &= ~(TH_FIN|TH_RST);
1007 1007
1008 /* 1008 /*
1009 * Send an ACK to resynchronize and drop any data. 1009 * Send an ACK to resynchronize and drop any data.
1010 * But keep on processing for RST or ACK. 1010 * But keep on processing for RST or ACK.
1011 */ 1011 */
1012 t_flags |= TF_ACKNOW; 1012 t_flags |= TF_ACKNOW;
1013 todrop = tlen; 1013 todrop = tlen;
1014 tcps = TCP_STAT_GETREF(); 1014 tcps = TCP_STAT_GETREF();
1015 tcps[TCP_STAT_RCVDUPPACK] += 1; 1015 tcps[TCP_STAT_RCVDUPPACK] += 1;
1016 tcps[TCP_STAT_RCVDUPBYTE] += todrop; 1016 tcps[TCP_STAT_RCVDUPBYTE] += todrop;
1017 TCP_STAT_PUTREF(); 1017 TCP_STAT_PUTREF();
1018 } else if ((tiflags & TH_RST) && 1018 } else if ((tiflags & TH_RST) &&
1019 th->th_seq != vp->rcv_nxt) { 1019 th->th_seq != vp->rcv_nxt) {
1020 /* 1020 /*
1021 * Test for reset before adjusting the sequence 1021 * Test for reset before adjusting the sequence
1022 * number for overlapping data. 1022 * number for overlapping data.
1023 */ 1023 */
1024 goto dropafterack_ratelim; 1024 goto dropafterack_ratelim;
1025 } else { 1025 } else {
1026 tcps = TCP_STAT_GETREF(); 1026 tcps = TCP_STAT_GETREF();
1027 tcps[TCP_STAT_RCVPARTDUPPACK] += 1; 1027 tcps[TCP_STAT_RCVPARTDUPPACK] += 1;
1028 tcps[TCP_STAT_RCVPARTDUPBYTE] += todrop; 1028 tcps[TCP_STAT_RCVPARTDUPBYTE] += todrop;
1029 TCP_STAT_PUTREF(); 1029 TCP_STAT_PUTREF();
1030 } 1030 }
1031 1031
1032// tcp_new_dsack(tp, th->th_seq, todrop); 1032// tcp_new_dsack(tp, th->th_seq, todrop);
1033// hdroptlen += todrop; /*drop from head afterwards*/ 1033// hdroptlen += todrop; /*drop from head afterwards*/
1034 1034
1035 th->th_seq += todrop; 1035 th->th_seq += todrop;
1036 tlen -= todrop; 1036 tlen -= todrop;
1037 tcp_urp_drop(th, todrop, &tiflags); 1037 tcp_urp_drop(th, todrop, &tiflags);
1038 } 1038 }
1039 1039
1040 /* 1040 /*
1041 * If new data are received on a connection after the 1041 * If new data are received on a connection after the
1042 * user processes are gone, then RST the other end. 1042 * user processes are gone, then RST the other end.
1043 */ 1043 */
1044 if (tlen) { 1044 if (tlen) {
1045 TCP_STATINC(TCP_STAT_RCVAFTERCLOSE); 1045 TCP_STATINC(TCP_STAT_RCVAFTERCLOSE);
1046 goto dropwithreset; 1046 goto dropwithreset;
1047 } 1047 }
1048 1048
1049 /* 1049 /*
1050 * If segment ends after window, drop trailing data 1050 * If segment ends after window, drop trailing data
1051 * (and PUSH and FIN); if nothing left, just ACK. 1051 * (and PUSH and FIN); if nothing left, just ACK.
1052 */ 1052 */
1053 todrop = (th->th_seq + tlen) - (vp->rcv_nxt + vp->rcv_wnd); 1053 todrop = (th->th_seq + tlen) - (vp->rcv_nxt + vp->rcv_wnd);
1054 1054
1055 if (todrop > 0) { 1055 if (todrop > 0) {
1056 TCP_STATINC(TCP_STAT_RCVPACKAFTERWIN); 1056 TCP_STATINC(TCP_STAT_RCVPACKAFTERWIN);
1057 if (todrop >= tlen) { 1057 if (todrop >= tlen) {
1058 /* 1058 /*
1059 * The segment actually starts after the window. 1059 * The segment actually starts after the window.
1060 * th->th_seq + tlen - vp->rcv_nxt - vp->rcv_wnd >= tlen 1060 * th->th_seq + tlen - vp->rcv_nxt - vp->rcv_wnd >= tlen
1061 * th->th_seq - vp->rcv_nxt - vp->rcv_wnd >= 0 1061 * th->th_seq - vp->rcv_nxt - vp->rcv_wnd >= 0
1062 * th->th_seq >= vp->rcv_nxt + vp->rcv_wnd 1062 * th->th_seq >= vp->rcv_nxt + vp->rcv_wnd
1063 */ 1063 */
1064 TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, tlen); 1064 TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, tlen);
1065 1065
1066 /* 1066 /*
1067 * If a new connection request is received 1067 * If a new connection request is received
1068 * while in TIME_WAIT, drop the old connection 1068 * while in TIME_WAIT, drop the old connection
1069 * and start over if the sequence numbers 1069 * and start over if the sequence numbers
1070 * are above the previous ones. 1070 * are above the previous ones.
1071 */ 1071 */
1072 if ((tiflags & TH_SYN) && 1072 if ((tiflags & TH_SYN) &&
1073 SEQ_GT(th->th_seq, vp->rcv_nxt)) { 1073 SEQ_GT(th->th_seq, vp->rcv_nxt)) {
1074 /* 1074 /*
1075 * We only support this in the !NOFDREF case, which 1075 * We only support this in the !NOFDREF case, which
1076 * is to say: not here. 1076 * is to say: not here.
1077 */ 1077 */
1078 goto dropwithreset; 1078 goto dropwithreset;
1079 } 1079 }
1080 1080
1081 /* 1081 /*
1082 * If window is closed can only take segments at 1082 * If window is closed can only take segments at
1083 * window edge, and have to drop data and PUSH from 1083 * window edge, and have to drop data and PUSH from
1084 * incoming segments. Continue processing, but 1084 * incoming segments. Continue processing, but
1085 * remember to ack. Otherwise, drop segment 1085 * remember to ack. Otherwise, drop segment
1086 * and (if not RST) ack. 1086 * and (if not RST) ack.
1087 */ 1087 */
1088 if (vp->rcv_wnd == 0 && th->th_seq == vp->rcv_nxt) { 1088 if (vp->rcv_wnd == 0 && th->th_seq == vp->rcv_nxt) {
1089 t_flags |= TF_ACKNOW; 1089 t_flags |= TF_ACKNOW;
1090 TCP_STATINC(TCP_STAT_RCVWINPROBE); 1090 TCP_STATINC(TCP_STAT_RCVWINPROBE);
1091 } else { 1091 } else {
1092 goto dropafterack; 1092 goto dropafterack;
1093 } 1093 }
1094 } else { 1094 } else {
1095 TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, todrop); 1095 TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, todrop);
1096 } 1096 }
1097 m_adj(m, -todrop); 1097 m_adj(m, -todrop);
1098 tlen -= todrop; 1098 tlen -= todrop;
1099 tiflags &= ~(TH_PUSH|TH_FIN); 1099 tiflags &= ~(TH_PUSH|TH_FIN);
1100 } 1100 }
1101 1101
1102 if (tiflags & TH_RST) { 1102 if (tiflags & TH_RST) {
1103 if (th->th_seq != vp->rcv_nxt) 1103 if (th->th_seq != vp->rcv_nxt)
1104 goto dropafterack_ratelim; 1104 goto dropafterack_ratelim;
1105 1105
1106 vtw_del(vp->ctl, vp->vtw); 1106 vtw_del(vp->ctl, vp->vtw);
1107 goto drop; 1107 goto drop;
1108 } 1108 }
1109 1109
1110 /* 1110 /*
1111 * If the ACK bit is off we drop the segment and return. 1111 * If the ACK bit is off we drop the segment and return.
1112 */ 1112 */
1113 if ((tiflags & TH_ACK) == 0) { 1113 if ((tiflags & TH_ACK) == 0) {
1114 if (t_flags & TF_ACKNOW) 1114 if (t_flags & TF_ACKNOW)
1115 goto dropafterack; 1115 goto dropafterack;
1116 goto drop; 1116 goto drop;
1117 } 1117 }
1118 1118
1119 /* 1119 /*
1120 * In TIME_WAIT state the only thing that should arrive 1120 * In TIME_WAIT state the only thing that should arrive
1121 * is a retransmission of the remote FIN. Acknowledge 1121 * is a retransmission of the remote FIN. Acknowledge
1122 * it and restart the finack timer. 1122 * it and restart the finack timer.
1123 */ 1123 */
1124 vtw_restart(vp); 1124 vtw_restart(vp);
1125 goto dropafterack; 1125 goto dropafterack;
1126 1126
1127dropafterack: 1127dropafterack:
1128 /* 1128 /*
1129 * Generate an ACK dropping incoming segment if it occupies 1129 * Generate an ACK dropping incoming segment if it occupies
1130 * sequence space, where the ACK reflects our state. 1130 * sequence space, where the ACK reflects our state.
1131 */ 1131 */
1132 if (tiflags & TH_RST) 1132 if (tiflags & TH_RST)
1133 goto drop; 1133 goto drop;
1134 goto dropafterack2; 1134 goto dropafterack2;
1135 1135
1136dropafterack_ratelim: 1136dropafterack_ratelim:
1137 /* 1137 /*
1138 * We may want to rate-limit ACKs against SYN/RST attack. 1138 * We may want to rate-limit ACKs against SYN/RST attack.
1139 */ 1139 */
1140 if (ppsratecheck(&tcp_ackdrop_ppslim_last, &tcp_ackdrop_ppslim_count, 1140 if (ppsratecheck(&tcp_ackdrop_ppslim_last, &tcp_ackdrop_ppslim_count,
1141 tcp_ackdrop_ppslim) == 0) { 1141 tcp_ackdrop_ppslim) == 0) {
1142 /* XXX stat */ 1142 /* XXX stat */
1143 goto drop; 1143 goto drop;
1144 } 1144 }
1145 /* ...fall into dropafterack2... */ 1145 /* ...fall into dropafterack2... */
1146 1146
1147dropafterack2: 1147dropafterack2:
1148 (void)tcp_respond(0, m, m, th, th->th_seq + tlen, th->th_ack, TH_ACK); 1148 (void)tcp_respond(0, m, m, th, th->th_seq + tlen, th->th_ack, TH_ACK);
1149 return; 1149 return;
1150 1150
1151dropwithreset: 1151dropwithreset:
1152 /* 1152 /*
1153 * Generate a RST, dropping incoming segment. 1153 * Generate a RST, dropping incoming segment.
1154 * Make ACK acceptable to originator of segment. 1154 * Make ACK acceptable to originator of segment.
1155 */ 1155 */
1156 if (tiflags & TH_RST) 1156 if (tiflags & TH_RST)
1157 goto drop; 1157 goto drop;
1158 1158
1159 if (tiflags & TH_ACK) { 1159 if (tiflags & TH_ACK) {
1160 tcp_respond(0, m, m, th, (tcp_seq)0, th->th_ack, TH_RST); 1160 tcp_respond(0, m, m, th, (tcp_seq)0, th->th_ack, TH_RST);
1161 } else { 1161 } else {
1162 if (tiflags & TH_SYN) 1162 if (tiflags & TH_SYN)
1163 ++tlen; 1163 ++tlen;
1164 (void)tcp_respond(0, m, m, th, th->th_seq + tlen, (tcp_seq)0, 1164 (void)tcp_respond(0, m, m, th, th->th_seq + tlen, (tcp_seq)0,
1165 TH_RST|TH_ACK); 1165 TH_RST|TH_ACK);
1166 } 1166 }
1167 return; 1167 return;
1168drop: 1168drop:
1169 m_freem(m); 1169 m_freem(m);
1170} 1170}
1171 1171
1172/* 1172/*
1173 * TCP input routine, follows pages 65-76 of RFC 793 very closely. 1173 * TCP input routine, follows pages 65-76 of RFC 793 very closely.
1174 */ 1174 */
1175void 1175void
1176tcp_input(struct mbuf *m, int off, int proto) 1176tcp_input(struct mbuf *m, int off, int proto)
1177{ 1177{
1178 struct tcphdr *th; 1178 struct tcphdr *th;
1179 struct ip *ip; 1179 struct ip *ip;
1180 struct inpcb *inp; 1180 struct inpcb *inp;
1181#ifdef INET6 1181#ifdef INET6
1182 struct ip6_hdr *ip6; 1182 struct ip6_hdr *ip6;
1183 struct in6pcb *in6p; 1183 struct in6pcb *in6p;
1184#endif 1184#endif
1185 u_int8_t *optp = NULL; 1185 u_int8_t *optp = NULL;
1186 int optlen = 0; 1186 int optlen = 0;
1187 int len, tlen, hdroptlen = 0; 1187 int len, tlen, hdroptlen = 0;
1188 struct tcpcb *tp = NULL; 1188 struct tcpcb *tp = NULL;
1189 int tiflags; 1189 int tiflags;
1190 struct socket *so = NULL; 1190 struct socket *so = NULL;
1191 int todrop, acked, ourfinisacked, needoutput = 0; 1191 int todrop, acked, ourfinisacked, needoutput = 0;
1192 bool dupseg; 1192 bool dupseg;
1193#ifdef TCP_DEBUG 1193#ifdef TCP_DEBUG
1194 short ostate = 0; 1194 short ostate = 0;
1195#endif 1195#endif
1196 u_long tiwin; 1196 u_long tiwin;
1197 struct tcp_opt_info opti; 1197 struct tcp_opt_info opti;
1198 int thlen, iphlen; 1198 int thlen, iphlen;
1199 int af; /* af on the wire */ 1199 int af; /* af on the wire */
1200 struct mbuf *tcp_saveti = NULL; 1200 struct mbuf *tcp_saveti = NULL;
1201 uint32_t ts_rtt; 1201 uint32_t ts_rtt;
1202 uint8_t iptos; 1202 uint8_t iptos;
1203 uint64_t *tcps; 1203 uint64_t *tcps;
1204 vestigial_inpcb_t vestige; 1204 vestigial_inpcb_t vestige;
1205 1205
1206 vestige.valid = 0; 1206 vestige.valid = 0;
1207 1207
1208 MCLAIM(m, &tcp_rx_mowner); 1208 MCLAIM(m, &tcp_rx_mowner);
1209 1209
1210 TCP_STATINC(TCP_STAT_RCVTOTAL); 1210 TCP_STATINC(TCP_STAT_RCVTOTAL);
1211 1211
1212 memset(&opti, 0, sizeof(opti)); 1212 memset(&opti, 0, sizeof(opti));
1213 opti.ts_present = 0; 1213 opti.ts_present = 0;
1214 opti.maxseg = 0; 1214 opti.maxseg = 0;
1215 1215
1216 /* 1216 /*
1217 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN. 1217 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN.
1218 * 1218 *
1219 * TCP is, by definition, unicast, so we reject all 1219 * TCP is, by definition, unicast, so we reject all
1220 * multicast outright. 1220 * multicast outright.
1221 * 1221 *
1222 * Note, there are additional src/dst address checks in 1222 * Note, there are additional src/dst address checks in
1223 * the AF-specific code below. 1223 * the AF-specific code below.
1224 */ 1224 */
1225 if (m->m_flags & (M_BCAST|M_MCAST)) { 1225 if (m->m_flags & (M_BCAST|M_MCAST)) {
1226 /* XXX stat */ 1226 /* XXX stat */
1227 goto drop; 1227 goto drop;
1228 } 1228 }
1229#ifdef INET6 1229#ifdef INET6
1230 if (m->m_flags & M_ANYCAST6) { 1230 if (m->m_flags & M_ANYCAST6) {
1231 /* XXX stat */ 1231 /* XXX stat */
1232 goto drop; 1232 goto drop;
1233 } 1233 }
1234#endif 1234#endif
1235 1235
1236 M_REGION_GET(th, struct tcphdr *, m, off, sizeof(struct tcphdr)); 1236 M_REGION_GET(th, struct tcphdr *, m, off, sizeof(struct tcphdr));
1237 if (th == NULL) { 1237 if (th == NULL) {
1238 TCP_STATINC(TCP_STAT_RCVSHORT); 1238 TCP_STATINC(TCP_STAT_RCVSHORT);
1239 return; 1239 return;
1240 } 1240 }
1241 1241
1242 /* 1242 /*
1243 * Enforce alignment requirements that are violated in 1243 * Enforce alignment requirements that are violated in
1244 * some cases, see kern/50766 for details. 1244 * some cases, see kern/50766 for details.
1245 */ 1245 */
1246 if (TCP_HDR_ALIGNED_P(th) == 0) { 1246 if (TCP_HDR_ALIGNED_P(th) == 0) {
1247 m = m_copyup(m, off + sizeof(struct tcphdr), 0); 1247 m = m_copyup(m, off + sizeof(struct tcphdr), 0);
1248 if (m == NULL) { 1248 if (m == NULL) {
1249 TCP_STATINC(TCP_STAT_RCVSHORT); 1249 TCP_STATINC(TCP_STAT_RCVSHORT);
1250 return; 1250 return;
1251 } 1251 }
1252 th = (struct tcphdr *)(mtod(m, char *) + off); 1252 th = (struct tcphdr *)(mtod(m, char *) + off);
1253 } 1253 }
1254 KASSERT(TCP_HDR_ALIGNED_P(th)); 1254 KASSERT(TCP_HDR_ALIGNED_P(th));
1255 1255
1256 /* 1256 /*
1257 * Get IP and TCP header. 1257 * Get IP and TCP header.
1258 * Note: IP leaves IP header in first mbuf. 1258 * Note: IP leaves IP header in first mbuf.
1259 */ 1259 */
1260 ip = mtod(m, struct ip *); 1260 ip = mtod(m, struct ip *);
1261#ifdef INET6 1261#ifdef INET6
1262 ip6 = mtod(m, struct ip6_hdr *); 1262 ip6 = mtod(m, struct ip6_hdr *);
1263#endif 1263#endif
1264 switch (ip->ip_v) { 1264 switch (ip->ip_v) {
1265 case 4: 1265 case 4:
1266 af = AF_INET; 1266 af = AF_INET;
1267 iphlen = sizeof(struct ip); 1267 iphlen = sizeof(struct ip);
1268 1268
1269 if (IN_MULTICAST(ip->ip_dst.s_addr) || 1269 if (IN_MULTICAST(ip->ip_dst.s_addr) ||
1270 in_broadcast(ip->ip_dst, m_get_rcvif_NOMPSAFE(m))) 1270 in_broadcast(ip->ip_dst, m_get_rcvif_NOMPSAFE(m)))
1271 goto drop; 1271 goto drop;
1272 1272
1273 /* We do the checksum after PCB lookup... */ 1273 /* We do the checksum after PCB lookup... */
1274 len = ntohs(ip->ip_len); 1274 len = ntohs(ip->ip_len);
1275 tlen = len - off; 1275 tlen = len - off;
1276 iptos = ip->ip_tos; 1276 iptos = ip->ip_tos;
1277 break; 1277 break;
1278#ifdef INET6 1278#ifdef INET6
1279 case 6: 1279 case 6:
1280 iphlen = sizeof(struct ip6_hdr); 1280 iphlen = sizeof(struct ip6_hdr);
1281 af = AF_INET6; 1281 af = AF_INET6;
1282 1282
1283 /* 1283 /*
1284 * Be proactive about unspecified IPv6 address in source. 1284 * Be proactive about unspecified IPv6 address in source.
1285 * As we use all-zero to indicate unbounded/unconnected pcb, 1285 * As we use all-zero to indicate unbounded/unconnected pcb,
1286 * unspecified IPv6 address can be used to confuse us. 1286 * unspecified IPv6 address can be used to confuse us.
1287 * 1287 *
1288 * Note that packets with unspecified IPv6 destination is 1288 * Note that packets with unspecified IPv6 destination is
1289 * already dropped in ip6_input. 1289 * already dropped in ip6_input.
1290 */ 1290 */
1291 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 1291 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1292 /* XXX stat */ 1292 /* XXX stat */
1293 goto drop; 1293 goto drop;
1294 } 1294 }
1295 1295
1296 /* 1296 /*
1297 * Make sure destination address is not multicast. 1297 * Make sure destination address is not multicast.
1298 * Source address checked in ip6_input(). 1298 * Source address checked in ip6_input().
1299 */ 1299 */
1300 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1300 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1301 /* XXX stat */ 1301 /* XXX stat */
1302 goto drop; 1302 goto drop;
1303 } 1303 }
1304 1304
1305 /* We do the checksum after PCB lookup... */ 1305 /* We do the checksum after PCB lookup... */
1306 len = m->m_pkthdr.len; 1306 len = m->m_pkthdr.len;
1307 tlen = len - off; 1307 tlen = len - off;
1308 iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 1308 iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
1309 break; 1309 break;
1310#endif 1310#endif
1311 default: 1311 default:
1312 m_freem(m); 1312 m_freem(m);
1313 return; 1313 return;
1314 } 1314 }
1315 1315
1316 1316
1317 /* 1317 /*
1318 * Check that TCP offset makes sense, pull out TCP options and 1318 * Check that TCP offset makes sense, pull out TCP options and
1319 * adjust length. 1319 * adjust length.
1320 */ 1320 */
1321 thlen = th->th_off << 2; 1321 thlen = th->th_off << 2;
1322 if (thlen < sizeof(struct tcphdr) || thlen > tlen) { 1322 if (thlen < sizeof(struct tcphdr) || thlen > tlen) {
1323 TCP_STATINC(TCP_STAT_RCVBADOFF); 1323 TCP_STATINC(TCP_STAT_RCVBADOFF);
1324 goto drop; 1324 goto drop;
1325 } 1325 }
1326 tlen -= thlen; 1326 tlen -= thlen;
1327 1327
1328 if (thlen > sizeof(struct tcphdr)) { 1328 if (thlen > sizeof(struct tcphdr)) {
1329 M_REGION_GET(th, struct tcphdr *, m, off, thlen); 1329 M_REGION_GET(th, struct tcphdr *, m, off, thlen);
1330 if (th == NULL) { 1330 if (th == NULL) {
1331 TCP_STATINC(TCP_STAT_RCVSHORT); 1331 TCP_STATINC(TCP_STAT_RCVSHORT);
1332 return; 1332 return;
1333 } 1333 }
1334 KASSERT(TCP_HDR_ALIGNED_P(th)); 1334 KASSERT(TCP_HDR_ALIGNED_P(th));
1335 optlen = thlen - sizeof(struct tcphdr); 1335 optlen = thlen - sizeof(struct tcphdr);
1336 optp = ((u_int8_t *)th) + sizeof(struct tcphdr); 1336 optp = ((u_int8_t *)th) + sizeof(struct tcphdr);
1337 1337
1338 /* 1338 /*
1339 * Do quick retrieval of timestamp options. 1339 * Do quick retrieval of timestamp options.
1340 * 1340 *
1341 * If timestamp is the only option and it's formatted as 1341 * If timestamp is the only option and it's formatted as
1342 * recommended in RFC 1323 appendix A, we quickly get the 1342 * recommended in RFC 1323 appendix A, we quickly get the
1343 * values now and don't bother calling tcp_dooptions(), 1343 * values now and don't bother calling tcp_dooptions(),
1344 * etc. 1344 * etc.
1345 */ 1345 */
1346 if ((optlen == TCPOLEN_TSTAMP_APPA || 1346 if ((optlen == TCPOLEN_TSTAMP_APPA ||
1347 (optlen > TCPOLEN_TSTAMP_APPA && 1347 (optlen > TCPOLEN_TSTAMP_APPA &&
1348 optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) && 1348 optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
1349 be32dec(optp) == TCPOPT_TSTAMP_HDR && 1349 be32dec(optp) == TCPOPT_TSTAMP_HDR &&
1350 (th->th_flags & TH_SYN) == 0) { 1350 (th->th_flags & TH_SYN) == 0) {
1351 opti.ts_present = 1; 1351 opti.ts_present = 1;
1352 opti.ts_val = be32dec(optp + 4); 1352 opti.ts_val = be32dec(optp + 4);
1353 opti.ts_ecr = be32dec(optp + 8); 1353 opti.ts_ecr = be32dec(optp + 8);
1354 optp = NULL; /* we've parsed the options */ 1354 optp = NULL; /* we've parsed the options */
1355 } 1355 }
1356 } 1356 }
1357 tiflags = th->th_flags; 1357 tiflags = th->th_flags;
1358 1358
1359 /* 1359 /*
1360 * Checksum extended TCP header and data 1360 * Checksum extended TCP header and data
1361 */ 1361 */
1362 if (tcp_input_checksum(af, m, th, off, thlen, tlen)) 1362 if (tcp_input_checksum(af, m, th, off, thlen, tlen))
1363 goto badcsum; 1363 goto badcsum;
1364 1364
1365 /* 1365 /*
1366 * Locate pcb for segment. 1366 * Locate pcb for segment.
1367 */ 1367 */
1368findpcb: 1368findpcb:
1369 inp = NULL; 1369 inp = NULL;
1370#ifdef INET6 1370#ifdef INET6
1371 in6p = NULL; 1371 in6p = NULL;
1372#endif 1372#endif
1373 switch (af) { 1373 switch (af) {
1374 case AF_INET: 1374 case AF_INET:
1375 inp = in_pcblookup_connect(&tcbtable, ip->ip_src, th->th_sport, 1375 inp = in_pcblookup_connect(&tcbtable, ip->ip_src, th->th_sport,
1376 ip->ip_dst, th->th_dport, &vestige); 1376 ip->ip_dst, th->th_dport, &vestige);
1377 if (inp == NULL && !vestige.valid) { 1377 if (inp == NULL && !vestige.valid) {
1378 TCP_STATINC(TCP_STAT_PCBHASHMISS); 1378 TCP_STATINC(TCP_STAT_PCBHASHMISS);
1379 inp = in_pcblookup_bind(&tcbtable, ip->ip_dst, 1379 inp = in_pcblookup_bind(&tcbtable, ip->ip_dst,
1380 th->th_dport); 1380 th->th_dport);
1381 } 1381 }
1382#ifdef INET6 1382#ifdef INET6
1383 if (inp == NULL && !vestige.valid) { 1383 if (inp == NULL && !vestige.valid) {
1384 struct in6_addr s, d; 1384 struct in6_addr s, d;
1385 1385
1386 /* mapped addr case */ 1386 /* mapped addr case */
1387 in6_in_2_v4mapin6(&ip->ip_src, &s); 1387 in6_in_2_v4mapin6(&ip->ip_src, &s);
1388 in6_in_2_v4mapin6(&ip->ip_dst, &d); 1388 in6_in_2_v4mapin6(&ip->ip_dst, &d);
1389 in6p = in6_pcblookup_connect(&tcbtable, &s, 1389 in6p = in6_pcblookup_connect(&tcbtable, &s,
1390 th->th_sport, &d, th->th_dport, 0, &vestige); 1390 th->th_sport, &d, th->th_dport, 0, &vestige);
1391 if (in6p == 0 && !vestige.valid) { 1391 if (in6p == 0 && !vestige.valid) {
1392 TCP_STATINC(TCP_STAT_PCBHASHMISS); 1392 TCP_STATINC(TCP_STAT_PCBHASHMISS);
1393 in6p = in6_pcblookup_bind(&tcbtable, &d, 1393 in6p = in6_pcblookup_bind(&tcbtable, &d,
1394 th->th_dport, 0); 1394 th->th_dport, 0);
1395 } 1395 }
1396 } 1396 }
1397#endif 1397#endif
1398#ifndef INET6 1398#ifndef INET6
1399 if (inp == NULL && !vestige.valid) 1399 if (inp == NULL && !vestige.valid)
1400#else 1400#else
1401 if (inp == NULL && in6p == NULL && !vestige.valid) 1401 if (inp == NULL && in6p == NULL && !vestige.valid)
1402#endif 1402#endif
1403 { 1403 {
1404 TCP_STATINC(TCP_STAT_NOPORT); 1404 TCP_STATINC(TCP_STAT_NOPORT);
1405 if (tcp_log_refused && 1405 if (tcp_log_refused &&
1406 (tiflags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN) { 1406 (tiflags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN) {
1407 tcp4_log_refused(ip, th); 1407 tcp4_log_refused(ip, th);
1408 } 1408 }
1409 tcp_fields_to_host(th); 1409 tcp_fields_to_host(th);
1410 goto dropwithreset_ratelim; 1410 goto dropwithreset_ratelim;
1411 } 1411 }
1412#if defined(IPSEC) 1412#if defined(IPSEC)
1413 if (ipsec_used) { 1413 if (ipsec_used) {
1414 if (inp && ipsec_in_reject(m, inp)) { 1414 if (inp && ipsec_in_reject(m, inp)) {
1415 goto drop; 1415 goto drop;
1416 } 1416 }
1417#ifdef INET6 1417#ifdef INET6
1418 else if (in6p && ipsec_in_reject(m, in6p)) { 1418 else if (in6p && ipsec_in_reject(m, in6p)) {
1419 goto drop; 1419 goto drop;
1420 } 1420 }
1421#endif 1421#endif
1422 } 1422 }
1423#endif /*IPSEC*/ 1423#endif /*IPSEC*/
1424 break; 1424 break;
1425#ifdef INET6 1425#ifdef INET6
1426 case AF_INET6: 1426 case AF_INET6:
1427 { 1427 {
1428 int faith; 1428 int faith;
1429 1429
1430#if defined(NFAITH) && NFAITH > 0 1430#if defined(NFAITH) && NFAITH > 0
1431 faith = faithprefix(&ip6->ip6_dst); 1431 faith = faithprefix(&ip6->ip6_dst);
1432#else 1432#else
1433 faith = 0; 1433 faith = 0;
1434#endif 1434#endif
1435 in6p = in6_pcblookup_connect(&tcbtable, &ip6->ip6_src, 1435 in6p = in6_pcblookup_connect(&tcbtable, &ip6->ip6_src,
1436 th->th_sport, &ip6->ip6_dst, th->th_dport, faith, &vestige); 1436 th->th_sport, &ip6->ip6_dst, th->th_dport, faith, &vestige);
1437 if (!in6p && !vestige.valid) { 1437 if (!in6p && !vestige.valid) {
1438 TCP_STATINC(TCP_STAT_PCBHASHMISS); 1438 TCP_STATINC(TCP_STAT_PCBHASHMISS);
1439 in6p = in6_pcblookup_bind(&tcbtable, &ip6->ip6_dst, 1439 in6p = in6_pcblookup_bind(&tcbtable, &ip6->ip6_dst,
1440 th->th_dport, faith); 1440 th->th_dport, faith);
1441 } 1441 }
1442 if (!in6p && !vestige.valid) { 1442 if (!in6p && !vestige.valid) {
1443 TCP_STATINC(TCP_STAT_NOPORT); 1443 TCP_STATINC(TCP_STAT_NOPORT);
1444 if (tcp_log_refused && 1444 if (tcp_log_refused &&
1445 (tiflags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN) { 1445 (tiflags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN) {
1446 tcp6_log_refused(ip6, th); 1446 tcp6_log_refused(ip6, th);
1447 } 1447 }
1448 tcp_fields_to_host(th); 1448 tcp_fields_to_host(th);
1449 goto dropwithreset_ratelim; 1449 goto dropwithreset_ratelim;
1450 } 1450 }
1451#if defined(IPSEC) 1451#if defined(IPSEC)
1452 if (ipsec_used && in6p && ipsec_in_reject(m, in6p)) { 1452 if (ipsec_used && in6p && ipsec_in_reject(m, in6p)) {
1453 goto drop; 1453 goto drop;
1454 } 1454 }
1455#endif 1455#endif
1456 break; 1456 break;
1457 } 1457 }
1458#endif 1458#endif
1459 } 1459 }
1460 1460
1461 tcp_fields_to_host(th); 1461 tcp_fields_to_host(th);
1462 1462
1463 /* 1463 /*
1464 * If the state is CLOSED (i.e., TCB does not exist) then 1464 * If the state is CLOSED (i.e., TCB does not exist) then
1465 * all data in the incoming segment is discarded. 1465 * all data in the incoming segment is discarded.
1466 * If the TCB exists but is in CLOSED state, it is embryonic, 1466 * If the TCB exists but is in CLOSED state, it is embryonic,
1467 * but should either do a listen or a connect soon. 1467 * but should either do a listen or a connect soon.
1468 */ 1468 */
1469 tp = NULL; 1469 tp = NULL;
1470 so = NULL; 1470 so = NULL;
1471 if (inp) { 1471 if (inp) {
1472 /* Check the minimum TTL for socket. */ 1472 /* Check the minimum TTL for socket. */
1473 if (ip->ip_ttl < inp->inp_ip_minttl) 1473 if (ip->ip_ttl < inp->inp_ip_minttl)
1474 goto drop; 1474 goto drop;
1475 1475
1476 tp = intotcpcb(inp); 1476 tp = intotcpcb(inp);
1477 so = inp->inp_socket; 1477 so = inp->inp_socket;
1478 } 1478 }
1479#ifdef INET6 1479#ifdef INET6
1480 else if (in6p) { 1480 else if (in6p) {
1481 tp = in6totcpcb(in6p); 1481 tp = in6totcpcb(in6p);
1482 so = in6p->in6p_socket; 1482 so = in6p->in6p_socket;
1483 } 1483 }
1484#endif 1484#endif
1485 else if (vestige.valid) { 1485 else if (vestige.valid) {
1486 /* We do not support the resurrection of vtw tcpcps. */ 1486 /* We do not support the resurrection of vtw tcpcps. */
1487 tcp_vtw_input(th, &vestige, m, tlen); 1487 tcp_vtw_input(th, &vestige, m, tlen);
1488 m = NULL; 1488 m = NULL;
1489 goto drop; 1489 goto drop;
1490 } 1490 }
1491 1491
1492 if (tp == NULL) 1492 if (tp == NULL)
1493 goto dropwithreset_ratelim; 1493 goto dropwithreset_ratelim;
1494 if (tp->t_state == TCPS_CLOSED) 1494 if (tp->t_state == TCPS_CLOSED)
1495 goto drop; 1495 goto drop;
1496 1496
1497 KASSERT(so->so_lock == softnet_lock); 1497 KASSERT(so->so_lock == softnet_lock);
1498 KASSERT(solocked(so)); 1498 KASSERT(solocked(so));
1499 1499
1500 /* Unscale the window into a 32-bit value. */ 1500 /* Unscale the window into a 32-bit value. */
1501 if ((tiflags & TH_SYN) == 0) 1501 if ((tiflags & TH_SYN) == 0)
1502 tiwin = th->th_win << tp->snd_scale; 1502 tiwin = th->th_win << tp->snd_scale;
1503 else 1503 else
1504 tiwin = th->th_win; 1504 tiwin = th->th_win;
1505 1505
1506#ifdef INET6 1506#ifdef INET6
1507 /* save packet options if user wanted */ 1507 /* save packet options if user wanted */
1508 if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS)) { 1508 if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS)) {
1509 if (in6p->in6p_options) { 1509 if (in6p->in6p_options) {
1510 m_freem(in6p->in6p_options); 1510 m_freem(in6p->in6p_options);
1511 in6p->in6p_options = NULL; 1511 in6p->in6p_options = NULL;
1512 } 1512 }
1513 ip6_savecontrol(in6p, &in6p->in6p_options, ip6, m); 1513 ip6_savecontrol(in6p, &in6p->in6p_options, ip6, m);
1514 } 1514 }
1515#endif 1515#endif
1516 1516
1517 if (so->so_options & SO_DEBUG) { 1517 if (so->so_options & SO_DEBUG) {
1518#ifdef TCP_DEBUG 1518#ifdef TCP_DEBUG
1519 ostate = tp->t_state; 1519 ostate = tp->t_state;
1520#endif 1520#endif
1521 1521
1522 tcp_saveti = NULL; 1522 tcp_saveti = NULL;
1523 if (iphlen + sizeof(struct tcphdr) > MHLEN) 1523 if (iphlen + sizeof(struct tcphdr) > MHLEN)
1524 goto nosave; 1524 goto nosave;
1525 1525
1526 if (m->m_len > iphlen && (m->m_flags & M_EXT) == 0) { 1526 if (m->m_len > iphlen && (m->m_flags & M_EXT) == 0) {
1527 tcp_saveti = m_copym(m, 0, iphlen, M_DONTWAIT); 1527 tcp_saveti = m_copym(m, 0, iphlen, M_DONTWAIT);
1528 if (tcp_saveti == NULL) 1528 if (tcp_saveti == NULL)
1529 goto nosave; 1529 goto nosave;
1530 } else { 1530 } else {
1531 MGETHDR(tcp_saveti, M_DONTWAIT, MT_HEADER); 1531 MGETHDR(tcp_saveti, M_DONTWAIT, MT_HEADER);
1532 if (tcp_saveti == NULL) 1532 if (tcp_saveti == NULL)
1533 goto nosave; 1533 goto nosave;
1534 MCLAIM(m, &tcp_mowner); 1534 MCLAIM(m, &tcp_mowner);
1535 tcp_saveti->m_len = iphlen; 1535 tcp_saveti->m_len = iphlen;
1536 m_copydata(m, 0, iphlen, 1536 m_copydata(m, 0, iphlen,
1537 mtod(tcp_saveti, void *)); 1537 mtod(tcp_saveti, void *));
1538 } 1538 }
1539 1539
1540 if (M_TRAILINGSPACE(tcp_saveti) < sizeof(struct tcphdr)) { 1540 if (M_TRAILINGSPACE(tcp_saveti) < sizeof(struct tcphdr)) {
1541 m_freem(tcp_saveti); 1541 m_freem(tcp_saveti);
1542 tcp_saveti = NULL; 1542 tcp_saveti = NULL;
1543 } else { 1543 } else {
1544 tcp_saveti->m_len += sizeof(struct tcphdr); 1544 tcp_saveti->m_len += sizeof(struct tcphdr);
1545 memcpy(mtod(tcp_saveti, char *) + iphlen, th, 1545 memcpy(mtod(tcp_saveti, char *) + iphlen, th,
1546 sizeof(struct tcphdr)); 1546 sizeof(struct tcphdr));
1547 } 1547 }
1548nosave:; 1548nosave:;
1549 } 1549 }
1550 1550
1551 if (so->so_options & SO_ACCEPTCONN) { 1551 if (so->so_options & SO_ACCEPTCONN) {
1552 union syn_cache_sa src; 1552 union syn_cache_sa src;
1553 union syn_cache_sa dst; 1553 union syn_cache_sa dst;
1554 1554
1555 KASSERT(tp->t_state == TCPS_LISTEN); 1555 KASSERT(tp->t_state == TCPS_LISTEN);
1556 1556
1557 memset(&src, 0, sizeof(src)); 1557 memset(&src, 0, sizeof(src));
1558 memset(&dst, 0, sizeof(dst)); 1558 memset(&dst, 0, sizeof(dst));
1559 switch (af) { 1559 switch (af) {
1560 case AF_INET: 1560 case AF_INET:
1561 src.sin.sin_len = sizeof(struct sockaddr_in); 1561 src.sin.sin_len = sizeof(struct sockaddr_in);
1562 src.sin.sin_family = AF_INET; 1562 src.sin.sin_family = AF_INET;
1563 src.sin.sin_addr = ip->ip_src; 1563 src.sin.sin_addr = ip->ip_src;
1564 src.sin.sin_port = th->th_sport; 1564 src.sin.sin_port = th->th_sport;
1565 1565
1566 dst.sin.sin_len = sizeof(struct sockaddr_in); 1566 dst.sin.sin_len = sizeof(struct sockaddr_in);
1567 dst.sin.sin_family = AF_INET; 1567 dst.sin.sin_family = AF_INET;
1568 dst.sin.sin_addr = ip->ip_dst; 1568 dst.sin.sin_addr = ip->ip_dst;
1569 dst.sin.sin_port = th->th_dport; 1569 dst.sin.sin_port = th->th_dport;
1570 break; 1570 break;
1571#ifdef INET6 1571#ifdef INET6
1572 case AF_INET6: 1572 case AF_INET6:
1573 src.sin6.sin6_len = sizeof(struct sockaddr_in6); 1573 src.sin6.sin6_len = sizeof(struct sockaddr_in6);
1574 src.sin6.sin6_family = AF_INET6; 1574 src.sin6.sin6_family = AF_INET6;
1575 src.sin6.sin6_addr = ip6->ip6_src; 1575 src.sin6.sin6_addr = ip6->ip6_src;
1576 src.sin6.sin6_port = th->th_sport; 1576 src.sin6.sin6_port = th->th_sport;
1577 1577
1578 dst.sin6.sin6_len = sizeof(struct sockaddr_in6); 1578 dst.sin6.sin6_len = sizeof(struct sockaddr_in6);
1579 dst.sin6.sin6_family = AF_INET6; 1579 dst.sin6.sin6_family = AF_INET6;
1580 dst.sin6.sin6_addr = ip6->ip6_dst; 1580 dst.sin6.sin6_addr = ip6->ip6_dst;
1581 dst.sin6.sin6_port = th->th_dport; 1581 dst.sin6.sin6_port = th->th_dport;
1582 break; 1582 break;
1583#endif 1583#endif
1584 } 1584 }
1585 1585
1586 if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 1586 if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
1587 if (tiflags & TH_RST) { 1587 if (tiflags & TH_RST) {
1588 syn_cache_reset(&src.sa, &dst.sa, th); 1588 syn_cache_reset(&src.sa, &dst.sa, th);
1589 } else if ((tiflags & (TH_ACK|TH_SYN)) == 1589 } else if ((tiflags & (TH_ACK|TH_SYN)) ==
1590 (TH_ACK|TH_SYN)) { 1590 (TH_ACK|TH_SYN)) {
1591 /* 1591 /*
1592 * Received a SYN,ACK. This should never 1592 * Received a SYN,ACK. This should never
1593 * happen while we are in LISTEN. Send an RST. 1593 * happen while we are in LISTEN. Send an RST.
1594 */ 1594 */
1595 goto badsyn; 1595 goto badsyn;
1596 } else if (tiflags & TH_ACK) { 1596 } else if (tiflags & TH_ACK) {
1597 so = syn_cache_get(&src.sa, &dst.sa, th, so, m); 1597 so = syn_cache_get(&src.sa, &dst.sa, th, so, m);
1598 if (so == NULL) { 1598 if (so == NULL) {
1599 /* 1599 /*
1600 * We don't have a SYN for this ACK; 1600 * We don't have a SYN for this ACK;
1601 * send an RST. 1601 * send an RST.
1602 */ 1602 */
1603 goto badsyn; 1603 goto badsyn;
1604 } else if (so == (struct socket *)(-1)) { 1604 } else if (so == (struct socket *)(-1)) {
1605 /* 1605 /*
1606 * We were unable to create the 1606 * We were unable to create the
1607 * connection. If the 3-way handshake 1607 * connection. If the 3-way handshake
1608 * was completed, and RST has been 1608 * was completed, and RST has been
1609 * sent to the peer. Since the mbuf 1609 * sent to the peer. Since the mbuf
1610 * might be in use for the reply, do 1610 * might be in use for the reply, do
1611 * not free it. 1611 * not free it.
1612 */ 1612 */
1613 m = NULL; 1613 m = NULL;
1614 } else { 1614 } else {
1615 /* 1615 /*
1616 * We have created a full-blown 1616 * We have created a full-blown
1617 * connection. 1617 * connection.
1618 */ 1618 */
1619 tp = NULL; 1619 tp = NULL;
1620 inp = NULL; 1620 inp = NULL;
1621#ifdef INET6 1621#ifdef INET6
1622 in6p = NULL; 1622 in6p = NULL;
1623#endif 1623#endif
1624 switch (so->so_proto->pr_domain->dom_family) { 1624 switch (so->so_proto->pr_domain->dom_family) {
1625 case AF_INET: 1625 case AF_INET:
1626 inp = sotoinpcb(so); 1626 inp = sotoinpcb(so);
1627 tp = intotcpcb(inp); 1627 tp = intotcpcb(inp);
1628 break; 1628 break;
1629#ifdef INET6 1629#ifdef INET6
1630 case AF_INET6: 1630 case AF_INET6:
1631 in6p = sotoin6pcb(so); 1631 in6p = sotoin6pcb(so);
1632 tp = in6totcpcb(in6p); 1632 tp = in6totcpcb(in6p);
1633 break; 1633 break;
1634#endif 1634#endif
1635 } 1635 }
1636 if (tp == NULL) 1636 if (tp == NULL)
1637 goto badsyn; /*XXX*/ 1637 goto badsyn; /*XXX*/
1638 tiwin <<= tp->snd_scale; 1638 tiwin <<= tp->snd_scale;
1639 goto after_listen; 1639 goto after_listen;
1640 } 1640 }
1641 } else { 1641 } else {
1642 /* 1642 /*
1643 * None of RST, SYN or ACK was set. 1643 * None of RST, SYN or ACK was set.
1644 * This is an invalid packet for a 1644 * This is an invalid packet for a
1645 * TCB in LISTEN state. Send a RST. 1645 * TCB in LISTEN state. Send a RST.
1646 */ 1646 */
1647 goto badsyn; 1647 goto badsyn;
1648 } 1648 }
1649 } else { 1649 } else {
1650 /* 1650 /*
1651 * Received a SYN. 1651 * Received a SYN.
1652 */ 1652 */
1653 1653
1654#ifdef INET6 1654#ifdef INET6
1655 /* 1655 /*
1656 * If deprecated address is forbidden, we do 1656 * If deprecated address is forbidden, we do
1657 * not accept SYN to deprecated interface 1657 * not accept SYN to deprecated interface
1658 * address to prevent any new inbound 1658 * address to prevent any new inbound
1659 * connection from getting established. 1659 * connection from getting established.
1660 * When we do not accept SYN, we send a TCP 1660 * When we do not accept SYN, we send a TCP
1661 * RST, with deprecated source address (instead 1661 * RST, with deprecated source address (instead
1662 * of dropping it). We compromise it as it is 1662 * of dropping it). We compromise it as it is
1663 * much better for peer to send a RST, and 1663 * much better for peer to send a RST, and
1664 * RST will be the final packet for the 1664 * RST will be the final packet for the
1665 * exchange. 1665 * exchange.
1666 * 1666 *
1667 * If we do not forbid deprecated addresses, we 1667 * If we do not forbid deprecated addresses, we
1668 * accept the SYN packet. RFC2462 does not 1668 * accept the SYN packet. RFC2462 does not
1669 * suggest dropping SYN in this case. 1669 * suggest dropping SYN in this case.
1670 * If we decipher RFC2462 5.5.4, it says like 1670 * If we decipher RFC2462 5.5.4, it says like
1671 * this: 1671 * this:
1672 * 1. use of deprecated addr with existing 1672 * 1. use of deprecated addr with existing
1673 * communication is okay - "SHOULD continue 1673 * communication is okay - "SHOULD continue
1674 * to be used" 1674 * to be used"
1675 * 2. use of it with new communication: 1675 * 2. use of it with new communication:
1676 * (2a) "SHOULD NOT be used if alternate 1676 * (2a) "SHOULD NOT be used if alternate
1677 * address with sufficient scope is 1677 * address with sufficient scope is
1678 * available" 1678 * available"
1679 * (2b) nothing mentioned otherwise. 1679 * (2b) nothing mentioned otherwise.
1680 * Here we fall into (2b) case as we have no 1680 * Here we fall into (2b) case as we have no
1681 * choice in our source address selection - we 1681 * choice in our source address selection - we
1682 * must obey the peer. 1682 * must obey the peer.
1683 * 1683 *
1684 * The wording in RFC2462 is confusing, and 1684 * The wording in RFC2462 is confusing, and
1685 * there are multiple description text for 1685 * there are multiple description text for
1686 * deprecated address handling - worse, they 1686 * deprecated address handling - worse, they
1687 * are not exactly the same. I believe 5.5.4 1687 * are not exactly the same. I believe 5.5.4
1688 * is the best one, so we follow 5.5.4. 1688 * is the best one, so we follow 5.5.4.
1689 */ 1689 */
1690 if (af == AF_INET6 && !ip6_use_deprecated) { 1690 if (af == AF_INET6 && !ip6_use_deprecated) {
1691 struct in6_ifaddr *ia6; 1691 struct in6_ifaddr *ia6;
1692 int s; 1692 int s;
1693 struct ifnet *rcvif = m_get_rcvif(m, &s); 1693 struct ifnet *rcvif = m_get_rcvif(m, &s);
1694 if (rcvif == NULL) 1694 if (rcvif == NULL)
1695 goto dropwithreset; /* XXX */ 1695 goto dropwithreset; /* XXX */
1696 if ((ia6 = in6ifa_ifpwithaddr(rcvif, 1696 if ((ia6 = in6ifa_ifpwithaddr(rcvif,
1697 &ip6->ip6_dst)) && 1697 &ip6->ip6_dst)) &&
1698 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 1698 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
1699 tp = NULL; 1699 tp = NULL;
1700 m_put_rcvif(rcvif, &s); 1700 m_put_rcvif(rcvif, &s);
1701 goto dropwithreset; 1701 goto dropwithreset;
1702 } 1702 }
1703 m_put_rcvif(rcvif, &s); 1703 m_put_rcvif(rcvif, &s);
1704 } 1704 }
1705#endif 1705#endif
1706 1706
1707 /* 1707 /*
1708 * LISTEN socket received a SYN from itself? This 1708 * LISTEN socket received a SYN from itself? This
1709 * can't possibly be valid; drop the packet. 1709 * can't possibly be valid; drop the packet.
1710 */ 1710 */
1711 if (th->th_sport == th->th_dport) { 1711 if (th->th_sport == th->th_dport) {
1712 int eq = 0; 1712 int eq = 0;
1713 1713
1714 switch (af) { 1714 switch (af) {
1715 case AF_INET: 1715 case AF_INET:
1716 eq = in_hosteq(ip->ip_src, ip->ip_dst); 1716 eq = in_hosteq(ip->ip_src, ip->ip_dst);
1717 break; 1717 break;
1718#ifdef INET6 1718#ifdef INET6
1719 case AF_INET6: 1719 case AF_INET6:
1720 eq = IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, 1720 eq = IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
1721 &ip6->ip6_dst); 1721 &ip6->ip6_dst);
1722 break; 1722 break;
1723#endif 1723#endif
1724 } 1724 }
1725 if (eq) { 1725 if (eq) {
1726 TCP_STATINC(TCP_STAT_BADSYN); 1726 TCP_STATINC(TCP_STAT_BADSYN);
1727 goto drop; 1727 goto drop;
1728 } 1728 }
1729 } 1729 }
1730 1730
1731 /* 1731 /*
1732 * SYN looks ok; create compressed TCP 1732 * SYN looks ok; create compressed TCP
1733 * state for it. 1733 * state for it.
1734 */ 1734 */
1735 if (so->so_qlen <= so->so_qlimit && 1735 if (so->so_qlen <= so->so_qlimit &&
1736 syn_cache_add(&src.sa, &dst.sa, th, off, 1736 syn_cache_add(&src.sa, &dst.sa, th, off,
1737 so, m, optp, optlen, &opti)) 1737 so, m, optp, optlen, &opti))
1738 m = NULL; 1738 m = NULL;
1739 } 1739 }
1740 1740
1741 goto drop; 1741 goto drop;
1742 } 1742 }
1743 1743
1744after_listen: 1744after_listen:
1745 /* 1745 /*
1746 * From here on, we're dealing with !LISTEN. 1746 * From here on, we're dealing with !LISTEN.
1747 */ 1747 */
1748 KASSERT(tp->t_state != TCPS_LISTEN); 1748 KASSERT(tp->t_state != TCPS_LISTEN);
1749 1749
1750 /* 1750 /*
1751 * Segment received on connection. 1751 * Segment received on connection.
1752 * Reset idle time and keep-alive timer. 1752 * Reset idle time and keep-alive timer.
1753 */ 1753 */
1754 tp->t_rcvtime = tcp_now; 1754 tp->t_rcvtime = tcp_now;
1755 if (TCPS_HAVEESTABLISHED(tp->t_state)) 1755 if (TCPS_HAVEESTABLISHED(tp->t_state))
1756 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepidle); 1756 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepidle);
1757 1757
1758 /* 1758 /*
1759 * Process options. 1759 * Process options.
1760 */ 1760 */
1761#ifdef TCP_SIGNATURE 1761#ifdef TCP_SIGNATURE
1762 if (optp || (tp->t_flags & TF_SIGNATURE)) 1762 if (optp || (tp->t_flags & TF_SIGNATURE))
1763#else 1763#else
1764 if (optp) 1764 if (optp)
1765#endif 1765#endif
1766 if (tcp_dooptions(tp, optp, optlen, th, m, off, &opti) < 0) 1766 if (tcp_dooptions(tp, optp, optlen, th, m, off, &opti) < 0)
1767 goto drop; 1767 goto drop;
1768 1768
1769 if (TCP_SACK_ENABLED(tp)) { 1769 if (TCP_SACK_ENABLED(tp)) {
1770 tcp_del_sackholes(tp, th); 1770 tcp_del_sackholes(tp, th);
1771 } 1771 }
1772 1772
1773 if (TCP_ECN_ALLOWED(tp)) { 1773 if (TCP_ECN_ALLOWED(tp)) {
1774 if (tiflags & TH_CWR) { 1774 if (tiflags & TH_CWR) {
1775 tp->t_flags &= ~TF_ECN_SND_ECE; 1775 tp->t_flags &= ~TF_ECN_SND_ECE;
1776 } 1776 }
1777 switch (iptos & IPTOS_ECN_MASK) { 1777 switch (iptos & IPTOS_ECN_MASK) {
1778 case IPTOS_ECN_CE: 1778 case IPTOS_ECN_CE:
1779 tp->t_flags |= TF_ECN_SND_ECE; 1779 tp->t_flags |= TF_ECN_SND_ECE;
1780 TCP_STATINC(TCP_STAT_ECN_CE); 1780 TCP_STATINC(TCP_STAT_ECN_CE);
1781 break; 1781 break;
1782 case IPTOS_ECN_ECT0: 1782 case IPTOS_ECN_ECT0:
1783 TCP_STATINC(TCP_STAT_ECN_ECT); 1783 TCP_STATINC(TCP_STAT_ECN_ECT);
1784 break; 1784 break;
1785 case IPTOS_ECN_ECT1: 1785 case IPTOS_ECN_ECT1:
1786 /* XXX: ignore for now -- rpaulo */ 1786 /* XXX: ignore for now -- rpaulo */
1787 break; 1787 break;
1788 } 1788 }
1789 /* 1789 /*
1790 * Congestion experienced. 1790 * Congestion experienced.
1791 * Ignore if we are already trying to recover. 1791 * Ignore if we are already trying to recover.
1792 */ 1792 */
1793 if ((tiflags & TH_ECE) && SEQ_GEQ(tp->snd_una, tp->snd_recover)) 1793 if ((tiflags & TH_ECE) && SEQ_GEQ(tp->snd_una, tp->snd_recover))
1794 tp->t_congctl->cong_exp(tp); 1794 tp->t_congctl->cong_exp(tp);
1795 } 1795 }
1796 1796
1797 if (opti.ts_present && opti.ts_ecr) { 1797 if (opti.ts_present && opti.ts_ecr) {
1798 /* 1798 /*
1799 * Calculate the RTT from the returned time stamp and the 1799 * Calculate the RTT from the returned time stamp and the
1800 * connection's time base. If the time stamp is later than 1800 * connection's time base. If the time stamp is later than
1801 * the current time, or is extremely old, fall back to non-1323 1801 * the current time, or is extremely old, fall back to non-1323
1802 * RTT calculation. Since ts_rtt is unsigned, we can test both 1802 * RTT calculation. Since ts_rtt is unsigned, we can test both
1803 * at the same time. 1803 * at the same time.
1804 * 1804 *
1805 * Note that ts_rtt is in units of slow ticks (500 1805 * Note that ts_rtt is in units of slow ticks (500
1806 * ms). Since most earthbound RTTs are < 500 ms, 1806 * ms). Since most earthbound RTTs are < 500 ms,
1807 * observed values will have large quantization noise. 1807 * observed values will have large quantization noise.
1808 * Our smoothed RTT is then the fraction of observed 1808 * Our smoothed RTT is then the fraction of observed
1809 * samples that are 1 tick instead of 0 (times 500 1809 * samples that are 1 tick instead of 0 (times 500
1810 * ms). 1810 * ms).
1811 * 1811 *
1812 * ts_rtt is increased by 1 to denote a valid sample, 1812 * ts_rtt is increased by 1 to denote a valid sample,
1813 * with 0 indicating an invalid measurement. This 1813 * with 0 indicating an invalid measurement. This
1814 * extra 1 must be removed when ts_rtt is used, or 1814 * extra 1 must be removed when ts_rtt is used, or
1815 * else an erroneous extra 500 ms will result. 1815 * else an erroneous extra 500 ms will result.
1816 */ 1816 */
1817 ts_rtt = TCP_TIMESTAMP(tp) - opti.ts_ecr + 1; 1817 ts_rtt = TCP_TIMESTAMP(tp) - opti.ts_ecr + 1;
1818 if (ts_rtt > TCP_PAWS_IDLE) 1818 if (ts_rtt > TCP_PAWS_IDLE)
1819 ts_rtt = 0; 1819 ts_rtt = 0;
1820 } else { 1820 } else {
1821 ts_rtt = 0; 1821 ts_rtt = 0;
1822 } 1822 }
1823 1823
1824 /* 1824 /*
1825 * Fast path: check for the two common cases of a uni-directional 1825 * Fast path: check for the two common cases of a uni-directional
1826 * data transfer. If: 1826 * data transfer. If:
1827 * o We are in the ESTABLISHED state, and 1827 * o We are in the ESTABLISHED state, and
1828 * o The packet has no control flags, and 1828 * o The packet has no control flags, and
1829 * o The packet is in-sequence, and 1829 * o The packet is in-sequence, and
1830 * o The window didn't change, and 1830 * o The window didn't change, and
1831 * o We are not retransmitting 1831 * o We are not retransmitting
1832 * It's a candidate. 1832 * It's a candidate.
1833 * 1833 *
1834 * If the length (tlen) is zero and the ack moved forward, we're 1834 * If the length (tlen) is zero and the ack moved forward, we're
1835 * the sender side of the transfer. Just free the data acked and 1835 * the sender side of the transfer. Just free the data acked and
1836 * wake any higher level process that was blocked waiting for 1836 * wake any higher level process that was blocked waiting for
1837 * space. 1837 * space.
1838 * 1838 *
1839 * If the length is non-zero and the ack didn't move, we're the 1839 * If the length is non-zero and the ack didn't move, we're the
1840 * receiver side. If we're getting packets in-order (the reassembly 1840 * receiver side. If we're getting packets in-order (the reassembly
1841 * queue is empty), add the data to the socket buffer and note 1841 * queue is empty), add the data to the socket buffer and note
1842 * that we need a delayed ack. 1842 * that we need a delayed ack.
1843 */ 1843 */
1844 if (tp->t_state == TCPS_ESTABLISHED && 1844 if (tp->t_state == TCPS_ESTABLISHED &&
1845 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ECE|TH_CWR|TH_ACK)) 1845 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ECE|TH_CWR|TH_ACK))
1846 == TH_ACK && 1846 == TH_ACK &&
1847 (!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) && 1847 (!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) &&
1848 th->th_seq == tp->rcv_nxt && 1848 th->th_seq == tp->rcv_nxt &&
1849 tiwin && tiwin == tp->snd_wnd && 1849 tiwin && tiwin == tp->snd_wnd &&
1850 tp->snd_nxt == tp->snd_max) { 1850 tp->snd_nxt == tp->snd_max) {
1851 1851
1852 /* 1852 /*
1853 * If last ACK falls within this segment's sequence numbers, 1853 * If last ACK falls within this segment's sequence numbers,
1854 * record the timestamp. 1854 * record the timestamp.
1855 * NOTE that the test is modified according to the latest 1855 * NOTE that the test is modified according to the latest
1856 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1856 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1857 * 1857 *
1858 * note that we already know 1858 * note that we already know
1859 * TSTMP_GEQ(opti.ts_val, tp->ts_recent) 1859 * TSTMP_GEQ(opti.ts_val, tp->ts_recent)
1860 */ 1860 */
1861 if (opti.ts_present && SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1861 if (opti.ts_present && SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1862 tp->ts_recent_age = tcp_now; 1862 tp->ts_recent_age = tcp_now;
1863 tp->ts_recent = opti.ts_val; 1863 tp->ts_recent = opti.ts_val;
1864 } 1864 }
1865 1865
1866 if (tlen == 0) { 1866 if (tlen == 0) {
1867 /* Ack prediction. */ 1867 /* Ack prediction. */
1868 if (SEQ_GT(th->th_ack, tp->snd_una) && 1868 if (SEQ_GT(th->th_ack, tp->snd_una) &&
1869 SEQ_LEQ(th->th_ack, tp->snd_max) && 1869 SEQ_LEQ(th->th_ack, tp->snd_max) &&
1870 tp->snd_cwnd >= tp->snd_wnd && 1870 tp->snd_cwnd >= tp->snd_wnd &&
1871 tp->t_partialacks < 0) { 1871 tp->t_partialacks < 0) {
1872 /* 1872 /*
1873 * this is a pure ack for outstanding data. 1873 * this is a pure ack for outstanding data.
1874 */ 1874 */
1875 if (ts_rtt) 1875 if (ts_rtt)
1876 tcp_xmit_timer(tp, ts_rtt - 1); 1876 tcp_xmit_timer(tp, ts_rtt - 1);
1877 else if (tp->t_rtttime && 1877 else if (tp->t_rtttime &&
1878 SEQ_GT(th->th_ack, tp->t_rtseq)) 1878 SEQ_GT(th->th_ack, tp->t_rtseq))
1879 tcp_xmit_timer(tp, 1879 tcp_xmit_timer(tp,
1880 tcp_now - tp->t_rtttime); 1880 tcp_now - tp->t_rtttime);
1881 acked = th->th_ack - tp->snd_una; 1881 acked = th->th_ack - tp->snd_una;
1882 tcps = TCP_STAT_GETREF(); 1882 tcps = TCP_STAT_GETREF();
1883 tcps[TCP_STAT_PREDACK]++; 1883 tcps[TCP_STAT_PREDACK]++;
1884 tcps[TCP_STAT_RCVACKPACK]++; 1884 tcps[TCP_STAT_RCVACKPACK]++;
1885 tcps[TCP_STAT_RCVACKBYTE] += acked; 1885 tcps[TCP_STAT_RCVACKBYTE] += acked;
1886 TCP_STAT_PUTREF(); 1886 TCP_STAT_PUTREF();
1887 nd6_hint(tp); 1887 nd6_hint(tp);
1888 1888
1889 if (acked > (tp->t_lastoff - tp->t_inoff)) 1889 if (acked > (tp->t_lastoff - tp->t_inoff))
1890 tp->t_lastm = NULL; 1890 tp->t_lastm = NULL;
1891 sbdrop(&so->so_snd, acked); 1891 sbdrop(&so->so_snd, acked);
1892 tp->t_lastoff -= acked; 1892 tp->t_lastoff -= acked;
1893 1893
1894 icmp_check(tp, th, acked); 1894 icmp_check(tp, th, acked);
1895 1895
1896 tp->snd_una = th->th_ack; 1896 tp->snd_una = th->th_ack;
1897 tp->snd_fack = tp->snd_una; 1897 tp->snd_fack = tp->snd_una;
1898 if (SEQ_LT(tp->snd_high, tp->snd_una)) 1898 if (SEQ_LT(tp->snd_high, tp->snd_una))
1899 tp->snd_high = tp->snd_una; 1899 tp->snd_high = tp->snd_una;
 1900 /*
 1901 * drag snd_wl2 along so only newer
 1902 * ACKs can update the window size.
 1903 * also avoids the state where snd_wl2
 1904 * is eventually larger than th_ack and thus
 1905 * blocking the window update mechanism and
 1906 * the connection gets stuck for a loooong
 1907 * time in the zero sized send window state.
 1908 *
 1909 * see PR/kern 55567
 1910 */
 1911 tp->snd_wl2 = tp->snd_una;
 1912
1900 m_freem(m); 1913 m_freem(m);
1901 1914
1902 /* 1915 /*
1903 * If all outstanding data are acked, stop 1916 * If all outstanding data are acked, stop
1904 * retransmit timer, otherwise restart timer 1917 * retransmit timer, otherwise restart timer
1905 * using current (possibly backed-off) value. 1918 * using current (possibly backed-off) value.
1906 * If process is waiting for space, 1919 * If process is waiting for space,
1907 * wakeup/selnotify/signal. If data 1920 * wakeup/selnotify/signal. If data
1908 * are ready to send, let tcp_output 1921 * are ready to send, let tcp_output
1909 * decide between more output or persist. 1922 * decide between more output or persist.
1910 */ 1923 */
1911 if (tp->snd_una == tp->snd_max) 1924 if (tp->snd_una == tp->snd_max)
1912 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1925 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1913 else if (TCP_TIMER_ISARMED(tp, 1926 else if (TCP_TIMER_ISARMED(tp,
1914 TCPT_PERSIST) == 0) 1927 TCPT_PERSIST) == 0)
1915 TCP_TIMER_ARM(tp, TCPT_REXMT, 1928 TCP_TIMER_ARM(tp, TCPT_REXMT,
1916 tp->t_rxtcur); 1929 tp->t_rxtcur);
1917 1930
1918 sowwakeup(so); 1931 sowwakeup(so);
1919 if (so->so_snd.sb_cc) { 1932 if (so->so_snd.sb_cc) {
1920 KERNEL_LOCK(1, NULL); 1933 KERNEL_LOCK(1, NULL);
1921 (void)tcp_output(tp); 1934 (void)tcp_output(tp);
1922 KERNEL_UNLOCK_ONE(NULL); 1935 KERNEL_UNLOCK_ONE(NULL);
1923 } 1936 }
1924 if (tcp_saveti) 1937 if (tcp_saveti)
1925 m_freem(tcp_saveti); 1938 m_freem(tcp_saveti);
1926 return; 1939 return;
1927 } 1940 }
1928 } else if (th->th_ack == tp->snd_una && 1941 } else if (th->th_ack == tp->snd_una &&
1929 TAILQ_FIRST(&tp->segq) == NULL && 1942 TAILQ_FIRST(&tp->segq) == NULL &&
1930 tlen <= sbspace(&so->so_rcv)) { 1943 tlen <= sbspace(&so->so_rcv)) {
1931 int newsize = 0; 1944 int newsize = 0;
1932 1945
1933 /* 1946 /*
1934 * this is a pure, in-sequence data packet 1947 * this is a pure, in-sequence data packet
1935 * with nothing on the reassembly queue and 1948 * with nothing on the reassembly queue and
1936 * we have enough buffer space to take it. 1949 * we have enough buffer space to take it.
1937 */ 1950 */
1938 tp->rcv_nxt += tlen; 1951 tp->rcv_nxt += tlen;
1939 tcps = TCP_STAT_GETREF(); 1952 tcps = TCP_STAT_GETREF();
1940 tcps[TCP_STAT_PREDDAT]++; 1953 tcps[TCP_STAT_PREDDAT]++;
1941 tcps[TCP_STAT_RCVPACK]++; 1954 tcps[TCP_STAT_RCVPACK]++;
1942 tcps[TCP_STAT_RCVBYTE] += tlen; 1955 tcps[TCP_STAT_RCVBYTE] += tlen;
1943 TCP_STAT_PUTREF(); 1956 TCP_STAT_PUTREF();
1944 nd6_hint(tp); 1957 nd6_hint(tp);
1945 1958
1946 /* 1959 /*
1947 * Automatic sizing enables the performance of large buffers 1960 * Automatic sizing enables the performance of large buffers
1948 * and most of the efficiency of small ones by only allocating 1961 * and most of the efficiency of small ones by only allocating
1949 * space when it is needed. 1962 * space when it is needed.
1950 * 1963 *
1951 * On the receive side the socket buffer memory is only rarely 1964 * On the receive side the socket buffer memory is only rarely
1952 * used to any significant extent. This allows us to be much 1965 * used to any significant extent. This allows us to be much
1953 * more aggressive in scaling the receive socket buffer. For 1966 * more aggressive in scaling the receive socket buffer. For
1954 * the case that the buffer space is actually used to a large 1967 * the case that the buffer space is actually used to a large
1955 * extent and we run out of kernel memory we can simply drop 1968 * extent and we run out of kernel memory we can simply drop
1956 * the new segments; TCP on the sender will just retransmit it 1969 * the new segments; TCP on the sender will just retransmit it
1957 * later. Setting the buffer size too big may only consume too 1970 * later. Setting the buffer size too big may only consume too
1958 * much kernel memory if the application doesn't read() from 1971 * much kernel memory if the application doesn't read() from
1959 * the socket or packet loss or reordering makes use of the 1972 * the socket or packet loss or reordering makes use of the
1960 * reassembly queue. 1973 * reassembly queue.
1961 * 1974 *
1962 * The criteria to step up the receive buffer one notch are: 1975 * The criteria to step up the receive buffer one notch are:
1963 * 1. the number of bytes received during the time it takes 1976 * 1. the number of bytes received during the time it takes
1964 * one timestamp to be reflected back to us (the RTT); 1977 * one timestamp to be reflected back to us (the RTT);
1965 * 2. received bytes per RTT is within seven eighth of the 1978 * 2. received bytes per RTT is within seven eighth of the
1966 * current socket buffer size; 1979 * current socket buffer size;
1967 * 3. receive buffer size has not hit maximal automatic size; 1980 * 3. receive buffer size has not hit maximal automatic size;
1968 * 1981 *
1969 * This algorithm does one step per RTT at most and only if 1982 * This algorithm does one step per RTT at most and only if
1970 * we receive a bulk stream w/o packet losses or reorderings. 1983 * we receive a bulk stream w/o packet losses or reorderings.
1971 * Shrinking the buffer during idle times is not necessary as 1984 * Shrinking the buffer during idle times is not necessary as
1972 * it doesn't consume any memory when idle. 1985 * it doesn't consume any memory when idle.
1973 * 1986 *
1974 * TODO: Only step up if the application is actually serving 1987 * TODO: Only step up if the application is actually serving
1975 * the buffer to better manage the socket buffer resources. 1988 * the buffer to better manage the socket buffer resources.
1976 */ 1989 */
1977 if (tcp_do_autorcvbuf && 1990 if (tcp_do_autorcvbuf &&
1978 opti.ts_ecr && 1991 opti.ts_ecr &&
1979 (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 1992 (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
1980 if (opti.ts_ecr > tp->rfbuf_ts && 1993 if (opti.ts_ecr > tp->rfbuf_ts &&
1981 opti.ts_ecr - tp->rfbuf_ts < PR_SLOWHZ) { 1994 opti.ts_ecr - tp->rfbuf_ts < PR_SLOWHZ) {
1982 if (tp->rfbuf_cnt > 1995 if (tp->rfbuf_cnt >
1983 (so->so_rcv.sb_hiwat / 8 * 7) && 1996 (so->so_rcv.sb_hiwat / 8 * 7) &&
1984 so->so_rcv.sb_hiwat < 1997 so->so_rcv.sb_hiwat <
1985 tcp_autorcvbuf_max) { 1998 tcp_autorcvbuf_max) {
1986 newsize = 1999 newsize =
1987 uimin(so->so_rcv.sb_hiwat + 2000 uimin(so->so_rcv.sb_hiwat +
1988 tcp_autorcvbuf_inc, 2001 tcp_autorcvbuf_inc,
1989 tcp_autorcvbuf_max); 2002 tcp_autorcvbuf_max);
1990 } 2003 }
1991 /* Start over with next RTT. */ 2004 /* Start over with next RTT. */
1992 tp->rfbuf_ts = 0; 2005 tp->rfbuf_ts = 0;
1993 tp->rfbuf_cnt = 0; 2006 tp->rfbuf_cnt = 0;
1994 } else 2007 } else
1995 tp->rfbuf_cnt += tlen; /* add up */ 2008 tp->rfbuf_cnt += tlen; /* add up */
1996 } 2009 }
1997 2010
1998 /* 2011 /*
1999 * Drop TCP, IP headers and TCP options then add data 2012 * Drop TCP, IP headers and TCP options then add data
2000 * to socket buffer. 2013 * to socket buffer.
2001 */ 2014 */
2002 if (so->so_state & SS_CANTRCVMORE) { 2015 if (so->so_state & SS_CANTRCVMORE) {
2003 m_freem(m); 2016 m_freem(m);
2004 } else { 2017 } else {
2005 /* 2018 /*
2006 * Set new socket buffer size. 2019 * Set new socket buffer size.
2007 * Give up when limit is reached. 2020 * Give up when limit is reached.
2008 */ 2021 */
2009 if (newsize) 2022 if (newsize)
2010 if (!sbreserve(&so->so_rcv, 2023 if (!sbreserve(&so->so_rcv,
2011 newsize, so)) 2024 newsize, so))
2012 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 2025 so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
2013 m_adj(m, off + thlen); 2026 m_adj(m, off + thlen);
2014 sbappendstream(&so->so_rcv, m); 2027 sbappendstream(&so->so_rcv, m);
2015 } 2028 }
2016 sorwakeup(so); 2029 sorwakeup(so);
2017 tcp_setup_ack(tp, th); 2030 tcp_setup_ack(tp, th);
2018 if (tp->t_flags & TF_ACKNOW) { 2031 if (tp->t_flags & TF_ACKNOW) {
2019 KERNEL_LOCK(1, NULL); 2032 KERNEL_LOCK(1, NULL);
2020 (void)tcp_output(tp); 2033 (void)tcp_output(tp);
2021 KERNEL_UNLOCK_ONE(NULL); 2034 KERNEL_UNLOCK_ONE(NULL);
2022 } 2035 }
2023 if (tcp_saveti) 2036 if (tcp_saveti)
2024 m_freem(tcp_saveti); 2037 m_freem(tcp_saveti);
2025 return; 2038 return;
2026 } 2039 }
2027 } 2040 }
2028 2041
2029 /* 2042 /*
2030 * Compute mbuf offset to TCP data segment. 2043 * Compute mbuf offset to TCP data segment.
2031 */ 2044 */
2032 hdroptlen = off + thlen; 2045 hdroptlen = off + thlen;
2033 2046
2034 /* 2047 /*
2035 * Calculate amount of space in receive window. Receive window is 2048 * Calculate amount of space in receive window. Receive window is
2036 * amount of space in rcv queue, but not less than advertised 2049 * amount of space in rcv queue, but not less than advertised
2037 * window. 2050 * window.
2038 */ 2051 */
2039 { 2052 {
2040 int win; 2053 int win;
2041 win = sbspace(&so->so_rcv); 2054 win = sbspace(&so->so_rcv);
2042 if (win < 0) 2055 if (win < 0)
2043 win = 0; 2056 win = 0;
2044 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 2057 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
2045 } 2058 }
2046 2059
2047 /* Reset receive buffer auto scaling when not in bulk receive mode. */ 2060 /* Reset receive buffer auto scaling when not in bulk receive mode. */
2048 tp->rfbuf_ts = 0; 2061 tp->rfbuf_ts = 0;
2049 tp->rfbuf_cnt = 0; 2062 tp->rfbuf_cnt = 0;
2050 2063
2051 switch (tp->t_state) { 2064 switch (tp->t_state) {
2052 /* 2065 /*
2053 * If the state is SYN_SENT: 2066 * If the state is SYN_SENT:
2054 * if seg contains an ACK, but not for our SYN, drop the input. 2067 * if seg contains an ACK, but not for our SYN, drop the input.
2055 * if seg contains a RST, then drop the connection. 2068 * if seg contains a RST, then drop the connection.
2056 * if seg does not contain SYN, then drop it. 2069 * if seg does not contain SYN, then drop it.
2057 * Otherwise this is an acceptable SYN segment 2070 * Otherwise this is an acceptable SYN segment
2058 * initialize tp->rcv_nxt and tp->irs 2071 * initialize tp->rcv_nxt and tp->irs
2059 * if seg contains ack then advance tp->snd_una 2072 * if seg contains ack then advance tp->snd_una
2060 * if seg contains a ECE and ECN support is enabled, the stream 2073 * if seg contains a ECE and ECN support is enabled, the stream
2061 * is ECN capable. 2074 * is ECN capable.
2062 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2075 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2063 * arrange for segment to be acked (eventually) 2076 * arrange for segment to be acked (eventually)
2064 * continue processing rest of data/controls, beginning with URG 2077 * continue processing rest of data/controls, beginning with URG
2065 */ 2078 */
2066 case TCPS_SYN_SENT: 2079 case TCPS_SYN_SENT:
2067 if ((tiflags & TH_ACK) && 2080 if ((tiflags & TH_ACK) &&
2068 (SEQ_LEQ(th->th_ack, tp->iss) || 2081 (SEQ_LEQ(th->th_ack, tp->iss) ||
2069 SEQ_GT(th->th_ack, tp->snd_max))) 2082 SEQ_GT(th->th_ack, tp->snd_max)))
2070 goto dropwithreset; 2083 goto dropwithreset;
2071 if (tiflags & TH_RST) { 2084 if (tiflags & TH_RST) {
2072 if (tiflags & TH_ACK) 2085 if (tiflags & TH_ACK)
2073 tp = tcp_drop(tp, ECONNREFUSED); 2086 tp = tcp_drop(tp, ECONNREFUSED);
2074 goto drop; 2087 goto drop;
2075 } 2088 }
2076 if ((tiflags & TH_SYN) == 0) 2089 if ((tiflags & TH_SYN) == 0)
2077 goto drop; 2090 goto drop;
2078 if (tiflags & TH_ACK) { 2091 if (tiflags & TH_ACK) {
2079 tp->snd_una = th->th_ack; 2092 tp->snd_una = th->th_ack;
2080 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2093 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2081 tp->snd_nxt = tp->snd_una; 2094 tp->snd_nxt = tp->snd_una;
2082 if (SEQ_LT(tp->snd_high, tp->snd_una)) 2095 if (SEQ_LT(tp->snd_high, tp->snd_una))
2083 tp->snd_high = tp->snd_una; 2096 tp->snd_high = tp->snd_una;
2084 TCP_TIMER_DISARM(tp, TCPT_REXMT); 2097 TCP_TIMER_DISARM(tp, TCPT_REXMT);
2085 2098
2086 if ((tiflags & TH_ECE) && tcp_do_ecn) { 2099 if ((tiflags & TH_ECE) && tcp_do_ecn) {
2087 tp->t_flags |= TF_ECN_PERMIT; 2100 tp->t_flags |= TF_ECN_PERMIT;
2088 TCP_STATINC(TCP_STAT_ECN_SHS); 2101 TCP_STATINC(TCP_STAT_ECN_SHS);
2089 } 2102 }
2090 } 2103 }
2091 tp->irs = th->th_seq; 2104 tp->irs = th->th_seq;
2092 tcp_rcvseqinit(tp); 2105 tcp_rcvseqinit(tp);
2093 tp->t_flags |= TF_ACKNOW; 2106 tp->t_flags |= TF_ACKNOW;
2094 tcp_mss_from_peer(tp, opti.maxseg); 2107 tcp_mss_from_peer(tp, opti.maxseg);
2095 2108
2096 /* 2109 /*
2097 * Initialize the initial congestion window. If we 2110 * Initialize the initial congestion window. If we
2098 * had to retransmit the SYN, we must initialize cwnd 2111 * had to retransmit the SYN, we must initialize cwnd
2099 * to 1 segment (i.e. the Loss Window). 2112 * to 1 segment (i.e. the Loss Window).
2100 */ 2113 */
2101 if (tp->t_flags & TF_SYN_REXMT) 2114 if (tp->t_flags & TF_SYN_REXMT)
2102 tp->snd_cwnd = tp->t_peermss; 2115 tp->snd_cwnd = tp->t_peermss;
2103 else { 2116 else {
2104 int ss = tcp_init_win; 2117 int ss = tcp_init_win;
2105 if (inp != NULL && in_localaddr(inp->inp_faddr)) 2118 if (inp != NULL && in_localaddr(inp->inp_faddr))
2106 ss = tcp_init_win_local; 2119 ss = tcp_init_win_local;
2107#ifdef INET6 2120#ifdef INET6
2108 if (in6p != NULL && in6_localaddr(&in6p->in6p_faddr)) 2121 if (in6p != NULL && in6_localaddr(&in6p->in6p_faddr))
2109 ss = tcp_init_win_local; 2122 ss = tcp_init_win_local;
2110#endif 2123#endif
2111 tp->snd_cwnd = TCP_INITIAL_WINDOW(ss, tp->t_peermss); 2124 tp->snd_cwnd = TCP_INITIAL_WINDOW(ss, tp->t_peermss);
2112 } 2125 }
2113 2126
2114 tcp_rmx_rtt(tp); 2127 tcp_rmx_rtt(tp);
2115 if (tiflags & TH_ACK) { 2128 if (tiflags & TH_ACK) {
2116 TCP_STATINC(TCP_STAT_CONNECTS); 2129 TCP_STATINC(TCP_STAT_CONNECTS);
2117 /* 2130 /*
2118 * move tcp_established before soisconnected 2131 * move tcp_established before soisconnected
2119 * because upcall handler can drive tcp_output 2132 * because upcall handler can drive tcp_output
2120 * functionality. 2133 * functionality.
2121 * XXX we might call soisconnected at the end of 2134 * XXX we might call soisconnected at the end of
2122 * all processing 2135 * all processing
2123 */ 2136 */
2124 tcp_established(tp); 2137 tcp_established(tp);
2125 soisconnected(so); 2138 soisconnected(so);
2126 /* Do window scaling on this connection? */ 2139 /* Do window scaling on this connection? */
2127 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2140 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2128 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2141 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
2129 tp->snd_scale = tp->requested_s_scale; 2142 tp->snd_scale = tp->requested_s_scale;
2130 tp->rcv_scale = tp->request_r_scale; 2143 tp->rcv_scale = tp->request_r_scale;
2131 } 2144 }
2132 TCP_REASS_LOCK(tp); 2145 TCP_REASS_LOCK(tp);
2133 (void)tcp_reass(tp, NULL, NULL, tlen); 2146 (void)tcp_reass(tp, NULL, NULL, tlen);
2134 /* 2147 /*
2135 * if we didn't have to retransmit the SYN, 2148 * if we didn't have to retransmit the SYN,
2136 * use its rtt as our initial srtt & rtt var. 2149 * use its rtt as our initial srtt & rtt var.
2137 */ 2150 */
2138 if (tp->t_rtttime) 2151 if (tp->t_rtttime)
2139 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime); 2152 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime);
2140 } else { 2153 } else {
2141 tp->t_state = TCPS_SYN_RECEIVED; 2154 tp->t_state = TCPS_SYN_RECEIVED;
2142 } 2155 }
2143 2156
2144 /* 2157 /*
2145 * Advance th->th_seq to correspond to first data byte. 2158 * Advance th->th_seq to correspond to first data byte.
2146 * If data, trim to stay within window, 2159 * If data, trim to stay within window,
2147 * dropping FIN if necessary. 2160 * dropping FIN if necessary.
2148 */ 2161 */
2149 th->th_seq++; 2162 th->th_seq++;
2150 if (tlen > tp->rcv_wnd) { 2163 if (tlen > tp->rcv_wnd) {
2151 todrop = tlen - tp->rcv_wnd; 2164 todrop = tlen - tp->rcv_wnd;
2152 m_adj(m, -todrop); 2165 m_adj(m, -todrop);
2153 tlen = tp->rcv_wnd; 2166 tlen = tp->rcv_wnd;
2154 tiflags &= ~TH_FIN; 2167 tiflags &= ~TH_FIN;
2155 tcps = TCP_STAT_GETREF(); 2168 tcps = TCP_STAT_GETREF();
2156 tcps[TCP_STAT_RCVPACKAFTERWIN]++; 2169 tcps[TCP_STAT_RCVPACKAFTERWIN]++;
2157 tcps[TCP_STAT_RCVBYTEAFTERWIN] += todrop; 2170 tcps[TCP_STAT_RCVBYTEAFTERWIN] += todrop;
2158 TCP_STAT_PUTREF(); 2171 TCP_STAT_PUTREF();
2159 } 2172 }
2160 tp->snd_wl1 = th->th_seq - 1; 2173 tp->snd_wl1 = th->th_seq - 1;
2161 tp->rcv_up = th->th_seq; 2174 tp->rcv_up = th->th_seq;
2162 goto step6; 2175 goto step6;
2163 2176
2164 /* 2177 /*
2165 * If the state is SYN_RECEIVED: 2178 * If the state is SYN_RECEIVED:
2166 * If seg contains an ACK, but not for our SYN, drop the input 2179 * If seg contains an ACK, but not for our SYN, drop the input
2167 * and generate an RST. See page 36, rfc793 2180 * and generate an RST. See page 36, rfc793
2168 */ 2181 */
2169 case TCPS_SYN_RECEIVED: 2182 case TCPS_SYN_RECEIVED:
2170 if ((tiflags & TH_ACK) && 2183 if ((tiflags & TH_ACK) &&
2171 (SEQ_LEQ(th->th_ack, tp->iss) || 2184 (SEQ_LEQ(th->th_ack, tp->iss) ||
2172 SEQ_GT(th->th_ack, tp->snd_max))) 2185 SEQ_GT(th->th_ack, tp->snd_max)))
2173 goto dropwithreset; 2186 goto dropwithreset;
2174 break; 2187 break;
2175 } 2188 }
2176 2189
2177 /* 2190 /*
2178 * From here on, we're dealing with !LISTEN and !SYN_SENT. 2191 * From here on, we're dealing with !LISTEN and !SYN_SENT.
2179 */ 2192 */
2180 KASSERT(tp->t_state != TCPS_LISTEN && 2193 KASSERT(tp->t_state != TCPS_LISTEN &&
2181 tp->t_state != TCPS_SYN_SENT); 2194 tp->t_state != TCPS_SYN_SENT);
2182 2195
2183 /* 2196 /*
2184 * RFC1323 PAWS: if we have a timestamp reply on this segment and 2197 * RFC1323 PAWS: if we have a timestamp reply on this segment and
2185 * it's less than ts_recent, drop it. 2198 * it's less than ts_recent, drop it.
2186 */ 2199 */
2187 if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent && 2200 if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
2188 TSTMP_LT(opti.ts_val, tp->ts_recent)) { 2201 TSTMP_LT(opti.ts_val, tp->ts_recent)) {
2189 /* Check to see if ts_recent is over 24 days old. */ 2202 /* Check to see if ts_recent is over 24 days old. */
2190 if (tcp_now - tp->ts_recent_age > TCP_PAWS_IDLE) { 2203 if (tcp_now - tp->ts_recent_age > TCP_PAWS_IDLE) {
2191 /* 2204 /*
2192 * Invalidate ts_recent. If this segment updates 2205 * Invalidate ts_recent. If this segment updates
2193 * ts_recent, the age will be reset later and ts_recent 2206 * ts_recent, the age will be reset later and ts_recent
2194 * will get a valid value. If it does not, setting 2207 * will get a valid value. If it does not, setting
2195 * ts_recent to zero will at least satisfy the 2208 * ts_recent to zero will at least satisfy the
2196 * requirement that zero be placed in the timestamp 2209 * requirement that zero be placed in the timestamp
2197 * echo reply when ts_recent isn't valid. The 2210 * echo reply when ts_recent isn't valid. The
2198 * age isn't reset until we get a valid ts_recent 2211 * age isn't reset until we get a valid ts_recent
2199 * because we don't want out-of-order segments to be 2212 * because we don't want out-of-order segments to be
2200 * dropped when ts_recent is old. 2213 * dropped when ts_recent is old.
2201 */ 2214 */
2202 tp->ts_recent = 0; 2215 tp->ts_recent = 0;
2203 } else { 2216 } else {
2204 tcps = TCP_STAT_GETREF(); 2217 tcps = TCP_STAT_GETREF();
2205 tcps[TCP_STAT_RCVDUPPACK]++; 2218 tcps[TCP_STAT_RCVDUPPACK]++;
2206 tcps[TCP_STAT_RCVDUPBYTE] += tlen; 2219 tcps[TCP_STAT_RCVDUPBYTE] += tlen;
2207 tcps[TCP_STAT_PAWSDROP]++; 2220 tcps[TCP_STAT_PAWSDROP]++;
2208 TCP_STAT_PUTREF(); 2221 TCP_STAT_PUTREF();
2209 tcp_new_dsack(tp, th->th_seq, tlen); 2222 tcp_new_dsack(tp, th->th_seq, tlen);
2210 goto dropafterack; 2223 goto dropafterack;
2211 } 2224 }
2212 } 2225 }
2213 2226
2214 /* 2227 /*
2215 * Check that at least some bytes of the segment are within the 2228 * Check that at least some bytes of the segment are within the
2216 * receive window. If segment begins before rcv_nxt, drop leading 2229 * receive window. If segment begins before rcv_nxt, drop leading
2217 * data (and SYN); if nothing left, just ack. 2230 * data (and SYN); if nothing left, just ack.
2218 */ 2231 */
2219 todrop = tp->rcv_nxt - th->th_seq; 2232 todrop = tp->rcv_nxt - th->th_seq;
2220 dupseg = false; 2233 dupseg = false;
2221 if (todrop > 0) { 2234 if (todrop > 0) {
2222 if (tiflags & TH_SYN) { 2235 if (tiflags & TH_SYN) {
2223 tiflags &= ~TH_SYN; 2236 tiflags &= ~TH_SYN;
2224 th->th_seq++; 2237 th->th_seq++;
2225 tcp_urp_drop(th, 1, &tiflags); 2238 tcp_urp_drop(th, 1, &tiflags);
2226 todrop--; 2239 todrop--;
2227 } 2240 }
2228 if (todrop > tlen || 2241 if (todrop > tlen ||
2229 (todrop == tlen && (tiflags & TH_FIN) == 0)) { 2242 (todrop == tlen && (tiflags & TH_FIN) == 0)) {
2230 /* 2243 /*
2231 * Any valid FIN or RST must be to the left of the 2244 * Any valid FIN or RST must be to the left of the
2232 * window. At this point the FIN or RST must be a 2245 * window. At this point the FIN or RST must be a
2233 * duplicate or out of sequence; drop it. 2246 * duplicate or out of sequence; drop it.
2234 */ 2247 */
2235 if (tiflags & TH_RST) 2248 if (tiflags & TH_RST)
2236 goto drop; 2249 goto drop;
2237 tiflags &= ~(TH_FIN|TH_RST); 2250 tiflags &= ~(TH_FIN|TH_RST);
2238 2251
2239 /* 2252 /*
2240 * Send an ACK to resynchronize and drop any data. 2253 * Send an ACK to resynchronize and drop any data.
2241 * But keep on processing for RST or ACK. 2254 * But keep on processing for RST or ACK.
2242 */ 2255 */
2243 tp->t_flags |= TF_ACKNOW; 2256 tp->t_flags |= TF_ACKNOW;
2244 todrop = tlen; 2257 todrop = tlen;
2245 dupseg = true; 2258 dupseg = true;
2246 tcps = TCP_STAT_GETREF(); 2259 tcps = TCP_STAT_GETREF();
2247 tcps[TCP_STAT_RCVDUPPACK]++; 2260 tcps[TCP_STAT_RCVDUPPACK]++;
2248 tcps[TCP_STAT_RCVDUPBYTE] += todrop; 2261 tcps[TCP_STAT_RCVDUPBYTE] += todrop;
2249 TCP_STAT_PUTREF(); 2262 TCP_STAT_PUTREF();
2250 } else if ((tiflags & TH_RST) && th->th_seq != tp->rcv_nxt) { 2263 } else if ((tiflags & TH_RST) && th->th_seq != tp->rcv_nxt) {
2251 /* 2264 /*
2252 * Test for reset before adjusting the sequence 2265 * Test for reset before adjusting the sequence
2253 * number for overlapping data. 2266 * number for overlapping data.
2254 */ 2267 */
2255 goto dropafterack_ratelim; 2268 goto dropafterack_ratelim;
2256 } else { 2269 } else {
2257 tcps = TCP_STAT_GETREF(); 2270 tcps = TCP_STAT_GETREF();
2258 tcps[TCP_STAT_RCVPARTDUPPACK]++; 2271 tcps[TCP_STAT_RCVPARTDUPPACK]++;
2259 tcps[TCP_STAT_RCVPARTDUPBYTE] += todrop; 2272 tcps[TCP_STAT_RCVPARTDUPBYTE] += todrop;
2260 TCP_STAT_PUTREF(); 2273 TCP_STAT_PUTREF();
2261 } 2274 }
2262 tcp_new_dsack(tp, th->th_seq, todrop); 2275 tcp_new_dsack(tp, th->th_seq, todrop);
2263 hdroptlen += todrop; /* drop from head afterwards (m_adj) */ 2276 hdroptlen += todrop; /* drop from head afterwards (m_adj) */
2264 th->th_seq += todrop; 2277 th->th_seq += todrop;
2265 tlen -= todrop; 2278 tlen -= todrop;
2266 tcp_urp_drop(th, todrop, &tiflags); 2279 tcp_urp_drop(th, todrop, &tiflags);
2267 } 2280 }
2268 2281
2269 /* 2282 /*
2270 * If new data is received on a connection after the user processes 2283 * If new data is received on a connection after the user processes
2271 * are gone, then RST the other end. 2284 * are gone, then RST the other end.
2272 */ 2285 */
2273 if ((so->so_state & SS_NOFDREF) && 2286 if ((so->so_state & SS_NOFDREF) &&
2274 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2287 tp->t_state > TCPS_CLOSE_WAIT && tlen) {
2275 tp = tcp_close(tp); 2288 tp = tcp_close(tp);
2276 TCP_STATINC(TCP_STAT_RCVAFTERCLOSE); 2289 TCP_STATINC(TCP_STAT_RCVAFTERCLOSE);
2277 goto dropwithreset; 2290 goto dropwithreset;
2278 } 2291 }
2279 2292
2280 /* 2293 /*
2281 * If the segment ends after the window, drop trailing data (and 2294 * If the segment ends after the window, drop trailing data (and
2282 * PUSH and FIN); if nothing left, just ACK. 2295 * PUSH and FIN); if nothing left, just ACK.
2283 */ 2296 */
2284 todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2297 todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2285 if (todrop > 0) { 2298 if (todrop > 0) {
2286 TCP_STATINC(TCP_STAT_RCVPACKAFTERWIN); 2299 TCP_STATINC(TCP_STAT_RCVPACKAFTERWIN);
2287 if (todrop >= tlen) { 2300 if (todrop >= tlen) {
2288 /* 2301 /*
2289 * The segment actually starts after the window. 2302 * The segment actually starts after the window.
2290 * th->th_seq + tlen - tp->rcv_nxt - tp->rcv_wnd >= tlen 2303 * th->th_seq + tlen - tp->rcv_nxt - tp->rcv_wnd >= tlen
2291 * th->th_seq - tp->rcv_nxt - tp->rcv_wnd >= 0 2304 * th->th_seq - tp->rcv_nxt - tp->rcv_wnd >= 0
2292 * th->th_seq >= tp->rcv_nxt + tp->rcv_wnd 2305 * th->th_seq >= tp->rcv_nxt + tp->rcv_wnd
2293 */ 2306 */
2294 TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, tlen); 2307 TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, tlen);
2295 2308
2296 /* 2309 /*
2297 * If a new connection request is received while in 2310 * If a new connection request is received while in
2298 * TIME_WAIT, drop the old connection and start over 2311 * TIME_WAIT, drop the old connection and start over
2299 * if the sequence numbers are above the previous 2312 * if the sequence numbers are above the previous
2300 * ones. 2313 * ones.
2301 * 2314 *
2302 * NOTE: We need to put the header fields back into 2315 * NOTE: We need to put the header fields back into
2303 * network order. 2316 * network order.
2304 */ 2317 */
2305 if ((tiflags & TH_SYN) && 2318 if ((tiflags & TH_SYN) &&
2306 tp->t_state == TCPS_TIME_WAIT && 2319 tp->t_state == TCPS_TIME_WAIT &&
2307 SEQ_GT(th->th_seq, tp->rcv_nxt)) { 2320 SEQ_GT(th->th_seq, tp->rcv_nxt)) {
2308 tp = tcp_close(tp); 2321 tp = tcp_close(tp);
2309 tcp_fields_to_net(th); 2322 tcp_fields_to_net(th);
2310 m_freem(tcp_saveti); 2323 m_freem(tcp_saveti);
2311 tcp_saveti = NULL; 2324 tcp_saveti = NULL;
2312 goto findpcb; 2325 goto findpcb;
2313 } 2326 }
2314 2327
2315 /* 2328 /*
2316 * If window is closed can only take segments at 2329 * If window is closed can only take segments at
2317 * window edge, and have to drop data and PUSH from 2330 * window edge, and have to drop data and PUSH from
2318 * incoming segments. Continue processing, but 2331 * incoming segments. Continue processing, but
2319 * remember to ack. Otherwise, drop segment 2332 * remember to ack. Otherwise, drop segment
2320 * and (if not RST) ack. 2333 * and (if not RST) ack.
2321 */ 2334 */
2322 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2335 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2323 KASSERT(todrop == tlen); 2336 KASSERT(todrop == tlen);
2324 tp->t_flags |= TF_ACKNOW; 2337 tp->t_flags |= TF_ACKNOW;
2325 TCP_STATINC(TCP_STAT_RCVWINPROBE); 2338 TCP_STATINC(TCP_STAT_RCVWINPROBE);
2326 } else { 2339 } else {
2327 goto dropafterack; 2340 goto dropafterack;
2328 } 2341 }
2329 } else { 2342 } else {
2330 TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, todrop); 2343 TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, todrop);
2331 } 2344 }
2332 m_adj(m, -todrop); 2345 m_adj(m, -todrop);
2333 tlen -= todrop; 2346 tlen -= todrop;
2334 tiflags &= ~(TH_PUSH|TH_FIN); 2347 tiflags &= ~(TH_PUSH|TH_FIN);
2335 } 2348 }
2336 2349
2337 /* 2350 /*
2338 * If last ACK falls within this segment's sequence numbers, 2351 * If last ACK falls within this segment's sequence numbers,
2339 * record the timestamp. 2352 * record the timestamp.
2340 * NOTE:  2353 * NOTE:
2341 * 1) That the test incorporates suggestions from the latest 2354 * 1) That the test incorporates suggestions from the latest
2342 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2355 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
2343 * 2) That updating only on newer timestamps interferes with 2356 * 2) That updating only on newer timestamps interferes with
2344 * our earlier PAWS tests, so this check should be solely 2357 * our earlier PAWS tests, so this check should be solely
2345 * predicated on the sequence space of this segment. 2358 * predicated on the sequence space of this segment.
2346 * 3) That we modify the segment boundary check to be  2359 * 3) That we modify the segment boundary check to be
2347 * Last.ACK.Sent <= SEG.SEQ + SEG.Len  2360 * Last.ACK.Sent <= SEG.SEQ + SEG.Len
2348 * instead of RFC1323's 2361 * instead of RFC1323's
2349 * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2362 * Last.ACK.Sent < SEG.SEQ + SEG.Len,
2350 * This modified check allows us to overcome RFC1323's 2363 * This modified check allows us to overcome RFC1323's
2351 * limitations as described in Stevens TCP/IP Illustrated 2364 * limitations as described in Stevens TCP/IP Illustrated
2352 * Vol. 2 p.869. In such cases, we can still calculate the 2365 * Vol. 2 p.869. In such cases, we can still calculate the
2353 * RTT correctly when RCV.NXT == Last.ACK.Sent. 2366 * RTT correctly when RCV.NXT == Last.ACK.Sent.
2354 */ 2367 */
2355 if (opti.ts_present && 2368 if (opti.ts_present &&
2356 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2369 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2357 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2370 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2358 ((tiflags & (TH_SYN|TH_FIN)) != 0))) { 2371 ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
2359 tp->ts_recent_age = tcp_now; 2372 tp->ts_recent_age = tcp_now;
2360 tp->ts_recent = opti.ts_val; 2373 tp->ts_recent = opti.ts_val;
2361 } 2374 }
2362 2375
2363 /* 2376 /*
2364 * If the RST bit is set examine the state: 2377 * If the RST bit is set examine the state:
2365 * RECEIVED state: 2378 * RECEIVED state:
2366 * If passive open, return to LISTEN state. 2379 * If passive open, return to LISTEN state.
2367 * If active open, inform user that connection was refused. 2380 * If active open, inform user that connection was refused.
2368 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT states: 2381 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT states:
2369 * Inform user that connection was reset, and close tcb. 2382 * Inform user that connection was reset, and close tcb.
2370 * CLOSING, LAST_ACK, TIME_WAIT states: 2383 * CLOSING, LAST_ACK, TIME_WAIT states:
2371 * Close the tcb. 2384 * Close the tcb.
2372 */ 2385 */
2373 if (tiflags & TH_RST) { 2386 if (tiflags & TH_RST) {
2374 if (th->th_seq != tp->rcv_nxt) 2387 if (th->th_seq != tp->rcv_nxt)
2375 goto dropafterack_ratelim; 2388 goto dropafterack_ratelim;
2376 2389
2377 switch (tp->t_state) { 2390 switch (tp->t_state) {
2378 case TCPS_SYN_RECEIVED: 2391 case TCPS_SYN_RECEIVED:
2379 so->so_error = ECONNREFUSED; 2392 so->so_error = ECONNREFUSED;
2380 goto close; 2393 goto close;
2381 2394
2382 case TCPS_ESTABLISHED: 2395 case TCPS_ESTABLISHED:
2383 case TCPS_FIN_WAIT_1: 2396 case TCPS_FIN_WAIT_1:
2384 case TCPS_FIN_WAIT_2: 2397 case TCPS_FIN_WAIT_2:
2385 case TCPS_CLOSE_WAIT: 2398 case TCPS_CLOSE_WAIT:
2386 so->so_error = ECONNRESET; 2399 so->so_error = ECONNRESET;
2387 close: 2400 close:
2388 tp->t_state = TCPS_CLOSED; 2401 tp->t_state = TCPS_CLOSED;
2389 TCP_STATINC(TCP_STAT_DROPS); 2402 TCP_STATINC(TCP_STAT_DROPS);
2390 tp = tcp_close(tp); 2403 tp = tcp_close(tp);
2391 goto drop; 2404 goto drop;
2392 2405
2393 case TCPS_CLOSING: 2406 case TCPS_CLOSING:
2394 case TCPS_LAST_ACK: 2407 case TCPS_LAST_ACK:
2395 case TCPS_TIME_WAIT: 2408 case TCPS_TIME_WAIT:
2396 tp = tcp_close(tp); 2409 tp = tcp_close(tp);
2397 goto drop; 2410 goto drop;
2398 } 2411 }
2399 } 2412 }
2400 2413
2401 /* 2414 /*
2402 * Since we've covered the SYN-SENT and SYN-RECEIVED states above 2415 * Since we've covered the SYN-SENT and SYN-RECEIVED states above
2403 * we must be in a synchronized state. RFC793 states (under Reset 2416 * we must be in a synchronized state. RFC793 states (under Reset
2404 * Generation) that any unacceptable segment (an out-of-order SYN 2417 * Generation) that any unacceptable segment (an out-of-order SYN
2405 * qualifies) received in a synchronized state must elicit only an 2418 * qualifies) received in a synchronized state must elicit only an
2406 * empty acknowledgment segment ... and the connection remains in 2419 * empty acknowledgment segment ... and the connection remains in
2407 * the same state. 2420 * the same state.
2408 */ 2421 */
2409 if (tiflags & TH_SYN) { 2422 if (tiflags & TH_SYN) {
2410 if (tp->rcv_nxt == th->th_seq) { 2423 if (tp->rcv_nxt == th->th_seq) {
2411 tcp_respond(tp, m, m, th, (tcp_seq)0, th->th_ack - 1, 2424 tcp_respond(tp, m, m, th, (tcp_seq)0, th->th_ack - 1,
2412 TH_ACK); 2425 TH_ACK);
2413 if (tcp_saveti) 2426 if (tcp_saveti)
2414 m_freem(tcp_saveti); 2427 m_freem(tcp_saveti);
2415 return; 2428 return;
2416 } 2429 }
2417 2430
2418 goto dropafterack_ratelim; 2431 goto dropafterack_ratelim;
2419 } 2432 }
2420 2433
2421 /* 2434 /*
2422 * If the ACK bit is off we drop the segment and return. 2435 * If the ACK bit is off we drop the segment and return.
2423 */ 2436 */
2424 if ((tiflags & TH_ACK) == 0) { 2437 if ((tiflags & TH_ACK) == 0) {
2425 if (tp->t_flags & TF_ACKNOW) 2438 if (tp->t_flags & TF_ACKNOW)
2426 goto dropafterack; 2439 goto dropafterack;
2427 goto drop; 2440 goto drop;
2428 } 2441 }
2429 2442
2430 /* 2443 /*
2431 * From here on, we're doing ACK processing. 2444 * From here on, we're doing ACK processing.
2432 */ 2445 */
2433 2446
2434 switch (tp->t_state) { 2447 switch (tp->t_state) {
2435 /* 2448 /*
2436 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 2449 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
2437 * ESTABLISHED state and continue processing, otherwise 2450 * ESTABLISHED state and continue processing, otherwise
2438 * send an RST. 2451 * send an RST.
2439 */ 2452 */
2440 case TCPS_SYN_RECEIVED: 2453 case TCPS_SYN_RECEIVED:
2441 if (SEQ_GT(tp->snd_una, th->th_ack) || 2454 if (SEQ_GT(tp->snd_una, th->th_ack) ||
2442 SEQ_GT(th->th_ack, tp->snd_max)) 2455 SEQ_GT(th->th_ack, tp->snd_max))
2443 goto dropwithreset; 2456 goto dropwithreset;
2444 TCP_STATINC(TCP_STAT_CONNECTS); 2457 TCP_STATINC(TCP_STAT_CONNECTS);
2445 soisconnected(so); 2458 soisconnected(so);
2446 tcp_established(tp); 2459 tcp_established(tp);
2447 /* Do window scaling? */ 2460 /* Do window scaling? */
2448 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2461 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2449 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2462 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
2450 tp->snd_scale = tp->requested_s_scale; 2463 tp->snd_scale = tp->requested_s_scale;
2451 tp->rcv_scale = tp->request_r_scale; 2464 tp->rcv_scale = tp->request_r_scale;
2452 } 2465 }
2453 TCP_REASS_LOCK(tp); 2466 TCP_REASS_LOCK(tp);
2454 (void)tcp_reass(tp, NULL, NULL, tlen); 2467 (void)tcp_reass(tp, NULL, NULL, tlen);
2455 tp->snd_wl1 = th->th_seq - 1; 2468 tp->snd_wl1 = th->th_seq - 1;
2456 /* FALLTHROUGH */ 2469 /* FALLTHROUGH */
2457 2470
2458 /* 2471 /*
2459 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2472 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2460 * ACKs. If the ack is in the range 2473 * ACKs. If the ack is in the range
2461 * tp->snd_una < th->th_ack <= tp->snd_max 2474 * tp->snd_una < th->th_ack <= tp->snd_max
2462 * then advance tp->snd_una to th->th_ack and drop 2475 * then advance tp->snd_una to th->th_ack and drop
2463 * data from the retransmission queue. If this ACK reflects 2476 * data from the retransmission queue. If this ACK reflects
2464 * more up to date window information we update our window information. 2477 * more up to date window information we update our window information.
2465 */ 2478 */
2466 case TCPS_ESTABLISHED: 2479 case TCPS_ESTABLISHED:
2467 case TCPS_FIN_WAIT_1: 2480 case TCPS_FIN_WAIT_1:
2468 case TCPS_FIN_WAIT_2: 2481 case TCPS_FIN_WAIT_2:
2469 case TCPS_CLOSE_WAIT: 2482 case TCPS_CLOSE_WAIT:
2470 case TCPS_CLOSING: 2483 case TCPS_CLOSING:
2471 case TCPS_LAST_ACK: 2484 case TCPS_LAST_ACK:
2472 case TCPS_TIME_WAIT: 2485 case TCPS_TIME_WAIT:
2473 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2486 if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
2474 if (tlen == 0 && !dupseg && tiwin == tp->snd_wnd) { 2487 if (tlen == 0 && !dupseg && tiwin == tp->snd_wnd) {
2475 TCP_STATINC(TCP_STAT_RCVDUPACK); 2488 TCP_STATINC(TCP_STAT_RCVDUPACK);
2476 /* 2489 /*
2477 * If we have outstanding data (other than 2490 * If we have outstanding data (other than
2478 * a window probe), this is a completely 2491 * a window probe), this is a completely
2479 * duplicate ack (ie, window info didn't 2492 * duplicate ack (ie, window info didn't
2480 * change), the ack is the biggest we've 2493 * change), the ack is the biggest we've
2481 * seen and we've seen exactly our rexmt 2494 * seen and we've seen exactly our rexmt
2482 * threshhold of them, assume a packet 2495 * threshhold of them, assume a packet
2483 * has been dropped and retransmit it. 2496 * has been dropped and retransmit it.
2484 * Kludge snd_nxt & the congestion 2497 * Kludge snd_nxt & the congestion
2485 * window so we send only this one 2498 * window so we send only this one
2486 * packet. 2499 * packet.
2487 */ 2500 */
2488 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 || 2501 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 ||
2489 th->th_ack != tp->snd_una) 2502 th->th_ack != tp->snd_una)
2490 tp->t_dupacks = 0; 2503 tp->t_dupacks = 0;
2491 else if (tp->t_partialacks < 0 && 2504 else if (tp->t_partialacks < 0 &&
2492 (++tp->t_dupacks == tcprexmtthresh || 2505 (++tp->t_dupacks == tcprexmtthresh ||
2493 TCP_FACK_FASTRECOV(tp))) { 2506 TCP_FACK_FASTRECOV(tp))) {
2494 /* 2507 /*
2495 * Do the fast retransmit, and adjust 2508 * Do the fast retransmit, and adjust
2496 * congestion control paramenters. 2509 * congestion control paramenters.
2497 */ 2510 */
2498 if (tp->t_congctl->fast_retransmit(tp, th)) { 2511 if (tp->t_congctl->fast_retransmit(tp, th)) {
2499 /* False fast retransmit */ 2512 /* False fast retransmit */
2500 break; 2513 break;
2501 } 2514 }
2502 goto drop; 2515 goto drop;
2503 } else if (tp->t_dupacks > tcprexmtthresh) { 2516 } else if (tp->t_dupacks > tcprexmtthresh) {
2504 tp->snd_cwnd += tp->t_segsz; 2517 tp->snd_cwnd += tp->t_segsz;
2505 KERNEL_LOCK(1, NULL); 2518 KERNEL_LOCK(1, NULL);
2506 (void)tcp_output(tp); 2519 (void)tcp_output(tp);
2507 KERNEL_UNLOCK_ONE(NULL); 2520 KERNEL_UNLOCK_ONE(NULL);
2508 goto drop; 2521 goto drop;
2509 } 2522 }
2510 } else { 2523 } else {
2511 /* 2524 /*
2512 * If the ack appears to be very old, only 2525 * If the ack appears to be very old, only
2513 * allow data that is in-sequence. This 2526 * allow data that is in-sequence. This
2514 * makes it somewhat more difficult to insert 2527 * makes it somewhat more difficult to insert
2515 * forged data by guessing sequence numbers. 2528 * forged data by guessing sequence numbers.
2516 * Sent an ack to try to update the send 2529 * Sent an ack to try to update the send
2517 * sequence number on the other side. 2530 * sequence number on the other side.
2518 */ 2531 */
2519 if (tlen && th->th_seq != tp->rcv_nxt && 2532 if (tlen && th->th_seq != tp->rcv_nxt &&
2520 SEQ_LT(th->th_ack, 2533 SEQ_LT(th->th_ack,
2521 tp->snd_una - tp->max_sndwnd)) 2534 tp->snd_una - tp->max_sndwnd))
2522 goto dropafterack; 2535 goto dropafterack;
2523 } 2536 }
2524 break; 2537 break;
2525 } 2538 }
2526 /* 2539 /*
2527 * If the congestion window was inflated to account 2540 * If the congestion window was inflated to account
2528 * for the other side's cached packets, retract it. 2541 * for the other side's cached packets, retract it.
2529 */ 2542 */
2530 tp->t_congctl->fast_retransmit_newack(tp, th); 2543 tp->t_congctl->fast_retransmit_newack(tp, th);
2531 2544
2532 if (SEQ_GT(th->th_ack, tp->snd_max)) { 2545 if (SEQ_GT(th->th_ack, tp->snd_max)) {
2533 TCP_STATINC(TCP_STAT_RCVACKTOOMUCH); 2546 TCP_STATINC(TCP_STAT_RCVACKTOOMUCH);
2534 goto dropafterack; 2547 goto dropafterack;
2535 } 2548 }
2536 acked = th->th_ack - tp->snd_una; 2549 acked = th->th_ack - tp->snd_una;
2537 tcps = TCP_STAT_GETREF(); 2550 tcps = TCP_STAT_GETREF();
2538 tcps[TCP_STAT_RCVACKPACK]++; 2551 tcps[TCP_STAT_RCVACKPACK]++;
2539 tcps[TCP_STAT_RCVACKBYTE] += acked; 2552 tcps[TCP_STAT_RCVACKBYTE] += acked;
2540 TCP_STAT_PUTREF(); 2553 TCP_STAT_PUTREF();
2541 2554
2542 /* 2555 /*
2543 * If we have a timestamp reply, update smoothed 2556 * If we have a timestamp reply, update smoothed
2544 * round trip time. If no timestamp is present but 2557 * round trip time. If no timestamp is present but
2545 * transmit timer is running and timed sequence 2558 * transmit timer is running and timed sequence
2546 * number was acked, update smoothed round trip time. 2559 * number was acked, update smoothed round trip time.
2547 * Since we now have an rtt measurement, cancel the 2560 * Since we now have an rtt measurement, cancel the
2548 * timer backoff (cf., Phil Karn's retransmit alg.). 2561 * timer backoff (cf., Phil Karn's retransmit alg.).
2549 * Recompute the initial retransmit timer. 2562 * Recompute the initial retransmit timer.
2550 */ 2563 */
2551 if (ts_rtt) 2564 if (ts_rtt)
2552 tcp_xmit_timer(tp, ts_rtt - 1); 2565 tcp_xmit_timer(tp, ts_rtt - 1);
2553 else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) 2566 else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
2554 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime); 2567 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime);
2555 2568
2556 /* 2569 /*
2557 * If all outstanding data is acked, stop retransmit 2570 * If all outstanding data is acked, stop retransmit
2558 * timer and remember to restart (more output or persist). 2571 * timer and remember to restart (more output or persist).
2559 * If there is more data to be acked, restart retransmit 2572 * If there is more data to be acked, restart retransmit
2560 * timer, using current (possibly backed-off) value. 2573 * timer, using current (possibly backed-off) value.
2561 */ 2574 */
2562 if (th->th_ack == tp->snd_max) { 2575 if (th->th_ack == tp->snd_max) {
2563 TCP_TIMER_DISARM(tp, TCPT_REXMT); 2576 TCP_TIMER_DISARM(tp, TCPT_REXMT);
2564 needoutput = 1; 2577 needoutput = 1;
2565 } else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) 2578 } else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
2566 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 2579 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
2567 2580
2568 /* 2581 /*
2569 * New data has been acked, adjust the congestion window. 2582 * New data has been acked, adjust the congestion window.
2570 */ 2583 */
2571 tp->t_congctl->newack(tp, th); 2584 tp->t_congctl->newack(tp, th);
2572 2585
2573 nd6_hint(tp); 2586 nd6_hint(tp);
2574 if (acked > so->so_snd.sb_cc) { 2587 if (acked > so->so_snd.sb_cc) {
2575 tp->snd_wnd -= so->so_snd.sb_cc; 2588 tp->snd_wnd -= so->so_snd.sb_cc;
2576 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 2589 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
2577 ourfinisacked = 1; 2590 ourfinisacked = 1;
2578 } else { 2591 } else {
2579 if (acked > (tp->t_lastoff - tp->t_inoff)) 2592 if (acked > (tp->t_lastoff - tp->t_inoff))
2580 tp->t_lastm = NULL; 2593 tp->t_lastm = NULL;
2581 sbdrop(&so->so_snd, acked); 2594 sbdrop(&so->so_snd, acked);
2582 tp->t_lastoff -= acked; 2595 tp->t_lastoff -= acked;
2583 if (tp->snd_wnd > acked) 2596 if (tp->snd_wnd > acked)
2584 tp->snd_wnd -= acked; 2597 tp->snd_wnd -= acked;
2585 else 2598 else
2586 tp->snd_wnd = 0; 2599 tp->snd_wnd = 0;
2587 ourfinisacked = 0; 2600 ourfinisacked = 0;
2588 } 2601 }
2589 sowwakeup(so); 2602 sowwakeup(so);
2590 2603
2591 icmp_check(tp, th, acked); 2604 icmp_check(tp, th, acked);
2592 2605
2593 tp->snd_una = th->th_ack; 2606 tp->snd_una = th->th_ack;
2594 if (SEQ_GT(tp->snd_una, tp->snd_fack)) 2607 if (SEQ_GT(tp->snd_una, tp->snd_fack))
2595 tp->snd_fack = tp->snd_una; 2608 tp->snd_fack = tp->snd_una;
2596 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2609 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2597 tp->snd_nxt = tp->snd_una; 2610 tp->snd_nxt = tp->snd_una;
2598 if (SEQ_LT(tp->snd_high, tp->snd_una)) 2611 if (SEQ_LT(tp->snd_high, tp->snd_una))
2599 tp->snd_high = tp->snd_una; 2612 tp->snd_high = tp->snd_una;
2600 2613
2601 switch (tp->t_state) { 2614 switch (tp->t_state) {
2602 2615
2603 /* 2616 /*
2604 * In FIN_WAIT_1 STATE in addition to the processing 2617 * In FIN_WAIT_1 STATE in addition to the processing
2605 * for the ESTABLISHED state if our FIN is now acknowledged 2618 * for the ESTABLISHED state if our FIN is now acknowledged
2606 * then enter FIN_WAIT_2. 2619 * then enter FIN_WAIT_2.
2607 */ 2620 */
2608 case TCPS_FIN_WAIT_1: 2621 case TCPS_FIN_WAIT_1:
2609 if (ourfinisacked) { 2622 if (ourfinisacked) {
2610 /* 2623 /*
2611 * If we can't receive any more 2624 * If we can't receive any more
2612 * data, then closing user can proceed. 2625 * data, then closing user can proceed.
2613 * Starting the timer is contrary to the 2626 * Starting the timer is contrary to the
2614 * specification, but if we don't get a FIN 2627 * specification, but if we don't get a FIN
2615 * we'll hang forever. 2628 * we'll hang forever.
2616 */ 2629 */
2617 if (so->so_state & SS_CANTRCVMORE) { 2630 if (so->so_state & SS_CANTRCVMORE) {
2618 soisdisconnected(so); 2631 soisdisconnected(so);
2619 if (tp->t_maxidle > 0) 2632 if (tp->t_maxidle > 0)
2620 TCP_TIMER_ARM(tp, TCPT_2MSL, 2633 TCP_TIMER_ARM(tp, TCPT_2MSL,
2621 tp->t_maxidle); 2634 tp->t_maxidle);
2622 } 2635 }
2623 tp->t_state = TCPS_FIN_WAIT_2; 2636 tp->t_state = TCPS_FIN_WAIT_2;
2624 } 2637 }
2625 break; 2638 break;
2626 2639
2627 /* 2640 /*
2628 * In CLOSING STATE in addition to the processing for 2641 * In CLOSING STATE in addition to the processing for
2629 * the ESTABLISHED state if the ACK acknowledges our FIN 2642 * the ESTABLISHED state if the ACK acknowledges our FIN
2630 * then enter the TIME-WAIT state, otherwise ignore 2643 * then enter the TIME-WAIT state, otherwise ignore
2631 * the segment. 2644 * the segment.
2632 */ 2645 */
2633 case TCPS_CLOSING: 2646 case TCPS_CLOSING:
2634 if (ourfinisacked) { 2647 if (ourfinisacked) {
2635 tp->t_state = TCPS_TIME_WAIT; 2648 tp->t_state = TCPS_TIME_WAIT;
2636 tcp_canceltimers(tp); 2649 tcp_canceltimers(tp);
2637 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl); 2650 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl);
2638 soisdisconnected(so); 2651 soisdisconnected(so);
2639 } 2652 }
2640 break; 2653 break;
2641 2654
2642 /* 2655 /*
2643 * In LAST_ACK, we may still be waiting for data to drain 2656 * In LAST_ACK, we may still be waiting for data to drain
2644 * and/or to be acked, as well as for the ack of our FIN. 2657 * and/or to be acked, as well as for the ack of our FIN.
2645 * If our FIN is now acknowledged, delete the TCB, 2658 * If our FIN is now acknowledged, delete the TCB,
2646 * enter the closed state and return. 2659 * enter the closed state and return.
2647 */ 2660 */
2648 case TCPS_LAST_ACK: 2661 case TCPS_LAST_ACK:
2649 if (ourfinisacked) { 2662 if (ourfinisacked) {
2650 tp = tcp_close(tp); 2663 tp = tcp_close(tp);
2651 goto drop; 2664 goto drop;
2652 } 2665 }
2653 break; 2666 break;
2654 2667
2655 /* 2668 /*
2656 * In TIME_WAIT state the only thing that should arrive 2669 * In TIME_WAIT state the only thing that should arrive
2657 * is a retransmission of the remote FIN. Acknowledge 2670 * is a retransmission of the remote FIN. Acknowledge
2658 * it and restart the finack timer. 2671 * it and restart the finack timer.
2659 */ 2672 */
2660 case TCPS_TIME_WAIT: 2673 case TCPS_TIME_WAIT:
2661 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl); 2674 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl);
2662 goto dropafterack; 2675 goto dropafterack;
2663 } 2676 }
2664 } 2677 }
2665 2678
2666step6: 2679step6:
2667 /* 2680 /*
2668 * Update window information. 2681 * Update window information.
2669 * Don't look at window if no ACK: TAC's send garbage on first SYN. 2682 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2670 */ 2683 */
2671 if ((tiflags & TH_ACK) && (SEQ_LT(tp->snd_wl1, th->th_seq) || 2684 if ((tiflags & TH_ACK) && (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2672 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2685 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2673 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2686 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2674 /* keep track of pure window updates */ 2687 /* keep track of pure window updates */
2675 if (tlen == 0 && 2688 if (tlen == 0 &&
2676 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2689 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2677 TCP_STATINC(TCP_STAT_RCVWINUPD); 2690 TCP_STATINC(TCP_STAT_RCVWINUPD);
2678 tp->snd_wnd = tiwin; 2691 tp->snd_wnd = tiwin;
2679 tp->snd_wl1 = th->th_seq; 2692 tp->snd_wl1 = th->th_seq;
2680 tp->snd_wl2 = th->th_ack; 2693 tp->snd_wl2 = th->th_ack;
2681 if (tp->snd_wnd > tp->max_sndwnd) 2694 if (tp->snd_wnd > tp->max_sndwnd)
2682 tp->max_sndwnd = tp->snd_wnd; 2695 tp->max_sndwnd = tp->snd_wnd;
2683 needoutput = 1; 2696 needoutput = 1;
2684 } 2697 }
2685 2698
2686 /* 2699 /*
2687 * Process segments with URG. 2700 * Process segments with URG.
2688 */ 2701 */
2689 if ((tiflags & TH_URG) && th->th_urp && 2702 if ((tiflags & TH_URG) && th->th_urp &&
2690 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2703 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2691 /* 2704 /*
2692 * This is a kludge, but if we receive and accept 2705 * This is a kludge, but if we receive and accept
2693 * random urgent pointers, we'll crash in 2706 * random urgent pointers, we'll crash in
2694 * soreceive. It's hard to imagine someone 2707 * soreceive. It's hard to imagine someone
2695 * actually wanting to send this much urgent data. 2708 * actually wanting to send this much urgent data.
2696 */ 2709 */
2697 if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2710 if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2698 th->th_urp = 0; /* XXX */ 2711 th->th_urp = 0; /* XXX */
2699 tiflags &= ~TH_URG; /* XXX */ 2712 tiflags &= ~TH_URG; /* XXX */
2700 goto dodata; /* XXX */ 2713 goto dodata; /* XXX */
2701 } 2714 }
2702 2715
2703 /* 2716 /*
2704 * If this segment advances the known urgent pointer, 2717 * If this segment advances the known urgent pointer,
2705 * then mark the data stream. This should not happen 2718 * then mark the data stream. This should not happen
2706 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2719 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2707 * a FIN has been received from the remote side. 2720 * a FIN has been received from the remote side.
2708 * In these states we ignore the URG. 2721 * In these states we ignore the URG.
2709 * 2722 *
2710 * According to RFC961 (Assigned Protocols), 2723 * According to RFC961 (Assigned Protocols),
2711 * the urgent pointer points to the last octet 2724 * the urgent pointer points to the last octet
2712 * of urgent data. We continue, however, 2725 * of urgent data. We continue, however,
2713 * to consider it to indicate the first octet 2726 * to consider it to indicate the first octet
2714 * of data past the urgent section as the original 2727 * of data past the urgent section as the original
2715 * spec states (in one of two places). 2728 * spec states (in one of two places).
2716 */ 2729 */
2717 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2730 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2718 tp->rcv_up = th->th_seq + th->th_urp; 2731 tp->rcv_up = th->th_seq + th->th_urp;
2719 so->so_oobmark = so->so_rcv.sb_cc + 2732 so->so_oobmark = so->so_rcv.sb_cc +
2720 (tp->rcv_up - tp->rcv_nxt) - 1; 2733 (tp->rcv_up - tp->rcv_nxt) - 1;
2721 if (so->so_oobmark == 0) 2734 if (so->so_oobmark == 0)
2722 so->so_state |= SS_RCVATMARK; 2735 so->so_state |= SS_RCVATMARK;
2723 sohasoutofband(so); 2736 sohasoutofband(so);
2724 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2737 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2725 } 2738 }
2726 2739
2727 /* 2740 /*
2728 * Remove out of band data so doesn't get presented to user. 2741 * Remove out of band data so doesn't get presented to user.
2729 * This can happen independent of advancing the URG pointer, 2742 * This can happen independent of advancing the URG pointer,
2730 * but if two URG's are pending at once, some out-of-band 2743 * but if two URG's are pending at once, some out-of-band
2731 * data may creep in... ick. 2744 * data may creep in... ick.
2732 */ 2745 */
2733 if (th->th_urp <= (u_int16_t)tlen && 2746 if (th->th_urp <= (u_int16_t)tlen &&
2734 (so->so_options & SO_OOBINLINE) == 0) 2747 (so->so_options & SO_OOBINLINE) == 0)
2735 tcp_pulloutofband(so, th, m, hdroptlen); 2748 tcp_pulloutofband(so, th, m, hdroptlen);
2736 } else { 2749 } else {
2737 /* 2750 /*
2738 * If no out of band data is expected, 2751 * If no out of band data is expected,
2739 * pull receive urgent pointer along 2752 * pull receive urgent pointer along
2740 * with the receive window. 2753 * with the receive window.
2741 */ 2754 */
2742 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2755 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2743 tp->rcv_up = tp->rcv_nxt; 2756 tp->rcv_up = tp->rcv_nxt;
2744 } 2757 }
2745dodata: 2758dodata:
2746 2759
2747 /* 2760 /*
2748 * Process the segment text, merging it into the TCP sequencing queue, 2761 * Process the segment text, merging it into the TCP sequencing queue,
2749 * and arranging for acknowledgement of receipt if necessary. 2762 * and arranging for acknowledgement of receipt if necessary.
2750 * This process logically involves adjusting tp->rcv_wnd as data 2763 * This process logically involves adjusting tp->rcv_wnd as data
2751 * is presented to the user (this happens in tcp_usrreq.c, 2764 * is presented to the user (this happens in tcp_usrreq.c,
2752 * tcp_rcvd()). If a FIN has already been received on this 2765 * tcp_rcvd()). If a FIN has already been received on this
2753 * connection then we just ignore the text. 2766 * connection then we just ignore the text.
2754 */ 2767 */
2755 if ((tlen || (tiflags & TH_FIN)) && 2768 if ((tlen || (tiflags & TH_FIN)) &&
2756 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2769 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2757 /* 2770 /*
2758 * Handle the common case: 2771 * Handle the common case:
2759 * o Segment is the next to be received, and 2772 * o Segment is the next to be received, and
2760 * o The queue is empty, and 2773 * o The queue is empty, and
2761 * o The connection is established 2774 * o The connection is established
2762 * In this case, we avoid calling tcp_reass. 2775 * In this case, we avoid calling tcp_reass.
2763 * 2776 *
2764 * tcp_setup_ack: set DELACK for segments received in order, 2777 * tcp_setup_ack: set DELACK for segments received in order,
2765 * but ack immediately when segments are out of order (so that 2778 * but ack immediately when segments are out of order (so that
2766 * fast retransmit can work). 2779 * fast retransmit can work).
2767 */ 2780 */
2768 TCP_REASS_LOCK(tp); 2781 TCP_REASS_LOCK(tp);
2769 if (th->th_seq == tp->rcv_nxt && 2782 if (th->th_seq == tp->rcv_nxt &&
2770 TAILQ_FIRST(&tp->segq) == NULL && 2783 TAILQ_FIRST(&tp->segq) == NULL &&
2771 tp->t_state == TCPS_ESTABLISHED) { 2784 tp->t_state == TCPS_ESTABLISHED) {
2772 tcp_setup_ack(tp, th); 2785 tcp_setup_ack(tp, th);
2773 tp->rcv_nxt += tlen; 2786 tp->rcv_nxt += tlen;
2774 tiflags = th->th_flags & TH_FIN; 2787 tiflags = th->th_flags & TH_FIN;
2775 tcps = TCP_STAT_GETREF(); 2788 tcps = TCP_STAT_GETREF();
2776 tcps[TCP_STAT_RCVPACK]++; 2789 tcps[TCP_STAT_RCVPACK]++;
2777 tcps[TCP_STAT_RCVBYTE] += tlen; 2790 tcps[TCP_STAT_RCVBYTE] += tlen;
2778 TCP_STAT_PUTREF(); 2791 TCP_STAT_PUTREF();
2779 nd6_hint(tp); 2792 nd6_hint(tp);
2780 if (so->so_state & SS_CANTRCVMORE) { 2793 if (so->so_state & SS_CANTRCVMORE) {
2781 m_freem(m); 2794 m_freem(m);
2782 } else { 2795 } else {
2783 m_adj(m, hdroptlen); 2796 m_adj(m, hdroptlen);
2784 sbappendstream(&(so)->so_rcv, m); 2797 sbappendstream(&(so)->so_rcv, m);
2785 } 2798 }
2786 TCP_REASS_UNLOCK(tp); 2799 TCP_REASS_UNLOCK(tp);
2787 sorwakeup(so); 2800 sorwakeup(so);
2788 } else { 2801 } else {
2789 m_adj(m, hdroptlen); 2802 m_adj(m, hdroptlen);
2790 tiflags = tcp_reass(tp, th, m, tlen); 2803 tiflags = tcp_reass(tp, th, m, tlen);
2791 tp->t_flags |= TF_ACKNOW; 2804 tp->t_flags |= TF_ACKNOW;
2792 } 2805 }
2793 2806
2794 /* 2807 /*
2795 * Note the amount of data that peer has sent into 2808 * Note the amount of data that peer has sent into
2796 * our window, in order to estimate the sender's 2809 * our window, in order to estimate the sender's
2797 * buffer size. 2810 * buffer size.
2798 */ 2811 */
2799 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2812 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2800 } else { 2813 } else {
2801 m_freem(m); 2814 m_freem(m);
2802 m = NULL; 2815 m = NULL;
2803 tiflags &= ~TH_FIN; 2816 tiflags &= ~TH_FIN;
2804 } 2817 }
2805 2818
2806 /* 2819 /*
2807 * If FIN is received ACK the FIN and let the user know 2820 * If FIN is received ACK the FIN and let the user know
2808 * that the connection is closing. Ignore a FIN received before 2821 * that the connection is closing. Ignore a FIN received before
2809 * the connection is fully established. 2822 * the connection is fully established.
2810 */ 2823 */
2811 if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) { 2824 if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) {
2812 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2825 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2813 socantrcvmore(so); 2826 socantrcvmore(so);
2814 tp->t_flags |= TF_ACKNOW; 2827 tp->t_flags |= TF_ACKNOW;
2815 tp->rcv_nxt++; 2828 tp->rcv_nxt++;
2816 } 2829 }
2817 switch (tp->t_state) { 2830 switch (tp->t_state) {
2818 2831
2819 /* 2832 /*
2820 * In ESTABLISHED STATE enter the CLOSE_WAIT state. 2833 * In ESTABLISHED STATE enter the CLOSE_WAIT state.
2821 */ 2834 */
2822 case TCPS_ESTABLISHED: 2835 case TCPS_ESTABLISHED:
2823 tp->t_state = TCPS_CLOSE_WAIT; 2836 tp->t_state = TCPS_CLOSE_WAIT;
2824 break; 2837 break;
2825 2838
2826 /* 2839 /*
2827 * If still in FIN_WAIT_1 STATE FIN has not been acked so 2840 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2828 * enter the CLOSING state. 2841 * enter the CLOSING state.
2829 */ 2842 */
2830 case TCPS_FIN_WAIT_1: 2843 case TCPS_FIN_WAIT_1:
2831 tp->t_state = TCPS_CLOSING; 2844 tp->t_state = TCPS_CLOSING;
2832 break; 2845 break;
2833 2846
2834 /* 2847 /*
2835 * In FIN_WAIT_2 state enter the TIME_WAIT state, 2848 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2836 * starting the time-wait timer, turning off the other 2849 * starting the time-wait timer, turning off the other
2837 * standard timers. 2850 * standard timers.
2838 */ 2851 */
2839 case TCPS_FIN_WAIT_2: 2852 case TCPS_FIN_WAIT_2:
2840 tp->t_state = TCPS_TIME_WAIT; 2853 tp->t_state = TCPS_TIME_WAIT;
2841 tcp_canceltimers(tp); 2854 tcp_canceltimers(tp);
2842 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl); 2855 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl);
2843 soisdisconnected(so); 2856 soisdisconnected(so);
2844 break; 2857 break;
2845 2858
2846 /* 2859 /*
2847 * In TIME_WAIT state restart the 2 MSL time_wait timer. 2860 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2848 */ 2861 */
2849 case TCPS_TIME_WAIT: 2862 case TCPS_TIME_WAIT:
2850 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl); 2863 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl);
2851 break; 2864 break;
2852 } 2865 }
2853 } 2866 }
2854#ifdef TCP_DEBUG 2867#ifdef TCP_DEBUG
2855 if (so->so_options & SO_DEBUG) 2868 if (so->so_options & SO_DEBUG)
2856 tcp_trace(TA_INPUT, ostate, tp, tcp_saveti, 0); 2869 tcp_trace(TA_INPUT, ostate, tp, tcp_saveti, 0);
2857#endif 2870#endif
2858 2871
2859 /* 2872 /*
2860 * Return any desired output. 2873 * Return any desired output.
2861 */ 2874 */
2862 if (needoutput || (tp->t_flags & TF_ACKNOW)) { 2875 if (needoutput || (tp->t_flags & TF_ACKNOW)) {
2863 KERNEL_LOCK(1, NULL); 2876 KERNEL_LOCK(1, NULL);
2864 (void)tcp_output(tp); 2877 (void)tcp_output(tp);
2865 KERNEL_UNLOCK_ONE(NULL); 2878 KERNEL_UNLOCK_ONE(NULL);
2866 } 2879 }
2867 if (tcp_saveti) 2880 if (tcp_saveti)
2868 m_freem(tcp_saveti); 2881 m_freem(tcp_saveti);
2869 2882
2870 if (tp->t_state == TCPS_TIME_WAIT 2883 if (tp->t_state == TCPS_TIME_WAIT
2871 && (so->so_state & SS_NOFDREF) 2884 && (so->so_state & SS_NOFDREF)
2872 && (tp->t_inpcb || af != AF_INET) 2885 && (tp->t_inpcb || af != AF_INET)
2873 && (tp->t_in6pcb || af != AF_INET6) 2886 && (tp->t_in6pcb || af != AF_INET6)
2874 && ((af == AF_INET ? tcp4_vtw_enable : tcp6_vtw_enable) & 1) != 0 2887 && ((af == AF_INET ? tcp4_vtw_enable : tcp6_vtw_enable) & 1) != 0
2875 && TAILQ_EMPTY(&tp->segq) 2888 && TAILQ_EMPTY(&tp->segq)
2876 && vtw_add(af, tp)) { 2889 && vtw_add(af, tp)) {
2877 ; 2890 ;
2878 } 2891 }
2879 return; 2892 return;
2880 2893
2881badsyn: 2894badsyn:
2882 /* 2895 /*
2883 * Received a bad SYN. Increment counters and dropwithreset. 2896 * Received a bad SYN. Increment counters and dropwithreset.
2884 */ 2897 */
2885 TCP_STATINC(TCP_STAT_BADSYN); 2898 TCP_STATINC(TCP_STAT_BADSYN);
2886 tp = NULL; 2899 tp = NULL;
2887 goto dropwithreset; 2900 goto dropwithreset;
2888 2901
2889dropafterack: 2902dropafterack:
2890 /* 2903 /*
2891 * Generate an ACK dropping incoming segment if it occupies 2904 * Generate an ACK dropping incoming segment if it occupies
2892 * sequence space, where the ACK reflects our state. 2905 * sequence space, where the ACK reflects our state.
2893 */ 2906 */
2894 if (tiflags & TH_RST) 2907 if (tiflags & TH_RST)
2895 goto drop; 2908 goto drop;
2896 goto dropafterack2; 2909 goto dropafterack2;
2897 2910
2898dropafterack_ratelim: 2911dropafterack_ratelim: