[BACK]Return to frag6.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / netinet6

Annotation of src/sys/netinet6/frag6.c, Revision 1.10.4.1

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

CVSweb <webmaster@jp.NetBSD.org>