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