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