2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
58 * $FreeBSD: src/sys/netinet/tcp_output.c,v 1.39.2.10 2001/07/07 04:30:38 silby Exp $
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/sysctl.h>
69 #include <sys/domain.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
74 #include <net/route.h>
76 #include <netinet/in.h>
77 #include <netinet/in_systm.h>
78 #include <netinet/ip.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/ip_var.h>
82 #include <netinet6/in6_pcb.h>
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_var.h>
86 #include <netinet/tcp.h>
88 #include <netinet/tcp_fsm.h>
89 #include <netinet/tcp_seq.h>
90 #include <netinet/tcp_timer.h>
91 #include <netinet/tcp_var.h>
92 #include <netinet/tcpip.h>
94 #include <netinet/tcp_debug.h>
96 #include <sys/kdebug.h>
99 #include <netinet6/ipsec.h>
102 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 1)
103 #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 3)
104 #define DBG_FNC_TCP_OUTPUT NETDBG_CODE(DBG_NETTCP, (4 << 8) | 1)
108 extern struct mbuf
*m_copypack();
111 static int path_mtu_discovery
= 1;
112 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, path_mtu_discovery
, CTLFLAG_RW
,
113 &path_mtu_discovery
, 1, "Enable Path MTU Discovery");
116 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, slowstart_flightsize
, CTLFLAG_RW
,
117 &ss_fltsz
, 1, "Slow start flight size");
119 int ss_fltsz_local
= 4; /* starts with four segments max */
120 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, local_slowstart_flightsize
, CTLFLAG_RW
,
121 &ss_fltsz_local
, 1, "Slow start flight size for local networks");
123 int tcp_do_newreno
= 0;
124 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, newreno
, CTLFLAG_RW
, &tcp_do_newreno
,
125 0, "Enable NewReno Algorithms");
127 struct mbuf
*m_copym_with_hdrs
__P((struct mbuf
*, int, int, int, struct mbuf
**, int*));
130 /* temporary: for testing */
132 extern int ipsec_bypass
;
135 extern int slowlink_wsize
; /* window correction for slow links */
138 * Tcp output routine: figure out what should be sent and send it.
142 register struct tcpcb
*tp
;
144 register struct socket
*so
= tp
->t_inpcb
->inp_socket
;
145 register long len
, win
;
146 int off
, flags
, error
;
147 register struct mbuf
*m
;
148 struct ip
*ip
= NULL
;
149 register struct ipovly
*ipov
= NULL
;
151 struct ip6_hdr
*ip6
= NULL
;
153 register struct tcphdr
*th
;
154 u_char opt
[TCP_MAXOLEN
];
155 unsigned ipoptlen
, optlen
, hdrlen
;
157 int maxburst
= TCP_MAXBURST
;
158 struct rmxp_tao
*taop
;
159 struct rmxp_tao tao_noncached
;
165 struct mbuf
*m_last
= 0;
166 struct mbuf
*m_head
= 0;
169 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
171 if (isipv6
= ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0)) {
173 KERNEL_DEBUG(DBG_LAYER_BEG
,
174 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
175 (((tp
->t_inpcb
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
176 (tp
->t_inpcb
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
183 KERNEL_DEBUG(DBG_LAYER_BEG
,
184 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
185 (((tp
->t_inpcb
->inp_laddr
.s_addr
& 0xffff) << 16) |
186 (tp
->t_inpcb
->inp_faddr
.s_addr
& 0xffff)),
190 * Determine length of data that should be transmitted,
191 * and flags that will be used.
192 * If there is some data or critical controls (SYN, RST)
193 * to send, then transmit; otherwise, investigate further.
195 idle
= (tp
->snd_max
== tp
->snd_una
);
197 if (idle
&& tp
->t_rcvtime
>= tp
->t_rxtcur
) {
199 if (idle
&& (ticks
- tp
->t_rcvtime
) >= tp
->t_rxtcur
) {
202 * We have been idle for "a while" and no acks are
203 * expected to clock out any data we send --
204 * slow start to get ack "clock" running again.
206 * Set the slow-start flight size depending on whether
207 * this is a local network or not.
211 (isipv6
&& in6_localaddr(&tp
->t_inpcb
->in6p_faddr
)) ||
214 in_localaddr(tp
->t_inpcb
->inp_faddr
)
219 tp
->snd_cwnd
= tp
->t_maxseg
* ss_fltsz_local
;
221 tp
->snd_cwnd
= tp
->t_maxseg
* ss_fltsz
;
225 off
= tp
->snd_nxt
- tp
->snd_una
;
226 win
= min(tp
->snd_wnd
, tp
->snd_cwnd
);
227 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0)
228 win
= min(win
, slowlink_wsize
);
230 flags
= tcp_outflags
[tp
->t_state
];
232 * Get standard flags, and add SYN or FIN if requested by 'hidden'
235 if (tp
->t_flags
& TF_NEEDFIN
)
237 if (tp
->t_flags
& TF_NEEDSYN
)
241 * If in persist timeout with window of 0, send 1 byte.
242 * Otherwise, if window is small but nonzero
243 * and timer expired, we will send what we can
244 * and go to transmit state.
249 * If we still have some data to send, then
250 * clear the FIN bit. Usually this would
251 * happen below when it realizes that we
252 * aren't sending all the data. However,
253 * if we have exactly 1 byte of unsent data,
254 * then it won't clear the FIN bit below,
255 * and if we are in persist state, we wind
256 * up sending the packet without recording
257 * that we sent the FIN bit.
259 * We can't just blindly clear the FIN bit,
260 * because if we don't have any more data
261 * to send then the probe will be the FIN
264 if (off
< so
->so_snd
.sb_cc
)
268 tp
->t_timer
[TCPT_PERSIST
] = 0;
273 len
= (long)ulmin(so
->so_snd
.sb_cc
, win
) - off
;
275 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
276 taop
= &tao_noncached
;
277 bzero(taop
, sizeof(*taop
));
281 * Lop off SYN bit if it has already been sent. However, if this
282 * is SYN-SENT state and if segment contains data and if we don't
283 * know that foreign host supports TAO, suppress sending segment.
285 if ((flags
& TH_SYN
) && SEQ_GT(tp
->snd_nxt
, tp
->snd_una
)) {
288 if (len
> 0 && tp
->t_state
== TCPS_SYN_SENT
&&
289 taop
->tao_ccsent
== 0) {
290 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
296 * Be careful not to send data and/or FIN on SYN segments
297 * in cases when no CC option will be sent.
298 * This measure is needed to prevent interoperability problems
299 * with not fully conformant TCP implementations.
301 if ((flags
& TH_SYN
) &&
302 ((tp
->t_flags
& TF_NOOPT
) || !(tp
->t_flags
& TF_REQ_CC
) ||
303 ((flags
& TH_ACK
) && !(tp
->t_flags
& TF_RCVD_CC
)))) {
310 * If FIN has been sent but not acked,
311 * but we haven't been called to retransmit,
312 * len will be -1. Otherwise, window shrank
313 * after we sent into it. If window shrank to 0,
314 * cancel pending retransmit, pull snd_nxt back
315 * to (closed) window, and set the persist timer
316 * if it isn't already going. If the window didn't
317 * close completely, just wait for an ACK.
321 tp
->t_timer
[TCPT_REXMT
] = 0;
323 tp
->snd_nxt
= tp
->snd_una
;
324 if (tp
->t_timer
[TCPT_PERSIST
] == 0)
328 if (len
> tp
->t_maxseg
) {
332 if (SEQ_LT(tp
->snd_nxt
+ len
, tp
->snd_una
+ so
->so_snd
.sb_cc
))
335 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0 ) /* Clips window size for slow links */
336 win
= min(sbspace(&so
->so_rcv
), slowlink_wsize
);
338 win
= sbspace(&so
->so_rcv
);
341 * Sender silly window avoidance. If connection is idle
342 * and can send all data, a maximum segment,
343 * at least a maximum default-size segment do it,
344 * or are forced, do it; otherwise don't bother.
345 * If peer's buffer is tiny, then send
346 * when window is at least half open.
347 * If retransmitting (possibly after persist timer forced us
348 * to send into a small window), then must resend.
351 if (len
== tp
->t_maxseg
)
353 if (!(tp
->t_flags
& TF_MORETOCOME
) &&
354 (idle
|| tp
->t_flags
& TF_NODELAY
) &&
355 (tp
->t_flags
& TF_NOPUSH
) == 0 &&
356 len
+ off
>= so
->so_snd
.sb_cc
)
360 if (len
>= tp
->max_sndwnd
/ 2 && tp
->max_sndwnd
> 0)
362 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
))
367 * Compare available window to amount of window
368 * known to peer (as advertised window less
369 * next expected input). If the difference is at least two
370 * max size segments, or at least 50% of the maximum possible
371 * window, then want to send a window update to peer.
375 * "adv" is the amount we can increase the window,
376 * taking into account that we are limited by
377 * TCP_MAXWIN << tp->rcv_scale.
379 long adv
= min(win
, (long)TCP_MAXWIN
<< tp
->rcv_scale
) -
380 (tp
->rcv_adv
- tp
->rcv_nxt
);
382 if (adv
>= (long) (2 * tp
->t_maxseg
))
384 if (2 * adv
>= (long) so
->so_rcv
.sb_hiwat
)
389 * Send if we owe peer an ACK.
391 if (tp
->t_flags
& TF_ACKNOW
)
393 if ((flags
& TH_RST
) ||
394 ((flags
& TH_SYN
) && (tp
->t_flags
& TF_NEEDSYN
) == 0))
396 if (SEQ_GT(tp
->snd_up
, tp
->snd_una
))
399 * If our state indicates that FIN should be sent
400 * and we have not yet done so, or we're retransmitting the FIN,
401 * then we need to send.
403 if (flags
& TH_FIN
&&
404 ((tp
->t_flags
& TF_SENTFIN
) == 0 || tp
->snd_nxt
== tp
->snd_una
))
408 * TCP window updates are not reliable, rather a polling protocol
409 * using ``persist'' packets is used to insure receipt of window
410 * updates. The three ``states'' for the output side are:
411 * idle not doing retransmits or persists
412 * persisting to move a small or zero window
413 * (re)transmitting and thereby not persisting
415 * tp->t_timer[TCPT_PERSIST]
416 * is set when we are in persist state.
418 * is set when we are called to send a persist packet.
419 * tp->t_timer[TCPT_REXMT]
420 * is set when we are retransmitting
421 * The output side is idle when both timers are zero.
423 * If send window is too small, there is data to transmit, and no
424 * retransmit or persist is pending, then go to persist state.
425 * If nothing happens soon, send when timer expires:
426 * if window is nonzero, transmit what we can,
427 * otherwise force out a byte.
429 if (so
->so_snd
.sb_cc
&& tp
->t_timer
[TCPT_REXMT
] == 0 &&
430 tp
->t_timer
[TCPT_PERSIST
] == 0) {
436 * No reason to send a segment, just return.
438 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
443 * Before ESTABLISHED, force sending of initial options
444 * unless TCP set not to do any options.
445 * NOTE: we assume that the IP/TCP header plus TCP options
446 * always fit in a single mbuf, leaving room for a maximum
448 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
453 hdrlen
= sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
);
456 hdrlen
= sizeof (struct tcpiphdr
);
457 if (flags
& TH_SYN
) {
458 tp
->snd_nxt
= tp
->iss
;
459 if ((tp
->t_flags
& TF_NOOPT
) == 0) {
462 opt
[0] = TCPOPT_MAXSEG
;
463 opt
[1] = TCPOLEN_MAXSEG
;
464 mss
= htons((u_short
) tcp_mssopt(tp
));
465 (void)memcpy(opt
+ 2, &mss
, sizeof(mss
));
466 optlen
= TCPOLEN_MAXSEG
;
468 if ((tp
->t_flags
& TF_REQ_SCALE
) &&
469 ((flags
& TH_ACK
) == 0 ||
470 (tp
->t_flags
& TF_RCVD_SCALE
))) {
471 *((u_int32_t
*)(opt
+ optlen
)) = htonl(
473 TCPOPT_WINDOW
<< 16 |
474 TCPOLEN_WINDOW
<< 8 |
475 tp
->request_r_scale
);
482 * Send a timestamp and echo-reply if this is a SYN and our side
483 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
484 * and our peer have sent timestamps in our SYN's.
486 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
487 (flags
& TH_RST
) == 0 &&
488 ((flags
& TH_ACK
) == 0 ||
489 (tp
->t_flags
& TF_RCVD_TSTMP
))) {
490 u_int32_t
*lp
= (u_int32_t
*)(opt
+ optlen
);
492 /* Form timestamp option as shown in appendix A of RFC 1323. */
493 *lp
++ = htonl(TCPOPT_TSTAMP_HDR
);
494 *lp
++ = htonl(tcp_now
);
495 *lp
= htonl(tp
->ts_recent
);
496 optlen
+= TCPOLEN_TSTAMP_APPA
;
500 * Send `CC-family' options if our side wants to use them (TF_REQ_CC),
501 * options are allowed (!TF_NOOPT) and it's not a RST.
503 if ((tp
->t_flags
& (TF_REQ_CC
|TF_NOOPT
)) == TF_REQ_CC
&&
504 (flags
& TH_RST
) == 0) {
505 switch (flags
& (TH_SYN
|TH_ACK
)) {
507 * This is a normal ACK, send CC if we received CC before
511 if (!(tp
->t_flags
& TF_RCVD_CC
))
516 * We can only get here in T/TCP's SYN_SENT* state, when
517 * we're a sending a non-SYN segment without waiting for
518 * the ACK of our SYN. A check above assures that we only
519 * do this if our peer understands T/TCP.
522 opt
[optlen
++] = TCPOPT_NOP
;
523 opt
[optlen
++] = TCPOPT_NOP
;
524 opt
[optlen
++] = TCPOPT_CC
;
525 opt
[optlen
++] = TCPOLEN_CC
;
526 *(u_int32_t
*)&opt
[optlen
] = htonl(tp
->cc_send
);
532 * This is our initial SYN, check whether we have to use
536 opt
[optlen
++] = TCPOPT_NOP
;
537 opt
[optlen
++] = TCPOPT_NOP
;
538 opt
[optlen
++] = tp
->t_flags
& TF_SENDCCNEW
?
539 TCPOPT_CCNEW
: TCPOPT_CC
;
540 opt
[optlen
++] = TCPOLEN_CC
;
541 *(u_int32_t
*)&opt
[optlen
] = htonl(tp
->cc_send
);
546 * This is a SYN,ACK; send CC and CC.echo if we received
549 case (TH_SYN
|TH_ACK
):
550 if (tp
->t_flags
& TF_RCVD_CC
) {
551 opt
[optlen
++] = TCPOPT_NOP
;
552 opt
[optlen
++] = TCPOPT_NOP
;
553 opt
[optlen
++] = TCPOPT_CC
;
554 opt
[optlen
++] = TCPOLEN_CC
;
555 *(u_int32_t
*)&opt
[optlen
] =
558 opt
[optlen
++] = TCPOPT_NOP
;
559 opt
[optlen
++] = TCPOPT_NOP
;
560 opt
[optlen
++] = TCPOPT_CCECHO
;
561 opt
[optlen
++] = TCPOLEN_CC
;
562 *(u_int32_t
*)&opt
[optlen
] =
573 ipoptlen
= ip6_optlen(tp
->t_inpcb
);
577 if (tp
->t_inpcb
->inp_options
) {
578 ipoptlen
= tp
->t_inpcb
->inp_options
->m_len
-
579 offsetof(struct ipoption
, ipopt_list
);
585 if (ipsec_bypass
== 0)
586 ipoptlen
+= ipsec_hdrsiz_tcp(tp
);
590 * Adjust data length if insertion of options will
591 * bump the packet length beyond the t_maxopd length.
592 * Clear the FIN bit because we cut off the tail of
595 if (len
+ optlen
+ ipoptlen
> tp
->t_maxopd
) {
597 * If there is still more to send, don't close the connection.
600 len
= tp
->t_maxopd
- optlen
- ipoptlen
;
604 /*#ifdef DIAGNOSTIC*/
606 if (max_linkhdr
+ hdrlen
> MCLBYTES
)
607 panic("tcphdr too big");
609 if (max_linkhdr
+ hdrlen
> MHLEN
)
610 panic("tcphdr too big");
615 * Grab a header mbuf, attaching a copy of data to
616 * be transmitted, and initialize the header from
617 * the template for sends on this connection.
620 if (tp
->t_force
&& len
== 1)
621 tcpstat
.tcps_sndprobe
++;
622 else if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
)) {
623 tcpstat
.tcps_sndrexmitpack
++;
624 tcpstat
.tcps_sndrexmitbyte
+= len
;
626 tcpstat
.tcps_sndpack
++;
627 tcpstat
.tcps_sndbyte
+= len
;
630 if ((m
= m_copypack(so
->so_snd
.sb_mb
, off
,
631 (int)len
, max_linkhdr
+ hdrlen
)) == 0) {
636 * m_copypack left space for our hdr; use it.
642 * try to use the new interface that allocates all
643 * the necessary mbuf hdrs under 1 mbuf lock and
644 * avoids rescanning the socket mbuf list if
645 * certain conditions are met. This routine can't
646 * be used in the following cases...
647 * 1) the protocol headers exceed the capacity of
648 * of a single mbuf header's data area (no cluster attached)
649 * 2) the length of the data being transmitted plus
650 * the protocol headers fits into a single mbuf header's
651 * data area (no cluster attached)
655 if (MHLEN
< hdrlen
+ max_linkhdr
) {
656 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
661 MCLGET(m
, M_DONTWAIT
);
662 if ((m
->m_flags
& M_EXT
) == 0) {
667 m
->m_data
+= max_linkhdr
;
671 if (len
<= MHLEN
- hdrlen
- max_linkhdr
) {
673 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
678 m
->m_data
+= max_linkhdr
;
681 m_copydata(so
->so_snd
.sb_mb
, off
, (int) len
,
682 mtod(m
, caddr_t
) + hdrlen
);
686 m
->m_next
= m_copy(so
->so_snd
.sb_mb
, off
, (int) len
);
687 if (m
->m_next
== 0) {
694 * determine whether the mbuf pointer and offset passed back by the 'last' call
695 * to m_copym_with_hdrs are still valid... if the head of the socket chain has
696 * changed (due to an incoming ACK for instance), or the offset into the chain we
697 * just computed is different from the one last returned by m_copym_with_hdrs (perhaps
698 * we're re-transmitting a packet sent earlier), than we can't pass the mbuf pointer and
699 * offset into it as valid hints for m_copym_with_hdrs to use (if valid, these hints allow
700 * m_copym_with_hdrs to avoid rescanning from the beginning of the socket buffer mbuf list.
701 * setting the mbuf pointer to NULL is sufficient to disable the hint mechanism.
703 if (m_head
!= so
->so_snd
.sb_mb
|| last_off
!= off
)
705 last_off
= off
+ len
;
706 m_head
= so
->so_snd
.sb_mb
;
709 * m_copym_with_hdrs will always return the last mbuf pointer and the offset into it that
710 * it acted on to fullfill the current request, whether a valid 'hint' was passed in or not
712 if ((m
= m_copym_with_hdrs(so
->so_snd
.sb_mb
, off
, (int) len
, M_DONTWAIT
, &m_last
, &m_off
)) == NULL
) {
716 m
->m_data
+= max_linkhdr
;
722 * If we're sending everything we've got, set PUSH.
723 * (This will keep happy those implementations which only
724 * give data to the user when a buffer fills or
727 if (off
+ len
== so
->so_snd
.sb_cc
)
730 if (tp
->t_flags
& TF_ACKNOW
)
731 tcpstat
.tcps_sndacks
++;
732 else if (flags
& (TH_SYN
|TH_FIN
|TH_RST
))
733 tcpstat
.tcps_sndctrl
++;
734 else if (SEQ_GT(tp
->snd_up
, tp
->snd_una
))
735 tcpstat
.tcps_sndurg
++;
737 tcpstat
.tcps_sndwinup
++;
739 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
745 if (isipv6
&& (MHLEN
< hdrlen
+ max_linkhdr
) &&
750 m
->m_data
+= max_linkhdr
;
753 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
756 ip6
= mtod(m
, struct ip6_hdr
*);
757 th
= (struct tcphdr
*)(ip6
+ 1);
758 tcp_fillheaders(tp
, ip6
, th
);
762 ip
= mtod(m
, struct ip
*);
763 ipov
= (struct ipovly
*)ip
;
764 th
= (struct tcphdr
*)(ip
+ 1);
765 /* this picks up the pseudo header (w/o the length) */
766 tcp_fillheaders(tp
, ip
, th
);
770 * Fill in fields, remembering maximum advertised
771 * window for use in delaying messages about window sizes.
772 * If resending a FIN, be sure not to use a new sequence number.
774 if (flags
& TH_FIN
&& tp
->t_flags
& TF_SENTFIN
&&
775 tp
->snd_nxt
== tp
->snd_max
)
778 * If we are doing retransmissions, then snd_nxt will
779 * not reflect the first unsent octet. For ACK only
780 * packets, we do not want the sequence number of the
781 * retransmitted packet, we want the sequence number
782 * of the next unsent octet. So, if there is no data
783 * (and no SYN or FIN), use snd_max instead of snd_nxt
784 * when filling in ti_seq. But if we are in persist
785 * state, snd_max might reflect one byte beyond the
786 * right edge of the window, so use snd_nxt in that
787 * case, since we know we aren't doing a retransmission.
788 * (retransmit and persist are mutually exclusive...)
790 if (len
|| (flags
& (TH_SYN
|TH_FIN
)) || tp
->t_timer
[TCPT_PERSIST
])
791 th
->th_seq
= htonl(tp
->snd_nxt
);
793 th
->th_seq
= htonl(tp
->snd_max
);
794 th
->th_ack
= htonl(tp
->rcv_nxt
);
796 bcopy(opt
, th
+ 1, optlen
);
797 th
->th_off
= (sizeof (struct tcphdr
) + optlen
) >> 2;
799 th
->th_flags
= flags
;
801 * Calculate receive window. Don't shrink window,
802 * but avoid silly window syndrome.
804 if (win
< (long)(so
->so_rcv
.sb_hiwat
/ 4) && win
< (long)tp
->t_maxseg
)
806 if (win
< (long)(tp
->rcv_adv
- tp
->rcv_nxt
))
807 win
= (long)(tp
->rcv_adv
- tp
->rcv_nxt
);
808 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0) {
809 if (win
> (long)slowlink_wsize
)
810 win
= slowlink_wsize
;
811 th
->th_win
= htons((u_short
) (win
>>tp
->rcv_scale
));
815 if (win
> (long)TCP_MAXWIN
<< tp
->rcv_scale
)
816 win
= (long)TCP_MAXWIN
<< tp
->rcv_scale
;
817 th
->th_win
= htons((u_short
) (win
>>tp
->rcv_scale
));
819 if (SEQ_GT(tp
->snd_up
, tp
->snd_nxt
)) {
820 th
->th_urp
= htons((u_short
)(tp
->snd_up
- tp
->snd_nxt
));
821 th
->th_flags
|= TH_URG
;
824 * If no urgent pointer to send, then we pull
825 * the urgent pointer to the left edge of the send window
826 * so that it doesn't drift into the send window on sequence
829 tp
->snd_up
= tp
->snd_una
; /* drag it along */
832 * Put TCP length in extended header, and then
833 * checksum extended header and data.
835 m
->m_pkthdr
.len
= hdrlen
+ len
; /* in6_cksum() need this */
839 * ip6_plen is not need to be filled now, and will be filled
842 th
->th_sum
= in6_cksum(m
, IPPROTO_TCP
, sizeof(struct ip6_hdr
),
843 sizeof(struct tcphdr
) + optlen
+ len
);
847 m
->m_pkthdr
.csum_flags
= CSUM_TCP
;
848 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
850 th
->th_sum
= in_addword(th
->th_sum
,
851 htons((u_short
)(optlen
+ len
)));
853 /* IP version must be set here for ipv4/ipv6 checking later */
854 KASSERT(ip
->ip_v
== IPVERSION
,
855 ("%s: IP version incorrect: %d", __FUNCTION__
, ip
->ip_v
));
859 * In transmit state, time the transmission and arrange for
860 * the retransmit. In persist state, just set snd_max.
862 if (tp
->t_force
== 0 || tp
->t_timer
[TCPT_PERSIST
] == 0) {
863 tcp_seq startseq
= tp
->snd_nxt
;
866 * Advance snd_nxt over sequence space of this segment.
868 if (flags
& (TH_SYN
|TH_FIN
)) {
871 if (flags
& TH_FIN
) {
873 tp
->t_flags
|= TF_SENTFIN
;
877 if (SEQ_GT(tp
->snd_nxt
, tp
->snd_max
)) {
878 tp
->snd_max
= tp
->snd_nxt
;
880 * Time this transmission if not a retransmission and
881 * not currently timing anything.
883 if (tp
->t_rtttime
== 0) {
885 tp
->t_rtseq
= startseq
;
886 tcpstat
.tcps_segstimed
++;
891 * Set retransmit timer if not currently set,
892 * and not doing an ack or a keep-alive probe.
893 * Initial value for retransmit timer is smoothed
894 * round-trip time + 2 * round-trip time variance.
895 * Initialize shift counter which is used for backoff
896 * of retransmit time.
898 if (tp
->t_timer
[TCPT_REXMT
] == 0 &&
899 tp
->snd_nxt
!= tp
->snd_una
) {
900 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
901 if (tp
->t_timer
[TCPT_PERSIST
]) {
902 tp
->t_timer
[TCPT_PERSIST
] = 0;
907 if (SEQ_GT(tp
->snd_nxt
+ len
, tp
->snd_max
))
908 tp
->snd_max
= tp
->snd_nxt
+ len
;
914 if (so
->so_options
& SO_DEBUG
)
915 tcp_trace(TA_OUTPUT
, tp
->t_state
, tp
, mtod(m
, void *), th
, 0);
919 * Fill in IP length and desired time to live and
920 * send to IP level. There should be a better way
921 * to handle ttl and tos; we could keep them in
922 * the template, but need a way to checksum without them.
925 * m->m_pkthdr.len should have been set before cksum calcuration,
926 * because in6_cksum() need it.
931 * we separately set hoplimit for every segment, since the
932 * user might want to change the value via setsockopt.
933 * Also, desired default hop limit might be changed via
934 * Neighbor Discovery.
936 ip6
->ip6_hlim
= in6_selecthlim(tp
->t_inpcb
,
937 tp
->t_inpcb
->in6p_route
.ro_rt
?
938 tp
->t_inpcb
->in6p_route
.ro_rt
->rt_ifp
941 /* TODO: IPv6 IP6TOS_ECT bit on */
943 if (ipsec_bypass
== 0 && ipsec_setsocket(m
, so
) != 0) {
949 error
= ip6_output(m
,
950 tp
->t_inpcb
->in6p_outputopts
,
951 &tp
->t_inpcb
->in6p_route
,
952 (so
->so_options
& SO_DONTROUTE
), NULL
, NULL
);
957 ip
->ip_len
= m
->m_pkthdr
.len
;
959 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
960 ip
->ip_ttl
= in6_selecthlim(tp
->t_inpcb
,
961 tp
->t_inpcb
->in6p_route
.ro_rt
?
962 tp
->t_inpcb
->in6p_route
.ro_rt
->rt_ifp
966 ip
->ip_ttl
= tp
->t_inpcb
->inp_ip_ttl
; /* XXX */
967 ip
->ip_tos
= tp
->t_inpcb
->inp_ip_tos
; /* XXX */
972 KERNEL_DEBUG(DBG_LAYER_BEG
,
973 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
974 (((tp
->t_inpcb
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
975 (tp
->t_inpcb
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
981 KERNEL_DEBUG(DBG_LAYER_BEG
,
982 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
983 (((tp
->t_inpcb
->inp_laddr
.s_addr
& 0xffff) << 16) |
984 (tp
->t_inpcb
->inp_faddr
.s_addr
& 0xffff)),
989 * See if we should do MTU discovery. We do it only if the following
991 * 1) we have a valid route to the destination
992 * 2) the MTU is not locked (if it is, then discovery has been
995 if (path_mtu_discovery
996 && (rt
= tp
->t_inpcb
->inp_route
.ro_rt
)
997 && rt
->rt_flags
& RTF_UP
998 && !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
1002 if (ipsec_bypass
== 0)
1003 ipsec_setsocket(m
, so
);
1005 error
= ip_output(m
, tp
->t_inpcb
->inp_options
, &tp
->t_inpcb
->inp_route
,
1006 (so
->so_options
& SO_DONTROUTE
), 0);
1011 * We know that the packet was lost, so back out the
1012 * sequence number advance, if any.
1014 if (tp
->t_force
== 0 || !tp
->t_timer
[TCPT_PERSIST
]) {
1016 * No need to check for TH_FIN here because
1017 * the TF_SENTFIN flag handles that case.
1019 if ((flags
& TH_SYN
) == 0)
1023 if (error
== ENOBUFS
) {
1024 if (!tp
->t_timer
[TCPT_REXMT
] &&
1025 !tp
->t_timer
[TCPT_PERSIST
])
1026 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
1027 tcp_quench(tp
->t_inpcb
, 0);
1028 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1031 if (error
== EMSGSIZE
) {
1033 * ip_output() will have already fixed the route
1034 * for us. tcp_mtudisc() will, as its last action,
1035 * initiate retransmission, so it is important to
1038 tcp_mtudisc(tp
->t_inpcb
, 0);
1039 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1042 if ((error
== EHOSTUNREACH
|| error
== ENETDOWN
)
1043 && TCPS_HAVERCVDSYN(tp
->t_state
)) {
1044 tp
->t_softerror
= error
;
1045 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1048 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1051 tcpstat
.tcps_sndtotal
++;
1054 * Data sent (as far as we can tell).
1055 * If this advertises a larger window than any other segment,
1056 * then remember the size of the advertised window.
1057 * Any pending ACK has now been sent.
1059 if (win
> 0 && SEQ_GT(tp
->rcv_nxt
+win
, tp
->rcv_adv
))
1060 tp
->rcv_adv
= tp
->rcv_nxt
+ win
;
1061 tp
->last_ack_sent
= tp
->rcv_nxt
;
1062 tp
->t_flags
&= ~(TF_ACKNOW
|TF_DELACK
);
1065 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1071 register struct tcpcb
*tp
;
1073 int t
= ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1;
1076 if (tp
->t_timer
[TCPT_REXMT
])
1077 panic("tcp_setpersist: retransmit pending");
1079 * Start/restart persistance timer.
1081 TCPT_RANGESET(tp
->t_timer
[TCPT_PERSIST
],
1082 t
* tcp_backoff
[tp
->t_rxtshift
],
1083 TCPTV_PERSMIN
, TCPTV_PERSMAX
);
1084 if (tp
->t_rxtshift
< TCP_MAXRXTSHIFT
)