1 /* $OpenBSD: ip6_input.c,v 1.248 2022/06/29 22:45:24 bluhm Exp $ */ 2 /* $KAME: ip6_input.c,v 1.188 2001/03/29 05:34:31 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1988, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 62 */ 63 64 #include "pf.h" 65 #include "carp.h" 66 67 #include <sys/param.h> 68 #include <sys/systm.h> 69 #include <sys/mbuf.h> 70 #include <sys/domain.h> 71 #include <sys/sysctl.h> 72 #include <sys/protosw.h> 73 #include <sys/socket.h> 74 #include <sys/socketvar.h> 75 #include <sys/errno.h> 76 #include <sys/time.h> 77 #include <sys/timeout.h> 78 #include <sys/kernel.h> 79 #include <sys/syslog.h> 80 #include <sys/task.h> 81 82 #include <net/if.h> 83 #include <net/if_var.h> 84 #include <net/if_types.h> 85 #include <net/route.h> 86 #include <net/netisr.h> 87 88 #include <netinet/in.h> 89 90 #include <netinet/ip.h> 91 92 #include <netinet/in_pcb.h> 93 #include <netinet/ip_var.h> 94 #include <netinet6/in6_var.h> 95 #include <netinet6/in6_ifattach.h> 96 #include <netinet/ip6.h> 97 #include <netinet6/ip6_var.h> 98 #include <netinet/icmp6.h> 99 #include <netinet6/nd6.h> 100 101 #include "gif.h" 102 #include "bpfilter.h" 103 104 #ifdef MROUTING 105 #include <netinet6/ip6_mroute.h> 106 #endif 107 108 #if NPF > 0 109 #include <net/pfvar.h> 110 #endif 111 112 #if NCARP > 0 113 #include <netinet/ip_carp.h> 114 #endif 115 116 struct niqueue ip6intrq = NIQUEUE_INITIALIZER(IPQ_MAXLEN, NETISR_IPV6); 117 118 struct cpumem *ip6counters; 119 120 uint8_t ip6_soiikey[IP6_SOIIKEY_LEN]; 121 122 int ip6_ours(struct mbuf **, int *, int, int); 123 int ip6_local(struct mbuf **, int *, int, int); 124 int ip6_check_rh0hdr(struct mbuf *, int *); 125 int ip6_hbhchcheck(struct mbuf **, int *, int *); 126 int ip6_hopopts_input(struct mbuf **, int *, u_int32_t *, u_int32_t *); 127 struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int); 128 int ip6_sysctl_soiikey(void *, size_t *, void *, size_t); 129 130 static struct mbuf_queue ip6send_mq; 131 132 static void ip6_send_dispatch(void *); 133 static struct task ip6send_task = 134 TASK_INITIALIZER(ip6_send_dispatch, &ip6send_mq); 135 136 /* 137 * IP6 initialization: fill in IP6 protocol switch table. 138 * All protocols not implemented in kernel go to raw IP6 protocol handler. 139 */ 140 void 141 ip6_init(void) 142 { 143 const struct protosw *pr; 144 int i; 145 146 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 147 if (pr == NULL) 148 panic("%s", __func__); 149 for (i = 0; i < IPPROTO_MAX; i++) 150 ip6_protox[i] = pr - inet6sw; 151 for (pr = inet6domain.dom_protosw; 152 pr < inet6domain.dom_protoswNPROTOSW; pr++) 153 if (pr->pr_domain->dom_family == PF_INET6 && 154 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW && 155 pr->pr_protocol < IPPROTO_MAX) 156 ip6_protox[pr->pr_protocol] = pr - inet6sw; 157 ip6_randomid_init(); 158 nd6_init(); 159 frag6_init(); 160 161 mq_init(&ip6send_mq, 64, IPL_SOFTNET); 162 163 ip6counters = counters_alloc(ip6s_ncounters); 164 #ifdef MROUTING 165 rt_timer_queue_init(&ip6_mrouterq, MCAST_EXPIRE_TIMEOUT, 166 &mf6c_expire_route); 167 #endif 168 } 169 170 /* 171 * Enqueue packet for local delivery. Queuing is used as a boundary 172 * between the network layer (input/forward path) running with shared 173 * NET_RLOCK_IN_SOFTNET() and the transport layer needing it exclusively. 174 */ 175 int 176 ip6_ours(struct mbuf **mp, int *offp, int nxt, int af) 177 { 178 /* We are already in a IPv4/IPv6 local deliver loop. */ 179 if (af != AF_UNSPEC) 180 return ip6_local(mp, offp, nxt, af); 181 182 niq_enqueue(&ip6intrq, *mp); 183 *mp = NULL; 184 return IPPROTO_DONE; 185 } 186 187 /* 188 * Dequeue and process locally delivered packets. 189 */ 190 void 191 ip6intr(void) 192 { 193 struct mbuf *m; 194 int off, nxt; 195 196 while ((m = niq_dequeue(&ip6intrq)) != NULL) { 197 #ifdef DIAGNOSTIC 198 if ((m->m_flags & M_PKTHDR) == 0) 199 panic("ip6intr no HDR"); 200 #endif 201 off = 0; 202 nxt = ip6_local(&m, &off, IPPROTO_IPV6, AF_UNSPEC); 203 KASSERT(nxt == IPPROTO_DONE); 204 } 205 } 206 207 void 208 ipv6_input(struct ifnet *ifp, struct mbuf *m) 209 { 210 int off, nxt; 211 212 off = 0; 213 nxt = ip6_input_if(&m, &off, IPPROTO_IPV6, AF_UNSPEC, ifp); 214 KASSERT(nxt == IPPROTO_DONE); 215 } 216 217 struct mbuf * 218 ipv6_check(struct ifnet *ifp, struct mbuf *m) 219 { 220 struct ip6_hdr *ip6; 221 222 if (m->m_len < sizeof(*ip6)) { 223 m = m_pullup(m, sizeof(*ip6)); 224 if (m == NULL) { 225 ip6stat_inc(ip6s_toosmall); 226 return (NULL); 227 } 228 } 229 230 ip6 = mtod(m, struct ip6_hdr *); 231 232 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 233 ip6stat_inc(ip6s_badvers); 234 goto bad; 235 } 236 237 /* 238 * Check against address spoofing/corruption. 239 */ 240 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 241 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 242 /* 243 * XXX: "badscope" is not very suitable for a multicast source. 244 */ 245 ip6stat_inc(ip6s_badscope); 246 goto bad; 247 } 248 if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) || 249 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) && 250 (ifp->if_flags & IFF_LOOPBACK) == 0) { 251 ip6stat_inc(ip6s_badscope); 252 goto bad; 253 } 254 /* Drop packets if interface ID portion is already filled. */ 255 if (((IN6_IS_SCOPE_EMBED(&ip6->ip6_src) && ip6->ip6_src.s6_addr16[1]) || 256 (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst) && ip6->ip6_dst.s6_addr16[1])) && 257 (ifp->if_flags & IFF_LOOPBACK) == 0) { 258 ip6stat_inc(ip6s_badscope); 259 goto bad; 260 } 261 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) && 262 !(m->m_flags & M_LOOP)) { 263 /* 264 * In this case, the packet should come from the loopback 265 * interface. However, we cannot just check the if_flags, 266 * because ip6_mloopback() passes the "actual" interface 267 * as the outgoing/incoming interface. 268 */ 269 ip6stat_inc(ip6s_badscope); 270 goto bad; 271 } 272 273 /* 274 * The following check is not documented in specs. A malicious 275 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack 276 * and bypass security checks (act as if it was from 127.0.0.1 by using 277 * IPv6 src ::ffff:127.0.0.1). Be cautious. 278 * 279 * This check chokes if we are in an SIIT cloud. As none of BSDs 280 * support IPv4-less kernel compilation, we cannot support SIIT 281 * environment at all. So, it makes more sense for us to reject any 282 * malicious packets for non-SIIT environment, than try to do a 283 * partial support for SIIT environment. 284 */ 285 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 286 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 287 ip6stat_inc(ip6s_badscope); 288 goto bad; 289 } 290 291 /* 292 * Reject packets with IPv4 compatible addresses (auto tunnel). 293 * 294 * The code forbids automatic tunneling as per RFC4213. 295 */ 296 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) || 297 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) { 298 ip6stat_inc(ip6s_badscope); 299 goto bad; 300 } 301 302 return (m); 303 bad: 304 m_freem(m); 305 return (NULL); 306 } 307 308 int 309 ip6_input_if(struct mbuf **mp, int *offp, int nxt, int af, struct ifnet *ifp) 310 { 311 struct mbuf *m; 312 struct ip6_hdr *ip6; 313 struct sockaddr_in6 sin6; 314 struct rtentry *rt = NULL; 315 int ours = 0; 316 u_int16_t src_scope, dst_scope; 317 #if NPF > 0 318 struct in6_addr odst; 319 #endif 320 int srcrt = 0; 321 322 KASSERT(*offp == 0); 323 324 ip6stat_inc(ip6s_total); 325 326 m = *mp = ipv6_check(ifp, *mp); 327 if (m == NULL) 328 goto bad; 329 330 ip6 = mtod(m, struct ip6_hdr *); 331 332 #if NCARP > 0 333 if (carp_lsdrop(ifp, m, AF_INET6, ip6->ip6_src.s6_addr32, 334 ip6->ip6_dst.s6_addr32, (ip6->ip6_nxt == IPPROTO_ICMPV6 ? 0 : 1))) 335 goto bad; 336 #endif 337 ip6stat_inc(ip6s_nxthist + ip6->ip6_nxt); 338 339 /* 340 * If the packet has been received on a loopback interface it 341 * can be destined to any local address, not necessarily to 342 * an address configured on `ifp'. 343 */ 344 if (ifp->if_flags & IFF_LOOPBACK) { 345 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) { 346 src_scope = ip6->ip6_src.s6_addr16[1]; 347 ip6->ip6_src.s6_addr16[1] = 0; 348 } 349 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) { 350 dst_scope = ip6->ip6_dst.s6_addr16[1]; 351 ip6->ip6_dst.s6_addr16[1] = 0; 352 } 353 } 354 355 #if NPF > 0 356 /* 357 * Packet filter 358 */ 359 odst = ip6->ip6_dst; 360 if (pf_test(AF_INET6, PF_IN, ifp, mp) != PF_PASS) 361 goto bad; 362 m = *mp; 363 if (m == NULL) 364 goto bad; 365 366 ip6 = mtod(m, struct ip6_hdr *); 367 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst); 368 #endif 369 370 /* 371 * Without embedded scope ID we cannot find link-local 372 * addresses in the routing table. 373 */ 374 if (ifp->if_flags & IFF_LOOPBACK) { 375 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) 376 ip6->ip6_src.s6_addr16[1] = src_scope; 377 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) 378 ip6->ip6_dst.s6_addr16[1] = dst_scope; 379 } else { 380 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) 381 ip6->ip6_src.s6_addr16[1] = htons(ifp->if_index); 382 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) 383 ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index); 384 } 385 386 /* 387 * Be more secure than RFC5095 and scan for type 0 routing headers. 388 * If pf has already scanned the header chain, do not do it twice. 389 */ 390 if (!(m->m_pkthdr.pf.flags & PF_TAG_PROCESSED) && 391 ip6_check_rh0hdr(m, offp)) { 392 ip6stat_inc(ip6s_badoptions); 393 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, *offp); 394 m = *mp = NULL; 395 goto bad; 396 } 397 398 #if NPF > 0 399 if (pf_ouraddr(m) == 1) { 400 nxt = ip6_ours(mp, offp, nxt, af); 401 goto out; 402 } 403 #endif 404 405 /* 406 * Multicast check 407 */ 408 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 409 /* 410 * Make sure M_MCAST is set. It should theoretically 411 * already be there, but let's play safe because upper 412 * layers check for this flag. 413 */ 414 m->m_flags |= M_MCAST; 415 416 /* 417 * See if we belong to the destination multicast group on the 418 * arrival interface. 419 */ 420 if (in6_hasmulti(&ip6->ip6_dst, ifp)) 421 ours = 1; 422 423 #ifdef MROUTING 424 if (ip6_mforwarding && ip6_mrouter[ifp->if_rdomain]) { 425 int error; 426 427 nxt = ip6_hbhchcheck(&m, offp, &ours); 428 if (nxt == IPPROTO_DONE) 429 goto out; 430 431 ip6 = mtod(m, struct ip6_hdr *); 432 433 /* 434 * If we are acting as a multicast router, all 435 * incoming multicast packets are passed to the 436 * kernel-level multicast forwarding function. 437 * The packet is returned (relatively) intact; if 438 * ip6_mforward() returns a non-zero value, the packet 439 * must be discarded, else it may be accepted below. 440 */ 441 KERNEL_LOCK(); 442 error = ip6_mforward(ip6, ifp, m); 443 KERNEL_UNLOCK(); 444 if (error) { 445 ip6stat_inc(ip6s_cantforward); 446 goto bad; 447 } 448 449 if (ours) { 450 if (af == AF_UNSPEC) 451 nxt = ip_deliver(mp, offp, nxt, 452 AF_INET6); 453 goto out; 454 } 455 goto bad; 456 } 457 #endif 458 if (!ours) { 459 ip6stat_inc(ip6s_notmember); 460 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) 461 ip6stat_inc(ip6s_cantforward); 462 goto bad; 463 } 464 nxt = ip6_ours(mp, offp, nxt, af); 465 goto out; 466 } 467 468 469 /* 470 * Unicast check 471 */ 472 memset(&sin6, 0, sizeof(struct sockaddr_in6)); 473 sin6.sin6_len = sizeof(struct sockaddr_in6); 474 sin6.sin6_family = AF_INET6; 475 sin6.sin6_addr = ip6->ip6_dst; 476 rt = rtalloc_mpath(sin6tosa(&sin6), &ip6->ip6_src.s6_addr32[0], 477 m->m_pkthdr.ph_rtableid); 478 479 /* 480 * Accept the packet if the route to the destination is marked 481 * as local. 482 */ 483 if (rtisvalid(rt) && ISSET(rt->rt_flags, RTF_LOCAL)) { 484 struct in6_ifaddr *ia6 = ifatoia6(rt->rt_ifa); 485 486 if (ip6_forwarding == 0 && rt->rt_ifidx != ifp->if_index && 487 !((ifp->if_flags & IFF_LOOPBACK) || 488 (ifp->if_type == IFT_ENC) || 489 (m->m_pkthdr.pf.flags & PF_TAG_TRANSLATE_LOCALHOST))) { 490 /* received on wrong interface */ 491 #if NCARP > 0 492 struct ifnet *out_if; 493 494 /* 495 * Virtual IPs on carp interfaces need to be checked 496 * also against the parent interface and other carp 497 * interfaces sharing the same parent. 498 */ 499 out_if = if_get(rt->rt_ifidx); 500 if (!(out_if && carp_strict_addr_chk(out_if, ifp))) { 501 ip6stat_inc(ip6s_wrongif); 502 if_put(out_if); 503 goto bad; 504 } 505 if_put(out_if); 506 #else 507 ip6stat_inc(ip6s_wrongif); 508 goto bad; 509 #endif 510 } 511 /* 512 * packets to a tentative, duplicated, or somehow invalid 513 * address must not be accepted. 514 */ 515 if ((ia6->ia6_flags & (IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED))) { 516 char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN]; 517 518 inet_ntop(AF_INET6, &ip6->ip6_src, src, sizeof(src)); 519 inet_ntop(AF_INET6, &ip6->ip6_dst, dst, sizeof(dst)); 520 /* address is not ready, so discard the packet. */ 521 nd6log((LOG_INFO, 522 "%s: packet to an unready address %s->%s\n", 523 __func__, src, dst)); 524 525 goto bad; 526 } else { 527 nxt = ip6_ours(mp, offp, nxt, af); 528 goto out; 529 } 530 } 531 532 #if NCARP > 0 533 if (ip6->ip6_nxt == IPPROTO_ICMPV6 && 534 carp_lsdrop(ifp, m, AF_INET6, ip6->ip6_src.s6_addr32, 535 ip6->ip6_dst.s6_addr32, 1)) 536 goto bad; 537 #endif 538 /* 539 * Now there is no reason to process the packet if it's not our own 540 * and we're not a router. 541 */ 542 if (!ip6_forwarding) { 543 ip6stat_inc(ip6s_cantforward); 544 goto bad; 545 } 546 547 nxt = ip6_hbhchcheck(&m, offp, &ours); 548 if (nxt == IPPROTO_DONE) 549 goto out; 550 551 if (ours) { 552 if (af == AF_UNSPEC) 553 nxt = ip_deliver(mp, offp, nxt, AF_INET6); 554 goto out; 555 } 556 557 #ifdef IPSEC 558 if (ipsec_in_use) { 559 int rv; 560 561 rv = ipsec_forward_check(m, *offp, AF_INET6); 562 if (rv != 0) { 563 ip6stat_inc(ip6s_cantforward); 564 goto bad; 565 } 566 /* 567 * Fall through, forward packet. Outbound IPsec policy 568 * checking will occur in ip6_forward(). 569 */ 570 } 571 #endif /* IPSEC */ 572 573 ip6_forward(m, rt, srcrt); 574 *mp = NULL; 575 return IPPROTO_DONE; 576 bad: 577 nxt = IPPROTO_DONE; 578 m_freemp(mp); 579 out: 580 rtfree(rt); 581 return nxt; 582 } 583 584 int 585 ip6_local(struct mbuf **mp, int *offp, int nxt, int af) 586 { 587 NET_ASSERT_WLOCKED(); 588 589 nxt = ip6_hbhchcheck(mp, offp, NULL); 590 if (nxt == IPPROTO_DONE) 591 return IPPROTO_DONE; 592 593 /* Check whether we are already in a IPv4/IPv6 local deliver loop. */ 594 if (af == AF_UNSPEC) 595 nxt = ip_deliver(mp, offp, nxt, AF_INET6); 596 return nxt; 597 } 598 599 /* On error free mbuf and return IPPROTO_DONE. */ 600 int 601 ip6_hbhchcheck(struct mbuf **mp, int *offp, int *oursp) 602 { 603 struct ip6_hdr *ip6; 604 u_int32_t plen, rtalert = ~0; 605 int nxt; 606 607 ip6 = mtod(*mp, struct ip6_hdr *); 608 609 /* 610 * Process Hop-by-Hop options header if it's contained. 611 * m may be modified in ip6_hopopts_input(). 612 * If a JumboPayload option is included, plen will also be modified. 613 */ 614 plen = (u_int32_t)ntohs(ip6->ip6_plen); 615 *offp = sizeof(struct ip6_hdr); 616 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 617 struct ip6_hbh *hbh; 618 619 if (ip6_hopopts_input(mp, offp, &plen, &rtalert)) 620 goto bad; /* m have already been freed */ 621 622 /* adjust pointer */ 623 ip6 = mtod(*mp, struct ip6_hdr *); 624 625 /* 626 * if the payload length field is 0 and the next header field 627 * indicates Hop-by-Hop Options header, then a Jumbo Payload 628 * option MUST be included. 629 */ 630 if (ip6->ip6_plen == 0 && plen == 0) { 631 /* 632 * Note that if a valid jumbo payload option is 633 * contained, ip6_hopopts_input() must set a valid 634 * (non-zero) payload length to the variable plen. 635 */ 636 ip6stat_inc(ip6s_badoptions); 637 icmp6_error(*mp, ICMP6_PARAM_PROB, 638 ICMP6_PARAMPROB_HEADER, 639 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); 640 goto bad; 641 } 642 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, *mp, 643 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 644 if (hbh == NULL) { 645 ip6stat_inc(ip6s_tooshort); 646 goto bad; 647 } 648 nxt = hbh->ip6h_nxt; 649 650 /* 651 * accept the packet if a router alert option is included 652 * and we act as an IPv6 router. 653 */ 654 if (rtalert != ~0 && ip6_forwarding && oursp != NULL) 655 *oursp = 1; 656 } else 657 nxt = ip6->ip6_nxt; 658 659 /* 660 * Check that the amount of data in the buffers 661 * is as at least much as the IPv6 header would have us expect. 662 * Trim mbufs if longer than we expect. 663 * Drop packet if shorter than we expect. 664 */ 665 if ((*mp)->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 666 ip6stat_inc(ip6s_tooshort); 667 m_freemp(mp); 668 goto bad; 669 } 670 if ((*mp)->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 671 if ((*mp)->m_len == (*mp)->m_pkthdr.len) { 672 (*mp)->m_len = sizeof(struct ip6_hdr) + plen; 673 (*mp)->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 674 } else { 675 m_adj((*mp), sizeof(struct ip6_hdr) + plen - 676 (*mp)->m_pkthdr.len); 677 } 678 } 679 680 return nxt; 681 bad: 682 return IPPROTO_DONE; 683 } 684 685 /* scan packet for RH0 routing header. Mostly stolen from pf.c:pf_test() */ 686 int 687 ip6_check_rh0hdr(struct mbuf *m, int *offp) 688 { 689 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 690 struct ip6_rthdr rthdr; 691 struct ip6_ext opt6; 692 u_int8_t proto = ip6->ip6_nxt; 693 int done = 0, lim, off, rh_cnt = 0; 694 695 off = ((caddr_t)ip6 - m->m_data) + sizeof(struct ip6_hdr); 696 lim = min(m->m_pkthdr.len, ntohs(ip6->ip6_plen) + sizeof(*ip6)); 697 do { 698 switch (proto) { 699 case IPPROTO_ROUTING: 700 *offp = off; 701 if (rh_cnt++) { 702 /* more than one rh header present */ 703 return (1); 704 } 705 706 if (off + sizeof(rthdr) > lim) { 707 /* packet to short to make sense */ 708 return (1); 709 } 710 711 m_copydata(m, off, sizeof(rthdr), &rthdr); 712 713 if (rthdr.ip6r_type == IPV6_RTHDR_TYPE_0) { 714 *offp += offsetof(struct ip6_rthdr, ip6r_type); 715 return (1); 716 } 717 718 off += (rthdr.ip6r_len + 1) * 8; 719 proto = rthdr.ip6r_nxt; 720 break; 721 case IPPROTO_AH: 722 case IPPROTO_HOPOPTS: 723 case IPPROTO_DSTOPTS: 724 /* get next header and header length */ 725 if (off + sizeof(opt6) > lim) { 726 /* 727 * Packet to short to make sense, we could 728 * reject the packet but as a router we 729 * should not do that so forward it. 730 */ 731 return (0); 732 } 733 734 m_copydata(m, off, sizeof(opt6), &opt6); 735 736 if (proto == IPPROTO_AH) 737 off += (opt6.ip6e_len + 2) * 4; 738 else 739 off += (opt6.ip6e_len + 1) * 8; 740 proto = opt6.ip6e_nxt; 741 break; 742 case IPPROTO_FRAGMENT: 743 default: 744 /* end of header stack */ 745 done = 1; 746 break; 747 } 748 } while (!done); 749 750 return (0); 751 } 752 753 /* 754 * Hop-by-Hop options header processing. If a valid jumbo payload option is 755 * included, the real payload length will be stored in plenp. 756 * On error free mbuf and return -1. 757 * 758 * rtalertp - XXX: should be stored in a more smart way 759 */ 760 int 761 ip6_hopopts_input(struct mbuf **mp, int *offp, u_int32_t *plenp, 762 u_int32_t *rtalertp) 763 { 764 int off = *offp, hbhlen; 765 struct ip6_hbh *hbh; 766 767 /* validation of the length of the header */ 768 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, *mp, 769 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 770 if (hbh == NULL) { 771 ip6stat_inc(ip6s_tooshort); 772 return -1; 773 } 774 hbhlen = (hbh->ip6h_len + 1) << 3; 775 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, *mp, sizeof(struct ip6_hdr), 776 hbhlen); 777 if (hbh == NULL) { 778 ip6stat_inc(ip6s_tooshort); 779 return -1; 780 } 781 off += hbhlen; 782 hbhlen -= sizeof(struct ip6_hbh); 783 784 if (ip6_process_hopopts(mp, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 785 hbhlen, rtalertp, plenp) < 0) 786 return (-1); 787 788 *offp = off; 789 return (0); 790 } 791 792 /* 793 * Search header for all Hop-by-hop options and process each option. 794 * This function is separate from ip6_hopopts_input() in order to 795 * handle a case where the sending node itself process its hop-by-hop 796 * options header. In such a case, the function is called from ip6_output(). 797 * On error free mbuf and return -1. 798 * 799 * The function assumes that hbh header is located right after the IPv6 header 800 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 801 * opthead + hbhlen is located in continuous memory region. 802 */ 803 int 804 ip6_process_hopopts(struct mbuf **mp, u_int8_t *opthead, int hbhlen, 805 u_int32_t *rtalertp, u_int32_t *plenp) 806 { 807 struct ip6_hdr *ip6; 808 int optlen = 0; 809 u_int8_t *opt = opthead; 810 u_int16_t rtalert_val; 811 u_int32_t jumboplen; 812 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 813 814 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 815 switch (*opt) { 816 case IP6OPT_PAD1: 817 optlen = 1; 818 break; 819 case IP6OPT_PADN: 820 if (hbhlen < IP6OPT_MINLEN) { 821 ip6stat_inc(ip6s_toosmall); 822 goto bad; 823 } 824 optlen = *(opt + 1) + 2; 825 break; 826 case IP6OPT_ROUTER_ALERT: 827 /* XXX may need check for alignment */ 828 if (hbhlen < IP6OPT_RTALERT_LEN) { 829 ip6stat_inc(ip6s_toosmall); 830 goto bad; 831 } 832 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 833 /* XXX stat */ 834 icmp6_error(*mp, ICMP6_PARAM_PROB, 835 ICMP6_PARAMPROB_HEADER, 836 erroff + opt + 1 - opthead); 837 return (-1); 838 } 839 optlen = IP6OPT_RTALERT_LEN; 840 memcpy((caddr_t)&rtalert_val, (caddr_t)(opt + 2), 2); 841 *rtalertp = ntohs(rtalert_val); 842 break; 843 case IP6OPT_JUMBO: 844 /* XXX may need check for alignment */ 845 if (hbhlen < IP6OPT_JUMBO_LEN) { 846 ip6stat_inc(ip6s_toosmall); 847 goto bad; 848 } 849 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 850 /* XXX stat */ 851 icmp6_error(*mp, ICMP6_PARAM_PROB, 852 ICMP6_PARAMPROB_HEADER, 853 erroff + opt + 1 - opthead); 854 return (-1); 855 } 856 optlen = IP6OPT_JUMBO_LEN; 857 858 /* 859 * IPv6 packets that have non 0 payload length 860 * must not contain a jumbo payload option. 861 */ 862 ip6 = mtod(*mp, struct ip6_hdr *); 863 if (ip6->ip6_plen) { 864 ip6stat_inc(ip6s_badoptions); 865 icmp6_error(*mp, ICMP6_PARAM_PROB, 866 ICMP6_PARAMPROB_HEADER, 867 erroff + opt - opthead); 868 return (-1); 869 } 870 871 /* 872 * We may see jumbolen in unaligned location, so 873 * we'd need to perform memcpy(). 874 */ 875 memcpy(&jumboplen, opt + 2, sizeof(jumboplen)); 876 jumboplen = (u_int32_t)htonl(jumboplen); 877 878 #if 1 879 /* 880 * if there are multiple jumbo payload options, 881 * *plenp will be non-zero and the packet will be 882 * rejected. 883 * the behavior may need some debate in ipngwg - 884 * multiple options does not make sense, however, 885 * there's no explicit mention in specification. 886 */ 887 if (*plenp != 0) { 888 ip6stat_inc(ip6s_badoptions); 889 icmp6_error(*mp, ICMP6_PARAM_PROB, 890 ICMP6_PARAMPROB_HEADER, 891 erroff + opt + 2 - opthead); 892 return (-1); 893 } 894 #endif 895 896 /* 897 * jumbo payload length must be larger than 65535. 898 */ 899 if (jumboplen <= IPV6_MAXPACKET) { 900 ip6stat_inc(ip6s_badoptions); 901 icmp6_error(*mp, ICMP6_PARAM_PROB, 902 ICMP6_PARAMPROB_HEADER, 903 erroff + opt + 2 - opthead); 904 return (-1); 905 } 906 *plenp = jumboplen; 907 908 break; 909 default: /* unknown option */ 910 if (hbhlen < IP6OPT_MINLEN) { 911 ip6stat_inc(ip6s_toosmall); 912 goto bad; 913 } 914 optlen = ip6_unknown_opt(mp, opt, 915 erroff + opt - opthead); 916 if (optlen == -1) 917 return (-1); 918 optlen += 2; 919 break; 920 } 921 } 922 923 return (0); 924 925 bad: 926 m_freemp(mp); 927 return (-1); 928 } 929 930 /* 931 * Unknown option processing. 932 * The third argument `off' is the offset from the IPv6 header to the option, 933 * which allows returning an ICMPv6 error even if the IPv6 header and the 934 * option header are not continuous. 935 * On error free mbuf and return -1. 936 */ 937 int 938 ip6_unknown_opt(struct mbuf **mp, u_int8_t *optp, int off) 939 { 940 struct ip6_hdr *ip6; 941 942 switch (IP6OPT_TYPE(*optp)) { 943 case IP6OPT_TYPE_SKIP: /* ignore the option */ 944 return ((int)*(optp + 1)); 945 case IP6OPT_TYPE_DISCARD: /* silently discard */ 946 m_freemp(mp); 947 return (-1); 948 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 949 ip6stat_inc(ip6s_badoptions); 950 icmp6_error(*mp, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 951 return (-1); 952 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 953 ip6stat_inc(ip6s_badoptions); 954 ip6 = mtod(*mp, struct ip6_hdr *); 955 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 956 ((*mp)->m_flags & (M_BCAST|M_MCAST))) 957 m_freemp(mp); 958 else 959 icmp6_error(*mp, ICMP6_PARAM_PROB, 960 ICMP6_PARAMPROB_OPTION, off); 961 return (-1); 962 } 963 964 m_freemp(mp); /* XXX: NOTREACHED */ 965 return (-1); 966 } 967 968 /* 969 * Create the "control" list for this pcb. 970 * 971 * The routine will be called from upper layer handlers like udp_input(). 972 * Thus the routine assumes that the caller (udp_input) have already 973 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 974 * very first mbuf on the mbuf chain. 975 * We may want to add some infinite loop prevention or sanity checks for safety. 976 * (This applies only when you are using KAME mbuf chain restriction, i.e. 977 * you are using IP6_EXTHDR_CHECK() not m_pulldown()) 978 */ 979 void 980 ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp) 981 { 982 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 983 984 if (in6p->inp_socket->so_options & SO_TIMESTAMP) { 985 struct timeval tv; 986 987 m_microtime(m, &tv); 988 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 989 SCM_TIMESTAMP, SOL_SOCKET); 990 if (*mp) 991 mp = &(*mp)->m_next; 992 } 993 994 /* RFC 2292 sec. 5 */ 995 if ((in6p->inp_flags & IN6P_PKTINFO) != 0) { 996 struct in6_pktinfo pi6; 997 memcpy(&pi6.ipi6_addr, &ip6->ip6_dst, sizeof(struct in6_addr)); 998 if (IN6_IS_SCOPE_EMBED(&pi6.ipi6_addr)) 999 pi6.ipi6_addr.s6_addr16[1] = 0; 1000 pi6.ipi6_ifindex = m ? m->m_pkthdr.ph_ifidx : 0; 1001 *mp = sbcreatecontrol((caddr_t) &pi6, 1002 sizeof(struct in6_pktinfo), 1003 IPV6_PKTINFO, IPPROTO_IPV6); 1004 if (*mp) 1005 mp = &(*mp)->m_next; 1006 } 1007 1008 if ((in6p->inp_flags & IN6P_HOPLIMIT) != 0) { 1009 int hlim = ip6->ip6_hlim & 0xff; 1010 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int), 1011 IPV6_HOPLIMIT, IPPROTO_IPV6); 1012 if (*mp) 1013 mp = &(*mp)->m_next; 1014 } 1015 1016 if ((in6p->inp_flags & IN6P_TCLASS) != 0) { 1017 u_int32_t flowinfo; 1018 int tclass; 1019 1020 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK); 1021 flowinfo >>= 20; 1022 1023 tclass = flowinfo & 0xff; 1024 *mp = sbcreatecontrol((caddr_t)&tclass, sizeof(tclass), 1025 IPV6_TCLASS, IPPROTO_IPV6); 1026 if (*mp) 1027 mp = &(*mp)->m_next; 1028 } 1029 1030 /* 1031 * IPV6_HOPOPTS socket option. Recall that we required super-user 1032 * privilege for the option (see ip6_ctloutput), but it might be too 1033 * strict, since there might be some hop-by-hop options which can be 1034 * returned to normal user. 1035 * See also RFC 2292 section 6 (or RFC 3542 section 8). 1036 */ 1037 if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) { 1038 /* 1039 * Check if a hop-by-hop options header is contained in the 1040 * received packet, and if so, store the options as ancillary 1041 * data. Note that a hop-by-hop options header must be 1042 * just after the IPv6 header, which is assured through the 1043 * IPv6 input processing. 1044 */ 1045 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1046 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 1047 struct ip6_hbh *hbh; 1048 int hbhlen = 0; 1049 struct mbuf *ext; 1050 1051 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 1052 ip6->ip6_nxt); 1053 if (ext == NULL) { 1054 ip6stat_inc(ip6s_tooshort); 1055 return; 1056 } 1057 hbh = mtod(ext, struct ip6_hbh *); 1058 hbhlen = (hbh->ip6h_len + 1) << 3; 1059 if (hbhlen != ext->m_len) { 1060 m_freem(ext); 1061 ip6stat_inc(ip6s_tooshort); 1062 return; 1063 } 1064 1065 /* 1066 * XXX: We copy the whole header even if a 1067 * jumbo payload option is included, the option which 1068 * is to be removed before returning according to 1069 * RFC2292. 1070 * Note: this constraint is removed in RFC3542. 1071 */ 1072 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 1073 IPV6_HOPOPTS, 1074 IPPROTO_IPV6); 1075 if (*mp) 1076 mp = &(*mp)->m_next; 1077 m_freem(ext); 1078 } 1079 } 1080 1081 /* IPV6_DSTOPTS and IPV6_RTHDR socket options */ 1082 if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) { 1083 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1084 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 1085 1086 /* 1087 * Search for destination options headers or routing 1088 * header(s) through the header chain, and stores each 1089 * header as ancillary data. 1090 * Note that the order of the headers remains in 1091 * the chain of ancillary data. 1092 */ 1093 while (1) { /* is explicit loop prevention necessary? */ 1094 struct ip6_ext *ip6e = NULL; 1095 int elen; 1096 struct mbuf *ext = NULL; 1097 1098 /* 1099 * if it is not an extension header, don't try to 1100 * pull it from the chain. 1101 */ 1102 switch (nxt) { 1103 case IPPROTO_DSTOPTS: 1104 case IPPROTO_ROUTING: 1105 case IPPROTO_HOPOPTS: 1106 case IPPROTO_AH: /* is it possible? */ 1107 break; 1108 default: 1109 goto loopend; 1110 } 1111 1112 ext = ip6_pullexthdr(m, off, nxt); 1113 if (ext == NULL) { 1114 ip6stat_inc(ip6s_tooshort); 1115 return; 1116 } 1117 ip6e = mtod(ext, struct ip6_ext *); 1118 if (nxt == IPPROTO_AH) 1119 elen = (ip6e->ip6e_len + 2) << 2; 1120 else 1121 elen = (ip6e->ip6e_len + 1) << 3; 1122 if (elen != ext->m_len) { 1123 m_freem(ext); 1124 ip6stat_inc(ip6s_tooshort); 1125 return; 1126 } 1127 1128 switch (nxt) { 1129 case IPPROTO_DSTOPTS: 1130 if (!(in6p->inp_flags & IN6P_DSTOPTS)) 1131 break; 1132 1133 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1134 IPV6_DSTOPTS, 1135 IPPROTO_IPV6); 1136 if (*mp) 1137 mp = &(*mp)->m_next; 1138 break; 1139 1140 case IPPROTO_ROUTING: 1141 if (!(in6p->inp_flags & IN6P_RTHDR)) 1142 break; 1143 1144 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1145 IPV6_RTHDR, 1146 IPPROTO_IPV6); 1147 if (*mp) 1148 mp = &(*mp)->m_next; 1149 break; 1150 1151 case IPPROTO_HOPOPTS: 1152 case IPPROTO_AH: /* is it possible? */ 1153 break; 1154 1155 default: 1156 /* 1157 * other cases have been filtered in the above. 1158 * none will visit this case. here we supply 1159 * the code just in case (nxt overwritten or 1160 * other cases). 1161 */ 1162 m_freem(ext); 1163 goto loopend; 1164 1165 } 1166 1167 /* proceed with the next header. */ 1168 off += elen; 1169 nxt = ip6e->ip6e_nxt; 1170 ip6e = NULL; 1171 m_freem(ext); 1172 ext = NULL; 1173 } 1174 loopend: 1175 ; 1176 } 1177 } 1178 1179 /* 1180 * pull single extension header from mbuf chain. returns single mbuf that 1181 * contains the result, or NULL on error. 1182 */ 1183 struct mbuf * 1184 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt) 1185 { 1186 struct ip6_ext ip6e; 1187 size_t elen; 1188 struct mbuf *n; 1189 1190 #ifdef DIAGNOSTIC 1191 switch (nxt) { 1192 case IPPROTO_DSTOPTS: 1193 case IPPROTO_ROUTING: 1194 case IPPROTO_HOPOPTS: 1195 case IPPROTO_AH: /* is it possible? */ 1196 break; 1197 default: 1198 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 1199 } 1200 #endif 1201 1202 if (off + sizeof(ip6e) > m->m_pkthdr.len) 1203 return NULL; 1204 1205 m_copydata(m, off, sizeof(ip6e), &ip6e); 1206 if (nxt == IPPROTO_AH) 1207 elen = (ip6e.ip6e_len + 2) << 2; 1208 else 1209 elen = (ip6e.ip6e_len + 1) << 3; 1210 1211 if (off + elen > m->m_pkthdr.len) 1212 return NULL; 1213 1214 MGET(n, M_DONTWAIT, MT_DATA); 1215 if (n && elen >= MLEN) { 1216 MCLGET(n, M_DONTWAIT); 1217 if ((n->m_flags & M_EXT) == 0) { 1218 m_free(n); 1219 n = NULL; 1220 } 1221 } 1222 if (!n) 1223 return NULL; 1224 1225 n->m_len = 0; 1226 if (elen >= m_trailingspace(n)) { 1227 m_free(n); 1228 return NULL; 1229 } 1230 1231 m_copydata(m, off, elen, mtod(n, caddr_t)); 1232 n->m_len = elen; 1233 return n; 1234 } 1235 1236 /* 1237 * Get offset to the previous header followed by the header 1238 * currently processed. 1239 */ 1240 int 1241 ip6_get_prevhdr(struct mbuf *m, int off) 1242 { 1243 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1244 1245 if (off == sizeof(struct ip6_hdr)) { 1246 return offsetof(struct ip6_hdr, ip6_nxt); 1247 } else if (off < sizeof(struct ip6_hdr)) { 1248 panic("%s: off < sizeof(struct ip6_hdr)", __func__); 1249 } else { 1250 int len, nlen, nxt; 1251 struct ip6_ext ip6e; 1252 1253 nxt = ip6->ip6_nxt; 1254 len = sizeof(struct ip6_hdr); 1255 nlen = 0; 1256 while (len < off) { 1257 m_copydata(m, len, sizeof(ip6e), &ip6e); 1258 1259 switch (nxt) { 1260 case IPPROTO_FRAGMENT: 1261 nlen = sizeof(struct ip6_frag); 1262 break; 1263 case IPPROTO_AH: 1264 nlen = (ip6e.ip6e_len + 2) << 2; 1265 break; 1266 default: 1267 nlen = (ip6e.ip6e_len + 1) << 3; 1268 break; 1269 } 1270 len += nlen; 1271 nxt = ip6e.ip6e_nxt; 1272 } 1273 1274 return (len - nlen); 1275 } 1276 } 1277 1278 /* 1279 * get next header offset. m will be retained. 1280 */ 1281 int 1282 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp) 1283 { 1284 struct ip6_hdr ip6; 1285 struct ip6_ext ip6e; 1286 struct ip6_frag fh; 1287 1288 /* just in case */ 1289 if (m == NULL) 1290 panic("%s: m == NULL", __func__); 1291 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 1292 return -1; 1293 1294 switch (proto) { 1295 case IPPROTO_IPV6: 1296 if (m->m_pkthdr.len < off + sizeof(ip6)) 1297 return -1; 1298 m_copydata(m, off, sizeof(ip6), &ip6); 1299 if (nxtp) 1300 *nxtp = ip6.ip6_nxt; 1301 off += sizeof(ip6); 1302 return off; 1303 1304 case IPPROTO_FRAGMENT: 1305 /* 1306 * terminate parsing if it is not the first fragment, 1307 * it does not make sense to parse through it. 1308 */ 1309 if (m->m_pkthdr.len < off + sizeof(fh)) 1310 return -1; 1311 m_copydata(m, off, sizeof(fh), &fh); 1312 if ((fh.ip6f_offlg & IP6F_OFF_MASK) != 0) 1313 return -1; 1314 if (nxtp) 1315 *nxtp = fh.ip6f_nxt; 1316 off += sizeof(struct ip6_frag); 1317 return off; 1318 1319 case IPPROTO_AH: 1320 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1321 return -1; 1322 m_copydata(m, off, sizeof(ip6e), &ip6e); 1323 if (nxtp) 1324 *nxtp = ip6e.ip6e_nxt; 1325 off += (ip6e.ip6e_len + 2) << 2; 1326 if (m->m_pkthdr.len < off) 1327 return -1; 1328 return off; 1329 1330 case IPPROTO_HOPOPTS: 1331 case IPPROTO_ROUTING: 1332 case IPPROTO_DSTOPTS: 1333 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1334 return -1; 1335 m_copydata(m, off, sizeof(ip6e), &ip6e); 1336 if (nxtp) 1337 *nxtp = ip6e.ip6e_nxt; 1338 off += (ip6e.ip6e_len + 1) << 3; 1339 if (m->m_pkthdr.len < off) 1340 return -1; 1341 return off; 1342 1343 case IPPROTO_NONE: 1344 case IPPROTO_ESP: 1345 case IPPROTO_IPCOMP: 1346 /* give up */ 1347 return -1; 1348 1349 default: 1350 return -1; 1351 } 1352 1353 return -1; 1354 } 1355 1356 /* 1357 * get offset for the last header in the chain. m will be kept untainted. 1358 */ 1359 int 1360 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp) 1361 { 1362 int newoff; 1363 int nxt; 1364 1365 if (!nxtp) { 1366 nxt = -1; 1367 nxtp = &nxt; 1368 } 1369 while (1) { 1370 newoff = ip6_nexthdr(m, off, proto, nxtp); 1371 if (newoff < 0) 1372 return off; 1373 else if (newoff < off) 1374 return -1; /* invalid */ 1375 else if (newoff == off) 1376 return newoff; 1377 1378 off = newoff; 1379 proto = *nxtp; 1380 } 1381 } 1382 1383 /* 1384 * System control for IP6 1385 */ 1386 1387 const u_char inet6ctlerrmap[PRC_NCMDS] = { 1388 0, 0, 0, 0, 1389 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1390 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1391 EMSGSIZE, EHOSTUNREACH, 0, 0, 1392 0, 0, 0, 0, 1393 ENOPROTOOPT 1394 }; 1395 1396 #ifdef MROUTING 1397 extern int ip6_mrtproto; 1398 #endif 1399 1400 const struct sysctl_bounded_args ipv6ctl_vars[] = { 1401 { IPV6CTL_DAD_PENDING, &ip6_dad_pending, SYSCTL_INT_READONLY }, 1402 #ifdef MROUTING 1403 { IPV6CTL_MRTPROTO, &ip6_mrtproto, SYSCTL_INT_READONLY }, 1404 #endif 1405 { IPV6CTL_FORWARDING, &ip6_forwarding, 0, 1 }, 1406 { IPV6CTL_SENDREDIRECTS, &ip6_sendredirects, 0, 1 }, 1407 { IPV6CTL_DEFHLIM, &ip6_defhlim, 0, 255 }, 1408 { IPV6CTL_MAXFRAGPACKETS, &ip6_maxfragpackets, 0, 1000 }, 1409 { IPV6CTL_LOG_INTERVAL, &ip6_log_interval, 0, INT_MAX }, 1410 { IPV6CTL_HDRNESTLIMIT, &ip6_hdrnestlimit, 0, 100 }, 1411 { IPV6CTL_DAD_COUNT, &ip6_dad_count, 0, 10 }, 1412 { IPV6CTL_AUTO_FLOWLABEL, &ip6_auto_flowlabel, 0, 1 }, 1413 { IPV6CTL_DEFMCASTHLIM, &ip6_defmcasthlim, 0, 255 }, 1414 { IPV6CTL_USE_DEPRECATED, &ip6_use_deprecated, 0, 1 }, 1415 { IPV6CTL_MAXFRAGS, &ip6_maxfrags, 0, 1000 }, 1416 { IPV6CTL_MFORWARDING, &ip6_mforwarding, 0, 1 }, 1417 { IPV6CTL_MULTIPATH, &ip6_multipath, 0, 1 }, 1418 { IPV6CTL_MCAST_PMTU, &ip6_mcast_pmtu, 0, 1 }, 1419 { IPV6CTL_NEIGHBORGCTHRESH, &ip6_neighborgcthresh, -1, 5 * 2048 }, 1420 { IPV6CTL_MAXDYNROUTES, &ip6_maxdynroutes, -1, 5 * 4096 }, 1421 }; 1422 1423 int 1424 ip6_sysctl_ip6stat(void *oldp, size_t *oldlenp, void *newp) 1425 { 1426 struct ip6stat *ip6stat; 1427 int ret; 1428 1429 CTASSERT(sizeof(*ip6stat) == (ip6s_ncounters * sizeof(uint64_t))); 1430 1431 ip6stat = malloc(sizeof(*ip6stat), M_TEMP, M_WAITOK); 1432 counters_read(ip6counters, (uint64_t *)ip6stat, ip6s_ncounters); 1433 ret = sysctl_rdstruct(oldp, oldlenp, newp, 1434 ip6stat, sizeof(*ip6stat)); 1435 free(ip6stat, M_TEMP, sizeof(*ip6stat)); 1436 1437 return (ret); 1438 } 1439 1440 int 1441 ip6_sysctl_soiikey(void *oldp, size_t *oldlenp, void *newp, size_t newlen) 1442 { 1443 uint8_t oldkey[IP6_SOIIKEY_LEN]; 1444 int error; 1445 1446 error = suser(curproc); 1447 if (error != 0) 1448 return (error); 1449 1450 memcpy(oldkey, ip6_soiikey, sizeof(oldkey)); 1451 1452 error = sysctl_struct(oldp, oldlenp, newp, newlen, ip6_soiikey, 1453 sizeof(ip6_soiikey)); 1454 1455 return (error); 1456 } 1457 1458 int 1459 ip6_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, 1460 void *newp, size_t newlen) 1461 { 1462 #ifdef MROUTING 1463 extern struct mrt6stat mrt6stat; 1464 #endif 1465 int error; 1466 1467 /* Almost all sysctl names at this level are terminal. */ 1468 if (namelen != 1 && name[0] != IPV6CTL_IFQUEUE) 1469 return (ENOTDIR); 1470 1471 switch (name[0]) { 1472 case IPV6CTL_STATS: 1473 return (ip6_sysctl_ip6stat(oldp, oldlenp, newp)); 1474 #ifdef MROUTING 1475 case IPV6CTL_MRTSTATS: 1476 if (newp != NULL) 1477 return (EPERM); 1478 NET_LOCK(); 1479 error = sysctl_struct(oldp, oldlenp, newp, newlen, 1480 &mrt6stat, sizeof(mrt6stat)); 1481 NET_UNLOCK(); 1482 return (error); 1483 case IPV6CTL_MRTMIF: 1484 if (newp) 1485 return (EPERM); 1486 NET_LOCK(); 1487 error = mrt6_sysctl_mif(oldp, oldlenp); 1488 NET_UNLOCK(); 1489 return (error); 1490 case IPV6CTL_MRTMFC: 1491 if (newp) 1492 return (EPERM); 1493 NET_LOCK(); 1494 error = mrt6_sysctl_mfc(oldp, oldlenp); 1495 NET_UNLOCK(); 1496 return (error); 1497 #else 1498 case IPV6CTL_MRTSTATS: 1499 case IPV6CTL_MRTPROTO: 1500 case IPV6CTL_MRTMIF: 1501 case IPV6CTL_MRTMFC: 1502 return (EOPNOTSUPP); 1503 #endif 1504 case IPV6CTL_MTUDISCTIMEOUT: 1505 NET_LOCK(); 1506 error = sysctl_int_bounded(oldp, oldlenp, newp, newlen, 1507 &ip6_mtudisc_timeout, 0, INT_MAX); 1508 rt_timer_queue_change(&icmp6_mtudisc_timeout_q, 1509 ip6_mtudisc_timeout); 1510 NET_UNLOCK(); 1511 return (error); 1512 case IPV6CTL_IFQUEUE: 1513 return (sysctl_niq(name + 1, namelen - 1, 1514 oldp, oldlenp, newp, newlen, &ip6intrq)); 1515 case IPV6CTL_SOIIKEY: 1516 return (ip6_sysctl_soiikey(oldp, oldlenp, newp, newlen)); 1517 default: 1518 NET_LOCK(); 1519 error = sysctl_bounded_arr(ipv6ctl_vars, nitems(ipv6ctl_vars), 1520 name, namelen, oldp, oldlenp, newp, newlen); 1521 NET_UNLOCK(); 1522 return (error); 1523 } 1524 /* NOTREACHED */ 1525 } 1526 1527 void 1528 ip6_send_dispatch(void *xmq) 1529 { 1530 struct mbuf_queue *mq = xmq; 1531 struct mbuf *m; 1532 struct mbuf_list ml; 1533 1534 mq_delist(mq, &ml); 1535 if (ml_empty(&ml)) 1536 return; 1537 1538 NET_LOCK(); 1539 while ((m = ml_dequeue(&ml)) != NULL) { 1540 ip6_output(m, NULL, NULL, 0, NULL, NULL); 1541 } 1542 NET_UNLOCK(); 1543 } 1544 1545 void 1546 ip6_send(struct mbuf *m) 1547 { 1548 mq_enqueue(&ip6send_mq, m); 1549 task_add(net_tq(0), &ip6send_task); 1550 } 1551