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

Annotation of src/sys/netinet6/ip6_input.c, Revision 1.14

1.14    ! itojun      1: /*     $NetBSD: ip6_input.c,v 1.13 2000/01/31 10:33:22 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: /*
                     33:  * Copyright (c) 1982, 1986, 1988, 1993
                     34:  *     The Regents of the University of California.  All rights reserved.
                     35:  *
                     36:  * Redistribution and use in source and binary forms, with or without
                     37:  * modification, are permitted provided that the following conditions
                     38:  * are met:
                     39:  * 1. Redistributions of source code must retain the above copyright
                     40:  *    notice, this list of conditions and the following disclaimer.
                     41:  * 2. Redistributions in binary form must reproduce the above copyright
                     42:  *    notice, this list of conditions and the following disclaimer in the
                     43:  *    documentation and/or other materials provided with the distribution.
                     44:  * 3. All advertising materials mentioning features or use of this software
                     45:  *    must display the following acknowledgement:
                     46:  *     This product includes software developed by the University of
                     47:  *     California, Berkeley and its contributors.
                     48:  * 4. Neither the name of the University nor the names of its contributors
                     49:  *    may be used to endorse or promote products derived from this software
                     50:  *    without specific prior written permission.
                     51:  *
                     52:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     53:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     54:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     55:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     56:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     57:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     58:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     59:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     60:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     61:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     62:  * SUCH DAMAGE.
                     63:  *
                     64:  *     @(#)ip_input.c  8.2 (Berkeley) 1/4/94
                     65:  */
                     66:
                     67: #include "opt_inet.h"
1.4       thorpej    68: #include "opt_ipsec.h"
1.2       itojun     69:
                     70: #include <sys/param.h>
                     71: #include <sys/systm.h>
                     72: #include <sys/malloc.h>
                     73: #include <sys/mbuf.h>
                     74: #include <sys/domain.h>
                     75: #include <sys/protosw.h>
                     76: #include <sys/socket.h>
                     77: #include <sys/socketvar.h>
                     78: #include <sys/errno.h>
                     79: #include <sys/time.h>
                     80: #include <sys/kernel.h>
                     81: #include <sys/syslog.h>
                     82: #include <sys/proc.h>
                     83:
                     84: #include <net/if.h>
                     85: #include <net/if_types.h>
                     86: #include <net/if_dl.h>
                     87: #include <net/route.h>
                     88: #include <net/netisr.h>
                     89:
                     90: #include <netinet/in.h>
1.9       itojun     91: #include <netinet/in_systm.h>
                     92: #ifdef INET
                     93: #include <netinet/ip.h>
                     94: #include <netinet/ip_icmp.h>
                     95: #endif /*INET*/
1.14    ! itojun     96: #include <netinet/ip6.h>
1.2       itojun     97: #include <netinet6/in6_var.h>
1.11      itojun     98: #include <netinet6/ip6_var.h>
1.2       itojun     99: #include <netinet6/in6_pcb.h>
1.14    ! itojun    100: #include <netinet/icmp6.h>
1.2       itojun    101: #include <netinet6/in6_ifattach.h>
                    102: #include <netinet6/nd6.h>
1.9       itojun    103: #include <netinet6/in6_prefix.h>
1.2       itojun    104:
1.9       itojun    105: #ifdef IPV6FIREWALL
                    106: #include <netinet6/ip6_fw.h>
                    107: #endif
1.2       itojun    108:
                    109: #include <netinet6/ip6protosw.h>
                    110:
                    111: /* we need it for NLOOP. */
                    112: #include "loop.h"
                    113: #include "faith.h"
                    114:
                    115: #include "gif.h"
                    116: #include "bpfilter.h"
                    117:
1.9       itojun    118: #include <net/net_osdep.h>
                    119:
1.2       itojun    120: extern struct domain inet6domain;
                    121: extern struct ip6protosw inet6sw[];
                    122:
                    123: u_char ip6_protox[IPPROTO_MAX];
                    124: static int ip6qmaxlen = IFQ_MAXLEN;
                    125: struct in6_ifaddr *in6_ifaddr;
                    126: struct ifqueue ip6intrq;
                    127:
                    128: extern struct ifnet loif[NLOOP];
                    129: int ip6_forward_srcrt;                 /* XXX */
                    130: int ip6_sourcecheck;                   /* XXX */
                    131: int ip6_sourcecheck_interval;          /* XXX */
1.9       itojun    132:
                    133: #ifdef IPV6FIREWALL
                    134: /* firewall hooks */
                    135: ip6_fw_chk_t *ip6_fw_chk_ptr;
                    136: ip6_fw_ctl_t *ip6_fw_ctl_ptr;
1.2       itojun    137: #endif
                    138:
                    139: struct ip6stat ip6stat;
                    140:
                    141: static void ip6_init2 __P((void *));
                    142:
1.5       itojun    143: static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
1.2       itojun    144:
1.12      itojun    145: #ifdef PTR
1.9       itojun    146: extern int             ip6_protocol_tr;
                    147:
                    148: int    ptr_in6         __P((struct mbuf *, struct mbuf **));
                    149: extern void ip_forward __P((struct mbuf *, int));
                    150: #endif
                    151:
