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

1.113.8.2! keiichi     1: /*     $NetBSD: ip6_input.c,v 1.114 2008/02/27 19:40:56 matt 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.
1.65      agc        45:  * 3. Neither the name of the University nor the names of its contributors
1.2       itojun     46:  *    may be used to endorse or promote products derived from this software
                     47:  *    without specific prior written permission.
                     48:  *
                     49:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     50:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     51:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     52:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     53:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     54:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     55:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     56:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     57:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     58:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     59:  * SUCH DAMAGE.
                     60:  *
                     61:  *     @(#)ip_input.c  8.2 (Berkeley) 1/4/94
                     62:  */
1.48      lukem      63:
                     64: #include <sys/cdefs.h>
1.113.8.2! keiichi    65: __KERNEL_RCSID(0, "$NetBSD: ip6_input.c,v 1.114 2008/02/27 19:40:56 matt Exp $");
1.2       itojun     66:
                     67: #include "opt_inet.h"
1.85      rpaulo     68: #include "opt_inet6.h"
1.4       thorpej    69: #include "opt_ipsec.h"
1.15      darrenr    70: #include "opt_pfil_hooks.h"
1.2       itojun     71:
                     72: #include <sys/param.h>
                     73: #include <sys/systm.h>
                     74: #include <sys/malloc.h>
                     75: #include <sys/mbuf.h>
                     76: #include <sys/domain.h>
                     77: #include <sys/protosw.h>
                     78: #include <sys/socket.h>
                     79: #include <sys/socketvar.h>
                     80: #include <sys/errno.h>
                     81: #include <sys/time.h>
                     82: #include <sys/kernel.h>
                     83: #include <sys/syslog.h>
                     84: #include <sys/proc.h>
1.46      simonb     85: #include <sys/sysctl.h>
1.2       itojun     86:
                     87: #include <net/if.h>
                     88: #include <net/if_types.h>
                     89: #include <net/if_dl.h>
                     90: #include <net/route.h>
                     91: #include <net/netisr.h>
1.15      darrenr    92: #ifdef PFIL_HOOKS
                     93: #include <net/pfil.h>
                     94: #endif
1.2       itojun     95:
                     96: #include <netinet/in.h>
1.9       itojun     97: #include <netinet/in_systm.h>
                     98: #ifdef INET
                     99: #include <netinet/ip.h>
                    100: #include <netinet/ip_icmp.h>
1.45      itojun    101: #endif /* INET */
1.14      itojun    102: #include <netinet/ip6.h>
1.2       itojun    103: #include <netinet6/in6_var.h>
1.11      itojun    104: #include <netinet6/ip6_var.h>
1.2       itojun    105: #include <netinet6/in6_pcb.h>
1.14      itojun    106: #include <netinet/icmp6.h>
1.81      rpaulo    107: #include <netinet6/scope6_var.h>
1.2       itojun    108: #include <netinet6/in6_ifattach.h>
                    109: #include <netinet6/nd6.h>
                    110:
1.37      itojun    111: #ifdef IPSEC
                    112: #include <netinet6/ipsec.h>
                    113: #endif
                    114:
1.94      degroote  115: #ifdef FAST_IPSEC
                    116: #include <netipsec/ipsec.h>
                    117: #include <netipsec/ipsec6.h>
                    118: #include <netipsec/key.h>
                    119: #endif /* FAST_IPSEC */
                    120:
1.2       itojun    121: #include <netinet6/ip6protosw.h>
                    122:
                    123: #include "faith.h"
                    124: #include "gif.h"
1.50      itojun    125:
                    126: #if NGIF > 0
                    127: #include <netinet6/in6_gif.h>
                    128: #endif
1.2       itojun    129:
1.9       itojun    130: #include <net/net_osdep.h>
                    131:
1.2       itojun    132: extern struct domain inet6domain;
                    133:
                    134: u_char ip6_protox[IPPROTO_MAX];
                    135: static int ip6qmaxlen = IFQ_MAXLEN;
                    136: struct in6_ifaddr *in6_ifaddr;
                    137: struct ifqueue ip6intrq;
                    138:
1.108     ad        139: extern callout_t in6_tmpaddrtimer_ch;
1.83      rpaulo    140:
1.2       itojun    141: int ip6_forward_srcrt;                 /* XXX */
                    142: int ip6_sourcecheck;                   /* XXX */
                    143: int ip6_sourcecheck_interval;          /* XXX */
1.9       itojun    144:
1.29      thorpej   145: #ifdef PFIL_HOOKS
                    146: struct pfil_head inet6_pfil_hook;
                    147: #endif
                    148:
1.2       itojun    149: struct ip6stat ip6stat;
                    150:
1.111     dyoung    151: static void ip6_init2(void *);
1.112     dyoung    152: static struct m_tag *ip6_setdstifaddr(struct mbuf *, const struct in6_ifaddr *);
1.2       itojun    153:
1.111     dyoung    154: static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
                    155: static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
1.2       itojun    156:
                    157: /*
                    158:  * IP6 initialization: fill in IP6 protocol switch table.
                    159:  * All protocols not implemented in kernel go to raw IP6 protocol handler.
                    160:  */
                    161: void
1.113.8.2! keiichi   162: ip6_init(void)
1.2       itojun    163: {
1.78      christos  164:        const struct ip6protosw *pr;
1.35      itojun    165:        int i;
1.2       itojun    166:
1.78      christos  167:        pr = (const struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
1.2       itojun    168:        if (pr == 0)
                    169:                panic("ip6_init");
                    170:        for (i = 0; i < IPPROTO_MAX; i++)
                    171:                ip6_protox[i] = pr - inet6sw;
1.78      christos  172:        for (pr = (const struct ip6protosw *)inet6domain.dom_protosw;
                    173:            pr < (const struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
1.2       itojun    174:                if (pr->pr_domain->dom_family == PF_INET6 &&
                    175:                    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
                    176:                        ip6_protox[pr->pr_protocol] = pr - inet6sw;
                    177:        ip6intrq.ifq_maxlen = ip6qmaxlen;
1.81      rpaulo    178:        scope6_init();
                    179:        addrsel_policy_init();
1.2       itojun    180:        nd6_init();
                    181:        frag6_init();
1.83      rpaulo    182:        ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
1.2       itojun    183:
                    184:        ip6_init2((void *)0);
1.98      liamjfoy  185: #ifdef GATEWAY
1.99      liamjfoy  186:        ip6flow_init(ip6_hashsize);
1.98      liamjfoy  187: #endif
1.29      thorpej   188:
                    189: #ifdef PFIL_HOOKS
                    190:        /* Register our Packet Filter hook. */
1.33      thorpej   191:        inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
                    192:        inet6_pfil_hook.ph_af   = AF_INET6;
1.29      thorpej   193:        i = pfil_head_register(&inet6_pfil_hook);
                    194:        if (i != 0)
                    195:                printf("ip6_init: WARNING: unable to register pfil hook, "
                    196:                    "error %d\n", i);
                    197: #endif /* PFIL_HOOKS */
1.2       itojun    198: }
                    199:
                    200: static void
1.90      christos  201: ip6_init2(void *dummy)
1.2       itojun    202: {
                    203:
                    204:        /* nd6_timer_init */
1.108     ad        205:        callout_init(&nd6_timer_ch, 0);
1.19      thorpej   206:        callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
1.83      rpaulo    207:
                    208:        /* timer for regeneranation of temporary addresses randomize ID */
1.108     ad        209:        callout_init(&in6_tmpaddrtimer_ch, 0);
1.83      rpaulo    210:        callout_reset(&in6_tmpaddrtimer_ch,
                    211:                      (ip6_temp_preferred_lifetime - ip6_desync_factor -
                    212:                       ip6_temp_regen_advance) * hz,
                    213:                      in6_tmpaddrtimer, NULL);
1.2       itojun    214: }
                    215:
                    216: /*
                    217:  * IP6 input interrupt handling. Just pass the packet to ip6_input.
                    218:  */
                    219: void
1.113.8.2! keiichi   220: ip6intr(void)
1.2       itojun    221: {
                    222:        int s;
                    223:        struct mbuf *m;
                    224:
                    225:        for (;;) {
1.41      thorpej   226:                s = splnet();
1.2       itojun    227:                IF_DEQUEUE(&ip6intrq, m);
                    228:                splx(s);
                    229:                if (m == 0)
                    230:                        return;
1.83      rpaulo    231:                /* drop the packet if IPv6 operation is disabled on the IF */
                    232:                if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED)) {
                    233:                        m_freem(m);
                    234:                        return;
                    235:                }
1.2       itojun    236:                ip6_input(m);
                    237:        }
                    238: }
                    239:
1.103     dyoung    240: extern struct  route ip6_forward_rt;
1.2       itojun    241:
                    242: void
1.96      dyoung    243: ip6_input(struct mbuf *m)
1.2       itojun    244: {
1.9       itojun    245:        struct ip6_hdr *ip6;
1.105     dyoung    246:        int hit, off = sizeof(struct ip6_hdr), nest;
1.2       itojun    247:        u_int32_t plen;
1.5       itojun    248:        u_int32_t rtalert = ~0;
1.104     yamt      249:        int nxt, ours = 0, rh_present = 0;
1.9       itojun    250:        struct ifnet *deliverifp = NULL;
1.64      itojun    251:        int srcrt = 0;
1.105     dyoung    252:        const struct rtentry *rt;
                    253:        union {
                    254:                struct sockaddr         dst;
                    255:                struct sockaddr_in6     dst6;
                    256:        } u;
1.94      degroote  257: #ifdef FAST_IPSEC
                    258:        struct m_tag *mtag;
                    259:        struct tdb_ident *tdbi;
                    260:        struct secpolicy *sp;
                    261:        int s, error;
                    262: #endif
1.2       itojun    263:
                    264: #ifdef IPSEC
                    265:        /*
                    266:         * should the inner packet be considered authentic?
                    267:         * see comment in ah4_input().
                    268:         */
1.49      itojun    269:        m->m_flags &= ~M_AUTHIPHDR;
                    270:        m->m_flags &= ~M_AUTHIPDGM;
1.2       itojun    271: #endif
1.9       itojun    272:
1.2       itojun    273:        /*
1.81      rpaulo    274:         * make sure we don't have onion peering information into m_tag.
                    275:         */
                    276:        ip6_delaux(m);
                    277:
                    278:        /*
1.44      itojun    279:         * mbuf statistics
1.2       itojun    280:         */
                    281:        if (m->m_flags & M_EXT) {
                    282:                if (m->m_next)
                    283:                        ip6stat.ip6s_mext2m++;
                    284:                else
                    285:                        ip6stat.ip6s_mext1++;
                    286:        } else {
1.96      dyoung    287: #define M2MMAX __arraycount(ip6stat.ip6s_m2m)
1.2       itojun    288:                if (m->m_next) {
1.9       itojun    289:                        if (m->m_flags & M_LOOP) {
1.77      peter     290:                                ip6stat.ip6s_m2m[lo0ifp->if_index]++; /* XXX */
1.40      itojun    291:                        } else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
1.2       itojun    292:                                ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
                    293:                        else
                    294:                                ip6stat.ip6s_m2m[0]++;
                    295:                } else
                    296:                        ip6stat.ip6s_m1++;
1.40      itojun    297: #undef M2MMAX
1.2       itojun    298:        }
                    299:
1.9       itojun    300:        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
                    301:        ip6stat.ip6s_total++;
                    302:
1.57      thorpej   303:        /*
                    304:         * If the IPv6 header is not aligned, slurp it up into a new
                    305:         * mbuf with space for link headers, in the event we forward
1.81      rpaulo    306:         * it.  Otherwise, if it is aligned, make sure the entire base
1.57      thorpej   307:         * IPv6 header is in the first mbuf of the chain.
                    308:         */
1.97      christos  309:        if (IP6_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
1.57      thorpej   310:                struct ifnet *inifp = m->m_pkthdr.rcvif;
                    311:                if ((m = m_copyup(m, sizeof(struct ip6_hdr),
                    312:                                  (max_linkhdr + 3) & ~3)) == NULL) {
                    313:                        /* XXXJRT new stat, please */
                    314:                        ip6stat.ip6s_toosmall++;
                    315:                        in6_ifstat_inc(inifp, ifs6_in_hdrerr);
                    316:                        return;
                    317:                }
                    318:        } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
                    319:                struct ifnet *inifp = m->m_pkthdr.rcvif;
1.55      itojun    320:                if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
1.9       itojun    321:                        ip6stat.ip6s_toosmall++;
                    322:                        in6_ifstat_inc(inifp, ifs6_in_hdrerr);
                    323:                        return;
                    324:                }
1.2       itojun    325:        }
                    326:
                    327:        ip6 = mtod(m, struct ip6_hdr *);
                    328:
                    329:        if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
                    330:                ip6stat.ip6s_badvers++;
1.9       itojun    331:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
1.2       itojun    332:                goto bad;
                    333:        }
