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

1.45    ! itojun      1: /*     $NetBSD: ip6_input.c,v 1.44 2001/10/16 06:24:44 itojun Exp $    */
1.40      itojun      2: /*     $KAME: ip6_input.c,v 1.188 2001/03/29 05:34:31 itojun Exp $     */
1.3       thorpej     3:
1.2       itojun      4: /*
                      5:  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
                      6:  * All rights reserved.
1.21      itojun      7:  *
1.2       itojun      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. Neither the name of the project nor the names of its contributors
                     17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
1.21      itojun     19:  *
1.2       itojun     20:  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: /*
                     34:  * Copyright (c) 1982, 1986, 1988, 1993
                     35:  *     The Regents of the University of California.  All rights reserved.
                     36:  *
                     37:  * Redistribution and use in source and binary forms, with or without
                     38:  * modification, are permitted provided that the following conditions
                     39:  * are met:
                     40:  * 1. Redistributions of source code must retain the above copyright
                     41:  *    notice, this list of conditions and the following disclaimer.
                     42:  * 2. Redistributions in binary form must reproduce the above copyright
                     43:  *    notice, this list of conditions and the following disclaimer in the
                     44:  *    documentation and/or other materials provided with the distribution.
                     45:  * 3. All advertising materials mentioning features or use of this software
                     46:  *    must display the following acknowledgement:
                     47:  *     This product includes software developed by the University of
                     48:  *     California, Berkeley and its contributors.
                     49:  * 4. Neither the name of the University nor the names of its contributors
                     50:  *    may be used to endorse or promote products derived from this software
                     51:  *    without specific prior written permission.
                     52:  *
                     53:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     54:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     55:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     56:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     57:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     58:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     59:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     60:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     61:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     62:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     63:  * SUCH DAMAGE.
                     64:  *
                     65:  *     @(#)ip_input.c  8.2 (Berkeley) 1/4/94
                     66:  */
                     67:
                     68: #include "opt_inet.h"
1.4       thorpej    69: #include "opt_ipsec.h"
1.15      darrenr    70: #include "opt_pfil_hooks.h"
1.2       itojun     71:
                     72: #include <sys/param.h>
                     73: #include <sys/systm.h>
                     74: #include <sys/malloc.h>
                     75: #include <sys/mbuf.h>
                     76: #include <sys/domain.h>
                     77: #include <sys/protosw.h>
                     78: #include <sys/socket.h>
                     79: #include <sys/socketvar.h>
                     80: #include <sys/errno.h>
                     81: #include <sys/time.h>
                     82: #include <sys/kernel.h>
                     83: #include <sys/syslog.h>
                     84: #include <sys/proc.h>
                     85:
                     86: #include <net/if.h>
                     87: #include <net/if_types.h>
                     88: #include <net/if_dl.h>
                     89: #include <net/route.h>
                     90: #include <net/netisr.h>
1.15      darrenr    91: #ifdef PFIL_HOOKS
                     92: #include <net/pfil.h>
                     93: #endif
1.2       itojun     94:
                     95: #include <netinet/in.h>
1.9       itojun     96: #include <netinet/in_systm.h>
                     97: #ifdef INET
                     98: #include <netinet/ip.h>
                     99: #include <netinet/ip_icmp.h>
1.45    ! itojun    100: #endif /* INET */
1.14      itojun    101: #include <netinet/ip6.h>
1.2       itojun    102: #include <netinet6/in6_var.h>
1.11      itojun    103: #include <netinet6/ip6_var.h>
1.2       itojun    104: #include <netinet6/in6_pcb.h>
1.14      itojun    105: #include <netinet/icmp6.h>
1.2       itojun    106: #include <netinet6/in6_ifattach.h>
                    107: #include <netinet6/nd6.h>
1.9       itojun    108: #include <netinet6/in6_prefix.h>
1.2       itojun    109:
1.37      itojun    110: #ifdef IPSEC
                    111: #include <netinet6/ipsec.h>
                    112: #endif
                    113:
1.2       itojun    114: #include <netinet6/ip6protosw.h>
                    115:
                    116: /* we need it for NLOOP. */
                    117: #include "loop.h"
                    118: #include "faith.h"
                    119: #include "gif.h"
                    120: #include "bpfilter.h"
                    121:
1.9       itojun    122: #include <net/net_osdep.h>
                    123:
1.2       itojun    124: extern struct domain inet6domain;
                    125:
                    126: u_char ip6_protox[IPPROTO_MAX];
                    127: static int ip6qmaxlen = IFQ_MAXLEN;
                    128: struct in6_ifaddr *in6_ifaddr;
                    129: struct ifqueue ip6intrq;
                    130:
                    131: extern struct ifnet loif[NLOOP];
                    132: int ip6_forward_srcrt;                 /* XXX */
                    133: int ip6_sourcecheck;                   /* XXX */
                    134: int ip6_sourcecheck_interval;          /* XXX */
1.9       itojun    135:
1.29      thorpej   136: #ifdef PFIL_HOOKS
                    137: struct pfil_head inet6_pfil_hook;
                    138: #endif
                    139:
1.2       itojun    140: struct ip6stat ip6stat;
                    141:
                    142: static void ip6_init2 __P((void *));
                    143:
