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

1.22.2.1! thorpej     1: /*     $NetBSD: ip6_input.c,v 1.22 2000/06/13 14:43:44 itojun Exp $    */
        !             2: /*     $KAME: ip6_input.c,v 1.95 2000/07/02 07:49:37 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.22      itojun    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.22      itojun    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.22      itojun    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.22      itojun    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:                if (ia6->ia6_flags & IN6_IFF_ANYCAST)
                    492:                        m->m_flags |= M_ANYCAST6;
1.22.2.1! thorpej   493:                /*
        !           494:                 * packets to a tentative, duplicated, or somehow invalid
        !           495:                 * address must not be accepted.
        !           496:                 */
1.2       itojun    497:                if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
1.22.2.1! thorpej   498:                        /* this address is ready */
1.2       itojun    499:                        ours = 1;
1.9       itojun    500:                        deliverifp = ia6->ia_ifp;       /* correct? */
1.2       itojun    501:                        goto hbhcheck;
                    502:                } else {
1.22.2.1! thorpej   503:                        /* address is not ready, so discard the packet. */
        !           504:                        log(LOG_INFO,
        !           505:                            "ip6_input: packet to an unready address %s->%s",
        !           506:                            ip6_sprintf(&ip6->ip6_src),
        !           507:                            ip6_sprintf(&ip6->ip6_dst));
        !           508:
        !           509:                        goto bad;
1.2       itojun    510:                }
                    511:        }
                    512:
                    513:        /*
                    514:         * FAITH(Firewall Aided Internet Translator)
                    515:         */
                    516: #if defined(NFAITH) && 0 < NFAITH
                    517:        if (ip6_keepfaith) {
                    518:                if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
                    519:                 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
                    520:                        /* XXX do we need more sanity checks? */
                    521:                        ours = 1;
1.9       itojun    522:                        deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /*faith*/
                    523:                        goto hbhcheck;
                    524:                }
                    525:        }
                    526: #endif
                    527:
                    528: #if 0
                    529:     {
                    530:        /*
                    531:         * Last resort: check in6_ifaddr for incoming interface.
                    532:         * The code is here until I update the "goto ours hack" code above
                    533:         * working right.
                    534:         */
                    535:        struct ifaddr *ifa;
                    536:        for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
                    537:             ifa;
                    538:             ifa = ifa->ifa_list.tqe_next) {
                    539:                if (ifa->ifa_addr == NULL)
                    540:                        continue;       /* just for safety */
                    541:                if (ifa->ifa_addr->sa_family != AF_INET6)
                    542:                        continue;
                    543:                if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
                    544:                        ours = 1;
                    545:                        deliverifp = ifa->ifa_ifp;
1.2       itojun    546:                        goto hbhcheck;
                    547:                }
                    548:        }
1.9       itojun    549:     }
1.2       itojun    550: #endif
                    551:
                    552:        /*
                    553:         * Now there is no reason to process the packet if it's not our own
                    554:         * and we're not a router.
                    555:         */
                    556:        if (!ip6_forwarding) {
                    557:                ip6stat.ip6s_cantforward++;
1.9       itojun    558:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2       itojun    559:                goto bad;
                    560:        }
                    561:
                    562:   hbhcheck:
                    563:        /*
                    564:         * Process Hop-by-Hop options header if it's contained.
                    565:         * m may be modified in ip6_hopopts_input().
                    566:         * If a JumboPayload option is included, plen will also be modified.
                    567:         */
                    568:        plen = (u_int32_t)ntohs(ip6->ip6_plen);
                    569:        if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9       itojun    570:                struct ip6_hbh *hbh;
                    571:
                    572:                if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
                    573: #if 0  /*touches NULL pointer*/
                    574:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
                    575: #endif
1.2       itojun    576:                        return; /* m have already been freed */
1.9       itojun    577:                }
1.22      itojun    578:
1.2       itojun    579:                /* adjust pointer */
                    580:                ip6 = mtod(m, struct ip6_hdr *);
1.22      itojun    581:
                    582:                /*
                    583:                 * if the payload length field is 0 and the next header field
                    584:                 * indicates Hop-by-Hop Options header, then a Jumbo Payload
                    585:                 * option MUST be included.
                    586:                 */
                    587:                if (ip6->ip6_plen == 0 && plen == 0) {
                    588:                        /*
                    589:                         * Note that if a valid jumbo payload option is
                    590:                         * contained, ip6_hoptops_input() must set a valid
                    591:                         * (non-zero) payload length to the variable plen.
                    592:                         */
                    593:                        ip6stat.ip6s_badoptions++;
                    594:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
                    595:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
                    596:                        icmp6_error(m, ICMP6_PARAM_PROB,
                    597:                                    ICMP6_PARAMPROB_HEADER,
                    598:                                    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
                    599:                        return;
                    600:                }
1.9       itojun    601: #ifndef PULLDOWN_TEST
                    602:                /* ip6_hopopts_input() ensures that mbuf is contiguous */
                    603:                hbh = (struct ip6_hbh *)(ip6 + 1);
                    604: #else
                    605:                IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
                    606:                        sizeof(struct ip6_hbh));
                    607:                if (hbh == NULL) {
                    608:                        ip6stat.ip6s_tooshort++;
                    609:                        return;
                    610:                }
                    611: #endif
                    612:                nxt = hbh->ip6h_nxt;
