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  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by the University of
   16  *      California, Berkeley and its contributors.
   17  * 4. Neither the name of the University nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  *      @(#)tcp_output.c        8.4 (Berkeley) 5/24/95
   34  * $FreeBSD$
   35  */
   36 
   37 #include "opt_inet.h"
   38 #include "opt_inet6.h"
   39 #include "opt_ipsec.h"
   40 #include "opt_tcpdebug.h"
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/kernel.h>
   45 #include <sys/sysctl.h>
   46 #include <sys/mbuf.h>
   47 #include <sys/domain.h>
   48 #include <sys/protosw.h>
   49 #include <sys/socket.h>
   50 #include <sys/socketvar.h>
   51 
   52 #include <net/route.h>
   53 
   54 #include <netinet/in.h>
   55 #include <netinet/in_systm.h>
   56 #include <netinet/ip.h>
   57 #include <netinet/in_pcb.h>
   58 #include <netinet/ip_var.h>
   59 #ifdef INET6
   60 #include <netinet6/in6_pcb.h>
   61 #include <netinet/ip6.h>
   62 #include <netinet6/ip6_var.h>
   63 #endif
   64 #include <netinet/tcp.h>
   65 #define TCPOUTFLAGS
   66 #include <netinet/tcp_fsm.h>
   67 #include <netinet/tcp_seq.h>
   68 #include <netinet/tcp_timer.h>
   69 #include <netinet/tcp_var.h>
   70 #include <netinet/tcpip.h>
   71 #ifdef TCPDEBUG
   72 #include <netinet/tcp_debug.h>
   73 #endif
   74 
   75 #ifdef IPSEC
   76 #include <netinet6/ipsec.h>
   77 #endif /*IPSEC*/
   78 
   79 #ifdef FAST_IPSEC
   80 #include <netipsec/ipsec.h>
   81 #define IPSEC
   82 #endif /*FAST_IPSEC*/
   83 
   84 #include <machine/in_cksum.h>
   85 
   86 #ifdef notyet
   87 extern struct mbuf *m_copypack();
   88 #endif
   89 
   90 int path_mtu_discovery = 1;
   91 SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW,
   92         &path_mtu_discovery, 1, "Enable Path MTU Discovery");
   93 
   94 int ss_fltsz = 1;
   95 SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW,
   96         &ss_fltsz, 1, "Slow start flight size");
   97 
   98 int ss_fltsz_local = 4;
   99 SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW,
  100         &ss_fltsz_local, 1, "Slow start flight size for local networks");
  101 
  102 int     tcp_do_newreno = 1;
  103 SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno,
  104         0, "Enable NewReno Algorithms");
  105 
  106 /*
  107  * Tcp output routine: figure out what should be sent and send it.
  108  */
  109 int
  110 tcp_output(tp)
  111         register struct tcpcb *tp;
  112 {
  113         register struct socket *so = tp->t_inpcb->inp_socket;
  114         register long len, win;
  115         int off, flags, error;
  116 #ifdef TCP_SIGNATURE
  117         int sigoff = 0;
  118 #endif
  119         register struct mbuf *m;
  120         struct ip *ip = NULL;
  121         register struct ipovly *ipov = NULL;
  122 #ifdef INET6
  123         struct ip6_hdr *ip6 = NULL;
  124 #endif /* INET6 */
  125         register struct tcphdr *th;
  126         u_char opt[TCP_MAXOLEN];
  127         unsigned ipoptlen, optlen, hdrlen;
  128         int idle, sendalot;
  129 #if 0
  130         int maxburst = TCP_MAXBURST;
  131 #endif
  132         struct rmxp_tao *taop;
  133 #ifdef INET6
  134         int isipv6;
  135 #endif
  136 
  137 #ifdef INET6
  138         isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
  139 #endif
  140 
  141         /*
  142          * Determine length of data that should be transmitted,
  143          * and flags that will be used.
  144          * If there is some data or critical controls (SYN, RST)
  145          * to send, then transmit; otherwise, investigate further.
  146          */
  147         idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
  148         if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) {
  149                 /*
  150                  * We have been idle for "a while" and no acks are
  151                  * expected to clock out any data we send --
  152                  * slow start to get ack "clock" running again.
  153                  *       
  154                  * Set the slow-start flight size depending on whether
  155                  * this is a local network or not.
  156                  */      
  157                 if (
  158 #ifdef INET6
  159                     (isipv6 && in6_localaddr(&tp->t_inpcb->in6p_faddr)) ||
  160                     (!isipv6 &&
  161 #endif
  162                      in_localaddr(tp->t_inpcb->inp_faddr)
  163 #ifdef INET6
  164                      )
  165 #endif
  166                     )
  167                         tp->snd_cwnd = tp->t_maxseg * ss_fltsz_local;
  168                 else     
  169                         tp->snd_cwnd = tp->t_maxseg * ss_fltsz;
  170         }
  171         tp->t_flags &= ~TF_LASTIDLE;
  172         if (idle) {
  173                 if (tp->t_flags & TF_MORETOCOME) {
  174                         tp->t_flags |= TF_LASTIDLE;
  175                         idle = 0;
  176                 }
  177         }
  178 again:
  179         sendalot = 0;
  180         off = tp->snd_nxt - tp->snd_una;
  181         win = min(tp->snd_wnd, tp->snd_cwnd);
  182         win = min(win, tp->snd_bwnd);
  183 
  184         flags = tcp_outflags[tp->t_state];
  185         /*
  186          * Get standard flags, and add SYN or FIN if requested by 'hidden'
  187          * state flags.
  188          */
  189         if (tp->t_flags & TF_NEEDFIN)
  190                 flags |= TH_FIN;
  191         if (tp->t_flags & TF_NEEDSYN)
  192                 flags |= TH_SYN;
  193 
  194         /*
  195          * If in persist timeout with window of 0, send 1 byte.
  196          * Otherwise, if window is small but nonzero
  197          * and timer expired, we will send what we can
  198          * and go to transmit state.
  199          */
  200         if (tp->t_force) {
  201                 if (win == 0) {
  202                         /*
  203                          * If we still have some data to send, then
  204                          * clear the FIN bit.  Usually this would
  205                          * happen below when it realizes that we
  206                          * aren't sending all the data.  However,
  207                          * if we have exactly 1 byte of unsent data,
  208                          * then it won't clear the FIN bit below,
  209                          * and if we are in persist state, we wind
  210                          * up sending the packet without recording
  211                          * that we sent the FIN bit.
  212                          *
  213                          * We can't just blindly clear the FIN bit,
  214                          * because if we don't have any more data
  215                          * to send then the probe will be the FIN
  216                          * itself.
  217                          */
  218                         if (off < so->so_snd.sb_cc)
  219                                 flags &= ~TH_FIN;
  220                         win = 1;
  221                 } else {
  222                         callout_stop(tp->tt_persist);
  223                         tp->t_rxtshift = 0;
  224                 }
  225         }
  226 
  227         /*
  228          * If snd_nxt == snd_max and we have transmitted a FIN, the 
  229          * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
  230          * a negative length.  This can also occur when tcp opens up
  231          * its congestion window while receiving additional duplicate
  232          * acks after fast-retransmit because TCP will reset snd_nxt
  233          * to snd_max after the fast-retransmit.
  234          *
  235          * In the normal retransmit-FIN-only case, however, snd_nxt will
  236          * be set to snd_una, the offset will be 0, and the length may
  237          * wind up 0.
  238          */
  239         len = (long)ulmin(so->so_snd.sb_cc, win) - off;
  240 
  241         taop = tcp_gettaocache(&tp->t_inpcb->inp_inc);
  242 
  243         /*
  244          * Lop off SYN bit if it has already been sent.  However, if this
  245          * is SYN-SENT state and if segment contains data and if we don't
  246          * know that foreign host supports TAO, suppress sending segment.
  247          */
  248         if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
  249                 flags &= ~TH_SYN;
  250                 off--, len++;
  251                 if (len > 0 && tp->t_state == TCPS_SYN_SENT &&
  252                     (taop == NULL || taop->tao_ccsent == 0))
  253                         return 0;
  254         }
  255 
  256         /*
  257          * Be careful not to send data and/or FIN on SYN segments
  258          * in cases when no CC option will be sent.
  259          * This measure is needed to prevent interoperability problems
  260          * with not fully conformant TCP implementations.
  261          */
  262         if ((flags & TH_SYN) &&
  263             ((tp->t_flags & TF_NOOPT) || !(tp->t_flags & TF_REQ_CC) ||
  264              ((flags & TH_ACK) && !(tp->t_flags & TF_RCVD_CC)))) {
  265                 len = 0;
  266                 flags &= ~TH_FIN;
  267         }
  268 
  269         if (len < 0) {
  270                 /*
  271                  * If FIN has been sent but not acked,
  272                  * but we haven't been called to retransmit,
  273                  * len will be < 0.  Otherwise, window shrank
  274                  * after we sent into it.  If window shrank to 0,
  275                  * cancel pending retransmit, pull snd_nxt back
  276                  * to (closed) window, and set the persist timer
  277                  * if it isn't already going.  If the window didn't
  278                  * close completely, just wait for an ACK.
  279                  */
  280                 len = 0;
  281                 if (win == 0) {
  282                         callout_stop(tp->tt_rexmt);
  283                         tp->t_rxtshift = 0;
  284                         tp->snd_nxt = tp->snd_una;
  285                         if (!callout_active(tp->tt_persist))
  286                                 tcp_setpersist(tp);
  287                 }
  288         }
  289 
  290         /*
  291          * len will be >= 0 after this point.  Truncate to the maximum
  292          * segment length and ensure that FIN is removed if the length
  293          * no longer contains the last data byte.
  294          */
  295         if (len > tp->t_maxseg) {
  296                 len = tp->t_maxseg;
  297                 sendalot = 1;
  298         }
  299         if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
  300                 flags &= ~TH_FIN;
  301 
  302         win = sbspace(&so->so_rcv);
  303 
  304         /*
  305          * Sender silly window avoidance.   We transmit under the following
  306          * conditions when len is non-zero:
  307          *
  308          *      - We have a full segment
  309          *      - This is the last buffer in a write()/send() and we are
  310          *        either idle or running NODELAY
  311          *      - we've timed out (e.g. persist timer)
  312          *      - we have more then 1/2 the maximum send window's worth of
  313          *        data (receiver may be limited the window size)
  314          *      - we need to retransmit
  315          */
  316         if (len) {
  317                 if (len == tp->t_maxseg)
  318                         goto send;
  319                 /*
  320                  * NOTE! on localhost connections an 'ack' from the remote
  321                  * end may occur synchronously with the output and cause
  322                  * us to flush a buffer queued with moretocome.  XXX
  323                  *
  324                  * note: the len + off check is almost certainly unnecessary.
  325                  */
  326                 if (!(tp->t_flags & TF_MORETOCOME) &&   /* normal case */
  327                     (idle || (tp->t_flags & TF_NODELAY)) &&
  328                     len + off >= so->so_snd.sb_cc &&
  329                     (tp->t_flags & TF_NOPUSH) == 0) {
  330                         goto send;
  331                 }
  332                 if (tp->t_force)                        /* typ. timeout case */
  333                         goto send;
  334                 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
  335                         goto send;
  336                 if (SEQ_LT(tp->snd_nxt, tp->snd_max))   /* retransmit case */
  337                         goto send;
  338         }
  339 
  340         /*
  341          * Compare available window to amount of window
  342          * known to peer (as advertised window less
  343          * next expected input).  If the difference is at least two
  344          * max size segments, or at least 50% of the maximum possible
  345          * window, then want to send a window update to peer.
  346          */
  347         if (win > 0) {
  348                 /*
  349                  * "adv" is the amount we can increase the window,
  350                  * taking into account that we are limited by
  351                  * TCP_MAXWIN << tp->rcv_scale.
  352                  */
  353                 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
  354                         (tp->rcv_adv - tp->rcv_nxt);
  355 
  356                 if (adv >= (long) (2 * tp->t_maxseg))
  357                         goto send;
  358                 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
  359                         goto send;
  360         }
  361 
  362         /*
  363          * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
  364          * is also a catch-all for the retransmit timer timeout case.
  365          */
  366         if (tp->t_flags & TF_ACKNOW)
  367                 goto send;
  368         if ((flags & TH_RST) ||
  369             ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
  370                 goto send;
  371         if (SEQ_GT(tp->snd_up, tp->snd_una))
  372                 goto send;
  373         /*
  374          * If our state indicates that FIN should be sent
  375          * and we have not yet done so, then we need to send.
  376          */
  377         if (flags & TH_FIN &&
  378             ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
  379                 goto send;
  380 
  381         /*
  382          * TCP window updates are not reliable, rather a polling protocol
  383          * using ``persist'' packets is used to insure receipt of window
  384          * updates.  The three ``states'' for the output side are:
  385          *      idle                    not doing retransmits or persists
  386          *      persisting              to move a small or zero window
  387          *      (re)transmitting        and thereby not persisting
  388          *
  389          * callout_active(tp->tt_persist)
  390          *      is true when we are in persist state.
  391          * tp->t_force
  392          *      is set when we are called to send a persist packet.
  393          * callout_active(tp->tt_rexmt)
  394          *      is set when we are retransmitting
  395          * The output side is idle when both timers are zero.
  396          *
  397          * If send window is too small, there is data to transmit, and no
  398          * retransmit or persist is pending, then go to persist state.
  399          * If nothing happens soon, send when timer expires:
  400          * if window is nonzero, transmit what we can,
  401          * otherwise force out a byte.
  402          */
  403         if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) &&
  404             !callout_active(tp->tt_persist)) {
  405                 tp->t_rxtshift = 0;
  406                 tcp_setpersist(tp);
  407         }
  408 
  409         /*
  410          * No reason to send a segment, just return.
  411          */
  412         return (0);
  413 
  414 send:
  415         /*
  416          * Before ESTABLISHED, force sending of initial options
  417          * unless TCP set not to do any options.
  418          * NOTE: we assume that the IP/TCP header plus TCP options
  419          * always fit in a single mbuf, leaving room for a maximum
  420          * link header, i.e.
  421          *      max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
  422          */
  423         optlen = 0;
  424 #ifdef INET6
  425         if (isipv6)
  426                 hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
  427         else
  428 #endif
  429         hdrlen = sizeof (struct tcpiphdr);
  430         if (flags & TH_SYN) {
  431                 tp->snd_nxt = tp->iss;
  432                 if ((tp->t_flags & TF_NOOPT) == 0) {
  433                         u_short mss;
  434 
  435                         opt[0] = TCPOPT_MAXSEG;
  436                         opt[1] = TCPOLEN_MAXSEG;
  437                         mss = htons((u_short) tcp_mssopt(tp));
  438                         (void)memcpy(opt + 2, &mss, sizeof(mss));
  439                         optlen = TCPOLEN_MAXSEG;
  440 
  441                         if ((tp->t_flags & TF_REQ_SCALE) &&
  442                             ((flags & TH_ACK) == 0 ||
  443                             (tp->t_flags & TF_RCVD_SCALE))) {
  444                                 *((u_int32_t *)(opt + optlen)) = htonl(
  445                                         TCPOPT_NOP << 24 |
  446                                         TCPOPT_WINDOW << 16 |
  447                                         TCPOLEN_WINDOW << 8 |
  448                                         tp->request_r_scale);
  449                                 optlen += 4;
  450                         }
  451                 }
  452         }
  453 
  454         /*
  455          * Send a timestamp and echo-reply if this is a SYN and our side
  456          * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
  457          * and our peer have sent timestamps in our SYN's.
  458          */
  459         if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
  460             (flags & TH_RST) == 0 &&
  461             ((flags & TH_ACK) == 0 ||
  462              (tp->t_flags & TF_RCVD_TSTMP))) {
  463                 u_int32_t *lp = (u_int32_t *)(opt + optlen);
  464 
  465                 /* Form timestamp option as shown in appendix A of RFC 1323. */
  466                 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
  467                 *lp++ = htonl(ticks);
  468                 *lp   = htonl(tp->ts_recent);
  469                 optlen += TCPOLEN_TSTAMP_APPA;
  470         }
  471 
  472         /*
  473          * Send `CC-family' options if our side wants to use them (TF_REQ_CC),
  474          * options are allowed (!TF_NOOPT) and it's not a RST.
  475          */
  476         if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
  477              (flags & TH_RST) == 0) {
  478                 switch (flags & (TH_SYN|TH_ACK)) {
  479                 /*
  480                  * This is a normal ACK, send CC if we received CC before
  481                  * from our peer.
  482                  */
  483                 case TH_ACK:
  484                         if (!(tp->t_flags & TF_RCVD_CC))
  485                                 break;
  486                         /*FALLTHROUGH*/
  487 
  488                 /*
  489                  * We can only get here in T/TCP's SYN_SENT* state, when
  490                  * we're a sending a non-SYN segment without waiting for
  491                  * the ACK of our SYN.  A check above assures that we only
  492                  * do this if our peer understands T/TCP.
  493                  */
  494                 case 0:
  495                         opt[optlen++] = TCPOPT_NOP;
  496                         opt[optlen++] = TCPOPT_NOP;
  497                         opt[optlen++] = TCPOPT_CC;
  498                         opt[optlen++] = TCPOLEN_CC;
  499                         *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
  500 
  501                         optlen += 4;
  502                         break;
  503 
  504                 /*
  505                  * This is our initial SYN, check whether we have to use
  506                  * CC or CC.new.
  507                  */
  508                 case TH_SYN:
  509                         opt[optlen++] = TCPOPT_NOP;
  510                         opt[optlen++] = TCPOPT_NOP;
  511                         opt[optlen++] = tp->t_flags & TF_SENDCCNEW ?
  512                                                 TCPOPT_CCNEW : TCPOPT_CC;
  513                         opt[optlen++] = TCPOLEN_CC;
  514                         *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
  515                         optlen += 4;
  516                         break;
  517 
  518                 /*
  519                  * This is a SYN,ACK; send CC and CC.echo if we received
  520                  * CC from our peer.
  521                  */
  522                 case (TH_SYN|TH_ACK):
  523                         if (tp->t_flags & TF_RCVD_CC) {
  524                                 opt[optlen++] = TCPOPT_NOP;
  525                                 opt[optlen++] = TCPOPT_NOP;
  526                                 opt[optlen++] = TCPOPT_CC;
  527                                 opt[optlen++] = TCPOLEN_CC;
  528                                 *(u_int32_t *)&opt[optlen] =
  529                                         htonl(tp->cc_send);
  530                                 optlen += 4;
  531                                 opt[optlen++] = TCPOPT_NOP;
  532                                 opt[optlen++] = TCPOPT_NOP;
  533                                 opt[optlen++] = TCPOPT_CCECHO;
  534                                 opt[optlen++] = TCPOLEN_CC;
  535                                 *(u_int32_t *)&opt[optlen] =
  536                                         htonl(tp->cc_recv);
  537                                 optlen += 4;
  538                         }
  539                         break;
  540                 }
  541         }
  542 
  543 #ifdef TCP_SIGNATURE
  544 #ifdef INET6
  545         if (!isipv6)
  546 #endif
  547         if (tp->t_flags & TF_SIGNATURE) {
  548                 int i;
  549                 u_char *bp;
  550                 /*
  551                  * Initialize TCP-MD5 option (RFC2385)
  552                  */
  553                 bp = (u_char *)opt + optlen;
  554                 *bp++ = TCPOPT_SIGNATURE;
  555                 *bp++ = TCPOLEN_SIGNATURE;
  556                 sigoff = optlen + 2;
  557                 for (i = 0; i < TCP_SIGLEN; i++)
  558                         *bp++ = 0;
  559                 optlen += TCPOLEN_SIGNATURE;
  560                 /*
  561                  * Terminate options list and maintain 32-bit alignment.
  562                  */
  563                 *bp++ = TCPOPT_NOP;
  564                 *bp++ = TCPOPT_EOL;
  565                 optlen += 2;
  566         }
  567 #endif /* TCP_SIGNATURE */
  568 
  569         hdrlen += optlen;
  570 
  571 #ifdef INET6
  572         if (isipv6)
  573                 ipoptlen = ip6_optlen(tp->t_inpcb);
  574         else
  575 #endif
  576       {
  577         if (tp->t_inpcb->inp_options) {
  578                 ipoptlen = tp->t_inpcb->inp_options->m_len -
  579                                 offsetof(struct ipoption, ipopt_list);
  580         } else {
  581                 ipoptlen = 0;
  582         }
  583       }
  584 #ifdef IPSEC
  585         ipoptlen += ipsec_hdrsiz_tcp(tp);
  586 #endif
  587 
  588         /*
  589          * Adjust data length if insertion of options will
  590          * bump the packet length beyond the t_maxopd length.
  591          * Clear the FIN bit because we cut off the tail of
  592          * the segment.
  593          */
  594         if (len + optlen + ipoptlen > tp->t_maxopd) {
  595                 /*
  596                  * If there is still more to send, don't close the connection.
  597                  */
  598                 flags &= ~TH_FIN;
  599                 len = tp->t_maxopd - optlen - ipoptlen;
  600                 sendalot = 1;
  601         }
  602 
  603 /*#ifdef DIAGNOSTIC*/
  604 #ifdef INET6
  605         if (max_linkhdr + hdrlen > MCLBYTES)
  606                 panic("tcphdr too big");
  607 #else
  608         if (max_linkhdr + hdrlen > MHLEN)
  609                 panic("tcphdr too big");
  610 #endif
  611 /*#endif*/
  612 
  613         /*
  614          * Grab a header mbuf, attaching a copy of data to
  615          * be transmitted, and initialize the header from
  616          * the template for sends on this connection.
  617          */
  618         if (len) {
  619                 if (tp->t_force && len == 1)
  620                         tcpstat.tcps_sndprobe++;
  621                 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
  622                         tcpstat.tcps_sndrexmitpack++;
  623                         tcpstat.tcps_sndrexmitbyte += len;
  624                 } else {
  625                         tcpstat.tcps_sndpack++;
  626                         tcpstat.tcps_sndbyte += len;
  627                 }
  628 #ifdef notyet
  629                 if ((m = m_copypack(so->so_snd.sb_mb, off,
  630                     (int)len, max_linkhdr + hdrlen)) == 0) {
  631                         error = ENOBUFS;
  632                         goto out;
  633                 }
  634                 /*
  635                  * m_copypack left space for our hdr; use it.
  636                  */
  637                 m->m_len += hdrlen;
  638                 m->m_data -= hdrlen;
  639 #else
  640                 MGETHDR(m, M_DONTWAIT, MT_HEADER);
  641                 if (m == NULL) {
  642                         error = ENOBUFS;
  643                         goto out;
  644                 }
  645 #ifdef INET6
  646                 if (MHLEN < hdrlen + max_linkhdr) {
  647                         MCLGET(m, M_DONTWAIT);
  648                         if ((m->m_flags & M_EXT) == 0) {
  649                                 m_freem(m);
  650                                 error = ENOBUFS;
  651                                 goto out;
  652                         }
  653                 }
  654 #endif
  655                 m->m_data += max_linkhdr;
  656                 m->m_len = hdrlen;
  657                 if (len <= MHLEN - hdrlen - max_linkhdr) {
  658                         m_copydata(so->so_snd.sb_mb, off, (int) len,
  659                             mtod(m, caddr_t) + hdrlen);
  660                         m->m_len += len;
  661                 } else {
  662                         m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
  663                         if (m->m_next == 0) {
  664                                 (void) m_free(m);
  665                                 error = ENOBUFS;
  666                                 goto out;
  667                         }
  668                 }
  669 #endif
  670                 /*
  671                  * If we're sending everything we've got, set PUSH.
  672                  * (This will keep happy those implementations which only
  673                  * give data to the user when a buffer fills or
  674                  * a PUSH comes in.)
  675                  */
  676                 if (off + len == so->so_snd.sb_cc)
  677                         flags |= TH_PUSH;
  678         } else {
  679                 if (tp->t_flags & TF_ACKNOW)
  680                         tcpstat.tcps_sndacks++;
  681                 else if (flags & (TH_SYN|TH_FIN|TH_RST))
  682                         tcpstat.tcps_sndctrl++;
  683                 else if (SEQ_GT(tp->snd_up, tp->snd_una))
  684                         tcpstat.tcps_sndurg++;
  685                 else
  686                         tcpstat.tcps_sndwinup++;
  687 
  688                 MGETHDR(m, M_DONTWAIT, MT_HEADER);
  689                 if (m == NULL) {
  690                         error = ENOBUFS;
  691                         goto out;
  692                 }
  693 #ifdef INET6
  694                 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
  695                     MHLEN >= hdrlen) {
  696                         MH_ALIGN(m, hdrlen);
  697                 } else
  698 #endif
  699                 m->m_data += max_linkhdr;
  700                 m->m_len = hdrlen;
  701         }
  702         m->m_pkthdr.rcvif = (struct ifnet *)0;
  703 #ifdef INET6
  704         if (isipv6) {
  705                 ip6 = mtod(m, struct ip6_hdr *);
  706                 th = (struct tcphdr *)(ip6 + 1);
  707                 tcp_fillheaders(tp, ip6, th);
  708         } else
  709 #endif /* INET6 */
  710       {
  711         ip = mtod(m, struct ip *);
  712         ipov = (struct ipovly *)ip;
  713         th = (struct tcphdr *)(ip + 1);
  714         /* this picks up the pseudo header (w/o the length) */
  715         tcp_fillheaders(tp, ip, th);
  716       }
  717 
  718         /*
  719          * Fill in fields, remembering maximum advertised
  720          * window for use in delaying messages about window sizes.
  721          * If resending a FIN, be sure not to use a new sequence number.
  722          */
  723         if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
  724             tp->snd_nxt == tp->snd_max)
  725                 tp->snd_nxt--;
  726         /*
  727          * If we are doing retransmissions, then snd_nxt will
  728          * not reflect the first unsent octet.  For ACK only
  729          * packets, we do not want the sequence number of the
  730          * retransmitted packet, we want the sequence number
  731          * of the next unsent octet.  So, if there is no data
  732          * (and no SYN or FIN), use snd_max instead of snd_nxt
  733          * when filling in ti_seq.  But if we are in persist
  734          * state, snd_max might reflect one byte beyond the
  735          * right edge of the window, so use snd_nxt in that
  736          * case, since we know we aren't doing a retransmission.
  737          * (retransmit and persist are mutually exclusive...)
  738          */
  739         if (len || (flags & (TH_SYN|TH_FIN)) 
  740             || callout_active(tp->tt_persist))
  741                 th->th_seq = htonl(tp->snd_nxt);
  742         else
  743                 th->th_seq = htonl(tp->snd_max);
  744         th->th_ack = htonl(tp->rcv_nxt);
  745         if (optlen) {
  746                 bcopy(opt, th + 1, optlen);
  747                 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
  748         }
  749         th->th_flags = flags;
  750         /*
  751          * Calculate receive window.  Don't shrink window,
  752          * but avoid silly window syndrome.
  753          */
  754         if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
  755                 win = 0;
  756         if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
  757                 win = (long)(tp->rcv_adv - tp->rcv_nxt);
  758         if (win > (long)TCP_MAXWIN << tp->rcv_scale)
  759                 win = (long)TCP_MAXWIN << tp->rcv_scale;
  760         th->th_win = htons((u_short) (win>>tp->rcv_scale));
  761 
  762         /*
  763          * Adjust the RXWIN0SENT flag - indicate that we have advertised
  764          * a 0 window.  This may cause the remote transmitter to stall.  This
  765          * flag tells soreceive() to disable delayed acknowledgements when
  766          * draining the buffer.  This can occur if the receiver is attempting
  767          * to read more data then can be buffered prior to transmitting on
  768          * the connection.
  769          */
  770         if (win == 0)
  771                 tp->t_flags |= TF_RXWIN0SENT;
  772         else
  773                 tp->t_flags &= ~TF_RXWIN0SENT;
  774 
  775         if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
  776                 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
  777                 th->th_flags |= TH_URG;
  778         } else
  779                 /*
  780                  * If no urgent pointer to send, then we pull
  781                  * the urgent pointer to the left edge of the send window
  782                  * so that it doesn't drift into the send window on sequence
  783                  * number wraparound.
  784                  */
  785                 tp->snd_up = tp->snd_una;               /* drag it along */
  786 
  787 #ifdef TCP_SIGNATURE
  788 #ifdef INET6
  789         if (!isipv6)
  790 #endif
  791         if (tp->t_flags & TF_SIGNATURE)
  792                 tcp_signature_compute(m, sizeof(struct ip), len, optlen,
  793                     (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND);
  794 #endif
  795 
  796         /*
  797          * Put TCP length in extended header, and then
  798          * checksum extended header and data.
  799          */
  800         m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
  801 #ifdef INET6
  802         if (isipv6)
  803                 /*
  804                  * ip6_plen is not need to be filled now, and will be filled
  805                  * in ip6_output.
  806                  */
  807                 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
  808                                        sizeof(struct tcphdr) + optlen + len);
  809         else
  810 #endif /* INET6 */
  811       {
  812         m->m_pkthdr.csum_flags = CSUM_TCP;
  813         m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
  814         if (len + optlen)
  815                 th->th_sum = in_addword(th->th_sum, 
  816                     htons((u_short)(optlen + len)));
  817 
  818         /* IP version must be set here for ipv4/ipv6 checking later */
  819         KASSERT(ip->ip_v == IPVERSION,
  820             ("%s: IP version incorrect: %d", __func__, ip->ip_v));
  821       }
  822 
  823         /*
  824          * In transmit state, time the transmission and arrange for
  825          * the retransmit.  In persist state, just set snd_max.
  826          */
  827         if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
  828                 tcp_seq startseq = tp->snd_nxt;
  829 
  830                 /*
  831                  * Advance snd_nxt over sequence space of this segment.
  832                  */
  833                 if (flags & (TH_SYN|TH_FIN)) {
  834                         if (flags & TH_SYN)
  835                                 tp->snd_nxt++;
  836                         if (flags & TH_FIN) {
  837                                 tp->snd_nxt++;
  838                                 tp->t_flags |= TF_SENTFIN;
  839                         }
  840                 }
  841                 tp->snd_nxt += len;
  842                 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
  843                         tp->snd_max = tp->snd_nxt;
  844                         /*
  845                          * Time this transmission if not a retransmission and
  846                          * not currently timing anything.
  847                          */
  848                         if (tp->t_rtttime == 0) {
  849                                 tp->t_rtttime = ticks;
  850                                 tp->t_rtseq = startseq;
  851                                 tcpstat.tcps_segstimed++;
  852                         }
  853                 }
  854 
  855                 /*
  856                  * Set retransmit timer if not currently set,
  857                  * and not doing a pure ack or a keep-alive probe.
  858                  * Initial value for retransmit timer is smoothed
  859                  * round-trip time + 2 * round-trip time variance.
  860                  * Initialize shift counter which is used for backoff
  861                  * of retransmit time.
  862                  */
  863                 if (!callout_active(tp->tt_rexmt) &&
  864                     tp->snd_nxt != tp->snd_una) {
  865                         if (callout_active(tp->tt_persist)) {
  866                                 callout_stop(tp->tt_persist);
  867                                 tp->t_rxtshift = 0;
  868                         }
  869                         callout_reset(tp->tt_rexmt, tp->t_rxtcur,
  870                                       tcp_timer_rexmt, tp);
  871                 }
  872         } else {
  873                 /*
  874                  * Persist case, update snd_max but since we are in
  875                  * persist mode (no window) we do not update snd_nxt.
  876                  */
  877                 int xlen = len;
  878                 if (flags & TH_SYN)
  879                         ++xlen;
  880                 if (flags & TH_FIN) {
  881                         ++xlen;
  882                         tp->t_flags |= TF_SENTFIN;
  883                 }
  884                 if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
  885                         tp->snd_max = tp->snd_nxt + xlen;
  886         }
  887 
  888 #ifdef TCPDEBUG
  889         /*
  890          * Trace.
  891          */
  892         if (so->so_options & SO_DEBUG)
  893                 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
  894 #endif
  895 
  896         /*
  897          * Fill in IP length and desired time to live and
  898          * send to IP level.  There should be a better way
  899          * to handle ttl and tos; we could keep them in
  900          * the template, but need a way to checksum without them.
  901          */
  902         /*
  903          * m->m_pkthdr.len should have been set before cksum calcuration,
  904          * because in6_cksum() need it.
  905          */
  906 #ifdef INET6
  907         if (isipv6) {
  908                 /*
  909                  * we separately set hoplimit for every segment, since the
  910                  * user might want to change the value via setsockopt.
  911                  * Also, desired default hop limit might be changed via
  912                  * Neighbor Discovery.
  913                  */
  914                 ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb,
  915                                                tp->t_inpcb->in6p_route.ro_rt ?
  916                                                tp->t_inpcb->in6p_route.ro_rt->rt_ifp
  917                                                : NULL);
  918 
  919                 /* TODO: IPv6 IP6TOS_ECT bit on */
  920                 error = ip6_output(m,
  921                             tp->t_inpcb->in6p_outputopts,
  922                             &tp->t_inpcb->in6p_route,
  923                             (so->so_options & SO_DONTROUTE), NULL, NULL,
  924                             tp->t_inpcb);
  925         } else
  926 #endif /* INET6 */
  927     {
  928         struct rtentry *rt;
  929         ip->ip_len = m->m_pkthdr.len;
  930 #ifdef INET6
  931         if (INP_CHECK_SOCKAF(so, AF_INET6))
  932                 ip->ip_ttl = in6_selecthlim(tp->t_inpcb,
  933                                             tp->t_inpcb->in6p_route.ro_rt ?
  934                                             tp->t_inpcb->in6p_route.ro_rt->rt_ifp
  935                                             : NULL);
  936         else
  937 #endif /* INET6 */
  938         ip->ip_ttl = tp->t_inpcb->inp_ip_ttl;   /* XXX */
  939         ip->ip_tos = tp->t_inpcb->inp_ip_tos;   /* XXX */
  940         /*
  941          * See if we should do MTU discovery.  We do it only if the following
  942          * are true:
  943          *      1) we have a valid route to the destination
  944          *      2) the MTU is not locked (if it is, then discovery has been
  945          *         disabled)
  946          */
  947         if (path_mtu_discovery
  948             && (rt = tp->t_inpcb->inp_route.ro_rt)
  949             && rt->rt_flags & RTF_UP
  950             && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
  951                 ip->ip_off |= IP_DF;
  952         }
  953         error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
  954             (so->so_options & SO_DONTROUTE), 0, tp->t_inpcb);
  955     }
  956         if (error) {
  957 
  958                 /*
  959                  * We know that the packet was lost, so back out the
  960                  * sequence number advance, if any.
  961                  */
  962                 if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
  963                         /*
  964                          * No need to check for TH_FIN here because
  965                          * the TF_SENTFIN flag handles that case.
  966                          */
  967                         if ((flags & TH_SYN) == 0)
  968                                 tp->snd_nxt -= len;
  969                 }
  970 
  971 out:
  972                 if (error == ENOBUFS) {
  973                         if (!callout_active(tp->tt_rexmt) &&
  974                             !callout_active(tp->tt_persist))
  975                                 callout_reset(tp->tt_rexmt, tp->t_rxtcur,
  976                                       tcp_timer_rexmt, tp);
  977                         tcp_quench(tp->t_inpcb, 0);
  978                         return (0);
  979                 }
  980                 if (error == EMSGSIZE) {
  981                         /*
  982                          * ip_output() will have already fixed the route
  983                          * for us.  tcp_mtudisc() will, as its last action,
  984                          * initiate retransmission, so it is important to
  985                          * not do so here.
  986                          */
  987                         tcp_mtudisc(tp->t_inpcb, 0);
  988                         return 0;
  989                 }
  990                 if ((error == EHOSTUNREACH || error == ENETDOWN)
  991                     && TCPS_HAVERCVDSYN(tp->t_state)) {
  992                         tp->t_softerror = error;
  993                         return (0);
  994                 }
  995                 return (error);
  996         }
  997         tcpstat.tcps_sndtotal++;
  998 
  999         /*
 1000          * Data sent (as far as we can tell).
 1001          * If this advertises a larger window than any other segment,
 1002          * then remember the size of the advertised window.
 1003          * Any pending ACK has now been sent.
 1004          */
 1005         if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
 1006                 tp->rcv_adv = tp->rcv_nxt + win;
 1007         tp->last_ack_sent = tp->rcv_nxt;
 1008         tp->t_flags &= ~TF_ACKNOW;
 1009         if (tcp_delack_enabled)
 1010                 callout_stop(tp->tt_delack);
 1011 #if 0
 1012         /*
 1013          * This completely breaks TCP if newreno is turned on.  What happens
 1014          * is that if delayed-acks are turned on on the receiver, this code
 1015          * on the transmitter effectively destroys the TCP window, forcing
 1016          * it to four packets (1.5Kx4 = 6K window).
 1017          */
 1018         if (sendalot && (!tcp_do_newreno || --maxburst))
 1019                 goto again;
 1020 #endif
 1021         if (sendalot)
 1022                 goto again;
 1023         return (0);
 1024 }
 1025 
 1026 void
 1027 tcp_setpersist(tp)
 1028         register struct tcpcb *tp;
 1029 {
 1030         int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
 1031         int tt;
 1032 
 1033         if (callout_active(tp->tt_rexmt))
 1034                 panic("tcp_setpersist: retransmit pending");
 1035         /*
 1036          * Start/restart persistance timer.
 1037          */
 1038         TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
 1039                       TCPTV_PERSMIN, TCPTV_PERSMAX);
 1040         callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp);
 1041         if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
 1042                 tp->t_rxtshift++;
 1043 }

Cache object: 8c60979fcd4a7e3ac71b9d392f402a0b


[ 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.