1.2       itojun    152: /*
                    153:  * IP6 initialization: fill in IP6 protocol switch table.
                    154:  * All protocols not implemented in kernel go to raw IP6 protocol handler.
                    155:  */
                    156: void
                    157: ip6_init()
                    158: {
                    159:        register struct ip6protosw *pr;
                    160:        register int i;
                    161:        struct timeval tv;
                    162:
                    163:        pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
                    164:        if (pr == 0)
                    165:                panic("ip6_init");
                    166:        for (i = 0; i < IPPROTO_MAX; i++)
                    167:                ip6_protox[i] = pr - inet6sw;
                    168:        for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
                    169:            pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
                    170:                if (pr->pr_domain->dom_family == PF_INET6 &&
                    171:                    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
                    172:                        ip6_protox[pr->pr_protocol] = pr - inet6sw;
                    173:        ip6intrq.ifq_maxlen = ip6qmaxlen;
                    174:        nd6_init();
                    175:        frag6_init();
1.9       itojun    176: #ifdef IPV6FIREWALL
                    177:        ip6_fw_init();
                    178: #endif
1.2       itojun    179:        /*
                    180:         * in many cases, random() here does NOT return random number
                    181:         * as initialization during bootstrap time occur in fixed order.
                    182:         */
                    183:        microtime(&tv);
                    184:        ip6_flow_seq = random() ^ tv.tv_usec;
                    185:
                    186:        ip6_init2((void *)0);
                    187: }
                    188:
                    189: static void
                    190: ip6_init2(dummy)
                    191:        void *dummy;
                    192: {
1.9       itojun    193:        int ret;
                    194:
                    195:        /* get EUI64 from somewhere */
                    196:        ret = in6_ifattach_getifid(NULL);
1.2       itojun    197:
                    198:        /*
                    199:         * to route local address of p2p link to loopback,
                    200:         * assign loopback address first.
                    201:         */
1.9       itojun    202:        in6_ifattach(&loif[0], IN6_IFT_LOOP, NULL, 0);
1.2       itojun    203:
                    204:        /* nd6_timer_init */
                    205:        timeout(nd6_timer, (caddr_t)0, hz);
1.9       itojun    206:        /* router renumbering prefix list maintenance */
                    207:        timeout(in6_rr_timer, (caddr_t)0, hz);
1.2       itojun    208: }
                    209:
                    210: /*
                    211:  * IP6 input interrupt handling. Just pass the packet to ip6_input.
                    212:  */
                    213: void
                    214: ip6intr()
                    215: {
                    216:        int s;
                    217:        struct mbuf *m;
                    218:
                    219:        for (;;) {
                    220:                s = splimp();
                    221:                IF_DEQUEUE(&ip6intrq, m);
                    222:                splx(s);
                    223:                if (m == 0)
                    224:                        return;
                    225:                ip6_input(m);
                    226:        }
                    227: }
                    228:
                    229: extern struct  route_in6 ip6_forward_rt;
                    230:
                    231: void
                    232: ip6_input(m)
                    233:        struct mbuf *m;
                    234: {
1.9       itojun    235:        struct ip6_hdr *ip6;
1.2       itojun    236:        int off = sizeof(struct ip6_hdr), nest;
                    237:        u_int32_t plen;
1.5       itojun    238:        u_int32_t rtalert = ~0;
1.2       itojun    239:        int nxt, ours = 0;
1.9       itojun    240:        struct ifnet *deliverifp = NULL;
1.2       itojun    241:
                    242: #ifdef IPSEC
                    243:        /*
                    244:         * should the inner packet be considered authentic?
                    245:         * see comment in ah4_input().
                    246:         */
                    247:        if (m) {
                    248:                m->m_flags &= ~M_AUTHIPHDR;
                    249:                m->m_flags &= ~M_AUTHIPDGM;
                    250:        }
                    251: #endif
1.9       itojun    252:
1.2       itojun    253:        /*
                    254:         * mbuf statistics by kazu
                    255:         */
                    256:        if (m->m_flags & M_EXT) {
                    257:                if (m->m_next)
                    258:                        ip6stat.ip6s_mext2m++;
                    259:                else
                    260:                        ip6stat.ip6s_mext1++;
                    261:        } else {
                    262:                if (m->m_next) {
1.9       itojun    263:                        if (m->m_flags & M_LOOP) {
1.2       itojun    264:                                ip6stat.ip6s_m2m[loif[0].if_index]++;   /*XXX*/
1.9       itojun    265:                        }
1.2       itojun    266:                        else if (m->m_pkthdr.rcvif->if_index <= 31)
                    267:                                ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
                    268:                        else
                    269:                                ip6stat.ip6s_m2m[0]++;
                    270:                } else
                    271:                        ip6stat.ip6s_m1++;
                    272:        }
                    273:
1.9       itojun    274:        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
                    275:        ip6stat.ip6s_total++;
                    276:
                    277: #ifndef PULLDOWN_TEST
                    278:        /* XXX is the line really necessary? */
1.2       itojun    279:        IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
1.9       itojun    280: #endif
1.2       itojun    281:
1.9       itojun    282:        if (m->m_len < sizeof(struct ip6_hdr)) {
                    283:                struct ifnet *inifp;
                    284:                inifp = m->m_pkthdr.rcvif;
                    285:                if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
                    286:                        ip6stat.ip6s_toosmall++;
                    287:                        in6_ifstat_inc(inifp, ifs6_in_hdrerr);
                    288:                        return;
                    289:                }
1.2       itojun    290:        }
                    291:
                    292:        ip6 = mtod(m, struct ip6_hdr *);
                    293:
                    294:        if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
                    295:                ip6stat.ip6s_badvers++;
1.9       itojun    296:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
1.2       itojun    297:                goto bad;
                    298:        }
                    299:
                    300:        ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
                    301:
1.9       itojun    302: #ifdef IPV6FIREWALL
                    303:        /*
                    304:         * Check with the firewall...
                    305:         */
                    306:        if (ip6_fw_chk_ptr) {
                    307:                u_short port = 0;
                    308:                /* If ipfw says divert, we have to just drop packet */
                    309:                /* use port as a dummy argument */
                    310:                if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
                    311:                        m_freem(m);
                    312:                        m = NULL;
                    313:                }
                    314:                if (!m)
                    315:                        return;
                    316:        }
                    317: #endif
                    318:
1.2       itojun    319:        /*
                    320:         * Scope check
                    321:         */
                    322:        if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
                    323:            IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
                    324:                ip6stat.ip6s_badscope++;
1.9       itojun    325:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1.2       itojun    326:                goto bad;
                    327:        }
