2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
56 * $FreeBSD: src/sys/netinet/tcp_output.c,v 1.39.2.10 2001/07/07 04:30:38 silby Exp $
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/sysctl.h>
67 #include <sys/domain.h>
68 #include <sys/protosw.h>
69 #include <sys/socket.h>
70 #include <sys/socketvar.h>
72 #include <net/route.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/in_pcb.h>
78 #include <netinet/ip_var.h>
80 #include <netinet6/in6_pcb.h>
81 #include <netinet/ip6.h>
82 #include <netinet6/ip6_var.h>
84 #include <netinet/tcp.h>
86 #include <netinet/tcp_fsm.h>
87 #include <netinet/tcp_seq.h>
88 #include <netinet/tcp_timer.h>
89 #include <netinet/tcp_var.h>
90 #include <netinet/tcpip.h>
92 #include <netinet/tcp_debug.h>
94 #include <sys/kdebug.h>
97 #include <netinet6/ipsec.h>
100 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 1)
101 #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 3)
102 #define DBG_FNC_TCP_OUTPUT NETDBG_CODE(DBG_NETTCP, (4 << 8) | 1)
106 extern struct mbuf
*m_copypack();
109 static int path_mtu_discovery
= 1;
110 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, path_mtu_discovery
, CTLFLAG_RW
,
111 &path_mtu_discovery
, 1, "Enable Path MTU Discovery");
114 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, slowstart_flightsize
, CTLFLAG_RW
,
115 &ss_fltsz
, 1, "Slow start flight size");
117 int ss_fltsz_local
= 4; /* starts with four segments max */
118 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, local_slowstart_flightsize
, CTLFLAG_RW
,
119 &ss_fltsz_local
, 1, "Slow start flight size for local networks");
121 int tcp_do_newreno
= 0;
122 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, newreno
, CTLFLAG_RW
, &tcp_do_newreno
,
123 0, "Enable NewReno Algorithms");
125 int tcp_packet_chaining
= 50;
126 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, packetchain
, CTLFLAG_RW
, &tcp_packet_chaining
,
127 0, "Enable TCP output packet chaining");
129 struct mbuf
*m_copym_with_hdrs(struct mbuf
*, int, int, int, struct mbuf
**, int*);
130 static long packchain_newlist
= 0;
131 static long packchain_looped
= 0;
132 static long packchain_sent
= 0;
135 /* temporary: for testing */
137 extern int ipsec_bypass
;
140 extern int slowlink_wsize
; /* window correction for slow links */
141 extern u_long route_generation
;
142 extern int fw_enable
; /* firewall is on: disable packet chaining */
143 extern int ipsec_bypass
;
145 extern vm_size_t so_cache_zone_element_size
;
147 static __inline__ u_int16_t
148 get_socket_id(struct socket
* s
)
152 if (so_cache_zone_element_size
== 0) {
155 val
= (u_int16_t
)(((u_int32_t
)s
) / so_cache_zone_element_size
);
163 * Tcp output routine: figure out what should be sent and send it.
167 register struct tcpcb
*tp
;
169 register struct socket
*so
= tp
->t_inpcb
->inp_socket
;
170 register long len
, win
;
171 int off
, flags
, error
;
172 register struct mbuf
*m
;
173 struct ip
*ip
= NULL
;
174 register struct ipovly
*ipov
= NULL
;
176 struct ip6_hdr
*ip6
= NULL
;
178 register struct tcphdr
*th
;
179 u_char opt
[TCP_MAXOLEN
];
180 unsigned ipoptlen
, optlen
, hdrlen
;
181 int idle
, sendalot
, howmuchsent
= 0;
182 int maxburst
= TCP_MAXBURST
;
183 struct rmxp_tao
*taop
;
184 struct rmxp_tao tao_noncached
;
187 struct mbuf
*m_last
= 0;
188 struct mbuf
*m_head
= 0;
189 struct mbuf
*packetlist
= 0;
190 struct mbuf
*lastpacket
= 0;
192 int isipv6
= tp
->t_inpcb
->inp_vflag
& INP_IPV6
;
194 short packchain_listadd
= 0;
195 u_int16_t socket_id
= get_socket_id(so
);
199 * Determine length of data that should be transmitted,
200 * and flags that will be used.
201 * If there is some data or critical controls (SYN, RST)
202 * to send, then transmit; otherwise, investigate further.
204 idle
= (tp
->snd_max
== tp
->snd_una
);
205 if (idle
&& tp
->t_rcvtime
>= tp
->t_rxtcur
) {
207 * We have been idle for "a while" and no acks are
208 * expected to clock out any data we send --
209 * slow start to get ack "clock" running again.
211 * Set the slow-start flight size depending on whether
212 * this is a local network or not.
216 (isipv6
&& in6_localaddr(&tp
->t_inpcb
->in6p_faddr
)) ||
219 in_localaddr(tp
->t_inpcb
->inp_faddr
)
224 tp
->snd_cwnd
= tp
->t_maxseg
* ss_fltsz_local
;
226 tp
->snd_cwnd
= tp
->t_maxseg
* ss_fltsz
;
230 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
235 KERNEL_DEBUG(DBG_LAYER_BEG
,
236 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
237 (((tp
->t_inpcb
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
238 (tp
->t_inpcb
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
245 KERNEL_DEBUG(DBG_LAYER_BEG
,
246 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
247 (((tp
->t_inpcb
->inp_laddr
.s_addr
& 0xffff) << 16) |
248 (tp
->t_inpcb
->inp_faddr
.s_addr
& 0xffff)),
251 * If the route generation id changed, we need to check that our
252 * local (source) IP address is still valid. If it isn't either
253 * return error or silently do nothing (assuming the address will
254 * come back before the TCP connection times out).
257 if ((tp
->t_inpcb
->inp_route
.ro_rt
!= NULL
&&
258 (tp
->t_inpcb
->inp_route
.ro_rt
->generation_id
!= route_generation
)) || (tp
->t_inpcb
->inp_route
.ro_rt
== NULL
)) {
259 /* check that the source address is still valid */
260 if (ifa_foraddr(tp
->t_inpcb
->inp_laddr
.s_addr
) == 0) {
261 if (tp
->t_state
>= TCPS_CLOSE_WAIT
) {
263 return(EADDRNOTAVAIL
);
266 /* set Retransmit timer if it wasn't set
267 * reset Persist timer and shift register as the
268 * adversed peer window may not be valid anymore
271 if (!tp
->t_timer
[TCPT_REXMT
]) {
272 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
273 if (tp
->t_timer
[TCPT_PERSIST
]) {
274 tp
->t_timer
[TCPT_PERSIST
] = 0;
280 error
= ip_output_list(packetlist
, packchain_listadd
, tp
->t_inpcb
->inp_options
, &tp
->t_inpcb
->inp_route
,
281 (so
->so_options
& SO_DONTROUTE
), 0);
284 if (so
->so_flags
& SOF_NOADDRAVAIL
)
285 return(EADDRNOTAVAIL
);
287 return(0); /* silently ignore and keep data in socket */
292 off
= tp
->snd_nxt
- tp
->snd_una
;
293 win
= min(tp
->snd_wnd
, tp
->snd_cwnd
);
294 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0)
295 win
= min(win
, slowlink_wsize
);
297 flags
= tcp_outflags
[tp
->t_state
];
299 * Get standard flags, and add SYN or FIN if requested by 'hidden'
302 if (tp
->t_flags
& TF_NEEDFIN
)
304 if (tp
->t_flags
& TF_NEEDSYN
)
308 * If in persist timeout with window of 0, send 1 byte.
309 * Otherwise, if window is small but nonzero
310 * and timer expired, we will send what we can
311 * and go to transmit state.
316 * If we still have some data to send, then
317 * clear the FIN bit. Usually this would
318 * happen below when it realizes that we
319 * aren't sending all the data. However,
320 * if we have exactly 1 byte of unsent data,
321 * then it won't clear the FIN bit below,
322 * and if we are in persist state, we wind
323 * up sending the packet without recording
324 * that we sent the FIN bit.
326 * We can't just blindly clear the FIN bit,
327 * because if we don't have any more data
328 * to send then the probe will be the FIN
331 if (off
< so
->so_snd
.sb_cc
)
335 tp
->t_timer
[TCPT_PERSIST
] = 0;
340 len
= (long)ulmin(so
->so_snd
.sb_cc
, win
) - off
;
342 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
343 taop
= &tao_noncached
;
344 bzero(taop
, sizeof(*taop
));
348 * Lop off SYN bit if it has already been sent. However, if this
349 * is SYN-SENT state and if segment contains data and if we don't
350 * know that foreign host supports TAO, suppress sending segment.
352 if ((flags
& TH_SYN
) && SEQ_GT(tp
->snd_nxt
, tp
->snd_una
)) {
355 if (len
> 0 && tp
->t_state
== TCPS_SYN_SENT
&&
356 taop
->tao_ccsent
== 0) {
358 error
= ip_output_list(packetlist
, packchain_listadd
, tp
->t_inpcb
->inp_options
, &tp
->t_inpcb
->inp_route
,
359 (so
->so_options
& SO_DONTROUTE
), 0);
362 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
368 * Be careful not to send data and/or FIN on SYN segments
369 * in cases when no CC option will be sent.
370 * This measure is needed to prevent interoperability problems
371 * with not fully conformant TCP implementations.
373 if ((flags
& TH_SYN
) &&
374 ((tp
->t_flags
& TF_NOOPT
) || !(tp
->t_flags
& TF_REQ_CC
) ||
375 ((flags
& TH_ACK
) && !(tp
->t_flags
& TF_RCVD_CC
)))) {
382 * If FIN has been sent but not acked,
383 * but we haven't been called to retransmit,
384 * len will be -1. Otherwise, window shrank
385 * after we sent into it. If window shrank to 0,
386 * cancel pending retransmit, pull snd_nxt back
387 * to (closed) window, and set the persist timer
388 * if it isn't already going. If the window didn't
389 * close completely, just wait for an ACK.
393 tp
->t_timer
[TCPT_REXMT
] = 0;
395 tp
->snd_nxt
= tp
->snd_una
;
396 if (tp
->t_timer
[TCPT_PERSIST
] == 0)
400 if (len
> tp
->t_maxseg
) {
405 if (SEQ_LT(tp
->snd_nxt
+ len
, tp
->snd_una
+ so
->so_snd
.sb_cc
))
408 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0 ) /* Clips window size for slow links */
409 win
= min(sbspace(&so
->so_rcv
), slowlink_wsize
);
411 win
= sbspace(&so
->so_rcv
);
414 * Sender silly window avoidance. If connection is idle
415 * and can send all data, a maximum segment,
416 * at least a maximum default-size segment do it,
417 * or are forced, do it; otherwise don't bother.
418 * If peer's buffer is tiny, then send
419 * when window is at least half open.
420 * If retransmitting (possibly after persist timer forced us
421 * to send into a small window), then must resend.
424 if (len
== tp
->t_maxseg
)
426 if (!(tp
->t_flags
& TF_MORETOCOME
) &&
427 (idle
|| tp
->t_flags
& TF_NODELAY
) &&
428 (tp
->t_flags
& TF_NOPUSH
) == 0 &&
429 len
+ off
>= so
->so_snd
.sb_cc
)
433 if (len
>= tp
->max_sndwnd
/ 2 && tp
->max_sndwnd
> 0)
435 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
))
440 * Compare available window to amount of window
441 * known to peer (as advertised window less
442 * next expected input). If the difference is at least two
443 * max size segments, or at least 50% of the maximum possible
444 * window, then want to send a window update to peer.
448 * "adv" is the amount we can increase the window,
449 * taking into account that we are limited by
450 * TCP_MAXWIN << tp->rcv_scale.
452 long adv
= min(win
, (long)TCP_MAXWIN
<< tp
->rcv_scale
) -
453 (tp
->rcv_adv
- tp
->rcv_nxt
);
455 if (adv
>= (long) (2 * tp
->t_maxseg
))
457 if (2 * adv
>= (long) so
->so_rcv
.sb_hiwat
)
462 * Send if we owe peer an ACK.
464 if (tp
->t_flags
& TF_ACKNOW
)
466 if ((flags
& TH_RST
) ||
467 ((flags
& TH_SYN
) && (tp
->t_flags
& TF_NEEDSYN
) == 0))
469 if (SEQ_GT(tp
->snd_up
, tp
->snd_una
))
472 * If our state indicates that FIN should be sent
473 * and we have not yet done so, or we're retransmitting the FIN,
474 * then we need to send.
476 if (flags
& TH_FIN
&&
477 ((tp
->t_flags
& TF_SENTFIN
) == 0 || tp
->snd_nxt
== tp
->snd_una
))
481 * TCP window updates are not reliable, rather a polling protocol
482 * using ``persist'' packets is used to insure receipt of window
483 * updates. The three ``states'' for the output side are:
484 * idle not doing retransmits or persists
485 * persisting to move a small or zero window
486 * (re)transmitting and thereby not persisting
488 * tp->t_timer[TCPT_PERSIST]
489 * is set when we are in persist state.
491 * is set when we are called to send a persist packet.
492 * tp->t_timer[TCPT_REXMT]
493 * is set when we are retransmitting
494 * The output side is idle when both timers are zero.
496 * If send window is too small, there is data to transmit, and no
497 * retransmit or persist is pending, then go to persist state.
498 * If nothing happens soon, send when timer expires:
499 * if window is nonzero, transmit what we can,
500 * otherwise force out a byte.
502 if (so
->so_snd
.sb_cc
&& tp
->t_timer
[TCPT_REXMT
] == 0 &&
503 tp
->t_timer
[TCPT_PERSIST
] == 0) {
509 * If there is no reason to send a segment, just return.
510 * but if there is some packets left in the packet list, send them now.
513 error
= ip_output_list(packetlist
, packchain_listadd
, tp
->t_inpcb
->inp_options
, &tp
->t_inpcb
->inp_route
,
514 (so
->so_options
& SO_DONTROUTE
), 0);
516 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
521 * Before ESTABLISHED, force sending of initial options
522 * unless TCP set not to do any options.
523 * NOTE: we assume that the IP/TCP header plus TCP options
524 * always fit in a single mbuf, leaving room for a maximum
526 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
531 hdrlen
= sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
);
534 hdrlen
= sizeof (struct tcpiphdr
);
535 if (flags
& TH_SYN
) {
536 tp
->snd_nxt
= tp
->iss
;
537 if ((tp
->t_flags
& TF_NOOPT
) == 0) {
540 opt
[0] = TCPOPT_MAXSEG
;
541 opt
[1] = TCPOLEN_MAXSEG
;
542 mss
= htons((u_short
) tcp_mssopt(tp
));
543 (void)memcpy(opt
+ 2, &mss
, sizeof(mss
));
544 optlen
= TCPOLEN_MAXSEG
;
546 if ((tp
->t_flags
& TF_REQ_SCALE
) &&
547 ((flags
& TH_ACK
) == 0 ||
548 (tp
->t_flags
& TF_RCVD_SCALE
))) {
549 *((u_int32_t
*)(opt
+ optlen
)) = htonl(
551 TCPOPT_WINDOW
<< 16 |
552 TCPOLEN_WINDOW
<< 8 |
553 tp
->request_r_scale
);
560 * Send a timestamp and echo-reply if this is a SYN and our side
561 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
562 * and our peer have sent timestamps in our SYN's.
564 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
565 (flags
& TH_RST
) == 0 &&
566 ((flags
& TH_ACK
) == 0 ||
567 (tp
->t_flags
& TF_RCVD_TSTMP
))) {
568 u_int32_t
*lp
= (u_int32_t
*)(opt
+ optlen
);
570 /* Form timestamp option as shown in appendix A of RFC 1323. */
571 *lp
++ = htonl(TCPOPT_TSTAMP_HDR
);
572 *lp
++ = htonl(tcp_now
);
573 *lp
= htonl(tp
->ts_recent
);
574 optlen
+= TCPOLEN_TSTAMP_APPA
;
578 * Send `CC-family' options if our side wants to use them (TF_REQ_CC),
579 * options are allowed (!TF_NOOPT) and it's not a RST.
581 if ((tp
->t_flags
& (TF_REQ_CC
|TF_NOOPT
)) == TF_REQ_CC
&&
582 (flags
& TH_RST
) == 0) {
583 switch (flags
& (TH_SYN
|TH_ACK
)) {
585 * This is a normal ACK, send CC if we received CC before
589 if (!(tp
->t_flags
& TF_RCVD_CC
))
594 * We can only get here in T/TCP's SYN_SENT* state, when
595 * we're a sending a non-SYN segment without waiting for
596 * the ACK of our SYN. A check above assures that we only
597 * do this if our peer understands T/TCP.
600 opt
[optlen
++] = TCPOPT_NOP
;
601 opt
[optlen
++] = TCPOPT_NOP
;
602 opt
[optlen
++] = TCPOPT_CC
;
603 opt
[optlen
++] = TCPOLEN_CC
;
604 *(u_int32_t
*)&opt
[optlen
] = htonl(tp
->cc_send
);
610 * This is our initial SYN, check whether we have to use
614 opt
[optlen
++] = TCPOPT_NOP
;
615 opt
[optlen
++] = TCPOPT_NOP
;
616 opt
[optlen
++] = tp
->t_flags
& TF_SENDCCNEW
?
617 TCPOPT_CCNEW
: TCPOPT_CC
;
618 opt
[optlen
++] = TCPOLEN_CC
;
619 *(u_int32_t
*)&opt
[optlen
] = htonl(tp
->cc_send
);
624 * This is a SYN,ACK; send CC and CC.echo if we received
627 case (TH_SYN
|TH_ACK
):
628 if (tp
->t_flags
& TF_RCVD_CC
) {
629 opt
[optlen
++] = TCPOPT_NOP
;
630 opt
[optlen
++] = TCPOPT_NOP
;
631 opt
[optlen
++] = TCPOPT_CC
;
632 opt
[optlen
++] = TCPOLEN_CC
;
633 *(u_int32_t
*)&opt
[optlen
] =
636 opt
[optlen
++] = TCPOPT_NOP
;
637 opt
[optlen
++] = TCPOPT_NOP
;
638 opt
[optlen
++] = TCPOPT_CCECHO
;
639 opt
[optlen
++] = TCPOLEN_CC
;
640 *(u_int32_t
*)&opt
[optlen
] =
651 ipoptlen
= ip6_optlen(tp
->t_inpcb
);
655 if (tp
->t_inpcb
->inp_options
) {
656 ipoptlen
= tp
->t_inpcb
->inp_options
->m_len
-
657 offsetof(struct ipoption
, ipopt_list
);
663 if (ipsec_bypass
== 0)
664 ipoptlen
+= ipsec_hdrsiz_tcp(tp
);
668 * Adjust data length if insertion of options will
669 * bump the packet length beyond the t_maxopd length.
670 * Clear the FIN bit because we cut off the tail of
673 if (len
+ optlen
+ ipoptlen
> tp
->t_maxopd
) {
675 * If there is still more to send, don't close the connection.
678 len
= tp
->t_maxopd
- optlen
- ipoptlen
;
683 /*#ifdef DIAGNOSTIC*/
685 if (max_linkhdr
+ hdrlen
> MCLBYTES
)
686 panic("tcphdr too big");
688 if (max_linkhdr
+ hdrlen
> MHLEN
)
689 panic("tcphdr too big");
694 * Grab a header mbuf, attaching a copy of data to
695 * be transmitted, and initialize the header from
696 * the template for sends on this connection.
699 if (tp
->t_force
&& len
== 1)
700 tcpstat
.tcps_sndprobe
++;
701 else if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
)) {
702 tcpstat
.tcps_sndrexmitpack
++;
703 tcpstat
.tcps_sndrexmitbyte
+= len
;
705 tcpstat
.tcps_sndpack
++;
706 tcpstat
.tcps_sndbyte
+= len
;
709 if ((m
= m_copypack(so
->so_snd
.sb_mb
, off
,
710 (int)len
, max_linkhdr
+ hdrlen
)) == 0) {
715 * m_copypack left space for our hdr; use it.
721 * try to use the new interface that allocates all
722 * the necessary mbuf hdrs under 1 mbuf lock and
723 * avoids rescanning the socket mbuf list if
724 * certain conditions are met. This routine can't
725 * be used in the following cases...
726 * 1) the protocol headers exceed the capacity of
727 * of a single mbuf header's data area (no cluster attached)
728 * 2) the length of the data being transmitted plus
729 * the protocol headers fits into a single mbuf header's
730 * data area (no cluster attached)
734 if (MHLEN
< hdrlen
+ max_linkhdr
) {
735 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
740 MCLGET(m
, M_DONTWAIT
);
741 if ((m
->m_flags
& M_EXT
) == 0) {
746 m
->m_data
+= max_linkhdr
;
750 if (len
<= MHLEN
- hdrlen
- max_linkhdr
) {
752 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
757 m
->m_data
+= max_linkhdr
;
760 /* makes sure we still have data left to be sent at this point */
761 if (so
->so_snd
.sb_mb
== NULL
|| off
== -1) {
762 if (m
!= NULL
) m_freem(m
);
763 error
= 0; /* should we return an error? */
766 m_copydata(so
->so_snd
.sb_mb
, off
, (int) len
,
767 mtod(m
, caddr_t
) + hdrlen
);
771 m
->m_next
= m_copy(so
->so_snd
.sb_mb
, off
, (int) len
);
772 if (m
->m_next
== 0) {
779 * determine whether the mbuf pointer and offset passed back by the 'last' call
780 * to m_copym_with_hdrs are still valid... if the head of the socket chain has
781 * changed (due to an incoming ACK for instance), or the offset into the chain we
782 * just computed is different from the one last returned by m_copym_with_hdrs (perhaps
783 * we're re-transmitting a packet sent earlier), than we can't pass the mbuf pointer and
784 * offset into it as valid hints for m_copym_with_hdrs to use (if valid, these hints allow
785 * m_copym_with_hdrs to avoid rescanning from the beginning of the socket buffer mbuf list.
786 * setting the mbuf pointer to NULL is sufficient to disable the hint mechanism.
788 if (m_head
!= so
->so_snd
.sb_mb
|| last_off
!= off
)
790 last_off
= off
+ len
;
791 m_head
= so
->so_snd
.sb_mb
;
793 /* makes sure we still have data left to be sent at this point */
794 if (m_head
== NULL
) {
795 error
= 0; /* should we return an error? */
800 * m_copym_with_hdrs will always return the last mbuf pointer and the offset into it that
801 * it acted on to fullfill the current request, whether a valid 'hint' was passed in or not
803 if ((m
= m_copym_with_hdrs(so
->so_snd
.sb_mb
, off
, (int) len
, M_DONTWAIT
, &m_last
, &m_off
)) == NULL
) {
807 m
->m_data
+= max_linkhdr
;
813 * If we're sending everything we've got, set PUSH.
814 * (This will keep happy those implementations which only
815 * give data to the user when a buffer fills or
818 if (off
+ len
== so
->so_snd
.sb_cc
)
821 if (tp
->t_flags
& TF_ACKNOW
)
822 tcpstat
.tcps_sndacks
++;
823 else if (flags
& (TH_SYN
|TH_FIN
|TH_RST
))
824 tcpstat
.tcps_sndctrl
++;
825 else if (SEQ_GT(tp
->snd_up
, tp
->snd_una
))
826 tcpstat
.tcps_sndurg
++;
828 tcpstat
.tcps_sndwinup
++;
830 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
836 if (isipv6
&& (MHLEN
< hdrlen
+ max_linkhdr
) &&
841 m
->m_data
+= max_linkhdr
;
844 m
->m_pkthdr
.rcvif
= 0;
847 ip6
= mtod(m
, struct ip6_hdr
*);
848 th
= (struct tcphdr
*)(ip6
+ 1);
849 tcp_fillheaders(tp
, ip6
, th
);
853 ip
= mtod(m
, struct ip
*);
854 ipov
= (struct ipovly
*)ip
;
855 th
= (struct tcphdr
*)(ip
+ 1);
856 /* this picks up the pseudo header (w/o the length) */
857 tcp_fillheaders(tp
, ip
, th
);
861 * Fill in fields, remembering maximum advertised
862 * window for use in delaying messages about window sizes.
863 * If resending a FIN, be sure not to use a new sequence number.
865 if (flags
& TH_FIN
&& tp
->t_flags
& TF_SENTFIN
&&
866 tp
->snd_nxt
== tp
->snd_max
)
869 * If we are doing retransmissions, then snd_nxt will
870 * not reflect the first unsent octet. For ACK only
871 * packets, we do not want the sequence number of the
872 * retransmitted packet, we want the sequence number
873 * of the next unsent octet. So, if there is no data
874 * (and no SYN or FIN), use snd_max instead of snd_nxt
875 * when filling in ti_seq. But if we are in persist
876 * state, snd_max might reflect one byte beyond the
877 * right edge of the window, so use snd_nxt in that
878 * case, since we know we aren't doing a retransmission.
879 * (retransmit and persist are mutually exclusive...)
881 if (len
|| (flags
& (TH_SYN
|TH_FIN
)) || tp
->t_timer
[TCPT_PERSIST
])
882 th
->th_seq
= htonl(tp
->snd_nxt
);
884 th
->th_seq
= htonl(tp
->snd_max
);
885 th
->th_ack
= htonl(tp
->rcv_nxt
);
887 bcopy(opt
, th
+ 1, optlen
);
888 th
->th_off
= (sizeof (struct tcphdr
) + optlen
) >> 2;
890 th
->th_flags
= flags
;
892 * Calculate receive window. Don't shrink window,
893 * but avoid silly window syndrome.
895 if (win
< (long)(so
->so_rcv
.sb_hiwat
/ 4) && win
< (long)tp
->t_maxseg
)
897 if (win
< (long)(tp
->rcv_adv
- tp
->rcv_nxt
))
898 win
= (long)(tp
->rcv_adv
- tp
->rcv_nxt
);
899 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0) {
900 if (win
> (long)slowlink_wsize
)
901 win
= slowlink_wsize
;
902 th
->th_win
= htons((u_short
) (win
>>tp
->rcv_scale
));
906 if (win
> (long)TCP_MAXWIN
<< tp
->rcv_scale
)
907 win
= (long)TCP_MAXWIN
<< tp
->rcv_scale
;
908 th
->th_win
= htons((u_short
) (win
>>tp
->rcv_scale
));
912 * Adjust the RXWIN0SENT flag - indicate that we have advertised
913 * a 0 window. This may cause the remote transmitter to stall. This
914 * flag tells soreceive() to disable delayed acknowledgements when
915 * draining the buffer. This can occur if the receiver is attempting
916 * to read more data then can be buffered prior to transmitting on
920 tp
->t_flags
|= TF_RXWIN0SENT
;
922 tp
->t_flags
&= ~TF_RXWIN0SENT
;
924 if (SEQ_GT(tp
->snd_up
, tp
->snd_nxt
)) {
925 th
->th_urp
= htons((u_short
)(tp
->snd_up
- tp
->snd_nxt
));
926 th
->th_flags
|= TH_URG
;
929 * If no urgent pointer to send, then we pull
930 * the urgent pointer to the left edge of the send window
931 * so that it doesn't drift into the send window on sequence
934 tp
->snd_up
= tp
->snd_una
; /* drag it along */
937 * Put TCP length in extended header, and then
938 * checksum extended header and data.
940 m
->m_pkthdr
.len
= hdrlen
+ len
; /* in6_cksum() need this */
944 * ip6_plen is not need to be filled now, and will be filled
947 th
->th_sum
= in6_cksum(m
, IPPROTO_TCP
, sizeof(struct ip6_hdr
),
948 sizeof(struct tcphdr
) + optlen
+ len
);
952 m
->m_pkthdr
.csum_flags
= CSUM_TCP
;
953 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
955 th
->th_sum
= in_addword(th
->th_sum
,
956 htons((u_short
)(optlen
+ len
)));
958 /* IP version must be set here for ipv4/ipv6 checking later */
959 KASSERT(ip
->ip_v
== IPVERSION
,
960 ("%s: IP version incorrect: %d", __FUNCTION__
, ip
->ip_v
));
964 * In transmit state, time the transmission and arrange for
965 * the retransmit. In persist state, just set snd_max.
967 if (tp
->t_force
== 0 || tp
->t_timer
[TCPT_PERSIST
] == 0) {
968 tcp_seq startseq
= tp
->snd_nxt
;
971 * Advance snd_nxt over sequence space of this segment.
973 if (flags
& (TH_SYN
|TH_FIN
)) {
976 if (flags
& TH_FIN
) {
978 tp
->t_flags
|= TF_SENTFIN
;
982 if (SEQ_GT(tp
->snd_nxt
, tp
->snd_max
)) {
983 tp
->snd_max
= tp
->snd_nxt
;
985 * Time this transmission if not a retransmission and
986 * not currently timing anything.
988 if (tp
->t_rtttime
== 0) {
990 tp
->t_rtseq
= startseq
;
991 tcpstat
.tcps_segstimed
++;
996 * Set retransmit timer if not currently set,
997 * and not doing an ack or a keep-alive probe.
998 * Initial value for retransmit timer is smoothed
999 * round-trip time + 2 * round-trip time variance.
1000 * Initialize shift counter which is used for backoff
1001 * of retransmit time.
1003 if (tp
->t_timer
[TCPT_REXMT
] == 0 &&
1004 tp
->snd_nxt
!= tp
->snd_una
) {
1005 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
1006 if (tp
->t_timer
[TCPT_PERSIST
]) {
1007 tp
->t_timer
[TCPT_PERSIST
] = 0;
1012 if (SEQ_GT(tp
->snd_nxt
+ len
, tp
->snd_max
))
1013 tp
->snd_max
= tp
->snd_nxt
+ len
;
1019 if (so
->so_options
& SO_DEBUG
)
1020 tcp_trace(TA_OUTPUT
, tp
->t_state
, tp
, mtod(m
, void *), th
, 0);
1024 * Fill in IP length and desired time to live and
1025 * send to IP level. There should be a better way
1026 * to handle ttl and tos; we could keep them in
1027 * the template, but need a way to checksum without them.
1030 * m->m_pkthdr.len should have been set before cksum calcuration,
1031 * because in6_cksum() need it.
1036 * we separately set hoplimit for every segment, since the
1037 * user might want to change the value via setsockopt.
1038 * Also, desired default hop limit might be changed via
1039 * Neighbor Discovery.
1041 ip6
->ip6_hlim
= in6_selecthlim(tp
->t_inpcb
,
1042 tp
->t_inpcb
->in6p_route
.ro_rt
?
1043 tp
->t_inpcb
->in6p_route
.ro_rt
->rt_ifp
1046 /* TODO: IPv6 IP6TOS_ECT bit on */
1048 if (ipsec_bypass
== 0 && ipsec_setsocket(m
, so
) != 0) {
1054 m
->m_pkthdr
.socket_id
= socket_id
;
1055 error
= ip6_output(m
,
1056 tp
->t_inpcb
->in6p_outputopts
,
1057 &tp
->t_inpcb
->in6p_route
,
1058 (so
->so_options
& SO_DONTROUTE
), NULL
, NULL
, 0);
1063 ip
->ip_len
= m
->m_pkthdr
.len
;
1066 ip
->ip_ttl
= in6_selecthlim(tp
->t_inpcb
,
1067 tp
->t_inpcb
->in6p_route
.ro_rt
?
1068 tp
->t_inpcb
->in6p_route
.ro_rt
->rt_ifp
1072 ip
->ip_ttl
= tp
->t_inpcb
->inp_ip_ttl
; /* XXX */
1073 ip
->ip_tos
= tp
->t_inpcb
->inp_ip_tos
; /* XXX */
1078 KERNEL_DEBUG(DBG_LAYER_BEG
,
1079 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
1080 (((tp
->t_inpcb
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
1081 (tp
->t_inpcb
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
1087 KERNEL_DEBUG(DBG_LAYER_BEG
,
1088 ((tp
->t_inpcb
->inp_fport
<< 16) | tp
->t_inpcb
->inp_lport
),
1089 (((tp
->t_inpcb
->inp_laddr
.s_addr
& 0xffff) << 16) |
1090 (tp
->t_inpcb
->inp_faddr
.s_addr
& 0xffff)),
1095 * See if we should do MTU discovery. We do it only if the following
1097 * 1) we have a valid route to the destination
1098 * 2) the MTU is not locked (if it is, then discovery has been
1101 if (path_mtu_discovery
1102 && (rt
= tp
->t_inpcb
->inp_route
.ro_rt
)
1103 && rt
->rt_flags
& RTF_UP
1104 && !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
1105 ip
->ip_off
|= IP_DF
;
1108 if (ipsec_bypass
== 0)
1109 ipsec_setsocket(m
, so
);
1113 * The socket is kept locked while sending out packets in ip_output, even if packet chaining is not active.
1116 m
->m_pkthdr
.socket_id
= socket_id
;
1118 m
->m_nextpkt
= NULL
;
1119 lastpacket
->m_nextpkt
= m
;
1121 packchain_listadd
++;
1124 m
->m_nextpkt
= NULL
;
1125 packchain_newlist
++;
1126 packetlist
= lastpacket
= m
;
1127 packchain_listadd
=0;
1130 if ((ipsec_bypass
== 0) || fw_enable
|| sendalot
== 0 || (tp
->t_state
!= TCPS_ESTABLISHED
) ||
1131 (tp
->snd_cwnd
<= (tp
->snd_wnd
/ 4)) ||
1132 (tp
->t_flags
& (TH_PUSH
| TF_ACKNOW
)) || tp
->t_force
!= 0 ||
1133 packchain_listadd
>= tcp_packet_chaining
) {
1134 lastpacket
->m_nextpkt
= 0;
1135 error
= ip_output_list(packetlist
, packchain_listadd
, tp
->t_inpcb
->inp_options
, &tp
->t_inpcb
->inp_route
,
1136 (so
->so_options
& SO_DONTROUTE
), 0);
1137 tp
->t_lastchain
= packchain_listadd
;
1146 tcpstat
.tcps_sndtotal
++;
1147 if (win
> 0 && SEQ_GT(tp
->rcv_nxt
+win
, tp
->rcv_adv
))
1148 tp
->rcv_adv
= tp
->rcv_nxt
+ win
;
1149 tp
->last_ack_sent
= tp
->rcv_nxt
;
1150 tp
->t_flags
&= ~(TF_ACKNOW
|TF_DELACK
);
1157 * We know that the packet was lost, so back out the
1158 * sequence number advance, if any.
1160 if (tp
->t_force
== 0 || !tp
->t_timer
[TCPT_PERSIST
]) {
1162 * No need to check for TH_FIN here because
1163 * the TF_SENTFIN flag handles that case.
1165 if ((flags
& TH_SYN
) == 0)
1166 tp
->snd_nxt
-= howmuchsent
;
1170 if (error
== ENOBUFS
) {
1171 if (!tp
->t_timer
[TCPT_REXMT
] &&
1172 !tp
->t_timer
[TCPT_PERSIST
])
1173 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
1174 tcp_quench(tp
->t_inpcb
, 0);
1176 m_freem_list(packetlist
);
1177 tp
->t_lastchain
= 0;
1178 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1181 if (error
== EMSGSIZE
) {
1183 * ip_output() will have already fixed the route
1184 * for us. tcp_mtudisc() will, as its last action,
1185 * initiate retransmission, so it is important to
1188 tcp_mtudisc(tp
->t_inpcb
, 0);
1190 m_freem_list(packetlist
);
1191 tp
->t_lastchain
= 0;
1192 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1195 if ((error
== EHOSTUNREACH
|| error
== ENETDOWN
)
1196 && TCPS_HAVERCVDSYN(tp
->t_state
)) {
1197 tp
->t_softerror
= error
;
1199 m_freem_list(packetlist
);
1200 tp
->t_lastchain
= 0;
1201 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1205 m_freem_list(packetlist
);
1206 tp
->t_lastchain
= 0;
1207 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1211 tcpstat
.tcps_sndtotal
++;
1214 * Data sent (as far as we can tell).
1215 * If this advertises a larger window than any other segment,
1216 * then remember the size of the advertised window.
1217 * Any pending ACK has now been sent.
1219 if (win
> 0 && SEQ_GT(tp
->rcv_nxt
+win
, tp
->rcv_adv
))
1220 tp
->rcv_adv
= tp
->rcv_nxt
+ win
;
1221 tp
->last_ack_sent
= tp
->rcv_nxt
;
1222 tp
->t_flags
&= ~(TF_ACKNOW
|TF_DELACK
);
1224 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT
| DBG_FUNC_END
,0,0,0,0,0);
1225 if (sendalot
&& (!tcp_do_newreno
|| --maxburst
))
1232 register struct tcpcb
*tp
;
1234 int t
= ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1;
1236 if (tp
->t_timer
[TCPT_REXMT
])
1237 panic("tcp_setpersist: retransmit pending");
1239 * Start/restart persistance timer.
1241 TCPT_RANGESET(tp
->t_timer
[TCPT_PERSIST
],
1242 t
* tcp_backoff
[tp
->t_rxtshift
],
1243 TCPTV_PERSMIN
, TCPTV_PERSMAX
);
1244 if (tp
->t_rxtshift
< TCP_MAXRXTSHIFT
)