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_dependson = VNET_MOD_INET /* XXX revisit - TCP/UDP needs this? */ 165 }; 166 167 static void 168 vnet_inet6_register(void) 169 { 170 171 vnet_mod_register(&vnet_inet6_modinfo); 172 } 173 174 SYSINIT(inet6, SI_SUB_PROTO_BEGIN, SI_ORDER_FIRST, vnet_inet6_register, 0); 175 #endif 176 177 /* 178 * IP6 initialization: fill in IP6 protocol switch table. 179 * All protocols not implemented in kernel go to raw IP6 protocol handler. 180 */ 181 void 182 ip6_init(void) 183 { 184 INIT_VNET_INET6(curvnet); 185 struct ip6protosw *pr; 186 int i; 187 188 V_ip6qmaxlen = IFQ_MAXLEN; 189 V_in6_maxmtu = 0; 190 #ifdef IP6_AUTO_LINKLOCAL 191 V_ip6_auto_linklocal = IP6_AUTO_LINKLOCAL; 192 #else 193 V_ip6_auto_linklocal = 1; /* enable by default */ 194 #endif 195 TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal", 196 &V_ip6_auto_linklocal); 197 198 #ifndef IPV6FORWARDING 199 #ifdef GATEWAY6 200 #define IPV6FORWARDING 1 /* forward IP6 packets not for us */ 201 #else 202 #define IPV6FORWARDING 0 /* don't forward IP6 packets not for us */ 203 #endif /* GATEWAY6 */ 204 #endif /* !IPV6FORWARDING */ 205 206 #ifndef IPV6_SENDREDIRECTS 207 #define IPV6_SENDREDIRECTS 1 208 #endif 209 210 V_ip6_forwarding = IPV6FORWARDING; /* act as router? */ 211 V_ip6_sendredirects = IPV6_SENDREDIRECTS; 212 V_ip6_defhlim = IPV6_DEFHLIM; 213 V_ip6_defmcasthlim = IPV6_DEFAULT_MULTICAST_HOPS; 214 V_ip6_accept_rtadv = 0; /* "IPV6FORWARDING ? 0 : 1" is dangerous */ 215 V_ip6_log_interval = 5; 216 V_ip6_hdrnestlimit = 15; /* How many header options will we process? */ 217 V_ip6_dad_count = 1; /* DupAddrDetectionTransmits */ 218 V_ip6_auto_flowlabel = 1; 219 V_ip6_use_deprecated = 1;/* allow deprecated addr (RFC2462 5.5.4) */ 220 V_ip6_rr_prune = 5; /* router renumbering prefix 221 * walk list every 5 sec. */ 222 V_ip6_mcast_pmtu = 0; /* enable pMTU discovery for multicast? */ 223 V_ip6_v6only = 1; 224 V_ip6_keepfaith = 0; 225 V_ip6_log_time = (time_t)0L; 226 #ifdef IPSTEALTH 227 V_ip6stealth = 0; 228 #endif 229 V_nd6_onlink_ns_rfc4861 = 0; /* allow 'on-link' nd6 NS (RFC 4861) */ 230 231 V_pmtu_expire = 60*10; 232 V_pmtu_probe = 60*2; 233 234 /* raw IP6 parameters */ 235 /* 236 * Nominal space allocated to a raw ip socket. 237 */ 238 #define RIPV6SNDQ 8192 239 #define RIPV6RCVQ 8192 240 V_rip6_sendspace = RIPV6SNDQ; 241 V_rip6_recvspace = RIPV6RCVQ; 242 243 /* ICMPV6 parameters */ 244 V_icmp6_rediraccept = 1; /* accept and process redirects */ 245 V_icmp6_redirtimeout = 10 * 60; /* 10 minutes */ 246 V_icmp6errppslim = 100; /* 100pps */ 247 /* control how to respond to NI queries */ 248 V_icmp6_nodeinfo = (ICMP6_NODEINFO_FQDNOK|ICMP6_NODEINFO_NODEADDROK); 249 250 /* UDP on IP6 parameters */ 251 V_udp6_sendspace = 9216; /* really max datagram size */ 252 V_udp6_recvspace = 40 * (1024 + sizeof(struct sockaddr_in6)); 253 /* 40 1K datagrams */ 254 V_dad_init = 0; 255 256 scope6_init(); 257 addrsel_policy_init(); 258 nd6_init(); 259 frag6_init(); 260 261 V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR; 262 263 /* Skip global initialization stuff for non-default instances. */ 264 if (!IS_DEFAULT_VNET(curvnet)) 265 return; 266 267 #ifdef DIAGNOSTIC 268 if (sizeof(struct protosw) != sizeof(struct ip6protosw)) 269 panic("sizeof(protosw) != sizeof(ip6protosw)"); 270 #endif 271 pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 272 if (pr == 0) 273 panic("ip6_init"); 274 275 /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */ 276 for (i = 0; i < IPPROTO_MAX; i++) 277 ip6_protox[i] = pr - inet6sw; 278 /* 279 * Cycle through IP protocols and put them into the appropriate place 280 * in ip6_protox[]. 281 */ 282 for (pr = (struct ip6protosw *)inet6domain.dom_protosw; 283 pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++) 284 if (pr->pr_domain->dom_family == PF_INET6 && 285 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) { 286 /* Be careful to only index valid IP protocols. */ 287 if (pr->pr_protocol < IPPROTO_MAX) 288 ip6_protox[pr->pr_protocol] = pr - inet6sw; 289 } 290 291 /* Initialize packet filter hooks. */ 292 inet6_pfil_hook.ph_type = PFIL_TYPE_AF; 293 inet6_pfil_hook.ph_af = AF_INET6; 294 if ((i = pfil_head_register(&inet6_pfil_hook)) != 0) 295 printf("%s: WARNING: unable to register pfil hook, " 296 "error %d\n", __func__, i); 297 298 ip6intrq.ifq_maxlen = V_ip6qmaxlen; /* XXX */ 299 mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF); 300 netisr_register(NETISR_IPV6, ip6_input, &ip6intrq, 0); 301 } 302 303 static int 304 ip6_init2_vnet(const void *unused __unused) 305 { 306 INIT_VNET_INET6(curvnet); 307 308 /* nd6_timer_init */ 309 callout_init(&V_nd6_timer_ch, 0); 310 callout_reset(&V_nd6_timer_ch, hz, nd6_timer, NULL); 311 312 /* timer for regeneranation of temporary addresses randomize ID */ 313 callout_init(&V_in6_tmpaddrtimer_ch, 0); 314 callout_reset(&V_in6_tmpaddrtimer_ch, 315 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor - 316 V_ip6_temp_regen_advance) * hz, 317 in6_tmpaddrtimer, NULL); 318 319 return (0); 320 } 321 322 static void 323 ip6_init2(void *dummy) 324 { 325 326 ip6_init2_vnet(NULL); 327 } 328 329 /* cheat */ 330 /* This must be after route_init(), which is now SI_ORDER_THIRD */ 331 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL); 332 333 void 334 ip6_input(struct mbuf *m) 335 { 336 INIT_VNET_NET(curvnet); 337 INIT_VNET_INET6(curvnet); 338 struct ip6_hdr *ip6; 339 int off = sizeof(struct ip6_hdr), nest; 340 u_int32_t plen; 341 u_int32_t rtalert = ~0; 342 int nxt, ours = 0; 343 struct ifnet *deliverifp = NULL, *ifp = NULL; 344 struct in6_addr odst; 345 struct route_in6 rin6; 346 int srcrt = 0; 347 struct llentry *lle = NULL; 348 struct sockaddr_in6 dst6, *dst; 349 350 bzero(&rin6, sizeof(struct route_in6)); 351 #ifdef IPSEC 352 /* 353 * should the inner packet be considered authentic? 354 * see comment in ah4_input(). 355 * NB: m cannot be NULL when passed to the input routine 356 */ 357 358 m->m_flags &= ~M_AUTHIPHDR; 359 m->m_flags &= ~M_AUTHIPDGM; 360 361 #endif /* IPSEC */ 362 363 /* 364 * make sure we don't have onion peering information into m_tag. 365 */ 366 ip6_delaux(m); 367 368 /* 369 * mbuf statistics 370 */ 371 if (m->m_flags & M_EXT) { 372 if (m->m_next) 373 V_ip6stat.ip6s_mext2m++; 374 else 375 V_ip6stat.ip6s_mext1++; 376 } else { 377 #define M2MMAX (sizeof(V_ip6stat.ip6s_m2m)/sizeof(V_ip6stat.ip6s_m2m[0])) 378 if (m->m_next) { 379 if (m->m_flags & M_LOOP) { 380 V_ip6stat.ip6s_m2m[V_loif[0].if_index]++; /* XXX */ 381 } else if (m->m_pkthdr.rcvif->if_index < M2MMAX) 382 V_ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++; 383 else 384 V_ip6stat.ip6s_m2m[0]++; 385 } else 386 V_ip6stat.ip6s_m1++; 387 #undef M2MMAX 388 } 389 390 /* drop the packet if IPv6 operation is disabled on the IF */ 391 if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED)) { 392 m_freem(m); 393 return; 394 } 395 396 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive); 397 V_ip6stat.ip6s_total++; 398 399 #ifndef PULLDOWN_TEST 400 /* 401 * L2 bridge code and some other code can return mbuf chain 402 * that does not conform to KAME requirement. too bad. 403 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 404 */ 405 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 406 struct mbuf *n; 407 408 MGETHDR(n, M_DONTWAIT, MT_HEADER); 409 if (n) 410 M_MOVE_PKTHDR(n, m); 411 if (n && n->m_pkthdr.len > MHLEN) { 412 MCLGET(n, M_DONTWAIT); 413 if ((n->m_flags & M_EXT) == 0) { 414 m_freem(n); 415 n = NULL; 416 } 417 } 418 if (n == NULL) { 419 m_freem(m); 420 return; /* ENOBUFS */ 421 } 422 423 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t)); 424 n->m_len = n->m_pkthdr.len; 425 m_freem(m); 426 m = n; 427 } 428 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */); 429 #endif 430 431 if (m->m_len < sizeof(struct ip6_hdr)) { 432 struct ifnet *inifp; 433 inifp = m->m_pkthdr.rcvif; 434 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 435 V_ip6stat.ip6s_toosmall++; 436 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 437 return; 438 } 439 } 440 441 ip6 = mtod(m, struct ip6_hdr *); 442 443 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 444 V_ip6stat.ip6s_badvers++; 445 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 446 goto bad; 447 } 448 449 V_ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; 450 451 /* 452 * Check against address spoofing/corruption. 453 */ 454 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 455 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 456 /* 457 * XXX: "badscope" is not very suitable for a multicast source. 458 */ 459 V_ip6stat.ip6s_badscope++; 460 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 461 goto bad; 462 } 463 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) && 464 !(m->m_flags & M_LOOP)) { 465 /* 466 * In this case, the packet should come from the loopback 467 * interface. However, we cannot just check the if_flags, 468 * because ip6_mloopback() passes the "actual" interface 469 * as the outgoing/incoming interface. 470 */ 471 V_ip6stat.ip6s_badscope++; 472 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 473 goto bad; 474 } 475 476 #ifdef ALTQ 477 if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) { 478 /* packet is dropped by traffic conditioner */ 479 return; 480 } 481 #endif 482 /* 483 * The following check is not documented in specs. A malicious 484 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack 485 * and bypass security checks (act as if it was from 127.0.0.1 by using 486 * IPv6 src ::ffff:127.0.0.1). Be cautious. 487 * 488 * This check chokes if we are in an SIIT cloud. As none of BSDs 489 * support IPv4-less kernel compilation, we cannot support SIIT 490 * environment at all. So, it makes more sense for us to reject any 491 * malicious packets for non-SIIT environment, than try to do a 492 * partial support for SIIT environment. 493 */ 494 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 495 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 496 V_ip6stat.ip6s_badscope++; 497 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 498 goto bad; 499 } 500 #if 0 501 /* 502 * Reject packets with IPv4 compatible addresses (auto tunnel). 503 * 504 * The code forbids auto tunnel relay case in RFC1933 (the check is 505 * stronger than RFC1933). We may want to re-enable it if mech-xx 506 * is revised to forbid relaying case. 507 */ 508 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) || 509 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) { 510 V_ip6stat.ip6s_badscope++; 511 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 512 goto bad; 513 } 514 #endif 515 516 /* 517 * Run through list of hooks for input packets. 518 * 519 * NB: Beware of the destination address changing 520 * (e.g. by NAT rewriting). When this happens, 521 * tell ip6_forward to do the right thing. 522 */ 523 odst = ip6->ip6_dst; 524 525 /* Jump over all PFIL processing if hooks are not active. */ 526 if (!PFIL_HOOKED(&inet6_pfil_hook)) 527 goto passin; 528 529 if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL)) 530 return; 531 if (m == NULL) /* consumed by filter */ 532 return; 533 ip6 = mtod(m, struct ip6_hdr *); 534 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst); 535 536 passin: 537 /* 538 * Disambiguate address scope zones (if there is ambiguity). 539 * We first make sure that the original source or destination address 540 * is not in our internal form for scoped addresses. Such addresses 541 * are not necessarily invalid spec-wise, but we cannot accept them due 542 * to the usage conflict. 543 * in6_setscope() then also checks and rejects the cases where src or 544 * dst are the loopback address and the receiving interface 545 * is not loopback. 546 */ 547 if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) { 548 V_ip6stat.ip6s_badscope++; /* XXX */ 549 goto bad; 550 } 551 if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) || 552 in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) { 553 V_ip6stat.ip6s_badscope++; 554 goto bad; 555 } 556 557 /* 558 * Multicast check 559 */ 560 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 561 struct in6_multi *in6m = 0; 562 563 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast); 564 /* 565 * See if we belong to the destination multicast group on the 566 * arrival interface. 567 */ 568 IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m); 569 if (in6m) 570 ours = 1; 571 else if (!ip6_mrouter) { 572 V_ip6stat.ip6s_notmember++; 573 V_ip6stat.ip6s_cantforward++; 574 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 575 goto bad; 576 } 577 deliverifp = m->m_pkthdr.rcvif; 578 goto hbhcheck; 579 } 580 581 /* 582 * Unicast check 583 */ 584 585 bzero(&dst6, sizeof(dst6)); 586 dst6.sin6_family = AF_INET6; 587 dst6.sin6_len = sizeof(struct sockaddr_in6); 588 dst6.sin6_addr = ip6->ip6_dst; 589 ifp = m->m_pkthdr.rcvif; 590 IF_AFDATA_LOCK(ifp); 591 lle = lla_lookup(LLTABLE6(ifp), 0, 592 (struct sockaddr *)&dst6); 593 IF_AFDATA_UNLOCK(ifp); 594 if ((lle != NULL) && (lle->la_flags & LLE_IFADDR)) { 595 ours = 1; 596 deliverifp = ifp; 597 LLE_RUNLOCK(lle); 598 goto hbhcheck; 599 } 600 if (lle != NULL) 601 LLE_RUNLOCK(lle); 602 603 dst = &rin6.ro_dst; 604 dst->sin6_len = sizeof(struct sockaddr_in6); 605 dst->sin6_family = AF_INET6; 606 dst->sin6_addr = ip6->ip6_dst; 607 rin6.ro_rt = rtalloc1((struct sockaddr *)dst, 0, 0); 608 if (rin6.ro_rt) 609 RT_UNLOCK(rin6.ro_rt); 610 611 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key)) 612 613 /* 614 * Accept the packet if the forwarding interface to the destination 615 * according to the routing table is the loopback interface, 616 * unless the associated route has a gateway. 617 * Note that this approach causes to accept a packet if there is a 618 * route to the loopback interface for the destination of the packet. 619 * But we think it's even useful in some situations, e.g. when using 620 * a special daemon which wants to intercept the packet. 621 * 622 * XXX: some OSes automatically make a cloned route for the destination 623 * of an outgoing packet. If the outgoing interface of the packet 624 * is a loopback one, the kernel would consider the packet to be 625 * accepted, even if we have no such address assinged on the interface. 626 * We check the cloned flag of the route entry to reject such cases, 627 * assuming that route entries for our own addresses are not made by 628 * cloning (it should be true because in6_addloop explicitly installs 629 * the host route). However, we might have to do an explicit check 630 * while it would be less efficient. Or, should we rather install a 631 * reject route for such a case? 632 */ 633 if (rin6.ro_rt && 634 (rin6.ro_rt->rt_flags & 635 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && 636 #ifdef RTF_WASCLONED 637 !(rin6.ro_rt->rt_flags & RTF_WASCLONED) && 638 #endif 639 #ifdef RTF_CLONED 640 !(rin6.ro_rt->rt_flags & RTF_CLONED) && 641 #endif 642 #if 0 643 /* 644 * The check below is redundant since the comparison of 645 * the destination and the key of the rtentry has 646 * already done through looking up the routing table. 647 */ 648 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 649 &rt6_key(rin6.ro_rt)->sin6_addr) 650 #endif 651 rin6.ro_rt->rt_ifp->if_type == IFT_LOOP) { 652 struct in6_ifaddr *ia6 = 653 (struct in6_ifaddr *)rin6.ro_rt->rt_ifa; 654 655 /* 656 * record address information into m_tag. 657 */ 658 (void)ip6_setdstifaddr(m, ia6); 659 660 /* 661 * packets to a tentative, duplicated, or somehow invalid 662 * address must not be accepted. 663 */ 664 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 665 /* this address is ready */ 666 ours = 1; 667 deliverifp = ia6->ia_ifp; /* correct? */ 668 /* Count the packet in the ip address stats */ 669 ia6->ia_ifa.if_ipackets++; 670 ia6->ia_ifa.if_ibytes += m->m_pkthdr.len; 671 goto hbhcheck; 672 } else { 673 char ip6bufs[INET6_ADDRSTRLEN]; 674 char ip6bufd[INET6_ADDRSTRLEN]; 675 /* address is not ready, so discard the packet. */ 676 nd6log((LOG_INFO, 677 "ip6_input: packet to an unready address %s->%s\n", 678 ip6_sprintf(ip6bufs, &ip6->ip6_src), 679 ip6_sprintf(ip6bufd, &ip6->ip6_dst))); 680 681 goto bad; 682 } 683 } 684 685 /* 686 * FAITH (Firewall Aided Internet Translator) 687 */ 688 if (V_ip6_keepfaith) { 689 if (rin6.ro_rt && rin6.ro_rt->rt_ifp && 690 rin6.ro_rt->rt_ifp->if_type == IFT_FAITH) { 691 /* XXX do we need more sanity checks? */ 692 ours = 1; 693 deliverifp = rin6.ro_rt->rt_ifp; /* faith */ 694 goto hbhcheck; 695 } 696 } 697 698 /* 699 * Now there is no reason to process the packet if it's not our own 700 * and we're not a router. 701 */ 702 if (!V_ip6_forwarding) { 703 V_ip6stat.ip6s_cantforward++; 704 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 705 goto bad; 706 } 707 708 hbhcheck: 709 /* 710 * record address information into m_tag, if we don't have one yet. 711 * note that we are unable to record it, if the address is not listed 712 * as our interface address (e.g. multicast addresses, addresses 713 * within FAITH prefixes and such). 714 */ 715 if (deliverifp && !ip6_getdstifaddr(m)) { 716 struct in6_ifaddr *ia6; 717 718 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); 719 if (ia6) { 720 if (!ip6_setdstifaddr(m, ia6)) { 721 /* 722 * XXX maybe we should drop the packet here, 723 * as we could not provide enough information 724 * to the upper layers. 725 */ 726 } 727 } 728 } 729 730 /* 731 * Process Hop-by-Hop options header if it's contained. 732 * m may be modified in ip6_hopopts_input(). 733 * If a JumboPayload option is included, plen will also be modified. 734 */ 735 plen = (u_int32_t)ntohs(ip6->ip6_plen); 736 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 737 struct ip6_hbh *hbh; 738 739 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) { 740 #if 0 /*touches NULL pointer*/ 741 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 742 #endif 743 goto out; /* m have already been freed */ 744 } 745 746 /* adjust pointer */ 747 ip6 = mtod(m, struct ip6_hdr *); 748 749 /* 750 * if the payload length field is 0 and the next header field 751 * indicates Hop-by-Hop Options header, then a Jumbo Payload 752 * option MUST be included. 753 */ 754 if (ip6->ip6_plen == 0 && plen == 0) { 755 /* 756 * Note that if a valid jumbo payload option is 757 * contained, ip6_hopopts_input() must set a valid 758 * (non-zero) payload length to the variable plen. 759 */ 760 V_ip6stat.ip6s_badoptions++; 761 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 762 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 763 icmp6_error(m, ICMP6_PARAM_PROB, 764 ICMP6_PARAMPROB_HEADER, 765 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); 766 goto out; 767 } 768 #ifndef PULLDOWN_TEST 769 /* ip6_hopopts_input() ensures that mbuf is contiguous */ 770 hbh = (struct ip6_hbh *)(ip6 + 1); 771 #else 772 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 773 sizeof(struct ip6_hbh)); 774 if (hbh == NULL) { 775 V_ip6stat.ip6s_tooshort++; 776 goto out; 777 } 778 #endif 779 nxt = hbh->ip6h_nxt; 780 781 /* 782 * If we are acting as a router and the packet contains a 783 * router alert option, see if we know the option value. 784 * Currently, we only support the option value for MLD, in which 785 * case we should pass the packet to the multicast routing 786 * daemon. 787 */ 788 if (rtalert != ~0 && V_ip6_forwarding) { 789 switch (rtalert) { 790 case IP6OPT_RTALERT_MLD: 791 ours = 1; 792 break; 793 default: 794 /* 795 * RFC2711 requires unrecognized values must be 796 * silently ignored. 797 */ 798 break; 799 } 800 } 801 } else 802 nxt = ip6->ip6_nxt; 803 804 /* 805 * Check that the amount of data in the buffers 806 * is as at least much as the IPv6 header would have us expect. 807 * Trim mbufs if longer than we expect. 808 * Drop packet if shorter than we expect. 809 */ 810 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 811 V_ip6stat.ip6s_tooshort++; 812 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 813 goto bad; 814 } 815 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 816 if (m->m_len == m->m_pkthdr.len) { 817 m->m_len = sizeof(struct ip6_hdr) + plen; 818 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 819 } else 820 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 821 } 822 823 /* 824 * Forward if desirable. 825 */ 826 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 827 /* 828 * If we are acting as a multicast router, all 829 * incoming multicast packets are passed to the 830 * kernel-level multicast forwarding function. 831 * The packet is returned (relatively) intact; if 832 * ip6_mforward() returns a non-zero value, the packet 833 * must be discarded, else it may be accepted below. 834 */ 835 if (ip6_mrouter && ip6_mforward && 836 ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) { 837 V_ip6stat.ip6s_cantforward++; 838 goto bad; 839 } 840 if (!ours) 841 goto bad; 842 } else if (!ours) { 843 ip6_forward(m, srcrt); 844 goto out; 845 } 846 847 ip6 = mtod(m, struct ip6_hdr *); 848 849 /* 850 * Malicious party may be able to use IPv4 mapped addr to confuse 851 * tcp/udp stack and bypass security checks (act as if it was from 852 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 853 * 854 * For SIIT end node behavior, you may want to disable the check. 855 * However, you will become vulnerable to attacks using IPv4 mapped 856 * source. 857 */ 858 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 859 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 860 V_ip6stat.ip6s_badscope++; 861 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 862 goto bad; 863 } 864 865 /* 866 * Tell launch routine the next header 867 */ 868 V_ip6stat.ip6s_delivered++; 869 in6_ifstat_inc(deliverifp, ifs6_in_deliver); 870 nest = 0; 871 872 while (nxt != IPPROTO_DONE) { 873 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) { 874 V_ip6stat.ip6s_toomanyhdr++; 875 goto bad; 876 } 877 878 /* 879 * protection against faulty packet - there should be 880 * more sanity checks in header chain processing. 881 */ 882 if (m->m_pkthdr.len < off) { 883 V_ip6stat.ip6s_tooshort++; 884 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 885 goto bad; 886 } 887 888 #ifdef IPSEC 889 /* 890 * enforce IPsec policy checking if we are seeing last header. 891 * note that we do not visit this with protocols with pcb layer 892 * code - like udp/tcp/raw ip. 893 */ 894 if (ip6_ipsec_input(m, nxt)) 895 goto bad; 896 #endif /* IPSEC */ 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