1.2       itojun    613:
                    614:                /*
                    615:                 * accept the packet if a router alert option is included
                    616:                 * and we act as an IPv6 router.
                    617:                 */
1.5       itojun    618:                if (rtalert != ~0 && ip6_forwarding)
1.2       itojun    619:                        ours = 1;
                    620:        } else
                    621:                nxt = ip6->ip6_nxt;
                    622:
                    623:        /*
                    624:         * Check that the amount of data in the buffers
                    625:         * is as at least much as the IPv6 header would have us expect.
                    626:         * Trim mbufs if longer than we expect.
                    627:         * Drop packet if shorter than we expect.
                    628:         */
                    629:        if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
                    630:                ip6stat.ip6s_tooshort++;
1.9       itojun    631:                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.2       itojun    632:                goto bad;
                    633:        }
                    634:        if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
                    635:                if (m->m_len == m->m_pkthdr.len) {
                    636:                        m->m_len = sizeof(struct ip6_hdr) + plen;
                    637:                        m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
                    638:                } else
                    639:                        m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
                    640:        }
                    641:
                    642:        /*
                    643:         * Forward if desirable.
                    644:         */
                    645:        if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
                    646:                /*
                    647:                 * If we are acting as a multicast router, all
                    648:                 * incoming multicast packets are passed to the
                    649:                 * kernel-level multicast forwarding function.
                    650:                 * The packet is returned (relatively) intact; if
                    651:                 * ip6_mforward() returns a non-zero value, the packet
                    652:                 * must be discarded, else it may be accepted below.
                    653:                 */
                    654:                if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
                    655:                        ip6stat.ip6s_cantforward++;
                    656:                        m_freem(m);
                    657:                        return;
                    658:                }
                    659:                if (!ours) {
                    660:                        m_freem(m);
                    661:                        return;
                    662:                }
1.22      itojun    663:        } else if (!ours) {
1.2       itojun    664:                ip6_forward(m, 0);
                    665:                return;
                    666:        }
                    667:
                    668:        /*
                    669:         * Tell launch routine the next header
                    670:         */
1.12      itojun    671: #ifdef IFA_STATS
1.9       itojun    672:        if (IFA_STATS && deliverifp != NULL) {
                    673:                struct in6_ifaddr *ia6;
                    674:                ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
                    675:                if (ia6)
                    676:                        ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
                    677:        }
                    678: #endif
1.2       itojun    679:        ip6stat.ip6s_delivered++;
1.9       itojun    680:        in6_ifstat_inc(deliverifp, ifs6_in_deliver);
1.2       itojun    681:        nest = 0;
                    682:        while (nxt != IPPROTO_DONE) {
                    683:                if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
                    684:                        ip6stat.ip6s_toomanyhdr++;
                    685:                        goto bad;
                    686:                }
1.8       itojun    687:
                    688:                /*
                    689:                 * protection against faulty packet - there should be
                    690:                 * more sanity checks in header chain processing.
                    691:                 */
                    692:                if (m->m_pkthdr.len < off) {
                    693:                        ip6stat.ip6s_tooshort++;
1.9       itojun    694:                        in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.8       itojun    695:                        goto bad;
                    696:                }
                    697:
1.2       itojun    698:                nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
                    699:        }
                    700:        return;
                    701:  bad:
                    702:        m_freem(m);
                    703: }
                    704:
                    705: /*
                    706:  * Hop-by-Hop options header processing. If a valid jumbo payload option is
                    707:  * included, the real payload length will be stored in plenp.
                    708:  */
                    709: static int
                    710: ip6_hopopts_input(plenp, rtalertp, mp, offp)
                    711:        u_int32_t *plenp;
