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

1.19    ! thorpej     1: /*     $NetBSD: ip6_input.c,v 1.18 2000/03/21 23:53:30 itojun Exp $    */
1.17      itojun      2: /*     $KAME: ip6_input.c,v 1.72 2000/03/21 09:23:19 itojun Exp $      */
1.3       thorpej     3:
1.2       itojun      4: /*
                      5:  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
                      6:  * All rights reserved.
                      7:  *
                      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.
                     19:  *
                     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.
                     45:  * 3. All advertising materials mentioning features or use of this software
                     46:  *    must display the following acknowledgement:
                     47:  *     This product includes software developed by the University of
                     48:  *     California, Berkeley and its contributors.
                     49:  * 4. Neither the name of the University nor the names of its contributors
                     50:  *    may be used to endorse or promote products derived from this software
                     51:  *    without specific prior written permission.
                     52:  *
                     53:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     54:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     55:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     56:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     57:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     58:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     59:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     60:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     61:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     62:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     63:  * SUCH DAMAGE.
                     64:  *
                     65:  *     @(#)ip_input.c  8.2 (Berkeley) 1/4/94
                     66:  */
                     67:
                     68: #include "opt_inet.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>
                     85:
                     86: #include <net/if.h>
                     87: #include <net/if_types.h>
                     88: #include <net/if_dl.h>
                     89: #include <net/route.h>
                     90: #include <net/netisr.h>
1.15      darrenr    91: #ifdef PFIL_HOOKS
                     92: #include <net/pfil.h>
                     93: #endif
1.2       itojun     94:
                     95: #include <netinet/in.h>
1.9       itojun     96: #include <netinet/in_systm.h>
                     97: #ifdef INET
                     98: #include <netinet/ip.h>
                     99: #include <netinet/ip_icmp.h>
                    100: #endif /*INET*/
1.14      itojun    101: #include <netinet/ip6.h>
1.2       itojun    102: #include <netinet6/in6_var.h>
1.11      itojun    103: #include <netinet6/ip6_var.h>
1.2       itojun    104: #include <netinet6/in6_pcb.h>
1.14      itojun    105: #include <netinet/icmp6.h>
1.2       itojun    106: #include <netinet6/in6_ifattach.h>
                    107: #include <netinet6/nd6.h>
1.9       itojun    108: #include <netinet6/in6_prefix.h>
1.2       itojun    109:
1.9       itojun    110: #ifdef IPV6FIREWALL
                    111: #include <netinet6/ip6_fw.h>
                    112: #endif
1.2       itojun    113:
                    114: #include <netinet6/ip6protosw.h>
                    115:
                    116: /* we need it for NLOOP. */
                    117: #include "loop.h"
                    118: #include "faith.h"
                    119:
                    120: #include "gif.h"
                    121: #include "bpfilter.h"
                    122:
1.9       itojun    123: #include <net/net_osdep.h>
                    124:
1.2       itojun    125: extern struct domain inet6domain;
                    126:
                    127: u_char ip6_protox[IPPROTO_MAX];
                    128: static int ip6qmaxlen = IFQ_MAXLEN;
                    129: struct in6_ifaddr *in6_ifaddr;
                    130: struct ifqueue ip6intrq;
                    131:
                    132: extern struct ifnet loif[NLOOP];
                    133: int ip6_forward_srcrt;                 /* XXX */
                    134: int ip6_sourcecheck;                   /* XXX */
                    135: int ip6_sourcecheck_interval;          /* XXX */
1.9       itojun    136:
                    137: #ifdef IPV6FIREWALL
                    138: /* firewall hooks */
                    139: ip6_fw_chk_t *ip6_fw_chk_ptr;
                    140: ip6_fw_ctl_t *ip6_fw_ctl_ptr;
1.2       itojun    141: #endif
                    142:
                    143: struct ip6stat ip6stat;
                    144:
                    145: static void ip6_init2 __P((void *));
                    146:
1.5       itojun    147: static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
1.2       itojun    148:
1.12      itojun    149: #ifdef PTR
1.9       itojun    150: extern int             ip6_protocol_tr;
                    151:
                    152: int    ptr_in6         __P((struct mbuf *, struct mbuf **));
                    153: extern void ip_forward __P((struct mbuf *, int));
                    154: #endif
                    155:
1.2       itojun    156: /*
                    157:  * IP6 initialization: fill in IP6 protocol switch table.
                    158:  * All protocols not implemented in kernel go to raw IP6 protocol handler.
                    159:  */
                    160: void
                    161: ip6_init()
                    162: {
                    163:        register struct ip6protosw *pr;
                    164:        register int i;
                    165:        struct timeval tv;
                    166:
                    167:        pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
                    168:        if (pr == 0)
                    169:                panic("ip6_init");
                    170:        for (i = 0; i < IPPROTO_MAX; i++)
                    171:                ip6_protox[i] = pr - inet6sw;
                    172:        for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
                    173:            pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
                    174:                if (pr->pr_domain->dom_family == PF_INET6 &&
                    175:                    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
                    176:                        ip6_protox[pr->pr_protocol] = pr - inet6sw;
                    177:        ip6intrq.ifq_maxlen = ip6qmaxlen;
                    178:        nd6_init();
                    179:        frag6_init();
1.9       itojun    180: #ifdef IPV6FIREWALL
                    181:        ip6_fw_init();
                    182: #endif
1.2       itojun    183:        /*
                    184:         * in many cases, random() here does NOT return random number
                    185:         * as initialization during bootstrap time occur in fixed order.
                    186:         */
                    187:        microtime(&tv);
                    188:        ip6_flow_seq = random() ^ tv.tv_usec;
                    189:
                    190:        ip6_init2((void *)0);
                    191: }
                    192:
                    193: static void
                    194: ip6_init2(dummy)
                    195:        void *dummy;
                    196: {
1.9       itojun    197:        int ret;
                    198:
                    199:        /* get EUI64 from somewhere */
                    200:        ret = in6_ifattach_getifid(NULL);
1.2       itojun    201:
                    202:        /*
                    203:         * to route local address of p2p link to loopback,
                    204:         * assign loopback address first.
                    205:         */
1.9       itojun    206:        in6_ifattach(&loif[0], IN6_IFT_LOOP, NULL, 0);
1.2       itojun    207:
                    208:        /* nd6_timer_init */
1.19    ! thorpej   209:        callout_init(&nd6_timer_ch);
        !           210:        callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
1.9       itojun    211:        /* router renumbering prefix list maintenance */
1.19    ! thorpej   212:        callout_init(&in6_rr_timer_ch);
        !           213:        callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL);
1.2       itojun    214: }
                    215:
                    216: /*
                    217:  * IP6 input interrupt handling. Just pass the packet to ip6_input.
                    218:  */
                    219: void
                    220: ip6intr()
                    221: {
                    222:        int s;
                    223:        struct mbuf *m;
                    224:
                    225:        for (;;) {
                    226:                s = splimp();
                    227:                IF_DEQUEUE(&ip6intrq, m);
                    228:                splx(s);
                    229:                if (m == 0)
                    230:                        return;
                    231:                ip6_input(m);
                    232:        }
                    233: }
                    234:
                    235: extern struct  route_in6 ip6_forward_rt;
                    236:
                    237: void
                    238: ip6_input(m)
                    239:        struct mbuf *m;
                    240: {
1.9       itojun    241:        struct ip6_hdr *ip6;
1.2       itojun    242:        int off = sizeof(struct ip6_hdr), nest;
                    243:        u_int32_t plen;
1.5       itojun    244:        u_int32_t rtalert = ~0;
1.2       itojun    245:        int nxt, ours = 0;
1.9       itojun    246:        struct ifnet *deliverifp = NULL;
1.15      darrenr   247: #ifdef PFIL_HOOKS
                    248:        struct packet_filter_hook *pfh;
                    249:        struct mbuf *m0;
                    250:        int rv;
                    251: #endif /* PFIL_HOOKS */
1.2       itojun    252:
                    253: #ifdef IPSEC
                    254:        /*
                    255:         * should the inner packet be considered authentic?
                    256:         * see comment in ah4_input().
                    257:         */
                    258:        if (m) {
                    259:                m->m_flags &= ~M_AUTHIPHDR;
                    260:                m->m_flags &= ~M_AUTHIPDGM;
                    261:        }
                    262: #endif
1.9       itojun    263:
1.2       itojun    264:        /*
                    265:         * mbuf statistics by kazu
                    266:         */
                    267:        if (m->m_flags & M_EXT) {
                    268:                if (m->m_next)
                    269:                        ip6stat.ip6s_mext2m++;
                    270:                else
                    271:                        ip6stat.ip6s_mext1++;
                    272:        } else {
                    273:                if (m->m_next) {
1.9       itojun    274:                        if (m->m_flags & M_LOOP) {
1.2       itojun    275:                                ip6stat.ip6s_m2m[loif[0].if_index]++;   /*XXX*/
1.9       itojun    276:                        }
1.2       itojun    277:                        else if (m->m_pkthdr.rcvif->if_index <= 31)
                    278:                                ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
                    279:                        else
                    280:                                ip6stat.ip6s_m2m[0]++;
                    281:                } else
                    282:                        ip6stat.ip6s_m1++;
                    283:        }
                    284:
1.9       itojun    285:        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
                    286:        ip6stat.ip6s_total++;
                    287:
                    288: #ifndef PULLDOWN_TEST
                    289:        /* XXX is the line really necessary? */
1.2       itojun    290:        IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
1.9       itojun    291: #endif
1.2       itojun    292:
1.9       itojun    293:        if (m->m_len < sizeof(struct ip6_hdr)) {
                    294:                struct ifnet *inifp;
                    295:                inifp = m->m_pkthdr.rcvif;
                    296:                if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
                    297:                        ip6stat.ip6s_toosmall++;
                    298:                        in6_ifstat_inc(inifp, ifs6_in_hdrerr);
                    299:                        return;
                    300:                }
1.2       itojun    301:        }
                    302:
                    303:        ip6 = mtod(m, struct ip6_hdr *);
                    304:
                    305:        if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
                    306:                ip6stat.ip6s_badvers++;
1.9       itojun    307:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
1.2       itojun    308:                goto bad;
                    309:        }
1.15      darrenr   310:
                    311: #ifdef PFIL_HOOKS
                    312:        /*
                    313:         * Run through list of hooks for input packets.  If there are any
                    314:         * filters which require that additional packets in the flow are
                    315:         * not fast-forwarded, they must clear the M_CANFASTFWD flag.
                    316:         * Note that filters must _never_ set this flag, as another filter
                    317:         * in the list may have previously cleared it.
                    318:         */
                    319:        m0 = m;
1.16      darrenr   320:        pfh = pfil_hook_get(PFIL_IN, &inetsw[ip_protox[IPPROTO_IPV6]].pr_pfh);
1.15      darrenr   321:        for (; pfh; pfh = pfh->pfil_link.tqe_next)
                    322:                if (pfh->pfil_func) {
                    323:                        rv = pfh->pfil_func(ip6, sizeof(*ip6),
                    324:                                            m->m_pkthdr.rcvif, 0, &m0);
                    325:                        if (rv)
                    326:                                return;
                    327:                        m = m0;
                    328:                        if (m == NULL)
                    329:                                return;
                    330:                        ip6 = mtod(m, struct ip6_hdr *);
                    331:                }
                    332: #endif /* PFIL_HOOKS */
                    333:
1.2       itojun    334:
                    335:        ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
                    336:
1.9       itojun    337: #ifdef IPV6FIREWALL
                    338:        /*
                    339:         * Check with the firewall...
                    340:         */
                    341:        if (ip6_fw_chk_ptr) {
                    342:                u_short port = 0;
                    343:                /* If ipfw says divert, we have to just drop packet */
                    344:                /* use port as a dummy argument */
                    345:                if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
                    346:                        m_freem(m);
                    347:                        m = NULL;
                    348:                }
                    349:                if (!m)
                    350:                        return;
                    351:        }
                    352: #endif
                    353:
1.2       itojun    354:        /*
                    355:         * Scope check
                    356:         */
                    357:        if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
                    358:            IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
                    359:                ip6stat.ip6s_badscope++;
1.9       itojun    360:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1.2       itojun    361:                goto bad;
                    362:        }
1.13      itojun    363:        /*
                    364:         * The following check is not documented in the spec.  Malicious party
                    365:         * may be able to use IPv4 mapped addr to confuse tcp/udp stack and
                    366:         * bypass security checks (act as if it was from 127.0.0.1 by using
                    367:         * IPv6 src ::ffff:127.0.0.1).  Be cautious.
                    368:         */
                    369:        if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
                    370:            IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
                    371:                ip6stat.ip6s_badscope++;
                    372:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
                    373:                goto bad;
                    374:        }
1.17      itojun    375: #if 0
1.13      itojun    376:        /*
1.17      itojun    377:         * Reject packets with IPv4 compatible addresses (auto tunnel).
                    378:         *
                    379:         * The code forbids auto tunnel relay case in RFC1933 (the check is
                    380:         * stronger than RFC1933).  We may want to re-enable it if mech-xx
                    381:         * is revised to forbid relaying case.
1.13      itojun    382:         */
                    383:        if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
                    384:            IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
                    385:                ip6stat.ip6s_badscope++;
                    386:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
                    387:                goto bad;
                    388:        }
                    389: #endif
