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

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

CVSweb <webmaster@jp.NetBSD.org>