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