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

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

CVSweb <webmaster@jp.NetBSD.org>