1.15      darrenr   334:
1.98      liamjfoy  335: #if defined(IPSEC)
                    336:        /* IPv6 fast forwarding is not compatible with IPsec. */
                    337:        m->m_flags &= ~M_CANFASTFWD;
                    338: #else
                    339:        /*
                    340:         * Assume that we can create a fast-forward IP flow entry
                    341:         * based on this packet.
                    342:         */
                    343:        m->m_flags |= M_CANFASTFWD;
                    344: #endif
                    345:
1.15      darrenr   346: #ifdef PFIL_HOOKS
                    347:        /*
                    348:         * Run through list of hooks for input packets.  If there are any
                    349:         * filters which require that additional packets in the flow are
                    350:         * not fast-forwarded, they must clear the M_CANFASTFWD flag.
                    351:         * Note that filters must _never_ set this flag, as another filter
                    352:         * in the list may have previously cleared it.
                    353:         */
1.39      itojun    354:        /*
                    355:         * let ipfilter look at packet on the wire,
                    356:         * not the decapsulated packet.
                    357:         */
                    358: #ifdef IPSEC
1.42      itojun    359:        if (!ipsec_getnhist(m))
1.94      degroote  360: #elif defined(FAST_IPSEC)
                    361:        if (!ipsec_indone(m))
1.39      itojun    362: #else
                    363:        if (1)
                    364: #endif
                    365:        {
1.64      itojun    366:                struct in6_addr odst;
                    367:
                    368:                odst = ip6->ip6_dst;
1.39      itojun    369:                if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif,
                    370:                                   PFIL_IN) != 0)
                    371:                        return;
                    372:                if (m == NULL)
                    373:                        return;
                    374:                ip6 = mtod(m, struct ip6_hdr *);
1.64      itojun    375:                srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
1.39      itojun    376:        }
1.15      darrenr   377: #endif /* PFIL_HOOKS */
                    378:
1.2       itojun    379:        ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
                    380:
1.30      thorpej   381: #ifdef ALTQ
                    382:        if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
                    383:                /* packet is dropped by traffic conditioner */
                    384:                return;
1.9       itojun    385:        }
                    386: #endif
                    387:
1.2       itojun    388:        /*
1.44      itojun    389:         * Check against address spoofing/corruption.
1.2       itojun    390:         */
                    391:        if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
                    392:            IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
1.55      itojun    393:                /*
                    394:                 * XXX: "badscope" is not very suitable for a multicast source.
                    395:                 */
1.2       itojun    396:                ip6stat.ip6s_badscope++;
1.9       itojun    397:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1.2       itojun    398:                goto bad;
                    399:        }
1.13      itojun    400:        /*
1.44      itojun    401:         * The following check is not documented in specs.  A malicious
                    402:         * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
                    403:         * and bypass security checks (act as if it was from 127.0.0.1 by using
                    404:         * IPv6 src ::ffff:127.0.0.1).  Be cautious.
1.35      itojun    405:         *
1.44      itojun    406:         * This check chokes if we are in an SIIT cloud.  As none of BSDs
                    407:         * support IPv4-less kernel compilation, we cannot support SIIT
                    408:         * environment at all.  So, it makes more sense for us to reject any
                    409:         * malicious packets for non-SIIT environment, than try to do a
1.52      wiz       410:         * partial support for SIIT environment.
1.13      itojun    411:         */
                    412:        if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
                    413:            IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
                    414:                ip6stat.ip6s_badscope++;
                    415:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
                    416:                goto bad;
                    417:        }
1.17      itojun    418: #if 0
1.13      itojun    419:        /*
1.17      itojun    420:         * Reject packets with IPv4 compatible addresses (auto tunnel).
                    421:         *
                    422:         * The code forbids auto tunnel relay case in RFC1933 (the check is
                    423:         * stronger than RFC1933).  We may want to re-enable it if mech-xx
                    424:         * is revised to forbid relaying case.
1.13      itojun    425:         */
                    426:        if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
                    427:            IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
                    428:                ip6stat.ip6s_badscope++;
                    429:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
                    430:                goto bad;
                    431:        }
                    432: #endif
1.35      itojun    433:
1.2       itojun    434:        /*
1.82      yamt      435:         * Disambiguate address scope zones (if there is ambiguity).
                    436:         * We first make sure that the original source or destination address
                    437:         * is not in our internal form for scoped addresses.  Such addresses
                    438:         * are not necessarily invalid spec-wise, but we cannot accept them due
                    439:         * to the usage conflict.
                    440:         * in6_setscope() then also checks and rejects the cases where src or
                    441:         * dst are the loopback address and the receiving interface
                    442:         * is not loopback.
                    443:         */
1.88      bouyer    444:        if (__predict_false(
                    445:            m_makewritable(&m, 0, sizeof(struct ip6_hdr), M_DONTWAIT)))
                    446:                goto bad;
                    447:        ip6 = mtod(m, struct ip6_hdr *);
1.82      yamt      448:        if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
                    449:                ip6stat.ip6s_badscope++; /* XXX */
                    450:                goto bad;
                    451:        }
                    452:        if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) ||
                    453:            in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) {
                    454:                ip6stat.ip6s_badscope++;
                    455:                goto bad;
                    456:        }
                    457:
                    458:        /*
1.2       itojun    459:         * Multicast check
                    460:         */
                    461:        if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
                    462:                struct  in6_multi *in6m = 0;
1.9       itojun    463:
                    464:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
1.2       itojun    465:                /*
                    466:                 * See if we belong to the destination multicast group on the
                    467:                 * arrival interface.
                    468:                 */
                    469:                IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
                    470:                if (in6m)
                    471:                        ours = 1;
                    472:                else if (!ip6_mrouter) {
                    473:                        ip6stat.ip6s_notmember++;
                    474:                        ip6stat.ip6s_cantforward++;
1.9       itojun    475:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2       itojun    476:                        goto bad;
                    477:                }
1.9       itojun    478:                deliverifp = m->m_pkthdr.rcvif;
1.2       itojun    479:                goto hbhcheck;
                    480:        }
                    481:
1.105     dyoung    482:        sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
                    483:
1.2       itojun    484:        /*
                    485:         *  Unicast check
                    486:         */
1.105     dyoung    487:        rt = rtcache_lookup2(&ip6_forward_rt, &u.dst, 1, &hit);
                    488:        if (hit)
                    489:                ip6stat.ip6s_forward_cachehit++;
1.93      joerg     490:        else
                    491:                ip6stat.ip6s_forward_cachemiss++;
1.22      itojun    492:
1.109     dyoung    493: #define rt6_getkey(__rt) satocsin6(rt_getkey(__rt))
1.2       itojun    494:
                    495:        /*
                    496:         * Accept the packet if the forwarding interface to the destination
                    497:         * according to the routing table is the loopback interface,
                    498:         * unless the associated route has a gateway.
                    499:         * Note that this approach causes to accept a packet if there is a
                    500:         * route to the loopback interface for the destination of the packet.
                    501:         * But we think it's even useful in some situations, e.g. when using
                    502:         * a special daemon which wants to intercept the packet.
                    503:         */
1.105     dyoung    504:        if (rt != NULL &&
                    505:            (rt->rt_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
                    506:            !(rt->rt_flags & RTF_CLONED) &&
1.2       itojun    507: #if 0
                    508:            /*
                    509:             * The check below is redundant since the comparison of
                    510:             * the destination and the key of the rtentry has
                    511:             * already done through looking up the routing table.
                    512:             */
1.109     dyoung    513:            IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &rt6_getkey(rt)->sin6_addr) &&
1.2       itojun    514: #endif
1.113.8.1  keiichi   515: #ifdef MOBILE_IPV6
                    516:            ((rt->rt_flags & RTF_ANNOUNCE) ||
                    517:            rt->rt_ifp->if_type == IFT_LOOP)
                    518: #else
                    519:            rt->rt_ifp->if_type == IFT_LOOP
                    520: #endif /* MOBILE_IPV6 */
                    521:            ) {
1.105     dyoung    522:                struct in6_ifaddr *ia6 = (struct in6_ifaddr *)rt->rt_ifa;
1.2       itojun    523:                if (ia6->ia6_flags & IN6_IFF_ANYCAST)
                    524:                        m->m_flags |= M_ANYCAST6;
1.113.8.1  keiichi   525: #ifdef MOBILE_IPV6
                    526:                /* check unicast NS */
                    527:                if ((rtcache_validate(&ip6_forward_rt)->rt_flags & RTF_ANNOUNCE) != 0) {
                    528:                        /* This route shows proxy nd. thus the packet was
                    529:                         *  captured. this packet should be tunneled to
                    530:                         * actual coa with tunneling unless this is NS.
                    531:                         */
                    532:                        int nh, loff;
                    533:                        struct icmp6_hdr *icp;
                    534:                        loff = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nh);
                    535:                        if (loff <  0 || nh != IPPROTO_ICMPV6)
                    536:                                goto mip6_forwarding;
                    537:                        IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, loff,
                    538:                            sizeof(*icp));
                    539:                        if (icp == NULL) {
                    540:                                icmp6stat.icp6s_tooshort++;
                    541:                                return;
                    542:                        }
                    543:                        if (icp->icmp6_type != ND_NEIGHBOR_SOLICIT)
                    544:                                goto mip6_forwarding;
                    545:                }
                    546: #endif /* MOBILE_IPV6 */
