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

1.61    ! itojun      1: /*     $NetBSD: ip6_input.c,v 1.60 2003/01/20 05:30:11 simonb 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:  */
1.48      lukem      67:
                     68: #include <sys/cdefs.h>
1.61    ! itojun     69: __KERNEL_RCSID(0, "$NetBSD: ip6_input.c,v 1.60 2003/01/20 05:30:11 simonb Exp $");
1.2       itojun     70:
                     71: #include "opt_inet.h"
1.4       thorpej    72: #include "opt_ipsec.h"
1.15      darrenr    73: #include "opt_pfil_hooks.h"
1.2       itojun     74:
                     75: #include <sys/param.h>
                     76: #include <sys/systm.h>
                     77: #include <sys/malloc.h>
                     78: #include <sys/mbuf.h>
                     79: #include <sys/domain.h>
                     80: #include <sys/protosw.h>
                     81: #include <sys/socket.h>
                     82: #include <sys/socketvar.h>
                     83: #include <sys/errno.h>
                     84: #include <sys/time.h>
                     85: #include <sys/kernel.h>
                     86: #include <sys/syslog.h>
                     87: #include <sys/proc.h>
1.46      simonb     88: #include <sys/sysctl.h>
1.2       itojun     89:
                     90: #include <net/if.h>
                     91: #include <net/if_types.h>
                     92: #include <net/if_dl.h>
                     93: #include <net/route.h>
                     94: #include <net/netisr.h>
1.15      darrenr    95: #ifdef PFIL_HOOKS
                     96: #include <net/pfil.h>
                     97: #endif
1.2       itojun     98:
                     99: #include <netinet/in.h>
1.9       itojun    100: #include <netinet/in_systm.h>
                    101: #ifdef INET
                    102: #include <netinet/ip.h>
                    103: #include <netinet/ip_icmp.h>
1.45      itojun    104: #endif /* INET */
1.14      itojun    105: #include <netinet/ip6.h>
1.2       itojun    106: #include <netinet6/in6_var.h>
1.11      itojun    107: #include <netinet6/ip6_var.h>
1.2       itojun    108: #include <netinet6/in6_pcb.h>
1.14      itojun    109: #include <netinet/icmp6.h>
1.2       itojun    110: #include <netinet6/in6_ifattach.h>
                    111: #include <netinet6/nd6.h>
                    112:
1.37      itojun    113: #ifdef IPSEC
                    114: #include <netinet6/ipsec.h>
                    115: #endif
                    116:
1.2       itojun    117: #include <netinet6/ip6protosw.h>
                    118:
                    119: /* we need it for NLOOP. */
                    120: #include "loop.h"
                    121: #include "faith.h"
                    122: #include "gif.h"
                    123: #include "bpfilter.h"
1.50      itojun    124:
                    125: #if NGIF > 0
                    126: #include <netinet6/in6_gif.h>
                    127: #endif
1.2       itojun    128:
1.9       itojun    129: #include <net/net_osdep.h>
                    130:
1.2       itojun    131: extern struct domain inet6domain;
                    132:
                    133: u_char ip6_protox[IPPROTO_MAX];
                    134: static int ip6qmaxlen = IFQ_MAXLEN;
                    135: struct in6_ifaddr *in6_ifaddr;
                    136: struct ifqueue ip6intrq;
                    137:
                    138: extern struct ifnet loif[NLOOP];
                    139: int ip6_forward_srcrt;                 /* XXX */
                    140: int ip6_sourcecheck;                   /* XXX */
                    141: int ip6_sourcecheck_interval;          /* XXX */
1.9       itojun    142:
1.29      thorpej   143: #ifdef PFIL_HOOKS
                    144: struct pfil_head inet6_pfil_hook;
                    145: #endif
                    146:
1.2       itojun    147: struct ip6stat ip6stat;
                    148:
                    149: static void ip6_init2 __P((void *));
                    150:
