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

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

CVSweb <webmaster@jp.NetBSD.org>