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 63 #include "opt_inet.h" 64 #include "opt_inet6.h" 65 #include "opt_ipsec.h" 66 #include "opt_route.h" 67 #include "opt_rss.h" 68 #include "opt_sctp.h" 69 70 #include <sys/param.h> 71 #include <sys/systm.h> 72 #include <sys/hhook.h> 73 #include <sys/malloc.h> 74 #include <sys/mbuf.h> 75 #include <sys/proc.h> 76 #include <sys/domain.h> 77 #include <sys/protosw.h> 78 #include <sys/sdt.h> 79 #include <sys/socket.h> 80 #include <sys/socketvar.h> 81 #include <sys/errno.h> 82 #include <sys/time.h> 83 #include <sys/kernel.h> 84 #include <sys/lock.h> 85 #include <sys/rmlock.h> 86 #include <sys/syslog.h> 87 #include <sys/sysctl.h> 88 #include <sys/eventhandler.h> 89 90 #include <net/if.h> 91 #include <net/if_var.h> 92 #include <net/if_types.h> 93 #include <net/if_private.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/ip_encap.h> 114 #include <netinet/in_pcb.h> 115 #include <netinet/icmp6.h> 116 #include <netinet6/scope6_var.h> 117 #include <netinet6/in6_ifattach.h> 118 #include <netinet6/mld6_var.h> 119 #include <netinet6/nd6.h> 120 #include <netinet6/in6_rss.h> 121 #include <netinet6/ip6_mroute.h> 122 #ifdef SCTP 123 #include <netinet/sctp_pcb.h> 124 #include <netinet6/sctp6_var.h> 125 #endif 126 127 #include <netipsec/ipsec_support.h> 128 129 ip6proto_input_t *ip6_protox[IPPROTO_MAX] = { 130 [0 ... IPPROTO_MAX - 1] = rip6_input }; 131 ip6proto_ctlinput_t *ip6_ctlprotox[IPPROTO_MAX] = { 132 [0 ... IPPROTO_MAX - 1] = rip6_ctlinput }; 133 134 VNET_DEFINE(struct in6_ifaddrhead, in6_ifaddrhead); 135 VNET_DEFINE(struct in6_ifaddrlisthead *, in6_ifaddrhashtbl); 136 VNET_DEFINE(u_long, in6_ifaddrhmask); 137 138 static struct netisr_handler ip6_nh = { 139 .nh_name = "ip6", 140 .nh_handler = ip6_input, 141 .nh_proto = NETISR_IPV6, 142 #ifdef RSS 143 .nh_m2cpuid = rss_soft_m2cpuid_v6, 144 .nh_policy = NETISR_POLICY_CPU, 145 .nh_dispatch = NETISR_DISPATCH_HYBRID, 146 #else 147 .nh_policy = NETISR_POLICY_FLOW, 148 #endif 149 }; 150 151 static int 152 sysctl_netinet6_intr_queue_maxlen(SYSCTL_HANDLER_ARGS) 153 { 154 int error, qlimit; 155 156 netisr_getqlimit(&ip6_nh, &qlimit); 157 error = sysctl_handle_int(oidp, &qlimit, 0, req); 158 if (error || !req->newptr) 159 return (error); 160 if (qlimit < 1) 161 return (EINVAL); 162 return (netisr_setqlimit(&ip6_nh, qlimit)); 163 } 164 SYSCTL_DECL(_net_inet6_ip6); 165 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRQMAXLEN, intr_queue_maxlen, 166 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 167 0, 0, sysctl_netinet6_intr_queue_maxlen, "I", 168 "Maximum size of the IPv6 input queue"); 169 170 VNET_DEFINE_STATIC(bool, ip6_sav) = true; 171 #define V_ip6_sav VNET(ip6_sav) 172 SYSCTL_BOOL(_net_inet6_ip6, OID_AUTO, source_address_validation, 173 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_sav), true, 174 "Drop incoming packets with source address that is a local address"); 175 176 SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, temp_max_desync_factor, 177 CTLFLAG_RD | CTLFLAG_VNET, 178 &VNET_NAME(ip6_temp_max_desync_factor), 0, 179 "RFC 8981 max desync factor"); 180 181 #ifdef RSS 182 static struct netisr_handler ip6_direct_nh = { 183 .nh_name = "ip6_direct", 184 .nh_handler = ip6_direct_input, 185 .nh_proto = NETISR_IPV6_DIRECT, 186 .nh_m2cpuid = rss_soft_m2cpuid_v6, 187 .nh_policy = NETISR_POLICY_CPU, 188 .nh_dispatch = NETISR_DISPATCH_HYBRID, 189 }; 190 191 static int 192 sysctl_netinet6_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS) 193 { 194 int error, qlimit; 195 196 netisr_getqlimit(&ip6_direct_nh, &qlimit); 197 error = sysctl_handle_int(oidp, &qlimit, 0, req); 198 if (error || !req->newptr) 199 return (error); 200 if (qlimit < 1) 201 return (EINVAL); 202 return (netisr_setqlimit(&ip6_direct_nh, qlimit)); 203 } 204 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRDQMAXLEN, intr_direct_queue_maxlen, 205 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 206 0, 0, sysctl_netinet6_intr_direct_queue_maxlen, "I", 207 "Maximum size of the IPv6 direct input queue"); 208 209 #endif 210 211 VNET_DEFINE(pfil_head_t, inet6_pfil_head); 212 VNET_DEFINE(pfil_head_t, inet6_local_pfil_head); 213 214 VNET_PCPUSTAT_DEFINE(struct ip6stat, ip6stat); 215 VNET_PCPUSTAT_SYSINIT(ip6stat); 216 #ifdef VIMAGE 217 VNET_PCPUSTAT_SYSUNINIT(ip6stat); 218 #endif /* VIMAGE */ 219 220 struct rmlock in6_ifaddr_lock; 221 RM_SYSINIT(in6_ifaddr_lock, &in6_ifaddr_lock, "in6_ifaddr_lock"); 222 223 static int ip6_hopopts_input(u_int32_t *, struct mbuf **, int *); 224 225 /* 226 * IP6 initialization: fill in IP6 protocol switch table. 227 * All protocols not implemented in kernel go to raw IP6 protocol handler. 228 */ 229 static void 230 ip6_vnet_init(void *arg __unused) 231 { 232 struct pfil_head_args args; 233 234 TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal", 235 &V_ip6_auto_linklocal); 236 TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv); 237 TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr); 238 TUNABLE_BOOL_FETCH("net.inet6.ip6.use_stableaddr", &V_ip6_use_stableaddr); 239 240 CK_STAILQ_INIT(&V_in6_ifaddrhead); 241 V_in6_ifaddrhashtbl = hashinit(IN6ADDR_NHASH, M_IFADDR, 242 &V_in6_ifaddrhmask); 243 244 /* Initialize packet filter hooks. */ 245 args.pa_version = PFIL_VERSION; 246 args.pa_flags = PFIL_IN | PFIL_OUT; 247 args.pa_type = PFIL_TYPE_IP6; 248 args.pa_headname = PFIL_INET6_NAME; 249 V_inet6_pfil_head = pfil_head_register(&args); 250 251 args.pa_flags = PFIL_OUT; 252 args.pa_headname = PFIL_INET6_LOCAL_NAME; 253 V_inet6_local_pfil_head = pfil_head_register(&args); 254 255 if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET6, 256 &V_ipsec_hhh_in[HHOOK_IPSEC_INET6], 257 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0) 258 printf("%s: WARNING: unable to register input helper hook\n", 259 __func__); 260 if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET6, 261 &V_ipsec_hhh_out[HHOOK_IPSEC_INET6], 262 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0) 263 printf("%s: WARNING: unable to register output helper hook\n", 264 __func__); 265 266 scope6_init(); 267 addrsel_policy_init(); 268 nd6_init(); 269 frag6_init(); 270 271 V_ip6_temp_max_desync_factor = TEMP_MAX_DESYNC_FACTOR_BASE + 272 (V_ip6_temp_preferred_lifetime >> 2) + 273 (V_ip6_temp_preferred_lifetime >> 3); 274 V_ip6_desync_factor = arc4random() % V_ip6_temp_max_desync_factor; 275 276 /* Skip global initialization stuff for non-default instances. */ 277 #ifdef VIMAGE 278 netisr_register_vnet(&ip6_nh); 279 #ifdef RSS 280 netisr_register_vnet(&ip6_direct_nh); 281 #endif 282 #endif 283 } 284 VNET_SYSINIT(ip6_vnet_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, 285 ip6_vnet_init, NULL); 286 287 static void 288 ip6_init(void *arg __unused) 289 { 290 struct ifnet *ifp; 291 292 /* 293 * Register statically those protocols that are unlikely to ever go 294 * dynamic. 295 */ 296 IP6PROTO_REGISTER(IPPROTO_ICMPV6, icmp6_input, rip6_ctlinput); 297 IP6PROTO_REGISTER(IPPROTO_DSTOPTS, dest6_input, NULL); 298 IP6PROTO_REGISTER(IPPROTO_ROUTING, route6_input, NULL); 299 IP6PROTO_REGISTER(IPPROTO_FRAGMENT, frag6_input, NULL); 300 IP6PROTO_REGISTER(IPPROTO_IPV4, encap6_input, NULL); 301 IP6PROTO_REGISTER(IPPROTO_IPV6, encap6_input, NULL); 302 IP6PROTO_REGISTER(IPPROTO_ETHERIP, encap6_input, NULL); 303 IP6PROTO_REGISTER(IPPROTO_GRE, encap6_input, NULL); 304 IP6PROTO_REGISTER(IPPROTO_PIM, encap6_input, NULL); 305 #ifdef SCTP /* XXX: has a loadable & static version */ 306 IP6PROTO_REGISTER(IPPROTO_SCTP, sctp6_input, sctp6_ctlinput); 307 #endif 308 309 EVENTHANDLER_REGISTER(vm_lowmem, frag6_drain, NULL, LOWMEM_PRI_DEFAULT); 310 EVENTHANDLER_REGISTER(mbuf_lowmem, frag6_drain, NULL, 311 LOWMEM_PRI_DEFAULT); 312 313 netisr_register(&ip6_nh); 314 #ifdef RSS 315 netisr_register(&ip6_direct_nh); 316 #endif 317 /* 318 * XXXGL: we use SYSINIT() here, but go over V_ifnet. See comment 319 * in sys/netinet/ip_input.c:ip_init(). 320 */ 321 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) 322 in6_ifarrival(NULL, ifp); 323 } 324 SYSINIT(ip6_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_init, NULL); 325 326 int 327 ip6proto_register(uint8_t proto, ip6proto_input_t input, 328 ip6proto_ctlinput_t ctl) 329 { 330 331 MPASS(proto > 0); 332 333 if (ip6_protox[proto] == rip6_input) { 334 ip6_protox[proto] = input; 335 ip6_ctlprotox[proto] = ctl; 336 return (0); 337 } else 338 return (EEXIST); 339 } 340 341 int 342 ip6proto_unregister(uint8_t proto) 343 { 344 345 MPASS(proto > 0); 346 347 if (ip6_protox[proto] != rip6_input) { 348 ip6_protox[proto] = rip6_input; 349 ip6_ctlprotox[proto] = rip6_ctlinput; 350 return (0); 351 } else 352 return (ENOENT); 353 } 354 355 #ifdef VIMAGE 356 static void 357 ip6_destroy(void *unused __unused) 358 { 359 struct ifaddr *ifa, *nifa; 360 struct ifnet *ifp; 361 int error; 362 363 #ifdef RSS 364 netisr_unregister_vnet(&ip6_direct_nh); 365 #endif 366 netisr_unregister_vnet(&ip6_nh); 367 368 pfil_head_unregister(V_inet6_pfil_head); 369 error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET6]); 370 if (error != 0) { 371 printf("%s: WARNING: unable to deregister input helper hook " 372 "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET6: " 373 "error %d returned\n", __func__, error); 374 } 375 error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET6]); 376 if (error != 0) { 377 printf("%s: WARNING: unable to deregister output helper hook " 378 "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET6: " 379 "error %d returned\n", __func__, error); 380 } 381 382 /* Cleanup addresses. */ 383 IFNET_RLOCK(); 384 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 385 /* Cannot lock here - lock recursion. */ 386 /* IF_ADDR_LOCK(ifp); */ 387 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) { 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 } 395 IFNET_RUNLOCK(); 396 397 /* Make sure any routes are gone as well. */ 398 rib_flush_routes_family(AF_INET6); 399 400 frag6_destroy(); 401 nd6_destroy(); 402 in6_ifattach_destroy(); 403 404 hashdestroy(V_in6_ifaddrhashtbl, M_IFADDR, V_in6_ifaddrhmask); 405 } 406 407 VNET_SYSUNINIT(inet6, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_destroy, NULL); 408 #endif 409 410 static int 411 ip6_input_hbh(struct mbuf **mp, uint32_t *rtalert, int *off, 412 int *nxt, int *ours) 413 { 414 struct mbuf *m; 415 struct ip6_hdr *ip6; 416 struct ip6_hbh *hbh; 417 418 if (ip6_hopopts_input(rtalert, mp, off)) { 419 #if 0 /*touches NULL pointer*/ 420 in6_ifstat_inc((*mp)->m_pkthdr.rcvif, ifs6_in_discard); 421 #endif 422 goto out; /* m have already been freed */ 423 } 424 425 /* adjust pointer */ 426 m = *mp; 427 ip6 = mtod(m, struct ip6_hdr *); 428 429 /* 430 * If the payload length field is 0 and the next header field indicates 431 * Hop-by-Hop Options header, then a Jumbo Payload option MUST be 432 * included. We no not support Jumbo Payloads so report an error. 433 */ 434 if (ip6->ip6_plen == 0) { 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 = ip6_protox[nxt](&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 ((rcvif->if_inet6->nd_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 struct ifnet *ifp = (m->m_flags & M_LOOP) ? V_loif : rcvif; 583 int ifindex = ifp->if_index; 584 if (ifindex >= IP6S_M2MMAX) 585 ifindex = 0; 586 IP6STAT_INC2(ip6s_m2m, ifindex); 587 } else 588 IP6STAT_INC(ip6s_m1); 589 } 590 591 in6_ifstat_inc(rcvif, ifs6_in_receive); 592 IP6STAT_INC(ip6s_total); 593 594 /* 595 * L2 bridge code and some other code can return mbuf chain 596 * that does not conform to KAME requirement. too bad. 597 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 598 */ 599 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 600 struct mbuf *n; 601 602 if (m->m_pkthdr.len > MHLEN) 603 n = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 604 else 605 n = m_gethdr(M_NOWAIT, MT_DATA); 606 if (n == NULL) 607 goto bad; 608 609 m_move_pkthdr(n, m); 610 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t)); 611 n->m_len = n->m_pkthdr.len; 612 m_freem(m); 613 m = n; 614 } 615 if (m->m_len < sizeof(struct ip6_hdr)) { 616 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 617 IP6STAT_INC(ip6s_toosmall); 618 in6_ifstat_inc(rcvif, ifs6_in_hdrerr); 619 goto bad; 620 } 621 } 622 623 ip6 = mtod(m, struct ip6_hdr *); 624 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 625 IP6STAT_INC(ip6s_badvers); 626 in6_ifstat_inc(rcvif, ifs6_in_hdrerr); 627 goto bad; 628 } 629 630 IP6STAT_INC2(ip6s_nxthist, ip6->ip6_nxt); 631 IP_PROBE(receive, NULL, NULL, ip6, rcvif, NULL, ip6); 632 633 /* 634 * Check against address spoofing/corruption. The unspecified address 635 * is checked further below. 636 */ 637 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 638 /* 639 * XXX: "badscope" is not very suitable for a multicast source. 640 */ 641 IP6STAT_INC(ip6s_badscope); 642 in6_ifstat_inc(rcvif, ifs6_in_addrerr); 643 goto bad; 644 } 645 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) && 646 !(m->m_flags & M_LOOP)) { 647 /* 648 * In this case, the packet should come from the loopback 649 * interface. However, we cannot just check the if_flags, 650 * because ip6_mloopback() passes the "actual" interface 651 * as the outgoing/incoming interface. 652 */ 653 IP6STAT_INC(ip6s_badscope); 654 in6_ifstat_inc(rcvif, ifs6_in_addrerr); 655 goto bad; 656 } 657 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 658 IPV6_ADDR_MC_SCOPE(&ip6->ip6_dst) == 0) { 659 /* 660 * RFC4291 2.7: 661 * Nodes must not originate a packet to a multicast address 662 * whose scop field contains the reserved value 0; if such 663 * a packet is received, it must be silently dropped. 664 */ 665 IP6STAT_INC(ip6s_badscope); 666 in6_ifstat_inc(rcvif, ifs6_in_addrerr); 667 goto bad; 668 } 669 /* 670 * The following check is not documented in specs. A malicious 671 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack 672 * and bypass security checks (act as if it was from 127.0.0.1 by using 673 * IPv6 src ::ffff:127.0.0.1). Be cautious. 674 * 675 * We have supported IPv6-only kernels for a few years and this issue 676 * has not come up. The world seems to move mostly towards not using 677 * v4mapped on the wire, so it makes sense for us to keep rejecting 678 * any such packets. 679 */ 680 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 681 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 682 IP6STAT_INC(ip6s_badscope); 683 in6_ifstat_inc(rcvif, ifs6_in_addrerr); 684 goto bad; 685 } 686 /* 687 * Try to forward the packet, but if we fail continue. 688 * ip6_tryforward() does not generate redirects, so fall 689 * through to normal processing if redirects are required. 690 * ip6_tryforward() does inbound and outbound packet firewall 691 * processing. If firewall has decided that destination becomes 692 * our local address, it sets M_FASTFWD_OURS flag. In this 693 * case skip another inbound firewall processing and update 694 * ip6 pointer. 695 */ 696 if (V_ip6_forwarding != 0 && V_ip6_sendredirects == 0 697 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 698 && (!IPSEC_ENABLED(ipv6) || 699 IPSEC_CAPS(ipv6, m, IPSEC_CAP_OPERABLE) == 0) 700 #endif 701 ) { 702 if ((m = ip6_tryforward(m)) == NULL) 703 return; 704 if (m->m_flags & M_FASTFWD_OURS) { 705 ip6 = mtod(m, struct ip6_hdr *); 706 goto passin; 707 } 708 } 709 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 710 /* 711 * Bypass packet filtering for packets previously handled by IPsec. 712 */ 713 if (IPSEC_ENABLED(ipv6) && 714 IPSEC_CAPS(ipv6, m, IPSEC_CAP_BYPASS_FILTER) != 0) 715 goto passin; 716 #endif 717 /* 718 * Run through list of hooks for input packets. 719 * 720 * NB: Beware of the destination address changing 721 * (e.g. by NAT rewriting). When this happens, 722 * tell ip6_forward to do the right thing. 723 */ 724 725 /* Jump over all PFIL processing if hooks are not active. */ 726 if (!PFIL_HOOKED_IN(V_inet6_pfil_head)) 727 goto passin; 728 729 odst = ip6->ip6_dst; 730 if (pfil_mbuf_in(V_inet6_pfil_head, &m, m->m_pkthdr.rcvif, 731 NULL) != PFIL_PASS) 732 return; 733 ip6 = mtod(m, struct ip6_hdr *); 734 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst); 735 if ((m->m_flags & (M_IP6_NEXTHOP | M_FASTFWD_OURS)) == M_IP6_NEXTHOP && 736 m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) { 737 /* 738 * Directly ship the packet on. This allows forwarding 739 * packets originally destined to us to some other directly 740 * connected host. 741 */ 742 ip6_forward(m, 1); 743 return; 744 } 745 746 passin: 747 /* 748 * The check is deferred to here to give firewalls a chance to block 749 * (and log) such packets. ip6_tryforward() will not process such 750 * packets. 751 */ 752 if (__predict_false(IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst))) { 753 IP6STAT_INC(ip6s_badscope); 754 in6_ifstat_inc(rcvif, ifs6_in_addrerr); 755 goto bad; 756 } 757 758 plen = (uint32_t)ntohs(ip6->ip6_plen); 759 760 /* 761 * We don't support Jumbograms, reject packets with plen == 0 as early 762 * as we can. 763 */ 764 if (plen == 0) 765 goto bad; 766 767 /* 768 * Disambiguate address scope zones (if there is ambiguity). 769 * We first make sure that the original source or destination address 770 * is not in our internal form for scoped addresses. Such addresses 771 * are not necessarily invalid spec-wise, but we cannot accept them due 772 * to the usage conflict. 773 * in6_setscope() then also checks and rejects the cases where src or 774 * dst are the loopback address and the receiving interface 775 * is not loopback. 776 */ 777 if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) { 778 IP6STAT_INC(ip6s_badscope); /* XXX */ 779 goto bad; 780 } 781 if (in6_setscope(&ip6->ip6_src, rcvif, NULL) || 782 in6_setscope(&ip6->ip6_dst, rcvif, NULL)) { 783 IP6STAT_INC(ip6s_badscope); 784 goto bad; 785 } 786 if (m->m_flags & M_FASTFWD_OURS) { 787 m->m_flags &= ~M_FASTFWD_OURS; 788 ours = 1; 789 goto hbhcheck; 790 } 791 /* 792 * Multicast check. Assume packet is for us to avoid 793 * prematurely taking locks. 794 */ 795 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 796 ours = 1; 797 in6_ifstat_inc(rcvif, ifs6_in_mcast); 798 goto hbhcheck; 799 } 800 /* 801 * Unicast check 802 * XXX: For now we keep link-local IPv6 addresses with embedded 803 * scope zone id, therefore we use zero zoneid here. 804 */ 805 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false); 806 if (ia != NULL) { 807 if (ia->ia6_flags & IN6_IFF_NOTREADY) { 808 char ip6bufs[INET6_ADDRSTRLEN]; 809 char ip6bufd[INET6_ADDRSTRLEN]; 810 /* address is not ready, so discard the packet. */ 811 nd6log((LOG_INFO, 812 "ip6_input: packet to an unready address %s->%s\n", 813 ip6_sprintf(ip6bufs, &ip6->ip6_src), 814 ip6_sprintf(ip6bufd, &ip6->ip6_dst))); 815 goto bad; 816 } 817 if (V_ip6_sav && !(m->m_flags & M_LOOP) && 818 __predict_false(in6_localip_fib(&ip6->ip6_src, 819 rcvif->if_fib))) { 820 IP6STAT_INC(ip6s_badscope); /* XXX */ 821 goto bad; 822 } 823 /* Count the packet in the ip address stats */ 824 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1); 825 counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len); 826 ours = 1; 827 goto hbhcheck; 828 } 829 830 /* 831 * Now there is no reason to process the packet if it's not our own 832 * and we're not a router. 833 */ 834 if (!V_ip6_forwarding) { 835 IP6STAT_INC(ip6s_cantforward); 836 goto bad; 837 } 838 839 hbhcheck: 840 /* 841 * Process Hop-by-Hop options header if it's contained. 842 * m may be modified in ip6_hopopts_input(). 843 */ 844 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 845 if (ip6_input_hbh(&m, &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_mrouting_enabled && 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 * We are going to ship the packet to the local protocol stack. Call the 903 * filter again for this 'output' action, allowing redirect-like rules 904 * to adjust the source address. 905 */ 906 if (PFIL_HOOKED_OUT(V_inet6_local_pfil_head)) { 907 if (pfil_mbuf_out(V_inet6_local_pfil_head, &m, V_loif, NULL) != 908 PFIL_PASS) 909 return; 910 ip6 = mtod(m, struct ip6_hdr *); 911 } 912 913 /* 914 * Tell launch routine the next header 915 */ 916 IP6STAT_INC(ip6s_delivered); 917 in6_ifstat_inc(rcvif, ifs6_in_deliver); 918 nest = 0; 919 920 while (nxt != IPPROTO_DONE) { 921 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) { 922 IP6STAT_INC(ip6s_toomanyhdr); 923 goto bad; 924 } 925 926 /* 927 * protection against faulty packet - there should be 928 * more sanity checks in header chain processing. 929 */ 930 if (m->m_pkthdr.len < off) { 931 IP6STAT_INC(ip6s_tooshort); 932 in6_ifstat_inc(rcvif, ifs6_in_truncated); 933 goto bad; 934 } 935 936 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 937 if (IPSEC_ENABLED(ipv6)) { 938 if (IPSEC_INPUT(ipv6, m, off, nxt) != 0) 939 return; 940 } 941 #endif /* IPSEC */ 942 943 nxt = ip6_protox[nxt](&m, &off, nxt); 944 } 945 return; 946 bad: 947 in6_ifstat_inc(rcvif, ifs6_in_discard); 948 if (m != NULL) 949 m_freem(m); 950 } 951 952 /* 953 * Hop-by-Hop options header processing. If a valid jumbo payload option is 954 * included report an error. 955 * 956 * rtalertp - XXX: should be stored more smart way 957 */ 958 static int 959 ip6_hopopts_input(u_int32_t *rtalertp, struct mbuf **mp, int *offp) 960 { 961 struct mbuf *m = *mp; 962 int off = *offp, hbhlen; 963 struct ip6_hbh *hbh; 964 965 /* validation of the length of the header */ 966 if (m->m_len < off + sizeof(*hbh)) { 967 m = m_pullup(m, off + sizeof(*hbh)); 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 hbhlen = (hbh->ip6h_len + 1) << 3; 976 977 if (m->m_len < off + hbhlen) { 978 m = m_pullup(m, off + hbhlen); 979 if (m == NULL) { 980 IP6STAT_INC(ip6s_exthdrtoolong); 981 *mp = NULL; 982 return (-1); 983 } 984 } 985 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 986 off += hbhlen; 987 hbhlen -= sizeof(struct ip6_hbh); 988 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 989 hbhlen, rtalertp) < 0) { 990 *mp = NULL; 991 return (-1); 992 } 993 994 *offp = off; 995 *mp = m; 996 return (0); 997 } 998 999 /* 1000 * Search header for all Hop-by-hop options and process each option. 1001 * This function is separate from ip6_hopopts_input() in order to 1002 * handle a case where the sending node itself process its hop-by-hop 1003 * options header. In such a case, the function is called from ip6_output(). 1004 * 1005 * The function assumes that hbh header is located right after the IPv6 header 1006 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 1007 * opthead + hbhlen is located in contiguous memory region. 1008 */ 1009 int 1010 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen, 1011 u_int32_t *rtalertp) 1012 { 1013 int optlen = 0; 1014 u_int8_t *opt = opthead; 1015 u_int16_t rtalert_val; 1016 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 1017 1018 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 1019 switch (*opt) { 1020 case IP6OPT_PAD1: 1021 optlen = 1; 1022 break; 1023 case IP6OPT_PADN: 1024 if (hbhlen < IP6OPT_MINLEN) { 1025 IP6STAT_INC(ip6s_toosmall); 1026 goto bad; 1027 } 1028 optlen = *(opt + 1) + 2; 1029 break; 1030 case IP6OPT_ROUTER_ALERT: 1031 /* XXX may need check for alignment */ 1032 if (hbhlen < IP6OPT_RTALERT_LEN) { 1033 IP6STAT_INC(ip6s_toosmall); 1034 goto bad; 1035 } 1036 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 1037 /* XXX stat */ 1038 icmp6_error(m, ICMP6_PARAM_PROB, 1039 ICMP6_PARAMPROB_HEADER, 1040 erroff + opt + 1 - opthead); 1041 return (-1); 1042 } 1043 optlen = IP6OPT_RTALERT_LEN; 1044 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 1045 *rtalertp = ntohs(rtalert_val); 1046 break; 1047 case IP6OPT_JUMBO: 1048 /* We do not support the Jumbo Payload option. */ 1049 goto bad; 1050 default: /* unknown option */ 1051 if (hbhlen < IP6OPT_MINLEN) { 1052 IP6STAT_INC(ip6s_toosmall); 1053 goto bad; 1054 } 1055 optlen = ip6_unknown_opt(opt, m, 1056 erroff + opt - opthead); 1057 if (optlen == -1) 1058 return (-1); 1059 optlen += 2; 1060 break; 1061 } 1062 } 1063 1064 return (0); 1065 1066 bad: 1067 m_freem(m); 1068 return (-1); 1069 } 1070 1071 /* 1072 * Unknown option processing. 1073 * The third argument `off' is the offset from the IPv6 header to the option, 1074 * which is necessary if the IPv6 header the and option header and IPv6 header 1075 * is not contiguous in order to return an ICMPv6 error. 1076 */ 1077 int 1078 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off) 1079 { 1080 struct ip6_hdr *ip6; 1081 1082 switch (IP6OPT_TYPE(*optp)) { 1083 case IP6OPT_TYPE_SKIP: /* ignore the option */ 1084 return ((int)*(optp + 1)); 1085 case IP6OPT_TYPE_DISCARD: /* silently discard */ 1086 m_freem(m); 1087 return (-1); 1088 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 1089 IP6STAT_INC(ip6s_badoptions); 1090 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 1091 return (-1); 1092 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 1093 IP6STAT_INC(ip6s_badoptions); 1094 ip6 = mtod(m, struct ip6_hdr *); 1095 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1096 (m->m_flags & (M_BCAST|M_MCAST))) 1097 m_freem(m); 1098 else 1099 icmp6_error(m, ICMP6_PARAM_PROB, 1100 ICMP6_PARAMPROB_OPTION, off); 1101 return (-1); 1102 } 1103 1104 m_freem(m); /* XXX: NOTREACHED */ 1105 return (-1); 1106 } 1107 1108 /* 1109 * Create the "control" list for this pcb. 1110 * These functions will not modify mbuf chain at all. 1111 * 1112 * The routine will be called from upper layer handlers like tcp6_input(). 1113 * Thus the routine assumes that the caller (tcp6_input) have already 1114 * called m_pullup() and all the extension headers are located in the 1115 * very first mbuf on the mbuf chain. 1116 * 1117 * ip6_savecontrol_v4 will handle those options that are possible to be 1118 * set on a v4-mapped socket. 1119 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those 1120 * options and handle the v6-only ones itself. 1121 */ 1122 struct mbuf ** 1123 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp, 1124 int *v4only) 1125 { 1126 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1127 1128 #if defined(SO_TIMESTAMP) && defined(SO_BINTIME) 1129 if ((inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) != 0) { 1130 union { 1131 struct timeval tv; 1132 struct bintime bt; 1133 struct timespec ts; 1134 } t; 1135 struct bintime boottimebin, bt1; 1136 struct timespec ts1; 1137 int ts_clock; 1138 bool stamped; 1139 1140 ts_clock = inp->inp_socket->so_ts_clock; 1141 stamped = false; 1142 1143 /* 1144 * Handle BINTIME first. We create the same output options 1145 * for both SO_BINTIME and the case where SO_TIMESTAMP is 1146 * set with the timestamp clock set to SO_TS_BINTIME. 1147 */ 1148 if ((inp->inp_socket->so_options & SO_BINTIME) != 0 || 1149 ts_clock == SO_TS_BINTIME) { 1150 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR | 1151 M_TSTMP)) { 1152 mbuf_tstmp2timespec(m, &ts1); 1153 timespec2bintime(&ts1, &t.bt); 1154 getboottimebin(&boottimebin); 1155 bintime_add(&t.bt, &boottimebin); 1156 } else { 1157 bintime(&t.bt); 1158 } 1159 *mp = sbcreatecontrol(&t.bt, sizeof(t.bt), SCM_BINTIME, 1160 SOL_SOCKET, M_NOWAIT); 1161 if (*mp != NULL) { 1162 mp = &(*mp)->m_next; 1163 stamped = true; 1164 } 1165 1166 /* 1167 * Suppress other timestamps if SO_TIMESTAMP is not 1168 * set. 1169 */ 1170 if ((inp->inp_socket->so_options & SO_TIMESTAMP) == 0) 1171 ts_clock = SO_TS_BINTIME; 1172 } 1173 1174 switch (ts_clock) { 1175 case SO_TS_REALTIME_MICRO: 1176 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR | 1177 M_TSTMP)) { 1178 mbuf_tstmp2timespec(m, &ts1); 1179 timespec2bintime(&ts1, &bt1); 1180 getboottimebin(&boottimebin); 1181 bintime_add(&bt1, &boottimebin); 1182 bintime2timeval(&bt1, &t.tv); 1183 } else { 1184 microtime(&t.tv); 1185 } 1186 *mp = sbcreatecontrol(&t.tv, sizeof(t.tv), 1187 SCM_TIMESTAMP, SOL_SOCKET, M_NOWAIT); 1188 if (*mp != NULL) { 1189 mp = &(*mp)->m_next; 1190 stamped = true; 1191 } 1192 break; 1193 1194 case SO_TS_BINTIME: 1195 break; 1196 1197 case SO_TS_REALTIME: 1198 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR | 1199 M_TSTMP)) { 1200 mbuf_tstmp2timespec(m, &t.ts); 1201 getboottimebin(&boottimebin); 1202 bintime2timespec(&boottimebin, &ts1); 1203 timespecadd(&t.ts, &ts1, &t.ts); 1204 } else { 1205 nanotime(&t.ts); 1206 } 1207 *mp = sbcreatecontrol(&t.ts, sizeof(t.ts), 1208 SCM_REALTIME, SOL_SOCKET, M_NOWAIT); 1209 if (*mp != NULL) { 1210 mp = &(*mp)->m_next; 1211 stamped = true; 1212 } 1213 break; 1214 1215 case SO_TS_MONOTONIC: 1216 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR | 1217 M_TSTMP)) 1218 mbuf_tstmp2timespec(m, &t.ts); 1219 else 1220 nanouptime(&t.ts); 1221 *mp = sbcreatecontrol(&t.ts, sizeof(t.ts), 1222 SCM_MONOTONIC, SOL_SOCKET, M_NOWAIT); 1223 if (*mp != NULL) { 1224 mp = &(*mp)->m_next; 1225 stamped = true; 1226 } 1227 break; 1228 1229 default: 1230 panic("unknown (corrupted) so_ts_clock"); 1231 } 1232 if (stamped && (m->m_flags & (M_PKTHDR | M_TSTMP)) == 1233 (M_PKTHDR | M_TSTMP)) { 1234 struct sock_timestamp_info sti; 1235 1236 bzero(&sti, sizeof(sti)); 1237 sti.st_info_flags = ST_INFO_HW; 1238 if ((m->m_flags & M_TSTMP_HPREC) != 0) 1239 sti.st_info_flags |= ST_INFO_HW_HPREC; 1240 *mp = sbcreatecontrol(&sti, sizeof(sti), SCM_TIME_INFO, 1241 SOL_SOCKET, M_NOWAIT); 1242 if (*mp != NULL) 1243 mp = &(*mp)->m_next; 1244 } 1245 } 1246 #endif 1247 1248 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y)) 1249 /* RFC 2292 sec. 5 */ 1250 if ((inp->inp_flags & IN6P_PKTINFO) != 0) { 1251 struct in6_pktinfo pi6; 1252 1253 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1254 #ifdef INET 1255 struct ip *ip; 1256 1257 ip = mtod(m, struct ip *); 1258 pi6.ipi6_addr.s6_addr32[0] = 0; 1259 pi6.ipi6_addr.s6_addr32[1] = 0; 1260 pi6.ipi6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 1261 pi6.ipi6_addr.s6_addr32[3] = ip->ip_dst.s_addr; 1262 #else 1263 /* We won't hit this code */ 1264 bzero(&pi6.ipi6_addr, sizeof(struct in6_addr)); 1265 #endif 1266 } else { 1267 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 1268 in6_clearscope(&pi6.ipi6_addr); /* XXX */ 1269 } 1270 pi6.ipi6_ifindex = 1271 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0; 1272 1273 *mp = sbcreatecontrol(&pi6, sizeof(struct in6_pktinfo), 1274 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6, 1275 M_NOWAIT); 1276 if (*mp) 1277 mp = &(*mp)->m_next; 1278 } 1279 1280 if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) { 1281 int hlim; 1282 1283 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1284 #ifdef INET 1285 struct ip *ip; 1286 1287 ip = mtod(m, struct ip *); 1288 hlim = ip->ip_ttl; 1289 #else 1290 /* We won't hit this code */ 1291 hlim = 0; 1292 #endif 1293 } else { 1294 hlim = ip6->ip6_hlim & 0xff; 1295 } 1296 *mp = sbcreatecontrol(&hlim, sizeof(int), 1297 IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), 1298 IPPROTO_IPV6, M_NOWAIT); 1299 if (*mp) 1300 mp = &(*mp)->m_next; 1301 } 1302 1303 if ((inp->inp_flags & IN6P_TCLASS) != 0) { 1304 int tclass; 1305 1306 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1307 #ifdef INET 1308 struct ip *ip; 1309 1310 ip = mtod(m, struct ip *); 1311 tclass = ip->ip_tos; 1312 #else 1313 /* We won't hit this code */ 1314 tclass = 0; 1315 #endif 1316 } else { 1317 u_int32_t flowinfo; 1318 1319 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK); 1320 flowinfo >>= 20; 1321 tclass = flowinfo & 0xff; 1322 } 1323 *mp = sbcreatecontrol(&tclass, sizeof(int), IPV6_TCLASS, 1324 IPPROTO_IPV6, M_NOWAIT); 1325 if (*mp) 1326 mp = &(*mp)->m_next; 1327 } 1328 1329 if (v4only != NULL) { 1330 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1331 *v4only = 1; 1332 } else { 1333 *v4only = 0; 1334 } 1335 } 1336 1337 return (mp); 1338 } 1339 1340 void 1341 ip6_savecontrol(struct inpcb *inp, struct mbuf *m, struct mbuf **mp) 1342 { 1343 struct ip6_hdr *ip6; 1344 int v4only = 0; 1345 1346 mp = ip6_savecontrol_v4(inp, m, mp, &v4only); 1347 if (v4only) 1348 return; 1349 1350 ip6 = mtod(m, struct ip6_hdr *); 1351 /* 1352 * IPV6_HOPOPTS socket option. Recall that we required super-user 1353 * privilege for the option (see ip6_ctloutput), but it might be too 1354 * strict, since there might be some hop-by-hop options which can be 1355 * returned to normal user. 1356 * See also RFC 2292 section 6 (or RFC 3542 section 8). 1357 */ 1358 if ((inp->inp_flags & IN6P_HOPOPTS) != 0) { 1359 /* 1360 * Check if a hop-by-hop options header is contatined in the 1361 * received packet, and if so, store the options as ancillary 1362 * data. Note that a hop-by-hop options header must be 1363 * just after the IPv6 header, which is assured through the 1364 * IPv6 input processing. 1365 */ 1366 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 1367 struct ip6_hbh *hbh; 1368 u_int hbhlen; 1369 1370 hbh = (struct ip6_hbh *)(ip6 + 1); 1371 hbhlen = (hbh->ip6h_len + 1) << 3; 1372 1373 /* 1374 * XXX: We copy the whole header even if a 1375 * jumbo payload option is included, the option which 1376 * is to be removed before returning according to 1377 * RFC2292. 1378 * Note: this constraint is removed in RFC3542 1379 */ 1380 *mp = sbcreatecontrol(hbh, hbhlen, 1381 IS2292(inp, IPV6_2292HOPOPTS, IPV6_HOPOPTS), 1382 IPPROTO_IPV6, M_NOWAIT); 1383 if (*mp) 1384 mp = &(*mp)->m_next; 1385 } 1386 } 1387 1388 if ((inp->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) { 1389 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 1390 1391 /* 1392 * Search for destination options headers or routing 1393 * header(s) through the header chain, and stores each 1394 * header as ancillary data. 1395 * Note that the order of the headers remains in 1396 * the chain of ancillary data. 1397 */ 1398 while (1) { /* is explicit loop prevention necessary? */ 1399 struct ip6_ext *ip6e = NULL; 1400 u_int elen; 1401 1402 /* 1403 * if it is not an extension header, don't try to 1404 * pull it from the chain. 1405 */ 1406 switch (nxt) { 1407 case IPPROTO_DSTOPTS: 1408 case IPPROTO_ROUTING: 1409 case IPPROTO_HOPOPTS: 1410 case IPPROTO_AH: /* is it possible? */ 1411 break; 1412 default: 1413 goto loopend; 1414 } 1415 1416 if (off + sizeof(*ip6e) > m->m_len) 1417 goto loopend; 1418 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 1419 if (nxt == IPPROTO_AH) 1420 elen = (ip6e->ip6e_len + 2) << 2; 1421 else 1422 elen = (ip6e->ip6e_len + 1) << 3; 1423 if (off + elen > m->m_len) 1424 goto loopend; 1425 1426 switch (nxt) { 1427 case IPPROTO_DSTOPTS: 1428 if (!(inp->inp_flags & IN6P_DSTOPTS)) 1429 break; 1430 1431 *mp = sbcreatecontrol(ip6e, elen, 1432 IS2292(inp, IPV6_2292DSTOPTS, IPV6_DSTOPTS), 1433 IPPROTO_IPV6, M_NOWAIT); 1434 if (*mp) 1435 mp = &(*mp)->m_next; 1436 break; 1437 case IPPROTO_ROUTING: 1438 if (!(inp->inp_flags & IN6P_RTHDR)) 1439 break; 1440 1441 *mp = sbcreatecontrol(ip6e, elen, 1442 IS2292(inp, IPV6_2292RTHDR, IPV6_RTHDR), 1443 IPPROTO_IPV6, M_NOWAIT); 1444 if (*mp) 1445 mp = &(*mp)->m_next; 1446 break; 1447 case IPPROTO_HOPOPTS: 1448 case IPPROTO_AH: /* is it possible? */ 1449 break; 1450 1451 default: 1452 /* 1453 * other cases have been filtered in the above. 1454 * none will visit this case. here we supply 1455 * the code just in case (nxt overwritten or 1456 * other cases). 1457 */ 1458 goto loopend; 1459 } 1460 1461 /* proceed with the next header. */ 1462 off += elen; 1463 nxt = ip6e->ip6e_nxt; 1464 ip6e = NULL; 1465 } 1466 loopend: 1467 ; 1468 } 1469 1470 if (inp->inp_flags2 & INP_RECVFLOWID) { 1471 uint32_t flowid, flow_type; 1472 1473 flowid = m->m_pkthdr.flowid; 1474 flow_type = M_HASHTYPE_GET(m); 1475 1476 /* 1477 * XXX should handle the failure of one or the 1478 * other - don't populate both? 1479 */ 1480 *mp = sbcreatecontrol(&flowid, sizeof(uint32_t), IPV6_FLOWID, 1481 IPPROTO_IPV6, M_NOWAIT); 1482 if (*mp) 1483 mp = &(*mp)->m_next; 1484 *mp = sbcreatecontrol(&flow_type, sizeof(uint32_t), 1485 IPV6_FLOWTYPE, IPPROTO_IPV6, M_NOWAIT); 1486 if (*mp) 1487 mp = &(*mp)->m_next; 1488 } 1489 1490 #ifdef RSS 1491 if (inp->inp_flags2 & INP_RECVRSSBUCKETID) { 1492 uint32_t flowid, flow_type; 1493 uint32_t rss_bucketid; 1494 1495 flowid = m->m_pkthdr.flowid; 1496 flow_type = M_HASHTYPE_GET(m); 1497 1498 if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) { 1499 *mp = sbcreatecontrol(&rss_bucketid, sizeof(uint32_t), 1500 IPV6_RSSBUCKETID, IPPROTO_IPV6, M_NOWAIT); 1501 if (*mp) 1502 mp = &(*mp)->m_next; 1503 } 1504 } 1505 #endif 1506 1507 } 1508 #undef IS2292 1509 1510 void 1511 ip6_notify_pmtu(struct inpcb *inp, struct sockaddr_in6 *dst, u_int32_t mtu) 1512 { 1513 struct socket *so; 1514 struct mbuf *m_mtu; 1515 struct ip6_mtuinfo mtuctl; 1516 1517 KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 1518 /* 1519 * Notify the error by sending IPV6_PATHMTU ancillary data if 1520 * application wanted to know the MTU value. 1521 * NOTE: we notify disconnected sockets, because some udp 1522 * applications keep sending sockets disconnected. 1523 * NOTE: our implementation doesn't notify connected sockets that has 1524 * foreign address that is different than given destination addresses 1525 * (this is permitted by RFC 3542). 1526 */ 1527 if ((inp->inp_flags & IN6P_MTU) == 0 || ( 1528 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 1529 !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &dst->sin6_addr))) 1530 return; 1531 1532 mtuctl.ip6m_mtu = mtu; 1533 mtuctl.ip6m_addr = *dst; 1534 if (sa6_recoverscope(&mtuctl.ip6m_addr)) 1535 return; 1536 1537 if ((m_mtu = sbcreatecontrol(&mtuctl, sizeof(mtuctl), IPV6_PATHMTU, 1538 IPPROTO_IPV6, M_NOWAIT)) == NULL) 1539 return; 1540 1541 so = inp->inp_socket; 1542 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu) 1543 == 0) { 1544 soroverflow(so); 1545 m_freem(m_mtu); 1546 /* XXX: should count statistics */ 1547 } else 1548 sorwakeup(so); 1549 } 1550 1551 /* 1552 * Get pointer to the previous header followed by the header 1553 * currently processed. 1554 */ 1555 int 1556 ip6_get_prevhdr(const struct mbuf *m, int off) 1557 { 1558 struct ip6_ext ip6e; 1559 struct ip6_hdr *ip6; 1560 int len, nlen, nxt; 1561 1562 if (off == sizeof(struct ip6_hdr)) 1563 return (offsetof(struct ip6_hdr, ip6_nxt)); 1564 if (off < sizeof(struct ip6_hdr)) 1565 panic("%s: off < sizeof(struct ip6_hdr)", __func__); 1566 1567 ip6 = mtod(m, struct ip6_hdr *); 1568 nxt = ip6->ip6_nxt; 1569 len = sizeof(struct ip6_hdr); 1570 nlen = 0; 1571 while (len < off) { 1572 m_copydata(m, len, sizeof(ip6e), (caddr_t)&ip6e); 1573 switch (nxt) { 1574 case IPPROTO_FRAGMENT: 1575 nlen = sizeof(struct ip6_frag); 1576 break; 1577 case IPPROTO_AH: 1578 nlen = (ip6e.ip6e_len + 2) << 2; 1579 break; 1580 default: 1581 nlen = (ip6e.ip6e_len + 1) << 3; 1582 } 1583 len += nlen; 1584 nxt = ip6e.ip6e_nxt; 1585 } 1586 return (len - nlen); 1587 } 1588 1589 /* 1590 * get next header offset. m will be retained. 1591 */ 1592 int 1593 ip6_nexthdr(const struct mbuf *m, int off, int proto, int *nxtp) 1594 { 1595 struct ip6_hdr ip6; 1596 struct ip6_ext ip6e; 1597 struct ip6_frag fh; 1598 1599 /* just in case */ 1600 if (m == NULL) 1601 panic("ip6_nexthdr: m == NULL"); 1602 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 1603 return -1; 1604 1605 switch (proto) { 1606 case IPPROTO_IPV6: 1607 if (m->m_pkthdr.len < off + sizeof(ip6)) 1608 return -1; 1609 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 1610 if (nxtp) 1611 *nxtp = ip6.ip6_nxt; 1612 off += sizeof(ip6); 1613 return off; 1614 1615 case IPPROTO_FRAGMENT: 1616 /* 1617 * terminate parsing if it is not the first fragment, 1618 * it does not make sense to parse through it. 1619 */ 1620 if (m->m_pkthdr.len < off + sizeof(fh)) 1621 return -1; 1622 m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 1623 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */ 1624 if (fh.ip6f_offlg & IP6F_OFF_MASK) 1625 return -1; 1626 if (nxtp) 1627 *nxtp = fh.ip6f_nxt; 1628 off += sizeof(struct ip6_frag); 1629 return off; 1630 1631 case IPPROTO_AH: 1632 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1633 return -1; 1634 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1635 if (nxtp) 1636 *nxtp = ip6e.ip6e_nxt; 1637 off += (ip6e.ip6e_len + 2) << 2; 1638 return off; 1639 1640 case IPPROTO_HOPOPTS: 1641 case IPPROTO_ROUTING: 1642 case IPPROTO_DSTOPTS: 1643 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1644 return -1; 1645 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1646 if (nxtp) 1647 *nxtp = ip6e.ip6e_nxt; 1648 off += (ip6e.ip6e_len + 1) << 3; 1649 return off; 1650 1651 case IPPROTO_NONE: 1652 case IPPROTO_ESP: 1653 case IPPROTO_IPCOMP: 1654 /* give up */ 1655 return -1; 1656 1657 default: 1658 return -1; 1659 } 1660 1661 /* NOTREACHED */ 1662 } 1663 1664 /* 1665 * get offset for the last header in the chain. m will be kept untainted. 1666 */ 1667 int 1668 ip6_lasthdr(const struct mbuf *m, int off, int proto, int *nxtp) 1669 { 1670 int newoff; 1671 int nxt; 1672 1673 if (!nxtp) { 1674 nxt = -1; 1675 nxtp = &nxt; 1676 } 1677 while (1) { 1678 newoff = ip6_nexthdr(m, off, proto, nxtp); 1679 if (newoff < 0) 1680 return off; 1681 else if (newoff < off) 1682 return -1; /* invalid */ 1683 else if (newoff == off) 1684 return newoff; 1685 1686 off = newoff; 1687 proto = *nxtp; 1688 } 1689 } 1690