1.5       itojun    712:        u_int32_t *rtalertp;    /* XXX: should be stored more smart way */
1.2       itojun    713:        struct mbuf **mp;
                    714:        int *offp;
                    715: {
                    716:        register struct mbuf *m = *mp;
                    717:        int off = *offp, hbhlen;
                    718:        struct ip6_hbh *hbh;
                    719:        u_int8_t *opt;
                    720:
                    721:        /* validation of the length of the header */
1.9       itojun    722: #ifndef PULLDOWN_TEST
1.2       itojun    723:        IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
                    724:        hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
                    725:        hbhlen = (hbh->ip6h_len + 1) << 3;
                    726:
                    727:        IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
                    728:        hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1.9       itojun    729: #else
                    730:        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                    731:                sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
                    732:        if (hbh == NULL) {
                    733:                ip6stat.ip6s_tooshort++;
                    734:                return -1;
                    735:        }
                    736:        hbhlen = (hbh->ip6h_len + 1) << 3;
                    737:        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
                    738:                hbhlen);
                    739:        if (hbh == NULL) {
                    740:                ip6stat.ip6s_tooshort++;
                    741:                return -1;
                    742:        }
                    743: #endif
1.2       itojun    744:        off += hbhlen;
                    745:        hbhlen -= sizeof(struct ip6_hbh);
                    746:        opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
                    747:
                    748:        if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
                    749:                                hbhlen, rtalertp, plenp) < 0)
                    750:                return(-1);
                    751:
                    752:        *offp = off;
                    753:        *mp = m;
                    754:        return(0);
                    755: }
                    756:
                    757: /*
                    758:  * Search header for all Hop-by-hop options and process each option.
                    759:  * This function is separate from ip6_hopopts_input() in order to
                    760:  * handle a case where the sending node itself process its hop-by-hop
                    761:  * options header. In such a case, the function is called from ip6_output().
                    762:  */
                    763: int
                    764: ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
                    765:        struct mbuf *m;
                    766:        u_int8_t *opthead;
                    767:        int hbhlen;
1.5       itojun    768:        u_int32_t *rtalertp;
1.2       itojun    769:        u_int32_t *plenp;
                    770: {
                    771:        struct ip6_hdr *ip6;
                    772:        int optlen = 0;
                    773:        u_int8_t *opt = opthead;
                    774:        u_int16_t rtalert_val;
1.22      itojun    775:        u_int32_t jumboplen;
1.2       itojun    776:
                    777:        for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
                    778:                switch(*opt) {
                    779:                 case IP6OPT_PAD1:
                    780:                         optlen = 1;
                    781:                         break;
                    782:                 case IP6OPT_PADN:
                    783:                         if (hbhlen < IP6OPT_MINLEN) {
                    784:                                 ip6stat.ip6s_toosmall++;
                    785:                                 goto bad;
                    786:                         }
                    787:                         optlen = *(opt + 1) + 2;
                    788:                         break;
                    789:                 case IP6OPT_RTALERT:
1.8       itojun    790:                         /* XXX may need check for alignment */
1.2       itojun    791:                         if (hbhlen < IP6OPT_RTALERT_LEN) {
                    792:                                 ip6stat.ip6s_toosmall++;
                    793:                                 goto bad;
                    794:                         }
                    795:                         if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2)
                    796:                                  /* XXX: should we discard the packet? */
                    797:                                 log(LOG_ERR, "length of router alert opt is inconsitent(%d)",
                    798:                                     *(opt + 1));
                    799:                         optlen = IP6OPT_RTALERT_LEN;
                    800:                         bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
                    801:                         *rtalertp = ntohs(rtalert_val);
                    802:                         break;
                    803:                 case IP6OPT_JUMBO:
1.22      itojun    804:                        /* XXX may need check for alignment */
                    805:                        if (hbhlen < IP6OPT_JUMBO_LEN) {
                    806:                                ip6stat.ip6s_toosmall++;
                    807:                                goto bad;
                    808:                        }
                    809:                        if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2)
                    810:                                 /* XXX: should we discard the packet? */
                    811:                                log(LOG_ERR, "length of jumbopayload opt "
                    812:                                    "is inconsistent(%d)",
                    813:                                    *(opt + 1));
                    814:                        optlen = IP6OPT_JUMBO_LEN;
                    815:
                    816:                        /*
                    817:                         * IPv6 packets that have non 0 payload length
                    818:                         * must not contain a jumbo paylod option.
                    819:                         */
                    820:                        ip6 = mtod(m, struct ip6_hdr *);
                    821:                        if (ip6->ip6_plen) {
                    822:                                ip6stat.ip6s_badoptions++;
                    823:                                icmp6_error(m, ICMP6_PARAM_PROB,
                    824:                                            ICMP6_PARAMPROB_HEADER,
                    825:                                            sizeof(struct ip6_hdr) +
                    826:                                            sizeof(struct ip6_hbh) +
                    827:                                            opt - opthead);
                    828:                                return(-1);
                    829:                        }