1.24      itojun    547:                /*
                    548:                 * packets to a tentative, duplicated, or somehow invalid
                    549:                 * address must not be accepted.
                    550:                 */
1.2       itojun    551:                if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
1.24      itojun    552:                        /* this address is ready */
1.2       itojun    553:                        ours = 1;
1.9       itojun    554:                        deliverifp = ia6->ia_ifp;       /* correct? */
1.2       itojun    555:                        goto hbhcheck;
                    556:                } else {
1.24      itojun    557:                        /* address is not ready, so discard the packet. */
1.34      itojun    558:                        nd6log((LOG_INFO,
1.27      itojun    559:                            "ip6_input: packet to an unready address %s->%s\n",
1.24      itojun    560:                            ip6_sprintf(&ip6->ip6_src),
1.34      itojun    561:                            ip6_sprintf(&ip6->ip6_dst)));
1.24      itojun    562:
                    563:                        goto bad;
1.2       itojun    564:                }
                    565:        }
                    566:
1.113.8.1  keiichi   567: #ifdef MOBILE_IPV6
                    568:  mip6_forwarding:
                    569: #endif /* MOBILE_IPV6 */
                    570:
1.2       itojun    571:        /*
1.55      itojun    572:         * FAITH (Firewall Aided Internet Translator)
1.2       itojun    573:         */
                    574: #if defined(NFAITH) && 0 < NFAITH
                    575:        if (ip6_keepfaith) {
1.105     dyoung    576:                if (rt != NULL && rt->rt_ifp != NULL &&
                    577:                    rt->rt_ifp->if_type == IFT_FAITH) {
1.2       itojun    578:                        /* XXX do we need more sanity checks? */
                    579:                        ours = 1;
1.105     dyoung    580:                        deliverifp = rt->rt_ifp; /* faith */
1.9       itojun    581:                        goto hbhcheck;
                    582:                }
                    583:        }
                    584: #endif
                    585:
                    586: #if 0
                    587:     {
                    588:        /*
                    589:         * Last resort: check in6_ifaddr for incoming interface.
                    590:         * The code is here until I update the "goto ours hack" code above
                    591:         * working right.
                    592:         */
                    593:        struct ifaddr *ifa;
1.113     dyoung    594:        IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
1.9       itojun    595:                if (ifa->ifa_addr == NULL)
                    596:                        continue;       /* just for safety */
                    597:                if (ifa->ifa_addr->sa_family != AF_INET6)
                    598:                        continue;
                    599:                if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
                    600:                        ours = 1;
                    601:                        deliverifp = ifa->ifa_ifp;
1.2       itojun    602:                        goto hbhcheck;
                    603:                }
                    604:        }
1.9       itojun    605:     }
1.2       itojun    606: #endif
                    607:
                    608:        /*
                    609:         * Now there is no reason to process the packet if it's not our own
                    610:         * and we're not a router.
                    611:         */
                    612:        if (!ip6_forwarding) {
                    613:                ip6stat.ip6s_cantforward++;
1.9       itojun    614:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2       itojun    615:                goto bad;
                    616:        }
                    617:
                    618:   hbhcheck:
                    619:        /*
1.81      rpaulo    620:         * record address information into m_tag, if we don't have one yet.
                    621:         * note that we are unable to record it, if the address is not listed
                    622:         * as our interface address (e.g. multicast addresses, addresses
                    623:         * within FAITH prefixes and such).
                    624:         */
1.111     dyoung    625:        if (deliverifp && ip6_getdstifaddr(m) == NULL) {
1.81      rpaulo    626:                struct in6_ifaddr *ia6;
                    627:
                    628:                ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
1.111     dyoung    629:                if (ia6 != NULL && ip6_setdstifaddr(m, ia6) == NULL) {
                    630:                        /*
                    631:                         * XXX maybe we should drop the packet here,
                    632:                         * as we could not provide enough information
                    633:                         * to the upper layers.
                    634:                         */
1.81      rpaulo    635:                }
                    636:        }
                    637:
                    638:        /*
1.2       itojun    639:         * Process Hop-by-Hop options header if it's contained.
                    640:         * m may be modified in ip6_hopopts_input().
                    641:         * If a JumboPayload option is included, plen will also be modified.
                    642:         */
                    643:        plen = (u_int32_t)ntohs(ip6->ip6_plen);
                    644:        if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9       itojun    645:                struct ip6_hbh *hbh;
                    646:
                    647:                if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
                    648: #if 0  /*touches NULL pointer*/
                    649:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
                    650: #endif
1.2       itojun    651:                        return; /* m have already been freed */
1.9       itojun    652:                }
1.22      itojun    653:
1.2       itojun    654:                /* adjust pointer */
                    655:                ip6 = mtod(m, struct ip6_hdr *);
1.22      itojun    656:
                    657:                /*
1.49      itojun    658:                 * if the payload length field is 0 and the next header field
1.22      itojun    659:                 * indicates Hop-by-Hop Options header, then a Jumbo Payload
                    660:                 * option MUST be included.
                    661:                 */
                    662:                if (ip6->ip6_plen == 0 && plen == 0) {
                    663:                        /*
                    664:                         * Note that if a valid jumbo payload option is
1.83      rpaulo    665:                         * contained, ip6_hopopts_input() must set a valid
1.55      itojun    666:                         * (non-zero) payload length to the variable plen.
1.22      itojun    667:                         */
                    668:                        ip6stat.ip6s_badoptions++;
                    669:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
                    670:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
                    671:                        icmp6_error(m, ICMP6_PARAM_PROB,
                    672:                                    ICMP6_PARAMPROB_HEADER,
1.97      christos  673:                                    (char *)&ip6->ip6_plen - (char *)ip6);
1.22      itojun    674:                        return;
                    675:                }
1.9       itojun    676:                IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
                    677:                        sizeof(struct ip6_hbh));
                    678:                if (hbh == NULL) {
                    679:                        ip6stat.ip6s_tooshort++;
                    680:                        return;
                    681:                }
1.57      thorpej   682:                KASSERT(IP6_HDR_ALIGNED_P(hbh));
1.9       itojun    683:                nxt = hbh->ip6h_nxt;
1.2       itojun    684:
                    685:                /*
                    686:                 * accept the packet if a router alert option is included
                    687:                 * and we act as an IPv6 router.
                    688:                 */
1.5       itojun    689:                if (rtalert != ~0 && ip6_forwarding)
1.2       itojun    690:                        ours = 1;
                    691:        } else
                    692:                nxt = ip6->ip6_nxt;
                    693:
                    694:        /*
                    695:         * Check that the amount of data in the buffers
                    696:         * is as at least much as the IPv6 header would have us expect.
                    697:         * Trim mbufs if longer than we expect.
                    698:         * Drop packet if shorter than we expect.
                    699:         */
                    700:        if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
                    701:                ip6stat.ip6s_tooshort++;
1.9       itojun    702:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.2       itojun    703:                goto bad;
                    704:        }
                    705:        if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
                    706:                if (m->m_len == m->m_pkthdr.len) {
                    707:                        m->m_len = sizeof(struct ip6_hdr) + plen;
                    708:                        m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
                    709:                } else
                    710:                        m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
                    711:        }
                    712:
                    713:        /*
                    714:         * Forward if desirable.
                    715:         */
                    716:        if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
                    717:                /*
                    718:                 * If we are acting as a multicast router, all
                    719:                 * incoming multicast packets are passed to the
                    720:                 * kernel-level multicast forwarding function.
                    721:                 * The packet is returned (relatively) intact; if
                    722:                 * ip6_mforward() returns a non-zero value, the packet
                    723:                 * must be discarded, else it may be accepted below.
                    724:                 */
                    725:                if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
                    726:                        ip6stat.ip6s_cantforward++;
                    727:                        m_freem(m);
                    728:                        return;
                    729:                }
                    730:                if (!ours) {
                    731:                        m_freem(m);
                    732:                        return;
                    733:                }
1.22      itojun    734:        } else if (!ours) {
1.64      itojun    735:                ip6_forward(m, srcrt);
1.2       itojun    736:                return;
1.56      itojun    737:        }
1.25      itojun    738:
                    739:        ip6 = mtod(m, struct ip6_hdr *);
                    740:
                    741:        /*
                    742:         * Malicious party may be able to use IPv4 mapped addr to confuse
                    743:         * tcp/udp stack and bypass security checks (act as if it was from
                    744:         * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
                    745:         *
                    746:         * For SIIT end node behavior, you may want to disable the check.
                    747:         * However, you will  become vulnerable to attacks using IPv4 mapped
                    748:         * source.
                    749:         */
                    750:        if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
                    751:            IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
                    752:                ip6stat.ip6s_badscope++;
                    753:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
                    754:                goto bad;
                    755:        }
1.2       itojun    756:
                    757:        /*
                    758:         * Tell launch routine the next header
                    759:         */
1.12      itojun    760: #ifdef IFA_STATS
1.28      itojun    761:        if (deliverifp != NULL) {
1.9       itojun    762:                struct in6_ifaddr *ia6;
                    763:                ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
                    764:                if (ia6)
                    765:                        ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
                    766:        }
                    767: #endif
1.2       itojun    768:        ip6stat.ip6s_delivered++;
1.9       itojun    769:        in6_ifstat_inc(deliverifp, ifs6_in_deliver);
1.2       itojun    770:        nest = 0;
1.40      itojun    771:
1.104     yamt      772:        rh_present = 0;
1.2       itojun    773:        while (nxt != IPPROTO_DONE) {
                    774:                if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
                    775:                        ip6stat.ip6s_toomanyhdr++;
1.104     yamt      776:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
1.2       itojun    777:                        goto bad;
                    778:                }
1.8       itojun    779:
                    780:                /*
                    781:                 * protection against faulty packet - there should be
                    782:                 * more sanity checks in header chain processing.
                    783:                 */
                    784:                if (m->m_pkthdr.len < off) {
                    785:                        ip6stat.ip6s_tooshort++;
1.9       itojun    786:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.8       itojun    787:                        goto bad;
                    788:                }
                    789:
1.104     yamt      790:                if (nxt == IPPROTO_ROUTING) {
                    791:                        if (rh_present++) {
                    792:                                in6_ifstat_inc(m->m_pkthdr.rcvif,
                    793:                                    ifs6_in_hdrerr);
                    794:                                ip6stat.ip6s_badoptions++;
                    795:                                goto bad;
                    796:                        }
                    797:                }
                    798:
1.37      itojun    799: #ifdef IPSEC
                    800:                /*
                    801:                 * enforce IPsec policy checking if we are seeing last header.
                    802:                 * note that we do not visit this with protocols with pcb layer
                    803:                 * code - like udp/tcp/raw ip.
                    804:                 */
                    805:                if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
                    806:                    ipsec6_in_reject(m, NULL)) {
                    807:                        ipsec6stat.in_polvio++;
                    808:                        goto bad;
                    809:                }
                    810: #endif
