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