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