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

1.10.4.2! msaitoh     1: /*     $NetBSD: frag6.c,v 1.10.4.1 2001/05/26 16:05:44 he 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:
1.10.4.2! msaitoh   209:        frag6_doing_reass = 1;
        !           210:
1.2       itojun    211:        for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
                    212:                if (ip6f->ip6f_ident == q6->ip6q_ident &&
                    213:                    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
                    214:                    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
                    215:                        break;
                    216:
                    217:        if (q6 == &ip6q) {
                    218:                /*
                    219:                 * the first fragment to arrive, create a reassembly queue.
                    220:                 */
                    221:                first_frag = 1;
                    222:
                    223:                /*
                    224:                 * Enforce upper bound on number of fragmented packets
                    225:                 * for which we attempt reassembly;
                    226:                 * If maxfrag is 0, never accept fragments.
                    227:                 * If maxfrag is -1, accept all fragments without limitation.
                    228:                 */
1.10.4.2! msaitoh   229:                if (ip6_maxfragpackets < 0)
        !           230:                        ;
        !           231:                else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
        !           232:                        goto dropfrag;
        !           233:                frag6_nfragpackets++;
1.2       itojun    234:                q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
                    235:                        M_DONTWAIT);
                    236:                if (q6 == NULL)
                    237:                        goto dropfrag;
1.9       itojun    238:                bzero(q6, sizeof(*q6));
1.2       itojun    239:
                    240:                frag6_insque(q6, &ip6q);
                    241:
1.9       itojun    242:                /* ip6q_nxt will be filled afterwards, from 1st fragment */
1.2       itojun    243:                q6->ip6q_down   = q6->ip6q_up = (struct ip6asfrag *)q6;
                    244: #ifdef notyet
                    245:                q6->ip6q_nxtp   = (u_char *)nxtp;
                    246: #endif
                    247:                q6->ip6q_ident  = ip6f->ip6f_ident;
                    248:                q6->ip6q_arrive = 0; /* Is it used anywhere? */
                    249:                q6->ip6q_ttl    = IPV6_FRAGTTL;
                    250:                q6->ip6q_src    = ip6->ip6_src;
                    251:                q6->ip6q_dst    = ip6->ip6_dst;
                    252:                q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
                    253:        }
                    254:
                    255:        /*
                    256:         * If it's the 1st fragment, record the length of the
                    257:         * unfragmentable part and the next header of the fragment header.
                    258:         */
                    259:        fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
                    260:        if (fragoff == 0) {
                    261:                q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr)
                    262:                        - sizeof(struct ip6_frag);
                    263:                q6->ip6q_nxt = ip6f->ip6f_nxt;
                    264:        }
                    265:
                    266:        /*
                    267:         * Check that the reassembled packet would not exceed 65535 bytes
                    268:         * in size.
                    269:         * If it would exceed, discard the fragment and return an ICMP error.
                    270:         */
1.9       itojun    271:        frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
1.2       itojun    272:        if (q6->ip6q_unfrglen >= 0) {
                    273:                /* The 1st fragment has already arrived. */
                    274:                if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
                    275:                        icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
1.9       itojun    276:                                    offset - sizeof(struct ip6_frag) +
                    277:                                        offsetof(struct ip6_frag, ip6f_offlg));
1.10.4.2! msaitoh   278:                        frag6_doing_reass = 0;
1.2       itojun    279:                        return(IPPROTO_DONE);
                    280:                }
                    281:        }
                    282:        else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
                    283:                icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
1.9       itojun    284:                            offset - sizeof(struct ip6_frag) +
                    285:                                offsetof(struct ip6_frag, ip6f_offlg));