1.94      degroote  811: #ifdef FAST_IPSEC
                    812:        /*
                    813:         * enforce IPsec policy checking if we are seeing last header.
                    814:         * note that we do not visit this with protocols with pcb layer
                    815:         * code - like udp/tcp/raw ip.
                    816:         */
                    817:        if ((inet6sw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0) {
                    818:                /*
                    819:                 * Check if the packet has already had IPsec processing
                    820:                 * done.  If so, then just pass it along.  This tag gets
                    821:                 * set during AH, ESP, etc. input handling, before the
                    822:                 * packet is returned to the ip input queue for delivery.
                    823:                 */
                    824:                mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
                    825:                s = splsoftnet();
                    826:                if (mtag != NULL) {
                    827:                        tdbi = (struct tdb_ident *)(mtag + 1);
                    828:                        sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
                    829:                } else {
                    830:                        sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
                    831:                                                                        IP_FORWARDING, &error);
                    832:                }
                    833:                if (sp != NULL) {
                    834:                        /*
                    835:                         * Check security policy against packet attributes.
                    836:                         */
                    837:                        error = ipsec_in_reject(sp, m);
                    838:                        KEY_FREESP(&sp);
                    839:                } else {
                    840:                        /* XXX error stat??? */
                    841:                        error = EINVAL;
                    842:                        DPRINTF(("ip6_input: no SP, packet discarded\n"));/*XXX*/
                    843:                }
                    844:                splx(s);
                    845:                if (error)
                    846:                        goto bad;
                    847:        }
                    848: #endif /* FAST_IPSEC */
                    849:
1.113.8.1  keiichi   850:                if (dest6_mip6_hao(m, off, nxt) < 0)
                    851:                        goto bad;
1.56      itojun    852:
1.2       itojun    853:                nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
                    854:        }
                    855:        return;
                    856:  bad:
                    857:        m_freem(m);
                    858: }
                    859:
                    860: /*
1.81      rpaulo    861:  * set/grab in6_ifaddr correspond to IPv6 destination address.
                    862:  */
                    863: static struct m_tag *
1.112     dyoung    864: ip6_setdstifaddr(struct mbuf *m, const struct in6_ifaddr *ia)
1.81      rpaulo    865: {
                    866:        struct m_tag *mtag;
                    867:
                    868:        mtag = ip6_addaux(m);
1.112     dyoung    869:        if (mtag != NULL) {
                    870:                struct ip6aux *ip6a;
                    871:
                    872:                ip6a = (struct ip6aux *)(mtag + 1);
1.113.8.1  keiichi   873:                in6_setscope(&ip6a->ip6a_src, ia->ia_ifp, &ip6a->ip6a_src_scope_id);
1.112     dyoung    874:                ip6a->ip6a_src = ia->ia_addr.sin6_addr;
1.113.8.1  keiichi   875:                ip6a->ip6a_src_flags = ia->ia6_flags;
1.112     dyoung    876:        }
1.81      rpaulo    877:        return mtag;    /* NULL if failed to set */
                    878: }
                    879:
1.112     dyoung    880: const struct ip6aux *
1.107     christos  881: ip6_getdstifaddr(struct mbuf *m)
1.81      rpaulo    882: {
                    883:        struct m_tag *mtag;
                    884:
                    885:        mtag = ip6_findaux(m);
1.112     dyoung    886:        if (mtag != NULL)
                    887:                return (struct ip6aux *)(mtag + 1);
1.81      rpaulo    888:        else
                    889:                return NULL;
                    890: }
                    891:
                    892: /*
1.2       itojun    893:  * Hop-by-Hop options header processing. If a valid jumbo payload option is
                    894:  * included, the real payload length will be stored in plenp.
1.107     christos  895:  *
                    896:  * rtalertp - XXX: should be stored more smart way
1.2       itojun    897:  */
                    898: static int
1.107     christos  899: ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
                    900:        struct mbuf **mp, int *offp)
1.2       itojun    901: {
1.35      itojun    902:        struct mbuf *m = *mp;
1.2       itojun    903:        int off = *offp, hbhlen;
                    904:        struct ip6_hbh *hbh;
                    905:
                    906:        /* validation of the length of the header */
1.9       itojun    907:        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                    908:                sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
                    909:        if (hbh == NULL) {
                    910:                ip6stat.ip6s_tooshort++;
                    911:                return -1;
                    912:        }
                    913:        hbhlen = (hbh->ip6h_len + 1) << 3;
                    914:        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
                    915:                hbhlen);
                    916:        if (hbh == NULL) {
                    917:                ip6stat.ip6s_tooshort++;
                    918:                return -1;
                    919:        }
1.57      thorpej   920:        KASSERT(IP6_HDR_ALIGNED_P(hbh));
1.2       itojun    921:        off += hbhlen;
                    922:        hbhlen -= sizeof(struct ip6_hbh);
                    923:
                    924:        if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
                    925:                                hbhlen, rtalertp, plenp) < 0)
1.58      itojun    926:                return (-1);
1.2       itojun    927:
                    928:        *offp = off;
                    929:        *mp = m;
1.58      itojun    930:        return (0);
1.2       itojun    931: }
                    932:
                    933: /*
                    934:  * Search header for all Hop-by-hop options and process each option.
                    935:  * This function is separate from ip6_hopopts_input() in order to
                    936:  * handle a case where the sending node itself process its hop-by-hop
                    937:  * options header. In such a case, the function is called from ip6_output().
1.55      itojun    938:  *
                    939:  * The function assumes that hbh header is located right after the IPv6 header
                    940:  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
                    941:  * opthead + hbhlen is located in continuous memory region.
1.2       itojun    942:  */
                    943: int
1.107     christos  944: ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
                    945:        u_int32_t *rtalertp, u_int32_t *plenp)
1.2       itojun    946: {
                    947:        struct ip6_hdr *ip6;
                    948:        int optlen = 0;
                    949:        u_int8_t *opt = opthead;
                    950:        u_int16_t rtalert_val;
1.22      itojun    951:        u_int32_t jumboplen;
1.55      itojun    952:        const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1.2       itojun    953:
                    954:        for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1.35      itojun    955:                switch (*opt) {
                    956:                case IP6OPT_PAD1:
                    957:                        optlen = 1;
                    958:                        break;
                    959:                case IP6OPT_PADN:
                    960:                        if (hbhlen < IP6OPT_MINLEN) {
                    961:                                ip6stat.ip6s_toosmall++;
                    962:                                goto bad;
                    963:                        }
                    964:                        optlen = *(opt + 1) + 2;
                    965:                        break;
                    966:                case IP6OPT_RTALERT:
                    967:                        /* XXX may need check for alignment */
                    968:                        if (hbhlen < IP6OPT_RTALERT_LEN) {
                    969:                                ip6stat.ip6s_toosmall++;
                    970:                                goto bad;
                    971:                        }
                    972:                        if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1.55      itojun    973:                                /* XXX stat */
                    974:                                icmp6_error(m, ICMP6_PARAM_PROB,
                    975:                                    ICMP6_PARAMPROB_HEADER,
                    976:                                    erroff + opt + 1 - opthead);
                    977:                                return (-1);
1.35      itojun    978:                        }
                    979:                        optlen = IP6OPT_RTALERT_LEN;
1.97      christos  980:                        bcopy((void *)(opt + 2), (void *)&rtalert_val, 2);
1.35      itojun    981:                        *rtalertp = ntohs(rtalert_val);
                    982:                        break;
                    983:                case IP6OPT_JUMBO:
1.22      itojun    984:                        /* XXX may need check for alignment */
                    985:                        if (hbhlen < IP6OPT_JUMBO_LEN) {
                    986:                                ip6stat.ip6s_toosmall++;
                    987:                                goto bad;
                    988:                        }
1.35      itojun    989:                        if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1.55      itojun    990:                                /* XXX stat */
                    991:                                icmp6_error(m, ICMP6_PARAM_PROB,
                    992:                                    ICMP6_PARAMPROB_HEADER,
                    993:                                    erroff + opt + 1 - opthead);
                    994:                                return (-1);
1.35      itojun    995:                        }
1.22      itojun    996:                        optlen = IP6OPT_JUMBO_LEN;
                    997:
                    998:                        /*
                    999:                         * IPv6 packets that have non 0 payload length
1.35      itojun   1000:                         * must not contain a jumbo payload option.
1.22      itojun   1001:                         */
                   1002:                        ip6 = mtod(m, struct ip6_hdr *);
                   1003:                        if (ip6->ip6_plen) {
                   1004:                                ip6stat.ip6s_badoptions++;
                   1005:                                icmp6_error(m, ICMP6_PARAM_PROB,
1.55      itojun   1006:                                    ICMP6_PARAMPROB_HEADER,
                   1007:                                    erroff + opt - opthead);
                   1008:                                return (-1);
1.22      itojun   1009:                        }
1.2       itojun   1010:
1.22      itojun   1011:                        /*
                   1012:                         * We may see jumbolen in unaligned location, so
                   1013:                         * we'd need to perform bcopy().
                   1014:                         */
                   1015:                        bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
                   1016:                        jumboplen = (u_int32_t)htonl(jumboplen);
                   1017:
                   1018: #if 1
                   1019:                        /*
                   1020:                         * if there are multiple jumbo payload options,
                   1021:                         * *plenp will be non-zero and the packet will be
                   1022:                         * rejected.
                   1023:                         * the behavior may need some debate in ipngwg -
                   1024:                         * multiple options does not make sense, however,
                   1025:                         * there's no explicit mention in specification.
                   1026:                         */
                   1027:                        if (*plenp != 0) {
                   1028:                                ip6stat.ip6s_badoptions++;
                   1029:                                icmp6_error(m, ICMP6_PARAM_PROB,
1.55      itojun   1030:                                    ICMP6_PARAMPROB_HEADER,
                   1031:                                    erroff + opt + 2 - opthead);
                   1032:                                return (-1);
1.22      itojun   1033:                        }
1.8       itojun   1034: #endif
1.2       itojun   1035:
1.22      itojun   1036:                        /*
                   1037:                         * jumbo payload length must be larger than 65535.
                   1038:                         */
                   1039:                        if (jumboplen <= IPV6_MAXPACKET) {
                   1040:                                ip6stat.ip6s_badoptions++;
                   1041:                                icmp6_error(m, ICMP6_PARAM_PROB,
1.55      itojun   1042:                                    ICMP6_PARAMPROB_HEADER,
                   1043:                                    erroff + opt + 2 - opthead);
                   1044:                                return (-1);
1.22      itojun   1045:                        }
                   1046:                        *plenp = jumboplen;
                   1047:
                   1048:                        break;
1.35      itojun   1049:                default:                /* unknown option */
                   1050:                        if (hbhlen < IP6OPT_MINLEN) {
                   1051:                                ip6stat.ip6s_toosmall++;
                   1052:                                goto bad;
                   1053:                        }
1.55      itojun   1054:                        optlen = ip6_unknown_opt(opt, m,
                   1055:                            erroff + opt - opthead);
                   1056:                        if (optlen == -1)
                   1057:                                return (-1);
