[BACK]Return to ip6_input.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / netinet6

Annotation of src/sys/netinet6/ip6_input.c, Revision 1.164.2.2

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

CVSweb <webmaster@jp.NetBSD.org>