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) { 777 switch (rtalert) { 778 case IP6OPT_RTALERT_MLD: 779 if (V_ip6_forwarding) 780 ours = 1; 781 break; 782 default: 783 /* 784 * RFC2711 requires unrecognized values must be 785 * silently ignored. 786 */ 787 break; 788 } 789 } 790 } else 791 nxt = ip6->ip6_nxt; 792 793 /* 794 * Check that the amount of data in the buffers 795 * is as at least much as the IPv6 header would have us expect. 796 * Trim mbufs if longer than we expect. 797 * Drop packet if shorter than we expect. 798 */ 799 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 800 V_ip6stat.ip6s_tooshort++; 801 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 802 goto bad; 803 } 804 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 805 if (m->m_len == m->m_pkthdr.len) { 806 m->m_len = sizeof(struct ip6_hdr) + plen; 807 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 808 } else 809 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 810 } 811 812 /* 813 * Forward if desirable. 814 */ 815 if (V_ip6_mrouter && 816 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 817 /* 818 * If we are acting as a multicast router, all 819 * incoming multicast packets are passed to the 820 * kernel-level multicast forwarding function. 821 * The packet is returned (relatively) intact; if 822 * ip6_mforward() returns a non-zero value, the packet 823 * must be discarded, else it may be accepted below. 824 * 825 * XXX TODO: Check hlim and multicast scope here to avoid 826 * unnecessarily calling into ip6_mforward(). 827 */ 828 if (ip6_mforward && 829 ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) { 830 IP6STAT_INC(ip6s_cantforward); 831 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 832 goto bad; 833 } 834 } else if (!ours) { 835 ip6_forward(m, srcrt); 836 goto out; 837 } 838 839 ip6 = mtod(m, struct ip6_hdr *); 840 841 /* 842 * Malicious party may be able to use IPv4 mapped addr to confuse 843 * tcp/udp stack and bypass security checks (act as if it was from 844 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 845 * 846 * For SIIT end node behavior, you may want to disable the check. 847 * However, you will become vulnerable to attacks using IPv4 mapped 848 * source. 849 */ 850 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 851 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 852 V_ip6stat.ip6s_badscope++; 853 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 854 goto bad; 855 } 856 857 /* 858 * Tell launch routine the next header 859 */ 860 V_ip6stat.ip6s_delivered++; 861 in6_ifstat_inc(deliverifp, ifs6_in_deliver); 862 nest = 0; 863 864 while (nxt != IPPROTO_DONE) { 865 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) { 866 V_ip6stat.ip6s_toomanyhdr++; 867 goto bad; 868 } 869 870 /* 871 * protection against faulty packet - there should be 872 * more sanity checks in header chain processing. 873 */ 874 if (m->m_pkthdr.len < off) { 875 V_ip6stat.ip6s_tooshort++; 876 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 877 goto bad; 878 } 879 880 #ifdef IPSEC 881 /* 882 * enforce IPsec policy checking if we are seeing last header. 883 * note that we do not visit this with protocols with pcb layer 884 * code - like udp/tcp/raw ip. 885 */ 886 if (ip6_ipsec_input(m, nxt)) 887 goto bad; 888 #endif /* IPSEC */ 889 890 /* 891 * Use mbuf flags to propagate Router Alert option to 892 * ICMPv6 layer, as hop-by-hop options have been stripped. 893 */ 894 if (nxt == IPPROTO_ICMPV6 && rtalert != ~0) 895 m->m_flags |= M_RTALERT_MLD; 896 897 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt); 898 } 899 goto out; 900 bad: 901 m_freem(m); 902 out: 903 if (rin6.ro_rt) 904 RTFREE(rin6.ro_rt); 905 } 906 907 /* 908 * set/grab in6_ifaddr correspond to IPv6 destination address. 909 * XXX backward compatibility wrapper 910 */ 911 static struct ip6aux * 912 ip6_setdstifaddr(struct mbuf *m, struct in6_ifaddr *ia6) 913 { 914 struct ip6aux *ip6a; 915 916 ip6a = ip6_addaux(m); 917 if (ip6a) 918 ip6a->ip6a_dstia6 = ia6; 919 return ip6a; /* NULL if failed to set */ 920 } 921 922 struct in6_ifaddr * 923 ip6_getdstifaddr(struct mbuf *m) 924 { 925 struct ip6aux *ip6a; 926 927 ip6a = ip6_findaux(m); 928 if (ip6a) 929 return ip6a->ip6a_dstia6; 930 else 931 return NULL; 932 } 933 934 /* 935 * Hop-by-Hop options header processing. If a valid jumbo payload option is 936 * included, the real payload length will be stored in plenp. 937 * 938 * rtalertp - XXX: should be stored more smart way 939 */ 940 static int 941 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp, 942 struct mbuf **mp, int *offp) 943 { 944 INIT_VNET_INET6(curvnet); 945 struct mbuf *m = *mp; 946 int off = *offp, hbhlen; 947 struct ip6_hbh *hbh; 948 u_int8_t *opt; 949 950 /* validation of the length of the header */ 951 #ifndef PULLDOWN_TEST 952 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1); 953 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 954 hbhlen = (hbh->ip6h_len + 1) << 3; 955 956 IP6_EXTHDR_CHECK(m, off, hbhlen, -1); 957 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 958 #else 959 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, 960 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 961 if (hbh == NULL) { 962 V_ip6stat.ip6s_tooshort++; 963 return -1; 964 } 965 hbhlen = (hbh->ip6h_len + 1) << 3; 966 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 967 hbhlen); 968 if (hbh == NULL) { 969 V_ip6stat.ip6s_tooshort++; 970 return -1; 971 } 972 #endif 973 off += hbhlen; 974 hbhlen -= sizeof(struct ip6_hbh); 975 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh); 976 977 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 978 hbhlen, rtalertp, plenp) < 0) 979 return (-1); 980 981 *offp = off; 982 *mp = m; 983 return (0); 984 } 985 986 /* 987 * Search header for all Hop-by-hop options and process each option. 988 * This function is separate from ip6_hopopts_input() in order to 989 * handle a case where the sending node itself process its hop-by-hop 990 * options header. In such a case, the function is called from ip6_output(). 991 * 992 * The function assumes that hbh header is located right after the IPv6 header 993 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 994 * opthead + hbhlen is located in continuous memory region. 995 */ 996 int 997 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen, 998 u_int32_t *rtalertp, u_int32_t *plenp) 999 { 1000 INIT_VNET_INET6(curvnet); 1001 struct ip6_hdr *ip6; 1002 int optlen = 0; 1003 u_int8_t *opt = opthead; 1004 u_int16_t rtalert_val; 1005 u_int32_t jumboplen; 1006 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 1007 1008 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 1009 switch (*opt) { 1010 case IP6OPT_PAD1: 1011 optlen = 1; 1012 break; 1013 case IP6OPT_PADN: 1014 if (hbhlen < IP6OPT_MINLEN) { 1015 V_ip6stat.ip6s_toosmall++; 1016 goto bad; 1017 } 1018 optlen = *(opt + 1) + 2; 1019 break; 1020 case IP6OPT_ROUTER_ALERT: 1021 /* XXX may need check for alignment */ 1022 if (hbhlen < IP6OPT_RTALERT_LEN) { 1023 V_ip6stat.ip6s_toosmall++; 1024 goto bad; 1025 } 1026 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 1027 /* XXX stat */ 1028 icmp6_error(m, ICMP6_PARAM_PROB, 1029 ICMP6_PARAMPROB_HEADER, 1030 erroff + opt + 1 - opthead); 1031 return (-1); 1032 } 1033 optlen = IP6OPT_RTALERT_LEN; 1034 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 1035 *rtalertp = ntohs(rtalert_val); 1036 break; 1037 case IP6OPT_JUMBO: 1038 /* XXX may need check for alignment */ 1039 if (hbhlen < IP6OPT_JUMBO_LEN) { 1040 V_ip6stat.ip6s_toosmall++; 1041 goto bad; 1042 } 1043 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 1044 /* XXX stat */ 1045 icmp6_error(m, ICMP6_PARAM_PROB, 1046 ICMP6_PARAMPROB_HEADER, 1047 erroff + opt + 1 - opthead); 1048 return (-1); 1049 } 1050 optlen = IP6OPT_JUMBO_LEN; 1051 1052 /* 1053 * IPv6 packets that have non 0 payload length 1054 * must not contain a jumbo payload option. 1055 */ 1056 ip6 = mtod(m, struct ip6_hdr *); 1057 if (ip6->ip6_plen) { 1058 V_ip6stat.ip6s_badoptions++; 1059 icmp6_error(m, ICMP6_PARAM_PROB, 1060 ICMP6_PARAMPROB_HEADER, 1061 erroff + opt - opthead); 1062 return (-1); 1063 } 1064 1065 /* 1066 * We may see jumbolen in unaligned location, so 1067 * we'd need to perform bcopy(). 1068 */ 1069 bcopy(opt + 2, &jumboplen, sizeof(jumboplen)); 1070 jumboplen = (u_int32_t)htonl(jumboplen); 1071 1072 #if 1 1073 /* 1074 * if there are multiple jumbo payload options, 1075 * *plenp will be non-zero and the packet will be 1076 * rejected. 1077 * the behavior may need some debate in ipngwg - 1078 * multiple options does not make sense, however, 1079 * there's no explicit mention in specification. 1080 */ 1081 if (*plenp != 0) { 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 #endif 1089 1090 /* 1091 * jumbo payload length must be larger than 65535. 1092 */ 1093 if (jumboplen <= IPV6_MAXPACKET) { 1094 V_ip6stat.ip6s_badoptions++; 1095 icmp6_error(m, ICMP6_PARAM_PROB, 1096 ICMP6_PARAMPROB_HEADER, 1097 erroff + opt + 2 - opthead); 1098 return (-1); 1099 } 1100 *plenp = jumboplen; 1101 1102 break; 1103 default: /* unknown option */ 1104 if (hbhlen < IP6OPT_MINLEN) { 1105 V_ip6stat.ip6s_toosmall++; 1106 goto bad; 1107 } 1108 optlen = ip6_unknown_opt(opt, m, 1109 erroff + opt - opthead); 1110 if (optlen == -1) 1111 return (-1); 1112 optlen += 2; 1113 break; 1114 } 1115 } 1116 1117 return (0); 1118 1119 bad: 1120 m_freem(m); 1121 return (-1); 1122 } 1123 1124 /* 1125 * Unknown option processing. 1126 * The third argument `off' is the offset from the IPv6 header to the option, 1127 * which is necessary if the IPv6 header the and option header and IPv6 header 1128 * is not continuous in order to return an ICMPv6 error. 1129 */ 1130 int 1131 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off) 1132 { 1133 INIT_VNET_INET6(curvnet); 1134 struct ip6_hdr *ip6; 1135 1136 switch (IP6OPT_TYPE(*optp)) { 1137 case IP6OPT_TYPE_SKIP: /* ignore the option */ 1138 return ((int)*(optp + 1)); 1139 case IP6OPT_TYPE_DISCARD: /* silently discard */ 1140 m_freem(m); 1141 return (-1); 1142 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 1143 V_ip6stat.ip6s_badoptions++; 1144 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 1145 return (-1); 1146 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 1147 V_ip6stat.ip6s_badoptions++; 1148 ip6 = mtod(m, struct ip6_hdr *); 1149 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1150 (m->m_flags & (M_BCAST|M_MCAST))) 1151 m_freem(m); 1152 else 1153 icmp6_error(m, ICMP6_PARAM_PROB, 1154 ICMP6_PARAMPROB_OPTION, off); 1155 return (-1); 1156 } 1157 1158 m_freem(m); /* XXX: NOTREACHED */ 1159 return (-1); 1160 } 1161 1162 /* 1163 * Create the "control" list for this pcb. 1164 * These functions will not modify mbuf chain at all. 1165 * 1166 * With KAME mbuf chain restriction: 1167 * The routine will be called from upper layer handlers like tcp6_input(). 1168 * Thus the routine assumes that the caller (tcp6_input) have already 1169 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 1170 * very first mbuf on the mbuf chain. 1171 * 1172 * ip6_savecontrol_v4 will handle those options that are possible to be 1173 * set on a v4-mapped socket. 1174 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those 1175 * options and handle the v6-only ones itself. 1176 */ 1177 struct mbuf ** 1178 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp, 1179 int *v4only) 1180 { 1181 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1182 1183 #ifdef SO_TIMESTAMP 1184 if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) { 1185 struct timeval tv; 1186 1187 microtime(&tv); 1188 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1189 SCM_TIMESTAMP, SOL_SOCKET); 1190 if (*mp) 1191 mp = &(*mp)->m_next; 1192 } 1193 #endif 1194 1195 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1196 if (v4only != NULL) 1197 *v4only = 1; 1198 return (mp); 1199 } 1200 1201 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y)) 1202 /* RFC 2292 sec. 5 */ 1203 if ((inp->inp_flags & IN6P_PKTINFO) != 0) { 1204 struct in6_pktinfo pi6; 1205 1206 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 1207 in6_clearscope(&pi6.ipi6_addr); /* XXX */ 1208 pi6.ipi6_ifindex = 1209 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0; 1210 1211 *mp = sbcreatecontrol((caddr_t) &pi6, 1212 sizeof(struct in6_pktinfo), 1213 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6); 1214 if (*mp) 1215 mp = &(*mp)->m_next; 1216 } 1217 1218 if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) { 1219 int hlim = ip6->ip6_hlim & 0xff; 1220 1221 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int), 1222 IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), 1223 IPPROTO_IPV6); 1224 if (*mp) 1225 mp = &(*mp)->m_next; 1226 } 1227 1228 if (v4only != NULL) 1229 *v4only = 0; 1230 return (mp); 1231 } 1232 1233 void 1234 ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp) 1235 { 1236 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1237 int v4only = 0; 1238 1239 mp = ip6_savecontrol_v4(in6p, m, mp, &v4only); 1240 if (v4only) 1241 return; 1242 1243 if ((in6p->inp_flags & IN6P_TCLASS) != 0) { 1244 u_int32_t flowinfo; 1245 int tclass; 1246 1247 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK); 1248 flowinfo >>= 20; 1249 1250 tclass = flowinfo & 0xff; 1251 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass), 1252 IPV6_TCLASS, IPPROTO_IPV6); 1253 if (*mp) 1254 mp = &(*mp)->m_next; 1255 } 1256 1257 /* 1258 * IPV6_HOPOPTS socket option. Recall that we required super-user 1259 * privilege for the option (see ip6_ctloutput), but it might be too 1260 * strict, since there might be some hop-by-hop options which can be 1261 * returned to normal user. 1262 * See also RFC 2292 section 6 (or RFC 3542 section 8). 1263 */ 1264 if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) { 1265 /* 1266 * Check if a hop-by-hop options header is contatined in the 1267 * received packet, and if so, store the options as ancillary 1268 * data. Note that a hop-by-hop options header must be 1269 * just after the IPv6 header, which is assured through the 1270 * IPv6 input processing. 1271 */ 1272 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 1273 struct ip6_hbh *hbh; 1274 int hbhlen = 0; 1275 #ifdef PULLDOWN_TEST 1276 struct mbuf *ext; 1277 #endif 1278 1279 #ifndef PULLDOWN_TEST 1280 hbh = (struct ip6_hbh *)(ip6 + 1); 1281 hbhlen = (hbh->ip6h_len + 1) << 3; 1282 #else 1283 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 1284 ip6->ip6_nxt); 1285 if (ext == NULL) { 1286 V_ip6stat.ip6s_tooshort++; 1287 return; 1288 } 1289 hbh = mtod(ext, struct ip6_hbh *); 1290 hbhlen = (hbh->ip6h_len + 1) << 3; 1291 if (hbhlen != ext->m_len) { 1292 m_freem(ext); 1293 V_ip6stat.ip6s_tooshort++; 1294 return; 1295 } 1296 #endif 1297 1298 /* 1299 * XXX: We copy the whole header even if a 1300 * jumbo payload option is included, the option which 1301 * is to be removed before returning according to 1302 * RFC2292. 1303 * Note: this constraint is removed in RFC3542 1304 */ 1305 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 1306 IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS), 1307 IPPROTO_IPV6); 1308 if (*mp) 1309 mp = &(*mp)->m_next; 1310 #ifdef PULLDOWN_TEST 1311 m_freem(ext); 1312 #endif 1313 } 1314 } 1315 1316 if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) { 1317 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 1318 1319 /* 1320 * Search for destination options headers or routing 1321 * header(s) through the header chain, and stores each 1322 * header as ancillary data. 1323 * Note that the order of the headers remains in 1324 * the chain of ancillary data. 1325 */ 1326 while (1) { /* is explicit loop prevention necessary? */ 1327 struct ip6_ext *ip6e = NULL; 1328 int elen; 1329 #ifdef PULLDOWN_TEST 1330 struct mbuf *ext = NULL; 1331 #endif 1332 1333 /* 1334 * if it is not an extension header, don't try to 1335 * pull it from the chain. 1336 */ 1337 switch (nxt) { 1338 case IPPROTO_DSTOPTS: 1339 case IPPROTO_ROUTING: 1340 case IPPROTO_HOPOPTS: 1341 case IPPROTO_AH: /* is it possible? */ 1342 break; 1343 default: 1344 goto loopend; 1345 } 1346 1347 #ifndef PULLDOWN_TEST 1348 if (off + sizeof(*ip6e) > m->m_len) 1349 goto loopend; 1350 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 1351 if (nxt == IPPROTO_AH) 1352 elen = (ip6e->ip6e_len + 2) << 2; 1353 else 1354 elen = (ip6e->ip6e_len + 1) << 3; 1355 if (off + elen > m->m_len) 1356 goto loopend; 1357 #else 1358 ext = ip6_pullexthdr(m, off, nxt); 1359 if (ext == NULL) { 1360 V_ip6stat.ip6s_tooshort++; 1361 return; 1362 } 1363 ip6e = mtod(ext, struct ip6_ext *); 1364 if (nxt == IPPROTO_AH) 1365 elen = (ip6e->ip6e_len + 2) << 2; 1366 else 1367 elen = (ip6e->ip6e_len + 1) << 3; 1368 if (elen != ext->m_len) { 1369 m_freem(ext); 1370 V_ip6stat.ip6s_tooshort++; 1371 return; 1372 } 1373 #endif 1374 1375 switch (nxt) { 1376 case IPPROTO_DSTOPTS: 1377 if (!(in6p->inp_flags & IN6P_DSTOPTS)) 1378 break; 1379 1380 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1381 IS2292(in6p, 1382 IPV6_2292DSTOPTS, IPV6_DSTOPTS), 1383 IPPROTO_IPV6); 1384 if (*mp) 1385 mp = &(*mp)->m_next; 1386 break; 1387 case IPPROTO_ROUTING: 1388 if (!in6p->inp_flags & IN6P_RTHDR) 1389 break; 1390 1391 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1392 IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR), 1393 IPPROTO_IPV6); 1394 if (*mp) 1395 mp = &(*mp)->m_next; 1396 break; 1397 case IPPROTO_HOPOPTS: 1398 case IPPROTO_AH: /* is it possible? */ 1399 break; 1400 1401 default: 1402 /* 1403 * other cases have been filtered in the above. 1404 * none will visit this case. here we supply 1405 * the code just in case (nxt overwritten or 1406 * other cases). 1407 */ 1408 #ifdef PULLDOWN_TEST 1409 m_freem(ext); 1410 #endif 1411 goto loopend; 1412 1413 } 1414 1415 /* proceed with the next header. */ 1416 off += elen; 1417 nxt = ip6e->ip6e_nxt; 1418 ip6e = NULL; 1419 #ifdef PULLDOWN_TEST 1420 m_freem(ext); 1421 ext = NULL; 1422 #endif 1423 } 1424 loopend: 1425 ; 1426 } 1427 } 1428 #undef IS2292 1429 1430 void 1431 ip6_notify_pmtu(struct inpcb *in6p, struct sockaddr_in6 *dst, u_int32_t *mtu) 1432 { 1433 struct socket *so; 1434 struct mbuf *m_mtu; 1435 struct ip6_mtuinfo mtuctl; 1436 1437 so = in6p->inp_socket; 1438 1439 if (mtu == NULL) 1440 return; 1441 1442 #ifdef DIAGNOSTIC 1443 if (so == NULL) /* I believe this is impossible */ 1444 panic("ip6_notify_pmtu: socket is NULL"); 1445 #endif 1446 1447 bzero(&mtuctl, sizeof(mtuctl)); /* zero-clear for safety */ 1448 mtuctl.ip6m_mtu = *mtu; 1449 mtuctl.ip6m_addr = *dst; 1450 if (sa6_recoverscope(&mtuctl.ip6m_addr)) 1451 return; 1452 1453 if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl), 1454 IPV6_PATHMTU, IPPROTO_IPV6)) == NULL) 1455 return; 1456 1457 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu) 1458 == 0) { 1459 m_freem(m_mtu); 1460 /* XXX: should count statistics */ 1461 } else 1462 sorwakeup(so); 1463 1464 return; 1465 } 1466 1467 #ifdef PULLDOWN_TEST 1468 /* 1469 * pull single extension header from mbuf chain. returns single mbuf that 1470 * contains the result, or NULL on error. 1471 */ 1472 static struct mbuf * 1473 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt) 1474 { 1475 struct ip6_ext ip6e; 1476 size_t elen; 1477 struct mbuf *n; 1478 1479 #ifdef DIAGNOSTIC 1480 switch (nxt) { 1481 case IPPROTO_DSTOPTS: 1482 case IPPROTO_ROUTING: 1483 case IPPROTO_HOPOPTS: 1484 case IPPROTO_AH: /* is it possible? */ 1485 break; 1486 default: 1487 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 1488 } 1489 #endif 1490 1491 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1492 if (nxt == IPPROTO_AH) 1493 elen = (ip6e.ip6e_len + 2) << 2; 1494 else 1495 elen = (ip6e.ip6e_len + 1) << 3; 1496 1497 MGET(n, M_DONTWAIT, MT_DATA); 1498 if (n && elen >= MLEN) { 1499 MCLGET(n, M_DONTWAIT); 1500 if ((n->m_flags & M_EXT) == 0) { 1501 m_free(n); 1502 n = NULL; 1503 } 1504 } 1505 if (!n) 1506 return NULL; 1507 1508 n->m_len = 0; 1509 if (elen >= M_TRAILINGSPACE(n)) { 1510 m_free(n); 1511 return NULL; 1512 } 1513 1514 m_copydata(m, off, elen, mtod(n, caddr_t)); 1515 n->m_len = elen; 1516 return n; 1517 } 1518 #endif 1519 1520 /* 1521 * Get pointer to the previous header followed by the header 1522 * currently processed. 1523 * XXX: This function supposes that 1524 * M includes all headers, 1525 * the next header field and the header length field of each header 1526 * are valid, and 1527 * the sum of each header length equals to OFF. 1528 * Because of these assumptions, this function must be called very 1529 * carefully. Moreover, it will not be used in the near future when 1530 * we develop `neater' mechanism to process extension headers. 1531 */ 1532 char * 1533 ip6_get_prevhdr(struct mbuf *m, int off) 1534 { 1535 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1536 1537 if (off == sizeof(struct ip6_hdr)) 1538 return (&ip6->ip6_nxt); 1539 else { 1540 int len, nxt; 1541 struct ip6_ext *ip6e = NULL; 1542 1543 nxt = ip6->ip6_nxt; 1544 len = sizeof(struct ip6_hdr); 1545 while (len < off) { 1546 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 1547 1548 switch (nxt) { 1549 case IPPROTO_FRAGMENT: 1550 len += sizeof(struct ip6_frag); 1551 break; 1552 case IPPROTO_AH: 1553 len += (ip6e->ip6e_len + 2) << 2; 1554 break; 1555 default: 1556 len += (ip6e->ip6e_len + 1) << 3; 1557 break; 1558 } 1559 nxt = ip6e->ip6e_nxt; 1560 } 1561 if (ip6e) 1562 return (&ip6e->ip6e_nxt); 1563 else 1564 return NULL; 1565 } 1566 } 1567 1568 /* 1569 * get next header offset. m will be retained. 1570 */ 1571 int 1572 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp) 1573 { 1574 struct ip6_hdr ip6; 1575 struct ip6_ext ip6e; 1576 struct ip6_frag fh; 1577 1578 /* just in case */ 1579 if (m == NULL) 1580 panic("ip6_nexthdr: m == NULL"); 1581 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 1582 return -1; 1583 1584 switch (proto) { 1585 case IPPROTO_IPV6: 1586 if (m->m_pkthdr.len < off + sizeof(ip6)) 1587 return -1; 1588 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 1589 if (nxtp) 1590 *nxtp = ip6.ip6_nxt; 1591 off += sizeof(ip6); 1592 return off; 1593 1594 case IPPROTO_FRAGMENT: 1595 /* 1596 * terminate parsing if it is not the first fragment, 1597 * it does not make sense to parse through it. 1598 */ 1599 if (m->m_pkthdr.len < off + sizeof(fh)) 1600 return -1; 1601 m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 1602 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */ 1603 if (fh.ip6f_offlg & IP6F_OFF_MASK) 1604 return -1; 1605 if (nxtp) 1606 *nxtp = fh.ip6f_nxt; 1607 off += sizeof(struct ip6_frag); 1608 return off; 1609 1610 case IPPROTO_AH: 1611 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1612 return -1; 1613 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1614 if (nxtp) 1615 *nxtp = ip6e.ip6e_nxt; 1616 off += (ip6e.ip6e_len + 2) << 2; 1617 return off; 1618 1619 case IPPROTO_HOPOPTS: 1620 case IPPROTO_ROUTING: 1621 case IPPROTO_DSTOPTS: 1622 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1623 return -1; 1624 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1625 if (nxtp) 1626 *nxtp = ip6e.ip6e_nxt; 1627 off += (ip6e.ip6e_len + 1) << 3; 1628 return off; 1629 1630 case IPPROTO_NONE: 1631 case IPPROTO_ESP: 1632 case IPPROTO_IPCOMP: 1633 /* give up */ 1634 return -1; 1635 1636 default: 1637 return -1; 1638 } 1639 1640 return -1; 1641 } 1642 1643 /* 1644 * get offset for the last header in the chain. m will be kept untainted. 1645 */ 1646 int 1647 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp) 1648 { 1649 int newoff; 1650 int nxt; 1651 1652 if (!nxtp) { 1653 nxt = -1; 1654 nxtp = &nxt; 1655 } 1656 while (1) { 1657 newoff = ip6_nexthdr(m, off, proto, nxtp); 1658 if (newoff < 0) 1659 return off; 1660 else if (newoff < off) 1661 return -1; /* invalid */ 1662 else if (newoff == off) 1663 return newoff; 1664 1665 off = newoff; 1666 proto = *nxtp; 1667 } 1668 } 1669 1670 struct ip6aux * 1671 ip6_addaux(struct mbuf *m) 1672 { 1673 struct m_tag *mtag; 1674 1675 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1676 if (!mtag) { 1677 mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux), 1678 M_NOWAIT); 1679 if (mtag) { 1680 m_tag_prepend(m, mtag); 1681 bzero(mtag + 1, sizeof(struct ip6aux)); 1682 } 1683 } 1684 return mtag ? (struct ip6aux *)(mtag + 1) : NULL; 1685 } 1686 1687 struct ip6aux * 1688 ip6_findaux(struct mbuf *m) 1689 { 1690 struct m_tag *mtag; 1691 1692 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1693 return mtag ? (struct ip6aux *)(mtag + 1) : NULL; 1694 } 1695 1696 void 1697 ip6_delaux(struct mbuf *m) 1698 { 1699 struct m_tag *mtag; 1700 1701 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1702 if (mtag) 1703 m_tag_delete(m, mtag); 1704 } 1705 1706 /* 1707 * System control for IP6 1708 */ 1709 1710 u_char inet6ctlerrmap[PRC_NCMDS] = { 1711 0, 0, 0, 0, 1712 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1713 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1714 EMSGSIZE, EHOSTUNREACH, 0, 0, 1715 0, 0, 0, 0, 1716 ENOPROTOOPT 1717 }; 1718