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

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

CVSweb <webmaster@jp.NetBSD.org>