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