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

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

CVSweb <webmaster@jp.NetBSD.org>