1.5       itojun    151: static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
1.2       itojun    152:
                    153: /*
                    154:  * IP6 initialization: fill in IP6 protocol switch table.
                    155:  * All protocols not implemented in kernel go to raw IP6 protocol handler.
                    156:  */
                    157: void
                    158: ip6_init()
                    159: {
1.35      itojun    160:        struct ip6protosw *pr;
                    161:        int i;
1.2       itojun    162:
                    163:        pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
                    164:        if (pr == 0)
                    165:                panic("ip6_init");
                    166:        for (i = 0; i < IPPROTO_MAX; i++)
                    167:                ip6_protox[i] = pr - inet6sw;
                    168:        for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
                    169:            pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
                    170:                if (pr->pr_domain->dom_family == PF_INET6 &&
                    171:                    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
                    172:                        ip6_protox[pr->pr_protocol] = pr - inet6sw;
                    173:        ip6intrq.ifq_maxlen = ip6qmaxlen;
                    174:        nd6_init();
                    175:        frag6_init();
1.54      itojun    176:        ip6_flow_seq = arc4random();
1.2       itojun    177:
                    178:        ip6_init2((void *)0);
1.29      thorpej   179:
                    180: #ifdef PFIL_HOOKS
                    181:        /* Register our Packet Filter hook. */
1.33      thorpej   182:        inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
                    183:        inet6_pfil_hook.ph_af   = AF_INET6;
1.29      thorpej   184:        i = pfil_head_register(&inet6_pfil_hook);
                    185:        if (i != 0)
                    186:                printf("ip6_init: WARNING: unable to register pfil hook, "
                    187:                    "error %d\n", i);
                    188: #endif /* PFIL_HOOKS */
1.2       itojun    189: }
                    190:
                    191: static void
                    192: ip6_init2(dummy)
                    193:        void *dummy;
                    194: {
                    195:
                    196:        /* nd6_timer_init */
1.19      thorpej   197:        callout_init(&nd6_timer_ch);
                    198:        callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
1.2       itojun    199: }
                    200:
                    201: /*
                    202:  * IP6 input interrupt handling. Just pass the packet to ip6_input.
                    203:  */
                    204: void
                    205: ip6intr()
                    206: {
                    207:        int s;
                    208:        struct mbuf *m;
                    209:
                    210:        for (;;) {
1.41      thorpej   211:                s = splnet();
1.2       itojun    212:                IF_DEQUEUE(&ip6intrq, m);
                    213:                splx(s);
                    214:                if (m == 0)
                    215:                        return;
                    216:                ip6_input(m);
                    217:        }
                    218: }
                    219:
                    220: extern struct  route_in6 ip6_forward_rt;
                    221:
                    222: void
                    223: ip6_input(m)
                    224:        struct mbuf *m;
                    225: {
1.9       itojun    226:        struct ip6_hdr *ip6;
1.2       itojun    227:        int off = sizeof(struct ip6_hdr), nest;
                    228:        u_int32_t plen;
1.5       itojun    229:        u_int32_t rtalert = ~0;
1.2       itojun    230:        int nxt, ours = 0;
1.9       itojun    231:        struct ifnet *deliverifp = NULL;
1.2       itojun    232:
                    233: #ifdef IPSEC
                    234:        /*
                    235:         * should the inner packet be considered authentic?
                    236:         * see comment in ah4_input().
                    237:         */
1.49      itojun    238:        m->m_flags &= ~M_AUTHIPHDR;
                    239:        m->m_flags &= ~M_AUTHIPDGM;
1.2       itojun    240: #endif
1.9       itojun    241:
1.2       itojun    242:        /*
1.44      itojun    243:         * mbuf statistics
1.2       itojun    244:         */
                    245:        if (m->m_flags & M_EXT) {
                    246:                if (m->m_next)
                    247:                        ip6stat.ip6s_mext2m++;
                    248:                else
                    249:                        ip6stat.ip6s_mext1++;
                    250:        } else {
1.40      itojun    251: #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
1.2       itojun    252:                if (m->m_next) {
1.9       itojun    253:                        if (m->m_flags & M_LOOP) {
1.44      itojun    254:                                ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
1.40      itojun    255:                        } else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
1.2       itojun    256:                                ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
                    257:                        else
                    258:                                ip6stat.ip6s_m2m[0]++;
                    259:                } else
                    260:                        ip6stat.ip6s_m1++;
1.40      itojun    261: #undef M2MMAX
1.2       itojun    262:        }
                    263:
1.9       itojun    264:        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
                    265:        ip6stat.ip6s_total++;
                    266:
1.57      thorpej   267:        /*
                    268:         * If the IPv6 header is not aligned, slurp it up into a new
                    269:         * mbuf with space for link headers, in the event we forward
                    270:         * it.  OTherwise, if it is aligned, make sure the entire base
                    271:         * IPv6 header is in the first mbuf of the chain.
                    272:         */
                    273:        if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
                    274:                struct ifnet *inifp = m->m_pkthdr.rcvif;
                    275:                if ((m = m_copyup(m, sizeof(struct ip6_hdr),
                    276:                                  (max_linkhdr + 3) & ~3)) == NULL) {
                    277:                        /* XXXJRT new stat, please */
                    278:                        ip6stat.ip6s_toosmall++;
                    279:                        in6_ifstat_inc(inifp, ifs6_in_hdrerr);
                    280:                        return;
                    281:                }
                    282:        } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
                    283:                struct ifnet *inifp = m->m_pkthdr.rcvif;
1.55      itojun    284:                if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
1.9       itojun    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)) {
1.55      itojun    340:                /*
                    341:                 * XXX: "badscope" is not very suitable for a multicast source.
                    342:                 */
1.2       itojun    343:                ip6stat.ip6s_badscope++;
1.9       itojun    344:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1.2       itojun    345:                goto bad;
                    346:        }
1.13      itojun    347:        /*
1.44      itojun    348:         * The following check is not documented in specs.  A malicious
                    349:         * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
                    350:         * and bypass security checks (act as if it was from 127.0.0.1 by using
                    351:         * IPv6 src ::ffff:127.0.0.1).  Be cautious.
1.35      itojun    352:         *
1.44      itojun    353:         * This check chokes if we are in an SIIT cloud.  As none of BSDs
                    354:         * support IPv4-less kernel compilation, we cannot support SIIT
                    355:         * environment at all.  So, it makes more sense for us to reject any
                    356:         * malicious packets for non-SIIT environment, than try to do a
1.52      wiz       357:         * partial support for SIIT environment.
1.13      itojun    358:         */
                    359:        if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
                    360:            IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
                    361:                ip6stat.ip6s_badscope++;
                    362:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
                    363:                goto bad;
                    364:        }
1.17      itojun    365: #if 0
1.13      itojun    366:        /*
1.17      itojun    367:         * Reject packets with IPv4 compatible addresses (auto tunnel).
                    368:         *
                    369:         * The code forbids auto tunnel relay case in RFC1933 (the check is
                    370:         * stronger than RFC1933).  We may want to re-enable it if mech-xx
                    371:         * is revised to forbid relaying case.
1.13      itojun    372:         */
                    373:        if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
                    374:            IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
                    375:                ip6stat.ip6s_badscope++;
                    376:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
                    377:                goto bad;
                    378:        }
                    379: #endif
1.35      itojun    380:
1.2       itojun    381:        if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
                    382:            IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
                    383:                if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
                    384:                        ours = 1;
1.9       itojun    385:                        deliverifp = m->m_pkthdr.rcvif;
1.2       itojun    386:                        goto hbhcheck;
                    387:                } else {
                    388:                        ip6stat.ip6s_badscope++;
1.9       itojun    389:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1.2       itojun    390:                        goto bad;
                    391:                }
                    392:        }
                    393:
1.38      itojun    394:        /* drop packets if interface ID portion is already filled */
                    395:        if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
                    396:                if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
                    397:                    ip6->ip6_src.s6_addr16[1]) {
                    398:                        ip6stat.ip6s_badscope++;
                    399:                        goto bad;
                    400:                }
                    401:                if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
                    402:                    ip6->ip6_dst.s6_addr16[1]) {
                    403:                        ip6stat.ip6s_badscope++;
                    404:                        goto bad;
                    405:                }
                    406:        }
1.2       itojun    407:
1.40      itojun    408:        if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
                    409:                ip6->ip6_src.s6_addr16[1]
                    410:                        = htons(m->m_pkthdr.rcvif->if_index);
                    411:        if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
                    412:                ip6->ip6_dst.s6_addr16[1]
                    413:                        = htons(m->m_pkthdr.rcvif->if_index);
                    414:
                    415:        /*
                    416:         * We use rt->rt_ifp to determine if the address is ours or not.
                    417:         * If rt_ifp is lo0, the address is ours.
                    418:         * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0,
                    419:         * so any address under fe80::%lo0/64 will be mistakenly considered
                    420:         * local.  The special case is supplied to handle the case properly
                    421:         * by actually looking at interface addresses
                    422:         * (using in6ifa_ifpwithaddr).
                    423:         */
                    424:        if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 &&
                    425:            IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
                    426:                if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) {
                    427:                        icmp6_error(m, ICMP6_DST_UNREACH,
                    428:                            ICMP6_DST_UNREACH_ADDR, 0);
                    429:                        /* m is already freed */
                    430:                        return;
1.21      itojun    431:                }
1.40      itojun    432:
                    433:                ours = 1;
                    434:                deliverifp = m->m_pkthdr.rcvif;
                    435:                goto hbhcheck;