1.2       itojun    830:
1.22      itojun    831:                        /*
                    832:                         * We may see jumbolen in unaligned location, so
                    833:                         * we'd need to perform bcopy().
                    834:                         */
                    835:                        bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
                    836:                        jumboplen = (u_int32_t)htonl(jumboplen);
                    837:
                    838: #if 1
                    839:                        /*
                    840:                         * if there are multiple jumbo payload options,
                    841:                         * *plenp will be non-zero and the packet will be
                    842:                         * rejected.
                    843:                         * the behavior may need some debate in ipngwg -
                    844:                         * multiple options does not make sense, however,
                    845:                         * there's no explicit mention in specification.
                    846:                         */
                    847:                        if (*plenp != 0) {
                    848:                                ip6stat.ip6s_badoptions++;
                    849:                                icmp6_error(m, ICMP6_PARAM_PROB,
                    850:                                            ICMP6_PARAMPROB_HEADER,
                    851:                                            sizeof(struct ip6_hdr) +
                    852:                                            sizeof(struct ip6_hbh) +
                    853:                                            opt + 2 - opthead);
                    854:                                return(-1);
                    855:                        }
1.8       itojun    856: #endif
1.2       itojun    857:
1.22      itojun    858:                        /*
                    859:                         * jumbo payload length must be larger than 65535.
                    860:                         */
                    861:                        if (jumboplen <= IPV6_MAXPACKET) {
                    862:                                ip6stat.ip6s_badoptions++;
                    863:                                icmp6_error(m, ICMP6_PARAM_PROB,
                    864:                                            ICMP6_PARAMPROB_HEADER,
                    865:                                            sizeof(struct ip6_hdr) +
                    866:                                            sizeof(struct ip6_hbh) +
                    867:                                            opt + 2 - opthead);
                    868:                                return(-1);
                    869:                        }
                    870:                        *plenp = jumboplen;
                    871:
                    872:                        break;
1.2       itojun    873:                 default:               /* unknown option */
                    874:                         if (hbhlen < IP6OPT_MINLEN) {
                    875:                                 ip6stat.ip6s_toosmall++;
                    876:                                 goto bad;
                    877:                         }
                    878:                         if ((optlen = ip6_unknown_opt(opt, m,
                    879:                                                       sizeof(struct ip6_hdr) +
                    880:                                                       sizeof(struct ip6_hbh) +
                    881:                                                       opt - opthead)) == -1)
                    882:                                 return(-1);
                    883:                         optlen += 2;
                    884:                         break;
                    885:                }
                    886:        }
                    887:
                    888:        return(0);
                    889:
                    890:   bad:
                    891:        m_freem(m);
                    892:        return(-1);
                    893: }
                    894:
                    895: /*
                    896:  * Unknown option processing.
                    897:  * The third argument `off' is the offset from the IPv6 header to the option,
                    898:  * which is necessary if the IPv6 header the and option header and IPv6 header
                    899:  * is not continuous in order to return an ICMPv6 error.
                    900:  */
                    901: int
                    902: ip6_unknown_opt(optp, m, off)
                    903:        u_int8_t *optp;
                    904:        struct mbuf *m;
                    905:        int off;
                    906: {
                    907:        struct ip6_hdr *ip6;
                    908:
                    909:        switch(IP6OPT_TYPE(*optp)) {
                    910:         case IP6OPT_TYPE_SKIP: /* ignore the option */
                    911:                 return((int)*(optp + 1));
                    912:         case IP6OPT_TYPE_DISCARD:      /* silently discard */
                    913:                 m_freem(m);
                    914:                 return(-1);
                    915:         case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
                    916:                 ip6stat.ip6s_badoptions++;
                    917:                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
                    918:                 return(-1);
                    919:         case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
                    920:                 ip6stat.ip6s_badoptions++;
                    921:                 ip6 = mtod(m, struct ip6_hdr *);
                    922:                 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
                    923:                     (m->m_flags & (M_BCAST|M_MCAST)))
                    924:                         m_freem(m);
                    925:                 else
                    926:                         icmp6_error(m, ICMP6_PARAM_PROB,
                    927:                                     ICMP6_PARAMPROB_OPTION, off);
                    928:                 return(-1);
                    929:        }
                    930:
                    931:        m_freem(m);             /* XXX: NOTREACHED */
                    932:        return(-1);
                    933: }
                    934:
                    935: /*
1.9       itojun    936:  * Create the "control" list for this pcb.
                    937:  *
                    938:  * The routine will be called from upper layer handlers like tcp6_input().
                    939:  * Thus the routine assumes that the caller (tcp6_input) have already
                    940:  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
                    941:  * very first mbuf on the mbuf chain.
                    942:  * We may want to add some infinite loop prevention or sanity checks for safety.
                    943:  * (This applies only when you are using KAME mbuf chain restriction, i.e.
                    944:  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
1.2       itojun    945:  */
                    946: void
                    947: ip6_savecontrol(in6p, mp, ip6, m)
                    948:        register struct in6pcb *in6p;
                    949:        register struct mbuf **mp;
                    950:        register struct ip6_hdr *ip6;
                    951:        register struct mbuf *m;
                    952: {
                    953:        struct proc *p = curproc;       /* XXX */
1.9       itojun    954:        int privileged;
1.2       itojun    955:
1.9       itojun    956:        privileged = 0;
                    957:        if (p && !suser(p->p_ucred, &p->p_acflag))
                    958:                privileged++;
                    959:
                    960: #ifdef SO_TIMESTAMP
1.2       itojun    961:        if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
                    962:                struct timeval tv;
                    963:
                    964:                microtime(&tv);
                    965:                *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
                    966:                        SCM_TIMESTAMP, SOL_SOCKET);
                    967:                if (*mp)
                    968:                        mp = &(*mp)->m_next;
                    969:        }
