2 * Copyright (c) 2000-2013 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/ip.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/ip_var.h>
98 #include <netinet6/in6_pcb.h>
99 #include <netinet/ip6.h>
100 #include <netinet6/ip6_var.h>
102 #include <netinet/tcp.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 <sys/kdebug.h>
114 #include <mach/sdt.h>
117 #include <netinet6/ipsec.h>
121 #include <security/mac_framework.h>
122 #endif /* MAC_SOCKET */
124 #include <netinet/lro_ext.h>
126 #include <netinet/mptcp_var.h>
127 #include <netinet/mptcp.h>
128 #include <netinet/mptcp_opt.h>
131 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 1)
132 #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 3)
133 #define DBG_FNC_TCP_OUTPUT NETDBG_CODE(DBG_NETTCP, (4 << 8) | 1)
135 int path_mtu_discovery
= 1;
136 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, path_mtu_discovery
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
137 &path_mtu_discovery
, 1, "Enable Path MTU Discovery");
140 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, slowstart_flightsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
141 &ss_fltsz
, 1, "Slow start flight size");
143 int ss_fltsz_local
= 8; /* starts with eight segments max */
144 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, local_slowstart_flightsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
145 &ss_fltsz_local
, 1, "Slow start flight size for local networks");
148 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tso
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
149 &tcp_do_tso
, 0, "Enable TCP Segmentation Offload");
152 int tcp_ecn_outbound
= 0;
153 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, ecn_initiate_out
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_ecn_outbound
,
154 0, "Initiate ECN for outbound connections");
156 int tcp_ecn_inbound
= 0;
157 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, ecn_negotiate_in
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_ecn_inbound
,
158 0, "Allow ECN negotiation for inbound connections");
160 int tcp_packet_chaining
= 50;
161 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, packetchain
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_packet_chaining
,
162 0, "Enable TCP output packet chaining");
164 int tcp_output_unlocked
= 1;
165 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, socket_unlocked_on_output
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_output_unlocked
,
166 0, "Unlock TCP when sending packets down to IP");
168 int tcp_do_rfc3390
= 1;
169 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rfc3390
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
170 &tcp_do_rfc3390
, 1, "Calculate intial slowstart cwnd depending on MSS");
172 int tcp_min_iaj_win
= MIN_IAJ_WIN
;
173 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, min_iaj_win
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
174 &tcp_min_iaj_win
, 1, "Minimum recv win based on inter-packet arrival jitter");
176 int tcp_acc_iaj_react_limit
= ACC_IAJ_REACT_LIMIT
;
177 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, acc_iaj_react_limit
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
178 &tcp_acc_iaj_react_limit
, 1, "Accumulated IAJ when receiver starts to react");
180 uint32_t tcp_do_autosendbuf
= 1;
181 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, doautosndbuf
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
182 &tcp_do_autosendbuf
, 1, "Enable send socket buffer auto-tuning");
184 uint32_t tcp_autosndbuf_inc
= 8 * 1024;
185 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autosndbufinc
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
186 &tcp_autosndbuf_inc
, 1, "Increment in send socket bufffer size");
188 uint32_t tcp_autosndbuf_max
= 512 * 1024;
189 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autosndbufmax
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
190 &tcp_autosndbuf_max
, 1, "Maximum send socket buffer size");
192 uint32_t tcp_prioritize_acks
= 1;
193 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, ack_prioritize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
194 &tcp_prioritize_acks
, 1, "Prioritize pure acks");
196 uint32_t tcp_use_rtt_recvbg
= 1;
197 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rtt_recvbg
,
198 CTLFLAG_RW
| CTLFLAG_LOCKED
,
199 &tcp_use_rtt_recvbg
, 1, "Use RTT for bg recv algorithm");
201 uint32_t tcp_recv_throttle_minwin
= 16 * 1024;
202 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, recv_throttle_minwin
,
203 CTLFLAG_RW
| CTLFLAG_LOCKED
,
204 &tcp_recv_throttle_minwin
, 1, "Minimum recv win for throttling");
207 static int32_t packchain_newlist
= 0;
208 static int32_t packchain_looped
= 0;
209 static int32_t packchain_sent
= 0;
211 /* temporary: for testing */
213 extern int ipsec_bypass
;
216 extern int slowlink_wsize
; /* window correction for slow links */
218 extern int fw_enable
; /* firewall check for packet chaining */
219 extern int fw_bypass
; /* firewall check: disable packet chaining if there is rules */
220 #endif /* IPFIREWALL */
222 extern u_int32_t dlil_filter_disable_tso_count
;
223 extern u_int32_t kipf_count
;
224 extern int tcp_recv_bg
;
225 extern int maxseg_unacked
;
227 static int tcp_ip_output(struct socket
*, struct tcpcb
*, struct mbuf
*, int,
228 struct mbuf
*, int, int, int32_t, boolean_t
);
230 extern uint32_t get_base_rtt(struct tcpcb
*tp
);
231 static struct mbuf
* tcp_send_lroacks(struct tcpcb
*tp
, struct mbuf
*m
, struct tcphdr
*th
);
232 static int tcp_recv_throttle(struct tcpcb
*tp
);
235 * Tcp output routine: figure out what should be sent and send it.
243 * ip_output_list:ENOMEM
244 * ip_output_list:EADDRNOTAVAIL
245 * ip_output_list:ENETUNREACH
246 * ip_output_list:EHOSTUNREACH
247 * ip_output_list:EACCES
248 * ip_output_list:EMSGSIZE
249 * ip_output_list:ENOBUFS
250 * ip_output_list:??? [ignorable: mostly IPSEC/firewall/DLIL]
251 * ip6_output_list:EINVAL
252 * ip6_output_list:EOPNOTSUPP
253 * ip6_output_list:EHOSTUNREACH
254 * ip6_output_list:EADDRNOTAVAIL
255 * ip6_output_list:ENETUNREACH
256 * ip6_output_list:EMSGSIZE
257 * ip6_output_list:ENOBUFS
258 * ip6_output_list:??? [ignorable: mostly IPSEC/firewall/DLIL]
261 tcp_output(struct tcpcb
*tp
)
263 struct inpcb
*inp
= tp
->t_inpcb
;
264 struct socket
*so
= inp
->inp_socket
;
265 int32_t len
, recwin
, sendwin
, off
;
267 register struct mbuf
*m
;
268 struct ip
*ip
= NULL
;
269 register struct ipovly
*ipov
= NULL
;
271 struct ip6_hdr
*ip6
= NULL
;
273 register struct tcphdr
*th
;
274 u_char opt
[TCP_MAXOLEN
];
275 unsigned ipoptlen
, optlen
, hdrlen
;
276 int idle
, sendalot
, lost
= 0;
282 unsigned ipsec_optlen
= 0;
287 struct mbuf
*m_lastm
= NULL
;
288 struct mbuf
*m_head
= NULL
;
289 struct mbuf
*packetlist
= NULL
;
290 struct mbuf
*tp_inp_options
= inp
->inp_depend4
.inp4_options
;
292 int isipv6
= inp
->inp_vflag
& INP_IPV6
;
294 short packchain_listadd
= 0;
295 int so_options
= so
->so_options
;
297 u_int32_t basertt
, svc_flags
= 0, allocated_len
;
298 u_int32_t lro_ackmore
= (tp
->t_lropktlen
!= 0) ? 1 : 0;
299 struct mbuf
*mnext
= NULL
;
302 unsigned int *dlenp
= NULL
;
303 u_int8_t
*finp
= NULL
;
304 u_int32_t
*sseqp
= NULL
;
305 u_int64_t dss_val
= 0;
306 int mptcp_acknow
= 0;
308 boolean_t cell
= FALSE
;
309 boolean_t wifi
= FALSE
;
312 * Determine length of data that should be transmitted,
313 * and flags that will be used.
314 * If there is some data or critical controls (SYN, RST)
315 * to send, then transmit; otherwise, investigate further.
317 idle
= (tp
->t_flags
& TF_LASTIDLE
) || (tp
->snd_max
== tp
->snd_una
);
319 /* Since idle_time is signed integer, the following integer subtraction
320 * will take care of wrap around of tcp_now
322 idle_time
= tcp_now
- tp
->t_rcvtime
;
323 if (idle
&& idle_time
>= TCP_IDLETIMEOUT(tp
)) {
324 if (CC_ALGO(tp
)->after_idle
!= NULL
)
325 CC_ALGO(tp
)->after_idle(tp
);
326 DTRACE_TCP5(cc
, void, NULL
, struct inpcb
*, inp
,
327 struct tcpcb
*, tp
, struct tcphdr
*, NULL
,
328 int32_t, TCP_CC_IDLE_TIMEOUT
);
330 tp
->t_flags
&= ~TF_LASTIDLE
;
332 if (tp
->t_flags
& TF_MORETOCOME
) {
333 tp
->t_flags
|= TF_LASTIDLE
;
338 if (tp
->t_mpflags
& TMPF_RESET
) {
339 tcp_check_timer_state(tp
);
341 * Once a RST has been sent for an MPTCP subflow,
342 * the subflow socket stays around until deleted.
343 * No packets such as FINs must be sent after RST.
350 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
354 KERNEL_DEBUG(DBG_LAYER_BEG
,
355 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
356 (((inp
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
357 (inp
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
363 KERNEL_DEBUG(DBG_LAYER_BEG
,
364 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
365 (((inp
->inp_laddr
.s_addr
& 0xffff) << 16) |
366 (inp
->inp_faddr
.s_addr
& 0xffff)),
370 * If the route generation id changed, we need to check that our
371 * local (source) IP address is still valid. If it isn't either
372 * return error or silently do nothing (assuming the address will
373 * come back before the TCP connection times out).
375 rt
= inp
->inp_route
.ro_rt
;
376 if (rt
!= NULL
&& ROUTE_UNUSABLE(&tp
->t_inpcb
->inp_route
)) {
378 struct in_ifaddr
*ia
= NULL
;
379 struct in6_ifaddr
*ia6
= NULL
;
380 int found_srcaddr
= 0;
382 /* disable multipages at the socket */
383 somultipages(so
, FALSE
);
385 /* Disable TSO for the socket until we know more */
386 tp
->t_flags
&= ~TF_TSO
;
389 ia6
= ifa_foraddr6(&inp
->in6p_laddr
);
393 ia
= ifa_foraddr(inp
->inp_laddr
.s_addr
);
398 /* check that the source address is still valid */
399 if (found_srcaddr
== 0) {
401 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_NOSRCADDR
));
403 if (tp
->t_state
>= TCPS_CLOSE_WAIT
) {
404 tcp_drop(tp
, EADDRNOTAVAIL
);
405 return(EADDRNOTAVAIL
);
408 /* set Retransmit timer if it wasn't set
409 * reset Persist timer and shift register as the
410 * advertised peer window may not be valid anymore
413 if (!tp
->t_timer
[TCPT_REXMT
]) {
414 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
415 if (tp
->t_timer
[TCPT_PERSIST
]) {
416 tp
->t_timer
[TCPT_PERSIST
] = 0;
418 tp
->t_persist_stop
= 0;
423 if (tp
->t_pktlist_head
!= NULL
)
424 m_freem_list(tp
->t_pktlist_head
);
425 TCP_PKTLIST_CLEAR(tp
);
427 /* drop connection if source address isn't available */
428 if (so
->so_flags
& SOF_NOADDRAVAIL
) {
429 tcp_drop(tp
, EADDRNOTAVAIL
);
430 return(EADDRNOTAVAIL
);
433 tcp_check_timer_state(tp
);
434 return(0); /* silently ignore, keep data in socket: address may be back */
438 IFA_REMREF(&ia
->ia_ifa
);
441 IFA_REMREF(&ia6
->ia_ifa
);
444 * Address is still valid; check for multipages capability
445 * again in case the outgoing interface has changed.
448 if ((ifp
= rt
->rt_ifp
) != NULL
) {
449 somultipages(so
, (ifp
->if_hwassist
& IFNET_MULTIPAGES
));
450 tcp_set_tso(tp
, ifp
);
452 if (rt
->rt_flags
& RTF_UP
)
455 * See if we should do MTU discovery. Don't do it if:
456 * 1) it is disabled via the sysctl
457 * 2) the route isn't up
458 * 3) the MTU is locked (if it is, then discovery has been
462 if (!path_mtu_discovery
|| ((rt
!= NULL
) &&
463 (!(rt
->rt_flags
& RTF_UP
) || (rt
->rt_rmx
.rmx_locks
& RTV_MTU
))))
464 tp
->t_flags
&= ~TF_PMTUD
;
466 tp
->t_flags
|= TF_PMTUD
;
472 cell
= IFNET_IS_CELLULAR(rt
->rt_ifp
);
473 wifi
= (!cell
&& IFNET_IS_WIFI(rt
->rt_ifp
));
477 * If we've recently taken a timeout, snd_max will be greater than
478 * snd_nxt. There may be SACK information that allows us to avoid
479 * resending already delivered data. Adjust snd_nxt accordingly.
481 if (SACK_ENABLED(tp
) && SEQ_LT(tp
->snd_nxt
, tp
->snd_max
))
484 off
= tp
->snd_nxt
- tp
->snd_una
;
485 sendwin
= min(tp
->snd_wnd
, tp
->snd_cwnd
);
487 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0)
488 sendwin
= min(sendwin
, slowlink_wsize
);
490 flags
= tcp_outflags
[tp
->t_state
];
492 * Send any SACK-generated retransmissions. If we're explicitly trying
493 * to send out new data (when sendalot is 1), bypass this function.
494 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
495 * we're replacing a (future) new transmission with a retransmission
496 * now, and we previously incremented snd_cwnd in tcp_input().
499 * Still in sack recovery , reset rxmit flag to zero.
505 if (SACK_ENABLED(tp
) && IN_FASTRECOVERY(tp
) &&
506 (p
= tcp_sack_output(tp
, &sack_bytes_rxmt
))) {
509 cwin
= min(tp
->snd_wnd
, tp
->snd_cwnd
) - sack_bytes_rxmt
;
512 /* Do not retransmit SACK segments beyond snd_recover */
513 if (SEQ_GT(p
->end
, tp
->snd_recover
)) {
515 * (At least) part of sack hole extends beyond
516 * snd_recover. Check to see if we can rexmit data
519 if (SEQ_GEQ(p
->rxmit
, tp
->snd_recover
)) {
521 * Can't rexmit any more data for this hole.
522 * That data will be rexmitted in the next
523 * sack recovery episode, when snd_recover
524 * moves past p->rxmit.
527 goto after_sack_rexmit
;
529 /* Can rexmit part of the current hole */
530 len
= ((int32_t)min(cwin
,
531 tp
->snd_recover
- p
->rxmit
));
533 len
= ((int32_t)min(cwin
, p
->end
- p
->rxmit
));
536 off
= p
->rxmit
- tp
->snd_una
; /* update off only if we really transmit SACK data */
539 tcpstat
.tcps_sack_rexmits
++;
540 tcpstat
.tcps_sack_rexmit_bytes
+=
541 min(len
, tp
->t_maxseg
);
543 nstat_route_tx(inp
->inp_route
.ro_rt
, 1,
544 min(len
, tp
->t_maxseg
),
545 NSTAT_TX_FLAG_RETRANSMIT
);
546 INP_ADD_STAT(inp
, cell
, wifi
, txpackets
, 1);
547 INP_ADD_STAT(inp
, cell
, wifi
, txbytes
,
548 min(len
, tp
->t_maxseg
));
549 tp
->t_stat
.txretransmitbytes
+= min(len
, tp
->t_maxseg
);
557 * Get standard flags, and add SYN or FIN if requested by 'hidden'
560 if (tp
->t_flags
& TF_NEEDFIN
)
562 if (tp
->t_flags
& TF_NEEDSYN
)
566 * If in persist timeout with window of 0, send 1 byte.
567 * Otherwise, if window is small but nonzero
568 * and timer expired, we will send what we can
569 * and go to transmit state.
574 * If we still have some data to send, then
575 * clear the FIN bit. Usually this would
576 * happen below when it realizes that we
577 * aren't sending all the data. However,
578 * if we have exactly 1 byte of unsent data,
579 * then it won't clear the FIN bit below,
580 * and if we are in persist state, we wind
581 * up sending the packet without recording
582 * that we sent the FIN bit.
584 * We can't just blindly clear the FIN bit,
585 * because if we don't have any more data
586 * to send then the probe will be the FIN
589 if (off
< so
->so_snd
.sb_cc
)
593 tp
->t_timer
[TCPT_PERSIST
] = 0;
596 tp
->t_persist_stop
= 0;
601 * If snd_nxt == snd_max and we have transmitted a FIN, the
602 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
603 * a negative length. This can also occur when TCP opens up
604 * its congestion window while receiving additional duplicate
605 * acks after fast-retransmit because TCP will reset snd_nxt
606 * to snd_max after the fast-retransmit.
608 * In the normal retransmit-FIN-only case, however, snd_nxt will
609 * be set to snd_una, the offset will be 0, and the length may
612 * If sack_rxmit is true we are retransmitting from the scoreboard
613 * in which case len is already set.
615 if (sack_rxmit
== 0) {
616 if (sack_bytes_rxmt
== 0)
617 len
= min(so
->so_snd
.sb_cc
, sendwin
) - off
;
622 * We are inside of a SACK recovery episode and are
623 * sending new data, having retransmitted all the
624 * data possible in the scoreboard.
626 len
= min(so
->so_snd
.sb_cc
, tp
->snd_wnd
)
629 * Don't remove this (len > 0) check !
630 * We explicitly check for len > 0 here (although it
631 * isn't really necessary), to work around a gcc
632 * optimization issue - to force gcc to compute
633 * len above. Without this check, the computation
634 * of len is bungled by the optimizer.
637 cwin
= tp
->snd_cwnd
-
638 (tp
->snd_nxt
- tp
->sack_newdata
) -
642 len
= imin(len
, cwin
);
650 * Lop off SYN bit if it has already been sent. However, if this
651 * is SYN-SENT state and if segment contains data and if we don't
652 * know that foreign host supports TAO, suppress sending segment.
654 if ((flags
& TH_SYN
) && SEQ_GT(tp
->snd_nxt
, tp
->snd_una
)) {
655 if (tp
->t_state
!= TCPS_SYN_RECEIVED
)
658 if (len
> 0 && tp
->t_state
== TCPS_SYN_SENT
) {
659 while (inp
->inp_sndinprog_cnt
== 0 &&
660 tp
->t_pktlist_head
!= NULL
) {
661 packetlist
= tp
->t_pktlist_head
;
662 packchain_listadd
= tp
->t_lastchain
;
664 TCP_PKTLIST_CLEAR(tp
);
666 error
= tcp_ip_output(so
, tp
, packetlist
,
667 packchain_listadd
, tp_inp_options
,
668 (so_options
& SO_DONTROUTE
),
669 (sack_rxmit
| (sack_bytes_rxmt
!= 0)), 0,
679 * tcp was closed while we were in ip,
682 if (inp
->inp_sndinprog_cnt
== 0 &&
683 (tp
->t_flags
& TF_CLOSING
)) {
684 tp
->t_flags
&= ~TF_CLOSING
;
685 (void) tcp_close(tp
);
687 tcp_check_timer_state(tp
);
689 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
,
696 * Be careful not to send data and/or FIN on SYN segments.
697 * This measure is needed to prevent interoperability problems
698 * with not fully conformant TCP implementations.
700 if ((flags
& TH_SYN
) && (tp
->t_flags
& TF_NOOPT
)) {
705 /* The check here used to be (len < 0). Some times len is zero when
706 * the congestion window is closed and we need to check if persist timer
707 * has to be set in that case. But don't set persist until connection
710 if (len
<= 0 && !(flags
& TH_SYN
)) {
712 * If FIN has been sent but not acked,
713 * but we haven't been called to retransmit,
714 * len will be < 0. Otherwise, window shrank
715 * after we sent into it. If window shrank to 0,
716 * cancel pending retransmit, pull snd_nxt back
717 * to (closed) window, and set the persist timer
718 * if it isn't already going. If the window didn't
719 * close completely, just wait for an ACK.
723 tp
->t_timer
[TCPT_REXMT
] = 0;
726 tp
->snd_nxt
= tp
->snd_una
;
727 if (tp
->t_timer
[TCPT_PERSIST
] == 0)
732 /* Automatic sizing of send socket buffer. Increase the send socket buffer
733 * size if all of the following criteria are met
734 * 1. the receiver has enough buffer space for this data
735 * 2. send buffer is filled to 7/8th with data (so we actually
736 * have data to make use of it);
737 * 3. our send window (slow start and congestion controlled) is
738 * larger than sent but unacknowledged data in send buffer.
740 basertt
= get_base_rtt(tp
);
741 if (tcp_do_autosendbuf
== 1 &&
742 !INP_WAIT_FOR_IF_FEEDBACK(inp
) && !IN_FASTRECOVERY(tp
) &&
743 (so
->so_snd
.sb_flags
& (SB_AUTOSIZE
| SB_TRIM
)) == SB_AUTOSIZE
&&
744 tcp_cansbgrow(&so
->so_snd
)) {
745 if ((tp
->snd_wnd
/ 4 * 5) >= so
->so_snd
.sb_hiwat
&&
746 so
->so_snd
.sb_cc
>= (so
->so_snd
.sb_hiwat
/ 8 * 7) &&
747 sendwin
>= (so
->so_snd
.sb_cc
-
748 (tp
->snd_nxt
- tp
->snd_una
))) {
749 /* Also increase the send buffer only if the
750 * round-trip time is not increasing because we do
751 * not want to contribute to latency by filling buffers.
752 * We also do not want to hold onto application's
753 * old data for too long. Interactive applications would
754 * rather discard old data.
758 if (sbreserve(&so
->so_snd
,
759 min(so
->so_snd
.sb_hiwat
+ tcp_autosndbuf_inc
,
760 tcp_autosndbuf_max
)) == 1) {
761 so
->so_snd
.sb_idealsize
= so
->so_snd
.sb_hiwat
;
764 so
->so_snd
.sb_idealsize
=
765 max(tcp_sendspace
, so
->so_snd
.sb_hiwat
-
766 (2 * tcp_autosndbuf_inc
));
767 so
->so_snd
.sb_flags
|= SB_TRIM
;
773 * Truncate to the maximum segment length or enable TCP Segmentation
774 * Offloading (if supported by hardware) and ensure that FIN is removed
775 * if the length no longer contains the last data byte.
777 * TSO may only be used if we are in a pure bulk sending state.
778 * The presence of TCP-MD5, SACK retransmits, SACK advertizements,
779 * ipfw rules and IP options, as well as disabling hardware checksum
780 * offload prevent using TSO. With TSO the TCP header is the same
781 * (except for the sequence number) for all generated packets. This
782 * makes it impossible to transmit any options which vary per generated
785 * The length of TSO bursts is limited to TCP_MAXWIN. That limit and
786 * removal of FIN (if not already catched here) are handled later after
787 * the exact length of the TCP options are known.
791 * Pre-calculate here as we save another lookup into the darknesses
792 * of IPsec that way and can actually decide if TSO is ok.
794 if (ipsec_bypass
== 0)
795 ipsec_optlen
= ipsec_hdrsiz_tcp(tp
);
798 if (len
> tp
->t_maxseg
) {
799 if ((tp
->t_flags
& TF_TSO
) && tcp_do_tso
&& hwcksum_tx
&&
800 ip_use_randomid
&& kipf_count
== 0 && dlil_filter_disable_tso_count
== 0 &&
801 tp
->rcv_numsacks
== 0 && sack_rxmit
== 0 && sack_bytes_rxmt
== 0 &&
802 inp
->inp_options
== NULL
&&
803 inp
->in6p_options
== NULL
808 && (fw_enable
== 0 || fw_bypass
)
820 if (so
->so_flags
& SOF_MP_SUBFLOW
) {
822 if ((tp
->t_mpflags
& TMPF_SND_MPPRIO
) ||
823 (tp
->t_mpflags
& TMPF_SND_REM_ADDR
) ||
824 (tp
->t_mpflags
& TMPF_SND_MPFAIL
)) {
834 * The contiguous bytes in the subflow socket buffer can be
835 * discontiguous at the MPTCP level. Since only one DSS
836 * option can be sent in one packet, reduce length to match
837 * the contiguous MPTCP level. Set sendalot to send remainder.
840 newlen
= mptcp_adj_sendlen(so
, off
, len
);
848 if (SEQ_LT(p
->rxmit
+ len
, tp
->snd_una
+ so
->so_snd
.sb_cc
))
851 if (SEQ_LT(tp
->snd_nxt
+ len
, tp
->snd_una
+ so
->so_snd
.sb_cc
))
855 recwin
= tcp_sbspace(tp
);
858 * If the socket is capable of doing unordered send,
859 * pull the amount of data that can be sent from the
860 * unordered priority queues to the serial queue in
861 * the socket buffer. If bytes are not yet available
862 * in the highest priority message, we may not be able
863 * to send any new data.
865 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
867 so
->so_msg_state
->msg_serial_bytes
) {
868 sbpull_unordered_data(so
, off
, len
);
870 /* check if len needs to be modified */
872 so
->so_msg_state
->msg_serial_bytes
) {
873 len
= so
->so_msg_state
->msg_serial_bytes
- off
;
876 tcpstat
.tcps_msg_sndwaithipri
++;
883 * Sender silly window avoidance. We transmit under the following
884 * conditions when len is non-zero:
886 * - we've timed out (e.g. persist timer)
887 * - we need to retransmit
888 * - We have a full segment (or more with TSO)
889 * - This is the last buffer in a write()/send() and we are
890 * either idle or running NODELAY
891 * - we have more then 1/2 the maximum send window's worth of
892 * data (receiver may be limited the window size)
897 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
))
903 * Send new data on the connection only if it is
904 * not flow controlled
906 if (!INP_WAIT_FOR_IF_FEEDBACK(inp
) ||
907 tp
->t_state
!= TCPS_ESTABLISHED
) {
908 if (len
>= tp
->t_maxseg
)
910 if (!(tp
->t_flags
& TF_MORETOCOME
) &&
911 (idle
|| tp
->t_flags
& TF_NODELAY
||
912 tp
->t_flags
& TF_MAXSEGSNT
||
913 ALLOW_LIMITED_TRANSMIT(tp
)) &&
914 (tp
->t_flags
& TF_NOPUSH
) == 0 &&
915 len
+ off
>= so
->so_snd
.sb_cc
)
917 if (len
>= tp
->max_sndwnd
/ 2 && tp
->max_sndwnd
> 0)
920 tcpstat
.tcps_fcholdpacket
++;
925 * Compare available window to amount of window
926 * known to peer (as advertised window less
927 * next expected input). If the difference is at least two
928 * max size segments, or at least 25% of the maximum possible
929 * window, then want to send a window update to peer.
930 * Skip this if the connection is in T/TCP half-open state.
932 if (recwin
> 0 && !(tp
->t_flags
& TF_NEEDSYN
)) {
934 * "adv" is the amount we can increase the window,
935 * taking into account that we are limited by
936 * TCP_MAXWIN << tp->rcv_scale.
938 int32_t adv
, oldwin
= 0;
939 adv
= imin(recwin
, (int)TCP_MAXWIN
<< tp
->rcv_scale
) -
940 (tp
->rcv_adv
- tp
->rcv_nxt
);
942 if (SEQ_GT(tp
->rcv_adv
, tp
->rcv_nxt
))
943 oldwin
= tp
->rcv_adv
- tp
->rcv_nxt
;
945 if (adv
>= (int32_t) (2 * tp
->t_maxseg
)) {
946 /* Update only if the resulting scaled value of the window changed, or
947 * if there is a change in the sequence since the last ack.
948 * This avoids what appears as dupe ACKS (see rdar://5640997)
950 * If streaming is detected avoid sending too many window updates.
951 * We will depend on the delack timer to send a window update
954 if (!(tp
->t_flags
& TF_STRETCHACK
) &&
955 (tp
->last_ack_sent
!= tp
->rcv_nxt
||
956 ((oldwin
+ adv
) >> tp
->rcv_scale
) >
957 (oldwin
>> tp
->rcv_scale
))) {
961 /* Make sure that the delayed ack timer is set if we
962 * delayed sending a window update because of streaming
965 if ((tp
->t_flags
& TF_STRETCHACK
) &&
966 !(tp
->t_flags
& TF_DELACK
)) {
967 tp
->t_flags
|= TF_DELACK
;
968 tp
->t_timer
[TCPT_DELACK
] =
969 OFFSET_FROM_START(tp
, tcp_delack
);
972 if (4 * adv
>= (int32_t) so
->so_rcv
.sb_hiwat
)
977 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW
978 * is also a catch-all for the retransmit timer timeout case.
980 if (tp
->t_flags
& TF_ACKNOW
)
982 if ((flags
& TH_RST
) ||
983 ((flags
& TH_SYN
) && (tp
->t_flags
& TF_NEEDSYN
) == 0))
985 if (SEQ_GT(tp
->snd_up
, tp
->snd_una
))
992 * If our state indicates that FIN should be sent
993 * and we have not yet done so, then we need to send.
995 if (flags
& TH_FIN
&&
996 ((tp
->t_flags
& TF_SENTFIN
) == 0 || tp
->snd_nxt
== tp
->snd_una
))
999 * In SACK, it is possible for tcp_output to fail to send a segment
1000 * after the retransmission timer has been turned off. Make sure
1001 * that the retransmission timer is set.
1003 if (SACK_ENABLED(tp
) && (tp
->t_state
>= TCPS_ESTABLISHED
) &&
1004 SEQ_GT(tp
->snd_max
, tp
->snd_una
) &&
1005 tp
->t_timer
[TCPT_REXMT
] == 0 &&
1006 tp
->t_timer
[TCPT_PERSIST
] == 0) {
1007 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
1011 * TCP window updates are not reliable, rather a polling protocol
1012 * using ``persist'' packets is used to insure receipt of window
1013 * updates. The three ``states'' for the output side are:
1014 * idle not doing retransmits or persists
1015 * persisting to move a small or zero window
1016 * (re)transmitting and thereby not persisting
1018 * tp->t_timer[TCPT_PERSIST]
1019 * is set when we are in persist state.
1021 * is set when we are called to send a persist packet.
1022 * tp->t_timer[TCPT_REXMT]
1023 * is set when we are retransmitting
1024 * The output side is idle when both timers are zero.
1026 * If send window is too small, there is data to transmit, and no
1027 * retransmit or persist is pending, then go to persist state.
1028 * If nothing happens soon, send when timer expires:
1029 * if window is nonzero, transmit what we can,
1030 * otherwise force out a byte.
1032 if (so
->so_snd
.sb_cc
&& tp
->t_timer
[TCPT_REXMT
] == 0 &&
1033 tp
->t_timer
[TCPT_PERSIST
] == 0) {
1040 * If there is no reason to send a segment, just return.
1041 * but if there is some packets left in the packet list, send them now.
1043 while (inp
->inp_sndinprog_cnt
== 0 &&
1044 tp
->t_pktlist_head
!= NULL
) {
1045 packetlist
= tp
->t_pktlist_head
;
1046 packchain_listadd
= tp
->t_lastchain
;
1048 TCP_PKTLIST_CLEAR(tp
);
1050 error
= tcp_ip_output(so
, tp
, packetlist
, packchain_listadd
,
1051 tp_inp_options
, (so_options
& SO_DONTROUTE
),
1052 (sack_rxmit
| (sack_bytes_rxmt
!= 0)), recwin
,
1059 /* tcp was closed while we were in ip; resume close */
1060 if (inp
->inp_sndinprog_cnt
== 0 &&
1061 (tp
->t_flags
& TF_CLOSING
)) {
1062 tp
->t_flags
&= ~TF_CLOSING
;
1063 (void) tcp_close(tp
);
1065 tcp_check_timer_state(tp
);
1067 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1072 * Set TF_MAXSEGSNT flag if the segment size is greater than
1073 * the max segment size.
1076 if (len
>= tp
->t_maxseg
)
1077 tp
->t_flags
|= TF_MAXSEGSNT
;
1079 tp
->t_flags
&= ~TF_MAXSEGSNT
;
1082 * Before ESTABLISHED, force sending of initial options
1083 * unless TCP set not to do any options.
1084 * NOTE: we assume that the IP/TCP header plus TCP options
1085 * always fit in a single mbuf, leaving room for a maximum
1087 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
1092 hdrlen
= sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
);
1095 hdrlen
= sizeof (struct tcpiphdr
);
1096 if (flags
& TH_SYN
) {
1097 tp
->snd_nxt
= tp
->iss
;
1098 if ((tp
->t_flags
& TF_NOOPT
) == 0) {
1101 opt
[0] = TCPOPT_MAXSEG
;
1102 opt
[1] = TCPOLEN_MAXSEG
;
1103 mss
= htons((u_short
) tcp_mssopt(tp
));
1104 (void)memcpy(opt
+ 2, &mss
, sizeof(mss
));
1105 optlen
= TCPOLEN_MAXSEG
;
1107 if ((tp
->t_flags
& TF_REQ_SCALE
) &&
1108 ((flags
& TH_ACK
) == 0 ||
1109 (tp
->t_flags
& TF_RCVD_SCALE
))) {
1110 *((u_int32_t
*)(void *)(opt
+ optlen
)) = htonl(
1112 TCPOPT_WINDOW
<< 16 |
1113 TCPOLEN_WINDOW
<< 8 |
1114 tp
->request_r_scale
);
1119 optlen
= mptcp_setup_syn_opts(so
, flags
, opt
,
1127 RFC 3168 states that:
1128 - If you ever sent an ECN-setup SYN/SYN-ACK you must be prepared
1129 to handle the TCP ECE flag, even if you also later send a
1130 non-ECN-setup SYN/SYN-ACK.
1131 - If you ever send a non-ECN-setup SYN/SYN-ACK, you must not set
1134 It is not clear how the ECE flag would ever be set if you never
1135 set the IP ECT flag on outbound packets. All the same, we use
1136 the TE_SETUPSENT to indicate that we have committed to handling
1137 the TCP ECE flag correctly. We use the TE_SENDIPECT to indicate
1138 whether or not we should set the IP ECT flag on outbound packets.
1141 * For a SYN-ACK, send an ECN setup SYN-ACK
1143 if (tcp_ecn_inbound
&& (flags
& (TH_SYN
| TH_ACK
)) == (TH_SYN
| TH_ACK
)) {
1144 if ((tp
->ecn_flags
& TE_SETUPRECEIVED
) != 0) {
1145 if ((tp
->ecn_flags
& TE_SETUPSENT
) == 0) {
1146 /* Setting TH_ECE makes this an ECN-setup SYN-ACK */
1150 * Record that we sent the ECN-setup and default to
1153 tp
->ecn_flags
|= (TE_SETUPSENT
| TE_SENDIPECT
);
1157 * We sent an ECN-setup SYN-ACK but it was dropped.
1158 * Fallback to non-ECN-setup SYN-ACK and clear flag
1159 * that to indicate we should not send data with IP ECT set.
1161 * Pretend we didn't receive an ECN-setup SYN.
1163 tp
->ecn_flags
&= ~TE_SETUPRECEIVED
;
1167 else if (tcp_ecn_outbound
&& (flags
& (TH_SYN
| TH_ACK
)) == TH_SYN
) {
1168 if ((tp
->ecn_flags
& TE_SETUPSENT
) == 0) {
1169 /* Setting TH_ECE and TH_CWR makes this an ECN-setup SYN */
1170 flags
|= (TH_ECE
| TH_CWR
);
1173 * Record that we sent the ECN-setup and default to
1176 tp
->ecn_flags
|= (TE_SETUPSENT
| TE_SENDIPECT
);
1180 * We sent an ECN-setup SYN but it was dropped.
1181 * Fall back to no ECN and clear flag indicating
1182 * we should send data with IP ECT set.
1184 tp
->ecn_flags
&= ~TE_SENDIPECT
;
1189 * Check if we should set the TCP CWR flag.
1190 * CWR flag is sent when we reduced the congestion window because
1191 * we received a TCP ECE or we performed a fast retransmit. We
1192 * never set the CWR flag on retransmitted packets. We only set
1193 * the CWR flag on data packets. Pure acks don't have this set.
1195 if ((tp
->ecn_flags
& TE_SENDCWR
) != 0 && len
!= 0 &&
1196 !SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) && !sack_rxmit
) {
1198 tp
->ecn_flags
&= ~TE_SENDCWR
;
1202 * Check if we should set the TCP ECE flag.
1204 if ((tp
->ecn_flags
& TE_SENDECE
) != 0 && len
== 0) {
1209 * Send a timestamp and echo-reply if this is a SYN and our side
1210 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
1211 * and our peer have sent timestamps in our SYN's.
1213 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
1214 (flags
& TH_RST
) == 0 &&
1215 ((flags
& TH_ACK
) == 0 ||
1216 (tp
->t_flags
& TF_RCVD_TSTMP
))) {
1217 u_int32_t
*lp
= (u_int32_t
*)(void *)(opt
+ optlen
);
1219 /* Form timestamp option as shown in appendix A of RFC 1323. */
1220 *lp
++ = htonl(TCPOPT_TSTAMP_HDR
);
1221 *lp
++ = htonl(tcp_now
);
1222 *lp
= htonl(tp
->ts_recent
);
1223 optlen
+= TCPOLEN_TSTAMP_APPA
;
1226 /* Note the timestamp for receive buffer autosizing */
1227 if (tp
->rfbuf_ts
== 0 && (so
->so_rcv
.sb_flags
& SB_AUTOSIZE
))
1228 tp
->rfbuf_ts
= tcp_now
;
1230 if (SACK_ENABLED(tp
) && ((tp
->t_flags
& TF_NOOPT
) == 0)) {
1232 * Tack on the SACK permitted option *last*.
1233 * And do padding of options after tacking this on.
1234 * This is because of MSS, TS, WinScale and Signatures are
1235 * all present, we have just 2 bytes left for the SACK
1236 * permitted option, which is just enough.
1239 * If this is the first SYN of connection (not a SYN
1240 * ACK), include SACK permitted option. If this is a
1241 * SYN ACK, include SACK permitted option if peer has
1242 * already done so. This is only for active connect,
1243 * since the syncache takes care of the passive connect.
1245 if ((flags
& TH_SYN
) &&
1246 (!(flags
& TH_ACK
) || (tp
->t_flags
& TF_SACK_PERMIT
))) {
1248 bp
= (u_char
*)opt
+ optlen
;
1250 *bp
++ = TCPOPT_SACK_PERMITTED
;
1251 *bp
++ = TCPOLEN_SACK_PERMITTED
;
1252 optlen
+= TCPOLEN_SACK_PERMITTED
;
1256 if (so
->so_flags
& SOF_MP_SUBFLOW
) {
1258 * Its important to piggyback acks with data as ack only packets
1259 * may get lost and data packets that don't send Data ACKs
1260 * still advance the subflow level ACK and therefore make it
1261 * hard for the remote end to recover in low cwnd situations.
1264 tp
->t_mpflags
|= (TMPF_SEND_DSN
|
1267 tp
->t_mpflags
|= TMPF_MPTCP_ACKNOW
;
1268 optlen
= mptcp_setup_opts(tp
, off
, &opt
[0], optlen
, flags
,
1269 len
, &dlenp
, &finp
, &dss_val
, &sseqp
);
1270 tp
->t_mpflags
&= ~TMPF_SEND_DSN
;
1274 if (SACK_ENABLED(tp
) && ((tp
->t_flags
& TF_NOOPT
) == 0)) {
1276 * Send SACKs if necessary. This should be the last
1277 * option processed. Only as many SACKs are sent as
1278 * are permitted by the maximum options size.
1280 * In general, SACK blocks consume 8*n+2 bytes.
1281 * So a full size SACK blocks option is 34 bytes
1282 * (to generate 4 SACK blocks). At a minimum,
1283 * we need 10 bytes (to generate 1 SACK block).
1284 * If TCP Timestamps (12 bytes) and TCP Signatures
1285 * (18 bytes) are both present, we'll just have
1286 * 10 bytes for SACK options 40 - (12 + 18).
1288 if (TCPS_HAVEESTABLISHED(tp
->t_state
) &&
1289 (tp
->t_flags
& TF_SACK_PERMIT
) && tp
->rcv_numsacks
> 0 &&
1290 MAX_TCPOPTLEN
- optlen
- 2 >= TCPOLEN_SACK
) {
1292 u_char
*bp
= (u_char
*)opt
+ optlen
;
1295 nsack
= (MAX_TCPOPTLEN
- optlen
- 2) / TCPOLEN_SACK
;
1296 nsack
= min(nsack
, tp
->rcv_numsacks
);
1297 sackoptlen
= (2 + nsack
* TCPOLEN_SACK
);
1300 * First we need to pad options so that the
1301 * SACK blocks can start at a 4-byte boundary
1302 * (sack option and length are at a 2 byte offset).
1304 padlen
= (MAX_TCPOPTLEN
- optlen
- sackoptlen
) % 4;
1306 while (padlen
-- > 0)
1309 tcpstat
.tcps_sack_send_blocks
++;
1310 *bp
++ = TCPOPT_SACK
;
1312 lp
= (u_int32_t
*)(void *)bp
;
1313 for (i
= 0; i
< nsack
; i
++) {
1314 struct sackblk sack
= tp
->sackblks
[i
];
1315 *lp
++ = htonl(sack
.start
);
1316 *lp
++ = htonl(sack
.end
);
1318 optlen
+= sackoptlen
;
1322 /* Pad TCP options to a 4 byte boundary */
1323 if (optlen
< MAX_TCPOPTLEN
&& (optlen
% sizeof(u_int32_t
))) {
1324 int pad
= sizeof(u_int32_t
) - (optlen
% sizeof(u_int32_t
));
1325 u_char
*bp
= (u_char
*)opt
+ optlen
;
1338 ipoptlen
= ip6_optlen(inp
);
1342 if (tp_inp_options
) {
1343 ipoptlen
= tp_inp_options
->m_len
-
1344 offsetof(struct ipoption
, ipopt_list
);
1349 ipoptlen
+= ipsec_optlen
;
1353 * Adjust data length if insertion of options will
1354 * bump the packet length beyond the t_maxopd length.
1355 * Clear the FIN bit because we cut off the tail of
1358 * When doing TSO limit a burst to TCP_MAXWIN minus the
1359 * IP, TCP and Options length to keep ip->ip_len from
1360 * overflowing. Prevent the last segment from being
1361 * fractional thus making them all equal sized and set
1362 * the flag to continue sending. TSO is disabled when
1363 * IP options or IPSEC are present.
1365 if (len
+ optlen
+ ipoptlen
> tp
->t_maxopd
) {
1367 * If there is still more to send, don't close the connection.
1373 tso_maxlen
= tp
->tso_max_segment_size
? tp
->tso_max_segment_size
: TCP_MAXWIN
;
1375 if (len
> tso_maxlen
- hdrlen
- optlen
) {
1376 len
= tso_maxlen
- hdrlen
- optlen
;
1377 len
= len
- (len
% (tp
->t_maxopd
- optlen
));
1379 } else if (tp
->t_flags
& TF_NEEDFIN
)
1382 len
= tp
->t_maxopd
- optlen
- ipoptlen
;
1387 /* Adjust the length in the DSS option, if it is lesser than len */
1390 * To test this path without SACK, artificially
1391 * decrement len with something like
1395 if (ntohs(*dlenp
) > len
) {
1396 *dlenp
= htons(len
);
1397 /* Unset the FIN flag, if len was adjusted */
1406 if (max_linkhdr
+ hdrlen
> MCLBYTES
)
1407 panic("tcphdr too big");
1409 /* Check if there is enough data in the send socket
1410 * buffer to start measuring bw
1412 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
1413 (tp
->t_bwmeas
!= NULL
) &&
1414 (tp
->t_flagsext
& TF_BWMEAS_INPROGRESS
) == 0 &&
1415 (so
->so_snd
.sb_cc
- (tp
->snd_max
- tp
->snd_una
)) >=
1416 tp
->t_bwmeas
->bw_minsize
) {
1417 tp
->t_bwmeas
->bw_size
= min((so
->so_snd
.sb_cc
- (tp
->snd_max
- tp
->snd_una
)),
1418 tp
->t_bwmeas
->bw_maxsize
);
1419 tp
->t_flagsext
|= TF_BWMEAS_INPROGRESS
;
1420 tp
->t_bwmeas
->bw_start
= tp
->snd_max
;
1421 tp
->t_bwmeas
->bw_ts
= tcp_now
;
1424 VERIFY(inp
->inp_flowhash
!= 0);
1426 * Grab a header mbuf, attaching a copy of data to
1427 * be transmitted, and initialize the header from
1428 * the template for sends on this connection.
1431 if (tp
->t_force
&& len
== 1)
1432 tcpstat
.tcps_sndprobe
++;
1433 else if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) || sack_rxmit
) {
1434 tcpstat
.tcps_sndrexmitpack
++;
1435 tcpstat
.tcps_sndrexmitbyte
+= len
;
1436 if (nstat_collect
) {
1437 nstat_route_tx(inp
->inp_route
.ro_rt
, 1,
1438 len
, NSTAT_TX_FLAG_RETRANSMIT
);
1439 INP_ADD_STAT(inp
, cell
, wifi
, txpackets
, 1);
1440 INP_ADD_STAT(inp
, cell
, wifi
, txbytes
, len
);
1441 tp
->t_stat
.txretransmitbytes
+= len
;
1444 tcpstat
.tcps_sndpack
++;
1445 tcpstat
.tcps_sndbyte
+= len
;
1447 if (nstat_collect
) {
1448 INP_ADD_STAT(inp
, cell
, wifi
, txpackets
, 1);
1449 INP_ADD_STAT(inp
, cell
, wifi
, txbytes
, len
);
1453 if (tp
->t_mpflags
& TMPF_MPTCP_TRUE
) {
1454 tcpstat
.tcps_mp_sndpacks
++;
1455 tcpstat
.tcps_mp_sndbytes
+= len
;
1459 * try to use the new interface that allocates all
1460 * the necessary mbuf hdrs under 1 mbuf lock and
1461 * avoids rescanning the socket mbuf list if
1462 * certain conditions are met. This routine can't
1463 * be used in the following cases...
1464 * 1) the protocol headers exceed the capacity of
1465 * of a single mbuf header's data area (no cluster attached)
1466 * 2) the length of the data being transmitted plus
1467 * the protocol headers fits into a single mbuf header's
1468 * data area (no cluster attached)
1472 /* minimum length we are going to allocate */
1473 allocated_len
= MHLEN
;
1474 if (MHLEN
< hdrlen
+ max_linkhdr
) {
1475 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
1480 MCLGET(m
, M_DONTWAIT
);
1481 if ((m
->m_flags
& M_EXT
) == 0) {
1486 m
->m_data
+= max_linkhdr
;
1488 allocated_len
= MCLBYTES
;
1490 if (len
<= allocated_len
- hdrlen
- max_linkhdr
) {
1492 VERIFY(allocated_len
<= MHLEN
);
1493 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
1498 m
->m_data
+= max_linkhdr
;
1501 /* makes sure we still have data left to be sent at this point */
1502 if (so
->so_snd
.sb_mb
== NULL
|| off
< 0) {
1503 if (m
!= NULL
) m_freem(m
);
1504 error
= 0; /* should we return an error? */
1507 m_copydata(so
->so_snd
.sb_mb
, off
, (int) len
,
1508 mtod(m
, caddr_t
) + hdrlen
);
1513 * Retain packet header metadata at the socket
1514 * buffer if this is is an MPTCP subflow,
1515 * otherwise move it.
1517 copymode
= M_COPYM_MOVE_HDR
;
1519 if ((tp
->t_mpflags
& TMPF_MPTCP_TRUE
) ||
1520 (tp
->t_mpflags
& TMPF_TCP_FALLBACK
)) {
1521 copymode
= M_COPYM_NOOP_HDR
;
1525 m
->m_next
= m_copym_mode(so
->so_snd
.sb_mb
, off
,
1526 (int) len
, M_DONTWAIT
, copymode
);
1527 if (m
->m_next
== NULL
) {
1534 * determine whether the mbuf pointer and
1535 * offset passed back by the 'last' call to
1536 * m_copym_with_hdrs are still valid... if the
1537 * head of the socket chain has changed (due
1538 * to an incoming ACK for instance), or the
1539 * offset into the chain we just computed is
1540 * different from the one last returned by
1541 * m_copym_with_hdrs (perhaps we're re-
1542 * transmitting a packet sent earlier), then
1543 * we can't pass the mbuf pointer and offset
1544 * into it as valid hints for m_copym_with_hdrs
1545 * to use (if valid, these hints allow
1546 * m_copym_with_hdrs to avoid rescanning from
1547 * the beginning of the socket buffer mbuf list.
1549 * Setting the mbuf pointer to NULL is
1550 * sufficient to disable the hint mechanism.
1552 if (m_head
!= so
->so_snd
.sb_mb
|| sack_rxmit
||
1555 last_off
= off
+ len
;
1556 m_head
= so
->so_snd
.sb_mb
;
1559 * make sure we still have data left
1560 * to be sent at this point
1562 if (m_head
== NULL
) {
1563 error
= 0; /* should we return an error? */
1568 * m_copym_with_hdrs will always return the
1569 * last mbuf pointer and the offset into it that
1570 * it acted on to fullfill the current request,
1571 * whether a valid 'hint' was passed in or not.
1573 if ((m
= m_copym_with_hdrs(so
->so_snd
.sb_mb
,
1574 off
, len
, M_DONTWAIT
, &m_lastm
, &m_off
,
1575 copymode
)) == NULL
) {
1579 m
->m_data
+= max_linkhdr
;
1584 * If we're sending everything we've got, set PUSH.
1585 * (This will keep happy those implementations which only
1586 * give data to the user when a buffer fills or
1589 if (off
+ len
== so
->so_snd
.sb_cc
)
1592 if (tp
->t_flags
& TF_ACKNOW
)
1593 tcpstat
.tcps_sndacks
++;
1594 else if (flags
& (TH_SYN
|TH_FIN
|TH_RST
))
1595 tcpstat
.tcps_sndctrl
++;
1596 else if (SEQ_GT(tp
->snd_up
, tp
->snd_una
))
1597 tcpstat
.tcps_sndurg
++;
1599 tcpstat
.tcps_sndwinup
++;
1601 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1606 if (MHLEN
< (hdrlen
+ max_linkhdr
)) {
1607 MCLGET(m
, M_DONTWAIT
);
1608 if ((m
->m_flags
& M_EXT
) == 0) {
1614 m
->m_data
+= max_linkhdr
;
1617 m
->m_pkthdr
.rcvif
= 0;
1619 /* Before opt is copied to the mbuf, set the csum field */
1620 mptcp_output_csum(tp
, m
, len
, hdrlen
, dss_val
, sseqp
);
1623 mac_mbuf_label_associate_inpcb(inp
, m
);
1627 ip6
= mtod(m
, struct ip6_hdr
*);
1628 th
= (struct tcphdr
*)(void *)(ip6
+ 1);
1629 tcp_fillheaders(tp
, ip6
, th
);
1630 if ((tp
->ecn_flags
& TE_SENDIPECT
) != 0 && len
&&
1631 !SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) && !sack_rxmit
) {
1632 ip6
->ip6_flow
|= htonl(IPTOS_ECN_ECT0
<< 20);
1634 svc_flags
|= PKT_SCF_IPV6
;
1638 ip
= mtod(m
, struct ip
*);
1639 ipov
= (struct ipovly
*)ip
;
1640 th
= (struct tcphdr
*)(void *)(ip
+ 1);
1641 /* this picks up the pseudo header (w/o the length) */
1642 tcp_fillheaders(tp
, ip
, th
);
1643 if ((tp
->ecn_flags
& TE_SENDIPECT
) != 0 && len
&&
1644 !SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) && !sack_rxmit
) {
1645 ip
->ip_tos
= IPTOS_ECN_ECT0
;
1650 * Fill in fields, remembering maximum advertised
1651 * window for use in delaying messages about window sizes.
1652 * If resending a FIN, be sure not to use a new sequence number.
1654 if (flags
& TH_FIN
&& (tp
->t_flags
& TF_SENTFIN
) &&
1655 tp
->snd_nxt
== tp
->snd_max
)
1658 * If we are doing retransmissions, then snd_nxt will
1659 * not reflect the first unsent octet. For ACK only
1660 * packets, we do not want the sequence number of the
1661 * retransmitted packet, we want the sequence number
1662 * of the next unsent octet. So, if there is no data
1663 * (and no SYN or FIN), use snd_max instead of snd_nxt
1664 * when filling in ti_seq. But if we are in persist
1665 * state, snd_max might reflect one byte beyond the
1666 * right edge of the window, so use snd_nxt in that
1667 * case, since we know we aren't doing a retransmission.
1668 * (retransmit and persist are mutually exclusive...)
1670 if (sack_rxmit
== 0) {
1671 if (len
|| (flags
& (TH_SYN
|TH_FIN
)) || tp
->t_timer
[TCPT_PERSIST
])
1672 th
->th_seq
= htonl(tp
->snd_nxt
);
1674 th
->th_seq
= htonl(tp
->snd_max
);
1676 th
->th_seq
= htonl(p
->rxmit
);
1678 tp
->sackhint
.sack_bytes_rexmit
+= len
;
1680 th
->th_ack
= htonl(tp
->rcv_nxt
);
1681 tp
->last_ack_sent
= tp
->rcv_nxt
;
1684 bcopy(opt
, th
+ 1, optlen
);
1685 th
->th_off
= (sizeof (struct tcphdr
) + optlen
) >> 2;
1687 th
->th_flags
= flags
;
1689 * Calculate receive window. Don't shrink window,
1690 * but avoid silly window syndrome.
1692 if (recwin
< (int32_t)(so
->so_rcv
.sb_hiwat
/ 4) && recwin
< (int)tp
->t_maxseg
)
1694 if (recwin
< (int32_t)(tp
->rcv_adv
- tp
->rcv_nxt
))
1695 recwin
= (int32_t)(tp
->rcv_adv
- tp
->rcv_nxt
);
1696 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0) {
1697 if (recwin
> (int32_t)slowlink_wsize
)
1698 recwin
= slowlink_wsize
;
1702 if (tcp_recv_bg
== 1 || IS_TCP_RECV_BG(so
)) {
1703 if (tcp_recv_throttle(tp
)) {
1704 uint32_t min_iaj_win
=
1705 tcp_min_iaj_win
* tp
->t_maxseg
;
1706 if (tp
->iaj_rwintop
== 0 ||
1707 SEQ_LT(tp
->iaj_rwintop
, tp
->rcv_adv
))
1708 tp
->iaj_rwintop
= tp
->rcv_adv
;
1709 if (SEQ_LT(tp
->iaj_rwintop
,
1710 tp
->rcv_nxt
+ min_iaj_win
))
1711 tp
->iaj_rwintop
= tp
->rcv_nxt
+ min_iaj_win
;
1712 recwin
= min(tp
->iaj_rwintop
- tp
->rcv_nxt
, recwin
);
1715 #endif /* TRAFFIC_MGT */
1717 if (recwin
> (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
))
1718 recwin
= (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
);
1719 th
->th_win
= htons((u_short
) (recwin
>>tp
->rcv_scale
));
1722 * Adjust the RXWIN0SENT flag - indicate that we have advertised
1723 * a 0 window. This may cause the remote transmitter to stall. This
1724 * flag tells soreceive() to disable delayed acknowledgements when
1725 * draining the buffer. This can occur if the receiver is attempting
1726 * to read more data then can be buffered prior to transmitting on
1729 if (th
->th_win
== 0)
1730 tp
->t_flags
|= TF_RXWIN0SENT
;
1732 tp
->t_flags
&= ~TF_RXWIN0SENT
;
1733 if (SEQ_GT(tp
->snd_up
, tp
->snd_nxt
)) {
1734 th
->th_urp
= htons((u_short
)(tp
->snd_up
- tp
->snd_nxt
));
1735 th
->th_flags
|= TH_URG
;
1738 * If no urgent pointer to send, then we pull
1739 * the urgent pointer to the left edge of the send window
1740 * so that it doesn't drift into the send window on sequence
1741 * number wraparound.
1743 tp
->snd_up
= tp
->snd_una
; /* drag it along */
1747 * Put TCP length in extended header, and then
1748 * checksum extended header and data.
1750 m
->m_pkthdr
.len
= hdrlen
+ len
; /* in6_cksum() need this */
1754 * ip6_plen is not need to be filled now, and will be filled
1757 m
->m_pkthdr
.csum_flags
= CSUM_TCPIPV6
;
1758 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
1760 th
->th_sum
= in_addword(th
->th_sum
,
1761 htons((u_short
)(optlen
+ len
)));
1766 m
->m_pkthdr
.csum_flags
= CSUM_TCP
;
1767 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
1769 th
->th_sum
= in_addword(th
->th_sum
,
1770 htons((u_short
)(optlen
+ len
)));
1774 * Enable TSO and specify the size of the segments.
1775 * The TCP pseudo header checksum is always provided.
1780 m
->m_pkthdr
.csum_flags
|= CSUM_TSO_IPV6
;
1783 m
->m_pkthdr
.csum_flags
|= CSUM_TSO_IPV4
;
1785 m
->m_pkthdr
.tso_segsz
= tp
->t_maxopd
- optlen
;
1787 m
->m_pkthdr
.tso_segsz
= 0;
1791 * In transmit state, time the transmission and arrange for
1792 * the retransmit. In persist state, just set snd_max.
1794 if (tp
->t_force
== 0 || tp
->t_timer
[TCPT_PERSIST
] == 0) {
1795 tcp_seq startseq
= tp
->snd_nxt
;
1798 * Advance snd_nxt over sequence space of this segment.
1800 if (flags
& (TH_SYN
|TH_FIN
)) {
1803 if (flags
& TH_FIN
) {
1805 tp
->t_flags
|= TF_SENTFIN
;
1811 if (SEQ_GT(tp
->snd_nxt
, tp
->snd_max
)) {
1812 tp
->snd_max
= tp
->snd_nxt
;
1814 * Time this transmission if not a retransmission and
1815 * not currently timing anything.
1817 if (tp
->t_rtttime
== 0) {
1818 tp
->t_rtttime
= tcp_now
;
1819 tp
->t_rtseq
= startseq
;
1820 tcpstat
.tcps_segstimed
++;
1825 * Set retransmit timer if not currently set,
1826 * and not doing an ack or a keep-alive probe.
1827 * Initial value for retransmit timer is smoothed
1828 * round-trip time + 2 * round-trip time variance.
1829 * Initialize shift counter which is used for backoff
1830 * of retransmit time.
1833 if (tp
->t_timer
[TCPT_REXMT
] == 0 &&
1834 ((sack_rxmit
&& tp
->snd_nxt
!= tp
->snd_max
) ||
1835 tp
->snd_nxt
!= tp
->snd_una
)) {
1836 if (tp
->t_timer
[TCPT_PERSIST
]) {
1837 tp
->t_timer
[TCPT_PERSIST
] = 0;
1840 tp
->t_persist_stop
= 0;
1842 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
1846 * Persist case, update snd_max but since we are in
1847 * persist mode (no window) we do not update snd_nxt.
1852 if (flags
& TH_FIN
) {
1854 tp
->t_flags
|= TF_SENTFIN
;
1856 if (SEQ_GT(tp
->snd_nxt
+ xlen
, tp
->snd_max
))
1857 tp
->snd_max
= tp
->snd_nxt
+ len
;
1864 if (so_options
& SO_DEBUG
)
1865 tcp_trace(TA_OUTPUT
, tp
->t_state
, tp
, mtod(m
, void *), th
, 0);
1869 * Fill in IP length and desired time to live and
1870 * send to IP level. There should be a better way
1871 * to handle ttl and tos; we could keep them in
1872 * the template, but need a way to checksum without them.
1876 * m->m_pkthdr.len should have been set before cksum calcuration,
1877 * because in6_cksum() need it.
1881 * we separately set hoplimit for every segment, since the
1882 * user might want to change the value via setsockopt.
1883 * Also, desired default hop limit might be changed via
1884 * Neighbor Discovery.
1886 ip6
->ip6_hlim
= in6_selecthlim(inp
, inp
->in6p_route
.ro_rt
?
1887 inp
->in6p_route
.ro_rt
->rt_ifp
: NULL
);
1889 /* TODO: IPv6 IP6TOS_ECT bit on */
1890 KERNEL_DEBUG(DBG_LAYER_BEG
,
1891 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
1892 (((inp
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
1893 (inp
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
1898 ip
->ip_len
= m
->m_pkthdr
.len
;
1899 ip
->ip_ttl
= inp
->inp_ip_ttl
; /* XXX */
1900 ip
->ip_tos
|= (inp
->inp_ip_tos
& ~IPTOS_ECN_MASK
);/* XXX */
1901 KERNEL_DEBUG(DBG_LAYER_BEG
,
1902 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
1903 (((inp
->inp_laddr
.s_addr
& 0xffff) << 16) |
1904 (inp
->inp_faddr
.s_addr
& 0xffff)), 0,0,0);
1908 * See if we should do MTU discovery.
1909 * Look at the flag updated on the following criterias:
1910 * 1) Path MTU discovery is authorized by the sysctl
1911 * 2) The route isn't set yet (unlikely but could happen)
1912 * 3) The route is up
1913 * 4) the MTU is not locked (if it is, then discovery has been
1914 * disabled for that route)
1919 if (path_mtu_discovery
&& (tp
->t_flags
& TF_PMTUD
))
1920 ip
->ip_off
|= IP_DF
;
1923 if (ipsec_bypass
== 0)
1924 ipsec_setsocket(m
, so
);
1928 * The socket is kept locked while sending out packets in ip_output, even if packet chaining is not active.
1933 * Embed the flow hash in pkt hdr and mark the packet as
1934 * capable of flow controlling
1936 m
->m_pkthdr
.pkt_flowsrc
= FLOWSRC_INPCB
;
1937 m
->m_pkthdr
.pkt_flowid
= inp
->inp_flowhash
;
1938 m
->m_pkthdr
.pkt_flags
|= PKTF_FLOW_ID
| PKTF_FLOW_LOCALSRC
;
1940 /* Disable flow advisory when using MPTCP. */
1941 if (!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
))
1943 m
->m_pkthdr
.pkt_flags
|= PKTF_FLOW_ADV
;
1944 m
->m_pkthdr
.pkt_proto
= IPPROTO_TCP
;
1946 m
->m_nextpkt
= NULL
;
1948 if (inp
->inp_last_outifp
!= NULL
&&
1949 !(inp
->inp_last_outifp
->if_flags
& IFF_LOOPBACK
)) {
1950 /* Hint to prioritize this packet if
1951 * 1. if the packet has no data
1952 * 2. the interface supports transmit-start model and did
1953 * not disable ACK prioritization.
1954 * 3. Only ACK flag is set.
1955 * 4. there is no outstanding data on this connection.
1957 if (tcp_prioritize_acks
!= 0 && len
== 0 &&
1958 (inp
->inp_last_outifp
->if_eflags
&
1959 (IFEF_TXSTART
| IFEF_NOACKPRI
)) == IFEF_TXSTART
&&
1960 th
->th_flags
== TH_ACK
&& tp
->snd_una
== tp
->snd_max
&&
1961 tp
->t_timer
[TCPT_REXMT
] == 0) {
1962 svc_flags
|= PKT_SCF_TCP_ACK
;
1964 set_packet_service_class(m
, so
, MBUF_SC_UNSPEC
, svc_flags
);
1967 tp
->t_pktlist_sentlen
+= len
;
1972 DTRACE_TCP5(send
, struct mbuf
*, m
, struct inpcb
*, inp
,
1973 struct ip6
*, ip6
, struct tcpcb
*, tp
, struct tcphdr
*,
1978 DTRACE_TCP5(send
, struct mbuf
*, m
, struct inpcb
*, inp
,
1979 struct ip
*, ip
, struct tcpcb
*, tp
, struct tcphdr
*, th
);
1982 if (tp
->t_pktlist_head
!= NULL
) {
1983 tp
->t_pktlist_tail
->m_nextpkt
= m
;
1984 tp
->t_pktlist_tail
= m
;
1986 packchain_newlist
++;
1987 tp
->t_pktlist_head
= tp
->t_pktlist_tail
= m
;
1990 if ((lro_ackmore
) && (!sackoptlen
) && (!tp
->t_timer
[TCPT_PERSIST
]) &&
1991 ((th
->th_flags
& TH_ACK
) == TH_ACK
) && (!len
) &&
1992 (tp
->t_state
== TCPS_ESTABLISHED
)) {
1993 /* For a pure ACK, see if you need to send more of them */
1994 mnext
= tcp_send_lroacks(tp
, m
, th
);
1996 tp
->t_pktlist_tail
->m_nextpkt
= mnext
;
1997 if (mnext
->m_nextpkt
== NULL
) {
1998 tp
->t_pktlist_tail
= mnext
;
2001 struct mbuf
*tail
, *next
;
2002 next
= mnext
->m_nextpkt
;
2003 tail
= next
->m_nextpkt
;
2006 tail
= tail
->m_nextpkt
;
2009 tp
->t_pktlist_tail
= next
;
2014 if (sendalot
== 0 || (tp
->t_state
!= TCPS_ESTABLISHED
) ||
2015 (tp
->snd_cwnd
<= (tp
->snd_wnd
/ 8)) ||
2016 (tp
->t_flags
& (TH_PUSH
| TF_ACKNOW
)) || tp
->t_force
!= 0 ||
2017 tp
->t_lastchain
>= tcp_packet_chaining
) {
2021 * Reset the stack memory of offset as the socket
2025 while (inp
->inp_sndinprog_cnt
== 0 &&
2026 tp
->t_pktlist_head
!= NULL
) {
2027 packetlist
= tp
->t_pktlist_head
;
2028 packchain_listadd
= tp
->t_lastchain
;
2030 lost
= tp
->t_pktlist_sentlen
;
2031 TCP_PKTLIST_CLEAR(tp
);
2033 error
= tcp_ip_output(so
, tp
, packetlist
,
2034 packchain_listadd
, tp_inp_options
,
2035 (so_options
& SO_DONTROUTE
),
2036 (sack_rxmit
| (sack_bytes_rxmt
!= 0)), recwin
,
2045 * Take into account the rest of unsent
2046 * packets in the packet list for this tcp
2047 * into "lost", since we're about to free
2048 * the whole list below.
2050 lost
+= tp
->t_pktlist_sentlen
;
2056 /* tcp was closed while we were in ip; resume close */
2057 if (inp
->inp_sndinprog_cnt
== 0 &&
2058 (tp
->t_flags
& TF_CLOSING
)) {
2059 tp
->t_flags
&= ~TF_CLOSING
;
2060 (void) tcp_close(tp
);
2066 tcpstat
.tcps_sndtotal
++;
2072 * Assume that the packets were lost, so back out the
2073 * sequence number advance, if any. Note that the "lost"
2074 * variable represents the amount of user data sent during
2075 * the recent call to ip_output_list() plus the amount of
2076 * user data in the packet list for this tcp at the moment.
2078 if (tp
->t_force
== 0 || tp
->t_timer
[TCPT_PERSIST
] == 0) {
2080 * No need to check for TH_FIN here because
2081 * the TF_SENTFIN flag handles that case.
2083 if ((flags
& TH_SYN
) == 0) {
2085 if (SEQ_GT((p
->rxmit
- lost
),
2089 lost
= p
->rxmit
- tp
->snd_una
;
2090 p
->rxmit
= tp
->snd_una
;
2092 tp
->sackhint
.sack_bytes_rexmit
-= lost
;
2094 if (SEQ_GT((tp
->snd_nxt
- lost
),
2096 tp
->snd_nxt
-= lost
;
2098 tp
->snd_nxt
= tp
->snd_una
;
2103 if (tp
->t_pktlist_head
!= NULL
)
2104 m_freem_list(tp
->t_pktlist_head
);
2105 TCP_PKTLIST_CLEAR(tp
);
2107 if (error
== ENOBUFS
) {
2108 if (!tp
->t_timer
[TCPT_REXMT
] &&
2109 !tp
->t_timer
[TCPT_PERSIST
])
2110 tp
->t_timer
[TCPT_REXMT
] =
2111 OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
2113 tp
->snd_cwnd
= tp
->t_maxseg
;
2114 tp
->t_bytes_acked
= 0;
2116 tcp_check_timer_state(tp
);
2117 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
2119 DTRACE_TCP5(cc
, void, NULL
, struct inpcb
*, inp
,
2120 struct tcpcb
*, tp
, struct tcphdr
*, NULL
,
2121 int32_t, TCP_CC_OUTPUT_ERROR
);
2124 if (error
== EMSGSIZE
) {
2126 * ip_output() will have already fixed the route
2127 * for us. tcp_mtudisc() will, as its last action,
2128 * initiate retransmission, so it is important to
2131 * If TSO was active we either got an interface
2132 * without TSO capabilits or TSO was turned off.
2133 * Disable it for this connection as too and
2134 * immediatly retry with MSS sized segments generated
2138 tp
->t_flags
&= ~TF_TSO
;
2140 tcp_mtudisc(inp
, 0);
2141 tcp_check_timer_state(tp
);
2143 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
2147 * Unless this is due to interface restriction policy,
2148 * treat EHOSTUNREACH/ENETDOWN as a soft error.
2150 if ((error
== EHOSTUNREACH
|| error
== ENETDOWN
) &&
2151 TCPS_HAVERCVDSYN(tp
->t_state
) &&
2152 !((inp
->inp_flags
& INP_NO_IFT_CELLULAR
) &&
2153 inp
->inp_last_outifp
!= NULL
&&
2154 IFNET_IS_CELLULAR(inp
->inp_last_outifp
))) {
2155 tp
->t_softerror
= error
;
2156 tcp_check_timer_state(tp
);
2157 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
,
2161 tcp_check_timer_state(tp
);
2162 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
2166 tcpstat
.tcps_sndtotal
++;
2168 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
,0,0,0,0,0);
2172 tcp_check_timer_state(tp
);
2177 tcp_ip_output(struct socket
*so
, struct tcpcb
*tp
, struct mbuf
*pkt
,
2178 int cnt
, struct mbuf
*opt
, int flags
, int sack_in_progress
, int recwin
,
2183 boolean_t unlocked
= FALSE
;
2184 boolean_t ifdenied
= FALSE
;
2185 struct inpcb
*inp
= tp
->t_inpcb
;
2186 struct ip_out_args ipoa
=
2187 { IFSCOPE_NONE
, { 0 }, IPOAF_SELECT_SRCIF
|IPOAF_BOUND_SRCADDR
, 0 };
2189 struct ifnet
*outif
= NULL
;
2191 struct ip6_out_args ip6oa
=
2192 { IFSCOPE_NONE
, { 0 }, IP6OAF_SELECT_SRCIF
|IP6OAF_BOUND_SRCADDR
, 0 };
2193 struct route_in6 ro6
;
2194 struct flowadv
*adv
=
2195 (isipv6
? &ip6oa
.ip6oa_flowadv
: &ipoa
.ipoa_flowadv
);
2197 struct flowadv
*adv
= &ipoa
.ipoa_flowadv
;
2200 /* If socket was bound to an ifindex, tell ip_output about it */
2201 if (inp
->inp_flags
& INP_BOUND_IF
) {
2204 ip6oa
.ip6oa_boundif
= inp
->inp_boundifp
->if_index
;
2205 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
2209 ipoa
.ipoa_boundif
= inp
->inp_boundifp
->if_index
;
2210 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
2214 if (inp
->inp_flags
& INP_NO_IFT_CELLULAR
) {
2217 ip6oa
.ip6oa_flags
|= IP6OAF_NO_CELLULAR
;
2220 ipoa
.ipoa_flags
|= IPOAF_NO_CELLULAR
;
2224 flags
|= IPV6_OUTARGS
;
2227 flags
|= IP_OUTARGS
;
2229 /* Copy the cached route and take an extra reference */
2232 in6p_route_copyout(inp
, &ro6
);
2235 inp_route_copyout(inp
, &ro
);
2238 * Data sent (as far as we can tell).
2239 * If this advertises a larger window than any other segment,
2240 * then remember the size of the advertised window.
2241 * Make sure ACK/DELACK conditions are cleared before
2242 * we unlock the socket.
2244 if (recwin
> 0 && SEQ_GT(tp
->rcv_nxt
+ recwin
, tp
->rcv_adv
))
2245 tp
->rcv_adv
= tp
->rcv_nxt
+ recwin
;
2246 tp
->last_ack_sent
= tp
->rcv_nxt
;
2247 tp
->t_flags
&= ~(TF_ACKNOW
| TF_DELACK
);
2248 tp
->t_timer
[TCPT_DELACK
] = 0;
2249 tp
->t_unacksegs
= 0;
2251 /* Increment the count of outstanding send operations */
2252 inp
->inp_sndinprog_cnt
++;
2255 * If allowed, unlock TCP socket while in IP
2256 * but only if the connection is established and
2257 * in a normal mode where reentrancy on the tcpcb won't be
2259 * - there is no SACK episode
2260 * - we're not in Fast Recovery mode
2261 * - if we're not sending from an upcall.
2263 if (tcp_output_unlocked
&& !so
->so_upcallusecount
&&
2264 (tp
->t_state
== TCPS_ESTABLISHED
) && (sack_in_progress
== 0) &&
2265 ((tp
->t_flags
& TF_FASTRECOVERY
) == 0)) {
2268 socket_unlock(so
, 0);
2272 * Don't send down a chain of packets when:
2273 * - TCP chaining is disabled
2274 * - there is an IPsec rule set
2275 * - there is a non default rule set for the firewall
2278 chain
= tcp_packet_chaining
> 1
2283 && (fw_enable
== 0 || fw_bypass
)
2285 ; // I'm important, not extraneous
2288 while (pkt
!= NULL
) {
2289 struct mbuf
*npkt
= pkt
->m_nextpkt
;
2292 pkt
->m_nextpkt
= NULL
;
2294 * If we are not chaining, make sure to set the packet
2295 * list count to 0 so that IP takes the right path;
2296 * this is important for cases such as IPSec where a
2297 * single mbuf might result in multiple mbufs as part
2298 * of the encapsulation. If a non-zero count is passed
2299 * down to IP, the head of the chain might change and
2300 * we could end up skipping it (thus generating bogus
2301 * packets). Fixing it in IP would be desirable, but
2302 * for now this would do it.
2308 error
= ip6_output_list(pkt
, cnt
,
2309 inp
->in6p_outputopts
, &ro6
, flags
, NULL
, NULL
,
2311 ifdenied
= (ip6oa
.ip6oa_retflags
& IP6OARF_IFDENIED
);
2314 error
= ip_output_list(pkt
, cnt
, opt
, &ro
, flags
, NULL
,
2316 ifdenied
= (ipoa
.ipoa_retflags
& IPOARF_IFDENIED
);
2319 if (chain
|| error
) {
2321 * If we sent down a chain then we are done since
2322 * the callee had taken care of everything; else
2323 * we need to free the rest of the chain ourselves.
2336 * Enter flow controlled state if the connection is established
2337 * and is not in recovery.
2339 * A connection will enter suspended state even if it is in
2342 if (((adv
->code
== FADV_FLOW_CONTROLLED
&& !IN_FASTRECOVERY(tp
)) ||
2343 adv
->code
== FADV_SUSPENDED
) &&
2344 !(tp
->t_flags
& TF_CLOSING
) &&
2345 tp
->t_state
== TCPS_ESTABLISHED
) {
2347 rc
= inp_set_fc_state(inp
, adv
->code
);
2350 DTRACE_TCP5(cc
, void, NULL
, struct inpcb
*, inp
,
2351 struct tcpcb
*, tp
, struct tcphdr
*, NULL
,
2352 int32_t, ((adv
->code
== FADV_FLOW_CONTROLLED
) ?
2353 TCP_CC_FLOW_CONTROL
: TCP_CC_SUSPEND
));
2357 * When an interface queue gets suspended, some of the
2358 * packets are dropped. Return ENOBUFS, to update the
2361 if (adv
->code
== FADV_SUSPENDED
)
2364 VERIFY(inp
->inp_sndinprog_cnt
> 0);
2365 if ( --inp
->inp_sndinprog_cnt
== 0)
2366 inp
->inp_flags
&= ~(INP_FC_FEEDBACK
);
2370 if (ro6
.ro_rt
!= NULL
&& (outif
= ro6
.ro_rt
->rt_ifp
) !=
2371 inp
->in6p_last_outifp
)
2372 inp
->in6p_last_outifp
= outif
;
2375 if (ro
.ro_rt
!= NULL
&& (outif
= ro
.ro_rt
->rt_ifp
) !=
2376 inp
->inp_last_outifp
)
2377 inp
->inp_last_outifp
= outif
;
2379 if (error
!= 0 && ifdenied
&& (inp
->inp_flags
& INP_NO_IFT_CELLULAR
))
2380 soevent(inp
->inp_socket
,
2381 (SO_FILT_HINT_LOCKED
|SO_FILT_HINT_IFDENIED
));
2383 /* Synchronize cached PCB route & options */
2386 in6p_route_copyin(inp
, &ro6
);
2389 inp_route_copyin(inp
, &ro
);
2391 if (tp
->t_state
< TCPS_ESTABLISHED
&& tp
->t_rxtshift
== 0 &&
2392 tp
->t_inpcb
->inp_route
.ro_rt
!= NULL
) {
2393 /* If we found the route and there is an rtt on it
2394 * reset the retransmit timer
2396 tcp_getrt_rtt(tp
, tp
->t_inpcb
->in6p_route
.ro_rt
);
2397 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
2404 register struct tcpcb
*tp
;
2406 int t
= ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1;
2408 /* If a PERSIST_TIMER option was set we will limit the
2409 * time the persist timer will be active for that connection
2410 * in order to avoid DOS by using zero window probes.
2411 * see rdar://5805356
2414 if ((tp
->t_persist_timeout
!= 0) &&
2415 (tp
->t_timer
[TCPT_PERSIST
] == 0) &&
2416 (tp
->t_persist_stop
== 0)) {
2417 tp
->t_persist_stop
= tcp_now
+ tp
->t_persist_timeout
;
2421 * Start/restart persistance timer.
2423 TCPT_RANGESET(tp
->t_timer
[TCPT_PERSIST
],
2424 t
* tcp_backoff
[tp
->t_rxtshift
],
2425 TCPTV_PERSMIN
, TCPTV_PERSMAX
, 0);
2426 tp
->t_timer
[TCPT_PERSIST
] = OFFSET_FROM_START(tp
, tp
->t_timer
[TCPT_PERSIST
]);
2428 if (tp
->t_rxtshift
< TCP_MAXRXTSHIFT
)
2433 * Send as many acks as data coalesced. Every other packet when stretch
2434 * ACK is not enabled. Every 8 packets, if stretch ACK is enabled.
2437 tcp_send_lroacks(struct tcpcb
*tp
, struct mbuf
*m
, struct tcphdr
*th
)
2439 struct mbuf
*mnext
= NULL
, *ack_chain
= NULL
, *tail
= NULL
;
2441 tcp_seq org_ack
= ntohl(th
->th_ack
);
2442 tcp_seq prev_ack
= 0;
2443 int tack_offset
= 28; /* XXX IPv6 and IP options not supported */
2444 int twin_offset
= 34; /* XXX IPv6 and IP options not supported */
2445 int ack_size
= (tp
->t_flags
& TF_STRETCHACK
) ?
2446 (maxseg_unacked
* tp
->t_maxseg
) : (tp
->t_maxseg
<< 1);
2447 int segs_acked
= (tp
->t_flags
& TF_STRETCHACK
) ? maxseg_unacked
: 2;
2448 struct mbuf
*prev_ack_pkt
= NULL
;
2449 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
2450 unsigned short winsz
= ntohs(th
->th_win
);
2451 unsigned int scaled_win
= winsz
<<tp
->rcv_scale
;
2452 tcp_seq win_rtedge
= org_ack
+ scaled_win
;
2454 count
= tp
->t_lropktlen
/tp
->t_maxseg
;
2456 prev_ack
= (org_ack
- tp
->t_lropktlen
) + ack_size
;
2457 if (prev_ack
< org_ack
) {
2458 ack_chain
= m_dup(m
, M_DONTWAIT
);
2460 th
->th_ack
= htonl(prev_ack
);
2461 /* Keep adv window constant for duplicated ACK packets */
2462 scaled_win
= win_rtedge
- prev_ack
;
2463 if (scaled_win
> (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
))
2464 scaled_win
= (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
);
2465 th
->th_win
= htons(scaled_win
>>tp
->rcv_scale
);
2466 if (lrodebug
== 5) {
2467 printf("%s: win = %d winsz = %d sc = %d"
2469 __func__
, scaled_win
>>tp
->rcv_scale
, winsz
,
2470 tp
->rcv_scale
, tp
->t_lropktlen
, count
);
2473 count
-= segs_acked
; /* accounts for prev_ack packet */
2474 count
= (count
<= segs_acked
) ? 0 : count
- segs_acked
;
2475 tcpstat
.tcps_sndacks
++;
2476 so_tc_update_stats(m
, so
, m_get_service_class(m
));
2482 tp
->t_lropktlen
= 0;
2486 prev_ack_pkt
= ack_chain
;
2489 if ((prev_ack
+ ack_size
) < org_ack
) {
2490 prev_ack
+= ack_size
;
2493 * The last ACK sent must have the ACK number that TCP
2494 * thinks is the last sent ACK number.
2498 mnext
= m_dup(prev_ack_pkt
, M_DONTWAIT
);
2500 /* Keep adv window constant for duplicated ACK packets */
2501 scaled_win
= win_rtedge
- prev_ack
;
2502 if (scaled_win
> (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
))
2503 scaled_win
= (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
);
2504 winsz
= htons(scaled_win
>>tp
->rcv_scale
);
2505 if (lrodebug
== 5) {
2506 printf("%s: winsz = %d ack %x count %d\n",
2507 __func__
, scaled_win
>>tp
->rcv_scale
,
2510 bcopy(&winsz
, mtod(prev_ack_pkt
, caddr_t
) + twin_offset
, 2);
2512 bcopy(&prev_ack
, mtod(prev_ack_pkt
, caddr_t
) + tack_offset
, 4);
2514 tail
->m_nextpkt
= mnext
;
2516 count
-= segs_acked
;
2517 tcpstat
.tcps_sndacks
++;
2518 so_tc_update_stats(m
, so
, m_get_service_class(m
));
2520 if (lrodebug
== 5) {
2521 printf("%s: failed to alloc mbuf.\n", __func__
);
2525 prev_ack_pkt
= mnext
;
2527 tp
->t_lropktlen
= 0;
2532 tcp_recv_throttle (struct tcpcb
*tp
)
2534 uint32_t base_rtt
, newsize
;
2536 struct sockbuf
*sbrcv
= &tp
->t_inpcb
->inp_socket
->so_rcv
;
2538 if (tcp_use_rtt_recvbg
== 1 &&
2539 TSTMP_SUPPORTED(tp
)) {
2541 * Timestamps are supported on this connection. Use
2542 * RTT to look for an increase in latency.
2546 * If the connection is already being throttled, leave it
2547 * in that state until rtt comes closer to base rtt
2549 if (tp
->t_flagsext
& TF_RECV_THROTTLE
)
2552 base_rtt
= get_base_rtt(tp
);
2554 if (base_rtt
!= 0 && tp
->t_rttcur
!= 0) {
2555 qdelay
= tp
->t_rttcur
- base_rtt
;
2557 * if latency increased on a background flow,
2558 * return 1 to start throttling.
2560 if (qdelay
> target_qdelay
) {
2561 tp
->t_flagsext
|= TF_RECV_THROTTLE
;
2564 * Reduce the recv socket buffer size to
2567 if (sbrcv
->sb_idealsize
>
2568 tcp_recv_throttle_minwin
) {
2569 newsize
= sbrcv
->sb_idealsize
>> 1;
2570 /* Set a minimum of 16 K */
2573 tcp_recv_throttle_minwin
);
2574 sbrcv
->sb_idealsize
= newsize
;
2584 * Timestamps are not supported or there is no good RTT
2585 * measurement. Use IPDV in this case.
2587 if (tp
->acc_iaj
> tcp_acc_iaj_react_limit
)