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

1.179   ! christos    1: /*     $NetBSD: ip6_input.c,v 1.178 2017/06/01 02:45:14 chs 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.179   ! christos   65: __KERNEL_RCSID(0, "$NetBSD: ip6_input.c,v 1.178 2017/06/01 02:45:14 chs 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:                                SOFTNET_LOCK();
                    794:                                error = ipsec6_input(m);
                    795:                                SOFTNET_UNLOCK();
1.146     christos  796:                                if (error)
                    797:                                        goto bad;
                    798:                        }
1.94      degroote  799:                }
1.142     christos  800: #endif /* IPSEC */
1.94      degroote  801:
1.169     ozaki-r   802:                SOFTNET_LOCK();
1.2       itojun    803:                nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
1.169     ozaki-r   804:                SOFTNET_UNLOCK();
1.2       itojun    805:        }
                    806:        return;
1.171     ozaki-r   807:
                    808:  bad_unref:
                    809:        rtcache_unref(rt, ro);
                    810:        percpu_putref(ip6_forward_rt_percpu);
1.2       itojun    811:  bad:
                    812:        m_freem(m);
1.171     ozaki-r   813:        return;
1.2       itojun    814: }
                    815:
                    816: /*
1.81      rpaulo    817:  * set/grab in6_ifaddr correspond to IPv6 destination address.
                    818:  */
                    819: static struct m_tag *
1.112     dyoung    820: ip6_setdstifaddr(struct mbuf *m, const struct in6_ifaddr *ia)
1.81      rpaulo    821: {
                    822:        struct m_tag *mtag;
1.144     christos  823:        struct ip6aux *ip6a;
1.81      rpaulo    824:
                    825:        mtag = ip6_addaux(m);
1.144     christos  826:        if (mtag == NULL)
                    827:                return NULL;
1.112     dyoung    828:
1.144     christos  829:        ip6a = (struct ip6aux *)(mtag + 1);
                    830:        if (in6_setscope(&ip6a->ip6a_src, ia->ia_ifp, &ip6a->ip6a_scope_id)) {
                    831:                IP6_STATINC(IP6_STAT_BADSCOPE);
                    832:                return NULL;
1.112     dyoung    833:        }
1.144     christos  834:
                    835:        ip6a->ip6a_src = ia->ia_addr.sin6_addr;
                    836:        ip6a->ip6a_flags = ia->ia6_flags;
                    837:        return mtag;
1.81      rpaulo    838: }
                    839:
1.112     dyoung    840: const struct ip6aux *
1.107     christos  841: ip6_getdstifaddr(struct mbuf *m)
1.81      rpaulo    842: {
                    843:        struct m_tag *mtag;
                    844:
                    845:        mtag = ip6_findaux(m);
1.112     dyoung    846:        if (mtag != NULL)
                    847:                return (struct ip6aux *)(mtag + 1);
1.81      rpaulo    848:        else
                    849:                return NULL;
                    850: }
                    851:
                    852: /*
1.2       itojun    853:  * Hop-by-Hop options header processing. If a valid jumbo payload option is
                    854:  * included, the real payload length will be stored in plenp.
1.107     christos  855:  *
                    856:  * rtalertp - XXX: should be stored more smart way
1.2       itojun    857:  */
1.136     drochner  858: int
1.107     christos  859: ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
                    860:        struct mbuf **mp, int *offp)
1.2       itojun    861: {
1.35      itojun    862:        struct mbuf *m = *mp;
1.2       itojun    863:        int off = *offp, hbhlen;
                    864:        struct ip6_hbh *hbh;
                    865:
                    866:        /* validation of the length of the header */
1.9       itojun    867:        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                    868:                sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
                    869:        if (hbh == NULL) {
1.116     thorpej   870:                IP6_STATINC(IP6_STAT_TOOSHORT);
1.9       itojun    871:                return -1;
                    872:        }
                    873:        hbhlen = (hbh->ip6h_len + 1) << 3;
                    874:        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
                    875:                hbhlen);
                    876:        if (hbh == NULL) {
1.116     thorpej   877:                IP6_STATINC(IP6_STAT_TOOSHORT);
1.9       itojun    878:                return -1;
                    879:        }
1.57      thorpej   880:        KASSERT(IP6_HDR_ALIGNED_P(hbh));
1.2       itojun    881:        off += hbhlen;
                    882:        hbhlen -= sizeof(struct ip6_hbh);
                    883:
                    884:        if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
                    885:                                hbhlen, rtalertp, plenp) < 0)
1.58      itojun    886:                return (-1);
1.2       itojun    887:
                    888:        *offp = off;
                    889:        *mp = m;
1.58      itojun    890:        return (0);
1.2       itojun    891: }
                    892:
                    893: /*
                    894:  * Search header for all Hop-by-hop options and process each option.
                    895:  * This function is separate from ip6_hopopts_input() in order to
                    896:  * handle a case where the sending node itself process its hop-by-hop
                    897:  * options header. In such a case, the function is called from ip6_output().
1.55      itojun    898:  *
                    899:  * The function assumes that hbh header is located right after the IPv6 header
                    900:  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
                    901:  * opthead + hbhlen is located in continuous memory region.
1.2       itojun    902:  */
1.136     drochner  903: static int
1.107     christos  904: ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
                    905:        u_int32_t *rtalertp, u_int32_t *plenp)
1.2       itojun    906: {
                    907:        struct ip6_hdr *ip6;
                    908:        int optlen = 0;
                    909:        u_int8_t *opt = opthead;
                    910:        u_int16_t rtalert_val;
1.22      itojun    911:        u_int32_t jumboplen;
1.55      itojun    912:        const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1.2       itojun    913:
                    914:        for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1.35      itojun    915:                switch (*opt) {
                    916:                case IP6OPT_PAD1:
                    917:                        optlen = 1;
                    918:                        break;
                    919:                case IP6OPT_PADN:
                    920:                        if (hbhlen < IP6OPT_MINLEN) {
1.116     thorpej   921:                                IP6_STATINC(IP6_STAT_TOOSMALL);
1.35      itojun    922:                                goto bad;
                    923:                        }
                    924:                        optlen = *(opt + 1) + 2;
                    925:                        break;
                    926:                case IP6OPT_RTALERT:
                    927:                        /* XXX may need check for alignment */
                    928:                        if (hbhlen < IP6OPT_RTALERT_LEN) {
1.116     thorpej   929:                                IP6_STATINC(IP6_STAT_TOOSMALL);
1.35      itojun    930:                                goto bad;
                    931:                        }
                    932:                        if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1.55      itojun    933:                                /* XXX stat */
                    934:                                icmp6_error(m, ICMP6_PARAM_PROB,
                    935:                                    ICMP6_PARAMPROB_HEADER,
                    936:                                    erroff + opt + 1 - opthead);
                    937:                                return (-1);
1.35      itojun    938:                        }
                    939:                        optlen = IP6OPT_RTALERT_LEN;
1.126     tsutsui   940:                        memcpy((void *)&rtalert_val, (void *)(opt + 2), 2);
1.35      itojun    941:                        *rtalertp = ntohs(rtalert_val);
                    942:                        break;
                    943:                case IP6OPT_JUMBO:
1.22      itojun    944:                        /* XXX may need check for alignment */
                    945:                        if (hbhlen < IP6OPT_JUMBO_LEN) {
1.116     thorpej   946:                                IP6_STATINC(IP6_STAT_TOOSMALL);
1.22      itojun    947:                                goto bad;
                    948:                        }
1.35      itojun    949:                        if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1.55      itojun    950:                                /* XXX stat */
                    951:                                icmp6_error(m, ICMP6_PARAM_PROB,
                    952:                                    ICMP6_PARAMPROB_HEADER,
                    953:                                    erroff + opt + 1 - opthead);
                    954:                                return (-1);
1.35      itojun    955:                        }
1.22      itojun    956:                        optlen = IP6OPT_JUMBO_LEN;
                    957:
                    958:                        /*
                    959:                         * IPv6 packets that have non 0 payload length
1.35      itojun    960:                         * must not contain a jumbo payload option.
1.22      itojun    961:                         */
                    962:                        ip6 = mtod(m, struct ip6_hdr *);
                    963:                        if (ip6->ip6_plen) {
1.116     thorpej   964:                                IP6_STATINC(IP6_STAT_BADOPTIONS);
1.22      itojun    965:                                icmp6_error(m, ICMP6_PARAM_PROB,
1.55      itojun    966:                                    ICMP6_PARAMPROB_HEADER,
                    967:                                    erroff + opt - opthead);
                    968:                                return (-1);
1.22      itojun    969:                        }
1.2       itojun    970:
1.22      itojun    971:                        /*
                    972:                         * We may see jumbolen in unaligned location, so
                    973:                         * we'd need to perform bcopy().
                    974:                         */
