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