1.5       itojun    144: static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
1.2       itojun    145:
                    146: /*
                    147:  * IP6 initialization: fill in IP6 protocol switch table.
                    148:  * All protocols not implemented in kernel go to raw IP6 protocol handler.
                    149:  */
                    150: void
                    151: ip6_init()
                    152: {
1.35      itojun    153:        struct ip6protosw *pr;
                    154:        int i;
1.2       itojun    155:        struct timeval tv;
                    156:
                    157:        pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
                    158:        if (pr == 0)
                    159:                panic("ip6_init");
                    160:        for (i = 0; i < IPPROTO_MAX; i++)
                    161:                ip6_protox[i] = pr - inet6sw;
                    162:        for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
                    163:            pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
                    164:                if (pr->pr_domain->dom_family == PF_INET6 &&
                    165:                    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
                    166:                        ip6_protox[pr->pr_protocol] = pr - inet6sw;
                    167:        ip6intrq.ifq_maxlen = ip6qmaxlen;
                    168:        nd6_init();
                    169:        frag6_init();
                    170:        /*
                    171:         * in many cases, random() here does NOT return random number
                    172:         * as initialization during bootstrap time occur in fixed order.
                    173:         */
                    174:        microtime(&tv);
                    175:        ip6_flow_seq = random() ^ tv.tv_usec;
                    176:
                    177:        ip6_init2((void *)0);
1.29      thorpej   178:
                    179: #ifdef PFIL_HOOKS
                    180:        /* Register our Packet Filter hook. */
1.33      thorpej   181:        inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
                    182:        inet6_pfil_hook.ph_af   = AF_INET6;
1.29      thorpej   183:        i = pfil_head_register(&inet6_pfil_hook);
                    184:        if (i != 0)
                    185:                printf("ip6_init: WARNING: unable to register pfil hook, "
                    186:                    "error %d\n", i);
                    187: #endif /* PFIL_HOOKS */
1.2       itojun    188: }
                    189:
                    190: static void
                    191: ip6_init2(dummy)
                    192:        void *dummy;
                    193: {
                    194:        /*
                    195:         * to route local address of p2p link to loopback,
1.21      itojun    196:         * assign loopback address first.
1.2       itojun    197:         */
1.20      itojun    198:        in6_ifattach(&loif[0], NULL);
1.2       itojun    199:
                    200:        /* nd6_timer_init */
1.19      thorpej   201:        callout_init(&nd6_timer_ch);
                    202:        callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
1.9       itojun    203:        /* router renumbering prefix list maintenance */
1.19      thorpej   204:        callout_init(&in6_rr_timer_ch);
                    205:        callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL);
1.2       itojun    206: }
                    207:
                    208: /*
                    209:  * IP6 input interrupt handling. Just pass the packet to ip6_input.
                    210:  */
                    211: void
                    212: ip6intr()
                    213: {
                    214:        int s;
                    215:        struct mbuf *m;
                    216:
                    217:        for (;;) {
1.41      thorpej   218:                s = splnet();
1.2       itojun    219:                IF_DEQUEUE(&ip6intrq, m);
                    220:                splx(s);
                    221:                if (m == 0)
                    222:                        return;
                    223:                ip6_input(m);
                    224:        }
                    225: }
                    226:
                    227: extern struct  route_in6 ip6_forward_rt;
                    228:
                    229: void
                    230: ip6_input(m)
                    231:        struct mbuf *m;
                    232: {
1.9       itojun    233:        struct ip6_hdr *ip6;
1.2       itojun    234:        int off = sizeof(struct ip6_hdr), nest;
                    235:        u_int32_t plen;
1.5       itojun    236:        u_int32_t rtalert = ~0;
1.2       itojun    237:        int nxt, ours = 0;
1.9       itojun    238:        struct ifnet *deliverifp = NULL;
1.2       itojun    239:
                    240: #ifdef IPSEC
                    241:        /*
                    242:         * should the inner packet be considered authentic?
                    243:         * see comment in ah4_input().
                    244:         */
                    245:        if (m) {
                    246:                m->m_flags &= ~M_AUTHIPHDR;
                    247:                m->m_flags &= ~M_AUTHIPDGM;
                    248:        }
                    249: #endif
1.9       itojun    250:
1.2       itojun    251:        /*
1.44      itojun    252:         * mbuf statistics
1.2       itojun    253:         */
                    254:        if (m->m_flags & M_EXT) {
                    255:                if (m->m_next)
                    256:                        ip6stat.ip6s_mext2m++;
                    257:                else
                    258:                        ip6stat.ip6s_mext1++;
                    259:        } else {
1.40      itojun    260: #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
1.2       itojun    261:                if (m->m_next) {
1.9       itojun    262:                        if (m->m_flags & M_LOOP) {
1.44      itojun    263:                                ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
1.40      itojun    264:                        } else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
1.2       itojun    265:                                ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
                    266:                        else
                    267:                                ip6stat.ip6s_m2m[0]++;
                    268:                } else
                    269:                        ip6stat.ip6s_m1++;
1.40      itojun    270: #undef M2MMAX
1.2       itojun    271:        }
                    272:
1.9       itojun    273:        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
                    274:        ip6stat.ip6s_total++;
                    275:
                    276: #ifndef PULLDOWN_TEST
                    277:        /* XXX is the line really necessary? */
1.2       itojun    278:        IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
1.9       itojun    279: #endif
1.2       itojun    280:
1.9       itojun    281:        if (m->m_len < sizeof(struct ip6_hdr)) {
                    282:                struct ifnet *inifp;
                    283:                inifp = m->m_pkthdr.rcvif;
                    284:                if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
                    285:                        ip6stat.ip6s_toosmall++;
                    286:                        in6_ifstat_inc(inifp, ifs6_in_hdrerr);
                    287:                        return;
                    288:                }
1.2       itojun    289:        }
                    290:
                    291:        ip6 = mtod(m, struct ip6_hdr *);
                    292:
                    293:        if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
                    294:                ip6stat.ip6s_badvers++;
1.9       itojun    295:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
1.2       itojun    296:                goto bad;
                    297:        }
1.15      darrenr   298:
                    299: #ifdef PFIL_HOOKS
                    300:        /*
                    301:         * Run through list of hooks for input packets.  If there are any
                    302:         * filters which require that additional packets in the flow are
                    303:         * not fast-forwarded, they must clear the M_CANFASTFWD flag.
                    304:         * Note that filters must _never_ set this flag, as another filter
                    305:         * in the list may have previously cleared it.
                    306:         */
1.39      itojun    307:        /*
                    308:         * let ipfilter look at packet on the wire,
                    309:         * not the decapsulated packet.
                    310:         */
                    311: #ifdef IPSEC
1.42      itojun    312:        if (!ipsec_getnhist(m))
1.39      itojun    313: #else
                    314:        if (1)
                    315: #endif
                    316:        {
                    317:                if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif,
                    318:                                   PFIL_IN) != 0)
                    319:                        return;
                    320:                if (m == NULL)
                    321:                        return;
                    322:                ip6 = mtod(m, struct ip6_hdr *);
                    323:        }
1.15      darrenr   324: #endif /* PFIL_HOOKS */
                    325:
1.2       itojun    326:        ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
                    327:
1.30      thorpej   328: #ifdef ALTQ
                    329:        if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
                    330:                /* packet is dropped by traffic conditioner */
                    331:                return;
1.9       itojun    332:        }
                    333: #endif
                    334:
1.2       itojun    335:        /*
1.44      itojun    336:         * Check against address spoofing/corruption.
1.2       itojun    337:         */
                    338:        if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
                    339:            IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
                    340:                ip6stat.ip6s_badscope++;
1.9       itojun    341:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1.2       itojun    342:                goto bad;
                    343:        }
1.13      itojun    344:        /*
1.44      itojun    345:         * The following check is not documented in specs.  A malicious
                    346:         * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
                    347:         * and bypass security checks (act as if it was from 127.0.0.1 by using
                    348:         * IPv6 src ::ffff:127.0.0.1).  Be cautious.
1.35      itojun    349:         *
1.44      itojun    350:         * This check chokes if we are in an SIIT cloud.  As none of BSDs
                    351:         * support IPv4-less kernel compilation, we cannot support SIIT
                    352:         * environment at all.  So, it makes more sense for us to reject any
                    353:         * malicious packets for non-SIIT environment, than try to do a
                    354:         * partical support for SIIT environment.
1.13      itojun    355:         */
                    356:        if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
                    357:            IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
                    358:                ip6stat.ip6s_badscope++;
                    359:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
                    360:                goto bad;
                    361:        }
1.17      itojun    362: #if 0
1.13      itojun    363:        /*
1.17      itojun    364:         * Reject packets with IPv4 compatible addresses (auto tunnel).
                    365:         *
                    366:         * The code forbids auto tunnel relay case in RFC1933 (the check is
                    367:         * stronger than RFC1933).  We may want to re-enable it if mech-xx
                    368:         * is revised to forbid relaying case.
1.13      itojun    369:         */
                    370:        if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
                    371:            IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
                    372:                ip6stat.ip6s_badscope++;
                    373:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
                    374:                goto bad;
                    375:        }
                    376: #endif
1.35      itojun    377:
1.2       itojun    378:        if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
                    379:            IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
                    380:                if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
                    381:                        ours = 1;
1.9       itojun    382:                        deliverifp = m->m_pkthdr.rcvif;
1.2       itojun    383:                        goto hbhcheck;
                    384:                } else {
                    385:                        ip6stat.ip6s_badscope++;
1.9       itojun    386:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1.2       itojun    387:                        goto bad;
                    388:                }
                    389:        }
                    390:
1.38      itojun    391:        /* drop packets if interface ID portion is already filled */
                    392:        if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
                    393:                if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
                    394:                    ip6->ip6_src.s6_addr16[1]) {
                    395:                        ip6stat.ip6s_badscope++;
                    396:                        goto bad;
                    397:                }
                    398:                if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
                    399:                    ip6->ip6_dst.s6_addr16[1]) {
                    400:                        ip6stat.ip6s_badscope++;
                    401:                        goto bad;
                    402:                }
                    403:        }
