1 /*
2 * Copyright (c) 2000-2024 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
61 * $FreeBSD: src/sys/netinet/tcp_output.c,v 1.39.2.10 2001/07/07 04:30:38 silby Exp $
62 */
63 /*
64 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
67 * Version 2.0.
68 */
69
70 #define _IP_VHL
71
72 #include "tcp_includes.h"
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/sysctl.h>
78 #include <sys/mbuf.h>
79 #include <sys/domain.h>
80 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <os/ptrtools.h>
84
85 #include <net/route.h>
86 #include <net/ntstat.h>
87 #include <net/if_var.h>
88 #include <net/if.h>
89 #include <net/if_types.h>
90 #include <net/dlil.h>
91 #include <net/droptap.h>
92
93 #include <netinet/in.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/in_var.h>
96 #include <netinet/in_tclass.h>
97 #include <netinet/ip.h>
98 #include <netinet/in_pcb.h>
99 #include <netinet/ip_var.h>
100 #include <mach/sdt.h>
101 #include <netinet6/in6_pcb.h>
102 #include <netinet/ip6.h>
103 #include <netinet6/ip6_var.h>
104 #include <netinet/tcp.h>
105 #include <netinet/tcp_cache.h>
106 #include <netinet/tcp_fsm.h>
107 #include <netinet/tcp_seq.h>
108 #include <netinet/tcp_timer.h>
109 #include <netinet/tcp_var.h>
110 #include <netinet/tcpip.h>
111 #include <netinet/tcp_cc.h>
112 #include <netinet/tcp_log.h>
113 #include <sys/kdebug.h>
114 #include <mach/sdt.h>
115
116 #if IPSEC
117 #include <netinet6/ipsec.h>
118 #endif /*IPSEC*/
119
120 #if MPTCP
121 #include <netinet/mptcp_var.h>
122 #include <netinet/mptcp.h>
123 #include <netinet/mptcp_opt.h>
124 #include <netinet/mptcp_seq.h>
125 #endif
126
127 #include <corecrypto/ccaes.h>
128
129 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 1)
130 #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 3)
131 #define DBG_FNC_TCP_OUTPUT NETDBG_CODE(DBG_NETTCP, (4 << 8) | 1)
132
133 SYSCTL_SKMEM_TCP_INT(OID_AUTO, path_mtu_discovery,
134 CTLFLAG_RW | CTLFLAG_LOCKED, int, path_mtu_discovery, 1,
135 "Enable Path MTU Discovery");
136
137 SYSCTL_SKMEM_TCP_INT(OID_AUTO, local_slowstart_flightsize,
138 CTLFLAG_RW | CTLFLAG_LOCKED, int, ss_fltsz_local, 8,
139 "Slow start flight size for local networks");
140
141 SYSCTL_SKMEM_TCP_INT(OID_AUTO, tso, CTLFLAG_RW | CTLFLAG_LOCKED,
142 int, tcp_do_tso, 1, "Enable TCP Segmentation Offload");
143
144 SYSCTL_SKMEM_TCP_INT(OID_AUTO, ecn_setup_percentage,
145 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_ecn_setup_percentage, 100,
146 "Max ECN setup percentage");
147
148 SYSCTL_SKMEM_TCP_INT(OID_AUTO, accurate_ecn,
149 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_acc_ecn, 0,
150 "Accurate ECN mode (0: disable, 1: enable Accurate ECN feedback");
151
152 int tcp_l4s_developer = 0;
153 SYSCTL_INT(_net_inet_tcp, OID_AUTO, l4s_developer,
154 CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_l4s_developer, 0,
155 "Developer L4S mode (0: system, 1: force enable L4S, 2: force disable L4S");
156
157 SYSCTL_SKMEM_TCP_INT(OID_AUTO, l4s,
158 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_l4s, 0,
159 "System L4S mode (0: disable, 1: enable L4S");
160
161 SYSCTL_SKMEM_TCP_INT(OID_AUTO, link_heuristics_flags,
162 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_link_heuristics_flags, TCP_LINK_HEURISTICS_DEFAULT,
163 "TCP LQM heuristics flags (1:rxmtcomp 2:noackpro 4:synrxmt 8:stealth 0x10:rtomin 0x20:notlp)");
164
165 SYSCTL_SKMEM_TCP_INT(OID_AUTO, link_heuristics_rto_min,
166 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_link_heuristics_rto_min, TCP_DEFAULT_LINK_HEUR_RTOMIN,
167 "");
168
169
170 // TO BE REMOVED
171 SYSCTL_SKMEM_TCP_INT(OID_AUTO, do_ack_compression,
172 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_do_ack_compression, 1,
173 "Enable TCP ACK compression (on (cell only): 1, off: 0, on (all interfaces): 2)");
174
175 SYSCTL_SKMEM_TCP_INT(OID_AUTO, ack_compression_rate,
176 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_ack_compression_rate, TCP_COMP_CHANGE_RATE,
177 "Rate at which we force sending new ACKs (in ms)");
178
179 SYSCTL_SKMEM_TCP_INT(OID_AUTO, randomize_timestamps,
180 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_randomize_timestamps, 1,
181 "Randomize TCP timestamps to prevent tracking (on: 1, off: 0)");
182
183 static int
184 sysctl_change_ecn_setting SYSCTL_HANDLER_ARGS
185 {
186 #pragma unused(oidp, arg1, arg2)
187 int i, err = 0, changed = 0;
188
189 err = sysctl_io_number(req, tcp_ecn, sizeof(int32_t),
190 &i, &changed);
191 if (err != 0 || req->newptr == USER_ADDR_NULL) {
192 return err;
193 }
194
195 if (changed) {
196 tcp_ecn = i;
197 SYSCTL_SKMEM_UPDATE_FIELD(tcp.ecn, tcp_ecn);
198 }
199 return err;
200 }
201
202 /* TODO: remove ecn_initiate_out once libnetcore ECN cleanup changes land */
203 int tcp_ecn_outbound = 2;
204 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, ecn_initiate_out,
205 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_ecn_outbound, 0,
206 sysctl_change_ecn_setting, "IU",
207 "Initiate ECN for outbound connections");
208
209 int tcp_ecn = 1;
210 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, ecn,
211 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_ecn, 0,
212 sysctl_change_ecn_setting, "IU",
213 "ECN system setting (0: disable, 1: enable)");
214
215 SYSCTL_SKMEM_TCP_INT(OID_AUTO, packetchain,
216 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_packet_chaining, 50,
217 "Enable TCP output packet chaining");
218
219 SYSCTL_SKMEM_TCP_INT(OID_AUTO, socket_unlocked_on_output,
220 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_output_unlocked, 1,
221 "Unlock TCP when sending packets down to IP");
222
223 SYSCTL_SKMEM_TCP_INT(OID_AUTO, min_iaj_win,
224 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_min_iaj_win, MIN_IAJ_WIN,
225 "Minimum recv win based on inter-packet arrival jitter");
226
227 SYSCTL_SKMEM_TCP_INT(OID_AUTO, acc_iaj_react_limit,
228 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_acc_iaj_react_limit,
229 ACC_IAJ_REACT_LIMIT, "Accumulated IAJ when receiver starts to react");
230
231 SYSCTL_SKMEM_TCP_INT(OID_AUTO, autosndbufinc,
232 CTLFLAG_RW | CTLFLAG_LOCKED, uint32_t, tcp_autosndbuf_inc,
233 8 * 1024, "Increment in send socket bufffer size");
234
235 SYSCTL_SKMEM_TCP_INT(OID_AUTO, autosndbufmax,
236 CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN, uint32_t, tcp_autosndbuf_max, 2 * 1024 * 1024,
237 "Maximum send socket buffer size");
238
239 SYSCTL_SKMEM_TCP_INT(OID_AUTO, rtt_recvbg,
240 CTLFLAG_RW | CTLFLAG_LOCKED, uint32_t, tcp_use_rtt_recvbg, 1,
241 "Use RTT for bg recv algorithm");
242
243 SYSCTL_SKMEM_TCP_INT(OID_AUTO, recv_throttle_minwin,
244 CTLFLAG_RW | CTLFLAG_LOCKED, uint32_t, tcp_recv_throttle_minwin, 16 * 1024,
245 "Minimum recv win for throttling");
246
247 SYSCTL_SKMEM_TCP_INT(OID_AUTO, enable_tlp,
248 CTLFLAG_RW | CTLFLAG_LOCKED,
249 int32_t, tcp_enable_tlp, 1, "Enable Tail loss probe");
250
251 static int32_t packchain_newlist = 0;
252 static int32_t packchain_looped = 0;
253 static int32_t packchain_sent = 0;
254
255 /* temporary: for testing */
256 #if IPSEC
257 extern int ipsec_bypass;
258 #endif
259
260 extern int slowlink_wsize; /* window correction for slow links */
261
262 extern u_int32_t kipf_count;
263
264 static int tcp_ip_output(struct socket *, struct tcpcb *, struct mbuf *,
265 int, struct mbuf *, int, int, boolean_t);
266 static int tcp_recv_throttle(struct tcpcb *tp);
267
268 __attribute__((noinline))
269 static int32_t
tcp_tfo_check(struct tcpcb * tp,int32_t len)270 tcp_tfo_check(struct tcpcb *tp, int32_t len)
271 {
272 struct socket *__single so = tp->t_inpcb->inp_socket;
273 unsigned int optlen = 0;
274 unsigned int cookie_len;
275
276 if (tp->t_flags & TF_NOOPT) {
277 goto fallback;
278 }
279
280 if (!(tp->t_flagsext & TF_FASTOPEN_FORCE_ENABLE) &&
281 !tcp_heuristic_do_tfo(tp)) {
282 tp->t_tfo_stats |= TFO_S_HEURISTICS_DISABLE;
283 tcpstat.tcps_tfo_heuristics_disable++;
284 goto fallback;
285 }
286
287 if (so->so_flags1 & SOF1_DATA_AUTHENTICATED) {
288 return len;
289 }
290
291 optlen += TCPOLEN_MAXSEG;
292
293 if (tp->t_flags & TF_REQ_SCALE) {
294 optlen += 4;
295 }
296
297 #if MPTCP
298 if ((so->so_flags & SOF_MP_SUBFLOW) && mptcp_enable &&
299 (tp->t_rxtshift <= mptcp_mpcap_retries ||
300 (tptomptp(tp)->mpt_mpte->mpte_flags & MPTE_FORCE_ENABLE))) {
301 optlen += sizeof(struct mptcp_mpcapable_opt_common) + sizeof(mptcp_key_t);
302 }
303 #endif /* MPTCP */
304
305 if (tp->t_flags & TF_REQ_TSTMP) {
306 optlen += TCPOLEN_TSTAMP_APPA;
307 }
308
309 if (SACK_ENABLED(tp)) {
310 optlen += TCPOLEN_SACK_PERMITTED;
311 }
312
313 /* Now, decide whether to use TFO or not */
314
315 /* Don't even bother trying if there is no space at all... */
316 if (MAX_TCPOPTLEN - optlen < TCPOLEN_FASTOPEN_REQ) {
317 goto fallback;
318 }
319
320 cookie_len = tcp_cache_get_cookie_len(tp);
321 if (cookie_len == 0) {
322 /* No cookie, so we request one */
323 return 0;
324 }
325
326 /* There is not enough space for the cookie, so we cannot do TFO */
327 if (MAX_TCPOPTLEN - optlen < cookie_len) {
328 goto fallback;
329 }
330
331 /* Do not send SYN+data if there is more in the queue than MSS */
332 if (so->so_snd.sb_cc > (tp->t_maxopd - MAX_TCPOPTLEN)) {
333 goto fallback;
334 }
335
336 /* Ok, everything looks good. We can go on and do TFO */
337 return len;
338
339 fallback:
340 tcp_disable_tfo(tp);
341 return 0;
342 }
343
344 /* Returns the number of bytes written to the TCP option-space */
345 __attribute__((noinline))
346 static unsigned int
347 tcp_tfo_write_cookie_rep(struct tcpcb *tp, unsigned int optlen,
348 u_char *__counted_by(optlen + 2 + TFO_COOKIE_LEN_DEFAULT) opt)
349 {
350 u_char out[CCAES_BLOCK_SIZE];
351 unsigned ret = 0;
352 u_char *bp;
353
354 if (MAX_TCPOPTLEN - optlen <
355 TCPOLEN_FASTOPEN_REQ + TFO_COOKIE_LEN_DEFAULT) {
356 return ret;
357 }
358
359 tcp_tfo_gen_cookie(tp->t_inpcb, out, sizeof(out));
360
361 bp = opt + optlen;
362
363 *bp++ = TCPOPT_FASTOPEN;
364 *bp++ = 2 + TFO_COOKIE_LEN_DEFAULT;
365 memcpy(bp, out, TFO_COOKIE_LEN_DEFAULT);
366 ret += 2 + TFO_COOKIE_LEN_DEFAULT;
367
368 tp->t_tfo_stats |= TFO_S_COOKIE_SENT;
369 tcpstat.tcps_tfo_cookie_sent++;
370
371 return ret;
372 }
373
374 __attribute__((noinline))
375 static unsigned int
tcp_tfo_write_cookie(struct tcpcb * tp,unsigned int optlen,int32_t len,u_char * __counted_by (TCP_MAXOLEN)opt)376 tcp_tfo_write_cookie(struct tcpcb *tp, unsigned int optlen, int32_t len,
377 u_char *__counted_by(TCP_MAXOLEN) opt)
378 {
379 uint8_t tfo_len;
380 struct socket *__single so = tp->t_inpcb->inp_socket;
381 unsigned ret = 0;
382 int res;
383 u_char *bp;
384
385 if (TCPOLEN_FASTOPEN_REQ > MAX_TCPOPTLEN - optlen) {
386 return 0;
387 }
388 tfo_len = (uint8_t)(MAX_TCPOPTLEN - optlen - TCPOLEN_FASTOPEN_REQ);
389
390 if (so->so_flags1 & SOF1_DATA_AUTHENTICATED) {
391 /* If there is some data, let's track it */
392 if (len > 0) {
393 tp->t_tfo_stats |= TFO_S_SYN_DATA_SENT;
394 tcpstat.tcps_tfo_syn_data_sent++;
395 }
396
397 return 0;
398 }
399
400 bp = opt + optlen;
401
402 /*
403 * The cookie will be copied in the appropriate place within the
404 * TCP-option space. That way we avoid the need for an intermediate
405 * variable.
406 */
407 res = tcp_cache_get_cookie(tp, bp + TCPOLEN_FASTOPEN_REQ, tfo_len, &tfo_len);
408 if (res == 0) {
409 *bp++ = TCPOPT_FASTOPEN;
410 *bp++ = TCPOLEN_FASTOPEN_REQ;
411 ret += TCPOLEN_FASTOPEN_REQ;
412
413 tp->t_tfo_flags |= TFO_F_COOKIE_REQ;
414
415 tp->t_tfo_stats |= TFO_S_COOKIE_REQ;
416 tcpstat.tcps_tfo_cookie_req++;
417 } else {
418 *bp++ = TCPOPT_FASTOPEN;
419 *bp++ = TCPOLEN_FASTOPEN_REQ + tfo_len;
420
421 ret += TCPOLEN_FASTOPEN_REQ + tfo_len;
422
423 tp->t_tfo_flags |= TFO_F_COOKIE_SENT;
424
425 /* If there is some data, let's track it */
426 if (len > 0) {
427 tp->t_tfo_stats |= TFO_S_SYN_DATA_SENT;
428 tcpstat.tcps_tfo_syn_data_sent++;
429 }
430 }
431
432 return ret;
433 }
434
435 static inline bool
tcp_send_ecn_flags_on_syn(struct tcpcb * tp)436 tcp_send_ecn_flags_on_syn(struct tcpcb *tp)
437 {
438 /* We allow Accurate ECN negotiation on first retransmission as well */
439 bool send_on_first_retrans = (tp->ecn_flags & TE_ACE_SETUPSENT) &&
440 (tp->t_rxtshift <= 1);
441
442 return !(tp->ecn_flags & (TE_SETUPSENT | TE_ACE_SETUPSENT)) || send_on_first_retrans;
443 }
444
445 void
tcp_set_l4s(struct tcpcb * tp,struct ifnet * ifp)446 tcp_set_l4s(struct tcpcb *tp, struct ifnet *ifp)
447 {
448 if (tp->t_state >= TCPS_ESTABLISHED) {
449 return;
450 }
451
452 /*
453 * L4S is enabled if,
454 * 1. It is not disabled explicitly by developer or interface setting or tcp options
455 * 2. It is enabled either by developer or interface setting or A/B deployment or tcp_options,
456 * It implicitly enables Accurate ECN which supports ACE and AccECN option for ECN feedback
457 */
458 bool l4s_disabled = (tcp_l4s_developer == tcp_l4s_developer_disable ||
459 (ifp != NULL && ifp->if_l4s_mode == IFRTYPE_L4S_DISABLE) ||
460 (tp->t_flagsext & TF_L4S_DISABLED) == 1);
461
462 tp->l4s_enabled = !l4s_disabled && (tcp_l4s_developer == tcp_l4s_developer_enable ||
463 (ifp != NULL && ifp->if_l4s_mode == IFRTYPE_L4S_ENABLE) || tcp_l4s == 1 ||
464 ((tp->t_flagsext & TF_L4S_ENABLED)));
465 }
466
467 void
tcp_set_accurate_ecn(struct tcpcb * tp)468 tcp_set_accurate_ecn(struct tcpcb *tp)
469 {
470 if ((tp->ecn_flags & TE_ACC_ECN_ON) == TE_ACC_ECN_ON) {
471 tp->accurate_ecn_on = true;
472 } else {
473 tp->accurate_ecn_on = false;
474 }
475 }
476
477 void
tcp_set_ecn(struct tcpcb * tp)478 tcp_set_ecn(struct tcpcb *tp)
479 {
480 bool ecn_enabled = tcp_ecn_enabled(tp->ecn_flags);
481
482 if (!ecn_enabled || !tcp_heuristic_do_ecn(tp)) {
483 tp->ecn_flags &= ~TE_ENABLE_ECN;
484 return;
485 }
486
487 /* ECN is enabled based on system settings */
488 tp->ecn_flags |= TE_ENABLE_ECN;
489
490 if (tp->l4s_enabled) {
491 /* Set the accurate ECN state */
492 if (tp->t_client_accecn_state == tcp_connection_client_accurate_ecn_feature_disabled) {
493 tp->t_client_accecn_state = tcp_connection_client_accurate_ecn_feature_enabled;
494 }
495 if (tp->t_server_accecn_state == tcp_connection_server_accurate_ecn_feature_disabled) {
496 tp->t_server_accecn_state = tcp_connection_server_accurate_ecn_feature_enabled;
497 }
498 }
499 }
500
501 bool
tcp_ecn_enabled(uint32_t ecn_flags)502 tcp_ecn_enabled(uint32_t ecn_flags)
503 {
504 /*
505 * Socket option has precedence
506 */
507 if (ecn_flags & TE_ECN_MODE_ENABLE) {
508 return true;
509 }
510 if (ecn_flags & TE_ECN_MODE_DISABLE) {
511 return false;
512 }
513
514 /*
515 * System wide settings come last
516 */
517 if (tcp_ecn == 1) {
518 return true;
519 } else {
520 return false;
521 }
522 }
523
524 uint32_t
tcp_flight_size(struct tcpcb * tp)525 tcp_flight_size(struct tcpcb *tp)
526 {
527 int ret;
528
529 VERIFY(tp->sackhint.sack_bytes_acked >= 0);
530 VERIFY(tp->sackhint.sack_bytes_rexmit >= 0);
531
532 /*
533 * RFC6675, SetPipe (), SACK'd bytes are discounted. All the rest is still in-flight.
534 */
535 ret = tp->snd_nxt - tp->snd_una - tp->sackhint.sack_bytes_acked;
536
537 if (TCP_RACK_ENABLED(tp)) {
538 /* In flight is bytes sent - bytes that left the network + bytes retransmitted */
539 const uint32_t bytes_sent = SEQ_MAX(tp->snd_max, tp->snd_nxt) - tp->snd_una;
540 const uint32_t bytes_not_in_flight = tp->bytes_sacked + tp->bytes_lost;
541 ret = bytes_sent - bytes_not_in_flight + tp->bytes_retransmitted;
542 }
543
544 if (ret < 0) {
545 /* It shouldn't happen when RACK is enabled */
546 if (TCP_RACK_ENABLED(tp)) {
547 os_log_error(OS_LOG_DEFAULT, "flight_size (%d) can't be negative "
548 "(snd_nxt:%u snd_max:%u, snd_una:%u, sacked:%u lost:%u retransmitted:%u)",
549 ret, tp->snd_nxt, tp->snd_max, tp->snd_una,
550 tp->bytes_sacked, tp->bytes_lost, tp->bytes_retransmitted);
551 }
552 /*
553 * This happens when the RTO-timer fires because snd_nxt gets artificially
554 * decreased. If we then receive some SACK-blogs, sack_bytes_acked is
555 * going to be high.
556 */
557 ret = 0;
558 }
559
560 return ret;
561 }
562
563 /*
564 * Either of ECT0 or ECT1 flag should be set
565 * when this function is called
566 */
567 static void
tcp_add_accecn_option(struct tcpcb * tp,uint16_t flags,uint32_t * __indexable lp,uint8_t * optlen)568 tcp_add_accecn_option(struct tcpcb *tp, uint16_t flags, uint32_t *__indexable lp, uint8_t *optlen)
569 {
570 uint8_t max_len = TCP_MAXOLEN - *optlen;
571 uint8_t len = TCPOLEN_ACCECN_EMPTY;
572
573 uint32_t e1b = (uint32_t)(tp->t_aecn.t_rcv_ect1_bytes & TCP_ACO_MASK);
574 uint32_t e0b = (uint32_t)(tp->t_aecn.t_rcv_ect0_bytes & TCP_ACO_MASK);
575 uint32_t ceb = (uint32_t)(tp->t_aecn.t_rcv_ce_bytes & TCP_ACO_MASK);
576
577 if (max_len < TCPOLEN_ACCECN_EMPTY) {
578 TCP_LOG(tp, "not enough space to add any AccECN option");
579 return;
580 }
581
582 if (!(flags & TH_SYN || (tp->ecn_flags & TE_ACE_FINAL_ACK_3WHS) ||
583 tp->snd_una == tp->iss + 1 ||
584 tp->ecn_flags & (TE_ACO_ECT1 | TE_ACO_ECT0))) {
585 /*
586 * Since this is neither a SYN-ACK packet, nor the final ACK of
587 * the 3WHS (nor the first acked data segment) nor any of the ECT byte
588 * counter flags are set, no need to send the option.
589 */
590 return;
591 }
592
593 if ((flags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK) &&
594 tp->t_rxtshift >= 1) {
595 /*
596 * If this is a SYN-ACK retransmission (first),
597 * retry without AccECN option and just with ACE fields.
598 * From second retransmission onwards, we don't send any
599 * Accurate ECN state.
600 */
601 return;
602 }
603
604 if (max_len < (TCPOLEN_ACCECN_EMPTY + 1 * TCPOLEN_ACCECN_COUNTER)) {
605 /* Can carry EMPTY option (2 bytes) which can be used to test path in SYN-ACK packet */
606 if (flags & TH_SYN) {
607 *(uint16_t *)lp++ = htons((TCPOPT_ACCECN1 << 8) | len);
608 *optlen += len;
609 }
610 } else if (max_len < (TCPOLEN_ACCECN_EMPTY + 2 * TCPOLEN_ACCECN_COUNTER)) {
611 /* Can carry one option */
612 len += 1 * TCPOLEN_ACCECN_COUNTER;
613 if (tp->ecn_flags & TE_ACO_ECT1) {
614 *lp++ = htonl((TCPOPT_ACCECN1 << 24) | (len << 16) | ((e1b >> 8) & 0xffff));
615 *(uint16_t *)lp++ = htons((uint16_t)((e1b & 0xff) << 8) | TCPOPT_NOP);
616 } else {
617 *lp++ = htonl((TCPOPT_ACCECN0 << 24) | (len << 16) | ((e0b >> 8) & 0xffff));
618 *(uint16_t *)lp++ = htons((uint16_t)((e0b & 0xff) << 8) | TCPOPT_NOP);
619 }
620 *optlen += len + 1; /* 1 NOPs */
621 } else if (max_len < (TCPOLEN_ACCECN_EMPTY + 3 * TCPOLEN_ACCECN_COUNTER)) {
622 /* Can carry two options */
623 len += 2 * TCPOLEN_ACCECN_COUNTER;
624 if (tp->ecn_flags & TE_ACO_ECT1) {
625 *lp++ = htonl((TCPOPT_ACCECN1 << 24) | (len << 16) | ((e1b >> 8) & 0xffff));
626 *lp++ = htonl(((e1b & 0xff) << 24) | (ceb & 0xffffff));
627 } else {
628 *lp++ = htonl((TCPOPT_ACCECN0 << 24) | (len << 16) | ((e0b >> 8) & 0xffff));
629 *lp++ = htonl(((e0b & 0xff) << 24) | (ceb & 0xffffff));
630 }
631 *optlen += len; /* 0 NOPs */
632 } else {
633 /*
634 * TCP option sufficient to hold full AccECN option
635 * but send counter that changed during the entire connection.
636 */
637 len += 3 * TCPOLEN_ACCECN_COUNTER;
638 /* Can carry all three options */
639 if (tp->ecn_flags & TE_ACO_ECT1) {
640 *lp++ = htonl((TCPOPT_ACCECN1 << 24) | (len << 16) | ((e1b >> 8) & 0xffff));
641 *lp++ = htonl(((e1b & 0xff) << 24) | (ceb & 0xffffff));
642 *lp++ = htonl(((e0b & 0xffffff) << 8) | TCPOPT_NOP);
643 } else {
644 *lp++ = htonl((TCPOPT_ACCECN0 << 24) | (len << 16) | ((e0b >> 8) & 0xffff));
645 *lp++ = htonl(((e0b & 0xff) << 24) | (ceb & 0xffffff));
646 *lp++ = htonl(((e1b & 0xffffff) << 8) | TCPOPT_NOP);
647 }
648 *optlen += len + 1; /* 1 NOP */
649 }
650 }
651
652 /*
653 * Insert TCP options according to the supplied parameters to the place
654 * optp in a consistent way. Can handle unaligned destinations.
655 *
656 * The order of the option processing is crucial for optimal packing and
657 * alignment for the scarce option space.
658 *
659 * The optimal order for a SYN/SYN-ACK segment is:
660 * MSS (4) + NOP (1) + Window scale (3) + SACK permitted (2) +
661 * Timestamp (10) + Signature (18) = 38 bytes out of a maximum of 40.
662 *
663 * The SACK options should be last. SACK blocks consume 8*n+2 bytes.
664 * So a full size SACK blocks option is 34 bytes (with 4 SACK blocks).
665 * At minimum we need 10 bytes (to generate 1 SACK block). If both
666 * TCP Timestamps (12 bytes) and TCP Signatures (18 bytes) are present,
667 * we only have 10 bytes for SACK options (40 - (12 + 18)).
668 */
669 uint8_t
tcp_addoptions(struct tcpopt * to,u_char * __ended_by (optend)optp,u_char * optend)670 tcp_addoptions(struct tcpopt *to, u_char * __ended_by(optend) optp, u_char * optend)
671 {
672 uint32_t mask;
673 uint8_t optlen = 0;
674
675 for (mask = 1; mask < TOF_MAXOPT; mask <<= 1) {
676 if ((to->to_flags & mask) != mask) {
677 continue;
678 }
679 if (optlen == TCP_MAXOLEN) {
680 break;
681 }
682 switch (to->to_flags & mask) {
683 case TOF_MSS:
684 while (optlen % 4) {
685 optlen += TCPOLEN_NOP;
686 *optp++ = TCPOPT_NOP;
687 }
688 if (TCP_MAXOLEN - optlen < TCPOLEN_MAXSEG) {
689 continue;
690 }
691 optlen += TCPOLEN_MAXSEG;
692 *optp++ = TCPOPT_MAXSEG;
693 *optp++ = TCPOLEN_MAXSEG;
694 to->to_mss = htons(to->to_mss);
695 bcopy((u_char *)&to->to_mss, optp, sizeof(to->to_mss));
696 optp += sizeof(to->to_mss);
697 optend = optend;
698 break;
699 case TOF_SCALE:
700 while (!optlen || optlen % 2 != 1) {
701 optlen += TCPOLEN_NOP;
702 *optp++ = TCPOPT_NOP;
703 }
704 if (TCP_MAXOLEN - optlen < TCPOLEN_WINDOW) {
705 continue;
706 }
707 optlen += TCPOLEN_WINDOW;
708 *optp++ = TCPOPT_WINDOW;
709 *optp++ = TCPOLEN_WINDOW;
710 *optp++ = to->to_wscale;
711 break;
712 case TOF_SACKPERM:
713 while (optlen % 2) {
714 optlen += TCPOLEN_NOP;
715 *optp++ = TCPOPT_NOP;
716 }
717 if (TCP_MAXOLEN - optlen < TCPOLEN_SACK_PERMITTED) {
718 continue;
719 }
720 optlen += TCPOLEN_SACK_PERMITTED;
721 *optp++ = TCPOPT_SACK_PERMITTED;
722 *optp++ = TCPOLEN_SACK_PERMITTED;
723 break;
724 case TOF_TS:
725 while (!optlen || optlen % 4 != 2) {
726 optlen += TCPOLEN_NOP;
727 *optp++ = TCPOPT_NOP;
728 }
729 if (TCP_MAXOLEN - optlen < TCPOLEN_TIMESTAMP) {
730 continue;
731 }
732 optlen += TCPOLEN_TIMESTAMP;
733 *optp++ = TCPOPT_TIMESTAMP;
734 *optp++ = TCPOLEN_TIMESTAMP;
735 to->to_tsval = htonl(to->to_tsval);
736 to->to_tsecr = htonl(to->to_tsecr);
737 bcopy((u_char *)&to->to_tsval, optp, sizeof(to->to_tsval));
738 optp += sizeof(to->to_tsval);
739 optend = optend;
740 bcopy((u_char *)&to->to_tsecr, optp, sizeof(to->to_tsecr));
741 optp += sizeof(to->to_tsecr);
742 optend = optend;
743 break;
744 case TOF_SACK:
745 {
746 int sackblks = 0;
747 struct sackblk *sack = (struct sackblk *)(void *)to->to_sacks;
748 tcp_seq sack_seq;
749
750 while (!optlen || optlen % 4 != 2) {
751 optlen += TCPOLEN_NOP;
752 *optp++ = TCPOPT_NOP;
753 }
754 if (TCP_MAXOLEN - optlen < TCPOLEN_SACKHDR + TCPOLEN_SACK) {
755 continue;
756 }
757 optlen += TCPOLEN_SACKHDR;
758 *optp++ = TCPOPT_SACK;
759 sackblks = min(to->to_nsacks,
760 (TCP_MAXOLEN - optlen) / TCPOLEN_SACK);
761 *optp++ = TCPOLEN_SACKHDR + (uint8_t)sackblks * TCPOLEN_SACK;
762 while (sackblks--) {
763 sack_seq = htonl(sack->start);
764 bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq));
765 optp += sizeof(sack_seq);
766 optend = optend;
767 sack_seq = htonl(sack->end);
768 bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq));
769 optp += sizeof(sack_seq);
770 optend = optend;
771 optlen += TCPOLEN_SACK;
772 sack++;
773 }
774 tcpstat.tcps_sack_send_blocks++;
775 break;
776 }
777 default:
778 /* SYN cookies are disabled when TFO is used */
779 break;
780 }
781 }
782
783 /* Terminate and pad TCP options to a 4 byte boundary. */
784 if (optlen % 4) {
785 optlen += TCPOLEN_EOL;
786 *optp++ = TCPOPT_EOL;
787 }
788 /*
789 * According to RFC 793 (STD0007):
790 * "The content of the header beyond the End-of-Option option
791 * must be header padding (i.e., zero)."
792 * and later: "The padding is composed of zeros."
793 */
794 while (optlen % 4) {
795 optlen += TCPOLEN_EOL;
796 *optp++ = TCPOPT_EOL;
797 }
798
799 ASSERT(optlen <= TCP_MAXOLEN);
800 return optlen;
801 }
802 /*
803 * Set up the ECN information for the <SYN,ACK> from
804 * client SYN information.
805 */
806 static uint16_t
tcp_accecn_synack_respond(struct tcpcb * tp,uint16_t thflags)807 tcp_accecn_synack_respond(struct tcpcb * tp, uint16_t thflags)
808 {
809 /* Server received either legacy or Accurate ECN setup SYN */
810 if (tp->ecn_flags & (TE_SETUPRECEIVED | TE_ACE_SETUPRECEIVED)) {
811 if (tcp_send_ecn_flags_on_syn(tp)) {
812 if (tp->l4s_enabled && (tp->ecn_flags & TE_ACE_SETUPRECEIVED)) {
813 /*
814 * Accurate ECN mode is on. Initialize packet and byte counters
815 * for the server sending SYN-ACK. Although s_cep will be initialized
816 * during input processing of ACK of SYN-ACK, initialize here as well
817 * in case ACK gets lost.
818 *
819 * Non-zero initial values are used to
820 * support a stateless handshake (see
821 * Section 5.1 of AccECN draft) and to be
822 * distinct from cases where the fields
823 * are incorrectly zeroed.
824 */
825 tp->t_aecn.t_rcv_ce_packets = 5;
826 tp->t_aecn.t_snd_ce_packets = 5;
827
828 /* Initialize CE byte counter to 0 */
829 tp->t_aecn.t_rcv_ce_bytes = tp->t_aecn.t_snd_ce_bytes = 0;
830
831 if (tp->ecn_flags & TE_ACE_SETUP_NON_ECT) {
832 tp->t_prev_ace_flags = TH_CWR;
833 thflags |= tp->t_prev_ace_flags;
834 /* Remove the setup flag as it is also used for final ACK */
835 tp->ecn_flags &= ~TE_ACE_SETUP_NON_ECT;
836 tcpstat.tcps_ecn_ace_syn_not_ect++;
837 } else if (tp->ecn_flags & TE_ACE_SETUP_ECT1) {
838 tp->t_prev_ace_flags = (TH_CWR | TH_ECE);
839 thflags |= tp->t_prev_ace_flags;
840 tp->ecn_flags &= ~TE_ACE_SETUP_ECT1;
841 tcpstat.tcps_ecn_ace_syn_ect1++;
842 } else if (tp->ecn_flags & TE_ACE_SETUP_ECT0) {
843 tp->t_prev_ace_flags = TH_AE;
844 thflags |= tp->t_prev_ace_flags;
845 tp->ecn_flags &= ~TE_ACE_SETUP_ECT0;
846 tcpstat.tcps_ecn_ace_syn_ect0++;
847 } else if (tp->ecn_flags & TE_ACE_SETUP_CE) {
848 tp->t_prev_ace_flags = (TH_AE | TH_CWR);
849 thflags |= tp->t_prev_ace_flags;
850 tp->ecn_flags &= ~TE_ACE_SETUP_CE;
851 /*
852 * Receive counter is updated on
853 * all acceptable packets except
854 * CE on SYN packets (SYN=1, ACK=0)
855 */
856 tcpstat.tcps_ecn_ace_syn_ce++;
857 } else {
858 if (tp->t_prev_ace_flags != 0) {
859 /* Set the flags for retransmitted SYN-ACK same as the previous one */
860 thflags |= tp->t_prev_ace_flags;
861 } else {
862 /* We shouldn't come here */
863 panic("ECN flags (0x%x) not set correctly", tp->ecn_flags);
864 }
865 }
866 /*
867 * We now send ECT1 packets when
868 * L4S and Accurate ECN mode is on
869 */
870 tp->ecn_flags |= TE_ACE_SETUPSENT;
871 if (tp->l4s_enabled) {
872 tp->ecn_flags |= TE_SENDIPECT;
873 tcp_set_accurate_ecn(tp);
874 }
875 } else if (tp->ecn_flags & TE_SETUPRECEIVED) {
876 /*
877 * Setting TH_ECE makes this an ECN-setup
878 * SYN-ACK
879 */
880 thflags |= TH_ECE;
881 /*
882 * Record that we sent the ECN-setup and
883 * default to setting IP ECT.
884 */
885 tp->ecn_flags |= (TE_SETUPSENT | TE_SENDIPECT);
886 }
887 tcpstat.tcps_ecn_server_setup++;
888 tcpstat.tcps_ecn_server_success++;
889 } else {
890 /*
891 * For classic ECN, we sent an ECN-setup SYN-ACK but it was
892 * dropped. Fallback to non-ECN-setup
893 * SYN-ACK and clear flag to indicate that
894 * we should not send data with IP ECT set
895 *
896 * Pretend we didn't receive an
897 * ECN-setup SYN.
898 *
899 * We already incremented the counter
900 * assuming that the ECN setup will
901 * succeed. Decrementing here
902 * tcps_ecn_server_success to correct it.
903 *
904 * For Accurate ECN, we don't yet remove TE_ACE_SETUPRECEIVED
905 * as the client might have received Accurate ECN SYN-ACK.
906 * We decide Accurate ECN's state on processing last ACK from the client.
907 */
908 if (tp->ecn_flags & (TE_SETUPSENT | TE_ACE_SETUPSENT)) {
909 tcpstat.tcps_ecn_lost_synack++;
910 tcpstat.tcps_ecn_server_success--;
911 tp->ecn_flags |= TE_LOST_SYNACK;
912 }
913 if (!tp->l4s_enabled) {
914 /* Do this only for classic ECN. */
915 tp->ecn_flags &=
916 ~(TE_SETUPRECEIVED | TE_SENDIPECT |
917 TE_SENDCWR);
918 }
919 }
920 }
921 return thflags;
922 }
923
924 /*
925 * Tcp output routine: figure out what should be sent and send it.
926 *
927 * Returns: 0 Success
928 * EADDRNOTAVAIL
929 * ENOBUFS
930 * EMSGSIZE
931 * EHOSTUNREACH
932 * ENETDOWN
933 * ip_output_list:ENOMEM
934 * ip_output_list:EADDRNOTAVAIL
935 * ip_output_list:ENETUNREACH
936 * ip_output_list:EHOSTUNREACH
937 * ip_output_list:EACCES
938 * ip_output_list:EMSGSIZE
939 * ip_output_list:ENOBUFS
940 * ip_output_list:??? [ignorable: mostly IPSEC/firewall/DLIL]
941 * ip6_output_list:EINVAL
942 * ip6_output_list:EOPNOTSUPP
943 * ip6_output_list:EHOSTUNREACH
944 * ip6_output_list:EADDRNOTAVAIL
945 * ip6_output_list:ENETUNREACH
946 * ip6_output_list:EMSGSIZE
947 * ip6_output_list:ENOBUFS
948 * ip6_output_list:??? [ignorable: mostly IPSEC/firewall/DLIL]
949 */
950 int
tcp_output(struct tcpcb * tp)951 tcp_output(struct tcpcb *tp)
952 {
953 uint32_t tcp_now_local = os_access_once(tcp_now);
954 uint32_t *tsvalptr;
955 uint64_t pacing_tx_time;
956 struct inpcb *__single inp = tp->t_inpcb;
957 struct socket *__single so = inp->inp_socket;
958 int32_t len, recwin, sendwin, off;
959 uint32_t max_len = 0;
960 uint16_t flags;
961 int error;
962 mbuf_ref_t m;
963 struct ip *ip = NULL;
964 struct ip6_hdr *ip6 = NULL;
965 struct tcphdr *th;
966 u_char opt[TCP_MAXOLEN];
967 unsigned int ipoptlen, optlen, hdrlen;
968 int idle, sendalot, lost = 0;
969 int sendalot_cnt = 0;
970 int i, rack_sack_rxmit = 0;
971 int tso = 0;
972 int sack_bytes_rxmt;
973 tcp_seq old_snd_nxt = 0;
974 struct sackhole *p;
975 struct tcp_seg_sent *seg;
976 #if IPSEC
977 size_t ipsec_optlen = 0;
978 #endif /* IPSEC */
979 int idle_time = 0;
980 struct mbuf *__single packetlist = NULL;
981 struct mbuf *__single tp_inp_options = inp->inp_depend4.inp4_options;
982 int isipv6 = inp->inp_vflag & INP_IPV6;
983 int packchain_listadd = 0;
984 int so_options = so->so_options;
985 rtentry_ref_t rt;
986 u_int32_t svc_flags = 0, allocated_len;
987 #if MPTCP
988 boolean_t mptcp_acknow;
989 #endif /* MPTCP */
990 stats_functional_type ifnet_count_type = stats_functional_type_unclassified;
991 int sotc = so->so_traffic_class;
992 boolean_t do_not_compress = FALSE;
993 bool sack_rescue_rxt = false;
994 bool sack_rxmted = false;
995 bool link_heuristics_enabled = false;
996
997 struct ifnet *outifp = inp != NULL ? inp->inp_last_outifp : NULL;
998
999 /*
1000 * Determine length of data that should be transmitted,
1001 * and flags that will be used.
1002 * If there is some data or critical controls (SYN, RST)
1003 * to send, then transmit; otherwise, investigate further.
1004 */
1005 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
1006
1007 /* Since idle_time is signed integer, the following integer subtraction
1008 * will take care of wrap around of tcp_now
1009 */
1010 idle_time = tcp_now_local - tp->t_rcvtime;
1011 if (idle && idle_time >= TCP_IDLETIMEOUT(tp)) {
1012 if (CC_ALGO(tp)->after_idle != NULL &&
1013 ((tp->tcp_cc_index != TCP_CC_ALGO_CUBIC_INDEX &&
1014 tp->tcp_cc_index != TCP_CC_ALGO_PRAGUE_INDEX) ||
1015 idle_time >= TCP_CC_CWND_NONVALIDATED_PERIOD)) {
1016 CC_ALGO(tp)->after_idle(tp);
1017 tcp_ccdbg_trace(tp, NULL, TCP_CC_IDLE_TIMEOUT);
1018 }
1019
1020 /*
1021 * Do some other tasks that need to be done after
1022 * idle time
1023 */
1024 if (!SLIST_EMPTY(&tp->t_rxt_segments)) {
1025 tcp_rxtseg_clean(tp);
1026 }
1027
1028 tp->t_forced_acks = TCP_FORCED_ACKS_COUNT;
1029 }
1030 tp->t_flags &= ~TF_LASTIDLE;
1031 if (idle) {
1032 if (tp->t_flags & TF_MORETOCOME) {
1033 tp->t_flags |= TF_LASTIDLE;
1034 idle = 0;
1035 }
1036 }
1037 #if MPTCP
1038 if (tp->t_mpflags & TMPF_RESET) {
1039 tcp_check_timer_state(tp);
1040 /*
1041 * Once a RST has been sent for an MPTCP subflow,
1042 * the subflow socket stays around until deleted.
1043 * No packets such as FINs must be sent after RST.
1044 */
1045 return 0;
1046 }
1047 #endif /* MPTCP */
1048
1049 link_heuristics_enabled = if_link_heuristics_enabled(outifp);
1050
1051 again:
1052 tcp_now_local = os_access_once(tcp_now);
1053 pacing_tx_time = 0;
1054 tsvalptr = NULL;
1055 #if MPTCP
1056 mptcp_acknow = FALSE;
1057
1058 if (so->so_flags & SOF_MP_SUBFLOW && SEQ_LT(tp->snd_nxt, tp->snd_una)) {
1059 os_log_error(mptcp_log_handle, "%s - %lx: snd_nxt is %u and snd_una is %u, cnt %d\n",
1060 __func__, (unsigned long)VM_KERNEL_ADDRPERM(tp->t_mpsub->mpts_mpte),
1061 tp->snd_nxt, tp->snd_una, sendalot_cnt);
1062 }
1063 #endif
1064 do_not_compress = FALSE;
1065 sendalot_cnt++;
1066
1067 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_START, 0, 0, 0, 0, 0);
1068
1069 if (isipv6) {
1070 KERNEL_DEBUG(DBG_LAYER_BEG,
1071 ((inp->inp_fport << 16) | inp->inp_lport),
1072 (((inp->in6p_laddr.s6_addr16[0] & 0xffff) << 16) |
1073 (inp->in6p_faddr.s6_addr16[0] & 0xffff)),
1074 sendalot, 0, 0);
1075 } else {
1076 KERNEL_DEBUG(DBG_LAYER_BEG,
1077 ((inp->inp_fport << 16) | inp->inp_lport),
1078 (((inp->inp_laddr.s_addr & 0xffff) << 16) |
1079 (inp->inp_faddr.s_addr & 0xffff)),
1080 sendalot, 0, 0);
1081 }
1082 /*
1083 * If the route generation id changed, we need to check that our
1084 * local (source) IP address is still valid. If it isn't either
1085 * return error or silently do nothing (assuming the address will
1086 * come back before the TCP connection times out).
1087 */
1088 rt = inp->inp_route.ro_rt;
1089 if (rt != NULL && ROUTE_UNUSABLE(&tp->t_inpcb->inp_route)) {
1090 struct ifnet *ifp;
1091 struct in_ifaddr *ia = NULL;
1092 struct in6_ifaddr *ia6 = NULL;
1093 int found_srcaddr = 0;
1094
1095 /* disable multipages at the socket */
1096 somultipages(so, FALSE);
1097
1098 /* Disable TSO for the socket until we know more */
1099 tp->t_flags &= ~TF_TSO;
1100
1101 soif2kcl(so, FALSE);
1102
1103 if (isipv6) {
1104 ia6 = ifa_foraddr6(&inp->in6p_laddr);
1105 if (ia6 != NULL) {
1106 found_srcaddr = 1;
1107 }
1108 } else {
1109 ia = ifa_foraddr(inp->inp_laddr.s_addr);
1110 if (ia != NULL) {
1111 found_srcaddr = 1;
1112 }
1113 }
1114
1115 /* check that the source address is still valid */
1116 if (found_srcaddr == 0) {
1117 soevent(so,
1118 (SO_FILT_HINT_LOCKED | SO_FILT_HINT_NOSRCADDR));
1119
1120 if (tp->t_state >= TCPS_CLOSE_WAIT) {
1121 tcp_drop(tp, EADDRNOTAVAIL);
1122 return EADDRNOTAVAIL;
1123 }
1124
1125 /*
1126 * Set retransmit timer if it wasn't set,
1127 * reset Persist timer and shift register as the
1128 * advertised peer window may not be valid anymore
1129 */
1130 if (tp->t_timer[TCPT_REXMT] == 0) {
1131 tcp_set_rto(tp);
1132 if (tp->t_timer[TCPT_PERSIST] != 0) {
1133 tp->t_timer[TCPT_PERSIST] = 0;
1134 tp->t_persist_stop = 0;
1135 TCP_RESET_REXMT_STATE(tp);
1136 }
1137 }
1138
1139 if (tp->t_pktlist_head != NULL) {
1140 m_drop_list(tp->t_pktlist_head, NULL, DROPTAP_FLAG_DIR_OUT | DROPTAP_FLAG_L2_MISSING, DROP_REASON_TCP_SRC_ADDR_NOT_AVAIL, NULL, 0);
1141 }
1142 TCP_PKTLIST_CLEAR(tp);
1143
1144 /* drop connection if source address isn't available */
1145 if (so->so_flags & SOF_NOADDRAVAIL) {
1146 tcp_drop(tp, EADDRNOTAVAIL);
1147 return EADDRNOTAVAIL;
1148 } else {
1149 TCP_LOG_OUTPUT(tp, "no source address silently ignored");
1150 tcp_check_timer_state(tp);
1151 return 0; /* silently ignore, keep data in socket: address may be back */
1152 }
1153 }
1154 if (ia != NULL) {
1155 ifa_remref(&ia->ia_ifa);
1156 }
1157
1158 if (ia6 != NULL) {
1159 ifa_remref(&ia6->ia_ifa);
1160 }
1161
1162 /*
1163 * Address is still valid; check for multipages capability
1164 * again in case the outgoing interface has changed.
1165 */
1166 RT_LOCK(rt);
1167 if ((ifp = rt->rt_ifp) != NULL) {
1168 somultipages(so, (ifp->if_hwassist & IFNET_MULTIPAGES));
1169 tcp_set_tso(tp, ifp);
1170 soif2kcl(so, (ifp->if_eflags & IFEF_2KCL));
1171 /* Don't do ECN for Loopback & Cellular */
1172 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0 && !IFNET_IS_CELLULAR(ifp)) {
1173 tcp_set_ecn(tp);
1174 }
1175
1176 /*
1177 * If the route changes, we cannot use the link heuristics
1178 * based on the previous outgoing interface
1179 */
1180 if (rt->rt_ifp != tp->t_inpcb->inp_last_outifp) {
1181 link_heuristics_enabled = false;
1182 tp->t_comp_rxmt_gencnt = 0;
1183 }
1184 }
1185 if (rt->rt_flags & RTF_UP) {
1186 RT_GENID_SYNC(rt);
1187 }
1188 /*
1189 * See if we should do MTU discovery. Don't do it if:
1190 * 1) it is disabled via the sysctl
1191 * 2) the route isn't up
1192 * 3) the MTU is locked (if it is, then discovery
1193 * has been disabled)
1194 */
1195
1196 if (!path_mtu_discovery || ((rt != NULL) &&
1197 (!(rt->rt_flags & RTF_UP) ||
1198 (rt->rt_rmx.rmx_locks & RTV_MTU)))) {
1199 tp->t_flags &= ~TF_PMTUD;
1200 } else {
1201 tp->t_flags |= TF_PMTUD;
1202 }
1203
1204 RT_UNLOCK(rt);
1205 }
1206
1207 if (rt != NULL) {
1208 ifnet_count_type = IFNET_COUNT_TYPE(rt->rt_ifp);
1209 }
1210
1211 /*
1212 * If we've recently taken a timeout, snd_max will be greater than
1213 * snd_nxt. There may be SACK information that allows us to avoid
1214 * resending already delivered data. Adjust snd_nxt accordingly.
1215 * It is ok to use this function with RACK as well as it is estimating
1216 * max_len based on a SACK hole.
1217 */
1218 if (SACK_ENABLED(tp) && SEQ_LT(tp->snd_nxt, tp->snd_max)) {
1219 if (TCP_RACK_ENABLED(tp)) {
1220 /*
1221 * Calculated in the same manner as when rack_in_recovery
1222 * is set and new data is transmitted after retransmitted data
1223 */
1224 int32_t cwin = tp->snd_cwnd - tcp_flight_size(tp);
1225 if (cwin > 0) {
1226 max_len = tcp_rack_adjust(tp, (uint32_t)cwin);
1227 }
1228 } else {
1229 max_len = tcp_sack_adjust(tp);
1230 }
1231 }
1232 sendalot = 0;
1233 off = tp->snd_nxt - tp->snd_una;
1234 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
1235
1236 if (tp->t_flags & TF_SLOWLINK && slowlink_wsize > 0) {
1237 sendwin = min(sendwin, slowlink_wsize);
1238 }
1239
1240 flags = tcp_outflags[tp->t_state];
1241 /*
1242 * Send any SACK-generated retransmissions. If we're explicitly
1243 * trying to send out new data (when sendalot is 1), bypass this
1244 * function. If we retransmit in fast recovery mode, decrement
1245 * snd_cwnd, since we're replacing a (future) new transmission
1246 * with a retransmission now, and we previously incremented
1247 * snd_cwnd in tcp_input().
1248 */
1249 /*
1250 * Still in sack recovery, reset rxmit flag to zero.
1251 */
1252 rack_sack_rxmit = 0;
1253 sack_bytes_rxmt = 0;
1254 len = 0;
1255 p = NULL;
1256 seg = NULL;
1257
1258 if (SACK_ENABLED(tp) && IN_FASTRECOVERY(tp)) {
1259 int32_t cwin = min(tp->snd_wnd, tp->snd_cwnd) - tcp_flight_size(tp);
1260 if (cwin <= 0 && sack_rxmted == false) {
1261 /* Allow to clock out at least on per period */
1262 cwin = tp->t_maxseg;
1263 }
1264
1265 sack_rxmted = true;
1266 if (cwin < 0) {
1267 cwin = 0;
1268 }
1269
1270 if (TCP_RACK_ENABLED(tp)) {
1271 uint16_t rack_seg_len = 0;
1272 if ((seg = tcp_rack_output(tp, cwin, &rack_seg_len)) != NULL) {
1273 len = min(cwin, rack_seg_len);
1274
1275 if (len > 0) {
1276 off = seg->start_seq - tp->snd_una;
1277 rack_sack_rxmit = 1;
1278 sendalot = 1;
1279 tcpstat.tcps_rack_rexmits++;
1280 } else {
1281 seg = NULL;
1282 }
1283 }
1284 } else if ((p = tcp_sack_output(tp, &sack_bytes_rxmt)) != NULL) {
1285 /* Do not retransmit SACK segments beyond snd_recover */
1286 if (SEQ_GT(p->end, tp->snd_recover)) {
1287 /*
1288 * (At least) part of sack hole extends beyond
1289 * snd_recover. Check to see if we can rexmit data
1290 * for this hole.
1291 */
1292 if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
1293 /*
1294 * Can't rexmit any more data for this hole.
1295 * That data will be rexmitted in the next
1296 * sack recovery episode, when snd_recover
1297 * moves past p->rxmit.
1298 */
1299 p = NULL;
1300 goto after_sack_rexmit;
1301 } else {
1302 /* Can rexmit part of the current hole */
1303 len = ((int32_t)min(cwin,
1304 tp->snd_recover - p->rxmit));
1305 }
1306 } else {
1307 len = ((int32_t)min(cwin, p->end - p->rxmit));
1308 }
1309 if (len > 0) {
1310 off = p->rxmit - tp->snd_una;
1311 rack_sack_rxmit = 1;
1312 sendalot = 1;
1313 /*
1314 * Optimization to avoid double retransmission due to SACK recovery
1315 * and when tp->snd_nxt points to already retransmitted segments
1316 */
1317 if (SEQ_LT(tp->snd_nxt, tp->snd_max) && SEQ_LEQ(tp->snd_nxt, p->rxmit) &&
1318 (uint32_t)len <= max_len) {
1319 sendalot = 0;
1320 }
1321
1322 tcpstat.tcps_sack_rexmits++;
1323 tcpstat.tcps_sack_rexmit_bytes +=
1324 min(len, tp->t_maxseg);
1325 } else {
1326 len = 0;
1327 }
1328 }
1329 }
1330 after_sack_rexmit:
1331 /*
1332 * Get standard flags, and add SYN or FIN if requested by 'hidden'
1333 * state flags.
1334 */
1335 if (tp->t_flags & TF_NEEDFIN) {
1336 flags |= TH_FIN;
1337 }
1338
1339 /*
1340 * If in persist timeout with window of 0, send 1 byte.
1341 * Otherwise, if window is small but nonzero
1342 * and timer expired, we will send what we can
1343 * and go to transmit state.
1344 */
1345 if (tp->t_flagsext & TF_FORCE) {
1346 if (sendwin == 0) {
1347 /*
1348 * If we still have some data to send, then
1349 * clear the FIN bit. Usually this would
1350 * happen below when it realizes that we
1351 * aren't sending all the data. However,
1352 * if we have exactly 1 byte of unsent data,
1353 * then it won't clear the FIN bit below,
1354 * and if we are in persist state, we wind
1355 * up sending the packet without recording
1356 * that we sent the FIN bit.
1357 *
1358 * We can't just blindly clear the FIN bit,
1359 * because if we don't have any more data
1360 * to send then the probe will be the FIN
1361 * itself.
1362 */
1363 if (off < so->so_snd.sb_cc) {
1364 flags &= ~TH_FIN;
1365 }
1366 sendwin = 1;
1367 } else {
1368 tp->t_timer[TCPT_PERSIST] = 0;
1369 tp->t_persist_stop = 0;
1370 TCP_RESET_REXMT_STATE(tp);
1371 }
1372 }
1373
1374 /*
1375 * If snd_nxt == snd_max and we have transmitted a FIN, the
1376 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
1377 * a negative length. This can also occur when TCP opens up
1378 * its congestion window while receiving additional duplicate
1379 * acks after fast-retransmit because TCP will reset snd_nxt
1380 * to snd_max after the fast-retransmit.
1381 *
1382 * In the normal retransmit-FIN-only case, however, snd_nxt will
1383 * be set to snd_una, the offset will be 0, and the length may
1384 * wind up 0.
1385 *
1386 * If sack_rxmit or rack_rxmit is true we are retransmitting from
1387 * the scoreboard in which case len is already set.
1388 */
1389 bool rack_in_recovery = TCP_RACK_ENABLED(tp) && IN_FASTRECOVERY(tp);
1390 if (rack_sack_rxmit == 0) {
1391 if (sack_bytes_rxmt == 0 && !rack_in_recovery) {
1392 len = min(so->so_snd.sb_cc, sendwin) - off;
1393 } else {
1394 int32_t cwin = tp->snd_cwnd - tcp_flight_size(tp);
1395 if (cwin < 0) {
1396 cwin = 0;
1397 }
1398 /*
1399 * We are inside of a SACK recovery episode and are
1400 * sending new data, having retransmitted all the
1401 * data possible in the scoreboard.
1402 */
1403 len = min(so->so_snd.sb_cc, tp->snd_wnd) - off;
1404 /*
1405 * Don't remove this (len > 0) check !
1406 * We explicitly check for len > 0 here (although it
1407 * isn't really necessary), to work around a gcc
1408 * optimization issue - to force gcc to compute
1409 * len above. Without this check, the computation
1410 * of len is bungled by the optimizer.
1411 */
1412 if (len > 0) {
1413 len = imin(len, cwin);
1414 } else {
1415 len = 0;
1416 }
1417 /*
1418 * At this point SACK recovery can not send any
1419 * data from scoreboard or any new data. Check
1420 * if we can do a rescue retransmit towards the
1421 * tail end of recovery window.
1422 * We don't do rescue retransmit for RACK.
1423 */
1424 if (len == 0 && cwin > 0 &&
1425 SEQ_LT(tp->snd_fack, tp->snd_recover) &&
1426 !(tp->t_flagsext & TF_RESCUE_RXT) && !TCP_RACK_ENABLED(tp)) {
1427 len = min((tp->snd_recover - tp->snd_fack),
1428 tp->t_maxseg);
1429 len = imin(len, cwin);
1430 old_snd_nxt = tp->snd_nxt;
1431 sack_rescue_rxt = true;
1432 tp->snd_nxt = tp->snd_recover - len;
1433 /*
1434 * If FIN has been sent, snd_max
1435 * must have been advanced to cover it.
1436 */
1437 if ((tp->t_flags & TF_SENTFIN) &&
1438 tp->snd_max == tp->snd_recover) {
1439 tp->snd_nxt--;
1440 }
1441
1442 off = tp->snd_nxt - tp->snd_una;
1443 sendalot = 0;
1444 tp->t_flagsext |= TF_RESCUE_RXT;
1445 }
1446 }
1447 }
1448
1449 if (max_len != 0 && len > 0) {
1450 len = min(len, max_len);
1451 }
1452
1453 /*
1454 * Lop off SYN bit if it has already been sent. However, if this
1455 * is SYN-SENT state and if segment contains data and if we don't
1456 * know that foreign host supports TAO, suppress sending segment.
1457 */
1458 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
1459 if (tp->t_state == TCPS_SYN_RECEIVED && TFO_ENABLED(tp) && tp->snd_nxt == tp->snd_una + 1) {
1460 /* We are sending the SYN again! */
1461 off--;
1462 len++;
1463 } else {
1464 if (tp->t_state != TCPS_SYN_RECEIVED || TFO_ENABLED(tp)) {
1465 flags &= ~TH_SYN;
1466 }
1467
1468 off--;
1469 len++;
1470 if (len > 0 && tp->t_state == TCPS_SYN_SENT) {
1471 while (inp->inp_sndinprog_cnt == 0 &&
1472 tp->t_pktlist_head != NULL) {
1473 packetlist = tp->t_pktlist_head;
1474 packchain_listadd = tp->t_lastchain;
1475 packchain_sent++;
1476 TCP_PKTLIST_CLEAR(tp);
1477
1478 error = tcp_ip_output(so, tp, packetlist,
1479 packchain_listadd, tp_inp_options,
1480 (so_options & SO_DONTROUTE),
1481 (rack_sack_rxmit || (sack_bytes_rxmt != 0)),
1482 isipv6);
1483 }
1484
1485 /*
1486 * tcp was closed while we were in ip,
1487 * resume close
1488 */
1489 if (inp->inp_sndinprog_cnt == 0 &&
1490 (tp->t_flags & TF_CLOSING)) {
1491 tp->t_flags &= ~TF_CLOSING;
1492 (void) tcp_close(tp);
1493 } else {
1494 tcp_check_timer_state(tp);
1495 }
1496 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END,
1497 0, 0, 0, 0, 0);
1498 return 0;
1499 }
1500 }
1501 }
1502
1503 /*
1504 * Be careful not to send data and/or FIN on SYN segments.
1505 * This measure is needed to prevent interoperability problems
1506 * with not fully conformant TCP implementations.
1507 *
1508 * In case of TFO, we handle the setting of the len in
1509 * tcp_tfo_check. In case TFO is not enabled, never ever send
1510 * SYN+data.
1511 */
1512 if ((flags & TH_SYN) && !TFO_ENABLED(tp)) {
1513 len = 0;
1514 flags &= ~TH_FIN;
1515 }
1516
1517 /*
1518 * Don't send a RST with data.
1519 */
1520 if (flags & TH_RST) {
1521 len = 0;
1522 }
1523
1524 if ((flags & TH_SYN) && tp->t_state <= TCPS_SYN_SENT && TFO_ENABLED(tp)) {
1525 len = tcp_tfo_check(tp, len);
1526 }
1527
1528 if ((tp->tcp_cc_index == TCP_CC_ALGO_PRAGUE_INDEX || inp->inp_max_pacing_rate != UINT64_MAX) &&
1529 tp->t_pacer.tso_burst_size != 0 && len > 0 &&
1530 (uint32_t)len > tp->t_pacer.tso_burst_size) {
1531 len = tp->t_pacer.tso_burst_size;
1532 sendalot = 1;
1533 }
1534
1535 /*
1536 * The check here used to be (len < 0). Some times len is zero
1537 * when the congestion window is closed and we need to check
1538 * if persist timer has to be set in that case. But don't set
1539 * persist until connection is established.
1540 */
1541 if (len <= 0 && !(flags & TH_SYN)) {
1542 /*
1543 * If FIN has been sent but not acked,
1544 * but we haven't been called to retransmit,
1545 * len will be < 0. Otherwise, window shrank
1546 * after we sent into it. If window shrank to 0,
1547 * cancel pending retransmit, pull snd_nxt back
1548 * to (closed) window, and set the persist timer
1549 * if it isn't already going. If the window didn't
1550 * close completely, just wait for an ACK.
1551 */
1552 len = 0;
1553 if (sendwin == 0) {
1554 tp->t_timer[TCPT_REXMT] = 0;
1555 tp->t_timer[TCPT_PTO] = 0;
1556 TCP_RESET_REXMT_STATE(tp);
1557 tp->snd_nxt = tp->snd_una;
1558 off = 0;
1559 if (tp->t_timer[TCPT_PERSIST] == 0) {
1560 tcp_setpersist(tp);
1561 }
1562 }
1563 }
1564
1565 /*
1566 * Automatic sizing of send socket buffer. Increase the send
1567 * socket buffer size if all of the following criteria are met
1568 * 1. the receiver has enough buffer space for this data
1569 * 2. send buffer is filled to 7/8th with data (so we actually
1570 * have data to make use of it);
1571 * 3. our send window (slow start and congestion controlled) is
1572 * larger than sent but unacknowledged data in send buffer.
1573 */
1574 if (!INP_WAIT_FOR_IF_FEEDBACK(inp) && !IN_FASTRECOVERY(tp) &&
1575 (so->so_snd.sb_flags & (SB_AUTOSIZE | SB_TRIM)) == SB_AUTOSIZE) {
1576 if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
1577 so->so_snd.sb_cc >= (so->so_snd.sb_hiwat / 8 * 7) &&
1578 sendwin >= (so->so_snd.sb_cc - (tp->snd_nxt - tp->snd_una))) {
1579 if (sbreserve(&so->so_snd,
1580 min(so->so_snd.sb_hiwat + tcp_autosndbuf_inc,
1581 tcp_autosndbuf_max)) == 1) {
1582 so->so_snd.sb_idealsize = so->so_snd.sb_hiwat;
1583 }
1584 }
1585 }
1586
1587 /*
1588 * Truncate to the maximum segment length or enable TCP Segmentation
1589 * Offloading (if supported by hardware) and ensure that FIN is removed
1590 * if the length no longer contains the last data byte.
1591 *
1592 * TSO may only be used if we are in a pure bulk sending state.
1593 * The presence of TCP-MD5, SACK retransmits, SACK advertizements,
1594 * filters and IP options, as well as disabling hardware checksum
1595 * offload prevent using TSO. With TSO the TCP header is the same
1596 * (except for the sequence number) for all generated packets. This
1597 * makes it impossible to transmit any options which vary per generated
1598 * segment or packet.
1599 *
1600 * The length of TSO bursts is limited to TCP_MAXWIN. That limit and
1601 * removal of FIN (if not already catched here) are handled later after
1602 * the exact length of the TCP options are known.
1603 */
1604 #if IPSEC
1605 /*
1606 * Pre-calculate here as we save another lookup into the darknesses
1607 * of IPsec that way and can actually decide if TSO is ok.
1608 */
1609 if (ipsec_bypass == 0) {
1610 ipsec_optlen = ipsec_hdrsiz_tcp(tp);
1611 }
1612 #endif
1613 if (len > tp->t_maxseg) {
1614 if ((tp->t_flags & TF_TSO) && tcp_do_tso && hwcksum_tx &&
1615 kipf_count == 0 &&
1616 tp->rcv_numsacks == 0 && rack_sack_rxmit == 0 &&
1617 sack_bytes_rxmt == 0 &&
1618 inp->inp_options == NULL &&
1619 inp->in6p_options == NULL
1620 #if IPSEC
1621 && ipsec_optlen == 0
1622 #endif
1623 ) {
1624 tso = 1;
1625 sendalot = 0;
1626 } else {
1627 len = tp->t_maxseg;
1628 sendalot = 1;
1629 tso = 0;
1630 }
1631 } else {
1632 tso = 0;
1633 }
1634
1635 /* Send one segment or less as a tail loss probe */
1636 if (tp->t_flagsext & TF_SENT_TLPROBE) {
1637 len = min(len, tp->t_maxseg);
1638 sendalot = 0;
1639 tso = 0;
1640 }
1641
1642 #if MPTCP
1643 if (so->so_flags & SOF_MP_SUBFLOW && off < 0) {
1644 os_log_error(mptcp_log_handle, "%s - %lx: offset is negative! len %d off %d\n",
1645 __func__, (unsigned long)VM_KERNEL_ADDRPERM(tp->t_mpsub->mpts_mpte),
1646 len, off);
1647 }
1648
1649 if ((so->so_flags & SOF_MP_SUBFLOW) &&
1650 !(tp->t_mpflags & TMPF_TCP_FALLBACK)) {
1651 int newlen = len;
1652 struct mptcb *mp_tp = tptomptp(tp);
1653 if (tp->t_state >= TCPS_ESTABLISHED &&
1654 (tp->t_mpflags & TMPF_SND_MPPRIO ||
1655 tp->t_mpflags & TMPF_SND_REM_ADDR ||
1656 tp->t_mpflags & TMPF_SND_MPFAIL ||
1657 (tp->t_mpflags & TMPF_SND_KEYS &&
1658 mp_tp->mpt_version == MPTCP_VERSION_0) ||
1659 tp->t_mpflags & TMPF_SND_JACK ||
1660 tp->t_mpflags & TMPF_MPTCP_ECHO_ADDR)) {
1661 if (len > 0) {
1662 len = 0;
1663 tso = 0;
1664 }
1665 /*
1666 * On a new subflow, don't try to send again, because
1667 * we are still waiting for the fourth ack.
1668 */
1669 if (!(tp->t_mpflags & TMPF_PREESTABLISHED)) {
1670 sendalot = 1;
1671 }
1672 mptcp_acknow = TRUE;
1673 } else {
1674 mptcp_acknow = FALSE;
1675 }
1676 /*
1677 * The contiguous bytes in the subflow socket buffer can be
1678 * discontiguous at the MPTCP level. Since only one DSS
1679 * option can be sent in one packet, reduce length to match
1680 * the contiguous MPTCP level. Set sendalot to send remainder.
1681 */
1682 if (len > 0 && off >= 0) {
1683 newlen = mptcp_adj_sendlen(so, off);
1684 }
1685
1686 if (newlen < len) {
1687 len = newlen;
1688 if (len <= tp->t_maxseg) {
1689 tso = 0;
1690 }
1691 }
1692 }
1693 #endif /* MPTCP */
1694
1695 if (rack_sack_rxmit) {
1696 if (TCP_RACK_ENABLED(tp)) {
1697 if (SEQ_LT(seg->start_seq + len, tp->snd_una + so->so_snd.sb_cc)) {
1698 flags &= ~TH_FIN;
1699 }
1700 } else {
1701 if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc)) {
1702 flags &= ~TH_FIN;
1703 }
1704 }
1705 } else {
1706 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) {
1707 flags &= ~TH_FIN;
1708 }
1709 }
1710 /*
1711 * Compare available window to amount of window
1712 * known to peer (as advertised window less
1713 * next expected input). If the difference is at least two
1714 * max size segments, or at least 25% of the maximum possible
1715 * window, then want to send a window update to peer.
1716 */
1717 recwin = tcp_sbspace(tp);
1718
1719 if (!(so->so_flags & SOF_MP_SUBFLOW)) {
1720 if (recwin < (int32_t)(so->so_rcv.sb_hiwat / 4) &&
1721 recwin < (int)tp->t_maxseg) {
1722 recwin = 0;
1723 }
1724 } else {
1725 struct mptcb *mp_tp = tptomptp(tp);
1726 struct socket *mp_so = mptetoso(mp_tp->mpt_mpte);
1727
1728 if (recwin < (int32_t)(mp_so->so_rcv.sb_hiwat / 4) &&
1729 recwin < (int)tp->t_maxseg) {
1730 recwin = 0;
1731 }
1732 }
1733
1734 #if TRAFFIC_MGT
1735 if (tcp_recv_bg == 1 || IS_TCP_RECV_BG(so)) {
1736 /*
1737 * Timestamp MUST be supported to use rledbat unless we haven't
1738 * yet negotiated it.
1739 */
1740 if (TCP_RLEDBAT_ENABLED(tp) || (tcp_rledbat && tp->t_state <
1741 TCPS_ESTABLISHED)) {
1742 if (recwin > 0 && tcp_cc_rledbat.get_rlwin != NULL) {
1743 /* Min of flow control window and rledbat window */
1744 recwin = imin(recwin, tcp_cc_rledbat.get_rlwin(tp));
1745 }
1746 } else if (recwin > 0 && tcp_recv_throttle(tp)) {
1747 uint32_t min_iaj_win = tcp_min_iaj_win * tp->t_maxseg;
1748 uint32_t bg_rwintop = tp->rcv_adv;
1749 if (SEQ_LT(bg_rwintop, tp->rcv_nxt + min_iaj_win)) {
1750 bg_rwintop = tp->rcv_nxt + min_iaj_win;
1751 }
1752 recwin = imin((int32_t)(bg_rwintop - tp->rcv_nxt),
1753 recwin);
1754 if (recwin < 0) {
1755 recwin = 0;
1756 }
1757 }
1758 }
1759 #endif /* TRAFFIC_MGT */
1760
1761 if (recwin > (int32_t)(TCP_MAXWIN << tp->rcv_scale)) {
1762 recwin = (int32_t)(TCP_MAXWIN << tp->rcv_scale);
1763 }
1764
1765 if (!(so->so_flags & SOF_MP_SUBFLOW)) {
1766 if (recwin < (int32_t)(tp->rcv_adv - tp->rcv_nxt)) {
1767 recwin = (int32_t)(tp->rcv_adv - tp->rcv_nxt);
1768 }
1769 } else {
1770 struct mptcb *mp_tp = tptomptp(tp);
1771 int64_t recwin_announced = (int64_t)(mp_tp->mpt_rcvadv - mp_tp->mpt_rcvnxt);
1772
1773 /* Don't remove what we announced at the MPTCP-layer */
1774 VERIFY(recwin_announced < INT32_MAX && recwin_announced > INT32_MIN);
1775 if (recwin < (int32_t)recwin_announced) {
1776 recwin = (int32_t)recwin_announced;
1777 }
1778
1779 if (mp_tp->mpt_flags & MPTCPF_FALLBACK_TO_TCP) {
1780 if (recwin < (int32_t)(tp->rcv_adv - tp->rcv_nxt)) {
1781 recwin = (int32_t)(tp->rcv_adv - tp->rcv_nxt);
1782 }
1783 }
1784 }
1785
1786 /* One day we should have a single ROUNDUP macro across xnu... */
1787 #ifndef ROUNDUP
1788 #define ROUNDUP(a, b) (((a) + ((b) - 1)) & (~((b) - 1)))
1789 #endif
1790 recwin = ROUNDUP(recwin, (1 << tp->rcv_scale));
1791 #undef ROUNDUP
1792
1793 /*
1794 * Sender silly window avoidance. We transmit under the following
1795 * conditions when len is non-zero:
1796 *
1797 * - we've timed out (e.g. persist timer)
1798 * - we need to retransmit
1799 * - We have a full segment (or more with TSO)
1800 * - This is the last buffer in a write()/send() and we are
1801 * either idle or running NODELAY
1802 * - we have more then 1/2 the maximum send window's worth of
1803 * data (receiver may be limited the window size)
1804 */
1805 if (len) {
1806 if (tp->t_flagsext & TF_FORCE) {
1807 goto send;
1808 }
1809 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
1810 if (TCP_RACK_ENABLED(tp) && rack_sack_rxmit == 0) {
1811 len = min(len, tp->snd_max - tp->snd_nxt);
1812 }
1813 goto send;
1814 }
1815 if (rack_sack_rxmit) {
1816 goto send;
1817 }
1818
1819 /*
1820 * If this here is the first segment after SYN/ACK and TFO
1821 * is being used, then we always send it, regardless of Nagle,...
1822 */
1823 if (tp->t_state == TCPS_SYN_RECEIVED &&
1824 TFO_ENABLED(tp) &&
1825 (tp->t_tfo_flags & TFO_F_COOKIE_VALID) &&
1826 tp->snd_nxt == tp->iss + 1) {
1827 goto send;
1828 }
1829
1830 /*
1831 * Send new data on the connection only if it is
1832 * not flow controlled
1833 */
1834 if (!INP_WAIT_FOR_IF_FEEDBACK(inp) ||
1835 tp->t_state != TCPS_ESTABLISHED) {
1836 if (off + len == tp->snd_wnd) {
1837 /* We are limited by the receiver's window... */
1838 if (tp->t_rcvwnd_limited_start_time == 0) {
1839 tp->t_rcvwnd_limited_start_time = net_uptime_us();
1840 }
1841 } else {
1842 /* We are no more limited by the receiver's window... */
1843 if (tp->t_rcvwnd_limited_start_time != 0) {
1844 uint64_t now = net_uptime_us();
1845
1846 ASSERT(now >= tp->t_rcvwnd_limited_start_time);
1847
1848 tp->t_rcvwnd_limited_total_time += (now - tp->t_rcvwnd_limited_start_time);
1849
1850 tp->t_rcvwnd_limited_start_time = 0;
1851 }
1852 }
1853
1854 if (len >= tp->t_maxseg) {
1855 goto send;
1856 }
1857
1858 if (!(tp->t_flags & TF_MORETOCOME) &&
1859 (idle || tp->t_flags & TF_NODELAY ||
1860 (tp->t_flags & TF_MAXSEGSNT) ||
1861 ALLOW_LIMITED_TRANSMIT(tp)) &&
1862 (tp->t_flags & TF_NOPUSH) == 0 &&
1863 (len + off >= so->so_snd.sb_cc ||
1864 /*
1865 * MPTCP needs to respect the DSS-mappings. So, it
1866 * may be sending data that *could* have been
1867 * coalesced, but cannot because of
1868 * mptcp_adj_sendlen().
1869 */
1870 so->so_flags & SOF_MP_SUBFLOW)) {
1871 goto send;
1872 }
1873 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
1874 goto send;
1875 }
1876 } else {
1877 tcpstat.tcps_fcholdpacket++;
1878 }
1879 }
1880
1881 if (recwin > 0) {
1882 /*
1883 * "adv" is the amount we can increase the window,
1884 * taking into account that we are limited by
1885 * TCP_MAXWIN << tp->rcv_scale.
1886 */
1887 int32_t adv, oldwin = 0;
1888 adv = imin(recwin, (int)TCP_MAXWIN << tp->rcv_scale) -
1889 (tp->rcv_adv - tp->rcv_nxt);
1890
1891 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
1892 oldwin = tp->rcv_adv - tp->rcv_nxt;
1893 }
1894
1895 if (adv >= (int32_t) (2 * tp->t_maxseg)) {
1896 /*
1897 * ACK every second full-sized segment, if the
1898 * ACK is advancing or the window becomes bigger
1899 */
1900 if (so->so_rcv.sb_cc < so->so_rcv.sb_lowat &&
1901 (tp->last_ack_sent != tp->rcv_nxt ||
1902 ((oldwin + adv) >> tp->rcv_scale) >
1903 (oldwin >> tp->rcv_scale))) {
1904 goto send;
1905 }
1906 } else if (tp->t_flags & TF_DELACK) {
1907 /*
1908 * If we delayed the ACK and the window
1909 * is not advancing by a lot (< 2MSS), ACK
1910 * immediately if the last incoming packet had
1911 * the push flag set and we emptied the buffer.
1912 *
1913 * This takes care of a sender doing small
1914 * repeated writes with Nagle enabled.
1915 */
1916 if (so->so_rcv.sb_cc == 0 &&
1917 tp->last_ack_sent != tp->rcv_nxt &&
1918 (tp->t_flagsext & TF_LAST_IS_PSH)) {
1919 goto send;
1920 }
1921 }
1922
1923 if (4 * adv >= (int32_t) so->so_rcv.sb_hiwat) {
1924 goto send;
1925 }
1926 }
1927
1928 /*
1929 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW
1930 * is also a catch-all for the retransmit timer timeout case.
1931 */
1932 if (tp->t_flags & TF_ACKNOW) {
1933 if (tp->t_forced_acks > 0) {
1934 tp->t_forced_acks--;
1935 }
1936 goto send;
1937 }
1938 if ((flags & TH_RST) || (flags & TH_SYN)) {
1939 goto send;
1940 }
1941 if (SEQ_GT(tp->snd_up, tp->snd_una)) {
1942 goto send;
1943 }
1944 #if MPTCP
1945 if (mptcp_acknow) {
1946 goto send;
1947 }
1948 #endif /* MPTCP */
1949 /*
1950 * If our state indicates that FIN should be sent
1951 * and we have not yet done so, then we need to send.
1952 */
1953 if ((flags & TH_FIN) &&
1954 (!(tp->t_flags & TF_SENTFIN) || tp->snd_nxt == tp->snd_una)) {
1955 goto send;
1956 }
1957 /*
1958 * In SACK, it is possible for tcp_output to fail to send a segment
1959 * after the retransmission timer has been turned off. Make sure
1960 * that the retransmission timer is set.
1961 */
1962 if (SACK_ENABLED(tp) && (tp->t_state >= TCPS_ESTABLISHED) &&
1963 SEQ_GT(tp->snd_max, tp->snd_una) &&
1964 tp->t_timer[TCPT_REXMT] == 0 &&
1965 tp->t_timer[TCPT_PERSIST] == 0) {
1966 tcp_set_rto(tp);
1967 goto just_return;
1968 }
1969 /*
1970 * TCP window updates are not reliable, rather a polling protocol
1971 * using ``persist'' packets is used to insure receipt of window
1972 * updates. The three ``states'' for the output side are:
1973 * idle not doing retransmits or persists
1974 * persisting to move a small or zero window
1975 * (re)transmitting and thereby not persisting
1976 *
1977 * tp->t_timer[TCPT_PERSIST]
1978 * is set when we are in persist state.
1979 * tp->t_force
1980 * is set when we are called to send a persist packet.
1981 * tp->t_timer[TCPT_REXMT]
1982 * is set when we are retransmitting
1983 * The output side is idle when both timers are zero.
1984 *
1985 * If send window is too small, there is data to transmit, and no
1986 * retransmit or persist is pending, then go to persist state.
1987 * If nothing happens soon, send when timer expires:
1988 * if window is nonzero, transmit what we can,
1989 * otherwise force out a byte.
1990 */
1991 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
1992 tp->t_timer[TCPT_PERSIST] == 0) {
1993 TCP_RESET_REXMT_STATE(tp);
1994 tcp_setpersist(tp);
1995 }
1996 just_return:
1997 /*
1998 * If there is no reason to send a segment, just return.
1999 * but if there is some packets left in the packet list, send them now.
2000 */
2001 while (inp->inp_sndinprog_cnt == 0 &&
2002 tp->t_pktlist_head != NULL) {
2003 packetlist = tp->t_pktlist_head;
2004 packchain_listadd = tp->t_lastchain;
2005 packchain_sent++;
2006 TCP_PKTLIST_CLEAR(tp);
2007
2008 error = tcp_ip_output(so, tp, packetlist,
2009 packchain_listadd,
2010 tp_inp_options, (so_options & SO_DONTROUTE),
2011 (rack_sack_rxmit || (sack_bytes_rxmt != 0)), isipv6);
2012 }
2013 /* tcp was closed while we were in ip; resume close */
2014 if (inp->inp_sndinprog_cnt == 0 &&
2015 (tp->t_flags & TF_CLOSING)) {
2016 tp->t_flags &= ~TF_CLOSING;
2017 (void) tcp_close(tp);
2018 } else {
2019 tcp_check_timer_state(tp);
2020 }
2021 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
2022 return 0;
2023
2024 send:
2025 /*
2026 * Set TF_MAXSEGSNT flag if the segment size is greater than
2027 * the max segment size.
2028 */
2029 if (len > 0) {
2030 do_not_compress = TRUE;
2031
2032 if (len >= tp->t_maxseg) {
2033 tp->t_flags |= TF_MAXSEGSNT;
2034 } else {
2035 tp->t_flags &= ~TF_MAXSEGSNT;
2036 }
2037 }
2038 /*
2039 * If we are connected and no segment has been ACKed or SACKed yet and we
2040 * hit a retransmission timeout, then we should disable AccECN option
2041 * for the rest of the connection.
2042 */
2043 if (tp->accurate_ecn_on && tp->t_state == TCPS_ESTABLISHED &&
2044 tp->snd_una == tp->iss + 1 && (tp->snd_fack == tp->iss)
2045 && tp->t_rxtshift > 0) {
2046 if ((tp->ecn_flags & TE_RETRY_WITHOUT_ACO) == 0) {
2047 tp->ecn_flags |= TE_RETRY_WITHOUT_ACO;
2048 }
2049 }
2050
2051 /*
2052 * Before ESTABLISHED, force sending of initial options
2053 * unless TCP set not to do any options.
2054 * NOTE: we assume that the IP/TCP header plus TCP options
2055 * always fit in a single mbuf, leaving room for a maximum
2056 * link header, i.e.
2057 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
2058 */
2059 optlen = 0;
2060 if (isipv6) {
2061 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
2062 } else {
2063 hdrlen = sizeof(struct tcpiphdr);
2064 }
2065 if (flags & TH_SYN) {
2066 tp->snd_nxt = tp->iss;
2067 tp->snd_fack = tp->iss;
2068 if ((tp->t_flags & TF_NOOPT) == 0) {
2069 u_short mss;
2070
2071 opt[0] = TCPOPT_MAXSEG;
2072 opt[1] = TCPOLEN_MAXSEG;
2073 mss = htons((u_short) tcp_mssopt(tp));
2074 (void)memcpy(opt + 2, &mss, sizeof(mss));
2075 optlen = TCPOLEN_MAXSEG;
2076
2077 if ((tp->t_flags & TF_REQ_SCALE) &&
2078 ((flags & TH_ACK) == 0 ||
2079 (tp->t_flags & TF_RCVD_SCALE))) {
2080 *((u_int32_t *)(void *)(opt + optlen)) = htonl(
2081 TCPOPT_NOP << 24 |
2082 TCPOPT_WINDOW << 16 |
2083 TCPOLEN_WINDOW << 8 |
2084 tp->request_r_scale);
2085 optlen += 4;
2086 }
2087 /* Check if L4S is enabled after outifp has been set and update the CC */
2088 if (tp->l4s_enabled && tp->tcp_cc_index == TCP_CC_ALGO_CUBIC_INDEX) {
2089 tcp_set_foreground_cc(so);
2090 }
2091 #if MPTCP
2092 if (mptcp_enable && (so->so_flags & SOF_MP_SUBFLOW)) {
2093 optlen = mptcp_setup_syn_opts(so, opt, opt + sizeof(opt), optlen);
2094 }
2095 #endif /* MPTCP */
2096 }
2097 }
2098
2099 /*
2100 * Send a timestamp and echo-reply if this is a SYN and our side
2101 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
2102 * and our peer have sent timestamps in our SYN's.
2103 */
2104 if ((tp->t_flags & (TF_REQ_TSTMP | TF_NOOPT)) == TF_REQ_TSTMP &&
2105 (flags & TH_RST) == 0 &&
2106 ((flags & TH_ACK) == 0 ||
2107 (tp->t_flags & TF_RCVD_TSTMP))) {
2108 uint32_t *lp = (u_int32_t *)(void *)(opt + optlen);
2109
2110 /* Form timestamp option as shown in appendix A of RFC 1323. */
2111 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
2112
2113 tsvalptr = lp;
2114 lp++; /* tsval will be set later (see access to tsvalptr) */
2115 *lp = htonl(tp->ts_recent);
2116 optlen += TCPOLEN_TSTAMP_APPA;
2117 }
2118
2119 if (SACK_ENABLED(tp) && ((tp->t_flags & TF_NOOPT) == 0)) {
2120 /*
2121 * Tack on the SACK permitted option *last*.
2122 * And do padding of options after tacking this on.
2123 * This is because of MSS, TS, WinScale and Signatures are
2124 * all present, we have just 2 bytes left for the SACK
2125 * permitted option, which is just enough.
2126 */
2127 /*
2128 * If this is the first SYN of connection (not a SYN
2129 * ACK), include SACK permitted option. If this is a
2130 * SYN ACK, include SACK permitted option if peer has
2131 * already done so. This is only for active connect,
2132 * since the syncookie takes care of the passive connect.
2133 */
2134 if ((flags & TH_SYN) &&
2135 (!(flags & TH_ACK) || (tp->t_flags & TF_SACK_PERMIT))) {
2136 u_char *bp;
2137 bp = (u_char *)opt + optlen;
2138
2139 *bp++ = TCPOPT_SACK_PERMITTED;
2140 *bp++ = TCPOLEN_SACK_PERMITTED;
2141 optlen += TCPOLEN_SACK_PERMITTED;
2142 }
2143 }
2144 #if MPTCP
2145 if (so->so_flags & SOF_MP_SUBFLOW) {
2146 /*
2147 * Its important to piggyback acks with data as ack only packets
2148 * may get lost and data packets that don't send Data ACKs
2149 * still advance the subflow level ACK and therefore make it
2150 * hard for the remote end to recover in low cwnd situations.
2151 */
2152 if (len != 0) {
2153 tp->t_mpflags |= (TMPF_SEND_DSN |
2154 TMPF_MPTCP_ACKNOW);
2155 } else {
2156 tp->t_mpflags |= TMPF_MPTCP_ACKNOW;
2157 }
2158 optlen = mptcp_setup_opts(tp, off, opt, opt + TCP_MAXOLEN, optlen, flags,
2159 len, &mptcp_acknow, &do_not_compress);
2160 tp->t_mpflags &= ~TMPF_SEND_DSN;
2161 }
2162 #endif /* MPTCP */
2163
2164 if (TFO_ENABLED(tp) && !(tp->t_flags & TF_NOOPT) &&
2165 (flags & (TH_SYN | TH_ACK)) == TH_SYN) {
2166 optlen += tcp_tfo_write_cookie(tp, optlen, len, opt);
2167 }
2168
2169 if (TFO_ENABLED(tp) &&
2170 (flags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK) &&
2171 (tp->t_tfo_flags & TFO_F_OFFER_COOKIE)) {
2172 optlen += tcp_tfo_write_cookie_rep(tp, optlen, opt);
2173 }
2174
2175 if (SACK_ENABLED(tp) && ((tp->t_flags & TF_NOOPT) == 0)) {
2176 /*
2177 * Send SACKs if necessary. This should be the last
2178 * option processed. Only as many SACKs are sent as
2179 * are permitted by the maximum options size.
2180 *
2181 * In general, SACK blocks consume 8*n+2 bytes.
2182 * So a full size SACK blocks option is 34 bytes
2183 * (to generate 4 SACK blocks). At a minimum,
2184 * we need 10 bytes (to generate 1 SACK block).
2185 * If TCP Timestamps (12 bytes) and TCP Signatures
2186 * (18 bytes) are both present, we'll just have
2187 * 10 bytes for SACK options 40 - (12 + 18).
2188 */
2189 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
2190 (tp->t_flags & TF_SACK_PERMIT) &&
2191 (tp->rcv_numsacks > 0 || TCP_SEND_DSACK_OPT(tp)) &&
2192 MAX_TCPOPTLEN - optlen >= TCPOLEN_SACK + 2) {
2193 unsigned int sackoptlen = 0;
2194 int nsack, padlen;
2195 u_char *bp = (u_char *)opt + optlen;
2196 u_int32_t *lp;
2197
2198 nsack = (MAX_TCPOPTLEN - optlen - 2) / TCPOLEN_SACK;
2199 /*
2200 * Send lesser SACK blocks when we want
2201 * to send the smallest recommended AccECN Option
2202 * if the space wouldn't permit sending all blocks.
2203 */
2204 if (nsack > 2 && tp->accurate_ecn_on &&
2205 (tp->ecn_flags & TE_RETRY_WITHOUT_ACO) == 0 &&
2206 tp->ecn_flags & (TE_ACO_ECT1 | TE_ACO_ECT0)) {
2207 nsack--;
2208 }
2209 nsack = min(nsack, (tp->rcv_numsacks +
2210 (TCP_SEND_DSACK_OPT(tp) ? 1 : 0)));
2211 sackoptlen = (2 + nsack * TCPOLEN_SACK);
2212 VERIFY(sackoptlen < UINT8_MAX);
2213
2214 /*
2215 * First we need to pad options so that the
2216 * SACK blocks can start at a 4-byte boundary
2217 * (sack option and length are at a 2 byte offset).
2218 */
2219 padlen = (MAX_TCPOPTLEN - optlen - sackoptlen) % 4;
2220 optlen += padlen;
2221 while (padlen-- > 0) {
2222 *bp++ = TCPOPT_NOP;
2223 }
2224
2225 tcpstat.tcps_sack_send_blocks++;
2226 *bp++ = TCPOPT_SACK;
2227 *bp++ = (uint8_t)sackoptlen;
2228 lp = (u_int32_t *)(void *)bp;
2229
2230 /*
2231 * First block of SACK option should represent
2232 * DSACK. Prefer to send SACK information if there
2233 * is space for only one SACK block. This will
2234 * allow for faster recovery.
2235 */
2236 if (TCP_SEND_DSACK_OPT(tp) && nsack > 0 &&
2237 (tp->rcv_numsacks == 0 || nsack > 1)) {
2238 *lp++ = htonl(tp->t_dsack_lseq);
2239 *lp++ = htonl(tp->t_dsack_rseq);
2240 tcpstat.tcps_dsack_sent++;
2241 tp->t_dsack_sent++;
2242 nsack--;
2243 }
2244 VERIFY(nsack == 0 || tp->rcv_numsacks >= nsack);
2245 for (i = 0; i < nsack; i++) {
2246 struct sackblk sack = tp->sackblks[i];
2247 *lp++ = htonl(sack.start);
2248 *lp++ = htonl(sack.end);
2249 }
2250 optlen += sackoptlen;
2251
2252 /* Make sure we didn't write too much */
2253 VERIFY((u_char *)lp - opt <= MAX_TCPOPTLEN);
2254 }
2255 }
2256
2257 /*
2258 * AccECN option - after SACK
2259 * Don't send on <SYN>,
2260 * send only on <SYN,ACK> before ACCECN is negotiated when
2261 * the client requests it or
2262 * when doing an AccECN session. Don't send AccECN option
2263 * if retransmitting a SYN-ACK or a data segment
2264 */
2265 if ((tp->accurate_ecn_on ||
2266 (tp->l4s_enabled && (flags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK) &&
2267 (tp->ecn_flags & TE_ACE_SETUPRECEIVED))) &&
2268 (tp->ecn_flags & TE_RETRY_WITHOUT_ACO) == 0) {
2269 uint32_t *lp = (uint32_t *)(void *)(opt + optlen);
2270 /* lp will become outdated after options are added */
2271 tcp_add_accecn_option(tp, flags, lp, (uint8_t *)&optlen);
2272 /* Make sure we didn't write more than 40 bytes */
2273 ASSERT((u_char *)lp - opt <= MAX_TCPOPTLEN);
2274 }
2275 /* Pad TCP options to a 4 byte boundary */
2276 if (optlen < MAX_TCPOPTLEN && (optlen % sizeof(u_int32_t))) {
2277 int pad = sizeof(u_int32_t) - (optlen % sizeof(u_int32_t));
2278 u_char *bp = (u_char *)opt + optlen;
2279
2280 optlen += pad;
2281 while (pad) {
2282 *bp++ = TCPOPT_EOL;
2283 pad--;
2284 }
2285 }
2286
2287 /*
2288 * For Accurate ECN, send ACE flag based on r.cep, if
2289 * We have completed handshake and are in ESTABLISHED state, and
2290 * This is not the final ACK of 3WHS.
2291 */
2292 if (tp->accurate_ecn_on && TCPS_HAVEESTABLISHED(tp->t_state) &&
2293 (tp->ecn_flags & TE_ACE_FINAL_ACK_3WHS) == 0) {
2294 uint8_t ace = tp->t_aecn.t_rcv_ce_packets & TCP_ACE_MASK;
2295 if (ace & 0x01) {
2296 flags |= TH_ECE;
2297 } else {
2298 flags &= ~TH_ECE;
2299 }
2300 if (ace & 0x02) {
2301 flags |= TH_CWR;
2302 } else {
2303 flags &= ~TH_CWR;
2304 }
2305 if (ace & 0x04) {
2306 flags |= TH_AE;
2307 } else {
2308 flags &= ~TH_AE;
2309 }
2310 }
2311
2312 /*
2313 * RFC 3168 states that:
2314 * - If you ever sent an ECN-setup SYN/SYN-ACK you must be prepared
2315 * to handle the TCP ECE flag, even if you also later send a
2316 * non-ECN-setup SYN/SYN-ACK.
2317 * - If you ever send a non-ECN-setup SYN/SYN-ACK, you must not set
2318 * the ip ECT flag.
2319 *
2320 * It is not clear how the ECE flag would ever be set if you never
2321 * set the IP ECT flag on outbound packets. All the same, we use
2322 * the TE_SETUPSENT to indicate that we have committed to handling
2323 * the TCP ECE flag correctly. We use the TE_SENDIPECT to indicate
2324 * whether or not we should set the IP ECT flag on outbound packet
2325 *
2326 * For a SYN-ACK, send an ECN setup SYN-ACK
2327 *
2328 * Below we send ECN for three different handhshake states:
2329 * 1. Server received SYN and is sending a SYN-ACK (state->TCPS_SYN_RECEIVED)
2330 * - both classic and Accurate ECN have special encoding
2331 * 2. Client is sending SYN packet (state->SYN_SENT)
2332 * - both classic and Accurate ECN have special encoding
2333 * 3. Client is sending final ACK of 3WHS (state->ESTABLISHED)
2334 * - Only Accurate ECN has special encoding
2335 */
2336 if ((flags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK) &&
2337 (tp->ecn_flags & TE_ENABLE_ECN)) {
2338 flags = tcp_accecn_synack_respond(tp, flags);
2339 } else if ((flags & (TH_SYN | TH_ACK)) == TH_SYN &&
2340 (tp->ecn_flags & TE_ENABLE_ECN)) {
2341 if (tcp_send_ecn_flags_on_syn(tp)) {
2342 if (tp->l4s_enabled) {
2343 /*
2344 * We are negotiating AccECN in SYN.
2345 * We only set TE_SENDIPECT after the handshake
2346 * is complete.
2347 */
2348 flags |= TH_ACE;
2349 tp->ecn_flags |= (TE_ACE_SETUPSENT);
2350 } else {
2351 /*
2352 * Setting TH_ECE and TH_CWR makes this an
2353 * ECN-setup SYN
2354 */
2355 flags |= (TH_ECE | TH_CWR);
2356 /*
2357 * Record that we sent the ECN-setup and default to
2358 * setting IP ECT.
2359 */
2360 tp->ecn_flags |= (TE_SETUPSENT | TE_SENDIPECT);
2361 }
2362 tcpstat.tcps_ecn_client_setup++;
2363 tp->ecn_flags |= TE_CLIENT_SETUP;
2364 } else {
2365 /*
2366 * We sent an ECN-setup SYN but it was dropped.
2367 * Fall back to non-ECN and clear flag indicating
2368 * we should send data with IP ECT set.
2369 */
2370 if (tp->ecn_flags & (TE_SETUPSENT | TE_ACE_SETUPSENT)) {
2371 tcpstat.tcps_ecn_lost_syn++;
2372 tp->ecn_flags |= TE_LOST_SYN;
2373 }
2374 tp->ecn_flags &= ~TE_SENDIPECT;
2375 }
2376 } else if (tp->accurate_ecn_on && (tp->ecn_flags & TE_ACE_FINAL_ACK_3WHS) &&
2377 len == 0 && (flags & (TH_FLAGS_ALL)) == TH_ACK) {
2378 /*
2379 * Client has processed SYN-ACK and moved to ESTABLISHED.
2380 * This is the final ACK of 3WHS. If ACC_ECN has been negotiated,
2381 * then send the handshake encoding as per Table 3 of Accurate ECN draft.
2382 * We are clearing the ACE flags just in case if they were set before.
2383 * TODO: if client has to carry data in the 3WHS ACK, then we need to send a pure ACK first
2384 */
2385 flags &= ~(TH_AE | TH_CWR | TH_ECE);
2386 if (tp->ecn_flags & TE_ACE_SETUP_NON_ECT) {
2387 flags |= TH_CWR;
2388 tp->ecn_flags &= ~TE_ACE_SETUP_NON_ECT;
2389 } else if (tp->ecn_flags & TE_ACE_SETUP_ECT1) {
2390 flags |= (TH_CWR | TH_ECE);
2391 tp->ecn_flags &= ~TE_ACE_SETUP_ECT1;
2392 } else if (tp->ecn_flags & TE_ACE_SETUP_ECT0) {
2393 flags |= TH_AE;
2394 tp->ecn_flags &= ~TE_ACE_SETUP_ECT0;
2395 } else if (tp->ecn_flags & TE_ACE_SETUP_CE) {
2396 flags |= (TH_AE | TH_CWR);
2397 tp->ecn_flags &= ~TE_ACE_SETUP_CE;
2398 }
2399 tp->ecn_flags &= ~(TE_ACE_FINAL_ACK_3WHS);
2400 }
2401
2402 /*
2403 * Check if we should set the TCP CWR flag.
2404 * CWR flag is sent when we reduced the congestion window because
2405 * we received a TCP ECE or we performed a fast retransmit. We
2406 * never set the CWR flag on retransmitted packets. We only set
2407 * the CWR flag on data packets. Pure acks don't have this set.
2408 */
2409 if ((tp->ecn_flags & TE_SENDCWR) != 0 && len != 0 &&
2410 !SEQ_LT(tp->snd_nxt, tp->snd_max) && !rack_sack_rxmit) {
2411 flags |= TH_CWR;
2412 tp->ecn_flags &= ~TE_SENDCWR;
2413 }
2414
2415 /*
2416 * Check if we should set the TCP ECE flag.
2417 */
2418 if ((tp->ecn_flags & TE_SENDECE) != 0 && len == 0) {
2419 flags |= TH_ECE;
2420 tcpstat.tcps_ecn_sent_ece++;
2421 }
2422
2423 hdrlen += optlen;
2424
2425 /* Reset DSACK sequence numbers */
2426 tp->t_dsack_lseq = 0;
2427 tp->t_dsack_rseq = 0;
2428
2429 if (isipv6) {
2430 ipoptlen = ip6_optlen(inp);
2431 } else {
2432 if (tp_inp_options) {
2433 ipoptlen = tp_inp_options->m_len -
2434 offsetof(struct ipoption, ipopt_list);
2435 } else {
2436 ipoptlen = 0;
2437 }
2438 }
2439 #if IPSEC
2440 ipoptlen += ipsec_optlen;
2441 #endif
2442
2443 /*
2444 * Adjust data length if insertion of options will
2445 * bump the packet length beyond the t_maxopd length.
2446 * Clear the FIN bit because we cut off the tail of
2447 * the segment.
2448 *
2449 * When doing TSO limit a burst to TCP_MAXWIN minus the
2450 * IP, TCP and Options length to keep ip->ip_len from
2451 * overflowing. Prevent the last segment from being
2452 * fractional thus making them all equal sized and set
2453 * the flag to continue sending. TSO is disabled when
2454 * IP options or IPSEC are present.
2455 */
2456 if (len + optlen + ipoptlen > tp->t_maxopd) {
2457 /*
2458 * If there is still more to send,
2459 * don't close the connection.
2460 */
2461 flags &= ~TH_FIN;
2462 if (tso) {
2463 int32_t tso_maxlen;
2464
2465 tso_maxlen = tp->tso_max_segment_size ?
2466 tp->tso_max_segment_size : TCP_MAXWIN;
2467
2468 /* hdrlen includes optlen */
2469 if (len > tso_maxlen - hdrlen) {
2470 len = tso_maxlen - hdrlen;
2471 sendalot = 1;
2472 } else if (tp->t_flags & TF_NEEDFIN) {
2473 sendalot = 1;
2474 }
2475
2476 if (len % (tp->t_maxopd - optlen) != 0) {
2477 len = len - (len % (tp->t_maxopd - optlen));
2478 sendalot = 1;
2479 }
2480 } else {
2481 len = tp->t_maxopd - optlen - ipoptlen;
2482 sendalot = 1;
2483 }
2484 }
2485
2486 if (!(flags & TH_SYN) &&
2487 ((tp->accurate_ecn_on && (tp->ecn_flags & TE_SENDIPECT) != 0) ||
2488 inp->inp_max_pacing_rate != UINT64_MAX)) {
2489 uint32_t pacing_delay;
2490
2491 pacing_delay = tcp_pacer_get_packet_tx_time(tp, len, &pacing_tx_time);
2492
2493 if (TSTMP_GT(tcp_now_local + pacing_delay, tp->t_latest_tx)) {
2494 /*
2495 * We need to make sure that time never moves backwards. This is
2496 * needed because `tcp_now` is not the same as `microuptime`
2497 * and thus two threads trying to send (one from the app, one
2498 * from dlil_input) may end up with different views on the time
2499 * and thus we may end up going backwards...
2500 * So, make sure t_latest_tx is strictly increasing.
2501 */
2502 tp->t_latest_tx = tcp_now_local + pacing_delay;
2503 }
2504 } else {
2505 if (TSTMP_GT(tcp_now_local, tp->t_latest_tx)) {
2506 tp->t_latest_tx = tcp_now_local;
2507 }
2508 }
2509
2510 if (tsvalptr != NULL) {
2511 uint32_t tsval;
2512
2513 /*
2514 * pacing_delay is folded into t_latest_tx, so that our
2515 * RTT-estimate is not artificially inflated.
2516 */
2517 tsval = tp->t_ts_offset + tp->t_latest_tx;
2518 *tsvalptr = htonl(tsval);
2519 }
2520
2521 if (max_linkhdr + hdrlen > MCLBYTES) {
2522 panic("tcphdr too big");
2523 }
2524
2525 /* Check if there is enough data in the send socket
2526 * buffer to start measuring bandwidth
2527 */
2528 if ((tp->t_flagsext & TF_MEASURESNDBW) != 0 &&
2529 (tp->t_bwmeas != NULL) &&
2530 (tp->t_flagsext & TF_BWMEAS_INPROGRESS) == 0) {
2531 tp->t_bwmeas->bw_size = min(min(
2532 (so->so_snd.sb_cc - (tp->snd_max - tp->snd_una)),
2533 tp->snd_cwnd), tp->snd_wnd);
2534 if (tp->t_bwmeas->bw_minsize > 0 &&
2535 tp->t_bwmeas->bw_size < tp->t_bwmeas->bw_minsize) {
2536 tp->t_bwmeas->bw_size = 0;
2537 }
2538 if (tp->t_bwmeas->bw_maxsize > 0) {
2539 tp->t_bwmeas->bw_size = min(tp->t_bwmeas->bw_size,
2540 tp->t_bwmeas->bw_maxsize);
2541 }
2542 if (tp->t_bwmeas->bw_size > 0) {
2543 tp->t_flagsext |= TF_BWMEAS_INPROGRESS;
2544 tp->t_bwmeas->bw_start = tp->snd_max;
2545 tp->t_bwmeas->bw_ts = tcp_now_local;
2546 }
2547 }
2548
2549 VERIFY(inp->inp_flowhash != 0);
2550 /*
2551 * Grab a header mbuf, attaching a copy of data to
2552 * be transmitted, and initialize the header from
2553 * the template for sends on this connection.
2554 */
2555 if (len) {
2556 /* Remember what the last head-of-line packet-size was */
2557 if (tp->t_pmtud_lastseg_size == 0 && tp->snd_nxt == tp->snd_una) {
2558 ASSERT(len + optlen + ipoptlen <= IP_MAXPACKET);
2559 tp->t_pmtud_lastseg_size = (uint16_t)(len + optlen + ipoptlen);
2560 }
2561 if ((tp->t_flagsext & TF_FORCE) && len == 1) {
2562 tcpstat.tcps_sndprobe++;
2563 } else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || rack_sack_rxmit) {
2564 tcpstat.tcps_sndrexmitpack++;
2565 tcpstat.tcps_sndrexmitbyte += len;
2566 if (nstat_collect) {
2567 nstat_route_tx(inp->inp_route.ro_rt, 1,
2568 len, NSTAT_TX_FLAG_RETRANSMIT);
2569 INP_ADD_TXSTAT(inp, ifnet_count_type, 1, len);
2570 tp->t_stat.txretransmitbytes += len;
2571 tp->t_stat.rxmitpkts++;
2572 }
2573 if (tp->ecn_flags & TE_SENDIPECT) {
2574 tp->t_ecn_capable_packets_lost++;
2575 }
2576 } else {
2577 tcpstat.tcps_sndpack++;
2578 tcpstat.tcps_sndbyte += len;
2579
2580 if (nstat_collect) {
2581 INP_ADD_TXSTAT(inp, ifnet_count_type, 1, len);
2582 }
2583 if (tp->ecn_flags & TE_SENDIPECT) {
2584 tp->t_ecn_capable_packets_sent++;
2585 }
2586 inp_decr_sndbytes_unsent(so, len);
2587 }
2588 #if MPTCP
2589 if (tp->t_mpflags & TMPF_MPTCP_TRUE) {
2590 tcpstat.tcps_mp_sndpacks++;
2591 tcpstat.tcps_mp_sndbytes += len;
2592 }
2593 #endif /* MPTCP */
2594 /*
2595 * try to use the new interface that allocates all
2596 * the necessary mbuf hdrs under 1 mbuf lock and
2597 * avoids rescanning the socket mbuf list if
2598 * certain conditions are met. This routine can't
2599 * be used in the following cases...
2600 * 1) the protocol headers exceed the capacity of
2601 * of a single mbuf header's data area (no cluster attached)
2602 * 2) the length of the data being transmitted plus
2603 * the protocol headers fits into a single mbuf header's
2604 * data area (no cluster attached)
2605 */
2606 m = NULL;
2607
2608 /* minimum length we are going to allocate */
2609 allocated_len = MHLEN;
2610 if (MHLEN < hdrlen + max_linkhdr) {
2611 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2612 if (m == NULL) {
2613 error = ENOBUFS;
2614 TCP_LOG(tp, "MGETHDR error ENOBUFS");
2615 goto out;
2616 }
2617 MCLGET(m, M_DONTWAIT);
2618 if ((m->m_flags & M_EXT) == 0) {
2619 m_freem(m);
2620 error = ENOBUFS;
2621 TCP_LOG(tp, "MCLGET error ENOBUFS");
2622 goto out;
2623 }
2624 m->m_data += max_linkhdr;
2625 m->m_len = hdrlen;
2626 allocated_len = MCLBYTES;
2627 }
2628 if (len <= allocated_len - hdrlen - max_linkhdr) {
2629 if (m == NULL) {
2630 VERIFY(allocated_len <= MHLEN);
2631 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2632 if (m == NULL) {
2633 error = ENOBUFS;
2634 TCP_LOG(tp, "MGETHDR error ENOBUFS");
2635 goto out;
2636 }
2637 m->m_data += max_linkhdr;
2638 m->m_len = hdrlen;
2639 }
2640 /* makes sure we still have data left to be sent at this point */
2641 if (so->so_snd.sb_mb == NULL || off < 0) {
2642 if (m != NULL) {
2643 m_freem(m);
2644 }
2645 error = 0; /* should we return an error? */
2646 goto out;
2647 }
2648 m_copydata(so->so_snd.sb_mb, off, (int) len,
2649 mtod(m, caddr_t) + hdrlen);
2650 m->m_len += len;
2651 } else {
2652 uint32_t copymode;
2653 /*
2654 * Retain packet header metadata at the socket
2655 * buffer if this is is an MPTCP subflow,
2656 * otherwise move it.
2657 */
2658 copymode = M_COPYM_MOVE_HDR;
2659 #if MPTCP
2660 if (so->so_flags & SOF_MP_SUBFLOW) {
2661 copymode = M_COPYM_NOOP_HDR;
2662 }
2663 #endif /* MPTCP */
2664 if (m != NULL) {
2665 if (so->so_snd.sb_flags & SB_SENDHEAD) {
2666 VERIFY(so->so_snd.sb_flags & SB_SENDHEAD);
2667 VERIFY(so->so_snd.sb_sendoff <= so->so_snd.sb_cc);
2668
2669 m->m_next = m_copym_mode(so->so_snd.sb_mb,
2670 off, (int)len, M_DONTWAIT,
2671 &so->so_snd.sb_sendhead,
2672 &so->so_snd.sb_sendoff, copymode);
2673
2674 VERIFY(so->so_snd.sb_sendoff <= so->so_snd.sb_cc);
2675 } else {
2676 m->m_next = m_copym_mode(so->so_snd.sb_mb,
2677 off, (int)len, M_DONTWAIT,
2678 NULL, NULL, copymode);
2679 }
2680 if (m->m_next == NULL) {
2681 (void) m_free(m);
2682 error = ENOBUFS;
2683 TCP_LOG(tp, "m_copym_mode error ENOBUFS");
2684 goto out;
2685 }
2686 } else {
2687 /*
2688 * make sure we still have data left
2689 * to be sent at this point
2690 */
2691 if (so->so_snd.sb_mb == NULL) {
2692 error = 0; /* should we return an error? */
2693 goto out;
2694 }
2695
2696 /*
2697 * m_copym_with_hdrs will always return the
2698 * last mbuf pointer and the offset into it that
2699 * it acted on to fullfill the current request,
2700 * whether a valid 'hint' was passed in or not.
2701 */
2702 if (so->so_snd.sb_flags & SB_SENDHEAD) {
2703 VERIFY(so->so_snd.sb_flags & SB_SENDHEAD);
2704 VERIFY(so->so_snd.sb_sendoff <= so->so_snd.sb_cc);
2705
2706 m = m_copym_with_hdrs(so->so_snd.sb_mb,
2707 off, len, M_DONTWAIT, &so->so_snd.sb_sendhead,
2708 &so->so_snd.sb_sendoff, copymode);
2709
2710 VERIFY(so->so_snd.sb_sendoff <= so->so_snd.sb_cc);
2711 } else {
2712 m = m_copym_with_hdrs(so->so_snd.sb_mb,
2713 off, len, M_DONTWAIT, NULL,
2714 NULL, copymode);
2715 }
2716 if (m == NULL) {
2717 error = ENOBUFS;
2718 TCP_LOG(tp, "m_copym_with_hdrs error ENOBUFS");
2719 goto out;
2720 }
2721 m->m_data += max_linkhdr;
2722 m->m_len = hdrlen;
2723 }
2724 }
2725 /*
2726 * If we're sending everything we've got, set PUSH.
2727 * (This will keep happy those implementations which only
2728 * give data to the user when a buffer fills or
2729 * a PUSH comes in.)
2730 *
2731 * On SYN-segments we should not add the PUSH-flag.
2732 */
2733 if (off + len == so->so_snd.sb_cc && !(flags & TH_SYN)) {
2734 flags |= TH_PUSH;
2735 }
2736 } else {
2737 if (tp->t_flags & TF_ACKNOW) {
2738 tcpstat.tcps_sndacks++;
2739 } else if (flags & (TH_SYN | TH_FIN | TH_RST)) {
2740 tcpstat.tcps_sndctrl++;
2741 } else if (SEQ_GT(tp->snd_up, tp->snd_una)) {
2742 tcpstat.tcps_sndurg++;
2743 } else {
2744 tcpstat.tcps_sndwinup++;
2745 }
2746
2747 MGETHDR(m, M_DONTWAIT, MT_HEADER); /* MAC-OK */
2748 if (m == NULL) {
2749 error = ENOBUFS;
2750 TCP_LOG(tp, "MGETHDR error ENOBUFS");
2751 goto out;
2752 }
2753 if (MHLEN < (hdrlen + max_linkhdr)) {
2754 MCLGET(m, M_DONTWAIT);
2755 if ((m->m_flags & M_EXT) == 0) {
2756 m_freem(m);
2757 error = ENOBUFS;
2758 TCP_LOG(tp, "MCLGET error ENOBUFS");
2759 goto out;
2760 }
2761 }
2762 m->m_data += max_linkhdr;
2763 m->m_len = hdrlen;
2764 }
2765 m->m_pkthdr.rcvif = 0;
2766 m_add_crumb(m, PKT_CRUMB_TCP_OUTPUT);
2767
2768 /* Any flag other than pure-ACK: Do not compress! */
2769 if (flags & ~(TH_ACK)) {
2770 do_not_compress = TRUE;
2771 }
2772
2773 if (tp->rcv_scale == 0) {
2774 do_not_compress = TRUE;
2775 }
2776
2777 if (do_not_compress) {
2778 m->m_pkthdr.comp_gencnt = 0;
2779 } else {
2780 if (TSTMP_LT(tp->t_comp_ack_lastinc + tcp_ack_compression_rate, tcp_now_local)) {
2781 tp->t_comp_ack_gencnt++;
2782 /*
2783 * 0 means no compression, and ACK gencnt is encoded on 31 bits
2784 */
2785 if (tp->t_comp_ack_gencnt <= TCP_ACK_COMPRESSION_DUMMY ||
2786 tp->t_comp_ack_gencnt > INT_MAX) {
2787 tp->t_comp_ack_gencnt = TCP_ACK_COMPRESSION_DUMMY + 1;
2788 }
2789 tp->t_comp_ack_lastinc = tcp_now_local;
2790 }
2791 m->m_pkthdr.comp_gencnt = tp->t_comp_ack_gencnt;
2792 }
2793
2794 if (isipv6) {
2795 ip6 = mtod(m, struct ip6_hdr *);
2796 th = (struct tcphdr *)(void *)(ip6 + 1);
2797 tcp_fillheaders(m, tp, ip6, th, NULL, NULL);
2798
2799 if (tp->accurate_ecn_on) {
2800 /* We send ECT1 for ALL packets (data, control, fast retransmits, RTO) */
2801 if ((tp->ecn_flags & TE_SENDIPECT) != 0 && !(flags & TH_SYN)) {
2802 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT1 << 20);
2803 }
2804 } else {
2805 if ((tp->ecn_flags & TE_SENDIPECT) != 0 && len &&
2806 !SEQ_LT(tp->snd_nxt, tp->snd_max) && !rack_sack_rxmit) {
2807 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
2808 }
2809 }
2810 svc_flags |= PKT_SCF_IPV6;
2811 #if PF_ECN
2812 m_pftag(m)->pftag_hdr = (void *)ip6;
2813 m_pftag(m)->pftag_flags |= PF_TAG_HDR_INET6;
2814 #endif /* PF_ECN */
2815 } else {
2816 ip = mtod(m, struct ip *);
2817 th = (struct tcphdr *)(void *)(ip + 1);
2818 /* this picks up the pseudo header (w/o the length) */
2819 tcp_fillheaders(m, tp, ip, th, NULL, NULL);
2820
2821 if (tp->accurate_ecn_on) {
2822 /* We send ECT1 for ALL packets (data, control, fast retransmits, RTO) */
2823 if ((tp->ecn_flags & TE_SENDIPECT) != 0 && !(flags & TH_SYN)) {
2824 ip->ip_tos |= IPTOS_ECN_ECT1;
2825 }
2826 } else {
2827 if ((tp->ecn_flags & TE_SENDIPECT) != 0 && len &&
2828 !SEQ_LT(tp->snd_nxt, tp->snd_max) &&
2829 !rack_sack_rxmit && !(flags & TH_SYN)) {
2830 ip->ip_tos |= IPTOS_ECN_ECT0;
2831 }
2832 }
2833 #if PF_ECN
2834 m_pftag(m)->pftag_hdr = (void *)ip;
2835 m_pftag(m)->pftag_flags |= PF_TAG_HDR_INET;
2836 #endif /* PF_ECN */
2837 }
2838
2839 if (pacing_tx_time) {
2840 mbuf_set_tx_time(m, pacing_tx_time);
2841 }
2842
2843 /*
2844 * Fill in fields, remembering maximum advertised
2845 * window for use in delaying messages about window sizes.
2846 * If resending a FIN, be sure not to use a new sequence number.
2847 */
2848 if ((flags & TH_FIN) && (tp->t_flags & TF_SENTFIN) &&
2849 tp->snd_nxt == tp->snd_max) {
2850 tp->snd_nxt--;
2851 }
2852 /*
2853 * If we are doing retransmissions, then snd_nxt will
2854 * not reflect the first unsent octet. For ACK only
2855 * packets, we do not want the sequence number of the
2856 * retransmitted packet, we want the sequence number
2857 * of the next unsent octet. So, if there is no data
2858 * (and no SYN or FIN), use snd_max instead of snd_nxt
2859 * when filling in ti_seq. But if we are in persist
2860 * state, snd_max might reflect one byte beyond the
2861 * right edge of the window, so use snd_nxt in that
2862 * case, since we know we aren't doing a retransmission.
2863 * (retransmit and persist are mutually exclusive...)
2864 *
2865 * Note the state of this retransmit segment to detect spurious
2866 * retransmissions.
2867 */
2868 if (rack_sack_rxmit == 0) {
2869 if (len || (flags & (TH_SYN | TH_FIN)) ||
2870 tp->t_timer[TCPT_PERSIST]) {
2871 th->th_seq = htonl(tp->snd_nxt);
2872 if (len > 0) {
2873 m->m_pkthdr.tx_start_seq = tp->snd_nxt;
2874 m->m_pkthdr.pkt_flags |= PKTF_START_SEQ;
2875 }
2876 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
2877 if (SACK_ENABLED(tp) && len > 1 &&
2878 !(tp->t_flagsext & TF_SENT_TLPROBE)) {
2879 tcp_rxtseg_insert(tp, tp->snd_nxt,
2880 (tp->snd_nxt + len - 1));
2881 }
2882 if (len > 0) {
2883 m->m_pkthdr.pkt_flags |=
2884 PKTF_TCP_REXMT;
2885 }
2886 }
2887 } else {
2888 th->th_seq = htonl(tp->snd_max);
2889 }
2890 } else {
2891 /* Use RACK if enabled otherwise use SACK */
2892 if (TCP_RACK_ENABLED(tp)) {
2893 th->th_seq = htonl(seg->start_seq);
2894 tcp_rxtseg_insert(tp, seg->start_seq, (seg->start_seq + len - 1));
2895 } else {
2896 th->th_seq = htonl(p->rxmit);
2897 tcp_rxtseg_insert(tp, p->rxmit, (p->rxmit + len - 1));
2898 p->rxmit += len;
2899 tp->sackhint.sack_bytes_rexmit += len;
2900 }
2901 if (len > 0) {
2902 m->m_pkthdr.tx_start_seq = ntohl(th->th_seq);
2903 m->m_pkthdr.pkt_flags |=
2904 (PKTF_TCP_REXMT | PKTF_START_SEQ);
2905 }
2906 }
2907 th->th_ack = htonl(tp->rcv_nxt);
2908 tp->last_ack_sent = tp->rcv_nxt;
2909 if (optlen) {
2910 bcopy(opt, th + 1, optlen);
2911 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
2912 }
2913 /* Separate AE from flags */
2914 tcp_set_flags(th, flags);
2915 th->th_win = htons((u_short) (recwin >> tp->rcv_scale));
2916 tp->t_last_recwin = recwin;
2917 if (!(so->so_flags & SOF_MP_SUBFLOW)) {
2918 if (recwin > 0 && SEQ_LT(tp->rcv_adv, tp->rcv_nxt + recwin)) {
2919 tp->rcv_adv = tp->rcv_nxt + recwin;
2920 }
2921 } else {
2922 struct mptcb *mp_tp = tptomptp(tp);
2923 if (recwin > 0) {
2924 tp->rcv_adv = tp->rcv_nxt + recwin;
2925 }
2926
2927 if (recwin > 0 && MPTCP_SEQ_LT(mp_tp->mpt_rcvadv, mp_tp->mpt_rcvnxt + recwin)) {
2928 mp_tp->mpt_rcvadv = mp_tp->mpt_rcvnxt + recwin;
2929 }
2930 }
2931
2932 /*
2933 * Adjust the RXWIN0SENT flag - indicate that we have advertised
2934 * a 0 window. This may cause the remote transmitter to stall. This
2935 * flag tells soreceive() to disable delayed acknowledgements when
2936 * draining the buffer. This can occur if the receiver is attempting
2937 * to read more data then can be buffered prior to transmitting on
2938 * the connection.
2939 */
2940 if (th->th_win == 0) {
2941 tp->t_flags |= TF_RXWIN0SENT;
2942 } else {
2943 tp->t_flags &= ~TF_RXWIN0SENT;
2944 }
2945
2946 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
2947 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
2948 th->th_flags |= TH_URG;
2949 } else {
2950 /*
2951 * If no urgent pointer to send, then we pull
2952 * the urgent pointer to the left edge of the send window
2953 * so that it doesn't drift into the send window on sequence
2954 * number wraparound.
2955 */
2956 tp->snd_up = tp->snd_una; /* drag it along */
2957 }
2958
2959 /*
2960 * Put TCP length in extended header, and then
2961 * checksum extended header and data.
2962 */
2963 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
2964
2965 /*
2966 * If this is potentially the last packet on the stream, then mark
2967 * it in order to enable some optimizations in the underlying
2968 * layers
2969 */
2970 if (tp->t_state != TCPS_ESTABLISHED &&
2971 (tp->t_state == TCPS_CLOSING || tp->t_state == TCPS_TIME_WAIT
2972 || tp->t_state == TCPS_LAST_ACK || (th->th_flags & TH_RST))) {
2973 m->m_pkthdr.pkt_flags |= PKTF_LAST_PKT;
2974 }
2975
2976 if (isipv6) {
2977 /*
2978 * ip6_plen is not need to be filled now, and will be filled
2979 * in ip6_output.
2980 */
2981 m->m_pkthdr.csum_flags = CSUM_TCPIPV6;
2982 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
2983 if (len + optlen) {
2984 th->th_sum = in_addword(th->th_sum,
2985 htons((u_short)(optlen + len)));
2986 }
2987 } else {
2988 m->m_pkthdr.csum_flags = CSUM_TCP;
2989 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
2990 if (len + optlen) {
2991 th->th_sum = in_addword(th->th_sum,
2992 htons((u_short)(optlen + len)));
2993 }
2994 }
2995
2996 /*
2997 * Enable TSO and specify the size of the segments.
2998 * The TCP pseudo header checksum is always provided.
2999 */
3000 if (tso) {
3001 if (isipv6) {
3002 m->m_pkthdr.csum_flags |= CSUM_TSO_IPV6;
3003 } else {
3004 m->m_pkthdr.csum_flags |= CSUM_TSO_IPV4;
3005 }
3006
3007 m->m_pkthdr.tso_segsz = (uint16_t)(tp->t_maxopd - optlen);
3008 m->m_pkthdr.tx_hdr_len = (uint16_t)hdrlen;
3009 } else {
3010 m->m_pkthdr.tso_segsz = 0;
3011 }
3012
3013 /*
3014 * In transmit state, time the transmission and arrange for
3015 * the retransmit. In persist state, just set snd_max.
3016 */
3017 if (!(tp->t_flagsext & TF_FORCE)
3018 || tp->t_timer[TCPT_PERSIST] == 0) {
3019 tcp_seq startseq = tp->snd_nxt;
3020
3021 /*
3022 * Advance snd_nxt over sequence space of this segment.
3023 */
3024 if (flags & (TH_SYN | TH_FIN)) {
3025 if (flags & TH_SYN) {
3026 tp->snd_nxt++;
3027 }
3028 if ((flags & TH_FIN) &&
3029 !(tp->t_flags & TF_SENTFIN)) {
3030 tp->snd_nxt++;
3031 tp->t_flags |= TF_SENTFIN;
3032 }
3033 }
3034 if (rack_sack_rxmit) {
3035 goto timer;
3036 }
3037 if (sack_rescue_rxt == true) {
3038 tp->snd_nxt = old_snd_nxt;
3039 sack_rescue_rxt = false;
3040 tcpstat.tcps_pto_in_recovery++;
3041 } else {
3042 tp->snd_nxt += len;
3043 }
3044 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
3045 tp->snd_max = tp->snd_nxt;
3046 tp->t_sndtime = tp->t_latest_tx;
3047 /*
3048 * Time this transmission if not a retransmission and
3049 * not currently timing anything.
3050 */
3051 if (tp->t_rtttime == 0) {
3052 tp->t_rtttime = tp->t_latest_tx;
3053 tp->t_rtseq = startseq;
3054 tcpstat.tcps_segstimed++;
3055
3056 /* update variables related to pipe ack */
3057 tp->t_pipeack_lastuna = tp->snd_una;
3058 }
3059 }
3060
3061 /*
3062 * Set retransmit timer if not currently set,
3063 * and not doing an ack or a keep-alive probe.
3064 */
3065 timer:
3066 if (tp->t_timer[TCPT_REXMT] == 0 &&
3067 ((rack_sack_rxmit && tp->snd_nxt != tp->snd_max) ||
3068 tp->snd_nxt != tp->snd_una || (flags & TH_FIN))) {
3069 if (tp->t_timer[TCPT_PERSIST]) {
3070 tp->t_timer[TCPT_PERSIST] = 0;
3071 tp->t_persist_stop = 0;
3072 TCP_RESET_REXMT_STATE(tp);
3073 }
3074 tcp_set_rto(tp);
3075 }
3076
3077 if (tcp_enable_tlp && len != 0) {
3078 tcp_set_pto(tp);
3079 }
3080 } else {
3081 /*
3082 * Persist case, update snd_max but since we are in
3083 * persist mode (no window) we do not update snd_nxt.
3084 */
3085 int xlen = len;
3086 if (flags & TH_SYN) {
3087 ++xlen;
3088 }
3089 if ((flags & TH_FIN) &&
3090 !(tp->t_flags & TF_SENTFIN)) {
3091 ++xlen;
3092 tp->t_flags |= TF_SENTFIN;
3093 }
3094 if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) {
3095 tp->snd_max = tp->snd_nxt + len;
3096 tp->t_sndtime = tp->t_latest_tx;
3097 }
3098 }
3099
3100 /*
3101 * Fill in IP length and desired time to live and
3102 * send to IP level. There should be a better way
3103 * to handle ttl and tos; we could keep them in
3104 * the template, but need a way to checksum without them.
3105 */
3106 /*
3107 * m->m_pkthdr.len should have been set before cksum calcuration,
3108 * because in6_cksum() need it.
3109 */
3110 if (isipv6) {
3111 /*
3112 * we separately set hoplimit for every segment, since the
3113 * user might want to change the value via setsockopt.
3114 * Also, desired default hop limit might be changed via
3115 * Neighbor Discovery.
3116 */
3117 ip6->ip6_hlim = in6_selecthlim(inp, inp->in6p_route.ro_rt ?
3118 inp->in6p_route.ro_rt->rt_ifp : NULL);
3119
3120 /* Don't set ECT bit if requested by an app */
3121
3122 /* Set ECN bits for testing purposes */
3123 if (tp->ecn_flags & TE_FORCE_ECT1) {
3124 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT1 << 20);
3125 } else if (tp->ecn_flags & TE_FORCE_ECT0) {
3126 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
3127 }
3128
3129 KERNEL_DEBUG(DBG_LAYER_BEG,
3130 ((inp->inp_fport << 16) | inp->inp_lport),
3131 (((inp->in6p_laddr.s6_addr16[0] & 0xffff) << 16) |
3132 (inp->in6p_faddr.s6_addr16[0] & 0xffff)),
3133 sendalot, 0, 0);
3134 } else {
3135 ASSERT(m->m_pkthdr.len <= IP_MAXPACKET);
3136 ip->ip_len = (u_short)m->m_pkthdr.len;
3137 ip->ip_ttl = inp->inp_ip_ttl; /* XXX */
3138
3139 /* Don't set ECN bit if requested by an app */
3140 ip->ip_tos |= (inp->inp_ip_tos & ~IPTOS_ECN_MASK);
3141
3142 /* Set ECN bits for testing purposes */
3143 if (tp->ecn_flags & TE_FORCE_ECT1) {
3144 ip->ip_tos |= IPTOS_ECN_ECT1;
3145 } else if (tp->ecn_flags & TE_FORCE_ECT0) {
3146 ip->ip_tos |= IPTOS_ECN_ECT0;
3147 }
3148
3149 KERNEL_DEBUG(DBG_LAYER_BEG,
3150 ((inp->inp_fport << 16) | inp->inp_lport),
3151 (((inp->inp_laddr.s_addr & 0xffff) << 16) |
3152 (inp->inp_faddr.s_addr & 0xffff)), 0, 0, 0);
3153 }
3154
3155 /*
3156 * See if we should do MTU discovery.
3157 * Look at the flag updated on the following criterias:
3158 * 1) Path MTU discovery is authorized by the sysctl
3159 * 2) The route isn't set yet (unlikely but could happen)
3160 * 3) The route is up
3161 * 4) the MTU is not locked (if it is, then discovery has been
3162 * disabled for that route)
3163 */
3164 if (!isipv6) {
3165 if (path_mtu_discovery && (tp->t_flags & TF_PMTUD)) {
3166 ip->ip_off |= IP_DF;
3167 }
3168 }
3169
3170 #if NECP
3171 {
3172 necp_kernel_policy_id policy_id;
3173 necp_kernel_policy_id skip_policy_id;
3174 u_int32_t route_rule_id;
3175 u_int32_t pass_flags;
3176 if (!necp_socket_is_allowed_to_send_recv(inp, NULL, 0, &policy_id, &route_rule_id, &skip_policy_id, &pass_flags)) {
3177 TCP_LOG_DROP_NECP(isipv6 ? (void *)ip6 : (void *)ip, th, tp, true);
3178 m_drop_if(m, outifp, DROPTAP_FLAG_DIR_OUT | DROPTAP_FLAG_L2_MISSING, DROP_REASON_TCP_NECP, NULL, 0);
3179 error = EHOSTUNREACH;
3180 goto out;
3181 }
3182 necp_mark_packet_from_socket(m, inp, policy_id, route_rule_id, skip_policy_id, pass_flags);
3183
3184 if (net_qos_policy_restricted != 0) {
3185 necp_socket_update_qos_marking(inp, inp->inp_route.ro_rt, route_rule_id);
3186 }
3187 }
3188 #endif /* NECP */
3189
3190 #if IPSEC
3191 if (inp->inp_sp != NULL) {
3192 ipsec_setsocket(m, so);
3193 }
3194 #endif /*IPSEC*/
3195
3196 /*
3197 * The socket is kept locked while sending out packets in ip_output, even if packet chaining is not active.
3198 */
3199 lost = 0;
3200
3201 /*
3202 * Embed the flow hash in pkt hdr and mark the packet as
3203 * capable of flow controlling
3204 */
3205 m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
3206 m->m_pkthdr.pkt_flowid = inp->inp_flowhash;
3207 m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC | PKTF_FLOW_ADV);
3208 m->m_pkthdr.pkt_proto = IPPROTO_TCP;
3209 m->m_pkthdr.tx_tcp_pid = so->last_pid;
3210 if (so->so_flags & SOF_DELEGATED) {
3211 m->m_pkthdr.tx_tcp_e_pid = so->e_pid;
3212 } else {
3213 m->m_pkthdr.tx_tcp_e_pid = 0;
3214 }
3215
3216 m->m_nextpkt = NULL;
3217
3218 if (outifp != NULL &&
3219 !(outifp->if_flags & IFF_LOOPBACK)) {
3220 /* Hint to prioritize this packet if
3221 * 1. if the packet has no data
3222 * 2. the interface supports transmit-start model and did
3223 * not disable ACK prioritization.
3224 * 3. Only ACK flag is set.
3225 * 4. there is no outstanding data on this connection.
3226 * 5. Link heuristics are not enabled for the interface
3227 */
3228 if (len == 0 && (outifp->if_eflags & (IFEF_TXSTART | IFEF_NOACKPRI)) == IFEF_TXSTART) {
3229 if (link_heuristics_enabled && (tcp_link_heuristics_flags & TCP_LINK_HEUR_NOACKPRI) != 0) {
3230 IF_TCP_STATINC(outifp, linkheur_noackpri);
3231 } else {
3232 if (th->th_flags == TH_ACK &&
3233 tp->snd_una == tp->snd_max &&
3234 tp->t_timer[TCPT_REXMT] == 0) {
3235 svc_flags |= PKT_SCF_TCP_ACK;
3236 }
3237 if (th->th_flags & TH_SYN) {
3238 svc_flags |= PKT_SCF_TCP_SYN;
3239 }
3240 }
3241 }
3242 set_packet_service_class(m, so, sotc, svc_flags);
3243 } else {
3244 /*
3245 * Optimization for loopback just set the mbuf
3246 * service class
3247 */
3248 (void) m_set_service_class(m, so_tc2msc(sotc));
3249 }
3250
3251 tp->t_pktlist_sentlen += len;
3252 tp->t_lastchain++;
3253
3254 if (isipv6) {
3255 DTRACE_TCP5(send, struct mbuf *, m, struct inpcb *, inp,
3256 struct ip6 *, ip6, struct tcpcb *, tp, struct tcphdr *,
3257 th);
3258 } else {
3259 DTRACE_TCP5(send, struct mbuf *, m, struct inpcb *, inp,
3260 struct ip *, ip, struct tcpcb *, tp, struct tcphdr *, th);
3261 }
3262
3263 if (tp->t_pktlist_head != NULL) {
3264 tp->t_pktlist_tail->m_nextpkt = m;
3265 tp->t_pktlist_tail = m;
3266 } else {
3267 packchain_newlist++;
3268 tp->t_pktlist_head = tp->t_pktlist_tail = m;
3269 }
3270
3271 /* Append segment to time-ordered list and RB tree used for RACK */
3272 if (TCP_RACK_ENABLED(tp) && len != 0) {
3273 uint8_t retransmit_flag = 0;
3274 if (tp->t_flagsext & TF_SENT_TLPROBE) {
3275 /* Only set the at least once retransmitted flag */
3276 retransmit_flag = (m->m_pkthdr.pkt_flags & PKTF_TCP_REXMT) ? TCP_SEGMENT_RETRANSMITTED_ATLEAST_ONCE : 0;
3277 } else {
3278 /* Set both RACK and EVER retransmitted flags */
3279 retransmit_flag = (m->m_pkthdr.pkt_flags & PKTF_TCP_REXMT) ? TCP_SEGMENT_RETRANSMITTED : 0;
3280 }
3281 tcp_seg_sent_insert(tp, seg, ntohl(th->th_seq),
3282 ntohl(th->th_seq) + len, tp->t_latest_tx, retransmit_flag);
3283 }
3284
3285 if ((th->th_flags & TH_SYN) != 0) {
3286 (void)os_add_overflow(tp->t_syn_sent, 1, &tp->t_syn_sent);
3287 if (tp->t_rxtshift > 0) {
3288 m->m_pkthdr.pkt_flags |= PKTF_TCP_REXMT;
3289 }
3290 }
3291 if ((th->th_flags & TH_FIN) != 0) {
3292 (void)os_add_overflow(tp->t_fin_sent, 1, &tp->t_fin_sent);
3293 if (tp->t_rxtshift > 0) {
3294 m->m_pkthdr.pkt_flags |= PKTF_TCP_REXMT;
3295 }
3296 }
3297 if ((th->th_flags & TH_RST) != 0) {
3298 (void)os_add_overflow(tp->t_rst_sent, 1, &tp->t_rst_sent);
3299 if (tp->t_rxtshift > 0) {
3300 m->m_pkthdr.pkt_flags |= PKTF_TCP_REXMT;
3301 }
3302 }
3303 TCP_LOG_TH_FLAGS(isipv6 ? (void *)ip6 : (void *)ip, th, tp, true,
3304 outifp != NULL ? outifp : inp->inp_boundifp);
3305
3306 if (link_heuristics_enabled && (tcp_link_heuristics_flags & TCP_LINK_HEUR_RXMT_COMP) != 0 &&
3307 (len != 0 || (th->th_flags & TH_FIN) != 0)) {
3308 /*
3309 * Set compression flag if gencnt of segment is the same as the last sent segment
3310 * otherwise record the gencnt of the segment that we are sending
3311 */
3312 uint32_t gencnt = ntohl(th->th_seq) & TCP_COMP_RXMT_GENCNT_MASK;
3313
3314 if ((m->m_pkthdr.pkt_flags & PKTF_TCP_REXMT) != 0 && gencnt == tp->t_comp_rxmt_gencnt) {
3315 IF_TCP_STATINC(outifp, linkheur_comprxmt);
3316 m->m_pkthdr.comp_gencnt = gencnt;
3317 } else {
3318 tp->t_comp_rxmt_gencnt = gencnt;
3319 }
3320 }
3321
3322 if (sendalot == 0 || (tp->t_state != TCPS_ESTABLISHED) ||
3323 (tp->t_flags & TF_ACKNOW) ||
3324 (tp->t_flagsext & TF_FORCE) ||
3325 tp->t_lastchain >= tcp_packet_chaining) {
3326 error = 0;
3327 while (inp->inp_sndinprog_cnt == 0 &&
3328 tp->t_pktlist_head != NULL) {
3329 packetlist = tp->t_pktlist_head;
3330 packchain_listadd = tp->t_lastchain;
3331 packchain_sent++;
3332 lost = tp->t_pktlist_sentlen;
3333 TCP_PKTLIST_CLEAR(tp);
3334
3335 error = tcp_ip_output(so, tp, packetlist,
3336 packchain_listadd, tp_inp_options,
3337 (so_options & SO_DONTROUTE),
3338 (rack_sack_rxmit || (sack_bytes_rxmt != 0)), isipv6);
3339 if (error) {
3340 /*
3341 * Take into account the rest of unsent
3342 * packets in the packet list for this tcp
3343 * into "lost", since we're about to free
3344 * the whole list below.
3345 */
3346 lost += tp->t_pktlist_sentlen;
3347 break;
3348 } else {
3349 lost = 0;
3350 }
3351 }
3352 /* tcp was closed while we were in ip; resume close */
3353 if (inp->inp_sndinprog_cnt == 0 &&
3354 (tp->t_flags & TF_CLOSING)) {
3355 tp->t_flags &= ~TF_CLOSING;
3356 (void) tcp_close(tp);
3357 return 0;
3358 }
3359 } else {
3360 error = 0;
3361 packchain_looped++;
3362 tcpstat.tcps_sndtotal++;
3363
3364 goto again;
3365 }
3366 if (error) {
3367 /*
3368 * Assume that the packets were lost, so back out the
3369 * sequence number advance, if any. Note that the "lost"
3370 * variable represents the amount of user data sent during
3371 * the recent call to ip_output_list() plus the amount of
3372 * user data in the packet list for this tcp at the moment.
3373 */
3374 if (!(tp->t_flagsext & TF_FORCE)
3375 || tp->t_timer[TCPT_PERSIST] == 0) {
3376 /*
3377 * No need to check for TH_FIN here because
3378 * the TF_SENTFIN flag handles that case.
3379 */
3380 if ((flags & TH_SYN) == 0) {
3381 /*
3382 * RACK will mark these segments lost on its own
3383 * when new ACK arrives, no need to adjust anything here.
3384 * In fact doing so would be wrong, as RACK segments are
3385 * ordered in time (not sequence number).
3386 */
3387 if (rack_sack_rxmit && !TCP_RACK_ENABLED(tp)) {
3388 if (SEQ_GT((p->rxmit - lost),
3389 tp->snd_una)) {
3390 p->rxmit -= lost;
3391
3392 if (SEQ_LT(p->rxmit, p->start)) {
3393 p->rxmit = p->start;
3394 }
3395 } else {
3396 lost = p->rxmit - tp->snd_una;
3397 p->rxmit = tp->snd_una;
3398
3399 if (SEQ_LT(p->rxmit, p->start)) {
3400 p->rxmit = p->start;
3401 }
3402 }
3403 tp->sackhint.sack_bytes_rexmit -= lost;
3404 if (tp->sackhint.sack_bytes_rexmit < 0) {
3405 tp->sackhint.sack_bytes_rexmit = 0;
3406 }
3407 } else {
3408 if (SEQ_GT((tp->snd_nxt - lost),
3409 tp->snd_una)) {
3410 tp->snd_nxt -= lost;
3411 } else {
3412 tp->snd_nxt = tp->snd_una;
3413 }
3414 }
3415 }
3416 }
3417 out:
3418 if (tp->t_pktlist_head != NULL) {
3419 m_drop_list(tp->t_pktlist_head, outifp, DROPTAP_FLAG_DIR_OUT | DROPTAP_FLAG_L2_MISSING, DROP_REASON_TCP_PKT_UNSENT, NULL, 0);
3420 }
3421 TCP_PKTLIST_CLEAR(tp);
3422
3423 if (error == ENOBUFS) {
3424 /*
3425 * Set retransmit timer if not currently set
3426 * when we failed to send a segment that can be
3427 * retransmitted (i.e. not pure ack or rst)
3428 */
3429 if (tp->t_timer[TCPT_REXMT] == 0 &&
3430 tp->t_timer[TCPT_PERSIST] == 0 &&
3431 (len != 0 || (flags & (TH_SYN | TH_FIN)) != 0 ||
3432 so->so_snd.sb_cc > 0)) {
3433 tcp_set_rto(tp);
3434 }
3435 tp->snd_cwnd = tp->t_maxseg;
3436 tp->t_bytes_acked = 0;
3437 tcp_check_timer_state(tp);
3438 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
3439
3440 TCP_LOG_OUTPUT(tp, "error ENOBUFS silently handled");
3441
3442 tcp_ccdbg_trace(tp, NULL, TCP_CC_OUTPUT_ERROR);
3443 return 0;
3444 }
3445 if (error == EMSGSIZE) {
3446 /*
3447 * ip_output() will have already fixed the route
3448 * for us. tcp_mtudisc() will, as its last action,
3449 * initiate retransmission, so it is important to
3450 * not do so here.
3451 *
3452 * If TSO was active we either got an interface
3453 * without TSO capabilits or TSO was turned off.
3454 * Disable it for this connection as too and
3455 * immediatly retry with MSS sized segments generated
3456 * by this function.
3457 */
3458 if (tso) {
3459 tp->t_flags &= ~TF_TSO;
3460 }
3461
3462 tcp_mtudisc(inp, 0);
3463 tcp_check_timer_state(tp);
3464
3465 TCP_LOG_OUTPUT(tp, "error EMSGSIZE silently handled");
3466
3467 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
3468 return 0;
3469 }
3470 /*
3471 * Unless this is due to interface restriction policy,
3472 * treat EHOSTUNREACH/ENETDOWN/EADDRNOTAVAIL as a soft error.
3473 */
3474 if ((error == EHOSTUNREACH || error == ENETDOWN || error == EADDRNOTAVAIL) &&
3475 TCPS_HAVERCVDSYN(tp->t_state) &&
3476 !inp_restricted_send(inp, outifp)) {
3477 tp->t_softerror = error;
3478 TCP_LOG_OUTPUT(tp, "soft error %d silently handled", error);
3479 error = 0;
3480 } else {
3481 TCP_LOG_OUTPUT(tp, "error %d", error);
3482 }
3483 tcp_check_timer_state(tp);
3484 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
3485 return error;
3486 }
3487
3488 tcpstat.tcps_sndtotal++;
3489
3490 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
3491 if (sendalot) {
3492 goto again;
3493 }
3494
3495 tcp_check_timer_state(tp);
3496
3497 return 0;
3498 }
3499
3500 static int
tcp_ip_output(struct socket * so,struct tcpcb * tp,struct mbuf * pkt,int cnt,struct mbuf * opt,int flags,int sack_in_progress,boolean_t isipv6)3501 tcp_ip_output(struct socket *so, struct tcpcb *tp, struct mbuf *pkt,
3502 int cnt, struct mbuf *opt, int flags, int sack_in_progress, boolean_t isipv6)
3503 {
3504 int error = 0;
3505 boolean_t chain;
3506 boolean_t unlocked = FALSE;
3507 boolean_t ifdenied = FALSE;
3508 struct inpcb *__single inp = tp->t_inpcb;
3509 struct ifnet *__single outif = NULL;
3510 bool check_qos_marking_again = (so->so_flags1 & SOF1_QOSMARKING_POLICY_OVERRIDE) ? FALSE : TRUE;
3511 bool fadv_congested = FALSE;
3512
3513 union {
3514 struct route _ro;
3515 struct route_in6 _ro6;
3516 } route_u_ = {};
3517 #define ro route_u_._ro
3518 #define ro6 route_u_._ro6
3519
3520 union {
3521 struct ip_out_args _ipoa;
3522 struct ip6_out_args _ip6oa;
3523 } out_args_u_ = {};
3524 #define ipoa out_args_u_._ipoa
3525 #define ip6oa out_args_u_._ip6oa
3526
3527 if (isipv6) {
3528 ip6oa.ip6oa_boundif = IFSCOPE_NONE;
3529 ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR;
3530 ip6oa.ip6oa_sotc = SO_TC_UNSPEC;
3531 ip6oa.ip6oa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
3532 } else {
3533 ipoa.ipoa_boundif = IFSCOPE_NONE;
3534 ipoa.ipoa_flags = IPOAF_SELECT_SRCIF | IPOAF_BOUND_SRCADDR;
3535 ipoa.ipoa_sotc = SO_TC_UNSPEC;
3536 ipoa.ipoa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
3537 }
3538
3539 struct flowadv *__single adv =
3540 (isipv6 ? &ip6oa.ip6oa_flowadv : &ipoa.ipoa_flowadv);
3541
3542 /* If socket was bound to an ifindex, tell ip_output about it */
3543 if (inp->inp_flags & INP_BOUND_IF) {
3544 if (isipv6) {
3545 ip6oa.ip6oa_boundif = inp->inp_boundifp->if_index;
3546 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
3547 } else {
3548 ipoa.ipoa_boundif = inp->inp_boundifp->if_index;
3549 ipoa.ipoa_flags |= IPOAF_BOUND_IF;
3550 }
3551 } else if (!in6_embedded_scope && isipv6 && (IN6_IS_SCOPE_EMBED(&inp->in6p_faddr))) {
3552 ip6oa.ip6oa_boundif = inp->inp_fifscope;
3553 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
3554 }
3555
3556 if (INP_NO_CELLULAR(inp)) {
3557 if (isipv6) {
3558 ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR;
3559 } else {
3560 ipoa.ipoa_flags |= IPOAF_NO_CELLULAR;
3561 }
3562 }
3563 if (INP_NO_EXPENSIVE(inp)) {
3564 if (isipv6) {
3565 ip6oa.ip6oa_flags |= IP6OAF_NO_EXPENSIVE;
3566 } else {
3567 ipoa.ipoa_flags |= IPOAF_NO_EXPENSIVE;
3568 }
3569 }
3570 if (INP_NO_CONSTRAINED(inp)) {
3571 if (isipv6) {
3572 ip6oa.ip6oa_flags |= IP6OAF_NO_CONSTRAINED;
3573 } else {
3574 ipoa.ipoa_flags |= IPOAF_NO_CONSTRAINED;
3575 }
3576 }
3577 if (INP_AWDL_UNRESTRICTED(inp)) {
3578 if (isipv6) {
3579 ip6oa.ip6oa_flags |= IP6OAF_AWDL_UNRESTRICTED;
3580 } else {
3581 ipoa.ipoa_flags |= IPOAF_AWDL_UNRESTRICTED;
3582 }
3583 }
3584 if (INP_INTCOPROC_ALLOWED(inp) && isipv6) {
3585 ip6oa.ip6oa_flags |= IP6OAF_INTCOPROC_ALLOWED;
3586 }
3587 if (INP_MANAGEMENT_ALLOWED(inp)) {
3588 if (isipv6) {
3589 ip6oa.ip6oa_flags |= IP6OAF_MANAGEMENT_ALLOWED;
3590 } else {
3591 ipoa.ipoa_flags |= IPOAF_MANAGEMENT_ALLOWED;
3592 }
3593 }
3594 if (INP_ULTRA_CONSTRAINED_ALLOWED(inp)) {
3595 if (isipv6) {
3596 ip6oa.ip6oa_flags |= IP6OAF_ULTRA_CONSTRAINED_ALLOWED;
3597 } else {
3598 ipoa.ipoa_flags |= IPOAF_ULTRA_CONSTRAINED_ALLOWED;
3599 }
3600 }
3601 if (isipv6) {
3602 ip6oa.ip6oa_sotc = so->so_traffic_class;
3603 ip6oa.ip6oa_netsvctype = so->so_netsvctype;
3604 ip6oa.qos_marking_gencount = inp->inp_policyresult.results.qos_marking_gencount;
3605 } else {
3606 ipoa.ipoa_sotc = so->so_traffic_class;
3607 ipoa.ipoa_netsvctype = so->so_netsvctype;
3608 ipoa.qos_marking_gencount = inp->inp_policyresult.results.qos_marking_gencount;
3609 }
3610 if ((so->so_flags1 & SOF1_QOSMARKING_ALLOWED)) {
3611 if (isipv6) {
3612 ip6oa.ip6oa_flags |= IP6OAF_QOSMARKING_ALLOWED;
3613 } else {
3614 ipoa.ipoa_flags |= IPOAF_QOSMARKING_ALLOWED;
3615 }
3616 }
3617 if (check_qos_marking_again) {
3618 if (isipv6) {
3619 ip6oa.ip6oa_flags |= IP6OAF_REDO_QOSMARKING_POLICY;
3620 } else {
3621 ipoa.ipoa_flags |= IPOAF_REDO_QOSMARKING_POLICY;
3622 }
3623 }
3624 if (isipv6) {
3625 flags |= IPV6_OUTARGS;
3626 } else {
3627 flags |= IP_OUTARGS;
3628 }
3629
3630 /* Copy the cached route and take an extra reference */
3631 if (isipv6) {
3632 in6p_route_copyout(inp, &ro6);
3633 } else {
3634 inp_route_copyout(inp, &ro);
3635 }
3636 #if (DEBUG || DEVELOPMENT)
3637 if ((so->so_flags & SOF_MARK_WAKE_PKT) && pkt != NULL) {
3638 so->so_flags &= ~SOF_MARK_WAKE_PKT;
3639 pkt->m_pkthdr.pkt_flags |= PKTF_WAKE_PKT;
3640 }
3641 #endif /* (DEBUG || DEVELOPMENT) */
3642
3643 /*
3644 * Make sure ACK/DELACK conditions are cleared before
3645 * we unlock the socket.
3646 */
3647 tp->last_ack_sent = tp->rcv_nxt;
3648 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
3649 tp->t_timer[TCPT_DELACK] = 0;
3650 tp->t_unacksegs = 0;
3651 tp->t_unacksegs_ce = 0;
3652
3653 /* Increment the count of outstanding send operations */
3654 inp->inp_sndinprog_cnt++;
3655
3656 /*
3657 * If allowed, unlock TCP socket while in IP
3658 * but only if the connection is established and
3659 * in a normal mode where reentrancy on the tcpcb won't be
3660 * an issue:
3661 * - there is no SACK episode
3662 * - we're not in Fast Recovery mode
3663 * - if we're not sending from an upcall.
3664 */
3665 if (tcp_output_unlocked && !so->so_upcallusecount &&
3666 (tp->t_state == TCPS_ESTABLISHED) && (sack_in_progress == 0) &&
3667 !IN_FASTRECOVERY(tp) && !(so->so_flags & SOF_MP_SUBFLOW)) {
3668 unlocked = TRUE;
3669 socket_unlock(so, 0);
3670 }
3671
3672 /*
3673 * Don't send down a chain of packets when:
3674 * - TCP chaining is disabled
3675 * - there is an IPsec rule set
3676 * - there is a non default rule set for the firewall
3677 */
3678
3679 chain = tcp_packet_chaining > 1
3680 #if IPSEC
3681 && ipsec_bypass
3682 #endif
3683 ; // I'm important, not extraneous
3684
3685 while (pkt != NULL) {
3686 struct mbuf *npkt = pkt->m_nextpkt;
3687
3688 if (!chain) {
3689 pkt->m_nextpkt = NULL;
3690 /*
3691 * If we are not chaining, make sure to set the packet
3692 * list count to 0 so that IP takes the right path;
3693 * this is important for cases such as IPsec where a
3694 * single mbuf might result in multiple mbufs as part
3695 * of the encapsulation. If a non-zero count is passed
3696 * down to IP, the head of the chain might change and
3697 * we could end up skipping it (thus generating bogus
3698 * packets). Fixing it in IP would be desirable, but
3699 * for now this would do it.
3700 */
3701 cnt = 0;
3702 }
3703 if (isipv6) {
3704 error = ip6_output_list(pkt, cnt,
3705 inp->in6p_outputopts, &ro6, flags, NULL, NULL,
3706 &ip6oa);
3707 ifdenied = (ip6oa.ip6oa_flags & IP6OAF_R_IFDENIED);
3708 } else {
3709 error = ip_output_list(pkt, cnt, opt, &ro, flags, NULL,
3710 &ipoa);
3711 ifdenied = (ipoa.ipoa_flags & IPOAF_R_IFDENIED);
3712 }
3713
3714 if (adv->code == FADV_CONGESTED) {
3715 fadv_congested = TRUE;
3716 }
3717
3718 if (chain || error) {
3719 /*
3720 * If we sent down a chain then we are done since
3721 * the callee had taken care of everything; else
3722 * we need to free the rest of the chain ourselves.
3723 */
3724 if (!chain) {
3725 m_drop_list(npkt, NULL, DROPTAP_FLAG_DIR_OUT | DROPTAP_FLAG_L2_MISSING, DROP_REASON_TCP_PKT_UNSENT, NULL, 0);
3726 }
3727 break;
3728 }
3729 pkt = npkt;
3730 }
3731
3732 if (unlocked) {
3733 socket_lock(so, 0);
3734 }
3735
3736 /*
3737 * Enter flow controlled state if the connection is established
3738 * and is not in recovery. Flow control is allowed only if there
3739 * is outstanding data.
3740 *
3741 * A connection will enter suspended state even if it is in
3742 * recovery.
3743 */
3744 if (((adv->code == FADV_FLOW_CONTROLLED && !IN_FASTRECOVERY(tp)) ||
3745 adv->code == FADV_SUSPENDED) &&
3746 !(tp->t_flags & TF_CLOSING) &&
3747 tp->t_state == TCPS_ESTABLISHED &&
3748 SEQ_GT(tp->snd_max, tp->snd_una)) {
3749 int rc;
3750 rc = inp_set_fc_state(inp, adv->code);
3751
3752 if (rc == 1) {
3753 tcp_ccdbg_trace(tp, NULL,
3754 ((adv->code == FADV_FLOW_CONTROLLED) ?
3755 TCP_CC_FLOW_CONTROL : TCP_CC_SUSPEND));
3756 if (adv->code == FADV_FLOW_CONTROLLED) {
3757 TCP_LOG_OUTPUT(tp, "flow controlled");
3758 } else {
3759 TCP_LOG_OUTPUT(tp, "flow suspended");
3760 }
3761 }
3762 }
3763
3764 if (fadv_congested && !IN_FASTRECOVERY(tp) && !(tp->t_flags & TF_CLOSING) &&
3765 tp->t_state == TCPS_ESTABLISHED) {
3766 TCP_LOG_OUTPUT(tp, "flow congestion notified");
3767 tcp_local_congestion_notification(tp);
3768 tcp_ccdbg_trace(tp, NULL, TCP_CC_FLOW_CONGESTION_NOTIFIED);
3769 }
3770
3771 /*
3772 * When an interface queue gets suspended, some of the
3773 * packets are dropped. Return ENOBUFS, to update the
3774 * pcb state.
3775 */
3776 if (adv->code == FADV_SUSPENDED) {
3777 error = ENOBUFS;
3778 }
3779
3780 VERIFY(inp->inp_sndinprog_cnt > 0);
3781 if (--inp->inp_sndinprog_cnt == 0) {
3782 inp->inp_flags &= ~(INP_FC_FEEDBACK);
3783 if (inp->inp_sndingprog_waiters > 0) {
3784 wakeup(&inp->inp_sndinprog_cnt);
3785 }
3786 }
3787
3788 if (isipv6) {
3789 /*
3790 * When an NECP IP tunnel policy forces the outbound interface,
3791 * ip6_output_list() informs the transport layer what is the actual
3792 * outgoing interface
3793 */
3794 if (ip6oa.ip6oa_flags & IP6OAF_BOUND_IF) {
3795 ifnet_head_lock_shared();
3796 outif = ifindex2ifnet[ip6oa.ip6oa_boundif];
3797 ifnet_head_done();
3798 } else if (ro6.ro_rt != NULL) {
3799 outif = ro6.ro_rt->rt_ifp;
3800 }
3801 } else {
3802 if (ro.ro_rt != NULL) {
3803 outif = ro.ro_rt->rt_ifp;
3804 }
3805 }
3806 if (check_qos_marking_again) {
3807 uint32_t qos_marking_gencount;
3808 bool allow_qos_marking;
3809 if (isipv6) {
3810 qos_marking_gencount = ip6oa.qos_marking_gencount;
3811 allow_qos_marking = ip6oa.ip6oa_flags & IP6OAF_QOSMARKING_ALLOWED ? TRUE : FALSE;
3812 } else {
3813 qos_marking_gencount = ipoa.qos_marking_gencount;
3814 allow_qos_marking = ipoa.ipoa_flags & IPOAF_QOSMARKING_ALLOWED ? TRUE : FALSE;
3815 }
3816 inp->inp_policyresult.results.qos_marking_gencount = qos_marking_gencount;
3817 if (allow_qos_marking == TRUE) {
3818 inp->inp_socket->so_flags1 |= SOF1_QOSMARKING_ALLOWED;
3819 } else {
3820 inp->inp_socket->so_flags1 &= ~SOF1_QOSMARKING_ALLOWED;
3821 }
3822 }
3823
3824 if (outif != NULL && outif != inp->inp_last_outifp) {
3825 /* Update the send byte count */
3826 if (so->so_snd.sb_cc > 0 && so->so_snd.sb_flags & SB_SNDBYTE_CNT) {
3827 inp_decr_sndbytes_total(so, so->so_snd.sb_cc);
3828 inp_decr_sndbytes_allunsent(so, tp->snd_una);
3829 so->so_snd.sb_flags &= ~SB_SNDBYTE_CNT;
3830 }
3831 inp->inp_last_outifp = outif;
3832 #if SKYWALK
3833 if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
3834 netns_set_ifnet(&inp->inp_netns_token, inp->inp_last_outifp);
3835 }
3836 #endif /* SKYWALK */
3837 }
3838
3839 if (error != 0 && ifdenied &&
3840 (INP_NO_CELLULAR(inp) || INP_NO_EXPENSIVE(inp) || INP_NO_CONSTRAINED(inp))) {
3841 soevent(so,
3842 (SO_FILT_HINT_LOCKED | SO_FILT_HINT_IFDENIED));
3843 }
3844
3845 /* Synchronize cached PCB route & options */
3846 if (isipv6) {
3847 in6p_route_copyin(inp, &ro6);
3848 } else {
3849 inp_route_copyin(inp, &ro);
3850 }
3851
3852 if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift == 0 &&
3853 tp->t_inpcb->inp_route.ro_rt != NULL) {
3854 /* If we found the route and there is an rtt on it
3855 * reset the retransmit timer
3856 */
3857 tcp_getrt_rtt(tp, tp->t_inpcb->in6p_route.ro_rt);
3858 tcp_set_rto(tp);
3859 }
3860 return error;
3861 #undef ro
3862 #undef ro6
3863 #undef ipoa
3864 #undef ip6oa
3865 }
3866
3867 int tcptv_persmin_val = TCPTV_PERSMIN;
3868
3869 void
tcp_setpersist(struct tcpcb * tp)3870 tcp_setpersist(struct tcpcb *tp)
3871 {
3872 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
3873
3874 /* If a PERSIST_TIMER option was set we will limit the
3875 * time the persist timer will be active for that connection
3876 * in order to avoid DOS by using zero window probes.
3877 * see rdar://5805356
3878 */
3879
3880 if (tp->t_persist_timeout != 0 &&
3881 tp->t_timer[TCPT_PERSIST] == 0 &&
3882 tp->t_persist_stop == 0) {
3883 tp->t_persist_stop = tcp_now + tp->t_persist_timeout;
3884 }
3885
3886 /*
3887 * Start/restart persistance timer.
3888 */
3889 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
3890 t * tcp_backoff[tp->t_rxtshift],
3891 tcptv_persmin_val, TCPTV_PERSMAX, 0);
3892 tp->t_timer[TCPT_PERSIST] = tcp_offset_from_start(tp, tp->t_timer[TCPT_PERSIST]);
3893
3894 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) {
3895 tp->t_rxtshift++;
3896 }
3897 }
3898
3899 static int
tcp_recv_throttle(struct tcpcb * tp)3900 tcp_recv_throttle(struct tcpcb *tp)
3901 {
3902 uint32_t base_rtt, newsize;
3903 struct sockbuf *__single sbrcv = &tp->t_inpcb->inp_socket->so_rcv;
3904
3905 if (tcp_use_rtt_recvbg == 1 &&
3906 TSTMP_SUPPORTED(tp)) {
3907 /*
3908 * Timestamps are supported on this connection. Use
3909 * RTT to look for an increase in latency.
3910 */
3911
3912 /*
3913 * If the connection is already being throttled, leave it
3914 * in that state until rtt comes closer to base rtt
3915 */
3916 if (tp->t_flagsext & TF_RECV_THROTTLE) {
3917 return 1;
3918 }
3919
3920 base_rtt = get_base_rtt(tp);
3921
3922 if (base_rtt != 0 && tp->t_rttcur != 0) {
3923 /*
3924 * if latency increased on a background flow,
3925 * return 1 to start throttling.
3926 */
3927 if (tp->t_rttcur > (base_rtt + target_qdelay)) {
3928 tp->t_flagsext |= TF_RECV_THROTTLE;
3929 if (tp->t_recv_throttle_ts == 0) {
3930 tp->t_recv_throttle_ts = tcp_now;
3931 }
3932 /*
3933 * Reduce the recv socket buffer size to
3934 * minimize latecy.
3935 */
3936 if (sbrcv->sb_idealsize >
3937 tcp_recv_throttle_minwin) {
3938 newsize = sbrcv->sb_idealsize >> 1;
3939 /* Set a minimum of 16 K */
3940 newsize =
3941 max(newsize,
3942 tcp_recv_throttle_minwin);
3943 sbrcv->sb_idealsize = newsize;
3944 }
3945 return 1;
3946 } else {
3947 return 0;
3948 }
3949 }
3950 }
3951
3952 /*
3953 * Timestamps are not supported or there is no good RTT
3954 * measurement. Use IPDV in this case.
3955 */
3956 if (tp->acc_iaj > tcp_acc_iaj_react_limit) {
3957 return 1;
3958 }
3959
3960 return 0;
3961 }
3962