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

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

CVSweb <webmaster@jp.NetBSD.org>