1 /* $FreeBSD: src/sys/netinet6/ip6_input.c,v 1.11.2.15 2003/01/24 05:11:35 sam Exp $ */ 2 /* $KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei 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 "opt_ip6fw.h" 65 #include "opt_inet.h" 66 #include "opt_inet6.h" 67 #include "opt_ipsec.h" 68 69 #include <sys/param.h> 70 #include <sys/systm.h> 71 #include <sys/malloc.h> 72 #include <sys/mbuf.h> 73 #include <sys/domain.h> 74 #include <sys/protosw.h> 75 #include <sys/socket.h> 76 #include <sys/socketvar.h> 77 #include <sys/errno.h> 78 #include <sys/time.h> 79 #include <sys/kernel.h> 80 #include <sys/syslog.h> 81 #include <sys/proc.h> 82 #include <sys/priv.h> 83 84 #include <net/if.h> 85 #include <net/if_types.h> 86 #include <net/if_dl.h> 87 #include <net/route.h> 88 #include <net/netisr.h> 89 #include <net/pfil.h> 90 91 #include <sys/thread2.h> 92 #include <sys/msgport2.h> 93 #include <net/netmsg2.h> 94 #include <net/netisr2.h> 95 96 #include <netinet/in.h> 97 #include <netinet/in_systm.h> 98 #ifdef INET 99 #include <netinet/ip.h> 100 #include <netinet/ip_icmp.h> 101 #endif /* INET */ 102 #include <netinet/ip6.h> 103 #include <netinet6/in6_var.h> 104 #include <netinet6/ip6_var.h> 105 #include <netinet/in_pcb.h> 106 #include <netinet/icmp6.h> 107 #include <netinet6/scope6_var.h> 108 #include <netinet6/in6_ifattach.h> 109 #include <netinet6/nd6.h> 110 111 #ifdef IPSEC 112 #include <netinet6/ipsec.h> 113 #ifdef INET6 114 #include <netinet6/ipsec6.h> 115 #endif 116 #endif 117 118 #ifdef FAST_IPSEC 119 #include <netproto/ipsec/ipsec.h> 120 #include <netproto/ipsec/ipsec6.h> 121 #define IPSEC 122 #endif /* FAST_IPSEC */ 123 124 #include <net/ip6fw/ip6_fw.h> 125 126 #include <netinet6/ip6protosw.h> 127 128 #include <net/net_osdep.h> 129 130 extern struct domain inet6domain; 131 extern struct protosw inet6sw[]; 132 133 u_char ip6_protox[IPPROTO_MAX]; 134 struct in6_ifaddr *in6_ifaddr; 135 136 extern struct callout in6_tmpaddrtimer_ch; 137 138 int ip6_forward_srcrt; /* XXX */ 139 int ip6_sourcecheck; /* XXX */ 140 int ip6_sourcecheck_interval; /* XXX */ 141 142 int ip6_ours_check_algorithm; 143 144 struct pfil_head inet6_pfil_hook; 145 146 /* firewall hooks */ 147 ip6_fw_chk_t *ip6_fw_chk_ptr; 148 ip6_fw_ctl_t *ip6_fw_ctl_ptr; 149 int ip6_fw_enable = 1; 150 151 struct ip6stat ip6stat; 152 153 static void ip6_init2 (void *); 154 static struct ip6aux *ip6_setdstifaddr (struct mbuf *, struct in6_ifaddr *); 155 static int ip6_hopopts_input (u_int32_t *, u_int32_t *, struct mbuf **, int *); 156 static void ip6_input(netmsg_t msg); 157 #ifdef PULLDOWN_TEST 158 static struct mbuf *ip6_pullexthdr (struct mbuf *, size_t, int); 159 #endif 160 static void transport6_processing_handler(netmsg_t netmsg); 161 162 /* 163 * IP6 initialization: fill in IP6 protocol switch table. 164 * All protocols not implemented in kernel go to raw IP6 protocol handler. 165 */ 166 void 167 ip6_init(void) 168 { 169 struct protosw *pr; 170 int i; 171 struct timeval tv; 172 173 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 174 if (pr == NULL) 175 panic("ip6_init"); 176 for (i = 0; i < IPPROTO_MAX; i++) 177 ip6_protox[i] = pr - inet6sw; 178 for (pr = inet6domain.dom_protosw; 179 pr < inet6domain.dom_protoswNPROTOSW; pr++) 180 if (pr->pr_domain->dom_family == PF_INET6 && 181 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 182 ip6_protox[pr->pr_protocol] = pr - inet6sw; 183 184 inet6_pfil_hook.ph_type = PFIL_TYPE_AF; 185 inet6_pfil_hook.ph_af = AF_INET6; 186 if ((i = pfil_head_register(&inet6_pfil_hook)) != 0) { 187 kprintf("%s: WARNING: unable to register pfil hook, " 188 "error %d\n", __func__, i); 189 } 190 191 netisr_register(NETISR_IPV6, ip6_input, NULL); /* XXX hashfn */ 192 scope6_init(); 193 addrsel_policy_init(); 194 nd6_init(); 195 frag6_init(); 196 /* 197 * in many cases, random() here does NOT return random number 198 * as initialization during bootstrap time occur in fixed order. 199 */ 200 microtime(&tv); 201 ip6_flow_seq = krandom() ^ tv.tv_usec; 202 microtime(&tv); 203 ip6_desync_factor = (krandom() ^ tv.tv_usec) % MAX_TEMP_DESYNC_FACTOR; 204 } 205 206 static void 207 ip6_init2(void *dummy) 208 { 209 /* nd6_timer_init */ 210 callout_init(&nd6_timer_ch); 211 callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL); 212 213 /* timer for regeneranation of temporary addresses randomize ID */ 214 callout_init(&in6_tmpaddrtimer_ch); 215 callout_reset(&in6_tmpaddrtimer_ch, 216 (ip6_temp_preferred_lifetime - ip6_desync_factor - 217 ip6_temp_regen_advance) * hz, 218 in6_tmpaddrtimer, NULL); 219 } 220 221 /* cheat */ 222 /* This must be after route_init(), which is now SI_ORDER_THIRD */ 223 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL); 224 225 extern struct route_in6 ip6_forward_rt; 226 227 static 228 void 229 ip6_input(netmsg_t msg) 230 { 231 struct mbuf *m = msg->packet.nm_packet; 232 struct ip6_hdr *ip6; 233 int off = sizeof(struct ip6_hdr), nest; 234 u_int32_t plen; 235 u_int32_t rtalert = ~0; 236 int nxt, ours = 0, rh_present = 0; 237 struct ifnet *deliverifp = NULL; 238 struct in6_addr odst; 239 int srcrt = 0; 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 * make sure we don't have onion peering information into m_aux. 254 */ 255 ip6_delaux(m); 256 257 /* 258 * mbuf statistics 259 */ 260 if (m->m_flags & M_EXT) { 261 if (m->m_next) 262 ip6stat.ip6s_mext2m++; 263 else 264 ip6stat.ip6s_mext1++; 265 } else { 266 #define M2MMAX NELEM(ip6stat.ip6s_m2m) 267 if (m->m_next) { 268 if (m->m_flags & M_LOOP) { 269 ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */ 270 } else if (m->m_pkthdr.rcvif->if_index < M2MMAX) 271 ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++; 272 else 273 ip6stat.ip6s_m2m[0]++; 274 } else 275 ip6stat.ip6s_m1++; 276 #undef M2MMAX 277 } 278 279 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive); 280 ip6stat.ip6s_total++; 281 282 #ifndef PULLDOWN_TEST 283 /* 284 * L2 bridge code and some other code can return mbuf chain 285 * that does not conform to KAME requirement. too bad. 286 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 287 */ 288 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 289 struct mbuf *n; 290 291 n = m_getb(m->m_pkthdr.len, MB_DONTWAIT, MT_HEADER, M_PKTHDR); 292 if (n == NULL) 293 goto bad; 294 M_MOVE_PKTHDR(n, m); 295 296 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t)); 297 n->m_len = n->m_pkthdr.len; 298 m_freem(m); 299 m = n; 300 } 301 IP6_EXTHDR_CHECK_VOIDRET(m, 0, sizeof(struct ip6_hdr)); 302 #endif 303 304 if (m->m_len < sizeof(struct ip6_hdr)) { 305 struct ifnet *inifp; 306 inifp = m->m_pkthdr.rcvif; 307 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 308 ip6stat.ip6s_toosmall++; 309 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 310 goto bad2; 311 } 312 } 313 314 ip6 = mtod(m, struct ip6_hdr *); 315 316 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 317 ip6stat.ip6s_badvers++; 318 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 319 goto bad; 320 } 321 322 /* 323 * Run through list of hooks for input packets. 324 * 325 * NB: Beware of the destination address changing 326 * (e.g. by NAT rewriting). When this happens, 327 * tell ip6_forward to do the right thing. 328 */ 329 if (pfil_has_hooks(&inet6_pfil_hook)) { 330 odst = ip6->ip6_dst; 331 if (pfil_run_hooks(&inet6_pfil_hook, &m, 332 m->m_pkthdr.rcvif, PFIL_IN)) { 333 goto bad2; 334 } 335 if (m == NULL) /* consumed by filter */ 336 goto bad2; 337 ip6 = mtod(m, struct ip6_hdr *); 338 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst); 339 } 340 341 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; 342 343 #ifdef ALTQ 344 if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) { 345 /* packet is dropped by traffic conditioner */ 346 return; 347 } 348 #endif 349 350 /* 351 * Check with the firewall... 352 */ 353 if (ip6_fw_enable && ip6_fw_chk_ptr) { 354 u_short port = 0; 355 /* If ipfw says divert, we have to just drop packet */ 356 /* use port as a dummy argument */ 357 if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) { 358 m_freem(m); 359 m = NULL; 360 } 361 if (!m) 362 goto bad2; 363 } 364 365 /* 366 * Check against address spoofing/corruption. 367 */ 368 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 369 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 370 /* 371 * XXX: "badscope" is not very suitable for a multicast source. 372 */ 373 ip6stat.ip6s_badscope++; 374 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 375 goto bad; 376 } 377 if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) || 378 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) && 379 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 380 ip6stat.ip6s_badscope++; 381 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 382 goto bad; 383 } 384 385 /* 386 * The following check is not documented in specs. A malicious 387 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack 388 * and bypass security checks (act as if it was from 127.0.0.1 by using 389 * IPv6 src ::ffff:127.0.0.1). Be cautious. 390 * 391 * This check chokes if we are in an SIIT cloud. As none of BSDs 392 * support IPv4-less kernel compilation, we cannot support SIIT 393 * environment at all. So, it makes more sense for us to reject any 394 * malicious packets for non-SIIT environment, than try to do a 395 * partical support for SIIT environment. 396 */ 397 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 398 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 399 ip6stat.ip6s_badscope++; 400 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 401 goto bad; 402 } 403 #if 0 404 /* 405 * Reject packets with IPv4 compatible addresses (auto tunnel). 406 * 407 * The code forbids auto tunnel relay case in RFC1933 (the check is 408 * stronger than RFC1933). We may want to re-enable it if mech-xx 409 * is revised to forbid relaying case. 410 */ 411 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) || 412 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) { 413 ip6stat.ip6s_badscope++; 414 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 415 goto bad; 416 } 417 #endif 418 419 /* drop packets if interface ID portion is already filled */ 420 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 421 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) && 422 ip6->ip6_src.s6_addr16[1]) { 423 ip6stat.ip6s_badscope++; 424 goto bad; 425 } 426 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) && 427 ip6->ip6_dst.s6_addr16[1]) { 428 ip6stat.ip6s_badscope++; 429 goto bad; 430 } 431 } 432 433 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 434 ip6->ip6_src.s6_addr16[1] 435 = htons(m->m_pkthdr.rcvif->if_index); 436 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 437 ip6->ip6_dst.s6_addr16[1] 438 = htons(m->m_pkthdr.rcvif->if_index); 439 440 #if 0 /* this case seems to be unnecessary. (jinmei, 20010401) */ 441 /* 442 * We use rt->rt_ifp to determine if the address is ours or not. 443 * If rt_ifp is lo0, the address is ours. 444 * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0, 445 * so any address under fe80::%lo0/64 will be mistakenly considered 446 * local. The special case is supplied to handle the case properly 447 * by actually looking at interface addresses 448 * (using in6ifa_ifpwithaddr). 449 */ 450 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) && 451 IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) { 452 if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) { 453 icmp6_error(m, ICMP6_DST_UNREACH, 454 ICMP6_DST_UNREACH_ADDR, 0); 455 /* m is already freed */ 456 goto bad2; 457 } 458 459 ours = 1; 460 deliverifp = m->m_pkthdr.rcvif; 461 goto hbhcheck; 462 } 463 #endif 464 465 /* 466 * Multicast check 467 * 468 * WARNING! For general subnet proxying the interface hw will 469 * likely filter out the multicast solicitations, so 470 * the interface must be in promiscuous mode. 471 */ 472 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 473 ours = 1; 474 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast); 475 deliverifp = m->m_pkthdr.rcvif; 476 goto hbhcheck; 477 } 478 479 /* 480 * Unicast check 481 */ 482 switch (ip6_ours_check_algorithm) { 483 default: 484 /* 485 * XXX: I intentionally broke our indentation rule here, 486 * since this switch-case is just for measurement and 487 * therefore should soon be removed. 488 */ 489 if (ip6_forward_rt.ro_rt != NULL && 490 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) && 491 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 492 &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr)) 493 ip6stat.ip6s_forward_cachehit++; 494 else { 495 struct sockaddr_in6 *dst6; 496 497 if (ip6_forward_rt.ro_rt) { 498 /* route is down or destination is different */ 499 ip6stat.ip6s_forward_cachemiss++; 500 RTFREE(ip6_forward_rt.ro_rt); 501 ip6_forward_rt.ro_rt = 0; 502 } 503 504 bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6)); 505 dst6 = &ip6_forward_rt.ro_dst; 506 dst6->sin6_len = sizeof(struct sockaddr_in6); 507 dst6->sin6_family = AF_INET6; 508 dst6->sin6_addr = ip6->ip6_dst; 509 510 rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING); 511 } 512 513 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key)) 514 515 /* 516 * Accept the packet if the forwarding interface to the destination 517 * according to the routing table is the loopback interface, 518 * unless the associated route has a gateway. 519 * Note that this approach causes to accept a packet if there is a 520 * route to the loopback interface for the destination of the packet. 521 * But we think it's even useful in some situations, e.g. when using 522 * a special daemon which wants to intercept the packet. 523 * 524 * XXX: some OSes automatically make a cloned route for the destination 525 * of an outgoing packet. If the outgoing interface of the packet 526 * is a loopback one, the kernel would consider the packet to be 527 * accepted, even if we have no such address assinged on the interface. 528 * We check the cloned flag of the route entry to reject such cases, 529 * assuming that route entries for our own addresses are not made by 530 * cloning (it should be true because in6_addloop explicitly installs 531 * the host route). However, we might have to do an explicit check 532 * while it would be less efficient. Or, should we rather install a 533 * reject route for such a case? 534 */ 535 if (ip6_forward_rt.ro_rt && 536 (ip6_forward_rt.ro_rt->rt_flags & 537 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && 538 #ifdef RTF_WASCLONED 539 !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) && 540 #endif 541 #ifdef RTF_CLONED 542 !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) && 543 #endif 544 #if 0 545 /* 546 * The check below is redundant since the comparison of 547 * the destination and the key of the rtentry has 548 * already done through looking up the routing table. 549 */ 550 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 551 &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) 552 #endif 553 ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) { 554 struct in6_ifaddr *ia6 = 555 (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa; 556 557 /* 558 * record address information into m_aux. 559 */ 560 ip6_setdstifaddr(m, ia6); 561 562 /* 563 * packets to a tentative, duplicated, or somehow invalid 564 * address must not be accepted. 565 */ 566 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 567 /* this address is ready */ 568 ours = 1; 569 deliverifp = ia6->ia_ifp; /* correct? */ 570 /* Count the packet in the ip address stats */ 571 IFA_STAT_INC(&ia6->ia_ifa, ipackets, 1); 572 IFA_STAT_INC(&ia6->ia_ifa, ibytes, m->m_pkthdr.len); 573 goto hbhcheck; 574 } else { 575 /* address is not ready, so discard the packet. */ 576 nd6log((LOG_INFO, 577 "ip6_input: packet to an unready address %s->%s\n", 578 ip6_sprintf(&ip6->ip6_src), 579 ip6_sprintf(&ip6->ip6_dst))); 580 581 goto bad; 582 } 583 } 584 } /* XXX indentation (see above) */ 585 586 /* 587 * FAITH(Firewall Aided Internet Translator) 588 */ 589 if (ip6_keepfaith) { 590 if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp 591 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) { 592 /* XXX do we need more sanity checks? */ 593 ours = 1; 594 deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */ 595 goto hbhcheck; 596 } 597 } 598 599 /* 600 * Now there is no reason to process the packet if it's not our own 601 * and we're not a router. 602 */ 603 if (!ip6_forwarding) { 604 ip6stat.ip6s_cantforward++; 605 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 606 goto bad; 607 } 608 609 hbhcheck: 610 /* 611 * record address information into m_aux, if we don't have one yet. 612 * note that we are unable to record it, if the address is not listed 613 * as our interface address (e.g. multicast addresses, addresses 614 * within FAITH prefixes and such). 615 */ 616 if (deliverifp && !ip6_getdstifaddr(m)) { 617 struct in6_ifaddr *ia6; 618 619 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); 620 if (ia6) { 621 if (!ip6_setdstifaddr(m, ia6)) { 622 /* 623 * XXX maybe we should drop the packet here, 624 * as we could not provide enough information 625 * to the upper layers. 626 */ 627 } 628 } 629 } 630 631 /* 632 * Process Hop-by-Hop options header if it's contained. 633 * m may be modified in ip6_hopopts_input(). 634 * If a JumboPayload option is included, plen will also be modified. 635 */ 636 plen = (u_int32_t)ntohs(ip6->ip6_plen); 637 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 638 struct ip6_hbh *hbh; 639 640 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) { 641 #if 0 /*touches NULL pointer*/ 642 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 643 #endif 644 goto bad2; /* m have already been freed */ 645 } 646 647 /* adjust pointer */ 648 ip6 = mtod(m, struct ip6_hdr *); 649 650 /* 651 * if the payload length field is 0 and the next header field 652 * indicates Hop-by-Hop Options header, then a Jumbo Payload 653 * option MUST be included. 654 */ 655 if (ip6->ip6_plen == 0 && plen == 0) { 656 /* 657 * Note that if a valid jumbo payload option is 658 * contained, ip6_hoptops_input() must set a valid 659 * (non-zero) payload length to the variable plen. 660 */ 661 ip6stat.ip6s_badoptions++; 662 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 663 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 664 icmp6_error(m, ICMP6_PARAM_PROB, 665 ICMP6_PARAMPROB_HEADER, 666 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); 667 goto bad2; 668 } 669 #ifndef PULLDOWN_TEST 670 /* ip6_hopopts_input() ensures that mbuf is contiguous */ 671 hbh = (struct ip6_hbh *)(ip6 + 1); 672 #else 673 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 674 sizeof(struct ip6_hbh)); 675 if (hbh == NULL) { 676 ip6stat.ip6s_tooshort++; 677 goto bad2; 678 } 679 #endif 680 nxt = hbh->ip6h_nxt; 681 682 /* 683 * If we are acting as a router and the packet contains a 684 * router alert option, see if we know the option value. 685 * Currently, we only support the option value for MLD, in which 686 * case we should pass the packet to the multicast routing 687 * daemon. 688 */ 689 if (rtalert != ~0 && ip6_forwarding) { 690 switch (rtalert) { 691 case IP6OPT_RTALERT_MLD: 692 ours = 1; 693 break; 694 default: 695 /* 696 * RFC2711 requires unrecognized values must be 697 * silently ignored. 698 */ 699 break; 700 } 701 } 702 } else 703 nxt = ip6->ip6_nxt; 704 705 /* 706 * Check that the amount of data in the buffers 707 * is as at least much as the IPv6 header would have us expect. 708 * Trim mbufs if longer than we expect. 709 * Drop packet if shorter than we expect. 710 */ 711 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 712 ip6stat.ip6s_tooshort++; 713 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 714 goto bad; 715 } 716 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 717 if (m->m_len == m->m_pkthdr.len) { 718 m->m_len = sizeof(struct ip6_hdr) + plen; 719 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 720 } else 721 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 722 } 723 724 /* 725 * Forward if desirable. 726 */ 727 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 728 /* 729 * If we are acting as a multicast router, all 730 * incoming multicast packets are passed to the 731 * kernel-level multicast forwarding function. 732 * The packet is returned (relatively) intact; if 733 * ip6_mforward() returns a non-zero value, the packet 734 * must be discarded, else it may be accepted below. 735 */ 736 if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) { 737 ip6stat.ip6s_cantforward++; 738 goto bad; 739 } 740 if (!ours) 741 goto bad; 742 } else if (!ours) { 743 ip6_forward(m, srcrt); 744 goto bad2; 745 } 746 747 ip6 = mtod(m, struct ip6_hdr *); 748 749 /* 750 * Malicious party may be able to use IPv4 mapped addr to confuse 751 * tcp/udp stack and bypass security checks (act as if it was from 752 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 753 * 754 * For SIIT end node behavior, you may want to disable the check. 755 * However, you will become vulnerable to attacks using IPv4 mapped 756 * source. 757 */ 758 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 759 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 760 ip6stat.ip6s_badscope++; 761 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 762 goto bad; 763 } 764 765 /* 766 * Tell launch routine the next header 767 */ 768 ip6stat.ip6s_delivered++; 769 in6_ifstat_inc(deliverifp, ifs6_in_deliver); 770 nest = 0; 771 772 rh_present = 0; 773 while (nxt != IPPROTO_DONE) { 774 struct protosw *sw6; 775 776 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { 777 ip6stat.ip6s_toomanyhdr++; 778 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 779 goto bad; 780 } 781 782 /* 783 * protection against faulty packet - there should be 784 * more sanity checks in header chain processing. 785 */ 786 if (m->m_pkthdr.len < off) { 787 ip6stat.ip6s_tooshort++; 788 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 789 goto bad; 790 } 791 792 #if 0 793 /* 794 * do we need to do it for every header? yeah, other 795 * functions can play with it (like re-allocate and copy). 796 */ 797 mhist = ip6_addaux(m); 798 if (mhist && M_TRAILINGSPACE(mhist) >= sizeof(nxt)) { 799 hist = mtod(mhist, caddr_t) + mhist->m_len; 800 bcopy(&nxt, hist, sizeof(nxt)); 801 mhist->m_len += sizeof(nxt); 802 } else { 803 ip6stat.ip6s_toomanyhdr++; 804 goto bad; 805 } 806 #endif 807 808 if (nxt == IPPROTO_ROUTING) { 809 if (rh_present++) { 810 in6_ifstat_inc(m->m_pkthdr.rcvif, 811 ifs6_in_hdrerr); 812 ip6stat.ip6s_badoptions++; 813 goto bad; 814 } 815 } 816 817 sw6 = &inet6sw[ip6_protox[nxt]]; 818 #ifdef IPSEC 819 /* 820 * enforce IPsec policy checking if we are seeing last header. 821 * note that we do not visit this with protocols with pcb layer 822 * code - like udp/tcp/raw ip. 823 */ 824 if ((sw6->pr_flags & PR_LASTHDR) && ipsec6_in_reject(m, NULL)) { 825 ipsec6stat.in_polvio++; 826 goto bad; 827 } 828 #endif 829 /* 830 * If this is a terminal header forward to the port, otherwise 831 * process synchronously for more headers. 832 */ 833 if (sw6->pr_flags & PR_LASTHDR) { 834 struct netmsg_packet *pmsg; 835 lwkt_port_t port; 836 837 port = netisr_cpuport(0); /* XXX */ 838 KKASSERT(port != NULL); 839 pmsg = &m->m_hdr.mh_netmsg; 840 netmsg_init(&pmsg->base, NULL, 841 &netisr_apanic_rport, 842 0, transport6_processing_handler); 843 pmsg->nm_packet = m; 844 pmsg->nm_nxt = nxt; 845 pmsg->base.lmsg.u.ms_result = off; 846 lwkt_sendmsg(port, &pmsg->base.lmsg); 847 /* done with m */ 848 nxt = IPPROTO_DONE; 849 } else { 850 nxt = sw6->pr_input(&m, &off, nxt); 851 } 852 } 853 goto bad2; 854 bad: 855 m_freem(m); 856 bad2: 857 ; 858 /* msg was embedded in the mbuf, do not reply! */ 859 } 860 861 /* 862 * We have to call the pr_input() function from the correct protocol 863 * thread. The sw6->pr_soport() request at the end of ip6_input() 864 * returns the port and we forward a netmsg to the port to execute 865 * this function. 866 */ 867 static void 868 transport6_processing_handler(netmsg_t netmsg) 869 { 870 struct netmsg_packet *pmsg = (struct netmsg_packet *)netmsg; 871 struct protosw *sw6; 872 int hlen; 873 int nxt; 874 875 sw6 = &inet6sw[ip6_protox[pmsg->nm_nxt]]; 876 hlen = pmsg->base.lmsg.u.ms_result; 877 878 nxt = sw6->pr_input(&pmsg->nm_packet, &hlen, pmsg->nm_nxt); 879 KKASSERT(nxt == IPPROTO_DONE); 880 /* netmsg was embedded in the mbuf, do not reply! */ 881 } 882 883 /* 884 * set/grab in6_ifaddr correspond to IPv6 destination address. 885 * XXX backward compatibility wrapper 886 */ 887 static struct ip6aux * 888 ip6_setdstifaddr(struct mbuf *m, struct in6_ifaddr *ia6) 889 { 890 struct ip6aux *n; 891 892 n = ip6_addaux(m); 893 if (n) 894 n->ip6a_dstia6 = ia6; 895 return n; /* NULL if failed to set */ 896 } 897 898 struct in6_ifaddr * 899 ip6_getdstifaddr(struct mbuf *m) 900 { 901 struct ip6aux *n; 902 903 n = ip6_findaux(m); 904 if (n) 905 return n->ip6a_dstia6; 906 else 907 return NULL; 908 } 909 910 /* 911 * Hop-by-Hop options header processing. If a valid jumbo payload option is 912 * included, the real payload length will be stored in plenp. 913 */ 914 static int 915 ip6_hopopts_input(u_int32_t *plenp, 916 u_int32_t *rtalertp,/* XXX: should be stored more smart way */ 917 struct mbuf **mp, 918 int *offp) 919 { 920 struct mbuf *m = *mp; 921 int off = *offp, hbhlen; 922 struct ip6_hbh *hbh; 923 924 /* validation of the length of the header */ 925 #ifndef PULLDOWN_TEST 926 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1); 927 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 928 hbhlen = (hbh->ip6h_len + 1) << 3; 929 930 IP6_EXTHDR_CHECK(m, off, hbhlen, -1); 931 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 932 #else 933 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, 934 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 935 if (hbh == NULL) { 936 ip6stat.ip6s_tooshort++; 937 return -1; 938 } 939 hbhlen = (hbh->ip6h_len + 1) << 3; 940 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 941 hbhlen); 942 if (hbh == NULL) { 943 ip6stat.ip6s_tooshort++; 944 return -1; 945 } 946 #endif 947 off += hbhlen; 948 hbhlen -= sizeof(struct ip6_hbh); 949 950 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 951 hbhlen, rtalertp, plenp) < 0) 952 return (-1); 953 954 *offp = off; 955 *mp = m; 956 return (0); 957 } 958 959 /* 960 * Search header for all Hop-by-hop options and process each option. 961 * This function is separate from ip6_hopopts_input() in order to 962 * handle a case where the sending node itself process its hop-by-hop 963 * options header. In such a case, the function is called from ip6_output(). 964 * 965 * The function assumes that hbh header is located right after the IPv6 header 966 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 967 * opthead + hbhlen is located in continuous memory region. 968 */ 969 int 970 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen, 971 u_int32_t *rtalertp, u_int32_t *plenp) 972 { 973 struct ip6_hdr *ip6; 974 int optlen = 0; 975 u_int8_t *opt = opthead; 976 u_int16_t rtalert_val; 977 u_int32_t jumboplen; 978 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 979 980 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 981 switch (*opt) { 982 case IP6OPT_PAD1: 983 optlen = 1; 984 break; 985 case IP6OPT_PADN: 986 if (hbhlen < IP6OPT_MINLEN) { 987 ip6stat.ip6s_toosmall++; 988 goto bad; 989 } 990 optlen = *(opt + 1) + 2; 991 break; 992 case IP6OPT_RTALERT: 993 /* XXX may need check for alignment */ 994 if (hbhlen < IP6OPT_RTALERT_LEN) { 995 ip6stat.ip6s_toosmall++; 996 goto bad; 997 } 998 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 999 /* XXX stat */ 1000 icmp6_error(m, ICMP6_PARAM_PROB, 1001 ICMP6_PARAMPROB_HEADER, 1002 erroff + opt + 1 - opthead); 1003 return (-1); 1004 } 1005 optlen = IP6OPT_RTALERT_LEN; 1006 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 1007 *rtalertp = ntohs(rtalert_val); 1008 break; 1009 case IP6OPT_JUMBO: 1010 /* XXX may need check for alignment */ 1011 if (hbhlen < IP6OPT_JUMBO_LEN) { 1012 ip6stat.ip6s_toosmall++; 1013 goto bad; 1014 } 1015 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 1016 /* XXX stat */ 1017 icmp6_error(m, ICMP6_PARAM_PROB, 1018 ICMP6_PARAMPROB_HEADER, 1019 erroff + opt + 1 - opthead); 1020 return (-1); 1021 } 1022 optlen = IP6OPT_JUMBO_LEN; 1023 1024 /* 1025 * IPv6 packets that have non 0 payload length 1026 * must not contain a jumbo payload option. 1027 */ 1028 ip6 = mtod(m, struct ip6_hdr *); 1029 if (ip6->ip6_plen) { 1030 ip6stat.ip6s_badoptions++; 1031 icmp6_error(m, ICMP6_PARAM_PROB, 1032 ICMP6_PARAMPROB_HEADER, 1033 erroff + opt - opthead); 1034 return (-1); 1035 } 1036 1037 /* 1038 * We may see jumbolen in unaligned location, so 1039 * we'd need to perform bcopy(). 1040 */ 1041 bcopy(opt + 2, &jumboplen, sizeof(jumboplen)); 1042 jumboplen = (u_int32_t)htonl(jumboplen); 1043 1044 #if 1 1045 /* 1046 * if there are multiple jumbo payload options, 1047 * *plenp will be non-zero and the packet will be 1048 * rejected. 1049 * the behavior may need some debate in ipngwg - 1050 * multiple options does not make sense, however, 1051 * there's no explicit mention in specification. 1052 */ 1053 if (*plenp != 0) { 1054 ip6stat.ip6s_badoptions++; 1055 icmp6_error(m, ICMP6_PARAM_PROB, 1056 ICMP6_PARAMPROB_HEADER, 1057 erroff + opt + 2 - opthead); 1058 return (-1); 1059 } 1060 #endif 1061 1062 /* 1063 * jumbo payload length must be larger than 65535. 1064 */ 1065 if (jumboplen <= IPV6_MAXPACKET) { 1066 ip6stat.ip6s_badoptions++; 1067 icmp6_error(m, ICMP6_PARAM_PROB, 1068 ICMP6_PARAMPROB_HEADER, 1069 erroff + opt + 2 - opthead); 1070 return (-1); 1071 } 1072 *plenp = jumboplen; 1073 1074 break; 1075 default: /* unknown option */ 1076 if (hbhlen < IP6OPT_MINLEN) { 1077 ip6stat.ip6s_toosmall++; 1078 goto bad; 1079 } 1080 optlen = ip6_unknown_opt(opt, m, 1081 erroff + opt - opthead); 1082 if (optlen == -1) 1083 return (-1); 1084 optlen += 2; 1085 break; 1086 } 1087 } 1088 1089 return (0); 1090 1091 bad: 1092 m_freem(m); 1093 return (-1); 1094 } 1095 1096 /* 1097 * Unknown option processing. 1098 * The third argument `off' is the offset from the IPv6 header to the option, 1099 * which is necessary if the IPv6 header the and option header and IPv6 header 1100 * is not continuous in order to return an ICMPv6 error. 1101 */ 1102 int 1103 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off) 1104 { 1105 struct ip6_hdr *ip6; 1106 1107 switch (IP6OPT_TYPE(*optp)) { 1108 case IP6OPT_TYPE_SKIP: /* ignore the option */ 1109 return ((int)*(optp + 1)); 1110 case IP6OPT_TYPE_DISCARD: /* silently discard */ 1111 m_freem(m); 1112 return (-1); 1113 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 1114 ip6stat.ip6s_badoptions++; 1115 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 1116 return (-1); 1117 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 1118 ip6stat.ip6s_badoptions++; 1119 ip6 = mtod(m, struct ip6_hdr *); 1120 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1121 (m->m_flags & (M_BCAST|M_MCAST))) 1122 m_freem(m); 1123 else 1124 icmp6_error(m, ICMP6_PARAM_PROB, 1125 ICMP6_PARAMPROB_OPTION, off); 1126 return (-1); 1127 } 1128 1129 m_freem(m); /* XXX: NOTREACHED */ 1130 return (-1); 1131 } 1132 1133 /* 1134 * Create the "control" list for this pcb. 1135 * The function will not modify mbuf chain at all. 1136 * 1137 * with KAME mbuf chain restriction: 1138 * The routine will be called from upper layer handlers like tcp6_input(). 1139 * Thus the routine assumes that the caller (tcp6_input) have already 1140 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 1141 * very first mbuf on the mbuf chain. 1142 */ 1143 void 1144 ip6_savecontrol(struct inpcb *in6p, struct mbuf **mp, struct ip6_hdr *ip6, 1145 struct mbuf *m) 1146 { 1147 #define IS2292(x, y) ((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y)) 1148 struct thread *td = curthread; /* XXX */ 1149 int privileged = 0; 1150 1151 1152 if (priv_check(td, PRIV_ROOT) == 0) 1153 privileged++; 1154 1155 #ifdef SO_TIMESTAMP 1156 if (in6p->in6p_socket->so_options & SO_TIMESTAMP) { 1157 struct timeval tv; 1158 1159 microtime(&tv); 1160 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1161 SCM_TIMESTAMP, SOL_SOCKET); 1162 if (*mp) { 1163 mp = &(*mp)->m_next; 1164 } 1165 } 1166 #endif 1167 1168 /* RFC 2292 sec. 5 */ 1169 if (in6p->in6p_flags & IN6P_PKTINFO) { 1170 struct in6_pktinfo pi6; 1171 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 1172 if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr)) 1173 pi6.ipi6_addr.s6_addr16[1] = 0; 1174 pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif) 1175 ? m->m_pkthdr.rcvif->if_index 1176 : 0; 1177 *mp = sbcreatecontrol((caddr_t) &pi6, 1178 sizeof(struct in6_pktinfo), IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), 1179 IPPROTO_IPV6); 1180 if (*mp) 1181 mp = &(*mp)->m_next; 1182 } 1183 1184 if (in6p->in6p_flags & IN6P_HOPLIMIT) { 1185 int hlim = ip6->ip6_hlim & 0xff; 1186 *mp = sbcreatecontrol((caddr_t) &hlim, 1187 sizeof(int), IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6); 1188 if (*mp) 1189 mp = &(*mp)->m_next; 1190 } 1191 1192 if ((in6p->in6p_flags & IN6P_TCLASS) != 0) { 1193 u_int32_t flowinfo; 1194 int tclass; 1195 1196 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK); 1197 flowinfo >>= 20; 1198 1199 tclass = flowinfo & 0xff; 1200 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass), 1201 IPV6_TCLASS, IPPROTO_IPV6); 1202 if (*mp) 1203 mp = &(*mp)->m_next; 1204 } 1205 1206 /* 1207 * IPV6_HOPOPTS socket option. We require super-user privilege 1208 * for the option, but it might be too strict, since there might 1209 * be some hop-by-hop options which can be returned to normal user. 1210 * See RFC 2292 section 6. 1211 */ 1212 if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) { 1213 /* 1214 * Check if a hop-by-hop options header is contatined in the 1215 * received packet, and if so, store the options as ancillary 1216 * data. Note that a hop-by-hop options header must be 1217 * just after the IPv6 header, which fact is assured through 1218 * the IPv6 input processing. 1219 */ 1220 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1221 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 1222 struct ip6_hbh *hbh; 1223 int hbhlen = 0; 1224 #ifdef PULLDOWN_TEST 1225 struct mbuf *ext; 1226 #endif 1227 1228 #ifndef PULLDOWN_TEST 1229 hbh = (struct ip6_hbh *)(ip6 + 1); 1230 hbhlen = (hbh->ip6h_len + 1) << 3; 1231 #else 1232 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 1233 ip6->ip6_nxt); 1234 if (ext == NULL) { 1235 ip6stat.ip6s_tooshort++; 1236 return; 1237 } 1238 hbh = mtod(ext, struct ip6_hbh *); 1239 hbhlen = (hbh->ip6h_len + 1) << 3; 1240 if (hbhlen != ext->m_len) { 1241 m_freem(ext); 1242 ip6stat.ip6s_tooshort++; 1243 return; 1244 } 1245 #endif 1246 1247 /* 1248 * XXX: We copy whole the header even if a jumbo 1249 * payload option is included, which option is to 1250 * be removed before returning in the RFC 2292. 1251 * Note: this constraint is removed in 2292bis. 1252 */ 1253 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 1254 IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS), IPPROTO_IPV6); 1255 if (*mp) 1256 mp = &(*mp)->m_next; 1257 #ifdef PULLDOWN_TEST 1258 m_freem(ext); 1259 #endif 1260 } 1261 } 1262 1263 /* IPV6_DSTOPTS and IPV6_RTHDR socket options */ 1264 if ((in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) { 1265 int proto, off, nxt; 1266 1267 /* 1268 * go through the header chain to see if a routing header is 1269 * contained in the packet. We need this information to store 1270 * destination options headers (if any) properly. 1271 * XXX: performance issue. We should record this info when 1272 * processing extension headers in incoming routine. 1273 * (todo) use m_aux? 1274 */ 1275 proto = IPPROTO_IPV6; 1276 off = 0; 1277 nxt = -1; 1278 while (1) { 1279 int newoff; 1280 1281 newoff = ip6_nexthdr(m, off, proto, &nxt); 1282 if (newoff < 0) 1283 break; 1284 if (newoff < off) /* invalid, check for safety */ 1285 break; 1286 if ((proto = nxt) == IPPROTO_ROUTING) 1287 break; 1288 off = newoff; 1289 } 1290 } 1291 1292 if ((in6p->in6p_flags & 1293 (IN6P_RTHDR | IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) { 1294 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1295 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 1296 1297 /* 1298 * Search for destination options headers or routing 1299 * header(s) through the header chain, and stores each 1300 * header as ancillary data. 1301 * Note that the order of the headers remains in 1302 * the chain of ancillary data. 1303 */ 1304 while (1) { /* is explicit loop prevention necessary? */ 1305 struct ip6_ext *ip6e = NULL; 1306 int elen; 1307 #ifdef PULLDOWN_TEST 1308 struct mbuf *ext = NULL; 1309 #endif 1310 1311 /* 1312 * if it is not an extension header, don't try to 1313 * pull it from the chain. 1314 */ 1315 switch (nxt) { 1316 case IPPROTO_DSTOPTS: 1317 case IPPROTO_ROUTING: 1318 case IPPROTO_HOPOPTS: 1319 case IPPROTO_AH: /* is it possible? */ 1320 break; 1321 default: 1322 goto loopend; 1323 } 1324 1325 #ifndef PULLDOWN_TEST 1326 if (off + sizeof(*ip6e) > m->m_len) 1327 goto loopend; 1328 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 1329 if (nxt == IPPROTO_AH) 1330 elen = (ip6e->ip6e_len + 2) << 2; 1331 else 1332 elen = (ip6e->ip6e_len + 1) << 3; 1333 if (off + elen > m->m_len) 1334 goto loopend; 1335 #else 1336 ext = ip6_pullexthdr(m, off, nxt); 1337 if (ext == NULL) { 1338 ip6stat.ip6s_tooshort++; 1339 return; 1340 } 1341 ip6e = mtod(ext, struct ip6_ext *); 1342 if (nxt == IPPROTO_AH) 1343 elen = (ip6e->ip6e_len + 2) << 2; 1344 else 1345 elen = (ip6e->ip6e_len + 1) << 3; 1346 if (elen != ext->m_len) { 1347 m_freem(ext); 1348 ip6stat.ip6s_tooshort++; 1349 return; 1350 } 1351 #endif 1352 1353 switch (nxt) { 1354 case IPPROTO_DSTOPTS: 1355 if ((in6p->in6p_flags & IN6P_DSTOPTS) == 0) 1356 break; 1357 1358 /* 1359 * We also require super-user privilege for 1360 * the option. 1361 * See the comments on IN6_HOPOPTS. 1362 */ 1363 if (!privileged) 1364 break; 1365 1366 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1367 IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS), 1368 IPPROTO_IPV6); 1369 if (*mp) 1370 mp = &(*mp)->m_next; 1371 break; 1372 case IPPROTO_ROUTING: 1373 if (!(in6p->in6p_flags & IN6P_RTHDR)) 1374 break; 1375 1376 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1377 IS2292(IPV6_2292RTHDR, IPV6_RTHDR), 1378 IPPROTO_IPV6); 1379 if (*mp) 1380 mp = &(*mp)->m_next; 1381 break; 1382 case IPPROTO_HOPOPTS: 1383 case IPPROTO_AH: /* is it possible? */ 1384 break; 1385 1386 default: 1387 /* 1388 * other cases have been filtered in the above. 1389 * none will visit this case. here we supply 1390 * the code just in case (nxt overwritten or 1391 * other cases). 1392 */ 1393 #ifdef PULLDOWN_TEST 1394 m_freem(ext); 1395 #endif 1396 goto loopend; 1397 1398 } 1399 1400 /* proceed with the next header. */ 1401 off += elen; 1402 nxt = ip6e->ip6e_nxt; 1403 ip6e = NULL; 1404 #ifdef PULLDOWN_TEST 1405 m_freem(ext); 1406 ext = NULL; 1407 #endif 1408 } 1409 loopend: 1410 ; 1411 } 1412 #undef IS2292 1413 } 1414 1415 void 1416 ip6_notify_pmtu(struct inpcb *in6p, struct sockaddr_in6 *dst, u_int32_t *mtu) 1417 { 1418 struct socket *so; 1419 struct mbuf *m_mtu; 1420 struct ip6_mtuinfo mtuctl; 1421 1422 so = in6p->inp_socket; 1423 1424 if (mtu == NULL) 1425 return; 1426 1427 #ifdef DIAGNOSTIC 1428 if (so == NULL) /* I believe this is impossible */ 1429 panic("ip6_notify_pmtu: socket is NULL"); 1430 #endif 1431 1432 bzero(&mtuctl, sizeof(mtuctl)); /* zero-clear for safety */ 1433 mtuctl.ip6m_mtu = *mtu; 1434 mtuctl.ip6m_addr = *dst; 1435 1436 if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl), 1437 IPV6_PATHMTU, IPPROTO_IPV6)) == NULL) 1438 return; 1439 1440 lwkt_gettoken(&so->so_rcv.ssb_token); 1441 if (sbappendaddr(&so->so_rcv.sb, (struct sockaddr *)dst, NULL, m_mtu) 1442 == 0) { 1443 m_freem(m_mtu); 1444 /* XXX: should count statistics */ 1445 } else { 1446 sorwakeup(so); 1447 } 1448 lwkt_reltoken(&so->so_rcv.ssb_token); 1449 } 1450 1451 #ifdef PULLDOWN_TEST 1452 /* 1453 * pull single extension header from mbuf chain. returns single mbuf that 1454 * contains the result, or NULL on error. 1455 */ 1456 static struct mbuf * 1457 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt) 1458 { 1459 struct ip6_ext ip6e; 1460 size_t elen; 1461 struct mbuf *n; 1462 1463 #ifdef DIAGNOSTIC 1464 switch (nxt) { 1465 case IPPROTO_DSTOPTS: 1466 case IPPROTO_ROUTING: 1467 case IPPROTO_HOPOPTS: 1468 case IPPROTO_AH: /* is it possible? */ 1469 break; 1470 default: 1471 kprintf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 1472 } 1473 #endif 1474 1475 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1476 if (nxt == IPPROTO_AH) 1477 elen = (ip6e.ip6e_len + 2) << 2; 1478 else 1479 elen = (ip6e.ip6e_len + 1) << 3; 1480 1481 n = m_getb(elen, MB_DONTWAIT, MT_DATA, 0); 1482 if (n == NULL) 1483 return NULL; 1484 n->m_len = 0; 1485 1486 if (elen >= M_TRAILINGSPACE(n)) { 1487 m_free(n); 1488 return NULL; 1489 } 1490 1491 m_copydata(m, off, elen, mtod(n, caddr_t)); 1492 n->m_len = elen; 1493 return n; 1494 } 1495 #endif 1496 1497 /* 1498 * Get pointer to the previous header followed by the header 1499 * currently processed. 1500 * XXX: This function supposes that 1501 * M includes all headers, 1502 * the next header field and the header length field of each header 1503 * are valid, and 1504 * the sum of each header length equals to OFF. 1505 * Because of these assumptions, this function must be called very 1506 * carefully. Moreover, it will not be used in the near future when 1507 * we develop `neater' mechanism to process extension headers. 1508 */ 1509 char * 1510 ip6_get_prevhdr(struct mbuf *m, int off) 1511 { 1512 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1513 1514 if (off == sizeof(struct ip6_hdr)) 1515 return (&ip6->ip6_nxt); 1516 else { 1517 int len, nxt; 1518 struct ip6_ext *ip6e = NULL; 1519 1520 nxt = ip6->ip6_nxt; 1521 len = sizeof(struct ip6_hdr); 1522 while (len < off) { 1523 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 1524 1525 switch (nxt) { 1526 case IPPROTO_FRAGMENT: 1527 len += sizeof(struct ip6_frag); 1528 break; 1529 case IPPROTO_AH: 1530 len += (ip6e->ip6e_len + 2) << 2; 1531 break; 1532 default: 1533 len += (ip6e->ip6e_len + 1) << 3; 1534 break; 1535 } 1536 nxt = ip6e->ip6e_nxt; 1537 } 1538 if (ip6e) 1539 return (&ip6e->ip6e_nxt); 1540 else 1541 return NULL; 1542 } 1543 } 1544 1545 /* 1546 * get next header offset. m will be retained. 1547 */ 1548 int 1549 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp) 1550 { 1551 struct ip6_hdr ip6; 1552 struct ip6_ext ip6e; 1553 struct ip6_frag fh; 1554 1555 /* just in case */ 1556 if (m == NULL) 1557 panic("ip6_nexthdr: m == NULL"); 1558 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 1559 return -1; 1560 1561 switch (proto) { 1562 case IPPROTO_IPV6: 1563 if (m->m_pkthdr.len < off + sizeof(ip6)) 1564 return -1; 1565 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 1566 if (nxtp) 1567 *nxtp = ip6.ip6_nxt; 1568 off += sizeof(ip6); 1569 return off; 1570 1571 case IPPROTO_FRAGMENT: 1572 /* 1573 * terminate parsing if it is not the first fragment, 1574 * it does not make sense to parse through it. 1575 */ 1576 if (m->m_pkthdr.len < off + sizeof(fh)) 1577 return -1; 1578 m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 1579 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */ 1580 if (fh.ip6f_offlg & IP6F_OFF_MASK) 1581 return -1; 1582 if (nxtp) 1583 *nxtp = fh.ip6f_nxt; 1584 off += sizeof(struct ip6_frag); 1585 return off; 1586 1587 case IPPROTO_AH: 1588 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1589 return -1; 1590 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1591 if (nxtp) 1592 *nxtp = ip6e.ip6e_nxt; 1593 off += (ip6e.ip6e_len + 2) << 2; 1594 return off; 1595 1596 case IPPROTO_HOPOPTS: 1597 case IPPROTO_ROUTING: 1598 case IPPROTO_DSTOPTS: 1599 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1600 return -1; 1601 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1602 if (nxtp) 1603 *nxtp = ip6e.ip6e_nxt; 1604 off += (ip6e.ip6e_len + 1) << 3; 1605 return off; 1606 1607 case IPPROTO_NONE: 1608 case IPPROTO_ESP: 1609 case IPPROTO_IPCOMP: 1610 /* give up */ 1611 return -1; 1612 1613 default: 1614 return -1; 1615 } 1616 1617 return -1; 1618 } 1619 1620 /* 1621 * get offset for the last header in the chain. m will be kept untainted. 1622 */ 1623 int 1624 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp) 1625 { 1626 int newoff; 1627 int nxt; 1628 1629 if (!nxtp) { 1630 nxt = -1; 1631 nxtp = &nxt; 1632 } 1633 while (1) { 1634 newoff = ip6_nexthdr(m, off, proto, nxtp); 1635 if (newoff < 0) 1636 return off; 1637 else if (newoff < off) 1638 return -1; /* invalid */ 1639 else if (newoff == off) 1640 return newoff; 1641 1642 off = newoff; 1643 proto = *nxtp; 1644 } 1645 } 1646 1647 struct ip6aux * 1648 ip6_addaux(struct mbuf *m) 1649 { 1650 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1651 if (!tag) { 1652 tag = m_tag_get(PACKET_TAG_IPV6_INPUT, 1653 sizeof (struct ip6aux), 1654 MB_DONTWAIT); 1655 if (tag) 1656 m_tag_prepend(m, tag); 1657 } 1658 if (tag) 1659 bzero(m_tag_data(tag), sizeof (struct ip6aux)); 1660 return tag ? (struct ip6aux *)m_tag_data(tag) : NULL; 1661 } 1662 1663 struct ip6aux * 1664 ip6_findaux(struct mbuf *m) 1665 { 1666 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1667 return tag ? (struct ip6aux*)(tag+1) : NULL; 1668 } 1669 1670 void 1671 ip6_delaux(struct mbuf *m) 1672 { 1673 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1674 if (tag) 1675 m_tag_delete(m, tag); 1676 } 1677 1678 /* 1679 * System control for IP6 1680 */ 1681 1682 u_char inet6ctlerrmap[PRC_NCMDS] = { 1683 0, 0, 0, 0, 1684 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1685 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1686 EMSGSIZE, EHOSTUNREACH, 0, 0, 1687 0, 0, 0, 0, 1688 ENOPROTOOPT 1689 }; 1690