1 /*- 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $ 30 */ 31 32 /*- 33 * Copyright (c) 1982, 1986, 1988, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 4. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 61 */ 62 63 #include <sys/cdefs.h> 64 __FBSDID("$FreeBSD$"); 65 66 #include "opt_inet.h" 67 #include "opt_inet6.h" 68 #include "opt_ipfw.h" 69 #include "opt_ipsec.h" 70 #include "opt_route.h" 71 72 #include <sys/param.h> 73 #include <sys/systm.h> 74 #include <sys/malloc.h> 75 #include <sys/mbuf.h> 76 #include <sys/proc.h> 77 #include <sys/domain.h> 78 #include <sys/protosw.h> 79 #include <sys/socket.h> 80 #include <sys/socketvar.h> 81 #include <sys/errno.h> 82 #include <sys/time.h> 83 #include <sys/kernel.h> 84 #include <sys/syslog.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> 91 #include <net/pfil.h> 92 #include <net/vnet.h> 93 94 #include <netinet/in.h> 95 #include <netinet/ip_var.h> 96 #include <netinet/in_systm.h> 97 #include <net/if_llatbl.h> 98 #ifdef INET 99 #include <netinet/ip.h> 100 #include <netinet/ip_icmp.h> 101 #endif /* INET */ 102 #include <netinet/ip6.h> 103 #include <netinet6/in6_var.h> 104 #include <netinet6/ip6_var.h> 105 #include <netinet/in_pcb.h> 106 #include <netinet/icmp6.h> 107 #include <netinet6/scope6_var.h> 108 #include <netinet6/in6_ifattach.h> 109 #include <netinet6/nd6.h> 110 111 #ifdef IPSEC 112 #include <netipsec/ipsec.h> 113 #include <netinet6/ip6_ipsec.h> 114 #include <netipsec/ipsec6.h> 115 #endif /* IPSEC */ 116 117 #include <netinet6/ip6protosw.h> 118 119 #ifdef FLOWTABLE 120 #include <net/flowtable.h> 121 VNET_DECLARE(int, ip6_output_flowtable_size); 122 #define V_ip6_output_flowtable_size VNET(ip6_output_flowtable_size) 123 #endif 124 125 extern struct domain inet6domain; 126 127 u_char ip6_protox[IPPROTO_MAX]; 128 VNET_DEFINE(struct in6_ifaddrhead, in6_ifaddrhead); 129 130 static struct netisr_handler ip6_nh = { 131 .nh_name = "ip6", 132 .nh_handler = ip6_input, 133 .nh_proto = NETISR_IPV6, 134 .nh_policy = NETISR_POLICY_FLOW, 135 }; 136 137 VNET_DECLARE(struct callout, in6_tmpaddrtimer_ch); 138 #define V_in6_tmpaddrtimer_ch VNET(in6_tmpaddrtimer_ch) 139 140 VNET_DEFINE(struct pfil_head, inet6_pfil_hook); 141 142 VNET_DEFINE(struct ip6stat, ip6stat); 143 144 struct rwlock in6_ifaddr_lock; 145 RW_SYSINIT(in6_ifaddr_lock, &in6_ifaddr_lock, "in6_ifaddr_lock"); 146 147 static void ip6_init2(void *); 148 static struct ip6aux *ip6_setdstifaddr(struct mbuf *, struct in6_ifaddr *); 149 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *); 150 #ifdef PULLDOWN_TEST 151 static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int); 152 #endif 153 154 /* 155 * IP6 initialization: fill in IP6 protocol switch table. 156 * All protocols not implemented in kernel go to raw IP6 protocol handler. 157 */ 158 void 159 ip6_init(void) 160 { 161 struct ip6protosw *pr; 162 int i; 163 164 TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal", 165 &V_ip6_auto_linklocal); 166 TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv); 167 TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr); 168 169 TAILQ_INIT(&V_in6_ifaddrhead); 170 171 /* Initialize packet filter hooks. */ 172 V_inet6_pfil_hook.ph_type = PFIL_TYPE_AF; 173 V_inet6_pfil_hook.ph_af = AF_INET6; 174 if ((i = pfil_head_register(&V_inet6_pfil_hook)) != 0) 175 printf("%s: WARNING: unable to register pfil hook, " 176 "error %d\n", __func__, i); 177 178 scope6_init(); 179 addrsel_policy_init(); 180 nd6_init(); 181 frag6_init(); 182 183 #ifdef FLOWTABLE 184 if (TUNABLE_INT_FETCH("net.inet6.ip6.output_flowtable_size", 185 &V_ip6_output_flowtable_size)) { 186 if (V_ip6_output_flowtable_size < 256) 187 V_ip6_output_flowtable_size = 256; 188 if (!powerof2(V_ip6_output_flowtable_size)) { 189 printf("flowtable must be power of 2 size\n"); 190 V_ip6_output_flowtable_size = 2048; 191 } 192 } else { 193 /* 194 * round up to the next power of 2 195 */ 196 V_ip6_output_flowtable_size = 1 << fls((1024 + maxusers * 64)-1); 197 } 198 V_ip6_ft = flowtable_alloc("ipv6", V_ip6_output_flowtable_size, FL_IPV6|FL_PCPU); 199 #endif 200 201 V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR; 202 203 /* Skip global initialization stuff for non-default instances. */ 204 if (!IS_DEFAULT_VNET(curvnet)) 205 return; 206 207 #ifdef DIAGNOSTIC 208 if (sizeof(struct protosw) != sizeof(struct ip6protosw)) 209 panic("sizeof(protosw) != sizeof(ip6protosw)"); 210 #endif 211 pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 212 if (pr == NULL) 213 panic("ip6_init"); 214 215 /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */ 216 for (i = 0; i < IPPROTO_MAX; i++) 217 ip6_protox[i] = pr - inet6sw; 218 /* 219 * Cycle through IP protocols and put them into the appropriate place 220 * in ip6_protox[]. 221 */ 222 for (pr = (struct ip6protosw *)inet6domain.dom_protosw; 223 pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++) 224 if (pr->pr_domain->dom_family == PF_INET6 && 225 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) { 226 /* Be careful to only index valid IP protocols. */ 227 if (pr->pr_protocol < IPPROTO_MAX) 228 ip6_protox[pr->pr_protocol] = pr - inet6sw; 229 } 230 231 netisr_register(&ip6_nh); 232 } 233 234 /* 235 * The protocol to be inserted into ip6_protox[] must be already registered 236 * in inet6sw[], either statically or through pf_proto_register(). 237 */ 238 int 239 ip6proto_register(short ip6proto) 240 { 241 struct ip6protosw *pr; 242 243 /* Sanity checks. */ 244 if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX) 245 return (EPROTONOSUPPORT); 246 247 /* 248 * The protocol slot must not be occupied by another protocol 249 * already. An index pointing to IPPROTO_RAW is unused. 250 */ 251 pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 252 if (pr == NULL) 253 return (EPFNOSUPPORT); 254 if (ip6_protox[ip6proto] != pr - inet6sw) /* IPPROTO_RAW */ 255 return (EEXIST); 256 257 /* 258 * Find the protocol position in inet6sw[] and set the index. 259 */ 260 for (pr = (struct ip6protosw *)inet6domain.dom_protosw; 261 pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++) { 262 if (pr->pr_domain->dom_family == PF_INET6 && 263 pr->pr_protocol && pr->pr_protocol == ip6proto) { 264 ip6_protox[pr->pr_protocol] = pr - inet6sw; 265 return (0); 266 } 267 } 268 return (EPROTONOSUPPORT); 269 } 270 271 int 272 ip6proto_unregister(short ip6proto) 273 { 274 struct ip6protosw *pr; 275 276 /* Sanity checks. */ 277 if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX) 278 return (EPROTONOSUPPORT); 279 280 /* Check if the protocol was indeed registered. */ 281 pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 282 if (pr == NULL) 283 return (EPFNOSUPPORT); 284 if (ip6_protox[ip6proto] == pr - inet6sw) /* IPPROTO_RAW */ 285 return (ENOENT); 286 287 /* Reset the protocol slot to IPPROTO_RAW. */ 288 ip6_protox[ip6proto] = pr - inet6sw; 289 return (0); 290 } 291 292 #ifdef VIMAGE 293 void 294 ip6_destroy() 295 { 296 297 nd6_destroy(); 298 callout_drain(&V_in6_tmpaddrtimer_ch); 299 } 300 #endif 301 302 static int 303 ip6_init2_vnet(const void *unused __unused) 304 { 305 306 /* nd6_timer_init */ 307 callout_init(&V_nd6_timer_ch, 0); 308 callout_reset(&V_nd6_timer_ch, hz, nd6_timer, curvnet); 309 310 /* timer for regeneranation of temporary addresses randomize ID */ 311 callout_init(&V_in6_tmpaddrtimer_ch, 0); 312 callout_reset(&V_in6_tmpaddrtimer_ch, 313 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor - 314 V_ip6_temp_regen_advance) * hz, 315 in6_tmpaddrtimer, curvnet); 316 317 return (0); 318 } 319 320 static void 321 ip6_init2(void *dummy) 322 { 323 324 ip6_init2_vnet(NULL); 325 } 326 327 /* cheat */ 328 /* This must be after route_init(), which is now SI_ORDER_THIRD */ 329 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL); 330 331 void 332 ip6_input(struct mbuf *m) 333 { 334 struct ip6_hdr *ip6; 335 int off = sizeof(struct ip6_hdr), nest; 336 u_int32_t plen; 337 u_int32_t rtalert = ~0; 338 int nxt, ours = 0; 339 struct ifnet *deliverifp = NULL, *ifp = NULL; 340 struct in6_addr odst; 341 struct route_in6 rin6; 342 int srcrt = 0; 343 struct llentry *lle = NULL; 344 struct sockaddr_in6 dst6, *dst; 345 346 bzero(&rin6, sizeof(struct route_in6)); 347 #ifdef IPSEC 348 /* 349 * should the inner packet be considered authentic? 350 * see comment in ah4_input(). 351 * NB: m cannot be NULL when passed to the input routine 352 */ 353 354 m->m_flags &= ~M_AUTHIPHDR; 355 m->m_flags &= ~M_AUTHIPDGM; 356 357 #endif /* IPSEC */ 358 359 /* 360 * make sure we don't have onion peering information into m_tag. 361 */ 362 ip6_delaux(m); 363 364 if (m->m_flags & M_FASTFWD_OURS) { 365 /* 366 * Firewall changed destination to local. 367 */ 368 m->m_flags &= ~M_FASTFWD_OURS; 369 ours = 1; 370 deliverifp = m->m_pkthdr.rcvif; 371 ip6 = mtod(m, struct ip6_hdr *); 372 goto hbhcheck; 373 } 374 375 /* 376 * mbuf statistics 377 */ 378 if (m->m_flags & M_EXT) { 379 if (m->m_next) 380 V_ip6stat.ip6s_mext2m++; 381 else 382 V_ip6stat.ip6s_mext1++; 383 } else { 384 #define M2MMAX (sizeof(V_ip6stat.ip6s_m2m)/sizeof(V_ip6stat.ip6s_m2m[0])) 385 if (m->m_next) { 386 if (m->m_flags & M_LOOP) { 387 V_ip6stat.ip6s_m2m[V_loif->if_index]++; 388 } else if (m->m_pkthdr.rcvif->if_index < M2MMAX) 389 V_ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++; 390 else 391 V_ip6stat.ip6s_m2m[0]++; 392 } else 393 V_ip6stat.ip6s_m1++; 394 #undef M2MMAX 395 } 396 397 /* drop the packet if IPv6 operation is disabled on the IF */ 398 if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED)) { 399 m_freem(m); 400 return; 401 } 402 403 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive); 404 V_ip6stat.ip6s_total++; 405 406 #ifndef PULLDOWN_TEST 407 /* 408 * L2 bridge code and some other code can return mbuf chain 409 * that does not conform to KAME requirement. too bad. 410 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 411 */ 412 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 413 struct mbuf *n; 414 415 MGETHDR(n, M_DONTWAIT, MT_HEADER); 416 if (n) 417 M_MOVE_PKTHDR(n, m); 418 if (n && n->m_pkthdr.len > MHLEN) { 419 MCLGET(n, M_DONTWAIT); 420 if ((n->m_flags & M_EXT) == 0) { 421 m_freem(n); 422 n = NULL; 423 } 424 } 425 if (n == NULL) { 426 m_freem(m); 427 return; /* ENOBUFS */ 428 } 429 430 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t)); 431 n->m_len = n->m_pkthdr.len; 432 m_freem(m); 433 m = n; 434 } 435 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */); 436 #endif 437 438 if (m->m_len < sizeof(struct ip6_hdr)) { 439 struct ifnet *inifp; 440 inifp = m->m_pkthdr.rcvif; 441 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 442 V_ip6stat.ip6s_toosmall++; 443 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 444 return; 445 } 446 } 447 448 ip6 = mtod(m, struct ip6_hdr *); 449 450 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 451 V_ip6stat.ip6s_badvers++; 452 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 453 goto bad; 454 } 455 456 V_ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; 457 458 /* 459 * Check against address spoofing/corruption. 460 */ 461 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 462 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 463 /* 464 * XXX: "badscope" is not very suitable for a multicast source. 465 */ 466 V_ip6stat.ip6s_badscope++; 467 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 468 goto bad; 469 } 470 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) && 471 !(m->m_flags & M_LOOP)) { 472 /* 473 * In this case, the packet should come from the loopback 474 * interface. However, we cannot just check the if_flags, 475 * because ip6_mloopback() passes the "actual" interface 476 * as the outgoing/incoming interface. 477 */ 478 V_ip6stat.ip6s_badscope++; 479 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 480 goto bad; 481 } 482 483 #ifdef ALTQ 484 if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) { 485 /* packet is dropped by traffic conditioner */ 486 return; 487 } 488 #endif 489 /* 490 * The following check is not documented in specs. A malicious 491 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack 492 * and bypass security checks (act as if it was from 127.0.0.1 by using 493 * IPv6 src ::ffff:127.0.0.1). Be cautious. 494 * 495 * This check chokes if we are in an SIIT cloud. As none of BSDs 496 * support IPv4-less kernel compilation, we cannot support SIIT 497 * environment at all. So, it makes more sense for us to reject any 498 * malicious packets for non-SIIT environment, than try to do a 499 * partial support for SIIT environment. 500 */ 501 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 502 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 503 V_ip6stat.ip6s_badscope++; 504 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 505 goto bad; 506 } 507 #if 0 508 /* 509 * Reject packets with IPv4 compatible addresses (auto tunnel). 510 * 511 * The code forbids auto tunnel relay case in RFC1933 (the check is 512 * stronger than RFC1933). We may want to re-enable it if mech-xx 513 * is revised to forbid relaying case. 514 */ 515 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) || 516 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) { 517 V_ip6stat.ip6s_badscope++; 518 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 519 goto bad; 520 } 521 #endif 522 #ifdef IPSEC 523 /* 524 * Bypass packet filtering for packets previously handled by IPsec. 525 */ 526 if (ip6_ipsec_filtertunnel(m)) 527 goto passin; 528 #endif /* IPSEC */ 529 530 /* 531 * Run through list of hooks for input packets. 532 * 533 * NB: Beware of the destination address changing 534 * (e.g. by NAT rewriting). When this happens, 535 * tell ip6_forward to do the right thing. 536 */ 537 odst = ip6->ip6_dst; 538 539 /* Jump over all PFIL processing if hooks are not active. */ 540 if (!PFIL_HOOKED(&V_inet6_pfil_hook)) 541 goto passin; 542 543 if (pfil_run_hooks(&V_inet6_pfil_hook, &m, 544 m->m_pkthdr.rcvif, PFIL_IN, NULL)) 545 return; 546 if (m == NULL) /* consumed by filter */ 547 return; 548 ip6 = mtod(m, struct ip6_hdr *); 549 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst); 550 551 #ifdef IPFIREWALL_FORWARD 552 if (m->m_flags & M_FASTFWD_OURS) { 553 m->m_flags &= ~M_FASTFWD_OURS; 554 ours = 1; 555 deliverifp = m->m_pkthdr.rcvif; 556 goto hbhcheck; 557 } 558 if (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) { 559 /* 560 * Directly ship the packet on. This allows forwarding 561 * packets originally destined to us to some other directly 562 * connected host. 563 */ 564 ip6_forward(m, 1); 565 goto out; 566 } 567 #endif /* IPFIREWALL_FORWARD */ 568 569 passin: 570 /* 571 * Disambiguate address scope zones (if there is ambiguity). 572 * We first make sure that the original source or destination address 573 * is not in our internal form for scoped addresses. Such addresses 574 * are not necessarily invalid spec-wise, but we cannot accept them due 575 * to the usage conflict. 576 * in6_setscope() then also checks and rejects the cases where src or 577 * dst are the loopback address and the receiving interface 578 * is not loopback. 579 */ 580 if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) { 581 V_ip6stat.ip6s_badscope++; /* XXX */ 582 goto bad; 583 } 584 if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) || 585 in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) { 586 V_ip6stat.ip6s_badscope++; 587 goto bad; 588 } 589 590 /* 591 * Multicast check. Assume packet is for us to avoid 592 * prematurely taking locks. 593 */ 594 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 595 ours = 1; 596 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast); 597 deliverifp = m->m_pkthdr.rcvif; 598 goto hbhcheck; 599 } 600 601 /* 602 * Unicast check 603 */ 604 605 bzero(&dst6, sizeof(dst6)); 606 dst6.sin6_family = AF_INET6; 607 dst6.sin6_len = sizeof(struct sockaddr_in6); 608 dst6.sin6_addr = ip6->ip6_dst; 609 ifp = m->m_pkthdr.rcvif; 610 IF_AFDATA_LOCK(ifp); 611 lle = lla_lookup(LLTABLE6(ifp), 0, 612 (struct sockaddr *)&dst6); 613 IF_AFDATA_UNLOCK(ifp); 614 if ((lle != NULL) && (lle->la_flags & LLE_IFADDR)) { 615 struct ifaddr *ifa; 616 struct in6_ifaddr *ia6; 617 int bad; 618 619 bad = 1; 620 #define sa_equal(a1, a2) \ 621 (bcmp((a1), (a2), ((a1))->sin6_len) == 0) 622 IF_ADDR_RLOCK(ifp); 623 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 624 if (ifa->ifa_addr->sa_family != dst6.sin6_family) 625 continue; 626 if (sa_equal(&dst6, ifa->ifa_addr)) 627 break; 628 } 629 KASSERT(ifa != NULL, ("%s: ifa not found for lle %p", 630 __func__, lle)); 631 #undef sa_equal 632 633 ia6 = (struct in6_ifaddr *)ifa; 634 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 635 /* Count the packet in the ip address stats */ 636 ia6->ia_ifa.if_ipackets++; 637 ia6->ia_ifa.if_ibytes += m->m_pkthdr.len; 638 639 /* 640 * record address information into m_tag. 641 */ 642 (void)ip6_setdstifaddr(m, ia6); 643 644 bad = 0; 645 } else { 646 char ip6bufs[INET6_ADDRSTRLEN]; 647 char ip6bufd[INET6_ADDRSTRLEN]; 648 /* address is not ready, so discard the packet. */ 649 nd6log((LOG_INFO, 650 "ip6_input: packet to an unready address %s->%s\n", 651 ip6_sprintf(ip6bufs, &ip6->ip6_src), 652 ip6_sprintf(ip6bufd, &ip6->ip6_dst))); 653 } 654 IF_ADDR_RUNLOCK(ifp); 655 LLE_RUNLOCK(lle); 656 if (bad) 657 goto bad; 658 else { 659 ours = 1; 660 deliverifp = ifp; 661 goto hbhcheck; 662 } 663 } 664 if (lle != NULL) 665 LLE_RUNLOCK(lle); 666 667 dst = &rin6.ro_dst; 668 dst->sin6_len = sizeof(struct sockaddr_in6); 669 dst->sin6_family = AF_INET6; 670 dst->sin6_addr = ip6->ip6_dst; 671 rin6.ro_rt = in6_rtalloc1((struct sockaddr *)dst, 0, 0, M_GETFIB(m)); 672 if (rin6.ro_rt) 673 RT_UNLOCK(rin6.ro_rt); 674 675 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key)) 676 677 /* 678 * Accept the packet if the forwarding interface to the destination 679 * according to the routing table is the loopback interface, 680 * unless the associated route has a gateway. 681 * Note that this approach causes to accept a packet if there is a 682 * route to the loopback interface for the destination of the packet. 683 * But we think it's even useful in some situations, e.g. when using 684 * a special daemon which wants to intercept the packet. 685 * 686 * XXX: some OSes automatically make a cloned route for the destination 687 * of an outgoing packet. If the outgoing interface of the packet 688 * is a loopback one, the kernel would consider the packet to be 689 * accepted, even if we have no such address assinged on the interface. 690 * We check the cloned flag of the route entry to reject such cases, 691 * assuming that route entries for our own addresses are not made by 692 * cloning (it should be true because in6_addloop explicitly installs 693 * the host route). However, we might have to do an explicit check 694 * while it would be less efficient. Or, should we rather install a 695 * reject route for such a case? 696 */ 697 if (rin6.ro_rt && 698 (rin6.ro_rt->rt_flags & 699 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && 700 #ifdef RTF_WASCLONED 701 !(rin6.ro_rt->rt_flags & RTF_WASCLONED) && 702 #endif 703 #ifdef RTF_CLONED 704 !(rin6.ro_rt->rt_flags & RTF_CLONED) && 705 #endif 706 #if 0 707 /* 708 * The check below is redundant since the comparison of 709 * the destination and the key of the rtentry has 710 * already done through looking up the routing table. 711 */ 712 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 713 &rt6_key(rin6.ro_rt)->sin6_addr) 714 #endif 715 rin6.ro_rt->rt_ifp->if_type == IFT_LOOP) { 716 int free_ia6 = 0; 717 struct in6_ifaddr *ia6; 718 719 /* 720 * found the loopback route to the interface address 721 */ 722 if (rin6.ro_rt->rt_gateway->sa_family == AF_LINK) { 723 struct sockaddr_in6 dest6; 724 725 bzero(&dest6, sizeof(dest6)); 726 dest6.sin6_family = AF_INET6; 727 dest6.sin6_len = sizeof(dest6); 728 dest6.sin6_addr = ip6->ip6_dst; 729 ia6 = (struct in6_ifaddr *) 730 ifa_ifwithaddr((struct sockaddr *)&dest6); 731 if (ia6 == NULL) 732 goto bad; 733 free_ia6 = 1; 734 } 735 else 736 ia6 = (struct in6_ifaddr *)rin6.ro_rt->rt_ifa; 737 738 /* 739 * record address information into m_tag. 740 */ 741 (void)ip6_setdstifaddr(m, ia6); 742 743 /* 744 * packets to a tentative, duplicated, or somehow invalid 745 * address must not be accepted. 746 */ 747 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 748 /* this address is ready */ 749 ours = 1; 750 deliverifp = ia6->ia_ifp; /* correct? */ 751 /* Count the packet in the ip address stats */ 752 ia6->ia_ifa.if_ipackets++; 753 ia6->ia_ifa.if_ibytes += m->m_pkthdr.len; 754 if (ia6 != NULL && free_ia6 != 0) 755 ifa_free(&ia6->ia_ifa); 756 goto hbhcheck; 757 } else { 758 char ip6bufs[INET6_ADDRSTRLEN]; 759 char ip6bufd[INET6_ADDRSTRLEN]; 760 /* address is not ready, so discard the packet. */ 761 nd6log((LOG_INFO, 762 "ip6_input: packet to an unready address %s->%s\n", 763 ip6_sprintf(ip6bufs, &ip6->ip6_src), 764 ip6_sprintf(ip6bufd, &ip6->ip6_dst))); 765 766 if (ia6 != NULL && free_ia6 != 0) 767 ifa_free(&ia6->ia_ifa); 768 goto bad; 769 } 770 } 771 772 /* 773 * FAITH (Firewall Aided Internet Translator) 774 */ 775 if (V_ip6_keepfaith) { 776 if (rin6.ro_rt && rin6.ro_rt->rt_ifp && 777 rin6.ro_rt->rt_ifp->if_type == IFT_FAITH) { 778 /* XXX do we need more sanity checks? */ 779 ours = 1; 780 deliverifp = rin6.ro_rt->rt_ifp; /* faith */ 781 goto hbhcheck; 782 } 783 } 784 785 /* 786 * Now there is no reason to process the packet if it's not our own 787 * and we're not a router. 788 */ 789 if (!V_ip6_forwarding) { 790 V_ip6stat.ip6s_cantforward++; 791 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 792 goto bad; 793 } 794 795 hbhcheck: 796 /* 797 * record address information into m_tag, if we don't have one yet. 798 * note that we are unable to record it, if the address is not listed 799 * as our interface address (e.g. multicast addresses, addresses 800 * within FAITH prefixes and such). 801 */ 802 if (deliverifp && !ip6_getdstifaddr(m)) { 803 struct in6_ifaddr *ia6; 804 805 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); 806 if (ia6) { 807 if (!ip6_setdstifaddr(m, ia6)) { 808 /* 809 * XXX maybe we should drop the packet here, 810 * as we could not provide enough information 811 * to the upper layers. 812 */ 813 } 814 ifa_free(&ia6->ia_ifa); 815 } 816 } 817 818 /* 819 * Process Hop-by-Hop options header if it's contained. 820 * m may be modified in ip6_hopopts_input(). 821 * If a JumboPayload option is included, plen will also be modified. 822 */ 823 plen = (u_int32_t)ntohs(ip6->ip6_plen); 824 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 825 struct ip6_hbh *hbh; 826 827 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) { 828 #if 0 /*touches NULL pointer*/ 829 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 830 #endif 831 goto out; /* m have already been freed */ 832 } 833 834 /* adjust pointer */ 835 ip6 = mtod(m, struct ip6_hdr *); 836 837 /* 838 * if the payload length field is 0 and the next header field 839 * indicates Hop-by-Hop Options header, then a Jumbo Payload 840 * option MUST be included. 841 */ 842 if (ip6->ip6_plen == 0 && plen == 0) { 843 /* 844 * Note that if a valid jumbo payload option is 845 * contained, ip6_hopopts_input() must set a valid 846 * (non-zero) payload length to the variable plen. 847 */ 848 V_ip6stat.ip6s_badoptions++; 849 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 850 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 851 icmp6_error(m, ICMP6_PARAM_PROB, 852 ICMP6_PARAMPROB_HEADER, 853 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); 854 goto out; 855 } 856 #ifndef PULLDOWN_TEST 857 /* ip6_hopopts_input() ensures that mbuf is contiguous */ 858 hbh = (struct ip6_hbh *)(ip6 + 1); 859 #else 860 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 861 sizeof(struct ip6_hbh)); 862 if (hbh == NULL) { 863 V_ip6stat.ip6s_tooshort++; 864 goto out; 865 } 866 #endif 867 nxt = hbh->ip6h_nxt; 868 869 /* 870 * If we are acting as a router and the packet contains a 871 * router alert option, see if we know the option value. 872 * Currently, we only support the option value for MLD, in which 873 * case we should pass the packet to the multicast routing 874 * daemon. 875 */ 876 if (rtalert != ~0) { 877 switch (rtalert) { 878 case IP6OPT_RTALERT_MLD: 879 if (V_ip6_forwarding) 880 ours = 1; 881 break; 882 default: 883 /* 884 * RFC2711 requires unrecognized values must be 885 * silently ignored. 886 */ 887 break; 888 } 889 } 890 } else 891 nxt = ip6->ip6_nxt; 892 893 /* 894 * Check that the amount of data in the buffers 895 * is as at least much as the IPv6 header would have us expect. 896 * Trim mbufs if longer than we expect. 897 * Drop packet if shorter than we expect. 898 */ 899 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 900 V_ip6stat.ip6s_tooshort++; 901 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 902 goto bad; 903 } 904 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 905 if (m->m_len == m->m_pkthdr.len) { 906 m->m_len = sizeof(struct ip6_hdr) + plen; 907 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 908 } else 909 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 910 } 911 912 /* 913 * Forward if desirable. 914 */ 915 if (V_ip6_mrouter && 916 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 917 /* 918 * If we are acting as a multicast router, all 919 * incoming multicast packets are passed to the 920 * kernel-level multicast forwarding function. 921 * The packet is returned (relatively) intact; if 922 * ip6_mforward() returns a non-zero value, the packet 923 * must be discarded, else it may be accepted below. 924 * 925 * XXX TODO: Check hlim and multicast scope here to avoid 926 * unnecessarily calling into ip6_mforward(). 927 */ 928 if (ip6_mforward && 929 ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) { 930 IP6STAT_INC(ip6s_cantforward); 931 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 932 goto bad; 933 } 934 } else if (!ours) { 935 ip6_forward(m, srcrt); 936 goto out; 937 } 938 939 ip6 = mtod(m, struct ip6_hdr *); 940 941 /* 942 * Malicious party may be able to use IPv4 mapped addr to confuse 943 * tcp/udp stack and bypass security checks (act as if it was from 944 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 945 * 946 * For SIIT end node behavior, you may want to disable the check. 947 * However, you will become vulnerable to attacks using IPv4 mapped 948 * source. 949 */ 950 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 951 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 952 V_ip6stat.ip6s_badscope++; 953 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 954 goto bad; 955 } 956 957 /* 958 * Tell launch routine the next header 959 */ 960 V_ip6stat.ip6s_delivered++; 961 in6_ifstat_inc(deliverifp, ifs6_in_deliver); 962 nest = 0; 963 964 while (nxt != IPPROTO_DONE) { 965 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) { 966 V_ip6stat.ip6s_toomanyhdr++; 967 goto bad; 968 } 969 970 /* 971 * protection against faulty packet - there should be 972 * more sanity checks in header chain processing. 973 */ 974 if (m->m_pkthdr.len < off) { 975 V_ip6stat.ip6s_tooshort++; 976 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 977 goto bad; 978 } 979 980 #ifdef IPSEC 981 /* 982 * enforce IPsec policy checking if we are seeing last header. 983 * note that we do not visit this with protocols with pcb layer 984 * code - like udp/tcp/raw ip. 985 */ 986 if (ip6_ipsec_input(m, nxt)) 987 goto bad; 988 #endif /* IPSEC */ 989 990 /* 991 * Use mbuf flags to propagate Router Alert option to 992 * ICMPv6 layer, as hop-by-hop options have been stripped. 993 */ 994 if (nxt == IPPROTO_ICMPV6 && rtalert != ~0) 995 m->m_flags |= M_RTALERT_MLD; 996 997 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt); 998 } 999 goto out; 1000 bad: 1001 m_freem(m); 1002 out: 1003 if (rin6.ro_rt) 1004 RTFREE(rin6.ro_rt); 1005 } 1006 1007 /* 1008 * set/grab in6_ifaddr correspond to IPv6 destination address. 1009 * XXX backward compatibility wrapper 1010 * 1011 * XXXRW: We should bump the refcount on ia6 before sticking it in the m_tag, 1012 * and then bump it when the tag is copied, and release it when the tag is 1013 * freed. Unfortunately, m_tags don't support deep copies (yet), so instead 1014 * we just bump the ia refcount when we receive it. This should be fixed. 1015 */ 1016 static struct ip6aux * 1017 ip6_setdstifaddr(struct mbuf *m, struct in6_ifaddr *ia6) 1018 { 1019 struct ip6aux *ip6a; 1020 1021 ip6a = ip6_addaux(m); 1022 if (ip6a) 1023 ip6a->ip6a_dstia6 = ia6; 1024 return ip6a; /* NULL if failed to set */ 1025 } 1026 1027 struct in6_ifaddr * 1028 ip6_getdstifaddr(struct mbuf *m) 1029 { 1030 struct ip6aux *ip6a; 1031 struct in6_ifaddr *ia; 1032 1033 ip6a = ip6_findaux(m); 1034 if (ip6a) { 1035 ia = ip6a->ip6a_dstia6; 1036 ifa_ref(&ia->ia_ifa); 1037 return ia; 1038 } else 1039 return NULL; 1040 } 1041 1042 /* 1043 * Hop-by-Hop options header processing. If a valid jumbo payload option is 1044 * included, the real payload length will be stored in plenp. 1045 * 1046 * rtalertp - XXX: should be stored more smart way 1047 */ 1048 static int 1049 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp, 1050 struct mbuf **mp, int *offp) 1051 { 1052 struct mbuf *m = *mp; 1053 int off = *offp, hbhlen; 1054 struct ip6_hbh *hbh; 1055 u_int8_t *opt; 1056 1057 /* validation of the length of the header */ 1058 #ifndef PULLDOWN_TEST 1059 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1); 1060 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 1061 hbhlen = (hbh->ip6h_len + 1) << 3; 1062 1063 IP6_EXTHDR_CHECK(m, off, hbhlen, -1); 1064 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 1065 #else 1066 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, 1067 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 1068 if (hbh == NULL) { 1069 V_ip6stat.ip6s_tooshort++; 1070 return -1; 1071 } 1072 hbhlen = (hbh->ip6h_len + 1) << 3; 1073 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 1074 hbhlen); 1075 if (hbh == NULL) { 1076 V_ip6stat.ip6s_tooshort++; 1077 return -1; 1078 } 1079 #endif 1080 off += hbhlen; 1081 hbhlen -= sizeof(struct ip6_hbh); 1082 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh); 1083 1084 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 1085 hbhlen, rtalertp, plenp) < 0) 1086 return (-1); 1087 1088 *offp = off; 1089 *mp = m; 1090 return (0); 1091 } 1092 1093 /* 1094 * Search header for all Hop-by-hop options and process each option. 1095 * This function is separate from ip6_hopopts_input() in order to 1096 * handle a case where the sending node itself process its hop-by-hop 1097 * options header. In such a case, the function is called from ip6_output(). 1098 * 1099 * The function assumes that hbh header is located right after the IPv6 header 1100 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 1101 * opthead + hbhlen is located in contiguous memory region. 1102 */ 1103 int 1104 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen, 1105 u_int32_t *rtalertp, u_int32_t *plenp) 1106 { 1107 struct ip6_hdr *ip6; 1108 int optlen = 0; 1109 u_int8_t *opt = opthead; 1110 u_int16_t rtalert_val; 1111 u_int32_t jumboplen; 1112 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 1113 1114 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 1115 switch (*opt) { 1116 case IP6OPT_PAD1: 1117 optlen = 1; 1118 break; 1119 case IP6OPT_PADN: 1120 if (hbhlen < IP6OPT_MINLEN) { 1121 V_ip6stat.ip6s_toosmall++; 1122 goto bad; 1123 } 1124 optlen = *(opt + 1) + 2; 1125 break; 1126 case IP6OPT_ROUTER_ALERT: 1127 /* XXX may need check for alignment */ 1128 if (hbhlen < IP6OPT_RTALERT_LEN) { 1129 V_ip6stat.ip6s_toosmall++; 1130 goto bad; 1131 } 1132 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 1133 /* XXX stat */ 1134 icmp6_error(m, ICMP6_PARAM_PROB, 1135 ICMP6_PARAMPROB_HEADER, 1136 erroff + opt + 1 - opthead); 1137 return (-1); 1138 } 1139 optlen = IP6OPT_RTALERT_LEN; 1140 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 1141 *rtalertp = ntohs(rtalert_val); 1142 break; 1143 case IP6OPT_JUMBO: 1144 /* XXX may need check for alignment */ 1145 if (hbhlen < IP6OPT_JUMBO_LEN) { 1146 V_ip6stat.ip6s_toosmall++; 1147 goto bad; 1148 } 1149 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 1150 /* XXX stat */ 1151 icmp6_error(m, ICMP6_PARAM_PROB, 1152 ICMP6_PARAMPROB_HEADER, 1153 erroff + opt + 1 - opthead); 1154 return (-1); 1155 } 1156 optlen = IP6OPT_JUMBO_LEN; 1157 1158 /* 1159 * IPv6 packets that have non 0 payload length 1160 * must not contain a jumbo payload option. 1161 */ 1162 ip6 = mtod(m, struct ip6_hdr *); 1163 if (ip6->ip6_plen) { 1164 V_ip6stat.ip6s_badoptions++; 1165 icmp6_error(m, ICMP6_PARAM_PROB, 1166 ICMP6_PARAMPROB_HEADER, 1167 erroff + opt - opthead); 1168 return (-1); 1169 } 1170 1171 /* 1172 * We may see jumbolen in unaligned location, so 1173 * we'd need to perform bcopy(). 1174 */ 1175 bcopy(opt + 2, &jumboplen, sizeof(jumboplen)); 1176 jumboplen = (u_int32_t)htonl(jumboplen); 1177 1178 #if 1 1179 /* 1180 * if there are multiple jumbo payload options, 1181 * *plenp will be non-zero and the packet will be 1182 * rejected. 1183 * the behavior may need some debate in ipngwg - 1184 * multiple options does not make sense, however, 1185 * there's no explicit mention in specification. 1186 */ 1187 if (*plenp != 0) { 1188 V_ip6stat.ip6s_badoptions++; 1189 icmp6_error(m, ICMP6_PARAM_PROB, 1190 ICMP6_PARAMPROB_HEADER, 1191 erroff + opt + 2 - opthead); 1192 return (-1); 1193 } 1194 #endif 1195 1196 /* 1197 * jumbo payload length must be larger than 65535. 1198 */ 1199 if (jumboplen <= IPV6_MAXPACKET) { 1200 V_ip6stat.ip6s_badoptions++; 1201 icmp6_error(m, ICMP6_PARAM_PROB, 1202 ICMP6_PARAMPROB_HEADER, 1203 erroff + opt + 2 - opthead); 1204 return (-1); 1205 } 1206 *plenp = jumboplen; 1207 1208 break; 1209 default: /* unknown option */ 1210 if (hbhlen < IP6OPT_MINLEN) { 1211 V_ip6stat.ip6s_toosmall++; 1212 goto bad; 1213 } 1214 optlen = ip6_unknown_opt(opt, m, 1215 erroff + opt - opthead); 1216 if (optlen == -1) 1217 return (-1); 1218 optlen += 2; 1219 break; 1220 } 1221 } 1222 1223 return (0); 1224 1225 bad: 1226 m_freem(m); 1227 return (-1); 1228 } 1229 1230 /* 1231 * Unknown option processing. 1232 * The third argument `off' is the offset from the IPv6 header to the option, 1233 * which is necessary if the IPv6 header the and option header and IPv6 header 1234 * is not contiguous in order to return an ICMPv6 error. 1235 */ 1236 int 1237 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off) 1238 { 1239 struct ip6_hdr *ip6; 1240 1241 switch (IP6OPT_TYPE(*optp)) { 1242 case IP6OPT_TYPE_SKIP: /* ignore the option */ 1243 return ((int)*(optp + 1)); 1244 case IP6OPT_TYPE_DISCARD: /* silently discard */ 1245 m_freem(m); 1246 return (-1); 1247 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 1248 V_ip6stat.ip6s_badoptions++; 1249 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 1250 return (-1); 1251 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 1252 V_ip6stat.ip6s_badoptions++; 1253 ip6 = mtod(m, struct ip6_hdr *); 1254 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1255 (m->m_flags & (M_BCAST|M_MCAST))) 1256 m_freem(m); 1257 else 1258 icmp6_error(m, ICMP6_PARAM_PROB, 1259 ICMP6_PARAMPROB_OPTION, off); 1260 return (-1); 1261 } 1262 1263 m_freem(m); /* XXX: NOTREACHED */ 1264 return (-1); 1265 } 1266 1267 /* 1268 * Create the "control" list for this pcb. 1269 * These functions will not modify mbuf chain at all. 1270 * 1271 * With KAME mbuf chain restriction: 1272 * The routine will be called from upper layer handlers like tcp6_input(). 1273 * Thus the routine assumes that the caller (tcp6_input) have already 1274 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 1275 * very first mbuf on the mbuf chain. 1276 * 1277 * ip6_savecontrol_v4 will handle those options that are possible to be 1278 * set on a v4-mapped socket. 1279 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those 1280 * options and handle the v6-only ones itself. 1281 */ 1282 struct mbuf ** 1283 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp, 1284 int *v4only) 1285 { 1286 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1287 1288 #ifdef SO_TIMESTAMP 1289 if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) { 1290 struct timeval tv; 1291 1292 microtime(&tv); 1293 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1294 SCM_TIMESTAMP, SOL_SOCKET); 1295 if (*mp) 1296 mp = &(*mp)->m_next; 1297 } 1298 #endif 1299 1300 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1301 if (v4only != NULL) 1302 *v4only = 1; 1303 return (mp); 1304 } 1305 1306 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y)) 1307 /* RFC 2292 sec. 5 */ 1308 if ((inp->inp_flags & IN6P_PKTINFO) != 0) { 1309 struct in6_pktinfo pi6; 1310 1311 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 1312 in6_clearscope(&pi6.ipi6_addr); /* XXX */ 1313 pi6.ipi6_ifindex = 1314 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0; 1315 1316 *mp = sbcreatecontrol((caddr_t) &pi6, 1317 sizeof(struct in6_pktinfo), 1318 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6); 1319 if (*mp) 1320 mp = &(*mp)->m_next; 1321 } 1322 1323 if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) { 1324 int hlim = ip6->ip6_hlim & 0xff; 1325 1326 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int), 1327 IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), 1328 IPPROTO_IPV6); 1329 if (*mp) 1330 mp = &(*mp)->m_next; 1331 } 1332 1333 if (v4only != NULL) 1334 *v4only = 0; 1335 return (mp); 1336 } 1337 1338 void 1339 ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp) 1340 { 1341 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1342 int v4only = 0; 1343 1344 mp = ip6_savecontrol_v4(in6p, m, mp, &v4only); 1345 if (v4only) 1346 return; 1347 1348 if ((in6p->inp_flags & IN6P_TCLASS) != 0) { 1349 u_int32_t flowinfo; 1350 int tclass; 1351 1352 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK); 1353 flowinfo >>= 20; 1354 1355 tclass = flowinfo & 0xff; 1356 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass), 1357 IPV6_TCLASS, IPPROTO_IPV6); 1358 if (*mp) 1359 mp = &(*mp)->m_next; 1360 } 1361 1362 /* 1363 * IPV6_HOPOPTS socket option. Recall that we required super-user 1364 * privilege for the option (see ip6_ctloutput), but it might be too 1365 * strict, since there might be some hop-by-hop options which can be 1366 * returned to normal user. 1367 * See also RFC 2292 section 6 (or RFC 3542 section 8). 1368 */ 1369 if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) { 1370 /* 1371 * Check if a hop-by-hop options header is contatined in the 1372 * received packet, and if so, store the options as ancillary 1373 * data. Note that a hop-by-hop options header must be 1374 * just after the IPv6 header, which is assured through the 1375 * IPv6 input processing. 1376 */ 1377 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 1378 struct ip6_hbh *hbh; 1379 int hbhlen = 0; 1380 #ifdef PULLDOWN_TEST 1381 struct mbuf *ext; 1382 #endif 1383 1384 #ifndef PULLDOWN_TEST 1385 hbh = (struct ip6_hbh *)(ip6 + 1); 1386 hbhlen = (hbh->ip6h_len + 1) << 3; 1387 #else 1388 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 1389 ip6->ip6_nxt); 1390 if (ext == NULL) { 1391 V_ip6stat.ip6s_tooshort++; 1392 return; 1393 } 1394 hbh = mtod(ext, struct ip6_hbh *); 1395 hbhlen = (hbh->ip6h_len + 1) << 3; 1396 if (hbhlen != ext->m_len) { 1397 m_freem(ext); 1398 V_ip6stat.ip6s_tooshort++; 1399 return; 1400 } 1401 #endif 1402 1403 /* 1404 * XXX: We copy the whole header even if a 1405 * jumbo payload option is included, the option which 1406 * is to be removed before returning according to 1407 * RFC2292. 1408 * Note: this constraint is removed in RFC3542 1409 */ 1410 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 1411 IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS), 1412 IPPROTO_IPV6); 1413 if (*mp) 1414 mp = &(*mp)->m_next; 1415 #ifdef PULLDOWN_TEST 1416 m_freem(ext); 1417 #endif 1418 } 1419 } 1420 1421 if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) { 1422 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 1423 1424 /* 1425 * Search for destination options headers or routing 1426 * header(s) through the header chain, and stores each 1427 * header as ancillary data. 1428 * Note that the order of the headers remains in 1429 * the chain of ancillary data. 1430 */ 1431 while (1) { /* is explicit loop prevention necessary? */ 1432 struct ip6_ext *ip6e = NULL; 1433 int elen; 1434 #ifdef PULLDOWN_TEST 1435 struct mbuf *ext = NULL; 1436 #endif 1437 1438 /* 1439 * if it is not an extension header, don't try to 1440 * pull it from the chain. 1441 */ 1442 switch (nxt) { 1443 case IPPROTO_DSTOPTS: 1444 case IPPROTO_ROUTING: 1445 case IPPROTO_HOPOPTS: 1446 case IPPROTO_AH: /* is it possible? */ 1447 break; 1448 default: 1449 goto loopend; 1450 } 1451 1452 #ifndef PULLDOWN_TEST 1453 if (off + sizeof(*ip6e) > m->m_len) 1454 goto loopend; 1455 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 1456 if (nxt == IPPROTO_AH) 1457 elen = (ip6e->ip6e_len + 2) << 2; 1458 else 1459 elen = (ip6e->ip6e_len + 1) << 3; 1460 if (off + elen > m->m_len) 1461 goto loopend; 1462 #else 1463 ext = ip6_pullexthdr(m, off, nxt); 1464 if (ext == NULL) { 1465 V_ip6stat.ip6s_tooshort++; 1466 return; 1467 } 1468 ip6e = mtod(ext, struct ip6_ext *); 1469 if (nxt == IPPROTO_AH) 1470 elen = (ip6e->ip6e_len + 2) << 2; 1471 else 1472 elen = (ip6e->ip6e_len + 1) << 3; 1473 if (elen != ext->m_len) { 1474 m_freem(ext); 1475 V_ip6stat.ip6s_tooshort++; 1476 return; 1477 } 1478 #endif 1479 1480 switch (nxt) { 1481 case IPPROTO_DSTOPTS: 1482 if (!(in6p->inp_flags & IN6P_DSTOPTS)) 1483 break; 1484 1485 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1486 IS2292(in6p, 1487 IPV6_2292DSTOPTS, IPV6_DSTOPTS), 1488 IPPROTO_IPV6); 1489 if (*mp) 1490 mp = &(*mp)->m_next; 1491 break; 1492 case IPPROTO_ROUTING: 1493 if (!(in6p->inp_flags & IN6P_RTHDR)) 1494 break; 1495 1496 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1497 IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR), 1498 IPPROTO_IPV6); 1499 if (*mp) 1500 mp = &(*mp)->m_next; 1501 break; 1502 case IPPROTO_HOPOPTS: 1503 case IPPROTO_AH: /* is it possible? */ 1504 break; 1505 1506 default: 1507 /* 1508 * other cases have been filtered in the above. 1509 * none will visit this case. here we supply 1510 * the code just in case (nxt overwritten or 1511 * other cases). 1512 */ 1513 #ifdef PULLDOWN_TEST 1514 m_freem(ext); 1515 #endif 1516 goto loopend; 1517 1518 } 1519 1520 /* proceed with the next header. */ 1521 off += elen; 1522 nxt = ip6e->ip6e_nxt; 1523 ip6e = NULL; 1524 #ifdef PULLDOWN_TEST 1525 m_freem(ext); 1526 ext = NULL; 1527 #endif 1528 } 1529 loopend: 1530 ; 1531 } 1532 } 1533 #undef IS2292 1534 1535 void 1536 ip6_notify_pmtu(struct inpcb *in6p, struct sockaddr_in6 *dst, u_int32_t *mtu) 1537 { 1538 struct socket *so; 1539 struct mbuf *m_mtu; 1540 struct ip6_mtuinfo mtuctl; 1541 1542 so = in6p->inp_socket; 1543 1544 if (mtu == NULL) 1545 return; 1546 1547 #ifdef DIAGNOSTIC 1548 if (so == NULL) /* I believe this is impossible */ 1549 panic("ip6_notify_pmtu: socket is NULL"); 1550 #endif 1551 1552 bzero(&mtuctl, sizeof(mtuctl)); /* zero-clear for safety */ 1553 mtuctl.ip6m_mtu = *mtu; 1554 mtuctl.ip6m_addr = *dst; 1555 if (sa6_recoverscope(&mtuctl.ip6m_addr)) 1556 return; 1557 1558 if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl), 1559 IPV6_PATHMTU, IPPROTO_IPV6)) == NULL) 1560 return; 1561 1562 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu) 1563 == 0) { 1564 m_freem(m_mtu); 1565 /* XXX: should count statistics */ 1566 } else 1567 sorwakeup(so); 1568 1569 return; 1570 } 1571 1572 #ifdef PULLDOWN_TEST 1573 /* 1574 * pull single extension header from mbuf chain. returns single mbuf that 1575 * contains the result, or NULL on error. 1576 */ 1577 static struct mbuf * 1578 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt) 1579 { 1580 struct ip6_ext ip6e; 1581 size_t elen; 1582 struct mbuf *n; 1583 1584 #ifdef DIAGNOSTIC 1585 switch (nxt) { 1586 case IPPROTO_DSTOPTS: 1587 case IPPROTO_ROUTING: 1588 case IPPROTO_HOPOPTS: 1589 case IPPROTO_AH: /* is it possible? */ 1590 break; 1591 default: 1592 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 1593 } 1594 #endif 1595 1596 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1597 if (nxt == IPPROTO_AH) 1598 elen = (ip6e.ip6e_len + 2) << 2; 1599 else 1600 elen = (ip6e.ip6e_len + 1) << 3; 1601 1602 MGET(n, M_DONTWAIT, MT_DATA); 1603 if (n && elen >= MLEN) { 1604 MCLGET(n, M_DONTWAIT); 1605 if ((n->m_flags & M_EXT) == 0) { 1606 m_free(n); 1607 n = NULL; 1608 } 1609 } 1610 if (!n) 1611 return NULL; 1612 1613 n->m_len = 0; 1614 if (elen >= M_TRAILINGSPACE(n)) { 1615 m_free(n); 1616 return NULL; 1617 } 1618 1619 m_copydata(m, off, elen, mtod(n, caddr_t)); 1620 n->m_len = elen; 1621 return n; 1622 } 1623 #endif 1624 1625 /* 1626 * Get pointer to the previous header followed by the header 1627 * currently processed. 1628 * XXX: This function supposes that 1629 * M includes all headers, 1630 * the next header field and the header length field of each header 1631 * are valid, and 1632 * the sum of each header length equals to OFF. 1633 * Because of these assumptions, this function must be called very 1634 * carefully. Moreover, it will not be used in the near future when 1635 * we develop `neater' mechanism to process extension headers. 1636 */ 1637 char * 1638 ip6_get_prevhdr(struct mbuf *m, int off) 1639 { 1640 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1641 1642 if (off == sizeof(struct ip6_hdr)) 1643 return (&ip6->ip6_nxt); 1644 else { 1645 int len, nxt; 1646 struct ip6_ext *ip6e = NULL; 1647 1648 nxt = ip6->ip6_nxt; 1649 len = sizeof(struct ip6_hdr); 1650 while (len < off) { 1651 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 1652 1653 switch (nxt) { 1654 case IPPROTO_FRAGMENT: 1655 len += sizeof(struct ip6_frag); 1656 break; 1657 case IPPROTO_AH: 1658 len += (ip6e->ip6e_len + 2) << 2; 1659 break; 1660 default: 1661 len += (ip6e->ip6e_len + 1) << 3; 1662 break; 1663 } 1664 nxt = ip6e->ip6e_nxt; 1665 } 1666 if (ip6e) 1667 return (&ip6e->ip6e_nxt); 1668 else 1669 return NULL; 1670 } 1671 } 1672 1673 /* 1674 * get next header offset. m will be retained. 1675 */ 1676 int 1677 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp) 1678 { 1679 struct ip6_hdr ip6; 1680 struct ip6_ext ip6e; 1681 struct ip6_frag fh; 1682 1683 /* just in case */ 1684 if (m == NULL) 1685 panic("ip6_nexthdr: m == NULL"); 1686 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 1687 return -1; 1688 1689 switch (proto) { 1690 case IPPROTO_IPV6: 1691 if (m->m_pkthdr.len < off + sizeof(ip6)) 1692 return -1; 1693 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 1694 if (nxtp) 1695 *nxtp = ip6.ip6_nxt; 1696 off += sizeof(ip6); 1697 return off; 1698 1699 case IPPROTO_FRAGMENT: 1700 /* 1701 * terminate parsing if it is not the first fragment, 1702 * it does not make sense to parse through it. 1703 */ 1704 if (m->m_pkthdr.len < off + sizeof(fh)) 1705 return -1; 1706 m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 1707 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */ 1708 if (fh.ip6f_offlg & IP6F_OFF_MASK) 1709 return -1; 1710 if (nxtp) 1711 *nxtp = fh.ip6f_nxt; 1712 off += sizeof(struct ip6_frag); 1713 return off; 1714 1715 case IPPROTO_AH: 1716 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1717 return -1; 1718 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1719 if (nxtp) 1720 *nxtp = ip6e.ip6e_nxt; 1721 off += (ip6e.ip6e_len + 2) << 2; 1722 return off; 1723 1724 case IPPROTO_HOPOPTS: 1725 case IPPROTO_ROUTING: 1726 case IPPROTO_DSTOPTS: 1727 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1728 return -1; 1729 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1730 if (nxtp) 1731 *nxtp = ip6e.ip6e_nxt; 1732 off += (ip6e.ip6e_len + 1) << 3; 1733 return off; 1734 1735 case IPPROTO_NONE: 1736 case IPPROTO_ESP: 1737 case IPPROTO_IPCOMP: 1738 /* give up */ 1739 return -1; 1740 1741 default: 1742 return -1; 1743 } 1744 1745 return -1; 1746 } 1747 1748 /* 1749 * get offset for the last header in the chain. m will be kept untainted. 1750 */ 1751 int 1752 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp) 1753 { 1754 int newoff; 1755 int nxt; 1756 1757 if (!nxtp) { 1758 nxt = -1; 1759 nxtp = &nxt; 1760 } 1761 while (1) { 1762 newoff = ip6_nexthdr(m, off, proto, nxtp); 1763 if (newoff < 0) 1764 return off; 1765 else if (newoff < off) 1766 return -1; /* invalid */ 1767 else if (newoff == off) 1768 return newoff; 1769 1770 off = newoff; 1771 proto = *nxtp; 1772 } 1773 } 1774 1775 struct ip6aux * 1776 ip6_addaux(struct mbuf *m) 1777 { 1778 struct m_tag *mtag; 1779 1780 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1781 if (!mtag) { 1782 mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux), 1783 M_NOWAIT); 1784 if (mtag) { 1785 m_tag_prepend(m, mtag); 1786 bzero(mtag + 1, sizeof(struct ip6aux)); 1787 } 1788 } 1789 return mtag ? (struct ip6aux *)(mtag + 1) : NULL; 1790 } 1791 1792 struct ip6aux * 1793 ip6_findaux(struct mbuf *m) 1794 { 1795 struct m_tag *mtag; 1796 1797 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1798 return mtag ? (struct ip6aux *)(mtag + 1) : NULL; 1799 } 1800 1801 void 1802 ip6_delaux(struct mbuf *m) 1803 { 1804 struct m_tag *mtag; 1805 1806 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1807 if (mtag) 1808 m_tag_delete(m, mtag); 1809 } 1810 1811 /* 1812 * System control for IP6 1813 */ 1814 1815 u_char inet6ctlerrmap[PRC_NCMDS] = { 1816 0, 0, 0, 0, 1817 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1818 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1819 EMSGSIZE, EHOSTUNREACH, 0, 0, 1820 0, 0, 0, 0, 1821 ENOPROTOOPT 1822 }; 1823