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