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