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

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

CVSweb <webmaster@jp.NetBSD.org>