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