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

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

CVSweb <webmaster@jp.NetBSD.org>