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