1.13      itojun    328:        /*
                    329:         * The following check is not documented in the spec.  Malicious party
                    330:         * may be able to use IPv4 mapped addr to confuse tcp/udp stack and
                    331:         * bypass security checks (act as if it was from 127.0.0.1 by using
                    332:         * IPv6 src ::ffff:127.0.0.1).  Be cautious.
                    333:         */
                    334:        if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
                    335:            IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
                    336:                ip6stat.ip6s_badscope++;
                    337:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
                    338:                goto bad;
                    339:        }
                    340: #if 1
                    341:        /*
                    342:         * We don't support it, so it is strange to get this.
                    343:         */
                    344:        if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
                    345:            IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
                    346:                ip6stat.ip6s_badscope++;
                    347:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
                    348:                goto bad;
                    349:        }
                    350: #endif
1.2       itojun    351:        if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
                    352:            IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
                    353:                if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
                    354:                        ours = 1;
1.9       itojun    355:                        deliverifp = m->m_pkthdr.rcvif;
1.2       itojun    356:                        goto hbhcheck;
                    357:                } else {
                    358:                        ip6stat.ip6s_badscope++;
1.9       itojun    359:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1.2       itojun    360:                        goto bad;
                    361:                }
                    362:        }
                    363:
                    364:        if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
                    365:                if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
                    366:                        ours = 1;
1.9       itojun    367:                        deliverifp = m->m_pkthdr.rcvif;
1.2       itojun    368:                        goto hbhcheck;
                    369:                }
                    370:        } else {
                    371:                if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
                    372:                        ip6->ip6_src.s6_addr16[1]
                    373:                                = htons(m->m_pkthdr.rcvif->if_index);
                    374:                if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
                    375:                        ip6->ip6_dst.s6_addr16[1]
                    376:                                = htons(m->m_pkthdr.rcvif->if_index);
                    377:        }
                    378:
1.12      itojun    379: #ifdef PTR
1.9       itojun    380:        /*
                    381:         *
                    382:         */
                    383:        if (ip6_protocol_tr)
                    384:        {
                    385:            struct mbuf *m1 = NULL;
                    386:
                    387:            switch (ptr_in6(m, &m1))
                    388:            {
                    389:              case IPPROTO_IP:                                  goto mcastcheck;
                    390:              case IPPROTO_IPV4:        ip_forward(m1, 0);      break;
                    391:              case IPPROTO_IPV6:        ip6_forward(m1, 0);     break;
                    392:              case IPPROTO_MAX:                 /* discard this packet  */
                    393:              default:
                    394:            }
                    395:
                    396:            if (m != m1)
                    397:                m_freem(m);
                    398:
                    399:            return;
                    400:        }
                    401:
                    402:   mcastcheck:
                    403: #endif
                    404:
1.2       itojun    405:        /*
                    406:         * Multicast check
                    407:         */
                    408:        if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
                    409:                struct  in6_multi *in6m = 0;
1.9       itojun    410:
                    411:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
1.2       itojun    412:                /*
                    413:                 * See if we belong to the destination multicast group on the
                    414:                 * arrival interface.
                    415:                 */
                    416:                IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
                    417:                if (in6m)
                    418:                        ours = 1;
                    419:                else if (!ip6_mrouter) {
                    420:                        ip6stat.ip6s_notmember++;
                    421:                        ip6stat.ip6s_cantforward++;
1.9       itojun    422:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2       itojun    423:                        goto bad;
                    424:                }
1.9       itojun    425:                deliverifp = m->m_pkthdr.rcvif;
1.2       itojun    426:                goto hbhcheck;
                    427:        }
                    428:
                    429:        /*
                    430:         *  Unicast check
                    431:         */
                    432:        if (ip6_forward_rt.ro_rt == 0 ||
                    433:            !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
                    434:                                &ip6_forward_rt.ro_dst.sin6_addr)) {
                    435:                if (ip6_forward_rt.ro_rt) {
                    436:                        RTFREE(ip6_forward_rt.ro_rt);
                    437:                        ip6_forward_rt.ro_rt = 0;
                    438:                }
                    439:                bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
                    440:                ip6_forward_rt.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
                    441:                ip6_forward_rt.ro_dst.sin6_family = AF_INET6;
                    442:                ip6_forward_rt.ro_dst.sin6_addr = ip6->ip6_dst;
                    443:
1.9       itojun    444:                rtalloc((struct route *)&ip6_forward_rt);
1.2       itojun    445:        }
                    446:
                    447: #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
                    448:
                    449:        /*
                    450:         * Accept the packet if the forwarding interface to the destination
                    451:         * according to the routing table is the loopback interface,
                    452:         * unless the associated route has a gateway.
                    453:         * Note that this approach causes to accept a packet if there is a
                    454:         * route to the loopback interface for the destination of the packet.
                    455:         * But we think it's even useful in some situations, e.g. when using
                    456:         * a special daemon which wants to intercept the packet.
                    457:         */
                    458:        if (ip6_forward_rt.ro_rt &&
                    459:            (ip6_forward_rt.ro_rt->rt_flags &
                    460:             (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
                    461: #if 0
                    462:            /*
                    463:             * The check below is redundant since the comparison of
                    464:             * the destination and the key of the rtentry has
                    465:             * already done through looking up the routing table.
                    466:             */
                    467:            IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
                    468:                               &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) &&
                    469: #endif
1.9       itojun    470:            ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
1.2       itojun    471:                struct in6_ifaddr *ia6 =
                    472:                        (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
                    473:                /* packet to tentative address must not be received */
                    474:                if (ia6->ia6_flags & IN6_IFF_ANYCAST)
                    475:                        m->m_flags |= M_ANYCAST6;
                    476:                if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
                    477:                        /* this interface is ready */
                    478:                        ours = 1;
1.9       itojun    479:                        deliverifp = ia6->ia_ifp;       /* correct? */
1.2       itojun    480:                        goto hbhcheck;
                    481:                } else {
                    482:                        /* this interface is not ready, fall through */
                    483:                }
                    484:        }
                    485:
                    486:        /*
                    487:         * FAITH(Firewall Aided Internet Translator)
                    488:         */
                    489: #if defined(NFAITH) && 0 < NFAITH
                    490:        if (ip6_keepfaith) {
                    491:                if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
                    492:                 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
                    493:                        /* XXX do we need more sanity checks? */
                    494:                        ours = 1;
1.9       itojun    495:                        deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /*faith*/
                    496:                        goto hbhcheck;
                    497:                }
                    498:        }
                    499: #endif
                    500:
                    501: #if 0
                    502:     {
                    503:        /*
                    504:         * Last resort: check in6_ifaddr for incoming interface.
                    505:         * The code is here until I update the "goto ours hack" code above
                    506:         * working right.
                    507:         */
                    508:        struct ifaddr *ifa;
                    509:        for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
                    510:             ifa;
                    511:             ifa = ifa->ifa_list.tqe_next) {
                    512:                if (ifa->ifa_addr == NULL)
                    513:                        continue;       /* just for safety */
                    514:                if (ifa->ifa_addr->sa_family != AF_INET6)
                    515:                        continue;
                    516:                if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
                    517:                        ours = 1;
                    518:                        deliverifp = ifa->ifa_ifp;
1.2       itojun    519:                        goto hbhcheck;
                    520:                }
                    521:        }