1.2       itojun    390:        if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
                    391:            IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
                    392:                if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
                    393:                        ours = 1;
1.9       itojun    394:                        deliverifp = m->m_pkthdr.rcvif;
1.2       itojun    395:                        goto hbhcheck;
                    396:                } else {
                    397:                        ip6stat.ip6s_badscope++;
1.9       itojun    398:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1.2       itojun    399:                        goto bad;
                    400:                }
                    401:        }
                    402:
                    403:        if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
                    404:                if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
                    405:                        ours = 1;
1.9       itojun    406:                        deliverifp = m->m_pkthdr.rcvif;
1.2       itojun    407:                        goto hbhcheck;
                    408:                }
                    409:        } else {
                    410:                if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
                    411:                        ip6->ip6_src.s6_addr16[1]
                    412:                                = htons(m->m_pkthdr.rcvif->if_index);
                    413:                if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
                    414:                        ip6->ip6_dst.s6_addr16[1]
                    415:                                = htons(m->m_pkthdr.rcvif->if_index);
                    416:        }
                    417:
1.12      itojun    418: #ifdef PTR
1.9       itojun    419:        /*
                    420:         *
                    421:         */
                    422:        if (ip6_protocol_tr)
                    423:        {
                    424:            struct mbuf *m1 = NULL;
                    425:
                    426:            switch (ptr_in6(m, &m1))
                    427:            {
                    428:              case IPPROTO_IP:                                  goto mcastcheck;
                    429:              case IPPROTO_IPV4:        ip_forward(m1, 0);      break;
                    430:              case IPPROTO_IPV6:        ip6_forward(m1, 0);     break;
                    431:              case IPPROTO_MAX:                 /* discard this packet  */
                    432:              default:
                    433:            }
                    434:
                    435:            if (m != m1)
                    436:                m_freem(m);
                    437:
                    438:            return;
                    439:        }
                    440:
                    441:   mcastcheck:
                    442: #endif
                    443:
1.2       itojun    444:        /*
                    445:         * Multicast check
                    446:         */
                    447:        if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
                    448:                struct  in6_multi *in6m = 0;
1.9       itojun    449:
                    450:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
1.2       itojun    451:                /*
                    452:                 * See if we belong to the destination multicast group on the
                    453:                 * arrival interface.
                    454:                 */
                    455:                IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
                    456:                if (in6m)
                    457:                        ours = 1;
                    458:                else if (!ip6_mrouter) {
                    459:                        ip6stat.ip6s_notmember++;
                    460:                        ip6stat.ip6s_cantforward++;
1.9       itojun    461:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2       itojun    462:                        goto bad;
                    463:                }
1.9       itojun    464:                deliverifp = m->m_pkthdr.rcvif;
1.2       itojun    465:                goto hbhcheck;
                    466:        }
                    467:
                    468:        /*
                    469:         *  Unicast check
                    470:         */
                    471:        if (ip6_forward_rt.ro_rt == 0 ||
                    472:            !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
                    473:                                &ip6_forward_rt.ro_dst.sin6_addr)) {
                    474:                if (ip6_forward_rt.ro_rt) {
                    475:                        RTFREE(ip6_forward_rt.ro_rt);
                    476:                        ip6_forward_rt.ro_rt = 0;
                    477:                }
                    478:                bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
                    479:                ip6_forward_rt.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
                    480:                ip6_forward_rt.ro_dst.sin6_family = AF_INET6;
                    481:                ip6_forward_rt.ro_dst.sin6_addr = ip6->ip6_dst;
                    482:
1.9       itojun    483:                rtalloc((struct route *)&ip6_forward_rt);
1.2       itojun    484:        }
                    485:
                    486: #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
                    487:
                    488:        /*
                    489:         * Accept the packet if the forwarding interface to the destination
                    490:         * according to the routing table is the loopback interface,
                    491:         * unless the associated route has a gateway.
                    492:         * Note that this approach causes to accept a packet if there is a
                    493:         * route to the loopback interface for the destination of the packet.
                    494:         * But we think it's even useful in some situations, e.g. when using
                    495:         * a special daemon which wants to intercept the packet.
                    496:         */
                    497:        if (ip6_forward_rt.ro_rt &&
                    498:            (ip6_forward_rt.ro_rt->rt_flags &
                    499:             (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
                    500: #if 0
                    501:            /*
                    502:             * The check below is redundant since the comparison of
                    503:             * the destination and the key of the rtentry has
                    504:             * already done through looking up the routing table.
                    505:             */
                    506:            IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
                    507:                               &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) &&
                    508: #endif
1.9       itojun    509:            ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
1.2       itojun    510:                struct in6_ifaddr *ia6 =
                    511:                        (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
                    512:                /* packet to tentative address must not be received */
                    513:                if (ia6->ia6_flags & IN6_IFF_ANYCAST)
                    514:                        m->m_flags |= M_ANYCAST6;
                    515:                if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
                    516:                        /* this interface is ready */
                    517:                        ours = 1;
1.9       itojun    518:                        deliverifp = ia6->ia_ifp;       /* correct? */
1.2       itojun    519:                        goto hbhcheck;
                    520:                } else {
                    521:                        /* this interface is not ready, fall through */
                    522:                }
                    523:        }
                    524:
                    525:        /*
                    526:         * FAITH(Firewall Aided Internet Translator)
                    527:         */
                    528: #if defined(NFAITH) && 0 < NFAITH
                    529:        if (ip6_keepfaith) {
                    530:                if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
                    531:                 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
                    532:                        /* XXX do we need more sanity checks? */
                    533:                        ours = 1;
1.9       itojun    534:                        deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /*faith*/
                    535:                        goto hbhcheck;
                    536:                }
                    537:        }
                    538: #endif
                    539:
                    540: #if 0
                    541:     {
                    542:        /*
                    543:         * Last resort: check in6_ifaddr for incoming interface.
                    544:         * The code is here until I update the "goto ours hack" code above
                    545:         * working right.
                    546:         */
                    547:        struct ifaddr *ifa;
                    548:        for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
                    549:             ifa;
                    550:             ifa = ifa->ifa_list.tqe_next) {
                    551:                if (ifa->ifa_addr == NULL)
                    552:                        continue;       /* just for safety */
                    553:                if (ifa->ifa_addr->sa_family != AF_INET6)
                    554:                        continue;
                    555:                if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
                    556:                        ours = 1;
                    557:                        deliverifp = ifa->ifa_ifp;
1.2       itojun    558:                        goto hbhcheck;
                    559:                }
                    560:        }
