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