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

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

CVSweb <webmaster@jp.NetBSD.org>