1.2       itojun    404:
1.40      itojun    405:        if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
                    406:                ip6->ip6_src.s6_addr16[1]
                    407:                        = htons(m->m_pkthdr.rcvif->if_index);
                    408:        if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
                    409:                ip6->ip6_dst.s6_addr16[1]
                    410:                        = htons(m->m_pkthdr.rcvif->if_index);
                    411:
                    412:        /*
                    413:         * We use rt->rt_ifp to determine if the address is ours or not.
                    414:         * If rt_ifp is lo0, the address is ours.
                    415:         * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0,
                    416:         * so any address under fe80::%lo0/64 will be mistakenly considered
                    417:         * local.  The special case is supplied to handle the case properly
                    418:         * by actually looking at interface addresses
                    419:         * (using in6ifa_ifpwithaddr).
                    420:         */
                    421:        if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 &&
                    422:            IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
                    423:                if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) {
                    424:                        icmp6_error(m, ICMP6_DST_UNREACH,
                    425:                            ICMP6_DST_UNREACH_ADDR, 0);
                    426:                        /* m is already freed */
                    427:                        return;
1.21      itojun    428:                }
1.40      itojun    429:
                    430:                ours = 1;
                    431:                deliverifp = m->m_pkthdr.rcvif;
                    432:                goto hbhcheck;
1.9       itojun    433:        }
                    434:
1.2       itojun    435:        /*
                    436:         * Multicast check
                    437:         */
                    438:        if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
                    439:                struct  in6_multi *in6m = 0;
1.9       itojun    440:
                    441:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
1.2       itojun    442:                /*
                    443:                 * See if we belong to the destination multicast group on the
                    444:                 * arrival interface.
                    445:                 */
                    446:                IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
                    447:                if (in6m)
                    448:                        ours = 1;
                    449:                else if (!ip6_mrouter) {
                    450:                        ip6stat.ip6s_notmember++;
                    451:                        ip6stat.ip6s_cantforward++;
1.9       itojun    452:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2       itojun    453:                        goto bad;
                    454:                }
1.9       itojun    455:                deliverifp = m->m_pkthdr.rcvif;
1.2       itojun    456:                goto hbhcheck;
                    457:        }
                    458:
                    459:        /*
                    460:         *  Unicast check
                    461:         */
1.22      itojun    462:        if (ip6_forward_rt.ro_rt != NULL &&
                    463:            (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
                    464:            IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1.35      itojun    465:                               &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
1.25      itojun    466:                ip6stat.ip6s_forward_cachehit++;
1.22      itojun    467:        else {
1.35      itojun    468:                struct sockaddr_in6 *dst6;
                    469:
1.2       itojun    470:                if (ip6_forward_rt.ro_rt) {
1.22      itojun    471:                        /* route is down or destination is different */
1.25      itojun    472:                        ip6stat.ip6s_forward_cachemiss++;
1.2       itojun    473:                        RTFREE(ip6_forward_rt.ro_rt);
                    474:                        ip6_forward_rt.ro_rt = 0;
                    475:                }
1.22      itojun    476:
1.2       itojun    477:                bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
1.35      itojun    478:                dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
                    479:                dst6->sin6_len = sizeof(struct sockaddr_in6);
                    480:                dst6->sin6_family = AF_INET6;
                    481:                dst6->sin6_addr = ip6->ip6_dst;
1.2       itojun    482:
1.9       itojun    483:                rtalloc((struct route *)&ip6_forward_rt);
1.2       itojun    484:        }
                    485:
                    486: #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
                    487:
                    488:        /*
                    489:         * Accept the packet if the forwarding interface to the destination
                    490:         * according to the routing table is the loopback interface,
                    491:         * unless the associated route has a gateway.
                    492:         * Note that this approach causes to accept a packet if there is a
                    493:         * route to the loopback interface for the destination of the packet.
                    494:         * But we think it's even useful in some situations, e.g. when using
                    495:         * a special daemon which wants to intercept the packet.
                    496:         */
                    497:        if (ip6_forward_rt.ro_rt &&
                    498:            (ip6_forward_rt.ro_rt->rt_flags &
                    499:             (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
                    500: #if 0
                    501:            /*
                    502:             * The check below is redundant since the comparison of
                    503:             * the destination and the key of the rtentry has
                    504:             * already done through looking up the routing table.
                    505:             */
                    506:            IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
                    507:                               &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) &&
                    508: #endif
1.9       itojun    509:            ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
1.2       itojun    510:                struct in6_ifaddr *ia6 =
                    511:                        (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
                    512:                if (ia6->ia6_flags & IN6_IFF_ANYCAST)
                    513:                        m->m_flags |= M_ANYCAST6;
1.24      itojun    514:                /*
                    515:                 * packets to a tentative, duplicated, or somehow invalid
                    516:                 * address must not be accepted.
                    517:                 */
1.2       itojun    518:                if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
1.24      itojun    519:                        /* this address is ready */
1.2       itojun    520:                        ours = 1;
1.9       itojun    521:                        deliverifp = ia6->ia_ifp;       /* correct? */
1.2       itojun    522:                        goto hbhcheck;
                    523:                } else {
1.24      itojun    524:                        /* address is not ready, so discard the packet. */
1.34      itojun    525:                        nd6log((LOG_INFO,
1.27      itojun    526:                            "ip6_input: packet to an unready address %s->%s\n",
1.24      itojun    527:                            ip6_sprintf(&ip6->ip6_src),
1.34      itojun    528:                            ip6_sprintf(&ip6->ip6_dst)));
1.24      itojun    529:
                    530:                        goto bad;
1.2       itojun    531:                }
                    532:        }
                    533:
                    534:        /*
                    535:         * FAITH(Firewall Aided Internet Translator)
                    536:         */
                    537: #if defined(NFAITH) && 0 < NFAITH
                    538:        if (ip6_keepfaith) {
                    539:                if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
                    540:                 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
                    541:                        /* XXX do we need more sanity checks? */
                    542:                        ours = 1;
1.45    ! itojun    543:                        deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
1.9       itojun    544:                        goto hbhcheck;
                    545:                }
                    546:        }
                    547: #endif
                    548:
                    549: #if 0
                    550:     {
                    551:        /*
                    552:         * Last resort: check in6_ifaddr for incoming interface.
                    553:         * The code is here until I update the "goto ours hack" code above
                    554:         * working right.
                    555:         */
                    556:        struct ifaddr *ifa;
                    557:        for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
                    558:             ifa;
                    559:             ifa = ifa->ifa_list.tqe_next) {
                    560:                if (ifa->ifa_addr == NULL)
                    561:                        continue;       /* just for safety */
                    562:                if (ifa->ifa_addr->sa_family != AF_INET6)
                    563:                        continue;
                    564:                if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
                    565:                        ours = 1;
                    566:                        deliverifp = ifa->ifa_ifp;
1.2       itojun    567:                        goto hbhcheck;
                    568:                }
                    569:        }
1.9       itojun    570:     }
1.2       itojun    571: #endif
                    572:
                    573:        /*
                    574:         * Now there is no reason to process the packet if it's not our own
                    575:         * and we're not a router.
                    576:         */
                    577:        if (!ip6_forwarding) {
                    578:                ip6stat.ip6s_cantforward++;
1.9       itojun    579:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2       itojun    580:                goto bad;
                    581:        }
                    582:
                    583:   hbhcheck:
                    584:        /*
                    585:         * Process Hop-by-Hop options header if it's contained.
                    586:         * m may be modified in ip6_hopopts_input().
                    587:         * If a JumboPayload option is included, plen will also be modified.
                    588:         */
                    589:        plen = (u_int32_t)ntohs(ip6->ip6_plen);
                    590:        if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9       itojun    591:                struct ip6_hbh *hbh;
                    592:
                    593:                if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
                    594: #if 0  /*touches NULL pointer*/
                    595:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
                    596: #endif