1.126     tsutsui   975:                        memcpy(&jumboplen, opt + 2, sizeof(jumboplen));
1.22      itojun    976:                        jumboplen = (u_int32_t)htonl(jumboplen);
                    977:
                    978: #if 1
                    979:                        /*
                    980:                         * if there are multiple jumbo payload options,
                    981:                         * *plenp will be non-zero and the packet will be
                    982:                         * rejected.
                    983:                         * the behavior may need some debate in ipngwg -
                    984:                         * multiple options does not make sense, however,
                    985:                         * there's no explicit mention in specification.
                    986:                         */
                    987:                        if (*plenp != 0) {
1.116     thorpej   988:                                IP6_STATINC(IP6_STAT_BADOPTIONS);
1.22      itojun    989:                                icmp6_error(m, ICMP6_PARAM_PROB,
1.55      itojun    990:                                    ICMP6_PARAMPROB_HEADER,
                    991:                                    erroff + opt + 2 - opthead);
                    992:                                return (-1);
1.22      itojun    993:                        }
1.8       itojun    994: #endif
1.2       itojun    995:
1.22      itojun    996:                        /*
                    997:                         * jumbo payload length must be larger than 65535.
                    998:                         */
                    999:                        if (jumboplen <= IPV6_MAXPACKET) {
1.116     thorpej  1000:                                IP6_STATINC(IP6_STAT_BADOPTIONS);
1.22      itojun   1001:                                icmp6_error(m, ICMP6_PARAM_PROB,
1.55      itojun   1002:                                    ICMP6_PARAMPROB_HEADER,
                   1003:                                    erroff + opt + 2 - opthead);
                   1004:                                return (-1);
1.22      itojun   1005:                        }
                   1006:                        *plenp = jumboplen;
                   1007:
                   1008:                        break;
1.35      itojun   1009:                default:                /* unknown option */
                   1010:                        if (hbhlen < IP6OPT_MINLEN) {
1.116     thorpej  1011:                                IP6_STATINC(IP6_STAT_TOOSMALL);
1.35      itojun   1012:                                goto bad;
                   1013:                        }
1.55      itojun   1014:                        optlen = ip6_unknown_opt(opt, m,
                   1015:                            erroff + opt - opthead);
                   1016:                        if (optlen == -1)
                   1017:                                return (-1);
1.35      itojun   1018:                        optlen += 2;
                   1019:                        break;
1.2       itojun   1020:                }
                   1021:        }
                   1022:
1.55      itojun   1023:        return (0);
1.2       itojun   1024:
                   1025:   bad:
                   1026:        m_freem(m);
1.55      itojun   1027:        return (-1);
1.2       itojun   1028: }
                   1029:
                   1030: /*
                   1031:  * Unknown option processing.
                   1032:  * The third argument `off' is the offset from the IPv6 header to the option,
                   1033:  * which is necessary if the IPv6 header the and option header and IPv6 header
                   1034:  * is not continuous in order to return an ICMPv6 error.
                   1035:  */
                   1036: int
1.107     christos 1037: ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1.2       itojun   1038: {
                   1039:        struct ip6_hdr *ip6;
                   1040:
1.35      itojun   1041:        switch (IP6OPT_TYPE(*optp)) {
                   1042:        case IP6OPT_TYPE_SKIP: /* ignore the option */
1.58      itojun   1043:                return ((int)*(optp + 1));
1.35      itojun   1044:        case IP6OPT_TYPE_DISCARD:       /* silently discard */
                   1045:                m_freem(m);
1.58      itojun   1046:                return (-1);
1.35      itojun   1047:        case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1.116     thorpej  1048:                IP6_STATINC(IP6_STAT_BADOPTIONS);
1.35      itojun   1049:                icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1.58      itojun   1050:                return (-1);
1.35      itojun   1051:        case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1.116     thorpej  1052:                IP6_STATINC(IP6_STAT_BADOPTIONS);
1.35      itojun   1053:                ip6 = mtod(m, struct ip6_hdr *);
                   1054:                if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
                   1055:                    (m->m_flags & (M_BCAST|M_MCAST)))
                   1056:                        m_freem(m);
                   1057:                else
                   1058:                        icmp6_error(m, ICMP6_PARAM_PROB,
                   1059:                                    ICMP6_PARAMPROB_OPTION, off);
1.58      itojun   1060:                return (-1);
1.2       itojun   1061:        }
                   1062:
                   1063:        m_freem(m);             /* XXX: NOTREACHED */
1.58      itojun   1064:        return (-1);
1.2       itojun   1065: }
                   1066:
                   1067: /*
1.9       itojun   1068:  * Create the "control" list for this pcb.
                   1069:  *
                   1070:  * The routine will be called from upper layer handlers like tcp6_input().
                   1071:  * Thus the routine assumes that the caller (tcp6_input) have already
                   1072:  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
                   1073:  * very first mbuf on the mbuf chain.
                   1074:  * We may want to add some infinite loop prevention or sanity checks for safety.
                   1075:  * (This applies only when you are using KAME mbuf chain restriction, i.e.
                   1076:  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
1.2       itojun   1077:  */
                   1078: void
1.107     christos 1079: ip6_savecontrol(struct in6pcb *in6p, struct mbuf **mp,
                   1080:        struct ip6_hdr *ip6, struct mbuf *m)
