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(pfil_head_t, inet6_pfil_head); 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 pfil_head_args args; 218 struct protosw *pr; 219 int i; 220 221 TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal", 222 &V_ip6_auto_linklocal); 223 TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv); 224 TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr); 225 226 CK_STAILQ_INIT(&V_in6_ifaddrhead); 227 V_in6_ifaddrhashtbl = hashinit(IN6ADDR_NHASH, M_IFADDR, 228 &V_in6_ifaddrhmask); 229 230 /* Initialize packet filter hooks. */ 231 args.pa_version = PFIL_VERSION; 232 args.pa_flags = PFIL_IN | PFIL_OUT; 233 args.pa_type = PFIL_TYPE_IP6; 234 args.pa_headname = PFIL_INET6_NAME; 235 V_inet6_pfil_head = pfil_head_register(&args); 236 237 if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET6, 238 &V_ipsec_hhh_in[HHOOK_IPSEC_INET6], 239 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0) 240 printf("%s: WARNING: unable to register input helper hook\n", 241 __func__); 242 if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET6, 243 &V_ipsec_hhh_out[HHOOK_IPSEC_INET6], 244 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0) 245 printf("%s: WARNING: unable to register output helper hook\n", 246 __func__); 247 248 scope6_init(); 249 addrsel_policy_init(); 250 nd6_init(); 251 frag6_init(); 252 253 V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR; 254 255 /* Skip global initialization stuff for non-default instances. */ 256 #ifdef VIMAGE 257 if (!IS_DEFAULT_VNET(curvnet)) { 258 netisr_register_vnet(&ip6_nh); 259 #ifdef RSS 260 netisr_register_vnet(&ip6_direct_nh); 261 #endif 262 return; 263 } 264 #endif 265 266 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 267 if (pr == NULL) 268 panic("ip6_init"); 269 270 /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */ 271 for (i = 0; i < IPPROTO_MAX; i++) 272 ip6_protox[i] = pr - inet6sw; 273 /* 274 * Cycle through IP protocols and put them into the appropriate place 275 * in ip6_protox[]. 276 */ 277 for (pr = inet6domain.dom_protosw; 278 pr < inet6domain.dom_protoswNPROTOSW; pr++) 279 if (pr->pr_domain->dom_family == PF_INET6 && 280 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) { 281 /* Be careful to only index valid IP protocols. */ 282 if (pr->pr_protocol < IPPROTO_MAX) 283 ip6_protox[pr->pr_protocol] = pr - inet6sw; 284 } 285 286 netisr_register(&ip6_nh); 287 #ifdef RSS 288 netisr_register(&ip6_direct_nh); 289 #endif 290 } 291 292 /* 293 * The protocol to be inserted into ip6_protox[] must be already registered 294 * in inet6sw[], either statically or through pf_proto_register(). 295 */ 296 int 297 ip6proto_register(short ip6proto) 298 { 299 struct protosw *pr; 300 301 /* Sanity checks. */ 302 if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX) 303 return (EPROTONOSUPPORT); 304 305 /* 306 * The protocol slot must not be occupied by another protocol 307 * already. An index pointing to IPPROTO_RAW is unused. 308 */ 309 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 310 if (pr == NULL) 311 return (EPFNOSUPPORT); 312 if (ip6_protox[ip6proto] != pr - inet6sw) /* IPPROTO_RAW */ 313 return (EEXIST); 314 315 /* 316 * Find the protocol position in inet6sw[] and set the index. 317 */ 318 for (pr = inet6domain.dom_protosw; 319 pr < inet6domain.dom_protoswNPROTOSW; pr++) { 320 if (pr->pr_domain->dom_family == PF_INET6 && 321 pr->pr_protocol && pr->pr_protocol == ip6proto) { 322 ip6_protox[pr->pr_protocol] = pr - inet6sw; 323 return (0); 324 } 325 } 326 return (EPROTONOSUPPORT); 327 } 328 329 int 330 ip6proto_unregister(short ip6proto) 331 { 332 struct protosw *pr; 333 334 /* Sanity checks. */ 335 if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX) 336 return (EPROTONOSUPPORT); 337 338 /* Check if the protocol was indeed registered. */ 339 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 340 if (pr == NULL) 341 return (EPFNOSUPPORT); 342 if (ip6_protox[ip6proto] == pr - inet6sw) /* IPPROTO_RAW */ 343 return (ENOENT); 344 345 /* Reset the protocol slot to IPPROTO_RAW. */ 346 ip6_protox[ip6proto] = pr - inet6sw; 347 return (0); 348 } 349 350 #ifdef VIMAGE 351 static void 352 ip6_destroy(void *unused __unused) 353 { 354 struct ifaddr *ifa, *nifa; 355 struct ifnet *ifp; 356 int error; 357 358 #ifdef RSS 359 netisr_unregister_vnet(&ip6_direct_nh); 360 #endif 361 netisr_unregister_vnet(&ip6_nh); 362 363 pfil_head_unregister(V_inet6_pfil_head); 364 error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET6]); 365 if (error != 0) { 366 printf("%s: WARNING: unable to deregister input helper hook " 367 "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET6: " 368 "error %d returned\n", __func__, error); 369 } 370 error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET6]); 371 if (error != 0) { 372 printf("%s: WARNING: unable to deregister output helper hook " 373 "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET6: " 374 "error %d returned\n", __func__, error); 375 } 376 377 /* Cleanup addresses. */ 378 IFNET_RLOCK(); 379 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 380 /* Cannot lock here - lock recursion. */ 381 /* IF_ADDR_LOCK(ifp); */ 382 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) { 383 384 if (ifa->ifa_addr->sa_family != AF_INET6) 385 continue; 386 in6_purgeaddr(ifa); 387 } 388 /* IF_ADDR_UNLOCK(ifp); */ 389 in6_ifdetach_destroy(ifp); 390 mld_domifdetach(ifp); 391 /* Make sure any routes are gone as well. */ 392 rt_flushifroutes_af(ifp, AF_INET6); 393 } 394 IFNET_RUNLOCK(); 395 396 frag6_destroy(); 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_IN(V_inet6_pfil_head)) 764 goto passin; 765 766 odst = ip6->ip6_dst; 767 if (pfil_run_hooks(V_inet6_pfil_head, &m, m->m_pkthdr.rcvif, PFIL_IN, 768 NULL) != PFIL_PASS) 769 return; 770 ip6 = mtod(m, struct ip6_hdr *); 771 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst); 772 if ((m->m_flags & (M_IP6_NEXTHOP | M_FASTFWD_OURS)) == M_IP6_NEXTHOP && 773 m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) { 774 /* 775 * Directly ship the packet on. This allows forwarding 776 * packets originally destined to us to some other directly 777 * connected host. 778 */ 779 ip6_forward(m, 1); 780 return; 781 } 782 783 passin: 784 /* 785 * Disambiguate address scope zones (if there is ambiguity). 786 * We first make sure that the original source or destination address 787 * is not in our internal form for scoped addresses. Such addresses 788 * are not necessarily invalid spec-wise, but we cannot accept them due 789 * to the usage conflict. 790 * in6_setscope() then also checks and rejects the cases where src or 791 * dst are the loopback address and the receiving interface 792 * is not loopback. 793 */ 794 if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) { 795 IP6STAT_INC(ip6s_badscope); /* XXX */ 796 goto bad; 797 } 798 if (in6_setscope(&ip6->ip6_src, rcvif, NULL) || 799 in6_setscope(&ip6->ip6_dst, rcvif, NULL)) { 800 IP6STAT_INC(ip6s_badscope); 801 goto bad; 802 } 803 if (m->m_flags & M_FASTFWD_OURS) { 804 m->m_flags &= ~M_FASTFWD_OURS; 805 ours = 1; 806 goto hbhcheck; 807 } 808 /* 809 * Multicast check. Assume packet is for us to avoid 810 * prematurely taking locks. 811 */ 812 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 813 ours = 1; 814 in6_ifstat_inc(rcvif, ifs6_in_mcast); 815 goto hbhcheck; 816 } 817 /* 818 * Unicast check 819 * XXX: For now we keep link-local IPv6 addresses with embedded 820 * scope zone id, therefore we use zero zoneid here. 821 */ 822 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 823 if (ia != NULL) { 824 if (ia->ia6_flags & IN6_IFF_NOTREADY) { 825 char ip6bufs[INET6_ADDRSTRLEN]; 826 char ip6bufd[INET6_ADDRSTRLEN]; 827 /* address is not ready, so discard the packet. */ 828 nd6log((LOG_INFO, 829 "ip6_input: packet to an unready address %s->%s\n", 830 ip6_sprintf(ip6bufs, &ip6->ip6_src), 831 ip6_sprintf(ip6bufd, &ip6->ip6_dst))); 832 ifa_free(&ia->ia_ifa); 833 goto bad; 834 } 835 /* Count the packet in the ip address stats */ 836 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1); 837 counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len); 838 ifa_free(&ia->ia_ifa); 839 ours = 1; 840 goto hbhcheck; 841 } 842 843 /* 844 * Now there is no reason to process the packet if it's not our own 845 * and we're not a router. 846 */ 847 if (!V_ip6_forwarding) { 848 IP6STAT_INC(ip6s_cantforward); 849 goto bad; 850 } 851 852 hbhcheck: 853 /* 854 * Process Hop-by-Hop options header if it's contained. 855 * m may be modified in ip6_hopopts_input(). 856 * If a JumboPayload option is included, plen will also be modified. 857 */ 858 plen = (u_int32_t)ntohs(ip6->ip6_plen); 859 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 860 if (ip6_input_hbh(&m, &plen, &rtalert, &off, &nxt, &ours) != 0) 861 return; 862 } else 863 nxt = ip6->ip6_nxt; 864 865 /* 866 * Use mbuf flags to propagate Router Alert option to 867 * ICMPv6 layer, as hop-by-hop options have been stripped. 868 */ 869 if (rtalert != ~0) 870 m->m_flags |= M_RTALERT_MLD; 871 872 /* 873 * Check that the amount of data in the buffers 874 * is as at least much as the IPv6 header would have us expect. 875 * Trim mbufs if longer than we expect. 876 * Drop packet if shorter than we expect. 877 */ 878 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 879 IP6STAT_INC(ip6s_tooshort); 880 in6_ifstat_inc(rcvif, ifs6_in_truncated); 881 goto bad; 882 } 883 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 884 if (m->m_len == m->m_pkthdr.len) { 885 m->m_len = sizeof(struct ip6_hdr) + plen; 886 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 887 } else 888 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 889 } 890 891 /* 892 * Forward if desirable. 893 */ 894 if (V_ip6_mrouter && 895 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 896 /* 897 * If we are acting as a multicast router, all 898 * incoming multicast packets are passed to the 899 * kernel-level multicast forwarding function. 900 * The packet is returned (relatively) intact; if 901 * ip6_mforward() returns a non-zero value, the packet 902 * must be discarded, else it may be accepted below. 903 * 904 * XXX TODO: Check hlim and multicast scope here to avoid 905 * unnecessarily calling into ip6_mforward(). 906 */ 907 if (ip6_mforward && ip6_mforward(ip6, rcvif, m)) { 908 IP6STAT_INC(ip6s_cantforward); 909 goto bad; 910 } 911 } else if (!ours) { 912 ip6_forward(m, srcrt); 913 return; 914 } 915 916 ip6 = mtod(m, struct ip6_hdr *); 917 918 /* 919 * Malicious party may be able to use IPv4 mapped addr to confuse 920 * tcp/udp stack and bypass security checks (act as if it was from 921 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 922 * 923 * For SIIT end node behavior, you may want to disable the check. 924 * However, you will become vulnerable to attacks using IPv4 mapped 925 * source. 926 */ 927 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 928 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 929 IP6STAT_INC(ip6s_badscope); 930 in6_ifstat_inc(rcvif, ifs6_in_addrerr); 931 goto bad; 932 } 933 934 /* 935 * Tell launch routine the next header 936 */ 937 IP6STAT_INC(ip6s_delivered); 938 in6_ifstat_inc(rcvif, ifs6_in_deliver); 939 nest = 0; 940 941 while (nxt != IPPROTO_DONE) { 942 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) { 943 IP6STAT_INC(ip6s_toomanyhdr); 944 goto bad; 945 } 946 947 /* 948 * protection against faulty packet - there should be 949 * more sanity checks in header chain processing. 950 */ 951 if (m->m_pkthdr.len < off) { 952 IP6STAT_INC(ip6s_tooshort); 953 in6_ifstat_inc(rcvif, ifs6_in_truncated); 954 goto bad; 955 } 956 957 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 958 if (IPSEC_ENABLED(ipv6)) { 959 if (IPSEC_INPUT(ipv6, m, off, nxt) != 0) 960 return; 961 } 962 #endif /* IPSEC */ 963 964 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt); 965 } 966 return; 967 bad: 968 in6_ifstat_inc(rcvif, ifs6_in_discard); 969 if (m != NULL) 970 m_freem(m); 971 } 972 973 /* 974 * Hop-by-Hop options header processing. If a valid jumbo payload option is 975 * included, the real payload length will be stored in plenp. 976 * 977 * rtalertp - XXX: should be stored more smart way 978 */ 979 static int 980 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp, 981 struct mbuf **mp, int *offp) 982 { 983 struct mbuf *m = *mp; 984 int off = *offp, hbhlen; 985 struct ip6_hbh *hbh; 986 987 /* validation of the length of the header */ 988 #ifndef PULLDOWN_TEST 989 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1); 990 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 991 hbhlen = (hbh->ip6h_len + 1) << 3; 992 993 IP6_EXTHDR_CHECK(m, off, hbhlen, -1); 994 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 995 #else 996 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, 997 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 998 if (hbh == NULL) { 999 IP6STAT_INC(ip6s_tooshort); 1000 return -1; 1001 } 1002 hbhlen = (hbh->ip6h_len + 1) << 3; 1003 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 1004 hbhlen); 1005 if (hbh == NULL) { 1006 IP6STAT_INC(ip6s_tooshort); 1007 return -1; 1008 } 1009 #endif 1010 off += hbhlen; 1011 hbhlen -= sizeof(struct ip6_hbh); 1012 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 1013 hbhlen, rtalertp, plenp) < 0) 1014 return (-1); 1015 1016 *offp = off; 1017 *mp = m; 1018 return (0); 1019 } 1020 1021 /* 1022 * Search header for all Hop-by-hop options and process each option. 1023 * This function is separate from ip6_hopopts_input() in order to 1024 * handle a case where the sending node itself process its hop-by-hop 1025 * options header. In such a case, the function is called from ip6_output(). 1026 * 1027 * The function assumes that hbh header is located right after the IPv6 header 1028 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 1029 * opthead + hbhlen is located in contiguous memory region. 1030 */ 1031 int 1032 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen, 1033 u_int32_t *rtalertp, u_int32_t *plenp) 1034 { 1035 struct ip6_hdr *ip6; 1036 int optlen = 0; 1037 u_int8_t *opt = opthead; 1038 u_int16_t rtalert_val; 1039 u_int32_t jumboplen; 1040 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 1041 1042 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 1043 switch (*opt) { 1044 case IP6OPT_PAD1: 1045 optlen = 1; 1046 break; 1047 case IP6OPT_PADN: 1048 if (hbhlen < IP6OPT_MINLEN) { 1049 IP6STAT_INC(ip6s_toosmall); 1050 goto bad; 1051 } 1052 optlen = *(opt + 1) + 2; 1053 break; 1054 case IP6OPT_ROUTER_ALERT: 1055 /* XXX may need check for alignment */ 1056 if (hbhlen < IP6OPT_RTALERT_LEN) { 1057 IP6STAT_INC(ip6s_toosmall); 1058 goto bad; 1059 } 1060 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 1061 /* XXX stat */ 1062 icmp6_error(m, ICMP6_PARAM_PROB, 1063 ICMP6_PARAMPROB_HEADER, 1064 erroff + opt + 1 - opthead); 1065 return (-1); 1066 } 1067 optlen = IP6OPT_RTALERT_LEN; 1068 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 1069 *rtalertp = ntohs(rtalert_val); 1070 break; 1071 case IP6OPT_JUMBO: 1072 /* XXX may need check for alignment */ 1073 if (hbhlen < IP6OPT_JUMBO_LEN) { 1074 IP6STAT_INC(ip6s_toosmall); 1075 goto bad; 1076 } 1077 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 1078 /* XXX stat */ 1079 icmp6_error(m, ICMP6_PARAM_PROB, 1080 ICMP6_PARAMPROB_HEADER, 1081 erroff + opt + 1 - opthead); 1082 return (-1); 1083 } 1084 optlen = IP6OPT_JUMBO_LEN; 1085 1086 /* 1087 * IPv6 packets that have non 0 payload length 1088 * must not contain a jumbo payload option. 1089 */ 1090 ip6 = mtod(m, struct ip6_hdr *); 1091 if (ip6->ip6_plen) { 1092 IP6STAT_INC(ip6s_badoptions); 1093 icmp6_error(m, ICMP6_PARAM_PROB, 1094 ICMP6_PARAMPROB_HEADER, 1095 erroff + opt - opthead); 1096 return (-1); 1097 } 1098 1099 /* 1100 * We may see jumbolen in unaligned location, so 1101 * we'd need to perform bcopy(). 1102 */ 1103 bcopy(opt + 2, &jumboplen, sizeof(jumboplen)); 1104 jumboplen = (u_int32_t)htonl(jumboplen); 1105 1106 #if 1 1107 /* 1108 * if there are multiple jumbo payload options, 1109 * *plenp will be non-zero and the packet will be 1110 * rejected. 1111 * the behavior may need some debate in ipngwg - 1112 * multiple options does not make sense, however, 1113 * there's no explicit mention in specification. 1114 */ 1115 if (*plenp != 0) { 1116 IP6STAT_INC(ip6s_badoptions); 1117 icmp6_error(m, ICMP6_PARAM_PROB, 1118 ICMP6_PARAMPROB_HEADER, 1119 erroff + opt + 2 - opthead); 1120 return (-1); 1121 } 1122 #endif 1123 1124 /* 1125 * jumbo payload length must be larger than 65535. 1126 */ 1127 if (jumboplen <= IPV6_MAXPACKET) { 1128 IP6STAT_INC(ip6s_badoptions); 1129 icmp6_error(m, ICMP6_PARAM_PROB, 1130 ICMP6_PARAMPROB_HEADER, 1131 erroff + opt + 2 - opthead); 1132 return (-1); 1133 } 1134 *plenp = jumboplen; 1135 1136 break; 1137 default: /* unknown option */ 1138 if (hbhlen < IP6OPT_MINLEN) { 1139 IP6STAT_INC(ip6s_toosmall); 1140 goto bad; 1141 } 1142 optlen = ip6_unknown_opt(opt, m, 1143 erroff + opt - opthead); 1144 if (optlen == -1) 1145 return (-1); 1146 optlen += 2; 1147 break; 1148 } 1149 } 1150 1151 return (0); 1152 1153 bad: 1154 m_freem(m); 1155 return (-1); 1156 } 1157 1158 /* 1159 * Unknown option processing. 1160 * The third argument `off' is the offset from the IPv6 header to the option, 1161 * which is necessary if the IPv6 header the and option header and IPv6 header 1162 * is not contiguous in order to return an ICMPv6 error. 1163 */ 1164 int 1165 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off) 1166 { 1167 struct ip6_hdr *ip6; 1168 1169 switch (IP6OPT_TYPE(*optp)) { 1170 case IP6OPT_TYPE_SKIP: /* ignore the option */ 1171 return ((int)*(optp + 1)); 1172 case IP6OPT_TYPE_DISCARD: /* silently discard */ 1173 m_freem(m); 1174 return (-1); 1175 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 1176 IP6STAT_INC(ip6s_badoptions); 1177 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 1178 return (-1); 1179 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 1180 IP6STAT_INC(ip6s_badoptions); 1181 ip6 = mtod(m, struct ip6_hdr *); 1182 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1183 (m->m_flags & (M_BCAST|M_MCAST))) 1184 m_freem(m); 1185 else 1186 icmp6_error(m, ICMP6_PARAM_PROB, 1187 ICMP6_PARAMPROB_OPTION, off); 1188 return (-1); 1189 } 1190 1191 m_freem(m); /* XXX: NOTREACHED */ 1192 return (-1); 1193 } 1194 1195 /* 1196 * Create the "control" list for this pcb. 1197 * These functions will not modify mbuf chain at all. 1198 * 1199 * With KAME mbuf chain restriction: 1200 * The routine will be called from upper layer handlers like tcp6_input(). 1201 * Thus the routine assumes that the caller (tcp6_input) have already 1202 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 1203 * very first mbuf on the mbuf chain. 1204 * 1205 * ip6_savecontrol_v4 will handle those options that are possible to be 1206 * set on a v4-mapped socket. 1207 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those 1208 * options and handle the v6-only ones itself. 1209 */ 1210 struct mbuf ** 1211 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp, 1212 int *v4only) 1213 { 1214 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1215 1216 #ifdef SO_TIMESTAMP 1217 if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) { 1218 union { 1219 struct timeval tv; 1220 struct bintime bt; 1221 struct timespec ts; 1222 } t; 1223 struct bintime boottimebin, bt1; 1224 struct timespec ts1; 1225 bool stamped; 1226 1227 stamped = false; 1228 switch (inp->inp_socket->so_ts_clock) { 1229 case SO_TS_REALTIME_MICRO: 1230 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR | 1231 M_TSTMP)) { 1232 mbuf_tstmp2timespec(m, &ts1); 1233 timespec2bintime(&ts1, &bt1); 1234 getboottimebin(&boottimebin); 1235 bintime_add(&bt1, &boottimebin); 1236 bintime2timeval(&bt1, &t.tv); 1237 } else { 1238 microtime(&t.tv); 1239 } 1240 *mp = sbcreatecontrol((caddr_t) &t.tv, sizeof(t.tv), 1241 SCM_TIMESTAMP, SOL_SOCKET); 1242 if (*mp != NULL) { 1243 mp = &(*mp)->m_next; 1244 stamped = true; 1245 } 1246 break; 1247 1248 case SO_TS_BINTIME: 1249 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR | 1250 M_TSTMP)) { 1251 mbuf_tstmp2timespec(m, &ts1); 1252 timespec2bintime(&ts1, &t.bt); 1253 getboottimebin(&boottimebin); 1254 bintime_add(&t.bt, &boottimebin); 1255 } else { 1256 bintime(&t.bt); 1257 } 1258 *mp = sbcreatecontrol((caddr_t)&t.bt, sizeof(t.bt), 1259 SCM_BINTIME, SOL_SOCKET); 1260 if (*mp != NULL) { 1261 mp = &(*mp)->m_next; 1262 stamped = true; 1263 } 1264 break; 1265 1266 case SO_TS_REALTIME: 1267 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR | 1268 M_TSTMP)) { 1269 mbuf_tstmp2timespec(m, &t.ts); 1270 getboottimebin(&boottimebin); 1271 bintime2timespec(&boottimebin, &ts1); 1272 timespecadd(&t.ts, &ts1, &t.ts); 1273 } else { 1274 nanotime(&t.ts); 1275 } 1276 *mp = sbcreatecontrol((caddr_t)&t.ts, sizeof(t.ts), 1277 SCM_REALTIME, SOL_SOCKET); 1278 if (*mp != NULL) { 1279 mp = &(*mp)->m_next; 1280 stamped = true; 1281 } 1282 break; 1283 1284 case SO_TS_MONOTONIC: 1285 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR | 1286 M_TSTMP)) 1287 mbuf_tstmp2timespec(m, &t.ts); 1288 else 1289 nanouptime(&t.ts); 1290 *mp = sbcreatecontrol((caddr_t)&t.ts, sizeof(t.ts), 1291 SCM_MONOTONIC, SOL_SOCKET); 1292 if (*mp != NULL) { 1293 mp = &(*mp)->m_next; 1294 stamped = true; 1295 } 1296 break; 1297 1298 default: 1299 panic("unknown (corrupted) so_ts_clock"); 1300 } 1301 if (stamped && (m->m_flags & (M_PKTHDR | M_TSTMP)) == 1302 (M_PKTHDR | M_TSTMP)) { 1303 struct sock_timestamp_info sti; 1304 1305 bzero(&sti, sizeof(sti)); 1306 sti.st_info_flags = ST_INFO_HW; 1307 if ((m->m_flags & M_TSTMP_HPREC) != 0) 1308 sti.st_info_flags |= ST_INFO_HW_HPREC; 1309 *mp = sbcreatecontrol((caddr_t)&sti, sizeof(sti), 1310 SCM_TIME_INFO, SOL_SOCKET); 1311 if (*mp != NULL) 1312 mp = &(*mp)->m_next; 1313 } 1314 } 1315 #endif 1316 1317 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y)) 1318 /* RFC 2292 sec. 5 */ 1319 if ((inp->inp_flags & IN6P_PKTINFO) != 0) { 1320 struct in6_pktinfo pi6; 1321 1322 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1323 #ifdef INET 1324 struct ip *ip; 1325 1326 ip = mtod(m, struct ip *); 1327 pi6.ipi6_addr.s6_addr32[0] = 0; 1328 pi6.ipi6_addr.s6_addr32[1] = 0; 1329 pi6.ipi6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 1330 pi6.ipi6_addr.s6_addr32[3] = ip->ip_dst.s_addr; 1331 #else 1332 /* We won't hit this code */ 1333 bzero(&pi6.ipi6_addr, sizeof(struct in6_addr)); 1334 #endif 1335 } else { 1336 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 1337 in6_clearscope(&pi6.ipi6_addr); /* XXX */ 1338 } 1339 pi6.ipi6_ifindex = 1340 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0; 1341 1342 *mp = sbcreatecontrol((caddr_t) &pi6, 1343 sizeof(struct in6_pktinfo), 1344 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6); 1345 if (*mp) 1346 mp = &(*mp)->m_next; 1347 } 1348 1349 if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) { 1350 int hlim; 1351 1352 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1353 #ifdef INET 1354 struct ip *ip; 1355 1356 ip = mtod(m, struct ip *); 1357 hlim = ip->ip_ttl; 1358 #else 1359 /* We won't hit this code */ 1360 hlim = 0; 1361 #endif 1362 } else { 1363 hlim = ip6->ip6_hlim & 0xff; 1364 } 1365 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int), 1366 IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), 1367 IPPROTO_IPV6); 1368 if (*mp) 1369 mp = &(*mp)->m_next; 1370 } 1371 1372 if ((inp->inp_flags & IN6P_TCLASS) != 0) { 1373 int tclass; 1374 1375 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1376 #ifdef INET 1377 struct ip *ip; 1378 1379 ip = mtod(m, struct ip *); 1380 tclass = ip->ip_tos; 1381 #else 1382 /* We won't hit this code */ 1383 tclass = 0; 1384 #endif 1385 } else { 1386 u_int32_t flowinfo; 1387 1388 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK); 1389 flowinfo >>= 20; 1390 tclass = flowinfo & 0xff; 1391 } 1392 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(int), 1393 IPV6_TCLASS, IPPROTO_IPV6); 1394 if (*mp) 1395 mp = &(*mp)->m_next; 1396 } 1397 1398 if (v4only != NULL) { 1399 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1400 *v4only = 1; 1401 } else { 1402 *v4only = 0; 1403 } 1404 } 1405 1406 return (mp); 1407 } 1408 1409 void 1410 ip6_savecontrol(struct inpcb *inp, struct mbuf *m, struct mbuf **mp) 1411 { 1412 struct ip6_hdr *ip6; 1413 int v4only = 0; 1414 1415 mp = ip6_savecontrol_v4(inp, m, mp, &v4only); 1416 if (v4only) 1417 return; 1418 1419 ip6 = mtod(m, struct ip6_hdr *); 1420 /* 1421 * IPV6_HOPOPTS socket option. Recall that we required super-user 1422 * privilege for the option (see ip6_ctloutput), but it might be too 1423 * strict, since there might be some hop-by-hop options which can be 1424 * returned to normal user. 1425 * See also RFC 2292 section 6 (or RFC 3542 section 8). 1426 */ 1427 if ((inp->inp_flags & IN6P_HOPOPTS) != 0) { 1428 /* 1429 * Check if a hop-by-hop options header is contatined in the 1430 * received packet, and if so, store the options as ancillary 1431 * data. Note that a hop-by-hop options header must be 1432 * just after the IPv6 header, which is assured through the 1433 * IPv6 input processing. 1434 */ 1435 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 1436 struct ip6_hbh *hbh; 1437 int hbhlen = 0; 1438 #ifdef PULLDOWN_TEST 1439 struct mbuf *ext; 1440 #endif 1441 1442 #ifndef PULLDOWN_TEST 1443 hbh = (struct ip6_hbh *)(ip6 + 1); 1444 hbhlen = (hbh->ip6h_len + 1) << 3; 1445 #else 1446 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 1447 ip6->ip6_nxt); 1448 if (ext == NULL) { 1449 IP6STAT_INC(ip6s_tooshort); 1450 return; 1451 } 1452 hbh = mtod(ext, struct ip6_hbh *); 1453 hbhlen = (hbh->ip6h_len + 1) << 3; 1454 if (hbhlen != ext->m_len) { 1455 m_freem(ext); 1456 IP6STAT_INC(ip6s_tooshort); 1457 return; 1458 } 1459 #endif 1460 1461 /* 1462 * XXX: We copy the whole header even if a 1463 * jumbo payload option is included, the option which 1464 * is to be removed before returning according to 1465 * RFC2292. 1466 * Note: this constraint is removed in RFC3542 1467 */ 1468 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 1469 IS2292(inp, IPV6_2292HOPOPTS, IPV6_HOPOPTS), 1470 IPPROTO_IPV6); 1471 if (*mp) 1472 mp = &(*mp)->m_next; 1473 #ifdef PULLDOWN_TEST 1474 m_freem(ext); 1475 #endif 1476 } 1477 } 1478 1479 if ((inp->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) { 1480 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 1481 1482 /* 1483 * Search for destination options headers or routing 1484 * header(s) through the header chain, and stores each 1485 * header as ancillary data. 1486 * Note that the order of the headers remains in 1487 * the chain of ancillary data. 1488 */ 1489 while (1) { /* is explicit loop prevention necessary? */ 1490 struct ip6_ext *ip6e = NULL; 1491 int elen; 1492 #ifdef PULLDOWN_TEST 1493 struct mbuf *ext = NULL; 1494 #endif 1495 1496 /* 1497 * if it is not an extension header, don't try to 1498 * pull it from the chain. 1499 */ 1500 switch (nxt) { 1501 case IPPROTO_DSTOPTS: 1502 case IPPROTO_ROUTING: 1503 case IPPROTO_HOPOPTS: 1504 case IPPROTO_AH: /* is it possible? */ 1505 break; 1506 default: 1507 goto loopend; 1508 } 1509 1510 #ifndef PULLDOWN_TEST 1511 if (off + sizeof(*ip6e) > m->m_len) 1512 goto loopend; 1513 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 1514 if (nxt == IPPROTO_AH) 1515 elen = (ip6e->ip6e_len + 2) << 2; 1516 else 1517 elen = (ip6e->ip6e_len + 1) << 3; 1518 if (off + elen > m->m_len) 1519 goto loopend; 1520 #else 1521 ext = ip6_pullexthdr(m, off, nxt); 1522 if (ext == NULL) { 1523 IP6STAT_INC(ip6s_tooshort); 1524 return; 1525 } 1526 ip6e = mtod(ext, struct ip6_ext *); 1527 if (nxt == IPPROTO_AH) 1528 elen = (ip6e->ip6e_len + 2) << 2; 1529 else 1530 elen = (ip6e->ip6e_len + 1) << 3; 1531 if (elen != ext->m_len) { 1532 m_freem(ext); 1533 IP6STAT_INC(ip6s_tooshort); 1534 return; 1535 } 1536 #endif 1537 1538 switch (nxt) { 1539 case IPPROTO_DSTOPTS: 1540 if (!(inp->inp_flags & IN6P_DSTOPTS)) 1541 break; 1542 1543 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1544 IS2292(inp, 1545 IPV6_2292DSTOPTS, IPV6_DSTOPTS), 1546 IPPROTO_IPV6); 1547 if (*mp) 1548 mp = &(*mp)->m_next; 1549 break; 1550 case IPPROTO_ROUTING: 1551 if (!(inp->inp_flags & IN6P_RTHDR)) 1552 break; 1553 1554 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1555 IS2292(inp, IPV6_2292RTHDR, IPV6_RTHDR), 1556 IPPROTO_IPV6); 1557 if (*mp) 1558 mp = &(*mp)->m_next; 1559 break; 1560 case IPPROTO_HOPOPTS: 1561 case IPPROTO_AH: /* is it possible? */ 1562 break; 1563 1564 default: 1565 /* 1566 * other cases have been filtered in the above. 1567 * none will visit this case. here we supply 1568 * the code just in case (nxt overwritten or 1569 * other cases). 1570 */ 1571 #ifdef PULLDOWN_TEST 1572 m_freem(ext); 1573 #endif 1574 goto loopend; 1575 1576 } 1577 1578 /* proceed with the next header. */ 1579 off += elen; 1580 nxt = ip6e->ip6e_nxt; 1581 ip6e = NULL; 1582 #ifdef PULLDOWN_TEST 1583 m_freem(ext); 1584 ext = NULL; 1585 #endif 1586 } 1587 loopend: 1588 ; 1589 } 1590 1591 if (inp->inp_flags2 & INP_RECVFLOWID) { 1592 uint32_t flowid, flow_type; 1593 1594 flowid = m->m_pkthdr.flowid; 1595 flow_type = M_HASHTYPE_GET(m); 1596 1597 /* 1598 * XXX should handle the failure of one or the 1599 * other - don't populate both? 1600 */ 1601 *mp = sbcreatecontrol((caddr_t) &flowid, 1602 sizeof(uint32_t), IPV6_FLOWID, IPPROTO_IPV6); 1603 if (*mp) 1604 mp = &(*mp)->m_next; 1605 *mp = sbcreatecontrol((caddr_t) &flow_type, 1606 sizeof(uint32_t), IPV6_FLOWTYPE, IPPROTO_IPV6); 1607 if (*mp) 1608 mp = &(*mp)->m_next; 1609 } 1610 1611 #ifdef RSS 1612 if (inp->inp_flags2 & INP_RECVRSSBUCKETID) { 1613 uint32_t flowid, flow_type; 1614 uint32_t rss_bucketid; 1615 1616 flowid = m->m_pkthdr.flowid; 1617 flow_type = M_HASHTYPE_GET(m); 1618 1619 if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) { 1620 *mp = sbcreatecontrol((caddr_t) &rss_bucketid, 1621 sizeof(uint32_t), IPV6_RSSBUCKETID, IPPROTO_IPV6); 1622 if (*mp) 1623 mp = &(*mp)->m_next; 1624 } 1625 } 1626 #endif 1627 1628 } 1629 #undef IS2292 1630 1631 void 1632 ip6_notify_pmtu(struct inpcb *inp, struct sockaddr_in6 *dst, u_int32_t mtu) 1633 { 1634 struct socket *so; 1635 struct mbuf *m_mtu; 1636 struct ip6_mtuinfo mtuctl; 1637 1638 KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 1639 /* 1640 * Notify the error by sending IPV6_PATHMTU ancillary data if 1641 * application wanted to know the MTU value. 1642 * NOTE: we notify disconnected sockets, because some udp 1643 * applications keep sending sockets disconnected. 1644 * NOTE: our implementation doesn't notify connected sockets that has 1645 * foreign address that is different than given destination addresses 1646 * (this is permitted by RFC 3542). 1647 */ 1648 if ((inp->inp_flags & IN6P_MTU) == 0 || ( 1649 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 1650 !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &dst->sin6_addr))) 1651 return; 1652 1653 mtuctl.ip6m_mtu = mtu; 1654 mtuctl.ip6m_addr = *dst; 1655 if (sa6_recoverscope(&mtuctl.ip6m_addr)) 1656 return; 1657 1658 if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl), 1659 IPV6_PATHMTU, IPPROTO_IPV6)) == NULL) 1660 return; 1661 1662 so = inp->inp_socket; 1663 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu) 1664 == 0) { 1665 m_freem(m_mtu); 1666 /* XXX: should count statistics */ 1667 } else 1668 sorwakeup(so); 1669 } 1670 1671 #ifdef PULLDOWN_TEST 1672 /* 1673 * pull single extension header from mbuf chain. returns single mbuf that 1674 * contains the result, or NULL on error. 1675 */ 1676 static struct mbuf * 1677 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt) 1678 { 1679 struct ip6_ext ip6e; 1680 size_t elen; 1681 struct mbuf *n; 1682 1683 #ifdef DIAGNOSTIC 1684 switch (nxt) { 1685 case IPPROTO_DSTOPTS: 1686 case IPPROTO_ROUTING: 1687 case IPPROTO_HOPOPTS: 1688 case IPPROTO_AH: /* is it possible? */ 1689 break; 1690 default: 1691 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 1692 } 1693 #endif 1694 1695 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1696 if (nxt == IPPROTO_AH) 1697 elen = (ip6e.ip6e_len + 2) << 2; 1698 else 1699 elen = (ip6e.ip6e_len + 1) << 3; 1700 1701 if (elen > MLEN) 1702 n = m_getcl(M_NOWAIT, MT_DATA, 0); 1703 else 1704 n = m_get(M_NOWAIT, MT_DATA); 1705 if (n == NULL) 1706 return NULL; 1707 1708 m_copydata(m, off, elen, mtod(n, caddr_t)); 1709 n->m_len = elen; 1710 return n; 1711 } 1712 #endif 1713 1714 /* 1715 * Get pointer to the previous header followed by the header 1716 * currently processed. 1717 */ 1718 int 1719 ip6_get_prevhdr(const struct mbuf *m, int off) 1720 { 1721 struct ip6_ext ip6e; 1722 struct ip6_hdr *ip6; 1723 int len, nlen, nxt; 1724 1725 if (off == sizeof(struct ip6_hdr)) 1726 return (offsetof(struct ip6_hdr, ip6_nxt)); 1727 if (off < sizeof(struct ip6_hdr)) 1728 panic("%s: off < sizeof(struct ip6_hdr)", __func__); 1729 1730 ip6 = mtod(m, struct ip6_hdr *); 1731 nxt = ip6->ip6_nxt; 1732 len = sizeof(struct ip6_hdr); 1733 nlen = 0; 1734 while (len < off) { 1735 m_copydata(m, len, sizeof(ip6e), (caddr_t)&ip6e); 1736 switch (nxt) { 1737 case IPPROTO_FRAGMENT: 1738 nlen = sizeof(struct ip6_frag); 1739 break; 1740 case IPPROTO_AH: 1741 nlen = (ip6e.ip6e_len + 2) << 2; 1742 break; 1743 default: 1744 nlen = (ip6e.ip6e_len + 1) << 3; 1745 } 1746 len += nlen; 1747 nxt = ip6e.ip6e_nxt; 1748 } 1749 return (len - nlen); 1750 } 1751 1752 /* 1753 * get next header offset. m will be retained. 1754 */ 1755 int 1756 ip6_nexthdr(const struct mbuf *m, int off, int proto, int *nxtp) 1757 { 1758 struct ip6_hdr ip6; 1759 struct ip6_ext ip6e; 1760 struct ip6_frag fh; 1761 1762 /* just in case */ 1763 if (m == NULL) 1764 panic("ip6_nexthdr: m == NULL"); 1765 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 1766 return -1; 1767 1768 switch (proto) { 1769 case IPPROTO_IPV6: 1770 if (m->m_pkthdr.len < off + sizeof(ip6)) 1771 return -1; 1772 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 1773 if (nxtp) 1774 *nxtp = ip6.ip6_nxt; 1775 off += sizeof(ip6); 1776 return off; 1777 1778 case IPPROTO_FRAGMENT: 1779 /* 1780 * terminate parsing if it is not the first fragment, 1781 * it does not make sense to parse through it. 1782 */ 1783 if (m->m_pkthdr.len < off + sizeof(fh)) 1784 return -1; 1785 m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 1786 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */ 1787 if (fh.ip6f_offlg & IP6F_OFF_MASK) 1788 return -1; 1789 if (nxtp) 1790 *nxtp = fh.ip6f_nxt; 1791 off += sizeof(struct ip6_frag); 1792 return off; 1793 1794 case IPPROTO_AH: 1795 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1796 return -1; 1797 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1798 if (nxtp) 1799 *nxtp = ip6e.ip6e_nxt; 1800 off += (ip6e.ip6e_len + 2) << 2; 1801 return off; 1802 1803 case IPPROTO_HOPOPTS: 1804 case IPPROTO_ROUTING: 1805 case IPPROTO_DSTOPTS: 1806 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1807 return -1; 1808 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1809 if (nxtp) 1810 *nxtp = ip6e.ip6e_nxt; 1811 off += (ip6e.ip6e_len + 1) << 3; 1812 return off; 1813 1814 case IPPROTO_NONE: 1815 case IPPROTO_ESP: 1816 case IPPROTO_IPCOMP: 1817 /* give up */ 1818 return -1; 1819 1820 default: 1821 return -1; 1822 } 1823 1824 /* NOTREACHED */ 1825 } 1826 1827 /* 1828 * get offset for the last header in the chain. m will be kept untainted. 1829 */ 1830 int 1831 ip6_lasthdr(const struct mbuf *m, int off, int proto, int *nxtp) 1832 { 1833 int newoff; 1834 int nxt; 1835 1836 if (!nxtp) { 1837 nxt = -1; 1838 nxtp = &nxt; 1839 } 1840 while (1) { 1841 newoff = ip6_nexthdr(m, off, proto, nxtp); 1842 if (newoff < 0) 1843 return off; 1844 else if (newoff < off) 1845 return -1; /* invalid */ 1846 else if (newoff == off) 1847 return newoff; 1848 1849 off = newoff; 1850 proto = *nxtp; 1851 } 1852 } 1853 1854 /* 1855 * System control for IP6 1856 */ 1857 1858 u_char inet6ctlerrmap[PRC_NCMDS] = { 1859 0, 0, 0, 0, 1860 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1861 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1862 EMSGSIZE, EHOSTUNREACH, 0, 0, 1863 0, 0, EHOSTUNREACH, 0, 1864 ENOPROTOOPT, ECONNREFUSED 1865 }; 1866