Sat Jun 20 17:29:31 2009 UTC ()
Follow exactly the recommendation of draft-ietf-tcpm-tcpsecure-11.txt:
Don't check gainst the last ack received, but the expected sequence number.
This makes RST handling independent of delayed ACK. From Joanne M Mikkelson.


(christos)
diff -r1.295 -r1.296 src/sys/netinet/tcp_input.c

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

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