1.10.4.2! msaitoh   286:                frag6_doing_reass = 0;
1.2       itojun    287:                return(IPPROTO_DONE);
                    288:        }
                    289:        /*
                    290:         * If it's the first fragment, do the above check for each
                    291:         * fragment already stored in the reassembly queue.
                    292:         */
                    293:        if (fragoff == 0) {
                    294:                for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
                    295:                     af6 = af6dwn) {
                    296:                        af6dwn = af6->ip6af_down;
                    297:
                    298:                        if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
                    299:                            IPV6_MAXPACKET) {
                    300:                                struct mbuf *merr = IP6_REASS_MBUF(af6);
                    301:                                struct ip6_hdr *ip6err;
                    302:                                int erroff = af6->ip6af_offset;
                    303:
                    304:                                /* dequeue the fragment. */
                    305:                                frag6_deq(af6);
1.9       itojun    306:                                free(af6, M_FTABLE);
1.2       itojun    307:
                    308:                                /* adjust pointer. */
                    309:                                ip6err = mtod(merr, struct ip6_hdr *);
                    310:
                    311:                                /*
                    312:                                 * Restore source and destination addresses
                    313:                                 * in the erroneous IPv6 header.
                    314:                                 */
                    315:                                ip6err->ip6_src = q6->ip6q_src;
                    316:                                ip6err->ip6_dst = q6->ip6q_dst;
                    317:
                    318:                                icmp6_error(merr, ICMP6_PARAM_PROB,
                    319:                                            ICMP6_PARAMPROB_HEADER,
1.9       itojun    320:                                            erroff - sizeof(struct ip6_frag) +
                    321:                                                offsetof(struct ip6_frag, ip6f_offlg));
1.2       itojun    322:                        }
                    323:                }
                    324:        }
                    325:
1.9       itojun    326:        ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
                    327:            M_DONTWAIT);
                    328:        if (ip6af == NULL)
                    329:                goto dropfrag;
                    330:        bzero(ip6af, sizeof(*ip6af));
                    331:        ip6af->ip6af_head = ip6->ip6_flow;
                    332:        ip6af->ip6af_len = ip6->ip6_plen;
                    333:        ip6af->ip6af_nxt = ip6->ip6_nxt;
                    334:        ip6af->ip6af_hlim = ip6->ip6_hlim;