1.9       itojun    561:     }
1.2       itojun    562: #endif
                    563:
                    564:        /*
                    565:         * Now there is no reason to process the packet if it's not our own
                    566:         * and we're not a router.
                    567:         */
                    568:        if (!ip6_forwarding) {
                    569:                ip6stat.ip6s_cantforward++;
1.9       itojun    570:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2       itojun    571:                goto bad;
                    572:        }
                    573:
                    574:   hbhcheck:
                    575:        /*
                    576:         * Process Hop-by-Hop options header if it's contained.
                    577:         * m may be modified in ip6_hopopts_input().
                    578:         * If a JumboPayload option is included, plen will also be modified.
                    579:         */
                    580:        plen = (u_int32_t)ntohs(ip6->ip6_plen);
                    581:        if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9       itojun    582:                struct ip6_hbh *hbh;
                    583:
                    584:                if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
                    585: #if 0  /*touches NULL pointer*/
                    586:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
                    587: #endif
1.2       itojun    588:                        return; /* m have already been freed */
1.9       itojun    589:                }
1.2       itojun    590:                /* adjust pointer */
                    591:                ip6 = mtod(m, struct ip6_hdr *);
1.9       itojun    592: #ifndef PULLDOWN_TEST
                    593:                /* ip6_hopopts_input() ensures that mbuf is contiguous */
                    594:                hbh = (struct ip6_hbh *)(ip6 + 1);
                    595: #else
                    596:                IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
                    597:                        sizeof(struct ip6_hbh));
                    598:                if (hbh == NULL) {
                    599:                        ip6stat.ip6s_tooshort++;
                    600:                        return;
                    601:                }
                    602: #endif
                    603:                nxt = hbh->ip6h_nxt;
1.2       itojun    604:
                    605:                /*
                    606:                 * accept the packet if a router alert option is included
                    607:                 * and we act as an IPv6 router.
                    608:                 */
1.5       itojun    609:                if (rtalert != ~0 && ip6_forwarding)
1.2       itojun    610:                        ours = 1;
                    611:        } else
                    612:                nxt = ip6->ip6_nxt;
                    613:
                    614:        /*
                    615:         * Check that the amount of data in the buffers
                    616:         * is as at least much as the IPv6 header would have us expect.
                    617:         * Trim mbufs if longer than we expect.
                    618:         * Drop packet if shorter than we expect.
                    619:         */
                    620:        if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
                    621:                ip6stat.ip6s_tooshort++;
1.9       itojun    622:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.2       itojun    623:                goto bad;
                    624:        }
                    625:        if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
                    626:                if (m->m_len == m->m_pkthdr.len) {
                    627:                        m->m_len = sizeof(struct ip6_hdr) + plen;
                    628:                        m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
                    629:                } else
                    630:                        m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
                    631:        }
                    632:
                    633:        /*
                    634:         * Forward if desirable.
                    635:         */
                    636:        if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
                    637:                /*
                    638:                 * If we are acting as a multicast router, all
                    639:                 * incoming multicast packets are passed to the
                    640:                 * kernel-level multicast forwarding function.
                    641:                 * The packet is returned (relatively) intact; if
                    642:                 * ip6_mforward() returns a non-zero value, the packet
                    643:                 * must be discarded, else it may be accepted below.
                    644:                 */
                    645:                if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
                    646:                        ip6stat.ip6s_cantforward++;
                    647:                        m_freem(m);
                    648:                        return;
                    649:                }
                    650:                if (!ours) {
                    651:                        m_freem(m);
                    652:                        return;
                    653:                }
                    654:        }
                    655:        else if (!ours) {
                    656:                ip6_forward(m, 0);
                    657:                return;
                    658:        }
                    659:
                    660:        /*
                    661:         * Tell launch routine the next header
                    662:         */
1.12      itojun    663: #ifdef IFA_STATS
1.9       itojun    664:        if (IFA_STATS && deliverifp != NULL) {
                    665:                struct in6_ifaddr *ia6;
                    666:                ip6 = mtod(m, struct ip6_hdr *);
                    667:                ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
                    668:                if (ia6)
                    669:                        ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
                    670:        }
                    671: #endif
1.2       itojun    672:        ip6stat.ip6s_delivered++;
1.9       itojun    673:        in6_ifstat_inc(deliverifp, ifs6_in_deliver);
1.2       itojun    674:        nest = 0;
                    675:        while (nxt != IPPROTO_DONE) {
                    676:                if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
                    677:                        ip6stat.ip6s_toomanyhdr++;
                    678:                        goto bad;
                    679:                }
1.8       itojun    680:
                    681:                /*
                    682:                 * protection against faulty packet - there should be
                    683:                 * more sanity checks in header chain processing.
                    684:                 */
                    685:                if (m->m_pkthdr.len < off) {
                    686:                        ip6stat.ip6s_tooshort++;
1.9       itojun    687:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.8       itojun    688:                        goto bad;
                    689:                }
                    690:
1.2       itojun    691:                nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
                    692:        }
                    693:        return;
                    694:  bad:
                    695:        m_freem(m);
                    696: }
                    697:
                    698: /*
                    699:  * Hop-by-Hop options header processing. If a valid jumbo payload option is
                    700:  * included, the real payload length will be stored in plenp.
                    701:  */
                    702: static int
                    703: ip6_hopopts_input(plenp, rtalertp, mp, offp)
                    704:        u_int32_t *plenp;
1.5       itojun    705:        u_int32_t *rtalertp;    /* XXX: should be stored more smart way */
1.2       itojun    706:        struct mbuf **mp;
                    707:        int *offp;
                    708: {
                    709:        register struct mbuf *m = *mp;
                    710:        int off = *offp, hbhlen;
                    711:        struct ip6_hbh *hbh;
                    712:        u_int8_t *opt;
                    713:
                    714:        /* validation of the length of the header */
1.9       itojun    715: #ifndef PULLDOWN_TEST
1.2       itojun    716:        IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
                    717:        hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
                    718:        hbhlen = (hbh->ip6h_len + 1) << 3;
                    719:
                    720:        IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
                    721:        hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1.9       itojun    722: #else
                    723:        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                    724:                sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
                    725:        if (hbh == NULL) {
                    726:                ip6stat.ip6s_tooshort++;
                    727:                return -1;
                    728:        }
                    729:        hbhlen = (hbh->ip6h_len + 1) << 3;
                    730:        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
                    731:                hbhlen);
                    732:        if (hbh == NULL) {
                    733:                ip6stat.ip6s_tooshort++;
                    734:                return -1;
                    735:        }
                    736: #endif