1.2       itojun    597:                        return; /* m have already been freed */
1.9       itojun    598:                }
1.22      itojun    599:
1.2       itojun    600:                /* adjust pointer */
                    601:                ip6 = mtod(m, struct ip6_hdr *);
1.22      itojun    602:
                    603:                /*
                    604:                 * if the payload length field is 0 and the next header field
                    605:                 * indicates Hop-by-Hop Options header, then a Jumbo Payload
                    606:                 * option MUST be included.
                    607:                 */
                    608:                if (ip6->ip6_plen == 0 && plen == 0) {
                    609:                        /*
                    610:                         * Note that if a valid jumbo payload option is
                    611:                         * contained, ip6_hoptops_input() must set a valid
                    612:                         * (non-zero) payload length to the variable plen.
                    613:                         */
                    614:                        ip6stat.ip6s_badoptions++;
                    615:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
                    616:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
                    617:                        icmp6_error(m, ICMP6_PARAM_PROB,
                    618:                                    ICMP6_PARAMPROB_HEADER,
                    619:                                    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
                    620:                        return;
                    621:                }
1.9       itojun    622: #ifndef PULLDOWN_TEST
                    623:                /* ip6_hopopts_input() ensures that mbuf is contiguous */
                    624:                hbh = (struct ip6_hbh *)(ip6 + 1);
                    625: #else
                    626:                IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
                    627:                        sizeof(struct ip6_hbh));
                    628:                if (hbh == NULL) {
                    629:                        ip6stat.ip6s_tooshort++;
                    630:                        return;
                    631:                }
                    632: #endif
                    633:                nxt = hbh->ip6h_nxt;
1.2       itojun    634:
                    635:                /*
                    636:                 * accept the packet if a router alert option is included
                    637:                 * and we act as an IPv6 router.
                    638:                 */
1.5       itojun    639:                if (rtalert != ~0 && ip6_forwarding)
1.2       itojun    640:                        ours = 1;
                    641:        } else
                    642:                nxt = ip6->ip6_nxt;
                    643:
                    644:        /*
                    645:         * Check that the amount of data in the buffers
                    646:         * is as at least much as the IPv6 header would have us expect.
                    647:         * Trim mbufs if longer than we expect.
                    648:         * Drop packet if shorter than we expect.
                    649:         */
                    650:        if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
                    651:                ip6stat.ip6s_tooshort++;
1.9       itojun    652:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.2       itojun    653:                goto bad;
                    654:        }
                    655:        if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
                    656:                if (m->m_len == m->m_pkthdr.len) {
                    657:                        m->m_len = sizeof(struct ip6_hdr) + plen;
                    658:                        m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
                    659:                } else
                    660:                        m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
                    661:        }
                    662:
                    663:        /*
                    664:         * Forward if desirable.
                    665:         */
                    666:        if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
                    667:                /*
                    668:                 * If we are acting as a multicast router, all
                    669:                 * incoming multicast packets are passed to the
                    670:                 * kernel-level multicast forwarding function.
                    671:                 * The packet is returned (relatively) intact; if
                    672:                 * ip6_mforward() returns a non-zero value, the packet
                    673:                 * must be discarded, else it may be accepted below.
                    674:                 */
                    675:                if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
                    676:                        ip6stat.ip6s_cantforward++;
                    677:                        m_freem(m);
                    678:                        return;
                    679:                }
                    680:                if (!ours) {
                    681:                        m_freem(m);
                    682:                        return;
                    683:                }
1.22      itojun    684:        } else if (!ours) {
1.2       itojun    685:                ip6_forward(m, 0);
                    686:                return;
                    687:        }
1.25      itojun    688:
                    689:        ip6 = mtod(m, struct ip6_hdr *);
                    690:
                    691:        /*
                    692:         * Malicious party may be able to use IPv4 mapped addr to confuse
                    693:         * tcp/udp stack and bypass security checks (act as if it was from
                    694:         * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
                    695:         *
                    696:         * For SIIT end node behavior, you may want to disable the check.
                    697:         * However, you will  become vulnerable to attacks using IPv4 mapped
                    698:         * source.
                    699:         */
                    700:        if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
                    701:            IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
                    702:                ip6stat.ip6s_badscope++;
                    703:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
                    704:                goto bad;
                    705:        }
1.2       itojun    706:
                    707:        /*
                    708:         * Tell launch routine the next header
                    709:         */
1.12      itojun    710: #ifdef IFA_STATS
1.28      itojun    711:        if (deliverifp != NULL) {
1.9       itojun    712:                struct in6_ifaddr *ia6;
                    713:                ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
                    714:                if (ia6)
                    715:                        ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
                    716:        }
                    717: #endif
1.2       itojun    718:        ip6stat.ip6s_delivered++;
1.9       itojun    719:        in6_ifstat_inc(deliverifp, ifs6_in_deliver);
1.2       itojun    720:        nest = 0;
1.40      itojun    721:
1.2       itojun    722:        while (nxt != IPPROTO_DONE) {
                    723:                if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
                    724:                        ip6stat.ip6s_toomanyhdr++;
                    725:                        goto bad;
                    726:                }
1.8       itojun    727:
                    728:                /*
                    729:                 * protection against faulty packet - there should be
                    730:                 * more sanity checks in header chain processing.
                    731:                 */
                    732:                if (m->m_pkthdr.len < off) {
                    733:                        ip6stat.ip6s_tooshort++;
1.9       itojun    734:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.8       itojun    735:                        goto bad;
                    736:                }
                    737:
1.37      itojun    738: #ifdef IPSEC
                    739:                /*
                    740:                 * enforce IPsec policy checking if we are seeing last header.
                    741:                 * note that we do not visit this with protocols with pcb layer
                    742:                 * code - like udp/tcp/raw ip.
                    743:                 */
                    744:                if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
                    745:                    ipsec6_in_reject(m, NULL)) {
                    746:                        ipsec6stat.in_polvio++;
                    747:                        goto bad;
                    748:                }
                    749: #endif
                    750:
1.2       itojun    751:                nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
                    752:        }
                    753:        return;
                    754:  bad:
                    755:        m_freem(m);
                    756: }
                    757:
                    758: /*
                    759:  * Hop-by-Hop options header processing. If a valid jumbo payload option is
                    760:  * included, the real payload length will be stored in plenp.
                    761:  */
                    762: static int
                    763: ip6_hopopts_input(plenp, rtalertp, mp, offp)
                    764:        u_int32_t *plenp;
1.5       itojun    765:        u_int32_t *rtalertp;    /* XXX: should be stored more smart way */
1.2       itojun    766:        struct mbuf **mp;
                    767:        int *offp;
                    768: {
1.35      itojun    769:        struct mbuf *m = *mp;
1.2       itojun    770:        int off = *offp, hbhlen;
                    771:        struct ip6_hbh *hbh;
                    772:        u_int8_t *opt;
                    773:
                    774:        /* validation of the length of the header */
1.9       itojun    775: #ifndef PULLDOWN_TEST
1.2       itojun    776:        IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
                    777:        hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
                    778:        hbhlen = (hbh->ip6h_len + 1) << 3;
                    779:
                    780:        IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
                    781:        hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1.9       itojun    782: #else
                    783:        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                    784:                sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
                    785:        if (hbh == NULL) {
                    786:                ip6stat.ip6s_tooshort++;
                    787:                return -1;
                    788:        }
                    789:        hbhlen = (hbh->ip6h_len + 1) << 3;
                    790:        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
                    791:                hbhlen);
                    792:        if (hbh == NULL) {
                    793:                ip6stat.ip6s_tooshort++;
                    794:                return -1;
                    795:        }
                    796: #endif
