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