1.9       itojun    970: #endif
1.2       itojun    971:        if (in6p->in6p_flags & IN6P_RECVDSTADDR) {
                    972:                *mp = sbcreatecontrol((caddr_t) &ip6->ip6_dst,
                    973:                        sizeof(struct in6_addr), IPV6_RECVDSTADDR,
                    974:                        IPPROTO_IPV6);
                    975:                if (*mp)
                    976:                        mp = &(*mp)->m_next;
                    977:        }
                    978:
                    979: #ifdef noyet
                    980:        /* options were tossed above */
                    981:        if (in6p->in6p_flags & IN6P_RECVOPTS)
                    982:                /* broken */
                    983:        /* ip6_srcroute doesn't do what we want here, need to fix */
                    984:        if (in6p->in6p_flags & IPV6P_RECVRETOPTS)
                    985:                /* broken */
                    986: #endif
                    987:
                    988:        /* RFC 2292 sec. 5 */
                    989:        if (in6p->in6p_flags & IN6P_PKTINFO) {
                    990:                struct in6_pktinfo pi6;
                    991:                bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
                    992:                if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
                    993:                        pi6.ipi6_addr.s6_addr16[1] = 0;
                    994:                pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
                    995:                                        ? m->m_pkthdr.rcvif->if_index
                    996:                                        : 0;
                    997:                *mp = sbcreatecontrol((caddr_t) &pi6,
                    998:                        sizeof(struct in6_pktinfo), IPV6_PKTINFO,
                    999:                        IPPROTO_IPV6);
                   1000:                if (*mp)
                   1001:                        mp = &(*mp)->m_next;
                   1002:        }
                   1003:        if (in6p->in6p_flags & IN6P_HOPLIMIT) {
                   1004:                int hlim = ip6->ip6_hlim & 0xff;
                   1005:                *mp = sbcreatecontrol((caddr_t) &hlim,
                   1006:                        sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
                   1007:                if (*mp)
                   1008:                        mp = &(*mp)->m_next;
                   1009:        }
                   1010:        /* IN6P_NEXTHOP - for outgoing packet only */
                   1011:
                   1012:        /*
                   1013:         * IPV6_HOPOPTS socket option. We require super-user privilege
                   1014:         * for the option, but it might be too strict, since there might
                   1015:         * be some hop-by-hop options which can be returned to normal user.
                   1016:         * See RFC 2292 section 6.
                   1017:         */
1.9       itojun   1018:        if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
1.2       itojun   1019:                /*
                   1020:                 * Check if a hop-by-hop options header is contatined in the
                   1021:                 * received packet, and if so, store the options as ancillary
                   1022:                 * data. Note that a hop-by-hop options header must be
                   1023:                 * just after the IPv6 header, which fact is assured through
                   1024:                 * the IPv6 input processing.
                   1025:                 */
                   1026:                struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                   1027:                if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9       itojun   1028:                        struct ip6_hbh *hbh;
                   1029:                        int hbhlen;
                   1030:
                   1031: #ifndef PULLDOWN_TEST
                   1032:                        hbh = (struct ip6_hbh *)(ip6 + 1);
                   1033:                        hbhlen = (hbh->ip6h_len + 1) << 3;
                   1034: #else
                   1035:                        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                   1036:                                sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
                   1037:                        if (hbh == NULL) {
                   1038:                                ip6stat.ip6s_tooshort++;
                   1039:                                return;
                   1040:                        }
                   1041:                        hbhlen = (hbh->ip6h_len + 1) << 3;
                   1042:                        IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
                   1043:                                sizeof(struct ip6_hdr), hbhlen);
                   1044:                        if (hbh == NULL) {
                   1045:                                ip6stat.ip6s_tooshort++;
                   1046:                                return;
                   1047:                        }
                   1048: #endif