1.2       itojun    335:        ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
                    336:        ip6af->ip6af_off = fragoff;
                    337:        ip6af->ip6af_frglen = frgpartlen;
                    338:        ip6af->ip6af_offset = offset;
                    339:        IP6_REASS_MBUF(ip6af) = m;
                    340:
                    341:        if (first_frag) {
                    342:                af6 = (struct ip6asfrag *)q6;
                    343:                goto insert;
                    344:        }
                    345:
                    346:        /*
                    347:         * Find a segment which begins after this one does.
                    348:         */
                    349:        for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
                    350:             af6 = af6->ip6af_down)
                    351:                if (af6->ip6af_off > ip6af->ip6af_off)
                    352:                        break;
                    353:
                    354: #if 0
                    355:        /*
                    356:         * If there is a preceding segment, it may provide some of
                    357:         * our data already.  If so, drop the data from the incoming
                    358:         * segment.  If it provides all of our data, drop us.
                    359:         */
                    360:        if (af6->ip6af_up != (struct ip6asfrag *)q6) {
                    361:                i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
                    362:                        - ip6af->ip6af_off;
                    363:                if (i > 0) {
                    364:                        if (i >= ip6af->ip6af_frglen)
                    365:                                goto dropfrag;
                    366:                        m_adj(IP6_REASS_MBUF(ip6af), i);
                    367:                        ip6af->ip6af_off += i;
                    368:                        ip6af->ip6af_frglen -= i;
                    369:                }
                    370:        }
                    371:
                    372:        /*
                    373:         * While we overlap succeeding segments trim them or,
                    374:         * if they are completely covered, dequeue them.
                    375:         */
                    376:        while (af6 != (struct ip6asfrag *)q6 &&
                    377:               ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
                    378:                i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
                    379:                if (i < af6->ip6af_frglen) {
                    380:                        af6->ip6af_frglen -= i;
                    381:                        af6->ip6af_off += i;
                    382:                        m_adj(IP6_REASS_MBUF(af6), i);
                    383:                        break;
                    384:                }
                    385:                af6 = af6->ip6af_down;
                    386:                m_freem(IP6_REASS_MBUF(af6->ip6af_up));
                    387:                frag6_deq(af6->ip6af_up);
                    388:        }
                    389: #else
                    390:        /*
                    391:         * If the incoming framgent overlaps some existing fragments in
                    392:         * the reassembly queue, drop it, since it is dangerous to override
                    393:         * existing fragments from a security point of view.
                    394:         */
                    395:        if (af6->ip6af_up != (struct ip6asfrag *)q6) {
                    396:                i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
                    397:                        - ip6af->ip6af_off;
                    398:                if (i > 0) {
1.10.4.1  he        399: #if 0                          /* suppress the noisy log */
1.2       itojun    400:                        log(LOG_ERR, "%d bytes of a fragment from %s "
                    401:                            "overlaps the previous fragment\n",
                    402:                            i, ip6_sprintf(&q6->ip6q_src));
1.10.4.1  he        403: #endif
                    404:                        free(ip6af, M_FTABLE);
1.2       itojun    405:                        goto dropfrag;
                    406:                }
                    407:        }
                    408:        if (af6 != (struct ip6asfrag *)q6) {
                    409:                i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
                    410:                if (i > 0) {
1.10.4.1  he        411: #if 0                          /* suppress the noisy log */
1.2       itojun    412:                        log(LOG_ERR, "%d bytes of a fragment from %s "
                    413:                            "overlaps the succeeding fragment",
                    414:                            i, ip6_sprintf(&q6->ip6q_src));
1.10.4.1  he        415: #endif
                    416:                        free(ip6af, M_FTABLE);
1.2       itojun    417:                        goto dropfrag;
                    418:                }
                    419:        }
                    420: #endif
                    421:
                    422: insert:
                    423:
                    424:        /*
                    425:         * Stick new segment in its place;
                    426:         * check for complete reassembly.
                    427:         * Move to front of packet queue, as we are
                    428:         * the most recently active fragmented packet.
                    429:         */
                    430:        frag6_enq(ip6af, af6->ip6af_up);
                    431: #if 0 /* xxx */
                    432:        if (q6 != ip6q.ip6q_next) {
                    433:                frag6_remque(q6);
                    434:                frag6_insque(q6, &ip6q);
                    435:        }
                    436: #endif
                    437:        next = 0;
                    438:        for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
                    439:             af6 = af6->ip6af_down) {
                    440:                if (af6->ip6af_off != next) {
                    441:                        frag6_doing_reass = 0;
                    442:                        return IPPROTO_DONE;
                    443:                }
                    444:                next += af6->ip6af_frglen;
                    445:        }
                    446:        if (af6->ip6af_up->ip6af_mff) {
                    447:                frag6_doing_reass = 0;
                    448:                return IPPROTO_DONE;
                    449:        }
                    450:
                    451:        /*
                    452:         * Reassembly is complete; concatenate fragments.
                    453:         */
                    454:        ip6af = q6->ip6q_down;
                    455:        t = m = IP6_REASS_MBUF(ip6af);
                    456:        af6 = ip6af->ip6af_down;
1.9       itojun    457:        frag6_deq(ip6af);
1.2       itojun    458:        while (af6 != (struct ip6asfrag *)q6) {
1.9       itojun    459:                af6dwn = af6->ip6af_down;
                    460:                frag6_deq(af6);
1.2       itojun    461:                while (t->m_next)
                    462:                        t = t->m_next;
                    463:                t->m_next = IP6_REASS_MBUF(af6);
1.9       itojun    464:                m_adj(t->m_next, af6->ip6af_offset);
                    465:                free(af6, M_FTABLE);
                    466:                af6 = af6dwn;
1.2       itojun    467:        }
                    468:
                    469:        /* adjust offset to point where the original next header starts */
                    470:        offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
1.9       itojun    471:        free(ip6af, M_FTABLE);
                    472:        ip6 = mtod(m, struct ip6_hdr *);
1.2       itojun    473:        ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
                    474:        ip6->ip6_src = q6->ip6q_src;
                    475:        ip6->ip6_dst = q6->ip6q_dst;
                    476:        nxt = q6->ip6q_nxt;
                    477: #ifdef notyet
                    478:        *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
                    479: #endif
                    480:
                    481:        /*
                    482:         * Delete frag6 header with as a few cost as possible.
                    483:         */