1.9       itojun    436:        }
                    437:
1.2       itojun    438:        /*
                    439:         * Multicast check
                    440:         */
                    441:        if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
                    442:                struct  in6_multi *in6m = 0;
1.9       itojun    443:
                    444:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
1.2       itojun    445:                /*
                    446:                 * See if we belong to the destination multicast group on the
                    447:                 * arrival interface.
                    448:                 */
                    449:                IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
                    450:                if (in6m)
                    451:                        ours = 1;
                    452:                else if (!ip6_mrouter) {
                    453:                        ip6stat.ip6s_notmember++;
                    454:                        ip6stat.ip6s_cantforward++;
1.9       itojun    455:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2       itojun    456:                        goto bad;
                    457:                }
1.9       itojun    458:                deliverifp = m->m_pkthdr.rcvif;
1.2       itojun    459:                goto hbhcheck;
                    460:        }
                    461:
                    462:        /*
                    463:         *  Unicast check
                    464:         */
1.22      itojun    465:        if (ip6_forward_rt.ro_rt != NULL &&
1.55      itojun    466:            (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
1.22      itojun    467:            IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1.35      itojun    468:                               &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
1.25      itojun    469:                ip6stat.ip6s_forward_cachehit++;
1.22      itojun    470:        else {
1.35      itojun    471:                struct sockaddr_in6 *dst6;
                    472:
1.2       itojun    473:                if (ip6_forward_rt.ro_rt) {
1.22      itojun    474:                        /* route is down or destination is different */
1.25      itojun    475:                        ip6stat.ip6s_forward_cachemiss++;
1.2       itojun    476:                        RTFREE(ip6_forward_rt.ro_rt);
                    477:                        ip6_forward_rt.ro_rt = 0;
                    478:                }
1.22      itojun    479:
1.2       itojun    480:                bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
1.35      itojun    481:                dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
                    482:                dst6->sin6_len = sizeof(struct sockaddr_in6);
                    483:                dst6->sin6_family = AF_INET6;
                    484:                dst6->sin6_addr = ip6->ip6_dst;
1.2       itojun    485:
1.9       itojun    486:                rtalloc((struct route *)&ip6_forward_rt);
1.2       itojun    487:        }
                    488:
                    489: #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
                    490:
                    491:        /*
                    492:         * Accept the packet if the forwarding interface to the destination
                    493:         * according to the routing table is the loopback interface,
                    494:         * unless the associated route has a gateway.
                    495:         * Note that this approach causes to accept a packet if there is a
                    496:         * route to the loopback interface for the destination of the packet.
                    497:         * But we think it's even useful in some situations, e.g. when using
                    498:         * a special daemon which wants to intercept the packet.
                    499:         */
                    500:        if (ip6_forward_rt.ro_rt &&
                    501:            (ip6_forward_rt.ro_rt->rt_flags &
                    502:             (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
1.55      itojun    503:            !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
1.2       itojun    504: #if 0
                    505:            /*
                    506:             * The check below is redundant since the comparison of
                    507:             * the destination and the key of the rtentry has
                    508:             * already done through looking up the routing table.
                    509:             */
                    510:            IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1.55      itojun    511:            &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) &&
1.2       itojun    512: #endif
1.9       itojun    513:            ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
1.2       itojun    514:                struct in6_ifaddr *ia6 =
                    515:                        (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
                    516:                if (ia6->ia6_flags & IN6_IFF_ANYCAST)
                    517:                        m->m_flags |= M_ANYCAST6;
1.24      itojun    518:                /*
                    519:                 * packets to a tentative, duplicated, or somehow invalid
                    520:                 * address must not be accepted.
                    521:                 */
1.2       itojun    522:                if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
1.24      itojun    523:                        /* this address is ready */
1.2       itojun    524:                        ours = 1;
1.9       itojun    525:                        deliverifp = ia6->ia_ifp;       /* correct? */
1.2       itojun    526:                        goto hbhcheck;
                    527:                } else {
1.24      itojun    528:                        /* address is not ready, so discard the packet. */
1.34      itojun    529:                        nd6log((LOG_INFO,
1.27      itojun    530:                            "ip6_input: packet to an unready address %s->%s\n",
1.24      itojun    531:                            ip6_sprintf(&ip6->ip6_src),
1.34      itojun    532:                            ip6_sprintf(&ip6->ip6_dst)));
1.24      itojun    533:
                    534:                        goto bad;
1.2       itojun    535:                }
                    536:        }
                    537:
                    538:        /*
1.55      itojun    539:         * FAITH (Firewall Aided Internet Translator)
1.2       itojun    540:         */
                    541: #if defined(NFAITH) && 0 < NFAITH
                    542:        if (ip6_keepfaith) {
                    543:                if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
                    544:                 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
                    545:                        /* XXX do we need more sanity checks? */
                    546:                        ours = 1;
1.45      itojun    547:                        deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
1.9       itojun    548:                        goto hbhcheck;
                    549:                }
                    550:        }
                    551: #endif
                    552:
                    553: #if 0
                    554:     {
                    555:        /*
                    556:         * Last resort: check in6_ifaddr for incoming interface.
                    557:         * The code is here until I update the "goto ours hack" code above
                    558:         * working right.
                    559:         */
                    560:        struct ifaddr *ifa;
                    561:        for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
                    562:             ifa;
                    563:             ifa = ifa->ifa_list.tqe_next) {
                    564:                if (ifa->ifa_addr == NULL)
                    565:                        continue;       /* just for safety */
                    566:                if (ifa->ifa_addr->sa_family != AF_INET6)
                    567:                        continue;
                    568:                if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
                    569:                        ours = 1;
                    570:                        deliverifp = ifa->ifa_ifp;
1.2       itojun    571:                        goto hbhcheck;
                    572:                }
                    573:        }
1.9       itojun    574:     }
1.2       itojun    575: #endif
                    576:
                    577:        /*
                    578:         * Now there is no reason to process the packet if it's not our own
                    579:         * and we're not a router.
                    580:         */
                    581:        if (!ip6_forwarding) {
                    582:                ip6stat.ip6s_cantforward++;
1.9       itojun    583:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2       itojun    584:                goto bad;
                    585:        }
                    586:
                    587:   hbhcheck:
                    588:        /*
                    589:         * Process Hop-by-Hop options header if it's contained.
                    590:         * m may be modified in ip6_hopopts_input().
                    591:         * If a JumboPayload option is included, plen will also be modified.
                    592:         */
                    593:        plen = (u_int32_t)ntohs(ip6->ip6_plen);
                    594:        if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9       itojun    595:                struct ip6_hbh *hbh;
                    596:
                    597:                if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
                    598: #if 0  /*touches NULL pointer*/
                    599:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
                    600: #endif
