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

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

CVSweb <webmaster@jp.NetBSD.org>