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