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