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