1.35      itojun   1058:                        optlen += 2;
                   1059:                        break;
1.2       itojun   1060:                }
                   1061:        }
                   1062:
1.55      itojun   1063:        return (0);
1.2       itojun   1064:
                   1065:   bad:
                   1066:        m_freem(m);
1.55      itojun   1067:        return (-1);
1.2       itojun   1068: }
                   1069:
                   1070: /*
                   1071:  * Unknown option processing.
                   1072:  * The third argument `off' is the offset from the IPv6 header to the option,
                   1073:  * which is necessary if the IPv6 header the and option header and IPv6 header
                   1074:  * is not continuous in order to return an ICMPv6 error.
                   1075:  */
                   1076: int
1.107     christos 1077: ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1.2       itojun   1078: {
                   1079:        struct ip6_hdr *ip6;
                   1080:
1.35      itojun   1081:        switch (IP6OPT_TYPE(*optp)) {
                   1082:        case IP6OPT_TYPE_SKIP: /* ignore the option */
1.58      itojun   1083:                return ((int)*(optp + 1));
1.35      itojun   1084:        case IP6OPT_TYPE_DISCARD:       /* silently discard */
                   1085:                m_freem(m);
1.58      itojun   1086:                return (-1);
1.35      itojun   1087:        case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
                   1088:                ip6stat.ip6s_badoptions++;
                   1089:                icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1.58      itojun   1090:                return (-1);
1.35      itojun   1091:        case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
                   1092:                ip6stat.ip6s_badoptions++;
                   1093:                ip6 = mtod(m, struct ip6_hdr *);
                   1094:                if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
                   1095:                    (m->m_flags & (M_BCAST|M_MCAST)))
                   1096:                        m_freem(m);
                   1097:                else
                   1098:                        icmp6_error(m, ICMP6_PARAM_PROB,
                   1099:                                    ICMP6_PARAMPROB_OPTION, off);
1.58      itojun   1100:                return (-1);
1.2       itojun   1101:        }
                   1102:
                   1103:        m_freem(m);             /* XXX: NOTREACHED */
1.58      itojun   1104:        return (-1);
1.2       itojun   1105: }
                   1106:
                   1107: /*
1.9       itojun   1108:  * Create the "control" list for this pcb.
                   1109:  *
                   1110:  * The routine will be called from upper layer handlers like tcp6_input().
                   1111:  * Thus the routine assumes that the caller (tcp6_input) have already
                   1112:  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
                   1113:  * very first mbuf on the mbuf chain.
                   1114:  * We may want to add some infinite loop prevention or sanity checks for safety.
                   1115:  * (This applies only when you are using KAME mbuf chain restriction, i.e.
                   1116:  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
1.2       itojun   1117:  */
                   1118: void
1.107     christos 1119: ip6_savecontrol(struct in6pcb *in6p, struct mbuf **mp,
                   1120:        struct ip6_hdr *ip6, struct mbuf *m)
1.2       itojun   1121: {
1.85      rpaulo   1122: #ifdef RFC2292
                   1123: #define IS2292(x, y)   ((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
                   1124: #else
                   1125: #define IS2292(x, y)   (y)
                   1126: #endif
                   1127:
1.9       itojun   1128: #ifdef SO_TIMESTAMP
1.2       itojun   1129:        if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
                   1130:                struct timeval tv;
                   1131:
                   1132:                microtime(&tv);
1.97      christos 1133:                *mp = sbcreatecontrol((void *) &tv, sizeof(tv),
1.63      itojun   1134:                    SCM_TIMESTAMP, SOL_SOCKET);
1.2       itojun   1135:                if (*mp)
                   1136:                        mp = &(*mp)->m_next;
                   1137:        }
1.9       itojun   1138: #endif
1.87      rpaulo   1139:
                   1140:        /* some OSes call this logic with IPv4 packet, for SO_TIMESTAMP */
                   1141:        if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
                   1142:                return;
                   1143:
1.2       itojun   1144:        /* RFC 2292 sec. 5 */
1.35      itojun   1145:        if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1.2       itojun   1146:                struct in6_pktinfo pi6;
1.85      rpaulo   1147:
1.2       itojun   1148:                bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1.81      rpaulo   1149:                in6_clearscope(&pi6.ipi6_addr); /* XXX */
1.84      christos 1150:                pi6.ipi6_ifindex = m->m_pkthdr.rcvif ?
                   1151:                    m->m_pkthdr.rcvif->if_index : 0;
1.97      christos 1152:                *mp = sbcreatecontrol((void *) &pi6,
1.85      rpaulo   1153:                    sizeof(struct in6_pktinfo),
                   1154:                    IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1.2       itojun   1155:                if (*mp)
                   1156:                        mp = &(*mp)->m_next;
                   1157:        }
1.85      rpaulo   1158:
1.2       itojun   1159:        if (in6p->in6p_flags & IN6P_HOPLIMIT) {
                   1160:                int hlim = ip6->ip6_hlim & 0xff;
1.85      rpaulo   1161:
1.97      christos 1162:                *mp = sbcreatecontrol((void *) &hlim, sizeof(int),
1.85      rpaulo   1163:                    IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
                   1164:                if (*mp)
                   1165:                        mp = &(*mp)->m_next;
                   1166:        }
                   1167:
                   1168:        if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
                   1169:                u_int32_t flowinfo;
                   1170:                int tclass;
                   1171:
                   1172:                flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
                   1173:                flowinfo >>= 20;
                   1174:
                   1175:                tclass = flowinfo & 0xff;
1.97      christos 1176:                *mp = sbcreatecontrol((void *)&tclass, sizeof(tclass),
1.85      rpaulo   1177:                    IPV6_TCLASS, IPPROTO_IPV6);
                   1178:
1.2       itojun   1179:                if (*mp)
                   1180:                        mp = &(*mp)->m_next;
                   1181:        }
                   1182:
                   1183:        /*
1.75      itojun   1184:         * IPV6_HOPOPTS socket option.  Recall that we required super-user
                   1185:         * privilege for the option (see ip6_ctloutput), but it might be too
                   1186:         * strict, since there might be some hop-by-hop options which can be
                   1187:         * returned to normal user.
1.85      rpaulo   1188:         * See also RFC3542 section 8 (or RFC2292 section 6).
1.2       itojun   1189:         */
1.75      itojun   1190:        if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
1.2       itojun   1191:                /*
                   1192:                 * Check if a hop-by-hop options header is contatined in the
                   1193:                 * received packet, and if so, store the options as ancillary
                   1194:                 * data. Note that a hop-by-hop options header must be
                   1195:                 * just after the IPv6 header, which fact is assured through
                   1196:                 * the IPv6 input processing.
                   1197:                 */
1.78      christos 1198:                struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
                   1199:                if (xip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9       itojun   1200:                        struct ip6_hbh *hbh;
                   1201:                        int hbhlen;
1.62      itojun   1202:                        struct mbuf *ext;
1.9       itojun   1203:
1.62      itojun   1204:                        ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1.78      christos 1205:                            xip6->ip6_nxt);
1.62      itojun   1206:                        if (ext == NULL) {
1.9       itojun   1207:                                ip6stat.ip6s_tooshort++;
                   1208:                                return;
                   1209:                        }
1.62      itojun   1210:                        hbh = mtod(ext, struct ip6_hbh *);
1.9       itojun   1211:                        hbhlen = (hbh->ip6h_len + 1) << 3;
1.62      itojun   1212:                        if (hbhlen != ext->m_len) {
                   1213:                                m_freem(ext);
1.9       itojun   1214:                                ip6stat.ip6s_tooshort++;
                   1215:                                return;
                   1216:                        }
1.2       itojun   1217:
                   1218:                        /*
                   1219:                         * XXX: We copy whole the header even if a jumbo
                   1220:                         * payload option is included, which option is to
                   1221:                         * be removed before returning in the RFC 2292.
1.85      rpaulo   1222:                         * Note: this constraint is removed in RFC3542.
1.2       itojun   1223:                         */
1.97      christos 1224:                        *mp = sbcreatecontrol((void *)hbh, hbhlen,
1.85      rpaulo   1225:                            IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
                   1226:                            IPPROTO_IPV6);
1.2       itojun   1227:                        if (*mp)
                   1228:                                mp = &(*mp)->m_next;
1.62      itojun   1229:                        m_freem(ext);
1.2       itojun   1230:                }
                   1231:        }
                   1232:
                   1233:        /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
                   1234:        if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
1.78      christos 1235:                struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
                   1236:                int nxt = xip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1.2       itojun   1237:
                   1238:                /*
                   1239:                 * Search for destination options headers or routing
                   1240:                 * header(s) through the header chain, and stores each
                   1241:                 * header as ancillary data.
                   1242:                 * Note that the order of the headers remains in
                   1243:                 * the chain of ancillary data.
                   1244:                 */
1.86      rpaulo   1245:                for (;;) {      /* is explicit loop prevention necessary? */
1.62      itojun   1246:                        struct ip6_ext *ip6e = NULL;
1.9       itojun   1247:                        int elen;
1.62      itojun   1248:                        struct mbuf *ext = NULL;
1.9       itojun   1249:
1.62      itojun   1250:                        /*
                   1251:                         * if it is not an extension header, don't try to
                   1252:                         * pull it from the chain.
                   1253:                         */
                   1254:                        switch (nxt) {
                   1255:                        case IPPROTO_DSTOPTS:
                   1256:                        case IPPROTO_ROUTING:
                   1257:                        case IPPROTO_HOPOPTS:
                   1258:                        case IPPROTO_AH: /* is it possible? */
                   1259:                                break;
                   1260:                        default:
                   1261:                                goto loopend;
                   1262:                        }
                   1263:
                   1264:                        ext = ip6_pullexthdr(m, off, nxt);
                   1265:                        if (ext == NULL) {
1.9       itojun   1266:                                ip6stat.ip6s_tooshort++;
                   1267:                                return;
                   1268:                        }
1.62      itojun   1269:                        ip6e = mtod(ext, struct ip6_ext *);
1.9       itojun   1270:                        if (nxt == IPPROTO_AH)
                   1271:                                elen = (ip6e->ip6e_len + 2) << 2;
                   1272:                        else
                   1273:                                elen = (ip6e->ip6e_len + 1) << 3;
1.62      itojun   1274:                        if (elen != ext->m_len) {
                   1275:                                m_freem(ext);
1.9       itojun   1276:                                ip6stat.ip6s_tooshort++;
                   1277:                                return;
                   1278:                        }
1.57      thorpej  1279:                        KASSERT(IP6_HDR_ALIGNED_P(ip6e));
1.2       itojun   1280:
1.35      itojun   1281:                        switch (nxt) {
1.62      itojun   1282:                        case IPPROTO_DSTOPTS:
1.35      itojun   1283:                                if (!in6p->in6p_flags & IN6P_DSTOPTS)
                   1284:                                        break;
                   1285:
1.97      christos 1286:                                *mp = sbcreatecontrol((void *)ip6e, elen,
1.85      rpaulo   1287:                                    IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
                   1288:                                    IPPROTO_IPV6);
1.35      itojun   1289:                                if (*mp)
                   1290:                                        mp = &(*mp)->m_next;
                   1291:                                break;
                   1292:
                   1293:                        case IPPROTO_ROUTING:
                   1294:                                if (!in6p->in6p_flags & IN6P_RTHDR)
                   1295:                                        break;
                   1296:
1.97      christos 1297:                                *mp = sbcreatecontrol((void *)ip6e, elen,
1.85      rpaulo   1298:                                    IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
                   1299:                                    IPPROTO_IPV6);
1.35      itojun   1300:                                if (*mp)
                   1301:                                        mp = &(*mp)->m_next;
                   1302:                                break;
                   1303:
1.62      itojun   1304:                        case IPPROTO_HOPOPTS:
                   1305:                        case IPPROTO_AH: /* is it possible? */
                   1306:                                break;
                   1307:
1.35      itojun   1308:                        default:
                   1309:                                /*
1.62      itojun   1310:                                 * other cases have been filtered in the above.
                   1311:                                 * none will visit this case.  here we supply
                   1312:                                 * the code just in case (nxt overwritten or
                   1313:                                 * other cases).
1.35      itojun   1314:                                 */
1.62      itojun   1315:                                m_freem(ext);
1.35      itojun   1316:                                goto loopend;
                   1317:
1.2       itojun   1318:                        }
                   1319:
                   1320:                        /* proceed with the next header. */
1.9       itojun   1321:                        off += elen;
1.2       itojun   1322:                        nxt = ip6e->ip6e_nxt;
1.62      itojun   1323:                        ip6e = NULL;
                   1324:                        m_freem(ext);
                   1325:                        ext = NULL;
1.2       itojun   1326:                }
                   1327:          loopend:
