]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_output.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / netinet / tcp_output.c
1 /*
2 * Copyright (c) 2000-2020 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
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/sysctl.h>
77 #include <sys/mbuf.h>
78 #include <sys/domain.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82
83 #include <net/route.h>
84 #include <net/ntstat.h>
85 #include <net/if_var.h>
86 #include <net/if.h>
87 #include <net/if_types.h>
88 #include <net/dlil.h>
89
90 #include <netinet/in.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/in_var.h>
93 #include <netinet/in_tclass.h>
94 #include <netinet/ip.h>
95 #include <netinet/in_pcb.h>
96 #include <netinet/ip_var.h>
97 #include <mach/sdt.h>
98 #include <netinet6/in6_pcb.h>
99 #include <netinet/ip6.h>
100 #include <netinet6/ip6_var.h>
101 #include <netinet/tcp.h>
102 #define TCPOUTFLAGS
103 #include <netinet/tcp_cache.h>
104 #include <netinet/tcp_fsm.h>
105 #include <netinet/tcp_seq.h>
106 #include <netinet/tcp_timer.h>
107 #include <netinet/tcp_var.h>
108 #include <netinet/tcpip.h>
109 #include <netinet/tcp_cc.h>
110 #if TCPDEBUG
111 #include <netinet/tcp_debug.h>
112 #endif
113 #include <netinet/tcp_log.h>
114 #include <sys/kdebug.h>
115 #include <mach/sdt.h>
116
117 #if IPSEC
118 #include <netinet6/ipsec.h>
119 #endif /*IPSEC*/
120
121 #if MPTCP
122 #include <netinet/mptcp_var.h>
123 #include <netinet/mptcp.h>
124 #include <netinet/mptcp_opt.h>
125 #include <netinet/mptcp_seq.h>
126 #endif
127
128 #include <corecrypto/ccaes.h>
129
130 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 1)
131 #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 3)
132 #define DBG_FNC_TCP_OUTPUT NETDBG_CODE(DBG_NETTCP, (4 << 8) | 1)
133
134 SYSCTL_SKMEM_TCP_INT(OID_AUTO, path_mtu_discovery,
135 CTLFLAG_RW | CTLFLAG_LOCKED, int, path_mtu_discovery, 1,
136 "Enable Path MTU Discovery");
137
138 SYSCTL_SKMEM_TCP_INT(OID_AUTO, local_slowstart_flightsize,
139 CTLFLAG_RW | CTLFLAG_LOCKED, int, ss_fltsz_local, 8,
140 "Slow start flight size for local networks");
141
142 int tcp_do_tso = 1;
143 SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_RW | CTLFLAG_LOCKED,
144 &tcp_do_tso, 0, "Enable TCP Segmentation Offload");
145
146 SYSCTL_SKMEM_TCP_INT(OID_AUTO, ecn_setup_percentage,
147 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_ecn_setup_percentage, 100,
148 "Max ECN setup percentage");
149
150 SYSCTL_SKMEM_TCP_INT(OID_AUTO, do_ack_compression,
151 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_do_ack_compression, 1,
152 "Enable TCP ACK compression (on (cell only): 1, off: 0, on (all interfaces): 2)");
153
154 SYSCTL_SKMEM_TCP_INT(OID_AUTO, ack_compression_rate,
155 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_ack_compression_rate, TCP_COMP_CHANGE_RATE,
156 "Rate at which we force sending new ACKs (in ms)");
157
158 SYSCTL_SKMEM_TCP_INT(OID_AUTO, randomize_timestamps,
159 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_randomize_timestamps, 1,
160 "Randomize TCP timestamps to prevent tracking (on: 1, off: 0)");
161
162 static int
163 sysctl_change_ecn_setting SYSCTL_HANDLER_ARGS
164 {
165 #pragma unused(oidp, arg1, arg2)
166 int i, err = 0, changed = 0;
167 struct ifnet *ifp;
168
169 err = sysctl_io_number(req, tcp_ecn_outbound, sizeof(int32_t),
170 &i, &changed);
171 if (err != 0 || req->newptr == USER_ADDR_NULL) {
172 return err;
173 }
174
175 if (changed) {
176 if ((tcp_ecn_outbound == 0 || tcp_ecn_outbound == 1) &&
177 (i == 0 || i == 1)) {
178 tcp_ecn_outbound = i;
179 SYSCTL_SKMEM_UPDATE_FIELD(tcp.ecn_initiate_out, tcp_ecn_outbound);
180 return err;
181 }
182 if (tcp_ecn_outbound == 2 && (i == 0 || i == 1)) {
183 /*
184 * Reset ECN enable flags on non-cellular
185 * interfaces so that the system default will take
186 * over
187 */
188 ifnet_head_lock_shared();
189 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
190 if (!IFNET_IS_CELLULAR(ifp)) {
191 if_clear_eflags(ifp,
192 IFEF_ECN_ENABLE |
193 IFEF_ECN_DISABLE);
194 }
195 }
196 ifnet_head_done();
197 } else {
198 /*
199 * Set ECN enable flags on non-cellular
200 * interfaces
201 */
202 ifnet_head_lock_shared();
203 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
204 if (!IFNET_IS_CELLULAR(ifp)) {
205 if_set_eflags(ifp, IFEF_ECN_ENABLE);
206 if_clear_eflags(ifp, IFEF_ECN_DISABLE);
207 }
208 }
209 ifnet_head_done();
210 }
211 tcp_ecn_outbound = i;
212 SYSCTL_SKMEM_UPDATE_FIELD(tcp.ecn_initiate_out, tcp_ecn_outbound);
213 }
214 /* Change the other one too as the work is done */
215 if (i == 2 || tcp_ecn_inbound == 2) {
216 tcp_ecn_inbound = i;
217 SYSCTL_SKMEM_UPDATE_FIELD(tcp.ecn_negotiate_in, tcp_ecn_inbound);
218 }
219 return err;
220 }
221
222 int tcp_ecn_outbound = 2;
223 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, ecn_initiate_out,
224 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_ecn_outbound, 0,
225 sysctl_change_ecn_setting, "IU",
226 "Initiate ECN for outbound connections");
227
228 int tcp_ecn_inbound = 2;
229 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, ecn_negotiate_in,
230 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_ecn_inbound, 0,
231 sysctl_change_ecn_setting, "IU",
232 "Initiate ECN for inbound connections");
233
234 SYSCTL_SKMEM_TCP_INT(OID_AUTO, packetchain,
235 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_packet_chaining, 50,
236 "Enable TCP output packet chaining");
237
238 SYSCTL_SKMEM_TCP_INT(OID_AUTO, socket_unlocked_on_output,
239 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_output_unlocked, 1,
240 "Unlock TCP when sending packets down to IP");
241
242 SYSCTL_SKMEM_TCP_INT(OID_AUTO, min_iaj_win,
243 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_min_iaj_win, MIN_IAJ_WIN,
244 "Minimum recv win based on inter-packet arrival jitter");
245
246 SYSCTL_SKMEM_TCP_INT(OID_AUTO, acc_iaj_react_limit,
247 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_acc_iaj_react_limit,
248 ACC_IAJ_REACT_LIMIT, "Accumulated IAJ when receiver starts to react");
249
250 SYSCTL_SKMEM_TCP_INT(OID_AUTO, autosndbufinc,
251 CTLFLAG_RW | CTLFLAG_LOCKED, uint32_t, tcp_autosndbuf_inc,
252 8 * 1024, "Increment in send socket bufffer size");
253
254 SYSCTL_SKMEM_TCP_INT(OID_AUTO, autosndbufmax,
255 CTLFLAG_RW | CTLFLAG_LOCKED, uint32_t, tcp_autosndbuf_max, 2 * 1024 * 1024,
256 "Maximum send socket buffer size");
257
258 SYSCTL_SKMEM_TCP_INT(OID_AUTO, rtt_recvbg,
259 CTLFLAG_RW | CTLFLAG_LOCKED, uint32_t, tcp_use_rtt_recvbg, 1,
260 "Use RTT for bg recv algorithm");
261
262 SYSCTL_SKMEM_TCP_INT(OID_AUTO, recv_throttle_minwin,
263 CTLFLAG_RW | CTLFLAG_LOCKED, uint32_t, tcp_recv_throttle_minwin, 16 * 1024,
264 "Minimum recv win for throttling");
265
266 SYSCTL_SKMEM_TCP_INT(OID_AUTO, enable_tlp,
267 CTLFLAG_RW | CTLFLAG_LOCKED,
268 int32_t, tcp_enable_tlp, 1, "Enable Tail loss probe");
269
270 static int32_t packchain_newlist = 0;
271 static int32_t packchain_looped = 0;
272 static int32_t packchain_sent = 0;
273
274 /* temporary: for testing */
275 #if IPSEC
276 extern int ipsec_bypass;
277 #endif
278
279 extern int slowlink_wsize; /* window correction for slow links */
280
281 extern u_int32_t dlil_filter_disable_tso_count;
282 extern u_int32_t kipf_count;
283
284 static int tcp_ip_output(struct socket *, struct tcpcb *, struct mbuf *,
285 int, struct mbuf *, int, int, boolean_t);
286 static int tcp_recv_throttle(struct tcpcb *tp);
287
288 static int32_t
289 tcp_tfo_check(struct tcpcb *tp, int32_t len)
290 {
291 struct socket *so = tp->t_inpcb->inp_socket;
292 unsigned int optlen = 0;
293 unsigned int cookie_len;
294
295 if (tp->t_flags & TF_NOOPT) {
296 goto fallback;
297 }
298
299 if (!(tp->t_flagsext & TF_FASTOPEN_FORCE_ENABLE) &&
300 !tcp_heuristic_do_tfo(tp)) {
301 tp->t_tfo_stats |= TFO_S_HEURISTICS_DISABLE;
302 tcpstat.tcps_tfo_heuristics_disable++;
303 goto fallback;
304 }
305
306 if (so->so_flags1 & SOF1_DATA_AUTHENTICATED) {
307 return len;
308 }
309
310 optlen += TCPOLEN_MAXSEG;
311
312 if (tp->t_flags & TF_REQ_SCALE) {
313 optlen += 4;
314 }
315
316 #if MPTCP
317 if ((so->so_flags & SOF_MP_SUBFLOW) && mptcp_enable &&
318 (tp->t_rxtshift <= mptcp_mpcap_retries ||
319 (tptomptp(tp)->mpt_mpte->mpte_flags & MPTE_FORCE_ENABLE))) {
320 optlen += sizeof(struct mptcp_mpcapable_opt_common) + sizeof(mptcp_key_t);
321 }
322 #endif /* MPTCP */
323
324 if (tp->t_flags & TF_REQ_TSTMP) {
325 optlen += TCPOLEN_TSTAMP_APPA;
326 }
327
328 if (SACK_ENABLED(tp)) {
329 optlen += TCPOLEN_SACK_PERMITTED;
330 }
331
332 /* Now, decide whether to use TFO or not */
333
334 /* Don't even bother trying if there is no space at all... */
335 if (MAX_TCPOPTLEN - optlen < TCPOLEN_FASTOPEN_REQ) {
336 goto fallback;
337 }
338
339 cookie_len = tcp_cache_get_cookie_len(tp);
340 if (cookie_len == 0) {
341 /* No cookie, so we request one */
342 return 0;
343 }
344
345 /* There is not enough space for the cookie, so we cannot do TFO */
346 if (MAX_TCPOPTLEN - optlen < cookie_len) {
347 goto fallback;
348 }
349
350 /* Do not send SYN+data if there is more in the queue than MSS */
351 if (so->so_snd.sb_cc > (tp->t_maxopd - MAX_TCPOPTLEN)) {
352 goto fallback;
353 }
354
355 /* Ok, everything looks good. We can go on and do TFO */
356 return len;
357
358 fallback:
359 tcp_disable_tfo(tp);
360 return 0;
361 }
362
363 /* Returns the number of bytes written to the TCP option-space */
364 static unsigned int
365 tcp_tfo_write_cookie_rep(struct tcpcb *tp, unsigned int optlen, u_char *opt)
366 {
367 u_char out[CCAES_BLOCK_SIZE];
368 unsigned ret = 0;
369 u_char *bp;
370
371 if ((MAX_TCPOPTLEN - optlen) <
372 (TCPOLEN_FASTOPEN_REQ + TFO_COOKIE_LEN_DEFAULT)) {
373 return ret;
374 }
375
376 tcp_tfo_gen_cookie(tp->t_inpcb, out, sizeof(out));
377
378 bp = opt + optlen;
379
380 *bp++ = TCPOPT_FASTOPEN;
381 *bp++ = 2 + TFO_COOKIE_LEN_DEFAULT;
382 memcpy(bp, out, TFO_COOKIE_LEN_DEFAULT);
383 ret += 2 + TFO_COOKIE_LEN_DEFAULT;
384
385 tp->t_tfo_stats |= TFO_S_COOKIE_SENT;
386 tcpstat.tcps_tfo_cookie_sent++;
387
388 return ret;
389 }
390
391 static unsigned int
392 tcp_tfo_write_cookie(struct tcpcb *tp, unsigned int optlen, int32_t len,
393 u_char *opt)
394 {
395 uint8_t tfo_len;
396 struct socket *so = tp->t_inpcb->inp_socket;
397 unsigned ret = 0;
398 int res;
399 u_char *bp;
400
401 if (TCPOLEN_FASTOPEN_REQ > MAX_TCPOPTLEN - optlen) {
402 return 0;
403 }
404 tfo_len = (uint8_t)(MAX_TCPOPTLEN - optlen - TCPOLEN_FASTOPEN_REQ);
405
406 if (so->so_flags1 & SOF1_DATA_AUTHENTICATED) {
407 /* If there is some data, let's track it */
408 if (len > 0) {
409 tp->t_tfo_stats |= TFO_S_SYN_DATA_SENT;
410 tcpstat.tcps_tfo_syn_data_sent++;
411 }
412
413 return 0;
414 }
415
416 bp = opt + optlen;
417
418 /*
419 * The cookie will be copied in the appropriate place within the
420 * TCP-option space. That way we avoid the need for an intermediate
421 * variable.
422 */
423 res = tcp_cache_get_cookie(tp, bp + TCPOLEN_FASTOPEN_REQ, &tfo_len);
424 if (res == 0) {
425 *bp++ = TCPOPT_FASTOPEN;
426 *bp++ = TCPOLEN_FASTOPEN_REQ;
427 ret += TCPOLEN_FASTOPEN_REQ;
428
429 tp->t_tfo_flags |= TFO_F_COOKIE_REQ;
430
431 tp->t_tfo_stats |= TFO_S_COOKIE_REQ;
432 tcpstat.tcps_tfo_cookie_req++;
433 } else {
434 *bp++ = TCPOPT_FASTOPEN;
435 *bp++ = TCPOLEN_FASTOPEN_REQ + tfo_len;
436
437 ret += TCPOLEN_FASTOPEN_REQ + tfo_len;
438
439 tp->t_tfo_flags |= TFO_F_COOKIE_SENT;
440
441 /* If there is some data, let's track it */
442 if (len > 0) {
443 tp->t_tfo_stats |= TFO_S_SYN_DATA_SENT;
444 tcpstat.tcps_tfo_syn_data_sent++;
445 }
446 }
447
448 return ret;
449 }
450
451 static inline bool
452 tcp_send_ecn_flags_on_syn(struct tcpcb *tp)
453 {
454 return !(tp->ecn_flags & TE_SETUPSENT);
455 }
456
457 void
458 tcp_set_ecn(struct tcpcb *tp, struct ifnet *ifp)
459 {
460 boolean_t inbound;
461
462 /*
463 * Socket option has precedence
464 */
465 if (tp->ecn_flags & TE_ECN_MODE_ENABLE) {
466 tp->ecn_flags |= TE_ENABLE_ECN;
467 goto check_heuristic;
468 }
469
470 if (tp->ecn_flags & TE_ECN_MODE_DISABLE) {
471 tp->ecn_flags &= ~TE_ENABLE_ECN;
472 return;
473 }
474 /*
475 * Per interface setting comes next
476 */
477 if (ifp != NULL) {
478 if (ifp->if_eflags & IFEF_ECN_ENABLE) {
479 tp->ecn_flags |= TE_ENABLE_ECN;
480 goto check_heuristic;
481 }
482
483 if (ifp->if_eflags & IFEF_ECN_DISABLE) {
484 tp->ecn_flags &= ~TE_ENABLE_ECN;
485 return;
486 }
487 }
488 /*
489 * System wide settings come last
490 */
491 inbound = (tp->t_inpcb->inp_socket->so_head != NULL);
492 if ((inbound && tcp_ecn_inbound == 1) ||
493 (!inbound && tcp_ecn_outbound == 1)) {
494 tp->ecn_flags |= TE_ENABLE_ECN;
495 goto check_heuristic;
496 } else {
497 tp->ecn_flags &= ~TE_ENABLE_ECN;
498 }
499
500 return;
501
502 check_heuristic:
503 if (!tcp_heuristic_do_ecn(tp)) {
504 tp->ecn_flags &= ~TE_ENABLE_ECN;
505 }
506
507 /*
508 * If the interface setting, system-level setting and heuristics
509 * allow to enable ECN, randomly select 5% of connections to
510 * enable it
511 */
512 if ((tp->ecn_flags & (TE_ECN_MODE_ENABLE | TE_ECN_MODE_DISABLE
513 | TE_ENABLE_ECN)) == TE_ENABLE_ECN) {
514 /*
515 * Use the random value in iss for randomizing
516 * this selection
517 */
518 if ((tp->iss % 100) >= tcp_ecn_setup_percentage) {
519 tp->ecn_flags &= ~TE_ENABLE_ECN;
520 }
521 }
522 }
523
524 int
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 (ret < 0) {
538 /*
539 * This happens when the RTO-timer fires because snd_nxt gets artificially
540 * decreased. If we then receive some SACK-blogs, sack_bytes_acked is
541 * going to be high.
542 */
543 ret = 0;
544 }
545
546 return ret;
547 }
548
549 /*
550 * Tcp output routine: figure out what should be sent and send it.
551 *
552 * Returns: 0 Success
553 * EADDRNOTAVAIL
554 * ENOBUFS
555 * EMSGSIZE
556 * EHOSTUNREACH
557 * ENETDOWN
558 * ip_output_list:ENOMEM
559 * ip_output_list:EADDRNOTAVAIL
560 * ip_output_list:ENETUNREACH
561 * ip_output_list:EHOSTUNREACH
562 * ip_output_list:EACCES
563 * ip_output_list:EMSGSIZE
564 * ip_output_list:ENOBUFS
565 * ip_output_list:??? [ignorable: mostly IPSEC/firewall/DLIL]
566 * ip6_output_list:EINVAL
567 * ip6_output_list:EOPNOTSUPP
568 * ip6_output_list:EHOSTUNREACH
569 * ip6_output_list:EADDRNOTAVAIL
570 * ip6_output_list:ENETUNREACH
571 * ip6_output_list:EMSGSIZE
572 * ip6_output_list:ENOBUFS
573 * ip6_output_list:??? [ignorable: mostly IPSEC/firewall/DLIL]
574 */
575 int
576 tcp_output(struct tcpcb *tp)
577 {
578 struct inpcb *inp = tp->t_inpcb;
579 struct socket *so = inp->inp_socket;
580 int32_t len, recwin, sendwin, off;
581 uint8_t flags;
582 int error;
583 struct mbuf *m;
584 struct ip *ip = NULL;
585 struct ip6_hdr *ip6 = NULL;
586 struct tcphdr *th;
587 u_char opt[TCP_MAXOLEN];
588 unsigned int ipoptlen, optlen, hdrlen;
589 int idle, sendalot, lost = 0;
590 int i, sack_rxmit;
591 int tso = 0;
592 int sack_bytes_rxmt;
593 tcp_seq old_snd_nxt = 0;
594 struct sackhole *p;
595 #if IPSEC
596 unsigned int ipsec_optlen = 0;
597 #endif /* IPSEC */
598 int idle_time = 0;
599 struct mbuf *packetlist = NULL;
600 struct mbuf *tp_inp_options = inp->inp_depend4.inp4_options;
601 int isipv6 = inp->inp_vflag & INP_IPV6;
602 int packchain_listadd = 0;
603 int so_options = so->so_options;
604 struct rtentry *rt;
605 u_int32_t svc_flags = 0, allocated_len;
606 unsigned int sackoptlen = 0;
607 #if MPTCP
608 boolean_t mptcp_acknow;
609 #endif /* MPTCP */
610 boolean_t cell = FALSE;
611 boolean_t wifi = FALSE;
612 boolean_t wired = FALSE;
613 boolean_t sack_rescue_rxt = FALSE;
614 int sotc = so->so_traffic_class;
615 boolean_t do_not_compress = FALSE;
616 boolean_t sack_rxmted = FALSE;
617
618 /*
619 * Determine length of data that should be transmitted,
620 * and flags that will be used.
621 * If there is some data or critical controls (SYN, RST)
622 * to send, then transmit; otherwise, investigate further.
623 */
624 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
625
626 /* Since idle_time is signed integer, the following integer subtraction
627 * will take care of wrap around of tcp_now
628 */
629 idle_time = tcp_now - tp->t_rcvtime;
630 if (idle && idle_time >= TCP_IDLETIMEOUT(tp)) {
631 if (CC_ALGO(tp)->after_idle != NULL &&
632 (tp->tcp_cc_index != TCP_CC_ALGO_CUBIC_INDEX ||
633 idle_time >= TCP_CC_CWND_NONVALIDATED_PERIOD)) {
634 CC_ALGO(tp)->after_idle(tp);
635 tcp_ccdbg_trace(tp, NULL, TCP_CC_IDLE_TIMEOUT);
636 }
637
638 /*
639 * Do some other tasks that need to be done after
640 * idle time
641 */
642 if (!SLIST_EMPTY(&tp->t_rxt_segments)) {
643 tcp_rxtseg_clean(tp);
644 }
645
646 /* If stretch ack was auto-disabled, re-evaluate it */
647 tcp_cc_after_idle_stretchack(tp);
648 tp->t_forced_acks = TCP_FORCED_ACKS_COUNT;
649 }
650 tp->t_flags &= ~TF_LASTIDLE;
651 if (idle) {
652 if (tp->t_flags & TF_MORETOCOME) {
653 tp->t_flags |= TF_LASTIDLE;
654 idle = 0;
655 }
656 }
657 #if MPTCP
658 if (tp->t_mpflags & TMPF_RESET) {
659 tcp_check_timer_state(tp);
660 /*
661 * Once a RST has been sent for an MPTCP subflow,
662 * the subflow socket stays around until deleted.
663 * No packets such as FINs must be sent after RST.
664 */
665 return 0;
666 }
667 #endif /* MPTCP */
668
669 again:
670 #if MPTCP
671 mptcp_acknow = FALSE;
672 #endif
673 do_not_compress = FALSE;
674
675 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_START, 0, 0, 0, 0, 0);
676
677 if (isipv6) {
678 KERNEL_DEBUG(DBG_LAYER_BEG,
679 ((inp->inp_fport << 16) | inp->inp_lport),
680 (((inp->in6p_laddr.s6_addr16[0] & 0xffff) << 16) |
681 (inp->in6p_faddr.s6_addr16[0] & 0xffff)),
682 sendalot, 0, 0);
683 } else {
684 KERNEL_DEBUG(DBG_LAYER_BEG,
685 ((inp->inp_fport << 16) | inp->inp_lport),
686 (((inp->inp_laddr.s_addr & 0xffff) << 16) |
687 (inp->inp_faddr.s_addr & 0xffff)),
688 sendalot, 0, 0);
689 }
690 /*
691 * If the route generation id changed, we need to check that our
692 * local (source) IP address is still valid. If it isn't either
693 * return error or silently do nothing (assuming the address will
694 * come back before the TCP connection times out).
695 */
696 rt = inp->inp_route.ro_rt;
697 if (rt != NULL && ROUTE_UNUSABLE(&tp->t_inpcb->inp_route)) {
698 struct ifnet *ifp;
699 struct in_ifaddr *ia = NULL;
700 struct in6_ifaddr *ia6 = NULL;
701 int found_srcaddr = 0;
702
703 /* disable multipages at the socket */
704 somultipages(so, FALSE);
705
706 /* Disable TSO for the socket until we know more */
707 tp->t_flags &= ~TF_TSO;
708
709 soif2kcl(so, FALSE);
710
711 if (isipv6) {
712 ia6 = ifa_foraddr6(&inp->in6p_laddr);
713 if (ia6 != NULL) {
714 found_srcaddr = 1;
715 }
716 } else {
717 ia = ifa_foraddr(inp->inp_laddr.s_addr);
718 if (ia != NULL) {
719 found_srcaddr = 1;
720 }
721 }
722
723 /* check that the source address is still valid */
724 if (found_srcaddr == 0) {
725 soevent(so,
726 (SO_FILT_HINT_LOCKED | SO_FILT_HINT_NOSRCADDR));
727
728 if (tp->t_state >= TCPS_CLOSE_WAIT) {
729 tcp_drop(tp, EADDRNOTAVAIL);
730 return EADDRNOTAVAIL;
731 }
732
733 /*
734 * Set retransmit timer if it wasn't set,
735 * reset Persist timer and shift register as the
736 * advertised peer window may not be valid anymore
737 */
738 if (tp->t_timer[TCPT_REXMT] == 0) {
739 tp->t_timer[TCPT_REXMT] =
740 OFFSET_FROM_START(tp, tp->t_rxtcur);
741 if (tp->t_timer[TCPT_PERSIST] != 0) {
742 tp->t_timer[TCPT_PERSIST] = 0;
743 tp->t_persist_stop = 0;
744 TCP_RESET_REXMT_STATE(tp);
745 }
746 }
747
748 if (tp->t_pktlist_head != NULL) {
749 m_freem_list(tp->t_pktlist_head);
750 }
751 TCP_PKTLIST_CLEAR(tp);
752
753 /* drop connection if source address isn't available */
754 if (so->so_flags & SOF_NOADDRAVAIL) {
755 tcp_drop(tp, EADDRNOTAVAIL);
756 return EADDRNOTAVAIL;
757 } else {
758 tcp_check_timer_state(tp);
759 return 0; /* silently ignore, keep data in socket: address may be back */
760 }
761 }
762 if (ia != NULL) {
763 IFA_REMREF(&ia->ia_ifa);
764 }
765
766 if (ia6 != NULL) {
767 IFA_REMREF(&ia6->ia_ifa);
768 }
769
770 /*
771 * Address is still valid; check for multipages capability
772 * again in case the outgoing interface has changed.
773 */
774 RT_LOCK(rt);
775 if ((ifp = rt->rt_ifp) != NULL) {
776 somultipages(so, (ifp->if_hwassist & IFNET_MULTIPAGES));
777 tcp_set_tso(tp, ifp);
778 soif2kcl(so, (ifp->if_eflags & IFEF_2KCL));
779 tcp_set_ecn(tp, ifp);
780 }
781 if (rt->rt_flags & RTF_UP) {
782 RT_GENID_SYNC(rt);
783 }
784 /*
785 * See if we should do MTU discovery. Don't do it if:
786 * 1) it is disabled via the sysctl
787 * 2) the route isn't up
788 * 3) the MTU is locked (if it is, then discovery
789 * has been disabled)
790 */
791
792 if (!path_mtu_discovery || ((rt != NULL) &&
793 (!(rt->rt_flags & RTF_UP) ||
794 (rt->rt_rmx.rmx_locks & RTV_MTU)))) {
795 tp->t_flags &= ~TF_PMTUD;
796 } else {
797 tp->t_flags |= TF_PMTUD;
798 }
799
800 RT_UNLOCK(rt);
801 }
802
803 if (rt != NULL) {
804 cell = IFNET_IS_CELLULAR(rt->rt_ifp);
805 wifi = (!cell && IFNET_IS_WIFI(rt->rt_ifp));
806 wired = (!wifi && IFNET_IS_WIRED(rt->rt_ifp));
807 }
808
809 /*
810 * If we've recently taken a timeout, snd_max will be greater than
811 * snd_nxt. There may be SACK information that allows us to avoid
812 * resending already delivered data. Adjust snd_nxt accordingly.
813 */
814 if (SACK_ENABLED(tp) && SEQ_LT(tp->snd_nxt, tp->snd_max)) {
815 tcp_sack_adjust(tp);
816 }
817 sendalot = 0;
818 off = tp->snd_nxt - tp->snd_una;
819 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
820
821 if (tp->t_flags & TF_SLOWLINK && slowlink_wsize > 0) {
822 sendwin = min(sendwin, slowlink_wsize);
823 }
824
825 flags = tcp_outflags[tp->t_state];
826 /*
827 * Send any SACK-generated retransmissions. If we're explicitly
828 * trying to send out new data (when sendalot is 1), bypass this
829 * function. If we retransmit in fast recovery mode, decrement
830 * snd_cwnd, since we're replacing a (future) new transmission
831 * with a retransmission now, and we previously incremented
832 * snd_cwnd in tcp_input().
833 */
834 /*
835 * Still in sack recovery , reset rxmit flag to zero.
836 */
837 sack_rxmit = 0;
838 sack_bytes_rxmt = 0;
839 len = 0;
840 p = NULL;
841 if (SACK_ENABLED(tp) && IN_FASTRECOVERY(tp) &&
842 (p = tcp_sack_output(tp, &sack_bytes_rxmt))) {
843 int32_t cwin;
844
845 if (tcp_do_better_lr) {
846 cwin = min(tp->snd_wnd, tp->snd_cwnd) - tcp_flight_size(tp);
847 if (cwin <= 0 && sack_rxmted == FALSE) {
848 /* Allow to clock out at least on per period */
849 cwin = tp->t_maxseg;
850 }
851
852 sack_rxmted = TRUE;
853 } else {
854 cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
855 }
856 if (cwin < 0) {
857 cwin = 0;
858 }
859 /* Do not retransmit SACK segments beyond snd_recover */
860 if (SEQ_GT(p->end, tp->snd_recover)) {
861 /*
862 * (At least) part of sack hole extends beyond
863 * snd_recover. Check to see if we can rexmit data
864 * for this hole.
865 */
866 if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
867 /*
868 * Can't rexmit any more data for this hole.
869 * That data will be rexmitted in the next
870 * sack recovery episode, when snd_recover
871 * moves past p->rxmit.
872 */
873 p = NULL;
874 goto after_sack_rexmit;
875 } else {
876 /* Can rexmit part of the current hole */
877 len = ((int32_t)min(cwin,
878 tp->snd_recover - p->rxmit));
879 }
880 } else {
881 len = ((int32_t)min(cwin, p->end - p->rxmit));
882 }
883 if (len > 0) {
884 off = p->rxmit - tp->snd_una;
885 sack_rxmit = 1;
886 sendalot = 1;
887 /* Everything sent after snd_nxt will allow us to account for fast-retransmit of the retransmitted segment */
888 tp->send_highest_sack = tp->snd_nxt;
889 tp->t_new_dupacks = 0;
890 tcpstat.tcps_sack_rexmits++;
891 tcpstat.tcps_sack_rexmit_bytes +=
892 min(len, tp->t_maxseg);
893 } else {
894 len = 0;
895 }
896 }
897 after_sack_rexmit:
898 /*
899 * Get standard flags, and add SYN or FIN if requested by 'hidden'
900 * state flags.
901 */
902 if (tp->t_flags & TF_NEEDFIN) {
903 flags |= TH_FIN;
904 }
905
906 /*
907 * If in persist timeout with window of 0, send 1 byte.
908 * Otherwise, if window is small but nonzero
909 * and timer expired, we will send what we can
910 * and go to transmit state.
911 */
912 if (tp->t_flagsext & TF_FORCE) {
913 if (sendwin == 0) {
914 /*
915 * If we still have some data to send, then
916 * clear the FIN bit. Usually this would
917 * happen below when it realizes that we
918 * aren't sending all the data. However,
919 * if we have exactly 1 byte of unsent data,
920 * then it won't clear the FIN bit below,
921 * and if we are in persist state, we wind
922 * up sending the packet without recording
923 * that we sent the FIN bit.
924 *
925 * We can't just blindly clear the FIN bit,
926 * because if we don't have any more data
927 * to send then the probe will be the FIN
928 * itself.
929 */
930 if (off < so->so_snd.sb_cc) {
931 flags &= ~TH_FIN;
932 }
933 sendwin = 1;
934 } else {
935 tp->t_timer[TCPT_PERSIST] = 0;
936 tp->t_persist_stop = 0;
937 TCP_RESET_REXMT_STATE(tp);
938 }
939 }
940
941 /*
942 * If snd_nxt == snd_max and we have transmitted a FIN, the
943 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
944 * a negative length. This can also occur when TCP opens up
945 * its congestion window while receiving additional duplicate
946 * acks after fast-retransmit because TCP will reset snd_nxt
947 * to snd_max after the fast-retransmit.
948 *
949 * In the normal retransmit-FIN-only case, however, snd_nxt will
950 * be set to snd_una, the offset will be 0, and the length may
951 * wind up 0.
952 *
953 * If sack_rxmit is true we are retransmitting from the scoreboard
954 * in which case len is already set.
955 */
956 if (sack_rxmit == 0) {
957 if (sack_bytes_rxmt == 0) {
958 len = min(so->so_snd.sb_cc, sendwin) - off;
959 } else {
960 int32_t cwin;
961
962 if (tcp_do_better_lr) {
963 cwin = tp->snd_cwnd - tcp_flight_size(tp);
964 } else {
965 cwin = tp->snd_cwnd -
966 (tp->snd_nxt - tp->sack_newdata) -
967 sack_bytes_rxmt;
968 }
969 if (cwin < 0) {
970 cwin = 0;
971 }
972 /*
973 * We are inside of a SACK recovery episode and are
974 * sending new data, having retransmitted all the
975 * data possible in the scoreboard.
976 */
977 len = min(so->so_snd.sb_cc, tp->snd_wnd) - off;
978 /*
979 * Don't remove this (len > 0) check !
980 * We explicitly check for len > 0 here (although it
981 * isn't really necessary), to work around a gcc
982 * optimization issue - to force gcc to compute
983 * len above. Without this check, the computation
984 * of len is bungled by the optimizer.
985 */
986 if (len > 0) {
987 len = imin(len, cwin);
988 } else {
989 len = 0;
990 }
991 /*
992 * At this point SACK recovery can not send any
993 * data from scoreboard or any new data. Check
994 * if we can do a rescue retransmit towards the
995 * tail end of recovery window.
996 */
997 if (len == 0 && cwin > 0 &&
998 SEQ_LT(tp->snd_fack, tp->snd_recover) &&
999 !(tp->t_flagsext & TF_RESCUE_RXT)) {
1000 len = min((tp->snd_recover - tp->snd_fack),
1001 tp->t_maxseg);
1002 len = imin(len, cwin);
1003 old_snd_nxt = tp->snd_nxt;
1004 sack_rescue_rxt = TRUE;
1005 tp->snd_nxt = tp->snd_recover - len;
1006 /*
1007 * If FIN has been sent, snd_max
1008 * must have been advanced to cover it.
1009 */
1010 if ((tp->t_flags & TF_SENTFIN) &&
1011 tp->snd_max == tp->snd_recover) {
1012 tp->snd_nxt--;
1013 }
1014
1015 off = tp->snd_nxt - tp->snd_una;
1016 sendalot = 0;
1017 tp->t_flagsext |= TF_RESCUE_RXT;
1018 }
1019 }
1020 }
1021
1022 /*
1023 * Lop off SYN bit if it has already been sent. However, if this
1024 * is SYN-SENT state and if segment contains data and if we don't
1025 * know that foreign host supports TAO, suppress sending segment.
1026 */
1027 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
1028 if (tp->t_state == TCPS_SYN_RECEIVED && tfo_enabled(tp) && tp->snd_nxt == tp->snd_una + 1) {
1029 /* We are sending the SYN again! */
1030 off--;
1031 len++;
1032 } else {
1033 if (tp->t_state != TCPS_SYN_RECEIVED || tfo_enabled(tp)) {
1034 flags &= ~TH_SYN;
1035 }
1036
1037 off--;
1038 len++;
1039 if (len > 0 && tp->t_state == TCPS_SYN_SENT) {
1040 while (inp->inp_sndinprog_cnt == 0 &&
1041 tp->t_pktlist_head != NULL) {
1042 packetlist = tp->t_pktlist_head;
1043 packchain_listadd = tp->t_lastchain;
1044 packchain_sent++;
1045 TCP_PKTLIST_CLEAR(tp);
1046
1047 error = tcp_ip_output(so, tp, packetlist,
1048 packchain_listadd, tp_inp_options,
1049 (so_options & SO_DONTROUTE),
1050 (sack_rxmit || (sack_bytes_rxmt != 0)),
1051 isipv6);
1052 }
1053
1054 /*
1055 * tcp was closed while we were in ip,
1056 * resume close
1057 */
1058 if (inp->inp_sndinprog_cnt == 0 &&
1059 (tp->t_flags & TF_CLOSING)) {
1060 tp->t_flags &= ~TF_CLOSING;
1061 (void) tcp_close(tp);
1062 } else {
1063 tcp_check_timer_state(tp);
1064 }
1065 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END,
1066 0, 0, 0, 0, 0);
1067 return 0;
1068 }
1069 }
1070 }
1071
1072 /*
1073 * Be careful not to send data and/or FIN on SYN segments.
1074 * This measure is needed to prevent interoperability problems
1075 * with not fully conformant TCP implementations.
1076 *
1077 * In case of TFO, we handle the setting of the len in
1078 * tcp_tfo_check. In case TFO is not enabled, never ever send
1079 * SYN+data.
1080 */
1081 if ((flags & TH_SYN) && !tfo_enabled(tp)) {
1082 len = 0;
1083 flags &= ~TH_FIN;
1084 }
1085
1086 /*
1087 * Don't send a RST with data.
1088 */
1089 if (flags & TH_RST) {
1090 len = 0;
1091 }
1092
1093 if ((flags & TH_SYN) && tp->t_state <= TCPS_SYN_SENT && tfo_enabled(tp)) {
1094 len = tcp_tfo_check(tp, len);
1095 }
1096
1097 /*
1098 * The check here used to be (len < 0). Some times len is zero
1099 * when the congestion window is closed and we need to check
1100 * if persist timer has to be set in that case. But don't set
1101 * persist until connection is established.
1102 */
1103 if (len <= 0 && !(flags & TH_SYN)) {
1104 /*
1105 * If FIN has been sent but not acked,
1106 * but we haven't been called to retransmit,
1107 * len will be < 0. Otherwise, window shrank
1108 * after we sent into it. If window shrank to 0,
1109 * cancel pending retransmit, pull snd_nxt back
1110 * to (closed) window, and set the persist timer
1111 * if it isn't already going. If the window didn't
1112 * close completely, just wait for an ACK.
1113 */
1114 len = 0;
1115 if (sendwin == 0) {
1116 tp->t_timer[TCPT_REXMT] = 0;
1117 tp->t_timer[TCPT_PTO] = 0;
1118 TCP_RESET_REXMT_STATE(tp);
1119 tp->snd_nxt = tp->snd_una;
1120 off = 0;
1121 if (tp->t_timer[TCPT_PERSIST] == 0) {
1122 tcp_setpersist(tp);
1123 }
1124 }
1125 }
1126
1127 /*
1128 * Automatic sizing of send socket buffer. Increase the send
1129 * socket buffer size if all of the following criteria are met
1130 * 1. the receiver has enough buffer space for this data
1131 * 2. send buffer is filled to 7/8th with data (so we actually
1132 * have data to make use of it);
1133 * 3. our send window (slow start and congestion controlled) is
1134 * larger than sent but unacknowledged data in send buffer.
1135 */
1136 if (!INP_WAIT_FOR_IF_FEEDBACK(inp) && !IN_FASTRECOVERY(tp) &&
1137 (so->so_snd.sb_flags & (SB_AUTOSIZE | SB_TRIM)) == SB_AUTOSIZE &&
1138 tcp_cansbgrow(&so->so_snd)) {
1139 if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
1140 so->so_snd.sb_cc >= (so->so_snd.sb_hiwat / 8 * 7) &&
1141 sendwin >= (so->so_snd.sb_cc - (tp->snd_nxt - tp->snd_una))) {
1142 if (sbreserve(&so->so_snd,
1143 min(so->so_snd.sb_hiwat + tcp_autosndbuf_inc,
1144 tcp_autosndbuf_max)) == 1) {
1145 so->so_snd.sb_idealsize = so->so_snd.sb_hiwat;
1146 }
1147 }
1148 }
1149
1150 /*
1151 * Truncate to the maximum segment length or enable TCP Segmentation
1152 * Offloading (if supported by hardware) and ensure that FIN is removed
1153 * if the length no longer contains the last data byte.
1154 *
1155 * TSO may only be used if we are in a pure bulk sending state.
1156 * The presence of TCP-MD5, SACK retransmits, SACK advertizements,
1157 * filters and IP options, as well as disabling hardware checksum
1158 * offload prevent using TSO. With TSO the TCP header is the same
1159 * (except for the sequence number) for all generated packets. This
1160 * makes it impossible to transmit any options which vary per generated
1161 * segment or packet.
1162 *
1163 * The length of TSO bursts is limited to TCP_MAXWIN. That limit and
1164 * removal of FIN (if not already catched here) are handled later after
1165 * the exact length of the TCP options are known.
1166 */
1167 #if IPSEC
1168 /*
1169 * Pre-calculate here as we save another lookup into the darknesses
1170 * of IPsec that way and can actually decide if TSO is ok.
1171 */
1172 if (ipsec_bypass == 0) {
1173 ipsec_optlen = (unsigned int)ipsec_hdrsiz_tcp(tp);
1174 }
1175 #endif
1176 if (len > tp->t_maxseg) {
1177 if ((tp->t_flags & TF_TSO) && tcp_do_tso && hwcksum_tx &&
1178 ip_use_randomid && kipf_count == 0 &&
1179 dlil_filter_disable_tso_count == 0 &&
1180 tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
1181 sack_bytes_rxmt == 0 &&
1182 inp->inp_options == NULL &&
1183 inp->in6p_options == NULL
1184 #if IPSEC
1185 && ipsec_optlen == 0
1186 #endif
1187 ) {
1188 tso = 1;
1189 sendalot = 0;
1190 } else {
1191 len = tp->t_maxseg;
1192 sendalot = 1;
1193 tso = 0;
1194 }
1195 } else {
1196 tso = 0;
1197 }
1198
1199 /* Send one segment or less as a tail loss probe */
1200 if (tp->t_flagsext & TF_SENT_TLPROBE) {
1201 len = min(len, tp->t_maxseg);
1202 sendalot = 0;
1203 tso = 0;
1204 }
1205
1206 #if MPTCP
1207 if (so->so_flags & SOF_MP_SUBFLOW && off < 0) {
1208 os_log_error(mptcp_log_handle, "%s - %lx: offset is negative! len %d off %d\n",
1209 __func__, (unsigned long)VM_KERNEL_ADDRPERM(tp->t_mpsub->mpts_mpte),
1210 len, off);
1211 }
1212
1213 if ((so->so_flags & SOF_MP_SUBFLOW) &&
1214 !(tp->t_mpflags & TMPF_TCP_FALLBACK)) {
1215 int newlen = len;
1216 if (tp->t_state >= TCPS_ESTABLISHED &&
1217 (tp->t_mpflags & TMPF_SND_MPPRIO ||
1218 tp->t_mpflags & TMPF_SND_REM_ADDR ||
1219 tp->t_mpflags & TMPF_SND_MPFAIL ||
1220 tp->t_mpflags & TMPF_SND_KEYS ||
1221 tp->t_mpflags & TMPF_SND_JACK)) {
1222 if (len > 0) {
1223 len = 0;
1224 tso = 0;
1225 }
1226 /*
1227 * On a new subflow, don't try to send again, because
1228 * we are still waiting for the fourth ack.
1229 */
1230 if (!(tp->t_mpflags & TMPF_PREESTABLISHED)) {
1231 sendalot = 1;
1232 }
1233 mptcp_acknow = TRUE;
1234 } else {
1235 mptcp_acknow = FALSE;
1236 }
1237 /*
1238 * The contiguous bytes in the subflow socket buffer can be
1239 * discontiguous at the MPTCP level. Since only one DSS
1240 * option can be sent in one packet, reduce length to match
1241 * the contiguous MPTCP level. Set sendalot to send remainder.
1242 */
1243 if (len > 0 && off >= 0) {
1244 newlen = mptcp_adj_sendlen(so, off);
1245 }
1246
1247 if (newlen < len) {
1248 len = newlen;
1249 if (len <= tp->t_maxseg) {
1250 tso = 0;
1251 }
1252 }
1253 }
1254 #endif /* MPTCP */
1255
1256 if (sack_rxmit) {
1257 if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc)) {
1258 flags &= ~TH_FIN;
1259 }
1260 } else {
1261 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) {
1262 flags &= ~TH_FIN;
1263 }
1264 }
1265 /*
1266 * Compare available window to amount of window
1267 * known to peer (as advertised window less
1268 * next expected input). If the difference is at least two
1269 * max size segments, or at least 25% of the maximum possible
1270 * window, then want to send a window update to peer.
1271 */
1272 recwin = tcp_sbspace(tp);
1273
1274 if (!(so->so_flags & SOF_MP_SUBFLOW)) {
1275 if (recwin < (int32_t)(so->so_rcv.sb_hiwat / 4) &&
1276 recwin < (int)tp->t_maxseg) {
1277 recwin = 0;
1278 }
1279 } else {
1280 struct mptcb *mp_tp = tptomptp(tp);
1281 struct socket *mp_so = mptetoso(mp_tp->mpt_mpte);
1282
1283 if (recwin < (int32_t)(mp_so->so_rcv.sb_hiwat / 4) &&
1284 recwin < (int)tp->t_maxseg) {
1285 recwin = 0;
1286 }
1287 }
1288
1289 #if TRAFFIC_MGT
1290 if (tcp_recv_bg == 1 || IS_TCP_RECV_BG(so)) {
1291 if (recwin > 0 && tcp_recv_throttle(tp)) {
1292 uint32_t min_iaj_win = tcp_min_iaj_win * tp->t_maxseg;
1293 uint32_t bg_rwintop = tp->rcv_adv;
1294 if (SEQ_LT(bg_rwintop, tp->rcv_nxt + min_iaj_win)) {
1295 bg_rwintop = tp->rcv_nxt + min_iaj_win;
1296 }
1297 recwin = imin((int32_t)(bg_rwintop - tp->rcv_nxt),
1298 recwin);
1299 if (recwin < 0) {
1300 recwin = 0;
1301 }
1302 }
1303 }
1304 #endif /* TRAFFIC_MGT */
1305
1306 if (recwin > (int32_t)(TCP_MAXWIN << tp->rcv_scale)) {
1307 recwin = (int32_t)(TCP_MAXWIN << tp->rcv_scale);
1308 }
1309
1310 if (!(so->so_flags & SOF_MP_SUBFLOW)) {
1311 if (recwin < (int32_t)(tp->rcv_adv - tp->rcv_nxt)) {
1312 recwin = (int32_t)(tp->rcv_adv - tp->rcv_nxt);
1313 }
1314 } else {
1315 struct mptcb *mp_tp = tptomptp(tp);
1316 int64_t recwin_announced = (int64_t)(mp_tp->mpt_rcvadv - mp_tp->mpt_rcvnxt);
1317
1318 /* Don't remove what we announced at the MPTCP-layer */
1319 VERIFY(recwin_announced < INT32_MAX && recwin_announced > INT32_MIN);
1320 if (recwin < (int32_t)recwin_announced) {
1321 recwin = (int32_t)recwin_announced;
1322 }
1323 }
1324
1325 /*
1326 * Sender silly window avoidance. We transmit under the following
1327 * conditions when len is non-zero:
1328 *
1329 * - we've timed out (e.g. persist timer)
1330 * - we need to retransmit
1331 * - We have a full segment (or more with TSO)
1332 * - This is the last buffer in a write()/send() and we are
1333 * either idle or running NODELAY
1334 * - we have more then 1/2 the maximum send window's worth of
1335 * data (receiver may be limited the window size)
1336 */
1337 if (len) {
1338 if (tp->t_flagsext & TF_FORCE) {
1339 goto send;
1340 }
1341 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
1342 goto send;
1343 }
1344 if (sack_rxmit) {
1345 goto send;
1346 }
1347
1348 /*
1349 * If this here is the first segment after SYN/ACK and TFO
1350 * is being used, then we always send it, regardless of Nagle,...
1351 */
1352 if (tp->t_state == TCPS_SYN_RECEIVED &&
1353 tfo_enabled(tp) &&
1354 (tp->t_tfo_flags & TFO_F_COOKIE_VALID) &&
1355 tp->snd_nxt == tp->iss + 1) {
1356 goto send;
1357 }
1358
1359 /*
1360 * Send new data on the connection only if it is
1361 * not flow controlled
1362 */
1363 if (!INP_WAIT_FOR_IF_FEEDBACK(inp) ||
1364 tp->t_state != TCPS_ESTABLISHED) {
1365 if (len >= tp->t_maxseg) {
1366 goto send;
1367 }
1368
1369 if (!(tp->t_flags & TF_MORETOCOME) &&
1370 (idle || tp->t_flags & TF_NODELAY ||
1371 (tp->t_flags & TF_MAXSEGSNT) ||
1372 ALLOW_LIMITED_TRANSMIT(tp)) &&
1373 (tp->t_flags & TF_NOPUSH) == 0 &&
1374 (len + off >= so->so_snd.sb_cc ||
1375 /*
1376 * MPTCP needs to respect the DSS-mappings. So, it
1377 * may be sending data that *could* have been
1378 * coalesced, but cannot because of
1379 * mptcp_adj_sendlen().
1380 */
1381 so->so_flags & SOF_MP_SUBFLOW)) {
1382 goto send;
1383 }
1384 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
1385 goto send;
1386 }
1387 } else {
1388 tcpstat.tcps_fcholdpacket++;
1389 }
1390 }
1391
1392 if (recwin > 0) {
1393 /*
1394 * "adv" is the amount we can increase the window,
1395 * taking into account that we are limited by
1396 * TCP_MAXWIN << tp->rcv_scale.
1397 */
1398 int32_t adv, oldwin = 0;
1399 adv = imin(recwin, (int)TCP_MAXWIN << tp->rcv_scale) -
1400 (tp->rcv_adv - tp->rcv_nxt);
1401
1402 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
1403 oldwin = tp->rcv_adv - tp->rcv_nxt;
1404 }
1405
1406 if (tcp_ack_strategy == TCP_ACK_STRATEGY_LEGACY) {
1407 if (adv >= (int32_t) (2 * tp->t_maxseg)) {
1408 /*
1409 * Update only if the resulting scaled value of
1410 * the window changed, or if there is a change in
1411 * the sequence since the last ack. This avoids
1412 * what appears as dupe ACKS (see rdar://5640997)
1413 *
1414 * If streaming is detected avoid sending too many
1415 * window updates. We will depend on the delack
1416 * timer to send a window update when needed.
1417 *
1418 * If there is more data to read, don't send an ACK.
1419 * Otherwise we will end up sending many ACKs if the
1420 * application is doing micro-reads.
1421 */
1422 if (!(tp->t_flags & TF_STRETCHACK) &&
1423 (tp->last_ack_sent != tp->rcv_nxt ||
1424 ((oldwin + adv) >> tp->rcv_scale) >
1425 (oldwin >> tp->rcv_scale))) {
1426 goto send;
1427 }
1428 }
1429 } else {
1430 if (adv >= (int32_t) (2 * tp->t_maxseg)) {
1431 /*
1432 * ACK every second full-sized segment, if the
1433 * ACK is advancing or the window becomes bigger
1434 */
1435 if (so->so_rcv.sb_cc < so->so_rcv.sb_lowat &&
1436 (tp->last_ack_sent != tp->rcv_nxt ||
1437 ((oldwin + adv) >> tp->rcv_scale) >
1438 (oldwin >> tp->rcv_scale))) {
1439 goto send;
1440 }
1441 } else if (tp->t_flags & TF_DELACK) {
1442 /*
1443 * If we delayed the ACK and the window
1444 * is not advancing by a lot (< 2MSS), ACK
1445 * immediately if the last incoming packet had
1446 * the push flag set and we emptied the buffer.
1447 *
1448 * This takes care of a sender doing small
1449 * repeated writes with Nagle enabled.
1450 */
1451 if (so->so_rcv.sb_cc == 0 &&
1452 tp->last_ack_sent != tp->rcv_nxt &&
1453 (tp->t_flagsext & TF_LAST_IS_PSH)) {
1454 goto send;
1455 }
1456 }
1457 }
1458 if (4 * adv >= (int32_t) so->so_rcv.sb_hiwat) {
1459 goto send;
1460 }
1461
1462 /*
1463 * Make sure that the delayed ack timer is set if
1464 * we delayed sending a window update because of
1465 * streaming detection.
1466 */
1467 if (tcp_ack_strategy == TCP_ACK_STRATEGY_LEGACY &&
1468 (tp->t_flags & TF_STRETCHACK) &&
1469 !(tp->t_flags & TF_DELACK)) {
1470 tp->t_flags |= TF_DELACK;
1471 tp->t_timer[TCPT_DELACK] =
1472 OFFSET_FROM_START(tp, tcp_delack);
1473 }
1474 }
1475
1476 /*
1477 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW
1478 * is also a catch-all for the retransmit timer timeout case.
1479 */
1480 if (tp->t_flags & TF_ACKNOW) {
1481 if (tp->t_forced_acks > 0) {
1482 tp->t_forced_acks--;
1483 }
1484 goto send;
1485 }
1486 if ((flags & TH_RST) || (flags & TH_SYN)) {
1487 goto send;
1488 }
1489 if (SEQ_GT(tp->snd_up, tp->snd_una)) {
1490 goto send;
1491 }
1492 #if MPTCP
1493 if (mptcp_acknow) {
1494 goto send;
1495 }
1496 #endif /* MPTCP */
1497 /*
1498 * If our state indicates that FIN should be sent
1499 * and we have not yet done so, then we need to send.
1500 */
1501 if ((flags & TH_FIN) &&
1502 (!(tp->t_flags & TF_SENTFIN) || tp->snd_nxt == tp->snd_una)) {
1503 goto send;
1504 }
1505 /*
1506 * In SACK, it is possible for tcp_output to fail to send a segment
1507 * after the retransmission timer has been turned off. Make sure
1508 * that the retransmission timer is set.
1509 */
1510 if (SACK_ENABLED(tp) && (tp->t_state >= TCPS_ESTABLISHED) &&
1511 SEQ_GT(tp->snd_max, tp->snd_una) &&
1512 tp->t_timer[TCPT_REXMT] == 0 &&
1513 tp->t_timer[TCPT_PERSIST] == 0) {
1514 tp->t_timer[TCPT_REXMT] = OFFSET_FROM_START(tp,
1515 tp->t_rxtcur);
1516 goto just_return;
1517 }
1518 /*
1519 * TCP window updates are not reliable, rather a polling protocol
1520 * using ``persist'' packets is used to insure receipt of window
1521 * updates. The three ``states'' for the output side are:
1522 * idle not doing retransmits or persists
1523 * persisting to move a small or zero window
1524 * (re)transmitting and thereby not persisting
1525 *
1526 * tp->t_timer[TCPT_PERSIST]
1527 * is set when we are in persist state.
1528 * tp->t_force
1529 * is set when we are called to send a persist packet.
1530 * tp->t_timer[TCPT_REXMT]
1531 * is set when we are retransmitting
1532 * The output side is idle when both timers are zero.
1533 *
1534 * If send window is too small, there is data to transmit, and no
1535 * retransmit or persist is pending, then go to persist state.
1536 * If nothing happens soon, send when timer expires:
1537 * if window is nonzero, transmit what we can,
1538 * otherwise force out a byte.
1539 */
1540 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
1541 tp->t_timer[TCPT_PERSIST] == 0) {
1542 TCP_RESET_REXMT_STATE(tp);
1543 tcp_setpersist(tp);
1544 }
1545 just_return:
1546 /*
1547 * If there is no reason to send a segment, just return.
1548 * but if there is some packets left in the packet list, send them now.
1549 */
1550 while (inp->inp_sndinprog_cnt == 0 &&
1551 tp->t_pktlist_head != NULL) {
1552 packetlist = tp->t_pktlist_head;
1553 packchain_listadd = tp->t_lastchain;
1554 packchain_sent++;
1555 TCP_PKTLIST_CLEAR(tp);
1556
1557 error = tcp_ip_output(so, tp, packetlist,
1558 packchain_listadd,
1559 tp_inp_options, (so_options & SO_DONTROUTE),
1560 (sack_rxmit || (sack_bytes_rxmt != 0)), isipv6);
1561 }
1562 /* tcp was closed while we were in ip; resume close */
1563 if (inp->inp_sndinprog_cnt == 0 &&
1564 (tp->t_flags & TF_CLOSING)) {
1565 tp->t_flags &= ~TF_CLOSING;
1566 (void) tcp_close(tp);
1567 } else {
1568 tcp_check_timer_state(tp);
1569 }
1570 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
1571 return 0;
1572
1573 send:
1574 /*
1575 * Set TF_MAXSEGSNT flag if the segment size is greater than
1576 * the max segment size.
1577 */
1578 if (len > 0) {
1579 do_not_compress = TRUE;
1580
1581 if (len >= tp->t_maxseg) {
1582 tp->t_flags |= TF_MAXSEGSNT;
1583 } else {
1584 tp->t_flags &= ~TF_MAXSEGSNT;
1585 }
1586 }
1587 /*
1588 * Before ESTABLISHED, force sending of initial options
1589 * unless TCP set not to do any options.
1590 * NOTE: we assume that the IP/TCP header plus TCP options
1591 * always fit in a single mbuf, leaving room for a maximum
1592 * link header, i.e.
1593 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
1594 */
1595 optlen = 0;
1596 if (isipv6) {
1597 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1598 } else {
1599 hdrlen = sizeof(struct tcpiphdr);
1600 }
1601 if (flags & TH_SYN) {
1602 tp->snd_nxt = tp->iss;
1603 if ((tp->t_flags & TF_NOOPT) == 0) {
1604 u_short mss;
1605
1606 opt[0] = TCPOPT_MAXSEG;
1607 opt[1] = TCPOLEN_MAXSEG;
1608 mss = htons((u_short) tcp_mssopt(tp));
1609 (void)memcpy(opt + 2, &mss, sizeof(mss));
1610 optlen = TCPOLEN_MAXSEG;
1611
1612 if ((tp->t_flags & TF_REQ_SCALE) &&
1613 ((flags & TH_ACK) == 0 ||
1614 (tp->t_flags & TF_RCVD_SCALE))) {
1615 *((u_int32_t *)(void *)(opt + optlen)) = htonl(
1616 TCPOPT_NOP << 24 |
1617 TCPOPT_WINDOW << 16 |
1618 TCPOLEN_WINDOW << 8 |
1619 tp->request_r_scale);
1620 optlen += 4;
1621 }
1622 #if MPTCP
1623 if (mptcp_enable && (so->so_flags & SOF_MP_SUBFLOW)) {
1624 optlen = mptcp_setup_syn_opts(so, opt, optlen);
1625 }
1626 #endif /* MPTCP */
1627 }
1628 }
1629
1630 /*
1631 * Send a timestamp and echo-reply if this is a SYN and our side
1632 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
1633 * and our peer have sent timestamps in our SYN's.
1634 */
1635 if ((tp->t_flags & (TF_REQ_TSTMP | TF_NOOPT)) == TF_REQ_TSTMP &&
1636 (flags & TH_RST) == 0 &&
1637 ((flags & TH_ACK) == 0 ||
1638 (tp->t_flags & TF_RCVD_TSTMP))) {
1639 u_int32_t *lp = (u_int32_t *)(void *)(opt + optlen);
1640
1641 /* Form timestamp option as shown in appendix A of RFC 1323. */
1642 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
1643 *lp++ = htonl(tcp_now + tp->t_ts_offset);
1644 *lp = htonl(tp->ts_recent);
1645 optlen += TCPOLEN_TSTAMP_APPA;
1646 }
1647
1648 if (SACK_ENABLED(tp) && ((tp->t_flags & TF_NOOPT) == 0)) {
1649 /*
1650 * Tack on the SACK permitted option *last*.
1651 * And do padding of options after tacking this on.
1652 * This is because of MSS, TS, WinScale and Signatures are
1653 * all present, we have just 2 bytes left for the SACK
1654 * permitted option, which is just enough.
1655 */
1656 /*
1657 * If this is the first SYN of connection (not a SYN
1658 * ACK), include SACK permitted option. If this is a
1659 * SYN ACK, include SACK permitted option if peer has
1660 * already done so. This is only for active connect,
1661 * since the syncache takes care of the passive connect.
1662 */
1663 if ((flags & TH_SYN) &&
1664 (!(flags & TH_ACK) || (tp->t_flags & TF_SACK_PERMIT))) {
1665 u_char *bp;
1666 bp = (u_char *)opt + optlen;
1667
1668 *bp++ = TCPOPT_SACK_PERMITTED;
1669 *bp++ = TCPOLEN_SACK_PERMITTED;
1670 optlen += TCPOLEN_SACK_PERMITTED;
1671 }
1672 }
1673 #if MPTCP
1674 if (so->so_flags & SOF_MP_SUBFLOW) {
1675 /*
1676 * Its important to piggyback acks with data as ack only packets
1677 * may get lost and data packets that don't send Data ACKs
1678 * still advance the subflow level ACK and therefore make it
1679 * hard for the remote end to recover in low cwnd situations.
1680 */
1681 if (len != 0) {
1682 tp->t_mpflags |= (TMPF_SEND_DSN |
1683 TMPF_MPTCP_ACKNOW);
1684 } else {
1685 tp->t_mpflags |= TMPF_MPTCP_ACKNOW;
1686 }
1687 optlen = mptcp_setup_opts(tp, off, &opt[0], optlen, flags,
1688 len, &mptcp_acknow, &do_not_compress);
1689 tp->t_mpflags &= ~TMPF_SEND_DSN;
1690 }
1691 #endif /* MPTCP */
1692
1693 if (tfo_enabled(tp) && !(tp->t_flags & TF_NOOPT) &&
1694 (flags & (TH_SYN | TH_ACK)) == TH_SYN) {
1695 optlen += tcp_tfo_write_cookie(tp, optlen, len, opt);
1696 }
1697
1698 if (tfo_enabled(tp) &&
1699 (flags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK) &&
1700 (tp->t_tfo_flags & TFO_F_OFFER_COOKIE)) {
1701 optlen += tcp_tfo_write_cookie_rep(tp, optlen, opt);
1702 }
1703
1704 if (SACK_ENABLED(tp) && ((tp->t_flags & TF_NOOPT) == 0)) {
1705 /*
1706 * Send SACKs if necessary. This should be the last
1707 * option processed. Only as many SACKs are sent as
1708 * are permitted by the maximum options size.
1709 *
1710 * In general, SACK blocks consume 8*n+2 bytes.
1711 * So a full size SACK blocks option is 34 bytes
1712 * (to generate 4 SACK blocks). At a minimum,
1713 * we need 10 bytes (to generate 1 SACK block).
1714 * If TCP Timestamps (12 bytes) and TCP Signatures
1715 * (18 bytes) are both present, we'll just have
1716 * 10 bytes for SACK options 40 - (12 + 18).
1717 */
1718 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1719 (tp->t_flags & TF_SACK_PERMIT) &&
1720 (tp->rcv_numsacks > 0 || TCP_SEND_DSACK_OPT(tp)) &&
1721 MAX_TCPOPTLEN - optlen - 2 >= TCPOLEN_SACK) {
1722 int nsack, padlen;
1723 u_char *bp = (u_char *)opt + optlen;
1724 u_int32_t *lp;
1725
1726 nsack = (MAX_TCPOPTLEN - optlen - 2) / TCPOLEN_SACK;
1727 nsack = min(nsack, (tp->rcv_numsacks +
1728 (TCP_SEND_DSACK_OPT(tp) ? 1 : 0)));
1729 sackoptlen = (2 + nsack * TCPOLEN_SACK);
1730 VERIFY(sackoptlen < UINT8_MAX);
1731
1732 /*
1733 * First we need to pad options so that the
1734 * SACK blocks can start at a 4-byte boundary
1735 * (sack option and length are at a 2 byte offset).
1736 */
1737 padlen = (MAX_TCPOPTLEN - optlen - sackoptlen) % 4;
1738 optlen += padlen;
1739 while (padlen-- > 0) {
1740 *bp++ = TCPOPT_NOP;
1741 }
1742
1743 tcpstat.tcps_sack_send_blocks++;
1744 *bp++ = TCPOPT_SACK;
1745 *bp++ = (uint8_t)sackoptlen;
1746 lp = (u_int32_t *)(void *)bp;
1747
1748 /*
1749 * First block of SACK option should represent
1750 * DSACK. Prefer to send SACK information if there
1751 * is space for only one SACK block. This will
1752 * allow for faster recovery.
1753 */
1754 if (TCP_SEND_DSACK_OPT(tp) && nsack > 0 &&
1755 (tp->rcv_numsacks == 0 || nsack > 1)) {
1756 *lp++ = htonl(tp->t_dsack_lseq);
1757 *lp++ = htonl(tp->t_dsack_rseq);
1758 tcpstat.tcps_dsack_sent++;
1759 tp->t_dsack_sent++;
1760 nsack--;
1761 }
1762 VERIFY(nsack == 0 || tp->rcv_numsacks >= nsack);
1763 for (i = 0; i < nsack; i++) {
1764 struct sackblk sack = tp->sackblks[i];
1765 *lp++ = htonl(sack.start);
1766 *lp++ = htonl(sack.end);
1767 }
1768 optlen += sackoptlen;
1769 }
1770 }
1771
1772 /* Pad TCP options to a 4 byte boundary */
1773 if (optlen < MAX_TCPOPTLEN && (optlen % sizeof(u_int32_t))) {
1774 int pad = sizeof(u_int32_t) - (optlen % sizeof(u_int32_t));
1775 u_char *bp = (u_char *)opt + optlen;
1776
1777 optlen += pad;
1778 while (pad) {
1779 *bp++ = TCPOPT_EOL;
1780 pad--;
1781 }
1782 }
1783
1784 /*
1785 * RFC 3168 states that:
1786 * - If you ever sent an ECN-setup SYN/SYN-ACK you must be prepared
1787 * to handle the TCP ECE flag, even if you also later send a
1788 * non-ECN-setup SYN/SYN-ACK.
1789 * - If you ever send a non-ECN-setup SYN/SYN-ACK, you must not set
1790 * the ip ECT flag.
1791 *
1792 * It is not clear how the ECE flag would ever be set if you never
1793 * set the IP ECT flag on outbound packets. All the same, we use
1794 * the TE_SETUPSENT to indicate that we have committed to handling
1795 * the TCP ECE flag correctly. We use the TE_SENDIPECT to indicate
1796 * whether or not we should set the IP ECT flag on outbound packet
1797 *
1798 * For a SYN-ACK, send an ECN setup SYN-ACK
1799 */
1800 if ((flags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK) &&
1801 (tp->ecn_flags & TE_ENABLE_ECN)) {
1802 if (tp->ecn_flags & TE_SETUPRECEIVED) {
1803 if (tcp_send_ecn_flags_on_syn(tp)) {
1804 /*
1805 * Setting TH_ECE makes this an ECN-setup
1806 * SYN-ACK
1807 */
1808 flags |= TH_ECE;
1809
1810 /*
1811 * Record that we sent the ECN-setup and
1812 * default to setting IP ECT.
1813 */
1814 tp->ecn_flags |= (TE_SETUPSENT | TE_SENDIPECT);
1815 tcpstat.tcps_ecn_server_setup++;
1816 tcpstat.tcps_ecn_server_success++;
1817 } else {
1818 /*
1819 * We sent an ECN-setup SYN-ACK but it was
1820 * dropped. Fallback to non-ECN-setup
1821 * SYN-ACK and clear flag to indicate that
1822 * we should not send data with IP ECT set
1823 *
1824 * Pretend we didn't receive an
1825 * ECN-setup SYN.
1826 *
1827 * We already incremented the counter
1828 * assuming that the ECN setup will
1829 * succeed. Decrementing here
1830 * tcps_ecn_server_success to correct it.
1831 */
1832 if (tp->ecn_flags & TE_SETUPSENT) {
1833 tcpstat.tcps_ecn_lost_synack++;
1834 tcpstat.tcps_ecn_server_success--;
1835 tp->ecn_flags |= TE_LOST_SYNACK;
1836 }
1837
1838 tp->ecn_flags &=
1839 ~(TE_SETUPRECEIVED | TE_SENDIPECT |
1840 TE_SENDCWR);
1841 }
1842 }
1843 } else if ((flags & (TH_SYN | TH_ACK)) == TH_SYN &&
1844 (tp->ecn_flags & TE_ENABLE_ECN)) {
1845 if (tcp_send_ecn_flags_on_syn(tp)) {
1846 /*
1847 * Setting TH_ECE and TH_CWR makes this an
1848 * ECN-setup SYN
1849 */
1850 flags |= (TH_ECE | TH_CWR);
1851 tcpstat.tcps_ecn_client_setup++;
1852 tp->ecn_flags |= TE_CLIENT_SETUP;
1853
1854 /*
1855 * Record that we sent the ECN-setup and default to
1856 * setting IP ECT.
1857 */
1858 tp->ecn_flags |= (TE_SETUPSENT | TE_SENDIPECT);
1859 } else {
1860 /*
1861 * We sent an ECN-setup SYN but it was dropped.
1862 * Fall back to non-ECN and clear flag indicating
1863 * we should send data with IP ECT set.
1864 */
1865 if (tp->ecn_flags & TE_SETUPSENT) {
1866 tcpstat.tcps_ecn_lost_syn++;
1867 tp->ecn_flags |= TE_LOST_SYN;
1868 }
1869 tp->ecn_flags &= ~TE_SENDIPECT;
1870 }
1871 }
1872
1873 /*
1874 * Check if we should set the TCP CWR flag.
1875 * CWR flag is sent when we reduced the congestion window because
1876 * we received a TCP ECE or we performed a fast retransmit. We
1877 * never set the CWR flag on retransmitted packets. We only set
1878 * the CWR flag on data packets. Pure acks don't have this set.
1879 */
1880 if ((tp->ecn_flags & TE_SENDCWR) != 0 && len != 0 &&
1881 !SEQ_LT(tp->snd_nxt, tp->snd_max) && !sack_rxmit) {
1882 flags |= TH_CWR;
1883 tp->ecn_flags &= ~TE_SENDCWR;
1884 }
1885
1886 /*
1887 * Check if we should set the TCP ECE flag.
1888 */
1889 if ((tp->ecn_flags & TE_SENDECE) != 0 && len == 0) {
1890 flags |= TH_ECE;
1891 tcpstat.tcps_ecn_sent_ece++;
1892 }
1893
1894
1895 hdrlen += optlen;
1896
1897 /* Reset DSACK sequence numbers */
1898 tp->t_dsack_lseq = 0;
1899 tp->t_dsack_rseq = 0;
1900
1901 if (isipv6) {
1902 ipoptlen = ip6_optlen(inp);
1903 } else {
1904 if (tp_inp_options) {
1905 ipoptlen = tp_inp_options->m_len -
1906 offsetof(struct ipoption, ipopt_list);
1907 } else {
1908 ipoptlen = 0;
1909 }
1910 }
1911 #if IPSEC
1912 ipoptlen += ipsec_optlen;
1913 #endif
1914
1915 /*
1916 * Adjust data length if insertion of options will
1917 * bump the packet length beyond the t_maxopd length.
1918 * Clear the FIN bit because we cut off the tail of
1919 * the segment.
1920 *
1921 * When doing TSO limit a burst to TCP_MAXWIN minus the
1922 * IP, TCP and Options length to keep ip->ip_len from
1923 * overflowing. Prevent the last segment from being
1924 * fractional thus making them all equal sized and set
1925 * the flag to continue sending. TSO is disabled when
1926 * IP options or IPSEC are present.
1927 */
1928 if (len + optlen + ipoptlen > tp->t_maxopd) {
1929 /*
1930 * If there is still more to send,
1931 * don't close the connection.
1932 */
1933 flags &= ~TH_FIN;
1934 if (tso) {
1935 int32_t tso_maxlen;
1936
1937 tso_maxlen = tp->tso_max_segment_size ?
1938 tp->tso_max_segment_size : TCP_MAXWIN;
1939
1940 if (len > tso_maxlen - hdrlen - optlen) {
1941 len = tso_maxlen - hdrlen - optlen;
1942 sendalot = 1;
1943 } else if (tp->t_flags & TF_NEEDFIN) {
1944 sendalot = 1;
1945 }
1946
1947 if (len % (tp->t_maxopd - optlen) != 0) {
1948 len = len - (len % (tp->t_maxopd - optlen));
1949 sendalot = 1;
1950 }
1951 } else {
1952 len = tp->t_maxopd - optlen - ipoptlen;
1953 sendalot = 1;
1954 }
1955 }
1956
1957 if (max_linkhdr + hdrlen > MCLBYTES) {
1958 panic("tcphdr too big");
1959 }
1960
1961 /* Check if there is enough data in the send socket
1962 * buffer to start measuring bandwidth
1963 */
1964 if ((tp->t_flagsext & TF_MEASURESNDBW) != 0 &&
1965 (tp->t_bwmeas != NULL) &&
1966 (tp->t_flagsext & TF_BWMEAS_INPROGRESS) == 0) {
1967 tp->t_bwmeas->bw_size = min(min(
1968 (so->so_snd.sb_cc - (tp->snd_max - tp->snd_una)),
1969 tp->snd_cwnd), tp->snd_wnd);
1970 if (tp->t_bwmeas->bw_minsize > 0 &&
1971 tp->t_bwmeas->bw_size < tp->t_bwmeas->bw_minsize) {
1972 tp->t_bwmeas->bw_size = 0;
1973 }
1974 if (tp->t_bwmeas->bw_maxsize > 0) {
1975 tp->t_bwmeas->bw_size = min(tp->t_bwmeas->bw_size,
1976 tp->t_bwmeas->bw_maxsize);
1977 }
1978 if (tp->t_bwmeas->bw_size > 0) {
1979 tp->t_flagsext |= TF_BWMEAS_INPROGRESS;
1980 tp->t_bwmeas->bw_start = tp->snd_max;
1981 tp->t_bwmeas->bw_ts = tcp_now;
1982 }
1983 }
1984
1985 VERIFY(inp->inp_flowhash != 0);
1986 /*
1987 * Grab a header mbuf, attaching a copy of data to
1988 * be transmitted, and initialize the header from
1989 * the template for sends on this connection.
1990 */
1991 if (len) {
1992 /* Remember what the last head-of-line packet-size was */
1993 if (tp->t_pmtud_lastseg_size == 0 && tp->snd_nxt == tp->snd_una) {
1994 ASSERT(len + optlen + ipoptlen <= IP_MAXPACKET);
1995 tp->t_pmtud_lastseg_size = (uint16_t)(len + optlen + ipoptlen);
1996 }
1997 if ((tp->t_flagsext & TF_FORCE) && len == 1) {
1998 tcpstat.tcps_sndprobe++;
1999 } else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
2000 tcpstat.tcps_sndrexmitpack++;
2001 tcpstat.tcps_sndrexmitbyte += len;
2002 if (nstat_collect) {
2003 nstat_route_tx(inp->inp_route.ro_rt, 1,
2004 len, NSTAT_TX_FLAG_RETRANSMIT);
2005 INP_ADD_STAT(inp, cell, wifi, wired,
2006 txpackets, 1);
2007 INP_ADD_STAT(inp, cell, wifi, wired,
2008 txbytes, len);
2009 tp->t_stat.txretransmitbytes += len;
2010 tp->t_stat.rxmitpkts++;
2011 }
2012 } else {
2013 tcpstat.tcps_sndpack++;
2014 tcpstat.tcps_sndbyte += len;
2015
2016 if (nstat_collect) {
2017 INP_ADD_STAT(inp, cell, wifi, wired,
2018 txpackets, 1);
2019 INP_ADD_STAT(inp, cell, wifi, wired,
2020 txbytes, len);
2021 }
2022 inp_decr_sndbytes_unsent(so, len);
2023 }
2024 inp_set_activity_bitmap(inp);
2025 #if MPTCP
2026 if (tp->t_mpflags & TMPF_MPTCP_TRUE) {
2027 tcpstat.tcps_mp_sndpacks++;
2028 tcpstat.tcps_mp_sndbytes += len;
2029 }
2030 #endif /* MPTCP */
2031 /*
2032 * try to use the new interface that allocates all
2033 * the necessary mbuf hdrs under 1 mbuf lock and
2034 * avoids rescanning the socket mbuf list if
2035 * certain conditions are met. This routine can't
2036 * be used in the following cases...
2037 * 1) the protocol headers exceed the capacity of
2038 * of a single mbuf header's data area (no cluster attached)
2039 * 2) the length of the data being transmitted plus
2040 * the protocol headers fits into a single mbuf header's
2041 * data area (no cluster attached)
2042 */
2043 m = NULL;
2044
2045 /* minimum length we are going to allocate */
2046 allocated_len = MHLEN;
2047 if (MHLEN < hdrlen + max_linkhdr) {
2048 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2049 if (m == NULL) {
2050 error = ENOBUFS;
2051 goto out;
2052 }
2053 MCLGET(m, M_DONTWAIT);
2054 if ((m->m_flags & M_EXT) == 0) {
2055 m_freem(m);
2056 error = ENOBUFS;
2057 goto out;
2058 }
2059 m->m_data += max_linkhdr;
2060 m->m_len = hdrlen;
2061 allocated_len = MCLBYTES;
2062 }
2063 if (len <= allocated_len - hdrlen - max_linkhdr) {
2064 if (m == NULL) {
2065 VERIFY(allocated_len <= MHLEN);
2066 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2067 if (m == NULL) {
2068 error = ENOBUFS;
2069 goto out;
2070 }
2071 m->m_data += max_linkhdr;
2072 m->m_len = hdrlen;
2073 }
2074 /* makes sure we still have data left to be sent at this point */
2075 if (so->so_snd.sb_mb == NULL || off < 0) {
2076 if (m != NULL) {
2077 m_freem(m);
2078 }
2079 error = 0; /* should we return an error? */
2080 goto out;
2081 }
2082 m_copydata(so->so_snd.sb_mb, off, (int) len,
2083 mtod(m, caddr_t) + hdrlen);
2084 m->m_len += len;
2085 } else {
2086 uint32_t copymode;
2087 /*
2088 * Retain packet header metadata at the socket
2089 * buffer if this is is an MPTCP subflow,
2090 * otherwise move it.
2091 */
2092 copymode = M_COPYM_MOVE_HDR;
2093 #if MPTCP
2094 if (so->so_flags & SOF_MP_SUBFLOW) {
2095 copymode = M_COPYM_NOOP_HDR;
2096 }
2097 #endif /* MPTCP */
2098 if (m != NULL) {
2099 m->m_next = m_copym_mode(so->so_snd.sb_mb,
2100 off, (int)len, M_DONTWAIT, copymode);
2101 if (m->m_next == NULL) {
2102 (void) m_free(m);
2103 error = ENOBUFS;
2104 goto out;
2105 }
2106 } else {
2107 /*
2108 * make sure we still have data left
2109 * to be sent at this point
2110 */
2111 if (so->so_snd.sb_mb == NULL) {
2112 error = 0; /* should we return an error? */
2113 goto out;
2114 }
2115
2116 /*
2117 * m_copym_with_hdrs will always return the
2118 * last mbuf pointer and the offset into it that
2119 * it acted on to fullfill the current request,
2120 * whether a valid 'hint' was passed in or not.
2121 */
2122 if ((m = m_copym_with_hdrs(so->so_snd.sb_mb,
2123 off, len, M_DONTWAIT, NULL, NULL,
2124 copymode)) == NULL) {
2125 error = ENOBUFS;
2126 goto out;
2127 }
2128 m->m_data += max_linkhdr;
2129 m->m_len = hdrlen;
2130 }
2131 }
2132 /*
2133 * If we're sending everything we've got, set PUSH.
2134 * (This will keep happy those implementations which only
2135 * give data to the user when a buffer fills or
2136 * a PUSH comes in.)
2137 *
2138 * On SYN-segments we should not add the PUSH-flag.
2139 */
2140 if (off + len == so->so_snd.sb_cc && !(flags & TH_SYN)) {
2141 flags |= TH_PUSH;
2142 }
2143 } else {
2144 if (tp->t_flags & TF_ACKNOW) {
2145 tcpstat.tcps_sndacks++;
2146 } else if (flags & (TH_SYN | TH_FIN | TH_RST)) {
2147 tcpstat.tcps_sndctrl++;
2148 } else if (SEQ_GT(tp->snd_up, tp->snd_una)) {
2149 tcpstat.tcps_sndurg++;
2150 } else {
2151 tcpstat.tcps_sndwinup++;
2152 }
2153
2154 MGETHDR(m, M_DONTWAIT, MT_HEADER); /* MAC-OK */
2155 if (m == NULL) {
2156 error = ENOBUFS;
2157 goto out;
2158 }
2159 if (MHLEN < (hdrlen + max_linkhdr)) {
2160 MCLGET(m, M_DONTWAIT);
2161 if ((m->m_flags & M_EXT) == 0) {
2162 m_freem(m);
2163 error = ENOBUFS;
2164 goto out;
2165 }
2166 }
2167 m->m_data += max_linkhdr;
2168 m->m_len = hdrlen;
2169 }
2170 m->m_pkthdr.rcvif = 0;
2171
2172 /* Any flag other than pure-ACK: Do not compress! */
2173 if (flags & ~(TH_ACK)) {
2174 do_not_compress = TRUE;
2175 }
2176
2177 if (tp->rcv_scale == 0) {
2178 do_not_compress = TRUE;
2179 }
2180
2181 if (do_not_compress || (tcp_do_ack_compression == 1 && !cell) || __improbable(!tcp_do_ack_compression)) {
2182 m->m_pkthdr.comp_gencnt = 0;
2183 } else {
2184 if (TSTMP_LT(tp->t_comp_lastinc + tcp_ack_compression_rate, tcp_now)) {
2185 tp->t_comp_gencnt++;
2186 /* 0 means no compression, thus jump this */
2187 if (tp->t_comp_gencnt <= TCP_ACK_COMPRESSION_DUMMY) {
2188 tp->t_comp_gencnt = TCP_ACK_COMPRESSION_DUMMY + 1;
2189 }
2190 tp->t_comp_lastinc = tcp_now;
2191 }
2192 m->m_pkthdr.comp_gencnt = tp->t_comp_gencnt;
2193 }
2194
2195 if (isipv6) {
2196 ip6 = mtod(m, struct ip6_hdr *);
2197 th = (struct tcphdr *)(void *)(ip6 + 1);
2198 tcp_fillheaders(tp, ip6, th);
2199 if ((tp->ecn_flags & TE_SENDIPECT) != 0 && len &&
2200 !SEQ_LT(tp->snd_nxt, tp->snd_max) && !sack_rxmit) {
2201 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
2202 }
2203 svc_flags |= PKT_SCF_IPV6;
2204 #if PF_ECN
2205 m_pftag(m)->pftag_hdr = (void *)ip6;
2206 m_pftag(m)->pftag_flags |= PF_TAG_HDR_INET6;
2207 #endif /* PF_ECN */
2208 } else {
2209 ip = mtod(m, struct ip *);
2210 th = (struct tcphdr *)(void *)(ip + 1);
2211 /* this picks up the pseudo header (w/o the length) */
2212 tcp_fillheaders(tp, ip, th);
2213 if ((tp->ecn_flags & TE_SENDIPECT) != 0 && len &&
2214 !SEQ_LT(tp->snd_nxt, tp->snd_max) &&
2215 !sack_rxmit && !(flags & TH_SYN)) {
2216 ip->ip_tos |= IPTOS_ECN_ECT0;
2217 }
2218 #if PF_ECN
2219 m_pftag(m)->pftag_hdr = (void *)ip;
2220 m_pftag(m)->pftag_flags |= PF_TAG_HDR_INET;
2221 #endif /* PF_ECN */
2222 }
2223
2224 /*
2225 * Fill in fields, remembering maximum advertised
2226 * window for use in delaying messages about window sizes.
2227 * If resending a FIN, be sure not to use a new sequence number.
2228 */
2229 if ((flags & TH_FIN) && (tp->t_flags & TF_SENTFIN) &&
2230 tp->snd_nxt == tp->snd_max) {
2231 tp->snd_nxt--;
2232 }
2233 /*
2234 * If we are doing retransmissions, then snd_nxt will
2235 * not reflect the first unsent octet. For ACK only
2236 * packets, we do not want the sequence number of the
2237 * retransmitted packet, we want the sequence number
2238 * of the next unsent octet. So, if there is no data
2239 * (and no SYN or FIN), use snd_max instead of snd_nxt
2240 * when filling in ti_seq. But if we are in persist
2241 * state, snd_max might reflect one byte beyond the
2242 * right edge of the window, so use snd_nxt in that
2243 * case, since we know we aren't doing a retransmission.
2244 * (retransmit and persist are mutually exclusive...)
2245 *
2246 * Note the state of this retransmit segment to detect spurious
2247 * retransmissions.
2248 */
2249 if (sack_rxmit == 0) {
2250 if (len || (flags & (TH_SYN | TH_FIN)) ||
2251 tp->t_timer[TCPT_PERSIST]) {
2252 th->th_seq = htonl(tp->snd_nxt);
2253 if (len > 0) {
2254 m->m_pkthdr.tx_start_seq = tp->snd_nxt;
2255 m->m_pkthdr.pkt_flags |= PKTF_START_SEQ;
2256 }
2257 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
2258 if (SACK_ENABLED(tp) && len > 1) {
2259 tcp_rxtseg_insert(tp, tp->snd_nxt,
2260 (tp->snd_nxt + len - 1));
2261 }
2262 if (len > 0) {
2263 m->m_pkthdr.pkt_flags |=
2264 PKTF_TCP_REXMT;
2265 }
2266 }
2267 } else {
2268 th->th_seq = htonl(tp->snd_max);
2269 }
2270 } else {
2271 th->th_seq = htonl(p->rxmit);
2272 if (len > 0) {
2273 m->m_pkthdr.pkt_flags |=
2274 (PKTF_TCP_REXMT | PKTF_START_SEQ);
2275 m->m_pkthdr.tx_start_seq = p->rxmit;
2276 }
2277 tcp_rxtseg_insert(tp, p->rxmit, (p->rxmit + len - 1));
2278 p->rxmit += len;
2279 tp->sackhint.sack_bytes_rexmit += len;
2280 }
2281 th->th_ack = htonl(tp->rcv_nxt);
2282 tp->last_ack_sent = tp->rcv_nxt;
2283 if (optlen) {
2284 bcopy(opt, th + 1, optlen);
2285 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
2286 }
2287 th->th_flags = flags;
2288 th->th_win = htons((u_short) (recwin >> tp->rcv_scale));
2289 tp->t_last_recwin = recwin;
2290 if (!(so->so_flags & SOF_MP_SUBFLOW)) {
2291 if (recwin > 0 && SEQ_LT(tp->rcv_adv, tp->rcv_nxt + recwin)) {
2292 tp->rcv_adv = tp->rcv_nxt + recwin;
2293 }
2294 } else {
2295 struct mptcb *mp_tp = tptomptp(tp);
2296 if (recwin > 0) {
2297 tp->rcv_adv = tp->rcv_nxt + recwin;
2298 }
2299
2300 if (recwin > 0 && MPTCP_SEQ_LT(mp_tp->mpt_rcvadv, mp_tp->mpt_rcvnxt + recwin)) {
2301 mp_tp->mpt_rcvadv = mp_tp->mpt_rcvnxt + recwin;
2302 }
2303 }
2304
2305 /*
2306 * Adjust the RXWIN0SENT flag - indicate that we have advertised
2307 * a 0 window. This may cause the remote transmitter to stall. This
2308 * flag tells soreceive() to disable delayed acknowledgements when
2309 * draining the buffer. This can occur if the receiver is attempting
2310 * to read more data then can be buffered prior to transmitting on
2311 * the connection.
2312 */
2313 if (th->th_win == 0) {
2314 tp->t_flags |= TF_RXWIN0SENT;
2315 } else {
2316 tp->t_flags &= ~TF_RXWIN0SENT;
2317 }
2318
2319 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
2320 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
2321 th->th_flags |= TH_URG;
2322 } else {
2323 /*
2324 * If no urgent pointer to send, then we pull
2325 * the urgent pointer to the left edge of the send window
2326 * so that it doesn't drift into the send window on sequence
2327 * number wraparound.
2328 */
2329 tp->snd_up = tp->snd_una; /* drag it along */
2330 }
2331
2332 /*
2333 * Put TCP length in extended header, and then
2334 * checksum extended header and data.
2335 */
2336 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
2337
2338 /*
2339 * If this is potentially the last packet on the stream, then mark
2340 * it in order to enable some optimizations in the underlying
2341 * layers
2342 */
2343 if (tp->t_state != TCPS_ESTABLISHED &&
2344 (tp->t_state == TCPS_CLOSING || tp->t_state == TCPS_TIME_WAIT
2345 || tp->t_state == TCPS_LAST_ACK || (th->th_flags & TH_RST))) {
2346 m->m_pkthdr.pkt_flags |= PKTF_LAST_PKT;
2347 }
2348
2349 if (isipv6) {
2350 /*
2351 * ip6_plen is not need to be filled now, and will be filled
2352 * in ip6_output.
2353 */
2354 m->m_pkthdr.csum_flags = CSUM_TCPIPV6;
2355 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
2356 if (len + optlen) {
2357 th->th_sum = in_addword(th->th_sum,
2358 htons((u_short)(optlen + len)));
2359 }
2360 } else {
2361 m->m_pkthdr.csum_flags = CSUM_TCP;
2362 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
2363 if (len + optlen) {
2364 th->th_sum = in_addword(th->th_sum,
2365 htons((u_short)(optlen + len)));
2366 }
2367 }
2368
2369 /*
2370 * Enable TSO and specify the size of the segments.
2371 * The TCP pseudo header checksum is always provided.
2372 */
2373 if (tso) {
2374 if (isipv6) {
2375 m->m_pkthdr.csum_flags |= CSUM_TSO_IPV6;
2376 } else {
2377 m->m_pkthdr.csum_flags |= CSUM_TSO_IPV4;
2378 }
2379
2380 m->m_pkthdr.tso_segsz = tp->t_maxopd - optlen;
2381 } else {
2382 m->m_pkthdr.tso_segsz = 0;
2383 }
2384
2385 /*
2386 * In transmit state, time the transmission and arrange for
2387 * the retransmit. In persist state, just set snd_max.
2388 */
2389 if (!(tp->t_flagsext & TF_FORCE)
2390 || tp->t_timer[TCPT_PERSIST] == 0) {
2391 tcp_seq startseq = tp->snd_nxt;
2392
2393 /*
2394 * Advance snd_nxt over sequence space of this segment.
2395 */
2396 if (flags & (TH_SYN | TH_FIN)) {
2397 if (flags & TH_SYN) {
2398 tp->snd_nxt++;
2399 }
2400 if ((flags & TH_FIN) &&
2401 !(tp->t_flags & TF_SENTFIN)) {
2402 tp->snd_nxt++;
2403 tp->t_flags |= TF_SENTFIN;
2404 }
2405 }
2406 if (sack_rxmit) {
2407 goto timer;
2408 }
2409 if (sack_rescue_rxt == TRUE) {
2410 tp->snd_nxt = old_snd_nxt;
2411 sack_rescue_rxt = FALSE;
2412 tcpstat.tcps_pto_in_recovery++;
2413 } else {
2414 tp->snd_nxt += len;
2415 }
2416 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
2417 tp->snd_max = tp->snd_nxt;
2418 tp->t_sndtime = tcp_now;
2419 /*
2420 * Time this transmission if not a retransmission and
2421 * not currently timing anything.
2422 */
2423 if (tp->t_rtttime == 0) {
2424 tp->t_rtttime = tcp_now;
2425 tp->t_rtseq = startseq;
2426 tcpstat.tcps_segstimed++;
2427
2428 /* update variables related to pipe ack */
2429 tp->t_pipeack_lastuna = tp->snd_una;
2430 }
2431 }
2432
2433 /*
2434 * Set retransmit timer if not currently set,
2435 * and not doing an ack or a keep-alive probe.
2436 */
2437 timer:
2438 if (tp->t_timer[TCPT_REXMT] == 0 &&
2439 ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
2440 tp->snd_nxt != tp->snd_una || (flags & TH_FIN))) {
2441 if (tp->t_timer[TCPT_PERSIST]) {
2442 tp->t_timer[TCPT_PERSIST] = 0;
2443 tp->t_persist_stop = 0;
2444 TCP_RESET_REXMT_STATE(tp);
2445 }
2446 tp->t_timer[TCPT_REXMT] =
2447 OFFSET_FROM_START(tp, tp->t_rxtcur);
2448 }
2449
2450 /*
2451 * Set tail loss probe timeout if new data is being
2452 * transmitted. This will be supported only when
2453 * SACK option is enabled on a connection.
2454 *
2455 * Every time new data is sent PTO will get reset.
2456 */
2457 if (tcp_enable_tlp && len != 0 && tp->t_state == TCPS_ESTABLISHED &&
2458 SACK_ENABLED(tp) && !IN_FASTRECOVERY(tp) &&
2459 tp->snd_nxt == tp->snd_max &&
2460 SEQ_GT(tp->snd_nxt, tp->snd_una) &&
2461 tp->t_rxtshift == 0 &&
2462 (tp->t_flagsext & (TF_SENT_TLPROBE | TF_PKTS_REORDERED)) == 0) {
2463 uint32_t pto, srtt;
2464
2465 if (tcp_do_better_lr) {
2466 srtt = tp->t_srtt >> TCP_RTT_SHIFT;
2467 pto = 2 * srtt;
2468 if ((tp->snd_max - tp->snd_una) <= tp->t_maxseg) {
2469 pto += tcp_delack;
2470 } else {
2471 pto += 2;
2472 }
2473 } else {
2474 /*
2475 * Using SRTT alone to set PTO can cause spurious
2476 * retransmissions on wireless networks where there
2477 * is a lot of variance in RTT. Taking variance
2478 * into account will avoid this.
2479 */
2480 srtt = tp->t_srtt >> TCP_RTT_SHIFT;
2481 pto = ((TCP_REXMTVAL(tp)) * 3) >> 1;
2482 pto = max(2 * srtt, pto);
2483 if ((tp->snd_max - tp->snd_una) == tp->t_maxseg) {
2484 pto = max(pto,
2485 (((3 * pto) >> 2) + tcp_delack * 2));
2486 } else {
2487 pto = max(10, pto);
2488 }
2489 }
2490
2491 /* if RTO is less than PTO, choose RTO instead */
2492 if (tp->t_rxtcur < pto) {
2493 pto = tp->t_rxtcur;
2494 }
2495
2496 tp->t_timer[TCPT_PTO] = OFFSET_FROM_START(tp, pto);
2497 }
2498 } else {
2499 /*
2500 * Persist case, update snd_max but since we are in
2501 * persist mode (no window) we do not update snd_nxt.
2502 */
2503 int xlen = len;
2504 if (flags & TH_SYN) {
2505 ++xlen;
2506 }
2507 if ((flags & TH_FIN) &&
2508 !(tp->t_flags & TF_SENTFIN)) {
2509 ++xlen;
2510 tp->t_flags |= TF_SENTFIN;
2511 }
2512 if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) {
2513 tp->snd_max = tp->snd_nxt + len;
2514 tp->t_sndtime = tcp_now;
2515 }
2516 }
2517
2518 #if TCPDEBUG
2519 /*
2520 * Trace.
2521 */
2522 if (so_options & SO_DEBUG) {
2523 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
2524 }
2525 #endif
2526
2527 /*
2528 * Fill in IP length and desired time to live and
2529 * send to IP level. There should be a better way
2530 * to handle ttl and tos; we could keep them in
2531 * the template, but need a way to checksum without them.
2532 */
2533 /*
2534 * m->m_pkthdr.len should have been set before cksum calcuration,
2535 * because in6_cksum() need it.
2536 */
2537 if (isipv6) {
2538 /*
2539 * we separately set hoplimit for every segment, since the
2540 * user might want to change the value via setsockopt.
2541 * Also, desired default hop limit might be changed via
2542 * Neighbor Discovery.
2543 */
2544 ip6->ip6_hlim = in6_selecthlim(inp, inp->in6p_route.ro_rt ?
2545 inp->in6p_route.ro_rt->rt_ifp : NULL);
2546
2547 /* TODO: IPv6 IP6TOS_ECT bit on */
2548 KERNEL_DEBUG(DBG_LAYER_BEG,
2549 ((inp->inp_fport << 16) | inp->inp_lport),
2550 (((inp->in6p_laddr.s6_addr16[0] & 0xffff) << 16) |
2551 (inp->in6p_faddr.s6_addr16[0] & 0xffff)),
2552 sendalot, 0, 0);
2553 } else {
2554 ASSERT(m->m_pkthdr.len <= IP_MAXPACKET);
2555 ip->ip_len = (u_short)m->m_pkthdr.len;
2556 ip->ip_ttl = inp->inp_ip_ttl; /* XXX */
2557 ip->ip_tos |= (inp->inp_ip_tos & ~IPTOS_ECN_MASK);/* XXX */
2558 KERNEL_DEBUG(DBG_LAYER_BEG,
2559 ((inp->inp_fport << 16) | inp->inp_lport),
2560 (((inp->inp_laddr.s_addr & 0xffff) << 16) |
2561 (inp->inp_faddr.s_addr & 0xffff)), 0, 0, 0);
2562 }
2563
2564 /*
2565 * See if we should do MTU discovery.
2566 * Look at the flag updated on the following criterias:
2567 * 1) Path MTU discovery is authorized by the sysctl
2568 * 2) The route isn't set yet (unlikely but could happen)
2569 * 3) The route is up
2570 * 4) the MTU is not locked (if it is, then discovery has been
2571 * disabled for that route)
2572 */
2573 if (!isipv6) {
2574 if (path_mtu_discovery && (tp->t_flags & TF_PMTUD)) {
2575 ip->ip_off |= IP_DF;
2576 }
2577 }
2578
2579 #if NECP
2580 {
2581 necp_kernel_policy_id policy_id;
2582 necp_kernel_policy_id skip_policy_id;
2583 u_int32_t route_rule_id;
2584 u_int32_t pass_flags;
2585 if (!necp_socket_is_allowed_to_send_recv(inp, NULL, 0, &policy_id, &route_rule_id, &skip_policy_id, &pass_flags)) {
2586 TCP_LOG_DROP_NECP(isipv6 ? (void *)ip6 : (void *)ip, th, tp, true);
2587 m_freem(m);
2588 error = EHOSTUNREACH;
2589 goto out;
2590 }
2591 necp_mark_packet_from_socket(m, inp, policy_id, route_rule_id, skip_policy_id, pass_flags);
2592
2593 if (net_qos_policy_restricted != 0) {
2594 necp_socket_update_qos_marking(inp, inp->inp_route.ro_rt, route_rule_id);
2595 }
2596 }
2597 #endif /* NECP */
2598
2599 #if IPSEC
2600 if (inp->inp_sp != NULL) {
2601 ipsec_setsocket(m, so);
2602 }
2603 #endif /*IPSEC*/
2604
2605 /*
2606 * The socket is kept locked while sending out packets in ip_output, even if packet chaining is not active.
2607 */
2608 lost = 0;
2609
2610 /*
2611 * Embed the flow hash in pkt hdr and mark the packet as
2612 * capable of flow controlling
2613 */
2614 m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
2615 m->m_pkthdr.pkt_flowid = inp->inp_flowhash;
2616 m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC | PKTF_FLOW_ADV);
2617 m->m_pkthdr.pkt_proto = IPPROTO_TCP;
2618 m->m_pkthdr.tx_tcp_pid = so->last_pid;
2619 if (so->so_flags & SOF_DELEGATED) {
2620 m->m_pkthdr.tx_tcp_e_pid = so->e_pid;
2621 } else {
2622 m->m_pkthdr.tx_tcp_e_pid = 0;
2623 }
2624
2625 m->m_nextpkt = NULL;
2626
2627 if (inp->inp_last_outifp != NULL &&
2628 !(inp->inp_last_outifp->if_flags & IFF_LOOPBACK)) {
2629 /* Hint to prioritize this packet if
2630 * 1. if the packet has no data
2631 * 2. the interface supports transmit-start model and did
2632 * not disable ACK prioritization.
2633 * 3. Only ACK flag is set.
2634 * 4. there is no outstanding data on this connection.
2635 */
2636 if (len == 0 && (inp->inp_last_outifp->if_eflags & (IFEF_TXSTART | IFEF_NOACKPRI)) == IFEF_TXSTART) {
2637 if (th->th_flags == TH_ACK &&
2638 tp->snd_una == tp->snd_max &&
2639 tp->t_timer[TCPT_REXMT] == 0) {
2640 svc_flags |= PKT_SCF_TCP_ACK;
2641 }
2642 if (th->th_flags & TH_SYN) {
2643 svc_flags |= PKT_SCF_TCP_SYN;
2644 }
2645 }
2646 set_packet_service_class(m, so, sotc, svc_flags);
2647 } else {
2648 /*
2649 * Optimization for loopback just set the mbuf
2650 * service class
2651 */
2652 (void) m_set_service_class(m, so_tc2msc(sotc));
2653 }
2654
2655 TCP_LOG_TH_FLAGS(isipv6 ? (void *)ip6 : (void *)ip, th, tp, true,
2656 inp->inp_last_outifp != NULL ? inp->inp_last_outifp :
2657 inp->inp_boundifp);
2658
2659 tp->t_pktlist_sentlen += len;
2660 tp->t_lastchain++;
2661
2662 if (isipv6) {
2663 DTRACE_TCP5(send, struct mbuf *, m, struct inpcb *, inp,
2664 struct ip6 *, ip6, struct tcpcb *, tp, struct tcphdr *,
2665 th);
2666 } else {
2667 DTRACE_TCP5(send, struct mbuf *, m, struct inpcb *, inp,
2668 struct ip *, ip, struct tcpcb *, tp, struct tcphdr *, th);
2669 }
2670
2671 if (tp->t_pktlist_head != NULL) {
2672 tp->t_pktlist_tail->m_nextpkt = m;
2673 tp->t_pktlist_tail = m;
2674 } else {
2675 packchain_newlist++;
2676 tp->t_pktlist_head = tp->t_pktlist_tail = m;
2677 }
2678
2679 if (sendalot == 0 || (tp->t_state != TCPS_ESTABLISHED) ||
2680 (tp->snd_cwnd <= (tp->snd_wnd / 8)) ||
2681 (tp->t_flags & TF_ACKNOW) ||
2682 (tp->t_flagsext & TF_FORCE) ||
2683 tp->t_lastchain >= tcp_packet_chaining) {
2684 error = 0;
2685 while (inp->inp_sndinprog_cnt == 0 &&
2686 tp->t_pktlist_head != NULL) {
2687 packetlist = tp->t_pktlist_head;
2688 packchain_listadd = tp->t_lastchain;
2689 packchain_sent++;
2690 lost = tp->t_pktlist_sentlen;
2691 TCP_PKTLIST_CLEAR(tp);
2692
2693 error = tcp_ip_output(so, tp, packetlist,
2694 packchain_listadd, tp_inp_options,
2695 (so_options & SO_DONTROUTE),
2696 (sack_rxmit || (sack_bytes_rxmt != 0)), isipv6);
2697 if (error) {
2698 /*
2699 * Take into account the rest of unsent
2700 * packets in the packet list for this tcp
2701 * into "lost", since we're about to free
2702 * the whole list below.
2703 */
2704 lost += tp->t_pktlist_sentlen;
2705 break;
2706 } else {
2707 lost = 0;
2708 }
2709 }
2710 /* tcp was closed while we were in ip; resume close */
2711 if (inp->inp_sndinprog_cnt == 0 &&
2712 (tp->t_flags & TF_CLOSING)) {
2713 tp->t_flags &= ~TF_CLOSING;
2714 (void) tcp_close(tp);
2715 return 0;
2716 }
2717 } else {
2718 error = 0;
2719 packchain_looped++;
2720 tcpstat.tcps_sndtotal++;
2721
2722 goto again;
2723 }
2724 if (error) {
2725 /*
2726 * Assume that the packets were lost, so back out the
2727 * sequence number advance, if any. Note that the "lost"
2728 * variable represents the amount of user data sent during
2729 * the recent call to ip_output_list() plus the amount of
2730 * user data in the packet list for this tcp at the moment.
2731 */
2732 if (!(tp->t_flagsext & TF_FORCE)
2733 || tp->t_timer[TCPT_PERSIST] == 0) {
2734 /*
2735 * No need to check for TH_FIN here because
2736 * the TF_SENTFIN flag handles that case.
2737 */
2738 if ((flags & TH_SYN) == 0) {
2739 if (sack_rxmit) {
2740 if (SEQ_GT((p->rxmit - lost),
2741 tp->snd_una)) {
2742 p->rxmit -= lost;
2743
2744 if (SEQ_LT(p->rxmit, p->start)) {
2745 p->rxmit = p->start;
2746 }
2747 } else {
2748 lost = p->rxmit - tp->snd_una;
2749 p->rxmit = tp->snd_una;
2750
2751 if (SEQ_LT(p->rxmit, p->start)) {
2752 p->rxmit = p->start;
2753 }
2754 }
2755 tp->sackhint.sack_bytes_rexmit -= lost;
2756 if (tp->sackhint.sack_bytes_rexmit < 0) {
2757 tp->sackhint.sack_bytes_rexmit = 0;
2758 }
2759 } else {
2760 if (SEQ_GT((tp->snd_nxt - lost),
2761 tp->snd_una)) {
2762 tp->snd_nxt -= lost;
2763 } else {
2764 tp->snd_nxt = tp->snd_una;
2765 }
2766 }
2767 }
2768 }
2769 out:
2770 if (tp->t_pktlist_head != NULL) {
2771 m_freem_list(tp->t_pktlist_head);
2772 }
2773 TCP_PKTLIST_CLEAR(tp);
2774
2775 if (error == ENOBUFS) {
2776 /*
2777 * Set retransmit timer if not currently set
2778 * when we failed to send a segment that can be
2779 * retransmitted (i.e. not pure ack or rst)
2780 */
2781 if (tp->t_timer[TCPT_REXMT] == 0 &&
2782 tp->t_timer[TCPT_PERSIST] == 0 &&
2783 (len != 0 || (flags & (TH_SYN | TH_FIN)) != 0 ||
2784 so->so_snd.sb_cc > 0)) {
2785 tp->t_timer[TCPT_REXMT] =
2786 OFFSET_FROM_START(tp, tp->t_rxtcur);
2787 }
2788 tp->snd_cwnd = tp->t_maxseg;
2789 tp->t_bytes_acked = 0;
2790 tcp_check_timer_state(tp);
2791 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
2792
2793 tcp_ccdbg_trace(tp, NULL, TCP_CC_OUTPUT_ERROR);
2794 return 0;
2795 }
2796 if (error == EMSGSIZE) {
2797 /*
2798 * ip_output() will have already fixed the route
2799 * for us. tcp_mtudisc() will, as its last action,
2800 * initiate retransmission, so it is important to
2801 * not do so here.
2802 *
2803 * If TSO was active we either got an interface
2804 * without TSO capabilits or TSO was turned off.
2805 * Disable it for this connection as too and
2806 * immediatly retry with MSS sized segments generated
2807 * by this function.
2808 */
2809 if (tso) {
2810 tp->t_flags &= ~TF_TSO;
2811 }
2812
2813 tcp_mtudisc(inp, 0);
2814 tcp_check_timer_state(tp);
2815
2816 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
2817 return 0;
2818 }
2819 /*
2820 * Unless this is due to interface restriction policy,
2821 * treat EHOSTUNREACH/ENETDOWN/EADDRNOTAVAIL as a soft error.
2822 */
2823 if ((error == EHOSTUNREACH || error == ENETDOWN || error == EADDRNOTAVAIL) &&
2824 TCPS_HAVERCVDSYN(tp->t_state) &&
2825 !inp_restricted_send(inp, inp->inp_last_outifp)) {
2826 tp->t_softerror = error;
2827 error = 0;
2828 }
2829 tcp_check_timer_state(tp);
2830 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
2831 return error;
2832 }
2833
2834 tcpstat.tcps_sndtotal++;
2835
2836 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
2837 if (sendalot) {
2838 goto again;
2839 }
2840
2841 tcp_check_timer_state(tp);
2842
2843 return 0;
2844 }
2845
2846 static int
2847 tcp_ip_output(struct socket *so, struct tcpcb *tp, struct mbuf *pkt,
2848 int cnt, struct mbuf *opt, int flags, int sack_in_progress, boolean_t isipv6)
2849 {
2850 int error = 0;
2851 boolean_t chain;
2852 boolean_t unlocked = FALSE;
2853 boolean_t ifdenied = FALSE;
2854 struct inpcb *inp = tp->t_inpcb;
2855 struct ip_out_args ipoa;
2856 struct route ro;
2857 struct ifnet *outif = NULL;
2858 bool check_qos_marking_again = (so->so_flags1 & SOF1_QOSMARKING_POLICY_OVERRIDE) ? FALSE : TRUE;
2859
2860 bzero(&ipoa, sizeof(ipoa));
2861 ipoa.ipoa_boundif = IFSCOPE_NONE;
2862 ipoa.ipoa_flags = IPOAF_SELECT_SRCIF | IPOAF_BOUND_SRCADDR;
2863 ipoa.ipoa_sotc = SO_TC_UNSPEC;
2864 ipoa.ipoa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
2865 struct ip6_out_args ip6oa;
2866 struct route_in6 ro6;
2867
2868 bzero(&ip6oa, sizeof(ip6oa));
2869 ip6oa.ip6oa_boundif = IFSCOPE_NONE;
2870 ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR;
2871 ip6oa.ip6oa_sotc = SO_TC_UNSPEC;
2872 ip6oa.ip6oa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
2873
2874 struct flowadv *adv =
2875 (isipv6 ? &ip6oa.ip6oa_flowadv : &ipoa.ipoa_flowadv);
2876
2877 /* If socket was bound to an ifindex, tell ip_output about it */
2878 if (inp->inp_flags & INP_BOUND_IF) {
2879 if (isipv6) {
2880 ip6oa.ip6oa_boundif = inp->inp_boundifp->if_index;
2881 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
2882 } else {
2883 ipoa.ipoa_boundif = inp->inp_boundifp->if_index;
2884 ipoa.ipoa_flags |= IPOAF_BOUND_IF;
2885 }
2886 }
2887
2888 if (INP_NO_CELLULAR(inp)) {
2889 if (isipv6) {
2890 ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR;
2891 } else {
2892 ipoa.ipoa_flags |= IPOAF_NO_CELLULAR;
2893 }
2894 }
2895 if (INP_NO_EXPENSIVE(inp)) {
2896 if (isipv6) {
2897 ip6oa.ip6oa_flags |= IP6OAF_NO_EXPENSIVE;
2898 } else {
2899 ipoa.ipoa_flags |= IPOAF_NO_EXPENSIVE;
2900 }
2901 }
2902 if (INP_NO_CONSTRAINED(inp)) {
2903 if (isipv6) {
2904 ip6oa.ip6oa_flags |= IP6OAF_NO_CONSTRAINED;
2905 } else {
2906 ipoa.ipoa_flags |= IPOAF_NO_CONSTRAINED;
2907 }
2908 }
2909 if (INP_AWDL_UNRESTRICTED(inp)) {
2910 if (isipv6) {
2911 ip6oa.ip6oa_flags |= IP6OAF_AWDL_UNRESTRICTED;
2912 } else {
2913 ipoa.ipoa_flags |= IPOAF_AWDL_UNRESTRICTED;
2914 }
2915 }
2916 if (INP_INTCOPROC_ALLOWED(inp) && isipv6) {
2917 ip6oa.ip6oa_flags |= IP6OAF_INTCOPROC_ALLOWED;
2918 }
2919 if (isipv6) {
2920 ip6oa.ip6oa_sotc = so->so_traffic_class;
2921 ip6oa.ip6oa_netsvctype = so->so_netsvctype;
2922 ip6oa.qos_marking_gencount = inp->inp_policyresult.results.qos_marking_gencount;
2923 } else {
2924 ipoa.ipoa_sotc = so->so_traffic_class;
2925 ipoa.ipoa_netsvctype = so->so_netsvctype;
2926 ipoa.qos_marking_gencount = inp->inp_policyresult.results.qos_marking_gencount;
2927 }
2928 if ((so->so_flags1 & SOF1_QOSMARKING_ALLOWED)) {
2929 if (isipv6) {
2930 ip6oa.ip6oa_flags |= IP6OAF_QOSMARKING_ALLOWED;
2931 } else {
2932 ipoa.ipoa_flags |= IPOAF_QOSMARKING_ALLOWED;
2933 }
2934 }
2935 if (check_qos_marking_again) {
2936 if (isipv6) {
2937 ip6oa.ip6oa_flags |= IP6OAF_REDO_QOSMARKING_POLICY;
2938 } else {
2939 ipoa.ipoa_flags |= IPOAF_REDO_QOSMARKING_POLICY;
2940 }
2941 }
2942 if (isipv6) {
2943 flags |= IPV6_OUTARGS;
2944 } else {
2945 flags |= IP_OUTARGS;
2946 }
2947
2948 /* Copy the cached route and take an extra reference */
2949 if (isipv6) {
2950 in6p_route_copyout(inp, &ro6);
2951 } else {
2952 inp_route_copyout(inp, &ro);
2953 }
2954
2955 /*
2956 * Make sure ACK/DELACK conditions are cleared before
2957 * we unlock the socket.
2958 */
2959 tp->last_ack_sent = tp->rcv_nxt;
2960 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
2961 tp->t_timer[TCPT_DELACK] = 0;
2962 tp->t_unacksegs = 0;
2963
2964 /* Increment the count of outstanding send operations */
2965 inp->inp_sndinprog_cnt++;
2966
2967 /*
2968 * If allowed, unlock TCP socket while in IP
2969 * but only if the connection is established and
2970 * in a normal mode where reentrancy on the tcpcb won't be
2971 * an issue:
2972 * - there is no SACK episode
2973 * - we're not in Fast Recovery mode
2974 * - if we're not sending from an upcall.
2975 */
2976 if (tcp_output_unlocked && !so->so_upcallusecount &&
2977 (tp->t_state == TCPS_ESTABLISHED) && (sack_in_progress == 0) &&
2978 !IN_FASTRECOVERY(tp) && !(so->so_flags & SOF_MP_SUBFLOW)) {
2979 unlocked = TRUE;
2980 socket_unlock(so, 0);
2981 }
2982
2983 /*
2984 * Don't send down a chain of packets when:
2985 * - TCP chaining is disabled
2986 * - there is an IPsec rule set
2987 * - there is a non default rule set for the firewall
2988 */
2989
2990 chain = tcp_packet_chaining > 1
2991 #if IPSEC
2992 && ipsec_bypass
2993 #endif
2994 ; // I'm important, not extraneous
2995
2996 while (pkt != NULL) {
2997 struct mbuf *npkt = pkt->m_nextpkt;
2998
2999 if (!chain) {
3000 pkt->m_nextpkt = NULL;
3001 /*
3002 * If we are not chaining, make sure to set the packet
3003 * list count to 0 so that IP takes the right path;
3004 * this is important for cases such as IPsec where a
3005 * single mbuf might result in multiple mbufs as part
3006 * of the encapsulation. If a non-zero count is passed
3007 * down to IP, the head of the chain might change and
3008 * we could end up skipping it (thus generating bogus
3009 * packets). Fixing it in IP would be desirable, but
3010 * for now this would do it.
3011 */
3012 cnt = 0;
3013 }
3014 if (isipv6) {
3015 error = ip6_output_list(pkt, cnt,
3016 inp->in6p_outputopts, &ro6, flags, NULL, NULL,
3017 &ip6oa);
3018 ifdenied = (ip6oa.ip6oa_retflags & IP6OARF_IFDENIED);
3019 } else {
3020 error = ip_output_list(pkt, cnt, opt, &ro, flags, NULL,
3021 &ipoa);
3022 ifdenied = (ipoa.ipoa_retflags & IPOARF_IFDENIED);
3023 }
3024
3025 if (chain || error) {
3026 /*
3027 * If we sent down a chain then we are done since
3028 * the callee had taken care of everything; else
3029 * we need to free the rest of the chain ourselves.
3030 */
3031 if (!chain) {
3032 m_freem_list(npkt);
3033 }
3034 break;
3035 }
3036 pkt = npkt;
3037 }
3038
3039 if (unlocked) {
3040 socket_lock(so, 0);
3041 }
3042
3043 /*
3044 * Enter flow controlled state if the connection is established
3045 * and is not in recovery. Flow control is allowed only if there
3046 * is outstanding data.
3047 *
3048 * A connection will enter suspended state even if it is in
3049 * recovery.
3050 */
3051 if (((adv->code == FADV_FLOW_CONTROLLED && !IN_FASTRECOVERY(tp)) ||
3052 adv->code == FADV_SUSPENDED) &&
3053 !(tp->t_flags & TF_CLOSING) &&
3054 tp->t_state == TCPS_ESTABLISHED &&
3055 SEQ_GT(tp->snd_max, tp->snd_una)) {
3056 int rc;
3057 rc = inp_set_fc_state(inp, adv->code);
3058
3059 if (rc == 1) {
3060 tcp_ccdbg_trace(tp, NULL,
3061 ((adv->code == FADV_FLOW_CONTROLLED) ?
3062 TCP_CC_FLOW_CONTROL : TCP_CC_SUSPEND));
3063 }
3064 }
3065
3066 /*
3067 * When an interface queue gets suspended, some of the
3068 * packets are dropped. Return ENOBUFS, to update the
3069 * pcb state.
3070 */
3071 if (adv->code == FADV_SUSPENDED) {
3072 error = ENOBUFS;
3073 }
3074
3075 VERIFY(inp->inp_sndinprog_cnt > 0);
3076 if (--inp->inp_sndinprog_cnt == 0) {
3077 inp->inp_flags &= ~(INP_FC_FEEDBACK);
3078 if (inp->inp_sndingprog_waiters > 0) {
3079 wakeup(&inp->inp_sndinprog_cnt);
3080 }
3081 }
3082
3083 if (isipv6) {
3084 /*
3085 * When an NECP IP tunnel policy forces the outbound interface,
3086 * ip6_output_list() informs the transport layer what is the actual
3087 * outgoing interface
3088 */
3089 if (ip6oa.ip6oa_flags & IP6OAF_BOUND_IF) {
3090 outif = ifindex2ifnet[ip6oa.ip6oa_boundif];
3091 } else if (ro6.ro_rt != NULL) {
3092 outif = ro6.ro_rt->rt_ifp;
3093 }
3094 } else {
3095 if (ro.ro_rt != NULL) {
3096 outif = ro.ro_rt->rt_ifp;
3097 }
3098 }
3099 if (check_qos_marking_again) {
3100 uint32_t qos_marking_gencount;
3101 bool allow_qos_marking;
3102 if (isipv6) {
3103 qos_marking_gencount = ip6oa.qos_marking_gencount;
3104 allow_qos_marking = ip6oa.ip6oa_flags & IP6OAF_QOSMARKING_ALLOWED ? TRUE : FALSE;
3105 } else {
3106 qos_marking_gencount = ipoa.qos_marking_gencount;
3107 allow_qos_marking = ipoa.ipoa_flags & IPOAF_QOSMARKING_ALLOWED ? TRUE : FALSE;
3108 }
3109 inp->inp_policyresult.results.qos_marking_gencount = qos_marking_gencount;
3110 if (allow_qos_marking == TRUE) {
3111 inp->inp_socket->so_flags1 |= SOF1_QOSMARKING_ALLOWED;
3112 } else {
3113 inp->inp_socket->so_flags1 &= ~SOF1_QOSMARKING_ALLOWED;
3114 }
3115 }
3116
3117 if (outif != NULL && outif != inp->inp_last_outifp) {
3118 /* Update the send byte count */
3119 if (so->so_snd.sb_cc > 0 && so->so_snd.sb_flags & SB_SNDBYTE_CNT) {
3120 inp_decr_sndbytes_total(so, so->so_snd.sb_cc);
3121 inp_decr_sndbytes_allunsent(so, tp->snd_una);
3122 so->so_snd.sb_flags &= ~SB_SNDBYTE_CNT;
3123 }
3124 inp->inp_last_outifp = outif;
3125 }
3126
3127 if (error != 0 && ifdenied &&
3128 (INP_NO_CELLULAR(inp) || INP_NO_EXPENSIVE(inp) || INP_NO_CONSTRAINED(inp))) {
3129 soevent(so,
3130 (SO_FILT_HINT_LOCKED | SO_FILT_HINT_IFDENIED));
3131 }
3132
3133 /* Synchronize cached PCB route & options */
3134 if (isipv6) {
3135 in6p_route_copyin(inp, &ro6);
3136 } else {
3137 inp_route_copyin(inp, &ro);
3138 }
3139
3140 if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift == 0 &&
3141 tp->t_inpcb->inp_route.ro_rt != NULL) {
3142 /* If we found the route and there is an rtt on it
3143 * reset the retransmit timer
3144 */
3145 tcp_getrt_rtt(tp, tp->t_inpcb->in6p_route.ro_rt);
3146 tp->t_timer[TCPT_REXMT] = OFFSET_FROM_START(tp, tp->t_rxtcur);
3147 }
3148 return error;
3149 }
3150
3151 int tcptv_persmin_val = TCPTV_PERSMIN;
3152
3153 void
3154 tcp_setpersist(struct tcpcb *tp)
3155 {
3156 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
3157
3158 /* If a PERSIST_TIMER option was set we will limit the
3159 * time the persist timer will be active for that connection
3160 * in order to avoid DOS by using zero window probes.
3161 * see rdar://5805356
3162 */
3163
3164 if (tp->t_persist_timeout != 0 &&
3165 tp->t_timer[TCPT_PERSIST] == 0 &&
3166 tp->t_persist_stop == 0) {
3167 tp->t_persist_stop = tcp_now + tp->t_persist_timeout;
3168 }
3169
3170 /*
3171 * Start/restart persistance timer.
3172 */
3173 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
3174 t * tcp_backoff[tp->t_rxtshift],
3175 tcptv_persmin_val, TCPTV_PERSMAX, 0);
3176 tp->t_timer[TCPT_PERSIST] = OFFSET_FROM_START(tp, tp->t_timer[TCPT_PERSIST]);
3177
3178 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) {
3179 tp->t_rxtshift++;
3180 }
3181 }
3182
3183 static int
3184 tcp_recv_throttle(struct tcpcb *tp)
3185 {
3186 uint32_t base_rtt, newsize;
3187 struct sockbuf *sbrcv = &tp->t_inpcb->inp_socket->so_rcv;
3188
3189 if (tcp_use_rtt_recvbg == 1 &&
3190 TSTMP_SUPPORTED(tp)) {
3191 /*
3192 * Timestamps are supported on this connection. Use
3193 * RTT to look for an increase in latency.
3194 */
3195
3196 /*
3197 * If the connection is already being throttled, leave it
3198 * in that state until rtt comes closer to base rtt
3199 */
3200 if (tp->t_flagsext & TF_RECV_THROTTLE) {
3201 return 1;
3202 }
3203
3204 base_rtt = get_base_rtt(tp);
3205
3206 if (base_rtt != 0 && tp->t_rttcur != 0) {
3207 /*
3208 * if latency increased on a background flow,
3209 * return 1 to start throttling.
3210 */
3211 if (tp->t_rttcur > (base_rtt + target_qdelay)) {
3212 tp->t_flagsext |= TF_RECV_THROTTLE;
3213 if (tp->t_recv_throttle_ts == 0) {
3214 tp->t_recv_throttle_ts = tcp_now;
3215 }
3216 /*
3217 * Reduce the recv socket buffer size to
3218 * minimize latecy.
3219 */
3220 if (sbrcv->sb_idealsize >
3221 tcp_recv_throttle_minwin) {
3222 newsize = sbrcv->sb_idealsize >> 1;
3223 /* Set a minimum of 16 K */
3224 newsize =
3225 max(newsize,
3226 tcp_recv_throttle_minwin);
3227 sbrcv->sb_idealsize = newsize;
3228 }
3229 return 1;
3230 } else {
3231 return 0;
3232 }
3233 }
3234 }
3235
3236 /*
3237 * Timestamps are not supported or there is no good RTT
3238 * measurement. Use IPDV in this case.
3239 */
3240 if (tp->acc_iaj > tcp_acc_iaj_react_limit) {
3241 return 1;
3242 }
3243
3244 return 0;
3245 }