ip6_input.c revision 130002
1/* $FreeBSD: head/sys/netinet6/ip6_input.c 130002 2004-06-02 15:41:18Z ume $ */ 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 129extern struct domain inet6domain; 130 131u_char ip6_protox[IPPROTO_MAX]; 132static struct ifqueue ip6intrq; 133static int ip6qmaxlen = IFQ_MAXLEN; 134struct in6_ifaddr *in6_ifaddr; 135 136extern struct callout in6_tmpaddrtimer_ch; 137 138int ip6_forward_srcrt; /* XXX */ 139int ip6_sourcecheck; /* XXX */ 140int ip6_sourcecheck_interval; /* XXX */ 141 142int ip6_ours_check_algorithm; 143 144#ifdef PFIL_HOOKS 145struct pfil_head inet6_pfil_hook; 146#endif 147 148/* firewall hooks */ 149ip6_fw_chk_t *ip6_fw_chk_ptr; 150ip6_fw_ctl_t *ip6_fw_ctl_ptr; 151int ip6_fw_enable = 1; 152 153struct ip6stat ip6stat; 154 155static void ip6_init2 __P((void *)); 156static struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *)); 157static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *)); 158#ifdef PULLDOWN_TEST 159static 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 */ 166void 167ip6_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 206static void 207ip6_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 */ 229SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL); 230 231extern struct route_in6 ip6_forward_rt; 232 233void 234ip6_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 */ 835static struct ip6aux * 836ip6_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 848struct in6_ifaddr * 849ip6_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 */ 865static int 866ip6_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 */ 923int 924ip6_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 */ 1060int 1061ip6_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 */ 1104void 1105ip6_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 ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1111 1112#ifdef SO_TIMESTAMP 1113 if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) { 1114 struct timeval tv; 1115 1116 microtime(&tv); 1117 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1118 SCM_TIMESTAMP, SOL_SOCKET); 1119 if (*mp) 1120 mp = &(*mp)->m_next; 1121 } 1122#endif 1123 1124 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) 1125 return; 1126 1127 /* RFC 2292 sec. 5 */ 1128 if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) { 1129 struct in6_pktinfo pi6; 1130 1131 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 1132 in6_clearscope(&pi6.ipi6_addr); /* XXX */ 1133 pi6.ipi6_ifindex = 1134 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0; 1135 1136 *mp = sbcreatecontrol((caddr_t) &pi6, 1137 sizeof(struct in6_pktinfo), 1138 IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6); 1139 if (*mp) 1140 mp = &(*mp)->m_next; 1141 } 1142 1143 if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) { 1144 int hlim = ip6->ip6_hlim & 0xff; 1145 1146 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int), 1147 IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6); 1148 if (*mp) 1149 mp = &(*mp)->m_next; 1150 } 1151 1152 if ((in6p->in6p_flags & IN6P_TCLASS) != 0) { 1153 u_int32_t flowinfo; 1154 int tclass; 1155 1156 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK); 1157 flowinfo >>= 20; 1158 1159 tclass = flowinfo & 0xff; 1160 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass), 1161 IPV6_TCLASS, IPPROTO_IPV6); 1162 if (*mp) 1163 mp = &(*mp)->m_next; 1164 } 1165 1166 /* 1167 * IPV6_HOPOPTS socket option. Recall that we required super-user 1168 * privilege for the option (see ip6_ctloutput), but it might be too 1169 * strict, since there might be some hop-by-hop options which can be 1170 * returned to normal user. 1171 * See also RFC 2292 section 6 (or RFC 3542 section 8). 1172 */ 1173 if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) { 1174 /* 1175 * Check if a hop-by-hop options header is contatined in the 1176 * received packet, and if so, store the options as ancillary 1177 * data. Note that a hop-by-hop options header must be 1178 * just after the IPv6 header, which is assured through the 1179 * IPv6 input processing. 1180 */ 1181 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 1182 struct ip6_hbh *hbh; 1183 int hbhlen = 0; 1184#ifdef PULLDOWN_TEST 1185 struct mbuf *ext; 1186#endif 1187 1188#ifndef PULLDOWN_TEST 1189 hbh = (struct ip6_hbh *)(ip6 + 1); 1190 hbhlen = (hbh->ip6h_len + 1) << 3; 1191#else 1192 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 1193 ip6->ip6_nxt); 1194 if (ext == NULL) { 1195 ip6stat.ip6s_tooshort++; 1196 return; 1197 } 1198 hbh = mtod(ext, struct ip6_hbh *); 1199 hbhlen = (hbh->ip6h_len + 1) << 3; 1200 if (hbhlen != ext->m_len) { 1201 m_freem(ext); 1202 ip6stat.ip6s_tooshort++; 1203 return; 1204 } 1205#endif 1206 1207 /* 1208 * XXX: We copy the whole header even if a 1209 * jumbo payload option is included, the option which 1210 * is to be removed before returning according to 1211 * RFC2292. 1212 * Note: this constraint is removed in 2292bis. 1213 */ 1214 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 1215 IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS), 1216 IPPROTO_IPV6); 1217 if (*mp) 1218 mp = &(*mp)->m_next; 1219#ifdef PULLDOWN_TEST 1220 m_freem(ext); 1221#endif 1222 } 1223 } 1224 1225 if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) { 1226 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 1227 1228 /* 1229 * Search for destination options headers or routing 1230 * header(s) through the header chain, and stores each 1231 * header as ancillary data. 1232 * Note that the order of the headers remains in 1233 * the chain of ancillary data. 1234 */ 1235 while (1) { /* is explicit loop prevention necessary? */ 1236 struct ip6_ext *ip6e = NULL; 1237 int elen; 1238#ifdef PULLDOWN_TEST 1239 struct mbuf *ext = NULL; 1240#endif 1241 1242 /* 1243 * if it is not an extension header, don't try to 1244 * pull it from the chain. 1245 */ 1246 switch (nxt) { 1247 case IPPROTO_DSTOPTS: 1248 case IPPROTO_ROUTING: 1249 case IPPROTO_HOPOPTS: 1250 case IPPROTO_AH: /* is it possible? */ 1251 break; 1252 default: 1253 goto loopend; 1254 } 1255 1256#ifndef PULLDOWN_TEST 1257 if (off + sizeof(*ip6e) > m->m_len) 1258 goto loopend; 1259 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 1260 if (nxt == IPPROTO_AH) 1261 elen = (ip6e->ip6e_len + 2) << 2; 1262 else 1263 elen = (ip6e->ip6e_len + 1) << 3; 1264 if (off + elen > m->m_len) 1265 goto loopend; 1266#else 1267 ext = ip6_pullexthdr(m, off, nxt); 1268 if (ext == NULL) { 1269 ip6stat.ip6s_tooshort++; 1270 return; 1271 } 1272 ip6e = mtod(ext, struct ip6_ext *); 1273 if (nxt == IPPROTO_AH) 1274 elen = (ip6e->ip6e_len + 2) << 2; 1275 else 1276 elen = (ip6e->ip6e_len + 1) << 3; 1277 if (elen != ext->m_len) { 1278 m_freem(ext); 1279 ip6stat.ip6s_tooshort++; 1280 return; 1281 } 1282#endif 1283 1284 switch (nxt) { 1285 case IPPROTO_DSTOPTS: 1286 if (!(in6p->in6p_flags & IN6P_DSTOPTS)) 1287 break; 1288 1289 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1290 IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS), 1291 IPPROTO_IPV6); 1292 if (*mp) 1293 mp = &(*mp)->m_next; 1294 break; 1295 case IPPROTO_ROUTING: 1296 if (!in6p->in6p_flags & IN6P_RTHDR) 1297 break; 1298 1299 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1300 IS2292(IPV6_2292RTHDR, IPV6_RTHDR), 1301 IPPROTO_IPV6); 1302 if (*mp) 1303 mp = &(*mp)->m_next; 1304 break; 1305 case IPPROTO_HOPOPTS: 1306 case IPPROTO_AH: /* is it possible? */ 1307 break; 1308 1309 default: 1310 /* 1311 * other cases have been filtered in the above. 1312 * none will visit this case. here we supply 1313 * the code just in case (nxt overwritten or 1314 * other cases). 1315 */ 1316#ifdef PULLDOWN_TEST 1317 m_freem(ext); 1318#endif 1319 goto loopend; 1320 1321 } 1322 1323 /* proceed with the next header. */ 1324 off += elen; 1325 nxt = ip6e->ip6e_nxt; 1326 ip6e = NULL; 1327#ifdef PULLDOWN_TEST 1328 m_freem(ext); 1329 ext = NULL; 1330#endif 1331 } 1332 loopend: 1333 ; 1334 } 1335 1336#undef IS2292 1337} 1338 1339void 1340ip6_notify_pmtu(in6p, dst, mtu) 1341 struct inpcb *in6p; 1342 struct sockaddr_in6 *dst; 1343 u_int32_t *mtu; 1344{ 1345 struct socket *so; 1346 struct mbuf *m_mtu; 1347 struct ip6_mtuinfo mtuctl; 1348 1349 so = in6p->inp_socket; 1350 1351 if (mtu == NULL) 1352 return; 1353 1354#ifdef DIAGNOSTIC 1355 if (so == NULL) /* I believe this is impossible */ 1356 panic("ip6_notify_pmtu: socket is NULL"); 1357#endif 1358 1359 bzero(&mtuctl, sizeof(mtuctl)); /* zero-clear for safety */ 1360 mtuctl.ip6m_mtu = *mtu; 1361 mtuctl.ip6m_addr = *dst; 1362 in6_recoverscope(&mtuctl.ip6m_addr, &mtuctl.ip6m_addr.sin6_addr, NULL); 1363 1364 if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl), 1365 IPV6_PATHMTU, IPPROTO_IPV6)) == NULL) 1366 return; 1367 1368 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu) 1369 == 0) { 1370 m_freem(m_mtu); 1371 /* XXX: should count statistics */ 1372 } else 1373 sorwakeup(so); 1374 1375 return; 1376} 1377 1378#ifdef PULLDOWN_TEST 1379/* 1380 * pull single extension header from mbuf chain. returns single mbuf that 1381 * contains the result, or NULL on error. 1382 */ 1383static struct mbuf * 1384ip6_pullexthdr(m, off, nxt) 1385 struct mbuf *m; 1386 size_t off; 1387 int nxt; 1388{ 1389 struct ip6_ext ip6e; 1390 size_t elen; 1391 struct mbuf *n; 1392 1393#ifdef DIAGNOSTIC 1394 switch (nxt) { 1395 case IPPROTO_DSTOPTS: 1396 case IPPROTO_ROUTING: 1397 case IPPROTO_HOPOPTS: 1398 case IPPROTO_AH: /* is it possible? */ 1399 break; 1400 default: 1401 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 1402 } 1403#endif 1404 1405 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1406 if (nxt == IPPROTO_AH) 1407 elen = (ip6e.ip6e_len + 2) << 2; 1408 else 1409 elen = (ip6e.ip6e_len + 1) << 3; 1410 1411 MGET(n, M_DONTWAIT, MT_DATA); 1412 if (n && elen >= MLEN) { 1413 MCLGET(n, M_DONTWAIT); 1414 if ((n->m_flags & M_EXT) == 0) { 1415 m_free(n); 1416 n = NULL; 1417 } 1418 } 1419 if (!n) 1420 return NULL; 1421 1422 n->m_len = 0; 1423 if (elen >= M_TRAILINGSPACE(n)) { 1424 m_free(n); 1425 return NULL; 1426 } 1427 1428 m_copydata(m, off, elen, mtod(n, caddr_t)); 1429 n->m_len = elen; 1430 return n; 1431} 1432#endif 1433 1434/* 1435 * Get pointer to the previous header followed by the header 1436 * currently processed. 1437 * XXX: This function supposes that 1438 * M includes all headers, 1439 * the next header field and the header length field of each header 1440 * are valid, and 1441 * the sum of each header length equals to OFF. 1442 * Because of these assumptions, this function must be called very 1443 * carefully. Moreover, it will not be used in the near future when 1444 * we develop `neater' mechanism to process extension headers. 1445 */ 1446char * 1447ip6_get_prevhdr(m, off) 1448 struct mbuf *m; 1449 int off; 1450{ 1451 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1452 1453 if (off == sizeof(struct ip6_hdr)) 1454 return (&ip6->ip6_nxt); 1455 else { 1456 int len, nxt; 1457 struct ip6_ext *ip6e = NULL; 1458 1459 nxt = ip6->ip6_nxt; 1460 len = sizeof(struct ip6_hdr); 1461 while (len < off) { 1462 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 1463 1464 switch (nxt) { 1465 case IPPROTO_FRAGMENT: 1466 len += sizeof(struct ip6_frag); 1467 break; 1468 case IPPROTO_AH: 1469 len += (ip6e->ip6e_len + 2) << 2; 1470 break; 1471 default: 1472 len += (ip6e->ip6e_len + 1) << 3; 1473 break; 1474 } 1475 nxt = ip6e->ip6e_nxt; 1476 } 1477 if (ip6e) 1478 return (&ip6e->ip6e_nxt); 1479 else 1480 return NULL; 1481 } 1482} 1483 1484/* 1485 * get next header offset. m will be retained. 1486 */ 1487int 1488ip6_nexthdr(m, off, proto, nxtp) 1489 struct mbuf *m; 1490 int off; 1491 int proto; 1492 int *nxtp; 1493{ 1494 struct ip6_hdr ip6; 1495 struct ip6_ext ip6e; 1496 struct ip6_frag fh; 1497 1498 /* just in case */ 1499 if (m == NULL) 1500 panic("ip6_nexthdr: m == NULL"); 1501 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 1502 return -1; 1503 1504 switch (proto) { 1505 case IPPROTO_IPV6: 1506 if (m->m_pkthdr.len < off + sizeof(ip6)) 1507 return -1; 1508 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 1509 if (nxtp) 1510 *nxtp = ip6.ip6_nxt; 1511 off += sizeof(ip6); 1512 return off; 1513 1514 case IPPROTO_FRAGMENT: 1515 /* 1516 * terminate parsing if it is not the first fragment, 1517 * it does not make sense to parse through it. 1518 */ 1519 if (m->m_pkthdr.len < off + sizeof(fh)) 1520 return -1; 1521 m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 1522 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */ 1523 if (fh.ip6f_offlg & IP6F_OFF_MASK) 1524 return -1; 1525 if (nxtp) 1526 *nxtp = fh.ip6f_nxt; 1527 off += sizeof(struct ip6_frag); 1528 return off; 1529 1530 case IPPROTO_AH: 1531 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1532 return -1; 1533 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1534 if (nxtp) 1535 *nxtp = ip6e.ip6e_nxt; 1536 off += (ip6e.ip6e_len + 2) << 2; 1537 return off; 1538 1539 case IPPROTO_HOPOPTS: 1540 case IPPROTO_ROUTING: 1541 case IPPROTO_DSTOPTS: 1542 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1543 return -1; 1544 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1545 if (nxtp) 1546 *nxtp = ip6e.ip6e_nxt; 1547 off += (ip6e.ip6e_len + 1) << 3; 1548 return off; 1549 1550 case IPPROTO_NONE: 1551 case IPPROTO_ESP: 1552 case IPPROTO_IPCOMP: 1553 /* give up */ 1554 return -1; 1555 1556 default: 1557 return -1; 1558 } 1559 1560 return -1; 1561} 1562 1563/* 1564 * get offset for the last header in the chain. m will be kept untainted. 1565 */ 1566int 1567ip6_lasthdr(m, off, proto, nxtp) 1568 struct mbuf *m; 1569 int off; 1570 int proto; 1571 int *nxtp; 1572{ 1573 int newoff; 1574 int nxt; 1575 1576 if (!nxtp) { 1577 nxt = -1; 1578 nxtp = &nxt; 1579 } 1580 while (1) { 1581 newoff = ip6_nexthdr(m, off, proto, nxtp); 1582 if (newoff < 0) 1583 return off; 1584 else if (newoff < off) 1585 return -1; /* invalid */ 1586 else if (newoff == off) 1587 return newoff; 1588 1589 off = newoff; 1590 proto = *nxtp; 1591 } 1592} 1593 1594struct ip6aux * 1595ip6_addaux(m) 1596 struct mbuf *m; 1597{ 1598 struct m_tag *mtag; 1599 1600 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1601 if (!mtag) { 1602 mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux), 1603 M_NOWAIT); 1604 if (mtag) { 1605 m_tag_prepend(m, mtag); 1606 bzero(mtag + 1, sizeof(struct ip6aux)); 1607 } 1608 } 1609 return mtag ? (struct ip6aux *)(mtag + 1) : NULL; 1610} 1611 1612struct ip6aux * 1613ip6_findaux(m) 1614 struct mbuf *m; 1615{ 1616 struct m_tag *mtag; 1617 1618 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1619 return mtag ? (struct ip6aux *)(mtag + 1) : NULL; 1620} 1621 1622void 1623ip6_delaux(m) 1624 struct mbuf *m; 1625{ 1626 struct m_tag *mtag; 1627 1628 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1629 if (mtag) 1630 m_tag_delete(m, mtag); 1631} 1632 1633/* 1634 * System control for IP6 1635 */ 1636 1637u_char inet6ctlerrmap[PRC_NCMDS] = { 1638 0, 0, 0, 0, 1639 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1640 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1641 EMSGSIZE, EHOSTUNREACH, 0, 0, 1642 0, 0, 0, 0, 1643 ENOPROTOOPT 1644}; 1645