1.2       itojun    797:        off += hbhlen;
                    798:        hbhlen -= sizeof(struct ip6_hbh);
                    799:        opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
                    800:
                    801:        if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
                    802:                                hbhlen, rtalertp, plenp) < 0)
                    803:                return(-1);
                    804:
                    805:        *offp = off;
                    806:        *mp = m;
                    807:        return(0);
                    808: }
                    809:
                    810: /*
                    811:  * Search header for all Hop-by-hop options and process each option.
                    812:  * This function is separate from ip6_hopopts_input() in order to
                    813:  * handle a case where the sending node itself process its hop-by-hop
                    814:  * options header. In such a case, the function is called from ip6_output().
                    815:  */
                    816: int
                    817: ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
                    818:        struct mbuf *m;
                    819:        u_int8_t *opthead;
                    820:        int hbhlen;
1.5       itojun    821:        u_int32_t *rtalertp;
1.2       itojun    822:        u_int32_t *plenp;
                    823: {
                    824:        struct ip6_hdr *ip6;
                    825:        int optlen = 0;
                    826:        u_int8_t *opt = opthead;
                    827:        u_int16_t rtalert_val;
1.22      itojun    828:        u_int32_t jumboplen;
1.2       itojun    829:
                    830:        for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1.35      itojun    831:                switch (*opt) {
                    832:                case IP6OPT_PAD1:
                    833:                        optlen = 1;
                    834:                        break;
                    835:                case IP6OPT_PADN:
                    836:                        if (hbhlen < IP6OPT_MINLEN) {
                    837:                                ip6stat.ip6s_toosmall++;
                    838:                                goto bad;
                    839:                        }
                    840:                        optlen = *(opt + 1) + 2;
                    841:                        break;
                    842:                case IP6OPT_RTALERT:
                    843:                        /* XXX may need check for alignment */
                    844:                        if (hbhlen < IP6OPT_RTALERT_LEN) {
                    845:                                ip6stat.ip6s_toosmall++;
                    846:                                goto bad;
                    847:                        }
                    848:                        if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
                    849:                                 /* XXX: should we discard the packet? */
                    850:                                log(LOG_ERR, "length of router alert opt is inconsitent(%d)",
                    851:                                    *(opt + 1));
                    852:                        }
                    853:                        optlen = IP6OPT_RTALERT_LEN;
                    854:                        bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
                    855:                        *rtalertp = ntohs(rtalert_val);
                    856:                        break;
                    857:                case IP6OPT_JUMBO:
1.22      itojun    858:                        /* XXX may need check for alignment */
                    859:                        if (hbhlen < IP6OPT_JUMBO_LEN) {
                    860:                                ip6stat.ip6s_toosmall++;
                    861:                                goto bad;
                    862:                        }
1.35      itojun    863:                        if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
                    864:                                /* XXX: should we discard the packet? */
1.22      itojun    865:                                log(LOG_ERR, "length of jumbopayload opt "
1.27      itojun    866:                                    "is inconsistent(%d)\n",
1.22      itojun    867:                                    *(opt + 1));
1.35      itojun    868:                        }
1.22      itojun    869:                        optlen = IP6OPT_JUMBO_LEN;
                    870:
                    871:                        /*
                    872:                         * IPv6 packets that have non 0 payload length
1.35      itojun    873:                         * must not contain a jumbo payload option.
1.22      itojun    874:                         */
                    875:                        ip6 = mtod(m, struct ip6_hdr *);
                    876:                        if (ip6->ip6_plen) {
                    877:                                ip6stat.ip6s_badoptions++;
                    878:                                icmp6_error(m, ICMP6_PARAM_PROB,
                    879:                                            ICMP6_PARAMPROB_HEADER,
                    880:                                            sizeof(struct ip6_hdr) +
                    881:                                            sizeof(struct ip6_hbh) +
                    882:                                            opt - opthead);
                    883:                                return(-1);
                    884:                        }
1.2       itojun    885:
1.22      itojun    886:                        /*
                    887:                         * We may see jumbolen in unaligned location, so
                    888:                         * we'd need to perform bcopy().
                    889:                         */
                    890:                        bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
                    891:                        jumboplen = (u_int32_t)htonl(jumboplen);
                    892:
                    893: #if 1
                    894:                        /*
                    895:                         * if there are multiple jumbo payload options,
                    896:                         * *plenp will be non-zero and the packet will be
                    897:                         * rejected.
                    898:                         * the behavior may need some debate in ipngwg -
                    899:                         * multiple options does not make sense, however,
                    900:                         * there's no explicit mention in specification.
                    901:                         */
                    902:                        if (*plenp != 0) {
                    903:                                ip6stat.ip6s_badoptions++;
                    904:                                icmp6_error(m, ICMP6_PARAM_PROB,
                    905:                                            ICMP6_PARAMPROB_HEADER,
                    906:                                            sizeof(struct ip6_hdr) +
                    907:                                            sizeof(struct ip6_hbh) +
                    908:                                            opt + 2 - opthead);
                    909:                                return(-1);
                    910:                        }
1.8       itojun    911: #endif
1.2       itojun    912:
1.22      itojun    913:                        /*
                    914:                         * jumbo payload length must be larger than 65535.
                    915:                         */
                    916:                        if (jumboplen <= IPV6_MAXPACKET) {
                    917:                                ip6stat.ip6s_badoptions++;
                    918:                                icmp6_error(m, ICMP6_PARAM_PROB,
                    919:                                            ICMP6_PARAMPROB_HEADER,
                    920:                                            sizeof(struct ip6_hdr) +
                    921:                                            sizeof(struct ip6_hbh) +
                    922:                                            opt + 2 - opthead);
                    923:                                return(-1);
                    924:                        }
                    925:                        *plenp = jumboplen;
                    926:
                    927:                        break;
1.35      itojun    928:                default:                /* unknown option */
                    929:                        if (hbhlen < IP6OPT_MINLEN) {
                    930:                                ip6stat.ip6s_toosmall++;
                    931:                                goto bad;
                    932:                        }
                    933:                        if ((optlen = ip6_unknown_opt(opt, m,
                    934:                                                      sizeof(struct ip6_hdr) +
                    935:                                                      sizeof(struct ip6_hbh) +
                    936:                                                      opt - opthead)) == -1)
                    937:                                return(-1);
                    938:                        optlen += 2;
                    939:                        break;
1.2       itojun    940:                }
                    941:        }
                    942:
                    943:        return(0);
                    944:
                    945:   bad:
                    946:        m_freem(m);
                    947:        return(-1);
                    948: }
                    949:
                    950: /*
                    951:  * Unknown option processing.
                    952:  * The third argument `off' is the offset from the IPv6 header to the option,
                    953:  * which is necessary if the IPv6 header the and option header and IPv6 header
                    954:  * is not continuous in order to return an ICMPv6 error.
                    955:  */
                    956: int
                    957: ip6_unknown_opt(optp, m, off)
                    958:        u_int8_t *optp;
                    959:        struct mbuf *m;
                    960:        int off;
                    961: {
                    962:        struct ip6_hdr *ip6;
                    963:
1.35      itojun    964:        switch (IP6OPT_TYPE(*optp)) {
                    965:        case IP6OPT_TYPE_SKIP: /* ignore the option */
                    966:                return((int)*(optp + 1));
                    967:        case IP6OPT_TYPE_DISCARD:       /* silently discard */
                    968:                m_freem(m);
                    969:                return(-1);
                    970:        case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
                    971:                ip6stat.ip6s_badoptions++;
                    972:                icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
                    973:                return(-1);
                    974:        case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
                    975:                ip6stat.ip6s_badoptions++;
                    976:                ip6 = mtod(m, struct ip6_hdr *);
                    977:                if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
                    978:                    (m->m_flags & (M_BCAST|M_MCAST)))
                    979:                        m_freem(m);
                    980:                else
                    981:                        icmp6_error(m, ICMP6_PARAM_PROB,
                    982:                                    ICMP6_PARAMPROB_OPTION, off);
                    983:                return(-1);
1.2       itojun    984:        }
                    985:
                    986:        m_freem(m);             /* XXX: NOTREACHED */
                    987:        return(-1);
                    988: }
                    989:
                    990: /*
1.9       itojun    991:  * Create the "control" list for this pcb.
                    992:  *
                    993:  * The routine will be called from upper layer handlers like tcp6_input().
                    994:  * Thus the routine assumes that the caller (tcp6_input) have already
                    995:  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
                    996:  * very first mbuf on the mbuf chain.
                    997:  * We may want to add some infinite loop prevention or sanity checks for safety.
                    998:  * (This applies only when you are using KAME mbuf chain restriction, i.e.
                    999:  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
1.2       itojun   1000:  */
                   1001: void
                   1002: ip6_savecontrol(in6p, mp, ip6, m)
