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