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

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

CVSweb <webmaster@jp.NetBSD.org>