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

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

CVSweb <webmaster@jp.NetBSD.org>