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