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_reass.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, 1994, 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_input.c 8.12 (Berkeley) 5/24/95
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD: releng/11.1/sys/netinet/tcp_reass.c 337388 2018-08-06 17:48:46Z jtl $");
   34 
   35 #include "opt_inet.h"
   36 #include "opt_inet6.h"
   37 #include "opt_tcpdebug.h"
   38 
   39 #include <sys/param.h>
   40 #include <sys/kernel.h>
   41 #include <sys/eventhandler.h>
   42 #include <sys/malloc.h>
   43 #include <sys/mbuf.h>
   44 #include <sys/socket.h>
   45 #include <sys/socketvar.h>
   46 #include <sys/sysctl.h>
   47 #include <sys/syslog.h>
   48 #include <sys/systm.h>
   49 
   50 #include <vm/uma.h>
   51 
   52 #include <net/if.h>
   53 #include <net/if_var.h>
   54 #include <net/route.h>
   55 #include <net/vnet.h>
   56 
   57 #include <netinet/in.h>
   58 #include <netinet/in_pcb.h>
   59 #include <netinet/in_systm.h>
   60 #include <netinet/in_var.h>
   61 #include <netinet/ip.h>
   62 #include <netinet/ip_var.h>
   63 #include <netinet/ip_options.h>
   64 #include <netinet/ip6.h>
   65 #include <netinet6/in6_pcb.h>
   66 #include <netinet6/ip6_var.h>
   67 #include <netinet6/nd6.h>
   68 #include <netinet/tcp.h>
   69 #include <netinet/tcp_fsm.h>
   70 #include <netinet/tcp_seq.h>
   71 #include <netinet/tcp_timer.h>
   72 #include <netinet/tcp_var.h>
   73 #include <netinet6/tcp6_var.h>
   74 #include <netinet/tcpip.h>
   75 #ifdef TCPDEBUG
   76 #include <netinet/tcp_debug.h>
   77 #endif /* TCPDEBUG */
   78 
   79 static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
   80     "TCP Segment Reassembly Queue");
   81 
   82 static int tcp_reass_maxseg = 0;
   83 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN,
   84     &tcp_reass_maxseg, 0,
   85     "Global maximum number of TCP Segments in Reassembly Queue");
   86 
   87 static uma_zone_t tcp_reass_zone;
   88 SYSCTL_UMA_CUR(_net_inet_tcp_reass, OID_AUTO, cursegments, 0,
   89     &tcp_reass_zone,
   90     "Global number of TCP Segments currently in Reassembly Queue");
   91 
   92 static u_int tcp_reass_maxqueuelen = 100;
   93 SYSCTL_UINT(_net_inet_tcp_reass, OID_AUTO, maxqueuelen, CTLFLAG_RWTUN,
   94     &tcp_reass_maxqueuelen, 0,
   95     "Maximum number of TCP Segments per Reassembly Queue");
   96 
   97 /* Initialize TCP reassembly queue */
   98 static void
   99 tcp_reass_zone_change(void *tag)
  100 {
  101 
  102         /* Set the zone limit and read back the effective value. */
  103         tcp_reass_maxseg = nmbclusters / 16;
  104         tcp_reass_maxseg = uma_zone_set_max(tcp_reass_zone,
  105             tcp_reass_maxseg);
  106 }
  107 
  108 void
  109 tcp_reass_global_init(void)
  110 {
  111 
  112         tcp_reass_maxseg = nmbclusters / 16;
  113         TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
  114             &tcp_reass_maxseg);
  115         tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent),
  116             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  117         /* Set the zone limit and read back the effective value. */
  118         tcp_reass_maxseg = uma_zone_set_max(tcp_reass_zone,
  119             tcp_reass_maxseg);
  120         EVENTHANDLER_REGISTER(nmbclusters_change,
  121             tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY);
  122 }
  123 
  124 void
  125 tcp_reass_flush(struct tcpcb *tp)
  126 {
  127         struct tseg_qent *qe;
  128 
  129         INP_WLOCK_ASSERT(tp->t_inpcb);
  130 
  131         while ((qe = LIST_FIRST(&tp->t_segq)) != NULL) {
  132                 LIST_REMOVE(qe, tqe_q);
  133                 m_freem(qe->tqe_m);
  134                 uma_zfree(tcp_reass_zone, qe);
  135                 tp->t_segqlen--;
  136         }
  137 
  138         KASSERT((tp->t_segqlen == 0),
  139             ("TCP reass queue %p segment count is %d instead of 0 after flush.",
  140             tp, tp->t_segqlen));
  141 }
  142 
  143 int
  144 tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
  145 {
  146         struct tseg_qent *q;
  147         struct tseg_qent *p = NULL;
  148         struct tseg_qent *nq;
  149         struct tseg_qent *te = NULL;
  150         struct socket *so = tp->t_inpcb->inp_socket;
  151         char *s = NULL;
  152         int flags;
  153         struct tseg_qent tqs;
  154 
  155         INP_WLOCK_ASSERT(tp->t_inpcb);
  156 
  157         /*
  158          * XXX: tcp_reass() is rather inefficient with its data structures
  159          * and should be rewritten (see NetBSD for optimizations).
  160          */
  161 
  162         /*
  163          * Call with th==NULL after become established to
  164          * force pre-ESTABLISHED data up to user socket.
  165          */
  166         if (th == NULL)
  167                 goto present;
  168 
  169         /*
  170          * Limit the number of segments that can be queued to reduce the
  171          * potential for mbuf exhaustion. For best performance, we want to be
  172          * able to queue a full window's worth of segments. The size of the
  173          * socket receive buffer determines our advertised window and grows
  174          * automatically when socket buffer autotuning is enabled. Use it as the
  175          * basis for our queue limit.
  176          *
  177          * However, allow the user to specify a ceiling for the number of
  178          * segments in each queue.
  179          *
  180          * Always let the missing segment through which caused this queue.
  181          * NB: Access to the socket buffer is left intentionally unlocked as we
  182          * can tolerate stale information here.
  183          *
  184          * XXXLAS: Using sbspace(so->so_rcv) instead of so->so_rcv.sb_hiwat
  185          * should work but causes packets to be dropped when they shouldn't.
  186          * Investigate why and re-evaluate the below limit after the behaviour
  187          * is understood.
  188          */
  189         if ((th->th_seq != tp->rcv_nxt || !TCPS_HAVEESTABLISHED(tp->t_state)) &&
  190             tp->t_segqlen >= min((so->so_rcv.sb_hiwat / tp->t_maxseg) + 1,
  191             tcp_reass_maxqueuelen)) {
  192                 TCPSTAT_INC(tcps_rcvreassfull);
  193                 *tlenp = 0;
  194                 if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) {
  195                         log(LOG_DEBUG, "%s; %s: queue limit reached, "
  196                             "segment dropped\n", s, __func__);
  197                         free(s, M_TCPLOG);
  198                 }
  199                 m_freem(m);
  200                 return (0);
  201         }
  202 
  203         /*
  204          * Allocate a new queue entry. If we can't, or hit the zone limit
  205          * just drop the pkt.
  206          *
  207          * Use a temporary structure on the stack for the missing segment
  208          * when the zone is exhausted. Otherwise we may get stuck.
  209          */
  210         te = uma_zalloc(tcp_reass_zone, M_NOWAIT);
  211         if (te == NULL) {
  212                 if (th->th_seq != tp->rcv_nxt || !TCPS_HAVEESTABLISHED(tp->t_state)) {
  213                         TCPSTAT_INC(tcps_rcvmemdrop);
  214                         m_freem(m);
  215                         *tlenp = 0;
  216                         if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL,
  217                             NULL))) {
  218                                 log(LOG_DEBUG, "%s; %s: global zone limit "
  219                                     "reached, segment dropped\n", s, __func__);
  220                                 free(s, M_TCPLOG);
  221                         }
  222                         return (0);
  223                 } else {
  224                         bzero(&tqs, sizeof(struct tseg_qent));
  225                         te = &tqs;
  226                         if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL,
  227                             NULL))) {
  228                                 log(LOG_DEBUG,
  229                                     "%s; %s: global zone limit reached, using "
  230                                     "stack for missing segment\n", s, __func__);
  231                                 free(s, M_TCPLOG);
  232                         }
  233                 }
  234         }
  235         tp->t_segqlen++;
  236 
  237         /*
  238          * Find a segment which begins after this one does.
  239          */
  240         LIST_FOREACH(q, &tp->t_segq, tqe_q) {
  241                 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
  242                         break;
  243                 p = q;
  244         }
  245 
  246         /*
  247          * If there is a preceding segment, it may provide some of
  248          * our data already.  If so, drop the data from the incoming
  249          * segment.  If it provides all of our data, drop us.
  250          */
  251         if (p != NULL) {
  252                 int i;
  253                 /* conversion to int (in i) handles seq wraparound */
  254                 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
  255                 if (i > 0) {
  256                         if (i >= *tlenp) {
  257                                 TCPSTAT_INC(tcps_rcvduppack);
  258                                 TCPSTAT_ADD(tcps_rcvdupbyte, *tlenp);
  259                                 m_freem(m);
  260                                 if (te != &tqs)
  261                                         uma_zfree(tcp_reass_zone, te);
  262                                 tp->t_segqlen--;
  263                                 /*
  264                                  * Try to present any queued data
  265                                  * at the left window edge to the user.
  266                                  * This is needed after the 3-WHS
  267                                  * completes.
  268                                  */
  269                                 goto present;   /* ??? */
  270                         }
  271                         m_adj(m, i);
  272                         *tlenp -= i;
  273                         th->th_seq += i;
  274                 }
  275         }
  276         tp->t_rcvoopack++;
  277         TCPSTAT_INC(tcps_rcvoopack);
  278         TCPSTAT_ADD(tcps_rcvoobyte, *tlenp);
  279 
  280         /*
  281          * While we overlap succeeding segments trim them or,
  282          * if they are completely covered, dequeue them.
  283          */
  284         while (q) {
  285                 int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
  286                 if (i <= 0)
  287                         break;
  288                 if (i < q->tqe_len) {
  289                         q->tqe_th->th_seq += i;
  290                         q->tqe_len -= i;
  291                         m_adj(q->tqe_m, i);
  292                         break;
  293                 }
  294 
  295                 nq = LIST_NEXT(q, tqe_q);
  296                 LIST_REMOVE(q, tqe_q);
  297                 m_freem(q->tqe_m);
  298                 uma_zfree(tcp_reass_zone, q);
  299                 tp->t_segqlen--;
  300                 q = nq;
  301         }
  302 
  303         /* Insert the new segment queue entry into place. */
  304         te->tqe_m = m;
  305         te->tqe_th = th;
  306         te->tqe_len = *tlenp;
  307 
  308         if (p == NULL) {
  309                 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
  310         } else {
  311                 KASSERT(te != &tqs, ("%s: temporary stack based entry not "
  312                     "first element in queue", __func__));
  313                 LIST_INSERT_AFTER(p, te, tqe_q);
  314         }
  315 
  316 present:
  317         /*
  318          * Present data to user, advancing rcv_nxt through
  319          * completed sequence space.
  320          */
  321         if (!TCPS_HAVEESTABLISHED(tp->t_state))
  322                 return (0);
  323         q = LIST_FIRST(&tp->t_segq);
  324         if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
  325                 return (0);
  326         SOCKBUF_LOCK(&so->so_rcv);
  327         do {
  328                 tp->rcv_nxt += q->tqe_len;
  329                 flags = q->tqe_th->th_flags & TH_FIN;
  330                 nq = LIST_NEXT(q, tqe_q);
  331                 LIST_REMOVE(q, tqe_q);
  332                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
  333                         m_freem(q->tqe_m);
  334                 else
  335                         sbappendstream_locked(&so->so_rcv, q->tqe_m, 0);
  336                 if (q != &tqs)
  337                         uma_zfree(tcp_reass_zone, q);
  338                 tp->t_segqlen--;
  339                 q = nq;
  340         } while (q && q->tqe_th->th_seq == tp->rcv_nxt);
  341         sorwakeup_locked(so);
  342         return (flags);
  343 }

Cache object: 940f0e4dbd87f369b7bae0dc1cbc21ec


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