The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/tcp_output.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    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.
   16  *
   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
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)tcp_output.c        8.4 (Berkeley) 5/24/95
   30  * $FreeBSD$
   31  */
   32 
   33 #include "opt_inet.h"
   34 #include "opt_inet6.h"
   35 #include "opt_ipsec.h"
   36 #include "opt_mac.h"
   37 #include "opt_tcpdebug.h"
   38 #include "opt_tcp_sack.h"
   39 
   40 #include <sys/param.h>
   41 #include <sys/systm.h>
   42 #include <sys/domain.h>
   43 #include <sys/kernel.h>
   44 #include <sys/lock.h>
   45 #include <sys/mac.h>
   46 #include <sys/mbuf.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>
   52 
   53 #include <net/route.h>
   54 
   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 #ifdef INET6
   61 #include <netinet6/in6_pcb.h>
   62 #include <netinet/ip6.h>
   63 #include <netinet6/ip6_var.h>
   64 #endif
   65 #include <netinet/tcp.h>
   66 #define TCPOUTFLAGS
   67 #include <netinet/tcp_fsm.h>
   68 #include <netinet/tcp_seq.h>
   69 #include <netinet/tcp_timer.h>
   70 #include <netinet/tcp_var.h>
   71 #include <netinet/tcpip.h>
   72 #ifdef TCPDEBUG
   73 #include <netinet/tcp_debug.h>
   74 #endif
   75 
   76 #ifdef IPSEC
   77 #include <netinet6/ipsec.h>
   78 #endif /*IPSEC*/
   79 
   80 #ifdef FAST_IPSEC
   81 #include <netipsec/ipsec.h>
   82 #define IPSEC
   83 #endif /*FAST_IPSEC*/
   84 
   85 #include <machine/in_cksum.h>
   86 
   87 #ifdef notyet
   88 extern struct mbuf *m_copypack();
   89 #endif
   90 
   91 int path_mtu_discovery = 1;
   92 SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW,
   93         &path_mtu_discovery, 1, "Enable Path MTU Discovery");
   94 
   95 int ss_fltsz = 1;
   96 SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW,
   97         &ss_fltsz, 1, "Slow start flight size");
   98 
   99 int ss_fltsz_local = 4;
  100 SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW,
  101         &ss_fltsz_local, 1, "Slow start flight size for local networks");
  102 
  103 int     tcp_do_newreno = 1;
  104 SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno,
  105         0, "Enable NewReno Algorithms");
  106 
  107 /*
  108  * Tcp output routine: figure out what should be sent and send it.
  109  */
  110 int
  111 tcp_output(struct tcpcb *tp)
  112 {
  113         struct socket *so = tp->t_inpcb->inp_socket;
  114         long len, recwin, sendwin;
  115         int off, flags, error;
  116 #ifdef TCP_SIGNATURE
  117         int sigoff = 0;
  118 #endif
  119         struct mbuf *m;
  120         struct ip *ip = NULL;
  121         struct ipovly *ipov = NULL;
  122         struct tcphdr *th;
  123         u_char opt[TCP_MAXOLEN];
  124         unsigned ipoptlen, optlen, hdrlen;
  125         int idle, sendalot;
  126         int i, sack_rxmit;
  127         int sack_bytes_rxmt;
  128         struct sackhole *p;
  129 #if 0
  130         int maxburst = TCP_MAXBURST;
  131 #endif
  132         struct rmxp_tao tao;
  133 #ifdef INET6
  134         struct ip6_hdr *ip6 = NULL;
  135         int isipv6;
  136 
  137         bzero(&tao, sizeof(tao));
  138         isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
  139 #endif
  140 
  141         INP_LOCK_ASSERT(tp->t_inpcb);
  142 
  143         /*
  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.
  148          */
  149         idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
  150         if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) {
  151                 /*
  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.
  155                  *
  156                  * Set the slow-start flight size depending on whether
  157                  * this is a local network or not.
  158                  */
  159                 int ss = ss_fltsz;
  160 #ifdef INET6
  161                 if (isipv6) {
  162                         if (in6_localaddr(&tp->t_inpcb->in6p_faddr))
  163                                 ss = ss_fltsz_local;
  164                 } else
  165 #endif /* INET6 */
  166                 if (in_localaddr(tp->t_inpcb->inp_faddr))
  167                         ss = ss_fltsz_local;
  168                 tp->snd_cwnd = tp->t_maxseg * ss;
  169         }
  170         tp->t_flags &= ~TF_LASTIDLE;
  171         if (idle) {
  172                 if (tp->t_flags & TF_MORETOCOME) {
  173                         tp->t_flags |= TF_LASTIDLE;
  174                         idle = 0;
  175                 }
  176         }
  177 again:
  178         /*
  179          * If we've recently taken a timeout, snd_max will be greater than
  180          * snd_nxt.  There may be SACK information that allows us to avoid
  181          * resending already delivered data.  Adjust snd_nxt accordingly.
  182          */
  183         if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max))
  184                 tcp_sack_adjust(tp);
  185         sendalot = 0;
  186         off = tp->snd_nxt - tp->snd_una;
  187         sendwin = min(tp->snd_wnd, tp->snd_cwnd);
  188         sendwin = min(sendwin, tp->snd_bwnd);
  189 
  190         flags = tcp_outflags[tp->t_state];
  191         /*
  192          * Send any SACK-generated retransmissions.  If we're explicitly trying
  193          * to send out new data (when sendalot is 1), bypass this function.
  194          * If we retransmit in fast recovery mode, decrement snd_cwnd, since
  195          * we're replacing a (future) new transmission with a retransmission
  196          * now, and we previously incremented snd_cwnd in tcp_input().
  197          */
  198         /*
  199          * Still in sack recovery , reset rxmit flag to zero.
  200          */
  201         sack_rxmit = 0;
  202         sack_bytes_rxmt = 0;
  203         len = 0;
  204         p = NULL;
  205         if (tp->sack_enable && IN_FASTRECOVERY(tp) &&
  206             (p = tcp_sack_output(tp, &sack_bytes_rxmt))) {
  207                 long cwin;
  208                 
  209                 cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
  210                 if (cwin < 0)
  211                         cwin = 0;
  212                 /* Do not retransmit SACK segments beyond snd_recover */
  213                 if (SEQ_GT(p->end, tp->snd_recover)) {
  214                         /*
  215                          * (At least) part of sack hole extends beyond
  216                          * snd_recover. Check to see if we can rexmit data
  217                          * for this hole.
  218                          */
  219                         if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
  220                                 /*
  221                                  * Can't rexmit any more data for this hole.
  222                                  * That data will be rexmitted in the next
  223                                  * sack recovery episode, when snd_recover
  224                                  * moves past p->rxmit.
  225                                  */
  226                                 p = NULL;
  227                                 goto after_sack_rexmit;
  228                         } else
  229                                 /* Can rexmit part of the current hole */
  230                                 len = ((long)ulmin(cwin,
  231                                                    tp->snd_recover - p->rxmit));
  232                 } else
  233                         len = ((long)ulmin(cwin, p->end - p->rxmit));
  234                 off = p->rxmit - tp->snd_una;
  235                 KASSERT(off >= 0,("%s: sack block to the left of una : %d",
  236                     __func__, off));
  237                 if (len > 0) {
  238                         sack_rxmit = 1;
  239                         sendalot = 1;
  240                         tcpstat.tcps_sack_rexmits++;
  241                         tcpstat.tcps_sack_rexmit_bytes +=
  242                             min(len, tp->t_maxseg);
  243                 }
  244         }
  245 after_sack_rexmit:
  246         /*
  247          * Get standard flags, and add SYN or FIN if requested by 'hidden'
  248          * state flags.
  249          */
  250         if (tp->t_flags & TF_NEEDFIN)
  251                 flags |= TH_FIN;
  252         if (tp->t_flags & TF_NEEDSYN)
  253                 flags |= TH_SYN;
  254 
  255         SOCKBUF_LOCK(&so->so_snd);
  256         /*
  257          * If in persist timeout with window of 0, send 1 byte.
  258          * Otherwise, if window is small but nonzero
  259          * and timer expired, we will send what we can
  260          * and go to transmit state.
  261          */
  262         if (tp->t_force) {
  263                 if (sendwin == 0) {
  264                         /*
  265                          * If we still have some data to send, then
  266                          * clear the FIN bit.  Usually this would
  267                          * happen below when it realizes that we
  268                          * aren't sending all the data.  However,
  269                          * if we have exactly 1 byte of unsent data,
  270                          * then it won't clear the FIN bit below,
  271                          * and if we are in persist state, we wind
  272                          * up sending the packet without recording
  273                          * that we sent the FIN bit.
  274                          *
  275                          * We can't just blindly clear the FIN bit,
  276                          * because if we don't have any more data
  277                          * to send then the probe will be the FIN
  278                          * itself.
  279                          */
  280                         if (off < so->so_snd.sb_cc)
  281                                 flags &= ~TH_FIN;
  282                         sendwin = 1;
  283                 } else {
  284                         callout_stop(tp->tt_persist);
  285                         tp->t_rxtshift = 0;
  286                 }
  287         }
  288 
  289         /*
  290          * If snd_nxt == snd_max and we have transmitted a FIN, the
  291          * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
  292          * a negative length.  This can also occur when TCP opens up
  293          * its congestion window while receiving additional duplicate
  294          * acks after fast-retransmit because TCP will reset snd_nxt
  295          * to snd_max after the fast-retransmit.
  296          *
  297          * In the normal retransmit-FIN-only case, however, snd_nxt will
  298          * be set to snd_una, the offset will be 0, and the length may
  299          * wind up 0.
  300          *
  301          * If sack_rxmit is true we are retransmitting from the scoreboard
  302          * in which case len is already set.
  303          */
  304         if (sack_rxmit == 0) {
  305                 if (sack_bytes_rxmt == 0)
  306                         len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off);
  307                 else {
  308                         long cwin;
  309 
  310                         /*
  311                          * We are inside of a SACK recovery episode and are
  312                          * sending new data, having retransmitted all the
  313                          * data possible in the scoreboard.
  314                          */
  315                         len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd) 
  316                                - off);
  317                         /*
  318                          * Don't remove this (len > 0) check !
  319                          * We explicitly check for len > 0 here (although it 
  320                          * isn't really necessary), to work around a gcc 
  321                          * optimization issue - to force gcc to compute
  322                          * len above. Without this check, the computation
  323                          * of len is bungled by the optimizer.
  324                          */
  325                         if (len > 0) {
  326                                 cwin = tp->snd_cwnd - 
  327                                         (tp->snd_nxt - tp->sack_newdata) -
  328                                         sack_bytes_rxmt;
  329                                 if (cwin < 0)
  330                                         cwin = 0;
  331                                 len = lmin(len, cwin);
  332                         }
  333                 }
  334         }
  335 
  336         /*
  337          * Lop off SYN bit if it has already been sent.  However, if this
  338          * is SYN-SENT state and if segment contains data and if we don't
  339          * know that foreign host supports TAO, suppress sending segment.
  340          */
  341         if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
  342                 flags &= ~TH_SYN;
  343                 off--, len++;
  344                 if (tcp_do_rfc1644)
  345                         tcp_hc_gettao(&tp->t_inpcb->inp_inc, &tao);
  346                 if (len > 0 && tp->t_state == TCPS_SYN_SENT &&
  347                      tao.tao_ccsent == 0)
  348                         goto just_return;
  349         }
  350 
  351         /*
  352          * Be careful not to send data and/or FIN on SYN segments
  353          * in cases when no CC option will be sent.
  354          * This measure is needed to prevent interoperability problems
  355          * with not fully conformant TCP implementations.
  356          */
  357         if ((flags & TH_SYN) &&
  358             ((tp->t_flags & TF_NOOPT) || !(tp->t_flags & TF_REQ_CC) ||
  359              ((flags & TH_ACK) && !(tp->t_flags & TF_RCVD_CC)))) {
  360                 len = 0;
  361                 flags &= ~TH_FIN;
  362         }
  363 
  364         if (len < 0) {
  365                 /*
  366                  * If FIN has been sent but not acked,
  367                  * but we haven't been called to retransmit,
  368                  * len will be < 0.  Otherwise, window shrank
  369                  * after we sent into it.  If window shrank to 0,
  370                  * cancel pending retransmit, pull snd_nxt back
  371                  * to (closed) window, and set the persist timer
  372                  * if it isn't already going.  If the window didn't
  373                  * close completely, just wait for an ACK.
  374                  */
  375                 len = 0;
  376                 if (sendwin == 0) {
  377                         callout_stop(tp->tt_rexmt);
  378                         tp->t_rxtshift = 0;
  379                         tp->snd_nxt = tp->snd_una;
  380                         if (!callout_active(tp->tt_persist))
  381                                 tcp_setpersist(tp);
  382                 }
  383         }
  384 
  385         /*
  386          * len will be >= 0 after this point.  Truncate to the maximum
  387          * segment length and ensure that FIN is removed if the length
  388          * no longer contains the last data byte.
  389          */
  390         if (len > tp->t_maxseg) {
  391                 len = tp->t_maxseg;
  392                 sendalot = 1;
  393         }
  394         if (sack_rxmit) {
  395                 if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
  396                         flags &= ~TH_FIN;
  397         } else {
  398                 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
  399                         flags &= ~TH_FIN;
  400         }
  401 
  402         recwin = sbspace(&so->so_rcv);
  403 
  404         /*
  405          * Sender silly window avoidance.   We transmit under the following
  406          * conditions when len is non-zero:
  407          *
  408          *      - We have a full segment
  409          *      - This is the last buffer in a write()/send() and we are
  410          *        either idle or running NODELAY
  411          *      - we've timed out (e.g. persist timer)
  412          *      - we have more then 1/2 the maximum send window's worth of
  413          *        data (receiver may be limited the window size)
  414          *      - we need to retransmit
  415          */
  416         if (len) {
  417                 if (len == tp->t_maxseg)
  418                         goto send;
  419                 /*
  420                  * NOTE! on localhost connections an 'ack' from the remote
  421                  * end may occur synchronously with the output and cause
  422                  * us to flush a buffer queued with moretocome.  XXX
  423                  *
  424                  * note: the len + off check is almost certainly unnecessary.
  425                  */
  426                 if (!(tp->t_flags & TF_MORETOCOME) &&   /* normal case */
  427                     (idle || (tp->t_flags & TF_NODELAY)) &&
  428                     len + off >= so->so_snd.sb_cc &&
  429                     (tp->t_flags & TF_NOPUSH) == 0) {
  430                         goto send;
  431                 }
  432                 if (tp->t_force)                        /* typ. timeout case */
  433                         goto send;
  434                 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
  435                         goto send;
  436                 if (SEQ_LT(tp->snd_nxt, tp->snd_max))   /* retransmit case */
  437                         goto send;
  438                 if (sack_rxmit)
  439                         goto send;
  440         }
  441 
  442         /*
  443          * Compare available window to amount of window
  444          * known to peer (as advertised window less
  445          * next expected input).  If the difference is at least two
  446          * max size segments, or at least 50% of the maximum possible
  447          * window, then want to send a window update to peer.
  448          * Skip this if the connection is in T/TCP half-open state.
  449          */
  450         if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN)) {
  451                 /*
  452                  * "adv" is the amount we can increase the window,
  453                  * taking into account that we are limited by
  454                  * TCP_MAXWIN << tp->rcv_scale.
  455                  */
  456                 long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) -
  457                         (tp->rcv_adv - tp->rcv_nxt);
  458 
  459                 if (adv >= (long) (2 * tp->t_maxseg))
  460                         goto send;
  461                 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
  462                         goto send;
  463         }
  464 
  465         /*
  466          * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
  467          * is also a catch-all for the retransmit timer timeout case.
  468          */
  469         if (tp->t_flags & TF_ACKNOW)
  470                 goto send;
  471         if ((flags & TH_RST) ||
  472             ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
  473                 goto send;
  474         if (SEQ_GT(tp->snd_up, tp->snd_una))
  475                 goto send;
  476         /*
  477          * If our state indicates that FIN should be sent
  478          * and we have not yet done so, then we need to send.
  479          */
  480         if (flags & TH_FIN &&
  481             ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
  482                 goto send;
  483         /*
  484          * In SACK, it is possible for tcp_output to fail to send a segment
  485          * after the retransmission timer has been turned off.  Make sure
  486          * that the retransmission timer is set.
  487          */
  488         if (tp->sack_enable && SEQ_GT(tp->snd_max, tp->snd_una) &&
  489             !callout_active(tp->tt_rexmt) &&
  490             !callout_active(tp->tt_persist)) {
  491                 callout_reset(tp->tt_rexmt, tp->t_rxtcur,
  492                               tcp_timer_rexmt, tp);
  493                 goto just_return;
  494         } 
  495         /*
  496          * TCP window updates are not reliable, rather a polling protocol
  497          * using ``persist'' packets is used to insure receipt of window
  498          * updates.  The three ``states'' for the output side are:
  499          *      idle                    not doing retransmits or persists
  500          *      persisting              to move a small or zero window
  501          *      (re)transmitting        and thereby not persisting
  502          *
  503          * callout_active(tp->tt_persist)
  504          *      is true when we are in persist state.
  505          * tp->t_force
  506          *      is set when we are called to send a persist packet.
  507          * callout_active(tp->tt_rexmt)
  508          *      is set when we are retransmitting
  509          * The output side is idle when both timers are zero.
  510          *
  511          * If send window is too small, there is data to transmit, and no
  512          * retransmit or persist is pending, then go to persist state.
  513          * If nothing happens soon, send when timer expires:
  514          * if window is nonzero, transmit what we can,
  515          * otherwise force out a byte.
  516          */
  517         if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) &&
  518             !callout_active(tp->tt_persist)) {
  519                 tp->t_rxtshift = 0;
  520                 tcp_setpersist(tp);
  521         }
  522 
  523         /*
  524          * No reason to send a segment, just return.
  525          */
  526 just_return:
  527         SOCKBUF_UNLOCK(&so->so_snd);
  528         return (0);
  529 
  530 send:
  531         SOCKBUF_LOCK_ASSERT(&so->so_snd);
  532         /*
  533          * Before ESTABLISHED, force sending of initial options
  534          * unless TCP set not to do any options.
  535          * NOTE: we assume that the IP/TCP header plus TCP options
  536          * always fit in a single mbuf, leaving room for a maximum
  537          * link header, i.e.
  538          *      max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
  539          */
  540         optlen = 0;
  541 #ifdef INET6
  542         if (isipv6)
  543                 hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
  544         else
  545 #endif
  546         hdrlen = sizeof (struct tcpiphdr);
  547         if (flags & TH_SYN) {
  548                 tp->snd_nxt = tp->iss;
  549                 if ((tp->t_flags & TF_NOOPT) == 0) {
  550                         u_short mss;
  551 
  552                         opt[0] = TCPOPT_MAXSEG;
  553                         opt[1] = TCPOLEN_MAXSEG;
  554                         mss = htons((u_short) tcp_mssopt(&tp->t_inpcb->inp_inc));
  555                         (void)memcpy(opt + 2, &mss, sizeof(mss));
  556                         optlen = TCPOLEN_MAXSEG;
  557 
  558                         /*
  559                          * If this is the first SYN of connection (not a SYN
  560                          * ACK), include SACK_PERMIT_HDR option.  If this is a
  561                          * SYN ACK, include SACK_PERMIT_HDR option if peer has
  562                          * already done so. This is only for active connect,
  563                          * since the syncache takes care of the passive connect.
  564                          */
  565                         if (tp->sack_enable && ((flags & TH_ACK) == 0 ||
  566                             (tp->t_flags & TF_SACK_PERMIT))) {
  567                                 *((u_int32_t *) (opt + optlen)) =
  568                                         htonl(TCPOPT_SACK_PERMIT_HDR);
  569                                 optlen += 4;
  570                         }
  571                         if ((tp->t_flags & TF_REQ_SCALE) &&
  572                             ((flags & TH_ACK) == 0 ||
  573                             (tp->t_flags & TF_RCVD_SCALE))) {
  574                                 *((u_int32_t *)(opt + optlen)) = htonl(
  575                                         TCPOPT_NOP << 24 |
  576                                         TCPOPT_WINDOW << 16 |
  577                                         TCPOLEN_WINDOW << 8 |
  578                                         tp->request_r_scale);
  579                                 optlen += 4;
  580                         }
  581                 }
  582         }
  583 
  584         /*
  585          * Send a timestamp and echo-reply if this is a SYN and our side
  586          * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
  587          * and our peer have sent timestamps in our SYN's.
  588          */
  589         if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
  590             (flags & TH_RST) == 0 &&
  591             ((flags & TH_ACK) == 0 ||
  592              (tp->t_flags & TF_RCVD_TSTMP))) {
  593                 u_int32_t *lp = (u_int32_t *)(opt + optlen);
  594 
  595                 /* Form timestamp option as shown in appendix A of RFC 1323. */
  596                 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
  597                 *lp++ = htonl(ticks);
  598                 *lp   = htonl(tp->ts_recent);
  599                 optlen += TCPOLEN_TSTAMP_APPA;
  600         }
  601 
  602         /*
  603          * Send SACKs if necessary.  This should be the last option processed.
  604          * Only as many SACKs are sent as are permitted by the maximum options
  605          * size.  No more than three SACKs are sent.
  606          */
  607         if (tp->sack_enable && tp->t_state == TCPS_ESTABLISHED &&
  608             (tp->t_flags & (TF_SACK_PERMIT|TF_NOOPT)) == TF_SACK_PERMIT &&
  609             tp->rcv_numsacks) {
  610                 u_int32_t *lp = (u_int32_t *)(opt + optlen);
  611                 u_int32_t *olp = lp++;
  612                 int count = 0;  /* actual number of SACKs inserted */
  613                 int maxsack = (MAX_TCPOPTLEN - (optlen + 4))/TCPOLEN_SACK;
  614 
  615                 tcpstat.tcps_sack_send_blocks++;
  616                 maxsack = min(maxsack, TCP_MAX_SACK);
  617                 for (i = 0; (i < tp->rcv_numsacks && count < maxsack); i++) {
  618                         struct sackblk sack = tp->sackblks[i];
  619                         if (sack.start == 0 && sack.end == 0)
  620                                 continue;
  621                         *lp++ = htonl(sack.start);
  622                         *lp++ = htonl(sack.end);
  623                         count++;
  624                 }
  625                 *olp = htonl(TCPOPT_SACK_HDR|(TCPOLEN_SACK*count+2));
  626                 optlen += TCPOLEN_SACK*count + 4; /* including leading NOPs */
  627         }
  628         /*
  629          * Send `CC-family' options if our side wants to use them (TF_REQ_CC),
  630          * options are allowed (!TF_NOOPT) and it's not a RST.
  631          */
  632         if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
  633              (flags & TH_RST) == 0) {
  634                 switch (flags & (TH_SYN|TH_ACK)) {
  635                 /*
  636                  * This is a normal ACK, send CC if we received CC before
  637                  * from our peer.
  638                  */
  639                 case TH_ACK:
  640                         if (!(tp->t_flags & TF_RCVD_CC))
  641                                 break;
  642                         /*FALLTHROUGH*/
  643 
  644                 /*
  645                  * We can only get here in T/TCP's SYN_SENT* state, when
  646                  * we're a sending a non-SYN segment without waiting for
  647                  * the ACK of our SYN.  A check above assures that we only
  648                  * do this if our peer understands T/TCP.
  649                  */
  650                 case 0:
  651                         opt[optlen++] = TCPOPT_NOP;
  652                         opt[optlen++] = TCPOPT_NOP;
  653                         opt[optlen++] = TCPOPT_CC;
  654                         opt[optlen++] = TCPOLEN_CC;
  655                         *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
  656 
  657                         optlen += 4;
  658                         break;
  659 
  660                 /*
  661                  * This is our initial SYN, check whether we have to use
  662                  * CC or CC.new.
  663                  */
  664                 case TH_SYN:
  665                         opt[optlen++] = TCPOPT_NOP;
  666                         opt[optlen++] = TCPOPT_NOP;
  667                         opt[optlen++] = tp->t_flags & TF_SENDCCNEW ?
  668                                                 TCPOPT_CCNEW : TCPOPT_CC;
  669                         opt[optlen++] = TCPOLEN_CC;
  670                         *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
  671                         optlen += 4;
  672                         break;
  673 
  674                 /*
  675                  * This is a SYN,ACK; send CC and CC.echo if we received
  676                  * CC from our peer.
  677                  */
  678                 case (TH_SYN|TH_ACK):
  679                         if (tp->t_flags & TF_RCVD_CC) {
  680                                 opt[optlen++] = TCPOPT_NOP;
  681                                 opt[optlen++] = TCPOPT_NOP;
  682                                 opt[optlen++] = TCPOPT_CC;
  683                                 opt[optlen++] = TCPOLEN_CC;
  684                                 *(u_int32_t *)&opt[optlen] =
  685                                         htonl(tp->cc_send);
  686                                 optlen += 4;
  687                                 opt[optlen++] = TCPOPT_NOP;
  688                                 opt[optlen++] = TCPOPT_NOP;
  689                                 opt[optlen++] = TCPOPT_CCECHO;
  690                                 opt[optlen++] = TCPOLEN_CC;
  691                                 *(u_int32_t *)&opt[optlen] =
  692                                         htonl(tp->cc_recv);
  693                                 optlen += 4;
  694                         }
  695                         break;
  696                 }
  697         }
  698 
  699 #ifdef TCP_SIGNATURE
  700 #ifdef INET6
  701         if (!isipv6)
  702 #endif
  703         if (tp->t_flags & TF_SIGNATURE) {
  704                 int i;
  705                 u_char *bp;
  706 
  707                 /* Initialize TCP-MD5 option (RFC2385) */
  708                 bp = (u_char *)opt + optlen;
  709                 *bp++ = TCPOPT_SIGNATURE;
  710                 *bp++ = TCPOLEN_SIGNATURE;
  711                 sigoff = optlen + 2;
  712                 for (i = 0; i < TCP_SIGLEN; i++)
  713                         *bp++ = 0;
  714                 optlen += TCPOLEN_SIGNATURE;
  715 
  716                 /* Terminate options list and maintain 32-bit alignment. */
  717                 *bp++ = TCPOPT_NOP;
  718                 *bp++ = TCPOPT_EOL;
  719                 optlen += 2;
  720         }
  721 #endif /* TCP_SIGNATURE */
  722 
  723         hdrlen += optlen;
  724 
  725 #ifdef INET6
  726         if (isipv6)
  727                 ipoptlen = ip6_optlen(tp->t_inpcb);
  728         else
  729 #endif
  730         if (tp->t_inpcb->inp_options)
  731                 ipoptlen = tp->t_inpcb->inp_options->m_len -
  732                                 offsetof(struct ipoption, ipopt_list);
  733         else
  734                 ipoptlen = 0;
  735 #ifdef IPSEC
  736         ipoptlen += ipsec_hdrsiz_tcp(tp);
  737 #endif
  738 
  739         /*
  740          * Adjust data length if insertion of options will
  741          * bump the packet length beyond the t_maxopd length.
  742          * Clear the FIN bit because we cut off the tail of
  743          * the segment.
  744          */
  745         if (len + optlen + ipoptlen > tp->t_maxopd) {
  746                 /*
  747                  * If there is still more to send, don't close the connection.
  748                  */
  749                 flags &= ~TH_FIN;
  750                 len = tp->t_maxopd - optlen - ipoptlen;
  751                 sendalot = 1;
  752         }
  753 
  754 /*#ifdef DIAGNOSTIC*/
  755 #ifdef INET6
  756         if (max_linkhdr + hdrlen > MCLBYTES)
  757 #else
  758         if (max_linkhdr + hdrlen > MHLEN)
  759 #endif
  760                 panic("tcphdr too big");
  761 /*#endif*/
  762 
  763         /*
  764          * Grab a header mbuf, attaching a copy of data to
  765          * be transmitted, and initialize the header from
  766          * the template for sends on this connection.
  767          */
  768         if (len) {
  769                 if (tp->t_force && len == 1)
  770                         tcpstat.tcps_sndprobe++;
  771                 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
  772                         tcpstat.tcps_sndrexmitpack++;
  773                         tcpstat.tcps_sndrexmitbyte += len;
  774                 } else {
  775                         tcpstat.tcps_sndpack++;
  776                         tcpstat.tcps_sndbyte += len;
  777                 }
  778 #ifdef notyet
  779                 if ((m = m_copypack(so->so_snd.sb_mb, off,
  780                     (int)len, max_linkhdr + hdrlen)) == 0) {
  781                         SOCKBUF_UNLOCK(&so->so_snd);
  782                         error = ENOBUFS;
  783                         goto out;
  784                 }
  785                 /*
  786                  * m_copypack left space for our hdr; use it.
  787                  */
  788                 m->m_len += hdrlen;
  789                 m->m_data -= hdrlen;
  790 #else
  791                 MGETHDR(m, M_DONTWAIT, MT_HEADER);
  792                 if (m == NULL) {
  793                         SOCKBUF_UNLOCK(&so->so_snd);
  794                         error = ENOBUFS;
  795                         goto out;
  796                 }
  797 #ifdef INET6
  798                 if (MHLEN < hdrlen + max_linkhdr) {
  799                         MCLGET(m, M_DONTWAIT);
  800                         if ((m->m_flags & M_EXT) == 0) {
  801                                 SOCKBUF_UNLOCK(&so->so_snd);
  802                                 m_freem(m);
  803                                 error = ENOBUFS;
  804                                 goto out;
  805                         }
  806                 }
  807 #endif
  808                 m->m_data += max_linkhdr;
  809                 m->m_len = hdrlen;
  810                 if (len <= MHLEN - hdrlen - max_linkhdr) {
  811                         m_copydata(so->so_snd.sb_mb, off, (int) len,
  812                             mtod(m, caddr_t) + hdrlen);
  813                         m->m_len += len;
  814                 } else {
  815                         m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
  816                         if (m->m_next == 0) {
  817                                 SOCKBUF_UNLOCK(&so->so_snd);
  818                                 (void) m_free(m);
  819                                 error = ENOBUFS;
  820                                 goto out;
  821                         }
  822                 }
  823 #endif
  824                 /*
  825                  * If we're sending everything we've got, set PUSH.
  826                  * (This will keep happy those implementations which only
  827                  * give data to the user when a buffer fills or
  828                  * a PUSH comes in.)
  829                  */
  830                 if (off + len == so->so_snd.sb_cc)
  831                         flags |= TH_PUSH;
  832                 SOCKBUF_UNLOCK(&so->so_snd);
  833         } else {
  834                 SOCKBUF_UNLOCK(&so->so_snd);
  835                 if (tp->t_flags & TF_ACKNOW)
  836                         tcpstat.tcps_sndacks++;
  837                 else if (flags & (TH_SYN|TH_FIN|TH_RST))
  838                         tcpstat.tcps_sndctrl++;
  839                 else if (SEQ_GT(tp->snd_up, tp->snd_una))
  840                         tcpstat.tcps_sndurg++;
  841                 else
  842                         tcpstat.tcps_sndwinup++;
  843 
  844                 MGETHDR(m, M_DONTWAIT, MT_HEADER);
  845                 if (m == NULL) {
  846                         error = ENOBUFS;
  847                         goto out;
  848                 }
  849 #ifdef INET6
  850                 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
  851                     MHLEN >= hdrlen) {
  852                         MH_ALIGN(m, hdrlen);
  853                 } else
  854 #endif
  855                 m->m_data += max_linkhdr;
  856                 m->m_len = hdrlen;
  857         }
  858         SOCKBUF_UNLOCK_ASSERT(&so->so_snd);
  859         m->m_pkthdr.rcvif = (struct ifnet *)0;
  860 #ifdef MAC
  861         mac_create_mbuf_from_inpcb(tp->t_inpcb, m);
  862 #endif
  863 #ifdef INET6
  864         if (isipv6) {
  865                 ip6 = mtod(m, struct ip6_hdr *);
  866                 th = (struct tcphdr *)(ip6 + 1);
  867                 tcpip_fillheaders(tp->t_inpcb, ip6, th);
  868         } else
  869 #endif /* INET6 */
  870         {
  871                 ip = mtod(m, struct ip *);
  872                 ipov = (struct ipovly *)ip;
  873                 th = (struct tcphdr *)(ip + 1);
  874                 tcpip_fillheaders(tp->t_inpcb, ip, th);
  875         }
  876 
  877         /*
  878          * Fill in fields, remembering maximum advertised
  879          * window for use in delaying messages about window sizes.
  880          * If resending a FIN, be sure not to use a new sequence number.
  881          */
  882         if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
  883             tp->snd_nxt == tp->snd_max)
  884                 tp->snd_nxt--;
  885         /*
  886          * If we are doing retransmissions, then snd_nxt will
  887          * not reflect the first unsent octet.  For ACK only
  888          * packets, we do not want the sequence number of the
  889          * retransmitted packet, we want the sequence number
  890          * of the next unsent octet.  So, if there is no data
  891          * (and no SYN or FIN), use snd_max instead of snd_nxt
  892          * when filling in ti_seq.  But if we are in persist
  893          * state, snd_max might reflect one byte beyond the
  894          * right edge of the window, so use snd_nxt in that
  895          * case, since we know we aren't doing a retransmission.
  896          * (retransmit and persist are mutually exclusive...)
  897          */
  898         if (sack_rxmit == 0) {
  899                 if (len || (flags & (TH_SYN|TH_FIN))
  900                     || callout_active(tp->tt_persist))
  901                         th->th_seq = htonl(tp->snd_nxt);
  902                 else
  903                         th->th_seq = htonl(tp->snd_max);
  904         } else {
  905                 th->th_seq = htonl(p->rxmit);
  906                 p->rxmit += len;
  907         }
  908         th->th_ack = htonl(tp->rcv_nxt);
  909         if (optlen) {
  910                 bcopy(opt, th + 1, optlen);
  911                 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
  912         }
  913         th->th_flags = flags;
  914         /*
  915          * Calculate receive window.  Don't shrink window,
  916          * but avoid silly window syndrome.
  917          */
  918         if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
  919             recwin < (long)tp->t_maxseg)
  920                 recwin = 0;
  921         if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
  922                 recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
  923         if (recwin > (long)TCP_MAXWIN << tp->rcv_scale)
  924                 recwin = (long)TCP_MAXWIN << tp->rcv_scale;
  925         th->th_win = htons((u_short) (recwin >> tp->rcv_scale));
  926 
  927 
  928         /*
  929          * Adjust the RXWIN0SENT flag - indicate that we have advertised
  930          * a 0 window.  This may cause the remote transmitter to stall.  This
  931          * flag tells soreceive() to disable delayed acknowledgements when
  932          * draining the buffer.  This can occur if the receiver is attempting
  933          * to read more data then can be buffered prior to transmitting on
  934          * the connection.
  935          */
  936         if (recwin == 0)
  937                 tp->t_flags |= TF_RXWIN0SENT;
  938         else
  939                 tp->t_flags &= ~TF_RXWIN0SENT;
  940         if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
  941                 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
  942                 th->th_flags |= TH_URG;
  943         } else
  944                 /*
  945                  * If no urgent pointer to send, then we pull
  946                  * the urgent pointer to the left edge of the send window
  947                  * so that it doesn't drift into the send window on sequence
  948                  * number wraparound.
  949                  */
  950                 tp->snd_up = tp->snd_una;               /* drag it along */
  951 
  952 #ifdef TCP_SIGNATURE
  953 #ifdef INET6
  954         if (!isipv6)
  955 #endif
  956         if (tp->t_flags & TF_SIGNATURE)
  957                 tcp_signature_compute(m, sizeof(struct ip), len, optlen,
  958                     (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND);
  959 #endif
  960 
  961         /*
  962          * Put TCP length in extended header, and then
  963          * checksum extended header and data.
  964          */
  965         m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
  966 #ifdef INET6
  967         if (isipv6)
  968                 /*
  969                  * ip6_plen is not need to be filled now, and will be filled
  970                  * in ip6_output.
  971                  */
  972                 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
  973                                        sizeof(struct tcphdr) + optlen + len);
  974         else
  975 #endif /* INET6 */
  976         {
  977                 m->m_pkthdr.csum_flags = CSUM_TCP;
  978                 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
  979                 th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
  980                     htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen));
  981 
  982                 /* IP version must be set here for ipv4/ipv6 checking later */
  983                 KASSERT(ip->ip_v == IPVERSION,
  984                     ("%s: IP version incorrect: %d", __func__, ip->ip_v));
  985         }
  986 
  987         /*
  988          * In transmit state, time the transmission and arrange for
  989          * the retransmit.  In persist state, just set snd_max.
  990          */
  991         if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
  992                 tcp_seq startseq = tp->snd_nxt;
  993 
  994                 /*
  995                  * Advance snd_nxt over sequence space of this segment.
  996                  */
  997                 if (flags & (TH_SYN|TH_FIN)) {
  998                         if (flags & TH_SYN)
  999                                 tp->snd_nxt++;
 1000                         if (flags & TH_FIN) {
 1001                                 tp->snd_nxt++;
 1002                                 tp->t_flags |= TF_SENTFIN;
 1003                         }
 1004                 }
 1005                 if (sack_rxmit)
 1006                         goto timer;
 1007                 tp->snd_nxt += len;
 1008                 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
 1009                         tp->snd_max = tp->snd_nxt;
 1010                         /*
 1011                          * Time this transmission if not a retransmission and
 1012                          * not currently timing anything.
 1013                          */
 1014                         if (tp->t_rtttime == 0) {
 1015                                 tp->t_rtttime = ticks;
 1016                                 tp->t_rtseq = startseq;
 1017                                 tcpstat.tcps_segstimed++;
 1018                         }
 1019                 }
 1020 
 1021                 /*
 1022                  * Set retransmit timer if not currently set,
 1023                  * and not doing a pure ack or a keep-alive probe.
 1024                  * Initial value for retransmit timer is smoothed
 1025                  * round-trip time + 2 * round-trip time variance.
 1026                  * Initialize shift counter which is used for backoff
 1027                  * of retransmit time.
 1028                  */
 1029 timer:
 1030                 if (!callout_active(tp->tt_rexmt) &&
 1031                     ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
 1032                      (tp->snd_nxt != tp->snd_una))) {
 1033                         if (callout_active(tp->tt_persist)) {
 1034                                 callout_stop(tp->tt_persist);
 1035                                 tp->t_rxtshift = 0;
 1036                         }
 1037                         callout_reset(tp->tt_rexmt, tp->t_rxtcur,
 1038                                       tcp_timer_rexmt, tp);
 1039                 }
 1040         } else {
 1041                 /*
 1042                  * Persist case, update snd_max but since we are in
 1043                  * persist mode (no window) we do not update snd_nxt.
 1044                  */
 1045                 int xlen = len;
 1046                 if (flags & TH_SYN)
 1047                         ++xlen;
 1048                 if (flags & TH_FIN) {
 1049                         ++xlen;
 1050                         tp->t_flags |= TF_SENTFIN;
 1051                 }
 1052                 if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
 1053                         tp->snd_max = tp->snd_nxt + len;
 1054         }
 1055 
 1056 #ifdef TCPDEBUG
 1057         /*
 1058          * Trace.
 1059          */
 1060         if (so->so_options & SO_DEBUG) {
 1061                 u_short save = 0;
 1062 #ifdef INET6
 1063                 if (!isipv6)
 1064 #endif
 1065                 {
 1066                         save = ipov->ih_len;
 1067                         ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */);
 1068                 }
 1069                 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
 1070 #ifdef INET6
 1071                 if (!isipv6)
 1072 #endif
 1073                 ipov->ih_len = save;
 1074         }
 1075 #endif
 1076 
 1077         /*
 1078          * Fill in IP length and desired time to live and
 1079          * send to IP level.  There should be a better way
 1080          * to handle ttl and tos; we could keep them in
 1081          * the template, but need a way to checksum without them.
 1082          */
 1083         /*
 1084          * m->m_pkthdr.len should have been set before cksum calcuration,
 1085          * because in6_cksum() need it.
 1086          */
 1087 #ifdef INET6
 1088         if (isipv6) {
 1089                 /*
 1090                  * we separately set hoplimit for every segment, since the
 1091                  * user might want to change the value via setsockopt.
 1092                  * Also, desired default hop limit might be changed via
 1093                  * Neighbor Discovery.
 1094                  */
 1095                 ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL);
 1096 
 1097                 /* TODO: IPv6 IP6TOS_ECT bit on */
 1098                 error = ip6_output(m,
 1099                             tp->t_inpcb->in6p_outputopts, NULL,
 1100                             (so->so_options & SO_DONTROUTE), NULL, NULL,
 1101                             tp->t_inpcb);
 1102         } else
 1103 #endif /* INET6 */
 1104     {
 1105         ip->ip_len = m->m_pkthdr.len;
 1106 #ifdef INET6
 1107         if (INP_CHECK_SOCKAF(so, AF_INET6))
 1108                 ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL);
 1109 #endif /* INET6 */
 1110         /*
 1111          * If we do path MTU discovery, then we set DF on every packet.
 1112          * This might not be the best thing to do according to RFC3390
 1113          * Section 2. However the tcp hostcache migitates the problem
 1114          * so it affects only the first tcp connection with a host.
 1115          */
 1116         if (path_mtu_discovery)
 1117                 ip->ip_off |= IP_DF;
 1118 
 1119         error = ip_output(m, tp->t_inpcb->inp_options, NULL,
 1120             (so->so_options & SO_DONTROUTE), 0, tp->t_inpcb);
 1121     }
 1122         if (error) {
 1123 
 1124                 /*
 1125                  * We know that the packet was lost, so back out the
 1126                  * sequence number advance, if any.
 1127                  */
 1128                 if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
 1129                         /*
 1130                          * No need to check for TH_FIN here because
 1131                          * the TF_SENTFIN flag handles that case.
 1132                          */
 1133                         if ((flags & TH_SYN) == 0) {
 1134                                 if (sack_rxmit)
 1135                                         p->rxmit -= len;
 1136                                 else
 1137                                         tp->snd_nxt -= len;
 1138                         }
 1139                 }
 1140 
 1141 out:
 1142                 SOCKBUF_UNLOCK_ASSERT(&so->so_snd);     /* Check gotos. */
 1143                 if (error == ENOBUFS) {
 1144                         if (!callout_active(tp->tt_rexmt) &&
 1145                             !callout_active(tp->tt_persist))
 1146                                 callout_reset(tp->tt_rexmt, tp->t_rxtcur,
 1147                                     tcp_timer_rexmt, tp);
 1148                         tp->snd_cwnd = tp->t_maxseg;
 1149                         return (0);
 1150                 }
 1151                 if (error == EMSGSIZE) {
 1152                         /*
 1153                          * ip_output() will have already fixed the route
 1154                          * for us.  tcp_mtudisc() will, as its last action,
 1155                          * initiate retransmission, so it is important to
 1156                          * not do so here.
 1157                          */
 1158                         tcp_mtudisc(tp->t_inpcb, 0);
 1159                         return 0;
 1160                 }
 1161                 if ((error == EHOSTUNREACH || error == ENETDOWN)
 1162                     && TCPS_HAVERCVDSYN(tp->t_state)) {
 1163                         tp->t_softerror = error;
 1164                         return (0);
 1165                 }
 1166                 return (error);
 1167         }
 1168         tcpstat.tcps_sndtotal++;
 1169 
 1170         /*
 1171          * Data sent (as far as we can tell).
 1172          * If this advertises a larger window than any other segment,
 1173          * then remember the size of the advertised window.
 1174          * Any pending ACK has now been sent.
 1175          */
 1176         if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
 1177                 tp->rcv_adv = tp->rcv_nxt + recwin;
 1178         tp->last_ack_sent = tp->rcv_nxt;
 1179         tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
 1180         if (callout_active(tp->tt_delack))
 1181                 callout_stop(tp->tt_delack);
 1182 #if 0
 1183         /*
 1184          * This completely breaks TCP if newreno is turned on.  What happens
 1185          * is that if delayed-acks are turned on on the receiver, this code
 1186          * on the transmitter effectively destroys the TCP window, forcing
 1187          * it to four packets (1.5Kx4 = 6K window).
 1188          */
 1189         if (sendalot && (!tcp_do_newreno || --maxburst))
 1190                 goto again;
 1191 #endif
 1192         if (sendalot)
 1193                 goto again;
 1194         return (0);
 1195 }
 1196 
 1197 void
 1198 tcp_setpersist(tp)
 1199         register struct tcpcb *tp;
 1200 {
 1201         int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
 1202         int tt;
 1203 
 1204         if (callout_active(tp->tt_rexmt))
 1205                 panic("tcp_setpersist: retransmit pending");
 1206         /*
 1207          * Start/restart persistance timer.
 1208          */
 1209         TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
 1210                       TCPTV_PERSMIN, TCPTV_PERSMAX);
 1211         callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp);
 1212         if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
 1213                 tp->t_rxtshift++;
 1214 }

Cache object: 88426c9e85896cf0c6ff0e78b0cb32db


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.