]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/tcp_output.c
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / bsd / netinet / tcp_output.c
CommitLineData
1c79356b 1/*
f427ee49 2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39037602 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
39037602 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39037602 17 *
2d21ac55
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
39037602 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
9bccf70c 61 * $FreeBSD: src/sys/netinet/tcp_output.c,v 1.39.2.10 2001/07/07 04:30:38 silby Exp $
1c79356b 62 */
2d21ac55
A
63/*
64 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
67 * Version 2.0.
68 */
1c79356b 69
f427ee49 70#define _IP_VHL
1c79356b 71
1c79356b
A
72
73#include <sys/param.h>
74#include <sys/systm.h>
9bccf70c
A
75#include <sys/kernel.h>
76#include <sys/sysctl.h>
1c79356b
A
77#include <sys/mbuf.h>
78#include <sys/domain.h>
79#include <sys/protosw.h>
80#include <sys/socket.h>
81#include <sys/socketvar.h>
82
83#include <net/route.h>
6d2010ae 84#include <net/ntstat.h>
2d21ac55 85#include <net/if_var.h>
316670eb
A
86#include <net/if.h>
87#include <net/if_types.h>
39236c6e 88#include <net/dlil.h>
1c79356b
A
89
90#include <netinet/in.h>
91#include <netinet/in_systm.h>
2d21ac55 92#include <netinet/in_var.h>
39037602 93#include <netinet/in_tclass.h>
1c79356b 94#include <netinet/ip.h>
9bccf70c 95#include <netinet/in_pcb.h>
1c79356b 96#include <netinet/ip_var.h>
6d2010ae 97#include <mach/sdt.h>
9bccf70c 98#include <netinet6/in6_pcb.h>
1c79356b 99#include <netinet/ip6.h>
1c79356b 100#include <netinet6/ip6_var.h>
1c79356b 101#include <netinet/tcp.h>
f427ee49 102#define TCPOUTFLAGS
3e170ce0 103#include <netinet/tcp_cache.h>
1c79356b
A
104#include <netinet/tcp_fsm.h>
105#include <netinet/tcp_seq.h>
106#include <netinet/tcp_timer.h>
107#include <netinet/tcp_var.h>
108#include <netinet/tcpip.h>
6d2010ae 109#include <netinet/tcp_cc.h>
1c79356b
A
110#if TCPDEBUG
111#include <netinet/tcp_debug.h>
112#endif
cb323159 113#include <netinet/tcp_log.h>
1c79356b 114#include <sys/kdebug.h>
6d2010ae 115#include <mach/sdt.h>
1c79356b 116
9bccf70c
A
117#if IPSEC
118#include <netinet6/ipsec.h>
119#endif /*IPSEC*/
fa4905b1 120
39236c6e
A
121#if MPTCP
122#include <netinet/mptcp_var.h>
123#include <netinet/mptcp.h>
124#include <netinet/mptcp_opt.h>
f427ee49 125#include <netinet/mptcp_seq.h>
39236c6e 126#endif
316670eb 127
3e170ce0
A
128#include <corecrypto/ccaes.h>
129
f427ee49
A
130#define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 1)
131#define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 3)
132#define DBG_FNC_TCP_OUTPUT NETDBG_CODE(DBG_NETTCP, (4 << 8) | 1)
1c79356b 133
5ba3f43e 134SYSCTL_SKMEM_TCP_INT(OID_AUTO, path_mtu_discovery,
f427ee49
A
135 CTLFLAG_RW | CTLFLAG_LOCKED, int, path_mtu_discovery, 1,
136 "Enable Path MTU Discovery");
9bccf70c 137
5ba3f43e 138SYSCTL_SKMEM_TCP_INT(OID_AUTO, local_slowstart_flightsize,
f427ee49
A
139 CTLFLAG_RW | CTLFLAG_LOCKED, int, ss_fltsz_local, 8,
140 "Slow start flight size for local networks");
9bccf70c 141
f427ee49 142int tcp_do_tso = 1;
6d2010ae 143SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_RW | CTLFLAG_LOCKED,
f427ee49 144 &tcp_do_tso, 0, "Enable TCP Segmentation Offload");
b0d623f7 145
5ba3f43e
A
146SYSCTL_SKMEM_TCP_INT(OID_AUTO, ecn_setup_percentage,
147 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_ecn_setup_percentage, 100,
39037602
A
148 "Max ECN setup percentage");
149
f427ee49
A
150SYSCTL_SKMEM_TCP_INT(OID_AUTO, do_ack_compression,
151 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_do_ack_compression, 1,
152 "Enable TCP ACK compression (on (cell only): 1, off: 0, on (all interfaces): 2)");
153
154SYSCTL_SKMEM_TCP_INT(OID_AUTO, ack_compression_rate,
155 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_ack_compression_rate, TCP_COMP_CHANGE_RATE,
156 "Rate at which we force sending new ACKs (in ms)");
157
4bd07ac2
A
158static int
159sysctl_change_ecn_setting SYSCTL_HANDLER_ARGS
160{
161#pragma unused(oidp, arg1, arg2)
162 int i, err = 0, changed = 0;
163 struct ifnet *ifp;
164
165 err = sysctl_io_number(req, tcp_ecn_outbound, sizeof(int32_t),
166 &i, &changed);
f427ee49 167 if (err != 0 || req->newptr == USER_ADDR_NULL) {
cb323159 168 return err;
f427ee49 169 }
4bd07ac2
A
170
171 if (changed) {
172 if ((tcp_ecn_outbound == 0 || tcp_ecn_outbound == 1) &&
173 (i == 0 || i == 1)) {
174 tcp_ecn_outbound = i;
5ba3f43e 175 SYSCTL_SKMEM_UPDATE_FIELD(tcp.ecn_initiate_out, tcp_ecn_outbound);
cb323159 176 return err;
4bd07ac2
A
177 }
178 if (tcp_ecn_outbound == 2 && (i == 0 || i == 1)) {
179 /*
180 * Reset ECN enable flags on non-cellular
181 * interfaces so that the system default will take
182 * over
183 */
184 ifnet_head_lock_shared();
185 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
186 if (!IFNET_IS_CELLULAR(ifp)) {
f427ee49
A
187 if_clear_eflags(ifp,
188 IFEF_ECN_ENABLE |
189 IFEF_ECN_DISABLE);
4bd07ac2
A
190 }
191 }
192 ifnet_head_done();
193 } else {
194 /*
195 * Set ECN enable flags on non-cellular
196 * interfaces
197 */
198 ifnet_head_lock_shared();
199 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
200 if (!IFNET_IS_CELLULAR(ifp)) {
f427ee49
A
201 if_set_eflags(ifp, IFEF_ECN_ENABLE);
202 if_clear_eflags(ifp, IFEF_ECN_DISABLE);
4bd07ac2
A
203 }
204 }
205 ifnet_head_done();
206 }
207 tcp_ecn_outbound = i;
5ba3f43e 208 SYSCTL_SKMEM_UPDATE_FIELD(tcp.ecn_initiate_out, tcp_ecn_outbound);
4bd07ac2
A
209 }
210 /* Change the other one too as the work is done */
5ba3f43e 211 if (i == 2 || tcp_ecn_inbound == 2) {
4bd07ac2 212 tcp_ecn_inbound = i;
5ba3f43e
A
213 SYSCTL_SKMEM_UPDATE_FIELD(tcp.ecn_negotiate_in, tcp_ecn_inbound);
214 }
cb323159 215 return err;
4bd07ac2
A
216}
217
490019cf 218int tcp_ecn_outbound = 2;
4bd07ac2
A
219SYSCTL_PROC(_net_inet_tcp, OID_AUTO, ecn_initiate_out,
220 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_ecn_outbound, 0,
221 sysctl_change_ecn_setting, "IU",
222 "Initiate ECN for outbound connections");
2d21ac55 223
490019cf 224int tcp_ecn_inbound = 2;
4bd07ac2
A
225SYSCTL_PROC(_net_inet_tcp, OID_AUTO, ecn_negotiate_in,
226 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_ecn_inbound, 0,
227 sysctl_change_ecn_setting, "IU",
228 "Initiate ECN for inbound connections");
2d21ac55 229
5ba3f43e 230SYSCTL_SKMEM_TCP_INT(OID_AUTO, packetchain,
f427ee49
A
231 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_packet_chaining, 50,
232 "Enable TCP output packet chaining");
91447636 233
5ba3f43e 234SYSCTL_SKMEM_TCP_INT(OID_AUTO, socket_unlocked_on_output,
f427ee49
A
235 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_output_unlocked, 1,
236 "Unlock TCP when sending packets down to IP");
6d2010ae 237
5ba3f43e 238SYSCTL_SKMEM_TCP_INT(OID_AUTO, min_iaj_win,
f427ee49
A
239 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_min_iaj_win, MIN_IAJ_WIN,
240 "Minimum recv win based on inter-packet arrival jitter");
6d2010ae 241
5ba3f43e 242SYSCTL_SKMEM_TCP_INT(OID_AUTO, acc_iaj_react_limit,
f427ee49
A
243 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_acc_iaj_react_limit,
244 ACC_IAJ_REACT_LIMIT, "Accumulated IAJ when receiver starts to react");
316670eb 245
5ba3f43e 246SYSCTL_SKMEM_TCP_INT(OID_AUTO, autosndbufinc,
f427ee49
A
247 CTLFLAG_RW | CTLFLAG_LOCKED, uint32_t, tcp_autosndbuf_inc,
248 8 * 1024, "Increment in send socket bufffer size");
316670eb 249
5ba3f43e 250SYSCTL_SKMEM_TCP_INT(OID_AUTO, autosndbufmax,
f427ee49
A
251 CTLFLAG_RW | CTLFLAG_LOCKED, uint32_t, tcp_autosndbuf_max, 2 * 1024 * 1024,
252 "Maximum send socket buffer size");
316670eb 253
5ba3f43e 254SYSCTL_SKMEM_TCP_INT(OID_AUTO, rtt_recvbg,
f427ee49
A
255 CTLFLAG_RW | CTLFLAG_LOCKED, uint32_t, tcp_use_rtt_recvbg, 1,
256 "Use RTT for bg recv algorithm");
39236c6e 257
5ba3f43e 258SYSCTL_SKMEM_TCP_INT(OID_AUTO, recv_throttle_minwin,
f427ee49
A
259 CTLFLAG_RW | CTLFLAG_LOCKED, uint32_t, tcp_recv_throttle_minwin, 16 * 1024,
260 "Minimum recv win for throttling");
39236c6e 261
5ba3f43e 262SYSCTL_SKMEM_TCP_INT(OID_AUTO, enable_tlp,
f427ee49
A
263 CTLFLAG_RW | CTLFLAG_LOCKED,
264 int32_t, tcp_enable_tlp, 1, "Enable Tail loss probe");
39236c6e 265
b0d623f7
A
266static int32_t packchain_newlist = 0;
267static int32_t packchain_looped = 0;
268static int32_t packchain_sent = 0;
9bccf70c
A
269
270/* temporary: for testing */
271#if IPSEC
272extern int ipsec_bypass;
273#endif
1c79356b 274
f427ee49 275extern int slowlink_wsize; /* window correction for slow links */
55e303ae 276
39236c6e 277extern u_int32_t dlil_filter_disable_tso_count;
b0d623f7 278extern u_int32_t kipf_count;
91447636 279
813fb2f6
A
280static int tcp_ip_output(struct socket *, struct tcpcb *, struct mbuf *,
281 int, struct mbuf *, int, int, boolean_t);
39236c6e 282static int tcp_recv_throttle(struct tcpcb *tp);
d12e1678 283
f427ee49
A
284static int32_t
285tcp_tfo_check(struct tcpcb *tp, int32_t len)
3e170ce0
A
286{
287 struct socket *so = tp->t_inpcb->inp_socket;
288 unsigned int optlen = 0;
289 unsigned int cookie_len;
290
f427ee49 291 if (tp->t_flags & TF_NOOPT) {
3e170ce0 292 goto fallback;
f427ee49 293 }
3e170ce0 294
cb323159
A
295 if (!(tp->t_flagsext & TF_FASTOPEN_FORCE_ENABLE) &&
296 !tcp_heuristic_do_tfo(tp)) {
39037602
A
297 tp->t_tfo_stats |= TFO_S_HEURISTICS_DISABLE;
298 tcpstat.tcps_tfo_heuristics_disable++;
3e170ce0 299 goto fallback;
39037602 300 }
3e170ce0 301
f427ee49 302 if (so->so_flags1 & SOF1_DATA_AUTHENTICATED) {
cb323159 303 return len;
f427ee49 304 }
5ba3f43e 305
3e170ce0
A
306 optlen += TCPOLEN_MAXSEG;
307
f427ee49 308 if (tp->t_flags & TF_REQ_SCALE) {
3e170ce0 309 optlen += 4;
f427ee49 310 }
3e170ce0
A
311
312#if MPTCP
313 if ((so->so_flags & SOF_MP_SUBFLOW) && mptcp_enable &&
cb323159 314 (tp->t_rxtshift <= mptcp_mpcap_retries ||
f427ee49 315 (tptomptp(tp)->mpt_mpte->mpte_flags & MPTE_FORCE_ENABLE))) {
3e170ce0 316 optlen += sizeof(struct mptcp_mpcapable_opt_common) + sizeof(mptcp_key_t);
f427ee49 317 }
3e170ce0
A
318#endif /* MPTCP */
319
f427ee49 320 if (tp->t_flags & TF_REQ_TSTMP) {
3e170ce0 321 optlen += TCPOLEN_TSTAMP_APPA;
f427ee49 322 }
3e170ce0 323
f427ee49 324 if (SACK_ENABLED(tp)) {
3e170ce0 325 optlen += TCPOLEN_SACK_PERMITTED;
f427ee49 326 }
3e170ce0
A
327
328 /* Now, decide whether to use TFO or not */
329
330 /* Don't even bother trying if there is no space at all... */
f427ee49 331 if (MAX_TCPOPTLEN - optlen < TCPOLEN_FASTOPEN_REQ) {
3e170ce0 332 goto fallback;
f427ee49 333 }
3e170ce0
A
334
335 cookie_len = tcp_cache_get_cookie_len(tp);
f427ee49 336 if (cookie_len == 0) {
3e170ce0 337 /* No cookie, so we request one */
cb323159 338 return 0;
f427ee49 339 }
3e170ce0 340
d190cdc3 341 /* There is not enough space for the cookie, so we cannot do TFO */
f427ee49 342 if (MAX_TCPOPTLEN - optlen < cookie_len) {
d190cdc3 343 goto fallback;
f427ee49 344 }
d190cdc3 345
3e170ce0 346 /* Do not send SYN+data if there is more in the queue than MSS */
f427ee49 347 if (so->so_snd.sb_cc > (tp->t_maxopd - MAX_TCPOPTLEN)) {
3e170ce0 348 goto fallback;
f427ee49 349 }
3e170ce0
A
350
351 /* Ok, everything looks good. We can go on and do TFO */
cb323159 352 return len;
3e170ce0
A
353
354fallback:
cb323159
A
355 tcp_disable_tfo(tp);
356 return 0;
3e170ce0
A
357}
358
359/* Returns the number of bytes written to the TCP option-space */
f427ee49
A
360static unsigned int
361tcp_tfo_write_cookie_rep(struct tcpcb *tp, unsigned int optlen, u_char *opt)
3e170ce0
A
362{
363 u_char out[CCAES_BLOCK_SIZE];
364 unsigned ret = 0;
365 u_char *bp;
366
367 if ((MAX_TCPOPTLEN - optlen) <
f427ee49 368 (TCPOLEN_FASTOPEN_REQ + TFO_COOKIE_LEN_DEFAULT)) {
cb323159 369 return ret;
f427ee49 370 }
3e170ce0
A
371
372 tcp_tfo_gen_cookie(tp->t_inpcb, out, sizeof(out));
373
374 bp = opt + optlen;
375
376 *bp++ = TCPOPT_FASTOPEN;
377 *bp++ = 2 + TFO_COOKIE_LEN_DEFAULT;
378 memcpy(bp, out, TFO_COOKIE_LEN_DEFAULT);
379 ret += 2 + TFO_COOKIE_LEN_DEFAULT;
380
381 tp->t_tfo_stats |= TFO_S_COOKIE_SENT;
382 tcpstat.tcps_tfo_cookie_sent++;
383
cb323159 384 return ret;
3e170ce0
A
385}
386
f427ee49
A
387static unsigned int
388tcp_tfo_write_cookie(struct tcpcb *tp, unsigned int optlen, int32_t len,
389 u_char *opt)
3e170ce0 390{
f427ee49 391 uint8_t tfo_len;
813fb2f6 392 struct socket *so = tp->t_inpcb->inp_socket;
3e170ce0
A
393 unsigned ret = 0;
394 int res;
395 u_char *bp;
396
f427ee49
A
397 if (TCPOLEN_FASTOPEN_REQ > MAX_TCPOPTLEN - optlen) {
398 return 0;
399 }
400 tfo_len = (uint8_t)(MAX_TCPOPTLEN - optlen - TCPOLEN_FASTOPEN_REQ);
401
5ba3f43e 402 if (so->so_flags1 & SOF1_DATA_AUTHENTICATED) {
813fb2f6
A
403 /* If there is some data, let's track it */
404 if (len > 0) {
405 tp->t_tfo_stats |= TFO_S_SYN_DATA_SENT;
406 tcpstat.tcps_tfo_syn_data_sent++;
407 }
408
cb323159 409 return 0;
813fb2f6
A
410 }
411
3e170ce0
A
412 bp = opt + optlen;
413
414 /*
415 * The cookie will be copied in the appropriate place within the
416 * TCP-option space. That way we avoid the need for an intermediate
417 * variable.
418 */
419 res = tcp_cache_get_cookie(tp, bp + TCPOLEN_FASTOPEN_REQ, &tfo_len);
420 if (res == 0) {
421 *bp++ = TCPOPT_FASTOPEN;
422 *bp++ = TCPOLEN_FASTOPEN_REQ;
423 ret += TCPOLEN_FASTOPEN_REQ;
424
425 tp->t_tfo_flags |= TFO_F_COOKIE_REQ;
426
427 tp->t_tfo_stats |= TFO_S_COOKIE_REQ;
428 tcpstat.tcps_tfo_cookie_req++;
429 } else {
430 *bp++ = TCPOPT_FASTOPEN;
431 *bp++ = TCPOLEN_FASTOPEN_REQ + tfo_len;
432
433 ret += TCPOLEN_FASTOPEN_REQ + tfo_len;
434
435 tp->t_tfo_flags |= TFO_F_COOKIE_SENT;
436
437 /* If there is some data, let's track it */
813fb2f6 438 if (len > 0) {
3e170ce0
A
439 tp->t_tfo_stats |= TFO_S_SYN_DATA_SENT;
440 tcpstat.tcps_tfo_syn_data_sent++;
441 }
442 }
443
cb323159 444 return ret;
3e170ce0
A
445}
446
447static inline bool
f427ee49 448tcp_send_ecn_flags_on_syn(struct tcpcb *tp)
3e170ce0 449{
f427ee49 450 return !(tp->ecn_flags & TE_SETUPSENT);
3e170ce0
A
451}
452
4bd07ac2
A
453void
454tcp_set_ecn(struct tcpcb *tp, struct ifnet *ifp)
455{
456 boolean_t inbound;
457
458 /*
459 * Socket option has precedence
460 */
461 if (tp->ecn_flags & TE_ECN_MODE_ENABLE) {
462 tp->ecn_flags |= TE_ENABLE_ECN;
463 goto check_heuristic;
464 }
465
466 if (tp->ecn_flags & TE_ECN_MODE_DISABLE) {
467 tp->ecn_flags &= ~TE_ENABLE_ECN;
468 return;
469 }
470 /*
471 * Per interface setting comes next
472 */
473 if (ifp != NULL) {
474 if (ifp->if_eflags & IFEF_ECN_ENABLE) {
475 tp->ecn_flags |= TE_ENABLE_ECN;
476 goto check_heuristic;
477 }
478
479 if (ifp->if_eflags & IFEF_ECN_DISABLE) {
480 tp->ecn_flags &= ~TE_ENABLE_ECN;
481 return;
482 }
483 }
484 /*
485 * System wide settings come last
486 */
487 inbound = (tp->t_inpcb->inp_socket->so_head != NULL);
488 if ((inbound && tcp_ecn_inbound == 1) ||
489 (!inbound && tcp_ecn_outbound == 1)) {
490 tp->ecn_flags |= TE_ENABLE_ECN;
491 goto check_heuristic;
492 } else {
493 tp->ecn_flags &= ~TE_ENABLE_ECN;
494 }
495
496 return;
497
498check_heuristic:
f427ee49 499 if (!tcp_heuristic_do_ecn(tp)) {
4bd07ac2 500 tp->ecn_flags &= ~TE_ENABLE_ECN;
f427ee49 501 }
490019cf
A
502
503 /*
504 * If the interface setting, system-level setting and heuristics
505 * allow to enable ECN, randomly select 5% of connections to
506 * enable it
507 */
508 if ((tp->ecn_flags & (TE_ECN_MODE_ENABLE | TE_ECN_MODE_DISABLE
509 | TE_ENABLE_ECN)) == TE_ENABLE_ECN) {
510 /*
511 * Use the random value in iss for randomizing
512 * this selection
513 */
f427ee49 514 if ((tp->iss % 100) >= tcp_ecn_setup_percentage) {
490019cf 515 tp->ecn_flags &= ~TE_ENABLE_ECN;
f427ee49
A
516 }
517 }
518}
519
520int
521tcp_flight_size(struct tcpcb *tp)
522{
523 int ret;
524
525 VERIFY(tp->sackhint.sack_bytes_acked >= 0);
526 VERIFY(tp->sackhint.sack_bytes_rexmit >= 0);
527
528 /*
529 * RFC6675, SetPipe (), SACK'd bytes are discounted. All the rest is still in-flight.
530 */
531 ret = tp->snd_nxt - tp->snd_una - tp->sackhint.sack_bytes_acked;
532
533 if (ret < 0) {
534 /*
535 * This happens when the RTO-timer fires because snd_nxt gets artificially
536 * decreased. If we then receive some SACK-blogs, sack_bytes_acked is
537 * going to be high.
538 */
539 ret = 0;
490019cf 540 }
f427ee49
A
541
542 return ret;
4bd07ac2
A
543}
544
1c79356b
A
545/*
546 * Tcp output routine: figure out what should be sent and send it.
2d21ac55
A
547 *
548 * Returns: 0 Success
549 * EADDRNOTAVAIL
550 * ENOBUFS
551 * EMSGSIZE
552 * EHOSTUNREACH
553 * ENETDOWN
554 * ip_output_list:ENOMEM
555 * ip_output_list:EADDRNOTAVAIL
556 * ip_output_list:ENETUNREACH
557 * ip_output_list:EHOSTUNREACH
558 * ip_output_list:EACCES
559 * ip_output_list:EMSGSIZE
560 * ip_output_list:ENOBUFS
561 * ip_output_list:??? [ignorable: mostly IPSEC/firewall/DLIL]
316670eb
A
562 * ip6_output_list:EINVAL
563 * ip6_output_list:EOPNOTSUPP
564 * ip6_output_list:EHOSTUNREACH
565 * ip6_output_list:EADDRNOTAVAIL
566 * ip6_output_list:ENETUNREACH
567 * ip6_output_list:EMSGSIZE
568 * ip6_output_list:ENOBUFS
569 * ip6_output_list:??? [ignorable: mostly IPSEC/firewall/DLIL]
1c79356b
A
570 */
571int
8ad349bb 572tcp_output(struct tcpcb *tp)
1c79356b 573{
39236c6e
A
574 struct inpcb *inp = tp->t_inpcb;
575 struct socket *so = inp->inp_socket;
b0d623f7 576 int32_t len, recwin, sendwin, off;
f427ee49
A
577 uint8_t flags;
578 int error;
fe8ab488 579 struct mbuf *m;
1c79356b 580 struct ip *ip = NULL;
1c79356b 581 struct ip6_hdr *ip6 = NULL;
fe8ab488 582 struct tcphdr *th;
1c79356b 583 u_char opt[TCP_MAXOLEN];
f427ee49 584 unsigned int ipoptlen, optlen, hdrlen;
2d21ac55 585 int idle, sendalot, lost = 0;
8ad349bb 586 int i, sack_rxmit;
b0d623f7 587 int tso = 0;
8ad349bb 588 int sack_bytes_rxmt;
3e170ce0 589 tcp_seq old_snd_nxt = 0;
8ad349bb 590 struct sackhole *p;
39236c6e 591#if IPSEC
f427ee49 592 unsigned int ipsec_optlen = 0;
39236c6e 593#endif /* IPSEC */
6d2010ae 594 int idle_time = 0;
2d21ac55 595 struct mbuf *packetlist = NULL;
39236c6e 596 struct mbuf *tp_inp_options = inp->inp_depend4.inp4_options;
f427ee49
A
597 int isipv6 = inp->inp_vflag & INP_IPV6;
598 int packchain_listadd = 0;
2d21ac55
A
599 int so_options = so->so_options;
600 struct rtentry *rt;
39037602 601 u_int32_t svc_flags = 0, allocated_len;
f427ee49 602 unsigned int sackoptlen = 0;
39236c6e 603#if MPTCP
5ba3f43e 604 boolean_t mptcp_acknow;
39236c6e
A
605#endif /* MPTCP */
606 boolean_t cell = FALSE;
607 boolean_t wifi = FALSE;
fe8ab488 608 boolean_t wired = FALSE;
3e170ce0 609 boolean_t sack_rescue_rxt = FALSE;
39037602 610 int sotc = so->so_traffic_class;
f427ee49
A
611 boolean_t do_not_compress = FALSE;
612 boolean_t sack_rxmted = FALSE;
55e303ae 613
1c79356b
A
614 /*
615 * Determine length of data that should be transmitted,
616 * and flags that will be used.
617 * If there is some data or critical controls (SYN, RST)
618 * to send, then transmit; otherwise, investigate further.
619 */
8ad349bb 620 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
6d2010ae
A
621
622 /* Since idle_time is signed integer, the following integer subtraction
623 * will take care of wrap around of tcp_now
624 */
625 idle_time = tcp_now - tp->t_rcvtime;
316670eb 626 if (idle && idle_time >= TCP_IDLETIMEOUT(tp)) {
3e170ce0
A
627 if (CC_ALGO(tp)->after_idle != NULL &&
628 (tp->tcp_cc_index != TCP_CC_ALGO_CUBIC_INDEX ||
629 idle_time >= TCP_CC_CWND_NONVALIDATED_PERIOD)) {
6d2010ae 630 CC_ALGO(tp)->after_idle(tp);
3e170ce0
A
631 tcp_ccdbg_trace(tp, NULL, TCP_CC_IDLE_TIMEOUT);
632 }
633
634 /*
635 * Do some other tasks that need to be done after
636 * idle time
637 */
f427ee49 638 if (!SLIST_EMPTY(&tp->t_rxt_segments)) {
3e170ce0 639 tcp_rxtseg_clean(tp);
f427ee49 640 }
3e170ce0
A
641
642 /* If stretch ack was auto-disabled, re-evaluate it */
643 tcp_cc_after_idle_stretchack(tp);
f427ee49 644 tp->t_forced_acks = TCP_FORCED_ACKS_COUNT;
9bccf70c 645 }
8ad349bb
A
646 tp->t_flags &= ~TF_LASTIDLE;
647 if (idle) {
648 if (tp->t_flags & TF_MORETOCOME) {
649 tp->t_flags |= TF_LASTIDLE;
650 idle = 0;
651 }
652 }
39037602 653#if MPTCP
39236c6e
A
654 if (tp->t_mpflags & TMPF_RESET) {
655 tcp_check_timer_state(tp);
39037602
A
656 /*
657 * Once a RST has been sent for an MPTCP subflow,
39236c6e
A
658 * the subflow socket stays around until deleted.
659 * No packets such as FINs must be sent after RST.
660 */
cb323159 661 return 0;
39236c6e
A
662 }
663#endif /* MPTCP */
664
1c79356b 665again:
5ba3f43e
A
666#if MPTCP
667 mptcp_acknow = FALSE;
668#endif
f427ee49 669 do_not_compress = FALSE;
5ba3f43e 670
f427ee49 671 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_START, 0, 0, 0, 0, 0);
55e303ae 672
55e303ae 673 if (isipv6) {
55e303ae 674 KERNEL_DEBUG(DBG_LAYER_BEG,
f427ee49
A
675 ((inp->inp_fport << 16) | inp->inp_lport),
676 (((inp->in6p_laddr.s6_addr16[0] & 0xffff) << 16) |
677 (inp->in6p_faddr.s6_addr16[0] & 0xffff)),
678 sendalot, 0, 0);
679 } else {
55e303ae 680 KERNEL_DEBUG(DBG_LAYER_BEG,
f427ee49
A
681 ((inp->inp_fport << 16) | inp->inp_lport),
682 (((inp->inp_laddr.s_addr & 0xffff) << 16) |
683 (inp->inp_faddr.s_addr & 0xffff)),
684 sendalot, 0, 0);
316670eb 685 }
55e303ae
A
686 /*
687 * If the route generation id changed, we need to check that our
688 * local (source) IP address is still valid. If it isn't either
689 * return error or silently do nothing (assuming the address will
690 * come back before the TCP connection times out).
691 */
39236c6e
A
692 rt = inp->inp_route.ro_rt;
693 if (rt != NULL && ROUTE_UNUSABLE(&tp->t_inpcb->inp_route)) {
2d21ac55 694 struct ifnet *ifp;
316670eb
A
695 struct in_ifaddr *ia = NULL;
696 struct in6_ifaddr *ia6 = NULL;
697 int found_srcaddr = 0;
2d21ac55
A
698
699 /* disable multipages at the socket */
700 somultipages(so, FALSE);
55e303ae 701
b0d623f7
A
702 /* Disable TSO for the socket until we know more */
703 tp->t_flags &= ~TF_TSO;
704
fe8ab488
A
705 soif2kcl(so, FALSE);
706
316670eb 707 if (isipv6) {
39236c6e 708 ia6 = ifa_foraddr6(&inp->in6p_laddr);
f427ee49 709 if (ia6 != NULL) {
316670eb 710 found_srcaddr = 1;
f427ee49 711 }
316670eb 712 } else {
39236c6e 713 ia = ifa_foraddr(inp->inp_laddr.s_addr);
f427ee49 714 if (ia != NULL) {
316670eb 715 found_srcaddr = 1;
f427ee49 716 }
316670eb
A
717 }
718
55e303ae 719 /* check that the source address is still valid */
316670eb 720 if (found_srcaddr == 0) {
316670eb
A
721 soevent(so,
722 (SO_FILT_HINT_LOCKED | SO_FILT_HINT_NOSRCADDR));
2d21ac55 723
55e303ae 724 if (tp->t_state >= TCPS_CLOSE_WAIT) {
2d21ac55 725 tcp_drop(tp, EADDRNOTAVAIL);
cb323159 726 return EADDRNOTAVAIL;
55e303ae
A
727 }
728
cb323159
A
729 /*
730 * Set retransmit timer if it wasn't set,
55e303ae 731 * reset Persist timer and shift register as the
6d2010ae 732 * advertised peer window may not be valid anymore
55e303ae 733 */
cb323159 734 if (tp->t_timer[TCPT_REXMT] == 0) {
fe8ab488
A
735 tp->t_timer[TCPT_REXMT] =
736 OFFSET_FROM_START(tp, tp->t_rxtcur);
cb323159 737 if (tp->t_timer[TCPT_PERSIST] != 0) {
55e303ae 738 tp->t_timer[TCPT_PERSIST] = 0;
6d2010ae 739 tp->t_persist_stop = 0;
39037602 740 TCP_RESET_REXMT_STATE(tp);
55e303ae
A
741 }
742 }
743
f427ee49 744 if (tp->t_pktlist_head != NULL) {
2d21ac55 745 m_freem_list(tp->t_pktlist_head);
f427ee49 746 }
2d21ac55
A
747 TCP_PKTLIST_CLEAR(tp);
748
749 /* drop connection if source address isn't available */
39037602 750 if (so->so_flags & SOF_NOADDRAVAIL) {
2d21ac55 751 tcp_drop(tp, EADDRNOTAVAIL);
cb323159 752 return EADDRNOTAVAIL;
fe8ab488 753 } else {
6d2010ae 754 tcp_check_timer_state(tp);
cb323159 755 return 0; /* silently ignore, keep data in socket: address may be back */
6d2010ae 756 }
55e303ae 757 }
f427ee49 758 if (ia != NULL) {
316670eb 759 IFA_REMREF(&ia->ia_ifa);
f427ee49 760 }
316670eb 761
f427ee49 762 if (ia6 != NULL) {
316670eb 763 IFA_REMREF(&ia6->ia_ifa);
f427ee49 764 }
2d21ac55
A
765
766 /*
767 * Address is still valid; check for multipages capability
768 * again in case the outgoing interface has changed.
769 */
b0d623f7
A
770 RT_LOCK(rt);
771 if ((ifp = rt->rt_ifp) != NULL) {
2d21ac55 772 somultipages(so, (ifp->if_hwassist & IFNET_MULTIPAGES));
b0d623f7 773 tcp_set_tso(tp, ifp);
4bd07ac2
A
774 soif2kcl(so, (ifp->if_eflags & IFEF_2KCL));
775 tcp_set_ecn(tp, ifp);
b0d623f7 776 }
f427ee49 777 if (rt->rt_flags & RTF_UP) {
39236c6e 778 RT_GENID_SYNC(rt);
f427ee49 779 }
2d21ac55
A
780 /*
781 * See if we should do MTU discovery. Don't do it if:
782 * 1) it is disabled via the sysctl
783 * 2) the route isn't up
fe8ab488
A
784 * 3) the MTU is locked (if it is, then discovery
785 * has been disabled)
2d21ac55
A
786 */
787
cb323159 788 if (!path_mtu_discovery || ((rt != NULL) &&
fe8ab488 789 (!(rt->rt_flags & RTF_UP) ||
f427ee49 790 (rt->rt_rmx.rmx_locks & RTV_MTU)))) {
2d21ac55 791 tp->t_flags &= ~TF_PMTUD;
f427ee49 792 } else {
2d21ac55 793 tp->t_flags |= TF_PMTUD;
f427ee49 794 }
2d21ac55 795
b0d623f7 796 RT_UNLOCK(rt);
55e303ae 797 }
8ad349bb 798
39236c6e
A
799 if (rt != NULL) {
800 cell = IFNET_IS_CELLULAR(rt->rt_ifp);
801 wifi = (!cell && IFNET_IS_WIFI(rt->rt_ifp));
fe8ab488 802 wired = (!wifi && IFNET_IS_WIRED(rt->rt_ifp));
39236c6e
A
803 }
804
8ad349bb
A
805 /*
806 * If we've recently taken a timeout, snd_max will be greater than
807 * snd_nxt. There may be SACK information that allows us to avoid
808 * resending already delivered data. Adjust snd_nxt accordingly.
809 */
f427ee49 810 if (SACK_ENABLED(tp) && SEQ_LT(tp->snd_nxt, tp->snd_max)) {
8ad349bb 811 tcp_sack_adjust(tp);
f427ee49 812 }
1c79356b
A
813 sendalot = 0;
814 off = tp->snd_nxt - tp->snd_una;
8ad349bb
A
815 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
816
f427ee49 817 if (tp->t_flags & TF_SLOWLINK && slowlink_wsize > 0) {
8ad349bb 818 sendwin = min(sendwin, slowlink_wsize);
f427ee49 819 }
1c79356b
A
820
821 flags = tcp_outflags[tp->t_state];
8ad349bb 822 /*
fe8ab488
A
823 * Send any SACK-generated retransmissions. If we're explicitly
824 * trying to send out new data (when sendalot is 1), bypass this
825 * function. If we retransmit in fast recovery mode, decrement
826 * snd_cwnd, since we're replacing a (future) new transmission
827 * with a retransmission now, and we previously incremented
828 * snd_cwnd in tcp_input().
8ad349bb
A
829 */
830 /*
831 * Still in sack recovery , reset rxmit flag to zero.
832 */
833 sack_rxmit = 0;
834 sack_bytes_rxmt = 0;
835 len = 0;
836 p = NULL;
39236c6e 837 if (SACK_ENABLED(tp) && IN_FASTRECOVERY(tp) &&
8ad349bb 838 (p = tcp_sack_output(tp, &sack_bytes_rxmt))) {
b0d623f7 839 int32_t cwin;
39037602 840
f427ee49
A
841 if (tcp_do_better_lr) {
842 cwin = min(tp->snd_wnd, tp->snd_cwnd) - tcp_flight_size(tp);
843 if (cwin <= 0 && sack_rxmted == FALSE) {
844 /* Allow to clock out at least on per period */
845 cwin = tp->t_maxseg;
846 }
847
848 sack_rxmted = TRUE;
849 } else {
850 cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
851 }
852 if (cwin < 0) {
8ad349bb 853 cwin = 0;
f427ee49 854 }
8ad349bb
A
855 /* Do not retransmit SACK segments beyond snd_recover */
856 if (SEQ_GT(p->end, tp->snd_recover)) {
857 /*
858 * (At least) part of sack hole extends beyond
859 * snd_recover. Check to see if we can rexmit data
860 * for this hole.
861 */
862 if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
863 /*
864 * Can't rexmit any more data for this hole.
865 * That data will be rexmitted in the next
866 * sack recovery episode, when snd_recover
867 * moves past p->rxmit.
868 */
869 p = NULL;
870 goto after_sack_rexmit;
f427ee49 871 } else {
8ad349bb 872 /* Can rexmit part of the current hole */
b0d623f7 873 len = ((int32_t)min(cwin,
f427ee49
A
874 tp->snd_recover - p->rxmit));
875 }
316670eb 876 } else {
b0d623f7 877 len = ((int32_t)min(cwin, p->end - p->rxmit));
316670eb 878 }
8ad349bb 879 if (len > 0) {
39037602 880 off = p->rxmit - tp->snd_una;
8ad349bb
A
881 sack_rxmit = 1;
882 sendalot = 1;
f427ee49
A
883 /* Everything sent after snd_nxt will allow us to account for fast-retransmit of the retransmitted segment */
884 tp->send_highest_sack = tp->snd_nxt;
885 tp->t_new_dupacks = 0;
8ad349bb
A
886 tcpstat.tcps_sack_rexmits++;
887 tcpstat.tcps_sack_rexmit_bytes +=
888 min(len, tp->t_maxseg);
316670eb 889 } else {
b0d623f7 890 len = 0;
316670eb 891 }
8ad349bb
A
892 }
893after_sack_rexmit:
1c79356b
A
894 /*
895 * Get standard flags, and add SYN or FIN if requested by 'hidden'
896 * state flags.
897 */
f427ee49 898 if (tp->t_flags & TF_NEEDFIN) {
1c79356b 899 flags |= TH_FIN;
f427ee49 900 }
1c79356b
A
901
902 /*
903 * If in persist timeout with window of 0, send 1 byte.
904 * Otherwise, if window is small but nonzero
905 * and timer expired, we will send what we can
906 * and go to transmit state.
907 */
fe8ab488 908 if (tp->t_flagsext & TF_FORCE) {
8ad349bb 909 if (sendwin == 0) {
1c79356b
A
910 /*
911 * If we still have some data to send, then
912 * clear the FIN bit. Usually this would
913 * happen below when it realizes that we
914 * aren't sending all the data. However,
915 * if we have exactly 1 byte of unsent data,
916 * then it won't clear the FIN bit below,
917 * and if we are in persist state, we wind
918 * up sending the packet without recording
919 * that we sent the FIN bit.
920 *
921 * We can't just blindly clear the FIN bit,
922 * because if we don't have any more data
923 * to send then the probe will be the FIN
924 * itself.
925 */
f427ee49 926 if (off < so->so_snd.sb_cc) {
1c79356b 927 flags &= ~TH_FIN;
f427ee49 928 }
8ad349bb 929 sendwin = 1;
1c79356b
A
930 } else {
931 tp->t_timer[TCPT_PERSIST] = 0;
6d2010ae 932 tp->t_persist_stop = 0;
39037602 933 TCP_RESET_REXMT_STATE(tp);
1c79356b
A
934 }
935 }
936
8ad349bb
A
937 /*
938 * If snd_nxt == snd_max and we have transmitted a FIN, the
939 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
940 * a negative length. This can also occur when TCP opens up
941 * its congestion window while receiving additional duplicate
942 * acks after fast-retransmit because TCP will reset snd_nxt
943 * to snd_max after the fast-retransmit.
944 *
945 * In the normal retransmit-FIN-only case, however, snd_nxt will
946 * be set to snd_una, the offset will be 0, and the length may
947 * wind up 0.
948 *
949 * If sack_rxmit is true we are retransmitting from the scoreboard
950 * in which case len is already set.
951 */
952 if (sack_rxmit == 0) {
3e170ce0 953 if (sack_bytes_rxmt == 0) {
b0d623f7 954 len = min(so->so_snd.sb_cc, sendwin) - off;
3e170ce0 955 } else {
b0d623f7 956 int32_t cwin;
8ad349bb 957
f427ee49
A
958 if (tcp_do_better_lr) {
959 cwin = tp->snd_cwnd - tcp_flight_size(tp);
960 } else {
961 cwin = tp->snd_cwnd -
962 (tp->snd_nxt - tp->sack_newdata) -
963 sack_bytes_rxmt;
964 }
965 if (cwin < 0) {
3e170ce0 966 cwin = 0;
f427ee49
A
967 }
968 /*
8ad349bb
A
969 * We are inside of a SACK recovery episode and are
970 * sending new data, having retransmitted all the
971 * data possible in the scoreboard.
972 */
f427ee49 973 len = min(so->so_snd.sb_cc, tp->snd_wnd) - off;
8ad349bb
A
974 /*
975 * Don't remove this (len > 0) check !
39037602
A
976 * We explicitly check for len > 0 here (although it
977 * isn't really necessary), to work around a gcc
8ad349bb
A
978 * optimization issue - to force gcc to compute
979 * len above. Without this check, the computation
980 * of len is bungled by the optimizer.
981 */
982 if (len > 0) {
b0d623f7 983 len = imin(len, cwin);
3e170ce0 984 } else {
b0d623f7 985 len = 0;
3e170ce0
A
986 }
987 /*
988 * At this point SACK recovery can not send any
989 * data from scoreboard or any new data. Check
990 * if we can do a rescue retransmit towards the
991 * tail end of recovery window.
992 */
993 if (len == 0 && cwin > 0 &&
994 SEQ_LT(tp->snd_fack, tp->snd_recover) &&
995 !(tp->t_flagsext & TF_RESCUE_RXT)) {
996 len = min((tp->snd_recover - tp->snd_fack),
997 tp->t_maxseg);
998 len = imin(len, cwin);
999 old_snd_nxt = tp->snd_nxt;
1000 sack_rescue_rxt = TRUE;
1001 tp->snd_nxt = tp->snd_recover - len;
1002 /*
1003 * If FIN has been sent, snd_max
1004 * must have been advanced to cover it.
1005 */
1006 if ((tp->t_flags & TF_SENTFIN) &&
f427ee49 1007 tp->snd_max == tp->snd_recover) {
3e170ce0 1008 tp->snd_nxt--;
f427ee49 1009 }
3e170ce0
A
1010
1011 off = tp->snd_nxt - tp->snd_una;
1012 sendalot = 0;
1013 tp->t_flagsext |= TF_RESCUE_RXT;
1014 }
8ad349bb 1015 }
1c79356b
A
1016 }
1017
1018 /*
1019 * Lop off SYN bit if it has already been sent. However, if this
1020 * is SYN-SENT state and if segment contains data and if we don't
1021 * know that foreign host supports TAO, suppress sending segment.
1022 */
1023 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
ea3f0419
A
1024 if (tp->t_state == TCPS_SYN_RECEIVED && tfo_enabled(tp) && tp->snd_nxt == tp->snd_una + 1) {
1025 /* We are sending the SYN again! */
1026 off--;
1027 len++;
1028 } else {
1029 if (tp->t_state != TCPS_SYN_RECEIVED || tfo_enabled(tp)) {
1030 flags &= ~TH_SYN;
2d21ac55 1031 }
fe8ab488 1032
ea3f0419
A
1033 off--;
1034 len++;
1035 if (len > 0 && tp->t_state == TCPS_SYN_SENT) {
1036 while (inp->inp_sndinprog_cnt == 0 &&
f427ee49 1037 tp->t_pktlist_head != NULL) {
ea3f0419
A
1038 packetlist = tp->t_pktlist_head;
1039 packchain_listadd = tp->t_lastchain;
1040 packchain_sent++;
1041 TCP_PKTLIST_CLEAR(tp);
1042
1043 error = tcp_ip_output(so, tp, packetlist,
1044 packchain_listadd, tp_inp_options,
1045 (so_options & SO_DONTROUTE),
1046 (sack_rxmit || (sack_bytes_rxmt != 0)),
1047 isipv6);
1048 }
1049
1050 /*
1051 * tcp was closed while we were in ip,
1052 * resume close
1053 */
1054 if (inp->inp_sndinprog_cnt == 0 &&
f427ee49 1055 (tp->t_flags & TF_CLOSING)) {
ea3f0419
A
1056 tp->t_flags &= ~TF_CLOSING;
1057 (void) tcp_close(tp);
1058 } else {
1059 tcp_check_timer_state(tp);
1060 }
1061 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END,
f427ee49 1062 0, 0, 0, 0, 0);
ea3f0419 1063 return 0;
91447636 1064 }
1c79356b
A
1065 }
1066 }
1067
1068 /*
8ad349bb 1069 * Be careful not to send data and/or FIN on SYN segments.
1c79356b
A
1070 * This measure is needed to prevent interoperability problems
1071 * with not fully conformant TCP implementations.
3e170ce0
A
1072 *
1073 * In case of TFO, we handle the setting of the len in
1074 * tcp_tfo_check. In case TFO is not enabled, never ever send
1075 * SYN+data.
1c79356b 1076 */
3e170ce0 1077 if ((flags & TH_SYN) && !tfo_enabled(tp)) {
1c79356b
A
1078 len = 0;
1079 flags &= ~TH_FIN;
1080 }
1081
cb323159
A
1082 /*
1083 * Don't send a RST with data.
1084 */
f427ee49 1085 if (flags & TH_RST) {
cb323159 1086 len = 0;
f427ee49 1087 }
cb323159 1088
f427ee49 1089 if ((flags & TH_SYN) && tp->t_state <= TCPS_SYN_SENT && tfo_enabled(tp)) {
3e170ce0 1090 len = tcp_tfo_check(tp, len);
f427ee49 1091 }
3e170ce0 1092
fe8ab488
A
1093 /*
1094 * The check here used to be (len < 0). Some times len is zero
1095 * when the congestion window is closed and we need to check
39037602 1096 * if persist timer has to be set in that case. But don't set
fe8ab488 1097 * persist until connection is established.
39037602 1098 */
6d2010ae 1099 if (len <= 0 && !(flags & TH_SYN)) {
1c79356b
A
1100 /*
1101 * If FIN has been sent but not acked,
1102 * but we haven't been called to retransmit,
8ad349bb 1103 * len will be < 0. Otherwise, window shrank
1c79356b
A
1104 * after we sent into it. If window shrank to 0,
1105 * cancel pending retransmit, pull snd_nxt back
1106 * to (closed) window, and set the persist timer
1107 * if it isn't already going. If the window didn't
1108 * close completely, just wait for an ACK.
1109 */
1110 len = 0;
8ad349bb 1111 if (sendwin == 0) {
1c79356b 1112 tp->t_timer[TCPT_REXMT] = 0;
fe8ab488 1113 tp->t_timer[TCPT_PTO] = 0;
39037602 1114 TCP_RESET_REXMT_STATE(tp);
1c79356b 1115 tp->snd_nxt = tp->snd_una;
fe8ab488 1116 off = 0;
f427ee49 1117 if (tp->t_timer[TCPT_PERSIST] == 0) {
1c79356b 1118 tcp_setpersist(tp);
f427ee49 1119 }
1c79356b
A
1120 }
1121 }
8ad349bb 1122
fe8ab488
A
1123 /*
1124 * Automatic sizing of send socket buffer. Increase the send
1125 * socket buffer size if all of the following criteria are met
316670eb
A
1126 * 1. the receiver has enough buffer space for this data
1127 * 2. send buffer is filled to 7/8th with data (so we actually
1128 * have data to make use of it);
1129 * 3. our send window (slow start and congestion controlled) is
1130 * larger than sent but unacknowledged data in send buffer.
1131 */
f427ee49 1132 if (!INP_WAIT_FOR_IF_FEEDBACK(inp) && !IN_FASTRECOVERY(tp) &&
316670eb
A
1133 (so->so_snd.sb_flags & (SB_AUTOSIZE | SB_TRIM)) == SB_AUTOSIZE &&
1134 tcp_cansbgrow(&so->so_snd)) {
1135 if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
fe8ab488 1136 so->so_snd.sb_cc >= (so->so_snd.sb_hiwat / 8 * 7) &&
39037602
A
1137 sendwin >= (so->so_snd.sb_cc - (tp->snd_nxt - tp->snd_una))) {
1138 if (sbreserve(&so->so_snd,
1139 min(so->so_snd.sb_hiwat + tcp_autosndbuf_inc,
1140 tcp_autosndbuf_max)) == 1) {
1141 so->so_snd.sb_idealsize = so->so_snd.sb_hiwat;
316670eb
A
1142 }
1143 }
1144 }
1145
8ad349bb 1146 /*
b0d623f7
A
1147 * Truncate to the maximum segment length or enable TCP Segmentation
1148 * Offloading (if supported by hardware) and ensure that FIN is removed
1149 * if the length no longer contains the last data byte.
1150 *
39236c6e
A
1151 * TSO may only be used if we are in a pure bulk sending state.
1152 * The presence of TCP-MD5, SACK retransmits, SACK advertizements,
f427ee49 1153 * filters and IP options, as well as disabling hardware checksum
39236c6e 1154 * offload prevent using TSO. With TSO the TCP header is the same
b0d623f7
A
1155 * (except for the sequence number) for all generated packets. This
1156 * makes it impossible to transmit any options which vary per generated
1157 * segment or packet.
1158 *
1159 * The length of TSO bursts is limited to TCP_MAXWIN. That limit and
1160 * removal of FIN (if not already catched here) are handled later after
1161 * the exact length of the TCP options are known.
1162 */
1163#if IPSEC
1164 /*
1165 * Pre-calculate here as we save another lookup into the darknesses
1166 * of IPsec that way and can actually decide if TSO is ok.
8ad349bb 1167 */
f427ee49
A
1168 if (ipsec_bypass == 0) {
1169 ipsec_optlen = (unsigned int)ipsec_hdrsiz_tcp(tp);
1170 }
b0d623f7 1171#endif
1c79356b 1172 if (len > tp->t_maxseg) {
39236c6e 1173 if ((tp->t_flags & TF_TSO) && tcp_do_tso && hwcksum_tx &&
fe8ab488
A
1174 ip_use_randomid && kipf_count == 0 &&
1175 dlil_filter_disable_tso_count == 0 &&
f427ee49 1176 tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
fe8ab488 1177 sack_bytes_rxmt == 0 &&
39236c6e
A
1178 inp->inp_options == NULL &&
1179 inp->in6p_options == NULL
b0d623f7
A
1180#if IPSEC
1181 && ipsec_optlen == 0
b0d623f7
A
1182#endif
1183 ) {
1184 tso = 1;
1185 sendalot = 0;
1186 } else {
1187 len = tp->t_maxseg;
1188 sendalot = 1;
1189 tso = 0;
1190 }
f427ee49
A
1191 } else {
1192 tso = 0;
1c79356b 1193 }
fe8ab488
A
1194
1195 /* Send one segment or less as a tail loss probe */
1196 if (tp->t_flagsext & TF_SENT_TLPROBE) {
1197 len = min(len, tp->t_maxseg);
1198 sendalot = 0;
1199 tso = 0;
1200 }
1201
39236c6e 1202#if MPTCP
cb323159
A
1203 if (so->so_flags & SOF_MP_SUBFLOW && off < 0) {
1204 os_log_error(mptcp_log_handle, "%s - %lx: offset is negative! len %d off %d\n",
1205 __func__, (unsigned long)VM_KERNEL_ADDRPERM(tp->t_mpsub->mpts_mpte),
1206 len, off);
1207 }
1208
39037602 1209 if ((so->so_flags & SOF_MP_SUBFLOW) &&
fe8ab488 1210 !(tp->t_mpflags & TMPF_TCP_FALLBACK)) {
39236c6e 1211 int newlen = len;
5ba3f43e
A
1212 if (tp->t_state >= TCPS_ESTABLISHED &&
1213 (tp->t_mpflags & TMPF_SND_MPPRIO ||
f427ee49
A
1214 tp->t_mpflags & TMPF_SND_REM_ADDR ||
1215 tp->t_mpflags & TMPF_SND_MPFAIL ||
1216 tp->t_mpflags & TMPF_SND_KEYS ||
1217 tp->t_mpflags & TMPF_SND_JACK)) {
39236c6e
A
1218 if (len > 0) {
1219 len = 0;
f427ee49 1220 tso = 0;
39236c6e 1221 }
5ba3f43e
A
1222 /*
1223 * On a new subflow, don't try to send again, because
1224 * we are still waiting for the fourth ack.
1225 */
f427ee49 1226 if (!(tp->t_mpflags & TMPF_PREESTABLISHED)) {
5ba3f43e 1227 sendalot = 1;
f427ee49 1228 }
fe8ab488 1229 mptcp_acknow = TRUE;
39236c6e 1230 } else {
fe8ab488 1231 mptcp_acknow = FALSE;
39236c6e
A
1232 }
1233 /*
1234 * The contiguous bytes in the subflow socket buffer can be
39037602 1235 * discontiguous at the MPTCP level. Since only one DSS
39236c6e
A
1236 * option can be sent in one packet, reduce length to match
1237 * the contiguous MPTCP level. Set sendalot to send remainder.
1238 */
cb323159 1239 if (len > 0 && off >= 0) {
5ba3f43e 1240 newlen = mptcp_adj_sendlen(so, off);
cb323159
A
1241 }
1242
39236c6e
A
1243 if (newlen < len) {
1244 len = newlen;
f427ee49
A
1245 if (len <= tp->t_maxseg) {
1246 tso = 0;
39236c6e
A
1247 }
1248 }
1249 }
f427ee49 1250#endif /* MPTCP */
39236c6e 1251
fe8ab488 1252 if (sack_rxmit) {
f427ee49 1253 if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc)) {
fe8ab488 1254 flags &= ~TH_FIN;
f427ee49 1255 }
fe8ab488 1256 } else {
f427ee49 1257 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) {
fe8ab488 1258 flags &= ~TH_FIN;
f427ee49 1259 }
fe8ab488 1260 }
39037602
A
1261 /*
1262 * Compare available window to amount of window
1263 * known to peer (as advertised window less
1264 * next expected input). If the difference is at least two
1265 * max size segments, or at least 25% of the maximum possible
1266 * window, then want to send a window update to peer.
39037602 1267 */
fe8ab488 1268 recwin = tcp_sbspace(tp);
cb323159
A
1269
1270 if (!(so->so_flags & SOF_MP_SUBFLOW)) {
1271 if (recwin < (int32_t)(so->so_rcv.sb_hiwat / 4) &&
1272 recwin < (int)tp->t_maxseg) {
1273 recwin = 0;
1274 }
1275 } else {
39037602 1276 struct mptcb *mp_tp = tptomptp(tp);
cb323159 1277 struct socket *mp_so = mptetoso(mp_tp->mpt_mpte);
39037602 1278
cb323159
A
1279 if (recwin < (int32_t)(mp_so->so_rcv.sb_hiwat / 4) &&
1280 recwin < (int)tp->t_maxseg) {
1281 recwin = 0;
39037602
A
1282 }
1283 }
39037602
A
1284
1285#if TRAFFIC_MGT
813fb2f6 1286 if (tcp_recv_bg == 1 || IS_TCP_RECV_BG(so)) {
d190cdc3
A
1287 if (recwin > 0 && tcp_recv_throttle(tp)) {
1288 uint32_t min_iaj_win = tcp_min_iaj_win * tp->t_maxseg;
813fb2f6 1289 uint32_t bg_rwintop = tp->rcv_adv;
f427ee49 1290 if (SEQ_LT(bg_rwintop, tp->rcv_nxt + min_iaj_win)) {
813fb2f6 1291 bg_rwintop = tp->rcv_nxt + min_iaj_win;
f427ee49 1292 }
813fb2f6
A
1293 recwin = imin((int32_t)(bg_rwintop - tp->rcv_nxt),
1294 recwin);
f427ee49 1295 if (recwin < 0) {
d190cdc3 1296 recwin = 0;
f427ee49 1297 }
39037602
A
1298 }
1299 }
1300#endif /* TRAFFIC_MGT */
1301
f427ee49 1302 if (recwin > (int32_t)(TCP_MAXWIN << tp->rcv_scale)) {
39037602 1303 recwin = (int32_t)(TCP_MAXWIN << tp->rcv_scale);
f427ee49 1304 }
5ba3f43e 1305
cb323159
A
1306 if (!(so->so_flags & SOF_MP_SUBFLOW)) {
1307 if (recwin < (int32_t)(tp->rcv_adv - tp->rcv_nxt)) {
1308 recwin = (int32_t)(tp->rcv_adv - tp->rcv_nxt);
1309 }
1310 } else {
1311 struct mptcb *mp_tp = tptomptp(tp);
f427ee49 1312 int64_t recwin_announced = (int64_t)(mp_tp->mpt_rcvadv - mp_tp->mpt_rcvnxt);
cb323159
A
1313
1314 /* Don't remove what we announced at the MPTCP-layer */
f427ee49
A
1315 VERIFY(recwin_announced < INT32_MAX && recwin_announced > INT32_MIN);
1316 if (recwin < (int32_t)recwin_announced) {
1317 recwin = (int32_t)recwin_announced;
cb323159
A
1318 }
1319 }
fe8ab488 1320
1c79356b 1321 /*
8ad349bb
A
1322 * Sender silly window avoidance. We transmit under the following
1323 * conditions when len is non-zero:
1324 *
316670eb
A
1325 * - we've timed out (e.g. persist timer)
1326 * - we need to retransmit
b0d623f7 1327 * - We have a full segment (or more with TSO)
8ad349bb
A
1328 * - This is the last buffer in a write()/send() and we are
1329 * either idle or running NODELAY
8ad349bb
A
1330 * - we have more then 1/2 the maximum send window's worth of
1331 * data (receiver may be limited the window size)
1c79356b
A
1332 */
1333 if (len) {
f427ee49 1334 if (tp->t_flagsext & TF_FORCE) {
1c79356b 1335 goto send;
f427ee49
A
1336 }
1337 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
8ad349bb 1338 goto send;
f427ee49
A
1339 }
1340 if (sack_rxmit) {
1c79356b 1341 goto send;
f427ee49 1342 }
316670eb 1343
cb323159
A
1344 /*
1345 * If this here is the first segment after SYN/ACK and TFO
1346 * is being used, then we always send it, regardless of Nagle,...
1347 */
1348 if (tp->t_state == TCPS_SYN_RECEIVED &&
1349 tfo_enabled(tp) &&
1350 (tp->t_tfo_flags & TFO_F_COOKIE_VALID) &&
f427ee49 1351 tp->snd_nxt == tp->iss + 1) {
cb323159 1352 goto send;
f427ee49 1353 }
cb323159 1354
316670eb
A
1355 /*
1356 * Send new data on the connection only if it is
1357 * not flow controlled
1358 */
39236c6e 1359 if (!INP_WAIT_FOR_IF_FEEDBACK(inp) ||
316670eb 1360 tp->t_state != TCPS_ESTABLISHED) {
f427ee49 1361 if (len >= tp->t_maxseg) {
316670eb 1362 goto send;
f427ee49 1363 }
5ba3f43e 1364
316670eb 1365 if (!(tp->t_flags & TF_MORETOCOME) &&
39037602
A
1366 (idle || tp->t_flags & TF_NODELAY ||
1367 (tp->t_flags & TF_MAXSEGSNT) ||
39236c6e 1368 ALLOW_LIMITED_TRANSMIT(tp)) &&
316670eb 1369 (tp->t_flags & TF_NOPUSH) == 0 &&
5ba3f43e 1370 (len + off >= so->so_snd.sb_cc ||
f427ee49
A
1371 /*
1372 * MPTCP needs to respect the DSS-mappings. So, it
1373 * may be sending data that *could* have been
1374 * coalesced, but cannot because of
1375 * mptcp_adj_sendlen().
1376 */
1377 so->so_flags & SOF_MP_SUBFLOW)) {
316670eb 1378 goto send;
f427ee49
A
1379 }
1380 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
316670eb 1381 goto send;
f427ee49 1382 }
316670eb
A
1383 } else {
1384 tcpstat.tcps_fcholdpacket++;
1385 }
1c79356b
A
1386 }
1387
f427ee49 1388 if (recwin > 0) {
1c79356b
A
1389 /*
1390 * "adv" is the amount we can increase the window,
1391 * taking into account that we are limited by
1392 * TCP_MAXWIN << tp->rcv_scale.
1393 */
39236c6e
A
1394 int32_t adv, oldwin = 0;
1395 adv = imin(recwin, (int)TCP_MAXWIN << tp->rcv_scale) -
f427ee49 1396 (tp->rcv_adv - tp->rcv_nxt);
1c79356b 1397
f427ee49 1398 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
39236c6e 1399 oldwin = tp->rcv_adv - tp->rcv_nxt;
f427ee49 1400 }
39236c6e 1401
f427ee49
A
1402 if (tcp_ack_strategy == TCP_ACK_STRATEGY_LEGACY) {
1403 if (adv >= (int32_t) (2 * tp->t_maxseg)) {
1404 /*
1405 * Update only if the resulting scaled value of
1406 * the window changed, or if there is a change in
1407 * the sequence since the last ack. This avoids
1408 * what appears as dupe ACKS (see rdar://5640997)
1409 *
1410 * If streaming is detected avoid sending too many
1411 * window updates. We will depend on the delack
1412 * timer to send a window update when needed.
1413 *
1414 * If there is more data to read, don't send an ACK.
1415 * Otherwise we will end up sending many ACKs if the
1416 * application is doing micro-reads.
1417 */
1418 if (!(tp->t_flags & TF_STRETCHACK) &&
1419 (tp->last_ack_sent != tp->rcv_nxt ||
1420 ((oldwin + adv) >> tp->rcv_scale) >
1421 (oldwin >> tp->rcv_scale))) {
1422 goto send;
1423 }
1424 }
1425 } else {
1426 if (adv >= (int32_t) (2 * tp->t_maxseg)) {
1427 /*
1428 * ACK every second full-sized segment, if the
1429 * ACK is advancing or the window becomes bigger
1430 */
1431 if (so->so_rcv.sb_cc < so->so_rcv.sb_lowat &&
1432 (tp->last_ack_sent != tp->rcv_nxt ||
1433 ((oldwin + adv) >> tp->rcv_scale) >
1434 (oldwin >> tp->rcv_scale))) {
1435 goto send;
1436 }
1437 } else if (tp->t_flags & TF_DELACK) {
1438 /*
1439 * If we delayed the ACK and the window
1440 * is not advancing by a lot (< 2MSS), ACK
1441 * immediately if the last incoming packet had
1442 * the push flag set and we emptied the buffer.
1443 *
1444 * This takes care of a sender doing small
1445 * repeated writes with Nagle enabled.
1446 */
1447 if (so->so_rcv.sb_cc == 0 &&
1448 tp->last_ack_sent != tp->rcv_nxt &&
1449 (tp->t_flagsext & TF_LAST_IS_PSH)) {
1450 goto send;
1451 }
316670eb 1452 }
4a3eedf9 1453 }
f427ee49 1454 if (4 * adv >= (int32_t) so->so_rcv.sb_hiwat) {
39037602 1455 goto send;
f427ee49 1456 }
39037602
A
1457
1458 /*
1459 * Make sure that the delayed ack timer is set if
1460 * we delayed sending a window update because of
1461 * streaming detection.
1462 */
f427ee49
A
1463 if (tcp_ack_strategy == TCP_ACK_STRATEGY_LEGACY &&
1464 (tp->t_flags & TF_STRETCHACK) &&
39037602
A
1465 !(tp->t_flags & TF_DELACK)) {
1466 tp->t_flags |= TF_DELACK;
1467 tp->t_timer[TCPT_DELACK] =
f427ee49 1468 OFFSET_FROM_START(tp, tcp_delack);
39037602 1469 }
1c79356b
A
1470 }
1471
1472 /*
fe8ab488 1473 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW
8ad349bb 1474 * is also a catch-all for the retransmit timer timeout case.
1c79356b 1475 */
f427ee49
A
1476 if (tp->t_flags & TF_ACKNOW) {
1477 if (tp->t_forced_acks > 0) {
1478 tp->t_forced_acks--;
1479 }
1c79356b 1480 goto send;
f427ee49
A
1481 }
1482 if ((flags & TH_RST) || (flags & TH_SYN)) {
1c79356b 1483 goto send;
f427ee49
A
1484 }
1485 if (SEQ_GT(tp->snd_up, tp->snd_una)) {
1c79356b 1486 goto send;
f427ee49 1487 }
39236c6e 1488#if MPTCP
f427ee49 1489 if (mptcp_acknow) {
39236c6e 1490 goto send;
f427ee49 1491 }
39236c6e 1492#endif /* MPTCP */
1c79356b
A
1493 /*
1494 * If our state indicates that FIN should be sent
8ad349bb 1495 * and we have not yet done so, then we need to send.
1c79356b 1496 */
8a3053a0 1497 if ((flags & TH_FIN) &&
f427ee49 1498 (!(tp->t_flags & TF_SENTFIN) || tp->snd_nxt == tp->snd_una)) {
1c79356b 1499 goto send;
f427ee49 1500 }
8ad349bb
A
1501 /*
1502 * In SACK, it is possible for tcp_output to fail to send a segment
1503 * after the retransmission timer has been turned off. Make sure
1504 * that the retransmission timer is set.
1505 */
39037602 1506 if (SACK_ENABLED(tp) && (tp->t_state >= TCPS_ESTABLISHED) &&
316670eb
A
1507 SEQ_GT(tp->snd_max, tp->snd_una) &&
1508 tp->t_timer[TCPT_REXMT] == 0 &&
8ad349bb 1509 tp->t_timer[TCPT_PERSIST] == 0) {
fe8ab488 1510 tp->t_timer[TCPT_REXMT] = OFFSET_FROM_START(tp,
f427ee49 1511 tp->t_rxtcur);
39236c6e 1512 goto just_return;
39037602 1513 }
1c79356b
A
1514 /*
1515 * TCP window updates are not reliable, rather a polling protocol
1516 * using ``persist'' packets is used to insure receipt of window
1517 * updates. The three ``states'' for the output side are:
1518 * idle not doing retransmits or persists
1519 * persisting to move a small or zero window
1520 * (re)transmitting and thereby not persisting
1521 *
1522 * tp->t_timer[TCPT_PERSIST]
1523 * is set when we are in persist state.
1524 * tp->t_force
1525 * is set when we are called to send a persist packet.
1526 * tp->t_timer[TCPT_REXMT]
1527 * is set when we are retransmitting
1528 * The output side is idle when both timers are zero.
1529 *
1530 * If send window is too small, there is data to transmit, and no
1531 * retransmit or persist is pending, then go to persist state.
1532 * If nothing happens soon, send when timer expires:
1533 * if window is nonzero, transmit what we can,
1534 * otherwise force out a byte.
1535 */
1536 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
1537 tp->t_timer[TCPT_PERSIST] == 0) {
39037602 1538 TCP_RESET_REXMT_STATE(tp);
1c79356b
A
1539 tcp_setpersist(tp);
1540 }
8ad349bb 1541just_return:
1c79356b 1542 /*
91447636
A
1543 * If there is no reason to send a segment, just return.
1544 * but if there is some packets left in the packet list, send them now.
1c79356b 1545 */
39236c6e 1546 while (inp->inp_sndinprog_cnt == 0 &&
f427ee49 1547 tp->t_pktlist_head != NULL) {
2d21ac55
A
1548 packetlist = tp->t_pktlist_head;
1549 packchain_listadd = tp->t_lastchain;
1550 packchain_sent++;
1551 TCP_PKTLIST_CLEAR(tp);
2d21ac55 1552
fe8ab488
A
1553 error = tcp_ip_output(so, tp, packetlist,
1554 packchain_listadd,
316670eb 1555 tp_inp_options, (so_options & SO_DONTROUTE),
5ba3f43e 1556 (sack_rxmit || (sack_bytes_rxmt != 0)), isipv6);
2d21ac55
A
1557 }
1558 /* tcp was closed while we were in ip; resume close */
39236c6e 1559 if (inp->inp_sndinprog_cnt == 0 &&
f427ee49 1560 (tp->t_flags & TF_CLOSING)) {
2d21ac55
A
1561 tp->t_flags &= ~TF_CLOSING;
1562 (void) tcp_close(tp);
6d2010ae
A
1563 } else {
1564 tcp_check_timer_state(tp);
91447636 1565 }
f427ee49 1566 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
cb323159 1567 return 0;
1c79356b
A
1568
1569send:
3e170ce0 1570 /*
39236c6e
A
1571 * Set TF_MAXSEGSNT flag if the segment size is greater than
1572 * the max segment size.
1573 */
1574 if (len > 0) {
f427ee49
A
1575 do_not_compress = TRUE;
1576
1577 if (len >= tp->t_maxseg) {
39236c6e 1578 tp->t_flags |= TF_MAXSEGSNT;
f427ee49 1579 } else {
39236c6e 1580 tp->t_flags &= ~TF_MAXSEGSNT;
f427ee49 1581 }
39236c6e 1582 }
1c79356b
A
1583 /*
1584 * Before ESTABLISHED, force sending of initial options
1585 * unless TCP set not to do any options.
1586 * NOTE: we assume that the IP/TCP header plus TCP options
1587 * always fit in a single mbuf, leaving room for a maximum
1588 * link header, i.e.
9bccf70c 1589 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
1c79356b
A
1590 */
1591 optlen = 0;
f427ee49
A
1592 if (isipv6) {
1593 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1594 } else {
1595 hdrlen = sizeof(struct tcpiphdr);
1596 }
1c79356b
A
1597 if (flags & TH_SYN) {
1598 tp->snd_nxt = tp->iss;
1599 if ((tp->t_flags & TF_NOOPT) == 0) {
1600 u_short mss;
1601
1602 opt[0] = TCPOPT_MAXSEG;
1603 opt[1] = TCPOLEN_MAXSEG;
9bccf70c 1604 mss = htons((u_short) tcp_mssopt(tp));
1c79356b
A
1605 (void)memcpy(opt + 2, &mss, sizeof(mss));
1606 optlen = TCPOLEN_MAXSEG;
1607
1608 if ((tp->t_flags & TF_REQ_SCALE) &&
1609 ((flags & TH_ACK) == 0 ||
1610 (tp->t_flags & TF_RCVD_SCALE))) {
316670eb 1611 *((u_int32_t *)(void *)(opt + optlen)) = htonl(
1c79356b 1612 TCPOPT_NOP << 24 |
f427ee49
A
1613 TCPOPT_WINDOW << 16 |
1614 TCPOLEN_WINDOW << 8 |
1615 tp->request_r_scale);
1c79356b
A
1616 optlen += 4;
1617 }
39236c6e 1618#if MPTCP
39037602 1619 if (mptcp_enable && (so->so_flags & SOF_MP_SUBFLOW)) {
5ba3f43e 1620 optlen = mptcp_setup_syn_opts(so, opt, optlen);
39236c6e
A
1621 }
1622#endif /* MPTCP */
1c79356b 1623 }
2d21ac55 1624 }
1c79356b 1625
3e170ce0 1626 /*
1c79356b
A
1627 * Send a timestamp and echo-reply if this is a SYN and our side
1628 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
1629 * and our peer have sent timestamps in our SYN's.
f427ee49
A
1630 */
1631 if ((tp->t_flags & (TF_REQ_TSTMP | TF_NOOPT)) == TF_REQ_TSTMP &&
1632 (flags & TH_RST) == 0 &&
1c79356b 1633 ((flags & TH_ACK) == 0 ||
f427ee49 1634 (tp->t_flags & TF_RCVD_TSTMP))) {
316670eb 1635 u_int32_t *lp = (u_int32_t *)(void *)(opt + optlen);
1c79356b 1636
f427ee49
A
1637 /* Form timestamp option as shown in appendix A of RFC 1323. */
1638 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
1639 *lp++ = htonl(tcp_now);
1640 *lp = htonl(tp->ts_recent);
1641 optlen += TCPOLEN_TSTAMP_APPA;
1642 }
316670eb 1643
39236c6e 1644 if (SACK_ENABLED(tp) && ((tp->t_flags & TF_NOOPT) == 0)) {
39037602 1645 /*
8ad349bb
A
1646 * Tack on the SACK permitted option *last*.
1647 * And do padding of options after tacking this on.
1648 * This is because of MSS, TS, WinScale and Signatures are
1649 * all present, we have just 2 bytes left for the SACK
1650 * permitted option, which is just enough.
1c79356b 1651 */
1c79356b 1652 /*
8ad349bb
A
1653 * If this is the first SYN of connection (not a SYN
1654 * ACK), include SACK permitted option. If this is a
1655 * SYN ACK, include SACK permitted option if peer has
1656 * already done so. This is only for active connect,
1657 * since the syncache takes care of the passive connect.
1c79356b 1658 */
8ad349bb
A
1659 if ((flags & TH_SYN) &&
1660 (!(flags & TH_ACK) || (tp->t_flags & TF_SACK_PERMIT))) {
1661 u_char *bp;
1662 bp = (u_char *)opt + optlen;
1663
1664 *bp++ = TCPOPT_SACK_PERMITTED;
1665 *bp++ = TCPOLEN_SACK_PERMITTED;
1666 optlen += TCPOLEN_SACK_PERMITTED;
1667 }
39236c6e
A
1668 }
1669#if MPTCP
1670 if (so->so_flags & SOF_MP_SUBFLOW) {
1671 /*
1672 * Its important to piggyback acks with data as ack only packets
1673 * may get lost and data packets that don't send Data ACKs
1674 * still advance the subflow level ACK and therefore make it
1675 * hard for the remote end to recover in low cwnd situations.
1676 */
fe8ab488 1677 if (len != 0) {
39236c6e
A
1678 tp->t_mpflags |= (TMPF_SEND_DSN |
1679 TMPF_MPTCP_ACKNOW);
fe8ab488 1680 } else {
39236c6e 1681 tp->t_mpflags |= TMPF_MPTCP_ACKNOW;
fe8ab488 1682 }
39236c6e 1683 optlen = mptcp_setup_opts(tp, off, &opt[0], optlen, flags,
f427ee49 1684 len, &mptcp_acknow, &do_not_compress);
39236c6e
A
1685 tp->t_mpflags &= ~TMPF_SEND_DSN;
1686 }
1687#endif /* MPTCP */
1c79356b 1688
3e170ce0 1689 if (tfo_enabled(tp) && !(tp->t_flags & TF_NOOPT) &&
f427ee49 1690 (flags & (TH_SYN | TH_ACK)) == TH_SYN) {
813fb2f6 1691 optlen += tcp_tfo_write_cookie(tp, optlen, len, opt);
f427ee49 1692 }
3e170ce0
A
1693
1694 if (tfo_enabled(tp) &&
1695 (flags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK) &&
f427ee49 1696 (tp->t_tfo_flags & TFO_F_OFFER_COOKIE)) {
3e170ce0 1697 optlen += tcp_tfo_write_cookie_rep(tp, optlen, opt);
f427ee49 1698 }
3e170ce0 1699
39236c6e 1700 if (SACK_ENABLED(tp) && ((tp->t_flags & TF_NOOPT) == 0)) {
1c79356b 1701 /*
8ad349bb
A
1702 * Send SACKs if necessary. This should be the last
1703 * option processed. Only as many SACKs are sent as
1704 * are permitted by the maximum options size.
1705 *
1706 * In general, SACK blocks consume 8*n+2 bytes.
1707 * So a full size SACK blocks option is 34 bytes
1708 * (to generate 4 SACK blocks). At a minimum,
1709 * we need 10 bytes (to generate 1 SACK block).
1710 * If TCP Timestamps (12 bytes) and TCP Signatures
1711 * (18 bytes) are both present, we'll just have
1712 * 10 bytes for SACK options 40 - (12 + 18).
1c79356b 1713 */
8ad349bb 1714 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
3e170ce0
A
1715 (tp->t_flags & TF_SACK_PERMIT) &&
1716 (tp->rcv_numsacks > 0 || TCP_SEND_DSACK_OPT(tp)) &&
8ad349bb 1717 MAX_TCPOPTLEN - optlen - 2 >= TCPOLEN_SACK) {
316670eb 1718 int nsack, padlen;
8ad349bb
A
1719 u_char *bp = (u_char *)opt + optlen;
1720 u_int32_t *lp;
1c79356b 1721
8ad349bb 1722 nsack = (MAX_TCPOPTLEN - optlen - 2) / TCPOLEN_SACK;
3e170ce0
A
1723 nsack = min(nsack, (tp->rcv_numsacks +
1724 (TCP_SEND_DSACK_OPT(tp) ? 1 : 0)));
8ad349bb 1725 sackoptlen = (2 + nsack * TCPOLEN_SACK);
f427ee49 1726 VERIFY(sackoptlen < UINT8_MAX);
8ad349bb
A
1727
1728 /*
1729 * First we need to pad options so that the
1730 * SACK blocks can start at a 4-byte boundary
1731 * (sack option and length are at a 2 byte offset).
1732 */
1733 padlen = (MAX_TCPOPTLEN - optlen - sackoptlen) % 4;
1734 optlen += padlen;
f427ee49 1735 while (padlen-- > 0) {
8ad349bb 1736 *bp++ = TCPOPT_NOP;
f427ee49 1737 }
8ad349bb
A
1738
1739 tcpstat.tcps_sack_send_blocks++;
1740 *bp++ = TCPOPT_SACK;
f427ee49 1741 *bp++ = (uint8_t)sackoptlen;
316670eb 1742 lp = (u_int32_t *)(void *)bp;
3e170ce0
A
1743
1744 /*
1745 * First block of SACK option should represent
1746 * DSACK. Prefer to send SACK information if there
1747 * is space for only one SACK block. This will
1748 * allow for faster recovery.
1749 */
1750 if (TCP_SEND_DSACK_OPT(tp) && nsack > 0 &&
1751 (tp->rcv_numsacks == 0 || nsack > 1)) {
1752 *lp++ = htonl(tp->t_dsack_lseq);
1753 *lp++ = htonl(tp->t_dsack_rseq);
1754 tcpstat.tcps_dsack_sent++;
4bd07ac2 1755 tp->t_dsack_sent++;
3e170ce0
A
1756 nsack--;
1757 }
1758 VERIFY(nsack == 0 || tp->rcv_numsacks >= nsack);
8ad349bb
A
1759 for (i = 0; i < nsack; i++) {
1760 struct sackblk sack = tp->sackblks[i];
1761 *lp++ = htonl(sack.start);
1762 *lp++ = htonl(sack.end);
1c79356b 1763 }
8ad349bb 1764 optlen += sackoptlen;
1c79356b 1765 }
8ad349bb
A
1766 }
1767
1768 /* Pad TCP options to a 4 byte boundary */
1769 if (optlen < MAX_TCPOPTLEN && (optlen % sizeof(u_int32_t))) {
1770 int pad = sizeof(u_int32_t) - (optlen % sizeof(u_int32_t));
1771 u_char *bp = (u_char *)opt + optlen;
1772
1773 optlen += pad;
1774 while (pad) {
1775 *bp++ = TCPOPT_EOL;
1776 pad--;
1777 }
1778 }
1779
3e170ce0
A
1780 /*
1781 * RFC 3168 states that:
1782 * - If you ever sent an ECN-setup SYN/SYN-ACK you must be prepared
1783 * to handle the TCP ECE flag, even if you also later send a
1784 * non-ECN-setup SYN/SYN-ACK.
1785 * - If you ever send a non-ECN-setup SYN/SYN-ACK, you must not set
1786 * the ip ECT flag.
1787 *
1788 * It is not clear how the ECE flag would ever be set if you never
1789 * set the IP ECT flag on outbound packets. All the same, we use
1790 * the TE_SETUPSENT to indicate that we have committed to handling
1791 * the TCP ECE flag correctly. We use the TE_SENDIPECT to indicate
1792 * whether or not we should set the IP ECT flag on outbound packet
1793 *
1794 * For a SYN-ACK, send an ECN setup SYN-ACK
1795 */
4bd07ac2
A
1796 if ((flags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK) &&
1797 (tp->ecn_flags & TE_ENABLE_ECN)) {
3e170ce0 1798 if (tp->ecn_flags & TE_SETUPRECEIVED) {
f427ee49 1799 if (tcp_send_ecn_flags_on_syn(tp)) {
3e170ce0
A
1800 /*
1801 * Setting TH_ECE makes this an ECN-setup
1802 * SYN-ACK
1803 */
1804 flags |= TH_ECE;
1805
1806 /*
1807 * Record that we sent the ECN-setup and
1808 * default to setting IP ECT.
1809 */
f427ee49 1810 tp->ecn_flags |= (TE_SETUPSENT | TE_SENDIPECT);
3e170ce0
A
1811 tcpstat.tcps_ecn_server_setup++;
1812 tcpstat.tcps_ecn_server_success++;
1813 } else {
1814 /*
1815 * We sent an ECN-setup SYN-ACK but it was
1816 * dropped. Fallback to non-ECN-setup
1817 * SYN-ACK and clear flag to indicate that
1818 * we should not send data with IP ECT set
1819 *
1820 * Pretend we didn't receive an
1821 * ECN-setup SYN.
1822 *
1823 * We already incremented the counter
1824 * assuming that the ECN setup will
1825 * succeed. Decrementing here
1826 * tcps_ecn_server_success to correct it.
1827 */
1828 if (tp->ecn_flags & TE_SETUPSENT) {
1829 tcpstat.tcps_ecn_lost_synack++;
1830 tcpstat.tcps_ecn_server_success--;
4bd07ac2 1831 tp->ecn_flags |= TE_LOST_SYNACK;
3e170ce0
A
1832 }
1833
1834 tp->ecn_flags &=
1835 ~(TE_SETUPRECEIVED | TE_SENDIPECT |
1836 TE_SENDCWR);
1837 }
1838 }
4bd07ac2
A
1839 } else if ((flags & (TH_SYN | TH_ACK)) == TH_SYN &&
1840 (tp->ecn_flags & TE_ENABLE_ECN)) {
f427ee49 1841 if (tcp_send_ecn_flags_on_syn(tp)) {
3e170ce0
A
1842 /*
1843 * Setting TH_ECE and TH_CWR makes this an
1844 * ECN-setup SYN
1845 */
1846 flags |= (TH_ECE | TH_CWR);
1847 tcpstat.tcps_ecn_client_setup++;
4bd07ac2 1848 tp->ecn_flags |= TE_CLIENT_SETUP;
3e170ce0
A
1849
1850 /*
1851 * Record that we sent the ECN-setup and default to
1852 * setting IP ECT.
1853 */
1854 tp->ecn_flags |= (TE_SETUPSENT | TE_SENDIPECT);
1855 } else {
1856 /*
1857 * We sent an ECN-setup SYN but it was dropped.
1858 * Fall back to non-ECN and clear flag indicating
1859 * we should send data with IP ECT set.
1860 */
4bd07ac2 1861 if (tp->ecn_flags & TE_SETUPSENT) {
3e170ce0 1862 tcpstat.tcps_ecn_lost_syn++;
4bd07ac2
A
1863 tp->ecn_flags |= TE_LOST_SYN;
1864 }
3e170ce0
A
1865 tp->ecn_flags &= ~TE_SENDIPECT;
1866 }
1867 }
1868
1869 /*
1870 * Check if we should set the TCP CWR flag.
1871 * CWR flag is sent when we reduced the congestion window because
1872 * we received a TCP ECE or we performed a fast retransmit. We
1873 * never set the CWR flag on retransmitted packets. We only set
1874 * the CWR flag on data packets. Pure acks don't have this set.
1875 */
1876 if ((tp->ecn_flags & TE_SENDCWR) != 0 && len != 0 &&
1877 !SEQ_LT(tp->snd_nxt, tp->snd_max) && !sack_rxmit) {
1878 flags |= TH_CWR;
1879 tp->ecn_flags &= ~TE_SENDCWR;
1880 }
1881
1882 /*
1883 * Check if we should set the TCP ECE flag.
1884 */
1885 if ((tp->ecn_flags & TE_SENDECE) != 0 && len == 0) {
1886 flags |= TH_ECE;
1887 tcpstat.tcps_ecn_sent_ece++;
1888 }
1889
1890
8ad349bb 1891 hdrlen += optlen;
1c79356b 1892
3e170ce0
A
1893 /* Reset DSACK sequence numbers */
1894 tp->t_dsack_lseq = 0;
1895 tp->t_dsack_rseq = 0;
1896
f427ee49 1897 if (isipv6) {
39236c6e 1898 ipoptlen = ip6_optlen(inp);
f427ee49 1899 } else {
2d21ac55
A
1900 if (tp_inp_options) {
1901 ipoptlen = tp_inp_options->m_len -
f427ee49 1902 offsetof(struct ipoption, ipopt_list);
fe8ab488 1903 } else {
9bccf70c 1904 ipoptlen = 0;
fe8ab488 1905 }
1c79356b
A
1906 }
1907#if IPSEC
f427ee49 1908 ipoptlen += ipsec_optlen;
1c79356b
A
1909#endif
1910
1911 /*
1912 * Adjust data length if insertion of options will
1913 * bump the packet length beyond the t_maxopd length.
1914 * Clear the FIN bit because we cut off the tail of
1915 * the segment.
b0d623f7
A
1916 *
1917 * When doing TSO limit a burst to TCP_MAXWIN minus the
1918 * IP, TCP and Options length to keep ip->ip_len from
1919 * overflowing. Prevent the last segment from being
1920 * fractional thus making them all equal sized and set
1921 * the flag to continue sending. TSO is disabled when
1922 * IP options or IPSEC are present.
1c79356b
A
1923 */
1924 if (len + optlen + ipoptlen > tp->t_maxopd) {
1925 /*
fe8ab488
A
1926 * If there is still more to send,
1927 * don't close the connection.
1c79356b
A
1928 */
1929 flags &= ~TH_FIN;
b0d623f7
A
1930 if (tso) {
1931 int32_t tso_maxlen;
1932
fe8ab488 1933 tso_maxlen = tp->tso_max_segment_size ?
f427ee49 1934 tp->tso_max_segment_size : TCP_MAXWIN;
b0d623f7
A
1935
1936 if (len > tso_maxlen - hdrlen - optlen) {
1937 len = tso_maxlen - hdrlen - optlen;
b0d623f7 1938 sendalot = 1;
fe8ab488 1939 } else if (tp->t_flags & TF_NEEDFIN) {
b0d623f7 1940 sendalot = 1;
fe8ab488 1941 }
f427ee49
A
1942
1943 if (len % (tp->t_maxopd - optlen) != 0) {
1944 len = len - (len % (tp->t_maxopd - optlen));
1945 sendalot = 1;
1946 }
b0d623f7
A
1947 } else {
1948 len = tp->t_maxopd - optlen - ipoptlen;
1949 sendalot = 1;
1950 }
1c79356b
A
1951 }
1952
f427ee49 1953 if (max_linkhdr + hdrlen > MCLBYTES) {
9bccf70c 1954 panic("tcphdr too big");
f427ee49 1955 }
1c79356b 1956
316670eb 1957 /* Check if there is enough data in the send socket
5ba3f43e 1958 * buffer to start measuring bandwidth
316670eb
A
1959 */
1960 if ((tp->t_flagsext & TF_MEASURESNDBW) != 0 &&
f427ee49
A
1961 (tp->t_bwmeas != NULL) &&
1962 (tp->t_flagsext & TF_BWMEAS_INPROGRESS) == 0) {
5ba3f43e 1963 tp->t_bwmeas->bw_size = min(min(
f427ee49
A
1964 (so->so_snd.sb_cc - (tp->snd_max - tp->snd_una)),
1965 tp->snd_cwnd), tp->snd_wnd);
5ba3f43e 1966 if (tp->t_bwmeas->bw_minsize > 0 &&
f427ee49 1967 tp->t_bwmeas->bw_size < tp->t_bwmeas->bw_minsize) {
5ba3f43e 1968 tp->t_bwmeas->bw_size = 0;
f427ee49
A
1969 }
1970 if (tp->t_bwmeas->bw_maxsize > 0) {
5ba3f43e
A
1971 tp->t_bwmeas->bw_size = min(tp->t_bwmeas->bw_size,
1972 tp->t_bwmeas->bw_maxsize);
f427ee49 1973 }
5ba3f43e
A
1974 if (tp->t_bwmeas->bw_size > 0) {
1975 tp->t_flagsext |= TF_BWMEAS_INPROGRESS;
1976 tp->t_bwmeas->bw_start = tp->snd_max;
1977 tp->t_bwmeas->bw_ts = tcp_now;
1978 }
316670eb
A
1979 }
1980
39236c6e 1981 VERIFY(inp->inp_flowhash != 0);
1c79356b
A
1982 /*
1983 * Grab a header mbuf, attaching a copy of data to
1984 * be transmitted, and initialize the header from
1985 * the template for sends on this connection.
1986 */
1987 if (len) {
f427ee49
A
1988 /* Remember what the last head-of-line packet-size was */
1989 if (tp->t_pmtud_lastseg_size == 0 && tp->snd_nxt == tp->snd_una) {
1990 ASSERT(len + optlen + ipoptlen <= IP_MAXPACKET);
1991 tp->t_pmtud_lastseg_size = (uint16_t)(len + optlen + ipoptlen);
1992 }
1993 if ((tp->t_flagsext & TF_FORCE) && len == 1) {
1c79356b 1994 tcpstat.tcps_sndprobe++;
f427ee49 1995 } else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
1c79356b
A
1996 tcpstat.tcps_sndrexmitpack++;
1997 tcpstat.tcps_sndrexmitbyte += len;
6d2010ae 1998 if (nstat_collect) {
39236c6e 1999 nstat_route_tx(inp->inp_route.ro_rt, 1,
f427ee49 2000 len, NSTAT_TX_FLAG_RETRANSMIT);
fe8ab488
A
2001 INP_ADD_STAT(inp, cell, wifi, wired,
2002 txpackets, 1);
2003 INP_ADD_STAT(inp, cell, wifi, wired,
2004 txbytes, len);
6d2010ae 2005 tp->t_stat.txretransmitbytes += len;
490019cf 2006 tp->t_stat.rxmitpkts++;
6d2010ae 2007 }
1c79356b
A
2008 } else {
2009 tcpstat.tcps_sndpack++;
2010 tcpstat.tcps_sndbyte += len;
39037602 2011
6d2010ae 2012 if (nstat_collect) {
fe8ab488
A
2013 INP_ADD_STAT(inp, cell, wifi, wired,
2014 txpackets, 1);
2015 INP_ADD_STAT(inp, cell, wifi, wired,
2016 txbytes, len);
6d2010ae 2017 }
39037602 2018 inp_decr_sndbytes_unsent(so, len);
1c79356b 2019 }
5ba3f43e 2020 inp_set_activity_bitmap(inp);
39236c6e
A
2021#if MPTCP
2022 if (tp->t_mpflags & TMPF_MPTCP_TRUE) {
2023 tcpstat.tcps_mp_sndpacks++;
2024 tcpstat.tcps_mp_sndbytes += len;
2025 }
2026#endif /* MPTCP */
9bccf70c 2027 /*
39037602
A
2028 * try to use the new interface that allocates all
2029 * the necessary mbuf hdrs under 1 mbuf lock and
2030 * avoids rescanning the socket mbuf list if
9bccf70c
A
2031 * certain conditions are met. This routine can't
2032 * be used in the following cases...
2033 * 1) the protocol headers exceed the capacity of
2034 * of a single mbuf header's data area (no cluster attached)
2035 * 2) the length of the data being transmitted plus
2036 * the protocol headers fits into a single mbuf header's
2037 * data area (no cluster attached)
2038 */
fa4905b1 2039 m = NULL;
99c3a104
A
2040
2041 /* minimum length we are going to allocate */
2042 allocated_len = MHLEN;
f427ee49 2043 if (MHLEN < hdrlen + max_linkhdr) {
99c3a104 2044 MGETHDR(m, M_DONTWAIT, MT_HEADER);
fa4905b1 2045 if (m == NULL) {
99c3a104 2046 error = ENOBUFS;
fa4905b1
A
2047 goto out;
2048 }
f427ee49
A
2049 MCLGET(m, M_DONTWAIT);
2050 if ((m->m_flags & M_EXT) == 0) {
2051 m_freem(m);
2052 error = ENOBUFS;
2053 goto out;
2054 }
fa4905b1
A
2055 m->m_data += max_linkhdr;
2056 m->m_len = hdrlen;
99c3a104 2057 allocated_len = MCLBYTES;
1c79356b 2058 }
99c3a104 2059 if (len <= allocated_len - hdrlen - max_linkhdr) {
f427ee49 2060 if (m == NULL) {
99c3a104
A
2061 VERIFY(allocated_len <= MHLEN);
2062 MGETHDR(m, M_DONTWAIT, MT_HEADER);
fa4905b1 2063 if (m == NULL) {
99c3a104 2064 error = ENOBUFS;
fa4905b1
A
2065 goto out;
2066 }
2067 m->m_data += max_linkhdr;
2068 m->m_len = hdrlen;
2069 }
55e303ae 2070 /* makes sure we still have data left to be sent at this point */
b0d623f7 2071 if (so->so_snd.sb_mb == NULL || off < 0) {
f427ee49
A
2072 if (m != NULL) {
2073 m_freem(m);
2074 }
55e303ae
A
2075 error = 0; /* should we return an error? */
2076 goto out;
2077 }
1c79356b
A
2078 m_copydata(so->so_snd.sb_mb, off, (int) len,
2079 mtod(m, caddr_t) + hdrlen);
2080 m->m_len += len;
2081 } else {
39236c6e
A
2082 uint32_t copymode;
2083 /*
2084 * Retain packet header metadata at the socket
2085 * buffer if this is is an MPTCP subflow,
2086 * otherwise move it.
2087 */
2088 copymode = M_COPYM_MOVE_HDR;
2089#if MPTCP
fe8ab488 2090 if (so->so_flags & SOF_MP_SUBFLOW) {
39236c6e
A
2091 copymode = M_COPYM_NOOP_HDR;
2092 }
2093#endif /* MPTCP */
2094 if (m != NULL) {
fe8ab488
A
2095 m->m_next = m_copym_mode(so->so_snd.sb_mb,
2096 off, (int)len, M_DONTWAIT, copymode);
39236c6e
A
2097 if (m->m_next == NULL) {
2098 (void) m_free(m);
fa4905b1
A
2099 error = ENOBUFS;
2100 goto out;
2101 }
2102 } else {
39236c6e
A
2103 /*
2104 * make sure we still have data left
2105 * to be sent at this point
2106 */
fe8ab488 2107 if (so->so_snd.sb_mb == NULL) {
55e303ae
A
2108 error = 0; /* should we return an error? */
2109 goto out;
2110 }
39037602 2111
9bccf70c 2112 /*
39236c6e
A
2113 * m_copym_with_hdrs will always return the
2114 * last mbuf pointer and the offset into it that
2115 * it acted on to fullfill the current request,
2116 * whether a valid 'hint' was passed in or not.
9bccf70c 2117 */
f427ee49 2118 if ((m = m_copym_with_hdrs(so->so_snd.sb_mb,
fe8ab488 2119 off, len, M_DONTWAIT, NULL, NULL,
39236c6e 2120 copymode)) == NULL) {
f427ee49 2121 error = ENOBUFS;
fa4905b1
A
2122 goto out;
2123 }
2124 m->m_data += max_linkhdr;
2125 m->m_len = hdrlen;
1c79356b
A
2126 }
2127 }
1c79356b
A
2128 /*
2129 * If we're sending everything we've got, set PUSH.
2130 * (This will keep happy those implementations which only
2131 * give data to the user when a buffer fills or
2132 * a PUSH comes in.)
3e170ce0
A
2133 *
2134 * On SYN-segments we should not add the PUSH-flag.
1c79356b 2135 */
f427ee49 2136 if (off + len == so->so_snd.sb_cc && !(flags & TH_SYN)) {
1c79356b 2137 flags |= TH_PUSH;
f427ee49 2138 }
1c79356b 2139 } else {
f427ee49 2140 if (tp->t_flags & TF_ACKNOW) {
1c79356b 2141 tcpstat.tcps_sndacks++;
f427ee49 2142 } else if (flags & (TH_SYN | TH_FIN | TH_RST)) {
1c79356b 2143 tcpstat.tcps_sndctrl++;
f427ee49 2144 } else if (SEQ_GT(tp->snd_up, tp->snd_una)) {
1c79356b 2145 tcpstat.tcps_sndurg++;
f427ee49 2146 } else {
1c79356b 2147 tcpstat.tcps_sndwinup++;
f427ee49 2148 }
1c79356b 2149
f427ee49 2150 MGETHDR(m, M_DONTWAIT, MT_HEADER); /* MAC-OK */
1c79356b
A
2151 if (m == NULL) {
2152 error = ENOBUFS;
2153 goto out;
2154 }
99c3a104 2155 if (MHLEN < (hdrlen + max_linkhdr)) {
cb323159
A
2156 MCLGET(m, M_DONTWAIT);
2157 if ((m->m_flags & M_EXT) == 0) {
2158 m_freem(m);
2159 error = ENOBUFS;
2160 goto out;
2161 }
99c3a104
A
2162 }
2163 m->m_data += max_linkhdr;
1c79356b
A
2164 m->m_len = hdrlen;
2165 }
91447636 2166 m->m_pkthdr.rcvif = 0;
f427ee49
A
2167
2168 /* Any flag other than pure-ACK: Do not compress! */
2169 if (flags & ~(TH_ACK)) {
2170 do_not_compress = TRUE;
2171 }
2172
2173 if (tp->rcv_scale == 0) {
2174 do_not_compress = TRUE;
2175 }
2176
2177 if (do_not_compress || (tcp_do_ack_compression == 1 && !cell) || __improbable(!tcp_do_ack_compression)) {
2178 m->m_pkthdr.comp_gencnt = 0;
2179 } else {
2180 if (TSTMP_LT(tp->t_comp_lastinc + tcp_ack_compression_rate, tcp_now)) {
2181 tp->t_comp_gencnt++;
2182 /* 0 means no compression, thus jump this */
2183 if (tp->t_comp_gencnt <= TCP_ACK_COMPRESSION_DUMMY) {
2184 tp->t_comp_gencnt = TCP_ACK_COMPRESSION_DUMMY + 1;
2185 }
2186 tp->t_comp_lastinc = tcp_now;
2187 }
2188 m->m_pkthdr.comp_gencnt = tp->t_comp_gencnt;
2189 }
2190
1c79356b
A
2191 if (isipv6) {
2192 ip6 = mtod(m, struct ip6_hdr *);
316670eb 2193 th = (struct tcphdr *)(void *)(ip6 + 1);
9bccf70c 2194 tcp_fillheaders(tp, ip6, th);
6d2010ae 2195 if ((tp->ecn_flags & TE_SENDIPECT) != 0 && len &&
f427ee49 2196 !SEQ_LT(tp->snd_nxt, tp->snd_max) && !sack_rxmit) {
6d2010ae
A
2197 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
2198 }
316670eb 2199 svc_flags |= PKT_SCF_IPV6;
fe8ab488 2200#if PF_ECN
39037602
A
2201 m_pftag(m)->pftag_hdr = (void *)ip6;
2202 m_pftag(m)->pftag_flags |= PF_TAG_HDR_INET6;
fe8ab488 2203#endif /* PF_ECN */
f427ee49 2204 } else {
9bccf70c 2205 ip = mtod(m, struct ip *);
316670eb 2206 th = (struct tcphdr *)(void *)(ip + 1);
9bccf70c
A
2207 /* this picks up the pseudo header (w/o the length) */
2208 tcp_fillheaders(tp, ip, th);
2d21ac55 2209 if ((tp->ecn_flags & TE_SENDIPECT) != 0 && len &&
3e170ce0
A
2210 !SEQ_LT(tp->snd_nxt, tp->snd_max) &&
2211 !sack_rxmit && !(flags & TH_SYN)) {
2212 ip->ip_tos |= IPTOS_ECN_ECT0;
2d21ac55 2213 }
fe8ab488 2214#if PF_ECN
39037602
A
2215 m_pftag(m)->pftag_hdr = (void *)ip;
2216 m_pftag(m)->pftag_flags |= PF_TAG_HDR_INET;
fe8ab488 2217#endif /* PF_ECN */
1c79356b 2218 }
1c79356b
A
2219
2220 /*
2221 * Fill in fields, remembering maximum advertised
2222 * window for use in delaying messages about window sizes.
2223 * If resending a FIN, be sure not to use a new sequence number.
2224 */
3e170ce0 2225 if ((flags & TH_FIN) && (tp->t_flags & TF_SENTFIN) &&
f427ee49 2226 tp->snd_nxt == tp->snd_max) {
1c79356b 2227 tp->snd_nxt--;
f427ee49 2228 }
1c79356b
A
2229 /*
2230 * If we are doing retransmissions, then snd_nxt will
2231 * not reflect the first unsent octet. For ACK only
2232 * packets, we do not want the sequence number of the
2233 * retransmitted packet, we want the sequence number
2234 * of the next unsent octet. So, if there is no data
2235 * (and no SYN or FIN), use snd_max instead of snd_nxt
2236 * when filling in ti_seq. But if we are in persist
2237 * state, snd_max might reflect one byte beyond the
2238 * right edge of the window, so use snd_nxt in that
2239 * case, since we know we aren't doing a retransmission.
2240 * (retransmit and persist are mutually exclusive...)
3e170ce0
A
2241 *
2242 * Note the state of this retransmit segment to detect spurious
2243 * retransmissions.
1c79356b 2244 */
8ad349bb 2245 if (sack_rxmit == 0) {
f427ee49 2246 if (len || (flags & (TH_SYN | TH_FIN)) ||
3e170ce0 2247 tp->t_timer[TCPT_PERSIST]) {
8ad349bb 2248 th->th_seq = htonl(tp->snd_nxt);
39037602
A
2249 if (len > 0) {
2250 m->m_pkthdr.tx_start_seq = tp->snd_nxt;
2251 m->m_pkthdr.pkt_flags |= PKTF_START_SEQ;
2252 }
3e170ce0
A
2253 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
2254 if (SACK_ENABLED(tp) && len > 1) {
2255 tcp_rxtseg_insert(tp, tp->snd_nxt,
2256 (tp->snd_nxt + len - 1));
2257 }
f427ee49 2258 if (len > 0) {
4bd07ac2
A
2259 m->m_pkthdr.pkt_flags |=
2260 PKTF_TCP_REXMT;
f427ee49 2261 }
3e170ce0
A
2262 }
2263 } else {
8ad349bb 2264 th->th_seq = htonl(tp->snd_max);
3e170ce0 2265 }
8ad349bb
A
2266 } else {
2267 th->th_seq = htonl(p->rxmit);
39037602
A
2268 if (len > 0) {
2269 m->m_pkthdr.pkt_flags |=
2270 (PKTF_TCP_REXMT | PKTF_START_SEQ);
2271 m->m_pkthdr.tx_start_seq = p->rxmit;
2272 }
3e170ce0 2273 tcp_rxtseg_insert(tp, p->rxmit, (p->rxmit + len - 1));
8ad349bb
A
2274 p->rxmit += len;
2275 tp->sackhint.sack_bytes_rexmit += len;
2276 }
1c79356b 2277 th->th_ack = htonl(tp->rcv_nxt);
4a3eedf9 2278 tp->last_ack_sent = tp->rcv_nxt;
1c79356b
A
2279 if (optlen) {
2280 bcopy(opt, th + 1, optlen);
f427ee49 2281 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
1c79356b
A
2282 }
2283 th->th_flags = flags;
f427ee49
A
2284 th->th_win = htons((u_short) (recwin >> tp->rcv_scale));
2285 tp->t_last_recwin = recwin;
cb323159
A
2286 if (!(so->so_flags & SOF_MP_SUBFLOW)) {
2287 if (recwin > 0 && SEQ_LT(tp->rcv_adv, tp->rcv_nxt + recwin)) {
2288 tp->rcv_adv = tp->rcv_nxt + recwin;
2289 }
2290 } else {
2291 struct mptcb *mp_tp = tptomptp(tp);
2292 if (recwin > 0) {
2293 tp->rcv_adv = tp->rcv_nxt + recwin;
2294 }
2295
f427ee49
A
2296 if (recwin > 0 && MPTCP_SEQ_LT(mp_tp->mpt_rcvadv, mp_tp->mpt_rcvnxt + recwin)) {
2297 mp_tp->mpt_rcvadv = mp_tp->mpt_rcvnxt + recwin;
cb323159
A
2298 }
2299 }
91447636 2300
8ad349bb
A
2301 /*
2302 * Adjust the RXWIN0SENT flag - indicate that we have advertised
2303 * a 0 window. This may cause the remote transmitter to stall. This
2304 * flag tells soreceive() to disable delayed acknowledgements when
2305 * draining the buffer. This can occur if the receiver is attempting
2306 * to read more data then can be buffered prior to transmitting on
2307 * the connection.
2308 */
f427ee49 2309 if (th->th_win == 0) {
8ad349bb 2310 tp->t_flags |= TF_RXWIN0SENT;
f427ee49 2311 } else {
8ad349bb 2312 tp->t_flags &= ~TF_RXWIN0SENT;
f427ee49 2313 }
cb323159 2314
1c79356b
A
2315 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
2316 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
2317 th->th_flags |= TH_URG;
39236c6e 2318 } else {
1c79356b
A
2319 /*
2320 * If no urgent pointer to send, then we pull
2321 * the urgent pointer to the left edge of the send window
2322 * so that it doesn't drift into the send window on sequence
2323 * number wraparound.
2324 */
f427ee49 2325 tp->snd_up = tp->snd_una; /* drag it along */
39236c6e 2326 }
1c79356b
A
2327
2328 /*
2329 * Put TCP length in extended header, and then
2330 * checksum extended header and data.
2331 */
9bccf70c 2332 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
39037602
A
2333
2334 /*
2335 * If this is potentially the last packet on the stream, then mark
2336 * it in order to enable some optimizations in the underlying
2337 * layers
2338 */
2339 if (tp->t_state != TCPS_ESTABLISHED &&
2340 (tp->t_state == TCPS_CLOSING || tp->t_state == TCPS_TIME_WAIT
f427ee49 2341 || tp->t_state == TCPS_LAST_ACK || (th->th_flags & TH_RST))) {
39037602 2342 m->m_pkthdr.pkt_flags |= PKTF_LAST_PKT;
f427ee49 2343 }
39037602 2344
6d2010ae 2345 if (isipv6) {
9bccf70c
A
2346 /*
2347 * ip6_plen is not need to be filled now, and will be filled
2348 * in ip6_output.
2349 */
6d2010ae
A
2350 m->m_pkthdr.csum_flags = CSUM_TCPIPV6;
2351 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
f427ee49 2352 if (len + optlen) {
39037602 2353 th->th_sum = in_addword(th->th_sum,
f427ee49
A
2354 htons((u_short)(optlen + len)));
2355 }
2356 } else {
9bccf70c
A
2357 m->m_pkthdr.csum_flags = CSUM_TCP;
2358 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
f427ee49 2359 if (len + optlen) {
39037602 2360 th->th_sum = in_addword(th->th_sum,
f427ee49
A
2361 htons((u_short)(optlen + len)));
2362 }
2d21ac55 2363 }
1c79356b 2364
b0d623f7
A
2365 /*
2366 * Enable TSO and specify the size of the segments.
2367 * The TCP pseudo header checksum is always provided.
b0d623f7
A
2368 */
2369 if (tso) {
f427ee49 2370 if (isipv6) {
39236c6e 2371 m->m_pkthdr.csum_flags |= CSUM_TSO_IPV6;
f427ee49 2372 } else {
39236c6e 2373 m->m_pkthdr.csum_flags |= CSUM_TSO_IPV4;
f427ee49 2374 }
b0d623f7
A
2375
2376 m->m_pkthdr.tso_segsz = tp->t_maxopd - optlen;
39236c6e 2377 } else {
b0d623f7 2378 m->m_pkthdr.tso_segsz = 0;
39236c6e 2379 }
b0d623f7 2380
1c79356b
A
2381 /*
2382 * In transmit state, time the transmission and arrange for
2383 * the retransmit. In persist state, just set snd_max.
2384 */
fe8ab488
A
2385 if (!(tp->t_flagsext & TF_FORCE)
2386 || tp->t_timer[TCPT_PERSIST] == 0) {
1c79356b
A
2387 tcp_seq startseq = tp->snd_nxt;
2388
2389 /*
2390 * Advance snd_nxt over sequence space of this segment.
2391 */
f427ee49
A
2392 if (flags & (TH_SYN | TH_FIN)) {
2393 if (flags & TH_SYN) {
1c79356b 2394 tp->snd_nxt++;
f427ee49 2395 }
39037602 2396 if ((flags & TH_FIN) &&
f427ee49 2397 !(tp->t_flags & TF_SENTFIN)) {
1c79356b
A
2398 tp->snd_nxt++;
2399 tp->t_flags |= TF_SENTFIN;
2400 }
2401 }
f427ee49 2402 if (sack_rxmit) {
8ad349bb 2403 goto timer;
f427ee49 2404 }
3e170ce0
A
2405 if (sack_rescue_rxt == TRUE) {
2406 tp->snd_nxt = old_snd_nxt;
2407 sack_rescue_rxt = FALSE;
2408 tcpstat.tcps_pto_in_recovery++;
2409 } else {
2410 tp->snd_nxt += len;
2411 }
1c79356b
A
2412 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
2413 tp->snd_max = tp->snd_nxt;
5ba3f43e 2414 tp->t_sndtime = tcp_now;
1c79356b
A
2415 /*
2416 * Time this transmission if not a retransmission and
2417 * not currently timing anything.
2418 */
9bccf70c 2419 if (tp->t_rtttime == 0) {
6d2010ae 2420 tp->t_rtttime = tcp_now;
1c79356b
A
2421 tp->t_rtseq = startseq;
2422 tcpstat.tcps_segstimed++;
3e170ce0
A
2423
2424 /* update variables related to pipe ack */
2425 tp->t_pipeack_lastuna = tp->snd_una;
1c79356b
A
2426 }
2427 }
2428
2429 /*
2430 * Set retransmit timer if not currently set,
2431 * and not doing an ack or a keep-alive probe.
1c79356b 2432 */
8ad349bb 2433timer:
1c79356b 2434 if (tp->t_timer[TCPT_REXMT] == 0 &&
8ad349bb 2435 ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
f427ee49 2436 tp->snd_nxt != tp->snd_una || (flags & TH_FIN))) {
1c79356b
A
2437 if (tp->t_timer[TCPT_PERSIST]) {
2438 tp->t_timer[TCPT_PERSIST] = 0;
6d2010ae 2439 tp->t_persist_stop = 0;
39037602 2440 TCP_RESET_REXMT_STATE(tp);
1c79356b 2441 }
fe8ab488 2442 tp->t_timer[TCPT_REXMT] =
f427ee49 2443 OFFSET_FROM_START(tp, tp->t_rxtcur);
fe8ab488
A
2444 }
2445
2446 /*
2447 * Set tail loss probe timeout if new data is being
2448 * transmitted. This will be supported only when
2449 * SACK option is enabled on a connection.
2450 *
2451 * Every time new data is sent PTO will get reset.
2452 */
d9a64523
A
2453 if (tcp_enable_tlp && len != 0 && tp->t_state == TCPS_ESTABLISHED &&
2454 SACK_ENABLED(tp) && !IN_FASTRECOVERY(tp) &&
2455 tp->snd_nxt == tp->snd_max &&
2456 SEQ_GT(tp->snd_nxt, tp->snd_una) &&
2457 tp->t_rxtshift == 0 &&
f427ee49
A
2458 (tp->t_flagsext & (TF_SENT_TLPROBE | TF_PKTS_REORDERED)) == 0) {
2459 uint32_t pto, srtt;
2460
2461 if (tcp_do_better_lr) {
2462 srtt = tp->t_srtt >> TCP_RTT_SHIFT;
2463 pto = 2 * srtt;
2464 if ((tp->snd_max - tp->snd_una) <= tp->t_maxseg) {
2465 pto += tcp_delack;
2466 } else {
2467 pto += 2;
2468 }
2469 } else {
2470 /*
2471 * Using SRTT alone to set PTO can cause spurious
2472 * retransmissions on wireless networks where there
2473 * is a lot of variance in RTT. Taking variance
2474 * into account will avoid this.
2475 */
2476 srtt = tp->t_srtt >> TCP_RTT_SHIFT;
2477 pto = ((TCP_REXMTVAL(tp)) * 3) >> 1;
2478 pto = max(2 * srtt, pto);
2479 if ((tp->snd_max - tp->snd_una) == tp->t_maxseg) {
2480 pto = max(pto,
2481 (((3 * pto) >> 2) + tcp_delack * 2));
2482 } else {
2483 pto = max(10, pto);
2484 }
2485 }
fe8ab488
A
2486
2487 /* if RTO is less than PTO, choose RTO instead */
f427ee49 2488 if (tp->t_rxtcur < pto) {
fe8ab488 2489 pto = tp->t_rxtcur;
f427ee49 2490 }
fe8ab488 2491
fe8ab488 2492 tp->t_timer[TCPT_PTO] = OFFSET_FROM_START(tp, pto);
1c79356b 2493 }
8ad349bb
A
2494 } else {
2495 /*
2496 * Persist case, update snd_max but since we are in
2497 * persist mode (no window) we do not update snd_nxt.
2498 */
2499 int xlen = len;
f427ee49 2500 if (flags & TH_SYN) {
8ad349bb 2501 ++xlen;
f427ee49 2502 }
39037602 2503 if ((flags & TH_FIN) &&
f427ee49 2504 !(tp->t_flags & TF_SENTFIN)) {
8ad349bb
A
2505 ++xlen;
2506 tp->t_flags |= TF_SENTFIN;
2507 }
5ba3f43e 2508 if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) {
1c79356b 2509 tp->snd_max = tp->snd_nxt + len;
5ba3f43e
A
2510 tp->t_sndtime = tcp_now;
2511 }
8ad349bb 2512 }
1c79356b
A
2513
2514#if TCPDEBUG
2515 /*
2516 * Trace.
2517 */
f427ee49 2518 if (so_options & SO_DEBUG) {
9bccf70c 2519 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
f427ee49 2520 }
9bccf70c 2521#endif
1c79356b
A
2522
2523 /*
2524 * Fill in IP length and desired time to live and
2525 * send to IP level. There should be a better way
2526 * to handle ttl and tos; we could keep them in
2527 * the template, but need a way to checksum without them.
2528 */
9bccf70c
A
2529 /*
2530 * m->m_pkthdr.len should have been set before cksum calcuration,
2531 * because in6_cksum() need it.
2532 */
1c79356b 2533 if (isipv6) {
9bccf70c 2534 /*
1c79356b
A
2535 * we separately set hoplimit for every segment, since the
2536 * user might want to change the value via setsockopt.
2537 * Also, desired default hop limit might be changed via
2538 * Neighbor Discovery.
2539 */
39236c6e
A
2540 ip6->ip6_hlim = in6_selecthlim(inp, inp->in6p_route.ro_rt ?
2541 inp->in6p_route.ro_rt->rt_ifp : NULL);
1c79356b
A
2542
2543 /* TODO: IPv6 IP6TOS_ECT bit on */
316670eb 2544 KERNEL_DEBUG(DBG_LAYER_BEG,
39236c6e
A
2545 ((inp->inp_fport << 16) | inp->inp_lport),
2546 (((inp->in6p_laddr.s6_addr16[0] & 0xffff) << 16) |
2547 (inp->in6p_faddr.s6_addr16[0] & 0xffff)),
f427ee49
A
2548 sendalot, 0, 0);
2549 } else {
2550 ASSERT(m->m_pkthdr.len <= IP_MAXPACKET);
2551 ip->ip_len = (u_short)m->m_pkthdr.len;
2552 ip->ip_ttl = inp->inp_ip_ttl; /* XXX */
39236c6e 2553 ip->ip_tos |= (inp->inp_ip_tos & ~IPTOS_ECN_MASK);/* XXX */
f427ee49
A
2554 KERNEL_DEBUG(DBG_LAYER_BEG,
2555 ((inp->inp_fport << 16) | inp->inp_lport),
2556 (((inp->inp_laddr.s_addr & 0xffff) << 16) |
2557 (inp->inp_faddr.s_addr & 0xffff)), 0, 0, 0);
2558 }
9bccf70c 2559
1c79356b 2560 /*
2d21ac55
A
2561 * See if we should do MTU discovery.
2562 * Look at the flag updated on the following criterias:
2563 * 1) Path MTU discovery is authorized by the sysctl
2564 * 2) The route isn't set yet (unlikely but could happen)
2565 * 3) The route is up
2566 * 4) the MTU is not locked (if it is, then discovery has been
2567 * disabled for that route)
1c79356b 2568 */
f427ee49
A
2569 if (!isipv6) {
2570 if (path_mtu_discovery && (tp->t_flags & TF_PMTUD)) {
316670eb 2571 ip->ip_off |= IP_DF;
f427ee49
A
2572 }
2573 }
3e170ce0 2574
fe8ab488
A
2575#if NECP
2576 {
2577 necp_kernel_policy_id policy_id;
d9a64523 2578 necp_kernel_policy_id skip_policy_id;
3e170ce0 2579 u_int32_t route_rule_id;
f427ee49
A
2580 u_int32_t pass_flags;
2581 if (!necp_socket_is_allowed_to_send_recv(inp, NULL, 0, &policy_id, &route_rule_id, &skip_policy_id, &pass_flags)) {
cb323159 2582 TCP_LOG_DROP_NECP(isipv6 ? (void *)ip6 : (void *)ip, th, tp, true);
fe8ab488
A
2583 m_freem(m);
2584 error = EHOSTUNREACH;
2585 goto out;
2586 }
f427ee49 2587 necp_mark_packet_from_socket(m, inp, policy_id, route_rule_id, skip_policy_id, pass_flags);
39037602
A
2588
2589 if (net_qos_policy_restricted != 0) {
f427ee49 2590 necp_socket_update_qos_marking(inp, inp->inp_route.ro_rt, route_rule_id);
39037602 2591 }
fe8ab488
A
2592 }
2593#endif /* NECP */
3e170ce0 2594
1c79356b 2595#if IPSEC
f427ee49 2596 if (inp->inp_sp != NULL) {
fe8ab488 2597 ipsec_setsocket(m, so);
f427ee49 2598 }
1c79356b 2599#endif /*IPSEC*/
91447636
A
2600
2601 /*
2602 * The socket is kept locked while sending out packets in ip_output, even if packet chaining is not active.
2603 */
2d21ac55 2604 lost = 0;
316670eb
A
2605
2606 /*
2607 * Embed the flow hash in pkt hdr and mark the packet as
2608 * capable of flow controlling
2609 */
39236c6e
A
2610 m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
2611 m->m_pkthdr.pkt_flowid = inp->inp_flowhash;
5ba3f43e 2612 m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC | PKTF_FLOW_ADV);
39236c6e 2613 m->m_pkthdr.pkt_proto = IPPROTO_TCP;
d9a64523 2614 m->m_pkthdr.tx_tcp_pid = so->last_pid;
f427ee49 2615 if (so->so_flags & SOF_DELEGATED) {
d9a64523 2616 m->m_pkthdr.tx_tcp_e_pid = so->e_pid;
f427ee49 2617 } else {
d9a64523 2618 m->m_pkthdr.tx_tcp_e_pid = 0;
f427ee49 2619 }
316670eb 2620
2d21ac55 2621 m->m_nextpkt = NULL;
6d2010ae 2622
39236c6e
A
2623 if (inp->inp_last_outifp != NULL &&
2624 !(inp->inp_last_outifp->if_flags & IFF_LOOPBACK)) {
316670eb
A
2625 /* Hint to prioritize this packet if
2626 * 1. if the packet has no data
39037602 2627 * 2. the interface supports transmit-start model and did
316670eb
A
2628 * not disable ACK prioritization.
2629 * 3. Only ACK flag is set.
2630 * 4. there is no outstanding data on this connection.
2631 */
f427ee49 2632 if (len == 0 && (inp->inp_last_outifp->if_eflags & (IFEF_TXSTART | IFEF_NOACKPRI)) == IFEF_TXSTART) {
39037602
A
2633 if (th->th_flags == TH_ACK &&
2634 tp->snd_una == tp->snd_max &&
f427ee49 2635 tp->t_timer[TCPT_REXMT] == 0) {
39037602 2636 svc_flags |= PKT_SCF_TCP_ACK;
f427ee49
A
2637 }
2638 if (th->th_flags & TH_SYN) {
39037602 2639 svc_flags |= PKT_SCF_TCP_SYN;
f427ee49 2640 }
39037602
A
2641 }
2642 set_packet_service_class(m, so, sotc, svc_flags);
2643 } else {
2644 /*
2645 * Optimization for loopback just set the mbuf
2646 * service class
2647 */
2648 (void) m_set_service_class(m, so_tc2msc(sotc));
316670eb 2649 }
6d2010ae 2650
cb323159
A
2651 TCP_LOG_TH_FLAGS(isipv6 ? (void *)ip6 : (void *)ip, th, tp, true,
2652 inp->inp_last_outifp != NULL ? inp->inp_last_outifp :
2653 inp->inp_boundifp);
2654
2d21ac55
A
2655 tp->t_pktlist_sentlen += len;
2656 tp->t_lastchain++;
6d2010ae 2657
316670eb 2658 if (isipv6) {
39236c6e 2659 DTRACE_TCP5(send, struct mbuf *, m, struct inpcb *, inp,
f427ee49
A
2660 struct ip6 *, ip6, struct tcpcb *, tp, struct tcphdr *,
2661 th);
2662 } else {
39236c6e 2663 DTRACE_TCP5(send, struct mbuf *, m, struct inpcb *, inp,
f427ee49 2664 struct ip *, ip, struct tcpcb *, tp, struct tcphdr *, th);
316670eb 2665 }
6d2010ae 2666
2d21ac55
A
2667 if (tp->t_pktlist_head != NULL) {
2668 tp->t_pktlist_tail->m_nextpkt = m;
2669 tp->t_pktlist_tail = m;
2670 } else {
91447636 2671 packchain_newlist++;
2d21ac55 2672 tp->t_pktlist_head = tp->t_pktlist_tail = m;
91447636
A
2673 }
2674
2d21ac55 2675 if (sendalot == 0 || (tp->t_state != TCPS_ESTABLISHED) ||
fe8ab488 2676 (tp->snd_cwnd <= (tp->snd_wnd / 8)) ||
5ba3f43e 2677 (tp->t_flags & TF_ACKNOW) ||
fe8ab488
A
2678 (tp->t_flagsext & TF_FORCE) ||
2679 tp->t_lastchain >= tcp_packet_chaining) {
2d21ac55 2680 error = 0;
39236c6e 2681 while (inp->inp_sndinprog_cnt == 0 &&
f427ee49 2682 tp->t_pktlist_head != NULL) {
2d21ac55
A
2683 packetlist = tp->t_pktlist_head;
2684 packchain_listadd = tp->t_lastchain;
2685 packchain_sent++;
2686 lost = tp->t_pktlist_sentlen;
2687 TCP_PKTLIST_CLEAR(tp);
2d21ac55
A
2688
2689 error = tcp_ip_output(so, tp, packetlist,
2690 packchain_listadd, tp_inp_options,
316670eb 2691 (so_options & SO_DONTROUTE),
5ba3f43e 2692 (sack_rxmit || (sack_bytes_rxmt != 0)), isipv6);
2d21ac55
A
2693 if (error) {
2694 /*
2695 * Take into account the rest of unsent
2696 * packets in the packet list for this tcp
2697 * into "lost", since we're about to free
2698 * the whole list below.
2699 */
2700 lost += tp->t_pktlist_sentlen;
2701 break;
2702 } else {
2703 lost = 0;
2704 }
2705 }
2706 /* tcp was closed while we were in ip; resume close */
39236c6e 2707 if (inp->inp_sndinprog_cnt == 0 &&
f427ee49 2708 (tp->t_flags & TF_CLOSING)) {
2d21ac55
A
2709 tp->t_flags &= ~TF_CLOSING;
2710 (void) tcp_close(tp);
cb323159 2711 return 0;
2d21ac55 2712 }
316670eb 2713 } else {
91447636
A
2714 error = 0;
2715 packchain_looped++;
2716 tcpstat.tcps_sndtotal++;
2d21ac55 2717
91447636
A
2718 goto again;
2719 }
1c79356b 2720 if (error) {
9bccf70c 2721 /*
2d21ac55
A
2722 * Assume that the packets were lost, so back out the
2723 * sequence number advance, if any. Note that the "lost"
2724 * variable represents the amount of user data sent during
2725 * the recent call to ip_output_list() plus the amount of
2726 * user data in the packet list for this tcp at the moment.
9bccf70c 2727 */
fe8ab488
A
2728 if (!(tp->t_flagsext & TF_FORCE)
2729 || tp->t_timer[TCPT_PERSIST] == 0) {
9bccf70c
A
2730 /*
2731 * No need to check for TH_FIN here because
2732 * the TF_SENTFIN flag handles that case.
2733 */
8ad349bb
A
2734 if ((flags & TH_SYN) == 0) {
2735 if (sack_rxmit) {
39236c6e
A
2736 if (SEQ_GT((p->rxmit - lost),
2737 tp->snd_una)) {
2738 p->rxmit -= lost;
f427ee49
A
2739
2740 if (SEQ_LT(p->rxmit, p->start)) {
2741 p->rxmit = p->start;
2742 }
39236c6e
A
2743 } else {
2744 lost = p->rxmit - tp->snd_una;
2745 p->rxmit = tp->snd_una;
f427ee49
A
2746
2747 if (SEQ_LT(p->rxmit, p->start)) {
2748 p->rxmit = p->start;
2749 }
39236c6e 2750 }
2d21ac55 2751 tp->sackhint.sack_bytes_rexmit -= lost;
f427ee49
A
2752 if (tp->sackhint.sack_bytes_rexmit < 0) {
2753 tp->sackhint.sack_bytes_rexmit = 0;
2754 }
39236c6e
A
2755 } else {
2756 if (SEQ_GT((tp->snd_nxt - lost),
f427ee49 2757 tp->snd_una)) {
39236c6e 2758 tp->snd_nxt -= lost;
f427ee49 2759 } else {
39236c6e 2760 tp->snd_nxt = tp->snd_una;
f427ee49 2761 }
39236c6e 2762 }
8ad349bb 2763 }
9bccf70c 2764 }
1c79356b 2765out:
f427ee49 2766 if (tp->t_pktlist_head != NULL) {
2d21ac55 2767 m_freem_list(tp->t_pktlist_head);
f427ee49 2768 }
2d21ac55
A
2769 TCP_PKTLIST_CLEAR(tp);
2770
1c79356b 2771 if (error == ENOBUFS) {
a39ff7e2
A
2772 /*
2773 * Set retransmit timer if not currently set
2774 * when we failed to send a segment that can be
2775 * retransmitted (i.e. not pure ack or rst)
2776 */
cb323159
A
2777 if (tp->t_timer[TCPT_REXMT] == 0 &&
2778 tp->t_timer[TCPT_PERSIST] == 0 &&
a39ff7e2 2779 (len != 0 || (flags & (TH_SYN | TH_FIN)) != 0 ||
f427ee49 2780 so->so_snd.sb_cc > 0)) {
39037602 2781 tp->t_timer[TCPT_REXMT] =
f427ee49
A
2782 OFFSET_FROM_START(tp, tp->t_rxtcur);
2783 }
b0d623f7
A
2784 tp->snd_cwnd = tp->t_maxseg;
2785 tp->t_bytes_acked = 0;
6d2010ae 2786 tcp_check_timer_state(tp);
f427ee49 2787 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
6d2010ae 2788
fe8ab488 2789 tcp_ccdbg_trace(tp, NULL, TCP_CC_OUTPUT_ERROR);
cb323159 2790 return 0;
1c79356b 2791 }
1c79356b
A
2792 if (error == EMSGSIZE) {
2793 /*
2794 * ip_output() will have already fixed the route
2795 * for us. tcp_mtudisc() will, as its last action,
2796 * initiate retransmission, so it is important to
2797 * not do so here.
b0d623f7
A
2798 *
2799 * If TSO was active we either got an interface
2800 * without TSO capabilits or TSO was turned off.
2801 * Disable it for this connection as too and
2802 * immediatly retry with MSS sized segments generated
2803 * by this function.
1c79356b 2804 */
f427ee49 2805 if (tso) {
b0d623f7 2806 tp->t_flags &= ~TF_TSO;
f427ee49 2807 }
b0d623f7 2808
39236c6e 2809 tcp_mtudisc(inp, 0);
6d2010ae
A
2810 tcp_check_timer_state(tp);
2811
f427ee49 2812 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
1c79356b
A
2813 return 0;
2814 }
39236c6e
A
2815 /*
2816 * Unless this is due to interface restriction policy,
2817 * treat EHOSTUNREACH/ENETDOWN as a soft error.
2818 */
2819 if ((error == EHOSTUNREACH || error == ENETDOWN) &&
39037602 2820 TCPS_HAVERCVDSYN(tp->t_state) &&
fe8ab488 2821 !inp_restricted_send(inp, inp->inp_last_outifp)) {
f427ee49
A
2822 tp->t_softerror = error;
2823 error = 0;
1c79356b 2824 }
6d2010ae 2825 tcp_check_timer_state(tp);
f427ee49 2826 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
cb323159 2827 return error;
1c79356b 2828 }
2d21ac55 2829
1c79356b
A
2830 tcpstat.tcps_sndtotal++;
2831
f427ee49
A
2832 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
2833 if (sendalot) {
1c79356b 2834 goto again;
f427ee49 2835 }
6d2010ae
A
2836
2837 tcp_check_timer_state(tp);
cb323159
A
2838
2839 return 0;
1c79356b
A
2840}
2841
2d21ac55
A
2842static int
2843tcp_ip_output(struct socket *so, struct tcpcb *tp, struct mbuf *pkt,
813fb2f6 2844 int cnt, struct mbuf *opt, int flags, int sack_in_progress, boolean_t isipv6)
2d21ac55
A
2845{
2846 int error = 0;
2847 boolean_t chain;
2848 boolean_t unlocked = FALSE;
39236c6e 2849 boolean_t ifdenied = FALSE;
c910b4d9 2850 struct inpcb *inp = tp->t_inpcb;
a39ff7e2 2851 struct ip_out_args ipoa;
b0d623f7 2852 struct route ro;
316670eb 2853 struct ifnet *outif = NULL;
f427ee49 2854 bool check_qos_marking_again = (so->so_flags1 & SOF1_QOSMARKING_POLICY_OVERRIDE) ? FALSE : TRUE;
a39ff7e2
A
2855
2856 bzero(&ipoa, sizeof(ipoa));
2857 ipoa.ipoa_boundif = IFSCOPE_NONE;
2858 ipoa.ipoa_flags = IPOAF_SELECT_SRCIF | IPOAF_BOUND_SRCADDR;
2859 ipoa.ipoa_sotc = SO_TC_UNSPEC;
2860 ipoa.ipoa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
a39ff7e2 2861 struct ip6_out_args ip6oa;
316670eb 2862 struct route_in6 ro6;
a39ff7e2
A
2863
2864 bzero(&ip6oa, sizeof(ip6oa));
2865 ip6oa.ip6oa_boundif = IFSCOPE_NONE;
2866 ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR;
2867 ip6oa.ip6oa_sotc = SO_TC_UNSPEC;
2868 ip6oa.ip6oa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
2869
316670eb
A
2870 struct flowadv *adv =
2871 (isipv6 ? &ip6oa.ip6oa_flowadv : &ipoa.ipoa_flowadv);
c910b4d9
A
2872
2873 /* If socket was bound to an ifindex, tell ip_output about it */
316670eb 2874 if (inp->inp_flags & INP_BOUND_IF) {
316670eb
A
2875 if (isipv6) {
2876 ip6oa.ip6oa_boundif = inp->inp_boundifp->if_index;
2877 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
f427ee49 2878 } else {
316670eb
A
2879 ipoa.ipoa_boundif = inp->inp_boundifp->if_index;
2880 ipoa.ipoa_flags |= IPOAF_BOUND_IF;
2881 }
2882 }
2883
fe8ab488 2884 if (INP_NO_CELLULAR(inp)) {
f427ee49 2885 if (isipv6) {
316670eb 2886 ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR;
f427ee49 2887 } else {
316670eb 2888 ipoa.ipoa_flags |= IPOAF_NO_CELLULAR;
f427ee49 2889 }
39037602 2890 }
fe8ab488 2891 if (INP_NO_EXPENSIVE(inp)) {
f427ee49 2892 if (isipv6) {
fe8ab488 2893 ip6oa.ip6oa_flags |= IP6OAF_NO_EXPENSIVE;
f427ee49 2894 } else {
fe8ab488 2895 ipoa.ipoa_flags |= IPOAF_NO_EXPENSIVE;
f427ee49 2896 }
fe8ab488 2897 }
cb323159 2898 if (INP_NO_CONSTRAINED(inp)) {
f427ee49 2899 if (isipv6) {
cb323159 2900 ip6oa.ip6oa_flags |= IP6OAF_NO_CONSTRAINED;
f427ee49 2901 } else {
cb323159 2902 ipoa.ipoa_flags |= IPOAF_NO_CONSTRAINED;
f427ee49 2903 }
cb323159 2904 }
fe8ab488 2905 if (INP_AWDL_UNRESTRICTED(inp)) {
f427ee49 2906 if (isipv6) {
fe8ab488 2907 ip6oa.ip6oa_flags |= IP6OAF_AWDL_UNRESTRICTED;
f427ee49 2908 } else {
fe8ab488 2909 ipoa.ipoa_flags |= IPOAF_AWDL_UNRESTRICTED;
f427ee49 2910 }
39037602 2911 }
39037602
A
2912 if (INP_INTCOPROC_ALLOWED(inp) && isipv6) {
2913 ip6oa.ip6oa_flags |= IP6OAF_INTCOPROC_ALLOWED;
2914 }
2915 if (isipv6) {
2916 ip6oa.ip6oa_sotc = so->so_traffic_class;
2917 ip6oa.ip6oa_netsvctype = so->so_netsvctype;
f427ee49
A
2918 ip6oa.qos_marking_gencount = inp->inp_policyresult.results.qos_marking_gencount;
2919 } else {
39037602
A
2920 ipoa.ipoa_sotc = so->so_traffic_class;
2921 ipoa.ipoa_netsvctype = so->so_netsvctype;
f427ee49 2922 ipoa.qos_marking_gencount = inp->inp_policyresult.results.qos_marking_gencount;
39037602
A
2923 }
2924 if ((so->so_flags1 & SOF1_QOSMARKING_ALLOWED)) {
f427ee49 2925 if (isipv6) {
39037602 2926 ip6oa.ip6oa_flags |= IP6OAF_QOSMARKING_ALLOWED;
f427ee49 2927 } else {
39037602 2928 ipoa.ipoa_flags |= IPOAF_QOSMARKING_ALLOWED;
f427ee49
A
2929 }
2930 }
2931 if (check_qos_marking_again) {
2932 if (isipv6) {
2933 ip6oa.ip6oa_flags |= IP6OAF_REDO_QOSMARKING_POLICY;
2934 } else {
2935 ipoa.ipoa_flags |= IPOAF_REDO_QOSMARKING_POLICY;
2936 }
316670eb 2937 }
f427ee49 2938 if (isipv6) {
316670eb 2939 flags |= IPV6_OUTARGS;
f427ee49 2940 } else {
316670eb 2941 flags |= IP_OUTARGS;
f427ee49 2942 }
2d21ac55 2943
b0d623f7 2944 /* Copy the cached route and take an extra reference */
f427ee49 2945 if (isipv6) {
316670eb 2946 in6p_route_copyout(inp, &ro6);
f427ee49 2947 } else {
316670eb 2948 inp_route_copyout(inp, &ro);
f427ee49 2949 }
b0d623f7
A
2950
2951 /*
2952 * Make sure ACK/DELACK conditions are cleared before
4a3eedf9
A
2953 * we unlock the socket.
2954 */
d1ecb069 2955 tp->last_ack_sent = tp->rcv_nxt;
4a3eedf9 2956 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
6d2010ae
A
2957 tp->t_timer[TCPT_DELACK] = 0;
2958 tp->t_unacksegs = 0;
b0d623f7 2959
316670eb
A
2960 /* Increment the count of outstanding send operations */
2961 inp->inp_sndinprog_cnt++;
2962
2d21ac55 2963 /*
b0d623f7 2964 * If allowed, unlock TCP socket while in IP
2d21ac55 2965 * but only if the connection is established and
6d2010ae
A
2966 * in a normal mode where reentrancy on the tcpcb won't be
2967 * an issue:
2968 * - there is no SACK episode
2969 * - we're not in Fast Recovery mode
2970 * - if we're not sending from an upcall.
b0d623f7 2971 */
316670eb 2972 if (tcp_output_unlocked && !so->so_upcallusecount &&
6d2010ae 2973 (tp->t_state == TCPS_ESTABLISHED) && (sack_in_progress == 0) &&
5ba3f43e 2974 !IN_FASTRECOVERY(tp) && !(so->so_flags & SOF_MP_SUBFLOW)) {
b0d623f7
A
2975 unlocked = TRUE;
2976 socket_unlock(so, 0);
2d21ac55 2977 }
39037602 2978
2d21ac55
A
2979 /*
2980 * Don't send down a chain of packets when:
2981 * - TCP chaining is disabled
2982 * - there is an IPsec rule set
2983 * - there is a non default rule set for the firewall
2984 */
2985
4a3eedf9 2986 chain = tcp_packet_chaining > 1
2d21ac55 2987#if IPSEC
f427ee49 2988 && ipsec_bypass
4a3eedf9 2989#endif
f427ee49 2990 ; // I'm important, not extraneous
4a3eedf9 2991
2d21ac55
A
2992 while (pkt != NULL) {
2993 struct mbuf *npkt = pkt->m_nextpkt;
2994
2995 if (!chain) {
2996 pkt->m_nextpkt = NULL;
2997 /*
2998 * If we are not chaining, make sure to set the packet
2999 * list count to 0 so that IP takes the right path;
cb323159 3000 * this is important for cases such as IPsec where a
2d21ac55
A
3001 * single mbuf might result in multiple mbufs as part
3002 * of the encapsulation. If a non-zero count is passed
3003 * down to IP, the head of the chain might change and
3004 * we could end up skipping it (thus generating bogus
3005 * packets). Fixing it in IP would be desirable, but
3006 * for now this would do it.
3007 */
3008 cnt = 0;
3009 }
39236c6e 3010 if (isipv6) {
316670eb
A
3011 error = ip6_output_list(pkt, cnt,
3012 inp->in6p_outputopts, &ro6, flags, NULL, NULL,
3013 &ip6oa);
39236c6e
A
3014 ifdenied = (ip6oa.ip6oa_retflags & IP6OARF_IFDENIED);
3015 } else {
316670eb
A
3016 error = ip_output_list(pkt, cnt, opt, &ro, flags, NULL,
3017 &ipoa);
39236c6e
A
3018 ifdenied = (ipoa.ipoa_retflags & IPOARF_IFDENIED);
3019 }
316670eb 3020
2d21ac55
A
3021 if (chain || error) {
3022 /*
3023 * If we sent down a chain then we are done since
3024 * the callee had taken care of everything; else
3025 * we need to free the rest of the chain ourselves.
3026 */
f427ee49 3027 if (!chain) {
2d21ac55 3028 m_freem_list(npkt);
f427ee49 3029 }
2d21ac55
A
3030 break;
3031 }
3032 pkt = npkt;
3033 }
3034
f427ee49 3035 if (unlocked) {
2d21ac55 3036 socket_lock(so, 0);
f427ee49 3037 }
2d21ac55 3038
39037602 3039 /*
316670eb 3040 * Enter flow controlled state if the connection is established
5ba3f43e
A
3041 * and is not in recovery. Flow control is allowed only if there
3042 * is outstanding data.
316670eb 3043 *
39037602 3044 * A connection will enter suspended state even if it is in
316670eb
A
3045 * recovery.
3046 */
3047 if (((adv->code == FADV_FLOW_CONTROLLED && !IN_FASTRECOVERY(tp)) ||
39037602 3048 adv->code == FADV_SUSPENDED) &&
316670eb 3049 !(tp->t_flags & TF_CLOSING) &&
5ba3f43e
A
3050 tp->t_state == TCPS_ESTABLISHED &&
3051 SEQ_GT(tp->snd_max, tp->snd_una)) {
316670eb
A
3052 int rc;
3053 rc = inp_set_fc_state(inp, adv->code);
3054
f427ee49 3055 if (rc == 1) {
39037602 3056 tcp_ccdbg_trace(tp, NULL,
fe8ab488 3057 ((adv->code == FADV_FLOW_CONTROLLED) ?
316670eb 3058 TCP_CC_FLOW_CONTROL : TCP_CC_SUSPEND));
f427ee49 3059 }
316670eb 3060 }
6d2010ae 3061
39037602 3062 /*
316670eb
A
3063 * When an interface queue gets suspended, some of the
3064 * packets are dropped. Return ENOBUFS, to update the
3065 * pcb state.
3066 */
f427ee49 3067 if (adv->code == FADV_SUSPENDED) {
316670eb 3068 error = ENOBUFS;
f427ee49 3069 }
316670eb
A
3070
3071 VERIFY(inp->inp_sndinprog_cnt > 0);
f427ee49 3072 if (--inp->inp_sndinprog_cnt == 0) {
316670eb 3073 inp->inp_flags &= ~(INP_FC_FEEDBACK);
cb323159
A
3074 if (inp->inp_sndingprog_waiters > 0) {
3075 wakeup(&inp->inp_sndinprog_cnt);
3076 }
3077 }
b0d623f7 3078
316670eb 3079 if (isipv6) {
cb323159
A
3080 /*
3081 * When an NECP IP tunnel policy forces the outbound interface,
3082 * ip6_output_list() informs the transport layer what is the actual
3083 * outgoing interface
3084 */
3085 if (ip6oa.ip6oa_flags & IP6OAF_BOUND_IF) {
3086 outif = ifindex2ifnet[ip6oa.ip6oa_boundif];
3087 } else if (ro6.ro_rt != NULL) {
39037602 3088 outif = ro6.ro_rt->rt_ifp;
cb323159 3089 }
f427ee49
A
3090 } else {
3091 if (ro.ro_rt != NULL) {
39037602 3092 outif = ro.ro_rt->rt_ifp;
f427ee49
A
3093 }
3094 }
3095 if (check_qos_marking_again) {
3096 uint32_t qos_marking_gencount;
3097 bool allow_qos_marking;
3098 if (isipv6) {
3099 qos_marking_gencount = ip6oa.qos_marking_gencount;
3100 allow_qos_marking = ip6oa.ip6oa_flags & IP6OAF_QOSMARKING_ALLOWED ? TRUE : FALSE;
3101 } else {
3102 qos_marking_gencount = ipoa.qos_marking_gencount;
3103 allow_qos_marking = ipoa.ipoa_flags & IPOAF_QOSMARKING_ALLOWED ? TRUE : FALSE;
3104 }
3105 inp->inp_policyresult.results.qos_marking_gencount = qos_marking_gencount;
3106 if (allow_qos_marking == TRUE) {
3107 inp->inp_socket->so_flags1 |= SOF1_QOSMARKING_ALLOWED;
3108 } else {
3109 inp->inp_socket->so_flags1 &= ~SOF1_QOSMARKING_ALLOWED;
3110 }
3111 }
39037602 3112
cb323159 3113 if (outif != NULL && outif != inp->inp_last_outifp) {
39037602 3114 /* Update the send byte count */
cb323159 3115 if (so->so_snd.sb_cc > 0 && so->so_snd.sb_flags & SB_SNDBYTE_CNT) {
39037602
A
3116 inp_decr_sndbytes_total(so, so->so_snd.sb_cc);
3117 inp_decr_sndbytes_allunsent(so, tp->snd_una);
3118 so->so_snd.sb_flags &= ~SB_SNDBYTE_CNT;
3119 }
3120 inp->inp_last_outifp = outif;
3121 }
316670eb 3122
39037602 3123 if (error != 0 && ifdenied &&
f427ee49 3124 (INP_NO_CELLULAR(inp) || INP_NO_EXPENSIVE(inp) || INP_NO_CONSTRAINED(inp))) {
39037602 3125 soevent(so,
f427ee49
A
3126 (SO_FILT_HINT_LOCKED | SO_FILT_HINT_IFDENIED));
3127 }
316670eb
A
3128
3129 /* Synchronize cached PCB route & options */
f427ee49 3130 if (isipv6) {
316670eb 3131 in6p_route_copyin(inp, &ro6);
f427ee49 3132 } else {
316670eb 3133 inp_route_copyin(inp, &ro);
f427ee49 3134 }
316670eb 3135
39037602 3136 if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift == 0 &&
f427ee49 3137 tp->t_inpcb->inp_route.ro_rt != NULL) {
316670eb
A
3138 /* If we found the route and there is an rtt on it
3139 * reset the retransmit timer
3140 */
3141 tcp_getrt_rtt(tp, tp->t_inpcb->in6p_route.ro_rt);
3142 tp->t_timer[TCPT_REXMT] = OFFSET_FROM_START(tp, tp->t_rxtcur);
3143 }
cb323159 3144 return error;
2d21ac55
A
3145}
3146
5ba3f43e
A
3147int tcptv_persmin_val = TCPTV_PERSMIN;
3148
1c79356b 3149void
39037602 3150tcp_setpersist(struct tcpcb *tp)
1c79356b 3151{
9bccf70c 3152 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
1c79356b 3153
6d2010ae
A
3154 /* If a PERSIST_TIMER option was set we will limit the
3155 * time the persist timer will be active for that connection
3156 * in order to avoid DOS by using zero window probes.
3157 * see rdar://5805356
3158 */
3159
cb323159
A
3160 if (tp->t_persist_timeout != 0 &&
3161 tp->t_timer[TCPT_PERSIST] == 0 &&
3162 tp->t_persist_stop == 0) {
6d2010ae
A
3163 tp->t_persist_stop = tcp_now + tp->t_persist_timeout;
3164 }
3165
1c79356b
A
3166 /*
3167 * Start/restart persistance timer.
3168 */
3169 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
3170 t * tcp_backoff[tp->t_rxtshift],
5ba3f43e 3171 tcptv_persmin_val, TCPTV_PERSMAX, 0);
6d2010ae
A
3172 tp->t_timer[TCPT_PERSIST] = OFFSET_FROM_START(tp, tp->t_timer[TCPT_PERSIST]);
3173
f427ee49 3174 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) {
1c79356b 3175 tp->t_rxtshift++;
316670eb 3176 }
316670eb 3177}
39236c6e
A
3178
3179static int
f427ee49 3180tcp_recv_throttle(struct tcpcb *tp)
39236c6e
A
3181{
3182 uint32_t base_rtt, newsize;
39236c6e
A
3183 struct sockbuf *sbrcv = &tp->t_inpcb->inp_socket->so_rcv;
3184
3185 if (tcp_use_rtt_recvbg == 1 &&
3186 TSTMP_SUPPORTED(tp)) {
39037602 3187 /*
39236c6e
A
3188 * Timestamps are supported on this connection. Use
3189 * RTT to look for an increase in latency.
3190 */
3191
39037602 3192 /*
39236c6e
A
3193 * If the connection is already being throttled, leave it
3194 * in that state until rtt comes closer to base rtt
3195 */
f427ee49 3196 if (tp->t_flagsext & TF_RECV_THROTTLE) {
cb323159 3197 return 1;
f427ee49 3198 }
39236c6e
A
3199
3200 base_rtt = get_base_rtt(tp);
39037602 3201
39236c6e 3202 if (base_rtt != 0 && tp->t_rttcur != 0) {
39236c6e
A
3203 /*
3204 * if latency increased on a background flow,
3205 * return 1 to start throttling.
3206 */
ecc0ceb4 3207 if (tp->t_rttcur > (base_rtt + target_qdelay)) {
39236c6e 3208 tp->t_flagsext |= TF_RECV_THROTTLE;
f427ee49 3209 if (tp->t_recv_throttle_ts == 0) {
ecc0ceb4 3210 tp->t_recv_throttle_ts = tcp_now;
f427ee49 3211 }
39236c6e
A
3212 /*
3213 * Reduce the recv socket buffer size to
3214 * minimize latecy.
3215 */
39037602 3216 if (sbrcv->sb_idealsize >
39236c6e
A
3217 tcp_recv_throttle_minwin) {
3218 newsize = sbrcv->sb_idealsize >> 1;
3219 /* Set a minimum of 16 K */
39037602
A
3220 newsize =
3221 max(newsize,
39236c6e
A
3222 tcp_recv_throttle_minwin);
3223 sbrcv->sb_idealsize = newsize;
3224 }
cb323159 3225 return 1;
39236c6e 3226 } else {
cb323159 3227 return 0;
39236c6e
A
3228 }
3229 }
3230 }
3231
3232 /*
3233 * Timestamps are not supported or there is no good RTT
3234 * measurement. Use IPDV in this case.
3235 */
f427ee49 3236 if (tp->acc_iaj > tcp_acc_iaj_react_limit) {
cb323159 3237 return 1;
f427ee49 3238 }
39037602 3239
cb323159 3240 return 0;
39236c6e 3241}