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