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