1.9       itojun    484:        if (offset < m->m_len) {
1.7       itojun    485:                ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
1.2       itojun    486:                        offset);
1.9       itojun    487:                m->m_data += sizeof(struct ip6_frag);
                    488:                m->m_len -= sizeof(struct ip6_frag);
                    489:        } else {
                    490:                /* this comes with no copy if the boundary is on cluster */
                    491:                if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
                    492:                        frag6_remque(q6);
                    493:                        free(q6, M_FTABLE);
                    494:                        frag6_nfragpackets--;
                    495:                        goto dropfrag;
                    496:                }
                    497:                m_adj(t, sizeof(struct ip6_frag));
                    498:                m_cat(m, t);
1.2       itojun    499:        }
                    500:
                    501:        /*
                    502:         * Store NXT to the original.
                    503:         */
                    504:        {
                    505:                char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
                    506:                *prvnxtp = nxt;
                    507:        }
                    508:
                    509:        frag6_remque(q6);
                    510:        free(q6, M_FTABLE);
                    511:        frag6_nfragpackets--;
                    512:
                    513:        if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
                    514:                int plen = 0;
                    515:                for (t = m; t; t = t->m_next)
                    516:                        plen += t->m_len;
                    517:                m->m_pkthdr.len = plen;
                    518:        }
                    519:
                    520:        ip6stat.ip6s_reassembled++;
1.7       itojun    521:        in6_ifstat_inc(dstifp, ifs6_reass_ok);
1.2       itojun    522:
                    523:        /*
                    524:         * Tell launch routine the next header
                    525:         */
                    526:
                    527:        *mp = m;
                    528:        *offp = offset;
                    529:
                    530:        frag6_doing_reass = 0;
                    531:        return nxt;
                    532:
                    533:  dropfrag:
1.7       itojun    534:        in6_ifstat_inc(dstifp, ifs6_reass_fail);
1.2       itojun    535:        ip6stat.ip6s_fragdropped++;
                    536:        m_freem(m);