1.2       itojun   1081: {
1.179   ! christos 1082:        struct socket *so = in6p->in6p_socket;
1.85      rpaulo   1083: #ifdef RFC2292
                   1084: #define IS2292(x, y)   ((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
                   1085: #else
                   1086: #define IS2292(x, y)   (y)
                   1087: #endif
                   1088:
1.179   ! christos 1089:        if (SOOPT_TIMESTAMP(so->so_options))
        !          1090:                mp = sbsavetimestamp(so->so_options, m, mp);
1.87      rpaulo   1091:
                   1092:        /* some OSes call this logic with IPv4 packet, for SO_TIMESTAMP */
                   1093:        if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
                   1094:                return;
                   1095:
1.2       itojun   1096:        /* RFC 2292 sec. 5 */
1.35      itojun   1097:        if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1.2       itojun   1098:                struct in6_pktinfo pi6;
1.85      rpaulo   1099:
1.126     tsutsui  1100:                memcpy(&pi6.ipi6_addr, &ip6->ip6_dst, sizeof(struct in6_addr));
1.81      rpaulo   1101:                in6_clearscope(&pi6.ipi6_addr); /* XXX */
1.160     ozaki-r  1102:                pi6.ipi6_ifindex = m->m_pkthdr.rcvif_index;
1.97      christos 1103:                *mp = sbcreatecontrol((void *) &pi6,
1.85      rpaulo   1104:                    sizeof(struct in6_pktinfo),
                   1105:                    IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1.2       itojun   1106:                if (*mp)
                   1107:                        mp = &(*mp)->m_next;
                   1108:        }
1.85      rpaulo   1109:
1.2       itojun   1110:        if (in6p->in6p_flags & IN6P_HOPLIMIT) {
                   1111:                int hlim = ip6->ip6_hlim & 0xff;
1.85      rpaulo   1112:
1.97      christos 1113:                *mp = sbcreatecontrol((void *) &hlim, sizeof(int),
1.85      rpaulo   1114:                    IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
                   1115:                if (*mp)
                   1116:                        mp = &(*mp)->m_next;
                   1117:        }
                   1118:
                   1119:        if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
                   1120:                u_int32_t flowinfo;
                   1121:                int tclass;
                   1122:
                   1123:                flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
                   1124:                flowinfo >>= 20;
                   1125:
                   1126:                tclass = flowinfo & 0xff;
1.97      christos 1127:                *mp = sbcreatecontrol((void *)&tclass, sizeof(tclass),
1.85      rpaulo   1128:                    IPV6_TCLASS, IPPROTO_IPV6);
                   1129:
1.2       itojun   1130:                if (*mp)
                   1131:                        mp = &(*mp)->m_next;
                   1132:        }
                   1133:
                   1134:        /*
1.75      itojun   1135:         * IPV6_HOPOPTS socket option.  Recall that we required super-user
                   1136:         * privilege for the option (see ip6_ctloutput), but it might be too
                   1137:         * strict, since there might be some hop-by-hop options which can be
                   1138:         * returned to normal user.
1.85      rpaulo   1139:         * See also RFC3542 section 8 (or RFC2292 section 6).
1.2       itojun   1140:         */
1.75      itojun   1141:        if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
1.2       itojun   1142:                /*
                   1143:                 * Check if a hop-by-hop options header is contatined in the
                   1144:                 * received packet, and if so, store the options as ancillary
                   1145:                 * data. Note that a hop-by-hop options header must be
                   1146:                 * just after the IPv6 header, which fact is assured through
                   1147:                 * the IPv6 input processing.
                   1148:                 */
1.78      christos 1149:                struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
                   1150:                if (xip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9       itojun   1151:                        struct ip6_hbh *hbh;
                   1152:                        int hbhlen;
1.62      itojun   1153:                        struct mbuf *ext;
1.9       itojun   1154:
1.62      itojun   1155:                        ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1.78      christos 1156:                            xip6->ip6_nxt);
1.62      itojun   1157:                        if (ext == NULL) {
1.116     thorpej  1158:                                IP6_STATINC(IP6_STAT_TOOSHORT);
1.9       itojun   1159:                                return;
                   1160:                        }
1.62      itojun   1161:                        hbh = mtod(ext, struct ip6_hbh *);
1.9       itojun   1162:                        hbhlen = (hbh->ip6h_len + 1) << 3;
1.62      itojun   1163:                        if (hbhlen != ext->m_len) {
                   1164:                                m_freem(ext);
1.116     thorpej  1165:                                IP6_STATINC(IP6_STAT_TOOSHORT);
1.9       itojun   1166:                                return;
                   1167:                        }
1.2       itojun   1168:
                   1169:                        /*
                   1170:                         * XXX: We copy whole the header even if a jumbo
                   1171:                         * payload option is included, which option is to
                   1172:                         * be removed before returning in the RFC 2292.
1.85      rpaulo   1173:                         * Note: this constraint is removed in RFC3542.
1.2       itojun   1174:                         */
1.97      christos 1175:                        *mp = sbcreatecontrol((void *)hbh, hbhlen,
1.85      rpaulo   1176:                            IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
                   1177:                            IPPROTO_IPV6);
1.2       itojun   1178:                        if (*mp)
                   1179:                                mp = &(*mp)->m_next;
1.62      itojun   1180:                        m_freem(ext);
1.2       itojun   1181:                }
                   1182:        }
                   1183:
                   1184:        /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
                   1185:        if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
1.78      christos 1186:                struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
                   1187:                int nxt = xip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1.2       itojun   1188:
                   1189:                /*
                   1190:                 * Search for destination options headers or routing
                   1191:                 * header(s) through the header chain, and stores each
                   1192:                 * header as ancillary data.
                   1193:                 * Note that the order of the headers remains in
                   1194:                 * the chain of ancillary data.
                   1195:                 */
1.86      rpaulo   1196:                for (;;) {      /* is explicit loop prevention necessary? */
1.62      itojun   1197:                        struct ip6_ext *ip6e = NULL;
1.9       itojun   1198:                        int elen;
1.62      itojun   1199:                        struct mbuf *ext = NULL;
1.9       itojun   1200:
1.62      itojun   1201:                        /*
                   1202:                         * if it is not an extension header, don't try to
                   1203:                         * pull it from the chain.
                   1204:                         */
                   1205:                        switch (nxt) {
                   1206:                        case IPPROTO_DSTOPTS:
                   1207:                        case IPPROTO_ROUTING:
                   1208:                        case IPPROTO_HOPOPTS:
                   1209:                        case IPPROTO_AH: /* is it possible? */
                   1210:                                break;
                   1211:                        default:
                   1212:                                goto loopend;
                   1213:                        }
                   1214:
                   1215:                        ext = ip6_pullexthdr(m, off, nxt);
                   1216:                        if (ext == NULL) {
1.116     thorpej  1217:                                IP6_STATINC(IP6_STAT_TOOSHORT);
1.9       itojun   1218:                                return;
                   1219:                        }
1.62      itojun   1220:                        ip6e = mtod(ext, struct ip6_ext *);
1.9       itojun   1221:                        if (nxt == IPPROTO_AH)
                   1222:                                elen = (ip6e->ip6e_len + 2) << 2;
                   1223:                        else
                   1224:                                elen = (ip6e->ip6e_len + 1) << 3;
1.62      itojun   1225:                        if (elen != ext->m_len) {
                   1226:                                m_freem(ext);
1.116     thorpej  1227:                                IP6_STATINC(IP6_STAT_TOOSHORT);
1.9       itojun   1228:                                return;
                   1229:                        }
1.57      thorpej  1230:                        KASSERT(IP6_HDR_ALIGNED_P(ip6e));
1.2       itojun   1231:
1.35      itojun   1232:                        switch (nxt) {
1.62      itojun   1233:                        case IPPROTO_DSTOPTS:
1.127     martin   1234:                                if (!(in6p->in6p_flags & IN6P_DSTOPTS))
1.35      itojun   1235:                                        break;
                   1236:
1.97      christos 1237:                                *mp = sbcreatecontrol((void *)ip6e, elen,
1.85      rpaulo   1238:                                    IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
                   1239:                                    IPPROTO_IPV6);
1.35      itojun   1240:                                if (*mp)
                   1241:                                        mp = &(*mp)->m_next;
                   1242:                                break;
                   1243:
                   1244:                        case IPPROTO_ROUTING:
1.127     martin   1245:                                if (!(in6p->in6p_flags & IN6P_RTHDR))
1.35      itojun   1246:                                        break;
                   1247:
1.97      christos 1248:                                *mp = sbcreatecontrol((void *)ip6e, elen,
1.85      rpaulo   1249:                                    IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
                   1250:                                    IPPROTO_IPV6);
1.35      itojun   1251:                                if (*mp)
                   1252:                                        mp = &(*mp)->m_next;
                   1253:                                break;
                   1254:
1.62      itojun   1255:                        case IPPROTO_HOPOPTS:
                   1256:                        case IPPROTO_AH: /* is it possible? */
                   1257:                                break;
                   1258:
1.35      itojun   1259:                        default:
                   1260:                                /*
1.62      itojun   1261:                                 * other cases have been filtered in the above.
                   1262:                                 * none will visit this case.  here we supply
                   1263:                                 * the code just in case (nxt overwritten or
                   1264:                                 * other cases).
1.35      itojun   1265:                                 */
1.62      itojun   1266:                                m_freem(ext);
1.35      itojun   1267:                                goto loopend;
                   1268:
1.2       itojun   1269:                        }
                   1270:
                   1271:                        /* proceed with the next header. */
1.9       itojun   1272:                        off += elen;
1.2       itojun   1273:                        nxt = ip6e->ip6e_nxt;
1.62      itojun   1274:                        ip6e = NULL;
                   1275:                        m_freem(ext);
                   1276:                        ext = NULL;
1.2       itojun   1277:                }
                   1278:          loopend:
