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

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

CVSweb <webmaster@jp.NetBSD.org>