1.10.4.2! msaitoh   537:        frag6_doing_reass = 0;
1.2       itojun    538:        return IPPROTO_DONE;
                    539: }
                    540:
                    541: /*
                    542:  * Free a fragment reassembly header and all
                    543:  * associated datagrams.
                    544:  */
                    545: void
                    546: frag6_freef(q6)
                    547:        struct ip6q *q6;
                    548: {
                    549:        struct ip6asfrag *af6, *down6;
                    550:
                    551:        for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
                    552:             af6 = down6) {
                    553:                struct mbuf *m = IP6_REASS_MBUF(af6);
                    554:
                    555:                down6 = af6->ip6af_down;
                    556:                frag6_deq(af6);
                    557:
                    558:                /*
                    559:                 * Return ICMP time exceeded error for the 1st fragment.
                    560:                 * Just free other fragments.
                    561:                 */
                    562:                if (af6->ip6af_off == 0) {
                    563:                        struct ip6_hdr *ip6;
                    564:
                    565:                        /* adjust pointer */
                    566:                        ip6 = mtod(m, struct ip6_hdr *);
                    567:
                    568:                        /* restoure source and destination addresses */
                    569:                        ip6->ip6_src = q6->ip6q_src;
                    570:                        ip6->ip6_dst = q6->ip6q_dst;
                    571:
                    572:                        icmp6_error(m, ICMP6_TIME_EXCEEDED,
                    573:                                    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
1.9       itojun    574:                } else
1.2       itojun    575:                        m_freem(m);
1.9       itojun    576:                free(af6, M_FTABLE);
1.2       itojun    577:        }
                    578:        frag6_remque(q6);
                    579:        free(q6, M_FTABLE);
                    580:        frag6_nfragpackets--;
                    581: }
                    582:
                    583: /*
                    584:  * Put an ip fragment on a reassembly chain.
                    585:  * Like insque, but pointers in middle of structure.
                    586:  */
                    587: void
                    588: frag6_enq(af6, up6)
                    589:        struct ip6asfrag *af6, *up6;
                    590: {
                    591:        af6->ip6af_up = up6;
                    592:        af6->ip6af_down = up6->ip6af_down;
                    593:        up6->ip6af_down->ip6af_up = af6;
                    594:        up6->ip6af_down = af6;
                    595: }
                    596:
                    597: /*
                    598:  * To frag6_enq as remque is to insque.
                    599:  */
                    600: void
                    601: frag6_deq(af6)
                    602:        struct ip6asfrag *af6;
                    603: {
                    604:        af6->ip6af_up->ip6af_down = af6->ip6af_down;
                    605:        af6->ip6af_down->ip6af_up = af6->ip6af_up;
                    606: }
                    607:
                    608: void
                    609: frag6_insque(new, old)
                    610:        struct ip6q *new, *old;
                    611: {
                    612:        new->ip6q_prev = old;
                    613:        new->ip6q_next = old->ip6q_next;
                    614:        old->ip6q_next->ip6q_prev= new;
                    615:        old->ip6q_next = new;
                    616: }
                    617:
                    618: void
                    619: frag6_remque(p6)
                    620:        struct ip6q *p6;
                    621: {
                    622:        p6->ip6q_prev->ip6q_next = p6->ip6q_next;
                    623:        p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
                    624: }
                    625:
                    626: /*
                    627:  * IP timer processing;
                    628:  * if a timer expires on a reassembly
                    629:  * queue, discard it.
                    630:  */
                    631: void
                    632: frag6_slowtimo()
                    633: {
                    634:        struct ip6q *q6;
1.4       itojun    635:        int s = splsoftnet();
1.2       itojun    636: #if 0
                    637:        extern struct   route_in6 ip6_forward_rt;
                    638: #endif
                    639:
                    640:        frag6_doing_reass = 1;
                    641:        q6 = ip6q.ip6q_next;
                    642:        if (q6)
                    643:                while (q6 != &ip6q) {
                    644:                        --q6->ip6q_ttl;
                    645:                        q6 = q6->ip6q_next;
                    646:                        if (q6->ip6q_prev->ip6q_ttl == 0) {
                    647:                                ip6stat.ip6s_fragtimeout++;
1.7       itojun    648:                                /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
1.2       itojun    649:                                frag6_freef(q6->ip6q_prev);
                    650:                        }
                    651:                }
                    652:        /*
                    653:         * If we are over the maximum number of fragments
                    654:         * (due to the limit being lowered), drain off
                    655:         * enough to get down to the new limit.
                    656:         */
1.10.4.2! msaitoh   657:        while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
        !           658:            ip6q.ip6q_prev) {
1.2       itojun    659:                ip6stat.ip6s_fragoverflow++;
1.7       itojun    660:                /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
1.2       itojun    661:                frag6_freef(ip6q.ip6q_prev);
                    662:        }
                    663:        frag6_doing_reass = 0;
                    664:
                    665: #if 0
                    666:        /*
                    667:         * Routing changes might produce a better route than we last used;
                    668:         * make sure we notice eventually, even if forwarding only for one
                    669:         * destination and the cache is never replaced.
                    670:         */
                    671:        if (ip6_forward_rt.ro_rt) {
                    672:                RTFREE(ip6_forward_rt.ro_rt);
                    673:                ip6_forward_rt.ro_rt = 0;
                    674:        }
                    675:        if (ipsrcchk_rt.ro_rt) {
                    676:                RTFREE(ipsrcchk_rt.ro_rt);
                    677:                ipsrcchk_rt.ro_rt = 0;
                    678:        }
                    679: #endif
                    680:
                    681:        splx(s);
                    682: }
                    683:
                    684: /*
                    685:  * Drain off all datagram fragments.
                    686:  */
                    687: void
                    688: frag6_drain()
                    689: {
                    690:        if (frag6_doing_reass)
                    691:                return;
                    692:        while (ip6q.ip6q_next != &ip6q) {
                    693:                ip6stat.ip6s_fragdropped++;
1.7       itojun    694:                /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
1.2       itojun    695:                frag6_freef(ip6q.ip6q_next);
                    696:        }
                    697: }

CVSweb <webmaster@jp.NetBSD.org>