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