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

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

CVSweb <webmaster@jp.NetBSD.org>