1.2       itojun    737:        off += hbhlen;
                    738:        hbhlen -= sizeof(struct ip6_hbh);
                    739:        opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
                    740:
                    741:        if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
                    742:                                hbhlen, rtalertp, plenp) < 0)
                    743:                return(-1);
                    744:
                    745:        *offp = off;
                    746:        *mp = m;
                    747:        return(0);
                    748: }
                    749:
                    750: /*
                    751:  * Search header for all Hop-by-hop options and process each option.
                    752:  * This function is separate from ip6_hopopts_input() in order to
                    753:  * handle a case where the sending node itself process its hop-by-hop
                    754:  * options header. In such a case, the function is called from ip6_output().
                    755:  */
                    756: int
                    757: ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
                    758:        struct mbuf *m;
                    759:        u_int8_t *opthead;
                    760:        int hbhlen;
1.5       itojun    761:        u_int32_t *rtalertp;
1.2       itojun    762:        u_int32_t *plenp;
                    763: {
                    764:        struct ip6_hdr *ip6;
                    765:        int optlen = 0;
                    766:        u_int8_t *opt = opthead;
                    767:        u_int16_t rtalert_val;
                    768:
                    769:        for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
                    770:                switch(*opt) {
                    771:                 case IP6OPT_PAD1:
                    772:                         optlen = 1;
                    773:                         break;
                    774:                 case IP6OPT_PADN:
                    775:                         if (hbhlen < IP6OPT_MINLEN) {
                    776:                                 ip6stat.ip6s_toosmall++;
                    777:                                 goto bad;
                    778:                         }
                    779:                         optlen = *(opt + 1) + 2;
                    780:                         break;
                    781:                 case IP6OPT_RTALERT:
1.8       itojun    782:                         /* XXX may need check for alignment */
1.2       itojun    783:                         if (hbhlen < IP6OPT_RTALERT_LEN) {
                    784:                                 ip6stat.ip6s_toosmall++;
                    785:                                 goto bad;
                    786:                         }
                    787:                         if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2)
                    788:                                  /* XXX: should we discard the packet? */
                    789:                                 log(LOG_ERR, "length of router alert opt is inconsitent(%d)",
                    790:                                     *(opt + 1));
                    791:                         optlen = IP6OPT_RTALERT_LEN;
                    792:                         bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
                    793:                         *rtalertp = ntohs(rtalert_val);
                    794:                         break;
                    795:                 case IP6OPT_JUMBO:
1.8       itojun    796:                         /* XXX may need check for alignment */
1.2       itojun    797:                         if (hbhlen < IP6OPT_JUMBO_LEN) {
                    798:                                 ip6stat.ip6s_toosmall++;
                    799:                                 goto bad;
                    800:                         }
                    801:                         if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2)
                    802:                                  /* XXX: should we discard the packet? */
                    803:                                 log(LOG_ERR, "length of jumbopayload opt "
                    804:                                     "is inconsistent(%d)",
                    805:                                     *(opt + 1));
                    806:                         optlen = IP6OPT_JUMBO_LEN;
                    807:
                    808:                         /*
                    809:                          * We can simply cast because of the alignment
                    810:                          * requirement of the jumbo payload option.
                    811:                          */
1.8       itojun    812: #if 0
1.2       itojun    813:                         *plenp = ntohl(*(u_int32_t *)(opt + 2));
1.8       itojun    814: #else
                    815:                         bcopy(opt + 2, plenp, sizeof(*plenp));
                    816:                         *plenp = htonl(*plenp);
                    817: #endif
1.2       itojun    818:                         if (*plenp <= IPV6_MAXPACKET) {
                    819:                                 /*
                    820:                                  * jumbo payload length must be larger
                    821:                                  * than 65535
                    822:                                  */
                    823:                                 ip6stat.ip6s_badoptions++;
                    824:                                 icmp6_error(m, ICMP6_PARAM_PROB,
                    825:                                             ICMP6_PARAMPROB_HEADER,
                    826:                                             sizeof(struct ip6_hdr) +
                    827:                                             sizeof(struct ip6_hbh) +
                    828:                                             opt + 2 - opthead);
                    829:                                 return(-1);
                    830:                         }
                    831:
                    832:                         ip6 = mtod(m, struct ip6_hdr *);
                    833:                         if (ip6->ip6_plen) {
                    834:                                 /*
                    835:                                  * IPv6 packets that have non 0 payload length
                    836:                                  * must not contain a jumbo paylod option.
                    837:                                  */
                    838:                                 ip6stat.ip6s_badoptions++;
                    839:                                 icmp6_error(m, ICMP6_PARAM_PROB,
                    840:                                             ICMP6_PARAMPROB_HEADER,
                    841:                                             sizeof(struct ip6_hdr) +
                    842:                                             sizeof(struct ip6_hbh) +
                    843:                                             opt - opthead);
                    844:                                 return(-1);
                    845:                         }
                    846:                         break;
                    847:                 default:               /* unknown option */
                    848:                         if (hbhlen < IP6OPT_MINLEN) {
                    849:                                 ip6stat.ip6s_toosmall++;
                    850:                                 goto bad;
                    851:                         }
                    852:                         if ((optlen = ip6_unknown_opt(opt, m,
                    853:                                                       sizeof(struct ip6_hdr) +
                    854:                                                       sizeof(struct ip6_hbh) +
                    855:                                                       opt - opthead)) == -1)
                    856:                                 return(-1);
                    857:                         optlen += 2;
                    858:                         break;
                    859:                }
                    860:        }
                    861:
                    862:        return(0);
                    863:
                    864:   bad:
                    865:        m_freem(m);
                    866:        return(-1);
                    867: }
                    868:
                    869: /*
                    870:  * Unknown option processing.
                    871:  * The third argument `off' is the offset from the IPv6 header to the option,
                    872:  * which is necessary if the IPv6 header the and option header and IPv6 header
                    873:  * is not continuous in order to return an ICMPv6 error.
                    874:  */
                    875: int
                    876: ip6_unknown_opt(optp, m, off)
                    877:        u_int8_t *optp;
                    878:        struct mbuf *m;
                    879:        int off;
                    880: {
                    881:        struct ip6_hdr *ip6;
                    882:
                    883:        switch(IP6OPT_TYPE(*optp)) {
                    884:         case IP6OPT_TYPE_SKIP: /* ignore the option */
                    885:                 return((int)*(optp + 1));
                    886:         case IP6OPT_TYPE_DISCARD:      /* silently discard */
                    887:                 m_freem(m);
                    888:                 return(-1);
                    889:         case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
                    890:                 ip6stat.ip6s_badoptions++;
                    891:                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
                    892:                 return(-1);
                    893:         case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
                    894:                 ip6stat.ip6s_badoptions++;
                    895:                 ip6 = mtod(m, struct ip6_hdr *);
                    896:                 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
                    897:                     (m->m_flags & (M_BCAST|M_MCAST)))
                    898:                         m_freem(m);
                    899:                 else
                    900:                         icmp6_error(m, ICMP6_PARAM_PROB,
                    901:                                     ICMP6_PARAMPROB_OPTION, off);
                    902:                 return(-1);
                    903:        }
                    904:
                    905:        m_freem(m);             /* XXX: NOTREACHED */
                    906:        return(-1);
                    907: }
                    908:
                    909: /*
1.9       itojun    910:  * Create the "control" list for this pcb.
                    911:  *
                    912:  * The routine will be called from upper layer handlers like tcp6_input().
                    913:  * Thus the routine assumes that the caller (tcp6_input) have already
                    914:  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
                    915:  * very first mbuf on the mbuf chain.
                    916:  * We may want to add some infinite loop prevention or sanity checks for safety.
                    917:  * (This applies only when you are using KAME mbuf chain restriction, i.e.
                    918:  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
1.2       itojun    919:  */
                    920: void
                    921: ip6_savecontrol(in6p, mp, ip6, m)
                    922:        register struct in6pcb *in6p;
                    923:        register struct mbuf **mp;
                    924:        register struct ip6_hdr *ip6;
                    925:        register struct mbuf *m;
                    926: {
                    927:        struct proc *p = curproc;       /* XXX */
1.9       itojun    928:        int privileged;
1.2       itojun    929:
1.9       itojun    930:        privileged = 0;
                    931:        if (p && !suser(p->p_ucred, &p->p_acflag))
                    932:                privileged++;
                    933:
                    934: #ifdef SO_TIMESTAMP
1.2       itojun    935:        if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
                    936:                struct timeval tv;
                    937:
                    938:                microtime(&tv);
                    939:                *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
                    940:                        SCM_TIMESTAMP, SOL_SOCKET);
                    941:                if (*mp)
                    942:                        mp = &(*mp)->m_next;
                    943:        }
