2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
60 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
61 * $FreeBSD: src/sys/netinet/tcp_output.c,v 1.39.2.10 2001/07/07 04:30:38 silby Exp $
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,
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/sysctl.h>
78 #include <sys/domain.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
83 #include <net/route.h>
84 #include <net/ntstat.h>
85 #include <net/if_var.h>
87 #include <net/if_types.h>
90 #include <netinet/in.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/in_var.h>
93 #include <netinet/in_tclass.h>
94 #include <netinet/ip.h>
95 #include <netinet/in_pcb.h>
96 #include <netinet/ip_var.h>
98 #include <netinet6/in6_pcb.h>
99 #include <netinet/ip6.h>
100 #include <netinet6/ip6_var.h>
101 #include <netinet/tcp.h>
103 #include <netinet/tcp_cache.h>
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>
109 #include <netinet/tcp_cc.h>
111 #include <netinet/tcp_debug.h>
113 #include <netinet/tcp_log.h>
114 #include <sys/kdebug.h>
115 #include <mach/sdt.h>
118 #include <netinet6/ipsec.h>
122 #include <netinet/mptcp_var.h>
123 #include <netinet/mptcp.h>
124 #include <netinet/mptcp_opt.h>
125 #include <netinet/mptcp_seq.h>
128 #include <corecrypto/ccaes.h>
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)
134 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, path_mtu_discovery
,
135 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, path_mtu_discovery
, 1,
136 "Enable Path MTU Discovery");
138 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, local_slowstart_flightsize
,
139 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, ss_fltsz_local
, 8,
140 "Slow start flight size for local networks");
143 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tso
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
144 &tcp_do_tso
, 0, "Enable TCP Segmentation Offload");
146 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, ecn_setup_percentage
,
147 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_ecn_setup_percentage
, 100,
148 "Max ECN setup percentage");
150 SYSCTL_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)");
154 SYSCTL_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)");
158 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, randomize_timestamps
,
159 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_randomize_timestamps
, 1,
160 "Randomize TCP timestamps to prevent tracking (on: 1, off: 0)");
163 sysctl_change_ecn_setting SYSCTL_HANDLER_ARGS
165 #pragma unused(oidp, arg1, arg2)
166 int i
, err
= 0, changed
= 0;
169 err
= sysctl_io_number(req
, tcp_ecn_outbound
, sizeof(int32_t),
171 if (err
!= 0 || req
->newptr
== USER_ADDR_NULL
) {
176 if ((tcp_ecn_outbound
== 0 || tcp_ecn_outbound
== 1) &&
177 (i
== 0 || i
== 1)) {
178 tcp_ecn_outbound
= i
;
179 SYSCTL_SKMEM_UPDATE_FIELD(tcp
.ecn_initiate_out
, tcp_ecn_outbound
);
182 if (tcp_ecn_outbound
== 2 && (i
== 0 || i
== 1)) {
184 * Reset ECN enable flags on non-cellular
185 * interfaces so that the system default will take
188 ifnet_head_lock_shared();
189 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
190 if (!IFNET_IS_CELLULAR(ifp
)) {
199 * Set ECN enable flags on non-cellular
202 ifnet_head_lock_shared();
203 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
204 if (!IFNET_IS_CELLULAR(ifp
)) {
205 if_set_eflags(ifp
, IFEF_ECN_ENABLE
);
206 if_clear_eflags(ifp
, IFEF_ECN_DISABLE
);
211 tcp_ecn_outbound
= i
;
212 SYSCTL_SKMEM_UPDATE_FIELD(tcp
.ecn_initiate_out
, tcp_ecn_outbound
);
214 /* Change the other one too as the work is done */
215 if (i
== 2 || tcp_ecn_inbound
== 2) {
217 SYSCTL_SKMEM_UPDATE_FIELD(tcp
.ecn_negotiate_in
, tcp_ecn_inbound
);
222 int tcp_ecn_outbound
= 2;
223 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, ecn_initiate_out
,
224 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_ecn_outbound
, 0,
225 sysctl_change_ecn_setting
, "IU",
226 "Initiate ECN for outbound connections");
228 int tcp_ecn_inbound
= 2;
229 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, ecn_negotiate_in
,
230 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_ecn_inbound
, 0,
231 sysctl_change_ecn_setting
, "IU",
232 "Initiate ECN for inbound connections");
234 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, packetchain
,
235 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_packet_chaining
, 50,
236 "Enable TCP output packet chaining");
238 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, socket_unlocked_on_output
,
239 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_output_unlocked
, 1,
240 "Unlock TCP when sending packets down to IP");
242 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, min_iaj_win
,
243 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_min_iaj_win
, MIN_IAJ_WIN
,
244 "Minimum recv win based on inter-packet arrival jitter");
246 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, acc_iaj_react_limit
,
247 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_acc_iaj_react_limit
,
248 ACC_IAJ_REACT_LIMIT
, "Accumulated IAJ when receiver starts to react");
250 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, autosndbufinc
,
251 CTLFLAG_RW
| CTLFLAG_LOCKED
, uint32_t, tcp_autosndbuf_inc
,
252 8 * 1024, "Increment in send socket bufffer size");
254 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, autosndbufmax
,
255 CTLFLAG_RW
| CTLFLAG_LOCKED
, uint32_t, tcp_autosndbuf_max
, 2 * 1024 * 1024,
256 "Maximum send socket buffer size");
258 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, rtt_recvbg
,
259 CTLFLAG_RW
| CTLFLAG_LOCKED
, uint32_t, tcp_use_rtt_recvbg
, 1,
260 "Use RTT for bg recv algorithm");
262 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, recv_throttle_minwin
,
263 CTLFLAG_RW
| CTLFLAG_LOCKED
, uint32_t, tcp_recv_throttle_minwin
, 16 * 1024,
264 "Minimum recv win for throttling");
266 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, enable_tlp
,
267 CTLFLAG_RW
| CTLFLAG_LOCKED
,
268 int32_t, tcp_enable_tlp
, 1, "Enable Tail loss probe");
270 static int32_t packchain_newlist
= 0;
271 static int32_t packchain_looped
= 0;
272 static int32_t packchain_sent
= 0;
274 /* temporary: for testing */
276 extern int ipsec_bypass
;
279 extern int slowlink_wsize
; /* window correction for slow links */
281 extern u_int32_t dlil_filter_disable_tso_count
;
282 extern u_int32_t kipf_count
;
284 static int tcp_ip_output(struct socket
*, struct tcpcb
*, struct mbuf
*,
285 int, struct mbuf
*, int, int, boolean_t
);
286 static int tcp_recv_throttle(struct tcpcb
*tp
);
289 tcp_tfo_check(struct tcpcb
*tp
, int32_t len
)
291 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
292 unsigned int optlen
= 0;
293 unsigned int cookie_len
;
295 if (tp
->t_flags
& TF_NOOPT
) {
299 if (!(tp
->t_flagsext
& TF_FASTOPEN_FORCE_ENABLE
) &&
300 !tcp_heuristic_do_tfo(tp
)) {
301 tp
->t_tfo_stats
|= TFO_S_HEURISTICS_DISABLE
;
302 tcpstat
.tcps_tfo_heuristics_disable
++;
306 if (so
->so_flags1
& SOF1_DATA_AUTHENTICATED
) {
310 optlen
+= TCPOLEN_MAXSEG
;
312 if (tp
->t_flags
& TF_REQ_SCALE
) {
317 if ((so
->so_flags
& SOF_MP_SUBFLOW
) && mptcp_enable
&&
318 (tp
->t_rxtshift
<= mptcp_mpcap_retries
||
319 (tptomptp(tp
)->mpt_mpte
->mpte_flags
& MPTE_FORCE_ENABLE
))) {
320 optlen
+= sizeof(struct mptcp_mpcapable_opt_common
) + sizeof(mptcp_key_t
);
324 if (tp
->t_flags
& TF_REQ_TSTMP
) {
325 optlen
+= TCPOLEN_TSTAMP_APPA
;
328 if (SACK_ENABLED(tp
)) {
329 optlen
+= TCPOLEN_SACK_PERMITTED
;
332 /* Now, decide whether to use TFO or not */
334 /* Don't even bother trying if there is no space at all... */
335 if (MAX_TCPOPTLEN
- optlen
< TCPOLEN_FASTOPEN_REQ
) {
339 cookie_len
= tcp_cache_get_cookie_len(tp
);
340 if (cookie_len
== 0) {
341 /* No cookie, so we request one */
345 /* There is not enough space for the cookie, so we cannot do TFO */
346 if (MAX_TCPOPTLEN
- optlen
< cookie_len
) {
350 /* Do not send SYN+data if there is more in the queue than MSS */
351 if (so
->so_snd
.sb_cc
> (tp
->t_maxopd
- MAX_TCPOPTLEN
)) {
355 /* Ok, everything looks good. We can go on and do TFO */
363 /* Returns the number of bytes written to the TCP option-space */
365 tcp_tfo_write_cookie_rep(struct tcpcb
*tp
, unsigned int optlen
, u_char
*opt
)
367 u_char out
[CCAES_BLOCK_SIZE
];
371 if ((MAX_TCPOPTLEN
- optlen
) <
372 (TCPOLEN_FASTOPEN_REQ
+ TFO_COOKIE_LEN_DEFAULT
)) {
376 tcp_tfo_gen_cookie(tp
->t_inpcb
, out
, sizeof(out
));
380 *bp
++ = TCPOPT_FASTOPEN
;
381 *bp
++ = 2 + TFO_COOKIE_LEN_DEFAULT
;
382 memcpy(bp
, out
, TFO_COOKIE_LEN_DEFAULT
);
383 ret
+= 2 + TFO_COOKIE_LEN_DEFAULT
;
385 tp
->t_tfo_stats
|= TFO_S_COOKIE_SENT
;
386 tcpstat
.tcps_tfo_cookie_sent
++;
392 tcp_tfo_write_cookie(struct tcpcb
*tp
, unsigned int optlen
, int32_t len
,
396 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
401 if (TCPOLEN_FASTOPEN_REQ
> MAX_TCPOPTLEN
- optlen
) {
404 tfo_len
= (uint8_t)(MAX_TCPOPTLEN
- optlen
- TCPOLEN_FASTOPEN_REQ
);
406 if (so
->so_flags1
& SOF1_DATA_AUTHENTICATED
) {
407 /* If there is some data, let's track it */
409 tp
->t_tfo_stats
|= TFO_S_SYN_DATA_SENT
;
410 tcpstat
.tcps_tfo_syn_data_sent
++;
419 * The cookie will be copied in the appropriate place within the
420 * TCP-option space. That way we avoid the need for an intermediate
423 res
= tcp_cache_get_cookie(tp
, bp
+ TCPOLEN_FASTOPEN_REQ
, &tfo_len
);
425 *bp
++ = TCPOPT_FASTOPEN
;
426 *bp
++ = TCPOLEN_FASTOPEN_REQ
;
427 ret
+= TCPOLEN_FASTOPEN_REQ
;
429 tp
->t_tfo_flags
|= TFO_F_COOKIE_REQ
;
431 tp
->t_tfo_stats
|= TFO_S_COOKIE_REQ
;
432 tcpstat
.tcps_tfo_cookie_req
++;
434 *bp
++ = TCPOPT_FASTOPEN
;
435 *bp
++ = TCPOLEN_FASTOPEN_REQ
+ tfo_len
;
437 ret
+= TCPOLEN_FASTOPEN_REQ
+ tfo_len
;
439 tp
->t_tfo_flags
|= TFO_F_COOKIE_SENT
;
441 /* If there is some data, let's track it */
443 tp
->t_tfo_stats
|= TFO_S_SYN_DATA_SENT
;
444 tcpstat
.tcps_tfo_syn_data_sent
++;
452 tcp_send_ecn_flags_on_syn(struct tcpcb
*tp
)
454 return !(tp
->ecn_flags
& TE_SETUPSENT
);
458 tcp_set_ecn(struct tcpcb
*tp
, struct ifnet
*ifp
)
463 * Socket option has precedence
465 if (tp
->ecn_flags
& TE_ECN_MODE_ENABLE
) {
466 tp
->ecn_flags
|= TE_ENABLE_ECN
;
467 goto check_heuristic
;
470 if (tp
->ecn_flags
& TE_ECN_MODE_DISABLE
) {
471 tp
->ecn_flags
&= ~TE_ENABLE_ECN
;
475 * Per interface setting comes next
478 if (ifp
->if_eflags
& IFEF_ECN_ENABLE
) {
479 tp
->ecn_flags
|= TE_ENABLE_ECN
;
480 goto check_heuristic
;
483 if (ifp
->if_eflags
& IFEF_ECN_DISABLE
) {
484 tp
->ecn_flags
&= ~TE_ENABLE_ECN
;
489 * System wide settings come last
491 inbound
= (tp
->t_inpcb
->inp_socket
->so_head
!= NULL
);
492 if ((inbound
&& tcp_ecn_inbound
== 1) ||
493 (!inbound
&& tcp_ecn_outbound
== 1)) {
494 tp
->ecn_flags
|= TE_ENABLE_ECN
;
495 goto check_heuristic
;
497 tp
->ecn_flags
&= ~TE_ENABLE_ECN
;
503 if (!tcp_heuristic_do_ecn(tp
)) {
504 tp
->ecn_flags
&= ~TE_ENABLE_ECN
;
508 * If the interface setting, system-level setting and heuristics
509 * allow to enable ECN, randomly select 5% of connections to
512 if ((tp
->ecn_flags
& (TE_ECN_MODE_ENABLE
| TE_ECN_MODE_DISABLE
513 | TE_ENABLE_ECN
)) == TE_ENABLE_ECN
) {
515 * Use the random value in iss for randomizing
518 if ((tp
->iss
% 100) >= tcp_ecn_setup_percentage
) {
519 tp
->ecn_flags
&= ~TE_ENABLE_ECN
;
525 tcp_flight_size(struct tcpcb
*tp
)
529 VERIFY(tp
->sackhint
.sack_bytes_acked
>= 0);
530 VERIFY(tp
->sackhint
.sack_bytes_rexmit
>= 0);
533 * RFC6675, SetPipe (), SACK'd bytes are discounted. All the rest is still in-flight.
535 ret
= tp
->snd_nxt
- tp
->snd_una
- tp
->sackhint
.sack_bytes_acked
;
539 * This happens when the RTO-timer fires because snd_nxt gets artificially
540 * decreased. If we then receive some SACK-blogs, sack_bytes_acked is
550 * Tcp output routine: figure out what should be sent and send it.
558 * ip_output_list:ENOMEM
559 * ip_output_list:EADDRNOTAVAIL
560 * ip_output_list:ENETUNREACH
561 * ip_output_list:EHOSTUNREACH
562 * ip_output_list:EACCES
563 * ip_output_list:EMSGSIZE
564 * ip_output_list:ENOBUFS
565 * ip_output_list:??? [ignorable: mostly IPSEC/firewall/DLIL]
566 * ip6_output_list:EINVAL
567 * ip6_output_list:EOPNOTSUPP
568 * ip6_output_list:EHOSTUNREACH
569 * ip6_output_list:EADDRNOTAVAIL
570 * ip6_output_list:ENETUNREACH
571 * ip6_output_list:EMSGSIZE
572 * ip6_output_list:ENOBUFS
573 * ip6_output_list:??? [ignorable: mostly IPSEC/firewall/DLIL]
576 tcp_output(struct tcpcb
*tp
)
578 struct inpcb
*inp
= tp
->t_inpcb
;
579 struct socket
*so
= inp
->inp_socket
;
580 int32_t len
, recwin
, sendwin
, off
;
584 struct ip
*ip
= NULL
;
585 struct ip6_hdr
*ip6
= NULL
;
587 u_char opt
[TCP_MAXOLEN
];
588 unsigned int ipoptlen
, optlen
, hdrlen
;
589 int idle
, sendalot
, lost
= 0;
593 tcp_seq old_snd_nxt
= 0;
596 unsigned int ipsec_optlen
= 0;
599 struct mbuf
*packetlist
= NULL
;
600 struct mbuf
*tp_inp_options
= inp
->inp_depend4
.inp4_options
;
601 int isipv6
= inp
->inp_vflag
& INP_IPV6
;
602 int packchain_listadd
= 0;
603 int so_options
= so
->so_options
;
605 u_int32_t svc_flags
= 0, allocated_len
;
606 unsigned int sackoptlen
= 0;
608 boolean_t mptcp_acknow
;
610 boolean_t cell
= FALSE
;
611 boolean_t wifi
= FALSE
;
612 boolean_t wired
= FALSE
;
613 boolean_t sack_rescue_rxt
= FALSE
;
614 int sotc
= so
->so_traffic_class
;
615 boolean_t do_not_compress
= FALSE
;
616 boolean_t sack_rxmted
= FALSE
;
619 * Determine length of data that should be transmitted,
620 * and flags that will be used.
621 * If there is some data or critical controls (SYN, RST)
622 * to send, then transmit; otherwise, investigate further.
624 idle
= (tp
->t_flags
& TF_LASTIDLE
) || (tp
->snd_max
== tp
->snd_una
);
626 /* Since idle_time is signed integer, the following integer subtraction
627 * will take care of wrap around of tcp_now
629 idle_time
= tcp_now
- tp
->t_rcvtime
;
630 if (idle
&& idle_time
>= TCP_IDLETIMEOUT(tp
)) {
631 if (CC_ALGO(tp
)->after_idle
!= NULL
&&
632 (tp
->tcp_cc_index
!= TCP_CC_ALGO_CUBIC_INDEX
||
633 idle_time
>= TCP_CC_CWND_NONVALIDATED_PERIOD
)) {
634 CC_ALGO(tp
)->after_idle(tp
);
635 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_IDLE_TIMEOUT
);
639 * Do some other tasks that need to be done after
642 if (!SLIST_EMPTY(&tp
->t_rxt_segments
)) {
643 tcp_rxtseg_clean(tp
);
646 /* If stretch ack was auto-disabled, re-evaluate it */
647 tcp_cc_after_idle_stretchack(tp
);
648 tp
->t_forced_acks
= TCP_FORCED_ACKS_COUNT
;
650 tp
->t_flags
&= ~TF_LASTIDLE
;
652 if (tp
->t_flags
& TF_MORETOCOME
) {
653 tp
->t_flags
|= TF_LASTIDLE
;
658 if (tp
->t_mpflags
& TMPF_RESET
) {
659 tcp_check_timer_state(tp
);
661 * Once a RST has been sent for an MPTCP subflow,
662 * the subflow socket stays around until deleted.
663 * No packets such as FINs must be sent after RST.
671 mptcp_acknow
= FALSE
;
673 do_not_compress
= FALSE
;
675 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_START
, 0, 0, 0, 0, 0);
678 KERNEL_DEBUG(DBG_LAYER_BEG
,
679 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
680 (((inp
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
681 (inp
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
684 KERNEL_DEBUG(DBG_LAYER_BEG
,
685 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
686 (((inp
->inp_laddr
.s_addr
& 0xffff) << 16) |
687 (inp
->inp_faddr
.s_addr
& 0xffff)),
691 * If the route generation id changed, we need to check that our
692 * local (source) IP address is still valid. If it isn't either
693 * return error or silently do nothing (assuming the address will
694 * come back before the TCP connection times out).
696 rt
= inp
->inp_route
.ro_rt
;
697 if (rt
!= NULL
&& ROUTE_UNUSABLE(&tp
->t_inpcb
->inp_route
)) {
699 struct in_ifaddr
*ia
= NULL
;
700 struct in6_ifaddr
*ia6
= NULL
;
701 int found_srcaddr
= 0;
703 /* disable multipages at the socket */
704 somultipages(so
, FALSE
);
706 /* Disable TSO for the socket until we know more */
707 tp
->t_flags
&= ~TF_TSO
;
712 ia6
= ifa_foraddr6(&inp
->in6p_laddr
);
717 ia
= ifa_foraddr(inp
->inp_laddr
.s_addr
);
723 /* check that the source address is still valid */
724 if (found_srcaddr
== 0) {
726 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_NOSRCADDR
));
728 if (tp
->t_state
>= TCPS_CLOSE_WAIT
) {
729 tcp_drop(tp
, EADDRNOTAVAIL
);
730 return EADDRNOTAVAIL
;
734 * Set retransmit timer if it wasn't set,
735 * reset Persist timer and shift register as the
736 * advertised peer window may not be valid anymore
738 if (tp
->t_timer
[TCPT_REXMT
] == 0) {
739 tp
->t_timer
[TCPT_REXMT
] =
740 OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
741 if (tp
->t_timer
[TCPT_PERSIST
] != 0) {
742 tp
->t_timer
[TCPT_PERSIST
] = 0;
743 tp
->t_persist_stop
= 0;
744 TCP_RESET_REXMT_STATE(tp
);
748 if (tp
->t_pktlist_head
!= NULL
) {
749 m_freem_list(tp
->t_pktlist_head
);
751 TCP_PKTLIST_CLEAR(tp
);
753 /* drop connection if source address isn't available */
754 if (so
->so_flags
& SOF_NOADDRAVAIL
) {
755 tcp_drop(tp
, EADDRNOTAVAIL
);
756 return EADDRNOTAVAIL
;
758 tcp_check_timer_state(tp
);
759 return 0; /* silently ignore, keep data in socket: address may be back */
763 IFA_REMREF(&ia
->ia_ifa
);
767 IFA_REMREF(&ia6
->ia_ifa
);
771 * Address is still valid; check for multipages capability
772 * again in case the outgoing interface has changed.
775 if ((ifp
= rt
->rt_ifp
) != NULL
) {
776 somultipages(so
, (ifp
->if_hwassist
& IFNET_MULTIPAGES
));
777 tcp_set_tso(tp
, ifp
);
778 soif2kcl(so
, (ifp
->if_eflags
& IFEF_2KCL
));
779 tcp_set_ecn(tp
, ifp
);
781 if (rt
->rt_flags
& RTF_UP
) {
785 * See if we should do MTU discovery. Don't do it if:
786 * 1) it is disabled via the sysctl
787 * 2) the route isn't up
788 * 3) the MTU is locked (if it is, then discovery
792 if (!path_mtu_discovery
|| ((rt
!= NULL
) &&
793 (!(rt
->rt_flags
& RTF_UP
) ||
794 (rt
->rt_rmx
.rmx_locks
& RTV_MTU
)))) {
795 tp
->t_flags
&= ~TF_PMTUD
;
797 tp
->t_flags
|= TF_PMTUD
;
804 cell
= IFNET_IS_CELLULAR(rt
->rt_ifp
);
805 wifi
= (!cell
&& IFNET_IS_WIFI(rt
->rt_ifp
));
806 wired
= (!wifi
&& IFNET_IS_WIRED(rt
->rt_ifp
));
810 * If we've recently taken a timeout, snd_max will be greater than
811 * snd_nxt. There may be SACK information that allows us to avoid
812 * resending already delivered data. Adjust snd_nxt accordingly.
814 if (SACK_ENABLED(tp
) && SEQ_LT(tp
->snd_nxt
, tp
->snd_max
)) {
818 off
= tp
->snd_nxt
- tp
->snd_una
;
819 sendwin
= min(tp
->snd_wnd
, tp
->snd_cwnd
);
821 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0) {
822 sendwin
= min(sendwin
, slowlink_wsize
);
825 flags
= tcp_outflags
[tp
->t_state
];
827 * Send any SACK-generated retransmissions. If we're explicitly
828 * trying to send out new data (when sendalot is 1), bypass this
829 * function. If we retransmit in fast recovery mode, decrement
830 * snd_cwnd, since we're replacing a (future) new transmission
831 * with a retransmission now, and we previously incremented
832 * snd_cwnd in tcp_input().
835 * Still in sack recovery , reset rxmit flag to zero.
841 if (SACK_ENABLED(tp
) && IN_FASTRECOVERY(tp
) &&
842 (p
= tcp_sack_output(tp
, &sack_bytes_rxmt
))) {
845 if (tcp_do_better_lr
) {
846 cwin
= min(tp
->snd_wnd
, tp
->snd_cwnd
) - tcp_flight_size(tp
);
847 if (cwin
<= 0 && sack_rxmted
== FALSE
) {
848 /* Allow to clock out at least on per period */
854 cwin
= min(tp
->snd_wnd
, tp
->snd_cwnd
) - sack_bytes_rxmt
;
859 /* Do not retransmit SACK segments beyond snd_recover */
860 if (SEQ_GT(p
->end
, tp
->snd_recover
)) {
862 * (At least) part of sack hole extends beyond
863 * snd_recover. Check to see if we can rexmit data
866 if (SEQ_GEQ(p
->rxmit
, tp
->snd_recover
)) {
868 * Can't rexmit any more data for this hole.
869 * That data will be rexmitted in the next
870 * sack recovery episode, when snd_recover
871 * moves past p->rxmit.
874 goto after_sack_rexmit
;
876 /* Can rexmit part of the current hole */
877 len
= ((int32_t)min(cwin
,
878 tp
->snd_recover
- p
->rxmit
));
881 len
= ((int32_t)min(cwin
, p
->end
- p
->rxmit
));
884 off
= p
->rxmit
- tp
->snd_una
;
887 /* Everything sent after snd_nxt will allow us to account for fast-retransmit of the retransmitted segment */
888 tp
->send_highest_sack
= tp
->snd_nxt
;
889 tp
->t_new_dupacks
= 0;
890 tcpstat
.tcps_sack_rexmits
++;
891 tcpstat
.tcps_sack_rexmit_bytes
+=
892 min(len
, tp
->t_maxseg
);
899 * Get standard flags, and add SYN or FIN if requested by 'hidden'
902 if (tp
->t_flags
& TF_NEEDFIN
) {
907 * If in persist timeout with window of 0, send 1 byte.
908 * Otherwise, if window is small but nonzero
909 * and timer expired, we will send what we can
910 * and go to transmit state.
912 if (tp
->t_flagsext
& TF_FORCE
) {
915 * If we still have some data to send, then
916 * clear the FIN bit. Usually this would
917 * happen below when it realizes that we
918 * aren't sending all the data. However,
919 * if we have exactly 1 byte of unsent data,
920 * then it won't clear the FIN bit below,
921 * and if we are in persist state, we wind
922 * up sending the packet without recording
923 * that we sent the FIN bit.
925 * We can't just blindly clear the FIN bit,
926 * because if we don't have any more data
927 * to send then the probe will be the FIN
930 if (off
< so
->so_snd
.sb_cc
) {
935 tp
->t_timer
[TCPT_PERSIST
] = 0;
936 tp
->t_persist_stop
= 0;
937 TCP_RESET_REXMT_STATE(tp
);
942 * If snd_nxt == snd_max and we have transmitted a FIN, the
943 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
944 * a negative length. This can also occur when TCP opens up
945 * its congestion window while receiving additional duplicate
946 * acks after fast-retransmit because TCP will reset snd_nxt
947 * to snd_max after the fast-retransmit.
949 * In the normal retransmit-FIN-only case, however, snd_nxt will
950 * be set to snd_una, the offset will be 0, and the length may
953 * If sack_rxmit is true we are retransmitting from the scoreboard
954 * in which case len is already set.
956 if (sack_rxmit
== 0) {
957 if (sack_bytes_rxmt
== 0) {
958 len
= min(so
->so_snd
.sb_cc
, sendwin
) - off
;
962 if (tcp_do_better_lr
) {
963 cwin
= tp
->snd_cwnd
- tcp_flight_size(tp
);
965 cwin
= tp
->snd_cwnd
-
966 (tp
->snd_nxt
- tp
->sack_newdata
) -
973 * We are inside of a SACK recovery episode and are
974 * sending new data, having retransmitted all the
975 * data possible in the scoreboard.
977 len
= min(so
->so_snd
.sb_cc
, tp
->snd_wnd
) - off
;
979 * Don't remove this (len > 0) check !
980 * We explicitly check for len > 0 here (although it
981 * isn't really necessary), to work around a gcc
982 * optimization issue - to force gcc to compute
983 * len above. Without this check, the computation
984 * of len is bungled by the optimizer.
987 len
= imin(len
, cwin
);
992 * At this point SACK recovery can not send any
993 * data from scoreboard or any new data. Check
994 * if we can do a rescue retransmit towards the
995 * tail end of recovery window.
997 if (len
== 0 && cwin
> 0 &&
998 SEQ_LT(tp
->snd_fack
, tp
->snd_recover
) &&
999 !(tp
->t_flagsext
& TF_RESCUE_RXT
)) {
1000 len
= min((tp
->snd_recover
- tp
->snd_fack
),
1002 len
= imin(len
, cwin
);
1003 old_snd_nxt
= tp
->snd_nxt
;
1004 sack_rescue_rxt
= TRUE
;
1005 tp
->snd_nxt
= tp
->snd_recover
- len
;
1007 * If FIN has been sent, snd_max
1008 * must have been advanced to cover it.
1010 if ((tp
->t_flags
& TF_SENTFIN
) &&
1011 tp
->snd_max
== tp
->snd_recover
) {
1015 off
= tp
->snd_nxt
- tp
->snd_una
;
1017 tp
->t_flagsext
|= TF_RESCUE_RXT
;
1023 * Lop off SYN bit if it has already been sent. However, if this
1024 * is SYN-SENT state and if segment contains data and if we don't
1025 * know that foreign host supports TAO, suppress sending segment.
1027 if ((flags
& TH_SYN
) && SEQ_GT(tp
->snd_nxt
, tp
->snd_una
)) {
1028 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& tfo_enabled(tp
) && tp
->snd_nxt
== tp
->snd_una
+ 1) {
1029 /* We are sending the SYN again! */
1033 if (tp
->t_state
!= TCPS_SYN_RECEIVED
|| tfo_enabled(tp
)) {
1039 if (len
> 0 && tp
->t_state
== TCPS_SYN_SENT
) {
1040 while (inp
->inp_sndinprog_cnt
== 0 &&
1041 tp
->t_pktlist_head
!= NULL
) {
1042 packetlist
= tp
->t_pktlist_head
;
1043 packchain_listadd
= tp
->t_lastchain
;
1045 TCP_PKTLIST_CLEAR(tp
);
1047 error
= tcp_ip_output(so
, tp
, packetlist
,
1048 packchain_listadd
, tp_inp_options
,
1049 (so_options
& SO_DONTROUTE
),
1050 (sack_rxmit
|| (sack_bytes_rxmt
!= 0)),
1055 * tcp was closed while we were in ip,
1058 if (inp
->inp_sndinprog_cnt
== 0 &&
1059 (tp
->t_flags
& TF_CLOSING
)) {
1060 tp
->t_flags
&= ~TF_CLOSING
;
1061 (void) tcp_close(tp
);
1063 tcp_check_timer_state(tp
);
1065 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
,
1073 * Be careful not to send data and/or FIN on SYN segments.
1074 * This measure is needed to prevent interoperability problems
1075 * with not fully conformant TCP implementations.
1077 * In case of TFO, we handle the setting of the len in
1078 * tcp_tfo_check. In case TFO is not enabled, never ever send
1081 if ((flags
& TH_SYN
) && !tfo_enabled(tp
)) {
1087 * Don't send a RST with data.
1089 if (flags
& TH_RST
) {
1093 if ((flags
& TH_SYN
) && tp
->t_state
<= TCPS_SYN_SENT
&& tfo_enabled(tp
)) {
1094 len
= tcp_tfo_check(tp
, len
);
1098 * The check here used to be (len < 0). Some times len is zero
1099 * when the congestion window is closed and we need to check
1100 * if persist timer has to be set in that case. But don't set
1101 * persist until connection is established.
1103 if (len
<= 0 && !(flags
& TH_SYN
)) {
1105 * If FIN has been sent but not acked,
1106 * but we haven't been called to retransmit,
1107 * len will be < 0. Otherwise, window shrank
1108 * after we sent into it. If window shrank to 0,
1109 * cancel pending retransmit, pull snd_nxt back
1110 * to (closed) window, and set the persist timer
1111 * if it isn't already going. If the window didn't
1112 * close completely, just wait for an ACK.
1116 tp
->t_timer
[TCPT_REXMT
] = 0;
1117 tp
->t_timer
[TCPT_PTO
] = 0;
1118 TCP_RESET_REXMT_STATE(tp
);
1119 tp
->snd_nxt
= tp
->snd_una
;
1121 if (tp
->t_timer
[TCPT_PERSIST
] == 0) {
1128 * Automatic sizing of send socket buffer. Increase the send
1129 * socket buffer size if all of the following criteria are met
1130 * 1. the receiver has enough buffer space for this data
1131 * 2. send buffer is filled to 7/8th with data (so we actually
1132 * have data to make use of it);
1133 * 3. our send window (slow start and congestion controlled) is
1134 * larger than sent but unacknowledged data in send buffer.
1136 if (!INP_WAIT_FOR_IF_FEEDBACK(inp
) && !IN_FASTRECOVERY(tp
) &&
1137 (so
->so_snd
.sb_flags
& (SB_AUTOSIZE
| SB_TRIM
)) == SB_AUTOSIZE
&&
1138 tcp_cansbgrow(&so
->so_snd
)) {
1139 if ((tp
->snd_wnd
/ 4 * 5) >= so
->so_snd
.sb_hiwat
&&
1140 so
->so_snd
.sb_cc
>= (so
->so_snd
.sb_hiwat
/ 8 * 7) &&
1141 sendwin
>= (so
->so_snd
.sb_cc
- (tp
->snd_nxt
- tp
->snd_una
))) {
1142 if (sbreserve(&so
->so_snd
,
1143 min(so
->so_snd
.sb_hiwat
+ tcp_autosndbuf_inc
,
1144 tcp_autosndbuf_max
)) == 1) {
1145 so
->so_snd
.sb_idealsize
= so
->so_snd
.sb_hiwat
;
1151 * Truncate to the maximum segment length or enable TCP Segmentation
1152 * Offloading (if supported by hardware) and ensure that FIN is removed
1153 * if the length no longer contains the last data byte.
1155 * TSO may only be used if we are in a pure bulk sending state.
1156 * The presence of TCP-MD5, SACK retransmits, SACK advertizements,
1157 * filters and IP options, as well as disabling hardware checksum
1158 * offload prevent using TSO. With TSO the TCP header is the same
1159 * (except for the sequence number) for all generated packets. This
1160 * makes it impossible to transmit any options which vary per generated
1161 * segment or packet.
1163 * The length of TSO bursts is limited to TCP_MAXWIN. That limit and
1164 * removal of FIN (if not already catched here) are handled later after
1165 * the exact length of the TCP options are known.
1169 * Pre-calculate here as we save another lookup into the darknesses
1170 * of IPsec that way and can actually decide if TSO is ok.
1172 if (ipsec_bypass
== 0) {
1173 ipsec_optlen
= (unsigned int)ipsec_hdrsiz_tcp(tp
);
1176 if (len
> tp
->t_maxseg
) {
1177 if ((tp
->t_flags
& TF_TSO
) && tcp_do_tso
&& hwcksum_tx
&&
1178 ip_use_randomid
&& kipf_count
== 0 &&
1179 dlil_filter_disable_tso_count
== 0 &&
1180 tp
->rcv_numsacks
== 0 && sack_rxmit
== 0 &&
1181 sack_bytes_rxmt
== 0 &&
1182 inp
->inp_options
== NULL
&&
1183 inp
->in6p_options
== NULL
1185 && ipsec_optlen
== 0
1199 /* Send one segment or less as a tail loss probe */
1200 if (tp
->t_flagsext
& TF_SENT_TLPROBE
) {
1201 len
= min(len
, tp
->t_maxseg
);
1207 if (so
->so_flags
& SOF_MP_SUBFLOW
&& off
< 0) {
1208 os_log_error(mptcp_log_handle
, "%s - %lx: offset is negative! len %d off %d\n",
1209 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(tp
->t_mpsub
->mpts_mpte
),
1213 if ((so
->so_flags
& SOF_MP_SUBFLOW
) &&
1214 !(tp
->t_mpflags
& TMPF_TCP_FALLBACK
)) {
1216 if (tp
->t_state
>= TCPS_ESTABLISHED
&&
1217 (tp
->t_mpflags
& TMPF_SND_MPPRIO
||
1218 tp
->t_mpflags
& TMPF_SND_REM_ADDR
||
1219 tp
->t_mpflags
& TMPF_SND_MPFAIL
||
1220 tp
->t_mpflags
& TMPF_SND_KEYS
||
1221 tp
->t_mpflags
& TMPF_SND_JACK
)) {
1227 * On a new subflow, don't try to send again, because
1228 * we are still waiting for the fourth ack.
1230 if (!(tp
->t_mpflags
& TMPF_PREESTABLISHED
)) {
1233 mptcp_acknow
= TRUE
;
1235 mptcp_acknow
= FALSE
;
1238 * The contiguous bytes in the subflow socket buffer can be
1239 * discontiguous at the MPTCP level. Since only one DSS
1240 * option can be sent in one packet, reduce length to match
1241 * the contiguous MPTCP level. Set sendalot to send remainder.
1243 if (len
> 0 && off
>= 0) {
1244 newlen
= mptcp_adj_sendlen(so
, off
);
1249 if (len
<= tp
->t_maxseg
) {
1257 if (SEQ_LT(p
->rxmit
+ len
, tp
->snd_una
+ so
->so_snd
.sb_cc
)) {
1261 if (SEQ_LT(tp
->snd_nxt
+ len
, tp
->snd_una
+ so
->so_snd
.sb_cc
)) {
1266 * Compare available window to amount of window
1267 * known to peer (as advertised window less
1268 * next expected input). If the difference is at least two
1269 * max size segments, or at least 25% of the maximum possible
1270 * window, then want to send a window update to peer.
1272 recwin
= tcp_sbspace(tp
);
1274 if (!(so
->so_flags
& SOF_MP_SUBFLOW
)) {
1275 if (recwin
< (int32_t)(so
->so_rcv
.sb_hiwat
/ 4) &&
1276 recwin
< (int)tp
->t_maxseg
) {
1280 struct mptcb
*mp_tp
= tptomptp(tp
);
1281 struct socket
*mp_so
= mptetoso(mp_tp
->mpt_mpte
);
1283 if (recwin
< (int32_t)(mp_so
->so_rcv
.sb_hiwat
/ 4) &&
1284 recwin
< (int)tp
->t_maxseg
) {
1290 if (tcp_recv_bg
== 1 || IS_TCP_RECV_BG(so
)) {
1291 if (recwin
> 0 && tcp_recv_throttle(tp
)) {
1292 uint32_t min_iaj_win
= tcp_min_iaj_win
* tp
->t_maxseg
;
1293 uint32_t bg_rwintop
= tp
->rcv_adv
;
1294 if (SEQ_LT(bg_rwintop
, tp
->rcv_nxt
+ min_iaj_win
)) {
1295 bg_rwintop
= tp
->rcv_nxt
+ min_iaj_win
;
1297 recwin
= imin((int32_t)(bg_rwintop
- tp
->rcv_nxt
),
1304 #endif /* TRAFFIC_MGT */
1306 if (recwin
> (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
)) {
1307 recwin
= (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
);
1310 if (!(so
->so_flags
& SOF_MP_SUBFLOW
)) {
1311 if (recwin
< (int32_t)(tp
->rcv_adv
- tp
->rcv_nxt
)) {
1312 recwin
= (int32_t)(tp
->rcv_adv
- tp
->rcv_nxt
);
1315 struct mptcb
*mp_tp
= tptomptp(tp
);
1316 int64_t recwin_announced
= (int64_t)(mp_tp
->mpt_rcvadv
- mp_tp
->mpt_rcvnxt
);
1318 /* Don't remove what we announced at the MPTCP-layer */
1319 VERIFY(recwin_announced
< INT32_MAX
&& recwin_announced
> INT32_MIN
);
1320 if (recwin
< (int32_t)recwin_announced
) {
1321 recwin
= (int32_t)recwin_announced
;
1326 * Sender silly window avoidance. We transmit under the following
1327 * conditions when len is non-zero:
1329 * - we've timed out (e.g. persist timer)
1330 * - we need to retransmit
1331 * - We have a full segment (or more with TSO)
1332 * - This is the last buffer in a write()/send() and we are
1333 * either idle or running NODELAY
1334 * - we have more then 1/2 the maximum send window's worth of
1335 * data (receiver may be limited the window size)
1338 if (tp
->t_flagsext
& TF_FORCE
) {
1341 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
)) {
1349 * If this here is the first segment after SYN/ACK and TFO
1350 * is being used, then we always send it, regardless of Nagle,...
1352 if (tp
->t_state
== TCPS_SYN_RECEIVED
&&
1354 (tp
->t_tfo_flags
& TFO_F_COOKIE_VALID
) &&
1355 tp
->snd_nxt
== tp
->iss
+ 1) {
1360 * Send new data on the connection only if it is
1361 * not flow controlled
1363 if (!INP_WAIT_FOR_IF_FEEDBACK(inp
) ||
1364 tp
->t_state
!= TCPS_ESTABLISHED
) {
1365 if (len
>= tp
->t_maxseg
) {
1369 if (!(tp
->t_flags
& TF_MORETOCOME
) &&
1370 (idle
|| tp
->t_flags
& TF_NODELAY
||
1371 (tp
->t_flags
& TF_MAXSEGSNT
) ||
1372 ALLOW_LIMITED_TRANSMIT(tp
)) &&
1373 (tp
->t_flags
& TF_NOPUSH
) == 0 &&
1374 (len
+ off
>= so
->so_snd
.sb_cc
||
1376 * MPTCP needs to respect the DSS-mappings. So, it
1377 * may be sending data that *could* have been
1378 * coalesced, but cannot because of
1379 * mptcp_adj_sendlen().
1381 so
->so_flags
& SOF_MP_SUBFLOW
)) {
1384 if (len
>= tp
->max_sndwnd
/ 2 && tp
->max_sndwnd
> 0) {
1388 tcpstat
.tcps_fcholdpacket
++;
1394 * "adv" is the amount we can increase the window,
1395 * taking into account that we are limited by
1396 * TCP_MAXWIN << tp->rcv_scale.
1398 int32_t adv
, oldwin
= 0;
1399 adv
= imin(recwin
, (int)TCP_MAXWIN
<< tp
->rcv_scale
) -
1400 (tp
->rcv_adv
- tp
->rcv_nxt
);
1402 if (SEQ_GT(tp
->rcv_adv
, tp
->rcv_nxt
)) {
1403 oldwin
= tp
->rcv_adv
- tp
->rcv_nxt
;
1406 if (tcp_ack_strategy
== TCP_ACK_STRATEGY_LEGACY
) {
1407 if (adv
>= (int32_t) (2 * tp
->t_maxseg
)) {
1409 * Update only if the resulting scaled value of
1410 * the window changed, or if there is a change in
1411 * the sequence since the last ack. This avoids
1412 * what appears as dupe ACKS (see rdar://5640997)
1414 * If streaming is detected avoid sending too many
1415 * window updates. We will depend on the delack
1416 * timer to send a window update when needed.
1418 * If there is more data to read, don't send an ACK.
1419 * Otherwise we will end up sending many ACKs if the
1420 * application is doing micro-reads.
1422 if (!(tp
->t_flags
& TF_STRETCHACK
) &&
1423 (tp
->last_ack_sent
!= tp
->rcv_nxt
||
1424 ((oldwin
+ adv
) >> tp
->rcv_scale
) >
1425 (oldwin
>> tp
->rcv_scale
))) {
1430 if (adv
>= (int32_t) (2 * tp
->t_maxseg
)) {
1432 * ACK every second full-sized segment, if the
1433 * ACK is advancing or the window becomes bigger
1435 if (so
->so_rcv
.sb_cc
< so
->so_rcv
.sb_lowat
&&
1436 (tp
->last_ack_sent
!= tp
->rcv_nxt
||
1437 ((oldwin
+ adv
) >> tp
->rcv_scale
) >
1438 (oldwin
>> tp
->rcv_scale
))) {
1441 } else if (tp
->t_flags
& TF_DELACK
) {
1443 * If we delayed the ACK and the window
1444 * is not advancing by a lot (< 2MSS), ACK
1445 * immediately if the last incoming packet had
1446 * the push flag set and we emptied the buffer.
1448 * This takes care of a sender doing small
1449 * repeated writes with Nagle enabled.
1451 if (so
->so_rcv
.sb_cc
== 0 &&
1452 tp
->last_ack_sent
!= tp
->rcv_nxt
&&
1453 (tp
->t_flagsext
& TF_LAST_IS_PSH
)) {
1458 if (4 * adv
>= (int32_t) so
->so_rcv
.sb_hiwat
) {
1463 * Make sure that the delayed ack timer is set if
1464 * we delayed sending a window update because of
1465 * streaming detection.
1467 if (tcp_ack_strategy
== TCP_ACK_STRATEGY_LEGACY
&&
1468 (tp
->t_flags
& TF_STRETCHACK
) &&
1469 !(tp
->t_flags
& TF_DELACK
)) {
1470 tp
->t_flags
|= TF_DELACK
;
1471 tp
->t_timer
[TCPT_DELACK
] =
1472 OFFSET_FROM_START(tp
, tcp_delack
);
1477 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW
1478 * is also a catch-all for the retransmit timer timeout case.
1480 if (tp
->t_flags
& TF_ACKNOW
) {
1481 if (tp
->t_forced_acks
> 0) {
1482 tp
->t_forced_acks
--;
1486 if ((flags
& TH_RST
) || (flags
& TH_SYN
)) {
1489 if (SEQ_GT(tp
->snd_up
, tp
->snd_una
)) {
1498 * If our state indicates that FIN should be sent
1499 * and we have not yet done so, then we need to send.
1501 if ((flags
& TH_FIN
) &&
1502 (!(tp
->t_flags
& TF_SENTFIN
) || tp
->snd_nxt
== tp
->snd_una
)) {
1506 * In SACK, it is possible for tcp_output to fail to send a segment
1507 * after the retransmission timer has been turned off. Make sure
1508 * that the retransmission timer is set.
1510 if (SACK_ENABLED(tp
) && (tp
->t_state
>= TCPS_ESTABLISHED
) &&
1511 SEQ_GT(tp
->snd_max
, tp
->snd_una
) &&
1512 tp
->t_timer
[TCPT_REXMT
] == 0 &&
1513 tp
->t_timer
[TCPT_PERSIST
] == 0) {
1514 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
,
1519 * TCP window updates are not reliable, rather a polling protocol
1520 * using ``persist'' packets is used to insure receipt of window
1521 * updates. The three ``states'' for the output side are:
1522 * idle not doing retransmits or persists
1523 * persisting to move a small or zero window
1524 * (re)transmitting and thereby not persisting
1526 * tp->t_timer[TCPT_PERSIST]
1527 * is set when we are in persist state.
1529 * is set when we are called to send a persist packet.
1530 * tp->t_timer[TCPT_REXMT]
1531 * is set when we are retransmitting
1532 * The output side is idle when both timers are zero.
1534 * If send window is too small, there is data to transmit, and no
1535 * retransmit or persist is pending, then go to persist state.
1536 * If nothing happens soon, send when timer expires:
1537 * if window is nonzero, transmit what we can,
1538 * otherwise force out a byte.
1540 if (so
->so_snd
.sb_cc
&& tp
->t_timer
[TCPT_REXMT
] == 0 &&
1541 tp
->t_timer
[TCPT_PERSIST
] == 0) {
1542 TCP_RESET_REXMT_STATE(tp
);
1547 * If there is no reason to send a segment, just return.
1548 * but if there is some packets left in the packet list, send them now.
1550 while (inp
->inp_sndinprog_cnt
== 0 &&
1551 tp
->t_pktlist_head
!= NULL
) {
1552 packetlist
= tp
->t_pktlist_head
;
1553 packchain_listadd
= tp
->t_lastchain
;
1555 TCP_PKTLIST_CLEAR(tp
);
1557 error
= tcp_ip_output(so
, tp
, packetlist
,
1559 tp_inp_options
, (so_options
& SO_DONTROUTE
),
1560 (sack_rxmit
|| (sack_bytes_rxmt
!= 0)), isipv6
);
1562 /* tcp was closed while we were in ip; resume close */
1563 if (inp
->inp_sndinprog_cnt
== 0 &&
1564 (tp
->t_flags
& TF_CLOSING
)) {
1565 tp
->t_flags
&= ~TF_CLOSING
;
1566 (void) tcp_close(tp
);
1568 tcp_check_timer_state(tp
);
1570 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
1575 * Set TF_MAXSEGSNT flag if the segment size is greater than
1576 * the max segment size.
1579 do_not_compress
= TRUE
;
1581 if (len
>= tp
->t_maxseg
) {
1582 tp
->t_flags
|= TF_MAXSEGSNT
;
1584 tp
->t_flags
&= ~TF_MAXSEGSNT
;
1588 * Before ESTABLISHED, force sending of initial options
1589 * unless TCP set not to do any options.
1590 * NOTE: we assume that the IP/TCP header plus TCP options
1591 * always fit in a single mbuf, leaving room for a maximum
1593 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
1597 hdrlen
= sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
);
1599 hdrlen
= sizeof(struct tcpiphdr
);
1601 if (flags
& TH_SYN
) {
1602 tp
->snd_nxt
= tp
->iss
;
1603 if ((tp
->t_flags
& TF_NOOPT
) == 0) {
1606 opt
[0] = TCPOPT_MAXSEG
;
1607 opt
[1] = TCPOLEN_MAXSEG
;
1608 mss
= htons((u_short
) tcp_mssopt(tp
));
1609 (void)memcpy(opt
+ 2, &mss
, sizeof(mss
));
1610 optlen
= TCPOLEN_MAXSEG
;
1612 if ((tp
->t_flags
& TF_REQ_SCALE
) &&
1613 ((flags
& TH_ACK
) == 0 ||
1614 (tp
->t_flags
& TF_RCVD_SCALE
))) {
1615 *((u_int32_t
*)(void *)(opt
+ optlen
)) = htonl(
1617 TCPOPT_WINDOW
<< 16 |
1618 TCPOLEN_WINDOW
<< 8 |
1619 tp
->request_r_scale
);
1623 if (mptcp_enable
&& (so
->so_flags
& SOF_MP_SUBFLOW
)) {
1624 optlen
= mptcp_setup_syn_opts(so
, opt
, optlen
);
1631 * Send a timestamp and echo-reply if this is a SYN and our side
1632 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
1633 * and our peer have sent timestamps in our SYN's.
1635 if ((tp
->t_flags
& (TF_REQ_TSTMP
| TF_NOOPT
)) == TF_REQ_TSTMP
&&
1636 (flags
& TH_RST
) == 0 &&
1637 ((flags
& TH_ACK
) == 0 ||
1638 (tp
->t_flags
& TF_RCVD_TSTMP
))) {
1639 u_int32_t
*lp
= (u_int32_t
*)(void *)(opt
+ optlen
);
1641 /* Form timestamp option as shown in appendix A of RFC 1323. */
1642 *lp
++ = htonl(TCPOPT_TSTAMP_HDR
);
1643 *lp
++ = htonl(tcp_now
+ tp
->t_ts_offset
);
1644 *lp
= htonl(tp
->ts_recent
);
1645 optlen
+= TCPOLEN_TSTAMP_APPA
;
1648 if (SACK_ENABLED(tp
) && ((tp
->t_flags
& TF_NOOPT
) == 0)) {
1650 * Tack on the SACK permitted option *last*.
1651 * And do padding of options after tacking this on.
1652 * This is because of MSS, TS, WinScale and Signatures are
1653 * all present, we have just 2 bytes left for the SACK
1654 * permitted option, which is just enough.
1657 * If this is the first SYN of connection (not a SYN
1658 * ACK), include SACK permitted option. If this is a
1659 * SYN ACK, include SACK permitted option if peer has
1660 * already done so. This is only for active connect,
1661 * since the syncache takes care of the passive connect.
1663 if ((flags
& TH_SYN
) &&
1664 (!(flags
& TH_ACK
) || (tp
->t_flags
& TF_SACK_PERMIT
))) {
1666 bp
= (u_char
*)opt
+ optlen
;
1668 *bp
++ = TCPOPT_SACK_PERMITTED
;
1669 *bp
++ = TCPOLEN_SACK_PERMITTED
;
1670 optlen
+= TCPOLEN_SACK_PERMITTED
;
1674 if (so
->so_flags
& SOF_MP_SUBFLOW
) {
1676 * Its important to piggyback acks with data as ack only packets
1677 * may get lost and data packets that don't send Data ACKs
1678 * still advance the subflow level ACK and therefore make it
1679 * hard for the remote end to recover in low cwnd situations.
1682 tp
->t_mpflags
|= (TMPF_SEND_DSN
|
1685 tp
->t_mpflags
|= TMPF_MPTCP_ACKNOW
;
1687 optlen
= mptcp_setup_opts(tp
, off
, &opt
[0], optlen
, flags
,
1688 len
, &mptcp_acknow
, &do_not_compress
);
1689 tp
->t_mpflags
&= ~TMPF_SEND_DSN
;
1693 if (tfo_enabled(tp
) && !(tp
->t_flags
& TF_NOOPT
) &&
1694 (flags
& (TH_SYN
| TH_ACK
)) == TH_SYN
) {
1695 optlen
+= tcp_tfo_write_cookie(tp
, optlen
, len
, opt
);
1698 if (tfo_enabled(tp
) &&
1699 (flags
& (TH_SYN
| TH_ACK
)) == (TH_SYN
| TH_ACK
) &&
1700 (tp
->t_tfo_flags
& TFO_F_OFFER_COOKIE
)) {
1701 optlen
+= tcp_tfo_write_cookie_rep(tp
, optlen
, opt
);
1704 if (SACK_ENABLED(tp
) && ((tp
->t_flags
& TF_NOOPT
) == 0)) {
1706 * Send SACKs if necessary. This should be the last
1707 * option processed. Only as many SACKs are sent as
1708 * are permitted by the maximum options size.
1710 * In general, SACK blocks consume 8*n+2 bytes.
1711 * So a full size SACK blocks option is 34 bytes
1712 * (to generate 4 SACK blocks). At a minimum,
1713 * we need 10 bytes (to generate 1 SACK block).
1714 * If TCP Timestamps (12 bytes) and TCP Signatures
1715 * (18 bytes) are both present, we'll just have
1716 * 10 bytes for SACK options 40 - (12 + 18).
1718 if (TCPS_HAVEESTABLISHED(tp
->t_state
) &&
1719 (tp
->t_flags
& TF_SACK_PERMIT
) &&
1720 (tp
->rcv_numsacks
> 0 || TCP_SEND_DSACK_OPT(tp
)) &&
1721 MAX_TCPOPTLEN
- optlen
- 2 >= TCPOLEN_SACK
) {
1723 u_char
*bp
= (u_char
*)opt
+ optlen
;
1726 nsack
= (MAX_TCPOPTLEN
- optlen
- 2) / TCPOLEN_SACK
;
1727 nsack
= min(nsack
, (tp
->rcv_numsacks
+
1728 (TCP_SEND_DSACK_OPT(tp
) ? 1 : 0)));
1729 sackoptlen
= (2 + nsack
* TCPOLEN_SACK
);
1730 VERIFY(sackoptlen
< UINT8_MAX
);
1733 * First we need to pad options so that the
1734 * SACK blocks can start at a 4-byte boundary
1735 * (sack option and length are at a 2 byte offset).
1737 padlen
= (MAX_TCPOPTLEN
- optlen
- sackoptlen
) % 4;
1739 while (padlen
-- > 0) {
1743 tcpstat
.tcps_sack_send_blocks
++;
1744 *bp
++ = TCPOPT_SACK
;
1745 *bp
++ = (uint8_t)sackoptlen
;
1746 lp
= (u_int32_t
*)(void *)bp
;
1749 * First block of SACK option should represent
1750 * DSACK. Prefer to send SACK information if there
1751 * is space for only one SACK block. This will
1752 * allow for faster recovery.
1754 if (TCP_SEND_DSACK_OPT(tp
) && nsack
> 0 &&
1755 (tp
->rcv_numsacks
== 0 || nsack
> 1)) {
1756 *lp
++ = htonl(tp
->t_dsack_lseq
);
1757 *lp
++ = htonl(tp
->t_dsack_rseq
);
1758 tcpstat
.tcps_dsack_sent
++;
1762 VERIFY(nsack
== 0 || tp
->rcv_numsacks
>= nsack
);
1763 for (i
= 0; i
< nsack
; i
++) {
1764 struct sackblk sack
= tp
->sackblks
[i
];
1765 *lp
++ = htonl(sack
.start
);
1766 *lp
++ = htonl(sack
.end
);
1768 optlen
+= sackoptlen
;
1772 /* Pad TCP options to a 4 byte boundary */
1773 if (optlen
< MAX_TCPOPTLEN
&& (optlen
% sizeof(u_int32_t
))) {
1774 int pad
= sizeof(u_int32_t
) - (optlen
% sizeof(u_int32_t
));
1775 u_char
*bp
= (u_char
*)opt
+ optlen
;
1785 * RFC 3168 states that:
1786 * - If you ever sent an ECN-setup SYN/SYN-ACK you must be prepared
1787 * to handle the TCP ECE flag, even if you also later send a
1788 * non-ECN-setup SYN/SYN-ACK.
1789 * - If you ever send a non-ECN-setup SYN/SYN-ACK, you must not set
1792 * It is not clear how the ECE flag would ever be set if you never
1793 * set the IP ECT flag on outbound packets. All the same, we use
1794 * the TE_SETUPSENT to indicate that we have committed to handling
1795 * the TCP ECE flag correctly. We use the TE_SENDIPECT to indicate
1796 * whether or not we should set the IP ECT flag on outbound packet
1798 * For a SYN-ACK, send an ECN setup SYN-ACK
1800 if ((flags
& (TH_SYN
| TH_ACK
)) == (TH_SYN
| TH_ACK
) &&
1801 (tp
->ecn_flags
& TE_ENABLE_ECN
)) {
1802 if (tp
->ecn_flags
& TE_SETUPRECEIVED
) {
1803 if (tcp_send_ecn_flags_on_syn(tp
)) {
1805 * Setting TH_ECE makes this an ECN-setup
1811 * Record that we sent the ECN-setup and
1812 * default to setting IP ECT.
1814 tp
->ecn_flags
|= (TE_SETUPSENT
| TE_SENDIPECT
);
1815 tcpstat
.tcps_ecn_server_setup
++;
1816 tcpstat
.tcps_ecn_server_success
++;
1819 * We sent an ECN-setup SYN-ACK but it was
1820 * dropped. Fallback to non-ECN-setup
1821 * SYN-ACK and clear flag to indicate that
1822 * we should not send data with IP ECT set
1824 * Pretend we didn't receive an
1827 * We already incremented the counter
1828 * assuming that the ECN setup will
1829 * succeed. Decrementing here
1830 * tcps_ecn_server_success to correct it.
1832 if (tp
->ecn_flags
& TE_SETUPSENT
) {
1833 tcpstat
.tcps_ecn_lost_synack
++;
1834 tcpstat
.tcps_ecn_server_success
--;
1835 tp
->ecn_flags
|= TE_LOST_SYNACK
;
1839 ~(TE_SETUPRECEIVED
| TE_SENDIPECT
|
1843 } else if ((flags
& (TH_SYN
| TH_ACK
)) == TH_SYN
&&
1844 (tp
->ecn_flags
& TE_ENABLE_ECN
)) {
1845 if (tcp_send_ecn_flags_on_syn(tp
)) {
1847 * Setting TH_ECE and TH_CWR makes this an
1850 flags
|= (TH_ECE
| TH_CWR
);
1851 tcpstat
.tcps_ecn_client_setup
++;
1852 tp
->ecn_flags
|= TE_CLIENT_SETUP
;
1855 * Record that we sent the ECN-setup and default to
1858 tp
->ecn_flags
|= (TE_SETUPSENT
| TE_SENDIPECT
);
1861 * We sent an ECN-setup SYN but it was dropped.
1862 * Fall back to non-ECN and clear flag indicating
1863 * we should send data with IP ECT set.
1865 if (tp
->ecn_flags
& TE_SETUPSENT
) {
1866 tcpstat
.tcps_ecn_lost_syn
++;
1867 tp
->ecn_flags
|= TE_LOST_SYN
;
1869 tp
->ecn_flags
&= ~TE_SENDIPECT
;
1874 * Check if we should set the TCP CWR flag.
1875 * CWR flag is sent when we reduced the congestion window because
1876 * we received a TCP ECE or we performed a fast retransmit. We
1877 * never set the CWR flag on retransmitted packets. We only set
1878 * the CWR flag on data packets. Pure acks don't have this set.
1880 if ((tp
->ecn_flags
& TE_SENDCWR
) != 0 && len
!= 0 &&
1881 !SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) && !sack_rxmit
) {
1883 tp
->ecn_flags
&= ~TE_SENDCWR
;
1887 * Check if we should set the TCP ECE flag.
1889 if ((tp
->ecn_flags
& TE_SENDECE
) != 0 && len
== 0) {
1891 tcpstat
.tcps_ecn_sent_ece
++;
1897 /* Reset DSACK sequence numbers */
1898 tp
->t_dsack_lseq
= 0;
1899 tp
->t_dsack_rseq
= 0;
1902 ipoptlen
= ip6_optlen(inp
);
1904 if (tp_inp_options
) {
1905 ipoptlen
= tp_inp_options
->m_len
-
1906 offsetof(struct ipoption
, ipopt_list
);
1912 ipoptlen
+= ipsec_optlen
;
1916 * Adjust data length if insertion of options will
1917 * bump the packet length beyond the t_maxopd length.
1918 * Clear the FIN bit because we cut off the tail of
1921 * When doing TSO limit a burst to TCP_MAXWIN minus the
1922 * IP, TCP and Options length to keep ip->ip_len from
1923 * overflowing. Prevent the last segment from being
1924 * fractional thus making them all equal sized and set
1925 * the flag to continue sending. TSO is disabled when
1926 * IP options or IPSEC are present.
1928 if (len
+ optlen
+ ipoptlen
> tp
->t_maxopd
) {
1930 * If there is still more to send,
1931 * don't close the connection.
1937 tso_maxlen
= tp
->tso_max_segment_size
?
1938 tp
->tso_max_segment_size
: TCP_MAXWIN
;
1940 if (len
> tso_maxlen
- hdrlen
- optlen
) {
1941 len
= tso_maxlen
- hdrlen
- optlen
;
1943 } else if (tp
->t_flags
& TF_NEEDFIN
) {
1947 if (len
% (tp
->t_maxopd
- optlen
) != 0) {
1948 len
= len
- (len
% (tp
->t_maxopd
- optlen
));
1952 len
= tp
->t_maxopd
- optlen
- ipoptlen
;
1957 if (max_linkhdr
+ hdrlen
> MCLBYTES
) {
1958 panic("tcphdr too big");
1961 /* Check if there is enough data in the send socket
1962 * buffer to start measuring bandwidth
1964 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
1965 (tp
->t_bwmeas
!= NULL
) &&
1966 (tp
->t_flagsext
& TF_BWMEAS_INPROGRESS
) == 0) {
1967 tp
->t_bwmeas
->bw_size
= min(min(
1968 (so
->so_snd
.sb_cc
- (tp
->snd_max
- tp
->snd_una
)),
1969 tp
->snd_cwnd
), tp
->snd_wnd
);
1970 if (tp
->t_bwmeas
->bw_minsize
> 0 &&
1971 tp
->t_bwmeas
->bw_size
< tp
->t_bwmeas
->bw_minsize
) {
1972 tp
->t_bwmeas
->bw_size
= 0;
1974 if (tp
->t_bwmeas
->bw_maxsize
> 0) {
1975 tp
->t_bwmeas
->bw_size
= min(tp
->t_bwmeas
->bw_size
,
1976 tp
->t_bwmeas
->bw_maxsize
);
1978 if (tp
->t_bwmeas
->bw_size
> 0) {
1979 tp
->t_flagsext
|= TF_BWMEAS_INPROGRESS
;
1980 tp
->t_bwmeas
->bw_start
= tp
->snd_max
;
1981 tp
->t_bwmeas
->bw_ts
= tcp_now
;
1985 VERIFY(inp
->inp_flowhash
!= 0);
1987 * Grab a header mbuf, attaching a copy of data to
1988 * be transmitted, and initialize the header from
1989 * the template for sends on this connection.
1992 /* Remember what the last head-of-line packet-size was */
1993 if (tp
->t_pmtud_lastseg_size
== 0 && tp
->snd_nxt
== tp
->snd_una
) {
1994 ASSERT(len
+ optlen
+ ipoptlen
<= IP_MAXPACKET
);
1995 tp
->t_pmtud_lastseg_size
= (uint16_t)(len
+ optlen
+ ipoptlen
);
1997 if ((tp
->t_flagsext
& TF_FORCE
) && len
== 1) {
1998 tcpstat
.tcps_sndprobe
++;
1999 } else if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) || sack_rxmit
) {
2000 tcpstat
.tcps_sndrexmitpack
++;
2001 tcpstat
.tcps_sndrexmitbyte
+= len
;
2002 if (nstat_collect
) {
2003 nstat_route_tx(inp
->inp_route
.ro_rt
, 1,
2004 len
, NSTAT_TX_FLAG_RETRANSMIT
);
2005 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
2007 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
2009 tp
->t_stat
.txretransmitbytes
+= len
;
2010 tp
->t_stat
.rxmitpkts
++;
2013 tcpstat
.tcps_sndpack
++;
2014 tcpstat
.tcps_sndbyte
+= len
;
2016 if (nstat_collect
) {
2017 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
2019 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
2022 inp_decr_sndbytes_unsent(so
, len
);
2024 inp_set_activity_bitmap(inp
);
2026 if (tp
->t_mpflags
& TMPF_MPTCP_TRUE
) {
2027 tcpstat
.tcps_mp_sndpacks
++;
2028 tcpstat
.tcps_mp_sndbytes
+= len
;
2032 * try to use the new interface that allocates all
2033 * the necessary mbuf hdrs under 1 mbuf lock and
2034 * avoids rescanning the socket mbuf list if
2035 * certain conditions are met. This routine can't
2036 * be used in the following cases...
2037 * 1) the protocol headers exceed the capacity of
2038 * of a single mbuf header's data area (no cluster attached)
2039 * 2) the length of the data being transmitted plus
2040 * the protocol headers fits into a single mbuf header's
2041 * data area (no cluster attached)
2045 /* minimum length we are going to allocate */
2046 allocated_len
= MHLEN
;
2047 if (MHLEN
< hdrlen
+ max_linkhdr
) {
2048 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
2053 MCLGET(m
, M_DONTWAIT
);
2054 if ((m
->m_flags
& M_EXT
) == 0) {
2059 m
->m_data
+= max_linkhdr
;
2061 allocated_len
= MCLBYTES
;
2063 if (len
<= allocated_len
- hdrlen
- max_linkhdr
) {
2065 VERIFY(allocated_len
<= MHLEN
);
2066 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
2071 m
->m_data
+= max_linkhdr
;
2074 /* makes sure we still have data left to be sent at this point */
2075 if (so
->so_snd
.sb_mb
== NULL
|| off
< 0) {
2079 error
= 0; /* should we return an error? */
2082 m_copydata(so
->so_snd
.sb_mb
, off
, (int) len
,
2083 mtod(m
, caddr_t
) + hdrlen
);
2088 * Retain packet header metadata at the socket
2089 * buffer if this is is an MPTCP subflow,
2090 * otherwise move it.
2092 copymode
= M_COPYM_MOVE_HDR
;
2094 if (so
->so_flags
& SOF_MP_SUBFLOW
) {
2095 copymode
= M_COPYM_NOOP_HDR
;
2099 m
->m_next
= m_copym_mode(so
->so_snd
.sb_mb
,
2100 off
, (int)len
, M_DONTWAIT
, copymode
);
2101 if (m
->m_next
== NULL
) {
2108 * make sure we still have data left
2109 * to be sent at this point
2111 if (so
->so_snd
.sb_mb
== NULL
) {
2112 error
= 0; /* should we return an error? */
2117 * m_copym_with_hdrs will always return the
2118 * last mbuf pointer and the offset into it that
2119 * it acted on to fullfill the current request,
2120 * whether a valid 'hint' was passed in or not.
2122 if ((m
= m_copym_with_hdrs(so
->so_snd
.sb_mb
,
2123 off
, len
, M_DONTWAIT
, NULL
, NULL
,
2124 copymode
)) == NULL
) {
2128 m
->m_data
+= max_linkhdr
;
2133 * If we're sending everything we've got, set PUSH.
2134 * (This will keep happy those implementations which only
2135 * give data to the user when a buffer fills or
2138 * On SYN-segments we should not add the PUSH-flag.
2140 if (off
+ len
== so
->so_snd
.sb_cc
&& !(flags
& TH_SYN
)) {
2144 if (tp
->t_flags
& TF_ACKNOW
) {
2145 tcpstat
.tcps_sndacks
++;
2146 } else if (flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
2147 tcpstat
.tcps_sndctrl
++;
2148 } else if (SEQ_GT(tp
->snd_up
, tp
->snd_una
)) {
2149 tcpstat
.tcps_sndurg
++;
2151 tcpstat
.tcps_sndwinup
++;
2154 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
2159 if (MHLEN
< (hdrlen
+ max_linkhdr
)) {
2160 MCLGET(m
, M_DONTWAIT
);
2161 if ((m
->m_flags
& M_EXT
) == 0) {
2167 m
->m_data
+= max_linkhdr
;
2170 m
->m_pkthdr
.rcvif
= 0;
2172 /* Any flag other than pure-ACK: Do not compress! */
2173 if (flags
& ~(TH_ACK
)) {
2174 do_not_compress
= TRUE
;
2177 if (tp
->rcv_scale
== 0) {
2178 do_not_compress
= TRUE
;
2181 if (do_not_compress
|| (tcp_do_ack_compression
== 1 && !cell
) || __improbable(!tcp_do_ack_compression
)) {
2182 m
->m_pkthdr
.comp_gencnt
= 0;
2184 if (TSTMP_LT(tp
->t_comp_lastinc
+ tcp_ack_compression_rate
, tcp_now
)) {
2185 tp
->t_comp_gencnt
++;
2186 /* 0 means no compression, thus jump this */
2187 if (tp
->t_comp_gencnt
<= TCP_ACK_COMPRESSION_DUMMY
) {
2188 tp
->t_comp_gencnt
= TCP_ACK_COMPRESSION_DUMMY
+ 1;
2190 tp
->t_comp_lastinc
= tcp_now
;
2192 m
->m_pkthdr
.comp_gencnt
= tp
->t_comp_gencnt
;
2196 ip6
= mtod(m
, struct ip6_hdr
*);
2197 th
= (struct tcphdr
*)(void *)(ip6
+ 1);
2198 tcp_fillheaders(tp
, ip6
, th
);
2199 if ((tp
->ecn_flags
& TE_SENDIPECT
) != 0 && len
&&
2200 !SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) && !sack_rxmit
) {
2201 ip6
->ip6_flow
|= htonl(IPTOS_ECN_ECT0
<< 20);
2203 svc_flags
|= PKT_SCF_IPV6
;
2205 m_pftag(m
)->pftag_hdr
= (void *)ip6
;
2206 m_pftag(m
)->pftag_flags
|= PF_TAG_HDR_INET6
;
2209 ip
= mtod(m
, struct ip
*);
2210 th
= (struct tcphdr
*)(void *)(ip
+ 1);
2211 /* this picks up the pseudo header (w/o the length) */
2212 tcp_fillheaders(tp
, ip
, th
);
2213 if ((tp
->ecn_flags
& TE_SENDIPECT
) != 0 && len
&&
2214 !SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) &&
2215 !sack_rxmit
&& !(flags
& TH_SYN
)) {
2216 ip
->ip_tos
|= IPTOS_ECN_ECT0
;
2219 m_pftag(m
)->pftag_hdr
= (void *)ip
;
2220 m_pftag(m
)->pftag_flags
|= PF_TAG_HDR_INET
;
2225 * Fill in fields, remembering maximum advertised
2226 * window for use in delaying messages about window sizes.
2227 * If resending a FIN, be sure not to use a new sequence number.
2229 if ((flags
& TH_FIN
) && (tp
->t_flags
& TF_SENTFIN
) &&
2230 tp
->snd_nxt
== tp
->snd_max
) {
2234 * If we are doing retransmissions, then snd_nxt will
2235 * not reflect the first unsent octet. For ACK only
2236 * packets, we do not want the sequence number of the
2237 * retransmitted packet, we want the sequence number
2238 * of the next unsent octet. So, if there is no data
2239 * (and no SYN or FIN), use snd_max instead of snd_nxt
2240 * when filling in ti_seq. But if we are in persist
2241 * state, snd_max might reflect one byte beyond the
2242 * right edge of the window, so use snd_nxt in that
2243 * case, since we know we aren't doing a retransmission.
2244 * (retransmit and persist are mutually exclusive...)
2246 * Note the state of this retransmit segment to detect spurious
2249 if (sack_rxmit
== 0) {
2250 if (len
|| (flags
& (TH_SYN
| TH_FIN
)) ||
2251 tp
->t_timer
[TCPT_PERSIST
]) {
2252 th
->th_seq
= htonl(tp
->snd_nxt
);
2254 m
->m_pkthdr
.tx_start_seq
= tp
->snd_nxt
;
2255 m
->m_pkthdr
.pkt_flags
|= PKTF_START_SEQ
;
2257 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
)) {
2258 if (SACK_ENABLED(tp
) && len
> 1) {
2259 tcp_rxtseg_insert(tp
, tp
->snd_nxt
,
2260 (tp
->snd_nxt
+ len
- 1));
2263 m
->m_pkthdr
.pkt_flags
|=
2268 th
->th_seq
= htonl(tp
->snd_max
);
2271 th
->th_seq
= htonl(p
->rxmit
);
2273 m
->m_pkthdr
.pkt_flags
|=
2274 (PKTF_TCP_REXMT
| PKTF_START_SEQ
);
2275 m
->m_pkthdr
.tx_start_seq
= p
->rxmit
;
2277 tcp_rxtseg_insert(tp
, p
->rxmit
, (p
->rxmit
+ len
- 1));
2279 tp
->sackhint
.sack_bytes_rexmit
+= len
;
2281 th
->th_ack
= htonl(tp
->rcv_nxt
);
2282 tp
->last_ack_sent
= tp
->rcv_nxt
;
2284 bcopy(opt
, th
+ 1, optlen
);
2285 th
->th_off
= (sizeof(struct tcphdr
) + optlen
) >> 2;
2287 th
->th_flags
= flags
;
2288 th
->th_win
= htons((u_short
) (recwin
>> tp
->rcv_scale
));
2289 tp
->t_last_recwin
= recwin
;
2290 if (!(so
->so_flags
& SOF_MP_SUBFLOW
)) {
2291 if (recwin
> 0 && SEQ_LT(tp
->rcv_adv
, tp
->rcv_nxt
+ recwin
)) {
2292 tp
->rcv_adv
= tp
->rcv_nxt
+ recwin
;
2295 struct mptcb
*mp_tp
= tptomptp(tp
);
2297 tp
->rcv_adv
= tp
->rcv_nxt
+ recwin
;
2300 if (recwin
> 0 && MPTCP_SEQ_LT(mp_tp
->mpt_rcvadv
, mp_tp
->mpt_rcvnxt
+ recwin
)) {
2301 mp_tp
->mpt_rcvadv
= mp_tp
->mpt_rcvnxt
+ recwin
;
2306 * Adjust the RXWIN0SENT flag - indicate that we have advertised
2307 * a 0 window. This may cause the remote transmitter to stall. This
2308 * flag tells soreceive() to disable delayed acknowledgements when
2309 * draining the buffer. This can occur if the receiver is attempting
2310 * to read more data then can be buffered prior to transmitting on
2313 if (th
->th_win
== 0) {
2314 tp
->t_flags
|= TF_RXWIN0SENT
;
2316 tp
->t_flags
&= ~TF_RXWIN0SENT
;
2319 if (SEQ_GT(tp
->snd_up
, tp
->snd_nxt
)) {
2320 th
->th_urp
= htons((u_short
)(tp
->snd_up
- tp
->snd_nxt
));
2321 th
->th_flags
|= TH_URG
;
2324 * If no urgent pointer to send, then we pull
2325 * the urgent pointer to the left edge of the send window
2326 * so that it doesn't drift into the send window on sequence
2327 * number wraparound.
2329 tp
->snd_up
= tp
->snd_una
; /* drag it along */
2333 * Put TCP length in extended header, and then
2334 * checksum extended header and data.
2336 m
->m_pkthdr
.len
= hdrlen
+ len
; /* in6_cksum() need this */
2339 * If this is potentially the last packet on the stream, then mark
2340 * it in order to enable some optimizations in the underlying
2343 if (tp
->t_state
!= TCPS_ESTABLISHED
&&
2344 (tp
->t_state
== TCPS_CLOSING
|| tp
->t_state
== TCPS_TIME_WAIT
2345 || tp
->t_state
== TCPS_LAST_ACK
|| (th
->th_flags
& TH_RST
))) {
2346 m
->m_pkthdr
.pkt_flags
|= PKTF_LAST_PKT
;
2351 * ip6_plen is not need to be filled now, and will be filled
2354 m
->m_pkthdr
.csum_flags
= CSUM_TCPIPV6
;
2355 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
2357 th
->th_sum
= in_addword(th
->th_sum
,
2358 htons((u_short
)(optlen
+ len
)));
2361 m
->m_pkthdr
.csum_flags
= CSUM_TCP
;
2362 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
2364 th
->th_sum
= in_addword(th
->th_sum
,
2365 htons((u_short
)(optlen
+ len
)));
2370 * Enable TSO and specify the size of the segments.
2371 * The TCP pseudo header checksum is always provided.
2375 m
->m_pkthdr
.csum_flags
|= CSUM_TSO_IPV6
;
2377 m
->m_pkthdr
.csum_flags
|= CSUM_TSO_IPV4
;
2380 m
->m_pkthdr
.tso_segsz
= tp
->t_maxopd
- optlen
;
2382 m
->m_pkthdr
.tso_segsz
= 0;
2386 * In transmit state, time the transmission and arrange for
2387 * the retransmit. In persist state, just set snd_max.
2389 if (!(tp
->t_flagsext
& TF_FORCE
)
2390 || tp
->t_timer
[TCPT_PERSIST
] == 0) {
2391 tcp_seq startseq
= tp
->snd_nxt
;
2394 * Advance snd_nxt over sequence space of this segment.
2396 if (flags
& (TH_SYN
| TH_FIN
)) {
2397 if (flags
& TH_SYN
) {
2400 if ((flags
& TH_FIN
) &&
2401 !(tp
->t_flags
& TF_SENTFIN
)) {
2403 tp
->t_flags
|= TF_SENTFIN
;
2409 if (sack_rescue_rxt
== TRUE
) {
2410 tp
->snd_nxt
= old_snd_nxt
;
2411 sack_rescue_rxt
= FALSE
;
2412 tcpstat
.tcps_pto_in_recovery
++;
2416 if (SEQ_GT(tp
->snd_nxt
, tp
->snd_max
)) {
2417 tp
->snd_max
= tp
->snd_nxt
;
2418 tp
->t_sndtime
= tcp_now
;
2420 * Time this transmission if not a retransmission and
2421 * not currently timing anything.
2423 if (tp
->t_rtttime
== 0) {
2424 tp
->t_rtttime
= tcp_now
;
2425 tp
->t_rtseq
= startseq
;
2426 tcpstat
.tcps_segstimed
++;
2428 /* update variables related to pipe ack */
2429 tp
->t_pipeack_lastuna
= tp
->snd_una
;
2434 * Set retransmit timer if not currently set,
2435 * and not doing an ack or a keep-alive probe.
2438 if (tp
->t_timer
[TCPT_REXMT
] == 0 &&
2439 ((sack_rxmit
&& tp
->snd_nxt
!= tp
->snd_max
) ||
2440 tp
->snd_nxt
!= tp
->snd_una
|| (flags
& TH_FIN
))) {
2441 if (tp
->t_timer
[TCPT_PERSIST
]) {
2442 tp
->t_timer
[TCPT_PERSIST
] = 0;
2443 tp
->t_persist_stop
= 0;
2444 TCP_RESET_REXMT_STATE(tp
);
2446 tp
->t_timer
[TCPT_REXMT
] =
2447 OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
2451 * Set tail loss probe timeout if new data is being
2452 * transmitted. This will be supported only when
2453 * SACK option is enabled on a connection.
2455 * Every time new data is sent PTO will get reset.
2457 if (tcp_enable_tlp
&& len
!= 0 && tp
->t_state
== TCPS_ESTABLISHED
&&
2458 SACK_ENABLED(tp
) && !IN_FASTRECOVERY(tp
) &&
2459 tp
->snd_nxt
== tp
->snd_max
&&
2460 SEQ_GT(tp
->snd_nxt
, tp
->snd_una
) &&
2461 tp
->t_rxtshift
== 0 &&
2462 (tp
->t_flagsext
& (TF_SENT_TLPROBE
| TF_PKTS_REORDERED
)) == 0) {
2465 if (tcp_do_better_lr
) {
2466 srtt
= tp
->t_srtt
>> TCP_RTT_SHIFT
;
2468 if ((tp
->snd_max
- tp
->snd_una
) <= tp
->t_maxseg
) {
2475 * Using SRTT alone to set PTO can cause spurious
2476 * retransmissions on wireless networks where there
2477 * is a lot of variance in RTT. Taking variance
2478 * into account will avoid this.
2480 srtt
= tp
->t_srtt
>> TCP_RTT_SHIFT
;
2481 pto
= ((TCP_REXMTVAL(tp
)) * 3) >> 1;
2482 pto
= max(2 * srtt
, pto
);
2483 if ((tp
->snd_max
- tp
->snd_una
) == tp
->t_maxseg
) {
2485 (((3 * pto
) >> 2) + tcp_delack
* 2));
2491 /* if RTO is less than PTO, choose RTO instead */
2492 if (tp
->t_rxtcur
< pto
) {
2496 tp
->t_timer
[TCPT_PTO
] = OFFSET_FROM_START(tp
, pto
);
2500 * Persist case, update snd_max but since we are in
2501 * persist mode (no window) we do not update snd_nxt.
2504 if (flags
& TH_SYN
) {
2507 if ((flags
& TH_FIN
) &&
2508 !(tp
->t_flags
& TF_SENTFIN
)) {
2510 tp
->t_flags
|= TF_SENTFIN
;
2512 if (SEQ_GT(tp
->snd_nxt
+ xlen
, tp
->snd_max
)) {
2513 tp
->snd_max
= tp
->snd_nxt
+ len
;
2514 tp
->t_sndtime
= tcp_now
;
2522 if (so_options
& SO_DEBUG
) {
2523 tcp_trace(TA_OUTPUT
, tp
->t_state
, tp
, mtod(m
, void *), th
, 0);
2528 * Fill in IP length and desired time to live and
2529 * send to IP level. There should be a better way
2530 * to handle ttl and tos; we could keep them in
2531 * the template, but need a way to checksum without them.
2534 * m->m_pkthdr.len should have been set before cksum calcuration,
2535 * because in6_cksum() need it.
2539 * we separately set hoplimit for every segment, since the
2540 * user might want to change the value via setsockopt.
2541 * Also, desired default hop limit might be changed via
2542 * Neighbor Discovery.
2544 ip6
->ip6_hlim
= in6_selecthlim(inp
, inp
->in6p_route
.ro_rt
?
2545 inp
->in6p_route
.ro_rt
->rt_ifp
: NULL
);
2547 /* TODO: IPv6 IP6TOS_ECT bit on */
2548 KERNEL_DEBUG(DBG_LAYER_BEG
,
2549 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
2550 (((inp
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
2551 (inp
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
2554 ASSERT(m
->m_pkthdr
.len
<= IP_MAXPACKET
);
2555 ip
->ip_len
= (u_short
)m
->m_pkthdr
.len
;
2556 ip
->ip_ttl
= inp
->inp_ip_ttl
; /* XXX */
2557 ip
->ip_tos
|= (inp
->inp_ip_tos
& ~IPTOS_ECN_MASK
);/* XXX */
2558 KERNEL_DEBUG(DBG_LAYER_BEG
,
2559 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
2560 (((inp
->inp_laddr
.s_addr
& 0xffff) << 16) |
2561 (inp
->inp_faddr
.s_addr
& 0xffff)), 0, 0, 0);
2565 * See if we should do MTU discovery.
2566 * Look at the flag updated on the following criterias:
2567 * 1) Path MTU discovery is authorized by the sysctl
2568 * 2) The route isn't set yet (unlikely but could happen)
2569 * 3) The route is up
2570 * 4) the MTU is not locked (if it is, then discovery has been
2571 * disabled for that route)
2574 if (path_mtu_discovery
&& (tp
->t_flags
& TF_PMTUD
)) {
2575 ip
->ip_off
|= IP_DF
;
2581 necp_kernel_policy_id policy_id
;
2582 necp_kernel_policy_id skip_policy_id
;
2583 u_int32_t route_rule_id
;
2584 u_int32_t pass_flags
;
2585 if (!necp_socket_is_allowed_to_send_recv(inp
, NULL
, 0, &policy_id
, &route_rule_id
, &skip_policy_id
, &pass_flags
)) {
2586 TCP_LOG_DROP_NECP(isipv6
? (void *)ip6
: (void *)ip
, th
, tp
, true);
2588 error
= EHOSTUNREACH
;
2591 necp_mark_packet_from_socket(m
, inp
, policy_id
, route_rule_id
, skip_policy_id
, pass_flags
);
2593 if (net_qos_policy_restricted
!= 0) {
2594 necp_socket_update_qos_marking(inp
, inp
->inp_route
.ro_rt
, route_rule_id
);
2600 if (inp
->inp_sp
!= NULL
) {
2601 ipsec_setsocket(m
, so
);
2606 * The socket is kept locked while sending out packets in ip_output, even if packet chaining is not active.
2611 * Embed the flow hash in pkt hdr and mark the packet as
2612 * capable of flow controlling
2614 m
->m_pkthdr
.pkt_flowsrc
= FLOWSRC_INPCB
;
2615 m
->m_pkthdr
.pkt_flowid
= inp
->inp_flowhash
;
2616 m
->m_pkthdr
.pkt_flags
|= (PKTF_FLOW_ID
| PKTF_FLOW_LOCALSRC
| PKTF_FLOW_ADV
);
2617 m
->m_pkthdr
.pkt_proto
= IPPROTO_TCP
;
2618 m
->m_pkthdr
.tx_tcp_pid
= so
->last_pid
;
2619 if (so
->so_flags
& SOF_DELEGATED
) {
2620 m
->m_pkthdr
.tx_tcp_e_pid
= so
->e_pid
;
2622 m
->m_pkthdr
.tx_tcp_e_pid
= 0;
2625 m
->m_nextpkt
= NULL
;
2627 if (inp
->inp_last_outifp
!= NULL
&&
2628 !(inp
->inp_last_outifp
->if_flags
& IFF_LOOPBACK
)) {
2629 /* Hint to prioritize this packet if
2630 * 1. if the packet has no data
2631 * 2. the interface supports transmit-start model and did
2632 * not disable ACK prioritization.
2633 * 3. Only ACK flag is set.
2634 * 4. there is no outstanding data on this connection.
2636 if (len
== 0 && (inp
->inp_last_outifp
->if_eflags
& (IFEF_TXSTART
| IFEF_NOACKPRI
)) == IFEF_TXSTART
) {
2637 if (th
->th_flags
== TH_ACK
&&
2638 tp
->snd_una
== tp
->snd_max
&&
2639 tp
->t_timer
[TCPT_REXMT
] == 0) {
2640 svc_flags
|= PKT_SCF_TCP_ACK
;
2642 if (th
->th_flags
& TH_SYN
) {
2643 svc_flags
|= PKT_SCF_TCP_SYN
;
2646 set_packet_service_class(m
, so
, sotc
, svc_flags
);
2649 * Optimization for loopback just set the mbuf
2652 (void) m_set_service_class(m
, so_tc2msc(sotc
));
2655 TCP_LOG_TH_FLAGS(isipv6
? (void *)ip6
: (void *)ip
, th
, tp
, true,
2656 inp
->inp_last_outifp
!= NULL
? inp
->inp_last_outifp
:
2659 tp
->t_pktlist_sentlen
+= len
;
2663 DTRACE_TCP5(send
, struct mbuf
*, m
, struct inpcb
*, inp
,
2664 struct ip6
*, ip6
, struct tcpcb
*, tp
, struct tcphdr
*,
2667 DTRACE_TCP5(send
, struct mbuf
*, m
, struct inpcb
*, inp
,
2668 struct ip
*, ip
, struct tcpcb
*, tp
, struct tcphdr
*, th
);
2671 if (tp
->t_pktlist_head
!= NULL
) {
2672 tp
->t_pktlist_tail
->m_nextpkt
= m
;
2673 tp
->t_pktlist_tail
= m
;
2675 packchain_newlist
++;
2676 tp
->t_pktlist_head
= tp
->t_pktlist_tail
= m
;
2679 if (sendalot
== 0 || (tp
->t_state
!= TCPS_ESTABLISHED
) ||
2680 (tp
->snd_cwnd
<= (tp
->snd_wnd
/ 8)) ||
2681 (tp
->t_flags
& TF_ACKNOW
) ||
2682 (tp
->t_flagsext
& TF_FORCE
) ||
2683 tp
->t_lastchain
>= tcp_packet_chaining
) {
2685 while (inp
->inp_sndinprog_cnt
== 0 &&
2686 tp
->t_pktlist_head
!= NULL
) {
2687 packetlist
= tp
->t_pktlist_head
;
2688 packchain_listadd
= tp
->t_lastchain
;
2690 lost
= tp
->t_pktlist_sentlen
;
2691 TCP_PKTLIST_CLEAR(tp
);
2693 error
= tcp_ip_output(so
, tp
, packetlist
,
2694 packchain_listadd
, tp_inp_options
,
2695 (so_options
& SO_DONTROUTE
),
2696 (sack_rxmit
|| (sack_bytes_rxmt
!= 0)), isipv6
);
2699 * Take into account the rest of unsent
2700 * packets in the packet list for this tcp
2701 * into "lost", since we're about to free
2702 * the whole list below.
2704 lost
+= tp
->t_pktlist_sentlen
;
2710 /* tcp was closed while we were in ip; resume close */
2711 if (inp
->inp_sndinprog_cnt
== 0 &&
2712 (tp
->t_flags
& TF_CLOSING
)) {
2713 tp
->t_flags
&= ~TF_CLOSING
;
2714 (void) tcp_close(tp
);
2720 tcpstat
.tcps_sndtotal
++;
2726 * Assume that the packets were lost, so back out the
2727 * sequence number advance, if any. Note that the "lost"
2728 * variable represents the amount of user data sent during
2729 * the recent call to ip_output_list() plus the amount of
2730 * user data in the packet list for this tcp at the moment.
2732 if (!(tp
->t_flagsext
& TF_FORCE
)
2733 || tp
->t_timer
[TCPT_PERSIST
] == 0) {
2735 * No need to check for TH_FIN here because
2736 * the TF_SENTFIN flag handles that case.
2738 if ((flags
& TH_SYN
) == 0) {
2740 if (SEQ_GT((p
->rxmit
- lost
),
2744 if (SEQ_LT(p
->rxmit
, p
->start
)) {
2745 p
->rxmit
= p
->start
;
2748 lost
= p
->rxmit
- tp
->snd_una
;
2749 p
->rxmit
= tp
->snd_una
;
2751 if (SEQ_LT(p
->rxmit
, p
->start
)) {
2752 p
->rxmit
= p
->start
;
2755 tp
->sackhint
.sack_bytes_rexmit
-= lost
;
2756 if (tp
->sackhint
.sack_bytes_rexmit
< 0) {
2757 tp
->sackhint
.sack_bytes_rexmit
= 0;
2760 if (SEQ_GT((tp
->snd_nxt
- lost
),
2762 tp
->snd_nxt
-= lost
;
2764 tp
->snd_nxt
= tp
->snd_una
;
2770 if (tp
->t_pktlist_head
!= NULL
) {
2771 m_freem_list(tp
->t_pktlist_head
);
2773 TCP_PKTLIST_CLEAR(tp
);
2775 if (error
== ENOBUFS
) {
2777 * Set retransmit timer if not currently set
2778 * when we failed to send a segment that can be
2779 * retransmitted (i.e. not pure ack or rst)
2781 if (tp
->t_timer
[TCPT_REXMT
] == 0 &&
2782 tp
->t_timer
[TCPT_PERSIST
] == 0 &&
2783 (len
!= 0 || (flags
& (TH_SYN
| TH_FIN
)) != 0 ||
2784 so
->so_snd
.sb_cc
> 0)) {
2785 tp
->t_timer
[TCPT_REXMT
] =
2786 OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
2788 tp
->snd_cwnd
= tp
->t_maxseg
;
2789 tp
->t_bytes_acked
= 0;
2790 tcp_check_timer_state(tp
);
2791 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
2793 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_OUTPUT_ERROR
);
2796 if (error
== EMSGSIZE
) {
2798 * ip_output() will have already fixed the route
2799 * for us. tcp_mtudisc() will, as its last action,
2800 * initiate retransmission, so it is important to
2803 * If TSO was active we either got an interface
2804 * without TSO capabilits or TSO was turned off.
2805 * Disable it for this connection as too and
2806 * immediatly retry with MSS sized segments generated
2810 tp
->t_flags
&= ~TF_TSO
;
2813 tcp_mtudisc(inp
, 0);
2814 tcp_check_timer_state(tp
);
2816 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
2820 * Unless this is due to interface restriction policy,
2821 * treat EHOSTUNREACH/ENETDOWN/EADDRNOTAVAIL as a soft error.
2823 if ((error
== EHOSTUNREACH
|| error
== ENETDOWN
|| error
== EADDRNOTAVAIL
) &&
2824 TCPS_HAVERCVDSYN(tp
->t_state
) &&
2825 !inp_restricted_send(inp
, inp
->inp_last_outifp
)) {
2826 tp
->t_softerror
= error
;
2829 tcp_check_timer_state(tp
);
2830 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
2834 tcpstat
.tcps_sndtotal
++;
2836 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
2841 tcp_check_timer_state(tp
);
2847 tcp_ip_output(struct socket
*so
, struct tcpcb
*tp
, struct mbuf
*pkt
,
2848 int cnt
, struct mbuf
*opt
, int flags
, int sack_in_progress
, boolean_t isipv6
)
2852 boolean_t unlocked
= FALSE
;
2853 boolean_t ifdenied
= FALSE
;
2854 struct inpcb
*inp
= tp
->t_inpcb
;
2855 struct ip_out_args ipoa
;
2857 struct ifnet
*outif
= NULL
;
2858 bool check_qos_marking_again
= (so
->so_flags1
& SOF1_QOSMARKING_POLICY_OVERRIDE
) ? FALSE
: TRUE
;
2860 bzero(&ipoa
, sizeof(ipoa
));
2861 ipoa
.ipoa_boundif
= IFSCOPE_NONE
;
2862 ipoa
.ipoa_flags
= IPOAF_SELECT_SRCIF
| IPOAF_BOUND_SRCADDR
;
2863 ipoa
.ipoa_sotc
= SO_TC_UNSPEC
;
2864 ipoa
.ipoa_netsvctype
= _NET_SERVICE_TYPE_UNSPEC
;
2865 struct ip6_out_args ip6oa
;
2866 struct route_in6 ro6
;
2868 bzero(&ip6oa
, sizeof(ip6oa
));
2869 ip6oa
.ip6oa_boundif
= IFSCOPE_NONE
;
2870 ip6oa
.ip6oa_flags
= IP6OAF_SELECT_SRCIF
| IP6OAF_BOUND_SRCADDR
;
2871 ip6oa
.ip6oa_sotc
= SO_TC_UNSPEC
;
2872 ip6oa
.ip6oa_netsvctype
= _NET_SERVICE_TYPE_UNSPEC
;
2874 struct flowadv
*adv
=
2875 (isipv6
? &ip6oa
.ip6oa_flowadv
: &ipoa
.ipoa_flowadv
);
2877 /* If socket was bound to an ifindex, tell ip_output about it */
2878 if (inp
->inp_flags
& INP_BOUND_IF
) {
2880 ip6oa
.ip6oa_boundif
= inp
->inp_boundifp
->if_index
;
2881 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
2883 ipoa
.ipoa_boundif
= inp
->inp_boundifp
->if_index
;
2884 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
2888 if (INP_NO_CELLULAR(inp
)) {
2890 ip6oa
.ip6oa_flags
|= IP6OAF_NO_CELLULAR
;
2892 ipoa
.ipoa_flags
|= IPOAF_NO_CELLULAR
;
2895 if (INP_NO_EXPENSIVE(inp
)) {
2897 ip6oa
.ip6oa_flags
|= IP6OAF_NO_EXPENSIVE
;
2899 ipoa
.ipoa_flags
|= IPOAF_NO_EXPENSIVE
;
2902 if (INP_NO_CONSTRAINED(inp
)) {
2904 ip6oa
.ip6oa_flags
|= IP6OAF_NO_CONSTRAINED
;
2906 ipoa
.ipoa_flags
|= IPOAF_NO_CONSTRAINED
;
2909 if (INP_AWDL_UNRESTRICTED(inp
)) {
2911 ip6oa
.ip6oa_flags
|= IP6OAF_AWDL_UNRESTRICTED
;
2913 ipoa
.ipoa_flags
|= IPOAF_AWDL_UNRESTRICTED
;
2916 if (INP_INTCOPROC_ALLOWED(inp
) && isipv6
) {
2917 ip6oa
.ip6oa_flags
|= IP6OAF_INTCOPROC_ALLOWED
;
2920 ip6oa
.ip6oa_sotc
= so
->so_traffic_class
;
2921 ip6oa
.ip6oa_netsvctype
= so
->so_netsvctype
;
2922 ip6oa
.qos_marking_gencount
= inp
->inp_policyresult
.results
.qos_marking_gencount
;
2924 ipoa
.ipoa_sotc
= so
->so_traffic_class
;
2925 ipoa
.ipoa_netsvctype
= so
->so_netsvctype
;
2926 ipoa
.qos_marking_gencount
= inp
->inp_policyresult
.results
.qos_marking_gencount
;
2928 if ((so
->so_flags1
& SOF1_QOSMARKING_ALLOWED
)) {
2930 ip6oa
.ip6oa_flags
|= IP6OAF_QOSMARKING_ALLOWED
;
2932 ipoa
.ipoa_flags
|= IPOAF_QOSMARKING_ALLOWED
;
2935 if (check_qos_marking_again
) {
2937 ip6oa
.ip6oa_flags
|= IP6OAF_REDO_QOSMARKING_POLICY
;
2939 ipoa
.ipoa_flags
|= IPOAF_REDO_QOSMARKING_POLICY
;
2943 flags
|= IPV6_OUTARGS
;
2945 flags
|= IP_OUTARGS
;
2948 /* Copy the cached route and take an extra reference */
2950 in6p_route_copyout(inp
, &ro6
);
2952 inp_route_copyout(inp
, &ro
);
2956 * Make sure ACK/DELACK conditions are cleared before
2957 * we unlock the socket.
2959 tp
->last_ack_sent
= tp
->rcv_nxt
;
2960 tp
->t_flags
&= ~(TF_ACKNOW
| TF_DELACK
);
2961 tp
->t_timer
[TCPT_DELACK
] = 0;
2962 tp
->t_unacksegs
= 0;
2964 /* Increment the count of outstanding send operations */
2965 inp
->inp_sndinprog_cnt
++;
2968 * If allowed, unlock TCP socket while in IP
2969 * but only if the connection is established and
2970 * in a normal mode where reentrancy on the tcpcb won't be
2972 * - there is no SACK episode
2973 * - we're not in Fast Recovery mode
2974 * - if we're not sending from an upcall.
2976 if (tcp_output_unlocked
&& !so
->so_upcallusecount
&&
2977 (tp
->t_state
== TCPS_ESTABLISHED
) && (sack_in_progress
== 0) &&
2978 !IN_FASTRECOVERY(tp
) && !(so
->so_flags
& SOF_MP_SUBFLOW
)) {
2980 socket_unlock(so
, 0);
2984 * Don't send down a chain of packets when:
2985 * - TCP chaining is disabled
2986 * - there is an IPsec rule set
2987 * - there is a non default rule set for the firewall
2990 chain
= tcp_packet_chaining
> 1
2994 ; // I'm important, not extraneous
2996 while (pkt
!= NULL
) {
2997 struct mbuf
*npkt
= pkt
->m_nextpkt
;
3000 pkt
->m_nextpkt
= NULL
;
3002 * If we are not chaining, make sure to set the packet
3003 * list count to 0 so that IP takes the right path;
3004 * this is important for cases such as IPsec where a
3005 * single mbuf might result in multiple mbufs as part
3006 * of the encapsulation. If a non-zero count is passed
3007 * down to IP, the head of the chain might change and
3008 * we could end up skipping it (thus generating bogus
3009 * packets). Fixing it in IP would be desirable, but
3010 * for now this would do it.
3015 error
= ip6_output_list(pkt
, cnt
,
3016 inp
->in6p_outputopts
, &ro6
, flags
, NULL
, NULL
,
3018 ifdenied
= (ip6oa
.ip6oa_retflags
& IP6OARF_IFDENIED
);
3020 error
= ip_output_list(pkt
, cnt
, opt
, &ro
, flags
, NULL
,
3022 ifdenied
= (ipoa
.ipoa_retflags
& IPOARF_IFDENIED
);
3025 if (chain
|| error
) {
3027 * If we sent down a chain then we are done since
3028 * the callee had taken care of everything; else
3029 * we need to free the rest of the chain ourselves.
3044 * Enter flow controlled state if the connection is established
3045 * and is not in recovery. Flow control is allowed only if there
3046 * is outstanding data.
3048 * A connection will enter suspended state even if it is in
3051 if (((adv
->code
== FADV_FLOW_CONTROLLED
&& !IN_FASTRECOVERY(tp
)) ||
3052 adv
->code
== FADV_SUSPENDED
) &&
3053 !(tp
->t_flags
& TF_CLOSING
) &&
3054 tp
->t_state
== TCPS_ESTABLISHED
&&
3055 SEQ_GT(tp
->snd_max
, tp
->snd_una
)) {
3057 rc
= inp_set_fc_state(inp
, adv
->code
);
3060 tcp_ccdbg_trace(tp
, NULL
,
3061 ((adv
->code
== FADV_FLOW_CONTROLLED
) ?
3062 TCP_CC_FLOW_CONTROL
: TCP_CC_SUSPEND
));
3067 * When an interface queue gets suspended, some of the
3068 * packets are dropped. Return ENOBUFS, to update the
3071 if (adv
->code
== FADV_SUSPENDED
) {
3075 VERIFY(inp
->inp_sndinprog_cnt
> 0);
3076 if (--inp
->inp_sndinprog_cnt
== 0) {
3077 inp
->inp_flags
&= ~(INP_FC_FEEDBACK
);
3078 if (inp
->inp_sndingprog_waiters
> 0) {
3079 wakeup(&inp
->inp_sndinprog_cnt
);
3085 * When an NECP IP tunnel policy forces the outbound interface,
3086 * ip6_output_list() informs the transport layer what is the actual
3087 * outgoing interface
3089 if (ip6oa
.ip6oa_flags
& IP6OAF_BOUND_IF
) {
3090 outif
= ifindex2ifnet
[ip6oa
.ip6oa_boundif
];
3091 } else if (ro6
.ro_rt
!= NULL
) {
3092 outif
= ro6
.ro_rt
->rt_ifp
;
3095 if (ro
.ro_rt
!= NULL
) {
3096 outif
= ro
.ro_rt
->rt_ifp
;
3099 if (check_qos_marking_again
) {
3100 uint32_t qos_marking_gencount
;
3101 bool allow_qos_marking
;
3103 qos_marking_gencount
= ip6oa
.qos_marking_gencount
;
3104 allow_qos_marking
= ip6oa
.ip6oa_flags
& IP6OAF_QOSMARKING_ALLOWED
? TRUE
: FALSE
;
3106 qos_marking_gencount
= ipoa
.qos_marking_gencount
;
3107 allow_qos_marking
= ipoa
.ipoa_flags
& IPOAF_QOSMARKING_ALLOWED
? TRUE
: FALSE
;
3109 inp
->inp_policyresult
.results
.qos_marking_gencount
= qos_marking_gencount
;
3110 if (allow_qos_marking
== TRUE
) {
3111 inp
->inp_socket
->so_flags1
|= SOF1_QOSMARKING_ALLOWED
;
3113 inp
->inp_socket
->so_flags1
&= ~SOF1_QOSMARKING_ALLOWED
;
3117 if (outif
!= NULL
&& outif
!= inp
->inp_last_outifp
) {
3118 /* Update the send byte count */
3119 if (so
->so_snd
.sb_cc
> 0 && so
->so_snd
.sb_flags
& SB_SNDBYTE_CNT
) {
3120 inp_decr_sndbytes_total(so
, so
->so_snd
.sb_cc
);
3121 inp_decr_sndbytes_allunsent(so
, tp
->snd_una
);
3122 so
->so_snd
.sb_flags
&= ~SB_SNDBYTE_CNT
;
3124 inp
->inp_last_outifp
= outif
;
3127 if (error
!= 0 && ifdenied
&&
3128 (INP_NO_CELLULAR(inp
) || INP_NO_EXPENSIVE(inp
) || INP_NO_CONSTRAINED(inp
))) {
3130 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_IFDENIED
));
3133 /* Synchronize cached PCB route & options */
3135 in6p_route_copyin(inp
, &ro6
);
3137 inp_route_copyin(inp
, &ro
);
3140 if (tp
->t_state
< TCPS_ESTABLISHED
&& tp
->t_rxtshift
== 0 &&
3141 tp
->t_inpcb
->inp_route
.ro_rt
!= NULL
) {
3142 /* If we found the route and there is an rtt on it
3143 * reset the retransmit timer
3145 tcp_getrt_rtt(tp
, tp
->t_inpcb
->in6p_route
.ro_rt
);
3146 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
3151 int tcptv_persmin_val
= TCPTV_PERSMIN
;
3154 tcp_setpersist(struct tcpcb
*tp
)
3156 int t
= ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1;
3158 /* If a PERSIST_TIMER option was set we will limit the
3159 * time the persist timer will be active for that connection
3160 * in order to avoid DOS by using zero window probes.
3161 * see rdar://5805356
3164 if (tp
->t_persist_timeout
!= 0 &&
3165 tp
->t_timer
[TCPT_PERSIST
] == 0 &&
3166 tp
->t_persist_stop
== 0) {
3167 tp
->t_persist_stop
= tcp_now
+ tp
->t_persist_timeout
;
3171 * Start/restart persistance timer.
3173 TCPT_RANGESET(tp
->t_timer
[TCPT_PERSIST
],
3174 t
* tcp_backoff
[tp
->t_rxtshift
],
3175 tcptv_persmin_val
, TCPTV_PERSMAX
, 0);
3176 tp
->t_timer
[TCPT_PERSIST
] = OFFSET_FROM_START(tp
, tp
->t_timer
[TCPT_PERSIST
]);
3178 if (tp
->t_rxtshift
< TCP_MAXRXTSHIFT
) {
3184 tcp_recv_throttle(struct tcpcb
*tp
)
3186 uint32_t base_rtt
, newsize
;
3187 struct sockbuf
*sbrcv
= &tp
->t_inpcb
->inp_socket
->so_rcv
;
3189 if (tcp_use_rtt_recvbg
== 1 &&
3190 TSTMP_SUPPORTED(tp
)) {
3192 * Timestamps are supported on this connection. Use
3193 * RTT to look for an increase in latency.
3197 * If the connection is already being throttled, leave it
3198 * in that state until rtt comes closer to base rtt
3200 if (tp
->t_flagsext
& TF_RECV_THROTTLE
) {
3204 base_rtt
= get_base_rtt(tp
);
3206 if (base_rtt
!= 0 && tp
->t_rttcur
!= 0) {
3208 * if latency increased on a background flow,
3209 * return 1 to start throttling.
3211 if (tp
->t_rttcur
> (base_rtt
+ target_qdelay
)) {
3212 tp
->t_flagsext
|= TF_RECV_THROTTLE
;
3213 if (tp
->t_recv_throttle_ts
== 0) {
3214 tp
->t_recv_throttle_ts
= tcp_now
;
3217 * Reduce the recv socket buffer size to
3220 if (sbrcv
->sb_idealsize
>
3221 tcp_recv_throttle_minwin
) {
3222 newsize
= sbrcv
->sb_idealsize
>> 1;
3223 /* Set a minimum of 16 K */
3226 tcp_recv_throttle_minwin
);
3227 sbrcv
->sb_idealsize
= newsize
;
3237 * Timestamps are not supported or there is no good RTT
3238 * measurement. Use IPDV in this case.
3240 if (tp
->acc_iaj
> tcp_acc_iaj_react_limit
) {