1.2       itojun    601:                        return; /* m have already been freed */
1.9       itojun    602:                }
1.22      itojun    603:
1.2       itojun    604:                /* adjust pointer */
                    605:                ip6 = mtod(m, struct ip6_hdr *);
1.22      itojun    606:
                    607:                /*
1.49      itojun    608:                 * if the payload length field is 0 and the next header field
1.22      itojun    609:                 * indicates Hop-by-Hop Options header, then a Jumbo Payload
                    610:                 * option MUST be included.
                    611:                 */
                    612:                if (ip6->ip6_plen == 0 && plen == 0) {
                    613:                        /*
                    614:                         * Note that if a valid jumbo payload option is
                    615:                         * contained, ip6_hoptops_input() must set a valid
1.55      itojun    616:                         * (non-zero) payload length to the variable plen.
1.22      itojun    617:                         */
                    618:                        ip6stat.ip6s_badoptions++;
                    619:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
                    620:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
                    621:                        icmp6_error(m, ICMP6_PARAM_PROB,
                    622:                                    ICMP6_PARAMPROB_HEADER,
                    623:                                    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
                    624:                        return;
                    625:                }
1.9       itojun    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:                }
1.57      thorpej   632:                KASSERT(IP6_HDR_ALIGNED_P(hbh));
1.9       itojun    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;
1.56      itojun    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
1.56      itojun    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:        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                    776:                sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
                    777:        if (hbh == NULL) {
                    778:                ip6stat.ip6s_tooshort++;
                    779:                return -1;
                    780:        }
                    781:        hbhlen = (hbh->ip6h_len + 1) << 3;
                    782:        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
                    783:                hbhlen);
                    784:        if (hbh == NULL) {
                    785:                ip6stat.ip6s_tooshort++;
                    786:                return -1;
                    787:        }
1.57      thorpej   788:        KASSERT(IP6_HDR_ALIGNED_P(hbh));
1.2       itojun    789:        off += hbhlen;
                    790:        hbhlen -= sizeof(struct ip6_hbh);
                    791:        opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
                    792:
                    793:        if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
                    794:                                hbhlen, rtalertp, plenp) < 0)
1.58      itojun    795:                return (-1);
1.2       itojun    796:
                    797:        *offp = off;
                    798:        *mp = m;
1.58      itojun    799:        return (0);
1.2       itojun    800: }
                    801:
                    802: /*
                    803:  * Search header for all Hop-by-hop options and process each option.
                    804:  * This function is separate from ip6_hopopts_input() in order to
                    805:  * handle a case where the sending node itself process its hop-by-hop
                    806:  * options header. In such a case, the function is called from ip6_output().
1.55      itojun    807:  *
                    808:  * The function assumes that hbh header is located right after the IPv6 header
                    809:  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
                    810:  * opthead + hbhlen is located in continuous memory region.
1.2       itojun    811:  */
                    812: int
                    813: ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
                    814:        struct mbuf *m;
                    815:        u_int8_t *opthead;
                    816:        int hbhlen;
1.5       itojun    817:        u_int32_t *rtalertp;
1.2       itojun    818:        u_int32_t *plenp;
                    819: {
                    820:        struct ip6_hdr *ip6;
                    821:        int optlen = 0;
                    822:        u_int8_t *opt = opthead;
                    823:        u_int16_t rtalert_val;
1.22      itojun    824:        u_int32_t jumboplen;
1.55      itojun    825:        const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1.2       itojun    826:
                    827:        for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1.35      itojun    828:                switch (*opt) {
                    829:                case IP6OPT_PAD1:
                    830:                        optlen = 1;
                    831:                        break;
                    832:                case IP6OPT_PADN:
                    833:                        if (hbhlen < IP6OPT_MINLEN) {
                    834:                                ip6stat.ip6s_toosmall++;
                    835:                                goto bad;
                    836:                        }
                    837:                        optlen = *(opt + 1) + 2;
                    838:                        break;
                    839:                case IP6OPT_RTALERT:
                    840:                        /* XXX may need check for alignment */
                    841:                        if (hbhlen < IP6OPT_RTALERT_LEN) {
                    842:                                ip6stat.ip6s_toosmall++;
                    843:                                goto bad;
                    844:                        }
                    845:                        if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1.55      itojun    846:                                /* XXX stat */
                    847:                                icmp6_error(m, ICMP6_PARAM_PROB,
                    848:                                    ICMP6_PARAMPROB_HEADER,
                    849:                                    erroff + opt + 1 - opthead);
                    850:                                return (-1);
1.35      itojun    851:                        }
                    852:                        optlen = IP6OPT_RTALERT_LEN;
                    853:                        bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
                    854:                        *rtalertp = ntohs(rtalert_val);
                    855:                        break;
                    856:                case IP6OPT_JUMBO:
1.22      itojun    857:                        /* XXX may need check for alignment */
                    858:                        if (hbhlen < IP6OPT_JUMBO_LEN) {
                    859:                                ip6stat.ip6s_toosmall++;
                    860:                                goto bad;
                    861:                        }
1.35      itojun    862:                        if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1.55      itojun    863:                                /* XXX stat */
                    864:                                icmp6_error(m, ICMP6_PARAM_PROB,
                    865:                                    ICMP6_PARAMPROB_HEADER,
                    866:                                    erroff + opt + 1 - opthead);
                    867:                                return (-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,
1.55      itojun    879:                                    ICMP6_PARAMPROB_HEADER,
                    880:                                    erroff + opt - opthead);
                    881:                                return (-1);
1.22      itojun    882:                        }
1.2       itojun    883:
1.22      itojun    884:                        /*
                    885:                         * We may see jumbolen in unaligned location, so
                    886:                         * we'd need to perform bcopy().
                    887:                         */
                    888:                        bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
                    889:                        jumboplen = (u_int32_t)htonl(jumboplen);
                    890:
                    891: #if 1
                    892:                        /*
                    893:                         * if there are multiple jumbo payload options,
                    894:                         * *plenp will be non-zero and the packet will be
                    895:                         * rejected.
                    896:                         * the behavior may need some debate in ipngwg -
                    897:                         * multiple options does not make sense, however,
                    898:                         * there's no explicit mention in specification.
                    899:                         */
                    900:                        if (*plenp != 0) {
                    901:                                ip6stat.ip6s_badoptions++;
                    902:                                icmp6_error(m, ICMP6_PARAM_PROB,
1.55      itojun    903:                                    ICMP6_PARAMPROB_HEADER,
                    904:                                    erroff + opt + 2 - opthead);
                    905:                                return (-1);
1.22      itojun    906:                        }
1.8       itojun    907: #endif
1.2       itojun    908:
1.22      itojun    909:                        /*
                    910:                         * jumbo payload length must be larger than 65535.
                    911:                         */
                    912:                        if (jumboplen <= IPV6_MAXPACKET) {
                    913:                                ip6stat.ip6s_badoptions++;
                    914:                                icmp6_error(m, ICMP6_PARAM_PROB,
1.55      itojun    915:                                    ICMP6_PARAMPROB_HEADER,
                    916:                                    erroff + opt + 2 - opthead);
                    917:                                return (-1);
1.22      itojun    918:                        }
                    919:                        *plenp = jumboplen;
                    920:
                    921:                        break;