1.9       itojun    522:     }
1.2       itojun    523: #endif
                    524:
                    525:        /*
                    526:         * Now there is no reason to process the packet if it's not our own
                    527:         * and we're not a router.
                    528:         */
                    529:        if (!ip6_forwarding) {
                    530:                ip6stat.ip6s_cantforward++;
1.9       itojun    531:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2       itojun    532:                goto bad;
                    533:        }
                    534:
                    535:   hbhcheck:
                    536:        /*
                    537:         * Process Hop-by-Hop options header if it's contained.
                    538:         * m may be modified in ip6_hopopts_input().
                    539:         * If a JumboPayload option is included, plen will also be modified.
                    540:         */
                    541:        plen = (u_int32_t)ntohs(ip6->ip6_plen);
                    542:        if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9       itojun    543:                struct ip6_hbh *hbh;
                    544:
                    545:                if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
                    546: #if 0  /*touches NULL pointer*/
                    547:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
                    548: #endif
1.2       itojun    549:                        return; /* m have already been freed */
1.9       itojun    550:                }
1.2       itojun    551:                /* adjust pointer */
                    552:                ip6 = mtod(m, struct ip6_hdr *);
1.9       itojun    553: #ifndef PULLDOWN_TEST
                    554:                /* ip6_hopopts_input() ensures that mbuf is contiguous */
                    555:                hbh = (struct ip6_hbh *)(ip6 + 1);
                    556: #else
                    557:                IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
                    558:                        sizeof(struct ip6_hbh));
                    559:                if (hbh == NULL) {
                    560:                        ip6stat.ip6s_tooshort++;
                    561:                        return;
                    562:                }
                    563: #endif
                    564:                nxt = hbh->ip6h_nxt;
1.2       itojun    565:
                    566:                /*
                    567:                 * accept the packet if a router alert option is included
                    568:                 * and we act as an IPv6 router.
                    569:                 */
1.5       itojun    570:                if (rtalert != ~0 && ip6_forwarding)
1.2       itojun    571:                        ours = 1;
                    572:        } else
                    573:                nxt = ip6->ip6_nxt;
                    574:
                    575:        /*
                    576:         * Check that the amount of data in the buffers
                    577:         * is as at least much as the IPv6 header would have us expect.
                    578:         * Trim mbufs if longer than we expect.
                    579:         * Drop packet if shorter than we expect.
                    580:         */
                    581:        if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
                    582:                ip6stat.ip6s_tooshort++;
1.9       itojun    583:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.2       itojun    584:                goto bad;
                    585:        }
                    586:        if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
                    587:                if (m->m_len == m->m_pkthdr.len) {
                    588:                        m->m_len = sizeof(struct ip6_hdr) + plen;
                    589:                        m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
                    590:                } else
                    591:                        m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
                    592:        }
                    593:
                    594:        /*
                    595:         * Forward if desirable.
                    596:         */
                    597:        if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
                    598:                /*
                    599:                 * If we are acting as a multicast router, all
                    600:                 * incoming multicast packets are passed to the
                    601:                 * kernel-level multicast forwarding function.
                    602:                 * The packet is returned (relatively) intact; if
                    603:                 * ip6_mforward() returns a non-zero value, the packet
                    604:                 * must be discarded, else it may be accepted below.
                    605:                 */
                    606:                if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
                    607:                        ip6stat.ip6s_cantforward++;
                    608:                        m_freem(m);
                    609:                        return;
                    610:                }
                    611:                if (!ours) {
                    612:                        m_freem(m);
                    613:                        return;
                    614:                }
                    615:        }
                    616:        else if (!ours) {
                    617:                ip6_forward(m, 0);
                    618:                return;
                    619:        }
                    620:
                    621:        /*
                    622:         * Tell launch routine the next header
                    623:         */
1.12      itojun    624: #ifdef IFA_STATS
1.9       itojun    625:        if (IFA_STATS && deliverifp != NULL) {
                    626:                struct in6_ifaddr *ia6;
                    627:                ip6 = mtod(m, struct ip6_hdr *);
                    628:                ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
                    629:                if (ia6)
                    630:                        ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
                    631:        }
                    632: #endif
1.2       itojun    633:        ip6stat.ip6s_delivered++;
1.9       itojun    634:        in6_ifstat_inc(deliverifp, ifs6_in_deliver);
1.2       itojun    635:        nest = 0;
                    636:        while (nxt != IPPROTO_DONE) {
                    637:                if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
                    638:                        ip6stat.ip6s_toomanyhdr++;
                    639:                        goto bad;
                    640:                }
1.8       itojun    641:
                    642:                /*
                    643:                 * protection against faulty packet - there should be
                    644:                 * more sanity checks in header chain processing.
                    645:                 */
                    646:                if (m->m_pkthdr.len < off) {
                    647:                        ip6stat.ip6s_tooshort++;
1.9       itojun    648:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.8       itojun    649:                        goto bad;
                    650:                }
                    651:
