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