1.35      itojun    922:                default:                /* unknown option */
                    923:                        if (hbhlen < IP6OPT_MINLEN) {
                    924:                                ip6stat.ip6s_toosmall++;
                    925:                                goto bad;
                    926:                        }
1.55      itojun    927:                        optlen = ip6_unknown_opt(opt, m,
                    928:                            erroff + opt - opthead);
                    929:                        if (optlen == -1)
                    930:                                return (-1);
1.35      itojun    931:                        optlen += 2;
                    932:                        break;
1.2       itojun    933:                }
                    934:        }
                    935:
1.55      itojun    936:        return (0);
1.2       itojun    937:
                    938:   bad:
                    939:        m_freem(m);
1.55      itojun    940:        return (-1);
1.2       itojun    941: }
                    942:
                    943: /*
                    944:  * Unknown option processing.
                    945:  * The third argument `off' is the offset from the IPv6 header to the option,
                    946:  * which is necessary if the IPv6 header the and option header and IPv6 header
                    947:  * is not continuous in order to return an ICMPv6 error.
                    948:  */
                    949: int
                    950: ip6_unknown_opt(optp, m, off)
                    951:        u_int8_t *optp;
                    952:        struct mbuf *m;
                    953:        int off;
                    954: {
                    955:        struct ip6_hdr *ip6;
                    956:
1.35      itojun    957:        switch (IP6OPT_TYPE(*optp)) {
                    958:        case IP6OPT_TYPE_SKIP: /* ignore the option */
1.58      itojun    959:                return ((int)*(optp + 1));
1.35      itojun    960:        case IP6OPT_TYPE_DISCARD:       /* silently discard */
                    961:                m_freem(m);
1.58      itojun    962:                return (-1);
1.35      itojun    963:        case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
                    964:                ip6stat.ip6s_badoptions++;
                    965:                icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1.58      itojun    966:                return (-1);
1.35      itojun    967:        case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
                    968:                ip6stat.ip6s_badoptions++;
                    969:                ip6 = mtod(m, struct ip6_hdr *);
                    970:                if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
                    971:                    (m->m_flags & (M_BCAST|M_MCAST)))
                    972:                        m_freem(m);
                    973:                else
                    974:                        icmp6_error(m, ICMP6_PARAM_PROB,
                    975:                                    ICMP6_PARAMPROB_OPTION, off);
1.58      itojun    976:                return (-1);
1.2       itojun    977:        }
                    978:
                    979:        m_freem(m);             /* XXX: NOTREACHED */
1.58      itojun    980:        return (-1);
1.2       itojun    981: }
                    982:
                    983: /*
1.9       itojun    984:  * Create the "control" list for this pcb.
                    985:  *
                    986:  * The routine will be called from upper layer handlers like tcp6_input().
                    987:  * Thus the routine assumes that the caller (tcp6_input) have already
                    988:  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
                    989:  * very first mbuf on the mbuf chain.
                    990:  * We may want to add some infinite loop prevention or sanity checks for safety.
                    991:  * (This applies only when you are using KAME mbuf chain restriction, i.e.
                    992:  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
1.2       itojun    993:  */
                    994: void
                    995: ip6_savecontrol(in6p, mp, ip6, m)
1.35      itojun    996:        struct in6pcb *in6p;
                    997:        struct mbuf **mp;
                    998:        struct ip6_hdr *ip6;
                    999:        struct mbuf *m;
1.2       itojun   1000: {
                   1001:        struct proc *p = curproc;       /* XXX */
1.9       itojun   1002:        int privileged;
1.2       itojun   1003:
1.9       itojun   1004:        privileged = 0;
                   1005:        if (p && !suser(p->p_ucred, &p->p_acflag))
                   1006:                privileged++;
                   1007:
                   1008: #ifdef SO_TIMESTAMP
1.2       itojun   1009:        if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
                   1010:                struct timeval tv;
                   1011:
                   1012:                microtime(&tv);
                   1013:                *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
                   1014:                        SCM_TIMESTAMP, SOL_SOCKET);
                   1015:                if (*mp)
                   1016:                        mp = &(*mp)->m_next;
                   1017:        }
1.9       itojun   1018: #endif
1.2       itojun   1019:        if (in6p->in6p_flags & IN6P_RECVDSTADDR) {
                   1020:                *mp = sbcreatecontrol((caddr_t) &ip6->ip6_dst,
                   1021:                        sizeof(struct in6_addr), IPV6_RECVDSTADDR,
                   1022:                        IPPROTO_IPV6);
                   1023:                if (*mp)
                   1024:                        mp = &(*mp)->m_next;
                   1025:        }
                   1026:
                   1027: #ifdef noyet
                   1028:        /* options were tossed above */
                   1029:        if (in6p->in6p_flags & IN6P_RECVOPTS)
                   1030:                /* broken */
                   1031:        /* ip6_srcroute doesn't do what we want here, need to fix */
                   1032:        if (in6p->in6p_flags & IPV6P_RECVRETOPTS)
                   1033:                /* broken */
                   1034: #endif
                   1035:
                   1036:        /* RFC 2292 sec. 5 */
