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