1 //==========================================================================
3 // src/sys/netinet/tcp_output.c
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
23 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
55 * $FreeBSD: src/sys/netinet/tcp_output.c,v 1.39.2.10 2001/07/07 04:30:38 silby Exp $
58 #include <sys/param.h>
59 #include <sys/sysctl.h>
61 #include <sys/domain.h>
62 #include <sys/protosw.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
66 #include <net/route.h>
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 #include <netinet/in_pcb.h>
72 #include <netinet/ip_var.h>
74 #include <netinet6/in6_pcb.h>
75 #include <netinet/ip6.h>
76 #include <netinet6/ip6_var.h>
78 #include <netinet/tcp.h>
80 #include <netinet/tcp_fsm.h>
81 #include <netinet/tcp_seq.h>
82 #include <netinet/tcp_timer.h>
83 #include <netinet/tcp_var.h>
84 #include <netinet/tcpip.h>
86 #include <netinet/tcp_debug.h>
90 #include <netinet6/ipsec.h>
94 extern struct mbuf *m_copypack();
97 static int path_mtu_discovery = 1;
98 SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW,
99 &path_mtu_discovery, 1, "Enable Path MTU Discovery");
102 SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW,
103 &ss_fltsz, 1, "Slow start flight size");
105 int ss_fltsz_local = TCP_MAXWIN; /* something large */
106 SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW,
107 &ss_fltsz_local, 1, "Slow start flight size for local networks");
109 int tcp_do_newreno = 1;
110 SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno,
111 0, "Enable NewReno Algorithms");
113 * Tcp output routine: figure out what should be sent and send it.
117 register struct tcpcb *tp;
119 register struct socket *so = tp->t_inpcb->inp_socket;
120 register long len, win;
121 int off, flags, error;
122 register struct mbuf *m;
123 struct ip *ip = NULL;
124 register struct ipovly *ipov = NULL;
126 struct ip6_hdr *ip6 = NULL;
128 register struct tcphdr *th;
129 u_char opt[TCP_MAXOLEN];
130 unsigned ipoptlen, optlen, hdrlen;
132 int maxburst = TCP_MAXBURST;
133 struct rmxp_tao *taop;
134 struct rmxp_tao tao_noncached;
140 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
144 * Determine length of data that should be transmitted,
145 * and flags that will be used.
146 * If there is some data or critical controls (SYN, RST)
147 * to send, then transmit; otherwise, investigate further.
149 idle = (tp->snd_max == tp->snd_una);
150 if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) {
152 * We have been idle for "a while" and no acks are
153 * expected to clock out any data we send --
154 * slow start to get ack "clock" running again.
156 * Set the slow-start flight size depending on whether
157 * this is a local network or not.
161 (isipv6 && in6_localaddr(&tp->t_inpcb->in6p_faddr)) ||
164 in_localaddr(tp->t_inpcb->inp_faddr)
169 tp->snd_cwnd = tp->t_maxseg * ss_fltsz_local;
171 tp->snd_cwnd = tp->t_maxseg * ss_fltsz;
175 off = tp->snd_nxt - tp->snd_una;
176 win = min(tp->snd_wnd, tp->snd_cwnd);
178 flags = tcp_outflags[tp->t_state];
180 * Get standard flags, and add SYN or FIN if requested by 'hidden'
183 if (tp->t_flags & TF_NEEDFIN)
185 if (tp->t_flags & TF_NEEDSYN)
189 * If in persist timeout with window of 0, send 1 byte.
190 * Otherwise, if window is small but nonzero
191 * and timer expired, we will send what we can
192 * and go to transmit state.
197 * If we still have some data to send, then
198 * clear the FIN bit. Usually this would
199 * happen below when it realizes that we
200 * aren't sending all the data. However,
201 * if we have exactly 1 byte of unsent data,
202 * then it won't clear the FIN bit below,
203 * and if we are in persist state, we wind
204 * up sending the packet without recording
205 * that we sent the FIN bit.
207 * We can't just blindly clear the FIN bit,
208 * because if we don't have any more data
209 * to send then the probe will be the FIN
212 if (off < so->so_snd.sb_cc)
216 callout_stop(tp->tt_persist);
221 len = (long)ulmin(so->so_snd.sb_cc, win) - off;
223 if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
224 taop = &tao_noncached;
225 bzero(taop, sizeof(*taop));
229 * Lop off SYN bit if it has already been sent. However, if this
230 * is SYN-SENT state and if segment contains data and if we don't
231 * know that foreign host supports TAO, suppress sending segment.
233 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
236 if (len > 0 && tp->t_state == TCPS_SYN_SENT &&
237 taop->tao_ccsent == 0)
242 * Be careful not to send data and/or FIN on SYN segments
243 * in cases when no CC option will be sent.
244 * This measure is needed to prevent interoperability problems
245 * with not fully conformant TCP implementations.
247 if ((flags & TH_SYN) &&
248 ((tp->t_flags & TF_NOOPT) || !(tp->t_flags & TF_REQ_CC) ||
249 ((flags & TH_ACK) && !(tp->t_flags & TF_RCVD_CC)))) {
256 * If FIN has been sent but not acked,
257 * but we haven't been called to retransmit,
258 * len will be -1. Otherwise, window shrank
259 * after we sent into it. If window shrank to 0,
260 * cancel pending retransmit, pull snd_nxt back
261 * to (closed) window, and set the persist timer
262 * if it isn't already going. If the window didn't
263 * close completely, just wait for an ACK.
267 callout_stop(tp->tt_rexmt);
269 tp->snd_nxt = tp->snd_una;
270 if (!callout_active(tp->tt_persist))
274 if (len > tp->t_maxseg) {
278 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
281 win = sbspace(&so->so_rcv);
284 * Sender silly window avoidance. If connection is idle
285 * and can send all data, a maximum segment,
286 * at least a maximum default-size segment do it,
287 * or are forced, do it; otherwise don't bother.
288 * If peer's buffer is tiny, then send
289 * when window is at least half open.
290 * If retransmitting (possibly after persist timer forced us
291 * to send into a small window), then must resend.
294 if (len == tp->t_maxseg)
296 if (!(tp->t_flags & TF_MORETOCOME) &&
297 (idle || tp->t_flags & TF_NODELAY) &&
298 (tp->t_flags & TF_NOPUSH) == 0 &&
299 len + off >= so->so_snd.sb_cc)
303 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
305 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
310 * Compare available window to amount of window
311 * known to peer (as advertised window less
312 * next expected input). If the difference is at least two
313 * max size segments, or at least 50% of the maximum possible
314 * window, then want to send a window update to peer.
318 * "adv" is the amount we can increase the window,
319 * taking into account that we are limited by
320 * TCP_MAXWIN << tp->rcv_scale.
322 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
323 (tp->rcv_adv - tp->rcv_nxt);
325 if (adv >= (long) (2 * tp->t_maxseg))
327 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
332 * Send if we owe peer an ACK.
334 if (tp->t_flags & TF_ACKNOW)
336 if ((flags & TH_RST) ||
337 ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
339 if (SEQ_GT(tp->snd_up, tp->snd_una))
342 * If our state indicates that FIN should be sent
343 * and we have not yet done so, or we're retransmitting the FIN,
344 * then we need to send.
346 if (flags & TH_FIN &&
347 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
351 * TCP window updates are not reliable, rather a polling protocol
352 * using ``persist'' packets is used to insure receipt of window
353 * updates. The three ``states'' for the output side are:
354 * idle not doing retransmits or persists
355 * persisting to move a small or zero window
356 * (re)transmitting and thereby not persisting
358 * callout_active(tp->tt_persist)
359 * is true when we are in persist state.
361 * is set when we are called to send a persist packet.
362 * callout_active(tp->tt_rexmt)
363 * is set when we are retransmitting
364 * The output side is idle when both timers are zero.
366 * If send window is too small, there is data to transmit, and no
367 * retransmit or persist is pending, then go to persist state.
368 * If nothing happens soon, send when timer expires:
369 * if window is nonzero, transmit what we can,
370 * otherwise force out a byte.
372 if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) &&
373 !callout_active(tp->tt_persist)) {
379 * No reason to send a segment, just return.
385 * Before ESTABLISHED, force sending of initial options
386 * unless TCP set not to do any options.
387 * NOTE: we assume that the IP/TCP header plus TCP options
388 * always fit in a single mbuf, leaving room for a maximum
390 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
395 hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
398 hdrlen = sizeof (struct tcpiphdr);
399 if (flags & TH_SYN) {
400 tp->snd_nxt = tp->iss;
401 if ((tp->t_flags & TF_NOOPT) == 0) {
404 opt[0] = TCPOPT_MAXSEG;
405 opt[1] = TCPOLEN_MAXSEG;
406 mss = htons((u_short) tcp_mssopt(tp));
407 (void)memcpy(opt + 2, &mss, sizeof(mss));
408 optlen = TCPOLEN_MAXSEG;
410 if ((tp->t_flags & TF_REQ_SCALE) &&
411 ((flags & TH_ACK) == 0 ||
412 (tp->t_flags & TF_RCVD_SCALE))) {
413 *((u_int32_t *)(opt + optlen)) = htonl(
415 TCPOPT_WINDOW << 16 |
416 TCPOLEN_WINDOW << 8 |
417 tp->request_r_scale);
424 * Send a timestamp and echo-reply if this is a SYN and our side
425 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
426 * and our peer have sent timestamps in our SYN's.
428 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
429 (flags & TH_RST) == 0 &&
430 ((flags & TH_ACK) == 0 ||
431 (tp->t_flags & TF_RCVD_TSTMP))) {
432 u_int32_t *lp = (u_int32_t *)(opt + optlen);
434 /* Form timestamp option as shown in appendix A of RFC 1323. */
435 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
436 *lp++ = htonl(ticks);
437 *lp = htonl(tp->ts_recent);
438 optlen += TCPOLEN_TSTAMP_APPA;
442 * Send `CC-family' options if our side wants to use them (TF_REQ_CC),
443 * options are allowed (!TF_NOOPT) and it's not a RST.
445 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
446 (flags & TH_RST) == 0) {
447 switch (flags & (TH_SYN|TH_ACK)) {
449 * This is a normal ACK, send CC if we received CC before
453 if (!(tp->t_flags & TF_RCVD_CC))
458 * We can only get here in T/TCP's SYN_SENT* state, when
459 * we're a sending a non-SYN segment without waiting for
460 * the ACK of our SYN. A check above assures that we only
461 * do this if our peer understands T/TCP.
464 opt[optlen++] = TCPOPT_NOP;
465 opt[optlen++] = TCPOPT_NOP;
466 opt[optlen++] = TCPOPT_CC;
467 opt[optlen++] = TCPOLEN_CC;
468 *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
474 * This is our initial SYN, check whether we have to use
478 opt[optlen++] = TCPOPT_NOP;
479 opt[optlen++] = TCPOPT_NOP;
480 opt[optlen++] = tp->t_flags & TF_SENDCCNEW ?
481 TCPOPT_CCNEW : TCPOPT_CC;
482 opt[optlen++] = TCPOLEN_CC;
483 *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
488 * This is a SYN,ACK; send CC and CC.echo if we received
491 case (TH_SYN|TH_ACK):
492 if (tp->t_flags & TF_RCVD_CC) {
493 opt[optlen++] = TCPOPT_NOP;
494 opt[optlen++] = TCPOPT_NOP;
495 opt[optlen++] = TCPOPT_CC;
496 opt[optlen++] = TCPOLEN_CC;
497 *(u_int32_t *)&opt[optlen] =
500 opt[optlen++] = TCPOPT_NOP;
501 opt[optlen++] = TCPOPT_NOP;
502 opt[optlen++] = TCPOPT_CCECHO;
503 opt[optlen++] = TCPOLEN_CC;
504 *(u_int32_t *)&opt[optlen] =
516 ipoptlen = ip6_optlen(tp->t_inpcb);
520 if (tp->t_inpcb->inp_options) {
521 ipoptlen = tp->t_inpcb->inp_options->m_len -
522 offsetof(struct ipoption, ipopt_list);
528 ipoptlen += ipsec_hdrsiz_tcp(tp);
532 * Adjust data length if insertion of options will
533 * bump the packet length beyond the t_maxopd length.
534 * Clear the FIN bit because we cut off the tail of
537 if (len + optlen + ipoptlen > tp->t_maxopd) {
539 * If there is still more to send, don't close the connection.
542 len = tp->t_maxopd - optlen - ipoptlen;
546 /*#ifdef DIAGNOSTIC*/
548 if (max_linkhdr + hdrlen > MCLBYTES)
549 panic("tcphdr too big");
551 if (max_linkhdr + hdrlen > MHLEN)
552 panic("tcphdr too big");
557 * Grab a header mbuf, attaching a copy of data to
558 * be transmitted, and initialize the header from
559 * the template for sends on this connection.
562 if (tp->t_force && len == 1)
563 tcpstat.tcps_sndprobe++;
564 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
565 tcpstat.tcps_sndrexmitpack++;
566 tcpstat.tcps_sndrexmitbyte += len;
568 tcpstat.tcps_sndpack++;
569 tcpstat.tcps_sndbyte += len;
572 if ((m = m_copypack(so->so_snd.sb_mb, off,
573 (int)len, max_linkhdr + hdrlen)) == 0) {
578 * m_copypack left space for our hdr; use it.
583 MGETHDR(m, M_DONTWAIT, MT_HEADER);
589 if (MHLEN < hdrlen + max_linkhdr) {
590 MCLGET(m, M_DONTWAIT);
591 if ((m->m_flags & M_EXT) == 0) {
598 m->m_data += max_linkhdr;
600 if (len <= MHLEN - hdrlen - max_linkhdr) {
601 m_copydata(so->so_snd.sb_mb, off, (int) len,
602 mtod(m, caddr_t) + hdrlen);
605 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
606 if (m->m_next == 0) {
614 * If we're sending everything we've got, set PUSH.
615 * (This will keep happy those implementations which only
616 * give data to the user when a buffer fills or
619 if (off + len == so->so_snd.sb_cc)
622 if (tp->t_flags & TF_ACKNOW)
623 tcpstat.tcps_sndacks++;
624 else if (flags & (TH_SYN|TH_FIN|TH_RST))
625 tcpstat.tcps_sndctrl++;
626 else if (SEQ_GT(tp->snd_up, tp->snd_una))
627 tcpstat.tcps_sndurg++;
629 tcpstat.tcps_sndwinup++;
631 MGETHDR(m, M_DONTWAIT, MT_HEADER);
637 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
642 m->m_data += max_linkhdr;
645 m->m_pkthdr.rcvif = (struct ifnet *)0;
648 ip6 = mtod(m, struct ip6_hdr *);
649 th = (struct tcphdr *)(ip6 + 1);
650 tcp_fillheaders(tp, ip6, th);
654 ip = mtod(m, struct ip *);
655 ipov = (struct ipovly *)ip;
656 th = (struct tcphdr *)(ip + 1);
657 /* this picks up the pseudo header (w/o the length) */
658 tcp_fillheaders(tp, ip, th);
662 * Fill in fields, remembering maximum advertised
663 * window for use in delaying messages about window sizes.
664 * If resending a FIN, be sure not to use a new sequence number.
666 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
667 tp->snd_nxt == tp->snd_max)
670 * If we are doing retransmissions, then snd_nxt will
671 * not reflect the first unsent octet. For ACK only
672 * packets, we do not want the sequence number of the
673 * retransmitted packet, we want the sequence number
674 * of the next unsent octet. So, if there is no data
675 * (and no SYN or FIN), use snd_max instead of snd_nxt
676 * when filling in ti_seq. But if we are in persist
677 * state, snd_max might reflect one byte beyond the
678 * right edge of the window, so use snd_nxt in that
679 * case, since we know we aren't doing a retransmission.
680 * (retransmit and persist are mutually exclusive...)
682 if (len || (flags & (TH_SYN|TH_FIN))
683 || callout_active(tp->tt_persist))
684 th->th_seq = htonl(tp->snd_nxt);
686 th->th_seq = htonl(tp->snd_max);
687 th->th_ack = htonl(tp->rcv_nxt);
689 bcopy(opt, th + 1, optlen);
690 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
692 th->th_flags = flags;
694 * Calculate receive window. Don't shrink window,
695 * but avoid silly window syndrome.
697 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
699 if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
700 win = (long)(tp->rcv_adv - tp->rcv_nxt);
701 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
702 win = (long)TCP_MAXWIN << tp->rcv_scale;
703 th->th_win = htons((u_short) (win>>tp->rcv_scale));
704 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
705 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
706 th->th_flags |= TH_URG;
709 * If no urgent pointer to send, then we pull
710 * the urgent pointer to the left edge of the send window
711 * so that it doesn't drift into the send window on sequence
714 tp->snd_up = tp->snd_una; /* drag it along */
717 * Put TCP length in extended header, and then
718 * checksum extended header and data.
720 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
724 * ip6_plen is not need to be filled now, and will be filled
727 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
728 sizeof(struct tcphdr) + optlen + len);
732 m->m_pkthdr.csum_flags = CSUM_TCP;
733 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
735 th->th_sum = in_addword(th->th_sum,
736 htons((u_short)(optlen + len)));
738 /* IP version must be set here for ipv4/ipv6 checking later */
742 * In transmit state, time the transmission and arrange for
743 * the retransmit. In persist state, just set snd_max.
745 if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
746 tcp_seq startseq = tp->snd_nxt;
749 * Advance snd_nxt over sequence space of this segment.
751 if (flags & (TH_SYN|TH_FIN)) {
754 if (flags & TH_FIN) {
756 tp->t_flags |= TF_SENTFIN;
760 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
761 tp->snd_max = tp->snd_nxt;
763 * Time this transmission if not a retransmission and
764 * not currently timing anything.
766 if (tp->t_rtttime == 0) {
767 tp->t_rtttime = ticks;
768 tp->t_rtseq = startseq;
769 tcpstat.tcps_segstimed++;
774 * Set retransmit timer if not currently set,
775 * and not doing an ack or a keep-alive probe.
776 * Initial value for retransmit timer is smoothed
777 * round-trip time + 2 * round-trip time variance.
778 * Initialize shift counter which is used for backoff
779 * of retransmit time.
781 if (!callout_active(tp->tt_rexmt) &&
782 tp->snd_nxt != tp->snd_una) {
783 if (callout_active(tp->tt_persist)) {
784 callout_stop(tp->tt_persist);
787 callout_reset(tp->tt_rexmt, tp->t_rxtcur,
788 tcp_timer_rexmt, tp);
791 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
792 tp->snd_max = tp->snd_nxt + len;
798 if (so->so_options & SO_DEBUG)
799 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
803 * Fill in IP length and desired time to live and
804 * send to IP level. There should be a better way
805 * to handle ttl and tos; we could keep them in
806 * the template, but need a way to checksum without them.
809 * m->m_pkthdr.len should have been set before cksum calcuration,
810 * because in6_cksum() need it.
815 * we separately set hoplimit for every segment, since the
816 * user might want to change the value via setsockopt.
817 * Also, desired default hop limit might be changed via
818 * Neighbor Discovery.
820 ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb,
821 tp->t_inpcb->in6p_route.ro_rt ?
822 tp->t_inpcb->in6p_route.ro_rt->rt_ifp
825 /* TODO: IPv6 IP6TOS_ECT bit on */
827 if (ipsec_setsocket(m, so) != 0) {
833 error = ip6_output(m,
834 tp->t_inpcb->in6p_outputopts,
835 &tp->t_inpcb->in6p_route,
836 (so->so_options & SO_DONTROUTE), NULL, NULL);
841 ip->ip_len = m->m_pkthdr.len;
843 if (INP_CHECK_SOCKAF(so, AF_INET6))
844 ip->ip_ttl = in6_selecthlim(tp->t_inpcb,
845 tp->t_inpcb->in6p_route.ro_rt ?
846 tp->t_inpcb->in6p_route.ro_rt->rt_ifp
850 ip->ip_ttl = tp->t_inpcb->inp_ip_ttl; /* XXX */
851 ip->ip_tos = tp->t_inpcb->inp_ip_tos; /* XXX */
853 * See if we should do MTU discovery. We do it only if the following
855 * 1) we have a valid route to the destination
856 * 2) the MTU is not locked (if it is, then discovery has been
859 if (path_mtu_discovery
860 && (rt = tp->t_inpcb->inp_route.ro_rt)
861 && rt->rt_flags & RTF_UP
862 && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
866 ipsec_setsocket(m, so);
868 error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
869 (so->so_options & SO_DONTROUTE), 0);
874 * We know that the packet was lost, so back out the
875 * sequence number advance, if any.
877 if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
879 * No need to check for TH_FIN here because
880 * the TF_SENTFIN flag handles that case.
882 if ((flags & TH_SYN) == 0)
887 if (error == ENOBUFS) {
888 if (!callout_active(tp->tt_rexmt) &&
889 !callout_active(tp->tt_persist))
890 callout_reset(tp->tt_rexmt, tp->t_rxtcur,
891 tcp_timer_rexmt, tp);
892 tcp_quench(tp->t_inpcb, 0);
895 if (error == EMSGSIZE) {
897 * ip_output() will have already fixed the route
898 * for us. tcp_mtudisc() will, as its last action,
899 * initiate retransmission, so it is important to
902 tcp_mtudisc(tp->t_inpcb, 0);
905 if ((error == EHOSTUNREACH || error == ENETDOWN)
906 && TCPS_HAVERCVDSYN(tp->t_state)) {
907 tp->t_softerror = error;
912 tcpstat.tcps_sndtotal++;
915 * Data sent (as far as we can tell).
916 * If this advertises a larger window than any other segment,
917 * then remember the size of the advertised window.
918 * Any pending ACK has now been sent.
920 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
921 tp->rcv_adv = tp->rcv_nxt + win;
922 tp->last_ack_sent = tp->rcv_nxt;
923 tp->t_flags &= ~TF_ACKNOW;
924 if (tcp_delack_enabled)
925 callout_stop(tp->tt_delack);
926 if (sendalot && (!tcp_do_newreno || --maxburst))
933 register struct tcpcb *tp;
935 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
938 if (callout_active(tp->tt_rexmt))
939 panic("tcp_setpersist: retransmit pending");
941 * Start/restart persistance timer.
943 TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
944 TCPTV_PERSMIN, TCPTV_PERSMAX);
945 callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp);
946 if (tp->t_rxtshift < tcp_rexmit_shift_max)