1.35      itojun   1037:        if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1.2       itojun   1038:                struct in6_pktinfo pi6;
                   1039:                bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
                   1040:                if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
                   1041:                        pi6.ipi6_addr.s6_addr16[1] = 0;
                   1042:                pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
                   1043:                                        ? m->m_pkthdr.rcvif->if_index
                   1044:                                        : 0;
                   1045:                *mp = sbcreatecontrol((caddr_t) &pi6,
                   1046:                        sizeof(struct in6_pktinfo), IPV6_PKTINFO,
                   1047:                        IPPROTO_IPV6);
                   1048:                if (*mp)
                   1049:                        mp = &(*mp)->m_next;
                   1050:        }
                   1051:        if (in6p->in6p_flags & IN6P_HOPLIMIT) {
                   1052:                int hlim = ip6->ip6_hlim & 0xff;
                   1053:                *mp = sbcreatecontrol((caddr_t) &hlim,
                   1054:                        sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
                   1055:                if (*mp)
                   1056:                        mp = &(*mp)->m_next;
                   1057:        }
                   1058:        /* IN6P_NEXTHOP - for outgoing packet only */
                   1059:
                   1060:        /*
                   1061:         * IPV6_HOPOPTS socket option. We require super-user privilege
                   1062:         * for the option, but it might be too strict, since there might
                   1063:         * be some hop-by-hop options which can be returned to normal user.
                   1064:         * See RFC 2292 section 6.
                   1065:         */
1.35      itojun   1066:        if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) {
1.2       itojun   1067:                /*
                   1068:                 * Check if a hop-by-hop options header is contatined in the
                   1069:                 * received packet, and if so, store the options as ancillary
                   1070:                 * data. Note that a hop-by-hop options header must be
                   1071:                 * just after the IPv6 header, which fact is assured through
                   1072:                 * the IPv6 input processing.
                   1073:                 */
                   1074:                struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                   1075:                if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9       itojun   1076:                        struct ip6_hbh *hbh;
                   1077:                        int hbhlen;
                   1078:
                   1079:                        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                   1080:                                sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
                   1081:                        if (hbh == NULL) {
                   1082:                                ip6stat.ip6s_tooshort++;
                   1083:                                return;
                   1084:                        }
                   1085:                        hbhlen = (hbh->ip6h_len + 1) << 3;
                   1086:                        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                   1087:                                sizeof(struct ip6_hdr), hbhlen);
                   1088:                        if (hbh == NULL) {
                   1089:                                ip6stat.ip6s_tooshort++;
                   1090:                                return;
                   1091:                        }
1.2       itojun   1092:
                   1093:                        /*
                   1094:                         * XXX: We copy whole the header even if a jumbo
                   1095:                         * payload option is included, which option is to
                   1096:                         * be removed before returning in the RFC 2292.
                   1097:                         * But it's too painful operation...
                   1098:                         */
1.9       itojun   1099:                        *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1.2       itojun   1100:                                              IPV6_HOPOPTS, IPPROTO_IPV6);
                   1101:                        if (*mp)
                   1102:                                mp = &(*mp)->m_next;
                   1103:                }
                   1104:        }
                   1105:
                   1106:        /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
                   1107:        if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
                   1108:                struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1.60      simonb   1109:                int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1.2       itojun   1110:
                   1111:                /*
                   1112:                 * Search for destination options headers or routing
                   1113:                 * header(s) through the header chain, and stores each
                   1114:                 * header as ancillary data.
                   1115:                 * Note that the order of the headers remains in
                   1116:                 * the chain of ancillary data.
                   1117:                 */
1.35      itojun   1118:                while (1) {     /* is explicit loop prevention necessary? */
1.9       itojun   1119:                        struct ip6_ext *ip6e;
                   1120:                        int elen;
                   1121:
                   1122:                        IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off,
                   1123:                                sizeof(struct ip6_ext));
                   1124:                        if (ip6e == NULL) {
                   1125:                                ip6stat.ip6s_tooshort++;
                   1126:                                return;
                   1127:                        }
                   1128:                        if (nxt == IPPROTO_AH)
                   1129:                                elen = (ip6e->ip6e_len + 2) << 2;
                   1130:                        else
                   1131:                                elen = (ip6e->ip6e_len + 1) << 3;
                   1132:                        IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off, elen);
                   1133:                        if (ip6e == NULL) {
                   1134:                                ip6stat.ip6s_tooshort++;
                   1135:                                return;
                   1136:                        }
1.57      thorpej  1137:                        KASSERT(IP6_HDR_ALIGNED_P(ip6e));
1.2       itojun   1138:
1.35      itojun   1139:                        switch (nxt) {
                   1140:                        case IPPROTO_DSTOPTS:
                   1141:                                if (!in6p->in6p_flags & IN6P_DSTOPTS)
                   1142:                                        break;
                   1143:
                   1144:                                /*
                   1145:                                 * We also require super-user privilege for
                   1146:                                 * the option.
                   1147:                                 * See the comments on IN6_HOPOPTS.
                   1148:                                 */
                   1149:                                if (!privileged)
                   1150:                                        break;
                   1151:
                   1152:                                *mp = sbcreatecontrol((caddr_t)ip6e, elen,
                   1153:                                                      IPV6_DSTOPTS,
                   1154:                                                      IPPROTO_IPV6);
                   1155:                                if (*mp)
                   1156:                                        mp = &(*mp)->m_next;
                   1157:                                break;
                   1158:
                   1159:                        case IPPROTO_ROUTING:
                   1160:                                if (!in6p->in6p_flags & IN6P_RTHDR)
                   1161:                                        break;
                   1162:
                   1163:                                *mp = sbcreatecontrol((caddr_t)ip6e, elen,
                   1164:                                                      IPV6_RTHDR,
                   1165:                                                      IPPROTO_IPV6);
                   1166:                                if (*mp)
                   1167:                                        mp = &(*mp)->m_next;
                   1168:                                break;
                   1169:
                   1170:                        case IPPROTO_UDP:
                   1171:                        case IPPROTO_TCP:
                   1172:                        case IPPROTO_ICMPV6:
                   1173:                        default:
                   1174:                                /*
                   1175:                                 * stop search if we encounter an upper
                   1176:                                 * layer protocol headers.
                   1177:                                 */
                   1178:                                goto loopend;
                   1179:
                   1180:                        case IPPROTO_HOPOPTS:
                   1181:                        case IPPROTO_AH: /* is it possible? */
                   1182:                                break;
1.2       itojun   1183:                        }
                   1184:
                   1185:                        /* proceed with the next header. */
1.9       itojun   1186:                        off += elen;
1.2       itojun   1187:                        nxt = ip6e->ip6e_nxt;
                   1188:                }
                   1189:          loopend:
1.36      cgd      1190:                ;
1.2       itojun   1191:        }
1.9       itojun   1192:        if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
1.2       itojun   1193:                /* to be done */
                   1194:        }
