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

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

CVSweb <webmaster@jp.NetBSD.org>