1.35      itojun   1003:        struct in6pcb *in6p;
                   1004:        struct mbuf **mp;
                   1005:        struct ip6_hdr *ip6;
                   1006:        struct mbuf *m;
1.2       itojun   1007: {
                   1008:        struct proc *p = curproc;       /* XXX */
1.9       itojun   1009:        int privileged;
1.2       itojun   1010:
1.9       itojun   1011:        privileged = 0;
                   1012:        if (p && !suser(p->p_ucred, &p->p_acflag))
                   1013:                privileged++;
                   1014:
                   1015: #ifdef SO_TIMESTAMP
1.2       itojun   1016:        if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
                   1017:                struct timeval tv;
                   1018:
                   1019:                microtime(&tv);
                   1020:                *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
                   1021:                        SCM_TIMESTAMP, SOL_SOCKET);
                   1022:                if (*mp)
                   1023:                        mp = &(*mp)->m_next;
                   1024:        }
1.9       itojun   1025: #endif
1.2       itojun   1026:        if (in6p->in6p_flags & IN6P_RECVDSTADDR) {
                   1027:                *mp = sbcreatecontrol((caddr_t) &ip6->ip6_dst,
                   1028:                        sizeof(struct in6_addr), IPV6_RECVDSTADDR,
                   1029:                        IPPROTO_IPV6);
                   1030:                if (*mp)
                   1031:                        mp = &(*mp)->m_next;
                   1032:        }
                   1033:
                   1034: #ifdef noyet
                   1035:        /* options were tossed above */
                   1036:        if (in6p->in6p_flags & IN6P_RECVOPTS)
                   1037:                /* broken */
                   1038:        /* ip6_srcroute doesn't do what we want here, need to fix */
                   1039:        if (in6p->in6p_flags & IPV6P_RECVRETOPTS)
                   1040:                /* broken */
                   1041: #endif
                   1042:
                   1043:        /* RFC 2292 sec. 5 */
1.35      itojun   1044:        if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1.2       itojun   1045:                struct in6_pktinfo pi6;
                   1046:                bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
                   1047:                if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
                   1048:                        pi6.ipi6_addr.s6_addr16[1] = 0;
                   1049:                pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
                   1050:                                        ? m->m_pkthdr.rcvif->if_index
                   1051:                                        : 0;
                   1052:                *mp = sbcreatecontrol((caddr_t) &pi6,
                   1053:                        sizeof(struct in6_pktinfo), IPV6_PKTINFO,
                   1054:                        IPPROTO_IPV6);
                   1055:                if (*mp)
                   1056:                        mp = &(*mp)->m_next;
                   1057:        }
                   1058:        if (in6p->in6p_flags & IN6P_HOPLIMIT) {
                   1059:                int hlim = ip6->ip6_hlim & 0xff;
                   1060:                *mp = sbcreatecontrol((caddr_t) &hlim,
                   1061:                        sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
                   1062:                if (*mp)
                   1063:                        mp = &(*mp)->m_next;
                   1064:        }
                   1065:        /* IN6P_NEXTHOP - for outgoing packet only */
                   1066:
                   1067:        /*
                   1068:         * IPV6_HOPOPTS socket option. We require super-user privilege
                   1069:         * for the option, but it might be too strict, since there might
                   1070:         * be some hop-by-hop options which can be returned to normal user.
                   1071:         * See RFC 2292 section 6.
                   1072:         */
1.35      itojun   1073:        if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) {
1.2       itojun   1074:                /*
                   1075:                 * Check if a hop-by-hop options header is contatined in the
                   1076:                 * received packet, and if so, store the options as ancillary
                   1077:                 * data. Note that a hop-by-hop options header must be
                   1078:                 * just after the IPv6 header, which fact is assured through
                   1079:                 * the IPv6 input processing.
                   1080:                 */
                   1081:                struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                   1082:                if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9       itojun   1083:                        struct ip6_hbh *hbh;
                   1084:                        int hbhlen;
                   1085:
                   1086: #ifndef PULLDOWN_TEST
                   1087:                        hbh = (struct ip6_hbh *)(ip6 + 1);
                   1088:                        hbhlen = (hbh->ip6h_len + 1) << 3;
                   1089: #else
                   1090:                        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                   1091:                                sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
                   1092:                        if (hbh == NULL) {
                   1093:                                ip6stat.ip6s_tooshort++;
                   1094:                                return;
                   1095:                        }
                   1096:                        hbhlen = (hbh->ip6h_len + 1) << 3;
                   1097:                        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                   1098:                                sizeof(struct ip6_hdr), hbhlen);
                   1099:                        if (hbh == NULL) {
                   1100:                                ip6stat.ip6s_tooshort++;
                   1101:                                return;
                   1102:                        }
                   1103: #endif
1.2       itojun   1104:
                   1105:                        /*
                   1106:                         * XXX: We copy whole the header even if a jumbo
                   1107:                         * payload option is included, which option is to
                   1108:                         * be removed before returning in the RFC 2292.
                   1109:                         * But it's too painful operation...
                   1110:                         */
1.9       itojun   1111:                        *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1.2       itojun   1112:                                              IPV6_HOPOPTS, IPPROTO_IPV6);
                   1113:                        if (*mp)
                   1114:                                mp = &(*mp)->m_next;
                   1115:                }
                   1116:        }
                   1117:
                   1118:        /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
                   1119:        if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
                   1120:                struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                   1121:                int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);;
                   1122:
                   1123:                /*
                   1124:                 * Search for destination options headers or routing
                   1125:                 * header(s) through the header chain, and stores each
                   1126:                 * header as ancillary data.
                   1127:                 * Note that the order of the headers remains in
                   1128:                 * the chain of ancillary data.
                   1129:                 */
1.35      itojun   1130:                while (1) {     /* is explicit loop prevention necessary? */
1.9       itojun   1131:                        struct ip6_ext *ip6e;
                   1132:                        int elen;
                   1133:
                   1134: #ifndef PULLDOWN_TEST
                   1135:                        ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
                   1136:                        if (nxt == IPPROTO_AH)
                   1137:                                elen = (ip6e->ip6e_len + 2) << 2;
                   1138:                        else
                   1139:                                elen = (ip6e->ip6e_len + 1) << 3;
                   1140: #else
                   1141:                        IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off,
                   1142:                                sizeof(struct ip6_ext));
                   1143:                        if (ip6e == NULL) {
                   1144:                                ip6stat.ip6s_tooshort++;
                   1145:                                return;
                   1146:                        }
                   1147:                        if (nxt == IPPROTO_AH)
                   1148:                                elen = (ip6e->ip6e_len + 2) << 2;
                   1149:                        else
                   1150:                                elen = (ip6e->ip6e_len + 1) << 3;
                   1151:                        IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off, elen);
                   1152:                        if (ip6e == NULL) {
                   1153:                                ip6stat.ip6s_tooshort++;
                   1154:                                return;
                   1155:                        }
                   1156: #endif