1.9       itojun   1195:        if ((in6p->in6p_flags & IN6P_DSTOPTS) && privileged) {
1.2       itojun   1196:                /* to be done */
                   1197:        }
                   1198:        /* IN6P_RTHDR - to be done */
                   1199:
                   1200: }
                   1201:
                   1202: /*
                   1203:  * Get pointer to the previous header followed by the header
                   1204:  * currently processed.
                   1205:  * XXX: This function supposes that
                   1206:  *     M includes all headers,
                   1207:  *     the next header field and the header length field of each header
                   1208:  *     are valid, and
                   1209:  *     the sum of each header length equals to OFF.
                   1210:  * Because of these assumptions, this function must be called very
                   1211:  * carefully. Moreover, it will not be used in the near future when
                   1212:  * we develop `neater' mechanism to process extension headers.
                   1213:  */
1.58      itojun   1214: u_int8_t *
1.2       itojun   1215: ip6_get_prevhdr(m, off)
                   1216:        struct mbuf *m;
                   1217:        int off;
                   1218: {
                   1219:        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                   1220:
                   1221:        if (off == sizeof(struct ip6_hdr))
1.58      itojun   1222:                return (&ip6->ip6_nxt);
1.2       itojun   1223:        else {
                   1224:                int len, nxt;
                   1225:                struct ip6_ext *ip6e = NULL;
                   1226:
                   1227:                nxt = ip6->ip6_nxt;
                   1228:                len = sizeof(struct ip6_hdr);
                   1229:                while (len < off) {
                   1230:                        ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
                   1231:
1.35      itojun   1232:                        switch (nxt) {
1.2       itojun   1233:                        case IPPROTO_FRAGMENT:
                   1234:                                len += sizeof(struct ip6_frag);
                   1235:                                break;
                   1236:                        case IPPROTO_AH:
                   1237:                                len += (ip6e->ip6e_len + 2) << 2;
                   1238:                                break;
                   1239:                        default:
                   1240:                                len += (ip6e->ip6e_len + 1) << 3;
                   1241:                                break;
                   1242:                        }
                   1243:                        nxt = ip6e->ip6e_nxt;
                   1244:                }
                   1245:                if (ip6e)
1.58      itojun   1246:                        return (&ip6e->ip6e_nxt);
1.2       itojun   1247:                else
                   1248:                        return NULL;
1.18      itojun   1249:        }
                   1250: }
                   1251:
                   1252: /*
                   1253:  * get next header offset.  m will be retained.
                   1254:  */
                   1255: int
                   1256: ip6_nexthdr(m, off, proto, nxtp)
                   1257:        struct mbuf *m;
                   1258:        int off;
                   1259:        int proto;
                   1260:        int *nxtp;
                   1261: {
                   1262:        struct ip6_hdr ip6;
                   1263:        struct ip6_ext ip6e;
                   1264:        struct ip6_frag fh;
                   1265:
                   1266:        /* just in case */
                   1267:        if (m == NULL)
                   1268:                panic("ip6_nexthdr: m == NULL");
                   1269:        if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
                   1270:                return -1;
                   1271:
                   1272:        switch (proto) {
                   1273:        case IPPROTO_IPV6:
                   1274:                if (m->m_pkthdr.len < off + sizeof(ip6))
                   1275:                        return -1;
                   1276:                m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
                   1277:                if (nxtp)
                   1278:                        *nxtp = ip6.ip6_nxt;
                   1279:                off += sizeof(ip6);
                   1280:                return off;
                   1281:
                   1282:        case IPPROTO_FRAGMENT:
                   1283:                /*
                   1284:                 * terminate parsing if it is not the first fragment,
                   1285:                 * it does not make sense to parse through it.
                   1286:                 */
                   1287:                if (m->m_pkthdr.len < off + sizeof(fh))
                   1288:                        return -1;
                   1289:                m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
                   1290:                if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
                   1291:                        return -1;
                   1292:                if (nxtp)
                   1293:                        *nxtp = fh.ip6f_nxt;
                   1294:                off += sizeof(struct ip6_frag);
                   1295:                return off;
                   1296:
                   1297:        case IPPROTO_AH:
                   1298:                if (m->m_pkthdr.len < off + sizeof(ip6e))
                   1299:                        return -1;
                   1300:                m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
                   1301:                if (nxtp)
                   1302:                        *nxtp = ip6e.ip6e_nxt;
                   1303:                off += (ip6e.ip6e_len + 2) << 2;
1.47      itojun   1304:                if (m->m_pkthdr.len < off)
                   1305:                        return -1;
1.18      itojun   1306:                return off;
                   1307:
                   1308:        case IPPROTO_HOPOPTS:
                   1309:        case IPPROTO_ROUTING:
                   1310:        case IPPROTO_DSTOPTS:
                   1311:                if (m->m_pkthdr.len < off + sizeof(ip6e))
                   1312:                        return -1;
                   1313:                m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
                   1314:                if (nxtp)
                   1315:                        *nxtp = ip6e.ip6e_nxt;
                   1316:                off += (ip6e.ip6e_len + 1) << 3;
1.47      itojun   1317:                if (m->m_pkthdr.len < off)
                   1318:                        return -1;
1.18      itojun   1319:                return off;
                   1320:
                   1321:        case IPPROTO_NONE:
                   1322:        case IPPROTO_ESP:
                   1323:        case IPPROTO_IPCOMP:
                   1324:                /* give up */
                   1325:                return -1;
                   1326:
                   1327:        default:
                   1328:                return -1;
                   1329:        }
                   1330: }
                   1331:
                   1332: /*
                   1333:  * get offset for the last header in the chain.  m will be kept untainted.
                   1334:  */
                   1335: int
                   1336: ip6_lasthdr(m, off, proto, nxtp)
                   1337:        struct mbuf *m;
                   1338:        int off;
                   1339:        int proto;
                   1340:        int *nxtp;
                   1341: {
                   1342:        int newoff;
                   1343:        int nxt;
                   1344:
                   1345:        if (!nxtp) {
                   1346:                nxt = -1;
                   1347:                nxtp = &nxt;
                   1348:        }
                   1349:        while (1) {
                   1350:                newoff = ip6_nexthdr(m, off, proto, nxtp);
                   1351:                if (newoff < 0)
                   1352:                        return off;
                   1353:                else if (newoff < off)
                   1354:                        return -1;      /* invalid */
                   1355:                else if (newoff == off)
                   1356:                        return newoff;
                   1357:
                   1358:                off = newoff;
                   1359:                proto = *nxtp;
1.2       itojun   1360:        }
                   1361: }
                   1362:
                   1363: /*
                   1364:  * System control for IP6
                   1365:  */
                   1366:
                   1367: u_char inet6ctlerrmap[PRC_NCMDS] = {
                   1368:        0,              0,              0,              0,
                   1369:        0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
                   1370:        EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
                   1371:        EMSGSIZE,       EHOSTUNREACH,   0,              0,
                   1372:        0,              0,              0,              0,
                   1373:        ENOPROTOOPT
                   1374: };
                   1375:
                   1376: int
                   1377: ip6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
                   1378:        int *name;
                   1379:        u_int namelen;
                   1380:        void *oldp;
                   1381:        size_t *oldlenp;
                   1382:        void *newp;
                   1383:        size_t newlen;
                   1384: {
1.26      itojun   1385:        int old, error;
                   1386:
1.2       itojun   1387:        /* All sysctl names at this level are terminal. */
                   1388:        if (namelen != 1)
                   1389:                return ENOTDIR;
                   1390:
                   1391:        switch (name[0]) {
                   1392:
                   1393:        case IPV6CTL_FORWARDING:
                   1394:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1395:                                  &ip6_forwarding);
                   1396:        case IPV6CTL_SENDREDIRECTS:
                   1397:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1398:                                &ip6_sendredirects);
                   1399:        case IPV6CTL_DEFHLIM:
                   1400:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_defhlim);
                   1401:        case IPV6CTL_MAXFRAGPACKETS:
                   1402:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1403:                                &ip6_maxfragpackets);
                   1404:        case IPV6CTL_ACCEPT_RTADV:
                   1405:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1406:                                &ip6_accept_rtadv);
                   1407:        case IPV6CTL_KEEPFAITH:
                   1408:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_keepfaith);
                   1409:        case IPV6CTL_LOG_INTERVAL:
                   1410:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1411:                                &ip6_log_interval);
                   1412:        case IPV6CTL_HDRNESTLIMIT:
                   1413:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1414:                                &ip6_hdrnestlimit);
                   1415:        case IPV6CTL_DAD_COUNT:
                   1416:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_dad_count);
                   1417:        case IPV6CTL_AUTO_FLOWLABEL:
                   1418:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1419:                                &ip6_auto_flowlabel);
                   1420:        case IPV6CTL_DEFMCASTHLIM:
                   1421:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1422:                                &ip6_defmcasthlim);
