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

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

CVSweb <webmaster@jp.NetBSD.org>