1.62      itojun   1328:                ;
1.2       itojun   1329:        }
1.62      itojun   1330: }
1.85      rpaulo   1331: #undef IS2292
                   1332:
                   1333:
                   1334: void
1.95      dyoung   1335: ip6_notify_pmtu(struct in6pcb *in6p, const struct sockaddr_in6 *dst,
                   1336:     uint32_t *mtu)
1.85      rpaulo   1337: {
                   1338:        struct socket *so;
                   1339:        struct mbuf *m_mtu;
                   1340:        struct ip6_mtuinfo mtuctl;
                   1341:
                   1342:        so = in6p->in6p_socket;
                   1343:
                   1344:        if (mtu == NULL)
                   1345:                return;
                   1346:
                   1347: #ifdef DIAGNOSTIC
                   1348:        if (so == NULL)         /* I believe this is impossible */
                   1349:                panic("ip6_notify_pmtu: socket is NULL");
                   1350: #endif
                   1351:
                   1352:        memset(&mtuctl, 0, sizeof(mtuctl));     /* zero-clear for safety */
                   1353:        mtuctl.ip6m_mtu = *mtu;
                   1354:        mtuctl.ip6m_addr = *dst;
                   1355:        if (sa6_recoverscope(&mtuctl.ip6m_addr))
                   1356:                return;
                   1357:
1.97      christos 1358:        if ((m_mtu = sbcreatecontrol((void *)&mtuctl, sizeof(mtuctl),
1.85      rpaulo   1359:            IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
                   1360:                return;
                   1361:
1.95      dyoung   1362:        if (sbappendaddr(&so->so_rcv, (const struct sockaddr *)dst, NULL, m_mtu)
1.85      rpaulo   1363:            == 0) {
                   1364:                m_freem(m_mtu);
                   1365:                /* XXX: should count statistics */
                   1366:        } else
                   1367:                sorwakeup(so);
                   1368:
                   1369:        return;
                   1370: }
1.62      itojun   1371:
                   1372: /*
                   1373:  * pull single extension header from mbuf chain.  returns single mbuf that
                   1374:  * contains the result, or NULL on error.
                   1375:  */
                   1376: static struct mbuf *
1.107     christos 1377: ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
1.62      itojun   1378: {
                   1379:        struct ip6_ext ip6e;
                   1380:        size_t elen;
                   1381:        struct mbuf *n;
                   1382:
                   1383: #ifdef DIAGNOSTIC
                   1384:        switch (nxt) {
                   1385:        case IPPROTO_DSTOPTS:
                   1386:        case IPPROTO_ROUTING:
                   1387:        case IPPROTO_HOPOPTS:
                   1388:        case IPPROTO_AH: /* is it possible? */
                   1389:                break;
                   1390:        default:
                   1391:                printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1.2       itojun   1392:        }
1.62      itojun   1393: #endif
1.2       itojun   1394:
1.97      christos 1395:        m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1.62      itojun   1396:        if (nxt == IPPROTO_AH)
                   1397:                elen = (ip6e.ip6e_len + 2) << 2;
                   1398:        else
                   1399:                elen = (ip6e.ip6e_len + 1) << 3;
                   1400:
                   1401:        MGET(n, M_DONTWAIT, MT_DATA);
                   1402:        if (n && elen >= MLEN) {
                   1403:                MCLGET(n, M_DONTWAIT);
                   1404:                if ((n->m_flags & M_EXT) == 0) {
                   1405:                        m_free(n);
                   1406:                        n = NULL;
                   1407:                }
                   1408:        }
                   1409:        if (!n)
                   1410:                return NULL;
                   1411:
                   1412:        n->m_len = 0;
                   1413:        if (elen >= M_TRAILINGSPACE(n)) {
                   1414:                m_free(n);
                   1415:                return NULL;
                   1416:        }
                   1417:
1.97      christos 1418:        m_copydata(m, off, elen, mtod(n, void *));
1.62      itojun   1419:        n->m_len = elen;
                   1420:        return n;
1.2       itojun   1421: }
                   1422:
                   1423: /*
                   1424:  * Get pointer to the previous header followed by the header
                   1425:  * currently processed.
                   1426:  * XXX: This function supposes that
                   1427:  *     M includes all headers,
                   1428:  *     the next header field and the header length field of each header
                   1429:  *     are valid, and
                   1430:  *     the sum of each header length equals to OFF.
                   1431:  * Because of these assumptions, this function must be called very
                   1432:  * carefully. Moreover, it will not be used in the near future when
                   1433:  * we develop `neater' mechanism to process extension headers.
                   1434:  */
1.58      itojun   1435: u_int8_t *
1.107     christos 1436: ip6_get_prevhdr(struct mbuf *m, int off)
1.2       itojun   1437: {
                   1438:        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                   1439:
                   1440:        if (off == sizeof(struct ip6_hdr))
1.58      itojun   1441:                return (&ip6->ip6_nxt);
1.2       itojun   1442:        else {
                   1443:                int len, nxt;
                   1444:                struct ip6_ext *ip6e = NULL;
                   1445:
                   1446:                nxt = ip6->ip6_nxt;
                   1447:                len = sizeof(struct ip6_hdr);
                   1448:                while (len < off) {
1.97      christos 1449:                        ip6e = (struct ip6_ext *)(mtod(m, char *) + len);
1.2       itojun   1450:
1.35      itojun   1451:                        switch (nxt) {
1.2       itojun   1452:                        case IPPROTO_FRAGMENT:
                   1453:                                len += sizeof(struct ip6_frag);
                   1454:                                break;
                   1455:                        case IPPROTO_AH:
                   1456:                                len += (ip6e->ip6e_len + 2) << 2;
                   1457:                                break;
                   1458:                        default:
                   1459:                                len += (ip6e->ip6e_len + 1) << 3;
                   1460:                                break;
                   1461:                        }
                   1462:                        nxt = ip6e->ip6e_nxt;
                   1463:                }
                   1464:                if (ip6e)
1.58      itojun   1465:                        return (&ip6e->ip6e_nxt);
1.2       itojun   1466:                else
                   1467:                        return NULL;
1.18      itojun   1468:        }
                   1469: }
                   1470:
                   1471: /*
                   1472:  * get next header offset.  m will be retained.
                   1473:  */
                   1474: int
1.107     christos 1475: ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
1.18      itojun   1476: {
                   1477:        struct ip6_hdr ip6;
                   1478:        struct ip6_ext ip6e;
                   1479:        struct ip6_frag fh;
                   1480:
                   1481:        /* just in case */
                   1482:        if (m == NULL)
                   1483:                panic("ip6_nexthdr: m == NULL");
                   1484:        if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
                   1485:                return -1;
                   1486:
                   1487:        switch (proto) {
                   1488:        case IPPROTO_IPV6:
1.83      rpaulo   1489:                /* do not chase beyond intermediate IPv6 headers */
                   1490:                if (off != 0)
                   1491:                        return -1;
1.18      itojun   1492:                if (m->m_pkthdr.len < off + sizeof(ip6))
                   1493:                        return -1;
1.97      christos 1494:                m_copydata(m, off, sizeof(ip6), (void *)&ip6);
1.18      itojun   1495:                if (nxtp)
                   1496:                        *nxtp = ip6.ip6_nxt;
                   1497:                off += sizeof(ip6);
                   1498:                return off;
                   1499:
                   1500:        case IPPROTO_FRAGMENT:
                   1501:                /*
                   1502:                 * terminate parsing if it is not the first fragment,
                   1503:                 * it does not make sense to parse through it.
                   1504:                 */
                   1505:                if (m->m_pkthdr.len < off + sizeof(fh))
                   1506:                        return -1;
1.97      christos 1507:                m_copydata(m, off, sizeof(fh), (void *)&fh);
1.67      itojun   1508:                if ((fh.ip6f_offlg & IP6F_OFF_MASK) != 0)
1.18      itojun   1509:                        return -1;
                   1510:                if (nxtp)
                   1511:                        *nxtp = fh.ip6f_nxt;
                   1512:                off += sizeof(struct ip6_frag);
                   1513:                return off;
                   1514:
                   1515:        case IPPROTO_AH:
                   1516:                if (m->m_pkthdr.len < off + sizeof(ip6e))
                   1517:                        return -1;
1.97      christos 1518:                m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1.18      itojun   1519:                if (nxtp)
                   1520:                        *nxtp = ip6e.ip6e_nxt;
                   1521:                off += (ip6e.ip6e_len + 2) << 2;
1.47      itojun   1522:                if (m->m_pkthdr.len < off)
                   1523:                        return -1;
1.18      itojun   1524:                return off;
                   1525:
                   1526:        case IPPROTO_HOPOPTS:
                   1527:        case IPPROTO_ROUTING:
                   1528:        case IPPROTO_DSTOPTS:
                   1529:                if (m->m_pkthdr.len < off + sizeof(ip6e))
                   1530:                        return -1;
1.97      christos 1531:                m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1.18      itojun   1532:                if (nxtp)
                   1533:                        *nxtp = ip6e.ip6e_nxt;
                   1534:                off += (ip6e.ip6e_len + 1) << 3;
1.47      itojun   1535:                if (m->m_pkthdr.len < off)
                   1536:                        return -1;
1.18      itojun   1537:                return off;
                   1538:
                   1539:        case IPPROTO_NONE:
                   1540:        case IPPROTO_ESP:
                   1541:        case IPPROTO_IPCOMP:
                   1542:                /* give up */
                   1543:                return -1;
                   1544:
                   1545:        default:
                   1546:                return -1;
                   1547:        }
                   1548: }
                   1549:
                   1550: /*
                   1551:  * get offset for the last header in the chain.  m will be kept untainted.
                   1552:  */
                   1553: int
1.107     christos 1554: ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
1.18      itojun   1555: {
                   1556:        int newoff;
                   1557:        int nxt;
                   1558:
                   1559:        if (!nxtp) {
                   1560:                nxt = -1;
                   1561:                nxtp = &nxt;
                   1562:        }
1.86      rpaulo   1563:        for (;;) {
1.18      itojun   1564:                newoff = ip6_nexthdr(m, off, proto, nxtp);
                   1565:                if (newoff < 0)
                   1566:                        return off;
                   1567:                else if (newoff < off)
                   1568:                        return -1;      /* invalid */
                   1569:                else if (newoff == off)
                   1570:                        return newoff;
                   1571:
                   1572:                off = newoff;
                   1573:                proto = *nxtp;
1.2       itojun   1574:        }
                   1575: }
                   1576:
1.81      rpaulo   1577: struct m_tag *
1.107     christos 1578: ip6_addaux(struct mbuf *m)
1.81      rpaulo   1579: {
                   1580:        struct m_tag *mtag;
                   1581:
                   1582:        mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
                   1583:        if (!mtag) {
                   1584:                mtag = m_tag_get(PACKET_TAG_INET6, sizeof(struct ip6aux),
                   1585:                    M_NOWAIT);
                   1586:                if (mtag) {
                   1587:                        m_tag_prepend(m, mtag);
                   1588:                        bzero(mtag + 1, sizeof(struct ip6aux));
                   1589:                }
                   1590:        }
                   1591:        return mtag;
                   1592: }
                   1593:
                   1594: struct m_tag *
1.107     christos 1595: ip6_findaux(struct mbuf *m)
1.81      rpaulo   1596: {
                   1597:        struct m_tag *mtag;
                   1598:
                   1599:        mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
                   1600:        return mtag;
                   1601: }
                   1602:
                   1603: void
1.107     christos 1604: ip6_delaux(struct mbuf *m)
1.81      rpaulo   1605: {
                   1606:        struct m_tag *mtag;
                   1607:
                   1608:        mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
                   1609:        if (mtag)
                   1610:                m_tag_delete(m, mtag);
                   1611: }
                   1612:
1.98      liamjfoy 1613: #ifdef GATEWAY
                   1614: /*
                   1615:  * sysctl helper routine for net.inet.ip6.maxflows. Since
                   1616:  * we could reduce this value, call ip6flow_reap();
                   1617:  */
                   1618: static int
1.101     liamjfoy 1619: sysctl_net_inet6_ip6_maxflows(SYSCTLFN_ARGS)
1.98      liamjfoy 1620: {
                   1621:         int s;
                   1622:
                   1623:         s = sysctl_lookup(SYSCTLFN_CALL(rnode));
1.100     liamjfoy 1624:         if (s || newp == NULL)
1.98      liamjfoy 1625:                 return (s);
                   1626:
                   1627:         s = splsoftnet();
                   1628:         ip6flow_reap(0);
                   1629:         splx(s);
                   1630:
                   1631:         return (0);
                   1632: }
1.99      liamjfoy 1633:
                   1634: static int
                   1635: sysctl_net_inet6_ip6_hashsize(SYSCTLFN_ARGS)
                   1636: {
                   1637:        int error, tmp;
                   1638:        struct sysctlnode node;
                   1639:
                   1640:        node = *rnode;
                   1641:        tmp = ip6_hashsize;
                   1642:        node.sysctl_data = &tmp;
                   1643:        error = sysctl_lookup(SYSCTLFN_CALL(&node));
                   1644:        if (error || newp == NULL)
                   1645:                return (error);
                   1646:
                   1647:        if ((tmp & (tmp - 1)) == 0 && tmp != 0) {
                   1648:                /*
                   1649:                 * Can only fail due to malloc()
                   1650:                 */
                   1651:                if (ip6flow_invalidate_all(tmp))
                   1652:                        return ENOMEM;
                   1653:        } else {
                   1654:                /*
                   1655:                 * EINVAL if not a power of 2
                   1656:                 */
                   1657:                return EINVAL;
                   1658:        }
                   1659:
                   1660:        return (0);
                   1661: }
1.98      liamjfoy 1662: #endif /* GATEWAY */
                   1663:
1.2       itojun   1664: /*
                   1665:  * System control for IP6
                   1666:  */
                   1667:
                   1668: u_char inet6ctlerrmap[PRC_NCMDS] = {
                   1669:        0,              0,              0,              0,
                   1670:        0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
                   1671:        EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
                   1672:        EMSGSIZE,       EHOSTUNREACH,   0,              0,
                   1673:        0,              0,              0,              0,
                   1674:        ENOPROTOOPT
                   1675: };
                   1676:
1.70      atatat   1677: SYSCTL_SETUP(sysctl_net_inet6_ip6_setup, "sysctl net.inet6.ip6 subtree setup")
1.2       itojun   1678: {
1.85      rpaulo   1679: #ifdef RFC2292
                   1680: #define IS2292(x, y)   ((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
                   1681: #else
                   1682: #define IS2292(x, y)   (y)
                   1683: #endif
1.26      itojun   1684:
1.73      atatat   1685:        sysctl_createv(clog, 0, NULL, NULL,
                   1686:                       CTLFLAG_PERMANENT,
1.70      atatat   1687:                       CTLTYPE_NODE, "net", NULL,
                   1688:                       NULL, 0, NULL, 0,
                   1689:                       CTL_NET, CTL_EOL);
1.73      atatat   1690:        sysctl_createv(clog, 0, NULL, NULL,
                   1691:                       CTLFLAG_PERMANENT,
1.74      atatat   1692:                       CTLTYPE_NODE, "inet6",
                   1693:                       SYSCTL_DESCR("PF_INET6 related settings"),
1.70      atatat   1694:                       NULL, 0, NULL, 0,
                   1695:                       CTL_NET, PF_INET6, CTL_EOL);
1.73      atatat   1696:        sysctl_createv(clog, 0, NULL, NULL,
                   1697:                       CTLFLAG_PERMANENT,
1.74      atatat   1698:                       CTLTYPE_NODE, "ip6",
                   1699:                       SYSCTL_DESCR("IPv6 related settings"),
1.70      atatat   1700:                       NULL, 0, NULL, 0,
                   1701:                       CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_EOL);
                   1702:
1.73      atatat   1703:        sysctl_createv(clog, 0, NULL, NULL,
                   1704:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1705:                       CTLTYPE_INT, "forwarding",
                   1706:                       SYSCTL_DESCR("Enable forwarding of INET6 datagrams"),
1.70      atatat   1707:                       NULL, 0, &ip6_forwarding, 0,
                   1708:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1709:                       IPV6CTL_FORWARDING, CTL_EOL);
1.73      atatat   1710:        sysctl_createv(clog, 0, NULL, NULL,
                   1711:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1712:                       CTLTYPE_INT, "redirect",
                   1713:                       SYSCTL_DESCR("Enable sending of ICMPv6 redirect messages"),
1.70      atatat   1714:                       NULL, 0, &ip6_sendredirects, 0,
                   1715:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1716:                       IPV6CTL_SENDREDIRECTS, CTL_EOL);
1.73      atatat   1717:        sysctl_createv(clog, 0, NULL, NULL,
                   1718:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1719:                       CTLTYPE_INT, "hlim",
                   1720:                       SYSCTL_DESCR("Hop limit for an INET6 datagram"),
1.70      atatat   1721:                       NULL, 0, &ip6_defhlim, 0,
                   1722:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1723:                       IPV6CTL_DEFHLIM, CTL_EOL);
                   1724: #ifdef notyet
1.73      atatat   1725:        sysctl_createv(clog, 0, NULL, NULL,
                   1726:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70      atatat   1727:                       CTLTYPE_INT, "mtu", NULL,
                   1728:                       NULL, 0, &, 0,
                   1729:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1730:                       IPV6CTL_DEFMTU, CTL_EOL);
                   1731: #endif
                   1732: #ifdef __no_idea__
1.73      atatat   1733:        sysctl_createv(clog, 0, NULL, NULL,
                   1734:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70      atatat   1735:                       CTLTYPE_INT, "forwsrcrt", NULL,
                   1736:                       NULL, 0, &?, 0,
                   1737:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1738:                       IPV6CTL_FORWSRCRT, CTL_EOL);
1.73      atatat   1739:        sysctl_createv(clog, 0, NULL, NULL,
                   1740:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70      atatat   1741:                       CTLTYPE_STRUCT, "mrtstats", NULL,
                   1742:                       NULL, 0, &?, sizeof(?),
                   1743:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1744:                       IPV6CTL_MRTSTATS, CTL_EOL);
1.73      atatat   1745:        sysctl_createv(clog, 0, NULL, NULL,
                   1746:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70      atatat   1747:                       CTLTYPE_?, "mrtproto", NULL,
                   1748:                       NULL, 0, &?, sizeof(?),
                   1749:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1750:                       IPV6CTL_MRTPROTO, CTL_EOL);
                   1751: #endif
1.73      atatat   1752:        sysctl_createv(clog, 0, NULL, NULL,
                   1753:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1754:                       CTLTYPE_INT, "maxfragpackets",
                   1755:                       SYSCTL_DESCR("Maximum number of fragments to buffer "
                   1756:                                    "for reassembly"),
1.70      atatat   1757:                       NULL, 0, &ip6_maxfragpackets, 0,
                   1758:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1759:                       IPV6CTL_MAXFRAGPACKETS, CTL_EOL);
                   1760: #ifdef __no_idea__
1.73      atatat   1761:        sysctl_createv(clog, 0, NULL, NULL,
                   1762:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70      atatat   1763:                       CTLTYPE_INT, "sourcecheck", NULL,
                   1764:                       NULL, 0, &?, 0,
                   1765:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1766:                       IPV6CTL_SOURCECHECK, CTL_EOL);
1.73      atatat   1767:        sysctl_createv(clog, 0, NULL, NULL,
                   1768:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70      atatat   1769:                       CTLTYPE_INT, "sourcecheck_logint", NULL,
                   1770:                       NULL, 0, &?, 0,
                   1771:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1772:                       IPV6CTL_SOURCECHECK_LOGINT, CTL_EOL);
                   1773: #endif
1.73      atatat   1774:        sysctl_createv(clog, 0, NULL, NULL,
                   1775:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1776:                       CTLTYPE_INT, "accept_rtadv",
                   1777:                       SYSCTL_DESCR("Accept router advertisements"),
1.70      atatat   1778:                       NULL, 0, &ip6_accept_rtadv, 0,
                   1779:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1780:                       IPV6CTL_ACCEPT_RTADV, CTL_EOL);
1.73      atatat   1781:        sysctl_createv(clog, 0, NULL, NULL,
                   1782:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1783:                       CTLTYPE_INT, "keepfaith",
                   1784:                       SYSCTL_DESCR("Activate faith interface"),
1.70      atatat   1785:                       NULL, 0, &ip6_keepfaith, 0,
                   1786:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1787:                       IPV6CTL_KEEPFAITH, CTL_EOL);
1.73      atatat   1788:        sysctl_createv(clog, 0, NULL, NULL,
                   1789:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1790:                       CTLTYPE_INT, "log_interval",
                   1791:                       SYSCTL_DESCR("Minumum interval between logging "
                   1792:                                    "unroutable packets"),
1.70      atatat   1793:                       NULL, 0, &ip6_log_interval, 0,
                   1794:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1795:                       IPV6CTL_LOG_INTERVAL, CTL_EOL);
1.73      atatat   1796:        sysctl_createv(clog, 0, NULL, NULL,
                   1797:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1798:                       CTLTYPE_INT, "hdrnestlimit",
                   1799:                       SYSCTL_DESCR("Maximum number of nested IPv6 headers"),
1.70      atatat   1800:                       NULL, 0, &ip6_hdrnestlimit, 0,
                   1801:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1802:                       IPV6CTL_HDRNESTLIMIT, CTL_EOL);
1.73      atatat   1803:        sysctl_createv(clog, 0, NULL, NULL,
                   1804:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1805:                       CTLTYPE_INT, "dad_count",
                   1806:                       SYSCTL_DESCR("Number of Duplicate Address Detection "
                   1807:                                    "probes to send"),
1.70      atatat   1808:                       NULL, 0, &ip6_dad_count, 0,
                   1809:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1810:                       IPV6CTL_DAD_COUNT, CTL_EOL);
1.73      atatat   1811:        sysctl_createv(clog, 0, NULL, NULL,
                   1812:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1813:                       CTLTYPE_INT, "auto_flowlabel",
                   1814:                       SYSCTL_DESCR("Assign random IPv6 flow labels"),
1.70      atatat   1815:                       NULL, 0, &ip6_auto_flowlabel, 0,
                   1816:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1817:                       IPV6CTL_AUTO_FLOWLABEL, CTL_EOL);
1.73      atatat   1818:        sysctl_createv(clog, 0, NULL, NULL,
                   1819:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1820:                       CTLTYPE_INT, "defmcasthlim",
                   1821:                       SYSCTL_DESCR("Default multicast hop limit"),
1.70      atatat   1822:                       NULL, 0, &ip6_defmcasthlim, 0,
                   1823:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1824:                       IPV6CTL_DEFMCASTHLIM, CTL_EOL);
1.51      itojun   1825: #if NGIF > 0
1.73      atatat   1826:        sysctl_createv(clog, 0, NULL, NULL,
                   1827:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1828:                       CTLTYPE_INT, "gifhlim",
                   1829:                       SYSCTL_DESCR("Default hop limit for a gif tunnel datagram"),
1.70      atatat   1830:                       NULL, 0, &ip6_gif_hlim, 0,
                   1831:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1832:                       IPV6CTL_GIF_HLIM, CTL_EOL);
                   1833: #endif /* NGIF */
1.73      atatat   1834:        sysctl_createv(clog, 0, NULL, NULL,
                   1835:                       CTLFLAG_PERMANENT,
1.74      atatat   1836:                       CTLTYPE_STRING, "kame_version",
                   1837:                       SYSCTL_DESCR("KAME Version"),
1.78      christos 1838:                       NULL, 0, __UNCONST(__KAME_VERSION), 0,
1.70      atatat   1839:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1840:                       IPV6CTL_KAME_VERSION, CTL_EOL);
1.73      atatat   1841:        sysctl_createv(clog, 0, NULL, NULL,
                   1842:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1843:                       CTLTYPE_INT, "use_deprecated",
                   1844:                       SYSCTL_DESCR("Allow use of deprecated addresses as "
                   1845:                                    "source addresses"),
1.70      atatat   1846:                       NULL, 0, &ip6_use_deprecated, 0,
                   1847:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1848:                       IPV6CTL_USE_DEPRECATED, CTL_EOL);
1.73      atatat   1849:        sysctl_createv(clog, 0, NULL, NULL,
                   1850:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70      atatat   1851:                       CTLTYPE_INT, "rr_prune", NULL,
                   1852:                       NULL, 0, &ip6_rr_prune, 0,
                   1853:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1854:                       IPV6CTL_RR_PRUNE, CTL_EOL);
1.73      atatat   1855:        sysctl_createv(clog, 0, NULL, NULL,
                   1856:                       CTLFLAG_PERMANENT
1.70      atatat   1857: #ifndef INET6_BINDV6ONLY
1.73      atatat   1858:                       |CTLFLAG_READWRITE,
1.70      atatat   1859: #endif
1.74      atatat   1860:                       CTLTYPE_INT, "v6only",
                   1861:                       SYSCTL_DESCR("Disallow PF_INET6 sockets from connecting "
                   1862:                                    "to PF_INET sockets"),
1.70      atatat   1863:                       NULL, 0, &ip6_v6only, 0,
                   1864:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1865:                       IPV6CTL_V6ONLY, CTL_EOL);
1.73      atatat   1866:        sysctl_createv(clog, 0, NULL, NULL,
                   1867:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1868:                       CTLTYPE_INT, "anonportmin",
                   1869:                       SYSCTL_DESCR("Lowest ephemeral port number to assign"),
1.70      atatat   1870:                       sysctl_net_inet_ip_ports, 0, &ip6_anonportmin, 0,
                   1871:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1872:                       IPV6CTL_ANONPORTMIN, CTL_EOL);
1.73      atatat   1873:        sysctl_createv(clog, 0, NULL, NULL,
                   1874:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1875:                       CTLTYPE_INT, "anonportmax",
                   1876:                       SYSCTL_DESCR("Highest ephemeral port number to assign"),
1.70      atatat   1877:                       sysctl_net_inet_ip_ports, 0, &ip6_anonportmax, 0,
                   1878:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1879:                       IPV6CTL_ANONPORTMAX, CTL_EOL);
1.26      itojun   1880: #ifndef IPNOPRIVPORTS
1.73      atatat   1881:        sysctl_createv(clog, 0, NULL, NULL,
                   1882:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1883:                       CTLTYPE_INT, "lowportmin",
                   1884:                       SYSCTL_DESCR("Lowest privileged ephemeral port number "
                   1885:                                    "to assign"),
1.70      atatat   1886:                       sysctl_net_inet_ip_ports, 0, &ip6_lowportmin, 0,
                   1887:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1888:                       IPV6CTL_LOWPORTMIN, CTL_EOL);
1.73      atatat   1889:        sysctl_createv(clog, 0, NULL, NULL,
                   1890:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1891:                       CTLTYPE_INT, "lowportmax",
                   1892:                       SYSCTL_DESCR("Highest privileged ephemeral port number "
                   1893:                                    "to assign"),
1.70      atatat   1894:                       sysctl_net_inet_ip_ports, 0, &ip6_lowportmax, 0,
                   1895:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1896:                       IPV6CTL_LOWPORTMAX, CTL_EOL);
                   1897: #endif /* IPNOPRIVPORTS */
1.73      atatat   1898:        sysctl_createv(clog, 0, NULL, NULL,
                   1899:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.83      rpaulo   1900:                       CTLTYPE_INT, "use_tempaddr",
                   1901:                       SYSCTL_DESCR("Use temporary address"),
                   1902:                       NULL, 0, &ip6_use_tempaddr, 0,
                   1903:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1904:                       CTL_CREATE, CTL_EOL);
                   1905:        sysctl_createv(clog, 0, NULL, NULL,
                   1906:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1907:                       CTLTYPE_INT, "temppltime",
                   1908:                       SYSCTL_DESCR("preferred lifetime of a temporary address"),
                   1909:                       NULL, 0, &ip6_temp_preferred_lifetime, 0,
                   1910:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1911:                       CTL_CREATE, CTL_EOL);
                   1912:        sysctl_createv(clog, 0, NULL, NULL,
                   1913:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1914:                       CTLTYPE_INT, "tempvltime",
                   1915:                       SYSCTL_DESCR("valid lifetime of a temporary address"),
                   1916:                       NULL, 0, &ip6_temp_valid_lifetime, 0,
                   1917:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1918:                       CTL_CREATE, CTL_EOL);
                   1919:        sysctl_createv(clog, 0, NULL, NULL,
                   1920:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1921:                       CTLTYPE_INT, "maxfrags",
                   1922:                       SYSCTL_DESCR("Maximum fragments in reassembly queue"),
1.70      atatat   1923:                       NULL, 0, &ip6_maxfrags, 0,
                   1924:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1925:                       IPV6CTL_MAXFRAGS, CTL_EOL);
1.79      rpaulo   1926:        sysctl_createv(clog, 0, NULL, NULL,
                   1927:                       CTLFLAG_PERMANENT,
                   1928:                       CTLTYPE_STRUCT, "stats",
                   1929:                       SYSCTL_DESCR("IPv6 statistics"),
                   1930:                       NULL, 0, &ip6stat, sizeof(ip6stat),
                   1931:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1932:                       IPV6CTL_STATS, CTL_EOL);
1.81      rpaulo   1933:        sysctl_createv(clog, 0, NULL, NULL,
                   1934:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1935:                       CTLTYPE_INT, "use_defaultzone",
                   1936:                       SYSCTL_DESCR("Whether to use the default scope zones"),
                   1937:                       NULL, 0, &ip6_use_defzone, 0,
                   1938:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1939:                       IPV6CTL_USE_DEFAULTZONE, CTL_EOL);
1.83      rpaulo   1940:        sysctl_createv(clog, 0, NULL, NULL,
                   1941:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1942:                       CTLTYPE_INT, "mcast_pmtu",
                   1943:                       SYSCTL_DESCR("Enable pMTU discovery for multicast packet"),
                   1944:                       NULL, 0, &ip6_mcast_pmtu, 0,
                   1945:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1946:                       CTL_CREATE, CTL_EOL);
1.98      liamjfoy 1947: #ifdef GATEWAY
                   1948:        sysctl_createv(clog, 0, NULL, NULL,
                   1949:                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1950:                        CTLTYPE_INT, "maxflows",
                   1951:                        SYSCTL_DESCR("Number of flows for fast forwarding (IPv6)"),
1.101     liamjfoy 1952:                        sysctl_net_inet6_ip6_maxflows, 0, &ip6_maxflows, 0,
1.98      liamjfoy 1953:                        CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1954:                        CTL_CREATE, CTL_EOL);
1.99      liamjfoy 1955:        sysctl_createv(clog, 0, NULL, NULL,
                   1956:                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1957:                        CTLTYPE_INT, "hashsize",
                   1958:                        SYSCTL_DESCR("Size of hash table for fast forwarding (IPv6)"),
                   1959:                        sysctl_net_inet6_ip6_hashsize, 0, &ip6_hashsize, 0,
                   1960:                        CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1961:                        CTL_CREATE, CTL_EOL);
1.98      liamjfoy 1962: #endif
1.2       itojun   1963: }

CVSweb <webmaster@jp.NetBSD.org>