1.51      itojun   1423: #if NGIF > 0
1.2       itojun   1424:        case IPV6CTL_GIF_HLIM:
                   1425:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1426:                                &ip6_gif_hlim);
1.51      itojun   1427: #endif
1.2       itojun   1428:        case IPV6CTL_KAME_VERSION:
                   1429:                return sysctl_rdstring(oldp, oldlenp, newp, __KAME_VERSION);
1.9       itojun   1430:        case IPV6CTL_USE_DEPRECATED:
                   1431:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1432:                                &ip6_use_deprecated);
1.10      itojun   1433:        case IPV6CTL_RR_PRUNE:
                   1434:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_rr_prune);
1.43      itojun   1435:        case IPV6CTL_V6ONLY:
                   1436: #ifdef INET6_BINDV6ONLY
                   1437:                return sysctl_rdint(oldp, oldlenp, newp, ip6_v6only);
                   1438: #else
                   1439:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_v6only);
1.26      itojun   1440: #endif
                   1441:        case IPV6CTL_ANONPORTMIN:
                   1442:                old = ip6_anonportmin;
                   1443:                error = sysctl_int(oldp, oldlenp, newp, newlen,
                   1444:                    &ip6_anonportmin);
                   1445:                if (ip6_anonportmin >= ip6_anonportmax || ip6_anonportmin < 0 ||
                   1446:                    ip6_anonportmin > 65535
                   1447: #ifndef IPNOPRIVPORTS
                   1448:                    || ip6_anonportmin < IPV6PORT_RESERVED
                   1449: #endif
                   1450:                    ) {
                   1451:                        ip6_anonportmin = old;
                   1452:                        return (EINVAL);
                   1453:                }
                   1454:                return (error);
                   1455:        case IPV6CTL_ANONPORTMAX:
                   1456:                old = ip6_anonportmax;
                   1457:                error = sysctl_int(oldp, oldlenp, newp, newlen,
                   1458:                    &ip6_anonportmax);
                   1459:                if (ip6_anonportmin >= ip6_anonportmax || ip6_anonportmax < 0 ||
                   1460:                    ip6_anonportmax > 65535
                   1461: #ifndef IPNOPRIVPORTS
                   1462:                    || ip6_anonportmax < IPV6PORT_RESERVED
                   1463: #endif
                   1464:                    ) {
                   1465:                        ip6_anonportmax = old;
                   1466:                        return (EINVAL);
                   1467:                }
                   1468:                return (error);
                   1469: #ifndef IPNOPRIVPORTS
                   1470:        case IPV6CTL_LOWPORTMIN:
                   1471:                old = ip6_lowportmin;
                   1472:                error = sysctl_int(oldp, oldlenp, newp, newlen,
                   1473:                    &ip6_lowportmin);
                   1474:                if (ip6_lowportmin >= ip6_lowportmax ||
                   1475:                    ip6_lowportmin > IPV6PORT_RESERVEDMAX ||
                   1476:                    ip6_lowportmin < IPV6PORT_RESERVEDMIN) {
                   1477:                        ip6_lowportmin = old;
                   1478:                        return (EINVAL);
                   1479:                }
                   1480:                return (error);
                   1481:        case IPV6CTL_LOWPORTMAX:
                   1482:                old = ip6_lowportmax;
                   1483:                error = sysctl_int(oldp, oldlenp, newp, newlen,
                   1484:                    &ip6_lowportmax);
                   1485:                if (ip6_lowportmin >= ip6_lowportmax ||
                   1486:                    ip6_lowportmax > IPV6PORT_RESERVEDMAX ||
                   1487:                    ip6_lowportmax < IPV6PORT_RESERVEDMIN) {
                   1488:                        ip6_lowportmax = old;
                   1489:                        return (EINVAL);
                   1490:                }
                   1491:                return (error);
1.11      itojun   1492: #endif
1.53      itojun   1493:        case IPV6CTL_MAXFRAGS:
                   1494:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_maxfrags);
1.2       itojun   1495:        default:
                   1496:                return EOPNOTSUPP;
                   1497:        }
                   1498:        /* NOTREACHED */
                   1499: }

CVSweb <webmaster@jp.NetBSD.org>