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

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

CVSweb <webmaster@jp.NetBSD.org>