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