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