2 * Copyright (c) 2000-2014 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
,
137 CTLFLAG_RW
| CTLFLAG_LOCKED
, &path_mtu_discovery
, 1,
138 "Enable Path MTU Discovery");
141 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, slowstart_flightsize
,
142 CTLFLAG_RW
| CTLFLAG_LOCKED
,&ss_fltsz
, 1,
143 "Slow start flight size");
145 int ss_fltsz_local
= 8; /* starts with eight segments max */
146 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, local_slowstart_flightsize
,
147 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ss_fltsz_local
, 1,
148 "Slow start flight size for local networks");
151 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tso
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
152 &tcp_do_tso
, 0, "Enable TCP Segmentation Offload");
154 int tcp_ecn_outbound
= 0;
155 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, ecn_initiate_out
,
156 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_ecn_outbound
, 0,
157 "Initiate ECN for outbound connections");
159 int tcp_ecn_inbound
= 0;
160 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, ecn_negotiate_in
,
161 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_ecn_inbound
, 0,
162 "Allow ECN negotiation for inbound connections");
164 int tcp_packet_chaining
= 50;
165 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, packetchain
,
166 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_packet_chaining
, 0,
167 "Enable TCP output packet chaining");
169 int tcp_output_unlocked
= 1;
170 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, socket_unlocked_on_output
,
171 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_output_unlocked
, 0,
172 "Unlock TCP when sending packets down to IP");
174 int tcp_do_rfc3390
= 1;
175 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rfc3390
,
176 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_do_rfc3390
, 1,
177 "Calculate intial slowstart cwnd depending on MSS");
179 int tcp_min_iaj_win
= MIN_IAJ_WIN
;
180 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, min_iaj_win
,
181 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_min_iaj_win
, 1,
182 "Minimum recv win based on inter-packet arrival jitter");
184 int tcp_acc_iaj_react_limit
= ACC_IAJ_REACT_LIMIT
;
185 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, acc_iaj_react_limit
,
186 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_acc_iaj_react_limit
, 1,
187 "Accumulated IAJ when receiver starts to react");
189 uint32_t tcp_do_autosendbuf
= 1;
190 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, doautosndbuf
,
191 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_do_autosendbuf
, 1,
192 "Enable send socket buffer auto-tuning");
194 uint32_t tcp_autosndbuf_inc
= 8 * 1024;
195 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autosndbufinc
,
196 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_autosndbuf_inc
, 1,
197 "Increment in send socket bufffer size");
199 uint32_t tcp_autosndbuf_max
= 512 * 1024;
200 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autosndbufmax
,
201 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_autosndbuf_max
, 1,
202 "Maximum send socket buffer size");
204 uint32_t tcp_prioritize_acks
= 1;
205 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, ack_prioritize
,
206 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_prioritize_acks
, 1,
207 "Prioritize pure acks");
209 uint32_t tcp_use_rtt_recvbg
= 1;
210 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rtt_recvbg
,
211 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_use_rtt_recvbg
, 1,
212 "Use RTT for bg recv algorithm");
214 uint32_t tcp_recv_throttle_minwin
= 16 * 1024;
215 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, recv_throttle_minwin
,
216 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_recv_throttle_minwin
, 1,
217 "Minimum recv win for throttling");
219 int32_t tcp_enable_tlp
= 1;
220 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, enable_tlp
,
221 CTLFLAG_RW
| CTLFLAG_LOCKED
,
222 &tcp_enable_tlp
, 1, "Enable Tail loss probe");
224 static int32_t packchain_newlist
= 0;
225 static int32_t packchain_looped
= 0;
226 static int32_t packchain_sent
= 0;
228 /* temporary: for testing */
230 extern int ipsec_bypass
;
233 extern int slowlink_wsize
; /* window correction for slow links */
235 extern int fw_enable
; /* firewall check for packet chaining */
236 extern int fw_bypass
; /* firewall check: disable packet chaining if there is rules */
237 #endif /* IPFIREWALL */
239 extern u_int32_t dlil_filter_disable_tso_count
;
240 extern u_int32_t kipf_count
;
241 extern int tcp_recv_bg
;
243 static int tcp_ip_output(struct socket
*, struct tcpcb
*, struct mbuf
*, int,
244 struct mbuf
*, int, int, int32_t, boolean_t
);
245 static struct mbuf
* tcp_send_lroacks(struct tcpcb
*tp
, struct mbuf
*m
, struct tcphdr
*th
);
246 static int tcp_recv_throttle(struct tcpcb
*tp
);
249 * Tcp output routine: figure out what should be sent and send it.
257 * ip_output_list:ENOMEM
258 * ip_output_list:EADDRNOTAVAIL
259 * ip_output_list:ENETUNREACH
260 * ip_output_list:EHOSTUNREACH
261 * ip_output_list:EACCES
262 * ip_output_list:EMSGSIZE
263 * ip_output_list:ENOBUFS
264 * ip_output_list:??? [ignorable: mostly IPSEC/firewall/DLIL]
265 * ip6_output_list:EINVAL
266 * ip6_output_list:EOPNOTSUPP
267 * ip6_output_list:EHOSTUNREACH
268 * ip6_output_list:EADDRNOTAVAIL
269 * ip6_output_list:ENETUNREACH
270 * ip6_output_list:EMSGSIZE
271 * ip6_output_list:ENOBUFS
272 * ip6_output_list:??? [ignorable: mostly IPSEC/firewall/DLIL]
275 tcp_output(struct tcpcb
*tp
)
277 struct inpcb
*inp
= tp
->t_inpcb
;
278 struct socket
*so
= inp
->inp_socket
;
279 int32_t len
, recwin
, sendwin
, off
;
282 struct ip
*ip
= NULL
;
283 struct ipovly
*ipov
= NULL
;
285 struct ip6_hdr
*ip6
= NULL
;
288 u_char opt
[TCP_MAXOLEN
];
289 unsigned ipoptlen
, optlen
, hdrlen
;
290 int idle
, sendalot
, lost
= 0;
296 unsigned ipsec_optlen
= 0;
299 struct mbuf
*packetlist
= NULL
;
300 struct mbuf
*tp_inp_options
= inp
->inp_depend4
.inp4_options
;
302 int isipv6
= inp
->inp_vflag
& INP_IPV6
;
304 short packchain_listadd
= 0;
305 int so_options
= so
->so_options
;
307 u_int32_t basertt
, svc_flags
= 0, allocated_len
;
308 u_int32_t lro_ackmore
= (tp
->t_lropktlen
!= 0) ? 1 : 0;
309 struct mbuf
*mnext
= NULL
;
312 unsigned int *dlenp
= NULL
;
313 u_int8_t
*finp
= NULL
;
314 u_int32_t
*sseqp
= NULL
;
315 u_int64_t dss_val
= 0;
316 boolean_t mptcp_acknow
= FALSE
;
317 boolean_t early_data_sent
= FALSE
;
319 boolean_t cell
= FALSE
;
320 boolean_t wifi
= FALSE
;
321 boolean_t wired
= FALSE
;
324 * Determine length of data that should be transmitted,
325 * and flags that will be used.
326 * If there is some data or critical controls (SYN, RST)
327 * to send, then transmit; otherwise, investigate further.
329 idle
= (tp
->t_flags
& TF_LASTIDLE
) || (tp
->snd_max
== tp
->snd_una
);
331 /* Since idle_time is signed integer, the following integer subtraction
332 * will take care of wrap around of tcp_now
334 idle_time
= tcp_now
- tp
->t_rcvtime
;
335 if (idle
&& idle_time
>= TCP_IDLETIMEOUT(tp
)) {
336 if (CC_ALGO(tp
)->after_idle
!= NULL
)
337 CC_ALGO(tp
)->after_idle(tp
);
338 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_IDLE_TIMEOUT
);
340 tp
->t_flags
&= ~TF_LASTIDLE
;
342 if (tp
->t_flags
& TF_MORETOCOME
) {
343 tp
->t_flags
|= TF_LASTIDLE
;
348 if (tp
->t_mpflags
& TMPF_RESET
) {
349 tcp_check_timer_state(tp
);
351 * Once a RST has been sent for an MPTCP subflow,
352 * the subflow socket stays around until deleted.
353 * No packets such as FINs must be sent after RST.
360 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
364 KERNEL_DEBUG(DBG_LAYER_BEG
,
365 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
366 (((inp
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
367 (inp
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
373 KERNEL_DEBUG(DBG_LAYER_BEG
,
374 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
375 (((inp
->inp_laddr
.s_addr
& 0xffff) << 16) |
376 (inp
->inp_faddr
.s_addr
& 0xffff)),
380 * If the route generation id changed, we need to check that our
381 * local (source) IP address is still valid. If it isn't either
382 * return error or silently do nothing (assuming the address will
383 * come back before the TCP connection times out).
385 rt
= inp
->inp_route
.ro_rt
;
386 if (rt
!= NULL
&& ROUTE_UNUSABLE(&tp
->t_inpcb
->inp_route
)) {
388 struct in_ifaddr
*ia
= NULL
;
389 struct in6_ifaddr
*ia6
= NULL
;
390 int found_srcaddr
= 0;
392 /* disable multipages at the socket */
393 somultipages(so
, FALSE
);
395 /* Disable TSO for the socket until we know more */
396 tp
->t_flags
&= ~TF_TSO
;
401 ia6
= ifa_foraddr6(&inp
->in6p_laddr
);
405 ia
= ifa_foraddr(inp
->inp_laddr
.s_addr
);
410 /* check that the source address is still valid */
411 if (found_srcaddr
== 0) {
413 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_NOSRCADDR
));
415 if (tp
->t_state
>= TCPS_CLOSE_WAIT
) {
416 tcp_drop(tp
, EADDRNOTAVAIL
);
417 return(EADDRNOTAVAIL
);
420 /* Set retransmit timer if it wasn't set,
421 * reset Persist timer and shift register as the
422 * advertised peer window may not be valid anymore
425 if (!tp
->t_timer
[TCPT_REXMT
]) {
426 tp
->t_timer
[TCPT_REXMT
] =
427 OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
428 if (tp
->t_timer
[TCPT_PERSIST
]) {
429 tp
->t_timer
[TCPT_PERSIST
] = 0;
431 tp
->t_persist_stop
= 0;
436 if (tp
->t_pktlist_head
!= NULL
)
437 m_freem_list(tp
->t_pktlist_head
);
438 TCP_PKTLIST_CLEAR(tp
);
440 /* drop connection if source address isn't available */
441 if (so
->so_flags
& SOF_NOADDRAVAIL
) {
442 tcp_drop(tp
, EADDRNOTAVAIL
);
443 return(EADDRNOTAVAIL
);
445 tcp_check_timer_state(tp
);
446 return(0); /* silently ignore, keep data in socket: address may be back */
450 IFA_REMREF(&ia
->ia_ifa
);
453 IFA_REMREF(&ia6
->ia_ifa
);
456 * Address is still valid; check for multipages capability
457 * again in case the outgoing interface has changed.
460 if ((ifp
= rt
->rt_ifp
) != NULL
) {
461 somultipages(so
, (ifp
->if_hwassist
& IFNET_MULTIPAGES
));
462 tcp_set_tso(tp
, ifp
);
464 (ifp
->if_eflags
& IFEF_2KCL
));
466 if (rt
->rt_flags
& RTF_UP
)
469 * See if we should do MTU discovery. Don't do it if:
470 * 1) it is disabled via the sysctl
471 * 2) the route isn't up
472 * 3) the MTU is locked (if it is, then discovery
476 if (!path_mtu_discovery
|| ((rt
!= NULL
) &&
477 (!(rt
->rt_flags
& RTF_UP
) ||
478 (rt
->rt_rmx
.rmx_locks
& RTV_MTU
))))
479 tp
->t_flags
&= ~TF_PMTUD
;
481 tp
->t_flags
|= TF_PMTUD
;
487 cell
= IFNET_IS_CELLULAR(rt
->rt_ifp
);
488 wifi
= (!cell
&& IFNET_IS_WIFI(rt
->rt_ifp
));
489 wired
= (!wifi
&& IFNET_IS_WIRED(rt
->rt_ifp
));
493 * If we've recently taken a timeout, snd_max will be greater than
494 * snd_nxt. There may be SACK information that allows us to avoid
495 * resending already delivered data. Adjust snd_nxt accordingly.
497 if (SACK_ENABLED(tp
) && SEQ_LT(tp
->snd_nxt
, tp
->snd_max
))
500 off
= tp
->snd_nxt
- tp
->snd_una
;
501 sendwin
= min(tp
->snd_wnd
, tp
->snd_cwnd
);
503 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0)
504 sendwin
= min(sendwin
, slowlink_wsize
);
506 flags
= tcp_outflags
[tp
->t_state
];
508 * Send any SACK-generated retransmissions. If we're explicitly
509 * trying to send out new data (when sendalot is 1), bypass this
510 * function. If we retransmit in fast recovery mode, decrement
511 * snd_cwnd, since we're replacing a (future) new transmission
512 * with a retransmission now, and we previously incremented
513 * snd_cwnd in tcp_input().
516 * Still in sack recovery , reset rxmit flag to zero.
522 if (SACK_ENABLED(tp
) && IN_FASTRECOVERY(tp
) &&
523 (p
= tcp_sack_output(tp
, &sack_bytes_rxmt
))) {
526 cwin
= min(tp
->snd_wnd
, tp
->snd_cwnd
) - sack_bytes_rxmt
;
529 /* Do not retransmit SACK segments beyond snd_recover */
530 if (SEQ_GT(p
->end
, tp
->snd_recover
)) {
532 * (At least) part of sack hole extends beyond
533 * snd_recover. Check to see if we can rexmit data
536 if (SEQ_GEQ(p
->rxmit
, tp
->snd_recover
)) {
538 * Can't rexmit any more data for this hole.
539 * That data will be rexmitted in the next
540 * sack recovery episode, when snd_recover
541 * moves past p->rxmit.
544 goto after_sack_rexmit
;
546 /* Can rexmit part of the current hole */
547 len
= ((int32_t)min(cwin
,
548 tp
->snd_recover
- p
->rxmit
));
550 len
= ((int32_t)min(cwin
, p
->end
- p
->rxmit
));
553 off
= p
->rxmit
- tp
->snd_una
;
556 tcpstat
.tcps_sack_rexmits
++;
557 tcpstat
.tcps_sack_rexmit_bytes
+=
558 min(len
, tp
->t_maxseg
);
560 nstat_route_tx(inp
->inp_route
.ro_rt
, 1,
561 min(len
, tp
->t_maxseg
),
562 NSTAT_TX_FLAG_RETRANSMIT
);
563 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
565 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
566 txbytes
, min(len
, tp
->t_maxseg
));
567 tp
->t_stat
.txretransmitbytes
+= min(len
, tp
->t_maxseg
);
575 * Get standard flags, and add SYN or FIN if requested by 'hidden'
578 if (tp
->t_flags
& TF_NEEDFIN
)
580 if (tp
->t_flags
& TF_NEEDSYN
)
584 * If in persist timeout with window of 0, send 1 byte.
585 * Otherwise, if window is small but nonzero
586 * and timer expired, we will send what we can
587 * and go to transmit state.
589 if (tp
->t_flagsext
& TF_FORCE
) {
592 * If we still have some data to send, then
593 * clear the FIN bit. Usually this would
594 * happen below when it realizes that we
595 * aren't sending all the data. However,
596 * if we have exactly 1 byte of unsent data,
597 * then it won't clear the FIN bit below,
598 * and if we are in persist state, we wind
599 * up sending the packet without recording
600 * that we sent the FIN bit.
602 * We can't just blindly clear the FIN bit,
603 * because if we don't have any more data
604 * to send then the probe will be the FIN
607 if (off
< so
->so_snd
.sb_cc
)
611 tp
->t_timer
[TCPT_PERSIST
] = 0;
614 tp
->t_persist_stop
= 0;
619 * If snd_nxt == snd_max and we have transmitted a FIN, the
620 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
621 * a negative length. This can also occur when TCP opens up
622 * its congestion window while receiving additional duplicate
623 * acks after fast-retransmit because TCP will reset snd_nxt
624 * to snd_max after the fast-retransmit.
626 * In the normal retransmit-FIN-only case, however, snd_nxt will
627 * be set to snd_una, the offset will be 0, and the length may
630 * If sack_rxmit is true we are retransmitting from the scoreboard
631 * in which case len is already set.
633 if (sack_rxmit
== 0) {
634 if (sack_bytes_rxmt
== 0)
635 len
= min(so
->so_snd
.sb_cc
, sendwin
) - off
;
640 * We are inside of a SACK recovery episode and are
641 * sending new data, having retransmitted all the
642 * data possible in the scoreboard.
644 len
= min(so
->so_snd
.sb_cc
, tp
->snd_wnd
)
647 * Don't remove this (len > 0) check !
648 * We explicitly check for len > 0 here (although it
649 * isn't really necessary), to work around a gcc
650 * optimization issue - to force gcc to compute
651 * len above. Without this check, the computation
652 * of len is bungled by the optimizer.
655 cwin
= tp
->snd_cwnd
-
656 (tp
->snd_nxt
- tp
->sack_newdata
) -
660 len
= imin(len
, cwin
);
668 if ((tp
->t_mpflags
& TMPF_FASTJOIN_SEND
) &&
669 (tp
->t_state
== TCPS_SYN_SENT
) &&
670 (!(tp
->t_flags
& TF_CLOSING
)) &&
671 (so
->so_snd
.sb_cc
!= 0) &&
672 (tp
->t_rxtshift
== 0)) {
676 len
= min(so
->so_snd
.sb_cc
, tp
->t_maxseg
);
677 early_data_sent
= TRUE
;
678 } else if (early_data_sent
) {
679 /* for now, we allow only one data segment to be sent */
684 * Lop off SYN bit if it has already been sent. However, if this
685 * is SYN-SENT state and if segment contains data and if we don't
686 * know that foreign host supports TAO, suppress sending segment.
688 if ((flags
& TH_SYN
) && SEQ_GT(tp
->snd_nxt
, tp
->snd_una
)) {
689 if (tp
->t_state
!= TCPS_SYN_RECEIVED
)
692 if (len
> 0 && tp
->t_state
== TCPS_SYN_SENT
) {
693 while (inp
->inp_sndinprog_cnt
== 0 &&
694 tp
->t_pktlist_head
!= NULL
) {
695 packetlist
= tp
->t_pktlist_head
;
696 packchain_listadd
= tp
->t_lastchain
;
698 TCP_PKTLIST_CLEAR(tp
);
700 error
= tcp_ip_output(so
, tp
, packetlist
,
701 packchain_listadd
, tp_inp_options
,
702 (so_options
& SO_DONTROUTE
),
703 (sack_rxmit
| (sack_bytes_rxmt
!= 0)), 0,
714 * tcp was closed while we were in ip,
717 if (inp
->inp_sndinprog_cnt
== 0 &&
718 (tp
->t_flags
& TF_CLOSING
)) {
719 tp
->t_flags
&= ~TF_CLOSING
;
720 (void) tcp_close(tp
);
722 tcp_check_timer_state(tp
);
724 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
,
731 * Be careful not to send data and/or FIN on SYN segments.
732 * This measure is needed to prevent interoperability problems
733 * with not fully conformant TCP implementations.
735 if ((flags
& TH_SYN
) && (tp
->t_flags
& TF_NOOPT
)) {
741 * The check here used to be (len < 0). Some times len is zero
742 * when the congestion window is closed and we need to check
743 * if persist timer has to be set in that case. But don't set
744 * persist until connection is established.
746 if (len
<= 0 && !(flags
& TH_SYN
)) {
748 * If FIN has been sent but not acked,
749 * but we haven't been called to retransmit,
750 * len will be < 0. Otherwise, window shrank
751 * after we sent into it. If window shrank to 0,
752 * cancel pending retransmit, pull snd_nxt back
753 * to (closed) window, and set the persist timer
754 * if it isn't already going. If the window didn't
755 * close completely, just wait for an ACK.
759 tp
->t_timer
[TCPT_REXMT
] = 0;
760 tp
->t_timer
[TCPT_PTO
] = 0;
763 tp
->snd_nxt
= tp
->snd_una
;
765 if (tp
->t_timer
[TCPT_PERSIST
] == 0)
771 * Automatic sizing of send socket buffer. Increase the send
772 * socket buffer size if all of the following criteria are met
773 * 1. the receiver has enough buffer space for this data
774 * 2. send buffer is filled to 7/8th with data (so we actually
775 * have data to make use of it);
776 * 3. our send window (slow start and congestion controlled) is
777 * larger than sent but unacknowledged data in send buffer.
779 basertt
= get_base_rtt(tp
);
780 if (tcp_do_autosendbuf
== 1 &&
781 !INP_WAIT_FOR_IF_FEEDBACK(inp
) && !IN_FASTRECOVERY(tp
) &&
782 (so
->so_snd
.sb_flags
& (SB_AUTOSIZE
| SB_TRIM
)) == SB_AUTOSIZE
&&
783 tcp_cansbgrow(&so
->so_snd
)) {
784 if ((tp
->snd_wnd
/ 4 * 5) >= so
->so_snd
.sb_hiwat
&&
785 so
->so_snd
.sb_cc
>= (so
->so_snd
.sb_hiwat
/ 8 * 7) &&
786 sendwin
>= (so
->so_snd
.sb_cc
-
787 (tp
->snd_nxt
- tp
->snd_una
))) {
788 /* Also increase the send buffer only if the
789 * round-trip time is not increasing because we do
790 * not want to contribute to latency by filling
792 * We also do not want to hold onto application's
793 * old data for too long. Interactive applications
794 * would rather discard old data.
796 if (tp
->t_rttcur
<= (basertt
+ 25)) {
797 if (sbreserve(&so
->so_snd
,
798 min(so
->so_snd
.sb_hiwat
+ tcp_autosndbuf_inc
,
799 tcp_autosndbuf_max
)) == 1) {
800 so
->so_snd
.sb_idealsize
= so
->so_snd
.sb_hiwat
;
803 so
->so_snd
.sb_idealsize
=
804 max(tcp_sendspace
, so
->so_snd
.sb_hiwat
-
805 (2 * tcp_autosndbuf_inc
));
806 so
->so_snd
.sb_flags
|= SB_TRIM
;
812 * Truncate to the maximum segment length or enable TCP Segmentation
813 * Offloading (if supported by hardware) and ensure that FIN is removed
814 * if the length no longer contains the last data byte.
816 * TSO may only be used if we are in a pure bulk sending state.
817 * The presence of TCP-MD5, SACK retransmits, SACK advertizements,
818 * ipfw rules and IP options, as well as disabling hardware checksum
819 * offload prevent using TSO. With TSO the TCP header is the same
820 * (except for the sequence number) for all generated packets. This
821 * makes it impossible to transmit any options which vary per generated
824 * The length of TSO bursts is limited to TCP_MAXWIN. That limit and
825 * removal of FIN (if not already catched here) are handled later after
826 * the exact length of the TCP options are known.
830 * Pre-calculate here as we save another lookup into the darknesses
831 * of IPsec that way and can actually decide if TSO is ok.
833 if (ipsec_bypass
== 0)
834 ipsec_optlen
= ipsec_hdrsiz_tcp(tp
);
836 if (len
> tp
->t_maxseg
) {
837 if ((tp
->t_flags
& TF_TSO
) && tcp_do_tso
&& hwcksum_tx
&&
838 ip_use_randomid
&& kipf_count
== 0 &&
839 dlil_filter_disable_tso_count
== 0 &&
840 tp
->rcv_numsacks
== 0 && sack_rxmit
== 0 &&
841 sack_bytes_rxmt
== 0 &&
842 inp
->inp_options
== NULL
&&
843 inp
->in6p_options
== NULL
848 && (fw_enable
== 0 || fw_bypass
)
860 /* Send one segment or less as a tail loss probe */
861 if (tp
->t_flagsext
& TF_SENT_TLPROBE
) {
862 len
= min(len
, tp
->t_maxseg
);
868 if ((so
->so_flags
& SOF_MP_SUBFLOW
) &&
869 !(tp
->t_mpflags
& TMPF_TCP_FALLBACK
)) {
871 if (!(tp
->t_mpflags
& TMPF_PREESTABLISHED
) &&
872 (tp
->t_state
> TCPS_CLOSED
) &&
873 ((tp
->t_mpflags
& TMPF_SND_MPPRIO
) ||
874 (tp
->t_mpflags
& TMPF_SND_REM_ADDR
) ||
875 (tp
->t_mpflags
& TMPF_SND_MPFAIL
))) {
882 mptcp_acknow
= FALSE
;
885 * The contiguous bytes in the subflow socket buffer can be
886 * discontiguous at the MPTCP level. Since only one DSS
887 * option can be sent in one packet, reduce length to match
888 * the contiguous MPTCP level. Set sendalot to send remainder.
891 newlen
= mptcp_adj_sendlen(so
, off
, len
);
900 * If the socket is capable of doing unordered send,
901 * pull the amount of data that can be sent from the
902 * unordered priority queues to the serial queue in
903 * the socket buffer. If bytes are not yet available
904 * in the highest priority message, we may not be able
905 * to send any new data.
907 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
909 so
->so_msg_state
->msg_serial_bytes
) {
910 sbpull_unordered_data(so
, off
, len
);
912 /* check if len needs to be modified */
914 so
->so_msg_state
->msg_serial_bytes
) {
915 len
= so
->so_msg_state
->msg_serial_bytes
- off
;
918 tcpstat
.tcps_msg_sndwaithipri
++;
925 if (SEQ_LT(p
->rxmit
+ len
, tp
->snd_una
+ so
->so_snd
.sb_cc
))
928 if (SEQ_LT(tp
->snd_nxt
+ len
, tp
->snd_una
+ so
->so_snd
.sb_cc
))
932 recwin
= tcp_sbspace(tp
);
935 * Sender silly window avoidance. We transmit under the following
936 * conditions when len is non-zero:
938 * - we've timed out (e.g. persist timer)
939 * - we need to retransmit
940 * - We have a full segment (or more with TSO)
941 * - This is the last buffer in a write()/send() and we are
942 * either idle or running NODELAY
943 * - we have more then 1/2 the maximum send window's worth of
944 * data (receiver may be limited the window size)
947 if (tp
->t_flagsext
& TF_FORCE
)
949 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
))
955 * Send new data on the connection only if it is
956 * not flow controlled
958 if (!INP_WAIT_FOR_IF_FEEDBACK(inp
) ||
959 tp
->t_state
!= TCPS_ESTABLISHED
) {
960 if (len
>= tp
->t_maxseg
)
962 if (!(tp
->t_flags
& TF_MORETOCOME
) &&
963 (idle
|| tp
->t_flags
& TF_NODELAY
||
964 tp
->t_flags
& TF_MAXSEGSNT
||
965 ALLOW_LIMITED_TRANSMIT(tp
)) &&
966 (tp
->t_flags
& TF_NOPUSH
) == 0 &&
967 len
+ off
>= so
->so_snd
.sb_cc
)
969 if (len
>= tp
->max_sndwnd
/ 2 && tp
->max_sndwnd
> 0)
972 tcpstat
.tcps_fcholdpacket
++;
977 * Compare available window to amount of window
978 * known to peer (as advertised window less
979 * next expected input). If the difference is at least two
980 * max size segments, or at least 25% of the maximum possible
981 * window, then want to send a window update to peer.
982 * Skip this if the connection is in T/TCP half-open state.
984 if (recwin
> 0 && !(tp
->t_flags
& TF_NEEDSYN
)) {
986 * "adv" is the amount we can increase the window,
987 * taking into account that we are limited by
988 * TCP_MAXWIN << tp->rcv_scale.
990 int32_t adv
, oldwin
= 0;
991 adv
= imin(recwin
, (int)TCP_MAXWIN
<< tp
->rcv_scale
) -
992 (tp
->rcv_adv
- tp
->rcv_nxt
);
994 if (SEQ_GT(tp
->rcv_adv
, tp
->rcv_nxt
))
995 oldwin
= tp
->rcv_adv
- tp
->rcv_nxt
;
997 if (adv
>= (int32_t) (2 * tp
->t_maxseg
)) {
999 * Update only if the resulting scaled value of
1000 * the window changed, or if there is a change in
1001 * the sequence since the last ack. This avoids
1002 * what appears as dupe ACKS (see rdar://5640997)
1004 * If streaming is detected avoid sending too many
1005 * window updates. We will depend on the delack
1006 * timer to send a window update when needed.
1008 if (!(tp
->t_flags
& TF_STRETCHACK
) &&
1009 (tp
->last_ack_sent
!= tp
->rcv_nxt
||
1010 ((oldwin
+ adv
) >> tp
->rcv_scale
) >
1011 (oldwin
>> tp
->rcv_scale
))) {
1016 * Make sure that the delayed ack timer is set if
1017 * we delayed sending a window update because of
1018 * streaming detection.
1020 if ((tp
->t_flags
& TF_STRETCHACK
) &&
1021 !(tp
->t_flags
& TF_DELACK
)) {
1022 tp
->t_flags
|= TF_DELACK
;
1023 tp
->t_timer
[TCPT_DELACK
] =
1024 OFFSET_FROM_START(tp
, tcp_delack
);
1027 if (4 * adv
>= (int32_t) so
->so_rcv
.sb_hiwat
)
1032 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW
1033 * is also a catch-all for the retransmit timer timeout case.
1035 if (tp
->t_flags
& TF_ACKNOW
)
1037 if ((flags
& TH_RST
) ||
1038 ((flags
& TH_SYN
) && (tp
->t_flags
& TF_NEEDSYN
) == 0))
1040 if (SEQ_GT(tp
->snd_up
, tp
->snd_una
))
1047 * If our state indicates that FIN should be sent
1048 * and we have not yet done so, then we need to send.
1050 if ((flags
& TH_FIN
) &&
1051 (!(tp
->t_flags
& TF_SENTFIN
) || tp
->snd_nxt
== tp
->snd_una
))
1054 * In SACK, it is possible for tcp_output to fail to send a segment
1055 * after the retransmission timer has been turned off. Make sure
1056 * that the retransmission timer is set.
1058 if (SACK_ENABLED(tp
) && (tp
->t_state
>= TCPS_ESTABLISHED
) &&
1059 SEQ_GT(tp
->snd_max
, tp
->snd_una
) &&
1060 tp
->t_timer
[TCPT_REXMT
] == 0 &&
1061 tp
->t_timer
[TCPT_PERSIST
] == 0) {
1062 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
,
1067 * TCP window updates are not reliable, rather a polling protocol
1068 * using ``persist'' packets is used to insure receipt of window
1069 * updates. The three ``states'' for the output side are:
1070 * idle not doing retransmits or persists
1071 * persisting to move a small or zero window
1072 * (re)transmitting and thereby not persisting
1074 * tp->t_timer[TCPT_PERSIST]
1075 * is set when we are in persist state.
1077 * is set when we are called to send a persist packet.
1078 * tp->t_timer[TCPT_REXMT]
1079 * is set when we are retransmitting
1080 * The output side is idle when both timers are zero.
1082 * If send window is too small, there is data to transmit, and no
1083 * retransmit or persist is pending, then go to persist state.
1084 * If nothing happens soon, send when timer expires:
1085 * if window is nonzero, transmit what we can,
1086 * otherwise force out a byte.
1088 if (so
->so_snd
.sb_cc
&& tp
->t_timer
[TCPT_REXMT
] == 0 &&
1089 tp
->t_timer
[TCPT_PERSIST
] == 0) {
1096 * If there is no reason to send a segment, just return.
1097 * but if there is some packets left in the packet list, send them now.
1099 while (inp
->inp_sndinprog_cnt
== 0 &&
1100 tp
->t_pktlist_head
!= NULL
) {
1101 packetlist
= tp
->t_pktlist_head
;
1102 packchain_listadd
= tp
->t_lastchain
;
1104 TCP_PKTLIST_CLEAR(tp
);
1106 error
= tcp_ip_output(so
, tp
, packetlist
,
1108 tp_inp_options
, (so_options
& SO_DONTROUTE
),
1109 (sack_rxmit
| (sack_bytes_rxmt
!= 0)), recwin
,
1116 /* tcp was closed while we were in ip; resume close */
1117 if (inp
->inp_sndinprog_cnt
== 0 &&
1118 (tp
->t_flags
& TF_CLOSING
)) {
1119 tp
->t_flags
&= ~TF_CLOSING
;
1120 (void) tcp_close(tp
);
1122 tcp_check_timer_state(tp
);
1124 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1129 * Set TF_MAXSEGSNT flag if the segment size is greater than
1130 * the max segment size.
1133 if (len
>= tp
->t_maxseg
)
1134 tp
->t_flags
|= TF_MAXSEGSNT
;
1136 tp
->t_flags
&= ~TF_MAXSEGSNT
;
1139 * Before ESTABLISHED, force sending of initial options
1140 * unless TCP set not to do any options.
1141 * NOTE: we assume that the IP/TCP header plus TCP options
1142 * always fit in a single mbuf, leaving room for a maximum
1144 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
1149 hdrlen
= sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
);
1152 hdrlen
= sizeof (struct tcpiphdr
);
1153 if (flags
& TH_SYN
) {
1154 tp
->snd_nxt
= tp
->iss
;
1155 if ((tp
->t_flags
& TF_NOOPT
) == 0) {
1158 opt
[0] = TCPOPT_MAXSEG
;
1159 opt
[1] = TCPOLEN_MAXSEG
;
1160 mss
= htons((u_short
) tcp_mssopt(tp
));
1161 (void)memcpy(opt
+ 2, &mss
, sizeof(mss
));
1162 optlen
= TCPOLEN_MAXSEG
;
1164 if ((tp
->t_flags
& TF_REQ_SCALE
) &&
1165 ((flags
& TH_ACK
) == 0 ||
1166 (tp
->t_flags
& TF_RCVD_SCALE
))) {
1167 *((u_int32_t
*)(void *)(opt
+ optlen
)) = htonl(
1169 TCPOPT_WINDOW
<< 16 |
1170 TCPOLEN_WINDOW
<< 8 |
1171 tp
->request_r_scale
);
1176 optlen
= mptcp_setup_syn_opts(so
, flags
, opt
,
1184 * RFC 3168 states that:
1185 * - If you ever sent an ECN-setup SYN/SYN-ACK you must be prepared
1186 * to handle the TCP ECE flag, even if you also later send a
1187 * non-ECN-setup SYN/SYN-ACK.
1188 * - If you ever send a non-ECN-setup SYN/SYN-ACK, you must not set
1191 * It is not clear how the ECE flag would ever be set if you never
1192 * set the IP ECT flag on outbound packets. All the same, we use
1193 * the TE_SETUPSENT to indicate that we have committed to handling
1194 * the TCP ECE flag correctly. We use the TE_SENDIPECT to indicate
1195 * whether or not we should set the IP ECT flag on outbound packet
1197 * For a SYN-ACK, send an ECN setup SYN-ACK
1199 if ((tcp_ecn_inbound
|| (tp
->t_flags
& TF_ENABLE_ECN
))
1200 && (flags
& (TH_SYN
| TH_ACK
)) == (TH_SYN
| TH_ACK
)) {
1201 if ((tp
->ecn_flags
& TE_SETUPRECEIVED
) != 0) {
1202 if ((tp
->ecn_flags
& TE_SETUPSENT
) == 0) {
1203 /* Setting TH_ECE makes this an ECN-setup SYN-ACK */
1207 * Record that we sent the ECN-setup and
1208 * default to setting IP ECT.
1210 tp
->ecn_flags
|= (TE_SETUPSENT
|TE_SENDIPECT
);
1211 tcpstat
.tcps_ecn_setup
++;
1214 * We sent an ECN-setup SYN-ACK but it was
1215 * dropped. Fallback to non-ECN-setup
1216 * SYN-ACK and clear flag to indicate that
1217 * we should not send data with IP ECT set
1219 * Pretend we didn't receive an
1222 tp
->ecn_flags
&= ~TE_SETUPRECEIVED
;
1224 * We already incremented the counter
1225 * assuming that the ECN setup will
1226 * succeed. Decrementing here to
1229 tcpstat
.tcps_ecn_setup
--;
1232 } else if ((tcp_ecn_outbound
|| (tp
->t_flags
& TF_ENABLE_ECN
))
1233 && (flags
& (TH_SYN
| TH_ACK
)) == TH_SYN
) {
1234 if ((tp
->ecn_flags
& TE_SETUPSENT
) == 0) {
1235 /* Setting TH_ECE and TH_CWR makes this an ECN-setup SYN */
1236 flags
|= (TH_ECE
| TH_CWR
);
1239 * Record that we sent the ECN-setup and default to
1242 tp
->ecn_flags
|= (TE_SETUPSENT
| TE_SENDIPECT
);
1245 * We sent an ECN-setup SYN but it was dropped.
1246 * Fall back to no ECN and clear flag indicating
1247 * we should send data with IP ECT set.
1249 tp
->ecn_flags
&= ~TE_SENDIPECT
;
1254 * Check if we should set the TCP CWR flag.
1255 * CWR flag is sent when we reduced the congestion window because
1256 * we received a TCP ECE or we performed a fast retransmit. We
1257 * never set the CWR flag on retransmitted packets. We only set
1258 * the CWR flag on data packets. Pure acks don't have this set.
1260 if ((tp
->ecn_flags
& TE_SENDCWR
) != 0 && len
!= 0 &&
1261 !SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) && !sack_rxmit
) {
1263 tp
->ecn_flags
&= ~TE_SENDCWR
;
1264 tcpstat
.tcps_sent_cwr
++;
1268 * Check if we should set the TCP ECE flag.
1270 if ((tp
->ecn_flags
& TE_SENDECE
) != 0 && len
== 0) {
1272 tcpstat
.tcps_sent_ece
++;
1276 * Send a timestamp and echo-reply if this is a SYN and our side
1277 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
1278 * and our peer have sent timestamps in our SYN's.
1280 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
1281 (flags
& TH_RST
) == 0 &&
1282 ((flags
& TH_ACK
) == 0 ||
1283 (tp
->t_flags
& TF_RCVD_TSTMP
))) {
1284 u_int32_t
*lp
= (u_int32_t
*)(void *)(opt
+ optlen
);
1286 /* Form timestamp option as shown in appendix A of RFC 1323. */
1287 *lp
++ = htonl(TCPOPT_TSTAMP_HDR
);
1288 *lp
++ = htonl(tcp_now
);
1289 *lp
= htonl(tp
->ts_recent
);
1290 optlen
+= TCPOLEN_TSTAMP_APPA
;
1293 /* Note the timestamp for receive buffer autosizing */
1294 if (tp
->rfbuf_ts
== 0 && (so
->so_rcv
.sb_flags
& SB_AUTOSIZE
))
1295 tp
->rfbuf_ts
= tcp_now
;
1297 if (SACK_ENABLED(tp
) && ((tp
->t_flags
& TF_NOOPT
) == 0)) {
1299 * Tack on the SACK permitted option *last*.
1300 * And do padding of options after tacking this on.
1301 * This is because of MSS, TS, WinScale and Signatures are
1302 * all present, we have just 2 bytes left for the SACK
1303 * permitted option, which is just enough.
1306 * If this is the first SYN of connection (not a SYN
1307 * ACK), include SACK permitted option. If this is a
1308 * SYN ACK, include SACK permitted option if peer has
1309 * already done so. This is only for active connect,
1310 * since the syncache takes care of the passive connect.
1312 if ((flags
& TH_SYN
) &&
1313 (!(flags
& TH_ACK
) || (tp
->t_flags
& TF_SACK_PERMIT
))) {
1315 bp
= (u_char
*)opt
+ optlen
;
1317 *bp
++ = TCPOPT_SACK_PERMITTED
;
1318 *bp
++ = TCPOLEN_SACK_PERMITTED
;
1319 optlen
+= TCPOLEN_SACK_PERMITTED
;
1323 if (so
->so_flags
& SOF_MP_SUBFLOW
) {
1325 * Its important to piggyback acks with data as ack only packets
1326 * may get lost and data packets that don't send Data ACKs
1327 * still advance the subflow level ACK and therefore make it
1328 * hard for the remote end to recover in low cwnd situations.
1331 tp
->t_mpflags
|= (TMPF_SEND_DSN
|
1334 tp
->t_mpflags
|= TMPF_MPTCP_ACKNOW
;
1336 optlen
= mptcp_setup_opts(tp
, off
, &opt
[0], optlen
, flags
,
1337 len
, &dlenp
, &finp
, &dss_val
, &sseqp
, &mptcp_acknow
);
1338 tp
->t_mpflags
&= ~TMPF_SEND_DSN
;
1342 if (SACK_ENABLED(tp
) && ((tp
->t_flags
& TF_NOOPT
) == 0)) {
1344 * Send SACKs if necessary. This should be the last
1345 * option processed. Only as many SACKs are sent as
1346 * are permitted by the maximum options size.
1348 * In general, SACK blocks consume 8*n+2 bytes.
1349 * So a full size SACK blocks option is 34 bytes
1350 * (to generate 4 SACK blocks). At a minimum,
1351 * we need 10 bytes (to generate 1 SACK block).
1352 * If TCP Timestamps (12 bytes) and TCP Signatures
1353 * (18 bytes) are both present, we'll just have
1354 * 10 bytes for SACK options 40 - (12 + 18).
1356 if (TCPS_HAVEESTABLISHED(tp
->t_state
) &&
1357 (tp
->t_flags
& TF_SACK_PERMIT
) && tp
->rcv_numsacks
> 0 &&
1358 MAX_TCPOPTLEN
- optlen
- 2 >= TCPOLEN_SACK
) {
1360 u_char
*bp
= (u_char
*)opt
+ optlen
;
1363 nsack
= (MAX_TCPOPTLEN
- optlen
- 2) / TCPOLEN_SACK
;
1364 nsack
= min(nsack
, tp
->rcv_numsacks
);
1365 sackoptlen
= (2 + nsack
* TCPOLEN_SACK
);
1368 * First we need to pad options so that the
1369 * SACK blocks can start at a 4-byte boundary
1370 * (sack option and length are at a 2 byte offset).
1372 padlen
= (MAX_TCPOPTLEN
- optlen
- sackoptlen
) % 4;
1374 while (padlen
-- > 0)
1377 tcpstat
.tcps_sack_send_blocks
++;
1378 *bp
++ = TCPOPT_SACK
;
1380 lp
= (u_int32_t
*)(void *)bp
;
1381 for (i
= 0; i
< nsack
; i
++) {
1382 struct sackblk sack
= tp
->sackblks
[i
];
1383 *lp
++ = htonl(sack
.start
);
1384 *lp
++ = htonl(sack
.end
);
1386 optlen
+= sackoptlen
;
1390 /* Pad TCP options to a 4 byte boundary */
1391 if (optlen
< MAX_TCPOPTLEN
&& (optlen
% sizeof(u_int32_t
))) {
1392 int pad
= sizeof(u_int32_t
) - (optlen
% sizeof(u_int32_t
));
1393 u_char
*bp
= (u_char
*)opt
+ optlen
;
1406 ipoptlen
= ip6_optlen(inp
);
1410 if (tp_inp_options
) {
1411 ipoptlen
= tp_inp_options
->m_len
-
1412 offsetof(struct ipoption
, ipopt_list
);
1418 ipoptlen
+= ipsec_optlen
;
1422 * Adjust data length if insertion of options will
1423 * bump the packet length beyond the t_maxopd length.
1424 * Clear the FIN bit because we cut off the tail of
1427 * When doing TSO limit a burst to TCP_MAXWIN minus the
1428 * IP, TCP and Options length to keep ip->ip_len from
1429 * overflowing. Prevent the last segment from being
1430 * fractional thus making them all equal sized and set
1431 * the flag to continue sending. TSO is disabled when
1432 * IP options or IPSEC are present.
1434 if (len
+ optlen
+ ipoptlen
> tp
->t_maxopd
) {
1436 * If there is still more to send,
1437 * don't close the connection.
1443 tso_maxlen
= tp
->tso_max_segment_size
?
1444 tp
->tso_max_segment_size
: TCP_MAXWIN
;
1446 if (len
> tso_maxlen
- hdrlen
- optlen
) {
1447 len
= tso_maxlen
- hdrlen
- optlen
;
1448 len
= len
- (len
% (tp
->t_maxopd
- optlen
));
1450 } else if (tp
->t_flags
& TF_NEEDFIN
) {
1454 len
= tp
->t_maxopd
- optlen
- ipoptlen
;
1459 /* Adjust the length in the DSS option, if it is lesser than len */
1462 * To test this path without SACK, artificially
1463 * decrement len with something like
1467 if (ntohs(*dlenp
) > len
) {
1468 *dlenp
= htons(len
);
1469 /* Unset the FIN flag, if len was adjusted */
1478 if (max_linkhdr
+ hdrlen
> MCLBYTES
)
1479 panic("tcphdr too big");
1481 /* Check if there is enough data in the send socket
1482 * buffer to start measuring bw
1484 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
1485 (tp
->t_bwmeas
!= NULL
) &&
1486 (tp
->t_flagsext
& TF_BWMEAS_INPROGRESS
) == 0 &&
1487 (so
->so_snd
.sb_cc
- (tp
->snd_max
- tp
->snd_una
)) >=
1488 tp
->t_bwmeas
->bw_minsize
) {
1489 tp
->t_bwmeas
->bw_size
= min(
1490 (so
->so_snd
.sb_cc
- (tp
->snd_max
- tp
->snd_una
)),
1491 tp
->t_bwmeas
->bw_maxsize
);
1492 tp
->t_flagsext
|= TF_BWMEAS_INPROGRESS
;
1493 tp
->t_bwmeas
->bw_start
= tp
->snd_max
;
1494 tp
->t_bwmeas
->bw_ts
= tcp_now
;
1497 VERIFY(inp
->inp_flowhash
!= 0);
1499 * Grab a header mbuf, attaching a copy of data to
1500 * be transmitted, and initialize the header from
1501 * the template for sends on this connection.
1504 if ((tp
->t_flagsext
& TF_FORCE
) && len
== 1)
1505 tcpstat
.tcps_sndprobe
++;
1506 else if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) || sack_rxmit
) {
1507 tcpstat
.tcps_sndrexmitpack
++;
1508 tcpstat
.tcps_sndrexmitbyte
+= len
;
1509 if (nstat_collect
) {
1510 nstat_route_tx(inp
->inp_route
.ro_rt
, 1,
1511 len
, NSTAT_TX_FLAG_RETRANSMIT
);
1512 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
1514 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
1516 tp
->t_stat
.txretransmitbytes
+= len
;
1519 tcpstat
.tcps_sndpack
++;
1520 tcpstat
.tcps_sndbyte
+= len
;
1522 if (nstat_collect
) {
1523 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
1525 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
1530 if (tp
->t_mpflags
& TMPF_MPTCP_TRUE
) {
1531 tcpstat
.tcps_mp_sndpacks
++;
1532 tcpstat
.tcps_mp_sndbytes
+= len
;
1536 * try to use the new interface that allocates all
1537 * the necessary mbuf hdrs under 1 mbuf lock and
1538 * avoids rescanning the socket mbuf list if
1539 * certain conditions are met. This routine can't
1540 * be used in the following cases...
1541 * 1) the protocol headers exceed the capacity of
1542 * of a single mbuf header's data area (no cluster attached)
1543 * 2) the length of the data being transmitted plus
1544 * the protocol headers fits into a single mbuf header's
1545 * data area (no cluster attached)
1549 /* minimum length we are going to allocate */
1550 allocated_len
= MHLEN
;
1551 if (MHLEN
< hdrlen
+ max_linkhdr
) {
1552 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
1557 MCLGET(m
, M_DONTWAIT
);
1558 if ((m
->m_flags
& M_EXT
) == 0) {
1563 m
->m_data
+= max_linkhdr
;
1565 allocated_len
= MCLBYTES
;
1567 if (len
<= allocated_len
- hdrlen
- max_linkhdr
) {
1569 VERIFY(allocated_len
<= MHLEN
);
1570 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
1575 m
->m_data
+= max_linkhdr
;
1578 /* makes sure we still have data left to be sent at this point */
1579 if (so
->so_snd
.sb_mb
== NULL
|| off
< 0) {
1580 if (m
!= NULL
) m_freem(m
);
1581 error
= 0; /* should we return an error? */
1584 m_copydata(so
->so_snd
.sb_mb
, off
, (int) len
,
1585 mtod(m
, caddr_t
) + hdrlen
);
1590 * Retain packet header metadata at the socket
1591 * buffer if this is is an MPTCP subflow,
1592 * otherwise move it.
1594 copymode
= M_COPYM_MOVE_HDR
;
1596 if (so
->so_flags
& SOF_MP_SUBFLOW
) {
1597 copymode
= M_COPYM_NOOP_HDR
;
1601 m
->m_next
= m_copym_mode(so
->so_snd
.sb_mb
,
1602 off
, (int)len
, M_DONTWAIT
, copymode
);
1603 if (m
->m_next
== NULL
) {
1610 * make sure we still have data left
1611 * to be sent at this point
1613 if (so
->so_snd
.sb_mb
== NULL
) {
1614 error
= 0; /* should we return an error? */
1619 * m_copym_with_hdrs will always return the
1620 * last mbuf pointer and the offset into it that
1621 * it acted on to fullfill the current request,
1622 * whether a valid 'hint' was passed in or not.
1624 if ((m
= m_copym_with_hdrs(so
->so_snd
.sb_mb
,
1625 off
, len
, M_DONTWAIT
, NULL
, NULL
,
1626 copymode
)) == NULL
) {
1630 m
->m_data
+= max_linkhdr
;
1635 * If we're sending everything we've got, set PUSH.
1636 * (This will keep happy those implementations which only
1637 * give data to the user when a buffer fills or
1640 if (off
+ len
== so
->so_snd
.sb_cc
)
1643 if (tp
->t_flags
& TF_ACKNOW
)
1644 tcpstat
.tcps_sndacks
++;
1645 else if (flags
& (TH_SYN
|TH_FIN
|TH_RST
))
1646 tcpstat
.tcps_sndctrl
++;
1647 else if (SEQ_GT(tp
->snd_up
, tp
->snd_una
))
1648 tcpstat
.tcps_sndurg
++;
1650 tcpstat
.tcps_sndwinup
++;
1652 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1657 if (MHLEN
< (hdrlen
+ max_linkhdr
)) {
1658 MCLGET(m
, M_DONTWAIT
);
1659 if ((m
->m_flags
& M_EXT
) == 0) {
1665 m
->m_data
+= max_linkhdr
;
1668 m
->m_pkthdr
.rcvif
= 0;
1670 /* Before opt is copied to the mbuf, set the csum field */
1671 mptcp_output_csum(tp
, m
, len
, hdrlen
, dss_val
, sseqp
);
1674 mac_mbuf_label_associate_inpcb(inp
, m
);
1678 ip6
= mtod(m
, struct ip6_hdr
*);
1679 th
= (struct tcphdr
*)(void *)(ip6
+ 1);
1680 tcp_fillheaders(tp
, ip6
, th
);
1681 if ((tp
->ecn_flags
& TE_SENDIPECT
) != 0 && len
&&
1682 !SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) && !sack_rxmit
) {
1683 ip6
->ip6_flow
|= htonl(IPTOS_ECN_ECT0
<< 20);
1685 svc_flags
|= PKT_SCF_IPV6
;
1687 m
->m_pkthdr
.pf_mtag
.pftag_hdr
= (void *)ip6
;
1688 m
->m_pkthdr
.pf_mtag
.pftag_flags
|= PF_TAG_HDR_INET6
;
1693 ip
= mtod(m
, struct ip
*);
1694 ipov
= (struct ipovly
*)ip
;
1695 th
= (struct tcphdr
*)(void *)(ip
+ 1);
1696 /* this picks up the pseudo header (w/o the length) */
1697 tcp_fillheaders(tp
, ip
, th
);
1698 if ((tp
->ecn_flags
& TE_SENDIPECT
) != 0 && len
&&
1699 !SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) && !sack_rxmit
) {
1700 ip
->ip_tos
= IPTOS_ECN_ECT0
;
1703 m
->m_pkthdr
.pf_mtag
.pftag_hdr
= (void *)ip
;
1704 m
->m_pkthdr
.pf_mtag
.pftag_flags
|= PF_TAG_HDR_INET
;
1709 * Fill in fields, remembering maximum advertised
1710 * window for use in delaying messages about window sizes.
1711 * If resending a FIN, be sure not to use a new sequence number.
1713 if (flags
& TH_FIN
&& (tp
->t_flags
& TF_SENTFIN
) &&
1714 tp
->snd_nxt
== tp
->snd_max
)
1717 * If we are doing retransmissions, then snd_nxt will
1718 * not reflect the first unsent octet. For ACK only
1719 * packets, we do not want the sequence number of the
1720 * retransmitted packet, we want the sequence number
1721 * of the next unsent octet. So, if there is no data
1722 * (and no SYN or FIN), use snd_max instead of snd_nxt
1723 * when filling in ti_seq. But if we are in persist
1724 * state, snd_max might reflect one byte beyond the
1725 * right edge of the window, so use snd_nxt in that
1726 * case, since we know we aren't doing a retransmission.
1727 * (retransmit and persist are mutually exclusive...)
1729 if (sack_rxmit
== 0) {
1730 if (len
|| (flags
& (TH_SYN
|TH_FIN
)) || tp
->t_timer
[TCPT_PERSIST
])
1731 th
->th_seq
= htonl(tp
->snd_nxt
);
1733 th
->th_seq
= htonl(tp
->snd_max
);
1735 th
->th_seq
= htonl(p
->rxmit
);
1737 tp
->sackhint
.sack_bytes_rexmit
+= len
;
1739 th
->th_ack
= htonl(tp
->rcv_nxt
);
1740 tp
->last_ack_sent
= tp
->rcv_nxt
;
1742 /* Initialize the ACK field to a value as 0 ack fields are dropped */
1743 if (early_data_sent
) {
1744 th
->th_ack
= th
->th_seq
+ 1;
1748 bcopy(opt
, th
+ 1, optlen
);
1749 th
->th_off
= (sizeof (struct tcphdr
) + optlen
) >> 2;
1751 th
->th_flags
= flags
;
1753 * Calculate receive window. Don't shrink window,
1754 * but avoid silly window syndrome.
1756 if (recwin
< (int32_t)(so
->so_rcv
.sb_hiwat
/ 4) && recwin
< (int)tp
->t_maxseg
)
1758 if (recwin
< (int32_t)(tp
->rcv_adv
- tp
->rcv_nxt
))
1759 recwin
= (int32_t)(tp
->rcv_adv
- tp
->rcv_nxt
);
1760 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0) {
1761 if (recwin
> (int32_t)slowlink_wsize
)
1762 recwin
= slowlink_wsize
;
1766 if (tcp_recv_bg
== 1 || IS_TCP_RECV_BG(so
)) {
1767 if (tcp_recv_throttle(tp
)) {
1768 uint32_t min_iaj_win
=
1769 tcp_min_iaj_win
* tp
->t_maxseg
;
1770 if (tp
->iaj_rwintop
== 0 ||
1771 SEQ_LT(tp
->iaj_rwintop
, tp
->rcv_adv
))
1772 tp
->iaj_rwintop
= tp
->rcv_adv
;
1773 if (SEQ_LT(tp
->iaj_rwintop
,
1774 tp
->rcv_nxt
+ min_iaj_win
))
1775 tp
->iaj_rwintop
= tp
->rcv_nxt
+ min_iaj_win
;
1776 recwin
= min(tp
->iaj_rwintop
- tp
->rcv_nxt
, recwin
);
1779 #endif /* TRAFFIC_MGT */
1781 if (recwin
> (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
))
1782 recwin
= (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
);
1783 th
->th_win
= htons((u_short
) (recwin
>>tp
->rcv_scale
));
1786 * Adjust the RXWIN0SENT flag - indicate that we have advertised
1787 * a 0 window. This may cause the remote transmitter to stall. This
1788 * flag tells soreceive() to disable delayed acknowledgements when
1789 * draining the buffer. This can occur if the receiver is attempting
1790 * to read more data then can be buffered prior to transmitting on
1793 if (th
->th_win
== 0)
1794 tp
->t_flags
|= TF_RXWIN0SENT
;
1796 tp
->t_flags
&= ~TF_RXWIN0SENT
;
1797 if (SEQ_GT(tp
->snd_up
, tp
->snd_nxt
)) {
1798 th
->th_urp
= htons((u_short
)(tp
->snd_up
- tp
->snd_nxt
));
1799 th
->th_flags
|= TH_URG
;
1802 * If no urgent pointer to send, then we pull
1803 * the urgent pointer to the left edge of the send window
1804 * so that it doesn't drift into the send window on sequence
1805 * number wraparound.
1807 tp
->snd_up
= tp
->snd_una
; /* drag it along */
1811 * Put TCP length in extended header, and then
1812 * checksum extended header and data.
1814 m
->m_pkthdr
.len
= hdrlen
+ len
; /* in6_cksum() need this */
1818 * ip6_plen is not need to be filled now, and will be filled
1821 m
->m_pkthdr
.csum_flags
= CSUM_TCPIPV6
;
1822 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
1824 th
->th_sum
= in_addword(th
->th_sum
,
1825 htons((u_short
)(optlen
+ len
)));
1830 m
->m_pkthdr
.csum_flags
= CSUM_TCP
;
1831 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
1833 th
->th_sum
= in_addword(th
->th_sum
,
1834 htons((u_short
)(optlen
+ len
)));
1838 * Enable TSO and specify the size of the segments.
1839 * The TCP pseudo header checksum is always provided.
1844 m
->m_pkthdr
.csum_flags
|= CSUM_TSO_IPV6
;
1847 m
->m_pkthdr
.csum_flags
|= CSUM_TSO_IPV4
;
1849 m
->m_pkthdr
.tso_segsz
= tp
->t_maxopd
- optlen
;
1851 m
->m_pkthdr
.tso_segsz
= 0;
1855 * In transmit state, time the transmission and arrange for
1856 * the retransmit. In persist state, just set snd_max.
1858 if (!(tp
->t_flagsext
& TF_FORCE
)
1859 || tp
->t_timer
[TCPT_PERSIST
] == 0) {
1860 tcp_seq startseq
= tp
->snd_nxt
;
1863 * Advance snd_nxt over sequence space of this segment.
1865 if (flags
& (TH_SYN
|TH_FIN
)) {
1868 if ((flags
& TH_FIN
) &&
1869 !(tp
->t_flags
& TF_SENTFIN
)) {
1871 tp
->t_flags
|= TF_SENTFIN
;
1877 if (SEQ_GT(tp
->snd_nxt
, tp
->snd_max
)) {
1878 tp
->snd_max
= tp
->snd_nxt
;
1880 * Time this transmission if not a retransmission and
1881 * not currently timing anything.
1883 if (tp
->t_rtttime
== 0) {
1884 tp
->t_rtttime
= tcp_now
;
1885 tp
->t_rtseq
= startseq
;
1886 tcpstat
.tcps_segstimed
++;
1891 * Set retransmit timer if not currently set,
1892 * and not doing an ack or a keep-alive probe.
1895 if (tp
->t_timer
[TCPT_REXMT
] == 0 &&
1896 ((sack_rxmit
&& tp
->snd_nxt
!= tp
->snd_max
) ||
1897 tp
->snd_nxt
!= tp
->snd_una
|| (flags
& TH_FIN
))) {
1898 if (tp
->t_timer
[TCPT_PERSIST
]) {
1899 tp
->t_timer
[TCPT_PERSIST
] = 0;
1902 tp
->t_persist_stop
= 0;
1904 tp
->t_timer
[TCPT_REXMT
] =
1905 OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
1909 * Set tail loss probe timeout if new data is being
1910 * transmitted. This will be supported only when
1911 * SACK option is enabled on a connection.
1913 * Every time new data is sent PTO will get reset.
1915 if (tcp_enable_tlp
&& tp
->t_state
== TCPS_ESTABLISHED
&&
1916 SACK_ENABLED(tp
) && !IN_FASTRECOVERY(tp
)
1917 && tp
->snd_nxt
== tp
->snd_max
1918 && SEQ_GT(tp
->snd_nxt
, tp
->snd_una
)
1919 && tp
->t_rxtshift
== 0
1920 && (tp
->t_flagsext
& (TF_SENT_TLPROBE
|TF_PKTS_REORDERED
)) == 0) {
1921 u_int32_t pto
, srtt
, new_rto
= 0;
1924 * Using SRTT alone to set PTO can cause spurious
1925 * retransmissions on wireless networks where there
1926 * is a lot of variance in RTT. Taking variance
1927 * into account will avoid this.
1929 srtt
= tp
->t_srtt
>> TCP_RTT_SHIFT
;
1930 pto
= ((TCP_REXMTVAL(tp
)) * 3) >> 1;
1931 pto
= max (2 * srtt
, pto
);
1932 if ((tp
->snd_max
- tp
->snd_una
) == tp
->t_maxseg
)
1934 (((3 * pto
) >> 2) + tcp_delack
* 2));
1938 /* if RTO is less than PTO, choose RTO instead */
1939 if (tp
->t_rxtcur
< pto
) {
1941 * Schedule PTO instead of RTO in favor of
1946 /* Reset the next RTO to be after PTO. */
1947 TCPT_RANGESET(new_rto
,
1948 (pto
+ TCP_REXMTVAL(tp
)),
1949 max(tp
->t_rttmin
, tp
->t_rttcur
+ 2),
1951 tp
->t_timer
[TCPT_REXMT
] =
1952 OFFSET_FROM_START(tp
, new_rto
);
1954 tp
->t_timer
[TCPT_PTO
] = OFFSET_FROM_START(tp
, pto
);
1958 * Persist case, update snd_max but since we are in
1959 * persist mode (no window) we do not update snd_nxt.
1964 if ((flags
& TH_FIN
) &&
1965 !(tp
->t_flags
& TF_SENTFIN
)) {
1967 tp
->t_flags
|= TF_SENTFIN
;
1969 if (SEQ_GT(tp
->snd_nxt
+ xlen
, tp
->snd_max
))
1970 tp
->snd_max
= tp
->snd_nxt
+ len
;
1977 if (so_options
& SO_DEBUG
)
1978 tcp_trace(TA_OUTPUT
, tp
->t_state
, tp
, mtod(m
, void *), th
, 0);
1982 * Fill in IP length and desired time to live and
1983 * send to IP level. There should be a better way
1984 * to handle ttl and tos; we could keep them in
1985 * the template, but need a way to checksum without them.
1989 * m->m_pkthdr.len should have been set before cksum calcuration,
1990 * because in6_cksum() need it.
1994 * we separately set hoplimit for every segment, since the
1995 * user might want to change the value via setsockopt.
1996 * Also, desired default hop limit might be changed via
1997 * Neighbor Discovery.
1999 ip6
->ip6_hlim
= in6_selecthlim(inp
, inp
->in6p_route
.ro_rt
?
2000 inp
->in6p_route
.ro_rt
->rt_ifp
: NULL
);
2002 /* TODO: IPv6 IP6TOS_ECT bit on */
2003 KERNEL_DEBUG(DBG_LAYER_BEG
,
2004 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
2005 (((inp
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
2006 (inp
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
2011 ip
->ip_len
= m
->m_pkthdr
.len
;
2012 ip
->ip_ttl
= inp
->inp_ip_ttl
; /* XXX */
2013 ip
->ip_tos
|= (inp
->inp_ip_tos
& ~IPTOS_ECN_MASK
);/* XXX */
2014 KERNEL_DEBUG(DBG_LAYER_BEG
,
2015 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
2016 (((inp
->inp_laddr
.s_addr
& 0xffff) << 16) |
2017 (inp
->inp_faddr
.s_addr
& 0xffff)), 0,0,0);
2021 * See if we should do MTU discovery.
2022 * Look at the flag updated on the following criterias:
2023 * 1) Path MTU discovery is authorized by the sysctl
2024 * 2) The route isn't set yet (unlikely but could happen)
2025 * 3) The route is up
2026 * 4) the MTU is not locked (if it is, then discovery has been
2027 * disabled for that route)
2032 if (path_mtu_discovery
&& (tp
->t_flags
& TF_PMTUD
))
2033 ip
->ip_off
|= IP_DF
;
2037 necp_kernel_policy_id policy_id
;
2038 if (!necp_socket_is_allowed_to_send_recv(inp
, &policy_id
)) {
2040 error
= EHOSTUNREACH
;
2044 necp_mark_packet_from_socket(m
, inp
, policy_id
);
2049 if (inp
->inp_sp
!= NULL
)
2050 ipsec_setsocket(m
, so
);
2054 * The socket is kept locked while sending out packets in ip_output, even if packet chaining is not active.
2059 * Embed the flow hash in pkt hdr and mark the packet as
2060 * capable of flow controlling
2062 m
->m_pkthdr
.pkt_flowsrc
= FLOWSRC_INPCB
;
2063 m
->m_pkthdr
.pkt_flowid
= inp
->inp_flowhash
;
2064 m
->m_pkthdr
.pkt_flags
|= PKTF_FLOW_ID
| PKTF_FLOW_LOCALSRC
;
2066 /* Disable flow advisory when using MPTCP. */
2067 if (!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
))
2069 m
->m_pkthdr
.pkt_flags
|= PKTF_FLOW_ADV
;
2070 m
->m_pkthdr
.pkt_proto
= IPPROTO_TCP
;
2072 m
->m_nextpkt
= NULL
;
2074 if (inp
->inp_last_outifp
!= NULL
&&
2075 !(inp
->inp_last_outifp
->if_flags
& IFF_LOOPBACK
)) {
2076 /* Hint to prioritize this packet if
2077 * 1. if the packet has no data
2078 * 2. the interface supports transmit-start model and did
2079 * not disable ACK prioritization.
2080 * 3. Only ACK flag is set.
2081 * 4. there is no outstanding data on this connection.
2083 if (tcp_prioritize_acks
!= 0 && len
== 0 &&
2084 (inp
->inp_last_outifp
->if_eflags
&
2085 (IFEF_TXSTART
| IFEF_NOACKPRI
)) == IFEF_TXSTART
&&
2086 th
->th_flags
== TH_ACK
&& tp
->snd_una
== tp
->snd_max
&&
2087 tp
->t_timer
[TCPT_REXMT
] == 0) {
2088 svc_flags
|= PKT_SCF_TCP_ACK
;
2090 set_packet_service_class(m
, so
, MBUF_SC_UNSPEC
, svc_flags
);
2093 tp
->t_pktlist_sentlen
+= len
;
2098 DTRACE_TCP5(send
, struct mbuf
*, m
, struct inpcb
*, inp
,
2099 struct ip6
*, ip6
, struct tcpcb
*, tp
, struct tcphdr
*,
2104 DTRACE_TCP5(send
, struct mbuf
*, m
, struct inpcb
*, inp
,
2105 struct ip
*, ip
, struct tcpcb
*, tp
, struct tcphdr
*, th
);
2108 if (tp
->t_pktlist_head
!= NULL
) {
2109 tp
->t_pktlist_tail
->m_nextpkt
= m
;
2110 tp
->t_pktlist_tail
= m
;
2112 packchain_newlist
++;
2113 tp
->t_pktlist_head
= tp
->t_pktlist_tail
= m
;
2116 if ((lro_ackmore
) && (!sackoptlen
) && (!tp
->t_timer
[TCPT_PERSIST
]) &&
2117 ((th
->th_flags
& TH_ACK
) == TH_ACK
) && (!len
) &&
2118 (tp
->t_state
== TCPS_ESTABLISHED
)) {
2119 /* For a pure ACK, see if you need to send more of them */
2120 mnext
= tcp_send_lroacks(tp
, m
, th
);
2122 tp
->t_pktlist_tail
->m_nextpkt
= mnext
;
2123 if (mnext
->m_nextpkt
== NULL
) {
2124 tp
->t_pktlist_tail
= mnext
;
2127 struct mbuf
*tail
, *next
;
2128 next
= mnext
->m_nextpkt
;
2129 tail
= next
->m_nextpkt
;
2132 tail
= tail
->m_nextpkt
;
2135 tp
->t_pktlist_tail
= next
;
2140 if (sendalot
== 0 || (tp
->t_state
!= TCPS_ESTABLISHED
) ||
2141 (tp
->snd_cwnd
<= (tp
->snd_wnd
/ 8)) ||
2142 (tp
->t_flags
& (TH_PUSH
| TF_ACKNOW
)) ||
2143 (tp
->t_flagsext
& TF_FORCE
) ||
2144 tp
->t_lastchain
>= tcp_packet_chaining
) {
2146 while (inp
->inp_sndinprog_cnt
== 0 &&
2147 tp
->t_pktlist_head
!= NULL
) {
2148 packetlist
= tp
->t_pktlist_head
;
2149 packchain_listadd
= tp
->t_lastchain
;
2151 lost
= tp
->t_pktlist_sentlen
;
2152 TCP_PKTLIST_CLEAR(tp
);
2154 error
= tcp_ip_output(so
, tp
, packetlist
,
2155 packchain_listadd
, tp_inp_options
,
2156 (so_options
& SO_DONTROUTE
),
2157 (sack_rxmit
| (sack_bytes_rxmt
!= 0)), recwin
,
2165 * Take into account the rest of unsent
2166 * packets in the packet list for this tcp
2167 * into "lost", since we're about to free
2168 * the whole list below.
2170 lost
+= tp
->t_pktlist_sentlen
;
2176 /* tcp was closed while we were in ip; resume close */
2177 if (inp
->inp_sndinprog_cnt
== 0 &&
2178 (tp
->t_flags
& TF_CLOSING
)) {
2179 tp
->t_flags
&= ~TF_CLOSING
;
2180 (void) tcp_close(tp
);
2186 tcpstat
.tcps_sndtotal
++;
2192 * Assume that the packets were lost, so back out the
2193 * sequence number advance, if any. Note that the "lost"
2194 * variable represents the amount of user data sent during
2195 * the recent call to ip_output_list() plus the amount of
2196 * user data in the packet list for this tcp at the moment.
2198 if (!(tp
->t_flagsext
& TF_FORCE
)
2199 || tp
->t_timer
[TCPT_PERSIST
] == 0) {
2201 * No need to check for TH_FIN here because
2202 * the TF_SENTFIN flag handles that case.
2204 if ((flags
& TH_SYN
) == 0) {
2206 if (SEQ_GT((p
->rxmit
- lost
),
2210 lost
= p
->rxmit
- tp
->snd_una
;
2211 p
->rxmit
= tp
->snd_una
;
2213 tp
->sackhint
.sack_bytes_rexmit
-= lost
;
2215 if (SEQ_GT((tp
->snd_nxt
- lost
),
2217 tp
->snd_nxt
-= lost
;
2219 tp
->snd_nxt
= tp
->snd_una
;
2224 if (tp
->t_pktlist_head
!= NULL
)
2225 m_freem_list(tp
->t_pktlist_head
);
2226 TCP_PKTLIST_CLEAR(tp
);
2228 if (error
== ENOBUFS
) {
2229 if (!tp
->t_timer
[TCPT_REXMT
] &&
2230 !tp
->t_timer
[TCPT_PERSIST
])
2231 tp
->t_timer
[TCPT_REXMT
] =
2232 OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
2233 tp
->snd_cwnd
= tp
->t_maxseg
;
2234 tp
->t_bytes_acked
= 0;
2235 tcp_check_timer_state(tp
);
2236 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
2238 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_OUTPUT_ERROR
);
2241 if (error
== EMSGSIZE
) {
2243 * ip_output() will have already fixed the route
2244 * for us. tcp_mtudisc() will, as its last action,
2245 * initiate retransmission, so it is important to
2248 * If TSO was active we either got an interface
2249 * without TSO capabilits or TSO was turned off.
2250 * Disable it for this connection as too and
2251 * immediatly retry with MSS sized segments generated
2255 tp
->t_flags
&= ~TF_TSO
;
2257 tcp_mtudisc(inp
, 0);
2258 tcp_check_timer_state(tp
);
2260 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
2264 * Unless this is due to interface restriction policy,
2265 * treat EHOSTUNREACH/ENETDOWN as a soft error.
2267 if ((error
== EHOSTUNREACH
|| error
== ENETDOWN
) &&
2268 TCPS_HAVERCVDSYN(tp
->t_state
) &&
2269 !inp_restricted_send(inp
, inp
->inp_last_outifp
)) {
2270 tp
->t_softerror
= error
;
2273 tcp_check_timer_state(tp
);
2274 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
2278 tcpstat
.tcps_sndtotal
++;
2280 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
,0,0,0,0,0);
2284 tcp_check_timer_state(tp
);
2289 tcp_ip_output(struct socket
*so
, struct tcpcb
*tp
, struct mbuf
*pkt
,
2290 int cnt
, struct mbuf
*opt
, int flags
, int sack_in_progress
, int recwin
,
2295 boolean_t unlocked
= FALSE
;
2296 boolean_t ifdenied
= FALSE
;
2297 struct inpcb
*inp
= tp
->t_inpcb
;
2298 struct ip_out_args ipoa
=
2299 { IFSCOPE_NONE
, { 0 }, IPOAF_SELECT_SRCIF
|IPOAF_BOUND_SRCADDR
, 0 };
2301 struct ifnet
*outif
= NULL
;
2303 struct ip6_out_args ip6oa
=
2304 { IFSCOPE_NONE
, { 0 }, IP6OAF_SELECT_SRCIF
|IP6OAF_BOUND_SRCADDR
, 0 };
2305 struct route_in6 ro6
;
2306 struct flowadv
*adv
=
2307 (isipv6
? &ip6oa
.ip6oa_flowadv
: &ipoa
.ipoa_flowadv
);
2309 struct flowadv
*adv
= &ipoa
.ipoa_flowadv
;
2312 /* If socket was bound to an ifindex, tell ip_output about it */
2313 if (inp
->inp_flags
& INP_BOUND_IF
) {
2316 ip6oa
.ip6oa_boundif
= inp
->inp_boundifp
->if_index
;
2317 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
2321 ipoa
.ipoa_boundif
= inp
->inp_boundifp
->if_index
;
2322 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
2326 if (INP_NO_CELLULAR(inp
)) {
2329 ip6oa
.ip6oa_flags
|= IP6OAF_NO_CELLULAR
;
2332 ipoa
.ipoa_flags
|= IPOAF_NO_CELLULAR
;
2334 if (INP_NO_EXPENSIVE(inp
)) {
2337 ip6oa
.ip6oa_flags
|= IP6OAF_NO_EXPENSIVE
;
2340 ipoa
.ipoa_flags
|= IPOAF_NO_EXPENSIVE
;
2343 if (INP_AWDL_UNRESTRICTED(inp
)) {
2346 ip6oa
.ip6oa_flags
|= IP6OAF_AWDL_UNRESTRICTED
;
2349 ipoa
.ipoa_flags
|= IPOAF_AWDL_UNRESTRICTED
;
2354 flags
|= IPV6_OUTARGS
;
2357 flags
|= IP_OUTARGS
;
2359 /* Copy the cached route and take an extra reference */
2362 in6p_route_copyout(inp
, &ro6
);
2365 inp_route_copyout(inp
, &ro
);
2368 * Data sent (as far as we can tell).
2369 * If this advertises a larger window than any other segment,
2370 * then remember the size of the advertised window.
2371 * Make sure ACK/DELACK conditions are cleared before
2372 * we unlock the socket.
2374 if (recwin
> 0 && SEQ_GT(tp
->rcv_nxt
+ recwin
, tp
->rcv_adv
))
2375 tp
->rcv_adv
= tp
->rcv_nxt
+ recwin
;
2376 tp
->last_ack_sent
= tp
->rcv_nxt
;
2377 tp
->t_flags
&= ~(TF_ACKNOW
| TF_DELACK
);
2378 tp
->t_timer
[TCPT_DELACK
] = 0;
2379 tp
->t_unacksegs
= 0;
2381 /* Increment the count of outstanding send operations */
2382 inp
->inp_sndinprog_cnt
++;
2385 * If allowed, unlock TCP socket while in IP
2386 * but only if the connection is established and
2387 * in a normal mode where reentrancy on the tcpcb won't be
2389 * - there is no SACK episode
2390 * - we're not in Fast Recovery mode
2391 * - if we're not sending from an upcall.
2393 if (tcp_output_unlocked
&& !so
->so_upcallusecount
&&
2394 (tp
->t_state
== TCPS_ESTABLISHED
) && (sack_in_progress
== 0) &&
2395 !IN_FASTRECOVERY(tp
)) {
2398 socket_unlock(so
, 0);
2402 * Don't send down a chain of packets when:
2403 * - TCP chaining is disabled
2404 * - there is an IPsec rule set
2405 * - there is a non default rule set for the firewall
2408 chain
= tcp_packet_chaining
> 1
2413 && (fw_enable
== 0 || fw_bypass
)
2415 ; // I'm important, not extraneous
2418 while (pkt
!= NULL
) {
2419 struct mbuf
*npkt
= pkt
->m_nextpkt
;
2422 pkt
->m_nextpkt
= NULL
;
2424 * If we are not chaining, make sure to set the packet
2425 * list count to 0 so that IP takes the right path;
2426 * this is important for cases such as IPSec where a
2427 * single mbuf might result in multiple mbufs as part
2428 * of the encapsulation. If a non-zero count is passed
2429 * down to IP, the head of the chain might change and
2430 * we could end up skipping it (thus generating bogus
2431 * packets). Fixing it in IP would be desirable, but
2432 * for now this would do it.
2438 error
= ip6_output_list(pkt
, cnt
,
2439 inp
->in6p_outputopts
, &ro6
, flags
, NULL
, NULL
,
2441 ifdenied
= (ip6oa
.ip6oa_retflags
& IP6OARF_IFDENIED
);
2444 error
= ip_output_list(pkt
, cnt
, opt
, &ro
, flags
, NULL
,
2446 ifdenied
= (ipoa
.ipoa_retflags
& IPOARF_IFDENIED
);
2449 if (chain
|| error
) {
2451 * If we sent down a chain then we are done since
2452 * the callee had taken care of everything; else
2453 * we need to free the rest of the chain ourselves.
2466 * Enter flow controlled state if the connection is established
2467 * and is not in recovery.
2469 * A connection will enter suspended state even if it is in
2472 if (((adv
->code
== FADV_FLOW_CONTROLLED
&& !IN_FASTRECOVERY(tp
)) ||
2473 adv
->code
== FADV_SUSPENDED
) &&
2474 !(tp
->t_flags
& TF_CLOSING
) &&
2475 tp
->t_state
== TCPS_ESTABLISHED
) {
2477 rc
= inp_set_fc_state(inp
, adv
->code
);
2480 tcp_ccdbg_trace(tp
, NULL
,
2481 ((adv
->code
== FADV_FLOW_CONTROLLED
) ?
2482 TCP_CC_FLOW_CONTROL
: TCP_CC_SUSPEND
));
2486 * When an interface queue gets suspended, some of the
2487 * packets are dropped. Return ENOBUFS, to update the
2490 if (adv
->code
== FADV_SUSPENDED
)
2493 VERIFY(inp
->inp_sndinprog_cnt
> 0);
2494 if ( --inp
->inp_sndinprog_cnt
== 0)
2495 inp
->inp_flags
&= ~(INP_FC_FEEDBACK
);
2499 if (ro6
.ro_rt
!= NULL
&& (outif
= ro6
.ro_rt
->rt_ifp
) !=
2500 inp
->in6p_last_outifp
)
2501 inp
->in6p_last_outifp
= outif
;
2504 if (ro
.ro_rt
!= NULL
&& (outif
= ro
.ro_rt
->rt_ifp
) !=
2505 inp
->inp_last_outifp
)
2506 inp
->inp_last_outifp
= outif
;
2508 if (error
!= 0 && ifdenied
&&
2509 (INP_NO_CELLULAR(inp
) || INP_NO_EXPENSIVE(inp
)))
2510 soevent(inp
->inp_socket
,
2511 (SO_FILT_HINT_LOCKED
|SO_FILT_HINT_IFDENIED
));
2513 /* Synchronize cached PCB route & options */
2516 in6p_route_copyin(inp
, &ro6
);
2519 inp_route_copyin(inp
, &ro
);
2521 if (tp
->t_state
< TCPS_ESTABLISHED
&& tp
->t_rxtshift
== 0 &&
2522 tp
->t_inpcb
->inp_route
.ro_rt
!= NULL
) {
2523 /* If we found the route and there is an rtt on it
2524 * reset the retransmit timer
2526 tcp_getrt_rtt(tp
, tp
->t_inpcb
->in6p_route
.ro_rt
);
2527 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
2534 register struct tcpcb
*tp
;
2536 int t
= ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1;
2538 /* If a PERSIST_TIMER option was set we will limit the
2539 * time the persist timer will be active for that connection
2540 * in order to avoid DOS by using zero window probes.
2541 * see rdar://5805356
2544 if ((tp
->t_persist_timeout
!= 0) &&
2545 (tp
->t_timer
[TCPT_PERSIST
] == 0) &&
2546 (tp
->t_persist_stop
== 0)) {
2547 tp
->t_persist_stop
= tcp_now
+ tp
->t_persist_timeout
;
2551 * Start/restart persistance timer.
2553 TCPT_RANGESET(tp
->t_timer
[TCPT_PERSIST
],
2554 t
* tcp_backoff
[tp
->t_rxtshift
],
2555 TCPTV_PERSMIN
, TCPTV_PERSMAX
, 0);
2556 tp
->t_timer
[TCPT_PERSIST
] = OFFSET_FROM_START(tp
, tp
->t_timer
[TCPT_PERSIST
]);
2558 if (tp
->t_rxtshift
< TCP_MAXRXTSHIFT
)
2563 * Send as many acks as data coalesced. Every other packet when stretch
2564 * ACK is not enabled. Every 8 packets, if stretch ACK is enabled.
2567 tcp_send_lroacks(struct tcpcb
*tp
, struct mbuf
*m
, struct tcphdr
*th
)
2569 struct mbuf
*mnext
= NULL
, *ack_chain
= NULL
, *tail
= NULL
;
2571 tcp_seq org_ack
= ntohl(th
->th_ack
);
2572 tcp_seq prev_ack
= 0;
2573 int tack_offset
= 28; /* XXX IPv6 and IP options not supported */
2574 int twin_offset
= 34; /* XXX IPv6 and IP options not supported */
2575 int ack_size
= (tp
->t_flags
& TF_STRETCHACK
) ?
2576 (maxseg_unacked
* tp
->t_maxseg
) : (tp
->t_maxseg
<< 1);
2577 int segs_acked
= (tp
->t_flags
& TF_STRETCHACK
) ? maxseg_unacked
: 2;
2578 struct mbuf
*prev_ack_pkt
= NULL
;
2579 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
2580 unsigned short winsz
= ntohs(th
->th_win
);
2581 unsigned int scaled_win
= winsz
<<tp
->rcv_scale
;
2582 tcp_seq win_rtedge
= org_ack
+ scaled_win
;
2584 count
= tp
->t_lropktlen
/tp
->t_maxseg
;
2586 prev_ack
= (org_ack
- tp
->t_lropktlen
) + ack_size
;
2587 if (prev_ack
< org_ack
) {
2588 ack_chain
= m_dup(m
, M_DONTWAIT
);
2590 th
->th_ack
= htonl(prev_ack
);
2591 /* Keep adv window constant for duplicated ACK packets */
2592 scaled_win
= win_rtedge
- prev_ack
;
2593 if (scaled_win
> (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
))
2594 scaled_win
= (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
);
2595 th
->th_win
= htons(scaled_win
>>tp
->rcv_scale
);
2596 if (lrodebug
== 5) {
2597 printf("%s: win = %d winsz = %d sc = %d"
2599 __func__
, scaled_win
>>tp
->rcv_scale
, winsz
,
2600 tp
->rcv_scale
, tp
->t_lropktlen
, count
);
2603 count
-= segs_acked
; /* accounts for prev_ack packet */
2604 count
= (count
<= segs_acked
) ? 0 : count
- segs_acked
;
2605 tcpstat
.tcps_sndacks
++;
2606 so_tc_update_stats(m
, so
, m_get_service_class(m
));
2612 tp
->t_lropktlen
= 0;
2616 prev_ack_pkt
= ack_chain
;
2619 if ((prev_ack
+ ack_size
) < org_ack
) {
2620 prev_ack
+= ack_size
;
2623 * The last ACK sent must have the ACK number that TCP
2624 * thinks is the last sent ACK number.
2628 mnext
= m_dup(prev_ack_pkt
, M_DONTWAIT
);
2630 /* Keep adv window constant for duplicated ACK packets */
2631 scaled_win
= win_rtedge
- prev_ack
;
2632 if (scaled_win
> (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
))
2633 scaled_win
= (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
);
2634 winsz
= htons(scaled_win
>>tp
->rcv_scale
);
2635 if (lrodebug
== 5) {
2636 printf("%s: winsz = %d ack %x count %d\n",
2637 __func__
, scaled_win
>>tp
->rcv_scale
,
2640 bcopy(&winsz
, mtod(prev_ack_pkt
, caddr_t
) + twin_offset
, 2);
2642 bcopy(&prev_ack
, mtod(prev_ack_pkt
, caddr_t
) + tack_offset
, 4);
2644 tail
->m_nextpkt
= mnext
;
2646 count
-= segs_acked
;
2647 tcpstat
.tcps_sndacks
++;
2648 so_tc_update_stats(m
, so
, m_get_service_class(m
));
2650 if (lrodebug
== 5) {
2651 printf("%s: failed to alloc mbuf.\n", __func__
);
2655 prev_ack_pkt
= mnext
;
2657 tp
->t_lropktlen
= 0;
2662 tcp_recv_throttle (struct tcpcb
*tp
)
2664 uint32_t base_rtt
, newsize
;
2666 struct sockbuf
*sbrcv
= &tp
->t_inpcb
->inp_socket
->so_rcv
;
2668 if (tcp_use_rtt_recvbg
== 1 &&
2669 TSTMP_SUPPORTED(tp
)) {
2671 * Timestamps are supported on this connection. Use
2672 * RTT to look for an increase in latency.
2676 * If the connection is already being throttled, leave it
2677 * in that state until rtt comes closer to base rtt
2679 if (tp
->t_flagsext
& TF_RECV_THROTTLE
)
2682 base_rtt
= get_base_rtt(tp
);
2684 if (base_rtt
!= 0 && tp
->t_rttcur
!= 0) {
2685 qdelay
= tp
->t_rttcur
- base_rtt
;
2687 * if latency increased on a background flow,
2688 * return 1 to start throttling.
2690 if (qdelay
> target_qdelay
) {
2691 tp
->t_flagsext
|= TF_RECV_THROTTLE
;
2694 * Reduce the recv socket buffer size to
2697 if (sbrcv
->sb_idealsize
>
2698 tcp_recv_throttle_minwin
) {
2699 newsize
= sbrcv
->sb_idealsize
>> 1;
2700 /* Set a minimum of 16 K */
2703 tcp_recv_throttle_minwin
);
2704 sbrcv
->sb_idealsize
= newsize
;
2714 * Timestamps are not supported or there is no good RTT
2715 * measurement. Use IPDV in this case.
2717 if (tp
->acc_iaj
> tcp_acc_iaj_react_limit
)