ip6_input.c revision 121472
1/* $FreeBSD: head/sys/netinet6/ip6_input.c 121472 2003-10-24 18:26:30Z ume $ */ 2/* $KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei 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. 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_ip6fw.h" 69#include "opt_inet.h" 70#include "opt_inet6.h" 71#include "opt_ipsec.h" 72#include "opt_pfil_hooks.h" 73#include "opt_random_ip_id.h" 74 75#include <sys/param.h> 76#include <sys/systm.h> 77#include <sys/malloc.h> 78#include <sys/mbuf.h> 79#include <sys/proc.h> 80#include <sys/domain.h> 81#include <sys/protosw.h> 82#include <sys/socket.h> 83#include <sys/socketvar.h> 84#include <sys/errno.h> 85#include <sys/time.h> 86#include <sys/kernel.h> 87#include <sys/syslog.h> 88 89#include <net/if.h> 90#include <net/if_types.h> 91#include <net/if_dl.h> 92#include <net/route.h> 93#include <net/netisr.h> 94#ifdef PFIL_HOOKS 95#include <net/pfil.h> 96#endif 97 98#include <netinet/in.h> 99#include <netinet/in_systm.h> 100#ifdef INET 101#include <netinet/ip.h> 102#include <netinet/ip_icmp.h> 103#endif /* INET */ 104#include <netinet/ip6.h> 105#include <netinet6/in6_var.h> 106#include <netinet6/ip6_var.h> 107#include <netinet/in_pcb.h> 108#include <netinet/icmp6.h> 109#include <netinet6/scope6_var.h> 110#include <netinet6/in6_ifattach.h> 111#include <netinet6/nd6.h> 112#include <netinet6/in6_prefix.h> 113 114#ifdef IPSEC 115#include <netinet6/ipsec.h> 116#ifdef INET6 117#include <netinet6/ipsec6.h> 118#endif 119#endif 120 121#ifdef FAST_IPSEC 122#include <netipsec/ipsec.h> 123#include <netipsec/ipsec6.h> 124#define IPSEC 125#endif /* FAST_IPSEC */ 126 127#include <netinet6/ip6_fw.h> 128 129#include <netinet6/ip6protosw.h> 130 131#include <net/net_osdep.h> 132 133extern struct domain inet6domain; 134 135u_char ip6_protox[IPPROTO_MAX]; 136static struct ifqueue ip6intrq; 137static int ip6qmaxlen = IFQ_MAXLEN; 138struct in6_ifaddr *in6_ifaddr; 139 140extern struct callout in6_tmpaddrtimer_ch; 141 142int ip6_forward_srcrt; /* XXX */ 143int ip6_sourcecheck; /* XXX */ 144int ip6_sourcecheck_interval; /* XXX */ 145 146int ip6_ours_check_algorithm; 147 148#ifdef PFIL_HOOKS 149struct pfil_head inet6_pfil_hook; 150#endif 151 152/* firewall hooks */ 153ip6_fw_chk_t *ip6_fw_chk_ptr; 154ip6_fw_ctl_t *ip6_fw_ctl_ptr; 155int ip6_fw_enable = 1; 156 157struct ip6stat ip6stat; 158 159static void ip6_init2 __P((void *)); 160static struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *)); 161static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *)); 162#ifdef PULLDOWN_TEST 163static struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int)); 164#endif 165 166/* 167 * IP6 initialization: fill in IP6 protocol switch table. 168 * All protocols not implemented in kernel go to raw IP6 protocol handler. 169 */ 170void 171ip6_init() 172{ 173 struct ip6protosw *pr; 174 int i; 175 176#ifdef DIAGNOSTIC 177 if (sizeof(struct protosw) != sizeof(struct ip6protosw)) 178 panic("sizeof(protosw) != sizeof(ip6protosw)"); 179#endif 180 pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 181 if (pr == 0) 182 panic("ip6_init"); 183 for (i = 0; i < IPPROTO_MAX; i++) 184 ip6_protox[i] = pr - inet6sw; 185 for (pr = (struct ip6protosw *)inet6domain.dom_protosw; 186 pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++) 187 if (pr->pr_domain->dom_family == PF_INET6 && 188 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 189 ip6_protox[pr->pr_protocol] = pr - inet6sw; 190#ifdef PFIL_HOOKS 191 inet6_pfil_hook.ph_type = PFIL_TYPE_AF; 192 inet6_pfil_hook.ph_af = AF_INET6; 193 if ((i = pfil_head_register(&inet6_pfil_hook)) != 0) 194 printf("%s: WARNING: unable to register pfil hook, " 195 "error %d\n", __func__, i); 196#endif /* PFIL_HOOKS */ 197 ip6intrq.ifq_maxlen = ip6qmaxlen; 198 mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF); 199 netisr_register(NETISR_IPV6, ip6_input, &ip6intrq); 200 scope6_init(); 201 nd6_init(); 202 frag6_init(); 203#ifndef RANDOM_IP_ID 204 ip6_flow_seq = arc4random(); 205#endif 206 ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR; 207} 208 209static void 210ip6_init2(dummy) 211 void *dummy; 212{ 213 214 /* nd6_timer_init */ 215 callout_init(&nd6_timer_ch, 0); 216 callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL); 217 218 /* router renumbering prefix list maintenance */ 219 callout_init(&in6_rr_timer_ch, 0); 220 callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL); 221 222 /* timer for regeneranation of temporary addresses randomize ID */ 223 callout_reset(&in6_tmpaddrtimer_ch, 224 (ip6_temp_preferred_lifetime - ip6_desync_factor - 225 ip6_temp_regen_advance) * hz, 226 in6_tmpaddrtimer, NULL); 227} 228 229/* cheat */ 230/* This must be after route_init(), which is now SI_ORDER_THIRD */ 231SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL); 232 233extern struct route_in6 ip6_forward_rt; 234 235void 236ip6_input(m) 237 struct mbuf *m; 238{ 239 struct ip6_hdr *ip6; 240 int off = sizeof(struct ip6_hdr), nest; 241 u_int32_t plen; 242 u_int32_t rtalert = ~0; 243 int nxt, ours = 0; 244 struct ifnet *deliverifp = NULL; 245#ifdef PFIL_HOOKS 246 struct in6_addr odst; 247#endif 248 int srcrt = 0; 249 250#ifdef IPSEC 251 /* 252 * should the inner packet be considered authentic? 253 * see comment in ah4_input(). 254 */ 255 if (m) { 256 m->m_flags &= ~M_AUTHIPHDR; 257 m->m_flags &= ~M_AUTHIPDGM; 258 } 259#endif 260 261 /* 262 * make sure we don't have onion peering information into m_aux. 263 */ 264 ip6_delaux(m); 265 266 /* 267 * mbuf statistics 268 */ 269 if (m->m_flags & M_EXT) { 270 if (m->m_next) 271 ip6stat.ip6s_mext2m++; 272 else 273 ip6stat.ip6s_mext1++; 274 } else { 275#define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0])) 276 if (m->m_next) { 277 if (m->m_flags & M_LOOP) { 278 ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */ 279 } else if (m->m_pkthdr.rcvif->if_index < M2MMAX) 280 ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++; 281 else 282 ip6stat.ip6s_m2m[0]++; 283 } else 284 ip6stat.ip6s_m1++; 285#undef M2MMAX 286 } 287 288 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive); 289 ip6stat.ip6s_total++; 290 291#ifndef PULLDOWN_TEST 292 /* 293 * L2 bridge code and some other code can return mbuf chain 294 * that does not conform to KAME requirement. too bad. 295 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 296 */ 297 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 298 struct mbuf *n; 299 300 MGETHDR(n, M_DONTWAIT, MT_HEADER); 301 if (n) 302 M_MOVE_PKTHDR(n, m); 303 if (n && n->m_pkthdr.len > MHLEN) { 304 MCLGET(n, M_DONTWAIT); 305 if ((n->m_flags & M_EXT) == 0) { 306 m_freem(n); 307 n = NULL; 308 } 309 } 310 if (n == NULL) { 311 m_freem(m); 312 return; /* ENOBUFS */ 313 } 314 315 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t)); 316 n->m_len = n->m_pkthdr.len; 317 m_freem(m); 318 m = n; 319 } 320 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */); 321#endif 322 323 if (m->m_len < sizeof(struct ip6_hdr)) { 324 struct ifnet *inifp; 325 inifp = m->m_pkthdr.rcvif; 326 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 327 ip6stat.ip6s_toosmall++; 328 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 329 return; 330 } 331 } 332 333 ip6 = mtod(m, struct ip6_hdr *); 334 335 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 336 ip6stat.ip6s_badvers++; 337 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 338 goto bad; 339 } 340 341#ifdef PFIL_HOOKS 342 /* 343 * Run through list of hooks for input packets. 344 * 345 * NB: Beware of the destination address changing 346 * (e.g. by NAT rewriting). When this happens, 347 * tell ip6_forward to do the right thing. 348 */ 349 odst = ip6->ip6_dst; 350 if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN)) 351 return; 352 if (m == NULL) /* consumed by filter */ 353 return; 354 ip6 = mtod(m, struct ip6_hdr *); 355 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst); 356#endif /* PFIL_HOOKS */ 357 358 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; 359 360 /* 361 * Check with the firewall... 362 */ 363 if (ip6_fw_enable && ip6_fw_chk_ptr) { 364 u_short port = 0; 365 /* If ipfw says divert, we have to just drop packet */ 366 /* use port as a dummy argument */ 367 if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) { 368 m_freem(m); 369 m = NULL; 370 } 371 if (!m) 372 return; 373 } 374 375 /* 376 * Check against address spoofing/corruption. 377 */ 378 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 379 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 380 /* 381 * XXX: "badscope" is not very suitable for a multicast source. 382 */ 383 ip6stat.ip6s_badscope++; 384 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 385 goto bad; 386 } 387 if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) || 388 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) && 389 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 390 ip6stat.ip6s_badscope++; 391 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 392 goto bad; 393 } 394 395 /* 396 * The following check is not documented in specs. A malicious 397 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack 398 * and bypass security checks (act as if it was from 127.0.0.1 by using 399 * IPv6 src ::ffff:127.0.0.1). Be cautious. 400 * 401 * This check chokes if we are in an SIIT cloud. As none of BSDs 402 * support IPv4-less kernel compilation, we cannot support SIIT 403 * environment at all. So, it makes more sense for us to reject any 404 * malicious packets for non-SIIT environment, than try to do a 405 * partial support for SIIT environment. 406 */ 407 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 408 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 409 ip6stat.ip6s_badscope++; 410 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 411 goto bad; 412 } 413#if 0 414 /* 415 * Reject packets with IPv4 compatible addresses (auto tunnel). 416 * 417 * The code forbids auto tunnel relay case in RFC1933 (the check is 418 * stronger than RFC1933). We may want to re-enable it if mech-xx 419 * is revised to forbid relaying case. 420 */ 421 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) || 422 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) { 423 ip6stat.ip6s_badscope++; 424 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 425 goto bad; 426 } 427#endif 428 429 /* drop packets if interface ID portion is already filled */ 430 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 431 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) && 432 ip6->ip6_src.s6_addr16[1]) { 433 ip6stat.ip6s_badscope++; 434 goto bad; 435 } 436 if ((IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) || 437 IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) && 438 ip6->ip6_dst.s6_addr16[1]) { 439 ip6stat.ip6s_badscope++; 440 goto bad; 441 } 442 } 443 444 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 445 ip6->ip6_src.s6_addr16[1] 446 = htons(m->m_pkthdr.rcvif->if_index); 447 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 448 ip6->ip6_dst.s6_addr16[1] 449 = htons(m->m_pkthdr.rcvif->if_index); 450 451 /* 452 * Multicast check 453 */ 454 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 455 struct in6_multi *in6m = 0; 456 457 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast); 458 /* 459 * See if we belong to the destination multicast group on the 460 * arrival interface. 461 */ 462 IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m); 463 if (in6m) 464 ours = 1; 465 else if (!ip6_mrouter) { 466 ip6stat.ip6s_notmember++; 467 ip6stat.ip6s_cantforward++; 468 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 469 goto bad; 470 } 471 deliverifp = m->m_pkthdr.rcvif; 472 goto hbhcheck; 473 } 474 475 /* 476 * Unicast check 477 */ 478 if (ip6_forward_rt.ro_rt != NULL && 479 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 && 480 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 481 &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr)) 482 ip6stat.ip6s_forward_cachehit++; 483 else { 484 struct sockaddr_in6 *dst6; 485 486 if (ip6_forward_rt.ro_rt) { 487 /* route is down or destination is different */ 488 ip6stat.ip6s_forward_cachemiss++; 489 RTFREE(ip6_forward_rt.ro_rt); 490 ip6_forward_rt.ro_rt = 0; 491 } 492 493 bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6)); 494 dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst; 495 dst6->sin6_len = sizeof(struct sockaddr_in6); 496 dst6->sin6_family = AF_INET6; 497 dst6->sin6_addr = ip6->ip6_dst; 498 499 rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING); 500 } 501 502#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key)) 503 504 /* 505 * Accept the packet if the forwarding interface to the destination 506 * according to the routing table is the loopback interface, 507 * unless the associated route has a gateway. 508 * Note that this approach causes to accept a packet if there is a 509 * route to the loopback interface for the destination of the packet. 510 * But we think it's even useful in some situations, e.g. when using 511 * a special daemon which wants to intercept the packet. 512 * 513 * XXX: some OSes automatically make a cloned route for the destination 514 * of an outgoing packet. If the outgoing interface of the packet 515 * is a loopback one, the kernel would consider the packet to be 516 * accepted, even if we have no such address assinged on the interface. 517 * We check the cloned flag of the route entry to reject such cases, 518 * assuming that route entries for our own addresses are not made by 519 * cloning (it should be true because in6_addloop explicitly installs 520 * the host route). However, we might have to do an explicit check 521 * while it would be less efficient. Or, should we rather install a 522 * reject route for such a case? 523 */ 524 if (ip6_forward_rt.ro_rt && 525 (ip6_forward_rt.ro_rt->rt_flags & 526 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && 527#ifdef RTF_WASCLONED 528 !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) && 529#endif 530#ifdef RTF_CLONED 531 !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) && 532#endif 533#if 0 534 /* 535 * The check below is redundant since the comparison of 536 * the destination and the key of the rtentry has 537 * already done through looking up the routing table. 538 */ 539 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 540 &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) 541#endif 542 ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) { 543 struct in6_ifaddr *ia6 = 544 (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa; 545 546 /* 547 * record address information into m_aux. 548 */ 549 (void)ip6_setdstifaddr(m, ia6); 550 551 /* 552 * packets to a tentative, duplicated, or somehow invalid 553 * address must not be accepted. 554 */ 555 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 556 /* this address is ready */ 557 ours = 1; 558 deliverifp = ia6->ia_ifp; /* correct? */ 559 /* Count the packet in the ip address stats */ 560 ia6->ia_ifa.if_ipackets++; 561 ia6->ia_ifa.if_ibytes += m->m_pkthdr.len; 562 goto hbhcheck; 563 } else { 564 /* address is not ready, so discard the packet. */ 565 nd6log((LOG_INFO, 566 "ip6_input: packet to an unready address %s->%s\n", 567 ip6_sprintf(&ip6->ip6_src), 568 ip6_sprintf(&ip6->ip6_dst))); 569 570 goto bad; 571 } 572 } 573 574 /* 575 * FAITH (Firewall Aided Internet Translator) 576 */ 577 if (ip6_keepfaith) { 578 if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp 579 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) { 580 /* XXX do we need more sanity checks? */ 581 ours = 1; 582 deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */ 583 goto hbhcheck; 584 } 585 } 586 587 /* 588 * Now there is no reason to process the packet if it's not our own 589 * and we're not a router. 590 */ 591 if (!ip6_forwarding) { 592 ip6stat.ip6s_cantforward++; 593 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 594 goto bad; 595 } 596 597 hbhcheck: 598 /* 599 * record address information into m_aux, if we don't have one yet. 600 * note that we are unable to record it, if the address is not listed 601 * as our interface address (e.g. multicast addresses, addresses 602 * within FAITH prefixes and such). 603 */ 604 if (deliverifp && !ip6_getdstifaddr(m)) { 605 struct in6_ifaddr *ia6; 606 607 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); 608 if (ia6) { 609 if (!ip6_setdstifaddr(m, ia6)) { 610 /* 611 * XXX maybe we should drop the packet here, 612 * as we could not provide enough information 613 * to the upper layers. 614 */ 615 } 616 } 617 } 618 619 /* 620 * Process Hop-by-Hop options header if it's contained. 621 * m may be modified in ip6_hopopts_input(). 622 * If a JumboPayload option is included, plen will also be modified. 623 */ 624 plen = (u_int32_t)ntohs(ip6->ip6_plen); 625 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 626 struct ip6_hbh *hbh; 627 628 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) { 629#if 0 /*touches NULL pointer*/ 630 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 631#endif 632 return; /* m have already been freed */ 633 } 634 635 /* adjust pointer */ 636 ip6 = mtod(m, struct ip6_hdr *); 637 638 /* 639 * if the payload length field is 0 and the next header field 640 * indicates Hop-by-Hop Options header, then a Jumbo Payload 641 * option MUST be included. 642 */ 643 if (ip6->ip6_plen == 0 && plen == 0) { 644 /* 645 * Note that if a valid jumbo payload option is 646 * contained, ip6_hopopts_input() must set a valid 647 * (non-zero) payload length to the variable plen. 648 */ 649 ip6stat.ip6s_badoptions++; 650 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 651 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 652 icmp6_error(m, ICMP6_PARAM_PROB, 653 ICMP6_PARAMPROB_HEADER, 654 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); 655 return; 656 } 657#ifndef PULLDOWN_TEST 658 /* ip6_hopopts_input() ensures that mbuf is contiguous */ 659 hbh = (struct ip6_hbh *)(ip6 + 1); 660#else 661 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 662 sizeof(struct ip6_hbh)); 663 if (hbh == NULL) { 664 ip6stat.ip6s_tooshort++; 665 return; 666 } 667#endif 668 nxt = hbh->ip6h_nxt; 669 670 /* 671 * accept the packet if a router alert option is included 672 * and we act as an IPv6 router. 673 */ 674 if (rtalert != ~0 && ip6_forwarding) 675 ours = 1; 676 } else 677 nxt = ip6->ip6_nxt; 678 679 /* 680 * Check that the amount of data in the buffers 681 * is as at least much as the IPv6 header would have us expect. 682 * Trim mbufs if longer than we expect. 683 * Drop packet if shorter than we expect. 684 */ 685 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 686 ip6stat.ip6s_tooshort++; 687 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 688 goto bad; 689 } 690 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 691 if (m->m_len == m->m_pkthdr.len) { 692 m->m_len = sizeof(struct ip6_hdr) + plen; 693 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 694 } else 695 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 696 } 697 698 /* 699 * Forward if desirable. 700 */ 701 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 702 /* 703 * If we are acting as a multicast router, all 704 * incoming multicast packets are passed to the 705 * kernel-level multicast forwarding function. 706 * The packet is returned (relatively) intact; if 707 * ip6_mforward() returns a non-zero value, the packet 708 * must be discarded, else it may be accepted below. 709 */ 710 if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) { 711 ip6stat.ip6s_cantforward++; 712 m_freem(m); 713 return; 714 } 715 if (!ours) { 716 m_freem(m); 717 return; 718 } 719 } else if (!ours) { 720 ip6_forward(m, srcrt); 721 return; 722 } 723 724 ip6 = mtod(m, struct ip6_hdr *); 725 726 /* 727 * Malicious party may be able to use IPv4 mapped addr to confuse 728 * tcp/udp stack and bypass security checks (act as if it was from 729 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 730 * 731 * For SIIT end node behavior, you may want to disable the check. 732 * However, you will become vulnerable to attacks using IPv4 mapped 733 * source. 734 */ 735 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 736 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 737 ip6stat.ip6s_badscope++; 738 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 739 goto bad; 740 } 741 742 /* 743 * Tell launch routine the next header 744 */ 745 ip6stat.ip6s_delivered++; 746 in6_ifstat_inc(deliverifp, ifs6_in_deliver); 747 nest = 0; 748 749 while (nxt != IPPROTO_DONE) { 750 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { 751 ip6stat.ip6s_toomanyhdr++; 752 goto bad; 753 } 754 755 /* 756 * protection against faulty packet - there should be 757 * more sanity checks in header chain processing. 758 */ 759 if (m->m_pkthdr.len < off) { 760 ip6stat.ip6s_tooshort++; 761 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 762 goto bad; 763 } 764 765#if 0 766 /* 767 * do we need to do it for every header? yeah, other 768 * functions can play with it (like re-allocate and copy). 769 */ 770 mhist = ip6_addaux(m); 771 if (mhist && M_TRAILINGSPACE(mhist) >= sizeof(nxt)) { 772 hist = mtod(mhist, caddr_t) + mhist->m_len; 773 bcopy(&nxt, hist, sizeof(nxt)); 774 mhist->m_len += sizeof(nxt); 775 } else { 776 ip6stat.ip6s_toomanyhdr++; 777 goto bad; 778 } 779#endif 780 781#ifdef IPSEC 782 /* 783 * enforce IPsec policy checking if we are seeing last header. 784 * note that we do not visit this with protocols with pcb layer 785 * code - like udp/tcp/raw ip. 786 */ 787 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 && 788 ipsec6_in_reject(m, NULL)) { 789 ipsec6stat.in_polvio++; 790 goto bad; 791 } 792#endif 793 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt); 794 } 795 return; 796 bad: 797 m_freem(m); 798} 799 800/* 801 * set/grab in6_ifaddr correspond to IPv6 destination address. 802 * XXX backward compatibility wrapper 803 */ 804static struct ip6aux * 805ip6_setdstifaddr(m, ia6) 806 struct mbuf *m; 807 struct in6_ifaddr *ia6; 808{ 809 struct ip6aux *n; 810 811 n = ip6_addaux(m); 812 if (n) 813 n->ip6a_dstia6 = ia6; 814 return n; /* NULL if failed to set */ 815} 816 817struct in6_ifaddr * 818ip6_getdstifaddr(m) 819 struct mbuf *m; 820{ 821 struct ip6aux *n; 822 823 n = ip6_findaux(m); 824 if (n) 825 return n->ip6a_dstia6; 826 else 827 return NULL; 828} 829 830/* 831 * Hop-by-Hop options header processing. If a valid jumbo payload option is 832 * included, the real payload length will be stored in plenp. 833 */ 834static int 835ip6_hopopts_input(plenp, rtalertp, mp, offp) 836 u_int32_t *plenp; 837 u_int32_t *rtalertp; /* XXX: should be stored more smart way */ 838 struct mbuf **mp; 839 int *offp; 840{ 841 struct mbuf *m = *mp; 842 int off = *offp, hbhlen; 843 struct ip6_hbh *hbh; 844 u_int8_t *opt; 845 846 /* validation of the length of the header */ 847#ifndef PULLDOWN_TEST 848 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1); 849 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 850 hbhlen = (hbh->ip6h_len + 1) << 3; 851 852 IP6_EXTHDR_CHECK(m, off, hbhlen, -1); 853 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 854#else 855 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, 856 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 857 if (hbh == NULL) { 858 ip6stat.ip6s_tooshort++; 859 return -1; 860 } 861 hbhlen = (hbh->ip6h_len + 1) << 3; 862 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 863 hbhlen); 864 if (hbh == NULL) { 865 ip6stat.ip6s_tooshort++; 866 return -1; 867 } 868#endif 869 off += hbhlen; 870 hbhlen -= sizeof(struct ip6_hbh); 871 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh); 872 873 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 874 hbhlen, rtalertp, plenp) < 0) 875 return (-1); 876 877 *offp = off; 878 *mp = m; 879 return (0); 880} 881 882/* 883 * Search header for all Hop-by-hop options and process each option. 884 * This function is separate from ip6_hopopts_input() in order to 885 * handle a case where the sending node itself process its hop-by-hop 886 * options header. In such a case, the function is called from ip6_output(). 887 * 888 * The function assumes that hbh header is located right after the IPv6 header 889 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 890 * opthead + hbhlen is located in continuous memory region. 891 */ 892int 893ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp) 894 struct mbuf *m; 895 u_int8_t *opthead; 896 int hbhlen; 897 u_int32_t *rtalertp; 898 u_int32_t *plenp; 899{ 900 struct ip6_hdr *ip6; 901 int optlen = 0; 902 u_int8_t *opt = opthead; 903 u_int16_t rtalert_val; 904 u_int32_t jumboplen; 905 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 906 907 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 908 switch (*opt) { 909 case IP6OPT_PAD1: 910 optlen = 1; 911 break; 912 case IP6OPT_PADN: 913 if (hbhlen < IP6OPT_MINLEN) { 914 ip6stat.ip6s_toosmall++; 915 goto bad; 916 } 917 optlen = *(opt + 1) + 2; 918 break; 919 case IP6OPT_ROUTER_ALERT: 920 /* XXX may need check for alignment */ 921 if (hbhlen < IP6OPT_RTALERT_LEN) { 922 ip6stat.ip6s_toosmall++; 923 goto bad; 924 } 925 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 926 /* XXX stat */ 927 icmp6_error(m, ICMP6_PARAM_PROB, 928 ICMP6_PARAMPROB_HEADER, 929 erroff + opt + 1 - opthead); 930 return (-1); 931 } 932 optlen = IP6OPT_RTALERT_LEN; 933 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 934 *rtalertp = ntohs(rtalert_val); 935 break; 936 case IP6OPT_JUMBO: 937 /* XXX may need check for alignment */ 938 if (hbhlen < IP6OPT_JUMBO_LEN) { 939 ip6stat.ip6s_toosmall++; 940 goto bad; 941 } 942 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 943 /* XXX stat */ 944 icmp6_error(m, ICMP6_PARAM_PROB, 945 ICMP6_PARAMPROB_HEADER, 946 erroff + opt + 1 - opthead); 947 return (-1); 948 } 949 optlen = IP6OPT_JUMBO_LEN; 950 951 /* 952 * IPv6 packets that have non 0 payload length 953 * must not contain a jumbo payload option. 954 */ 955 ip6 = mtod(m, struct ip6_hdr *); 956 if (ip6->ip6_plen) { 957 ip6stat.ip6s_badoptions++; 958 icmp6_error(m, ICMP6_PARAM_PROB, 959 ICMP6_PARAMPROB_HEADER, 960 erroff + opt - opthead); 961 return (-1); 962 } 963 964 /* 965 * We may see jumbolen in unaligned location, so 966 * we'd need to perform bcopy(). 967 */ 968 bcopy(opt + 2, &jumboplen, sizeof(jumboplen)); 969 jumboplen = (u_int32_t)htonl(jumboplen); 970 971#if 1 972 /* 973 * if there are multiple jumbo payload options, 974 * *plenp will be non-zero and the packet will be 975 * rejected. 976 * the behavior may need some debate in ipngwg - 977 * multiple options does not make sense, however, 978 * there's no explicit mention in specification. 979 */ 980 if (*plenp != 0) { 981 ip6stat.ip6s_badoptions++; 982 icmp6_error(m, ICMP6_PARAM_PROB, 983 ICMP6_PARAMPROB_HEADER, 984 erroff + opt + 2 - opthead); 985 return (-1); 986 } 987#endif 988 989 /* 990 * jumbo payload length must be larger than 65535. 991 */ 992 if (jumboplen <= IPV6_MAXPACKET) { 993 ip6stat.ip6s_badoptions++; 994 icmp6_error(m, ICMP6_PARAM_PROB, 995 ICMP6_PARAMPROB_HEADER, 996 erroff + opt + 2 - opthead); 997 return (-1); 998 } 999 *plenp = jumboplen; 1000 1001 break; 1002 default: /* unknown option */ 1003 if (hbhlen < IP6OPT_MINLEN) { 1004 ip6stat.ip6s_toosmall++; 1005 goto bad; 1006 } 1007 optlen = ip6_unknown_opt(opt, m, 1008 erroff + opt - opthead); 1009 if (optlen == -1) 1010 return (-1); 1011 optlen += 2; 1012 break; 1013 } 1014 } 1015 1016 return (0); 1017 1018 bad: 1019 m_freem(m); 1020 return (-1); 1021} 1022 1023/* 1024 * Unknown option processing. 1025 * The third argument `off' is the offset from the IPv6 header to the option, 1026 * which is necessary if the IPv6 header the and option header and IPv6 header 1027 * is not continuous in order to return an ICMPv6 error. 1028 */ 1029int 1030ip6_unknown_opt(optp, m, off) 1031 u_int8_t *optp; 1032 struct mbuf *m; 1033 int off; 1034{ 1035 struct ip6_hdr *ip6; 1036 1037 switch (IP6OPT_TYPE(*optp)) { 1038 case IP6OPT_TYPE_SKIP: /* ignore the option */ 1039 return ((int)*(optp + 1)); 1040 case IP6OPT_TYPE_DISCARD: /* silently discard */ 1041 m_freem(m); 1042 return (-1); 1043 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 1044 ip6stat.ip6s_badoptions++; 1045 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 1046 return (-1); 1047 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 1048 ip6stat.ip6s_badoptions++; 1049 ip6 = mtod(m, struct ip6_hdr *); 1050 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1051 (m->m_flags & (M_BCAST|M_MCAST))) 1052 m_freem(m); 1053 else 1054 icmp6_error(m, ICMP6_PARAM_PROB, 1055 ICMP6_PARAMPROB_OPTION, off); 1056 return (-1); 1057 } 1058 1059 m_freem(m); /* XXX: NOTREACHED */ 1060 return (-1); 1061} 1062 1063/* 1064 * Create the "control" list for this pcb. 1065 * The function will not modify mbuf chain at all. 1066 * 1067 * with KAME mbuf chain restriction: 1068 * The routine will be called from upper layer handlers like tcp6_input(). 1069 * Thus the routine assumes that the caller (tcp6_input) have already 1070 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 1071 * very first mbuf on the mbuf chain. 1072 */ 1073void 1074ip6_savecontrol(in6p, mp, ip6, m) 1075 struct inpcb *in6p; 1076 struct mbuf **mp; 1077 struct ip6_hdr *ip6; 1078 struct mbuf *m; 1079{ 1080#define IS2292(x, y) ((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y)) 1081 struct thread *td = curthread; /* XXX */ 1082 int privileged = 0; 1083 1084 if (td && !suser(td)) 1085 privileged++; 1086 1087#ifdef SO_TIMESTAMP 1088 if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) { 1089 struct timeval tv; 1090 1091 microtime(&tv); 1092 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1093 SCM_TIMESTAMP, SOL_SOCKET); 1094 if (*mp) 1095 mp = &(*mp)->m_next; 1096 } 1097#endif 1098 1099 /* RFC 2292 sec. 5 */ 1100 if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) { 1101 struct in6_pktinfo pi6; 1102 1103 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 1104 in6_clearscope(&pi6.ipi6_addr); /* XXX */ 1105 pi6.ipi6_ifindex = 1106 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0; 1107 1108 *mp = sbcreatecontrol((caddr_t) &pi6, 1109 sizeof(struct in6_pktinfo), 1110 IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6); 1111 if (*mp) 1112 mp = &(*mp)->m_next; 1113 } 1114 1115 if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) { 1116 int hlim = ip6->ip6_hlim & 0xff; 1117 1118 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int), 1119 IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6); 1120 if (*mp) 1121 mp = &(*mp)->m_next; 1122 } 1123 1124 if ((in6p->in6p_flags & IN6P_TCLASS) != 0) { 1125 u_int32_t flowinfo; 1126 int tclass; 1127 1128 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK); 1129 flowinfo >>= 20; 1130 1131 tclass = flowinfo & 0xff; 1132 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass), 1133 IPV6_TCLASS, IPPROTO_IPV6); 1134 if (*mp) 1135 mp = &(*mp)->m_next; 1136 } 1137 1138 /* 1139 * IPV6_HOPOPTS socket option. We require super-user privilege 1140 * for the option, but it might be too strict, since there might 1141 * be some hop-by-hop options which can be returned to normal user. 1142 * See RFC 2292 section 6. 1143 */ 1144 if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) { 1145#ifdef DIAGNOSTIC 1146 if (!privileged) 1147 panic("IN6P_HOPOPTS is set for unprivileged socket"); 1148#endif 1149 /* 1150 * Check if a hop-by-hop options header is contatined in the 1151 * received packet, and if so, store the options as ancillary 1152 * data. Note that a hop-by-hop options header must be 1153 * just after the IPv6 header, which is assured through the 1154 * IPv6 input processing. 1155 */ 1156 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 1157 struct ip6_hbh *hbh; 1158 int hbhlen = 0; 1159#ifdef PULLDOWN_TEST 1160 struct mbuf *ext; 1161#endif 1162 1163#ifndef PULLDOWN_TEST 1164 hbh = (struct ip6_hbh *)(ip6 + 1); 1165 hbhlen = (hbh->ip6h_len + 1) << 3; 1166#else 1167 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 1168 ip6->ip6_nxt); 1169 if (ext == NULL) { 1170 ip6stat.ip6s_tooshort++; 1171 return; 1172 } 1173 hbh = mtod(ext, struct ip6_hbh *); 1174 hbhlen = (hbh->ip6h_len + 1) << 3; 1175 if (hbhlen != ext->m_len) { 1176 m_freem(ext); 1177 ip6stat.ip6s_tooshort++; 1178 return; 1179 } 1180#endif 1181 1182 /* 1183 * XXX: We copy the whole header even if a 1184 * jumbo payload option is included, the option which 1185 * is to be removed before returning according to 1186 * RFC2292. 1187 * Note: this constraint is removed in 2292bis. 1188 */ 1189 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 1190 IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS), 1191 IPPROTO_IPV6); 1192 if (*mp) 1193 mp = &(*mp)->m_next; 1194#ifdef PULLDOWN_TEST 1195 m_freem(ext); 1196#endif 1197 } 1198 } 1199 1200 if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) { 1201 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 1202 1203 /* 1204 * Search for destination options headers or routing 1205 * header(s) through the header chain, and stores each 1206 * header as ancillary data. 1207 * Note that the order of the headers remains in 1208 * the chain of ancillary data. 1209 */ 1210 while (1) { /* is explicit loop prevention necessary? */ 1211 struct ip6_ext *ip6e = NULL; 1212 int elen; 1213#ifdef PULLDOWN_TEST 1214 struct mbuf *ext = NULL; 1215#endif 1216 1217 /* 1218 * if it is not an extension header, don't try to 1219 * pull it from the chain. 1220 */ 1221 switch (nxt) { 1222 case IPPROTO_DSTOPTS: 1223 case IPPROTO_ROUTING: 1224 case IPPROTO_HOPOPTS: 1225 case IPPROTO_AH: /* is it possible? */ 1226 break; 1227 default: 1228 goto loopend; 1229 } 1230 1231#ifndef PULLDOWN_TEST 1232 if (off + sizeof(*ip6e) > m->m_len) 1233 goto loopend; 1234 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 1235 if (nxt == IPPROTO_AH) 1236 elen = (ip6e->ip6e_len + 2) << 2; 1237 else 1238 elen = (ip6e->ip6e_len + 1) << 3; 1239 if (off + elen > m->m_len) 1240 goto loopend; 1241#else 1242 ext = ip6_pullexthdr(m, off, nxt); 1243 if (ext == NULL) { 1244 ip6stat.ip6s_tooshort++; 1245 return; 1246 } 1247 ip6e = mtod(ext, struct ip6_ext *); 1248 if (nxt == IPPROTO_AH) 1249 elen = (ip6e->ip6e_len + 2) << 2; 1250 else 1251 elen = (ip6e->ip6e_len + 1) << 3; 1252 if (elen != ext->m_len) { 1253 m_freem(ext); 1254 ip6stat.ip6s_tooshort++; 1255 return; 1256 } 1257#endif 1258 1259 switch (nxt) { 1260 case IPPROTO_DSTOPTS: 1261 if (!(in6p->in6p_flags & IN6P_DSTOPTS)) 1262 break; 1263 1264 /* 1265 * We also require super-user privilege for 1266 * the option. See comments on IN6_HOPOPTS. 1267 */ 1268 if (!privileged) 1269 break; 1270 1271 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1272 IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS), 1273 IPPROTO_IPV6); 1274 if (*mp) 1275 mp = &(*mp)->m_next; 1276 break; 1277 case IPPROTO_ROUTING: 1278 if (!in6p->in6p_flags & IN6P_RTHDR) 1279 break; 1280 1281 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1282 IS2292(IPV6_2292RTHDR, IPV6_RTHDR), 1283 IPPROTO_IPV6); 1284 if (*mp) 1285 mp = &(*mp)->m_next; 1286 break; 1287 case IPPROTO_HOPOPTS: 1288 case IPPROTO_AH: /* is it possible? */ 1289 break; 1290 1291 default: 1292 /* 1293 * other cases have been filtered in the above. 1294 * none will visit this case. here we supply 1295 * the code just in case (nxt overwritten or 1296 * other cases). 1297 */ 1298#ifdef PULLDOWN_TEST 1299 m_freem(ext); 1300#endif 1301 goto loopend; 1302 1303 } 1304 1305 /* proceed with the next header. */ 1306 off += elen; 1307 nxt = ip6e->ip6e_nxt; 1308 ip6e = NULL; 1309#ifdef PULLDOWN_TEST 1310 m_freem(ext); 1311 ext = NULL; 1312#endif 1313 } 1314 loopend: 1315 ; 1316 } 1317 1318#undef IS2292 1319} 1320 1321#ifdef PULLDOWN_TEST 1322/* 1323 * pull single extension header from mbuf chain. returns single mbuf that 1324 * contains the result, or NULL on error. 1325 */ 1326static struct mbuf * 1327ip6_pullexthdr(m, off, nxt) 1328 struct mbuf *m; 1329 size_t off; 1330 int nxt; 1331{ 1332 struct ip6_ext ip6e; 1333 size_t elen; 1334 struct mbuf *n; 1335 1336#ifdef DIAGNOSTIC 1337 switch (nxt) { 1338 case IPPROTO_DSTOPTS: 1339 case IPPROTO_ROUTING: 1340 case IPPROTO_HOPOPTS: 1341 case IPPROTO_AH: /* is it possible? */ 1342 break; 1343 default: 1344 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 1345 } 1346#endif 1347 1348 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1349 if (nxt == IPPROTO_AH) 1350 elen = (ip6e.ip6e_len + 2) << 2; 1351 else 1352 elen = (ip6e.ip6e_len + 1) << 3; 1353 1354 MGET(n, M_DONTWAIT, MT_DATA); 1355 if (n && elen >= MLEN) { 1356 MCLGET(n, M_DONTWAIT); 1357 if ((n->m_flags & M_EXT) == 0) { 1358 m_free(n); 1359 n = NULL; 1360 } 1361 } 1362 if (!n) 1363 return NULL; 1364 1365 n->m_len = 0; 1366 if (elen >= M_TRAILINGSPACE(n)) { 1367 m_free(n); 1368 return NULL; 1369 } 1370 1371 m_copydata(m, off, elen, mtod(n, caddr_t)); 1372 n->m_len = elen; 1373 return n; 1374} 1375#endif 1376 1377/* 1378 * Get pointer to the previous header followed by the header 1379 * currently processed. 1380 * XXX: This function supposes that 1381 * M includes all headers, 1382 * the next header field and the header length field of each header 1383 * are valid, and 1384 * the sum of each header length equals to OFF. 1385 * Because of these assumptions, this function must be called very 1386 * carefully. Moreover, it will not be used in the near future when 1387 * we develop `neater' mechanism to process extension headers. 1388 */ 1389char * 1390ip6_get_prevhdr(m, off) 1391 struct mbuf *m; 1392 int off; 1393{ 1394 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1395 1396 if (off == sizeof(struct ip6_hdr)) 1397 return (&ip6->ip6_nxt); 1398 else { 1399 int len, nxt; 1400 struct ip6_ext *ip6e = NULL; 1401 1402 nxt = ip6->ip6_nxt; 1403 len = sizeof(struct ip6_hdr); 1404 while (len < off) { 1405 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 1406 1407 switch (nxt) { 1408 case IPPROTO_FRAGMENT: 1409 len += sizeof(struct ip6_frag); 1410 break; 1411 case IPPROTO_AH: 1412 len += (ip6e->ip6e_len + 2) << 2; 1413 break; 1414 default: 1415 len += (ip6e->ip6e_len + 1) << 3; 1416 break; 1417 } 1418 nxt = ip6e->ip6e_nxt; 1419 } 1420 if (ip6e) 1421 return (&ip6e->ip6e_nxt); 1422 else 1423 return NULL; 1424 } 1425} 1426 1427/* 1428 * get next header offset. m will be retained. 1429 */ 1430int 1431ip6_nexthdr(m, off, proto, nxtp) 1432 struct mbuf *m; 1433 int off; 1434 int proto; 1435 int *nxtp; 1436{ 1437 struct ip6_hdr ip6; 1438 struct ip6_ext ip6e; 1439 struct ip6_frag fh; 1440 1441 /* just in case */ 1442 if (m == NULL) 1443 panic("ip6_nexthdr: m == NULL"); 1444 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 1445 return -1; 1446 1447 switch (proto) { 1448 case IPPROTO_IPV6: 1449 if (m->m_pkthdr.len < off + sizeof(ip6)) 1450 return -1; 1451 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 1452 if (nxtp) 1453 *nxtp = ip6.ip6_nxt; 1454 off += sizeof(ip6); 1455 return off; 1456 1457 case IPPROTO_FRAGMENT: 1458 /* 1459 * terminate parsing if it is not the first fragment, 1460 * it does not make sense to parse through it. 1461 */ 1462 if (m->m_pkthdr.len < off + sizeof(fh)) 1463 return -1; 1464 m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 1465 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */ 1466 if (fh.ip6f_offlg & IP6F_OFF_MASK) 1467 return -1; 1468 if (nxtp) 1469 *nxtp = fh.ip6f_nxt; 1470 off += sizeof(struct ip6_frag); 1471 return off; 1472 1473 case IPPROTO_AH: 1474 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1475 return -1; 1476 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1477 if (nxtp) 1478 *nxtp = ip6e.ip6e_nxt; 1479 off += (ip6e.ip6e_len + 2) << 2; 1480 return off; 1481 1482 case IPPROTO_HOPOPTS: 1483 case IPPROTO_ROUTING: 1484 case IPPROTO_DSTOPTS: 1485 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1486 return -1; 1487 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1488 if (nxtp) 1489 *nxtp = ip6e.ip6e_nxt; 1490 off += (ip6e.ip6e_len + 1) << 3; 1491 return off; 1492 1493 case IPPROTO_NONE: 1494 case IPPROTO_ESP: 1495 case IPPROTO_IPCOMP: 1496 /* give up */ 1497 return -1; 1498 1499 default: 1500 return -1; 1501 } 1502 1503 return -1; 1504} 1505 1506/* 1507 * get offset for the last header in the chain. m will be kept untainted. 1508 */ 1509int 1510ip6_lasthdr(m, off, proto, nxtp) 1511 struct mbuf *m; 1512 int off; 1513 int proto; 1514 int *nxtp; 1515{ 1516 int newoff; 1517 int nxt; 1518 1519 if (!nxtp) { 1520 nxt = -1; 1521 nxtp = &nxt; 1522 } 1523 while (1) { 1524 newoff = ip6_nexthdr(m, off, proto, nxtp); 1525 if (newoff < 0) 1526 return off; 1527 else if (newoff < off) 1528 return -1; /* invalid */ 1529 else if (newoff == off) 1530 return newoff; 1531 1532 off = newoff; 1533 proto = *nxtp; 1534 } 1535} 1536 1537struct ip6aux * 1538ip6_addaux(m) 1539 struct mbuf *m; 1540{ 1541 struct m_tag *mtag; 1542 1543 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1544 if (!mtag) { 1545 mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux), 1546 M_NOWAIT); 1547 if (mtag) 1548 m_tag_prepend(m, mtag); 1549 } 1550 if (mtag) 1551 bzero(mtag+1, sizeof (struct ip6aux)); 1552 return mtag ? (struct ip6aux*)(mtag+1) : NULL; 1553} 1554 1555struct ip6aux * 1556ip6_findaux(m) 1557 struct mbuf *m; 1558{ 1559 struct m_tag *mtag; 1560 1561 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1562 return mtag ? (struct ip6aux*)(mtag+1) : NULL; 1563} 1564 1565void 1566ip6_delaux(m) 1567 struct mbuf *m; 1568{ 1569 struct m_tag *mtag; 1570 1571 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1572 if (mtag) 1573 m_tag_delete(m, mtag); 1574} 1575 1576/* 1577 * System control for IP6 1578 */ 1579 1580u_char inet6ctlerrmap[PRC_NCMDS] = { 1581 0, 0, 0, 0, 1582 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1583 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1584 EMSGSIZE, EHOSTUNREACH, 0, 0, 1585 0, 0, 0, 0, 1586 ENOPROTOOPT 1587}; 1588