1.2       itojun   1049:
                   1050:                        /*
                   1051:                         * XXX: We copy whole the header even if a jumbo
                   1052:                         * payload option is included, which option is to
                   1053:                         * be removed before returning in the RFC 2292.
                   1054:                         * But it's too painful operation...
                   1055:                         */
1.9       itojun   1056:                        *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1.2       itojun   1057:                                              IPV6_HOPOPTS, IPPROTO_IPV6);
                   1058:                        if (*mp)
                   1059:                                mp = &(*mp)->m_next;
                   1060:                }
                   1061:        }
                   1062:
                   1063:        /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
                   1064:        if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
                   1065:                struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                   1066:                int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);;
                   1067:
                   1068:                /*
                   1069:                 * Search for destination options headers or routing
                   1070:                 * header(s) through the header chain, and stores each
                   1071:                 * header as ancillary data.
                   1072:                 * Note that the order of the headers remains in
                   1073:                 * the chain of ancillary data.
                   1074:                 */
                   1075:                while(1) {      /* is explicit loop prevention necessary? */
1.9       itojun   1076:                        struct ip6_ext *ip6e;
                   1077:                        int elen;
                   1078:
                   1079: #ifndef PULLDOWN_TEST
                   1080:                        ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
                   1081:                        if (nxt == IPPROTO_AH)
                   1082:                                elen = (ip6e->ip6e_len + 2) << 2;
                   1083:                        else
                   1084:                                elen = (ip6e->ip6e_len + 1) << 3;
                   1085: #else
                   1086:                        IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off,
                   1087:                                sizeof(struct ip6_ext));
                   1088:                        if (ip6e == NULL) {
                   1089:                                ip6stat.ip6s_tooshort++;
                   1090:                                return;
                   1091:                        }
                   1092:                        if (nxt == IPPROTO_AH)
                   1093:                                elen = (ip6e->ip6e_len + 2) << 2;
                   1094:                        else
                   1095:                                elen = (ip6e->ip6e_len + 1) << 3;
                   1096:                        IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off, elen);
                   1097:                        if (ip6e == NULL) {
                   1098:                                ip6stat.ip6s_tooshort++;
                   1099:                                return;
                   1100:                        }
                   1101: #endif
1.2       itojun   1102:
                   1103:                        switch(nxt) {
                   1104:                         case IPPROTO_DSTOPTS:
                   1105:                                 if (!in6p->in6p_flags & IN6P_DSTOPTS)
                   1106:                                         break;
                   1107:
                   1108:                                 /*
                   1109:                                  * We also require super-user privilege for
                   1110:                                  * the option.
                   1111:                                  * See the comments on IN6_HOPOPTS.
                   1112:                                  */
1.9       itojun   1113:                                 if (!privileged)
1.2       itojun   1114:                                         break;
                   1115:
1.9       itojun   1116:                                 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1.2       itojun   1117:                                                       IPV6_DSTOPTS,
                   1118:                                                       IPPROTO_IPV6);
                   1119:                                 if (*mp)
                   1120:                                         mp = &(*mp)->m_next;
                   1121:                                 break;
                   1122:
                   1123:                         case IPPROTO_ROUTING:
                   1124:                                 if (!in6p->in6p_flags & IN6P_RTHDR)
                   1125:                                         break;
                   1126:
1.9       itojun   1127:                                 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1.2       itojun   1128:                                                       IPV6_RTHDR,
                   1129:                                                       IPPROTO_IPV6);
                   1130:                                 if (*mp)
                   1131:                                         mp = &(*mp)->m_next;
                   1132:                                 break;
                   1133:
                   1134:                         case IPPROTO_UDP:
                   1135:                         case IPPROTO_TCP:
                   1136:                         case IPPROTO_ICMPV6:
                   1137:                         default:
                   1138:                                 /*
                   1139:                                  * stop search if we encounter an upper
                   1140:                                  * layer protocol headers.
                   1141:                                  */
                   1142:                                 goto loopend;
                   1143:
                   1144:                         case IPPROTO_HOPOPTS:
                   1145:                         case IPPROTO_AH: /* is it possible? */
                   1146:                                 break;
                   1147:                        }
                   1148:
                   1149:                        /* proceed with the next header. */
1.9       itojun   1150:                        off += elen;
1.2       itojun   1151:                        nxt = ip6e->ip6e_nxt;
                   1152:                }
                   1153:          loopend:
                   1154:        }
1.9       itojun   1155:        if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
1.2       itojun   1156:                /* to be done */
                   1157:        }