1.62      itojun   1279:                ;
1.2       itojun   1280:        }
1.62      itojun   1281: }
1.85      rpaulo   1282: #undef IS2292
                   1283:
                   1284:
                   1285: void
1.95      dyoung   1286: ip6_notify_pmtu(struct in6pcb *in6p, const struct sockaddr_in6 *dst,
                   1287:     uint32_t *mtu)
1.85      rpaulo   1288: {
                   1289:        struct socket *so;
                   1290:        struct mbuf *m_mtu;
                   1291:        struct ip6_mtuinfo mtuctl;
                   1292:
                   1293:        so = in6p->in6p_socket;
                   1294:
                   1295:        if (mtu == NULL)
                   1296:                return;
                   1297:
1.177     ozaki-r  1298:        KASSERT(so != NULL);
1.85      rpaulo   1299:
                   1300:        memset(&mtuctl, 0, sizeof(mtuctl));     /* zero-clear for safety */
                   1301:        mtuctl.ip6m_mtu = *mtu;
                   1302:        mtuctl.ip6m_addr = *dst;
                   1303:        if (sa6_recoverscope(&mtuctl.ip6m_addr))
                   1304:                return;
                   1305:
1.97      christos 1306:        if ((m_mtu = sbcreatecontrol((void *)&mtuctl, sizeof(mtuctl),
1.85      rpaulo   1307:            IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
                   1308:                return;
                   1309:
1.95      dyoung   1310:        if (sbappendaddr(&so->so_rcv, (const struct sockaddr *)dst, NULL, m_mtu)
1.85      rpaulo   1311:            == 0) {
                   1312:                m_freem(m_mtu);
                   1313:                /* XXX: should count statistics */
                   1314:        } else
                   1315:                sorwakeup(so);
                   1316:
                   1317:        return;
                   1318: }
1.62      itojun   1319:
                   1320: /*
                   1321:  * pull single extension header from mbuf chain.  returns single mbuf that
                   1322:  * contains the result, or NULL on error.
                   1323:  */
                   1324: static struct mbuf *
1.107     christos 1325: ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
1.62      itojun   1326: {
                   1327:        struct ip6_ext ip6e;
                   1328:        size_t elen;
                   1329:        struct mbuf *n;
                   1330:
                   1331: #ifdef DIAGNOSTIC
                   1332:        switch (nxt) {
                   1333:        case IPPROTO_DSTOPTS:
                   1334:        case IPPROTO_ROUTING:
                   1335:        case IPPROTO_HOPOPTS:
                   1336:        case IPPROTO_AH: /* is it possible? */
                   1337:                break;
                   1338:        default:
                   1339:                printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1.2       itojun   1340:        }
1.62      itojun   1341: #endif
1.2       itojun   1342:
1.97      christos 1343:        m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1.62      itojun   1344:        if (nxt == IPPROTO_AH)
                   1345:                elen = (ip6e.ip6e_len + 2) << 2;
                   1346:        else
                   1347:                elen = (ip6e.ip6e_len + 1) << 3;
                   1348:
                   1349:        MGET(n, M_DONTWAIT, MT_DATA);
                   1350:        if (n && elen >= MLEN) {
                   1351:                MCLGET(n, M_DONTWAIT);
                   1352:                if ((n->m_flags & M_EXT) == 0) {
                   1353:                        m_free(n);
                   1354:                        n = NULL;
                   1355:                }
                   1356:        }
                   1357:        if (!n)
                   1358:                return NULL;
                   1359:
                   1360:        n->m_len = 0;
                   1361:        if (elen >= M_TRAILINGSPACE(n)) {
                   1362:                m_free(n);
                   1363:                return NULL;
                   1364:        }
                   1365:
1.97      christos 1366:        m_copydata(m, off, elen, mtod(n, void *));
1.62      itojun   1367:        n->m_len = elen;
                   1368:        return n;
1.2       itojun   1369: }
                   1370:
                   1371: /*
                   1372:  * Get pointer to the previous header followed by the header
                   1373:  * currently processed.
                   1374:  * XXX: This function supposes that
                   1375:  *     M includes all headers,
                   1376:  *     the next header field and the header length field of each header
                   1377:  *     are valid, and
                   1378:  *     the sum of each header length equals to OFF.
                   1379:  * Because of these assumptions, this function must be called very
                   1380:  * carefully. Moreover, it will not be used in the near future when
                   1381:  * we develop `neater' mechanism to process extension headers.
                   1382:  */
1.58      itojun   1383: u_int8_t *
1.107     christos 1384: ip6_get_prevhdr(struct mbuf *m, int off)
1.2       itojun   1385: {
                   1386:        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                   1387:
                   1388:        if (off == sizeof(struct ip6_hdr))
1.58      itojun   1389:                return (&ip6->ip6_nxt);
1.2       itojun   1390:        else {
                   1391:                int len, nxt;
                   1392:                struct ip6_ext *ip6e = NULL;
                   1393:
                   1394:                nxt = ip6->ip6_nxt;
                   1395:                len = sizeof(struct ip6_hdr);
                   1396:                while (len < off) {
1.97      christos 1397:                        ip6e = (struct ip6_ext *)(mtod(m, char *) + len);
1.2       itojun   1398:
1.35      itojun   1399:                        switch (nxt) {
1.2       itojun   1400:                        case IPPROTO_FRAGMENT:
                   1401:                                len += sizeof(struct ip6_frag);
                   1402:                                break;
                   1403:                        case IPPROTO_AH:
                   1404:                                len += (ip6e->ip6e_len + 2) << 2;
                   1405:                                break;
                   1406:                        default:
                   1407:                                len += (ip6e->ip6e_len + 1) << 3;
                   1408:                                break;
                   1409:                        }
                   1410:                        nxt = ip6e->ip6e_nxt;
                   1411:                }
                   1412:                if (ip6e)
1.58      itojun   1413:                        return (&ip6e->ip6e_nxt);
1.2       itojun   1414:                else
                   1415:                        return NULL;
1.18      itojun   1416:        }
                   1417: }
                   1418:
                   1419: /*
                   1420:  * get next header offset.  m will be retained.
                   1421:  */
                   1422: int
1.107     christos 1423: ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
1.18      itojun   1424: {
                   1425:        struct ip6_hdr ip6;
                   1426:        struct ip6_ext ip6e;
                   1427:        struct ip6_frag fh;
                   1428:
                   1429:        /* just in case */
                   1430:        if (m == NULL)
                   1431:                panic("ip6_nexthdr: m == NULL");
                   1432:        if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
                   1433:                return -1;
                   1434:
                   1435:        switch (proto) {
                   1436:        case IPPROTO_IPV6:
1.83      rpaulo   1437:                /* do not chase beyond intermediate IPv6 headers */
                   1438:                if (off != 0)
                   1439:                        return -1;
1.18      itojun   1440:                if (m->m_pkthdr.len < off + sizeof(ip6))
                   1441:                        return -1;
1.97      christos 1442:                m_copydata(m, off, sizeof(ip6), (void *)&ip6);
1.18      itojun   1443:                if (nxtp)
                   1444:                        *nxtp = ip6.ip6_nxt;
                   1445:                off += sizeof(ip6);
                   1446:                return off;
                   1447:
                   1448:        case IPPROTO_FRAGMENT:
                   1449:                /*
                   1450:                 * terminate parsing if it is not the first fragment,
                   1451:                 * it does not make sense to parse through it.
                   1452:                 */
                   1453:                if (m->m_pkthdr.len < off + sizeof(fh))
                   1454:                        return -1;
1.97      christos 1455:                m_copydata(m, off, sizeof(fh), (void *)&fh);
1.67      itojun   1456:                if ((fh.ip6f_offlg & IP6F_OFF_MASK) != 0)
1.18      itojun   1457:                        return -1;
                   1458:                if (nxtp)
                   1459:                        *nxtp = fh.ip6f_nxt;
                   1460:                off += sizeof(struct ip6_frag);
                   1461:                return off;
                   1462:
                   1463:        case IPPROTO_AH:
                   1464:                if (m->m_pkthdr.len < off + sizeof(ip6e))
                   1465:                        return -1;
1.97      christos 1466:                m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1.18      itojun   1467:                if (nxtp)
                   1468:                        *nxtp = ip6e.ip6e_nxt;
                   1469:                off += (ip6e.ip6e_len + 2) << 2;
1.47      itojun   1470:                if (m->m_pkthdr.len < off)
                   1471:                        return -1;
1.18      itojun   1472:                return off;
                   1473:
                   1474:        case IPPROTO_HOPOPTS:
                   1475:        case IPPROTO_ROUTING:
                   1476:        case IPPROTO_DSTOPTS:
                   1477:                if (m->m_pkthdr.len < off + sizeof(ip6e))
                   1478:                        return -1;
1.97      christos 1479:                m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1.18      itojun   1480:                if (nxtp)
                   1481:                        *nxtp = ip6e.ip6e_nxt;
                   1482:                off += (ip6e.ip6e_len + 1) << 3;
1.47      itojun   1483:                if (m->m_pkthdr.len < off)
                   1484:                        return -1;
1.18      itojun   1485:                return off;
                   1486:
                   1487:        case IPPROTO_NONE:
                   1488:        case IPPROTO_ESP:
                   1489:        case IPPROTO_IPCOMP:
                   1490:                /* give up */
                   1491:                return -1;
                   1492:
                   1493:        default:
                   1494:                return -1;
                   1495:        }
                   1496: }
                   1497:
                   1498: /*
                   1499:  * get offset for the last header in the chain.  m will be kept untainted.
                   1500:  */
                   1501: int
1.107     christos 1502: ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
1.18      itojun   1503: {
                   1504:        int newoff;
                   1505:        int nxt;
                   1506:
                   1507:        if (!nxtp) {
                   1508:                nxt = -1;
                   1509:                nxtp = &nxt;
                   1510:        }
1.86      rpaulo   1511:        for (;;) {
1.18      itojun   1512:                newoff = ip6_nexthdr(m, off, proto, nxtp);
                   1513:                if (newoff < 0)
                   1514:                        return off;
                   1515:                else if (newoff < off)
                   1516:                        return -1;      /* invalid */
                   1517:                else if (newoff == off)
                   1518:                        return newoff;
                   1519:
                   1520:                off = newoff;
                   1521:                proto = *nxtp;
1.2       itojun   1522:        }
                   1523: }
                   1524:
1.81      rpaulo   1525: struct m_tag *
1.107     christos 1526: ip6_addaux(struct mbuf *m)
1.81      rpaulo   1527: {
                   1528:        struct m_tag *mtag;
                   1529:
                   1530:        mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
                   1531:        if (!mtag) {
                   1532:                mtag = m_tag_get(PACKET_TAG_INET6, sizeof(struct ip6aux),
                   1533:                    M_NOWAIT);
                   1534:                if (mtag) {
                   1535:                        m_tag_prepend(m, mtag);
1.124     cegger   1536:                        memset(mtag + 1, 0, sizeof(struct ip6aux));
1.81      rpaulo   1537:                }
                   1538:        }
                   1539:        return mtag;
                   1540: }
                   1541:
                   1542: struct m_tag *
1.107     christos 1543: ip6_findaux(struct mbuf *m)
1.81      rpaulo   1544: {
                   1545:        struct m_tag *mtag;
                   1546:
                   1547:        mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
                   1548:        return mtag;
                   1549: }
                   1550:
                   1551: void
1.107     christos 1552: ip6_delaux(struct mbuf *m)
1.81      rpaulo   1553: {
                   1554:        struct m_tag *mtag;
                   1555:
                   1556:        mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
                   1557:        if (mtag)
                   1558:                m_tag_delete(m, mtag);
                   1559: }
                   1560:
1.2       itojun   1561: /*
                   1562:  * System control for IP6
                   1563:  */
                   1564:
1.120     matt     1565: const u_char inet6ctlerrmap[PRC_NCMDS] = {
1.2       itojun   1566:        0,              0,              0,              0,
                   1567:        0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
                   1568:        EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
                   1569:        EMSGSIZE,       EHOSTUNREACH,   0,              0,
                   1570:        0,              0,              0,              0,
                   1571:        ENOPROTOOPT
                   1572: };
                   1573:
1.153     christos 1574: extern int sysctl_net_inet6_addrctlpolicy(SYSCTLFN_ARGS);
                   1575:
1.116     thorpej  1576: static int
                   1577: sysctl_net_inet6_ip6_stats(SYSCTLFN_ARGS)
                   1578: {
                   1579:
1.119     thorpej  1580:        return (NETSTAT_SYSCTL(ip6stat_percpu, IP6_NSTATS));
1.116     thorpej  1581: }
                   1582:
1.128     pooka    1583: static void
                   1584: sysctl_net_inet6_ip6_setup(struct sysctllog **clog)
1.2       itojun   1585: {
1.85      rpaulo   1586: #ifdef RFC2292
                   1587: #define IS2292(x, y)   ((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
                   1588: #else
                   1589: #define IS2292(x, y)   (y)
                   1590: #endif
1.26      itojun   1591:
1.73      atatat   1592:        sysctl_createv(clog, 0, NULL, NULL,
                   1593:                       CTLFLAG_PERMANENT,
1.74      atatat   1594:                       CTLTYPE_NODE, "inet6",
                   1595:                       SYSCTL_DESCR("PF_INET6 related settings"),
1.70      atatat   1596:                       NULL, 0, NULL, 0,
                   1597:                       CTL_NET, PF_INET6, CTL_EOL);
1.73      atatat   1598:        sysctl_createv(clog, 0, NULL, NULL,
                   1599:                       CTLFLAG_PERMANENT,
1.74      atatat   1600:                       CTLTYPE_NODE, "ip6",
                   1601:                       SYSCTL_DESCR("IPv6 related settings"),
1.70      atatat   1602:                       NULL, 0, NULL, 0,
                   1603:                       CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_EOL);
                   1604:
1.73      atatat   1605:        sysctl_createv(clog, 0, NULL, NULL,
                   1606:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1607:                       CTLTYPE_INT, "forwarding",
                   1608:                       SYSCTL_DESCR("Enable forwarding of INET6 datagrams"),
1.70      atatat   1609:                       NULL, 0, &ip6_forwarding, 0,
                   1610:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1611:                       IPV6CTL_FORWARDING, CTL_EOL);
1.73      atatat   1612:        sysctl_createv(clog, 0, NULL, NULL,
                   1613:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1614:                       CTLTYPE_INT, "redirect",
                   1615:                       SYSCTL_DESCR("Enable sending of ICMPv6 redirect messages"),
1.70      atatat   1616:                       NULL, 0, &ip6_sendredirects, 0,
                   1617:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1618:                       IPV6CTL_SENDREDIRECTS, CTL_EOL);
1.73      atatat   1619:        sysctl_createv(clog, 0, NULL, NULL,
                   1620:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1621:                       CTLTYPE_INT, "hlim",
                   1622:                       SYSCTL_DESCR("Hop limit for an INET6 datagram"),
1.70      atatat   1623:                       NULL, 0, &ip6_defhlim, 0,
                   1624:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1625:                       IPV6CTL_DEFHLIM, CTL_EOL);
                   1626: #ifdef notyet
1.73      atatat   1627:        sysctl_createv(clog, 0, NULL, NULL,
                   1628:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70      atatat   1629:                       CTLTYPE_INT, "mtu", NULL,
                   1630:                       NULL, 0, &, 0,
                   1631:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1632:                       IPV6CTL_DEFMTU, CTL_EOL);
                   1633: #endif
                   1634: #ifdef __no_idea__
1.73      atatat   1635:        sysctl_createv(clog, 0, NULL, NULL,
                   1636:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70      atatat   1637:                       CTLTYPE_INT, "forwsrcrt", NULL,
                   1638:                       NULL, 0, &?, 0,
                   1639:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1640:                       IPV6CTL_FORWSRCRT, CTL_EOL);
1.73      atatat   1641:        sysctl_createv(clog, 0, NULL, NULL,
                   1642:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70      atatat   1643:                       CTLTYPE_STRUCT, "mrtstats", NULL,
                   1644:                       NULL, 0, &?, sizeof(?),
                   1645:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1646:                       IPV6CTL_MRTSTATS, CTL_EOL);
1.73      atatat   1647:        sysctl_createv(clog, 0, NULL, NULL,
                   1648:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70      atatat   1649:                       CTLTYPE_?, "mrtproto", NULL,
                   1650:                       NULL, 0, &?, sizeof(?),
                   1651:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1652:                       IPV6CTL_MRTPROTO, CTL_EOL);
                   1653: #endif
1.73      atatat   1654:        sysctl_createv(clog, 0, NULL, NULL,
                   1655:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1656:                       CTLTYPE_INT, "maxfragpackets",
                   1657:                       SYSCTL_DESCR("Maximum number of fragments to buffer "
                   1658:                                    "for reassembly"),
1.70      atatat   1659:                       NULL, 0, &ip6_maxfragpackets, 0,
                   1660:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1661:                       IPV6CTL_MAXFRAGPACKETS, CTL_EOL);
                   1662: #ifdef __no_idea__
1.73      atatat   1663:        sysctl_createv(clog, 0, NULL, NULL,
                   1664:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70      atatat   1665:                       CTLTYPE_INT, "sourcecheck", NULL,
                   1666:                       NULL, 0, &?, 0,
                   1667:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1668:                       IPV6CTL_SOURCECHECK, CTL_EOL);
1.73      atatat   1669:        sysctl_createv(clog, 0, NULL, NULL,
                   1670:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70      atatat   1671:                       CTLTYPE_INT, "sourcecheck_logint", NULL,
                   1672:                       NULL, 0, &?, 0,
                   1673:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1674:                       IPV6CTL_SOURCECHECK_LOGINT, CTL_EOL);
                   1675: #endif
1.73      atatat   1676:        sysctl_createv(clog, 0, NULL, NULL,
                   1677:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1678:                       CTLTYPE_INT, "accept_rtadv",
                   1679:                       SYSCTL_DESCR("Accept router advertisements"),
1.70      atatat   1680:                       NULL, 0, &ip6_accept_rtadv, 0,
                   1681:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1682:                       IPV6CTL_ACCEPT_RTADV, CTL_EOL);
1.73      atatat   1683:        sysctl_createv(clog, 0, NULL, NULL,
                   1684:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.131     spz      1685:                       CTLTYPE_INT, "rtadv_maxroutes",
                   1686:                       SYSCTL_DESCR("Maximum number of routes accepted via router advertisements"),
                   1687:                       NULL, 0, &ip6_rtadv_maxroutes, 0,
                   1688:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1689:                       IPV6CTL_RTADV_MAXROUTES, CTL_EOL);
                   1690:        sysctl_createv(clog, 0, NULL, NULL,
                   1691:                       CTLFLAG_PERMANENT,
                   1692:                       CTLTYPE_INT, "rtadv_numroutes",
                   1693:                       SYSCTL_DESCR("Current number of routes accepted via router advertisements"),
                   1694:                       NULL, 0, &nd6_numroutes, 0,
                   1695:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1696:                       IPV6CTL_RTADV_NUMROUTES, CTL_EOL);
                   1697:        sysctl_createv(clog, 0, NULL, NULL,
                   1698:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1699:                       CTLTYPE_INT, "keepfaith",
                   1700:                       SYSCTL_DESCR("Activate faith interface"),
1.70      atatat   1701:                       NULL, 0, &ip6_keepfaith, 0,
                   1702:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1703:                       IPV6CTL_KEEPFAITH, CTL_EOL);
1.73      atatat   1704:        sysctl_createv(clog, 0, NULL, NULL,
                   1705:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1706:                       CTLTYPE_INT, "log_interval",
                   1707:                       SYSCTL_DESCR("Minumum interval between logging "
                   1708:                                    "unroutable packets"),
1.70      atatat   1709:                       NULL, 0, &ip6_log_interval, 0,
                   1710:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1711:                       IPV6CTL_LOG_INTERVAL, CTL_EOL);
1.73      atatat   1712:        sysctl_createv(clog, 0, NULL, NULL,
                   1713:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1714:                       CTLTYPE_INT, "hdrnestlimit",
                   1715:                       SYSCTL_DESCR("Maximum number of nested IPv6 headers"),
1.70      atatat   1716:                       NULL, 0, &ip6_hdrnestlimit, 0,
                   1717:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1718:                       IPV6CTL_HDRNESTLIMIT, CTL_EOL);
1.73      atatat   1719:        sysctl_createv(clog, 0, NULL, NULL,
                   1720:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1721:                       CTLTYPE_INT, "dad_count",
                   1722:                       SYSCTL_DESCR("Number of Duplicate Address Detection "
                   1723:                                    "probes to send"),
1.70      atatat   1724:                       NULL, 0, &ip6_dad_count, 0,
                   1725:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1726:                       IPV6CTL_DAD_COUNT, CTL_EOL);
1.73      atatat   1727:        sysctl_createv(clog, 0, NULL, NULL,
                   1728:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1729:                       CTLTYPE_INT, "auto_flowlabel",
                   1730:                       SYSCTL_DESCR("Assign random IPv6 flow labels"),
1.70      atatat   1731:                       NULL, 0, &ip6_auto_flowlabel, 0,
                   1732:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1733:                       IPV6CTL_AUTO_FLOWLABEL, CTL_EOL);
1.73      atatat   1734:        sysctl_createv(clog, 0, NULL, NULL,
                   1735:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1736:                       CTLTYPE_INT, "defmcasthlim",
                   1737:                       SYSCTL_DESCR("Default multicast hop limit"),
1.70      atatat   1738:                       NULL, 0, &ip6_defmcasthlim, 0,
                   1739:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1740:                       IPV6CTL_DEFMCASTHLIM, CTL_EOL);
1.73      atatat   1741:        sysctl_createv(clog, 0, NULL, NULL,
                   1742:                       CTLFLAG_PERMANENT,
1.74      atatat   1743:                       CTLTYPE_STRING, "kame_version",
                   1744:                       SYSCTL_DESCR("KAME Version"),
1.78      christos 1745:                       NULL, 0, __UNCONST(__KAME_VERSION), 0,
1.70      atatat   1746:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1747:                       IPV6CTL_KAME_VERSION, CTL_EOL);
1.73      atatat   1748:        sysctl_createv(clog, 0, NULL, NULL,
                   1749:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1750:                       CTLTYPE_INT, "use_deprecated",
                   1751:                       SYSCTL_DESCR("Allow use of deprecated addresses as "
                   1752:                                    "source addresses"),
1.70      atatat   1753:                       NULL, 0, &ip6_use_deprecated, 0,
                   1754:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1755:                       IPV6CTL_USE_DEPRECATED, CTL_EOL);
1.73      atatat   1756:        sysctl_createv(clog, 0, NULL, NULL,
                   1757:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70      atatat   1758:                       CTLTYPE_INT, "rr_prune", NULL,
                   1759:                       NULL, 0, &ip6_rr_prune, 0,
                   1760:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1761:                       IPV6CTL_RR_PRUNE, CTL_EOL);
1.73      atatat   1762:        sysctl_createv(clog, 0, NULL, NULL,
                   1763:                       CTLFLAG_PERMANENT
1.70      atatat   1764: #ifndef INET6_BINDV6ONLY
1.73      atatat   1765:                       |CTLFLAG_READWRITE,
1.70      atatat   1766: #endif
1.74      atatat   1767:                       CTLTYPE_INT, "v6only",
                   1768:                       SYSCTL_DESCR("Disallow PF_INET6 sockets from connecting "
                   1769:                                    "to PF_INET sockets"),
1.70      atatat   1770:                       NULL, 0, &ip6_v6only, 0,
                   1771:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1772:                       IPV6CTL_V6ONLY, CTL_EOL);
1.73      atatat   1773:        sysctl_createv(clog, 0, NULL, NULL,
                   1774:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1775:                       CTLTYPE_INT, "anonportmin",
                   1776:                       SYSCTL_DESCR("Lowest ephemeral port number to assign"),
1.70      atatat   1777:                       sysctl_net_inet_ip_ports, 0, &ip6_anonportmin, 0,
                   1778:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1779:                       IPV6CTL_ANONPORTMIN, CTL_EOL);
1.73      atatat   1780:        sysctl_createv(clog, 0, NULL, NULL,
                   1781:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1782:                       CTLTYPE_INT, "anonportmax",
                   1783:                       SYSCTL_DESCR("Highest ephemeral port number to assign"),
1.70      atatat   1784:                       sysctl_net_inet_ip_ports, 0, &ip6_anonportmax, 0,
                   1785:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1786:                       IPV6CTL_ANONPORTMAX, CTL_EOL);
1.26      itojun   1787: #ifndef IPNOPRIVPORTS
1.73      atatat   1788:        sysctl_createv(clog, 0, NULL, NULL,
                   1789:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1790:                       CTLTYPE_INT, "lowportmin",
                   1791:                       SYSCTL_DESCR("Lowest privileged ephemeral port number "
                   1792:                                    "to assign"),
1.70      atatat   1793:                       sysctl_net_inet_ip_ports, 0, &ip6_lowportmin, 0,
                   1794:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1795:                       IPV6CTL_LOWPORTMIN, CTL_EOL);
1.73      atatat   1796:        sysctl_createv(clog, 0, NULL, NULL,
                   1797:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1798:                       CTLTYPE_INT, "lowportmax",
                   1799:                       SYSCTL_DESCR("Highest privileged ephemeral port number "
                   1800:                                    "to assign"),
1.70      atatat   1801:                       sysctl_net_inet_ip_ports, 0, &ip6_lowportmax, 0,
                   1802:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1803:                       IPV6CTL_LOWPORTMAX, CTL_EOL);
                   1804: #endif /* IPNOPRIVPORTS */
1.73      atatat   1805:        sysctl_createv(clog, 0, NULL, NULL,
                   1806:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.153     christos 1807:                       CTLTYPE_INT, "auto_linklocal",
                   1808:                       SYSCTL_DESCR("Default value of per-interface flag for "
                   1809:                                    "adding an IPv6 link-local address to "
                   1810:                                    "interfaces when attached"),
                   1811:                       NULL, 0, &ip6_auto_linklocal, 0,
                   1812:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1813:                       IPV6CTL_AUTO_LINKLOCAL, CTL_EOL);
                   1814:        sysctl_createv(clog, 0, NULL, NULL,
                   1815:                       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
                   1816:                       CTLTYPE_STRUCT, "addctlpolicy",
                   1817:                       SYSCTL_DESCR("Return the current address control"
                   1818:                           " policy"),
                   1819:                       sysctl_net_inet6_addrctlpolicy, 0, NULL, 0,
                   1820:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1821:                       IPV6CTL_ADDRCTLPOLICY, CTL_EOL);
                   1822:        sysctl_createv(clog, 0, NULL, NULL,
                   1823:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.83      rpaulo   1824:                       CTLTYPE_INT, "use_tempaddr",
                   1825:                       SYSCTL_DESCR("Use temporary address"),
                   1826:                       NULL, 0, &ip6_use_tempaddr, 0,
                   1827:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1828:                       CTL_CREATE, CTL_EOL);
                   1829:        sysctl_createv(clog, 0, NULL, NULL,
                   1830:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.150     roy      1831:                       CTLTYPE_INT, "prefer_tempaddr",
                   1832:                       SYSCTL_DESCR("Prefer temporary address as source "
                   1833:                                    "address"),
                   1834:                       NULL, 0, &ip6_prefer_tempaddr, 0,
                   1835:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1836:                       CTL_CREATE, CTL_EOL);
                   1837:        sysctl_createv(clog, 0, NULL, NULL,
                   1838:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.83      rpaulo   1839:                       CTLTYPE_INT, "temppltime",
                   1840:                       SYSCTL_DESCR("preferred lifetime of a temporary address"),
                   1841:                       NULL, 0, &ip6_temp_preferred_lifetime, 0,
                   1842:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1843:                       CTL_CREATE, CTL_EOL);
                   1844:        sysctl_createv(clog, 0, NULL, NULL,
                   1845:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1846:                       CTLTYPE_INT, "tempvltime",
                   1847:                       SYSCTL_DESCR("valid lifetime of a temporary address"),
                   1848:                       NULL, 0, &ip6_temp_valid_lifetime, 0,
                   1849:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1850:                       CTL_CREATE, CTL_EOL);
                   1851:        sysctl_createv(clog, 0, NULL, NULL,
                   1852:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74      atatat   1853:                       CTLTYPE_INT, "maxfrags",
                   1854:                       SYSCTL_DESCR("Maximum fragments in reassembly queue"),
1.70      atatat   1855:                       NULL, 0, &ip6_maxfrags, 0,
                   1856:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1857:                       IPV6CTL_MAXFRAGS, CTL_EOL);
1.79      rpaulo   1858:        sysctl_createv(clog, 0, NULL, NULL,
                   1859:                       CTLFLAG_PERMANENT,
                   1860:                       CTLTYPE_STRUCT, "stats",
                   1861:                       SYSCTL_DESCR("IPv6 statistics"),
1.116     thorpej  1862:                       sysctl_net_inet6_ip6_stats, 0, NULL, 0,
1.79      rpaulo   1863:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1864:                       IPV6CTL_STATS, CTL_EOL);
1.81      rpaulo   1865:        sysctl_createv(clog, 0, NULL, NULL,
                   1866:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1867:                       CTLTYPE_INT, "use_defaultzone",
                   1868:                       SYSCTL_DESCR("Whether to use the default scope zones"),
                   1869:                       NULL, 0, &ip6_use_defzone, 0,
                   1870:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1871:                       IPV6CTL_USE_DEFAULTZONE, CTL_EOL);
1.83      rpaulo   1872:        sysctl_createv(clog, 0, NULL, NULL,
                   1873:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1874:                       CTLTYPE_INT, "mcast_pmtu",
                   1875:                       SYSCTL_DESCR("Enable pMTU discovery for multicast packet"),
                   1876:                       NULL, 0, &ip6_mcast_pmtu, 0,
                   1877:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1878:                       CTL_CREATE, CTL_EOL);
1.138     christos 1879:        /* anonportalgo RFC6056 subtree */
1.140     christos 1880:        const struct sysctlnode *portalgo_node;
                   1881:        sysctl_createv(clog, 0, NULL, &portalgo_node,
1.138     christos 1882:                       CTLFLAG_PERMANENT,
                   1883:                       CTLTYPE_NODE, "anonportalgo",
                   1884:                       SYSCTL_DESCR("Anonymous port algorithm selection (RFC 6056)"),
                   1885:                       NULL, 0, NULL, 0,
                   1886:                       CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_CREATE, CTL_EOL);
1.140     christos 1887:        sysctl_createv(clog, 0, &portalgo_node, NULL,
1.138     christos 1888:                       CTLFLAG_PERMANENT,
                   1889:                       CTLTYPE_STRING, "available",
                   1890:                       SYSCTL_DESCR("available algorithms"),
1.140     christos 1891:                       sysctl_portalgo_available, 0, NULL, PORTALGO_MAXLEN,
1.138     christos 1892:                       CTL_CREATE, CTL_EOL);
1.140     christos 1893:        sysctl_createv(clog, 0, &portalgo_node, NULL,
1.138     christos 1894:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1895:                       CTLTYPE_STRING, "selected",
                   1896:                       SYSCTL_DESCR("selected algorithm"),
1.140     christos 1897:                       sysctl_portalgo_selected6, 0, NULL, PORTALGO_MAXLEN,
1.138     christos 1898:                       CTL_CREATE, CTL_EOL);
1.141     christos 1899:        sysctl_createv(clog, 0, &portalgo_node, NULL,
                   1900:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1901:                       CTLTYPE_STRUCT, "reserve",
                   1902:                       SYSCTL_DESCR("bitmap of reserved ports"),
                   1903:                       sysctl_portalgo_reserve6, 0, NULL, 0,
                   1904:                       CTL_CREATE, CTL_EOL);
1.139     christos 1905:        sysctl_createv(clog, 0, NULL, NULL,
                   1906:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1907:                       CTLTYPE_INT, "neighborgcthresh",
                   1908:                       SYSCTL_DESCR("Maximum number of entries in neighbor"
                   1909:                        " cache"),
                   1910:                       NULL, 1, &ip6_neighborgcthresh, 0,
                   1911:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1912:                       CTL_CREATE, CTL_EOL);
                   1913:        sysctl_createv(clog, 0, NULL, NULL,
                   1914:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1915:                       CTLTYPE_INT, "maxifprefixes",
                   1916:                       SYSCTL_DESCR("Maximum number of prefixes created by"
                   1917:                           " route advertisement per interface"),
                   1918:                       NULL, 1, &ip6_maxifprefixes, 0,
                   1919:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1920:                       CTL_CREATE, CTL_EOL);
                   1921:        sysctl_createv(clog, 0, NULL, NULL,
                   1922:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1923:                       CTLTYPE_INT, "maxifdefrouters",
                   1924:                       SYSCTL_DESCR("Maximum number of default routers created"
                   1925:                           " by route advertisement per interface"),
                   1926:                       NULL, 1, &ip6_maxifdefrouters, 0,
                   1927:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1928:                       CTL_CREATE, CTL_EOL);
                   1929:        sysctl_createv(clog, 0, NULL, NULL,
                   1930:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1931:                       CTLTYPE_INT, "maxdynroutes",
                   1932:                       SYSCTL_DESCR("Maximum number of routes created via"
                   1933:                           " redirect"),
                   1934:                       NULL, 1, &ip6_maxdynroutes, 0,
                   1935:                       CTL_NET, PF_INET6, IPPROTO_IPV6,
                   1936:                       CTL_CREATE, CTL_EOL);
1.2       itojun   1937: }
1.116     thorpej  1938:
                   1939: void
                   1940: ip6_statinc(u_int stat)
                   1941: {
                   1942:
                   1943:        KASSERT(stat < IP6_NSTATS);
                   1944:        IP6_STATINC(stat);
                   1945: }

CVSweb <webmaster@jp.NetBSD.org>