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

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

CVSweb <webmaster@jp.NetBSD.org>