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

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

CVSweb <webmaster@jp.NetBSD.org>