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