1.9       itojun    944: #endif
1.2       itojun    945:        if (in6p->in6p_flags & IN6P_RECVDSTADDR) {
                    946:                *mp = sbcreatecontrol((caddr_t) &ip6->ip6_dst,
                    947:                        sizeof(struct in6_addr), IPV6_RECVDSTADDR,
                    948:                        IPPROTO_IPV6);
                    949:                if (*mp)
                    950:                        mp = &(*mp)->m_next;
                    951:        }
                    952:
                    953: #ifdef noyet
                    954:        /* options were tossed above */
                    955:        if (in6p->in6p_flags & IN6P_RECVOPTS)
                    956:                /* broken */
                    957:        /* ip6_srcroute doesn't do what we want here, need to fix */
                    958:        if (in6p->in6p_flags & IPV6P_RECVRETOPTS)
                    959:                /* broken */
                    960: #endif
                    961:
                    962:        /* RFC 2292 sec. 5 */
                    963:        if (in6p->in6p_flags & IN6P_PKTINFO) {
                    964:                struct in6_pktinfo pi6;
                    965:                bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
                    966:                if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
                    967:                        pi6.ipi6_addr.s6_addr16[1] = 0;
                    968:                pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
                    969:                                        ? m->m_pkthdr.rcvif->if_index
                    970:                                        : 0;
                    971:                *mp = sbcreatecontrol((caddr_t) &pi6,
                    972:                        sizeof(struct in6_pktinfo), IPV6_PKTINFO,
                    973:                        IPPROTO_IPV6);
                    974:                if (*mp)
                    975:                        mp = &(*mp)->m_next;
                    976:        }
                    977:        if (in6p->in6p_flags & IN6P_HOPLIMIT) {
                    978:                int hlim = ip6->ip6_hlim & 0xff;
                    979:                *mp = sbcreatecontrol((caddr_t) &hlim,
                    980:                        sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
                    981:                if (*mp)
                    982:                        mp = &(*mp)->m_next;
                    983:        }
                    984:        /* IN6P_NEXTHOP - for outgoing packet only */
                    985:
                    986:        /*
                    987:         * IPV6_HOPOPTS socket option. We require super-user privilege
                    988:         * for the option, but it might be too strict, since there might
                    989:         * be some hop-by-hop options which can be returned to normal user.
                    990:         * See RFC 2292 section 6.
                    991:         */
1.9       itojun    992:        if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
1.2       itojun    993:                /*
                    994:                 * Check if a hop-by-hop options header is contatined in the
                    995:                 * received packet, and if so, store the options as ancillary
                    996:                 * data. Note that a hop-by-hop options header must be
                    997:                 * just after the IPv6 header, which fact is assured through
                    998:                 * the IPv6 input processing.
                    999:                 */
                   1000:                struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                   1001:                if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9       itojun   1002:                        struct ip6_hbh *hbh;
                   1003:                        int hbhlen;
                   1004:
                   1005: #ifndef PULLDOWN_TEST
                   1006:                        hbh = (struct ip6_hbh *)(ip6 + 1);
                   1007:                        hbhlen = (hbh->ip6h_len + 1) << 3;
                   1008: #else
                   1009:                        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                   1010:                                sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
                   1011:                        if (hbh == NULL) {
                   1012:                                ip6stat.ip6s_tooshort++;
                   1013:                                return;
                   1014:                        }
                   1015:                        hbhlen = (hbh->ip6h_len + 1) << 3;
                   1016:                        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                   1017:                                sizeof(struct ip6_hdr), hbhlen);
                   1018:                        if (hbh == NULL) {
                   1019:                                ip6stat.ip6s_tooshort++;
                   1020:                                return;
                   1021:                        }
                   1022: #endif
1.2       itojun   1023:
                   1024:                        /*
                   1025:                         * XXX: We copy whole the header even if a jumbo
                   1026:                         * payload option is included, which option is to
                   1027:                         * be removed before returning in the RFC 2292.
                   1028:                         * But it's too painful operation...
                   1029:                         */