1.2       itojun    652:                nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
                    653:        }
                    654:        return;
                    655:  bad:
                    656:        m_freem(m);
                    657: }
                    658:
                    659: /*
                    660:  * Hop-by-Hop options header processing. If a valid jumbo payload option is
                    661:  * included, the real payload length will be stored in plenp.
                    662:  */
                    663: static int
                    664: ip6_hopopts_input(plenp, rtalertp, mp, offp)
                    665:        u_int32_t *plenp;
1.5       itojun    666:        u_int32_t *rtalertp;    /* XXX: should be stored more smart way */
1.2       itojun    667:        struct mbuf **mp;
                    668:        int *offp;
                    669: {
                    670:        register struct mbuf *m = *mp;
                    671:        int off = *offp, hbhlen;
                    672:        struct ip6_hbh *hbh;
                    673:        u_int8_t *opt;
                    674:
                    675:        /* validation of the length of the header */
1.9       itojun    676: #ifndef PULLDOWN_TEST
1.2       itojun    677:        IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
                    678:        hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
                    679:        hbhlen = (hbh->ip6h_len + 1) << 3;
                    680:
                    681:        IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
                    682:        hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1.9       itojun    683: #else
                    684:        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                    685:                sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
                    686:        if (hbh == NULL) {
                    687:                ip6stat.ip6s_tooshort++;
                    688:                return -1;
                    689:        }
                    690:        hbhlen = (hbh->ip6h_len + 1) << 3;
                    691:        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
                    692:                hbhlen);
                    693:        if (hbh == NULL) {
                    694:                ip6stat.ip6s_tooshort++;
                    695:                return -1;
                    696:        }
                    697: #endif
1.2       itojun    698:        off += hbhlen;
                    699:        hbhlen -= sizeof(struct ip6_hbh);
                    700:        opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
                    701:
                    702:        if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
                    703:                                hbhlen, rtalertp, plenp) < 0)
                    704:                return(-1);
                    705:
                    706:        *offp = off;
                    707:        *mp = m;
                    708:        return(0);
                    709: }
                    710:
                    711: /*
                    712:  * Search header for all Hop-by-hop options and process each option.
                    713:  * This function is separate from ip6_hopopts_input() in order to
                    714:  * handle a case where the sending node itself process its hop-by-hop
                    715:  * options header. In such a case, the function is called from ip6_output().
                    716:  */
                    717: int
                    718: ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
                    719:        struct mbuf *m;
                    720:        u_int8_t *opthead;
                    721:        int hbhlen;
1.5       itojun    722:        u_int32_t *rtalertp;
1.2       itojun    723:        u_int32_t *plenp;
                    724: {
                    725:        struct ip6_hdr *ip6;
                    726:        int optlen = 0;
                    727:        u_int8_t *opt = opthead;
                    728:        u_int16_t rtalert_val;
                    729:
                    730:        for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
                    731:                switch(*opt) {
                    732:                 case IP6OPT_PAD1:
                    733:                         optlen = 1;
                    734:                         break;
                    735:                 case IP6OPT_PADN:
                    736:                         if (hbhlen < IP6OPT_MINLEN) {
                    737:                                 ip6stat.ip6s_toosmall++;
                    738:                                 goto bad;
                    739:                         }
                    740:                         optlen = *(opt + 1) + 2;
                    741:                         break;
                    742:                 case IP6OPT_RTALERT:
1.8       itojun    743:                         /* XXX may need check for alignment */
1.2       itojun    744:                         if (hbhlen < IP6OPT_RTALERT_LEN) {
                    745:                                 ip6stat.ip6s_toosmall++;
                    746:                                 goto bad;
                    747:                         }
                    748:                         if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2)
                    749:                                  /* XXX: should we discard the packet? */
                    750:                                 log(LOG_ERR, "length of router alert opt is inconsitent(%d)",
                    751:                                     *(opt + 1));
                    752:                         optlen = IP6OPT_RTALERT_LEN;
                    753:                         bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
                    754:                         *rtalertp = ntohs(rtalert_val);
                    755:                         break;
                    756:                 case IP6OPT_JUMBO:
1.8       itojun    757:                         /* XXX may need check for alignment */
1.2       itojun    758:                         if (hbhlen < IP6OPT_JUMBO_LEN) {
                    759:                                 ip6stat.ip6s_toosmall++;
                    760:                                 goto bad;
                    761:                         }
                    762:                         if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2)
                    763:                                  /* XXX: should we discard the packet? */
                    764:                                 log(LOG_ERR, "length of jumbopayload opt "
                    765:                                     "is inconsistent(%d)",
                    766:                                     *(opt + 1));
                    767:                         optlen = IP6OPT_JUMBO_LEN;
                    768:
                    769:                         /*
                    770:                          * We can simply cast because of the alignment
                    771:                          * requirement of the jumbo payload option.
                    772:                          */
1.8       itojun    773: #if 0
1.2       itojun    774:                         *plenp = ntohl(*(u_int32_t *)(opt + 2));
1.8       itojun    775: #else
                    776:                         bcopy(opt + 2, plenp, sizeof(*plenp));
                    777:                         *plenp = htonl(*plenp);
                    778: #endif
