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

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

CVSweb <webmaster@jp.NetBSD.org>