1.9       itojun   1030:                        *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1.2       itojun   1031:                                              IPV6_HOPOPTS, IPPROTO_IPV6);
                   1032:                        if (*mp)
                   1033:                                mp = &(*mp)->m_next;
                   1034:                }
                   1035:        }
                   1036:
                   1037:        /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
                   1038:        if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
                   1039:                struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                   1040:                int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);;
                   1041:
                   1042:                /*
                   1043:                 * Search for destination options headers or routing
                   1044:                 * header(s) through the header chain, and stores each
                   1045:                 * header as ancillary data.
                   1046:                 * Note that the order of the headers remains in
                   1047:                 * the chain of ancillary data.
                   1048:                 */
                   1049:                while(1) {      /* is explicit loop prevention necessary? */
1.9       itojun   1050:                        struct ip6_ext *ip6e;
                   1051:                        int elen;
                   1052:
                   1053: #ifndef PULLDOWN_TEST
                   1054:                        ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
                   1055:                        if (nxt == IPPROTO_AH)
                   1056:                                elen = (ip6e->ip6e_len + 2) << 2;
                   1057:                        else
                   1058:                                elen = (ip6e->ip6e_len + 1) << 3;
                   1059: #else
                   1060:                        IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off,
                   1061:                                sizeof(struct ip6_ext));
                   1062:                        if (ip6e == NULL) {
                   1063:                                ip6stat.ip6s_tooshort++;
                   1064:                                return;
                   1065:                        }
                   1066:                        if (nxt == IPPROTO_AH)
                   1067:                                elen = (ip6e->ip6e_len + 2) << 2;
                   1068:                        else
                   1069:                                elen = (ip6e->ip6e_len + 1) << 3;
                   1070:                        IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off, elen);
                   1071:                        if (ip6e == NULL) {
                   1072:                                ip6stat.ip6s_tooshort++;
                   1073:                                return;
                   1074:                        }
                   1075: #endif
1.2       itojun   1076:
                   1077:                        switch(nxt) {
                   1078:                         case IPPROTO_DSTOPTS:
                   1079:                                 if (!in6p->in6p_flags & IN6P_DSTOPTS)
                   1080:                                         break;
                   1081:
                   1082:                                 /*
                   1083:                                  * We also require super-user privilege for
                   1084:                                  * the option.
                   1085:                                  * See the comments on IN6_HOPOPTS.
                   1086:                                  */
1.9       itojun   1087:                                 if (!privileged)
1.2       itojun   1088:                                         break;
                   1089:
1.9       itojun   1090:                                 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1.2       itojun   1091:                                                       IPV6_DSTOPTS,
                   1092:                                                       IPPROTO_IPV6);
                   1093:                                 if (*mp)
                   1094:                                         mp = &(*mp)->m_next;
                   1095:                                 break;
                   1096:
                   1097:                         case IPPROTO_ROUTING:
                   1098:                                 if (!in6p->in6p_flags & IN6P_RTHDR)
                   1099:                                         break;
                   1100:
1.9       itojun   1101:                                 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1.2       itojun   1102:                                                       IPV6_RTHDR,
                   1103:                                                       IPPROTO_IPV6);
                   1104:                                 if (*mp)
                   1105:                                         mp = &(*mp)->m_next;
                   1106:                                 break;
                   1107:
                   1108:                         case IPPROTO_UDP:
                   1109:                         case IPPROTO_TCP:
                   1110:                         case IPPROTO_ICMPV6:
                   1111:                         default:
                   1112:                                 /*
                   1113:                                  * stop search if we encounter an upper
                   1114:                                  * layer protocol headers.
                   1115:                                  */
                   1116:                                 goto loopend;
                   1117:
                   1118:                         case IPPROTO_HOPOPTS:
                   1119:                         case IPPROTO_AH: /* is it possible? */
                   1120:                                 break;
                   1121:                        }
                   1122:
                   1123:                        /* proceed with the next header. */
1.9       itojun   1124:                        off += elen;
1.2       itojun   1125:                        nxt = ip6e->ip6e_nxt;
                   1126:                }
                   1127:          loopend:
                   1128:        }
1.9       itojun   1129:        if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
1.2       itojun   1130:                /* to be done */
                   1131:        }
