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