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

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

CVSweb <webmaster@jp.NetBSD.org>