1.9       itojun   1132:        if ((in6p->in6p_flags & IN6P_DSTOPTS) && privileged) {
1.2       itojun   1133:                /* to be done */
                   1134:        }
                   1135:        /* IN6P_RTHDR - to be done */
                   1136:
                   1137: }
                   1138:
                   1139: /*
                   1140:  * Get pointer to the previous header followed by the header
                   1141:  * currently processed.
                   1142:  * XXX: This function supposes that
                   1143:  *     M includes all headers,
                   1144:  *     the next header field and the header length field of each header
                   1145:  *     are valid, and
                   1146:  *     the sum of each header length equals to OFF.
                   1147:  * Because of these assumptions, this function must be called very
                   1148:  * carefully. Moreover, it will not be used in the near future when
                   1149:  * we develop `neater' mechanism to process extension headers.
                   1150:  */
                   1151: char *
                   1152: ip6_get_prevhdr(m, off)
                   1153:        struct mbuf *m;
                   1154:        int off;
                   1155: {
                   1156:        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                   1157:
                   1158:        if (off == sizeof(struct ip6_hdr))
                   1159:                return(&ip6->ip6_nxt);
                   1160:        else {
                   1161:                int len, nxt;
                   1162:                struct ip6_ext *ip6e = NULL;
                   1163:
                   1164:                nxt = ip6->ip6_nxt;
                   1165:                len = sizeof(struct ip6_hdr);
                   1166:                while (len < off) {
                   1167:                        ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
                   1168:
                   1169:                        switch(nxt) {
                   1170:                        case IPPROTO_FRAGMENT:
                   1171:                                len += sizeof(struct ip6_frag);
                   1172:                                break;
                   1173:                        case IPPROTO_AH:
                   1174:                                len += (ip6e->ip6e_len + 2) << 2;
                   1175:                                break;
                   1176:                        default:
                   1177:                                len += (ip6e->ip6e_len + 1) << 3;
                   1178:                                break;
                   1179:                        }
                   1180:                        nxt = ip6e->ip6e_nxt;
                   1181:                }
                   1182:                if (ip6e)
                   1183:                        return(&ip6e->ip6e_nxt);
                   1184:                else
                   1185:                        return NULL;
1.18      itojun   1186:        }
                   1187: }
                   1188:
                   1189: /*
                   1190:  * get next header offset.  m will be retained.
                   1191:  */
                   1192: int
                   1193: ip6_nexthdr(m, off, proto, nxtp)
                   1194:        struct mbuf *m;
                   1195:        int off;
                   1196:        int proto;
                   1197:        int *nxtp;
                   1198: {
                   1199:        struct ip6_hdr ip6;
                   1200:        struct ip6_ext ip6e;
                   1201:        struct ip6_frag fh;
                   1202:
                   1203:        /* just in case */
                   1204:        if (m == NULL)
                   1205:                panic("ip6_nexthdr: m == NULL");
                   1206:        if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
                   1207:                return -1;
                   1208:
                   1209:        switch (proto) {
                   1210:        case IPPROTO_IPV6:
                   1211:                if (m->m_pkthdr.len < off + sizeof(ip6))
                   1212:                        return -1;
                   1213:                m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
                   1214:                if (nxtp)
                   1215:                        *nxtp = ip6.ip6_nxt;
                   1216:                off += sizeof(ip6);
                   1217:                return off;
                   1218:
                   1219:        case IPPROTO_FRAGMENT:
                   1220:                /*
                   1221:                 * terminate parsing if it is not the first fragment,
                   1222:                 * it does not make sense to parse through it.
                   1223:                 */
                   1224:                if (m->m_pkthdr.len < off + sizeof(fh))
                   1225:                        return -1;
                   1226:                m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
                   1227:                if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
                   1228:                        return -1;
                   1229:                if (nxtp)
                   1230:                        *nxtp = fh.ip6f_nxt;
                   1231:                off += sizeof(struct ip6_frag);
                   1232:                return off;
                   1233:
                   1234:        case IPPROTO_AH:
                   1235:                if (m->m_pkthdr.len < off + sizeof(ip6e))
                   1236:                        return -1;
                   1237:                m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
                   1238:                if (nxtp)
                   1239:                        *nxtp = ip6e.ip6e_nxt;
                   1240:                off += (ip6e.ip6e_len + 2) << 2;
                   1241:                return off;
                   1242:
                   1243:        case IPPROTO_HOPOPTS:
                   1244:        case IPPROTO_ROUTING:
                   1245:        case IPPROTO_DSTOPTS:
                   1246:                if (m->m_pkthdr.len < off + sizeof(ip6e))
                   1247:                        return -1;
                   1248:                m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
                   1249:                if (nxtp)
                   1250:                        *nxtp = ip6e.ip6e_nxt;
                   1251:                off += (ip6e.ip6e_len + 1) << 3;
                   1252:                return off;
                   1253:
                   1254:        case IPPROTO_NONE:
                   1255:        case IPPROTO_ESP:
                   1256:        case IPPROTO_IPCOMP:
                   1257:                /* give up */
                   1258:                return -1;
                   1259:
                   1260:        default:
                   1261:                return -1;
                   1262:        }
                   1263:
                   1264:        return -1;
                   1265: }
                   1266:
                   1267: /*
                   1268:  * get offset for the last header in the chain.  m will be kept untainted.
                   1269:  */
                   1270: int
                   1271: ip6_lasthdr(m, off, proto, nxtp)
                   1272:        struct mbuf *m;
                   1273:        int off;
                   1274:        int proto;
                   1275:        int *nxtp;
                   1276: {
                   1277:        int newoff;
                   1278:        int nxt;
                   1279:
                   1280:        if (!nxtp) {
                   1281:                nxt = -1;
                   1282:                nxtp = &nxt;
                   1283:        }
                   1284:        while (1) {
                   1285:                newoff = ip6_nexthdr(m, off, proto, nxtp);
                   1286:                if (newoff < 0)
                   1287:                        return off;
                   1288:                else if (newoff < off)
                   1289:                        return -1;      /* invalid */
                   1290:                else if (newoff == off)
                   1291:                        return newoff;
                   1292:
                   1293:                off = newoff;
                   1294:                proto = *nxtp;
1.2       itojun   1295:        }
                   1296: }
                   1297:
                   1298: /*
                   1299:  * System control for IP6
                   1300:  */
                   1301:
                   1302: u_char inet6ctlerrmap[PRC_NCMDS] = {
                   1303:        0,              0,              0,              0,
                   1304:        0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
                   1305:        EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
                   1306:        EMSGSIZE,       EHOSTUNREACH,   0,              0,
                   1307:        0,              0,              0,              0,
                   1308:        ENOPROTOOPT
                   1309: };
                   1310:
                   1311: #include <vm/vm.h>
                   1312: #include <sys/sysctl.h>
                   1313:
                   1314: int
                   1315: ip6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
                   1316:        int *name;
                   1317:        u_int namelen;
                   1318:        void *oldp;
                   1319:        size_t *oldlenp;
                   1320:        void *newp;
                   1321:        size_t newlen;
                   1322: {
                   1323:        /* All sysctl names at this level are terminal. */
                   1324:        if (namelen != 1)
                   1325:                return ENOTDIR;
                   1326:
                   1327:        switch (name[0]) {
                   1328:
                   1329:        case IPV6CTL_FORWARDING:
                   1330:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1331:                                  &ip6_forwarding);
                   1332:        case IPV6CTL_SENDREDIRECTS:
                   1333:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1334:                                &ip6_sendredirects);
                   1335:        case IPV6CTL_DEFHLIM:
                   1336:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_defhlim);
                   1337:        case IPV6CTL_MAXFRAGPACKETS:
                   1338:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1339:                                &ip6_maxfragpackets);
                   1340:        case IPV6CTL_ACCEPT_RTADV:
                   1341:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1342:                                &ip6_accept_rtadv);
                   1343:        case IPV6CTL_KEEPFAITH:
                   1344:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_keepfaith);
                   1345:        case IPV6CTL_LOG_INTERVAL:
                   1346:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1347:                                &ip6_log_interval);
                   1348:        case IPV6CTL_HDRNESTLIMIT:
                   1349:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1350:                                &ip6_hdrnestlimit);
                   1351:        case IPV6CTL_DAD_COUNT:
                   1352:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_dad_count);
                   1353:        case IPV6CTL_AUTO_FLOWLABEL:
                   1354:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1355:                                &ip6_auto_flowlabel);
                   1356:        case IPV6CTL_DEFMCASTHLIM:
                   1357:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1358:                                &ip6_defmcasthlim);
                   1359:        case IPV6CTL_GIF_HLIM:
                   1360:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1361:                                &ip6_gif_hlim);
                   1362:        case IPV6CTL_KAME_VERSION:
                   1363:                return sysctl_rdstring(oldp, oldlenp, newp, __KAME_VERSION);
1.9       itojun   1364:        case IPV6CTL_USE_DEPRECATED:
                   1365:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1366:                                &ip6_use_deprecated);
1.10      itojun   1367:        case IPV6CTL_RR_PRUNE:
                   1368:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_rr_prune);
1.12      itojun   1369: #ifndef INET6_BINDV6ONLY
1.11      itojun   1370:        case IPV6CTL_BINDV6ONLY:
                   1371:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1372:                                &ip6_bindv6only);
                   1373: #endif
1.2       itojun   1374:        default:
                   1375:                return EOPNOTSUPP;
                   1376:        }
                   1377:        /* NOTREACHED */
                   1378: }

CVSweb <webmaster@jp.NetBSD.org>