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