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