1.2       itojun    779:                         if (*plenp <= IPV6_MAXPACKET) {
                    780:                                 /*
                    781:                                  * jumbo payload length must be larger
                    782:                                  * than 65535
                    783:                                  */
                    784:                                 ip6stat.ip6s_badoptions++;
                    785:                                 icmp6_error(m, ICMP6_PARAM_PROB,
                    786:                                             ICMP6_PARAMPROB_HEADER,
                    787:                                             sizeof(struct ip6_hdr) +
                    788:                                             sizeof(struct ip6_hbh) +
                    789:                                             opt + 2 - opthead);
                    790:                                 return(-1);
                    791:                         }
                    792:
                    793:                         ip6 = mtod(m, struct ip6_hdr *);
                    794:                         if (ip6->ip6_plen) {
                    795:                                 /*
                    796:                                  * IPv6 packets that have non 0 payload length
                    797:                                  * must not contain a jumbo paylod option.
                    798:                                  */
                    799:                                 ip6stat.ip6s_badoptions++;
                    800:                                 icmp6_error(m, ICMP6_PARAM_PROB,
                    801:                                             ICMP6_PARAMPROB_HEADER,
                    802:                                             sizeof(struct ip6_hdr) +
                    803:                                             sizeof(struct ip6_hbh) +
                    804:                                             opt - opthead);
                    805:                                 return(-1);
                    806:                         }
                    807:                         break;
                    808:                 default:               /* unknown option */
                    809:                         if (hbhlen < IP6OPT_MINLEN) {
                    810:                                 ip6stat.ip6s_toosmall++;
                    811:                                 goto bad;
                    812:                         }
                    813:                         if ((optlen = ip6_unknown_opt(opt, m,
                    814:                                                       sizeof(struct ip6_hdr) +
                    815:                                                       sizeof(struct ip6_hbh) +
                    816:                                                       opt - opthead)) == -1)
                    817:                                 return(-1);
                    818:                         optlen += 2;
                    819:                         break;
                    820:                }
                    821:        }
                    822:
                    823:        return(0);
                    824:
                    825:   bad:
                    826:        m_freem(m);
                    827:        return(-1);
                    828: }
                    829:
                    830: /*
                    831:  * Unknown option processing.
                    832:  * The third argument `off' is the offset from the IPv6 header to the option,
                    833:  * which is necessary if the IPv6 header the and option header and IPv6 header
                    834:  * is not continuous in order to return an ICMPv6 error.
                    835:  */
                    836: int
                    837: ip6_unknown_opt(optp, m, off)
                    838:        u_int8_t *optp;
                    839:        struct mbuf *m;
                    840:        int off;
                    841: {
                    842:        struct ip6_hdr *ip6;
                    843:
                    844:        switch(IP6OPT_TYPE(*optp)) {
                    845:         case IP6OPT_TYPE_SKIP: /* ignore the option */
                    846:                 return((int)*(optp + 1));
                    847:         case IP6OPT_TYPE_DISCARD:      /* silently discard */
                    848:                 m_freem(m);
                    849:                 return(-1);
                    850:         case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
                    851:                 ip6stat.ip6s_badoptions++;
                    852:                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
                    853:                 return(-1);
                    854:         case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
                    855:                 ip6stat.ip6s_badoptions++;
                    856:                 ip6 = mtod(m, struct ip6_hdr *);
                    857:                 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
                    858:                     (m->m_flags & (M_BCAST|M_MCAST)))
                    859:                         m_freem(m);
                    860:                 else
                    861:                         icmp6_error(m, ICMP6_PARAM_PROB,
                    862:                                     ICMP6_PARAMPROB_OPTION, off);
                    863:                 return(-1);
                    864:        }
                    865:
                    866:        m_freem(m);             /* XXX: NOTREACHED */
                    867:        return(-1);
                    868: }
                    869:
                    870: /*
1.9       itojun    871:  * Create the "control" list for this pcb.
                    872:  *
                    873:  * The routine will be called from upper layer handlers like tcp6_input().
                    874:  * Thus the routine assumes that the caller (tcp6_input) have already
                    875:  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
                    876:  * very first mbuf on the mbuf chain.
                    877:  * We may want to add some infinite loop prevention or sanity checks for safety.
                    878:  * (This applies only when you are using KAME mbuf chain restriction, i.e.
                    879:  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
1.2       itojun    880:  */
                    881: void
                    882: ip6_savecontrol(in6p, mp, ip6, m)
                    883:        register struct in6pcb *in6p;
                    884:        register struct mbuf **mp;
                    885:        register struct ip6_hdr *ip6;
                    886:        register struct mbuf *m;
                    887: {
                    888:        struct proc *p = curproc;       /* XXX */
1.9       itojun    889:        int privileged;
1.2       itojun    890:
1.9       itojun    891:        privileged = 0;
                    892:        if (p && !suser(p->p_ucred, &p->p_acflag))
                    893:                privileged++;
                    894:
                    895: #ifdef SO_TIMESTAMP
1.2       itojun    896:        if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
                    897:                struct timeval tv;
                    898:
                    899:                microtime(&tv);
                    900:                *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
                    901:                        SCM_TIMESTAMP, SOL_SOCKET);
                    902:                if (*mp)
                    903:                        mp = &(*mp)->m_next;
                    904:        }
