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

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

CVSweb <webmaster@jp.NetBSD.org>