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/netinet6/frag6.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $FreeBSD: releng/6.3/sys/netinet6/frag6.c 170562 2007-06-11 17:04:10Z jinmei $  */
    2 /*      $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $     */
    3 
    4 /*-
    5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. Neither the name of the project nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 #include <sys/param.h>
   34 #include <sys/systm.h>
   35 #include <sys/malloc.h>
   36 #include <sys/mbuf.h>
   37 #include <sys/domain.h>
   38 #include <sys/protosw.h>
   39 #include <sys/socket.h>
   40 #include <sys/errno.h>
   41 #include <sys/time.h>
   42 #include <sys/kernel.h>
   43 #include <sys/syslog.h>
   44 
   45 #include <net/if.h>
   46 #include <net/route.h>
   47 
   48 #include <netinet/in.h>
   49 #include <netinet/in_var.h>
   50 #include <netinet/ip6.h>
   51 #include <netinet6/ip6_var.h>
   52 #include <netinet/icmp6.h>
   53 #include <netinet/in_systm.h>   /* for ECN definitions */
   54 #include <netinet/ip.h>         /* for ECN definitions */
   55 
   56 #include <net/net_osdep.h>
   57 
   58 /*
   59  * Define it to get a correct behavior on per-interface statistics.
   60  * You will need to perform an extra routing table lookup, per fragment,
   61  * to do it.  This may, or may not be, a performance hit.
   62  */
   63 #define IN6_IFSTAT_STRICT
   64 
   65 static void frag6_enq __P((struct ip6asfrag *, struct ip6asfrag *));
   66 static void frag6_deq __P((struct ip6asfrag *));
   67 static void frag6_insque __P((struct ip6q *, struct ip6q *));
   68 static void frag6_remque __P((struct ip6q *));
   69 static void frag6_freef __P((struct ip6q *));
   70 
   71 static struct mtx ip6qlock;
   72 /*
   73  * These fields all protected by ip6qlock.
   74  */
   75 static u_int frag6_nfragpackets;
   76 static u_int frag6_nfrags;
   77 static struct   ip6q ip6q;      /* ip6 reassemble queue */
   78 
   79 #define IP6Q_LOCK_INIT()        mtx_init(&ip6qlock, "ip6qlock", NULL, MTX_DEF);
   80 #define IP6Q_LOCK()             mtx_lock(&ip6qlock)
   81 #define IP6Q_TRYLOCK()          mtx_trylock(&ip6qlock)
   82 #define IP6Q_LOCK_ASSERT()      mtx_assert(&ip6qlock, MA_OWNED)
   83 #define IP6Q_UNLOCK()           mtx_unlock(&ip6qlock)
   84 
   85 static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
   86 
   87 /*
   88  * Initialise reassembly queue and fragment identifier.
   89  */
   90 static void
   91 frag6_change(void *tag)
   92 {
   93 
   94         ip6_maxfragpackets = nmbclusters / 4;
   95         ip6_maxfrags = nmbclusters / 4;
   96 }
   97 
   98 void
   99 frag6_init()
  100 {
  101 
  102         ip6_maxfragpackets = nmbclusters / 4;
  103         ip6_maxfrags = nmbclusters / 4;
  104         EVENTHANDLER_REGISTER(nmbclusters_change,
  105             frag6_change, NULL, EVENTHANDLER_PRI_ANY);
  106 
  107         IP6Q_LOCK_INIT();
  108 
  109         ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
  110 }
  111 
  112 /*
  113  * In RFC2460, fragment and reassembly rule do not agree with each other,
  114  * in terms of next header field handling in fragment header.
  115  * While the sender will use the same value for all of the fragmented packets,
  116  * receiver is suggested not to check the consistency.
  117  *
  118  * fragment rule (p20):
  119  *      (2) A Fragment header containing:
  120  *      The Next Header value that identifies the first header of
  121  *      the Fragmentable Part of the original packet.
  122  *              -> next header field is same for all fragments
  123  *
  124  * reassembly rule (p21):
  125  *      The Next Header field of the last header of the Unfragmentable
  126  *      Part is obtained from the Next Header field of the first
  127  *      fragment's Fragment header.
  128  *              -> should grab it from the first fragment only
  129  *
  130  * The following note also contradicts with fragment rule - noone is going to
  131  * send different fragment with different next header field.
  132  *
  133  * additional note (p22):
  134  *      The Next Header values in the Fragment headers of different
  135  *      fragments of the same original packet may differ.  Only the value
  136  *      from the Offset zero fragment packet is used for reassembly.
  137  *              -> should grab it from the first fragment only
  138  *
  139  * There is no explicit reason given in the RFC.  Historical reason maybe?
  140  */
  141 /*
  142  * Fragment input
  143  */
  144 int
  145 frag6_input(mp, offp, proto)
  146         struct mbuf **mp;
  147         int *offp, proto;
  148 {
  149         struct mbuf *m = *mp, *t;
  150         struct ip6_hdr *ip6;
  151         struct ip6_frag *ip6f;
  152         struct ip6q *q6;
  153         struct ip6asfrag *af6, *ip6af, *af6dwn;
  154 #ifdef IN6_IFSTAT_STRICT
  155         struct in6_ifaddr *ia;
  156 #endif
  157         int offset = *offp, nxt, i, next;
  158         int first_frag = 0;
  159         int fragoff, frgpartlen;        /* must be larger than u_int16_t */
  160         struct ifnet *dstifp;
  161         u_int8_t ecn, ecn0;
  162 
  163         ip6 = mtod(m, struct ip6_hdr *);
  164 #ifndef PULLDOWN_TEST
  165         IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
  166         ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
  167 #else
  168         IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
  169         if (ip6f == NULL)
  170                 return (IPPROTO_DONE);
  171 #endif
  172 
  173         dstifp = NULL;
  174 #ifdef IN6_IFSTAT_STRICT
  175         /* find the destination interface of the packet. */
  176         if ((ia = ip6_getdstifaddr(m)) != NULL)
  177                 dstifp = ia->ia_ifp;
  178 #else
  179         /* we are violating the spec, this is not the destination interface */
  180         if ((m->m_flags & M_PKTHDR) != 0)
  181                 dstifp = m->m_pkthdr.rcvif;
  182 #endif
  183 
  184         /* jumbo payload can't contain a fragment header */
  185         if (ip6->ip6_plen == 0) {
  186                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
  187                 in6_ifstat_inc(dstifp, ifs6_reass_fail);
  188                 return IPPROTO_DONE;
  189         }
  190 
  191         /*
  192          * check whether fragment packet's fragment length is
  193          * multiple of 8 octets.
  194          * sizeof(struct ip6_frag) == 8
  195          * sizeof(struct ip6_hdr) = 40
  196          */
  197         if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
  198             (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
  199                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
  200                     offsetof(struct ip6_hdr, ip6_plen));
  201                 in6_ifstat_inc(dstifp, ifs6_reass_fail);
  202                 return IPPROTO_DONE;
  203         }
  204 
  205         ip6stat.ip6s_fragments++;
  206         in6_ifstat_inc(dstifp, ifs6_reass_reqd);
  207 
  208         /* offset now points to data portion */
  209         offset += sizeof(struct ip6_frag);
  210 
  211         IP6Q_LOCK();
  212 
  213         /*
  214          * Enforce upper bound on number of fragments.
  215          * If maxfrag is 0, never accept fragments.
  216          * If maxfrag is -1, accept all fragments without limitation.
  217          */
  218         if (ip6_maxfrags < 0)
  219                 ;
  220         else if (frag6_nfrags >= (u_int)ip6_maxfrags)
  221                 goto dropfrag;
  222 
  223         for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
  224                 if (ip6f->ip6f_ident == q6->ip6q_ident &&
  225                     IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
  226                     IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
  227                         break;
  228 
  229         if (q6 == &ip6q) {
  230                 /*
  231                  * the first fragment to arrive, create a reassembly queue.
  232                  */
  233                 first_frag = 1;
  234 
  235                 /*
  236                  * Enforce upper bound on number of fragmented packets
  237                  * for which we attempt reassembly;
  238                  * If maxfragpackets is 0, never accept fragments.
  239                  * If maxfragpackets is -1, accept all fragments without
  240                  * limitation.
  241                  */
  242                 if (ip6_maxfragpackets < 0)
  243                         ;
  244                 else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
  245                         goto dropfrag;
  246                 frag6_nfragpackets++;
  247                 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
  248                     M_NOWAIT);
  249                 if (q6 == NULL)
  250                         goto dropfrag;
  251                 bzero(q6, sizeof(*q6));
  252 
  253                 frag6_insque(q6, &ip6q);
  254 
  255                 /* ip6q_nxt will be filled afterwards, from 1st fragment */
  256                 q6->ip6q_down   = q6->ip6q_up = (struct ip6asfrag *)q6;
  257 #ifdef notyet
  258                 q6->ip6q_nxtp   = (u_char *)nxtp;
  259 #endif
  260                 q6->ip6q_ident  = ip6f->ip6f_ident;
  261                 q6->ip6q_ttl    = IPV6_FRAGTTL;
  262                 q6->ip6q_src    = ip6->ip6_src;
  263                 q6->ip6q_dst    = ip6->ip6_dst;
  264                 q6->ip6q_ecn    =
  265                     (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
  266                 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
  267 
  268                 q6->ip6q_nfrag = 0;
  269         }
  270 
  271         /*
  272          * If it's the 1st fragment, record the length of the
  273          * unfragmentable part and the next header of the fragment header.
  274          */
  275         fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
  276         if (fragoff == 0) {
  277                 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
  278                     sizeof(struct ip6_frag);
  279                 q6->ip6q_nxt = ip6f->ip6f_nxt;
  280         }
  281 
  282         /*
  283          * Check that the reassembled packet would not exceed 65535 bytes
  284          * in size.
  285          * If it would exceed, discard the fragment and return an ICMP error.
  286          */
  287         frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
  288         if (q6->ip6q_unfrglen >= 0) {
  289                 /* The 1st fragment has already arrived. */
  290                 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
  291                         icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
  292                             offset - sizeof(struct ip6_frag) +
  293                             offsetof(struct ip6_frag, ip6f_offlg));
  294                         IP6Q_UNLOCK();
  295                         return (IPPROTO_DONE);
  296                 }
  297         } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
  298                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
  299                     offset - sizeof(struct ip6_frag) +
  300                     offsetof(struct ip6_frag, ip6f_offlg));
  301                 IP6Q_UNLOCK();
  302                 return (IPPROTO_DONE);
  303         }
  304         /*
  305          * If it's the first fragment, do the above check for each
  306          * fragment already stored in the reassembly queue.
  307          */
  308         if (fragoff == 0) {
  309                 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
  310                      af6 = af6dwn) {
  311                         af6dwn = af6->ip6af_down;
  312 
  313                         if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
  314                             IPV6_MAXPACKET) {
  315                                 struct mbuf *merr = IP6_REASS_MBUF(af6);
  316                                 struct ip6_hdr *ip6err;
  317                                 int erroff = af6->ip6af_offset;
  318 
  319                                 /* dequeue the fragment. */
  320                                 frag6_deq(af6);
  321                                 free(af6, M_FTABLE);
  322 
  323                                 /* adjust pointer. */
  324                                 ip6err = mtod(merr, struct ip6_hdr *);
  325 
  326                                 /*
  327                                  * Restore source and destination addresses
  328                                  * in the erroneous IPv6 header.
  329                                  */
  330                                 ip6err->ip6_src = q6->ip6q_src;
  331                                 ip6err->ip6_dst = q6->ip6q_dst;
  332 
  333                                 icmp6_error(merr, ICMP6_PARAM_PROB,
  334                                     ICMP6_PARAMPROB_HEADER,
  335                                     erroff - sizeof(struct ip6_frag) +
  336                                     offsetof(struct ip6_frag, ip6f_offlg));
  337                         }
  338                 }
  339         }
  340 
  341         ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
  342             M_NOWAIT);
  343         if (ip6af == NULL)
  344                 goto dropfrag;
  345         bzero(ip6af, sizeof(*ip6af));
  346         ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
  347         ip6af->ip6af_off = fragoff;
  348         ip6af->ip6af_frglen = frgpartlen;
  349         ip6af->ip6af_offset = offset;
  350         IP6_REASS_MBUF(ip6af) = m;
  351 
  352         if (first_frag) {
  353                 af6 = (struct ip6asfrag *)q6;
  354                 goto insert;
  355         }
  356 
  357         /*
  358          * Handle ECN by comparing this segment with the first one;
  359          * if CE is set, do not lose CE.
  360          * drop if CE and not-ECT are mixed for the same packet.
  361          */
  362         ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
  363         ecn0 = q6->ip6q_ecn;
  364         if (ecn == IPTOS_ECN_CE) {
  365                 if (ecn0 == IPTOS_ECN_NOTECT) {
  366                         free(ip6af, M_FTABLE);
  367                         goto dropfrag;
  368                 }
  369                 if (ecn0 != IPTOS_ECN_CE)
  370                         q6->ip6q_ecn = IPTOS_ECN_CE;
  371         }
  372         if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
  373                 free(ip6af, M_FTABLE);
  374                 goto dropfrag;
  375         }
  376 
  377         /*
  378          * Find a segment which begins after this one does.
  379          */
  380         for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
  381              af6 = af6->ip6af_down)
  382                 if (af6->ip6af_off > ip6af->ip6af_off)
  383                         break;
  384 
  385 #if 0
  386         /*
  387          * If there is a preceding segment, it may provide some of
  388          * our data already.  If so, drop the data from the incoming
  389          * segment.  If it provides all of our data, drop us.
  390          */
  391         if (af6->ip6af_up != (struct ip6asfrag *)q6) {
  392                 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
  393                         - ip6af->ip6af_off;
  394                 if (i > 0) {
  395                         if (i >= ip6af->ip6af_frglen)
  396                                 goto dropfrag;
  397                         m_adj(IP6_REASS_MBUF(ip6af), i);
  398                         ip6af->ip6af_off += i;
  399                         ip6af->ip6af_frglen -= i;
  400                 }
  401         }
  402 
  403         /*
  404          * While we overlap succeeding segments trim them or,
  405          * if they are completely covered, dequeue them.
  406          */
  407         while (af6 != (struct ip6asfrag *)q6 &&
  408                ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
  409                 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
  410                 if (i < af6->ip6af_frglen) {
  411                         af6->ip6af_frglen -= i;
  412                         af6->ip6af_off += i;
  413                         m_adj(IP6_REASS_MBUF(af6), i);
  414                         break;
  415                 }
  416                 af6 = af6->ip6af_down;
  417                 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
  418                 frag6_deq(af6->ip6af_up);
  419         }
  420 #else
  421         /*
  422          * If the incoming framgent overlaps some existing fragments in
  423          * the reassembly queue, drop it, since it is dangerous to override
  424          * existing fragments from a security point of view.
  425          * We don't know which fragment is the bad guy - here we trust
  426          * fragment that came in earlier, with no real reason.
  427          *
  428          * Note: due to changes after disabling this part, mbuf passed to
  429          * m_adj() below now does not meet the requirement.
  430          */
  431         if (af6->ip6af_up != (struct ip6asfrag *)q6) {
  432                 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
  433                         - ip6af->ip6af_off;
  434                 if (i > 0) {
  435 #if 0                           /* suppress the noisy log */
  436                         log(LOG_ERR, "%d bytes of a fragment from %s "
  437                             "overlaps the previous fragment\n",
  438                             i, ip6_sprintf(&q6->ip6q_src));
  439 #endif
  440                         free(ip6af, M_FTABLE);
  441                         goto dropfrag;
  442                 }
  443         }
  444         if (af6 != (struct ip6asfrag *)q6) {
  445                 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
  446                 if (i > 0) {
  447 #if 0                           /* suppress the noisy log */
  448                         log(LOG_ERR, "%d bytes of a fragment from %s "
  449                             "overlaps the succeeding fragment",
  450                             i, ip6_sprintf(&q6->ip6q_src));
  451 #endif
  452                         free(ip6af, M_FTABLE);
  453                         goto dropfrag;
  454                 }
  455         }
  456 #endif
  457 
  458 insert:
  459 
  460         /*
  461          * Stick new segment in its place;
  462          * check for complete reassembly.
  463          * Move to front of packet queue, as we are
  464          * the most recently active fragmented packet.
  465          */
  466         frag6_enq(ip6af, af6->ip6af_up);
  467         frag6_nfrags++;
  468         q6->ip6q_nfrag++;
  469 #if 0 /* xxx */
  470         if (q6 != ip6q.ip6q_next) {
  471                 frag6_remque(q6);
  472                 frag6_insque(q6, &ip6q);
  473         }
  474 #endif
  475         next = 0;
  476         for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
  477              af6 = af6->ip6af_down) {
  478                 if (af6->ip6af_off != next) {
  479                         IP6Q_UNLOCK();
  480                         return IPPROTO_DONE;
  481                 }
  482                 next += af6->ip6af_frglen;
  483         }
  484         if (af6->ip6af_up->ip6af_mff) {
  485                 IP6Q_UNLOCK();
  486                 return IPPROTO_DONE;
  487         }
  488 
  489         /*
  490          * Reassembly is complete; concatenate fragments.
  491          */
  492         ip6af = q6->ip6q_down;
  493         t = m = IP6_REASS_MBUF(ip6af);
  494         af6 = ip6af->ip6af_down;
  495         frag6_deq(ip6af);
  496         while (af6 != (struct ip6asfrag *)q6) {
  497                 af6dwn = af6->ip6af_down;
  498                 frag6_deq(af6);
  499                 while (t->m_next)
  500                         t = t->m_next;
  501                 t->m_next = IP6_REASS_MBUF(af6);
  502                 m_adj(t->m_next, af6->ip6af_offset);
  503                 free(af6, M_FTABLE);
  504                 af6 = af6dwn;
  505         }
  506 
  507         /* adjust offset to point where the original next header starts */
  508         offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
  509         free(ip6af, M_FTABLE);
  510         ip6 = mtod(m, struct ip6_hdr *);
  511         ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
  512         if (q6->ip6q_ecn == IPTOS_ECN_CE)
  513                 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
  514         nxt = q6->ip6q_nxt;
  515 #ifdef notyet
  516         *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
  517 #endif
  518 
  519         /* Delete frag6 header */
  520         if (m->m_len >= offset + sizeof(struct ip6_frag)) {
  521                 /* This is the only possible case with !PULLDOWN_TEST */
  522                 ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
  523                     offset);
  524                 m->m_data += sizeof(struct ip6_frag);
  525                 m->m_len -= sizeof(struct ip6_frag);
  526         } else {
  527                 /* this comes with no copy if the boundary is on cluster */
  528                 if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
  529                         frag6_remque(q6);
  530                         frag6_nfrags -= q6->ip6q_nfrag;
  531                         free(q6, M_FTABLE);
  532                         frag6_nfragpackets--;
  533                         goto dropfrag;
  534                 }
  535                 m_adj(t, sizeof(struct ip6_frag));
  536                 m_cat(m, t);
  537         }
  538 
  539         /*
  540          * Store NXT to the original.
  541          */
  542         {
  543                 char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
  544                 *prvnxtp = nxt;
  545         }
  546 
  547         frag6_remque(q6);
  548         frag6_nfrags -= q6->ip6q_nfrag;
  549         free(q6, M_FTABLE);
  550         frag6_nfragpackets--;
  551 
  552         if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
  553                 int plen = 0;
  554                 for (t = m; t; t = t->m_next)
  555                         plen += t->m_len;
  556                 m->m_pkthdr.len = plen;
  557         }
  558 
  559         ip6stat.ip6s_reassembled++;
  560         in6_ifstat_inc(dstifp, ifs6_reass_ok);
  561 
  562         /*
  563          * Tell launch routine the next header
  564          */
  565 
  566         *mp = m;
  567         *offp = offset;
  568 
  569         IP6Q_UNLOCK();
  570         return nxt;
  571 
  572  dropfrag:
  573         IP6Q_UNLOCK();
  574         in6_ifstat_inc(dstifp, ifs6_reass_fail);
  575         ip6stat.ip6s_fragdropped++;
  576         m_freem(m);
  577         return IPPROTO_DONE;
  578 }
  579 
  580 /*
  581  * Free a fragment reassembly header and all
  582  * associated datagrams.
  583  */
  584 void
  585 frag6_freef(q6)
  586         struct ip6q *q6;
  587 {
  588         struct ip6asfrag *af6, *down6;
  589 
  590         IP6Q_LOCK_ASSERT();
  591 
  592         for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
  593              af6 = down6) {
  594                 struct mbuf *m = IP6_REASS_MBUF(af6);
  595 
  596                 down6 = af6->ip6af_down;
  597                 frag6_deq(af6);
  598 
  599                 /*
  600                  * Return ICMP time exceeded error for the 1st fragment.
  601                  * Just free other fragments.
  602                  */
  603                 if (af6->ip6af_off == 0) {
  604                         struct ip6_hdr *ip6;
  605 
  606                         /* adjust pointer */
  607                         ip6 = mtod(m, struct ip6_hdr *);
  608 
  609                         /* restore source and destination addresses */
  610                         ip6->ip6_src = q6->ip6q_src;
  611                         ip6->ip6_dst = q6->ip6q_dst;
  612 
  613                         icmp6_error(m, ICMP6_TIME_EXCEEDED,
  614                                     ICMP6_TIME_EXCEED_REASSEMBLY, 0);
  615                 } else
  616                         m_freem(m);
  617                 free(af6, M_FTABLE);
  618         }
  619         frag6_remque(q6);
  620         frag6_nfrags -= q6->ip6q_nfrag;
  621         free(q6, M_FTABLE);
  622         frag6_nfragpackets--;
  623 }
  624 
  625 /*
  626  * Put an ip fragment on a reassembly chain.
  627  * Like insque, but pointers in middle of structure.
  628  */
  629 void
  630 frag6_enq(af6, up6)
  631         struct ip6asfrag *af6, *up6;
  632 {
  633 
  634         IP6Q_LOCK_ASSERT();
  635 
  636         af6->ip6af_up = up6;
  637         af6->ip6af_down = up6->ip6af_down;
  638         up6->ip6af_down->ip6af_up = af6;
  639         up6->ip6af_down = af6;
  640 }
  641 
  642 /*
  643  * To frag6_enq as remque is to insque.
  644  */
  645 void
  646 frag6_deq(af6)
  647         struct ip6asfrag *af6;
  648 {
  649 
  650         IP6Q_LOCK_ASSERT();
  651 
  652         af6->ip6af_up->ip6af_down = af6->ip6af_down;
  653         af6->ip6af_down->ip6af_up = af6->ip6af_up;
  654 }
  655 
  656 void
  657 frag6_insque(new, old)
  658         struct ip6q *new, *old;
  659 {
  660 
  661         IP6Q_LOCK_ASSERT();
  662 
  663         new->ip6q_prev = old;
  664         new->ip6q_next = old->ip6q_next;
  665         old->ip6q_next->ip6q_prev= new;
  666         old->ip6q_next = new;
  667 }
  668 
  669 void
  670 frag6_remque(p6)
  671         struct ip6q *p6;
  672 {
  673 
  674         IP6Q_LOCK_ASSERT();
  675 
  676         p6->ip6q_prev->ip6q_next = p6->ip6q_next;
  677         p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
  678 }
  679 
  680 /*
  681  * IPv6 reassembling timer processing;
  682  * if a timer expires on a reassembly
  683  * queue, discard it.
  684  */
  685 void
  686 frag6_slowtimo()
  687 {
  688         struct ip6q *q6;
  689         int s = splnet();
  690 
  691         IP6Q_LOCK();
  692         q6 = ip6q.ip6q_next;
  693         if (q6)
  694                 while (q6 != &ip6q) {
  695                         --q6->ip6q_ttl;
  696                         q6 = q6->ip6q_next;
  697                         if (q6->ip6q_prev->ip6q_ttl == 0) {
  698                                 ip6stat.ip6s_fragtimeout++;
  699                                 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
  700                                 frag6_freef(q6->ip6q_prev);
  701                         }
  702                 }
  703         /*
  704          * If we are over the maximum number of fragments
  705          * (due to the limit being lowered), drain off
  706          * enough to get down to the new limit.
  707          */
  708         while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
  709             ip6q.ip6q_prev) {
  710                 ip6stat.ip6s_fragoverflow++;
  711                 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
  712                 frag6_freef(ip6q.ip6q_prev);
  713         }
  714         IP6Q_UNLOCK();
  715 
  716 #if 0
  717         /*
  718          * Routing changes might produce a better route than we last used;
  719          * make sure we notice eventually, even if forwarding only for one
  720          * destination and the cache is never replaced.
  721          */
  722         if (ip6_forward_rt.ro_rt) {
  723                 RTFREE(ip6_forward_rt.ro_rt);
  724                 ip6_forward_rt.ro_rt = 0;
  725         }
  726         if (ipsrcchk_rt.ro_rt) {
  727                 RTFREE(ipsrcchk_rt.ro_rt);
  728                 ipsrcchk_rt.ro_rt = 0;
  729         }
  730 #endif
  731 
  732         splx(s);
  733 }
  734 
  735 /*
  736  * Drain off all datagram fragments.
  737  */
  738 void
  739 frag6_drain()
  740 {
  741 
  742         if (IP6Q_TRYLOCK() == 0)
  743                 return;
  744         while (ip6q.ip6q_next != &ip6q) {
  745                 ip6stat.ip6s_fragdropped++;
  746                 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
  747                 frag6_freef(ip6q.ip6q_next);
  748         }
  749         IP6Q_UNLOCK();
  750 }

Cache object: af189547cf567683b42af5e3af5de4d2


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