2 * Copyright (c) 2000-2012 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>
89 #include <netinet/in.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/ip_var.h>
97 #include <netinet6/in6_pcb.h>
98 #include <netinet/ip6.h>
99 #include <netinet6/ip6_var.h>
101 #include <netinet/tcp.h>
103 #include <netinet/tcp_fsm.h>
104 #include <netinet/tcp_seq.h>
105 #include <netinet/tcp_timer.h>
106 #include <netinet/tcp_var.h>
107 #include <netinet/tcpip.h>
108 #include <netinet/tcp_cc.h>
110 #include <netinet/tcp_debug.h>
112 #include <sys/kdebug.h>
113 #include <mach/sdt.h>
116 #include <netinet6/ipsec.h>
120 #include <security/mac_framework.h>
121 #endif /* MAC_SOCKET */
123 #include <netinet/lro_ext.h>
125 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 1)
126 #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 3)
127 #define DBG_FNC_TCP_OUTPUT NETDBG_CODE(DBG_NETTCP, (4 << 8) | 1)
130 extern struct mbuf
*m_copypack();
133 int path_mtu_discovery
= 1;
134 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, path_mtu_discovery
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
135 &path_mtu_discovery
, 1, "Enable Path MTU Discovery");
138 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, slowstart_flightsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
139 &ss_fltsz
, 1, "Slow start flight size");
141 int ss_fltsz_local
= 8; /* starts with eight segments max */
142 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, local_slowstart_flightsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
143 &ss_fltsz_local
, 1, "Slow start flight size for local networks");
146 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tso
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
147 &tcp_do_tso
, 0, "Enable TCP Segmentation Offload");
150 int tcp_ecn_outbound
= 0;
151 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, ecn_initiate_out
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_ecn_outbound
,
152 0, "Initiate ECN for outbound connections");
154 int tcp_ecn_inbound
= 0;
155 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, ecn_negotiate_in
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_ecn_inbound
,
156 0, "Allow ECN negotiation for inbound connections");
158 int tcp_packet_chaining
= 50;
159 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, packetchain
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_packet_chaining
,
160 0, "Enable TCP output packet chaining");
162 int tcp_output_unlocked
= 1;
163 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, socket_unlocked_on_output
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_output_unlocked
,
164 0, "Unlock TCP when sending packets down to IP");
166 int tcp_do_rfc3390
= 1;
167 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rfc3390
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
168 &tcp_do_rfc3390
, 1, "Calculate intial slowstart cwnd depending on MSS");
170 int tcp_min_iaj_win
= MIN_IAJ_WIN
;
171 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, min_iaj_win
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
172 &tcp_min_iaj_win
, 1, "Minimum recv win based on inter-packet arrival jitter");
174 int tcp_acc_iaj_react_limit
= ACC_IAJ_REACT_LIMIT
;
175 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, acc_iaj_react_limit
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
176 &tcp_acc_iaj_react_limit
, 1, "Accumulated IAJ when receiver starts to react");
178 uint32_t tcp_do_autosendbuf
= 1;
179 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, doautosndbuf
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
180 &tcp_do_autosendbuf
, 1, "Enable send socket buffer auto-tuning");
182 uint32_t tcp_autosndbuf_inc
= 8 * 1024;
183 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autosndbufinc
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
184 &tcp_autosndbuf_inc
, 1, "Increment in send socket bufffer size");
186 uint32_t tcp_autosndbuf_max
= 512 * 1024;
187 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autosndbufmax
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
188 &tcp_autosndbuf_max
, 1, "Maximum send socket buffer size");
190 uint32_t tcp_prioritize_acks
= 1;
191 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, ack_prioritize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
192 &tcp_prioritize_acks
, 1, "Prioritize pure acks");
194 static int32_t packchain_newlist
= 0;
195 static int32_t packchain_looped
= 0;
196 static int32_t packchain_sent
= 0;
198 /* temporary: for testing */
200 extern int ipsec_bypass
;
203 extern int slowlink_wsize
; /* window correction for slow links */
205 extern int fw_enable
; /* firewall check for packet chaining */
206 extern int fw_bypass
; /* firewall check: disable packet chaining if there is rules */
207 #endif /* IPFIREWALL */
209 extern vm_size_t so_cache_zone_element_size
;
211 extern int ip_use_randomid
;
212 #endif /* RANDOM_IP_ID */
213 extern u_int32_t dlil_filter_count
;
214 extern u_int32_t kipf_count
;
215 extern int tcp_recv_bg
;
216 extern int maxseg_unacked
;
218 static int tcp_ip_output(struct socket
*, struct tcpcb
*, struct mbuf
*, int,
219 struct mbuf
*, int, int, int32_t, boolean_t
);
221 extern uint32_t get_base_rtt(struct tcpcb
*tp
);
222 static struct mbuf
* tcp_send_lroacks(struct tcpcb
*tp
, struct mbuf
*m
, struct tcphdr
*th
);
224 static __inline__ u_int16_t
225 get_socket_id(struct socket
* s
)
229 if (so_cache_zone_element_size
== 0) {
232 val
= (u_int16_t
)(((uintptr_t)s
) / so_cache_zone_element_size
);
240 * Tcp output routine: figure out what should be sent and send it.
248 * ip_output_list:ENOMEM
249 * ip_output_list:EADDRNOTAVAIL
250 * ip_output_list:ENETUNREACH
251 * ip_output_list:EHOSTUNREACH
252 * ip_output_list:EACCES
253 * ip_output_list:EMSGSIZE
254 * ip_output_list:ENOBUFS
255 * ip_output_list:??? [ignorable: mostly IPSEC/firewall/DLIL]
256 * ip6_output_list:EINVAL
257 * ip6_output_list:EOPNOTSUPP
258 * ip6_output_list:EHOSTUNREACH
259 * ip6_output_list:EADDRNOTAVAIL
260 * ip6_output_list:ENETUNREACH
261 * ip6_output_list:EMSGSIZE
262 * ip6_output_list:ENOBUFS
263 * ip6_output_list:??? [ignorable: mostly IPSEC/firewall/DLIL]
266 tcp_output(struct tcpcb
*tp
)
268 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
269 int32_t len
, recwin
, sendwin
, off
;
271 register struct mbuf
*m
;
272 struct ip
*ip
= NULL
;
273 register struct ipovly
*ipov
= NULL
;
275 struct ip6_hdr
*ip6
= NULL
;
277 register struct tcphdr
*th
;
278 u_char opt
[TCP_MAXOLEN
];
279 unsigned ipoptlen
, optlen
, hdrlen
;
280 int idle
, sendalot
, lost
= 0;
286 unsigned ipsec_optlen
= 0;
291 struct mbuf
*m_lastm
= NULL
;
292 struct mbuf
*m_head
= NULL
;
293 struct mbuf
*packetlist
= NULL
;
294 struct mbuf
*tp_inp_options
= tp
->t_inpcb
->inp_depend4
.inp4_options
;
296 int isipv6
= tp
->t_inpcb
->inp_vflag
& INP_IPV6
;
298 short packchain_listadd
= 0;
299 u_int16_t socket_id
= get_socket_id(so
);
300 int so_options
= so
->so_options
;
302 u_int32_t basertt
, svc_flags
= 0;
303 u_int32_t lro_ackmore
= (tp
->t_lropktlen
!= 0) ? 1 : 0;
304 struct mbuf
*mnext
= NULL
;
308 * Determine length of data that should be transmitted,
309 * and flags that will be used.
310 * If there is some data or critical controls (SYN, RST)
311 * to send, then transmit; otherwise, investigate further.
313 idle
= (tp
->t_flags
& TF_LASTIDLE
) || (tp
->snd_max
== tp
->snd_una
);
315 /* Since idle_time is signed integer, the following integer subtraction
316 * will take care of wrap around of tcp_now
318 idle_time
= tcp_now
- tp
->t_rcvtime
;
319 if (idle
&& idle_time
>= TCP_IDLETIMEOUT(tp
)) {
320 if (CC_ALGO(tp
)->after_idle
!= NULL
)
321 CC_ALGO(tp
)->after_idle(tp
);
322 DTRACE_TCP5(cc
, void, NULL
, struct inpcb
*, tp
->t_inpcb
,
323 struct tcpcb
*, tp
, struct tcphdr
*, NULL
,
324 int32_t, TCP_CC_IDLE_TIMEOUT
);
326 tp
->t_flags
&= ~TF_LASTIDLE
;
328 if (tp
->t_flags
& TF_MORETOCOME
) {
329 tp
->t_flags
|= TF_LASTIDLE
;
334 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
338 KERNEL_DEBUG(DBG_LAYER_BEG
,
339 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
340 (((tp
->t_inpcb
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
341 (tp
->t_inpcb
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
347 KERNEL_DEBUG(DBG_LAYER_BEG
,
348 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
349 (((tp
->t_inpcb
->inp_laddr
.s_addr
& 0xffff) << 16) |
350 (tp
->t_inpcb
->inp_faddr
.s_addr
& 0xffff)),
354 * If the route generation id changed, we need to check that our
355 * local (source) IP address is still valid. If it isn't either
356 * return error or silently do nothing (assuming the address will
357 * come back before the TCP connection times out).
359 rt
= tp
->t_inpcb
->inp_route
.ro_rt
;
360 if (rt
!= NULL
&& (!(rt
->rt_flags
& RTF_UP
) ||
361 rt
->generation_id
!= route_generation
)) {
363 struct in_ifaddr
*ia
= NULL
;
364 struct in6_ifaddr
*ia6
= NULL
;
365 int found_srcaddr
= 0;
367 /* disable multipages at the socket */
368 somultipages(so
, FALSE
);
370 /* Disable TSO for the socket until we know more */
371 tp
->t_flags
&= ~TF_TSO
;
374 ia6
= ifa_foraddr6(&tp
->t_inpcb
->in6p_laddr
);
378 ia
= ifa_foraddr(tp
->t_inpcb
->inp_laddr
.s_addr
);
383 /* check that the source address is still valid */
384 if (found_srcaddr
== 0) {
387 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_NOSRCADDR
));
389 if (tp
->t_state
>= TCPS_CLOSE_WAIT
) {
390 tcp_drop(tp
, EADDRNOTAVAIL
);
391 return(EADDRNOTAVAIL
);
394 /* set Retransmit timer if it wasn't set
395 * reset Persist timer and shift register as the
396 * advertised peer window may not be valid anymore
399 if (!tp
->t_timer
[TCPT_REXMT
]) {
400 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
401 if (tp
->t_timer
[TCPT_PERSIST
]) {
402 tp
->t_timer
[TCPT_PERSIST
] = 0;
404 tp
->t_persist_stop
= 0;
409 if (tp
->t_pktlist_head
!= NULL
)
410 m_freem_list(tp
->t_pktlist_head
);
411 TCP_PKTLIST_CLEAR(tp
);
413 /* drop connection if source address isn't available */
414 if (so
->so_flags
& SOF_NOADDRAVAIL
) {
415 tcp_drop(tp
, EADDRNOTAVAIL
);
416 return(EADDRNOTAVAIL
);
419 tcp_check_timer_state(tp
);
420 return(0); /* silently ignore, keep data in socket: address may be back */
424 IFA_REMREF(&ia
->ia_ifa
);
427 IFA_REMREF(&ia6
->ia_ifa
);
430 * Address is still valid; check for multipages capability
431 * again in case the outgoing interface has changed.
434 if ((ifp
= rt
->rt_ifp
) != NULL
) {
435 somultipages(so
, (ifp
->if_hwassist
& IFNET_MULTIPAGES
));
436 tcp_set_tso(tp
, ifp
);
438 if (rt
->rt_flags
& RTF_UP
)
439 rt
->generation_id
= route_generation
;
441 * See if we should do MTU discovery. Don't do it if:
442 * 1) it is disabled via the sysctl
443 * 2) the route isn't up
444 * 3) the MTU is locked (if it is, then discovery has been
448 if (!path_mtu_discovery
|| ((rt
!= NULL
) &&
449 (!(rt
->rt_flags
& RTF_UP
) || (rt
->rt_rmx
.rmx_locks
& RTV_MTU
))))
450 tp
->t_flags
&= ~TF_PMTUD
;
452 tp
->t_flags
|= TF_PMTUD
;
458 * If we've recently taken a timeout, snd_max will be greater than
459 * snd_nxt. There may be SACK information that allows us to avoid
460 * resending already delivered data. Adjust snd_nxt accordingly.
462 if (tp
->sack_enable
&& SEQ_LT(tp
->snd_nxt
, tp
->snd_max
))
465 off
= tp
->snd_nxt
- tp
->snd_una
;
466 sendwin
= min(tp
->snd_wnd
, tp
->snd_cwnd
);
468 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0)
469 sendwin
= min(sendwin
, slowlink_wsize
);
471 flags
= tcp_outflags
[tp
->t_state
];
473 * Send any SACK-generated retransmissions. If we're explicitly trying
474 * to send out new data (when sendalot is 1), bypass this function.
475 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
476 * we're replacing a (future) new transmission with a retransmission
477 * now, and we previously incremented snd_cwnd in tcp_input().
480 * Still in sack recovery , reset rxmit flag to zero.
486 if (tp
->sack_enable
&& IN_FASTRECOVERY(tp
) &&
487 (p
= tcp_sack_output(tp
, &sack_bytes_rxmt
))) {
490 cwin
= min(tp
->snd_wnd
, tp
->snd_cwnd
) - sack_bytes_rxmt
;
493 /* Do not retransmit SACK segments beyond snd_recover */
494 if (SEQ_GT(p
->end
, tp
->snd_recover
)) {
496 * (At least) part of sack hole extends beyond
497 * snd_recover. Check to see if we can rexmit data
500 if (SEQ_GEQ(p
->rxmit
, tp
->snd_recover
)) {
502 * Can't rexmit any more data for this hole.
503 * That data will be rexmitted in the next
504 * sack recovery episode, when snd_recover
505 * moves past p->rxmit.
508 goto after_sack_rexmit
;
510 /* Can rexmit part of the current hole */
511 len
= ((int32_t)min(cwin
,
512 tp
->snd_recover
- p
->rxmit
));
514 len
= ((int32_t)min(cwin
, p
->end
- p
->rxmit
));
517 off
= p
->rxmit
- tp
->snd_una
; /* update off only if we really transmit SACK data */
520 tcpstat
.tcps_sack_rexmits
++;
521 tcpstat
.tcps_sack_rexmit_bytes
+=
522 min(len
, tp
->t_maxseg
);
524 nstat_route_tx(tp
->t_inpcb
->inp_route
.ro_rt
, 1,
525 min(len
, tp
->t_maxseg
), NSTAT_TX_FLAG_RETRANSMIT
);
526 locked_add_64(&tp
->t_inpcb
->inp_stat
->txpackets
, 1);
527 locked_add_64(&tp
->t_inpcb
->inp_stat
->txbytes
,
528 min(len
, tp
->t_maxseg
));
529 tp
->t_stat
.txretransmitbytes
+= min(len
, tp
->t_maxseg
);
537 * Get standard flags, and add SYN or FIN if requested by 'hidden'
540 if (tp
->t_flags
& TF_NEEDFIN
)
542 if (tp
->t_flags
& TF_NEEDSYN
)
546 * If in persist timeout with window of 0, send 1 byte.
547 * Otherwise, if window is small but nonzero
548 * and timer expired, we will send what we can
549 * and go to transmit state.
554 * If we still have some data to send, then
555 * clear the FIN bit. Usually this would
556 * happen below when it realizes that we
557 * aren't sending all the data. However,
558 * if we have exactly 1 byte of unsent data,
559 * then it won't clear the FIN bit below,
560 * and if we are in persist state, we wind
561 * up sending the packet without recording
562 * that we sent the FIN bit.
564 * We can't just blindly clear the FIN bit,
565 * because if we don't have any more data
566 * to send then the probe will be the FIN
569 if (off
< so
->so_snd
.sb_cc
)
573 tp
->t_timer
[TCPT_PERSIST
] = 0;
576 tp
->t_persist_stop
= 0;
581 * If snd_nxt == snd_max and we have transmitted a FIN, the
582 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
583 * a negative length. This can also occur when TCP opens up
584 * its congestion window while receiving additional duplicate
585 * acks after fast-retransmit because TCP will reset snd_nxt
586 * to snd_max after the fast-retransmit.
588 * In the normal retransmit-FIN-only case, however, snd_nxt will
589 * be set to snd_una, the offset will be 0, and the length may
592 * If sack_rxmit is true we are retransmitting from the scoreboard
593 * in which case len is already set.
595 if (sack_rxmit
== 0) {
596 if (sack_bytes_rxmt
== 0)
597 len
= min(so
->so_snd
.sb_cc
, sendwin
) - off
;
602 * We are inside of a SACK recovery episode and are
603 * sending new data, having retransmitted all the
604 * data possible in the scoreboard.
606 len
= min(so
->so_snd
.sb_cc
, tp
->snd_wnd
)
609 * Don't remove this (len > 0) check !
610 * We explicitly check for len > 0 here (although it
611 * isn't really necessary), to work around a gcc
612 * optimization issue - to force gcc to compute
613 * len above. Without this check, the computation
614 * of len is bungled by the optimizer.
617 cwin
= tp
->snd_cwnd
-
618 (tp
->snd_nxt
- tp
->sack_newdata
) -
622 len
= imin(len
, cwin
);
630 * Lop off SYN bit if it has already been sent. However, if this
631 * is SYN-SENT state and if segment contains data and if we don't
632 * know that foreign host supports TAO, suppress sending segment.
634 if ((flags
& TH_SYN
) && SEQ_GT(tp
->snd_nxt
, tp
->snd_una
)) {
637 if (len
> 0 && tp
->t_state
== TCPS_SYN_SENT
) {
638 while (tp
->t_inpcb
->inp_sndinprog_cnt
== 0 &&
639 tp
->t_pktlist_head
!= NULL
) {
640 packetlist
= tp
->t_pktlist_head
;
641 packchain_listadd
= tp
->t_lastchain
;
643 TCP_PKTLIST_CLEAR(tp
);
645 error
= tcp_ip_output(so
, tp
, packetlist
,
646 packchain_listadd
, tp_inp_options
,
647 (so_options
& SO_DONTROUTE
),
648 (sack_rxmit
| (sack_bytes_rxmt
!= 0)), 0,
658 * tcp was closed while we were in ip,
661 if (tp
->t_inpcb
->inp_sndinprog_cnt
== 0 &&
662 (tp
->t_flags
& TF_CLOSING
)) {
663 tp
->t_flags
&= ~TF_CLOSING
;
664 (void) tcp_close(tp
);
666 tcp_check_timer_state(tp
);
668 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
,
675 * Be careful not to send data and/or FIN on SYN segments.
676 * This measure is needed to prevent interoperability problems
677 * with not fully conformant TCP implementations.
679 if ((flags
& TH_SYN
) && (tp
->t_flags
& TF_NOOPT
)) {
684 /* The check here used to be (len < 0). Some times len is zero when
685 * the congestion window is closed and we need to check if persist timer
686 * has to be set in that case. But don't set persist until connection
689 if (len
<= 0 && !(flags
& TH_SYN
)) {
691 * If FIN has been sent but not acked,
692 * but we haven't been called to retransmit,
693 * len will be < 0. Otherwise, window shrank
694 * after we sent into it. If window shrank to 0,
695 * cancel pending retransmit, pull snd_nxt back
696 * to (closed) window, and set the persist timer
697 * if it isn't already going. If the window didn't
698 * close completely, just wait for an ACK.
702 tp
->t_timer
[TCPT_REXMT
] = 0;
705 tp
->snd_nxt
= tp
->snd_una
;
706 if (tp
->t_timer
[TCPT_PERSIST
] == 0)
711 /* Automatic sizing of send socket buffer. Increase the send socket buffer
712 * size if all of the following criteria are met
713 * 1. the receiver has enough buffer space for this data
714 * 2. send buffer is filled to 7/8th with data (so we actually
715 * have data to make use of it);
716 * 3. our send window (slow start and congestion controlled) is
717 * larger than sent but unacknowledged data in send buffer.
719 basertt
= get_base_rtt(tp
);
720 if (tcp_do_autosendbuf
== 1 &&
721 !INP_WAIT_FOR_IF_FEEDBACK(tp
->t_inpcb
) && !IN_FASTRECOVERY(tp
) &&
722 (so
->so_snd
.sb_flags
& (SB_AUTOSIZE
| SB_TRIM
)) == SB_AUTOSIZE
&&
723 tcp_cansbgrow(&so
->so_snd
)) {
724 if ((tp
->snd_wnd
/ 4 * 5) >= so
->so_snd
.sb_hiwat
&&
725 so
->so_snd
.sb_cc
>= (so
->so_snd
.sb_hiwat
/ 8 * 7) &&
726 sendwin
>= (so
->so_snd
.sb_cc
-
727 (tp
->snd_nxt
- tp
->snd_una
))) {
728 /* Also increase the send buffer only if the
729 * round-trip time is not increasing because we do
730 * not want to contribute to latency by filling buffers.
731 * We also do not want to hold onto application's
732 * old data for too long. Interactive applications would
733 * rather discard old data.
737 if (sbreserve(&so
->so_snd
,
738 min(so
->so_snd
.sb_hiwat
+ tcp_autosndbuf_inc
,
739 tcp_autosndbuf_max
)) == 1) {
740 so
->so_snd
.sb_idealsize
= so
->so_snd
.sb_hiwat
;
743 so
->so_snd
.sb_idealsize
=
744 max(tcp_sendspace
, so
->so_snd
.sb_hiwat
-
745 (2 * tcp_autosndbuf_inc
));
746 so
->so_snd
.sb_flags
|= SB_TRIM
;
752 * Truncate to the maximum segment length or enable TCP Segmentation
753 * Offloading (if supported by hardware) and ensure that FIN is removed
754 * if the length no longer contains the last data byte.
756 * TSO may only be used if we are in a pure bulk sending state. The
757 * presence of TCP-MD5, SACK retransmits, SACK advertizements, ipfw rules
758 * and IP options prevent using TSO. With TSO the TCP header is the same
759 * (except for the sequence number) for all generated packets. This
760 * makes it impossible to transmit any options which vary per generated
763 * The length of TSO bursts is limited to TCP_MAXWIN. That limit and
764 * removal of FIN (if not already catched here) are handled later after
765 * the exact length of the TCP options are known.
769 * Pre-calculate here as we save another lookup into the darknesses
770 * of IPsec that way and can actually decide if TSO is ok.
772 if (ipsec_bypass
== 0)
773 ipsec_optlen
= ipsec_hdrsiz_tcp(tp
);
776 if (len
> tp
->t_maxseg
) {
777 if ((tp
->t_flags
& TF_TSO
) && tcp_do_tso
&&
780 #endif /* RANDOM_IP_ID */
781 kipf_count
== 0 && dlil_filter_count
== 0 &&
782 tp
->rcv_numsacks
== 0 && sack_rxmit
== 0 && sack_bytes_rxmt
== 0 &&
783 tp
->t_inpcb
->inp_options
== NULL
&&
784 tp
->t_inpcb
->in6p_options
== NULL
789 && (fw_enable
== 0 || fw_bypass
)
801 if (SEQ_LT(p
->rxmit
+ len
, tp
->snd_una
+ so
->so_snd
.sb_cc
))
804 if (SEQ_LT(tp
->snd_nxt
+ len
, tp
->snd_una
+ so
->so_snd
.sb_cc
))
808 recwin
= tcp_sbspace(tp
);
811 * Sender silly window avoidance. We transmit under the following
812 * conditions when len is non-zero:
814 * - we've timed out (e.g. persist timer)
815 * - we need to retransmit
816 * - We have a full segment (or more with TSO)
817 * - This is the last buffer in a write()/send() and we are
818 * either idle or running NODELAY
819 * - we have more then 1/2 the maximum send window's worth of
820 * data (receiver may be limited the window size)
824 tp
->t_flags
&= ~TF_MAXSEGSNT
;
827 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
)) {
828 tp
->t_flags
&= ~TF_MAXSEGSNT
;
835 * Send new data on the connection only if it is
836 * not flow controlled
838 if (!INP_WAIT_FOR_IF_FEEDBACK(tp
->t_inpcb
) ||
839 tp
->t_state
!= TCPS_ESTABLISHED
) {
840 if (len
>= tp
->t_maxseg
) {
841 tp
->t_flags
|= TF_MAXSEGSNT
;
844 if (!(tp
->t_flags
& TF_MORETOCOME
) &&
845 (idle
|| tp
->t_flags
& TF_NODELAY
|| tp
->t_flags
& TF_MAXSEGSNT
) &&
846 (tp
->t_flags
& TF_NOPUSH
) == 0 &&
847 len
+ off
>= so
->so_snd
.sb_cc
) {
848 tp
->t_flags
&= ~TF_MAXSEGSNT
;
851 if (len
>= tp
->max_sndwnd
/ 2 && tp
->max_sndwnd
> 0) {
852 tp
->t_flags
&= ~TF_MAXSEGSNT
;
856 tcpstat
.tcps_fcholdpacket
++;
861 * Compare available window to amount of window
862 * known to peer (as advertised window less
863 * next expected input). If the difference is at least two
864 * max size segments, or at least 25% of the maximum possible
865 * window, then want to send a window update to peer.
866 * Skip this if the connection is in T/TCP half-open state.
868 if (recwin
> 0 && !(tp
->t_flags
& TF_NEEDSYN
)) {
870 * "adv" is the amount we can increase the window,
871 * taking into account that we are limited by
872 * TCP_MAXWIN << tp->rcv_scale.
874 int32_t adv
= imin(recwin
, (int)TCP_MAXWIN
<< tp
->rcv_scale
) -
875 (tp
->rcv_adv
- tp
->rcv_nxt
);
877 if (adv
>= (int32_t) (2 * tp
->t_maxseg
)) {
878 /* Update only if the resulting scaled value of the window changed, or
879 * if there is a change in the sequence since the last ack.
880 * This avoids what appears as dupe ACKS (see rdar://5640997)
882 * If streaming is detected avoid sending too many window updates.
883 * We will depend on the delack timer to send a window update
886 if ((tp
->t_flags
& TF_STRETCHACK
) == 0 &&
887 (tp
->last_ack_sent
!= tp
->rcv_nxt
||
888 ((recwin
+ adv
) >> tp
->rcv_scale
) > recwin
)) {
892 /* Make sure that the delayed ack timer is set if we
893 * delayed sending a window update because of streaming
896 if ((tp
->t_flags
& TF_STRETCHACK
) != 0 &&
897 (tp
->t_flags
& TF_DELACK
) == 0) {
898 tp
->t_flags
|= TF_DELACK
;
899 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
902 if (4 * adv
>= (int32_t) so
->so_rcv
.sb_hiwat
)
907 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW
908 * is also a catch-all for the retransmit timer timeout case.
910 if (tp
->t_flags
& TF_ACKNOW
)
912 if ((flags
& TH_RST
) ||
913 ((flags
& TH_SYN
) && (tp
->t_flags
& TF_NEEDSYN
) == 0))
915 if (SEQ_GT(tp
->snd_up
, tp
->snd_una
))
918 * If our state indicates that FIN should be sent
919 * and we have not yet done so, then we need to send.
921 if (flags
& TH_FIN
&&
922 ((tp
->t_flags
& TF_SENTFIN
) == 0 || tp
->snd_nxt
== tp
->snd_una
))
925 * In SACK, it is possible for tcp_output to fail to send a segment
926 * after the retransmission timer has been turned off. Make sure
927 * that the retransmission timer is set.
929 if (tp
->sack_enable
&& (tp
->t_state
>= TCPS_ESTABLISHED
) &&
930 SEQ_GT(tp
->snd_max
, tp
->snd_una
) &&
931 tp
->t_timer
[TCPT_REXMT
] == 0 &&
932 tp
->t_timer
[TCPT_PERSIST
] == 0) {
933 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
937 * TCP window updates are not reliable, rather a polling protocol
938 * using ``persist'' packets is used to insure receipt of window
939 * updates. The three ``states'' for the output side are:
940 * idle not doing retransmits or persists
941 * persisting to move a small or zero window
942 * (re)transmitting and thereby not persisting
944 * tp->t_timer[TCPT_PERSIST]
945 * is set when we are in persist state.
947 * is set when we are called to send a persist packet.
948 * tp->t_timer[TCPT_REXMT]
949 * is set when we are retransmitting
950 * The output side is idle when both timers are zero.
952 * If send window is too small, there is data to transmit, and no
953 * retransmit or persist is pending, then go to persist state.
954 * If nothing happens soon, send when timer expires:
955 * if window is nonzero, transmit what we can,
956 * otherwise force out a byte.
958 if (so
->so_snd
.sb_cc
&& tp
->t_timer
[TCPT_REXMT
] == 0 &&
959 tp
->t_timer
[TCPT_PERSIST
] == 0) {
966 * If there is no reason to send a segment, just return.
967 * but if there is some packets left in the packet list, send them now.
969 while (tp
->t_inpcb
->inp_sndinprog_cnt
== 0 &&
970 tp
->t_pktlist_head
!= NULL
) {
971 packetlist
= tp
->t_pktlist_head
;
972 packchain_listadd
= tp
->t_lastchain
;
974 TCP_PKTLIST_CLEAR(tp
);
976 error
= tcp_ip_output(so
, tp
, packetlist
, packchain_listadd
,
977 tp_inp_options
, (so_options
& SO_DONTROUTE
),
978 (sack_rxmit
| (sack_bytes_rxmt
!= 0)), recwin
,
985 /* tcp was closed while we were in ip; resume close */
986 if (tp
->t_inpcb
->inp_sndinprog_cnt
== 0 &&
987 (tp
->t_flags
& TF_CLOSING
)) {
988 tp
->t_flags
&= ~TF_CLOSING
;
989 (void) tcp_close(tp
);
991 tcp_check_timer_state(tp
);
993 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
998 * Before ESTABLISHED, force sending of initial options
999 * unless TCP set not to do any options.
1000 * NOTE: we assume that the IP/TCP header plus TCP options
1001 * always fit in a single mbuf, leaving room for a maximum
1003 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
1008 hdrlen
= sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
);
1011 hdrlen
= sizeof (struct tcpiphdr
);
1012 if (flags
& TH_SYN
) {
1013 tp
->snd_nxt
= tp
->iss
;
1014 if ((tp
->t_flags
& TF_NOOPT
) == 0) {
1017 opt
[0] = TCPOPT_MAXSEG
;
1018 opt
[1] = TCPOLEN_MAXSEG
;
1019 mss
= htons((u_short
) tcp_mssopt(tp
));
1020 (void)memcpy(opt
+ 2, &mss
, sizeof(mss
));
1021 optlen
= TCPOLEN_MAXSEG
;
1023 if ((tp
->t_flags
& TF_REQ_SCALE
) &&
1024 ((flags
& TH_ACK
) == 0 ||
1025 (tp
->t_flags
& TF_RCVD_SCALE
))) {
1026 *((u_int32_t
*)(void *)(opt
+ optlen
)) = htonl(
1028 TCPOPT_WINDOW
<< 16 |
1029 TCPOLEN_WINDOW
<< 8 |
1030 tp
->request_r_scale
);
1038 RFC 3168 states that:
1039 - If you ever sent an ECN-setup SYN/SYN-ACK you must be prepared
1040 to handle the TCP ECE flag, even if you also later send a
1041 non-ECN-setup SYN/SYN-ACK.
1042 - If you ever send a non-ECN-setup SYN/SYN-ACK, you must not set
1045 It is not clear how the ECE flag would ever be set if you never
1046 set the IP ECT flag on outbound packets. All the same, we use
1047 the TE_SETUPSENT to indicate that we have committed to handling
1048 the TCP ECE flag correctly. We use the TE_SENDIPECT to indicate
1049 whether or not we should set the IP ECT flag on outbound packets.
1052 * For a SYN-ACK, send an ECN setup SYN-ACK
1054 if (tcp_ecn_inbound
&& (flags
& (TH_SYN
| TH_ACK
)) == (TH_SYN
| TH_ACK
)) {
1055 if ((tp
->ecn_flags
& TE_SETUPRECEIVED
) != 0) {
1056 if ((tp
->ecn_flags
& TE_SETUPSENT
) == 0) {
1057 /* Setting TH_ECE makes this an ECN-setup SYN-ACK */
1061 * Record that we sent the ECN-setup and default to
1064 tp
->ecn_flags
|= (TE_SETUPSENT
| TE_SENDIPECT
);
1068 * We sent an ECN-setup SYN-ACK but it was dropped.
1069 * Fallback to non-ECN-setup SYN-ACK and clear flag
1070 * that to indicate we should not send data with IP ECT set.
1072 * Pretend we didn't receive an ECN-setup SYN.
1074 tp
->ecn_flags
&= ~TE_SETUPRECEIVED
;
1078 else if (tcp_ecn_outbound
&& (flags
& (TH_SYN
| TH_ACK
)) == TH_SYN
) {
1079 if ((tp
->ecn_flags
& TE_SETUPSENT
) == 0) {
1080 /* Setting TH_ECE and TH_CWR makes this an ECN-setup SYN */
1081 flags
|= (TH_ECE
| TH_CWR
);
1084 * Record that we sent the ECN-setup and default to
1087 tp
->ecn_flags
|= (TE_SETUPSENT
| TE_SENDIPECT
);
1091 * We sent an ECN-setup SYN but it was dropped.
1092 * Fall back to no ECN and clear flag indicating
1093 * we should send data with IP ECT set.
1095 tp
->ecn_flags
&= ~TE_SENDIPECT
;
1100 * Check if we should set the TCP CWR flag.
1101 * CWR flag is sent when we reduced the congestion window because
1102 * we received a TCP ECE or we performed a fast retransmit. We
1103 * never set the CWR flag on retransmitted packets. We only set
1104 * the CWR flag on data packets. Pure acks don't have this set.
1106 if ((tp
->ecn_flags
& TE_SENDCWR
) != 0 && len
!= 0 &&
1107 !SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) && !sack_rxmit
) {
1109 tp
->ecn_flags
&= ~TE_SENDCWR
;
1113 * Check if we should set the TCP ECE flag.
1115 if ((tp
->ecn_flags
& TE_SENDECE
) != 0 && len
== 0) {
1120 * Send a timestamp and echo-reply if this is a SYN and our side
1121 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
1122 * and our peer have sent timestamps in our SYN's.
1124 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
1125 (flags
& TH_RST
) == 0 &&
1126 ((flags
& TH_ACK
) == 0 ||
1127 (tp
->t_flags
& TF_RCVD_TSTMP
))) {
1128 u_int32_t
*lp
= (u_int32_t
*)(void *)(opt
+ optlen
);
1130 /* Form timestamp option as shown in appendix A of RFC 1323. */
1131 *lp
++ = htonl(TCPOPT_TSTAMP_HDR
);
1132 *lp
++ = htonl(tcp_now
);
1133 *lp
= htonl(tp
->ts_recent
);
1134 optlen
+= TCPOLEN_TSTAMP_APPA
;
1137 /* Note the timestamp for receive buffer autosizing */
1138 if (tp
->rfbuf_ts
== 0 && (so
->so_rcv
.sb_flags
& SB_AUTOSIZE
))
1139 tp
->rfbuf_ts
= tcp_now
;
1141 if (tp
->sack_enable
&& ((tp
->t_flags
& TF_NOOPT
) == 0)) {
1143 * Tack on the SACK permitted option *last*.
1144 * And do padding of options after tacking this on.
1145 * This is because of MSS, TS, WinScale and Signatures are
1146 * all present, we have just 2 bytes left for the SACK
1147 * permitted option, which is just enough.
1150 * If this is the first SYN of connection (not a SYN
1151 * ACK), include SACK permitted option. If this is a
1152 * SYN ACK, include SACK permitted option if peer has
1153 * already done so. This is only for active connect,
1154 * since the syncache takes care of the passive connect.
1156 if ((flags
& TH_SYN
) &&
1157 (!(flags
& TH_ACK
) || (tp
->t_flags
& TF_SACK_PERMIT
))) {
1159 bp
= (u_char
*)opt
+ optlen
;
1161 *bp
++ = TCPOPT_SACK_PERMITTED
;
1162 *bp
++ = TCPOLEN_SACK_PERMITTED
;
1163 optlen
+= TCPOLEN_SACK_PERMITTED
;
1167 * Send SACKs if necessary. This should be the last
1168 * option processed. Only as many SACKs are sent as
1169 * are permitted by the maximum options size.
1171 * In general, SACK blocks consume 8*n+2 bytes.
1172 * So a full size SACK blocks option is 34 bytes
1173 * (to generate 4 SACK blocks). At a minimum,
1174 * we need 10 bytes (to generate 1 SACK block).
1175 * If TCP Timestamps (12 bytes) and TCP Signatures
1176 * (18 bytes) are both present, we'll just have
1177 * 10 bytes for SACK options 40 - (12 + 18).
1179 if (TCPS_HAVEESTABLISHED(tp
->t_state
) &&
1180 (tp
->t_flags
& TF_SACK_PERMIT
) && tp
->rcv_numsacks
> 0 &&
1181 MAX_TCPOPTLEN
- optlen
- 2 >= TCPOLEN_SACK
) {
1183 u_char
*bp
= (u_char
*)opt
+ optlen
;
1186 nsack
= (MAX_TCPOPTLEN
- optlen
- 2) / TCPOLEN_SACK
;
1187 nsack
= min(nsack
, tp
->rcv_numsacks
);
1188 sackoptlen
= (2 + nsack
* TCPOLEN_SACK
);
1191 * First we need to pad options so that the
1192 * SACK blocks can start at a 4-byte boundary
1193 * (sack option and length are at a 2 byte offset).
1195 padlen
= (MAX_TCPOPTLEN
- optlen
- sackoptlen
) % 4;
1197 while (padlen
-- > 0)
1200 tcpstat
.tcps_sack_send_blocks
++;
1201 *bp
++ = TCPOPT_SACK
;
1203 lp
= (u_int32_t
*)(void *)bp
;
1204 for (i
= 0; i
< nsack
; i
++) {
1205 struct sackblk sack
= tp
->sackblks
[i
];
1206 *lp
++ = htonl(sack
.start
);
1207 *lp
++ = htonl(sack
.end
);
1209 optlen
+= sackoptlen
;
1213 /* Pad TCP options to a 4 byte boundary */
1214 if (optlen
< MAX_TCPOPTLEN
&& (optlen
% sizeof(u_int32_t
))) {
1215 int pad
= sizeof(u_int32_t
) - (optlen
% sizeof(u_int32_t
));
1216 u_char
*bp
= (u_char
*)opt
+ optlen
;
1229 ipoptlen
= ip6_optlen(tp
->t_inpcb
);
1233 if (tp_inp_options
) {
1234 ipoptlen
= tp_inp_options
->m_len
-
1235 offsetof(struct ipoption
, ipopt_list
);
1240 ipoptlen
+= ipsec_optlen
;
1244 * Adjust data length if insertion of options will
1245 * bump the packet length beyond the t_maxopd length.
1246 * Clear the FIN bit because we cut off the tail of
1249 * When doing TSO limit a burst to TCP_MAXWIN minus the
1250 * IP, TCP and Options length to keep ip->ip_len from
1251 * overflowing. Prevent the last segment from being
1252 * fractional thus making them all equal sized and set
1253 * the flag to continue sending. TSO is disabled when
1254 * IP options or IPSEC are present.
1256 if (len
+ optlen
+ ipoptlen
> tp
->t_maxopd
) {
1258 * If there is still more to send, don't close the connection.
1264 tso_maxlen
= tp
->tso_max_segment_size
? tp
->tso_max_segment_size
: TCP_MAXWIN
;
1266 if (len
> tso_maxlen
- hdrlen
- optlen
) {
1267 len
= tso_maxlen
- hdrlen
- optlen
;
1268 len
= len
- (len
% (tp
->t_maxopd
- optlen
));
1270 } else if (tp
->t_flags
& TF_NEEDFIN
)
1273 len
= tp
->t_maxopd
- optlen
- ipoptlen
;
1278 /*#ifdef DIAGNOSTIC*/
1280 if (max_linkhdr
+ hdrlen
> MCLBYTES
)
1281 panic("tcphdr too big");
1283 if (max_linkhdr
+ hdrlen
> MHLEN
)
1284 panic("tcphdr too big");
1288 /* Check if there is enough data in the send socket
1289 * buffer to start measuring bw
1291 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
1292 (tp
->t_bwmeas
!= NULL
) &&
1293 (tp
->t_flagsext
& TF_BWMEAS_INPROGRESS
) == 0 &&
1294 (so
->so_snd
.sb_cc
- (tp
->snd_max
- tp
->snd_una
)) >=
1295 tp
->t_bwmeas
->bw_minsize
) {
1296 tp
->t_bwmeas
->bw_size
= min((so
->so_snd
.sb_cc
- (tp
->snd_max
- tp
->snd_una
)),
1297 tp
->t_bwmeas
->bw_maxsize
);
1298 tp
->t_flagsext
|= TF_BWMEAS_INPROGRESS
;
1299 tp
->t_bwmeas
->bw_start
= tp
->snd_max
;
1300 tp
->t_bwmeas
->bw_ts
= tcp_now
;
1303 VERIFY(tp
->t_inpcb
->inp_flowhash
!= 0);
1306 * Grab a header mbuf, attaching a copy of data to
1307 * be transmitted, and initialize the header from
1308 * the template for sends on this connection.
1311 if (tp
->t_force
&& len
== 1)
1312 tcpstat
.tcps_sndprobe
++;
1313 else if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) || sack_rxmit
) {
1314 tcpstat
.tcps_sndrexmitpack
++;
1315 tcpstat
.tcps_sndrexmitbyte
+= len
;
1316 if (nstat_collect
) {
1317 nstat_route_tx(tp
->t_inpcb
->inp_route
.ro_rt
, 1, len
, NSTAT_TX_FLAG_RETRANSMIT
);
1318 locked_add_64(&tp
->t_inpcb
->inp_stat
->txpackets
, 1);
1319 locked_add_64(&tp
->t_inpcb
->inp_stat
->txbytes
, len
);
1320 tp
->t_stat
.txretransmitbytes
+= len
;
1323 tcpstat
.tcps_sndpack
++;
1324 tcpstat
.tcps_sndbyte
+= len
;
1325 if (nstat_collect
) {
1326 locked_add_64(&tp
->t_inpcb
->inp_stat
->txpackets
, 1);
1327 locked_add_64(&tp
->t_inpcb
->inp_stat
->txbytes
, len
);
1331 if ((m
= m_copypack(so
->so_snd
.sb_mb
, off
,
1332 (int)len
, max_linkhdr
+ hdrlen
)) == 0) {
1337 * m_copypack left space for our hdr; use it.
1340 m
->m_data
-= hdrlen
;
1343 * try to use the new interface that allocates all
1344 * the necessary mbuf hdrs under 1 mbuf lock and
1345 * avoids rescanning the socket mbuf list if
1346 * certain conditions are met. This routine can't
1347 * be used in the following cases...
1348 * 1) the protocol headers exceed the capacity of
1349 * of a single mbuf header's data area (no cluster attached)
1350 * 2) the length of the data being transmitted plus
1351 * the protocol headers fits into a single mbuf header's
1352 * data area (no cluster attached)
1356 if (MHLEN
< hdrlen
+ max_linkhdr
) {
1357 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1362 MCLGET(m
, M_DONTWAIT
);
1363 if ((m
->m_flags
& M_EXT
) == 0) {
1368 m
->m_data
+= max_linkhdr
;
1372 if (len
<= MHLEN
- hdrlen
- max_linkhdr
) {
1374 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1379 m
->m_data
+= max_linkhdr
;
1382 /* makes sure we still have data left to be sent at this point */
1383 if (so
->so_snd
.sb_mb
== NULL
|| off
< 0) {
1384 if (m
!= NULL
) m_freem(m
);
1385 error
= 0; /* should we return an error? */
1388 m_copydata(so
->so_snd
.sb_mb
, off
, (int) len
,
1389 mtod(m
, caddr_t
) + hdrlen
);
1393 m
->m_next
= m_copy(so
->so_snd
.sb_mb
, off
, (int) len
);
1394 if (m
->m_next
== 0) {
1401 * determine whether the mbuf pointer and offset passed back by the 'last' call
1402 * to m_copym_with_hdrs are still valid... if the head of the socket chain has
1403 * changed (due to an incoming ACK for instance), or the offset into the chain we
1404 * just computed is different from the one last returned by m_copym_with_hdrs (perhaps
1405 * we're re-transmitting a packet sent earlier), than we can't pass the mbuf pointer and
1406 * offset into it as valid hints for m_copym_with_hdrs to use (if valid, these hints allow
1407 * m_copym_with_hdrs to avoid rescanning from the beginning of the socket buffer mbuf list.
1408 * setting the mbuf pointer to NULL is sufficient to disable the hint mechanism.
1410 if (m_head
!= so
->so_snd
.sb_mb
|| sack_rxmit
|| last_off
!= off
)
1412 last_off
= off
+ len
;
1413 m_head
= so
->so_snd
.sb_mb
;
1415 /* makes sure we still have data left to be sent at this point */
1416 if (m_head
== NULL
) {
1417 error
= 0; /* should we return an error? */
1422 * m_copym_with_hdrs will always return the last mbuf pointer and the offset into it that
1423 * it acted on to fullfill the current request, whether a valid 'hint' was passed in or not
1425 if ((m
= m_copym_with_hdrs(so
->so_snd
.sb_mb
, off
, len
, M_DONTWAIT
, &m_lastm
, &m_off
)) == NULL
) {
1429 m
->m_data
+= max_linkhdr
;
1435 * If we're sending everything we've got, set PUSH.
1436 * (This will keep happy those implementations which only
1437 * give data to the user when a buffer fills or
1440 if (off
+ len
== so
->so_snd
.sb_cc
)
1443 if (tp
->t_flags
& TF_ACKNOW
)
1444 tcpstat
.tcps_sndacks
++;
1445 else if (flags
& (TH_SYN
|TH_FIN
|TH_RST
))
1446 tcpstat
.tcps_sndctrl
++;
1447 else if (SEQ_GT(tp
->snd_up
, tp
->snd_una
))
1448 tcpstat
.tcps_sndurg
++;
1450 tcpstat
.tcps_sndwinup
++;
1452 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1458 if (isipv6
&& (MHLEN
< hdrlen
+ max_linkhdr
) &&
1460 MH_ALIGN(m
, hdrlen
);
1463 m
->m_data
+= max_linkhdr
;
1466 m
->m_pkthdr
.rcvif
= 0;
1468 mac_mbuf_label_associate_inpcb(tp
->t_inpcb
, m
);
1472 ip6
= mtod(m
, struct ip6_hdr
*);
1473 th
= (struct tcphdr
*)(void *)(ip6
+ 1);
1474 tcp_fillheaders(tp
, ip6
, th
);
1475 if ((tp
->ecn_flags
& TE_SENDIPECT
) != 0 && len
&&
1476 !SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) && !sack_rxmit
) {
1477 ip6
->ip6_flow
|= htonl(IPTOS_ECN_ECT0
<< 20);
1479 svc_flags
|= PKT_SCF_IPV6
;
1483 ip
= mtod(m
, struct ip
*);
1484 ipov
= (struct ipovly
*)ip
;
1485 th
= (struct tcphdr
*)(void *)(ip
+ 1);
1486 /* this picks up the pseudo header (w/o the length) */
1487 tcp_fillheaders(tp
, ip
, th
);
1488 if ((tp
->ecn_flags
& TE_SENDIPECT
) != 0 && len
&&
1489 !SEQ_LT(tp
->snd_nxt
, tp
->snd_max
) && !sack_rxmit
) {
1490 ip
->ip_tos
= IPTOS_ECN_ECT0
;
1495 * Fill in fields, remembering maximum advertised
1496 * window for use in delaying messages about window sizes.
1497 * If resending a FIN, be sure not to use a new sequence number.
1499 if (flags
& TH_FIN
&& (tp
->t_flags
& TF_SENTFIN
) &&
1500 tp
->snd_nxt
== tp
->snd_max
)
1503 * If we are doing retransmissions, then snd_nxt will
1504 * not reflect the first unsent octet. For ACK only
1505 * packets, we do not want the sequence number of the
1506 * retransmitted packet, we want the sequence number
1507 * of the next unsent octet. So, if there is no data
1508 * (and no SYN or FIN), use snd_max instead of snd_nxt
1509 * when filling in ti_seq. But if we are in persist
1510 * state, snd_max might reflect one byte beyond the
1511 * right edge of the window, so use snd_nxt in that
1512 * case, since we know we aren't doing a retransmission.
1513 * (retransmit and persist are mutually exclusive...)
1515 if (sack_rxmit
== 0) {
1516 if (len
|| (flags
& (TH_SYN
|TH_FIN
)) || tp
->t_timer
[TCPT_PERSIST
])
1517 th
->th_seq
= htonl(tp
->snd_nxt
);
1519 th
->th_seq
= htonl(tp
->snd_max
);
1521 th
->th_seq
= htonl(p
->rxmit
);
1523 tp
->sackhint
.sack_bytes_rexmit
+= len
;
1525 th
->th_ack
= htonl(tp
->rcv_nxt
);
1526 tp
->last_ack_sent
= tp
->rcv_nxt
;
1529 bcopy(opt
, th
+ 1, optlen
);
1530 th
->th_off
= (sizeof (struct tcphdr
) + optlen
) >> 2;
1532 th
->th_flags
= flags
;
1534 * Calculate receive window. Don't shrink window,
1535 * but avoid silly window syndrome.
1537 if (recwin
< (int32_t)(so
->so_rcv
.sb_hiwat
/ 4) && recwin
< (int)tp
->t_maxseg
)
1539 if (recwin
< (int32_t)(tp
->rcv_adv
- tp
->rcv_nxt
))
1540 recwin
= (int32_t)(tp
->rcv_adv
- tp
->rcv_nxt
);
1541 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0) {
1542 if (recwin
> (int32_t)slowlink_wsize
)
1543 recwin
= slowlink_wsize
;
1547 if (tcp_recv_bg
== 1 || IS_TCP_RECV_BG(so
)) {
1548 if (tp
->acc_iaj
> tcp_acc_iaj_react_limit
) {
1549 uint32_t min_iaj_win
= tcp_min_iaj_win
* tp
->t_maxseg
;
1550 if (tp
->iaj_rwintop
== 0 ||
1551 SEQ_LT(tp
->iaj_rwintop
, tp
->rcv_adv
))
1552 tp
->iaj_rwintop
= tp
->rcv_adv
;
1553 if (SEQ_LT(tp
->iaj_rwintop
, tp
->rcv_nxt
+ min_iaj_win
))
1554 tp
->iaj_rwintop
= tp
->rcv_nxt
+ min_iaj_win
;
1555 recwin
= min(tp
->iaj_rwintop
- tp
->rcv_nxt
, recwin
);
1558 #endif /* TRAFFIC_MGT */
1560 if (recwin
> (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
))
1561 recwin
= (int32_t)(TCP_MAXWIN
<< tp
->rcv_scale
);
1562 th
->th_win
= htons((u_short
) (recwin
>>tp
->rcv_scale
));
1565 * Adjust the RXWIN0SENT flag - indicate that we have advertised
1566 * a 0 window. This may cause the remote transmitter to stall. This
1567 * flag tells soreceive() to disable delayed acknowledgements when
1568 * draining the buffer. This can occur if the receiver is attempting
1569 * to read more data then can be buffered prior to transmitting on
1572 if (th
->th_win
== 0)
1573 tp
->t_flags
|= TF_RXWIN0SENT
;
1575 tp
->t_flags
&= ~TF_RXWIN0SENT
;
1576 if (SEQ_GT(tp
->snd_up
, tp
->snd_nxt
)) {
1577 th
->th_urp
= htons((u_short
)(tp
->snd_up
- tp
->snd_nxt
));
1578 th
->th_flags
|= TH_URG
;
1581 * If no urgent pointer to send, then we pull
1582 * the urgent pointer to the left edge of the send window
1583 * so that it doesn't drift into the send window on sequence
1584 * number wraparound.
1586 tp
->snd_up
= tp
->snd_una
; /* drag it along */
1589 * Put TCP length in extended header, and then
1590 * checksum extended header and data.
1592 m
->m_pkthdr
.len
= hdrlen
+ len
; /* in6_cksum() need this */
1596 * ip6_plen is not need to be filled now, and will be filled
1599 m
->m_pkthdr
.csum_flags
= CSUM_TCPIPV6
;
1600 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
1602 th
->th_sum
= in_addword(th
->th_sum
,
1603 htons((u_short
)(optlen
+ len
)));
1608 m
->m_pkthdr
.csum_flags
= CSUM_TCP
;
1609 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
1611 th
->th_sum
= in_addword(th
->th_sum
,
1612 htons((u_short
)(optlen
+ len
)));
1616 * Enable TSO and specify the size of the segments.
1617 * The TCP pseudo header checksum is always provided.
1622 m
->m_pkthdr
.csum_flags
= CSUM_TSO_IPV6
;
1625 m
->m_pkthdr
.csum_flags
= CSUM_TSO_IPV4
;
1627 m
->m_pkthdr
.tso_segsz
= tp
->t_maxopd
- optlen
;
1630 m
->m_pkthdr
.tso_segsz
= 0;
1633 * In transmit state, time the transmission and arrange for
1634 * the retransmit. In persist state, just set snd_max.
1636 if (tp
->t_force
== 0 || tp
->t_timer
[TCPT_PERSIST
] == 0) {
1637 tcp_seq startseq
= tp
->snd_nxt
;
1640 * Advance snd_nxt over sequence space of this segment.
1642 if (flags
& (TH_SYN
|TH_FIN
)) {
1645 if (flags
& TH_FIN
) {
1647 tp
->t_flags
|= TF_SENTFIN
;
1653 if (SEQ_GT(tp
->snd_nxt
, tp
->snd_max
)) {
1654 tp
->snd_max
= tp
->snd_nxt
;
1656 * Time this transmission if not a retransmission and
1657 * not currently timing anything.
1659 if (tp
->t_rtttime
== 0) {
1660 tp
->t_rtttime
= tcp_now
;
1661 tp
->t_rtseq
= startseq
;
1662 tcpstat
.tcps_segstimed
++;
1667 * Set retransmit timer if not currently set,
1668 * and not doing an ack or a keep-alive probe.
1669 * Initial value for retransmit timer is smoothed
1670 * round-trip time + 2 * round-trip time variance.
1671 * Initialize shift counter which is used for backoff
1672 * of retransmit time.
1675 if (tp
->t_timer
[TCPT_REXMT
] == 0 &&
1676 ((sack_rxmit
&& tp
->snd_nxt
!= tp
->snd_max
) ||
1677 tp
->snd_nxt
!= tp
->snd_una
)) {
1678 if (tp
->t_timer
[TCPT_PERSIST
]) {
1679 tp
->t_timer
[TCPT_PERSIST
] = 0;
1682 tp
->t_persist_stop
= 0;
1684 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
1688 * Persist case, update snd_max but since we are in
1689 * persist mode (no window) we do not update snd_nxt.
1694 if (flags
& TH_FIN
) {
1696 tp
->t_flags
|= TF_SENTFIN
;
1698 if (SEQ_GT(tp
->snd_nxt
+ xlen
, tp
->snd_max
))
1699 tp
->snd_max
= tp
->snd_nxt
+ len
;
1706 if (so_options
& SO_DEBUG
)
1707 tcp_trace(TA_OUTPUT
, tp
->t_state
, tp
, mtod(m
, void *), th
, 0);
1711 * Fill in IP length and desired time to live and
1712 * send to IP level. There should be a better way
1713 * to handle ttl and tos; we could keep them in
1714 * the template, but need a way to checksum without them.
1718 * m->m_pkthdr.len should have been set before cksum calcuration,
1719 * because in6_cksum() need it.
1723 * we separately set hoplimit for every segment, since the
1724 * user might want to change the value via setsockopt.
1725 * Also, desired default hop limit might be changed via
1726 * Neighbor Discovery.
1728 ip6
->ip6_hlim
= in6_selecthlim(tp
->t_inpcb
,
1729 tp
->t_inpcb
->in6p_route
.ro_rt
?
1730 tp
->t_inpcb
->in6p_route
.ro_rt
->rt_ifp
1733 /* TODO: IPv6 IP6TOS_ECT bit on */
1734 KERNEL_DEBUG(DBG_LAYER_BEG
,
1735 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
1736 (((tp
->t_inpcb
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
1737 (tp
->t_inpcb
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
1742 ip
->ip_len
= m
->m_pkthdr
.len
;
1743 ip
->ip_ttl
= tp
->t_inpcb
->inp_ip_ttl
; /* XXX */
1744 ip
->ip_tos
|= (tp
->t_inpcb
->inp_ip_tos
& ~IPTOS_ECN_MASK
);/* XXX */
1745 KERNEL_DEBUG(DBG_LAYER_BEG
,
1746 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
1747 (((tp
->t_inpcb
->inp_laddr
.s_addr
& 0xffff) << 16) |
1748 (tp
->t_inpcb
->inp_faddr
.s_addr
& 0xffff)),
1753 * See if we should do MTU discovery.
1754 * Look at the flag updated on the following criterias:
1755 * 1) Path MTU discovery is authorized by the sysctl
1756 * 2) The route isn't set yet (unlikely but could happen)
1757 * 3) The route is up
1758 * 4) the MTU is not locked (if it is, then discovery has been
1759 * disabled for that route)
1764 if (path_mtu_discovery
&& (tp
->t_flags
& TF_PMTUD
))
1765 ip
->ip_off
|= IP_DF
;
1768 if (ipsec_bypass
== 0)
1769 ipsec_setsocket(m
, so
);
1773 * The socket is kept locked while sending out packets in ip_output, even if packet chaining is not active.
1776 m
->m_pkthdr
.socket_id
= socket_id
;
1779 * Embed the flow hash in pkt hdr and mark the packet as
1780 * capable of flow controlling
1782 m
->m_pkthdr
.m_flowhash
= tp
->t_inpcb
->inp_flowhash
;
1783 m
->m_pkthdr
.m_fhflags
|=
1784 (PF_TAG_TCP
| PF_TAG_FLOWHASH
| PF_TAG_FLOWADV
);
1786 m
->m_nextpkt
= NULL
;
1788 if (tp
->t_inpcb
->inp_last_outifp
!= NULL
&&
1789 tp
->t_inpcb
->inp_last_outifp
!= lo_ifp
) {
1790 /* Hint to prioritize this packet if
1791 * 1. if the packet has no data
1792 * 2. the interface supports transmit-start model and did
1793 * not disable ACK prioritization.
1794 * 3. Only ACK flag is set.
1795 * 4. there is no outstanding data on this connection.
1797 if (tcp_prioritize_acks
!= 0 && len
== 0 &&
1798 (tp
->t_inpcb
->inp_last_outifp
->if_eflags
&
1799 (IFEF_TXSTART
| IFEF_NOACKPRI
)) == IFEF_TXSTART
&&
1800 th
->th_flags
== TH_ACK
&& tp
->snd_una
== tp
->snd_max
&&
1801 tp
->t_timer
[TCPT_REXMT
] == 0) {
1802 svc_flags
|= PKT_SCF_TCP_ACK
;
1804 set_packet_service_class(m
, so
, MBUF_SC_UNSPEC
, svc_flags
);
1807 tp
->t_pktlist_sentlen
+= len
;
1812 DTRACE_TCP5(send
, struct mbuf
*, m
, struct inpcb
*, tp
->t_inpcb
,
1813 struct ip6
*, ip6
, struct tcpcb
*, tp
, struct tcphdr
*,
1818 DTRACE_TCP5(send
, struct mbuf
*, m
, struct inpcb
*, tp
->t_inpcb
,
1819 struct ip
*, ip
, struct tcpcb
*, tp
, struct tcphdr
*, th
);
1822 if (tp
->t_pktlist_head
!= NULL
) {
1823 tp
->t_pktlist_tail
->m_nextpkt
= m
;
1824 tp
->t_pktlist_tail
= m
;
1826 packchain_newlist
++;
1827 tp
->t_pktlist_head
= tp
->t_pktlist_tail
= m
;
1830 if ((lro_ackmore
) && (!sackoptlen
) && (!tp
->t_timer
[TCPT_PERSIST
]) &&
1831 ((th
->th_flags
& TH_ACK
) == TH_ACK
) && (!len
) &&
1832 (tp
->t_state
== TCPS_ESTABLISHED
)) {
1833 /* For a pure ACK, see if you need to send more of them */
1834 mnext
= tcp_send_lroacks(tp
, m
, th
);
1836 tp
->t_pktlist_tail
->m_nextpkt
= mnext
;
1837 if (mnext
->m_nextpkt
== NULL
) {
1838 tp
->t_pktlist_tail
= mnext
;
1841 struct mbuf
*tail
, *next
;
1842 next
= mnext
->m_nextpkt
;
1843 tail
= next
->m_nextpkt
;
1846 tail
= tail
->m_nextpkt
;
1849 tp
->t_pktlist_tail
= next
;
1854 if (sendalot
== 0 || (tp
->t_state
!= TCPS_ESTABLISHED
) ||
1855 (tp
->snd_cwnd
<= (tp
->snd_wnd
/ 8)) ||
1856 (tp
->t_flags
& (TH_PUSH
| TF_ACKNOW
)) || tp
->t_force
!= 0 ||
1857 tp
->t_lastchain
>= tcp_packet_chaining
) {
1859 while (tp
->t_inpcb
->inp_sndinprog_cnt
== 0 &&
1860 tp
->t_pktlist_head
!= NULL
) {
1861 packetlist
= tp
->t_pktlist_head
;
1862 packchain_listadd
= tp
->t_lastchain
;
1864 lost
= tp
->t_pktlist_sentlen
;
1865 TCP_PKTLIST_CLEAR(tp
);
1867 error
= tcp_ip_output(so
, tp
, packetlist
,
1868 packchain_listadd
, tp_inp_options
,
1869 (so_options
& SO_DONTROUTE
),
1870 (sack_rxmit
| (sack_bytes_rxmt
!= 0)), recwin
,
1879 * Take into account the rest of unsent
1880 * packets in the packet list for this tcp
1881 * into "lost", since we're about to free
1882 * the whole list below.
1884 lost
+= tp
->t_pktlist_sentlen
;
1890 /* tcp was closed while we were in ip; resume close */
1891 if (tp
->t_inpcb
->inp_sndinprog_cnt
== 0 &&
1892 (tp
->t_flags
& TF_CLOSING
)) {
1893 tp
->t_flags
&= ~TF_CLOSING
;
1894 (void) tcp_close(tp
);
1900 tcpstat
.tcps_sndtotal
++;
1906 * Assume that the packets were lost, so back out the
1907 * sequence number advance, if any. Note that the "lost"
1908 * variable represents the amount of user data sent during
1909 * the recent call to ip_output_list() plus the amount of
1910 * user data in the packet list for this tcp at the moment.
1912 if (tp
->t_force
== 0 || tp
->t_timer
[TCPT_PERSIST
] == 0) {
1914 * No need to check for TH_FIN here because
1915 * the TF_SENTFIN flag handles that case.
1917 if ((flags
& TH_SYN
) == 0) {
1920 tp
->sackhint
.sack_bytes_rexmit
-= lost
;
1922 tp
->snd_nxt
-= lost
;
1926 if (tp
->t_pktlist_head
!= NULL
)
1927 m_freem_list(tp
->t_pktlist_head
);
1928 TCP_PKTLIST_CLEAR(tp
);
1930 if (error
== ENOBUFS
) {
1931 if (!tp
->t_timer
[TCPT_REXMT
] &&
1932 !tp
->t_timer
[TCPT_PERSIST
])
1933 tp
->t_timer
[TCPT_REXMT
] =
1934 OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
1936 tp
->snd_cwnd
= tp
->t_maxseg
;
1937 tp
->t_bytes_acked
= 0;
1939 tcp_check_timer_state(tp
);
1940 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1942 DTRACE_TCP5(cc
, void, NULL
, struct inpcb
*, tp
->t_inpcb
,
1943 struct tcpcb
*, tp
, struct tcphdr
*, NULL
,
1944 int32_t, TCP_CC_OUTPUT_ERROR
);
1947 if (error
== EMSGSIZE
) {
1949 * ip_output() will have already fixed the route
1950 * for us. tcp_mtudisc() will, as its last action,
1951 * initiate retransmission, so it is important to
1954 * If TSO was active we either got an interface
1955 * without TSO capabilits or TSO was turned off.
1956 * Disable it for this connection as too and
1957 * immediatly retry with MSS sized segments generated
1961 tp
->t_flags
&= ~TF_TSO
;
1963 tcp_mtudisc(tp
->t_inpcb
, 0);
1964 tcp_check_timer_state(tp
);
1966 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1969 if ((error
== EHOSTUNREACH
|| error
== ENETDOWN
)
1970 && TCPS_HAVERCVDSYN(tp
->t_state
)) {
1971 tp
->t_softerror
= error
;
1972 tcp_check_timer_state(tp
);
1973 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1976 tcp_check_timer_state(tp
);
1977 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1981 tcpstat
.tcps_sndtotal
++;
1983 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
,0,0,0,0,0);
1987 tcp_check_timer_state(tp
);
1992 tcp_ip_output(struct socket
*so
, struct tcpcb
*tp
, struct mbuf
*pkt
,
1993 int cnt
, struct mbuf
*opt
, int flags
, int sack_in_progress
, int recwin
,
1998 boolean_t unlocked
= FALSE
;
1999 struct inpcb
*inp
= tp
->t_inpcb
;
2000 struct ip_out_args ipoa
=
2001 { IFSCOPE_NONE
, { 0 }, IPOAF_SELECT_SRCIF
| IPOAF_BOUND_SRCADDR
};
2003 struct ifnet
*outif
= NULL
;
2005 struct ip6_out_args ip6oa
=
2006 { IFSCOPE_NONE
, { 0 }, IP6OAF_SELECT_SRCIF
| IP6OAF_BOUND_SRCADDR
};
2007 struct route_in6 ro6
;
2008 struct flowadv
*adv
=
2009 (isipv6
? &ip6oa
.ip6oa_flowadv
: &ipoa
.ipoa_flowadv
);
2011 struct flowadv
*adv
= &ipoa
.ipoa_flowadv
;
2014 /* If socket was bound to an ifindex, tell ip_output about it */
2015 if (inp
->inp_flags
& INP_BOUND_IF
) {
2018 ip6oa
.ip6oa_boundif
= inp
->inp_boundifp
->if_index
;
2019 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
2023 ipoa
.ipoa_boundif
= inp
->inp_boundifp
->if_index
;
2024 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
2028 if (inp
->inp_flags
& INP_NO_IFT_CELLULAR
) {
2031 ip6oa
.ip6oa_flags
|= IP6OAF_NO_CELLULAR
;
2034 ipoa
.ipoa_flags
|= IPOAF_NO_CELLULAR
;
2038 flags
|= IPV6_OUTARGS
;
2041 flags
|= IP_OUTARGS
;
2043 /* Copy the cached route and take an extra reference */
2046 in6p_route_copyout(inp
, &ro6
);
2049 inp_route_copyout(inp
, &ro
);
2052 * Data sent (as far as we can tell).
2053 * If this advertises a larger window than any other segment,
2054 * then remember the size of the advertised window.
2055 * Make sure ACK/DELACK conditions are cleared before
2056 * we unlock the socket.
2058 if (recwin
> 0 && SEQ_GT(tp
->rcv_nxt
+ recwin
, tp
->rcv_adv
))
2059 tp
->rcv_adv
= tp
->rcv_nxt
+ recwin
;
2060 tp
->last_ack_sent
= tp
->rcv_nxt
;
2061 tp
->t_flags
&= ~(TF_ACKNOW
| TF_DELACK
);
2062 tp
->t_timer
[TCPT_DELACK
] = 0;
2063 tp
->t_unacksegs
= 0;
2065 /* Increment the count of outstanding send operations */
2066 inp
->inp_sndinprog_cnt
++;
2069 * If allowed, unlock TCP socket while in IP
2070 * but only if the connection is established and
2071 * in a normal mode where reentrancy on the tcpcb won't be
2073 * - there is no SACK episode
2074 * - we're not in Fast Recovery mode
2075 * - if we're not sending from an upcall.
2077 if (tcp_output_unlocked
&& !so
->so_upcallusecount
&&
2078 (tp
->t_state
== TCPS_ESTABLISHED
) && (sack_in_progress
== 0) &&
2079 ((tp
->t_flags
& TF_FASTRECOVERY
) == 0)) {
2082 socket_unlock(so
, 0);
2086 * Don't send down a chain of packets when:
2087 * - TCP chaining is disabled
2088 * - there is an IPsec rule set
2089 * - there is a non default rule set for the firewall
2092 chain
= tcp_packet_chaining
> 1
2097 && (fw_enable
== 0 || fw_bypass
)
2099 ; // I'm important, not extraneous
2102 while (pkt
!= NULL
) {
2103 struct mbuf
*npkt
= pkt
->m_nextpkt
;
2106 pkt
->m_nextpkt
= NULL
;
2108 * If we are not chaining, make sure to set the packet
2109 * list count to 0 so that IP takes the right path;
2110 * this is important for cases such as IPSec where a
2111 * single mbuf might result in multiple mbufs as part
2112 * of the encapsulation. If a non-zero count is passed
2113 * down to IP, the head of the chain might change and
2114 * we could end up skipping it (thus generating bogus
2115 * packets). Fixing it in IP would be desirable, but
2116 * for now this would do it.
2122 error
= ip6_output_list(pkt
, cnt
,
2123 inp
->in6p_outputopts
, &ro6
, flags
, NULL
, NULL
,
2127 error
= ip_output_list(pkt
, cnt
, opt
, &ro
, flags
, NULL
,
2130 if (chain
|| error
) {
2132 * If we sent down a chain then we are done since
2133 * the callee had taken care of everything; else
2134 * we need to free the rest of the chain ourselves.
2147 * Enter flow controlled state if the connection is established
2148 * and is not in recovery.
2150 * A connection will enter suspended state even if it is in
2153 if (((adv
->code
== FADV_FLOW_CONTROLLED
&& !IN_FASTRECOVERY(tp
)) ||
2154 adv
->code
== FADV_SUSPENDED
) &&
2155 !(tp
->t_flags
& TF_CLOSING
) &&
2156 tp
->t_state
== TCPS_ESTABLISHED
) {
2158 rc
= inp_set_fc_state(inp
, adv
->code
);
2161 DTRACE_TCP5(cc
, void, NULL
, struct inpcb
*, inp
,
2162 struct tcpcb
*, tp
, struct tcphdr
*, NULL
,
2163 int32_t, ((adv
->code
== FADV_FLOW_CONTROLLED
) ?
2164 TCP_CC_FLOW_CONTROL
: TCP_CC_SUSPEND
));
2168 * When an interface queue gets suspended, some of the
2169 * packets are dropped. Return ENOBUFS, to update the
2172 if (adv
->code
== FADV_SUSPENDED
)
2175 VERIFY(inp
->inp_sndinprog_cnt
> 0);
2176 if ( --inp
->inp_sndinprog_cnt
== 0)
2177 inp
->inp_flags
&= ~(INP_FC_FEEDBACK
);
2181 if (ro6
.ro_rt
!= NULL
&& (outif
= ro6
.ro_rt
->rt_ifp
) !=
2182 inp
->in6p_last_outifp
)
2183 inp
->in6p_last_outifp
= outif
;
2186 if (ro
.ro_rt
!= NULL
&& (outif
= ro
.ro_rt
->rt_ifp
) !=
2187 inp
->inp_last_outifp
)
2188 inp
->inp_last_outifp
= outif
;
2190 if ((inp
->inp_flags
& INP_NO_IFT_CELLULAR
) && outif
!= NULL
&&
2191 outif
->if_type
== IFT_CELLULAR
)
2192 soevent(inp
->inp_socket
,
2193 (SO_FILT_HINT_LOCKED
|SO_FILT_HINT_IFDENIED
));
2195 /* Synchronize cached PCB route & options */
2198 in6p_route_copyin(inp
, &ro6
);
2201 inp_route_copyin(inp
, &ro
);
2203 if (tp
->t_state
< TCPS_ESTABLISHED
&& tp
->t_rxtshift
== 0 &&
2204 tp
->t_inpcb
->inp_route
.ro_rt
!= NULL
) {
2205 /* If we found the route and there is an rtt on it
2206 * reset the retransmit timer
2208 tcp_getrt_rtt(tp
, tp
->t_inpcb
->in6p_route
.ro_rt
);
2209 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
2216 register struct tcpcb
*tp
;
2218 int t
= ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1;
2220 /* If a PERSIST_TIMER option was set we will limit the
2221 * time the persist timer will be active for that connection
2222 * in order to avoid DOS by using zero window probes.
2223 * see rdar://5805356
2226 if ((tp
->t_persist_timeout
!= 0) &&
2227 (tp
->t_timer
[TCPT_PERSIST
] == 0) &&
2228 (tp
->t_persist_stop
== 0)) {
2229 tp
->t_persist_stop
= tcp_now
+ tp
->t_persist_timeout
;
2233 * Start/restart persistance timer.
2235 TCPT_RANGESET(tp
->t_timer
[TCPT_PERSIST
],
2236 t
* tcp_backoff
[tp
->t_rxtshift
],
2237 TCPTV_PERSMIN
, TCPTV_PERSMAX
, 0);
2238 tp
->t_timer
[TCPT_PERSIST
] = OFFSET_FROM_START(tp
, tp
->t_timer
[TCPT_PERSIST
]);
2240 if (tp
->t_rxtshift
< TCP_MAXRXTSHIFT
)
2245 * Send as many acks as data coalesced. Every other packet when stretch
2246 * ACK is not enabled. Every 8 packets, if stretch ACK is enabled.
2249 tcp_send_lroacks(struct tcpcb
*tp
, struct mbuf
*m
, struct tcphdr
*th
)
2251 struct mbuf
*mnext
= NULL
, *ack_chain
= NULL
, *tail
= NULL
;
2253 tcp_seq org_ack
= ntohl(th
->th_ack
);
2254 tcp_seq prev_ack
= 0;
2255 int tack_offset
= 28; /* XXX IPv6 not supported */
2256 int ack_size
= (tp
->t_flags
& TF_STRETCHACK
) ?
2257 (maxseg_unacked
* tp
->t_maxseg
) : (tp
->t_maxseg
<< 1);
2258 int segs_acked
= (tp
->t_flags
& TF_STRETCHACK
) ? maxseg_unacked
: 2;
2259 struct mbuf
*prev_ack_pkt
= NULL
;
2260 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
2262 count
= tp
->t_lropktlen
/tp
->t_maxseg
;
2264 prev_ack
= (org_ack
- tp
->t_lropktlen
) + ack_size
;
2265 if (prev_ack
< org_ack
) {
2266 ack_chain
= m_dup(m
, M_DONTWAIT
);
2268 th
->th_ack
= htonl(prev_ack
);
2270 count
-= segs_acked
; /* accounts for prev_ack packet */
2271 count
= (count
<= segs_acked
) ? 0 : count
- segs_acked
;
2272 tcpstat
.tcps_sndacks
++;
2273 so_tc_update_stats(m
, so
, m_get_service_class(m
));
2279 tp
->t_lropktlen
= 0;
2283 prev_ack_pkt
= ack_chain
;
2286 if ((prev_ack
+ ack_size
) < org_ack
) {
2287 prev_ack
+= ack_size
;
2290 * The last ACK sent must have the ACK number that TCP
2291 * thinks is the last sent ACK number.
2295 mnext
= m_dup(prev_ack_pkt
, M_DONTWAIT
);
2298 bcopy(&prev_ack
, mtod(prev_ack_pkt
, caddr_t
) + tack_offset
, 4);
2300 tail
->m_nextpkt
= mnext
;
2302 count
-= segs_acked
;
2303 tcpstat
.tcps_sndacks
++;
2304 so_tc_update_stats(m
, so
, m_get_service_class(m
));
2305 if (lrodebug
== 5) {
2306 printf("%s: lropktlen = %d count = %d, th_ack = %x \n",
2307 __func__
, tp
->t_lropktlen
, count
,
2311 if (lrodebug
== 5) {
2312 printf("%s: failed to alloc mbuf.\n", __func__
);
2316 prev_ack_pkt
= mnext
;
2318 tp
->t_lropktlen
= 0;