Fri Jul 24 04:33:50 2015 UTC ()
If we are sending a window probe and there's unacked data in the socket, make
sure at least the persist timer is running.


(matt)
diff -r1.183 -r1.184 src/sys/netinet/tcp_output.c

cvs diff -r1.183 -r1.184 src/sys/netinet/tcp_output.c (switch to unified diff)

--- src/sys/netinet/tcp_output.c 2015/05/16 01:15:34 1.183
+++ src/sys/netinet/tcp_output.c 2015/07/24 04:33:50 1.184
@@ -1,1716 +1,1726 @@ @@ -1,1716 +1,1726 @@
1/* $NetBSD: tcp_output.c,v 1.183 2015/05/16 01:15:34 kefren Exp $ */ 1/* $NetBSD: tcp_output.c,v 1.184 2015/07/24 04:33:50 matt 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, 2001, 2005, 2006 The NetBSD Foundation, Inc. 73 * Copyright (c) 1997, 1998, 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, 1995 107 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 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_output.c 8.4 (Berkeley) 5/24/95 134 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
135 */ 135 */
136 136
137#include <sys/cdefs.h> 137#include <sys/cdefs.h>
138__KERNEL_RCSID(0, "$NetBSD: tcp_output.c,v 1.183 2015/05/16 01:15:34 kefren Exp $"); 138__KERNEL_RCSID(0, "$NetBSD: tcp_output.c,v 1.184 2015/07/24 04:33:50 matt Exp $");
139 139
140#include "opt_inet.h" 140#include "opt_inet.h"
141#include "opt_ipsec.h" 141#include "opt_ipsec.h"
142#include "opt_tcp_debug.h" 142#include "opt_tcp_debug.h"
143 143
144#include <sys/param.h> 144#include <sys/param.h>
145#include <sys/systm.h> 145#include <sys/systm.h>
146#include <sys/mbuf.h> 146#include <sys/mbuf.h>
147#include <sys/protosw.h> 147#include <sys/protosw.h>
148#include <sys/socket.h> 148#include <sys/socket.h>
149#include <sys/socketvar.h> 149#include <sys/socketvar.h>
150#include <sys/errno.h> 150#include <sys/errno.h>
151#include <sys/domain.h> 151#include <sys/domain.h>
152#include <sys/kernel.h> 152#include <sys/kernel.h>
153#ifdef TCP_SIGNATURE 153#ifdef TCP_SIGNATURE
154#include <sys/md5.h> 154#include <sys/md5.h>
155#endif 155#endif
156 156
157#include <net/if.h> 157#include <net/if.h>
158#include <net/route.h> 158#include <net/route.h>
159 159
160#include <netinet/in.h> 160#include <netinet/in.h>
161#include <netinet/in_systm.h> 161#include <netinet/in_systm.h>
162#include <netinet/ip.h> 162#include <netinet/ip.h>
163#include <netinet/in_pcb.h> 163#include <netinet/in_pcb.h>
164#include <netinet/ip_var.h> 164#include <netinet/ip_var.h>
165 165
166#ifdef INET6 166#ifdef INET6
167#ifndef INET 167#ifndef INET
168#include <netinet/in.h> 168#include <netinet/in.h>
169#endif 169#endif
170#include <netinet/ip6.h> 170#include <netinet/ip6.h>
171#include <netinet6/in6_var.h> 171#include <netinet6/in6_var.h>
172#include <netinet6/ip6_var.h> 172#include <netinet6/ip6_var.h>
173#include <netinet6/in6_pcb.h> 173#include <netinet6/in6_pcb.h>
174#include <netinet6/nd6.h> 174#include <netinet6/nd6.h>
175#endif 175#endif
176 176
177#ifdef IPSEC 177#ifdef IPSEC
178#include <netipsec/ipsec.h> 178#include <netipsec/ipsec.h>
179#include <netipsec/key.h> 179#include <netipsec/key.h>
180#ifdef INET6 180#ifdef INET6
181#include <netipsec/ipsec6.h> 181#include <netipsec/ipsec6.h>
182#endif 182#endif
183#endif /* IPSEC*/ 183#endif /* IPSEC*/
184 184
185#include <netinet/tcp.h> 185#include <netinet/tcp.h>
186#define TCPOUTFLAGS 186#define TCPOUTFLAGS
187#include <netinet/tcp_fsm.h> 187#include <netinet/tcp_fsm.h>
188#include <netinet/tcp_seq.h> 188#include <netinet/tcp_seq.h>
189#include <netinet/tcp_timer.h> 189#include <netinet/tcp_timer.h>
190#include <netinet/tcp_var.h> 190#include <netinet/tcp_var.h>
191#include <netinet/tcp_private.h> 191#include <netinet/tcp_private.h>
192#include <netinet/tcp_congctl.h> 192#include <netinet/tcp_congctl.h>
193#include <netinet/tcpip.h> 193#include <netinet/tcpip.h>
194#include <netinet/tcp_debug.h> 194#include <netinet/tcp_debug.h>
195#include <netinet/in_offload.h> 195#include <netinet/in_offload.h>
196#include <netinet6/in6_offload.h> 196#include <netinet6/in6_offload.h>
197 197
198#ifdef notyet 198#ifdef notyet
199extern struct mbuf *m_copypack(); 199extern struct mbuf *m_copypack();
200#endif 200#endif
201 201
202/* 202/*
203 * Knob to enable Congestion Window Monitoring, and control 203 * Knob to enable Congestion Window Monitoring, and control
204 * the burst size it allows. Default burst is 4 packets, per 204 * the burst size it allows. Default burst is 4 packets, per
205 * the Internet draft. 205 * the Internet draft.
206 */ 206 */
207int tcp_cwm = 0; 207int tcp_cwm = 0;
208int tcp_cwm_burstsize = 4; 208int tcp_cwm_burstsize = 4;
209 209
210int tcp_do_autosndbuf = 1; 210int tcp_do_autosndbuf = 1;
211int tcp_autosndbuf_inc = 8 * 1024; 211int tcp_autosndbuf_inc = 8 * 1024;
212int tcp_autosndbuf_max = 256 * 1024; 212int tcp_autosndbuf_max = 256 * 1024;
213 213
214#ifdef TCP_OUTPUT_COUNTERS 214#ifdef TCP_OUTPUT_COUNTERS
215#include <sys/device.h> 215#include <sys/device.h>
216 216
217extern struct evcnt tcp_output_bigheader; 217extern struct evcnt tcp_output_bigheader;
218extern struct evcnt tcp_output_predict_hit; 218extern struct evcnt tcp_output_predict_hit;
219extern struct evcnt tcp_output_predict_miss; 219extern struct evcnt tcp_output_predict_miss;
220extern struct evcnt tcp_output_copysmall; 220extern struct evcnt tcp_output_copysmall;
221extern struct evcnt tcp_output_copybig; 221extern struct evcnt tcp_output_copybig;
222extern struct evcnt tcp_output_refbig; 222extern struct evcnt tcp_output_refbig;
223 223
224#define TCP_OUTPUT_COUNTER_INCR(ev) (ev)->ev_count++ 224#define TCP_OUTPUT_COUNTER_INCR(ev) (ev)->ev_count++
225#else 225#else
226 226
227#define TCP_OUTPUT_COUNTER_INCR(ev) /* nothing */ 227#define TCP_OUTPUT_COUNTER_INCR(ev) /* nothing */
228 228
229#endif /* TCP_OUTPUT_COUNTERS */ 229#endif /* TCP_OUTPUT_COUNTERS */
230 230
231static 231static
232#ifndef GPROF 232#ifndef GPROF
233inline 233inline
234#endif 234#endif
235int 235int
236tcp_segsize(struct tcpcb *tp, int *txsegsizep, int *rxsegsizep, 236tcp_segsize(struct tcpcb *tp, int *txsegsizep, int *rxsegsizep,
237 bool *alwaysfragp) 237 bool *alwaysfragp)
238{ 238{
239#ifdef INET 239#ifdef INET
240 struct inpcb *inp = tp->t_inpcb; 240 struct inpcb *inp = tp->t_inpcb;
241#endif 241#endif
242#ifdef INET6 242#ifdef INET6
243 struct in6pcb *in6p = tp->t_in6pcb; 243 struct in6pcb *in6p = tp->t_in6pcb;
244#endif 244#endif
245 struct socket *so = NULL; 245 struct socket *so = NULL;
246 struct rtentry *rt; 246 struct rtentry *rt;
247 struct ifnet *ifp; 247 struct ifnet *ifp;
248 int size; 248 int size;
249 int hdrlen; 249 int hdrlen;
250 int optlen; 250 int optlen;
251 251
252 *alwaysfragp = false; 252 *alwaysfragp = false;
253 253
254#ifdef DIAGNOSTIC 254#ifdef DIAGNOSTIC
255 if (tp->t_inpcb && tp->t_in6pcb) 255 if (tp->t_inpcb && tp->t_in6pcb)
256 panic("tcp_segsize: both t_inpcb and t_in6pcb are set"); 256 panic("tcp_segsize: both t_inpcb and t_in6pcb are set");
257#endif 257#endif
258 switch (tp->t_family) { 258 switch (tp->t_family) {
259#ifdef INET 259#ifdef INET
260 case AF_INET: 260 case AF_INET:
261 hdrlen = sizeof(struct ip) + sizeof(struct tcphdr); 261 hdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
262 break; 262 break;
263#endif 263#endif
264#ifdef INET6 264#ifdef INET6
265 case AF_INET6: 265 case AF_INET6:
266 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 266 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
267 break; 267 break;
268#endif 268#endif
269 default: 269 default:
270 size = tcp_mssdflt; 270 size = tcp_mssdflt;
271 goto out; 271 goto out;
272 } 272 }
273 273
274 rt = NULL; 274 rt = NULL;
275#ifdef INET 275#ifdef INET
276 if (inp) { 276 if (inp) {
277 rt = in_pcbrtentry(inp); 277 rt = in_pcbrtentry(inp);
278 so = inp->inp_socket; 278 so = inp->inp_socket;
279 } 279 }
280#endif 280#endif
281#ifdef INET6 281#ifdef INET6
282 if (in6p) { 282 if (in6p) {
283 rt = in6_pcbrtentry(in6p); 283 rt = in6_pcbrtentry(in6p);
284 so = in6p->in6p_socket; 284 so = in6p->in6p_socket;
285 } 285 }
286#endif 286#endif
287 if (rt == NULL) { 287 if (rt == NULL) {
288 size = tcp_mssdflt; 288 size = tcp_mssdflt;
289 goto out; 289 goto out;
290 } 290 }
291 291
292 ifp = rt->rt_ifp; 292 ifp = rt->rt_ifp;
293 293
294 size = tcp_mssdflt; 294 size = tcp_mssdflt;
295 if (tp->t_mtudisc && rt->rt_rmx.rmx_mtu != 0) { 295 if (tp->t_mtudisc && rt->rt_rmx.rmx_mtu != 0) {
296#ifdef INET6 296#ifdef INET6
297 if (in6p && rt->rt_rmx.rmx_mtu < IPV6_MMTU) { 297 if (in6p && rt->rt_rmx.rmx_mtu < IPV6_MMTU) {
298 /* 298 /*
299 * RFC2460 section 5, last paragraph: if path MTU is 299 * RFC2460 section 5, last paragraph: if path MTU is
300 * smaller than 1280, use 1280 as packet size and 300 * smaller than 1280, use 1280 as packet size and
301 * attach fragment header. 301 * attach fragment header.
302 */ 302 */
303 size = IPV6_MMTU - hdrlen - sizeof(struct ip6_frag); 303 size = IPV6_MMTU - hdrlen - sizeof(struct ip6_frag);
304 *alwaysfragp = true; 304 *alwaysfragp = true;
305 } else 305 } else
306 size = rt->rt_rmx.rmx_mtu - hdrlen; 306 size = rt->rt_rmx.rmx_mtu - hdrlen;
307#else 307#else
308 size = rt->rt_rmx.rmx_mtu - hdrlen; 308 size = rt->rt_rmx.rmx_mtu - hdrlen;
309#endif 309#endif
310 } else if (ifp->if_flags & IFF_LOOPBACK) 310 } else if (ifp->if_flags & IFF_LOOPBACK)
311 size = ifp->if_mtu - hdrlen; 311 size = ifp->if_mtu - hdrlen;
312#ifdef INET 312#ifdef INET
313 else if (inp && tp->t_mtudisc) 313 else if (inp && tp->t_mtudisc)
314 size = ifp->if_mtu - hdrlen; 314 size = ifp->if_mtu - hdrlen;
315 else if (inp && in_localaddr(inp->inp_faddr)) 315 else if (inp && in_localaddr(inp->inp_faddr))
316 size = ifp->if_mtu - hdrlen; 316 size = ifp->if_mtu - hdrlen;
317#endif 317#endif
318#ifdef INET6 318#ifdef INET6
319 else if (in6p) { 319 else if (in6p) {
320#ifdef INET 320#ifdef INET
321 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) { 321 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
322 /* mapped addr case */ 322 /* mapped addr case */
323 struct in_addr d; 323 struct in_addr d;
324 bcopy(&in6p->in6p_faddr.s6_addr32[3], &d, sizeof(d)); 324 bcopy(&in6p->in6p_faddr.s6_addr32[3], &d, sizeof(d));
325 if (tp->t_mtudisc || in_localaddr(d)) 325 if (tp->t_mtudisc || in_localaddr(d))
326 size = ifp->if_mtu - hdrlen; 326 size = ifp->if_mtu - hdrlen;
327 } else 327 } else
328#endif 328#endif
329 { 329 {
330 /* 330 /*
331 * for IPv6, path MTU discovery is always turned on, 331 * for IPv6, path MTU discovery is always turned on,
332 * or the node must use packet size <= 1280. 332 * or the node must use packet size <= 1280.
333 */ 333 */
334 size = tp->t_mtudisc ? IN6_LINKMTU(ifp) : IPV6_MMTU; 334 size = tp->t_mtudisc ? IN6_LINKMTU(ifp) : IPV6_MMTU;
335 size -= hdrlen; 335 size -= hdrlen;
336 } 336 }
337 } 337 }
338#endif 338#endif
339 out: 339 out:
340 /* 340 /*
341 * Now we must make room for whatever extra TCP/IP options are in 341 * Now we must make room for whatever extra TCP/IP options are in
342 * the packet. 342 * the packet.
343 */ 343 */
344 optlen = tcp_optlen(tp); 344 optlen = tcp_optlen(tp);
345 345
346 /* 346 /*
347 * XXX tp->t_ourmss should have the right size, but without this code 347 * XXX tp->t_ourmss should have the right size, but without this code
348 * fragmentation will occur... need more investigation 348 * fragmentation will occur... need more investigation
349 */ 349 */
350#ifdef INET 350#ifdef INET
351 if (inp) { 351 if (inp) {
352#if defined(IPSEC) 352#if defined(IPSEC)
353 if (ipsec_used && 353 if (ipsec_used &&
354 !IPSEC_PCB_SKIP_IPSEC(inp->inp_sp, IPSEC_DIR_OUTBOUND)) 354 !IPSEC_PCB_SKIP_IPSEC(inp->inp_sp, IPSEC_DIR_OUTBOUND))
355 optlen += ipsec4_hdrsiz_tcp(tp); 355 optlen += ipsec4_hdrsiz_tcp(tp);
356#endif 356#endif
357 optlen += ip_optlen(inp); 357 optlen += ip_optlen(inp);
358 } 358 }
359#endif 359#endif
360#ifdef INET6 360#ifdef INET6
361#ifdef INET 361#ifdef INET
362 if (in6p && tp->t_family == AF_INET) { 362 if (in6p && tp->t_family == AF_INET) {
363#if defined(IPSEC) 363#if defined(IPSEC)
364 if (ipsec_used && 364 if (ipsec_used &&
365 !IPSEC_PCB_SKIP_IPSEC(in6p->in6p_sp, IPSEC_DIR_OUTBOUND)) 365 !IPSEC_PCB_SKIP_IPSEC(in6p->in6p_sp, IPSEC_DIR_OUTBOUND))
366 optlen += ipsec4_hdrsiz_tcp(tp); 366 optlen += ipsec4_hdrsiz_tcp(tp);
367#endif 367#endif
368 /* XXX size -= ip_optlen(in6p); */ 368 /* XXX size -= ip_optlen(in6p); */
369 } else 369 } else
370#endif 370#endif
371 if (in6p && tp->t_family == AF_INET6) { 371 if (in6p && tp->t_family == AF_INET6) {
372#if defined(IPSEC) 372#if defined(IPSEC)
373 if (ipsec_used && 373 if (ipsec_used &&
374 !IPSEC_PCB_SKIP_IPSEC(in6p->in6p_sp, IPSEC_DIR_OUTBOUND)) 374 !IPSEC_PCB_SKIP_IPSEC(in6p->in6p_sp, IPSEC_DIR_OUTBOUND))
375 optlen += ipsec6_hdrsiz_tcp(tp); 375 optlen += ipsec6_hdrsiz_tcp(tp);
376#endif 376#endif
377 optlen += ip6_optlen(in6p); 377 optlen += ip6_optlen(in6p);
378 } 378 }
379#endif 379#endif
380 size -= optlen; 380 size -= optlen;
381 381
382 /* there may not be any room for data if mtu is too small */ 382 /* there may not be any room for data if mtu is too small */
383 if (size < 0) 383 if (size < 0)
384 return (EMSGSIZE); 384 return (EMSGSIZE);
385 385
386 /* 386 /*
387 * *rxsegsizep holds *estimated* inbound segment size (estimation 387 * *rxsegsizep holds *estimated* inbound segment size (estimation
388 * assumes that path MTU is the same for both ways). this is only 388 * assumes that path MTU is the same for both ways). this is only
389 * for silly window avoidance, do not use the value for other purposes. 389 * for silly window avoidance, do not use the value for other purposes.
390 * 390 *
391 * ipseclen is subtracted from both sides, this may not be right. 391 * ipseclen is subtracted from both sides, this may not be right.
392 * I'm not quite sure about this (could someone comment). 392 * I'm not quite sure about this (could someone comment).
393 */ 393 */
394 *txsegsizep = min(tp->t_peermss - optlen, size); 394 *txsegsizep = min(tp->t_peermss - optlen, size);
395 /* 395 /*
396 * Never send more than half a buffer full. This insures that we can 396 * Never send more than half a buffer full. This insures that we can
397 * always keep 2 packets on the wire, no matter what SO_SNDBUF is, and 397 * always keep 2 packets on the wire, no matter what SO_SNDBUF is, and
398 * therefore acks will never be delayed unless we run out of data to 398 * therefore acks will never be delayed unless we run out of data to
399 * transmit. 399 * transmit.
400 */ 400 */
401 if (so) 401 if (so)
402 *txsegsizep = min(so->so_snd.sb_hiwat >> 1, *txsegsizep); 402 *txsegsizep = min(so->so_snd.sb_hiwat >> 1, *txsegsizep);
403 *rxsegsizep = min(tp->t_ourmss - optlen, size); 403 *rxsegsizep = min(tp->t_ourmss - optlen, size);
404 404
405 if (*txsegsizep != tp->t_segsz) { 405 if (*txsegsizep != tp->t_segsz) {
406 /* 406 /*
407 * If the new segment size is larger, we don't want to 407 * If the new segment size is larger, we don't want to
408 * mess up the congestion window, but if it is smaller 408 * mess up the congestion window, but if it is smaller
409 * we'll have to reduce the congestion window to ensure 409 * we'll have to reduce the congestion window to ensure
410 * that we don't get into trouble with initial windows 410 * that we don't get into trouble with initial windows
411 * and the rest. In any case, if the segment size 411 * and the rest. In any case, if the segment size
412 * has changed, chances are the path has, too, and 412 * has changed, chances are the path has, too, and
413 * our congestion window will be different. 413 * our congestion window will be different.
414 */ 414 */
415 if (*txsegsizep < tp->t_segsz) { 415 if (*txsegsizep < tp->t_segsz) {
416 tp->snd_cwnd = max((tp->snd_cwnd / tp->t_segsz) 416 tp->snd_cwnd = max((tp->snd_cwnd / tp->t_segsz)
417 * *txsegsizep, *txsegsizep); 417 * *txsegsizep, *txsegsizep);
418 tp->snd_ssthresh = max((tp->snd_ssthresh / tp->t_segsz) 418 tp->snd_ssthresh = max((tp->snd_ssthresh / tp->t_segsz)
419 * *txsegsizep, *txsegsizep); 419 * *txsegsizep, *txsegsizep);
420 } 420 }
421 tp->t_segsz = *txsegsizep; 421 tp->t_segsz = *txsegsizep;
422 } 422 }
423 423
424 return (0); 424 return (0);
425} 425}
426 426
427static 427static
428#ifndef GPROF 428#ifndef GPROF
429inline 429inline
430#endif 430#endif
431int 431int
432tcp_build_datapkt(struct tcpcb *tp, struct socket *so, int off, 432tcp_build_datapkt(struct tcpcb *tp, struct socket *so, int off,
433 long len, int hdrlen, struct mbuf **mp) 433 long len, int hdrlen, struct mbuf **mp)
434{ 434{
435 struct mbuf *m, *m0; 435 struct mbuf *m, *m0;
436 uint64_t *tcps; 436 uint64_t *tcps;
437 437
438 tcps = TCP_STAT_GETREF(); 438 tcps = TCP_STAT_GETREF();
439 if (tp->t_force && len == 1) 439 if (tp->t_force && len == 1)
440 tcps[TCP_STAT_SNDPROBE]++; 440 tcps[TCP_STAT_SNDPROBE]++;
441 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 441 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
442 tp->t_sndrexmitpack++; 442 tp->t_sndrexmitpack++;
443 tcps[TCP_STAT_SNDREXMITPACK]++; 443 tcps[TCP_STAT_SNDREXMITPACK]++;
444 tcps[TCP_STAT_SNDREXMITBYTE] += len; 444 tcps[TCP_STAT_SNDREXMITBYTE] += len;
445 } else { 445 } else {
446 tcps[TCP_STAT_SNDPACK]++; 446 tcps[TCP_STAT_SNDPACK]++;
447 tcps[TCP_STAT_SNDBYTE] += len; 447 tcps[TCP_STAT_SNDBYTE] += len;
448 } 448 }
449 TCP_STAT_PUTREF(); 449 TCP_STAT_PUTREF();
450#ifdef notyet 450#ifdef notyet
451 if ((m = m_copypack(so->so_snd.sb_mb, off, 451 if ((m = m_copypack(so->so_snd.sb_mb, off,
452 (int)len, max_linkhdr + hdrlen)) == 0) 452 (int)len, max_linkhdr + hdrlen)) == 0)
453 return (ENOBUFS); 453 return (ENOBUFS);
454 /* 454 /*
455 * m_copypack left space for our hdr; use it. 455 * m_copypack left space for our hdr; use it.
456 */ 456 */
457 m->m_len += hdrlen; 457 m->m_len += hdrlen;
458 m->m_data -= hdrlen; 458 m->m_data -= hdrlen;
459#else 459#else
460 MGETHDR(m, M_DONTWAIT, MT_HEADER); 460 MGETHDR(m, M_DONTWAIT, MT_HEADER);
461 if (__predict_false(m == NULL)) 461 if (__predict_false(m == NULL))
462 return (ENOBUFS); 462 return (ENOBUFS);
463 MCLAIM(m, &tcp_tx_mowner); 463 MCLAIM(m, &tcp_tx_mowner);
464 464
465 /* 465 /*
466 * XXX Because other code assumes headers will fit in 466 * XXX Because other code assumes headers will fit in
467 * XXX one header mbuf. 467 * XXX one header mbuf.
468 * 468 *
469 * (This code should almost *never* be run.) 469 * (This code should almost *never* be run.)
470 */ 470 */
471 if (__predict_false((max_linkhdr + hdrlen) > MHLEN)) { 471 if (__predict_false((max_linkhdr + hdrlen) > MHLEN)) {
472 TCP_OUTPUT_COUNTER_INCR(&tcp_output_bigheader); 472 TCP_OUTPUT_COUNTER_INCR(&tcp_output_bigheader);
473 MCLGET(m, M_DONTWAIT); 473 MCLGET(m, M_DONTWAIT);
474 if ((m->m_flags & M_EXT) == 0) { 474 if ((m->m_flags & M_EXT) == 0) {
475 m_freem(m); 475 m_freem(m);
476 return (ENOBUFS); 476 return (ENOBUFS);
477 } 477 }
478 } 478 }
479 479
480 m->m_data += max_linkhdr; 480 m->m_data += max_linkhdr;
481 m->m_len = hdrlen; 481 m->m_len = hdrlen;
482 482
483 /* 483 /*
484 * To avoid traversing the whole sb_mb chain for correct 484 * To avoid traversing the whole sb_mb chain for correct
485 * data to send, remember last sent mbuf, its offset and 485 * data to send, remember last sent mbuf, its offset and
486 * the sent size. When called the next time, see if the 486 * the sent size. When called the next time, see if the
487 * data to send is directly following the previous transfer. 487 * data to send is directly following the previous transfer.
488 * This is important for large TCP windows. 488 * This is important for large TCP windows.
489 */ 489 */
490 if (off == 0 || tp->t_lastm == NULL || 490 if (off == 0 || tp->t_lastm == NULL ||
491 (tp->t_lastoff + tp->t_lastlen) != off) { 491 (tp->t_lastoff + tp->t_lastlen) != off) {
492 TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_miss); 492 TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_miss);
493 /* 493 /*
494 * Either a new packet or a retransmit. 494 * Either a new packet or a retransmit.
495 * Start from the beginning. 495 * Start from the beginning.
496 */ 496 */
497 tp->t_lastm = so->so_snd.sb_mb; 497 tp->t_lastm = so->so_snd.sb_mb;
498 tp->t_inoff = off; 498 tp->t_inoff = off;
499 } else { 499 } else {
500 TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_hit); 500 TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_hit);
501 tp->t_inoff += tp->t_lastlen; 501 tp->t_inoff += tp->t_lastlen;
502 } 502 }
503 503
504 /* Traverse forward to next packet */ 504 /* Traverse forward to next packet */
505 while (tp->t_inoff > 0) { 505 while (tp->t_inoff > 0) {
506 if (tp->t_lastm == NULL) 506 if (tp->t_lastm == NULL)
507 panic("tp->t_lastm == NULL"); 507 panic("tp->t_lastm == NULL");
508 if (tp->t_inoff < tp->t_lastm->m_len) 508 if (tp->t_inoff < tp->t_lastm->m_len)
509 break; 509 break;
510 tp->t_inoff -= tp->t_lastm->m_len; 510 tp->t_inoff -= tp->t_lastm->m_len;
511 tp->t_lastm = tp->t_lastm->m_next; 511 tp->t_lastm = tp->t_lastm->m_next;
512 } 512 }
513 513
514 tp->t_lastoff = off; 514 tp->t_lastoff = off;
515 tp->t_lastlen = len; 515 tp->t_lastlen = len;
516 m0 = tp->t_lastm; 516 m0 = tp->t_lastm;
517 off = tp->t_inoff; 517 off = tp->t_inoff;
518 518
519 if (len <= M_TRAILINGSPACE(m)) { 519 if (len <= M_TRAILINGSPACE(m)) {
520 m_copydata(m0, off, (int) len, mtod(m, char *) + hdrlen); 520 m_copydata(m0, off, (int) len, mtod(m, char *) + hdrlen);
521 m->m_len += len; 521 m->m_len += len;
522 TCP_OUTPUT_COUNTER_INCR(&tcp_output_copysmall); 522 TCP_OUTPUT_COUNTER_INCR(&tcp_output_copysmall);
523 } else { 523 } else {
524 m->m_next = m_copym(m0, off, (int) len, M_DONTWAIT); 524 m->m_next = m_copym(m0, off, (int) len, M_DONTWAIT);
525 if (m->m_next == NULL) { 525 if (m->m_next == NULL) {
526 m_freem(m); 526 m_freem(m);
527 return (ENOBUFS); 527 return (ENOBUFS);
528 } 528 }
529#ifdef TCP_OUTPUT_COUNTERS 529#ifdef TCP_OUTPUT_COUNTERS
530 if (m->m_next->m_flags & M_EXT) 530 if (m->m_next->m_flags & M_EXT)
531 TCP_OUTPUT_COUNTER_INCR(&tcp_output_refbig); 531 TCP_OUTPUT_COUNTER_INCR(&tcp_output_refbig);
532 else 532 else
533 TCP_OUTPUT_COUNTER_INCR(&tcp_output_copybig); 533 TCP_OUTPUT_COUNTER_INCR(&tcp_output_copybig);
534#endif /* TCP_OUTPUT_COUNTERS */ 534#endif /* TCP_OUTPUT_COUNTERS */
535 } 535 }
536#endif 536#endif
537 537
538 *mp = m; 538 *mp = m;
539 return (0); 539 return (0);
540} 540}
541 541
542/* 542/*
543 * Tcp output routine: figure out what should be sent and send it. 543 * Tcp output routine: figure out what should be sent and send it.
544 */ 544 */
545int 545int
546tcp_output(struct tcpcb *tp) 546tcp_output(struct tcpcb *tp)
547{ 547{
548 struct rtentry *rt; 548 struct rtentry *rt;
549 struct socket *so; 549 struct socket *so;
550 struct route *ro; 550 struct route *ro;
551 long len, win; 551 long len, win;
552 int off, flags, error; 552 int off, flags, error;
553 struct mbuf *m; 553 struct mbuf *m;
554 struct ip *ip; 554 struct ip *ip;
555#ifdef INET6 555#ifdef INET6
556 struct ip6_hdr *ip6; 556 struct ip6_hdr *ip6;
557#endif 557#endif
558 struct tcphdr *th; 558 struct tcphdr *th;
559 u_char opt[MAX_TCPOPTLEN]; 559 u_char opt[MAX_TCPOPTLEN];
560#define OPT_FITS(more) ((optlen + (more)) < sizeof(opt)) 560#define OPT_FITS(more) ((optlen + (more)) < sizeof(opt))
561 unsigned optlen, hdrlen, packetlen; 561 unsigned optlen, hdrlen, packetlen;
562 unsigned int sack_numblks; 562 unsigned int sack_numblks;
563 int idle, sendalot, txsegsize, rxsegsize; 563 int idle, sendalot, txsegsize, rxsegsize;
564 int txsegsize_nosack; 564 int txsegsize_nosack;
565 int maxburst = TCP_MAXBURST; 565 int maxburst = TCP_MAXBURST;
566 int af; /* address family on the wire */ 566 int af; /* address family on the wire */
567 int iphdrlen; 567 int iphdrlen;
568 int has_tso4, has_tso6; 568 int has_tso4, has_tso6;
569 int has_tso, use_tso; 569 int has_tso, use_tso;
570 bool alwaysfrag; 570 bool alwaysfrag;
571 int sack_rxmit; 571 int sack_rxmit;
572 int sack_bytes_rxmt; 572 int sack_bytes_rxmt;
573 int ecn_tos; 573 int ecn_tos;
574 struct sackhole *p; 574 struct sackhole *p;
575#ifdef TCP_SIGNATURE 575#ifdef TCP_SIGNATURE
576 int sigoff = 0; 576 int sigoff = 0;
577#endif 577#endif
578 uint64_t *tcps; 578 uint64_t *tcps;
579 579
580#ifdef DIAGNOSTIC 580#ifdef DIAGNOSTIC
581 if (tp->t_inpcb && tp->t_in6pcb) 581 if (tp->t_inpcb && tp->t_in6pcb)
582 panic("tcp_output: both t_inpcb and t_in6pcb are set"); 582 panic("tcp_output: both t_inpcb and t_in6pcb are set");
583#endif 583#endif
584 so = NULL; 584 so = NULL;
585 ro = NULL; 585 ro = NULL;
586 if (tp->t_inpcb) { 586 if (tp->t_inpcb) {
587 so = tp->t_inpcb->inp_socket; 587 so = tp->t_inpcb->inp_socket;
588 ro = &tp->t_inpcb->inp_route; 588 ro = &tp->t_inpcb->inp_route;
589 } 589 }
590#ifdef INET6 590#ifdef INET6
591 else if (tp->t_in6pcb) { 591 else if (tp->t_in6pcb) {
592 so = tp->t_in6pcb->in6p_socket; 592 so = tp->t_in6pcb->in6p_socket;
593 ro = &tp->t_in6pcb->in6p_route; 593 ro = &tp->t_in6pcb->in6p_route;
594 } 594 }
595#endif 595#endif
596 596
597 switch (af = tp->t_family) { 597 switch (af = tp->t_family) {
598#ifdef INET 598#ifdef INET
599 case AF_INET: 599 case AF_INET:
600 if (tp->t_inpcb) 600 if (tp->t_inpcb)
601 break; 601 break;
602#ifdef INET6 602#ifdef INET6
603 /* mapped addr case */ 603 /* mapped addr case */
604 if (tp->t_in6pcb) 604 if (tp->t_in6pcb)
605 break; 605 break;
606#endif 606#endif
607 return (EINVAL); 607 return (EINVAL);
608#endif 608#endif
609#ifdef INET6 609#ifdef INET6
610 case AF_INET6: 610 case AF_INET6:
611 if (tp->t_in6pcb) 611 if (tp->t_in6pcb)
612 break; 612 break;
613 return (EINVAL); 613 return (EINVAL);
614#endif 614#endif
615 default: 615 default:
616 return (EAFNOSUPPORT); 616 return (EAFNOSUPPORT);
617 } 617 }
618 618
619 if (tcp_segsize(tp, &txsegsize, &rxsegsize, &alwaysfrag)) 619 if (tcp_segsize(tp, &txsegsize, &rxsegsize, &alwaysfrag))
620 return (EMSGSIZE); 620 return (EMSGSIZE);
621 621
622 idle = (tp->snd_max == tp->snd_una); 622 idle = (tp->snd_max == tp->snd_una);
623 623
624 /* 624 /*
625 * Determine if we can use TCP segmentation offload: 625 * Determine if we can use TCP segmentation offload:
626 * - If we're using IPv4 626 * - If we're using IPv4
627 * - If there is not an IPsec policy that prevents it 627 * - If there is not an IPsec policy that prevents it
628 * - If the interface can do it 628 * - If the interface can do it
629 */ 629 */
630 has_tso4 = has_tso6 = false; 630 has_tso4 = has_tso6 = false;
631#if defined(INET) 631#if defined(INET)
632 has_tso4 = tp->t_inpcb != NULL && 632 has_tso4 = tp->t_inpcb != NULL &&
633#if defined(IPSEC) 633#if defined(IPSEC)
634 (!ipsec_used || IPSEC_PCB_SKIP_IPSEC(tp->t_inpcb->inp_sp, 634 (!ipsec_used || IPSEC_PCB_SKIP_IPSEC(tp->t_inpcb->inp_sp,
635 IPSEC_DIR_OUTBOUND)) && 635 IPSEC_DIR_OUTBOUND)) &&
636#endif 636#endif
637 (rt = rtcache_validate(&tp->t_inpcb->inp_route)) != NULL && 637 (rt = rtcache_validate(&tp->t_inpcb->inp_route)) != NULL &&
638 (rt->rt_ifp->if_capenable & IFCAP_TSOv4) != 0; 638 (rt->rt_ifp->if_capenable & IFCAP_TSOv4) != 0;
639#endif /* defined(INET) */ 639#endif /* defined(INET) */
640#if defined(INET6) 640#if defined(INET6)
641 has_tso6 = tp->t_in6pcb != NULL && 641 has_tso6 = tp->t_in6pcb != NULL &&
642#if defined(IPSEC) 642#if defined(IPSEC)
643 (!ipsec_used || IPSEC_PCB_SKIP_IPSEC(tp->t_in6pcb->in6p_sp, 643 (!ipsec_used || IPSEC_PCB_SKIP_IPSEC(tp->t_in6pcb->in6p_sp,
644 IPSEC_DIR_OUTBOUND)) && 644 IPSEC_DIR_OUTBOUND)) &&
645#endif 645#endif
646 (rt = rtcache_validate(&tp->t_in6pcb->in6p_route)) != NULL && 646 (rt = rtcache_validate(&tp->t_in6pcb->in6p_route)) != NULL &&
647 (rt->rt_ifp->if_capenable & IFCAP_TSOv6) != 0; 647 (rt->rt_ifp->if_capenable & IFCAP_TSOv6) != 0;
648#endif /* defined(INET6) */ 648#endif /* defined(INET6) */
649 has_tso = (has_tso4 || has_tso6) && !alwaysfrag; 649 has_tso = (has_tso4 || has_tso6) && !alwaysfrag;
650 650
651 /* 651 /*
652 * Restart Window computation. From draft-floyd-incr-init-win-03: 652 * Restart Window computation. From draft-floyd-incr-init-win-03:
653 * 653 *
654 * Optionally, a TCP MAY set the restart window to the 654 * Optionally, a TCP MAY set the restart window to the
655 * minimum of the value used for the initial window and 655 * minimum of the value used for the initial window and
656 * the current value of cwnd (in other words, using a 656 * the current value of cwnd (in other words, using a
657 * larger value for the restart window should never increase 657 * larger value for the restart window should never increase
658 * the size of cwnd). 658 * the size of cwnd).
659 */ 659 */
660 if (tcp_cwm) { 660 if (tcp_cwm) {
661 /* 661 /*
662 * Hughes/Touch/Heidemann Congestion Window Monitoring. 662 * Hughes/Touch/Heidemann Congestion Window Monitoring.
663 * Count the number of packets currently pending 663 * Count the number of packets currently pending
664 * acknowledgement, and limit our congestion window 664 * acknowledgement, and limit our congestion window
665 * to a pre-determined allowed burst size plus that count. 665 * to a pre-determined allowed burst size plus that count.
666 * This prevents bursting once all pending packets have 666 * This prevents bursting once all pending packets have
667 * been acknowledged (i.e. transmission is idle). 667 * been acknowledged (i.e. transmission is idle).
668 * 668 *
669 * XXX Link this to Initial Window? 669 * XXX Link this to Initial Window?
670 */ 670 */
671 tp->snd_cwnd = min(tp->snd_cwnd, 671 tp->snd_cwnd = min(tp->snd_cwnd,
672 (tcp_cwm_burstsize * txsegsize) + 672 (tcp_cwm_burstsize * txsegsize) +
673 (tp->snd_nxt - tp->snd_una)); 673 (tp->snd_nxt - tp->snd_una));
674 } else { 674 } else {
675 if (idle && (tcp_now - tp->t_rcvtime) >= tp->t_rxtcur) { 675 if (idle && (tcp_now - tp->t_rcvtime) >= tp->t_rxtcur) {
676 /* 676 /*
677 * We have been idle for "a while" and no acks are 677 * We have been idle for "a while" and no acks are
678 * expected to clock out any data we send -- 678 * expected to clock out any data we send --
679 * slow start to get ack "clock" running again. 679 * slow start to get ack "clock" running again.
680 */ 680 */
681 int ss = tcp_init_win; 681 int ss = tcp_init_win;
682#ifdef INET 682#ifdef INET
683 if (tp->t_inpcb && 683 if (tp->t_inpcb &&
684 in_localaddr(tp->t_inpcb->inp_faddr)) 684 in_localaddr(tp->t_inpcb->inp_faddr))
685 ss = tcp_init_win_local; 685 ss = tcp_init_win_local;
686#endif 686#endif
687#ifdef INET6 687#ifdef INET6
688 if (tp->t_in6pcb && 688 if (tp->t_in6pcb &&
689 in6_localaddr(&tp->t_in6pcb->in6p_faddr)) 689 in6_localaddr(&tp->t_in6pcb->in6p_faddr))
690 ss = tcp_init_win_local; 690 ss = tcp_init_win_local;
691#endif 691#endif
692 tp->snd_cwnd = min(tp->snd_cwnd, 692 tp->snd_cwnd = min(tp->snd_cwnd,
693 TCP_INITIAL_WINDOW(ss, txsegsize)); 693 TCP_INITIAL_WINDOW(ss, txsegsize));
694 } 694 }
695 } 695 }
696 696
697 txsegsize_nosack = txsegsize; 697 txsegsize_nosack = txsegsize;
698again: 698again:
699 ecn_tos = 0; 699 ecn_tos = 0;
700 use_tso = has_tso; 700 use_tso = has_tso;
701 if ((tp->t_flags & (TF_ECN_SND_CWR|TF_ECN_SND_ECE)) != 0) { 701 if ((tp->t_flags & (TF_ECN_SND_CWR|TF_ECN_SND_ECE)) != 0) {
702 /* don't duplicate CWR/ECE. */ 702 /* don't duplicate CWR/ECE. */
703 use_tso = 0; 703 use_tso = 0;
704 } 704 }
705 TCP_REASS_LOCK(tp); 705 TCP_REASS_LOCK(tp);
706 sack_numblks = tcp_sack_numblks(tp); 706 sack_numblks = tcp_sack_numblks(tp);
707 if (sack_numblks) { 707 if (sack_numblks) {
708 int sackoptlen; 708 int sackoptlen;
709 709
710 sackoptlen = TCP_SACK_OPTLEN(sack_numblks); 710 sackoptlen = TCP_SACK_OPTLEN(sack_numblks);
711 if (sackoptlen > txsegsize_nosack) { 711 if (sackoptlen > txsegsize_nosack) {
712 sack_numblks = 0; /* give up SACK */ 712 sack_numblks = 0; /* give up SACK */
713 txsegsize = txsegsize_nosack; 713 txsegsize = txsegsize_nosack;
714 } else { 714 } else {
715 if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) { 715 if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) {
716 /* don't duplicate D-SACK. */ 716 /* don't duplicate D-SACK. */
717 use_tso = 0; 717 use_tso = 0;
718 } 718 }
719 txsegsize = txsegsize_nosack - sackoptlen; 719 txsegsize = txsegsize_nosack - sackoptlen;
720 } 720 }
721 } else { 721 } else {
722 txsegsize = txsegsize_nosack; 722 txsegsize = txsegsize_nosack;
723 } 723 }
724 724
725 /* 725 /*
726 * Determine length of data that should be transmitted, and 726 * Determine length of data that should be transmitted, and
727 * flags that should be used. If there is some data or critical 727 * flags that should be used. If there is some data or critical
728 * controls (SYN, RST) to send, then transmit; otherwise, 728 * controls (SYN, RST) to send, then transmit; otherwise,
729 * investigate further. 729 * investigate further.
730 * 730 *
731 * Readjust SACK information to avoid resending duplicate data. 731 * Readjust SACK information to avoid resending duplicate data.
732 */ 732 */
733 if (TCP_SACK_ENABLED(tp) && SEQ_LT(tp->snd_nxt, tp->snd_max)) 733 if (TCP_SACK_ENABLED(tp) && SEQ_LT(tp->snd_nxt, tp->snd_max))
734 tcp_sack_adjust(tp); 734 tcp_sack_adjust(tp);
735 sendalot = 0; 735 sendalot = 0;
736 off = tp->snd_nxt - tp->snd_una; 736 off = tp->snd_nxt - tp->snd_una;
737 win = min(tp->snd_wnd, tp->snd_cwnd); 737 win = min(tp->snd_wnd, tp->snd_cwnd);
738 738
739 flags = tcp_outflags[tp->t_state]; 739 flags = tcp_outflags[tp->t_state];
740 740
741 /* 741 /*
742 * Send any SACK-generated retransmissions. If we're explicitly trying 742 * Send any SACK-generated retransmissions. If we're explicitly trying
743 * to send out new data (when sendalot is 1), bypass this function. 743 * to send out new data (when sendalot is 1), bypass this function.
744 * If we retransmit in fast recovery mode, decrement snd_cwnd, since 744 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
745 * we're replacing a (future) new transmission with a retransmission 745 * we're replacing a (future) new transmission with a retransmission
746 * now, and we previously incremented snd_cwnd in tcp_input(). 746 * now, and we previously incremented snd_cwnd in tcp_input().
747 */ 747 */
748 /* 748 /*
749 * Still in sack recovery , reset rxmit flag to zero. 749 * Still in sack recovery , reset rxmit flag to zero.
750 */ 750 */
751 sack_rxmit = 0; 751 sack_rxmit = 0;
752 sack_bytes_rxmt = 0; 752 sack_bytes_rxmt = 0;
753 len = 0; 753 len = 0;
754 p = NULL; 754 p = NULL;
755 do { 755 do {
756 long cwin; 756 long cwin;
757 if (!TCP_SACK_ENABLED(tp)) 757 if (!TCP_SACK_ENABLED(tp))
758 break; 758 break;
759 if (tp->t_partialacks < 0)  759 if (tp->t_partialacks < 0)
760 break; 760 break;
761 p = tcp_sack_output(tp, &sack_bytes_rxmt); 761 p = tcp_sack_output(tp, &sack_bytes_rxmt);
762 if (p == NULL) 762 if (p == NULL)
763 break; 763 break;
764  764
765 cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt; 765 cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
766 if (cwin < 0) 766 if (cwin < 0)
767 cwin = 0; 767 cwin = 0;
768 /* Do not retransmit SACK segments beyond snd_recover */ 768 /* Do not retransmit SACK segments beyond snd_recover */
769 if (SEQ_GT(p->end, tp->snd_recover)) { 769 if (SEQ_GT(p->end, tp->snd_recover)) {
770 /* 770 /*
771 * (At least) part of sack hole extends beyond 771 * (At least) part of sack hole extends beyond
772 * snd_recover. Check to see if we can rexmit data 772 * snd_recover. Check to see if we can rexmit data
773 * for this hole. 773 * for this hole.
774 */ 774 */
775 if (SEQ_GEQ(p->rxmit, tp->snd_recover)) { 775 if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
776 /* 776 /*
777 * Can't rexmit any more data for this hole. 777 * Can't rexmit any more data for this hole.
778 * That data will be rexmitted in the next 778 * That data will be rexmitted in the next
779 * sack recovery episode, when snd_recover 779 * sack recovery episode, when snd_recover
780 * moves past p->rxmit. 780 * moves past p->rxmit.
781 */ 781 */
782 p = NULL; 782 p = NULL;
783 break; 783 break;
784 } 784 }
785 /* Can rexmit part of the current hole */ 785 /* Can rexmit part of the current hole */
786 len = ((long)ulmin(cwin, tp->snd_recover - p->rxmit)); 786 len = ((long)ulmin(cwin, tp->snd_recover - p->rxmit));
787 } else 787 } else
788 len = ((long)ulmin(cwin, p->end - p->rxmit)); 788 len = ((long)ulmin(cwin, p->end - p->rxmit));
789 off = p->rxmit - tp->snd_una; 789 off = p->rxmit - tp->snd_una;
790 if (off + len > so->so_snd.sb_cc) { 790 if (off + len > so->so_snd.sb_cc) {
791 /* 1 for TH_FIN */ 791 /* 1 for TH_FIN */
792 KASSERT(off + len == so->so_snd.sb_cc + 1); 792 KASSERT(off + len == so->so_snd.sb_cc + 1);
793 KASSERT(p->rxmit + len == tp->snd_max); 793 KASSERT(p->rxmit + len == tp->snd_max);
794 len = so->so_snd.sb_cc - off; 794 len = so->so_snd.sb_cc - off;
795 } 795 }
796 if (len > 0) { 796 if (len > 0) {
797 sack_rxmit = 1; 797 sack_rxmit = 1;
798 sendalot = 1; 798 sendalot = 1;
799 } 799 }
800 } while (/*CONSTCOND*/0); 800 } while (/*CONSTCOND*/0);
801 801
802 /* 802 /*
803 * If in persist timeout with window of 0, send 1 byte. 803 * If in persist timeout with window of 0, send 1 byte.
804 * Otherwise, if window is small but nonzero 804 * Otherwise, if window is small but nonzero
805 * and timer expired, we will send what we can 805 * and timer expired, we will send what we can
806 * and go to transmit state. 806 * and go to transmit state.
807 */ 807 */
808 if (tp->t_force) { 808 if (tp->t_force) {
809 if (win == 0) { 809 if (win == 0) {
810 /* 810 /*
811 * If we still have some data to send, then 811 * If we still have some data to send, then
812 * clear the FIN bit. Usually this would 812 * clear the FIN bit. Usually this would
813 * happen below when it realizes that we 813 * happen below when it realizes that we
814 * aren't sending all the data. However, 814 * aren't sending all the data. However,
815 * if we have exactly 1 byte of unset data, 815 * if we have exactly 1 byte of unset data,
816 * then it won't clear the FIN bit below, 816 * then it won't clear the FIN bit below,
817 * and if we are in persist state, we wind 817 * and if we are in persist state, we wind
818 * up sending the packet without recording 818 * up sending the packet without recording
819 * that we sent the FIN bit. 819 * that we sent the FIN bit.
820 * 820 *
821 * We can't just blindly clear the FIN bit, 821 * We can't just blindly clear the FIN bit,
822 * because if we don't have any more data 822 * because if we don't have any more data
823 * to send then the probe will be the FIN 823 * to send then the probe will be the FIN
824 * itself. 824 * itself.
825 */ 825 */
826 if (off < so->so_snd.sb_cc) 826 if (off < so->so_snd.sb_cc)
827 flags &= ~TH_FIN; 827 flags &= ~TH_FIN;
828 win = 1; 828 win = 1;
829 } else { 829 } else {
830 TCP_TIMER_DISARM(tp, TCPT_PERSIST); 830 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
831 tp->t_rxtshift = 0; 831 tp->t_rxtshift = 0;
832 } 832 }
833 } 833 }
834 834
835 if (sack_rxmit == 0) { 835 if (sack_rxmit == 0) {
836 if (TCP_SACK_ENABLED(tp) && tp->t_partialacks >= 0) { 836 if (TCP_SACK_ENABLED(tp) && tp->t_partialacks >= 0) {
837 long cwin; 837 long cwin;
838 838
839 /* 839 /*
840 * We are inside of a SACK recovery episode and are 840 * We are inside of a SACK recovery episode and are
841 * sending new data, having retransmitted all the 841 * sending new data, having retransmitted all the
842 * data possible in the scoreboard. 842 * data possible in the scoreboard.
843 */ 843 */
844 if (tp->snd_wnd < so->so_snd.sb_cc) { 844 if (tp->snd_wnd < so->so_snd.sb_cc) {
845 len = tp->snd_wnd - off; 845 len = tp->snd_wnd - off;
846 flags &= ~TH_FIN; 846 flags &= ~TH_FIN;
847 } else { 847 } else {
848 len = so->so_snd.sb_cc - off; 848 len = so->so_snd.sb_cc - off;
849 } 849 }
850 850
851 /* 851 /*
852 * From FreeBSD: 852 * From FreeBSD:
853 * Don't remove this (len > 0) check ! 853 * Don't remove this (len > 0) check !
854 * We explicitly check for len > 0 here (although it  854 * We explicitly check for len > 0 here (although it
855 * isn't really necessary), to work around a gcc  855 * isn't really necessary), to work around a gcc
856 * optimization issue - to force gcc to compute 856 * optimization issue - to force gcc to compute
857 * len above. Without this check, the computation 857 * len above. Without this check, the computation
858 * of len is bungled by the optimizer. 858 * of len is bungled by the optimizer.
859 */ 859 */
860 if (len > 0) { 860 if (len > 0) {
861 cwin = tp->snd_cwnd -  861 cwin = tp->snd_cwnd -
862 (tp->snd_nxt - tp->sack_newdata) - 862 (tp->snd_nxt - tp->sack_newdata) -
863 sack_bytes_rxmt; 863 sack_bytes_rxmt;
864 if (cwin < 0) 864 if (cwin < 0)
865 cwin = 0; 865 cwin = 0;
866 if (cwin < len) { 866 if (cwin < len) {
867 len = cwin; 867 len = cwin;
868 flags &= ~TH_FIN; 868 flags &= ~TH_FIN;
869 } 869 }
870 } 870 }
871 } else if (win < so->so_snd.sb_cc) { 871 } else if (win < so->so_snd.sb_cc) {
872 len = win - off; 872 len = win - off;
873 flags &= ~TH_FIN; 873 flags &= ~TH_FIN;
874 } else { 874 } else {
875 len = so->so_snd.sb_cc - off; 875 len = so->so_snd.sb_cc - off;
876 } 876 }
877 } 877 }
878 878
879 if (len < 0) { 879 if (len < 0) {
880 /* 880 /*
881 * If FIN has been sent but not acked, 881 * If FIN has been sent but not acked,
882 * but we haven't been called to retransmit, 882 * but we haven't been called to retransmit,
883 * len will be -1. Otherwise, window shrank 883 * len will be -1. Otherwise, window shrank
884 * after we sent into it. If window shrank to 0, 884 * after we sent into it. If window shrank to 0,
885 * cancel pending retransmit, pull snd_nxt back 885 * cancel pending retransmit, pull snd_nxt back
886 * to (closed) window, and set the persist timer 886 * to (closed) window, and set the persist timer
887 * if it isn't already going. If the window didn't 887 * if it isn't already going. If the window didn't
888 * close completely, just wait for an ACK. 888 * close completely, just wait for an ACK.
889 * 889 *
890 * If we have a pending FIN, either it has already been 890 * If we have a pending FIN, either it has already been
891 * transmitted or it is outside the window, so drop it. 891 * transmitted or it is outside the window, so drop it.
892 * If the FIN has been transmitted, but this is not a 892 * If the FIN has been transmitted, but this is not a
893 * retransmission, then len must be -1. Therefore we also 893 * retransmission, then len must be -1. Therefore we also
894 * prevent here the sending of `gratuitous FINs'. This 894 * prevent here the sending of `gratuitous FINs'. This
895 * eliminates the need to check for that case below (e.g. 895 * eliminates the need to check for that case below (e.g.
896 * to back up snd_nxt before the FIN so that the sequence 896 * to back up snd_nxt before the FIN so that the sequence
897 * number is correct). 897 * number is correct).
898 */ 898 */
899 len = 0; 899 len = 0;
900 flags &= ~TH_FIN; 900 flags &= ~TH_FIN;
901 if (win == 0) { 901 if (win == 0) {
902 TCP_TIMER_DISARM(tp, TCPT_REXMT); 902 TCP_TIMER_DISARM(tp, TCPT_REXMT);
903 tp->t_rxtshift = 0; 903 tp->t_rxtshift = 0;
904 tp->snd_nxt = tp->snd_una; 904 tp->snd_nxt = tp->snd_una;
905 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) 905 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
906 tcp_setpersist(tp); 906 tcp_setpersist(tp);
907 } 907 }
908 } 908 }
909 909
910 /* 910 /*
911 * Automatic sizing enables the performance of large buffers 911 * Automatic sizing enables the performance of large buffers
912 * and most of the efficiency of small ones by only allocating 912 * and most of the efficiency of small ones by only allocating
913 * space when it is needed. 913 * space when it is needed.
914 * 914 *
915 * The criteria to step up the send buffer one notch are: 915 * The criteria to step up the send buffer one notch are:
916 * 1. receive window of remote host is larger than send buffer 916 * 1. receive window of remote host is larger than send buffer
917 * (with a fudge factor of 5/4th); 917 * (with a fudge factor of 5/4th);
918 * 2. send buffer is filled to 7/8th with data (so we actually 918 * 2. send buffer is filled to 7/8th with data (so we actually
919 * have data to make use of it); 919 * have data to make use of it);
920 * 3. send buffer fill has not hit maximal automatic size; 920 * 3. send buffer fill has not hit maximal automatic size;
921 * 4. our send window (slow start and cogestion controlled) is 921 * 4. our send window (slow start and cogestion controlled) is
922 * larger than sent but unacknowledged data in send buffer. 922 * larger than sent but unacknowledged data in send buffer.
923 * 923 *
924 * The remote host receive window scaling factor may limit the 924 * The remote host receive window scaling factor may limit the
925 * growing of the send buffer before it reaches its allowed 925 * growing of the send buffer before it reaches its allowed
926 * maximum. 926 * maximum.
927 * 927 *
928 * It scales directly with slow start or congestion window 928 * It scales directly with slow start or congestion window
929 * and does at most one step per received ACK. This fast 929 * and does at most one step per received ACK. This fast
930 * scaling has the drawback of growing the send buffer beyond 930 * scaling has the drawback of growing the send buffer beyond
931 * what is strictly necessary to make full use of a given 931 * what is strictly necessary to make full use of a given
932 * delay*bandwith product. However testing has shown this not 932 * delay*bandwith product. However testing has shown this not
933 * to be much of an problem. At worst we are trading wasting 933 * to be much of an problem. At worst we are trading wasting
934 * of available bandwith (the non-use of it) for wasting some 934 * of available bandwith (the non-use of it) for wasting some
935 * socket buffer memory. 935 * socket buffer memory.
936 * 936 *
937 * TODO: Shrink send buffer during idle periods together 937 * TODO: Shrink send buffer during idle periods together
938 * with congestion window. Requires another timer. 938 * with congestion window. Requires another timer.
939 */ 939 */
940 if (tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) { 940 if (tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
941 if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat && 941 if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
942 so->so_snd.sb_cc >= (so->so_snd.sb_hiwat / 8 * 7) && 942 so->so_snd.sb_cc >= (so->so_snd.sb_hiwat / 8 * 7) &&
943 so->so_snd.sb_cc < tcp_autosndbuf_max && 943 so->so_snd.sb_cc < tcp_autosndbuf_max &&
944 win >= (so->so_snd.sb_cc - (tp->snd_nxt - tp->snd_una))) { 944 win >= (so->so_snd.sb_cc - (tp->snd_nxt - tp->snd_una))) {
945 if (!sbreserve(&so->so_snd, 945 if (!sbreserve(&so->so_snd,
946 min(so->so_snd.sb_hiwat + tcp_autosndbuf_inc, 946 min(so->so_snd.sb_hiwat + tcp_autosndbuf_inc,
947 tcp_autosndbuf_max), so)) 947 tcp_autosndbuf_max), so))
948 so->so_snd.sb_flags &= ~SB_AUTOSIZE; 948 so->so_snd.sb_flags &= ~SB_AUTOSIZE;
949 } 949 }
950 } 950 }
951 951
952 if (len > txsegsize) { 952 if (len > txsegsize) {
953 if (use_tso) { 953 if (use_tso) {
954 /* 954 /*
955 * Truncate TSO transfers to IP_MAXPACKET, and make 955 * Truncate TSO transfers to IP_MAXPACKET, and make
956 * sure that we send equal size transfers down the 956 * sure that we send equal size transfers down the
957 * stack (rather than big-small-big-small-...). 957 * stack (rather than big-small-big-small-...).
958 */ 958 */
959#ifdef INET6 959#ifdef INET6
960 CTASSERT(IPV6_MAXPACKET == IP_MAXPACKET); 960 CTASSERT(IPV6_MAXPACKET == IP_MAXPACKET);
961#endif 961#endif
962 len = (min(len, IP_MAXPACKET) / txsegsize) * txsegsize; 962 len = (min(len, IP_MAXPACKET) / txsegsize) * txsegsize;
963 if (len <= txsegsize) { 963 if (len <= txsegsize) {
964 use_tso = 0; 964 use_tso = 0;
965 } 965 }
966 } else 966 } else
967 len = txsegsize; 967 len = txsegsize;
968 flags &= ~TH_FIN; 968 flags &= ~TH_FIN;
969 sendalot = 1; 969 sendalot = 1;
970 } else 970 } else
971 use_tso = 0; 971 use_tso = 0;
972 if (sack_rxmit) { 972 if (sack_rxmit) {
973 if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc)) 973 if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
974 flags &= ~TH_FIN; 974 flags &= ~TH_FIN;
975 } 975 }
976 976
977 win = sbspace(&so->so_rcv); 977 win = sbspace(&so->so_rcv);
978 978
979 /* 979 /*
980 * Sender silly window avoidance. If connection is idle 980 * Sender silly window avoidance. If connection is idle
981 * and can send all data, a maximum segment, 981 * and can send all data, a maximum segment,
982 * at least a maximum default-size segment do it, 982 * at least a maximum default-size segment do it,
983 * or are forced, do it; otherwise don't bother. 983 * or are forced, do it; otherwise don't bother.
984 * If peer's buffer is tiny, then send 984 * If peer's buffer is tiny, then send
985 * when window is at least half open. 985 * when window is at least half open.
986 * If retransmitting (possibly after persist timer forced us 986 * If retransmitting (possibly after persist timer forced us
987 * to send into a small window), then must resend. 987 * to send into a small window), then must resend.
988 */ 988 */
989 if (len) { 989 if (len) {
990 if (len >= txsegsize) 990 if (len >= txsegsize)
991 goto send; 991 goto send;
992 if ((so->so_state & SS_MORETOCOME) == 0 && 992 if ((so->so_state & SS_MORETOCOME) == 0 &&
993 ((idle || tp->t_flags & TF_NODELAY) && 993 ((idle || tp->t_flags & TF_NODELAY) &&
994 len + off >= so->so_snd.sb_cc)) 994 len + off >= so->so_snd.sb_cc))
995 goto send; 995 goto send;
996 if (tp->t_force) 996 if (tp->t_force)
997 goto send; 997 goto send;
998 if (len >= tp->max_sndwnd / 2) 998 if (len >= tp->max_sndwnd / 2)
999 goto send; 999 goto send;
1000 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 1000 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
1001 goto send; 1001 goto send;
1002 if (sack_rxmit) 1002 if (sack_rxmit)
1003 goto send; 1003 goto send;
1004 } 1004 }
1005 1005
1006 /* 1006 /*
1007 * Compare available window to amount of window known to peer 1007 * Compare available window to amount of window known to peer
1008 * (as advertised window less next expected input). If the 1008 * (as advertised window less next expected input). If the
1009 * difference is at least twice the size of the largest segment 1009 * difference is at least twice the size of the largest segment
1010 * we expect to receive (i.e. two segments) or at least 50% of 1010 * we expect to receive (i.e. two segments) or at least 50% of
1011 * the maximum possible window, then want to send a window update 1011 * the maximum possible window, then want to send a window update
1012 * to peer. 1012 * to peer.
1013 */ 1013 */
1014 if (win > 0) { 1014 if (win > 0) {
1015 /* 1015 /*
1016 * "adv" is the amount we can increase the window, 1016 * "adv" is the amount we can increase the window,
1017 * taking into account that we are limited by 1017 * taking into account that we are limited by
1018 * TCP_MAXWIN << tp->rcv_scale. 1018 * TCP_MAXWIN << tp->rcv_scale.
1019 */ 1019 */
1020 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) - 1020 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
1021 (tp->rcv_adv - tp->rcv_nxt); 1021 (tp->rcv_adv - tp->rcv_nxt);
1022 1022
1023 /* 1023 /*
1024 * If the new window size ends up being the same as the old 1024 * If the new window size ends up being the same as the old
1025 * size when it is scaled, then don't force a window update. 1025 * size when it is scaled, then don't force a window update.
1026 */ 1026 */
1027 if ((tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale == 1027 if ((tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale ==
1028 (adv + tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale) 1028 (adv + tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale)
1029 goto dontupdate; 1029 goto dontupdate;
1030 if (adv >= (long) (2 * rxsegsize)) 1030 if (adv >= (long) (2 * rxsegsize))
1031 goto send; 1031 goto send;
1032 if (2 * adv >= (long) so->so_rcv.sb_hiwat) 1032 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
1033 goto send; 1033 goto send;
1034 } 1034 }
1035dontupdate: 1035dontupdate:
1036 1036
1037 /* 1037 /*
1038 * Send if we owe peer an ACK. 1038 * Send if we owe peer an ACK.
1039 */ 1039 */
1040 if (tp->t_flags & TF_ACKNOW) 1040 if (tp->t_flags & TF_ACKNOW)
1041 goto send; 1041 goto send;
1042 if (flags & (TH_SYN|TH_FIN|TH_RST)) 1042 if (flags & (TH_SYN|TH_FIN|TH_RST))
1043 goto send; 1043 goto send;
1044 if (SEQ_GT(tp->snd_up, tp->snd_una)) 1044 if (SEQ_GT(tp->snd_up, tp->snd_una))
1045 goto send; 1045 goto send;
1046 /* 1046 /*
1047 * In SACK, it is possible for tcp_output to fail to send a segment 1047 * In SACK, it is possible for tcp_output to fail to send a segment
1048 * after the retransmission timer has been turned off. Make sure 1048 * after the retransmission timer has been turned off. Make sure
1049 * that the retransmission timer is set. 1049 * that the retransmission timer is set.
1050 */ 1050 */
1051 if (TCP_SACK_ENABLED(tp) && SEQ_GT(tp->snd_max, tp->snd_una) && 1051 if (TCP_SACK_ENABLED(tp) && SEQ_GT(tp->snd_max, tp->snd_una) &&
1052 !TCP_TIMER_ISARMED(tp, TCPT_REXMT) && 1052 !TCP_TIMER_ISARMED(tp, TCPT_REXMT) &&
1053 !TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) { 1053 !TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
1054 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 1054 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1055 goto just_return; 1055 goto just_return;
1056 } 1056 }
1057 1057
1058 /* 1058 /*
1059 * TCP window updates are not reliable, rather a polling protocol 1059 * TCP window updates are not reliable, rather a polling protocol
1060 * using ``persist'' packets is used to insure receipt of window 1060 * using ``persist'' packets is used to insure receipt of window
1061 * updates. The three ``states'' for the output side are: 1061 * updates. The three ``states'' for the output side are:
1062 * idle not doing retransmits or persists 1062 * idle not doing retransmits or persists
1063 * persisting to move a small or zero window 1063 * persisting to move a small or zero window
1064 * (re)transmitting and thereby not persisting 1064 * (re)transmitting and thereby not persisting
1065 * 1065 *
1066 * tp->t_timer[TCPT_PERSIST] 1066 * tp->t_timer[TCPT_PERSIST]
1067 * is set when we are in persist state. 1067 * is set when we are in persist state.
1068 * tp->t_force 1068 * tp->t_force
1069 * is set when we are called to send a persist packet. 1069 * is set when we are called to send a persist packet.
1070 * tp->t_timer[TCPT_REXMT] 1070 * tp->t_timer[TCPT_REXMT]
1071 * is set when we are retransmitting 1071 * is set when we are retransmitting
1072 * The output side is idle when both timers are zero. 1072 * The output side is idle when both timers are zero.
1073 * 1073 *
1074 * If send window is too small, there is data to transmit, and no 1074 * If send window is too small, there is data to transmit, and no
1075 * retransmit or persist is pending, then go to persist state. 1075 * retransmit or persist is pending, then go to persist state.
1076 * If nothing happens soon, send when timer expires: 1076 * If nothing happens soon, send when timer expires:
1077 * if window is nonzero, transmit what we can, 1077 * if window is nonzero, transmit what we can,
1078 * otherwise force out a byte. 1078 * otherwise force out a byte.
1079 */ 1079 */
1080 if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 1080 if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
1081 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) { 1081 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
1082 tp->t_rxtshift = 0; 1082 tp->t_rxtshift = 0;
1083 tcp_setpersist(tp); 1083 tcp_setpersist(tp);
1084 } 1084 }
1085 1085
1086 /* 1086 /*
1087 * No reason to send a segment, just return. 1087 * No reason to send a segment, just return.
1088 */ 1088 */
1089just_return: 1089just_return:
1090 TCP_REASS_UNLOCK(tp); 1090 TCP_REASS_UNLOCK(tp);
1091 return (0); 1091 return (0);
1092 1092
1093send: 1093send:
1094 /* 1094 /*
1095 * Before ESTABLISHED, force sending of initial options 1095 * Before ESTABLISHED, force sending of initial options
1096 * unless TCP set not to do any options. 1096 * unless TCP set not to do any options.
1097 * NOTE: we assume that the IP/TCP header plus TCP options 1097 * NOTE: we assume that the IP/TCP header plus TCP options
1098 * always fit in a single mbuf, leaving room for a maximum 1098 * always fit in a single mbuf, leaving room for a maximum
1099 * link header, i.e. 1099 * link header, i.e.
1100 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 1100 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
1101 */ 1101 */
1102 optlen = 0; 1102 optlen = 0;
1103 switch (af) { 1103 switch (af) {
1104#ifdef INET 1104#ifdef INET
1105 case AF_INET: 1105 case AF_INET:
1106 iphdrlen = sizeof(struct ip) + sizeof(struct tcphdr); 1106 iphdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
1107 break; 1107 break;
1108#endif 1108#endif
1109#ifdef INET6 1109#ifdef INET6
1110 case AF_INET6: 1110 case AF_INET6:
1111 iphdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 1111 iphdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1112 break; 1112 break;
1113#endif 1113#endif
1114 default: /*pacify gcc*/ 1114 default: /*pacify gcc*/
1115 iphdrlen = 0; 1115 iphdrlen = 0;
1116 break; 1116 break;
1117 } 1117 }
1118 hdrlen = iphdrlen; 1118 hdrlen = iphdrlen;
1119 if (flags & TH_SYN) { 1119 if (flags & TH_SYN) {
1120 struct rtentry *synrt; 1120 struct rtentry *synrt;
1121 1121
1122 synrt = NULL; 1122 synrt = NULL;
1123#ifdef INET 1123#ifdef INET
1124 if (tp->t_inpcb) 1124 if (tp->t_inpcb)
1125 synrt = in_pcbrtentry(tp->t_inpcb); 1125 synrt = in_pcbrtentry(tp->t_inpcb);
1126#endif 1126#endif
1127#ifdef INET6 1127#ifdef INET6
1128 if (tp->t_in6pcb) 1128 if (tp->t_in6pcb)
1129 synrt = in6_pcbrtentry(tp->t_in6pcb); 1129 synrt = in6_pcbrtentry(tp->t_in6pcb);
1130#endif 1130#endif
1131 1131
1132 tp->snd_nxt = tp->iss; 1132 tp->snd_nxt = tp->iss;
1133 tp->t_ourmss = tcp_mss_to_advertise(synrt != NULL ? 1133 tp->t_ourmss = tcp_mss_to_advertise(synrt != NULL ?
1134 synrt->rt_ifp : NULL, af); 1134 synrt->rt_ifp : NULL, af);
1135 if ((tp->t_flags & TF_NOOPT) == 0 && OPT_FITS(4)) { 1135 if ((tp->t_flags & TF_NOOPT) == 0 && OPT_FITS(4)) {
1136 opt[0] = TCPOPT_MAXSEG; 1136 opt[0] = TCPOPT_MAXSEG;
1137 opt[1] = 4; 1137 opt[1] = 4;
1138 opt[2] = (tp->t_ourmss >> 8) & 0xff; 1138 opt[2] = (tp->t_ourmss >> 8) & 0xff;
1139 opt[3] = tp->t_ourmss & 0xff; 1139 opt[3] = tp->t_ourmss & 0xff;
1140 optlen = 4; 1140 optlen = 4;
1141 1141
1142 if ((tp->t_flags & TF_REQ_SCALE) && 1142 if ((tp->t_flags & TF_REQ_SCALE) &&
1143 ((flags & TH_ACK) == 0 || 1143 ((flags & TH_ACK) == 0 ||
1144 (tp->t_flags & TF_RCVD_SCALE)) && 1144 (tp->t_flags & TF_RCVD_SCALE)) &&
1145 OPT_FITS(4)) { 1145 OPT_FITS(4)) {
1146 *((u_int32_t *) (opt + optlen)) = htonl( 1146 *((u_int32_t *) (opt + optlen)) = htonl(
1147 TCPOPT_NOP << 24 | 1147 TCPOPT_NOP << 24 |
1148 TCPOPT_WINDOW << 16 | 1148 TCPOPT_WINDOW << 16 |
1149 TCPOLEN_WINDOW << 8 | 1149 TCPOLEN_WINDOW << 8 |
1150 tp->request_r_scale); 1150 tp->request_r_scale);
1151 optlen += 4; 1151 optlen += 4;
1152 } 1152 }
1153 if (tcp_do_sack && OPT_FITS(4)) { 1153 if (tcp_do_sack && OPT_FITS(4)) {
1154 u_int8_t *cp = (u_int8_t *)(opt + optlen); 1154 u_int8_t *cp = (u_int8_t *)(opt + optlen);
1155 1155
1156 cp[0] = TCPOPT_SACK_PERMITTED; 1156 cp[0] = TCPOPT_SACK_PERMITTED;
1157 cp[1] = 2; 1157 cp[1] = 2;
1158 cp[2] = TCPOPT_NOP; 1158 cp[2] = TCPOPT_NOP;
1159 cp[3] = TCPOPT_NOP; 1159 cp[3] = TCPOPT_NOP;
1160 optlen += 4; 1160 optlen += 4;
1161 } 1161 }
1162 } 1162 }
1163 } 1163 }
1164 1164
1165 /* 1165 /*
1166 * Send a timestamp and echo-reply if this is a SYN and our side 1166 * Send a timestamp and echo-reply if this is a SYN and our side
1167 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 1167 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
1168 * and our peer have sent timestamps in our SYN's. 1168 * and our peer have sent timestamps in our SYN's.
1169 */ 1169 */
1170 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 1170 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
1171 (flags & TH_RST) == 0 && 1171 (flags & TH_RST) == 0 &&
1172 ((flags & (TH_SYN|TH_ACK)) == TH_SYN || 1172 ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
1173 (tp->t_flags & TF_RCVD_TSTMP)) && OPT_FITS(TCPOLEN_TSTAMP_APPA)) { 1173 (tp->t_flags & TF_RCVD_TSTMP)) && OPT_FITS(TCPOLEN_TSTAMP_APPA)) {
1174 u_int32_t *lp = (u_int32_t *)(opt + optlen); 1174 u_int32_t *lp = (u_int32_t *)(opt + optlen);
1175 1175
1176 /* Form timestamp option as shown in appendix A of RFC 1323. */ 1176 /* Form timestamp option as shown in appendix A of RFC 1323. */
1177 *lp++ = htonl(TCPOPT_TSTAMP_HDR); 1177 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
1178 *lp++ = htonl(TCP_TIMESTAMP(tp)); 1178 *lp++ = htonl(TCP_TIMESTAMP(tp));
1179 *lp = htonl(tp->ts_recent); 1179 *lp = htonl(tp->ts_recent);
1180 optlen += TCPOLEN_TSTAMP_APPA; 1180 optlen += TCPOLEN_TSTAMP_APPA;
1181 1181
1182 /* Set receive buffer autosizing timestamp. */ 1182 /* Set receive buffer autosizing timestamp. */
1183 if (tp->rfbuf_ts == 0 && (so->so_rcv.sb_flags & SB_AUTOSIZE)) 1183 if (tp->rfbuf_ts == 0 && (so->so_rcv.sb_flags & SB_AUTOSIZE))
1184 tp->rfbuf_ts = TCP_TIMESTAMP(tp); 1184 tp->rfbuf_ts = TCP_TIMESTAMP(tp);
1185 } 1185 }
1186 1186
1187 /* 1187 /*
1188 * Tack on the SACK block if it is necessary. 1188 * Tack on the SACK block if it is necessary.
1189 */ 1189 */
1190 if (sack_numblks) { 1190 if (sack_numblks) {
1191 int sack_len; 1191 int sack_len;
1192 u_char *bp = (u_char *)(opt + optlen); 1192 u_char *bp = (u_char *)(opt + optlen);
1193 u_int32_t *lp = (u_int32_t *)(bp + 4); 1193 u_int32_t *lp = (u_int32_t *)(bp + 4);
1194 struct ipqent *tiqe; 1194 struct ipqent *tiqe;
1195 1195
1196 sack_len = sack_numblks * 8 + 2; 1196 sack_len = sack_numblks * 8 + 2;
1197 if (OPT_FITS(sack_len + 2)) { 1197 if (OPT_FITS(sack_len + 2)) {
1198 bp[0] = TCPOPT_NOP; 1198 bp[0] = TCPOPT_NOP;
1199 bp[1] = TCPOPT_NOP; 1199 bp[1] = TCPOPT_NOP;
1200 bp[2] = TCPOPT_SACK; 1200 bp[2] = TCPOPT_SACK;
1201 bp[3] = sack_len; 1201 bp[3] = sack_len;
1202 if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) { 1202 if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) {
1203 sack_numblks--; 1203 sack_numblks--;
1204 *lp++ = htonl(tp->rcv_dsack_block.left); 1204 *lp++ = htonl(tp->rcv_dsack_block.left);
1205 *lp++ = htonl(tp->rcv_dsack_block.right); 1205 *lp++ = htonl(tp->rcv_dsack_block.right);
1206 tp->rcv_sack_flags &= ~TCPSACK_HAVED; 1206 tp->rcv_sack_flags &= ~TCPSACK_HAVED;
1207 } 1207 }
1208 for (tiqe = TAILQ_FIRST(&tp->timeq); 1208 for (tiqe = TAILQ_FIRST(&tp->timeq);
1209 sack_numblks > 0; 1209 sack_numblks > 0;
1210 tiqe = TAILQ_NEXT(tiqe, ipqe_timeq)) { 1210 tiqe = TAILQ_NEXT(tiqe, ipqe_timeq)) {
1211 KASSERT(tiqe != NULL); 1211 KASSERT(tiqe != NULL);
1212 sack_numblks--; 1212 sack_numblks--;
1213 *lp++ = htonl(tiqe->ipqe_seq); 1213 *lp++ = htonl(tiqe->ipqe_seq);
1214 *lp++ = htonl(tiqe->ipqe_seq + tiqe->ipqe_len + 1214 *lp++ = htonl(tiqe->ipqe_seq + tiqe->ipqe_len +
1215 ((tiqe->ipqe_flags & TH_FIN) != 0 ? 1 : 0)); 1215 ((tiqe->ipqe_flags & TH_FIN) != 0 ? 1 : 0));
1216 } 1216 }
1217 optlen += sack_len + 2; 1217 optlen += sack_len + 2;
1218 } 1218 }
1219 } 1219 }
1220 TCP_REASS_UNLOCK(tp); 1220 TCP_REASS_UNLOCK(tp);
1221 1221
1222#ifdef TCP_SIGNATURE 1222#ifdef TCP_SIGNATURE
1223 if ((tp->t_flags & TF_SIGNATURE) && OPT_FITS(TCPOLEN_SIGNATURE + 2)) { 1223 if ((tp->t_flags & TF_SIGNATURE) && OPT_FITS(TCPOLEN_SIGNATURE + 2)) {
1224 u_char *bp; 1224 u_char *bp;
1225 /* 1225 /*
1226 * Initialize TCP-MD5 option (RFC2385) 1226 * Initialize TCP-MD5 option (RFC2385)
1227 */ 1227 */
1228 bp = (u_char *)opt + optlen; 1228 bp = (u_char *)opt + optlen;
1229 *bp++ = TCPOPT_SIGNATURE; 1229 *bp++ = TCPOPT_SIGNATURE;
1230 *bp++ = TCPOLEN_SIGNATURE; 1230 *bp++ = TCPOLEN_SIGNATURE;
1231 sigoff = optlen + 2; 1231 sigoff = optlen + 2;
1232 memset(bp, 0, TCP_SIGLEN); 1232 memset(bp, 0, TCP_SIGLEN);
1233 bp += TCP_SIGLEN; 1233 bp += TCP_SIGLEN;
1234 optlen += TCPOLEN_SIGNATURE; 1234 optlen += TCPOLEN_SIGNATURE;
1235 /* 1235 /*
1236 * Terminate options list and maintain 32-bit alignment. 1236 * Terminate options list and maintain 32-bit alignment.
1237 */ 1237 */
1238 *bp++ = TCPOPT_NOP; 1238 *bp++ = TCPOPT_NOP;
1239 *bp++ = TCPOPT_EOL; 1239 *bp++ = TCPOPT_EOL;
1240 optlen += 2; 1240 optlen += 2;
1241 } else if ((tp->t_flags & TF_SIGNATURE) != 0) { 1241 } else if ((tp->t_flags & TF_SIGNATURE) != 0) {
1242 error = ECONNABORTED; 1242 error = ECONNABORTED;
1243 goto out; 1243 goto out;
1244 } 1244 }
1245#endif /* TCP_SIGNATURE */ 1245#endif /* TCP_SIGNATURE */
1246 1246
1247 hdrlen += optlen; 1247 hdrlen += optlen;
1248 1248
1249#ifdef DIAGNOSTIC 1249#ifdef DIAGNOSTIC
1250 if (!use_tso && len > txsegsize) 1250 if (!use_tso && len > txsegsize)
1251 panic("tcp data to be sent is larger than segment"); 1251 panic("tcp data to be sent is larger than segment");
1252 else if (use_tso && len > IP_MAXPACKET) 1252 else if (use_tso && len > IP_MAXPACKET)
1253 panic("tcp data to be sent is larger than max TSO size"); 1253 panic("tcp data to be sent is larger than max TSO size");
1254 if (max_linkhdr + hdrlen > MCLBYTES) 1254 if (max_linkhdr + hdrlen > MCLBYTES)
1255 panic("tcphdr too big"); 1255 panic("tcphdr too big");
1256#endif 1256#endif
1257 1257
1258 /* 1258 /*
1259 * Grab a header mbuf, attaching a copy of data to 1259 * Grab a header mbuf, attaching a copy of data to
1260 * be transmitted, and initialize the header from 1260 * be transmitted, and initialize the header from
1261 * the template for sends on this connection. 1261 * the template for sends on this connection.
1262 */ 1262 */
1263 if (len) { 1263 if (len) {
1264 error = tcp_build_datapkt(tp, so, off, len, hdrlen, &m); 1264 error = tcp_build_datapkt(tp, so, off, len, hdrlen, &m);
1265 if (error) 1265 if (error)
1266 goto out; 1266 goto out;
1267 /* 1267 /*
1268 * If we're sending everything we've got, set PUSH. 1268 * If we're sending everything we've got, set PUSH.
1269 * (This will keep happy those implementations which only 1269 * (This will keep happy those implementations which only
1270 * give data to the user when a buffer fills or 1270 * give data to the user when a buffer fills or
1271 * a PUSH comes in.) 1271 * a PUSH comes in.)
1272 */ 1272 */
1273 if (off + len == so->so_snd.sb_cc) 1273 if (off + len == so->so_snd.sb_cc)
1274 flags |= TH_PUSH; 1274 flags |= TH_PUSH;
1275 } else { 1275 } else {
1276 tcps = TCP_STAT_GETREF(); 1276 tcps = TCP_STAT_GETREF();
1277 if (tp->t_flags & TF_ACKNOW) 1277 if (tp->t_flags & TF_ACKNOW)
1278 tcps[TCP_STAT_SNDACKS]++; 1278 tcps[TCP_STAT_SNDACKS]++;
1279 else if (flags & (TH_SYN|TH_FIN|TH_RST)) 1279 else if (flags & (TH_SYN|TH_FIN|TH_RST))
1280 tcps[TCP_STAT_SNDCTRL]++; 1280 tcps[TCP_STAT_SNDCTRL]++;
1281 else if (SEQ_GT(tp->snd_up, tp->snd_una)) 1281 else if (SEQ_GT(tp->snd_up, tp->snd_una))
1282 tcps[TCP_STAT_SNDURG]++; 1282 tcps[TCP_STAT_SNDURG]++;
1283 else 1283 else
1284 tcps[TCP_STAT_SNDWINUP]++; 1284 tcps[TCP_STAT_SNDWINUP]++;
1285 TCP_STAT_PUTREF(); 1285 TCP_STAT_PUTREF();
1286 1286
1287 MGETHDR(m, M_DONTWAIT, MT_HEADER); 1287 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1288 if (m != NULL && max_linkhdr + hdrlen > MHLEN) { 1288 if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
1289 MCLGET(m, M_DONTWAIT); 1289 MCLGET(m, M_DONTWAIT);
1290 if ((m->m_flags & M_EXT) == 0) { 1290 if ((m->m_flags & M_EXT) == 0) {
1291 m_freem(m); 1291 m_freem(m);
1292 m = NULL; 1292 m = NULL;
1293 } 1293 }
1294 } 1294 }
1295 if (m == NULL) { 1295 if (m == NULL) {
1296 error = ENOBUFS; 1296 error = ENOBUFS;
1297 goto out; 1297 goto out;
1298 } 1298 }
1299 MCLAIM(m, &tcp_tx_mowner); 1299 MCLAIM(m, &tcp_tx_mowner);
1300 m->m_data += max_linkhdr; 1300 m->m_data += max_linkhdr;
1301 m->m_len = hdrlen; 1301 m->m_len = hdrlen;
1302 } 1302 }
1303 m->m_pkthdr.rcvif = NULL; 1303 m->m_pkthdr.rcvif = NULL;
1304 switch (af) { 1304 switch (af) {
1305#ifdef INET 1305#ifdef INET
1306 case AF_INET: 1306 case AF_INET:
1307 ip = mtod(m, struct ip *); 1307 ip = mtod(m, struct ip *);
1308#ifdef INET6 1308#ifdef INET6
1309 ip6 = NULL; 1309 ip6 = NULL;
1310#endif 1310#endif
1311 th = (struct tcphdr *)(ip + 1); 1311 th = (struct tcphdr *)(ip + 1);
1312 break; 1312 break;
1313#endif 1313#endif
1314#ifdef INET6 1314#ifdef INET6
1315 case AF_INET6: 1315 case AF_INET6:
1316 ip = NULL; 1316 ip = NULL;
1317 ip6 = mtod(m, struct ip6_hdr *); 1317 ip6 = mtod(m, struct ip6_hdr *);
1318 th = (struct tcphdr *)(ip6 + 1); 1318 th = (struct tcphdr *)(ip6 + 1);
1319 break; 1319 break;
1320#endif 1320#endif
1321 default: /*pacify gcc*/ 1321 default: /*pacify gcc*/
1322 ip = NULL; 1322 ip = NULL;
1323#ifdef INET6 1323#ifdef INET6
1324 ip6 = NULL; 1324 ip6 = NULL;
1325#endif 1325#endif
1326 th = NULL; 1326 th = NULL;
1327 break; 1327 break;
1328 } 1328 }
1329 if (tp->t_template == 0) 1329 if (tp->t_template == 0)
1330 panic("tcp_output"); 1330 panic("tcp_output");
1331 if (tp->t_template->m_len < iphdrlen) 1331 if (tp->t_template->m_len < iphdrlen)
1332 panic("tcp_output"); 1332 panic("tcp_output");
1333 bcopy(mtod(tp->t_template, void *), mtod(m, void *), iphdrlen); 1333 bcopy(mtod(tp->t_template, void *), mtod(m, void *), iphdrlen);
1334 1334
1335 /* 1335 /*
1336 * If we are starting a connection, send ECN setup 1336 * If we are starting a connection, send ECN setup
1337 * SYN packet. If we are on a retransmit, we may 1337 * SYN packet. If we are on a retransmit, we may
1338 * resend those bits a number of times as per 1338 * resend those bits a number of times as per
1339 * RFC 3168. 1339 * RFC 3168.
1340 */ 1340 */
1341 if (tp->t_state == TCPS_SYN_SENT && tcp_do_ecn) { 1341 if (tp->t_state == TCPS_SYN_SENT && tcp_do_ecn) {
1342 if (tp->t_flags & TF_SYN_REXMT) { 1342 if (tp->t_flags & TF_SYN_REXMT) {
1343 if (tp->t_ecn_retries--) 1343 if (tp->t_ecn_retries--)
1344 flags |= TH_ECE|TH_CWR; 1344 flags |= TH_ECE|TH_CWR;
1345 } else { 1345 } else {
1346 flags |= TH_ECE|TH_CWR; 1346 flags |= TH_ECE|TH_CWR;
1347 tp->t_ecn_retries = tcp_ecn_maxretries; 1347 tp->t_ecn_retries = tcp_ecn_maxretries;
1348 } 1348 }
1349 } 1349 }
1350 1350
1351 if (TCP_ECN_ALLOWED(tp)) { 1351 if (TCP_ECN_ALLOWED(tp)) {
1352 /* 1352 /*
1353 * If the peer has ECN, mark data packets 1353 * If the peer has ECN, mark data packets
1354 * ECN capable. Ignore pure ack packets, retransmissions 1354 * ECN capable. Ignore pure ack packets, retransmissions
1355 * and window probes. 1355 * and window probes.
1356 */ 1356 */
1357 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) && 1357 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
1358 !(tp->t_force && len == 1)) { 1358 !(tp->t_force && len == 1)) {
1359 ecn_tos = IPTOS_ECN_ECT0; 1359 ecn_tos = IPTOS_ECN_ECT0;
1360 TCP_STATINC(TCP_STAT_ECN_ECT); 1360 TCP_STATINC(TCP_STAT_ECN_ECT);
1361 } 1361 }
1362 1362
1363 /* 1363 /*
1364 * Reply with proper ECN notifications. 1364 * Reply with proper ECN notifications.
1365 */ 1365 */
1366 if (tp->t_flags & TF_ECN_SND_CWR) { 1366 if (tp->t_flags & TF_ECN_SND_CWR) {
1367 flags |= TH_CWR; 1367 flags |= TH_CWR;
1368 tp->t_flags &= ~TF_ECN_SND_CWR; 1368 tp->t_flags &= ~TF_ECN_SND_CWR;
1369 }  1369 }
1370 if (tp->t_flags & TF_ECN_SND_ECE) { 1370 if (tp->t_flags & TF_ECN_SND_ECE) {
1371 flags |= TH_ECE; 1371 flags |= TH_ECE;
1372 } 1372 }
1373 } 1373 }
1374 1374
1375 1375
1376 /* 1376 /*
1377 * If we are doing retransmissions, then snd_nxt will 1377 * If we are doing retransmissions, then snd_nxt will
1378 * not reflect the first unsent octet. For ACK only 1378 * not reflect the first unsent octet. For ACK only
1379 * packets, we do not want the sequence number of the 1379 * packets, we do not want the sequence number of the
1380 * retransmitted packet, we want the sequence number 1380 * retransmitted packet, we want the sequence number
1381 * of the next unsent octet. So, if there is no data 1381 * of the next unsent octet. So, if there is no data
1382 * (and no SYN or FIN), use snd_max instead of snd_nxt 1382 * (and no SYN or FIN), use snd_max instead of snd_nxt
1383 * when filling in ti_seq. But if we are in persist 1383 * when filling in ti_seq. But if we are in persist
1384 * state, snd_max might reflect one byte beyond the 1384 * state, snd_max might reflect one byte beyond the
1385 * right edge of the window, so use snd_nxt in that 1385 * right edge of the window, so use snd_nxt in that
1386 * case, since we know we aren't doing a retransmission. 1386 * case, since we know we aren't doing a retransmission.
1387 * (retransmit and persist are mutually exclusive...) 1387 * (retransmit and persist are mutually exclusive...)
1388 */ 1388 */
1389 if (TCP_SACK_ENABLED(tp) && sack_rxmit) { 1389 if (TCP_SACK_ENABLED(tp) && sack_rxmit) {
1390 th->th_seq = htonl(p->rxmit); 1390 th->th_seq = htonl(p->rxmit);
1391 p->rxmit += len; 1391 p->rxmit += len;
1392 } else { 1392 } else {
1393 if (len || (flags & (TH_SYN|TH_FIN)) || 1393 if (len || (flags & (TH_SYN|TH_FIN)) ||
1394 TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) 1394 TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
1395 th->th_seq = htonl(tp->snd_nxt); 1395 th->th_seq = htonl(tp->snd_nxt);
1396 else 1396 else
1397 th->th_seq = htonl(tp->snd_max); 1397 th->th_seq = htonl(tp->snd_max);
1398 } 1398 }
1399 th->th_ack = htonl(tp->rcv_nxt); 1399 th->th_ack = htonl(tp->rcv_nxt);
1400 if (optlen) { 1400 if (optlen) {
1401 bcopy((void *)opt, (void *)(th + 1), optlen); 1401 bcopy((void *)opt, (void *)(th + 1), optlen);
1402 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 1402 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
1403 } 1403 }
1404 th->th_flags = flags; 1404 th->th_flags = flags;
1405 /* 1405 /*
1406 * Calculate receive window. Don't shrink window, 1406 * Calculate receive window. Don't shrink window,
1407 * but avoid silly window syndrome. 1407 * but avoid silly window syndrome.
1408 */ 1408 */
1409 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize) 1409 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
1410 win = 0; 1410 win = 0;
1411 if (win > (long)TCP_MAXWIN << tp->rcv_scale) 1411 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
1412 win = (long)TCP_MAXWIN << tp->rcv_scale; 1412 win = (long)TCP_MAXWIN << tp->rcv_scale;
1413 if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt)) 1413 if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt))
1414 win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt); 1414 win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt);
1415 th->th_win = htons((u_int16_t) (win>>tp->rcv_scale)); 1415 th->th_win = htons((u_int16_t) (win>>tp->rcv_scale));
1416 if (th->th_win == 0) { 1416 if (th->th_win == 0) {
1417 tp->t_sndzerowin++; 1417 tp->t_sndzerowin++;
1418 } 1418 }
1419 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 1419 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
1420 u_int32_t urp = tp->snd_up - tp->snd_nxt; 1420 u_int32_t urp = tp->snd_up - tp->snd_nxt;
1421 if (urp > IP_MAXPACKET) 1421 if (urp > IP_MAXPACKET)
1422 urp = IP_MAXPACKET; 1422 urp = IP_MAXPACKET;
1423 th->th_urp = htons((u_int16_t)urp); 1423 th->th_urp = htons((u_int16_t)urp);
1424 th->th_flags |= TH_URG; 1424 th->th_flags |= TH_URG;
1425 } else 1425 } else
1426 /* 1426 /*
1427 * If no urgent pointer to send, then we pull 1427 * If no urgent pointer to send, then we pull
1428 * the urgent pointer to the left edge of the send window 1428 * the urgent pointer to the left edge of the send window
1429 * so that it doesn't drift into the send window on sequence 1429 * so that it doesn't drift into the send window on sequence
1430 * number wraparound. 1430 * number wraparound.
1431 */ 1431 */
1432 tp->snd_up = tp->snd_una; /* drag it along */ 1432 tp->snd_up = tp->snd_una; /* drag it along */
1433 1433
1434#ifdef TCP_SIGNATURE 1434#ifdef TCP_SIGNATURE
1435 if (sigoff && (tp->t_flags & TF_SIGNATURE)) { 1435 if (sigoff && (tp->t_flags & TF_SIGNATURE)) {
1436 struct secasvar *sav; 1436 struct secasvar *sav;
1437 u_int8_t *sigp; 1437 u_int8_t *sigp;
1438 1438
1439 sav = tcp_signature_getsav(m, th); 1439 sav = tcp_signature_getsav(m, th);
1440 1440
1441 if (sav == NULL) { 1441 if (sav == NULL) {
1442 if (m) 1442 if (m)
1443 m_freem(m); 1443 m_freem(m);
1444 return (EPERM); 1444 return (EPERM);
1445 } 1445 }
1446 1446
1447 m->m_pkthdr.len = hdrlen + len; 1447 m->m_pkthdr.len = hdrlen + len;
1448 sigp = (char *)th + sizeof(*th) + sigoff; 1448 sigp = (char *)th + sizeof(*th) + sigoff;
1449 tcp_signature(m, th, (char *)th - mtod(m, char *), sav, sigp); 1449 tcp_signature(m, th, (char *)th - mtod(m, char *), sav, sigp);
1450 1450
1451 key_sa_recordxfer(sav, m); 1451 key_sa_recordxfer(sav, m);
1452 KEY_FREESAV(&sav); 1452 KEY_FREESAV(&sav);
1453 } 1453 }
1454#endif 1454#endif
1455 1455
1456 /* 1456 /*
1457 * Set ourselves up to be checksummed just before the packet 1457 * Set ourselves up to be checksummed just before the packet
1458 * hits the wire. 1458 * hits the wire.
1459 */ 1459 */
1460 switch (af) { 1460 switch (af) {
1461#ifdef INET 1461#ifdef INET
1462 case AF_INET: 1462 case AF_INET:
1463 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 1463 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1464 if (use_tso) { 1464 if (use_tso) {
1465 m->m_pkthdr.segsz = txsegsize; 1465 m->m_pkthdr.segsz = txsegsize;
1466 m->m_pkthdr.csum_flags = M_CSUM_TSOv4; 1466 m->m_pkthdr.csum_flags = M_CSUM_TSOv4;
1467 } else { 1467 } else {
1468 m->m_pkthdr.csum_flags = M_CSUM_TCPv4; 1468 m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
1469 if (len + optlen) { 1469 if (len + optlen) {
1470 /* Fixup the pseudo-header checksum. */ 1470 /* Fixup the pseudo-header checksum. */
1471 /* XXXJRT Not IP Jumbogram safe. */ 1471 /* XXXJRT Not IP Jumbogram safe. */
1472 th->th_sum = in_cksum_addword(th->th_sum, 1472 th->th_sum = in_cksum_addword(th->th_sum,
1473 htons((u_int16_t) (len + optlen))); 1473 htons((u_int16_t) (len + optlen)));
1474 } 1474 }
1475 } 1475 }
1476 break; 1476 break;
1477#endif 1477#endif
1478#ifdef INET6 1478#ifdef INET6
1479 case AF_INET6: 1479 case AF_INET6:
1480 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 1480 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1481 if (use_tso) { 1481 if (use_tso) {
1482 m->m_pkthdr.segsz = txsegsize; 1482 m->m_pkthdr.segsz = txsegsize;
1483 m->m_pkthdr.csum_flags = M_CSUM_TSOv6; 1483 m->m_pkthdr.csum_flags = M_CSUM_TSOv6;
1484 } else { 1484 } else {
1485 m->m_pkthdr.csum_flags = M_CSUM_TCPv6; 1485 m->m_pkthdr.csum_flags = M_CSUM_TCPv6;
1486 if (len + optlen) { 1486 if (len + optlen) {
1487 /* Fixup the pseudo-header checksum. */ 1487 /* Fixup the pseudo-header checksum. */
1488 /* XXXJRT: Not IPv6 Jumbogram safe. */ 1488 /* XXXJRT: Not IPv6 Jumbogram safe. */
1489 th->th_sum = in_cksum_addword(th->th_sum, 1489 th->th_sum = in_cksum_addword(th->th_sum,
1490 htons((u_int16_t) (len + optlen))); 1490 htons((u_int16_t) (len + optlen)));
1491 } 1491 }
1492 } 1492 }
1493 break; 1493 break;
1494#endif 1494#endif
1495 } 1495 }
1496 1496
1497 /* 1497 /*
1498 * In transmit state, time the transmission and arrange for 1498 * In transmit state, time the transmission and arrange for
1499 * the retransmit. In persist state, just set snd_max. 1499 * the retransmit. In persist state, just set snd_max.
1500 */ 1500 */
1501 if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) { 1501 if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
1502 tcp_seq startseq = tp->snd_nxt; 1502 tcp_seq startseq = tp->snd_nxt;
1503 1503
1504 /* 1504 /*
1505 * Advance snd_nxt over sequence space of this segment. 1505 * Advance snd_nxt over sequence space of this segment.
1506 * There are no states in which we send both a SYN and a FIN, 1506 * There are no states in which we send both a SYN and a FIN,
1507 * so we collapse the tests for these flags. 1507 * so we collapse the tests for these flags.
1508 */ 1508 */
1509 if (flags & (TH_SYN|TH_FIN)) 1509 if (flags & (TH_SYN|TH_FIN))
1510 tp->snd_nxt++; 1510 tp->snd_nxt++;
1511 if (sack_rxmit) 1511 if (sack_rxmit)
1512 goto timer; 1512 goto timer;
1513 tp->snd_nxt += len; 1513 tp->snd_nxt += len;
1514 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 1514 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
1515 tp->snd_max = tp->snd_nxt; 1515 tp->snd_max = tp->snd_nxt;
1516 /* 1516 /*
1517 * Time this transmission if not a retransmission and 1517 * Time this transmission if not a retransmission and
1518 * not currently timing anything. 1518 * not currently timing anything.
1519 */ 1519 */
1520 if (tp->t_rtttime == 0) { 1520 if (tp->t_rtttime == 0) {
1521 tp->t_rtttime = tcp_now; 1521 tp->t_rtttime = tcp_now;
1522 tp->t_rtseq = startseq; 1522 tp->t_rtseq = startseq;
1523 TCP_STATINC(TCP_STAT_SEGSTIMED); 1523 TCP_STATINC(TCP_STAT_SEGSTIMED);
1524 } 1524 }
1525 } 1525 }
1526 1526
1527 /* 1527 /*
1528 * Set retransmit timer if not currently set, 1528 * Set retransmit timer if not currently set,
1529 * and not doing an ack or a keep-alive probe. 1529 * and not doing an ack or a keep-alive probe.
1530 * Initial value for retransmit timer is smoothed 1530 * Initial value for retransmit timer is smoothed
1531 * round-trip time + 2 * round-trip time variance. 1531 * round-trip time + 2 * round-trip time variance.
1532 * Initialize shift counter which is used for backoff 1532 * Initialize shift counter which is used for backoff
1533 * of retransmit time. 1533 * of retransmit time.
1534 */ 1534 */
1535timer: 1535timer:
1536 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 1536 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0) {
1537 ((sack_rxmit && tp->snd_nxt != tp->snd_max) || 1537 if ((sack_rxmit && tp->snd_nxt != tp->snd_max)
1538 tp->snd_nxt != tp->snd_una)) { 1538 || tp->snd_nxt != tp->snd_una) {
1539 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) { 1539 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
1540 TCP_TIMER_DISARM(tp, TCPT_PERSIST); 1540 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
 1541 tp->t_rxtshift = 0;
 1542 }
 1543 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
 1544 } else if (len == 0 && so->so_snd.sb_cc > 0
 1545 && TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
 1546 /*
 1547 * If we are sending a window probe and there's
 1548 * unacked data in the socket, make sure at
 1549 * least the persist timer is running.
 1550 */
1541 tp->t_rxtshift = 0; 1551 tp->t_rxtshift = 0;
 1552 tcp_setpersist(tp);
1542 } 1553 }
1543 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 
1544 } 1554 }
1545 } else 1555 } else
1546 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 1556 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
1547 tp->snd_max = tp->snd_nxt + len; 1557 tp->snd_max = tp->snd_nxt + len;
1548 1558
1549#ifdef TCP_DEBUG 1559#ifdef TCP_DEBUG
1550 /* 1560 /*
1551 * Trace. 1561 * Trace.
1552 */ 1562 */
1553 if (so->so_options & SO_DEBUG) 1563 if (so->so_options & SO_DEBUG)
1554 tcp_trace(TA_OUTPUT, tp->t_state, tp, m, 0); 1564 tcp_trace(TA_OUTPUT, tp->t_state, tp, m, 0);
1555#endif 1565#endif
1556 1566
1557 /* 1567 /*
1558 * Fill in IP length and desired time to live and 1568 * Fill in IP length and desired time to live and
1559 * send to IP level. There should be a better way 1569 * send to IP level. There should be a better way
1560 * to handle ttl and tos; we could keep them in 1570 * to handle ttl and tos; we could keep them in
1561 * the template, but need a way to checksum without them. 1571 * the template, but need a way to checksum without them.
1562 */ 1572 */
1563 m->m_pkthdr.len = hdrlen + len; 1573 m->m_pkthdr.len = hdrlen + len;
1564 1574
1565 switch (af) { 1575 switch (af) {
1566#ifdef INET 1576#ifdef INET
1567 case AF_INET: 1577 case AF_INET:
1568 ip->ip_len = htons(m->m_pkthdr.len); 1578 ip->ip_len = htons(m->m_pkthdr.len);
1569 packetlen = m->m_pkthdr.len; 1579 packetlen = m->m_pkthdr.len;
1570 if (tp->t_inpcb) { 1580 if (tp->t_inpcb) {
1571 ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl; 1581 ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;
1572 ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos | ecn_tos; 1582 ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos | ecn_tos;
1573 } 1583 }
1574#ifdef INET6 1584#ifdef INET6
1575 else if (tp->t_in6pcb) { 1585 else if (tp->t_in6pcb) {
1576 ip->ip_ttl = in6_selecthlim(tp->t_in6pcb, NULL); /*XXX*/ 1586 ip->ip_ttl = in6_selecthlim(tp->t_in6pcb, NULL); /*XXX*/
1577 ip->ip_tos = ecn_tos; /*XXX*/ 1587 ip->ip_tos = ecn_tos; /*XXX*/
1578 } 1588 }
1579#endif 1589#endif
1580 break; 1590 break;
1581#endif 1591#endif
1582#ifdef INET6 1592#ifdef INET6
1583 case AF_INET6: 1593 case AF_INET6:
1584 packetlen = m->m_pkthdr.len; 1594 packetlen = m->m_pkthdr.len;
1585 ip6->ip6_nxt = IPPROTO_TCP; 1595 ip6->ip6_nxt = IPPROTO_TCP;
1586 if (tp->t_in6pcb) { 1596 if (tp->t_in6pcb) {
1587 /* 1597 /*
1588 * we separately set hoplimit for every segment, since 1598 * we separately set hoplimit for every segment, since
1589 * the user might want to change the value via 1599 * the user might want to change the value via
1590 * setsockopt. Also, desired default hop limit might 1600 * setsockopt. Also, desired default hop limit might
1591 * be changed via Neighbor Discovery. 1601 * be changed via Neighbor Discovery.
1592 */ 1602 */
1593 ip6->ip6_hlim = in6_selecthlim_rt(tp->t_in6pcb); 1603 ip6->ip6_hlim = in6_selecthlim_rt(tp->t_in6pcb);
1594 } 1604 }
1595 ip6->ip6_flow |= htonl(ecn_tos << 20); 1605 ip6->ip6_flow |= htonl(ecn_tos << 20);
1596 /* ip6->ip6_flow = ??? (from template) */ 1606 /* ip6->ip6_flow = ??? (from template) */
1597 /* ip6_plen will be filled in ip6_output(). */ 1607 /* ip6_plen will be filled in ip6_output(). */
1598 break; 1608 break;
1599#endif 1609#endif
1600 default: /*pacify gcc*/ 1610 default: /*pacify gcc*/
1601 packetlen = 0; 1611 packetlen = 0;
1602 break; 1612 break;
1603 } 1613 }
1604 1614
1605 switch (af) { 1615 switch (af) {
1606#ifdef INET 1616#ifdef INET
1607 case AF_INET: 1617 case AF_INET:
1608 { 1618 {
1609 struct mbuf *opts; 1619 struct mbuf *opts;
1610 1620
1611 if (tp->t_inpcb) 1621 if (tp->t_inpcb)
1612 opts = tp->t_inpcb->inp_options; 1622 opts = tp->t_inpcb->inp_options;
1613 else 1623 else
1614 opts = NULL; 1624 opts = NULL;
1615 error = ip_output(m, opts, ro, 1625 error = ip_output(m, opts, ro,
1616 (tp->t_mtudisc ? IP_MTUDISC : 0) | 1626 (tp->t_mtudisc ? IP_MTUDISC : 0) |
1617 (so->so_options & SO_DONTROUTE), NULL, so); 1627 (so->so_options & SO_DONTROUTE), NULL, so);
1618 break; 1628 break;
1619 } 1629 }
1620#endif 1630#endif
1621#ifdef INET6 1631#ifdef INET6
1622 case AF_INET6: 1632 case AF_INET6:
1623 { 1633 {
1624 struct ip6_pktopts *opts; 1634 struct ip6_pktopts *opts;
1625 1635
1626 if (tp->t_in6pcb) 1636 if (tp->t_in6pcb)
1627 opts = tp->t_in6pcb->in6p_outputopts; 1637 opts = tp->t_in6pcb->in6p_outputopts;
1628 else 1638 else
1629 opts = NULL; 1639 opts = NULL;
1630 error = ip6_output(m, opts, ro, so->so_options & SO_DONTROUTE, 1640 error = ip6_output(m, opts, ro, so->so_options & SO_DONTROUTE,
1631 NULL, so, NULL); 1641 NULL, so, NULL);
1632 break; 1642 break;
1633 } 1643 }
1634#endif 1644#endif
1635 default: 1645 default:
1636 error = EAFNOSUPPORT; 1646 error = EAFNOSUPPORT;
1637 break; 1647 break;
1638 } 1648 }
1639 if (error) { 1649 if (error) {
1640out: 1650out:
1641 if (error == ENOBUFS) { 1651 if (error == ENOBUFS) {
1642 TCP_STATINC(TCP_STAT_SELFQUENCH); 1652 TCP_STATINC(TCP_STAT_SELFQUENCH);
1643#ifdef INET 1653#ifdef INET
1644 if (tp->t_inpcb) 1654 if (tp->t_inpcb)
1645 tcp_quench(tp->t_inpcb, 0); 1655 tcp_quench(tp->t_inpcb, 0);
1646#endif 1656#endif
1647#ifdef INET6 1657#ifdef INET6
1648 if (tp->t_in6pcb) 1658 if (tp->t_in6pcb)
1649 tcp6_quench(tp->t_in6pcb, 0); 1659 tcp6_quench(tp->t_in6pcb, 0);
1650#endif 1660#endif
1651 error = 0; 1661 error = 0;
1652 } else if ((error == EHOSTUNREACH || error == ENETDOWN) && 1662 } else if ((error == EHOSTUNREACH || error == ENETDOWN) &&
1653 TCPS_HAVERCVDSYN(tp->t_state)) { 1663 TCPS_HAVERCVDSYN(tp->t_state)) {
1654 tp->t_softerror = error; 1664 tp->t_softerror = error;
1655 error = 0; 1665 error = 0;
1656 } 1666 }
1657 1667
1658 /* Back out the seqence number advance. */ 1668 /* Back out the seqence number advance. */
1659 if (sack_rxmit) 1669 if (sack_rxmit)
1660 p->rxmit -= len; 1670 p->rxmit -= len;
1661 1671
1662 /* Restart the delayed ACK timer, if necessary. */ 1672 /* Restart the delayed ACK timer, if necessary. */
1663 if (tp->t_flags & TF_DELACK) 1673 if (tp->t_flags & TF_DELACK)
1664 TCP_RESTART_DELACK(tp); 1674 TCP_RESTART_DELACK(tp);
1665 1675
1666 return (error); 1676 return (error);
1667 } 1677 }
1668 1678
1669 if (packetlen > tp->t_pmtud_mtu_sent) 1679 if (packetlen > tp->t_pmtud_mtu_sent)
1670 tp->t_pmtud_mtu_sent = packetlen; 1680 tp->t_pmtud_mtu_sent = packetlen;
1671  1681
1672 tcps = TCP_STAT_GETREF(); 1682 tcps = TCP_STAT_GETREF();
1673 tcps[TCP_STAT_SNDTOTAL]++; 1683 tcps[TCP_STAT_SNDTOTAL]++;
1674 if (tp->t_flags & TF_DELACK) 1684 if (tp->t_flags & TF_DELACK)
1675 tcps[TCP_STAT_DELACK]++; 1685 tcps[TCP_STAT_DELACK]++;
1676 TCP_STAT_PUTREF(); 1686 TCP_STAT_PUTREF();
1677 1687
1678 /* 1688 /*
1679 * Data sent (as far as we can tell). 1689 * Data sent (as far as we can tell).
1680 * If this advertises a larger window than any other segment, 1690 * If this advertises a larger window than any other segment,
1681 * then remember the size of the advertised window. 1691 * then remember the size of the advertised window.
1682 * Any pending ACK has now been sent. 1692 * Any pending ACK has now been sent.
1683 */ 1693 */
1684 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 1694 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
1685 tp->rcv_adv = tp->rcv_nxt + win; 1695 tp->rcv_adv = tp->rcv_nxt + win;
1686 tp->last_ack_sent = tp->rcv_nxt; 1696 tp->last_ack_sent = tp->rcv_nxt;
1687 tp->t_flags &= ~TF_ACKNOW; 1697 tp->t_flags &= ~TF_ACKNOW;
1688 TCP_CLEAR_DELACK(tp); 1698 TCP_CLEAR_DELACK(tp);
1689#ifdef DIAGNOSTIC 1699#ifdef DIAGNOSTIC
1690 if (maxburst < 0) 1700 if (maxburst < 0)
1691 printf("tcp_output: maxburst exceeded by %d\n", -maxburst); 1701 printf("tcp_output: maxburst exceeded by %d\n", -maxburst);
1692#endif 1702#endif
1693 if (sendalot && (tp->t_congctl == &tcp_reno_ctl || --maxburst)) 1703 if (sendalot && (tp->t_congctl == &tcp_reno_ctl || --maxburst))
1694 goto again; 1704 goto again;
1695 return (0); 1705 return (0);
1696} 1706}
1697 1707
1698void 1708void
1699tcp_setpersist(struct tcpcb *tp) 1709tcp_setpersist(struct tcpcb *tp)
1700{ 1710{
1701 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2); 1711 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
1702 int nticks; 1712 int nticks;
1703 1713
1704 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT)) 1714 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
1705 panic("tcp_output REXMT"); 1715 panic("tcp_output REXMT");
1706 /* 1716 /*
1707 * Start/restart persistance timer. 1717 * Start/restart persistance timer.
1708 */ 1718 */
1709 if (t < tp->t_rttmin) 1719 if (t < tp->t_rttmin)
1710 t = tp->t_rttmin; 1720 t = tp->t_rttmin;
1711 TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift], 1721 TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift],
1712 TCPTV_PERSMIN, TCPTV_PERSMAX); 1722 TCPTV_PERSMIN, TCPTV_PERSMAX);
1713 TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks); 1723 TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks);
1714 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 1724 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1715 tp->t_rxtshift++; 1725 tp->t_rxtshift++;
1716} 1726}