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 */
136 extern u_long route_generation
;
140 * Tcp output routine: figure out what should be sent and send it.
144 register struct tcpcb
*tp
;
146 register struct socket
*so
= tp
->t_inpcb
->inp_socket
;
147 register long len
, win
;
148 int off
, flags
, error
;
149 register struct mbuf
*m
;
150 struct ip
*ip
= NULL
;
151 register struct ipovly
*ipov
= NULL
;
153 struct ip6_hdr
*ip6
= NULL
;
155 register struct tcphdr
*th
;
156 u_char opt
[TCP_MAXOLEN
];
157 unsigned ipoptlen
, optlen
, hdrlen
;
159 int maxburst
= TCP_MAXBURST
;
160 struct rmxp_tao
*taop
;
161 struct rmxp_tao tao_noncached
;
164 struct mbuf
*m_last
= 0;
165 struct mbuf
*m_head
= 0;
167 int isipv6
= tp
->t_inpcb
->inp_vflag
& INP_IPV6
;
172 * Determine length of data that should be transmitted,
173 * and flags that will be used.
174 * If there is some data or critical controls (SYN, RST)
175 * to send, then transmit; otherwise, investigate further.
177 idle
= (tp
->snd_max
== tp
->snd_una
);
179 if (idle
&& tp
->t_rcvtime
>= tp
->t_rxtcur
) {
181 if (idle
&& (ticks
- tp
->t_rcvtime
) >= tp
->t_rxtcur
) {
184 * We have been idle for "a while" and no acks are
185 * expected to clock out any data we send --
186 * slow start to get ack "clock" running again.
188 * Set the slow-start flight size depending on whether
189 * this is a local network or not.
193 (isipv6
&& in6_localaddr(&tp
->t_inpcb
->in6p_faddr
)) ||
196 in_localaddr(tp
->t_inpcb
->inp_faddr
)
201 tp
->snd_cwnd
= tp
->t_maxseg
* ss_fltsz_local
;
203 tp
->snd_cwnd
= tp
->t_maxseg
* ss_fltsz
;
207 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
212 KERNEL_DEBUG(DBG_LAYER_BEG
,
213 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
214 (((tp
->t_inpcb
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
215 (tp
->t_inpcb
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
222 KERNEL_DEBUG(DBG_LAYER_BEG
,
223 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
224 (((tp
->t_inpcb
->inp_laddr
.s_addr
& 0xffff) << 16) |
225 (tp
->t_inpcb
->inp_faddr
.s_addr
& 0xffff)),
228 * If the route generation id changed, we need to check that our
229 * local (source) IP address is still valid. If it isn't either
230 * return error or silently do nothing (assuming the address will
231 * come back before the TCP connection times out).
234 if ((tp
->t_inpcb
->inp_route
.ro_rt
!= NULL
&&
235 (tp
->t_inpcb
->inp_route
.ro_rt
->generation_id
!= route_generation
)) || (tp
->t_inpcb
->inp_route
.ro_rt
== NULL
)) {
236 /* check that the source address is still valid */
237 if (ifa_foraddr(tp
->t_inpcb
->inp_laddr
.s_addr
) == NULL
) {
238 if (tp
->t_state
>= TCPS_CLOSE_WAIT
) {
240 return(EADDRNOTAVAIL
);
243 /* set Retransmit timer if it wasn't set
244 * reset Persist timer and shift register as the
245 * adversed peer window may not be valid anymore
248 if (!tp
->t_timer
[TCPT_REXMT
]) {
249 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
250 if (tp
->t_timer
[TCPT_PERSIST
]) {
251 tp
->t_timer
[TCPT_PERSIST
] = 0;
256 if (so
->so_flags
& SOF_NOADDRAVAIL
)
257 return(EADDRNOTAVAIL
);
259 return(0); /* silently ignore and keep data in socket */
264 off
= tp
->snd_nxt
- tp
->snd_una
;
265 win
= min(tp
->snd_wnd
, tp
->snd_cwnd
);
266 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0)
267 win
= min(win
, slowlink_wsize
);
269 flags
= tcp_outflags
[tp
->t_state
];
271 * Get standard flags, and add SYN or FIN if requested by 'hidden'
274 if (tp
->t_flags
& TF_NEEDFIN
)
276 if (tp
->t_flags
& TF_NEEDSYN
)
280 * If in persist timeout with window of 0, send 1 byte.
281 * Otherwise, if window is small but nonzero
282 * and timer expired, we will send what we can
283 * and go to transmit state.
288 * If we still have some data to send, then
289 * clear the FIN bit. Usually this would
290 * happen below when it realizes that we
291 * aren't sending all the data. However,
292 * if we have exactly 1 byte of unsent data,
293 * then it won't clear the FIN bit below,
294 * and if we are in persist state, we wind
295 * up sending the packet without recording
296 * that we sent the FIN bit.
298 * We can't just blindly clear the FIN bit,
299 * because if we don't have any more data
300 * to send then the probe will be the FIN
303 if (off
< so
->so_snd
.sb_cc
)
307 tp
->t_timer
[TCPT_PERSIST
] = 0;
312 len
= (long)ulmin(so
->so_snd
.sb_cc
, win
) - off
;
314 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
315 taop
= &tao_noncached
;
316 bzero(taop
, sizeof(*taop
));
320 * Lop off SYN bit if it has already been sent. However, if this
321 * is SYN-SENT state and if segment contains data and if we don't
322 * know that foreign host supports TAO, suppress sending segment.
324 if ((flags
& TH_SYN
) && SEQ_GT(tp
->snd_nxt
, tp
->snd_una
)) {
327 if (len
> 0 && tp
->t_state
== TCPS_SYN_SENT
&&
328 taop
->tao_ccsent
== 0) {
329 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
335 * Be careful not to send data and/or FIN on SYN segments
336 * in cases when no CC option will be sent.
337 * This measure is needed to prevent interoperability problems
338 * with not fully conformant TCP implementations.
340 if ((flags
& TH_SYN
) &&
341 ((tp
->t_flags
& TF_NOOPT
) || !(tp
->t_flags
& TF_REQ_CC
) ||
342 ((flags
& TH_ACK
) && !(tp
->t_flags
& TF_RCVD_CC
)))) {
349 * If FIN has been sent but not acked,
350 * but we haven't been called to retransmit,
351 * len will be -1. Otherwise, window shrank
352 * after we sent into it. If window shrank to 0,
353 * cancel pending retransmit, pull snd_nxt back
354 * to (closed) window, and set the persist timer
355 * if it isn't already going. If the window didn't
356 * close completely, just wait for an ACK.
360 tp
->t_timer
[TCPT_REXMT
] = 0;
362 tp
->snd_nxt
= tp
->snd_una
;
363 if (tp
->t_timer
[TCPT_PERSIST
] == 0)
367 if (len
> tp
->t_maxseg
) {
371 if (SEQ_LT(tp
->snd_nxt
+ len
, tp
->snd_una
+ so
->so_snd
.sb_cc
))
374 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0 ) /* Clips window size for slow links */
375 win
= min(sbspace(&so
->so_rcv
), slowlink_wsize
);
377 win
= sbspace(&so
->so_rcv
);
380 * Sender silly window avoidance. If connection is idle
381 * and can send all data, a maximum segment,
382 * at least a maximum default-size segment do it,
383 * or are forced, do it; otherwise don't bother.
384 * If peer's buffer is tiny, then send
385 * when window is at least half open.
386 * If retransmitting (possibly after persist timer forced us
387 * to send into a small window), then must resend.
390 if (len
== tp
->t_maxseg
)
392 if (!(tp
->t_flags
& TF_MORETOCOME
) &&
393 (idle
|| tp
->t_flags
& TF_NODELAY
) &&
394 (tp
->t_flags
& TF_NOPUSH
) == 0 &&
395 len
+ off
>= so
->so_snd
.sb_cc
)
399 if (len
>= tp
->max_sndwnd
/ 2 && tp
->max_sndwnd
> 0)
401 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
))
406 * Compare available window to amount of window
407 * known to peer (as advertised window less
408 * next expected input). If the difference is at least two
409 * max size segments, or at least 50% of the maximum possible
410 * window, then want to send a window update to peer.
414 * "adv" is the amount we can increase the window,
415 * taking into account that we are limited by
416 * TCP_MAXWIN << tp->rcv_scale.
418 long adv
= min(win
, (long)TCP_MAXWIN
<< tp
->rcv_scale
) -
419 (tp
->rcv_adv
- tp
->rcv_nxt
);
421 if (adv
>= (long) (2 * tp
->t_maxseg
))
423 if (2 * adv
>= (long) so
->so_rcv
.sb_hiwat
)
428 * Send if we owe peer an ACK.
430 if (tp
->t_flags
& TF_ACKNOW
)
432 if ((flags
& TH_RST
) ||
433 ((flags
& TH_SYN
) && (tp
->t_flags
& TF_NEEDSYN
) == 0))
435 if (SEQ_GT(tp
->snd_up
, tp
->snd_una
))
438 * If our state indicates that FIN should be sent
439 * and we have not yet done so, or we're retransmitting the FIN,
440 * then we need to send.
442 if (flags
& TH_FIN
&&
443 ((tp
->t_flags
& TF_SENTFIN
) == 0 || tp
->snd_nxt
== tp
->snd_una
))
447 * TCP window updates are not reliable, rather a polling protocol
448 * using ``persist'' packets is used to insure receipt of window
449 * updates. The three ``states'' for the output side are:
450 * idle not doing retransmits or persists
451 * persisting to move a small or zero window
452 * (re)transmitting and thereby not persisting
454 * tp->t_timer[TCPT_PERSIST]
455 * is set when we are in persist state.
457 * is set when we are called to send a persist packet.
458 * tp->t_timer[TCPT_REXMT]
459 * is set when we are retransmitting
460 * The output side is idle when both timers are zero.
462 * If send window is too small, there is data to transmit, and no
463 * retransmit or persist is pending, then go to persist state.
464 * If nothing happens soon, send when timer expires:
465 * if window is nonzero, transmit what we can,
466 * otherwise force out a byte.
468 if (so
->so_snd
.sb_cc
&& tp
->t_timer
[TCPT_REXMT
] == 0 &&
469 tp
->t_timer
[TCPT_PERSIST
] == 0) {
475 * No reason to send a segment, just return.
477 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
482 * Before ESTABLISHED, force sending of initial options
483 * unless TCP set not to do any options.
484 * NOTE: we assume that the IP/TCP header plus TCP options
485 * always fit in a single mbuf, leaving room for a maximum
487 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
492 hdrlen
= sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
);
495 hdrlen
= sizeof (struct tcpiphdr
);
496 if (flags
& TH_SYN
) {
497 tp
->snd_nxt
= tp
->iss
;
498 if ((tp
->t_flags
& TF_NOOPT
) == 0) {
501 opt
[0] = TCPOPT_MAXSEG
;
502 opt
[1] = TCPOLEN_MAXSEG
;
503 mss
= htons((u_short
) tcp_mssopt(tp
));
504 (void)memcpy(opt
+ 2, &mss
, sizeof(mss
));
505 optlen
= TCPOLEN_MAXSEG
;
507 if ((tp
->t_flags
& TF_REQ_SCALE
) &&
508 ((flags
& TH_ACK
) == 0 ||
509 (tp
->t_flags
& TF_RCVD_SCALE
))) {
510 *((u_int32_t
*)(opt
+ optlen
)) = htonl(
512 TCPOPT_WINDOW
<< 16 |
513 TCPOLEN_WINDOW
<< 8 |
514 tp
->request_r_scale
);
521 * Send a timestamp and echo-reply if this is a SYN and our side
522 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
523 * and our peer have sent timestamps in our SYN's.
525 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
526 (flags
& TH_RST
) == 0 &&
527 ((flags
& TH_ACK
) == 0 ||
528 (tp
->t_flags
& TF_RCVD_TSTMP
))) {
529 u_int32_t
*lp
= (u_int32_t
*)(opt
+ optlen
);
531 /* Form timestamp option as shown in appendix A of RFC 1323. */
532 *lp
++ = htonl(TCPOPT_TSTAMP_HDR
);
533 *lp
++ = htonl(tcp_now
);
534 *lp
= htonl(tp
->ts_recent
);
535 optlen
+= TCPOLEN_TSTAMP_APPA
;
539 * Send `CC-family' options if our side wants to use them (TF_REQ_CC),
540 * options are allowed (!TF_NOOPT) and it's not a RST.
542 if ((tp
->t_flags
& (TF_REQ_CC
|TF_NOOPT
)) == TF_REQ_CC
&&
543 (flags
& TH_RST
) == 0) {
544 switch (flags
& (TH_SYN
|TH_ACK
)) {
546 * This is a normal ACK, send CC if we received CC before
550 if (!(tp
->t_flags
& TF_RCVD_CC
))
555 * We can only get here in T/TCP's SYN_SENT* state, when
556 * we're a sending a non-SYN segment without waiting for
557 * the ACK of our SYN. A check above assures that we only
558 * do this if our peer understands T/TCP.
561 opt
[optlen
++] = TCPOPT_NOP
;
562 opt
[optlen
++] = TCPOPT_NOP
;
563 opt
[optlen
++] = TCPOPT_CC
;
564 opt
[optlen
++] = TCPOLEN_CC
;
565 *(u_int32_t
*)&opt
[optlen
] = htonl(tp
->cc_send
);
571 * This is our initial SYN, check whether we have to use
575 opt
[optlen
++] = TCPOPT_NOP
;
576 opt
[optlen
++] = TCPOPT_NOP
;
577 opt
[optlen
++] = tp
->t_flags
& TF_SENDCCNEW
?
578 TCPOPT_CCNEW
: TCPOPT_CC
;
579 opt
[optlen
++] = TCPOLEN_CC
;
580 *(u_int32_t
*)&opt
[optlen
] = htonl(tp
->cc_send
);
585 * This is a SYN,ACK; send CC and CC.echo if we received
588 case (TH_SYN
|TH_ACK
):
589 if (tp
->t_flags
& TF_RCVD_CC
) {
590 opt
[optlen
++] = TCPOPT_NOP
;
591 opt
[optlen
++] = TCPOPT_NOP
;
592 opt
[optlen
++] = TCPOPT_CC
;
593 opt
[optlen
++] = TCPOLEN_CC
;
594 *(u_int32_t
*)&opt
[optlen
] =
597 opt
[optlen
++] = TCPOPT_NOP
;
598 opt
[optlen
++] = TCPOPT_NOP
;
599 opt
[optlen
++] = TCPOPT_CCECHO
;
600 opt
[optlen
++] = TCPOLEN_CC
;
601 *(u_int32_t
*)&opt
[optlen
] =
612 ipoptlen
= ip6_optlen(tp
->t_inpcb
);
616 if (tp
->t_inpcb
->inp_options
) {
617 ipoptlen
= tp
->t_inpcb
->inp_options
->m_len
-
618 offsetof(struct ipoption
, ipopt_list
);
624 if (ipsec_bypass
== 0)
625 ipoptlen
+= ipsec_hdrsiz_tcp(tp
);
629 * Adjust data length if insertion of options will
630 * bump the packet length beyond the t_maxopd length.
631 * Clear the FIN bit because we cut off the tail of
634 if (len
+ optlen
+ ipoptlen
> tp
->t_maxopd
) {
636 * If there is still more to send, don't close the connection.
639 len
= tp
->t_maxopd
- optlen
- ipoptlen
;
643 /*#ifdef DIAGNOSTIC*/
645 if (max_linkhdr
+ hdrlen
> MCLBYTES
)
646 panic("tcphdr too big");
648 if (max_linkhdr
+ hdrlen
> MHLEN
)
649 panic("tcphdr too big");
654 * Grab a header mbuf, attaching a copy of data to
655 * be transmitted, and initialize the header from
656 * the template for sends on this connection.
659 if (tp
->t_force
&& len
== 1)
660 tcpstat
.tcps_sndprobe
++;
661 else if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
)) {
662 tcpstat
.tcps_sndrexmitpack
++;
663 tcpstat
.tcps_sndrexmitbyte
+= len
;
665 tcpstat
.tcps_sndpack
++;
666 tcpstat
.tcps_sndbyte
+= len
;
669 if ((m
= m_copypack(so
->so_snd
.sb_mb
, off
,
670 (int)len
, max_linkhdr
+ hdrlen
)) == 0) {
675 * m_copypack left space for our hdr; use it.
681 * try to use the new interface that allocates all
682 * the necessary mbuf hdrs under 1 mbuf lock and
683 * avoids rescanning the socket mbuf list if
684 * certain conditions are met. This routine can't
685 * be used in the following cases...
686 * 1) the protocol headers exceed the capacity of
687 * of a single mbuf header's data area (no cluster attached)
688 * 2) the length of the data being transmitted plus
689 * the protocol headers fits into a single mbuf header's
690 * data area (no cluster attached)
694 if (MHLEN
< hdrlen
+ max_linkhdr
) {
695 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
700 MCLGET(m
, M_DONTWAIT
);
701 if ((m
->m_flags
& M_EXT
) == 0) {
706 m
->m_data
+= max_linkhdr
;
710 if (len
<= MHLEN
- hdrlen
- max_linkhdr
) {
712 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
717 m
->m_data
+= max_linkhdr
;
720 /* makes sure we still have data left to be sent at this point */
721 if (so
->so_snd
.sb_mb
== NULL
|| off
== -1) {
722 if (m
!= NULL
) m_freem(m
);
723 error
= 0; /* should we return an error? */
726 m_copydata(so
->so_snd
.sb_mb
, off
, (int) len
,
727 mtod(m
, caddr_t
) + hdrlen
);
731 m
->m_next
= m_copy(so
->so_snd
.sb_mb
, off
, (int) len
);
732 if (m
->m_next
== 0) {
739 * determine whether the mbuf pointer and offset passed back by the 'last' call
740 * to m_copym_with_hdrs are still valid... if the head of the socket chain has
741 * changed (due to an incoming ACK for instance), or the offset into the chain we
742 * just computed is different from the one last returned by m_copym_with_hdrs (perhaps
743 * we're re-transmitting a packet sent earlier), than we can't pass the mbuf pointer and
744 * offset into it as valid hints for m_copym_with_hdrs to use (if valid, these hints allow
745 * m_copym_with_hdrs to avoid rescanning from the beginning of the socket buffer mbuf list.
746 * setting the mbuf pointer to NULL is sufficient to disable the hint mechanism.
748 if (m_head
!= so
->so_snd
.sb_mb
|| last_off
!= off
)
750 last_off
= off
+ len
;
751 m_head
= so
->so_snd
.sb_mb
;
753 /* makes sure we still have data left to be sent at this point */
754 if (m_head
== NULL
) {
755 error
= 0; /* should we return an error? */
760 * m_copym_with_hdrs will always return the last mbuf pointer and the offset into it that
761 * it acted on to fullfill the current request, whether a valid 'hint' was passed in or not
763 if ((m
= m_copym_with_hdrs(so
->so_snd
.sb_mb
, off
, (int) len
, M_DONTWAIT
, &m_last
, &m_off
)) == NULL
) {
767 m
->m_data
+= max_linkhdr
;
773 * If we're sending everything we've got, set PUSH.
774 * (This will keep happy those implementations which only
775 * give data to the user when a buffer fills or
778 if (off
+ len
== so
->so_snd
.sb_cc
)
781 if (tp
->t_flags
& TF_ACKNOW
)
782 tcpstat
.tcps_sndacks
++;
783 else if (flags
& (TH_SYN
|TH_FIN
|TH_RST
))
784 tcpstat
.tcps_sndctrl
++;
785 else if (SEQ_GT(tp
->snd_up
, tp
->snd_una
))
786 tcpstat
.tcps_sndurg
++;
788 tcpstat
.tcps_sndwinup
++;
790 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
796 if (isipv6
&& (MHLEN
< hdrlen
+ max_linkhdr
) &&
801 m
->m_data
+= max_linkhdr
;
804 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
807 ip6
= mtod(m
, struct ip6_hdr
*);
808 th
= (struct tcphdr
*)(ip6
+ 1);
809 tcp_fillheaders(tp
, ip6
, th
);
813 ip
= mtod(m
, struct ip
*);
814 ipov
= (struct ipovly
*)ip
;
815 th
= (struct tcphdr
*)(ip
+ 1);
816 /* this picks up the pseudo header (w/o the length) */
817 tcp_fillheaders(tp
, ip
, th
);
821 * Fill in fields, remembering maximum advertised
822 * window for use in delaying messages about window sizes.
823 * If resending a FIN, be sure not to use a new sequence number.
825 if (flags
& TH_FIN
&& tp
->t_flags
& TF_SENTFIN
&&
826 tp
->snd_nxt
== tp
->snd_max
)
829 * If we are doing retransmissions, then snd_nxt will
830 * not reflect the first unsent octet. For ACK only
831 * packets, we do not want the sequence number of the
832 * retransmitted packet, we want the sequence number
833 * of the next unsent octet. So, if there is no data
834 * (and no SYN or FIN), use snd_max instead of snd_nxt
835 * when filling in ti_seq. But if we are in persist
836 * state, snd_max might reflect one byte beyond the
837 * right edge of the window, so use snd_nxt in that
838 * case, since we know we aren't doing a retransmission.
839 * (retransmit and persist are mutually exclusive...)
841 if (len
|| (flags
& (TH_SYN
|TH_FIN
)) || tp
->t_timer
[TCPT_PERSIST
])
842 th
->th_seq
= htonl(tp
->snd_nxt
);
844 th
->th_seq
= htonl(tp
->snd_max
);
845 th
->th_ack
= htonl(tp
->rcv_nxt
);
847 bcopy(opt
, th
+ 1, optlen
);
848 th
->th_off
= (sizeof (struct tcphdr
) + optlen
) >> 2;
850 th
->th_flags
= flags
;
852 * Calculate receive window. Don't shrink window,
853 * but avoid silly window syndrome.
855 if (win
< (long)(so
->so_rcv
.sb_hiwat
/ 4) && win
< (long)tp
->t_maxseg
)
857 if (win
< (long)(tp
->rcv_adv
- tp
->rcv_nxt
))
858 win
= (long)(tp
->rcv_adv
- tp
->rcv_nxt
);
859 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0) {
860 if (win
> (long)slowlink_wsize
)
861 win
= slowlink_wsize
;
862 th
->th_win
= htons((u_short
) (win
>>tp
->rcv_scale
));
866 if (win
> (long)TCP_MAXWIN
<< tp
->rcv_scale
)
867 win
= (long)TCP_MAXWIN
<< tp
->rcv_scale
;
868 th
->th_win
= htons((u_short
) (win
>>tp
->rcv_scale
));
870 if (SEQ_GT(tp
->snd_up
, tp
->snd_nxt
)) {
871 th
->th_urp
= htons((u_short
)(tp
->snd_up
- tp
->snd_nxt
));
872 th
->th_flags
|= TH_URG
;
875 * If no urgent pointer to send, then we pull
876 * the urgent pointer to the left edge of the send window
877 * so that it doesn't drift into the send window on sequence
880 tp
->snd_up
= tp
->snd_una
; /* drag it along */
883 * Put TCP length in extended header, and then
884 * checksum extended header and data.
886 m
->m_pkthdr
.len
= hdrlen
+ len
; /* in6_cksum() need this */
890 * ip6_plen is not need to be filled now, and will be filled
893 th
->th_sum
= in6_cksum(m
, IPPROTO_TCP
, sizeof(struct ip6_hdr
),
894 sizeof(struct tcphdr
) + optlen
+ len
);
898 m
->m_pkthdr
.csum_flags
= CSUM_TCP
;
899 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
901 th
->th_sum
= in_addword(th
->th_sum
,
902 htons((u_short
)(optlen
+ len
)));
904 /* IP version must be set here for ipv4/ipv6 checking later */
905 KASSERT(ip
->ip_v
== IPVERSION
,
906 ("%s: IP version incorrect: %d", __FUNCTION__
, ip
->ip_v
));
910 * In transmit state, time the transmission and arrange for
911 * the retransmit. In persist state, just set snd_max.
913 if (tp
->t_force
== 0 || tp
->t_timer
[TCPT_PERSIST
] == 0) {
914 tcp_seq startseq
= tp
->snd_nxt
;
917 * Advance snd_nxt over sequence space of this segment.
919 if (flags
& (TH_SYN
|TH_FIN
)) {
922 if (flags
& TH_FIN
) {
924 tp
->t_flags
|= TF_SENTFIN
;
928 if (SEQ_GT(tp
->snd_nxt
, tp
->snd_max
)) {
929 tp
->snd_max
= tp
->snd_nxt
;
931 * Time this transmission if not a retransmission and
932 * not currently timing anything.
934 if (tp
->t_rtttime
== 0) {
936 tp
->t_rtseq
= startseq
;
937 tcpstat
.tcps_segstimed
++;
942 * Set retransmit timer if not currently set,
943 * and not doing an ack or a keep-alive probe.
944 * Initial value for retransmit timer is smoothed
945 * round-trip time + 2 * round-trip time variance.
946 * Initialize shift counter which is used for backoff
947 * of retransmit time.
949 if (tp
->t_timer
[TCPT_REXMT
] == 0 &&
950 tp
->snd_nxt
!= tp
->snd_una
) {
951 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
952 if (tp
->t_timer
[TCPT_PERSIST
]) {
953 tp
->t_timer
[TCPT_PERSIST
] = 0;
958 if (SEQ_GT(tp
->snd_nxt
+ len
, tp
->snd_max
))
959 tp
->snd_max
= tp
->snd_nxt
+ len
;
965 if (so
->so_options
& SO_DEBUG
)
966 tcp_trace(TA_OUTPUT
, tp
->t_state
, tp
, mtod(m
, void *), th
, 0);
970 * Fill in IP length and desired time to live and
971 * send to IP level. There should be a better way
972 * to handle ttl and tos; we could keep them in
973 * the template, but need a way to checksum without them.
976 * m->m_pkthdr.len should have been set before cksum calcuration,
977 * because in6_cksum() need it.
982 * we separately set hoplimit for every segment, since the
983 * user might want to change the value via setsockopt.
984 * Also, desired default hop limit might be changed via
985 * Neighbor Discovery.
987 ip6
->ip6_hlim
= in6_selecthlim(tp
->t_inpcb
,
988 tp
->t_inpcb
->in6p_route
.ro_rt
?
989 tp
->t_inpcb
->in6p_route
.ro_rt
->rt_ifp
992 /* TODO: IPv6 IP6TOS_ECT bit on */
994 if (ipsec_bypass
== 0 && ipsec_setsocket(m
, so
) != 0) {
1000 error
= ip6_output(m
,
1001 tp
->t_inpcb
->in6p_outputopts
,
1002 &tp
->t_inpcb
->in6p_route
,
1003 (so
->so_options
& SO_DONTROUTE
), NULL
, NULL
);
1008 ip
->ip_len
= m
->m_pkthdr
.len
;
1011 ip
->ip_ttl
= in6_selecthlim(tp
->t_inpcb
,
1012 tp
->t_inpcb
->in6p_route
.ro_rt
?
1013 tp
->t_inpcb
->in6p_route
.ro_rt
->rt_ifp
1017 ip
->ip_ttl
= tp
->t_inpcb
->inp_ip_ttl
; /* XXX */
1018 ip
->ip_tos
= tp
->t_inpcb
->inp_ip_tos
; /* XXX */
1023 KERNEL_DEBUG(DBG_LAYER_BEG
,
1024 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
1025 (((tp
->t_inpcb
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
1026 (tp
->t_inpcb
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
1032 KERNEL_DEBUG(DBG_LAYER_BEG
,
1033 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
1034 (((tp
->t_inpcb
->inp_laddr
.s_addr
& 0xffff) << 16) |
1035 (tp
->t_inpcb
->inp_faddr
.s_addr
& 0xffff)),
1040 * See if we should do MTU discovery. We do it only if the following
1042 * 1) we have a valid route to the destination
1043 * 2) the MTU is not locked (if it is, then discovery has been
1046 if (path_mtu_discovery
1047 && (rt
= tp
->t_inpcb
->inp_route
.ro_rt
)
1048 && rt
->rt_flags
& RTF_UP
1049 && !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
1050 ip
->ip_off
|= IP_DF
;
1053 if (ipsec_bypass
== 0)
1054 ipsec_setsocket(m
, so
);
1056 error
= ip_output(m
, tp
->t_inpcb
->inp_options
, &tp
->t_inpcb
->inp_route
,
1057 (so
->so_options
& SO_DONTROUTE
), 0);
1062 * We know that the packet was lost, so back out the
1063 * sequence number advance, if any.
1065 if (tp
->t_force
== 0 || !tp
->t_timer
[TCPT_PERSIST
]) {
1067 * No need to check for TH_FIN here because
1068 * the TF_SENTFIN flag handles that case.
1070 if ((flags
& TH_SYN
) == 0)
1074 if (error
== ENOBUFS
) {
1075 if (!tp
->t_timer
[TCPT_REXMT
] &&
1076 !tp
->t_timer
[TCPT_PERSIST
])
1077 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
1078 tcp_quench(tp
->t_inpcb
, 0);
1079 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1082 if (error
== EMSGSIZE
) {
1084 * ip_output() will have already fixed the route
1085 * for us. tcp_mtudisc() will, as its last action,
1086 * initiate retransmission, so it is important to
1089 tcp_mtudisc(tp
->t_inpcb
, 0);
1090 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1093 if ((error
== EHOSTUNREACH
|| error
== ENETDOWN
)
1094 && TCPS_HAVERCVDSYN(tp
->t_state
)) {
1095 tp
->t_softerror
= error
;
1096 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1099 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1102 tcpstat
.tcps_sndtotal
++;
1105 * Data sent (as far as we can tell).
1106 * If this advertises a larger window than any other segment,
1107 * then remember the size of the advertised window.
1108 * Any pending ACK has now been sent.
1110 if (win
> 0 && SEQ_GT(tp
->rcv_nxt
+win
, tp
->rcv_adv
))
1111 tp
->rcv_adv
= tp
->rcv_nxt
+ win
;
1112 tp
->last_ack_sent
= tp
->rcv_nxt
;
1113 tp
->t_flags
&= ~(TF_ACKNOW
|TF_DELACK
);
1115 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1123 register struct tcpcb
*tp
;
1125 int t
= ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1;
1128 if (tp
->t_timer
[TCPT_REXMT
])
1129 panic("tcp_setpersist: retransmit pending");
1131 * Start/restart persistance timer.
1133 TCPT_RANGESET(tp
->t_timer
[TCPT_PERSIST
],
1134 t
* tcp_backoff
[tp
->t_rxtshift
],
1135 TCPTV_PERSMIN
, TCPTV_PERSMAX
);
1136 if (tp
->t_rxtshift
< TCP_MAXRXTSHIFT
)