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