1.9       itojun    905: #endif
1.2       itojun    906:        if (in6p->in6p_flags & IN6P_RECVDSTADDR) {
                    907:                *mp = sbcreatecontrol((caddr_t) &ip6->ip6_dst,
                    908:                        sizeof(struct in6_addr), IPV6_RECVDSTADDR,
                    909:                        IPPROTO_IPV6);
                    910:                if (*mp)
                    911:                        mp = &(*mp)->m_next;
                    912:        }
                    913:
                    914: #ifdef noyet
                    915:        /* options were tossed above */
                    916:        if (in6p->in6p_flags & IN6P_RECVOPTS)
                    917:                /* broken */
                    918:        /* ip6_srcroute doesn't do what we want here, need to fix */
                    919:        if (in6p->in6p_flags & IPV6P_RECVRETOPTS)
                    920:                /* broken */
                    921: #endif
                    922:
                    923:        /* RFC 2292 sec. 5 */
                    924:        if (in6p->in6p_flags & IN6P_PKTINFO) {
                    925:                struct in6_pktinfo pi6;
                    926:                bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
                    927:                if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
                    928:                        pi6.ipi6_addr.s6_addr16[1] = 0;
                    929:                pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
                    930:                                        ? m->m_pkthdr.rcvif->if_index
                    931:                                        : 0;
                    932:                *mp = sbcreatecontrol((caddr_t) &pi6,
                    933:                        sizeof(struct in6_pktinfo), IPV6_PKTINFO,
                    934:                        IPPROTO_IPV6);
                    935:                if (*mp)
                    936:                        mp = &(*mp)->m_next;
                    937:        }
                    938:        if (in6p->in6p_flags & IN6P_HOPLIMIT) {
                    939:                int hlim = ip6->ip6_hlim & 0xff;
                    940:                *mp = sbcreatecontrol((caddr_t) &hlim,
                    941:                        sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
                    942:                if (*mp)
                    943:                        mp = &(*mp)->m_next;
                    944:        }
                    945:        /* IN6P_NEXTHOP - for outgoing packet only */
                    946:
                    947:        /*
                    948:         * IPV6_HOPOPTS socket option. We require super-user privilege
                    949:         * for the option, but it might be too strict, since there might
                    950:         * be some hop-by-hop options which can be returned to normal user.
                    951:         * See RFC 2292 section 6.
                    952:         */
1.9       itojun    953:        if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
1.2       itojun    954:                /*
                    955:                 * Check if a hop-by-hop options header is contatined in the
                    956:                 * received packet, and if so, store the options as ancillary
                    957:                 * data. Note that a hop-by-hop options header must be
                    958:                 * just after the IPv6 header, which fact is assured through
                    959:                 * the IPv6 input processing.
                    960:                 */
                    961:                struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                    962:                if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9       itojun    963:                        struct ip6_hbh *hbh;
                    964:                        int hbhlen;
                    965:
                    966: #ifndef PULLDOWN_TEST
                    967:                        hbh = (struct ip6_hbh *)(ip6 + 1);
                    968:                        hbhlen = (hbh->ip6h_len + 1) << 3;
                    969: #else
                    970:                        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                    971:                                sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
                    972:                        if (hbh == NULL) {
                    973:                                ip6stat.ip6s_tooshort++;
                    974:                                return;
                    975:                        }
                    976:                        hbhlen = (hbh->ip6h_len + 1) << 3;
                    977:                        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                    978:                                sizeof(struct ip6_hdr), hbhlen);
                    979:                        if (hbh == NULL) {
                    980:                                ip6stat.ip6s_tooshort++;
                    981:                                return;
                    982:                        }
                    983: #endif
1.2       itojun    984:
                    985:                        /*
                    986:                         * XXX: We copy whole the header even if a jumbo
                    987:                         * payload option is included, which option is to
                    988:                         * be removed before returning in the RFC 2292.
                    989:                         * But it's too painful operation...
                    990:                         */
1.9       itojun    991:                        *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1.2       itojun    992:                                              IPV6_HOPOPTS, IPPROTO_IPV6);
                    993:                        if (*mp)
                    994:                                mp = &(*mp)->m_next;
                    995:                }
                    996:        }
                    997:
                    998:        /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
                    999:        if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
                   1000:                struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                   1001:                int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);;
                   1002:
                   1003:                /*
                   1004:                 * Search for destination options headers or routing
                   1005:                 * header(s) through the header chain, and stores each
                   1006:                 * header as ancillary data.
                   1007:                 * Note that the order of the headers remains in
                   1008:                 * the chain of ancillary data.
                   1009:                 */
                   1010:                while(1) {      /* is explicit loop prevention necessary? */
1.9       itojun   1011:                        struct ip6_ext *ip6e;
                   1012:                        int elen;
                   1013:
                   1014: #ifndef PULLDOWN_TEST
                   1015:                        ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
                   1016:                        if (nxt == IPPROTO_AH)
                   1017:                                elen = (ip6e->ip6e_len + 2) << 2;
                   1018:                        else
                   1019:                                elen = (ip6e->ip6e_len + 1) << 3;
                   1020: #else
                   1021:                        IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off,
                   1022:                                sizeof(struct ip6_ext));
                   1023:                        if (ip6e == NULL) {
                   1024:                                ip6stat.ip6s_tooshort++;
                   1025:                                return;
                   1026:                        }
                   1027:                        if (nxt == IPPROTO_AH)
                   1028:                                elen = (ip6e->ip6e_len + 2) << 2;
                   1029:                        else
                   1030:                                elen = (ip6e->ip6e_len + 1) << 3;
                   1031:                        IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off, elen);
                   1032:                        if (ip6e == NULL) {
                   1033:                                ip6stat.ip6s_tooshort++;
                   1034:                                return;
                   1035:                        }
                   1036: #endif
1.2       itojun   1037:
                   1038:                        switch(nxt) {
                   1039:                         case IPPROTO_DSTOPTS:
                   1040:                                 if (!in6p->in6p_flags & IN6P_DSTOPTS)
                   1041:                                         break;
                   1042:
                   1043:                                 /*
                   1044:                                  * We also require super-user privilege for
                   1045:                                  * the option.
                   1046:                                  * See the comments on IN6_HOPOPTS.
                   1047:                                  */
1.9       itojun   1048:                                 if (!privileged)
1.2       itojun   1049:                                         break;
                   1050:
1.9       itojun   1051:                                 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1.2       itojun   1052:                                                       IPV6_DSTOPTS,
                   1053:                                                       IPPROTO_IPV6);
                   1054:                                 if (*mp)
                   1055:                                         mp = &(*mp)->m_next;
                   1056:                                 break;
                   1057:
                   1058:                         case IPPROTO_ROUTING:
                   1059:                                 if (!in6p->in6p_flags & IN6P_RTHDR)
                   1060:                                         break;
                   1061:
1.9       itojun   1062:                                 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1.2       itojun   1063:                                                       IPV6_RTHDR,
                   1064:                                                       IPPROTO_IPV6);
                   1065:                                 if (*mp)
                   1066:                                         mp = &(*mp)->m_next;
                   1067:                                 break;
                   1068:
                   1069:                         case IPPROTO_UDP:
                   1070:                         case IPPROTO_TCP:
                   1071:                         case IPPROTO_ICMPV6:
                   1072:                         default:
                   1073:                                 /*
                   1074:                                  * stop search if we encounter an upper
                   1075:                                  * layer protocol headers.
                   1076:                                  */
                   1077:                                 goto loopend;
                   1078:
                   1079:                         case IPPROTO_HOPOPTS:
                   1080:                         case IPPROTO_AH: /* is it possible? */
                   1081:                                 break;
                   1082:                        }
                   1083:
                   1084:                        /* proceed with the next header. */
