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