2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
34 #include "opt_inet6.h"
35 #include "opt_ipsec.h"
37 #include "opt_tcpdebug.h"
38 #include "opt_tcp_sack.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/domain.h>
43 #include <sys/kernel.h>
47 #include <sys/mutex.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
53 #include <net/route.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
58 #include <netinet/in_pcb.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/ip_options.h>
62 #include <netinet6/in6_pcb.h>
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
66 #include <netinet/tcp.h>
68 #include <netinet/tcp_fsm.h>
69 #include <netinet/tcp_seq.h>
70 #include <netinet/tcp_timer.h>
71 #include <netinet/tcp_var.h>
72 #include <netinet/tcpip.h>
74 #include <netinet/tcp_debug.h>
78 #include <netinet6/ipsec.h>
82 #include <netipsec/ipsec.h>
86 #include <machine/in_cksum.h>
89 extern struct mbuf *m_copypack();
92 int path_mtu_discovery = 1;
93 SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW,
94 &path_mtu_discovery, 1, "Enable Path MTU Discovery");
97 SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW,
98 &ss_fltsz, 1, "Slow start flight size");
100 int ss_fltsz_local = 4;
101 SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW,
102 &ss_fltsz_local, 1, "Slow start flight size for local networks");
104 int tcp_do_newreno = 1;
105 SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno,
106 0, "Enable NewReno Algorithms");
109 * Tcp output routine: figure out what should be sent and send it.
112 tcp_output(struct tcpcb *tp)
114 struct socket *so = tp->t_inpcb->inp_socket;
115 long len, recwin, sendwin;
116 int off, flags, error;
121 struct ip *ip = NULL;
122 struct ipovly *ipov = NULL;
124 u_char opt[TCP_MAXOLEN];
125 unsigned ipoptlen, optlen, hdrlen;
131 int maxburst = TCP_MAXBURST;
134 struct ip6_hdr *ip6 = NULL;
137 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
140 INP_LOCK_ASSERT(tp->t_inpcb);
143 * Determine length of data that should be transmitted,
144 * and flags that will be used.
145 * If there is some data or critical controls (SYN, RST)
146 * to send, then transmit; otherwise, investigate further.
148 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
149 if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) {
151 * We have been idle for "a while" and no acks are
152 * expected to clock out any data we send --
153 * slow start to get ack "clock" running again.
155 * Set the slow-start flight size depending on whether
156 * this is a local network or not.
161 if (in6_localaddr(&tp->t_inpcb->in6p_faddr))
165 if (in_localaddr(tp->t_inpcb->inp_faddr))
167 tp->snd_cwnd = tp->t_maxseg * ss;
169 tp->t_flags &= ~TF_LASTIDLE;
171 if (tp->t_flags & TF_MORETOCOME) {
172 tp->t_flags |= TF_LASTIDLE;
178 * If we've recently taken a timeout, snd_max will be greater than
179 * snd_nxt. There may be SACK information that allows us to avoid
180 * resending already delivered data. Adjust snd_nxt accordingly.
182 if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max))
185 off = tp->snd_nxt - tp->snd_una;
186 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
187 sendwin = min(sendwin, tp->snd_bwnd);
189 flags = tcp_outflags[tp->t_state];
191 * Send any SACK-generated retransmissions. If we're explicitly trying
192 * to send out new data (when sendalot is 1), bypass this function.
193 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
194 * we're replacing a (future) new transmission with a retransmission
195 * now, and we previously incremented snd_cwnd in tcp_input().
198 * Still in sack recovery , reset rxmit flag to zero.
204 if (tp->sack_enable && IN_FASTRECOVERY(tp) &&
205 (p = tcp_sack_output(tp, &sack_bytes_rxmt))) {
208 cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
211 /* Do not retransmit SACK segments beyond snd_recover */
212 if (SEQ_GT(p->end, tp->snd_recover)) {
214 * (At least) part of sack hole extends beyond
215 * snd_recover. Check to see if we can rexmit data
218 if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
220 * Can't rexmit any more data for this hole.
221 * That data will be rexmitted in the next
222 * sack recovery episode, when snd_recover
223 * moves past p->rxmit.
226 goto after_sack_rexmit;
228 /* Can rexmit part of the current hole */
229 len = ((long)ulmin(cwin,
230 tp->snd_recover - p->rxmit));
232 len = ((long)ulmin(cwin, p->end - p->rxmit));
233 off = p->rxmit - tp->snd_una;
234 KASSERT(off >= 0,("%s: sack block to the left of una : %d",
239 tcpstat.tcps_sack_rexmits++;
240 tcpstat.tcps_sack_rexmit_bytes +=
241 min(len, tp->t_maxseg);
246 * Get standard flags, and add SYN or FIN if requested by 'hidden'
249 if (tp->t_flags & TF_NEEDFIN)
251 if (tp->t_flags & TF_NEEDSYN)
254 SOCKBUF_LOCK(&so->so_snd);
256 * If in persist timeout with window of 0, send 1 byte.
257 * Otherwise, if window is small but nonzero
258 * and timer expired, we will send what we can
259 * and go to transmit state.
261 if (tp->t_flags & TF_FORCEDATA) {
264 * If we still have some data to send, then
265 * clear the FIN bit. Usually this would
266 * happen below when it realizes that we
267 * aren't sending all the data. However,
268 * if we have exactly 1 byte of unsent data,
269 * then it won't clear the FIN bit below,
270 * and if we are in persist state, we wind
271 * up sending the packet without recording
272 * that we sent the FIN bit.
274 * We can't just blindly clear the FIN bit,
275 * because if we don't have any more data
276 * to send then the probe will be the FIN
279 if (off < so->so_snd.sb_cc)
283 callout_stop(tp->tt_persist);
289 * If snd_nxt == snd_max and we have transmitted a FIN, the
290 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
291 * a negative length. This can also occur when TCP opens up
292 * its congestion window while receiving additional duplicate
293 * acks after fast-retransmit because TCP will reset snd_nxt
294 * to snd_max after the fast-retransmit.
296 * In the normal retransmit-FIN-only case, however, snd_nxt will
297 * be set to snd_una, the offset will be 0, and the length may
300 * If sack_rxmit is true we are retransmitting from the scoreboard
301 * in which case len is already set.
303 if (sack_rxmit == 0) {
304 if (sack_bytes_rxmt == 0)
305 len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off);
310 * We are inside of a SACK recovery episode and are
311 * sending new data, having retransmitted all the
312 * data possible in the scoreboard.
314 len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd)
317 * Don't remove this (len > 0) check !
318 * We explicitly check for len > 0 here (although it
319 * isn't really necessary), to work around a gcc
320 * optimization issue - to force gcc to compute
321 * len above. Without this check, the computation
322 * of len is bungled by the optimizer.
325 cwin = tp->snd_cwnd -
326 (tp->snd_nxt - tp->sack_newdata) -
330 len = lmin(len, cwin);
336 * Lop off SYN bit if it has already been sent. However, if this
337 * is SYN-SENT state and if segment contains data and if we don't
338 * know that foreign host supports TAO, suppress sending segment.
340 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
341 if (tp->t_state != TCPS_SYN_RECEIVED)
347 * Be careful not to send data and/or FIN on SYN segments.
348 * This measure is needed to prevent interoperability problems
349 * with not fully conformant TCP implementations.
351 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
358 * If FIN has been sent but not acked,
359 * but we haven't been called to retransmit,
360 * len will be < 0. Otherwise, window shrank
361 * after we sent into it. If window shrank to 0,
362 * cancel pending retransmit, pull snd_nxt back
363 * to (closed) window, and set the persist timer
364 * if it isn't already going. If the window didn't
365 * close completely, just wait for an ACK.
369 callout_stop(tp->tt_rexmt);
371 tp->snd_nxt = tp->snd_una;
372 if (!callout_active(tp->tt_persist))
378 * len will be >= 0 after this point. Truncate to the maximum
379 * segment length and ensure that FIN is removed if the length
380 * no longer contains the last data byte.
382 if (len > tp->t_maxseg) {
387 if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
390 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
394 recwin = sbspace(&so->so_rcv);
397 * Sender silly window avoidance. We transmit under the following
398 * conditions when len is non-zero:
400 * - We have a full segment
401 * - This is the last buffer in a write()/send() and we are
402 * either idle or running NODELAY
403 * - we've timed out (e.g. persist timer)
404 * - we have more then 1/2 the maximum send window's worth of
405 * data (receiver may be limited the window size)
406 * - we need to retransmit
409 if (len == tp->t_maxseg)
412 * NOTE! on localhost connections an 'ack' from the remote
413 * end may occur synchronously with the output and cause
414 * us to flush a buffer queued with moretocome. XXX
416 * note: the len + off check is almost certainly unnecessary.
418 if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */
419 (idle || (tp->t_flags & TF_NODELAY)) &&
420 len + off >= so->so_snd.sb_cc &&
421 (tp->t_flags & TF_NOPUSH) == 0) {
424 if (tp->t_flags & TF_FORCEDATA) /* typ. timeout case */
426 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
428 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */
435 * Compare available window to amount of window
436 * known to peer (as advertised window less
437 * next expected input). If the difference is at least two
438 * max size segments, or at least 50% of the maximum possible
439 * window, then want to send a window update to peer.
440 * Skip this if the connection is in T/TCP half-open state.
442 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN)) {
444 * "adv" is the amount we can increase the window,
445 * taking into account that we are limited by
446 * TCP_MAXWIN << tp->rcv_scale.
448 long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) -
449 (tp->rcv_adv - tp->rcv_nxt);
451 if (adv >= (long) (2 * tp->t_maxseg))
453 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
458 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW
459 * is also a catch-all for the retransmit timer timeout case.
461 if (tp->t_flags & TF_ACKNOW)
463 if ((flags & TH_RST) ||
464 ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
466 if (SEQ_GT(tp->snd_up, tp->snd_una))
469 * If our state indicates that FIN should be sent
470 * and we have not yet done so, then we need to send.
472 if (flags & TH_FIN &&
473 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
476 * In SACK, it is possible for tcp_output to fail to send a segment
477 * after the retransmission timer has been turned off. Make sure
478 * that the retransmission timer is set.
480 if (tp->sack_enable && SEQ_GT(tp->snd_max, tp->snd_una) &&
481 !callout_active(tp->tt_rexmt) &&
482 !callout_active(tp->tt_persist)) {
483 callout_reset(tp->tt_rexmt, tp->t_rxtcur,
484 tcp_timer_rexmt, tp);
488 * TCP window updates are not reliable, rather a polling protocol
489 * using ``persist'' packets is used to insure receipt of window
490 * updates. The three ``states'' for the output side are:
491 * idle not doing retransmits or persists
492 * persisting to move a small or zero window
493 * (re)transmitting and thereby not persisting
495 * callout_active(tp->tt_persist)
496 * is true when we are in persist state.
497 * (tp->t_flags & TF_FORCEDATA)
498 * is set when we are called to send a persist packet.
499 * callout_active(tp->tt_rexmt)
500 * is set when we are retransmitting
501 * The output side is idle when both timers are zero.
503 * If send window is too small, there is data to transmit, and no
504 * retransmit or persist is pending, then go to persist state.
505 * If nothing happens soon, send when timer expires:
506 * if window is nonzero, transmit what we can,
507 * otherwise force out a byte.
509 if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) &&
510 !callout_active(tp->tt_persist)) {
516 * No reason to send a segment, just return.
519 SOCKBUF_UNLOCK(&so->so_snd);
523 SOCKBUF_LOCK_ASSERT(&so->so_snd);
525 * Before ESTABLISHED, force sending of initial options
526 * unless TCP set not to do any options.
527 * NOTE: we assume that the IP/TCP header plus TCP options
528 * always fit in a single mbuf, leaving room for a maximum
530 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
535 hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
538 hdrlen = sizeof (struct tcpiphdr);
539 if (flags & TH_SYN) {
540 tp->snd_nxt = tp->iss;
541 if ((tp->t_flags & TF_NOOPT) == 0) {
544 opt[0] = TCPOPT_MAXSEG;
545 opt[1] = TCPOLEN_MAXSEG;
546 mss = htons((u_short) tcp_mssopt(&tp->t_inpcb->inp_inc));
547 (void)memcpy(opt + 2, &mss, sizeof(mss));
548 optlen = TCPOLEN_MAXSEG;
550 if ((tp->t_flags & TF_REQ_SCALE) &&
551 ((flags & TH_ACK) == 0 ||
552 (tp->t_flags & TF_RCVD_SCALE))) {
553 *((u_int32_t *)(opt + optlen)) = htonl(
555 TCPOPT_WINDOW << 16 |
556 TCPOLEN_WINDOW << 8 |
557 tp->request_r_scale);
564 * Send a timestamp and echo-reply if this is a SYN and our side
565 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
566 * and our peer have sent timestamps in our SYN's.
568 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
569 (flags & TH_RST) == 0 &&
570 ((flags & TH_ACK) == 0 ||
571 (tp->t_flags & TF_RCVD_TSTMP))) {
572 u_int32_t *lp = (u_int32_t *)(opt + optlen);
574 /* Form timestamp option as shown in appendix A of RFC 1323. */
575 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
576 *lp++ = htonl(ticks);
577 *lp = htonl(tp->ts_recent);
578 optlen += TCPOLEN_TSTAMP_APPA;
585 if (tp->t_flags & TF_SIGNATURE) {
589 /* Initialize TCP-MD5 option (RFC2385) */
590 bp = (u_char *)opt + optlen;
591 *bp++ = TCPOPT_SIGNATURE;
592 *bp++ = TCPOLEN_SIGNATURE;
594 for (i = 0; i < TCP_SIGLEN; i++)
596 optlen += TCPOLEN_SIGNATURE;
598 #endif /* TCP_SIGNATURE */
600 if (tp->sack_enable && ((tp->t_flags & TF_NOOPT) == 0)) {
602 * Tack on the SACK permitted option *last*.
603 * And do padding of options after tacking this on.
604 * This is because of MSS, TS, WinScale and Signatures are
605 * all present, we have just 2 bytes left for the SACK
606 * permitted option, which is just enough.
609 * If this is the first SYN of connection (not a SYN
610 * ACK), include SACK permitted option. If this is a
611 * SYN ACK, include SACK permitted option if peer has
612 * already done so. This is only for active connect,
613 * since the syncache takes care of the passive connect.
615 if ((flags & TH_SYN) &&
616 (!(flags & TH_ACK) || (tp->t_flags & TF_SACK_PERMIT))) {
618 bp = (u_char *)opt + optlen;
620 *bp++ = TCPOPT_SACK_PERMITTED;
621 *bp++ = TCPOLEN_SACK_PERMITTED;
622 optlen += TCPOLEN_SACK_PERMITTED;
626 * Send SACKs if necessary. This should be the last
627 * option processed. Only as many SACKs are sent as
628 * are permitted by the maximum options size.
630 * In general, SACK blocks consume 8*n+2 bytes.
631 * So a full size SACK blocks option is 34 bytes
632 * (to generate 4 SACK blocks). At a minimum,
633 * we need 10 bytes (to generate 1 SACK block).
634 * If TCP Timestamps (12 bytes) and TCP Signatures
635 * (18 bytes) are both present, we'll just have
636 * 10 bytes for SACK options 40 - (12 + 18).
638 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
639 (tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks > 0 &&
640 MAX_TCPOPTLEN - optlen - 2 >= TCPOLEN_SACK) {
641 int nsack, sackoptlen, padlen;
642 u_char *bp = (u_char *)opt + optlen;
645 nsack = (MAX_TCPOPTLEN - optlen - 2) / TCPOLEN_SACK;
646 nsack = min(nsack, tp->rcv_numsacks);
647 sackoptlen = (2 + nsack * TCPOLEN_SACK);
650 * First we need to pad options so that the
651 * SACK blocks can start at a 4-byte boundary
652 * (sack option and length are at a 2 byte offset).
654 padlen = (MAX_TCPOPTLEN - optlen - sackoptlen) % 4;
659 tcpstat.tcps_sack_send_blocks++;
662 lp = (u_int32_t *)bp;
663 for (i = 0; i < nsack; i++) {
664 struct sackblk sack = tp->sackblks[i];
665 *lp++ = htonl(sack.start);
666 *lp++ = htonl(sack.end);
668 optlen += sackoptlen;
672 /* Pad TCP options to a 4 byte boundary */
673 if (optlen < MAX_TCPOPTLEN && (optlen % sizeof(u_int32_t))) {
674 int pad = sizeof(u_int32_t) - (optlen % sizeof(u_int32_t));
675 u_char *bp = (u_char *)opt + optlen;
688 ipoptlen = ip6_optlen(tp->t_inpcb);
691 if (tp->t_inpcb->inp_options)
692 ipoptlen = tp->t_inpcb->inp_options->m_len -
693 offsetof(struct ipoption, ipopt_list);
697 ipoptlen += ipsec_hdrsiz_tcp(tp);
701 * Adjust data length if insertion of options will
702 * bump the packet length beyond the t_maxopd length.
703 * Clear the FIN bit because we cut off the tail of
706 if (len + optlen + ipoptlen > tp->t_maxopd) {
708 * If there is still more to send, don't close the connection.
711 len = tp->t_maxopd - optlen - ipoptlen;
715 /*#ifdef DIAGNOSTIC*/
717 if (max_linkhdr + hdrlen > MCLBYTES)
719 if (max_linkhdr + hdrlen > MHLEN)
721 panic("tcphdr too big");
725 * Grab a header mbuf, attaching a copy of data to
726 * be transmitted, and initialize the header from
727 * the template for sends on this connection.
730 if ((tp->t_flags & TF_FORCEDATA) && len == 1)
731 tcpstat.tcps_sndprobe++;
732 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
733 tcpstat.tcps_sndrexmitpack++;
734 tcpstat.tcps_sndrexmitbyte += len;
736 tcpstat.tcps_sndpack++;
737 tcpstat.tcps_sndbyte += len;
740 if ((m = m_copypack(so->so_snd.sb_mb, off,
741 (int)len, max_linkhdr + hdrlen)) == 0) {
742 SOCKBUF_UNLOCK(&so->so_snd);
747 * m_copypack left space for our hdr; use it.
752 MGETHDR(m, M_DONTWAIT, MT_DATA);
754 SOCKBUF_UNLOCK(&so->so_snd);
759 if (MHLEN < hdrlen + max_linkhdr) {
760 MCLGET(m, M_DONTWAIT);
761 if ((m->m_flags & M_EXT) == 0) {
762 SOCKBUF_UNLOCK(&so->so_snd);
769 m->m_data += max_linkhdr;
771 if (len <= MHLEN - hdrlen - max_linkhdr) {
772 m_copydata(so->so_snd.sb_mb, off, (int) len,
773 mtod(m, caddr_t) + hdrlen);
776 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
777 if (m->m_next == 0) {
778 SOCKBUF_UNLOCK(&so->so_snd);
786 * If we're sending everything we've got, set PUSH.
787 * (This will keep happy those implementations which only
788 * give data to the user when a buffer fills or
791 if (off + len == so->so_snd.sb_cc)
793 SOCKBUF_UNLOCK(&so->so_snd);
795 SOCKBUF_UNLOCK(&so->so_snd);
796 if (tp->t_flags & TF_ACKNOW)
797 tcpstat.tcps_sndacks++;
798 else if (flags & (TH_SYN|TH_FIN|TH_RST))
799 tcpstat.tcps_sndctrl++;
800 else if (SEQ_GT(tp->snd_up, tp->snd_una))
801 tcpstat.tcps_sndurg++;
803 tcpstat.tcps_sndwinup++;
805 MGETHDR(m, M_DONTWAIT, MT_DATA);
811 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
816 m->m_data += max_linkhdr;
819 SOCKBUF_UNLOCK_ASSERT(&so->so_snd);
820 m->m_pkthdr.rcvif = (struct ifnet *)0;
822 mac_create_mbuf_from_inpcb(tp->t_inpcb, m);
826 ip6 = mtod(m, struct ip6_hdr *);
827 th = (struct tcphdr *)(ip6 + 1);
828 tcpip_fillheaders(tp->t_inpcb, ip6, th);
832 ip = mtod(m, struct ip *);
833 ipov = (struct ipovly *)ip;
834 th = (struct tcphdr *)(ip + 1);
835 tcpip_fillheaders(tp->t_inpcb, ip, th);
839 * Fill in fields, remembering maximum advertised
840 * window for use in delaying messages about window sizes.
841 * If resending a FIN, be sure not to use a new sequence number.
843 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
844 tp->snd_nxt == tp->snd_max)
847 * If we are doing retransmissions, then snd_nxt will
848 * not reflect the first unsent octet. For ACK only
849 * packets, we do not want the sequence number of the
850 * retransmitted packet, we want the sequence number
851 * of the next unsent octet. So, if there is no data
852 * (and no SYN or FIN), use snd_max instead of snd_nxt
853 * when filling in ti_seq. But if we are in persist
854 * state, snd_max might reflect one byte beyond the
855 * right edge of the window, so use snd_nxt in that
856 * case, since we know we aren't doing a retransmission.
857 * (retransmit and persist are mutually exclusive...)
859 if (sack_rxmit == 0) {
860 if (len || (flags & (TH_SYN|TH_FIN))
861 || callout_active(tp->tt_persist))
862 th->th_seq = htonl(tp->snd_nxt);
864 th->th_seq = htonl(tp->snd_max);
866 th->th_seq = htonl(p->rxmit);
868 tp->sackhint.sack_bytes_rexmit += len;
870 th->th_ack = htonl(tp->rcv_nxt);
872 bcopy(opt, th + 1, optlen);
873 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
875 th->th_flags = flags;
877 * Calculate receive window. Don't shrink window,
878 * but avoid silly window syndrome.
880 if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
881 recwin < (long)tp->t_maxseg)
883 if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
884 recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
885 if (recwin > (long)TCP_MAXWIN << tp->rcv_scale)
886 recwin = (long)TCP_MAXWIN << tp->rcv_scale;
887 th->th_win = htons((u_short) (recwin >> tp->rcv_scale));
891 * Adjust the RXWIN0SENT flag - indicate that we have advertised
892 * a 0 window. This may cause the remote transmitter to stall. This
893 * flag tells soreceive() to disable delayed acknowledgements when
894 * draining the buffer. This can occur if the receiver is attempting
895 * to read more data then can be buffered prior to transmitting on
899 tp->t_flags |= TF_RXWIN0SENT;
901 tp->t_flags &= ~TF_RXWIN0SENT;
902 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
903 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
904 th->th_flags |= TH_URG;
907 * If no urgent pointer to send, then we pull
908 * the urgent pointer to the left edge of the send window
909 * so that it doesn't drift into the send window on sequence
912 tp->snd_up = tp->snd_una; /* drag it along */
918 if (tp->t_flags & TF_SIGNATURE)
919 tcp_signature_compute(m, sizeof(struct ip), len, optlen,
920 (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND);
924 * Put TCP length in extended header, and then
925 * checksum extended header and data.
927 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
931 * ip6_plen is not need to be filled now, and will be filled
934 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
935 sizeof(struct tcphdr) + optlen + len);
939 m->m_pkthdr.csum_flags = CSUM_TCP;
940 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
941 th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
942 htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen));
944 /* IP version must be set here for ipv4/ipv6 checking later */
945 KASSERT(ip->ip_v == IPVERSION,
946 ("%s: IP version incorrect: %d", __func__, ip->ip_v));
950 * In transmit state, time the transmission and arrange for
951 * the retransmit. In persist state, just set snd_max.
953 if ((tp->t_flags & TF_FORCEDATA) == 0 ||
954 !callout_active(tp->tt_persist)) {
955 tcp_seq startseq = tp->snd_nxt;
958 * Advance snd_nxt over sequence space of this segment.
960 if (flags & (TH_SYN|TH_FIN)) {
963 if (flags & TH_FIN) {
965 tp->t_flags |= TF_SENTFIN;
971 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
972 tp->snd_max = tp->snd_nxt;
974 * Time this transmission if not a retransmission and
975 * not currently timing anything.
977 if (tp->t_rtttime == 0) {
978 tp->t_rtttime = ticks;
979 tp->t_rtseq = startseq;
980 tcpstat.tcps_segstimed++;
985 * Set retransmit timer if not currently set,
986 * and not doing a pure ack or a keep-alive probe.
987 * Initial value for retransmit timer is smoothed
988 * round-trip time + 2 * round-trip time variance.
989 * Initialize shift counter which is used for backoff
990 * of retransmit time.
993 if (!callout_active(tp->tt_rexmt) &&
994 ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
995 (tp->snd_nxt != tp->snd_una))) {
996 if (callout_active(tp->tt_persist)) {
997 callout_stop(tp->tt_persist);
1000 callout_reset(tp->tt_rexmt, tp->t_rxtcur,
1001 tcp_timer_rexmt, tp);
1005 * Persist case, update snd_max but since we are in
1006 * persist mode (no window) we do not update snd_nxt.
1011 if (flags & TH_FIN) {
1013 tp->t_flags |= TF_SENTFIN;
1015 if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
1016 tp->snd_max = tp->snd_nxt + len;
1023 if (so->so_options & SO_DEBUG) {
1029 save = ipov->ih_len;
1030 ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */);
1032 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
1036 ipov->ih_len = save;
1041 * Fill in IP length and desired time to live and
1042 * send to IP level. There should be a better way
1043 * to handle ttl and tos; we could keep them in
1044 * the template, but need a way to checksum without them.
1047 * m->m_pkthdr.len should have been set before cksum calcuration,
1048 * because in6_cksum() need it.
1053 * we separately set hoplimit for every segment, since the
1054 * user might want to change the value via setsockopt.
1055 * Also, desired default hop limit might be changed via
1056 * Neighbor Discovery.
1058 ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL);
1060 /* TODO: IPv6 IP6TOS_ECT bit on */
1061 error = ip6_output(m,
1062 tp->t_inpcb->in6p_outputopts, NULL,
1063 ((so->so_options & SO_DONTROUTE) ?
1064 IP_ROUTETOIF : 0), NULL, NULL, tp->t_inpcb);
1068 ip->ip_len = m->m_pkthdr.len;
1070 if (INP_CHECK_SOCKAF(so, AF_INET6))
1071 ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL);
1074 * If we do path MTU discovery, then we set DF on every packet.
1075 * This might not be the best thing to do according to RFC3390
1076 * Section 2. However the tcp hostcache migitates the problem
1077 * so it affects only the first tcp connection with a host.
1079 if (path_mtu_discovery)
1080 ip->ip_off |= IP_DF;
1082 error = ip_output(m, tp->t_inpcb->inp_options, NULL,
1083 ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0,
1089 * We know that the packet was lost, so back out the
1090 * sequence number advance, if any.
1092 if ((tp->t_flags & TF_FORCEDATA) == 0 ||
1093 !callout_active(tp->tt_persist)) {
1095 * No need to check for TH_FIN here because
1096 * the TF_SENTFIN flag handles that case.
1098 if ((flags & TH_SYN) == 0) {
1101 tp->sackhint.sack_bytes_rexmit -= len;
1102 KASSERT(tp->sackhint.sack_bytes_rexmit
1104 ("sackhint bytes rtx >= 0"));
1111 SOCKBUF_UNLOCK_ASSERT(&so->so_snd); /* Check gotos. */
1112 if (error == ENOBUFS) {
1113 if (!callout_active(tp->tt_rexmt) &&
1114 !callout_active(tp->tt_persist))
1115 callout_reset(tp->tt_rexmt, tp->t_rxtcur,
1116 tcp_timer_rexmt, tp);
1117 tp->snd_cwnd = tp->t_maxseg;
1120 if (error == EMSGSIZE) {
1122 * ip_output() will have already fixed the route
1123 * for us. tcp_mtudisc() will, as its last action,
1124 * initiate retransmission, so it is important to
1127 tcp_mtudisc(tp->t_inpcb, 0);
1130 if ((error == EHOSTUNREACH || error == ENETDOWN)
1131 && TCPS_HAVERCVDSYN(tp->t_state)) {
1132 tp->t_softerror = error;
1137 tcpstat.tcps_sndtotal++;
1140 * Data sent (as far as we can tell).
1141 * If this advertises a larger window than any other segment,
1142 * then remember the size of the advertised window.
1143 * Any pending ACK has now been sent.
1145 if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
1146 tp->rcv_adv = tp->rcv_nxt + recwin;
1147 tp->last_ack_sent = tp->rcv_nxt;
1148 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
1149 if (callout_active(tp->tt_delack))
1150 callout_stop(tp->tt_delack);
1153 * This completely breaks TCP if newreno is turned on. What happens
1154 * is that if delayed-acks are turned on on the receiver, this code
1155 * on the transmitter effectively destroys the TCP window, forcing
1156 * it to four packets (1.5Kx4 = 6K window).
1158 if (sendalot && (!tcp_do_newreno || --maxburst))
1168 register struct tcpcb *tp;
1170 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
1173 if (callout_active(tp->tt_rexmt))
1174 panic("tcp_setpersist: retransmit pending");
1176 * Start/restart persistance timer.
1178 TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
1179 TCPTV_PERSMIN, TCPTV_PERSMAX);
1180 callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp);
1181 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)