1.9       itojun   1085:                        off += elen;
1.2       itojun   1086:                        nxt = ip6e->ip6e_nxt;
                   1087:                }
                   1088:          loopend:
                   1089:        }
1.9       itojun   1090:        if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
1.2       itojun   1091:                /* to be done */
                   1092:        }
1.9       itojun   1093:        if ((in6p->in6p_flags & IN6P_DSTOPTS) && privileged) {
1.2       itojun   1094:                /* to be done */
                   1095:        }
                   1096:        /* IN6P_RTHDR - to be done */
                   1097:
                   1098: }
                   1099:
                   1100: /*
                   1101:  * Get pointer to the previous header followed by the header
                   1102:  * currently processed.
                   1103:  * XXX: This function supposes that
                   1104:  *     M includes all headers,
                   1105:  *     the next header field and the header length field of each header
                   1106:  *     are valid, and
                   1107:  *     the sum of each header length equals to OFF.
                   1108:  * Because of these assumptions, this function must be called very
                   1109:  * carefully. Moreover, it will not be used in the near future when
                   1110:  * we develop `neater' mechanism to process extension headers.
                   1111:  */
                   1112: char *
                   1113: ip6_get_prevhdr(m, off)
                   1114:        struct mbuf *m;
                   1115:        int off;
                   1116: {
                   1117:        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                   1118:
                   1119:        if (off == sizeof(struct ip6_hdr))
                   1120:                return(&ip6->ip6_nxt);
                   1121:        else {
                   1122:                int len, nxt;
                   1123:                struct ip6_ext *ip6e = NULL;
                   1124:
                   1125:                nxt = ip6->ip6_nxt;
                   1126:                len = sizeof(struct ip6_hdr);
                   1127:                while (len < off) {
                   1128:                        ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
                   1129:
                   1130:                        switch(nxt) {
                   1131:                        case IPPROTO_FRAGMENT:
                   1132:                                len += sizeof(struct ip6_frag);
                   1133:                                break;
                   1134:                        case IPPROTO_AH:
                   1135:                                len += (ip6e->ip6e_len + 2) << 2;
                   1136:                                break;
                   1137:                        default:
                   1138:                                len += (ip6e->ip6e_len + 1) << 3;
                   1139:                                break;
                   1140:                        }
                   1141:                        nxt = ip6e->ip6e_nxt;
                   1142:                }
                   1143:                if (ip6e)
                   1144:                        return(&ip6e->ip6e_nxt);
                   1145:                else
                   1146:                        return NULL;
                   1147:        }
                   1148: }
                   1149:
                   1150: /*
                   1151:  * System control for IP6
                   1152:  */
                   1153:
                   1154: u_char inet6ctlerrmap[PRC_NCMDS] = {
                   1155:        0,              0,              0,              0,
                   1156:        0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
                   1157:        EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
                   1158:        EMSGSIZE,       EHOSTUNREACH,   0,              0,
                   1159:        0,              0,              0,              0,
                   1160:        ENOPROTOOPT
                   1161: };
                   1162:
                   1163: #include <vm/vm.h>
                   1164: #include <sys/sysctl.h>
                   1165:
                   1166: int
                   1167: ip6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
                   1168:        int *name;
                   1169:        u_int namelen;
                   1170:        void *oldp;
                   1171:        size_t *oldlenp;
                   1172:        void *newp;
                   1173:        size_t newlen;
                   1174: {
                   1175:        /* All sysctl names at this level are terminal. */
                   1176:        if (namelen != 1)
                   1177:                return ENOTDIR;
                   1178:
                   1179:        switch (name[0]) {
                   1180:
                   1181:        case IPV6CTL_FORWARDING:
                   1182:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1183:                                  &ip6_forwarding);
                   1184:        case IPV6CTL_SENDREDIRECTS:
                   1185:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1186:                                &ip6_sendredirects);
                   1187:        case IPV6CTL_DEFHLIM:
                   1188:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_defhlim);
                   1189:        case IPV6CTL_MAXFRAGPACKETS:
                   1190:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1191:                                &ip6_maxfragpackets);
                   1192:        case IPV6CTL_ACCEPT_RTADV:
                   1193:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1194:                                &ip6_accept_rtadv);
                   1195:        case IPV6CTL_KEEPFAITH:
                   1196:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_keepfaith);
                   1197:        case IPV6CTL_LOG_INTERVAL:
                   1198:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1199:                                &ip6_log_interval);
                   1200:        case IPV6CTL_HDRNESTLIMIT:
                   1201:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1202:                                &ip6_hdrnestlimit);
                   1203:        case IPV6CTL_DAD_COUNT:
                   1204:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_dad_count);
                   1205:        case IPV6CTL_AUTO_FLOWLABEL:
                   1206:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1207:                                &ip6_auto_flowlabel);
                   1208:        case IPV6CTL_DEFMCASTHLIM:
                   1209:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1210:                                &ip6_defmcasthlim);
                   1211:        case IPV6CTL_GIF_HLIM:
                   1212:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1213:                                &ip6_gif_hlim);
                   1214:        case IPV6CTL_KAME_VERSION:
                   1215:                return sysctl_rdstring(oldp, oldlenp, newp, __KAME_VERSION);
1.9       itojun   1216:        case IPV6CTL_USE_DEPRECATED:
                   1217:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1218:                                &ip6_use_deprecated);
1.10      itojun   1219:        case IPV6CTL_RR_PRUNE:
                   1220:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_rr_prune);
1.12      itojun   1221: #ifndef INET6_BINDV6ONLY
1.11      itojun   1222:        case IPV6CTL_BINDV6ONLY:
                   1223:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1224:                                &ip6_bindv6only);
                   1225: #endif
1.2       itojun   1226:        default:
                   1227:                return EOPNOTSUPP;
                   1228:        }
                   1229:        /* NOTREACHED */
                   1230: }

CVSweb <webmaster@jp.NetBSD.org>