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

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

CVSweb <webmaster@jp.NetBSD.org>