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

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

CVSweb <webmaster@jp.NetBSD.org>