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

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

CVSweb <webmaster@jp.NetBSD.org>