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/bsd/netinet6/frag6.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 /*      $FreeBSD: src/sys/netinet6/frag6.c,v 1.2.2.5 2001/07/03 11:01:50 ume Exp $      */
    2 /*      $KAME: frag6.c,v 1.31 2001/05/17 13:45:34 jinmei 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 #include <kern/queue.h>
   45 
   46 #include <net/if.h>
   47 #include <net/route.h>
   48 
   49 #include <netinet/in.h>
   50 #include <netinet/in_var.h>
   51 #include <netinet/ip6.h>
   52 #include <netinet6/ip6_var.h>
   53 #include <netinet/icmp6.h>
   54 
   55 #include <net/net_osdep.h>
   56 
   57 /*
   58  * Define it to get a correct behavior on per-interface statistics.
   59  * You will need to perform an extra routing table lookup, per fragment,
   60  * to do it.  This may, or may not be, a performance hit.
   61  */
   62 #define IN6_IFSTAT_STRICT
   63 
   64 static void frag6_enq __P((struct ip6asfrag *, struct ip6asfrag *));
   65 static void frag6_deq __P((struct ip6asfrag *));
   66 static void frag6_insque __P((struct ip6q *, struct ip6q *));
   67 static void frag6_remque __P((struct ip6q *));
   68 static void frag6_freef __P((struct ip6q *));
   69 
   70 /* XXX we eventually need splreass6, or some real semaphore */
   71 int frag6_doing_reass;
   72 u_int frag6_nfragpackets;
   73 struct  ip6q ip6q;      /* ip6 reassemble queue */
   74 
   75 #ifndef __APPLE__
   76 MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
   77 #endif
   78 
   79 /*
   80  * Initialise reassembly queue and fragment identifier.
   81  */
   82 void
   83 frag6_init()
   84 {
   85         struct timeval tv;
   86 
   87         ip6_maxfragpackets = nmbclusters / 4;
   88 
   89         /*
   90          * in many cases, random() here does NOT return random number
   91          * as initialization during bootstrap time occur in fixed order.
   92          */
   93         microtime(&tv);
   94         ip6_id = random() ^ tv.tv_usec;
   95         ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
   96 }
   97 
   98 /*
   99  * In RFC2460, fragment and reassembly rule do not agree with each other,
  100  * in terms of next header field handling in fragment header.
  101  * While the sender will use the same value for all of the fragmented packets,
  102  * receiver is suggested not to check the consistency.
  103  *
  104  * fragment rule (p20):
  105  *      (2) A Fragment header containing:
  106  *      The Next Header value that identifies the first header of
  107  *      the Fragmentable Part of the original packet.
  108  *              -> next header field is same for all fragments
  109  *
  110  * reassembly rule (p21):
  111  *      The Next Header field of the last header of the Unfragmentable
  112  *      Part is obtained from the Next Header field of the first
  113  *      fragment's Fragment header.
  114  *              -> should grab it from the first fragment only
  115  *
  116  * The following note also contradicts with fragment rule - noone is going to
  117  * send different fragment with different next header field.
  118  *
  119  * additional note (p22):
  120  *      The Next Header values in the Fragment headers of different
  121  *      fragments of the same original packet may differ.  Only the value
  122  *      from the Offset zero fragment packet is used for reassembly.
  123  *              -> should grab it from the first fragment only
  124  *
  125  * There is no explicit reason given in the RFC.  Historical reason maybe?
  126  */
  127 /*
  128  * Fragment input
  129  */
  130 int
  131 frag6_input(mp, offp)
  132         struct mbuf **mp;
  133         int *offp;
  134 {
  135         struct mbuf *m = *mp, *t;
  136         struct ip6_hdr *ip6;
  137         struct ip6_frag *ip6f;
  138         struct ip6q *q6;
  139         struct ip6asfrag *af6, *ip6af, *af6dwn;
  140         int offset = *offp, nxt, i, next;
  141         int first_frag = 0;
  142         int fragoff, frgpartlen;        /* must be larger than u_int16_t */
  143         struct ifnet *dstifp;
  144 #ifdef IN6_IFSTAT_STRICT
  145         static struct route_in6 ro;
  146         struct sockaddr_in6 *dst;
  147 #endif
  148 
  149         ip6 = mtod(m, struct ip6_hdr *);
  150 #ifndef PULLDOWN_TEST
  151         IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
  152         ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
  153 #else
  154         IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
  155         if (ip6f == NULL)
  156                 return IPPROTO_DONE;
  157 #endif
  158 
  159         dstifp = NULL;
  160 #ifdef IN6_IFSTAT_STRICT
  161         /* find the destination interface of the packet. */
  162         dst = (struct sockaddr_in6 *)&ro.ro_dst;
  163         if (ro.ro_rt
  164          && ((ro.ro_rt->rt_flags & RTF_UP) == 0
  165           || !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
  166                 rtfree(ro.ro_rt);
  167                 ro.ro_rt = (struct rtentry *)0;
  168         }
  169         if (ro.ro_rt == NULL) {
  170                 bzero(dst, sizeof(*dst));
  171                 dst->sin6_family = AF_INET6;
  172                 dst->sin6_len = sizeof(struct sockaddr_in6);
  173                 dst->sin6_addr = ip6->ip6_dst;
  174         }
  175         rtalloc((struct route *)&ro);
  176         if (ro.ro_rt != NULL && ro.ro_rt->rt_ifa != NULL)
  177                 dstifp = ((struct in6_ifaddr *)ro.ro_rt->rt_ifa)->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,
  200                             ICMP6_PARAMPROB_HEADER,
  201                             offsetof(struct ip6_hdr, ip6_plen));
  202                 in6_ifstat_inc(dstifp, ifs6_reass_fail);
  203                 return IPPROTO_DONE;
  204         }
  205 
  206         ip6stat.ip6s_fragments++;
  207         in6_ifstat_inc(dstifp, ifs6_reass_reqd);
  208         
  209         /* offset now points to data portion */
  210         offset += sizeof(struct ip6_frag);
  211 
  212         frag6_doing_reass = 1;
  213 
  214         for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
  215                 if (ip6f->ip6f_ident == q6->ip6q_ident &&
  216                     IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
  217                     IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
  218                         break;
  219 
  220         if (q6 == &ip6q) {
  221                 /*
  222                  * the first fragment to arrive, create a reassembly queue.
  223                  */
  224                 first_frag = 1;
  225 
  226                 /*
  227                  * Enforce upper bound on number of fragmented packets
  228                  * for which we attempt reassembly;
  229                  * If maxfrag is 0, never accept fragments.
  230                  * If maxfrag is -1, accept all fragments without limitation.
  231                  */
  232                 if (ip6_maxfragpackets < 0)
  233                         ;
  234                 else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
  235                         goto dropfrag;
  236                 frag6_nfragpackets++;
  237                 q6 = (struct ip6q *)_MALLOC(sizeof(struct ip6q), M_FTABLE,
  238                         M_DONTWAIT);
  239                 if (q6 == NULL)
  240                         goto dropfrag;
  241                 bzero(q6, sizeof(*q6));
  242 
  243                 frag6_insque(q6, &ip6q);
  244 
  245                 /* ip6q_nxt will be filled afterwards, from 1st fragment */
  246                 q6->ip6q_down   = q6->ip6q_up = (struct ip6asfrag *)q6;
  247 #ifdef notyet
  248                 q6->ip6q_nxtp   = (u_char *)nxtp;
  249 #endif
  250                 q6->ip6q_ident  = ip6f->ip6f_ident;
  251                 q6->ip6q_arrive = 0; /* Is it used anywhere? */
  252                 q6->ip6q_ttl    = IPV6_FRAGTTL;
  253                 q6->ip6q_src    = ip6->ip6_src;
  254                 q6->ip6q_dst    = ip6->ip6_dst;
  255                 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
  256         }
  257 
  258         /*
  259          * If it's the 1st fragment, record the length of the
  260          * unfragmentable part and the next header of the fragment header.
  261          */
  262         fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
  263         if (fragoff == 0) {
  264                 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr)
  265                         - sizeof(struct ip6_frag);
  266                 q6->ip6q_nxt = ip6f->ip6f_nxt;
  267         }
  268 
  269         /*
  270          * Check that the reassembled packet would not exceed 65535 bytes
  271          * in size.
  272          * If it would exceed, discard the fragment and return an ICMP error.
  273          */
  274         frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
  275         if (q6->ip6q_unfrglen >= 0) {
  276                 /* The 1st fragment has already arrived. */
  277                 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
  278                         icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
  279                                     offset - sizeof(struct ip6_frag) +
  280                                         offsetof(struct ip6_frag, ip6f_offlg));
  281                         frag6_doing_reass = 0;
  282                         return(IPPROTO_DONE);
  283                 }
  284         }
  285         else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
  286                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
  287                             offset - sizeof(struct ip6_frag) +
  288                                 offsetof(struct ip6_frag, ip6f_offlg));
  289                 frag6_doing_reass = 0;
  290                 return(IPPROTO_DONE);
  291         }
  292         /*
  293          * If it's the first fragment, do the above check for each
  294          * fragment already stored in the reassembly queue.
  295          */
  296         if (fragoff == 0) {
  297                 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
  298                      af6 = af6dwn) {
  299                         af6dwn = af6->ip6af_down;
  300 
  301                         if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
  302                             IPV6_MAXPACKET) {
  303                                 struct mbuf *merr = IP6_REASS_MBUF(af6);
  304                                 struct ip6_hdr *ip6err;
  305                                 int erroff = af6->ip6af_offset;
  306 
  307                                 /* dequeue the fragment. */
  308                                 frag6_deq(af6);
  309                                 FREE(af6, M_FTABLE);
  310 
  311                                 /* adjust pointer. */
  312                                 ip6err = mtod(merr, struct ip6_hdr *);
  313 
  314                                 /*
  315                                  * Restore source and destination addresses
  316                                  * in the erroneous IPv6 header.
  317                                  */
  318                                 ip6err->ip6_src = q6->ip6q_src;
  319                                 ip6err->ip6_dst = q6->ip6q_dst;
  320 
  321                                 icmp6_error(merr, ICMP6_PARAM_PROB,
  322                                             ICMP6_PARAMPROB_HEADER,
  323                                             erroff - sizeof(struct ip6_frag) +
  324                                                 offsetof(struct ip6_frag, ip6f_offlg));
  325                         }
  326                 }
  327         }
  328 
  329         ip6af = (struct ip6asfrag *)_MALLOC(sizeof(struct ip6asfrag), M_FTABLE,
  330             M_DONTWAIT);
  331         if (ip6af == NULL)
  332                 goto dropfrag;
  333         bzero(ip6af, sizeof(*ip6af));
  334         ip6af->ip6af_head = ip6->ip6_flow;
  335         ip6af->ip6af_len = ip6->ip6_plen;
  336         ip6af->ip6af_nxt = ip6->ip6_nxt;
  337         ip6af->ip6af_hlim = ip6->ip6_hlim;
  338         ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
  339         ip6af->ip6af_off = fragoff;
  340         ip6af->ip6af_frglen = frgpartlen;
  341         ip6af->ip6af_offset = offset;
  342         IP6_REASS_MBUF(ip6af) = m;
  343 
  344         if (first_frag) {
  345                 af6 = (struct ip6asfrag *)q6;
  346                 goto insert;
  347         }
  348 
  349         /*
  350          * Find a segment which begins after this one does.
  351          */
  352         for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
  353              af6 = af6->ip6af_down)
  354                 if (af6->ip6af_off > ip6af->ip6af_off)
  355                         break;
  356 
  357 #if 0
  358         /*
  359          * If there is a preceding segment, it may provide some of
  360          * our data already.  If so, drop the data from the incoming
  361          * segment.  If it provides all of our data, drop us.
  362          */
  363         if (af6->ip6af_up != (struct ip6asfrag *)q6) {
  364                 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
  365                         - ip6af->ip6af_off;
  366                 if (i > 0) {
  367                         if (i >= ip6af->ip6af_frglen)
  368                                 goto dropfrag;
  369                         m_adj(IP6_REASS_MBUF(ip6af), i);
  370                         ip6af->ip6af_off += i;
  371                         ip6af->ip6af_frglen -= i;
  372                 }
  373         }
  374 
  375         /*
  376          * While we overlap succeeding segments trim them or,
  377          * if they are completely covered, dequeue them.
  378          */
  379         while (af6 != (struct ip6asfrag *)q6 &&
  380                ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
  381                 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
  382                 if (i < af6->ip6af_frglen) {
  383                         af6->ip6af_frglen -= i;
  384                         af6->ip6af_off += i;
  385                         m_adj(IP6_REASS_MBUF(af6), i);
  386                         break;
  387                 }
  388                 af6 = af6->ip6af_down;
  389                 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
  390                 frag6_deq(af6->ip6af_up);
  391         }
  392 #else
  393         /*
  394          * If the incoming framgent overlaps some existing fragments in
  395          * the reassembly queue, drop it, since it is dangerous to override
  396          * existing fragments from a security point of view.
  397          */
  398         if (af6->ip6af_up != (struct ip6asfrag *)q6) {
  399                 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
  400                         - ip6af->ip6af_off;
  401                 if (i > 0) {
  402 #if 0                           /* suppress the noisy log */
  403                         log(LOG_ERR, "%d bytes of a fragment from %s "
  404                             "overlaps the previous fragment\n",
  405                             i, ip6_sprintf(&q6->ip6q_src));
  406 #endif
  407                         FREE(ip6af, M_FTABLE);
  408                         goto dropfrag;
  409                 }
  410         }
  411         if (af6 != (struct ip6asfrag *)q6) {
  412                 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
  413                 if (i > 0) {
  414 #if 0                           /* suppress the noisy log */
  415                         log(LOG_ERR, "%d bytes of a fragment from %s "
  416                             "overlaps the succeeding fragment",
  417                             i, ip6_sprintf(&q6->ip6q_src));
  418 #endif
  419                         FREE(ip6af, M_FTABLE);
  420                         goto dropfrag;
  421                 }
  422         }
  423 #endif
  424 
  425 insert:
  426 
  427         /*
  428          * Stick new segment in its place;
  429          * check for complete reassembly.
  430          * Move to front of packet queue, as we are
  431          * the most recently active fragmented packet.
  432          */
  433         frag6_enq(ip6af, af6->ip6af_up);
  434 #if 0 /* xxx */
  435         if (q6 != ip6q.ip6q_next) {
  436                 frag6_remque(q6);
  437                 frag6_insque(q6, &ip6q);
  438         }
  439 #endif
  440         next = 0;
  441         for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
  442              af6 = af6->ip6af_down) {
  443                 if (af6->ip6af_off != next) {
  444                         frag6_doing_reass = 0;
  445                         return IPPROTO_DONE;
  446                 }
  447                 next += af6->ip6af_frglen;
  448         }
  449         if (af6->ip6af_up->ip6af_mff) {
  450                 frag6_doing_reass = 0;
  451                 return IPPROTO_DONE;
  452         }
  453 
  454         /*
  455          * Reassembly is complete; concatenate fragments.
  456          */
  457         ip6af = q6->ip6q_down;
  458         t = m = IP6_REASS_MBUF(ip6af);
  459         af6 = ip6af->ip6af_down;
  460         frag6_deq(ip6af);
  461         while (af6 != (struct ip6asfrag *)q6) {
  462                 af6dwn = af6->ip6af_down;
  463                 frag6_deq(af6);
  464                 while (t->m_next)
  465                         t = t->m_next;
  466                 t->m_next = IP6_REASS_MBUF(af6);
  467                 m_adj(t->m_next, af6->ip6af_offset);
  468                 FREE(af6, M_FTABLE);
  469                 af6 = af6dwn;
  470         }
  471 
  472         /* adjust offset to point where the original next header starts */
  473         offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
  474         FREE(ip6af, M_FTABLE);
  475         ip6 = mtod(m, struct ip6_hdr *);
  476         ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
  477         ip6->ip6_src = q6->ip6q_src;
  478         ip6->ip6_dst = q6->ip6q_dst;
  479         nxt = q6->ip6q_nxt;
  480 #if notyet
  481         *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
  482 #endif
  483 
  484         /*
  485          * Delete frag6 header with as a few cost as possible.
  486          */
  487         if (offset < m->m_len) {
  488                 ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
  489                         offset);
  490                 m->m_data += sizeof(struct ip6_frag);
  491                 m->m_len -= sizeof(struct ip6_frag);
  492         } else {
  493                 /* this comes with no copy if the boundary is on cluster */
  494                 if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
  495                         frag6_remque(q6);
  496                         FREE(q6, M_FTABLE);
  497                         frag6_nfragpackets--;
  498                         goto dropfrag;
  499                 }
  500                 m_adj(t, sizeof(struct ip6_frag));
  501                 m_cat(m, t);
  502         }
  503 
  504         /*
  505          * Store NXT to the original.
  506          */
  507         {
  508                 char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
  509                 *prvnxtp = nxt;
  510         }
  511 
  512         frag6_remque(q6);
  513         FREE(q6, M_FTABLE);
  514         frag6_nfragpackets--;
  515 
  516         if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
  517                 int plen = 0;
  518                 for (t = m; t; t = t->m_next)
  519                         plen += t->m_len;
  520                 m->m_pkthdr.len = plen;
  521         }
  522         
  523         ip6stat.ip6s_reassembled++;
  524         in6_ifstat_inc(dstifp, ifs6_reass_ok);
  525 
  526         /*
  527          * Tell launch routine the next header
  528          */
  529 
  530         *mp = m;
  531         *offp = offset;
  532 
  533         frag6_doing_reass = 0;
  534         return nxt;
  535 
  536  dropfrag:
  537         in6_ifstat_inc(dstifp, ifs6_reass_fail);
  538         ip6stat.ip6s_fragdropped++;
  539         m_freem(m);
  540         frag6_doing_reass = 0;
  541         return IPPROTO_DONE;
  542 }
  543 
  544 /*
  545  * Free a fragment reassembly header and all
  546  * associated datagrams.
  547  */
  548 void
  549 frag6_freef(q6)
  550         struct ip6q *q6;
  551 {
  552         struct ip6asfrag *af6, *down6;
  553 
  554         for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
  555              af6 = down6) {
  556                 struct mbuf *m = IP6_REASS_MBUF(af6);
  557 
  558                 down6 = af6->ip6af_down;
  559                 frag6_deq(af6);
  560 
  561                 /*
  562                  * Return ICMP time exceeded error for the 1st fragment.
  563                  * Just free other fragments.
  564                  */
  565                 if (af6->ip6af_off == 0) {
  566                         struct ip6_hdr *ip6;
  567 
  568                         /* adjust pointer */
  569                         ip6 = mtod(m, struct ip6_hdr *);
  570 
  571                         /* restoure source and destination addresses */
  572                         ip6->ip6_src = q6->ip6q_src;
  573                         ip6->ip6_dst = q6->ip6q_dst;
  574 
  575                         icmp6_error(m, ICMP6_TIME_EXCEEDED,
  576                                     ICMP6_TIME_EXCEED_REASSEMBLY, 0);
  577                 } else
  578                         m_freem(m);
  579                 FREE(af6, M_FTABLE);
  580 
  581         }
  582         frag6_remque(q6);
  583         FREE(q6, M_FTABLE);
  584         frag6_nfragpackets--;
  585 }
  586 
  587 /*
  588  * Put an ip fragment on a reassembly chain.
  589  * Like insque, but pointers in middle of structure.
  590  */
  591 void
  592 frag6_enq(af6, up6)
  593         struct ip6asfrag *af6, *up6;
  594 {
  595         af6->ip6af_up = up6;
  596         af6->ip6af_down = up6->ip6af_down;
  597         up6->ip6af_down->ip6af_up = af6;
  598         up6->ip6af_down = af6;
  599 }
  600 
  601 /*
  602  * To frag6_enq as remque is to insque.
  603  */
  604 void
  605 frag6_deq(af6)
  606         struct ip6asfrag *af6;
  607 {
  608         af6->ip6af_up->ip6af_down = af6->ip6af_down;
  609         af6->ip6af_down->ip6af_up = af6->ip6af_up;
  610 }
  611 
  612 void
  613 frag6_insque(new, old)
  614         struct ip6q *new, *old;
  615 {
  616         new->ip6q_prev = old;
  617         new->ip6q_next = old->ip6q_next;
  618         old->ip6q_next->ip6q_prev= new;
  619         old->ip6q_next = new;
  620 }
  621 
  622 void
  623 frag6_remque(p6)
  624         struct ip6q *p6;
  625 {
  626         p6->ip6q_prev->ip6q_next = p6->ip6q_next;
  627         p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
  628 }
  629 
  630 /*
  631  * IPv6 reassembling timer processing;
  632  * if a timer expires on a reassembly
  633  * queue, discard it.
  634  */
  635 void
  636 frag6_slowtimo()
  637 {
  638         struct ip6q *q6;
  639         int s = splnet();
  640 
  641         frag6_doing_reass = 1;
  642         q6 = ip6q.ip6q_next;
  643         if (q6)
  644                 while (q6 != &ip6q) {
  645                         --q6->ip6q_ttl;
  646                         q6 = q6->ip6q_next;
  647                         if (q6->ip6q_prev->ip6q_ttl == 0) {
  648                                 ip6stat.ip6s_fragtimeout++;
  649                                 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
  650                                 frag6_freef(q6->ip6q_prev);
  651                         }
  652                 }
  653         /*
  654          * If we are over the maximum number of fragments
  655          * (due to the limit being lowered), drain off
  656          * enough to get down to the new limit.
  657          */
  658         while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
  659             ip6q.ip6q_prev) {
  660                 ip6stat.ip6s_fragoverflow++;
  661                 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
  662                 frag6_freef(ip6q.ip6q_prev);
  663         }
  664         frag6_doing_reass = 0;
  665 
  666 #if 0
  667         /*
  668          * Routing changes might produce a better route than we last used;
  669          * make sure we notice eventually, even if forwarding only for one
  670          * destination and the cache is never replaced.
  671          */
  672         if (ip6_forward_rt.ro_rt) {
  673                 rtfree(ip6_forward_rt.ro_rt);
  674                 ip6_forward_rt.ro_rt = 0;
  675         }
  676         if (ipsrcchk_rt.ro_rt) {
  677                 rtfree(ipsrcchk_rt.ro_rt);
  678                 ipsrcchk_rt.ro_rt = 0;
  679         }
  680 #endif
  681 
  682         splx(s);
  683 }
  684 
  685 /*
  686  * Drain off all datagram fragments.
  687  */
  688 void
  689 frag6_drain()
  690 {
  691         if (frag6_doing_reass)
  692                 return;
  693         while (ip6q.ip6q_next != &ip6q) {
  694                 ip6stat.ip6s_fragdropped++;
  695                 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
  696                 frag6_freef(ip6q.ip6q_next);
  697         }
  698 }

Cache object: 515817880e1428b0700dd367d7aaf48f


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