1.9       itojun   1158:        if ((in6p->in6p_flags & IN6P_DSTOPTS) && privileged) {
1.2       itojun   1159:                /* to be done */
                   1160:        }
                   1161:        /* IN6P_RTHDR - to be done */
                   1162:
                   1163: }
                   1164:
                   1165: /*
                   1166:  * Get pointer to the previous header followed by the header
                   1167:  * currently processed.
                   1168:  * XXX: This function supposes that
                   1169:  *     M includes all headers,
                   1170:  *     the next header field and the header length field of each header
                   1171:  *     are valid, and
                   1172:  *     the sum of each header length equals to OFF.
                   1173:  * Because of these assumptions, this function must be called very
                   1174:  * carefully. Moreover, it will not be used in the near future when
                   1175:  * we develop `neater' mechanism to process extension headers.
                   1176:  */
                   1177: char *
                   1178: ip6_get_prevhdr(m, off)
                   1179:        struct mbuf *m;
                   1180:        int off;
                   1181: {
                   1182:        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
                   1183:
                   1184:        if (off == sizeof(struct ip6_hdr))
                   1185:                return(&ip6->ip6_nxt);
                   1186:        else {
                   1187:                int len, nxt;
                   1188:                struct ip6_ext *ip6e = NULL;
                   1189:
                   1190:                nxt = ip6->ip6_nxt;
                   1191:                len = sizeof(struct ip6_hdr);
                   1192:                while (len < off) {
                   1193:                        ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
                   1194:
                   1195:                        switch(nxt) {
                   1196:                        case IPPROTO_FRAGMENT:
                   1197:                                len += sizeof(struct ip6_frag);
                   1198:                                break;
                   1199:                        case IPPROTO_AH:
                   1200:                                len += (ip6e->ip6e_len + 2) << 2;
                   1201:                                break;
                   1202:                        default:
                   1203:                                len += (ip6e->ip6e_len + 1) << 3;
                   1204:                                break;
                   1205:                        }
                   1206:                        nxt = ip6e->ip6e_nxt;
                   1207:                }
                   1208:                if (ip6e)
                   1209:                        return(&ip6e->ip6e_nxt);
                   1210:                else
                   1211:                        return NULL;
1.18      itojun   1212:        }
                   1213: }
                   1214:
                   1215: /*
                   1216:  * get next header offset.  m will be retained.
                   1217:  */
                   1218: int
                   1219: ip6_nexthdr(m, off, proto, nxtp)
                   1220:        struct mbuf *m;
                   1221:        int off;
                   1222:        int proto;
                   1223:        int *nxtp;
                   1224: {
                   1225:        struct ip6_hdr ip6;
                   1226:        struct ip6_ext ip6e;
                   1227:        struct ip6_frag fh;
                   1228:
                   1229:        /* just in case */
                   1230:        if (m == NULL)
                   1231:                panic("ip6_nexthdr: m == NULL");
                   1232:        if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
                   1233:                return -1;
                   1234:
                   1235:        switch (proto) {
                   1236:        case IPPROTO_IPV6:
                   1237:                if (m->m_pkthdr.len < off + sizeof(ip6))
                   1238:                        return -1;
                   1239:                m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
                   1240:                if (nxtp)
                   1241:                        *nxtp = ip6.ip6_nxt;
                   1242:                off += sizeof(ip6);
                   1243:                return off;
                   1244:
                   1245:        case IPPROTO_FRAGMENT:
                   1246:                /*
                   1247:                 * terminate parsing if it is not the first fragment,
                   1248:                 * it does not make sense to parse through it.
                   1249:                 */
                   1250:                if (m->m_pkthdr.len < off + sizeof(fh))
                   1251:                        return -1;
                   1252:                m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
                   1253:                if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
                   1254:                        return -1;
                   1255:                if (nxtp)
                   1256:                        *nxtp = fh.ip6f_nxt;
                   1257:                off += sizeof(struct ip6_frag);
                   1258:                return off;
                   1259:
                   1260:        case IPPROTO_AH:
                   1261:                if (m->m_pkthdr.len < off + sizeof(ip6e))
                   1262:                        return -1;
                   1263:                m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
                   1264:                if (nxtp)
                   1265:                        *nxtp = ip6e.ip6e_nxt;
                   1266:                off += (ip6e.ip6e_len + 2) << 2;
                   1267:                return off;
                   1268:
                   1269:        case IPPROTO_HOPOPTS:
                   1270:        case IPPROTO_ROUTING:
                   1271:        case IPPROTO_DSTOPTS:
                   1272:                if (m->m_pkthdr.len < off + sizeof(ip6e))
                   1273:                        return -1;
                   1274:                m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
                   1275:                if (nxtp)
                   1276:                        *nxtp = ip6e.ip6e_nxt;
                   1277:                off += (ip6e.ip6e_len + 1) << 3;
                   1278:                return off;
                   1279:
                   1280:        case IPPROTO_NONE:
                   1281:        case IPPROTO_ESP:
                   1282:        case IPPROTO_IPCOMP:
                   1283:                /* give up */
                   1284:                return -1;
                   1285:
                   1286:        default:
                   1287:                return -1;
                   1288:        }
                   1289:
                   1290:        return -1;
                   1291: }
                   1292:
                   1293: /*
                   1294:  * get offset for the last header in the chain.  m will be kept untainted.
                   1295:  */
                   1296: int
                   1297: ip6_lasthdr(m, off, proto, nxtp)
                   1298:        struct mbuf *m;
                   1299:        int off;
                   1300:        int proto;
                   1301:        int *nxtp;
                   1302: {
                   1303:        int newoff;
                   1304:        int nxt;
                   1305:
                   1306:        if (!nxtp) {
                   1307:                nxt = -1;
                   1308:                nxtp = &nxt;
                   1309:        }
                   1310:        while (1) {
                   1311:                newoff = ip6_nexthdr(m, off, proto, nxtp);
                   1312:                if (newoff < 0)
                   1313:                        return off;
                   1314:                else if (newoff < off)
                   1315:                        return -1;      /* invalid */
                   1316:                else if (newoff == off)
                   1317:                        return newoff;
                   1318:
                   1319:                off = newoff;
                   1320:                proto = *nxtp;
1.2       itojun   1321:        }
                   1322: }
                   1323:
                   1324: /*
                   1325:  * System control for IP6
                   1326:  */
                   1327:
                   1328: u_char inet6ctlerrmap[PRC_NCMDS] = {
                   1329:        0,              0,              0,              0,
                   1330:        0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
                   1331:        EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
                   1332:        EMSGSIZE,       EHOSTUNREACH,   0,              0,
                   1333:        0,              0,              0,              0,
                   1334:        ENOPROTOOPT
                   1335: };
                   1336:
                   1337: #include <vm/vm.h>
                   1338: #include <sys/sysctl.h>
                   1339:
                   1340: int
                   1341: ip6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
                   1342:        int *name;
                   1343:        u_int namelen;
                   1344:        void *oldp;
                   1345:        size_t *oldlenp;
                   1346:        void *newp;
                   1347:        size_t newlen;
                   1348: {
                   1349:        /* All sysctl names at this level are terminal. */
                   1350:        if (namelen != 1)
                   1351:                return ENOTDIR;
                   1352:
                   1353:        switch (name[0]) {
                   1354:
                   1355:        case IPV6CTL_FORWARDING:
                   1356:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1357:                                  &ip6_forwarding);
                   1358:        case IPV6CTL_SENDREDIRECTS:
                   1359:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1360:                                &ip6_sendredirects);
                   1361:        case IPV6CTL_DEFHLIM:
                   1362:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_defhlim);
                   1363:        case IPV6CTL_MAXFRAGPACKETS:
                   1364:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1365:                                &ip6_maxfragpackets);
                   1366:        case IPV6CTL_ACCEPT_RTADV:
                   1367:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1368:                                &ip6_accept_rtadv);
                   1369:        case IPV6CTL_KEEPFAITH:
                   1370:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_keepfaith);
                   1371:        case IPV6CTL_LOG_INTERVAL:
                   1372:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1373:                                &ip6_log_interval);
                   1374:        case IPV6CTL_HDRNESTLIMIT:
                   1375:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1376:                                &ip6_hdrnestlimit);
                   1377:        case IPV6CTL_DAD_COUNT:
                   1378:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_dad_count);
                   1379:        case IPV6CTL_AUTO_FLOWLABEL:
                   1380:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1381:                                &ip6_auto_flowlabel);
                   1382:        case IPV6CTL_DEFMCASTHLIM:
                   1383:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1384:                                &ip6_defmcasthlim);
                   1385:        case IPV6CTL_GIF_HLIM:
                   1386:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1387:                                &ip6_gif_hlim);
                   1388:        case IPV6CTL_KAME_VERSION:
                   1389:                return sysctl_rdstring(oldp, oldlenp, newp, __KAME_VERSION);
1.9       itojun   1390:        case IPV6CTL_USE_DEPRECATED:
                   1391:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1392:                                &ip6_use_deprecated);
1.10      itojun   1393:        case IPV6CTL_RR_PRUNE:
                   1394:                return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_rr_prune);
1.12      itojun   1395: #ifndef INET6_BINDV6ONLY
1.11      itojun   1396:        case IPV6CTL_BINDV6ONLY:
                   1397:                return sysctl_int(oldp, oldlenp, newp, newlen,
                   1398:                                &ip6_bindv6only);
                   1399: #endif
1.2       itojun   1400:        default:
                   1401:                return EOPNOTSUPP;
                   1402:        }
                   1403:        /* NOTREACHED */
                   1404: }

CVSweb <webmaster@jp.NetBSD.org>