1.2       itojun   1157:
1.35      itojun   1158:                        switch (nxt) {
                   1159:                        case IPPROTO_DSTOPTS:
                   1160:                                if (!in6p->in6p_flags & IN6P_DSTOPTS)
                   1161:                                        break;
                   1162:
                   1163:                                /*
                   1164:                                 * We also require super-user privilege for
                   1165:                                 * the option.
                   1166:                                 * See the comments on IN6_HOPOPTS.
                   1167:                                 */
                   1168:                                if (!privileged)
                   1169:                                        break;
                   1170:
                   1171:                                *mp = sbcreatecontrol((caddr_t)ip6e, elen,
                   1172:                                                      IPV6_DSTOPTS,
                   1173:                                                      IPPROTO_IPV6);
                   1174:                                if (*mp)
                   1175:                                        mp = &(*mp)->m_next;
                   1176:                                break;
                   1177:
                   1178:                        case IPPROTO_ROUTING:
                   1179:                                if (!in6p->in6p_flags & IN6P_RTHDR)
                   1180:                                        break;
                   1181:
                   1182:                                *mp = sbcreatecontrol((caddr_t)ip6e, elen,
                   1183:                                                      IPV6_RTHDR,
                   1184:                                                      IPPROTO_IPV6);
                   1185:                                if (*mp)
                   1186:                                        mp = &(*mp)->m_next;
                   1187:                                break;
                   1188:
                   1189:                        case IPPROTO_UDP:
                   1190:                        case IPPROTO_TCP:
                   1191:                        case IPPROTO_ICMPV6:
                   1192:                        default:
                   1193:                                /*
                   1194:                                 * stop search if we encounter an upper
                   1195:                                 * layer protocol headers.
                   1196:                                 */
                   1197:                                goto loopend;
                   1198:
                   1199:                        case IPPROTO_HOPOPTS:
                   1200:                        case IPPROTO_AH: /* is it possible? */
                   1201:                                break;
1.2       itojun   1202:                        }
                   1203:
                   1204:                        /* proceed with the next header. */
1.9       itojun   1205:                        off += elen;
1.2       itojun   1206:                        nxt = ip6e->ip6e_nxt;
                   1207:                }
                   1208:          loopend:
1.36      cgd      1209:                ;
1.2       itojun   1210:        }
1.9       itojun   1211:        if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
1.2       itojun   1212:                /* to be done */
                   1213:        }
1.9       itojun   1214:        if ((in6p->in6p_flags & IN6P_DSTOPTS) && privileged) {
1.2       itojun   1215:                /* to be done */
                   1216:        }
                   1217:        /* IN6P_RTHDR - to be done */
                   1218:
                   1219: }
                   1220:
                   1221: /*
                   1222:  * Get pointer to the previous header followed by the header
                   1223:  * currently processed.
                   1224:  * XXX: This function supposes that
                   1225:  *     M includes all headers,
                   1226:  *     the next header field and the header length field of each header
                   1227:  *     are valid, and
                   1228:  *     the sum of each header length equals to OFF.
                   1229:  * Because of these assumptions, this function must be called very
                   1230:  * carefully. Moreover, it will not be used in the near future when
                   1231:  * we develop `neater' mechanism to process extension headers.
                   1232:  */
                   1233: char *
                   1234: ip6_get_prevhdr(m, off)
                   1235:        struct mbuf *m;
                   1236:        int off;
                   1237: {
                   1238:        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                   1239:
                   1240:        if (off == sizeof(struct ip6_hdr))
                   1241:                return(&ip6->ip6_nxt);
                   1242:        else {
                   1243:                int len, nxt;
                   1244:                struct ip6_ext *ip6e = NULL;
                   1245:
                   1246:                nxt = ip6->ip6_nxt;
                   1247:                len = sizeof(struct ip6_hdr);
                   1248:                while (len < off) {
                   1249:                        ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
                   1250:
1.35      itojun   1251:                        switch (nxt) {
1.2       itojun   1252:                        case IPPROTO_FRAGMENT:
                   1253:                                len += sizeof(struct ip6_frag);
                   1254:                                break;
                   1255:                        case IPPROTO_AH:
                   1256:                                len += (ip6e->ip6e_len + 2) << 2;
                   1257:                                break;
                   1258:                        default:
                   1259:                                len += (ip6e->ip6e_len + 1) << 3;
                   1260:                                break;
                   1261:                        }
                   1262:                        nxt = ip6e->ip6e_nxt;
                   1263:                }
                   1264:                if (ip6e)
                   1265:                        return(&ip6e->ip6e_nxt);
                   1266:                else
                   1267:                        return NULL;
1.18      itojun   1268:        }
                   1269: }
                   1270:
                   1271: /*
                   1272:  * get next header offset.  m will be retained.
                   1273:  */
                   1274: int
                   1275: ip6_nexthdr(m, off, proto, nxtp)
                   1276:        struct mbuf *m;
                   1277:        int off;
                   1278:        int proto;
                   1279:        int *nxtp;
                   1280: {
                   1281:        struct ip6_hdr ip6;
                   1282:        struct ip6_ext ip6e;
                   1283:        struct ip6_frag fh;
                   1284:
                   1285:        /* just in case */
                   1286:        if (m == NULL)
                   1287:                panic("ip6_nexthdr: m == NULL");
                   1288:        if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
                   1289:                return -1;
                   1290:
                   1291:        switch (proto) {
                   1292:        case IPPROTO_IPV6:
                   1293:                if (m->m_pkthdr.len < off + sizeof(ip6))
                   1294:                        return -1;
                   1295:                m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
                   1296:                if (nxtp)
                   1297:                        *nxtp = ip6.ip6_nxt;
                   1298:                off += sizeof(ip6);
                   1299:                return off;
                   1300:
                   1301:        case IPPROTO_FRAGMENT:
                   1302:                /*
                   1303:                 * terminate parsing if it is not the first fragment,
                   1304:                 * it does not make sense to parse through it.
                   1305:                 */
                   1306:                if (m->m_pkthdr.len < off + sizeof(fh))
                   1307:                        return -1;
                   1308:                m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
                   1309:                if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
                   1310:                        return -1;
                   1311:                if (nxtp)
                   1312:                        *nxtp = fh.ip6f_nxt;
                   1313:                off += sizeof(struct ip6_frag);
                   1314:                return off;
                   1315:
                   1316:        case IPPROTO_AH:
                   1317:                if (m->m_pkthdr.len < off + sizeof(ip6e))
                   1318:                        return -1;
                   1319:                m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
                   1320:                if (nxtp)
                   1321:                        *nxtp = ip6e.ip6e_nxt;
                   1322:                off += (ip6e.ip6e_len + 2) << 2;
                   1323:                return off;
                   1324:
                   1325:        case IPPROTO_HOPOPTS:
                   1326:        case IPPROTO_ROUTING:
                   1327:        case IPPROTO_DSTOPTS:
                   1328:                if (m->m_pkthdr.len < off + sizeof(ip6e))
                   1329:                        return -1;
                   1330:                m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
                   1331:                if (nxtp)
                   1332:                        *nxtp = ip6e.ip6e_nxt;
                   1333:                off += (ip6e.ip6e_len + 1) << 3;
                   1334:                return off;
                   1335:
                   1336:        case IPPROTO_NONE:
                   1337:        case IPPROTO_ESP:
                   1338:        case IPPROTO_IPCOMP:
                   1339:                /* give up */
                   1340:                return -1;
                   1341:
                   1342:        default:
                   1343:                return -1;
                   1344:        }
                   1345:
                   1346:        return -1;
                   1347: }
                   1348:
                   1349: /*
                   1350:  * get offset for the last header in the chain.  m will be kept untainted.
                   1351:  */
                   1352: int
                   1353: ip6_lasthdr(m, off, proto, nxtp)
                   1354:        struct mbuf *m;
                   1355:        int off;
                   1356:        int proto;
                   1357:        int *nxtp;
                   1358: {
                   1359:        int newoff;
                   1360:        int nxt;
                   1361:
                   1362:        if (!nxtp) {
                   1363:                nxt = -1;
                   1364:                nxtp = &nxt;
                   1365:        }
                   1366:        while (1) {
                   1367:                newoff = ip6_nexthdr(m, off, proto, nxtp);
                   1368:                if (newoff < 0)
                   1369:                        return off;
                   1370:                else if (newoff < off)
                   1371:                        return -1;      /* invalid */
                   1372:                else if (newoff == off)
                   1373:                        return newoff;
                   1374:
                   1375:                off = newoff;
                   1376:                proto = *nxtp;
1.2       itojun   1377:        }
                   1378: }
                   1379:
                   1380: /*
                   1381:  * System control for IP6
                   1382:  */
                   1383:
                   1384: u_char inet6ctlerrmap[PRC_NCMDS] = {
                   1385:        0,              0,              0,              0,
                   1386:        0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
                   1387:        EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
                   1388:        EMSGSIZE,       EHOSTUNREACH,   0,              0,
                   1389:        0,              0,              0,              0,
                   1390:        ENOPROTOOPT
                   1391: };
                   1392:
1.23      mrg      1393: #include <uvm/uvm_extern.h>
1.2       itojun   1394: #include <sys/sysctl.h>
                   1395:
                   1396: int
                   1397: ip6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
                   1398:        int *name;
                   1399:        u_int namelen;
                   1400:        void *oldp;
                   1401:        size_t *oldlenp;
                   1402:        void *newp;
                   1403:        size_t newlen;
                   1404: {
1.26      itojun   1405:        int old, error;
                   1406:
1.2       itojun   1407:        /* All sysctl names at this level are terminal. */
                   1408:        if (namelen != 1)
                   1409:                return ENOTDIR;
                   1410:
                   1411:        switch (name[0]) {
                   1412:
                   1413:        case IPV6CTL_FORWARDING:
                   1414:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1415:                                  &ip6_forwarding);
                   1416:        case IPV6CTL_SENDREDIRECTS:
                   1417:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1418:                                &ip6_sendredirects);
                   1419:        case IPV6CTL_DEFHLIM:
                   1420:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_defhlim);
                   1421:        case IPV6CTL_MAXFRAGPACKETS:
                   1422:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1423:                                &ip6_maxfragpackets);
                   1424:        case IPV6CTL_ACCEPT_RTADV:
                   1425:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1426:                                &ip6_accept_rtadv);
                   1427:        case IPV6CTL_KEEPFAITH:
                   1428:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_keepfaith);
                   1429:        case IPV6CTL_LOG_INTERVAL:
                   1430:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1431:                                &ip6_log_interval);
                   1432:        case IPV6CTL_HDRNESTLIMIT:
                   1433:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1434:                                &ip6_hdrnestlimit);
                   1435:        case IPV6CTL_DAD_COUNT:
                   1436:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_dad_count);
                   1437:        case IPV6CTL_AUTO_FLOWLABEL:
                   1438:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1439:                                &ip6_auto_flowlabel);
                   1440:        case IPV6CTL_DEFMCASTHLIM:
                   1441:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1442:                                &ip6_defmcasthlim);
                   1443:        case IPV6CTL_GIF_HLIM:
                   1444:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1445:                                &ip6_gif_hlim);
                   1446:        case IPV6CTL_KAME_VERSION:
                   1447:                return sysctl_rdstring(oldp, oldlenp, newp, __KAME_VERSION);
1.9       itojun   1448:        case IPV6CTL_USE_DEPRECATED:
                   1449:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1450:                                &ip6_use_deprecated);
1.10      itojun   1451:        case IPV6CTL_RR_PRUNE:
                   1452:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_rr_prune);
1.43      itojun   1453:        case IPV6CTL_V6ONLY:
                   1454: #ifdef INET6_BINDV6ONLY
                   1455:                return sysctl_rdint(oldp, oldlenp, newp, ip6_v6only);
                   1456: #else
                   1457:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_v6only);
1.26      itojun   1458: #endif
                   1459:        case IPV6CTL_ANONPORTMIN:
                   1460:                old = ip6_anonportmin;
                   1461:                error = sysctl_int(oldp, oldlenp, newp, newlen,
                   1462:                    &ip6_anonportmin);
                   1463:                if (ip6_anonportmin >= ip6_anonportmax || ip6_anonportmin < 0 ||
                   1464:                    ip6_anonportmin > 65535
                   1465: #ifndef IPNOPRIVPORTS
                   1466:                    || ip6_anonportmin < IPV6PORT_RESERVED
                   1467: #endif
                   1468:                    ) {
                   1469:                        ip6_anonportmin = old;
                   1470:                        return (EINVAL);
                   1471:                }
                   1472:                return (error);
                   1473:        case IPV6CTL_ANONPORTMAX:
                   1474:                old = ip6_anonportmax;
                   1475:                error = sysctl_int(oldp, oldlenp, newp, newlen,
                   1476:                    &ip6_anonportmax);
                   1477:                if (ip6_anonportmin >= ip6_anonportmax || ip6_anonportmax < 0 ||
                   1478:                    ip6_anonportmax > 65535
                   1479: #ifndef IPNOPRIVPORTS
                   1480:                    || ip6_anonportmax < IPV6PORT_RESERVED
                   1481: #endif
                   1482:                    ) {
                   1483:                        ip6_anonportmax = old;
                   1484:                        return (EINVAL);
                   1485:                }
                   1486:                return (error);
                   1487: #ifndef IPNOPRIVPORTS
                   1488:        case IPV6CTL_LOWPORTMIN:
                   1489:                old = ip6_lowportmin;
                   1490:                error = sysctl_int(oldp, oldlenp, newp, newlen,
                   1491:                    &ip6_lowportmin);
                   1492:                if (ip6_lowportmin >= ip6_lowportmax ||
                   1493:                    ip6_lowportmin > IPV6PORT_RESERVEDMAX ||
                   1494:                    ip6_lowportmin < IPV6PORT_RESERVEDMIN) {
                   1495:                        ip6_lowportmin = old;
                   1496:                        return (EINVAL);
                   1497:                }
                   1498:                return (error);
                   1499:        case IPV6CTL_LOWPORTMAX:
                   1500:                old = ip6_lowportmax;
                   1501:                error = sysctl_int(oldp, oldlenp, newp, newlen,
                   1502:                    &ip6_lowportmax);
                   1503:                if (ip6_lowportmin >= ip6_lowportmax ||
                   1504:                    ip6_lowportmax > IPV6PORT_RESERVEDMAX ||
                   1505:                    ip6_lowportmax < IPV6PORT_RESERVEDMIN) {
                   1506:                        ip6_lowportmax = old;
                   1507:                        return (EINVAL);
                   1508:                }
                   1509:                return (error);
1.11      itojun   1510: #endif
1.2       itojun   1511:        default:
                   1512:                return EOPNOTSUPP;
                   1513:        }
                   1514:        /* NOTREACHED */
                   1515: }

CVSweb <webmaster@jp.NetBSD.org>