1/* 2 * Copyright (c) 2003-2013 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 29/* 30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 31 * All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 3. Neither the name of the project nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 */ 57 58/* 59 * Copyright (c) 1982, 1986, 1988, 1993 60 * The Regents of the University of California. All rights reserved. 61 * 62 * Redistribution and use in source and binary forms, with or without 63 * modification, are permitted provided that the following conditions 64 * are met: 65 * 1. Redistributions of source code must retain the above copyright 66 * notice, this list of conditions and the following disclaimer. 67 * 2. Redistributions in binary form must reproduce the above copyright 68 * notice, this list of conditions and the following disclaimer in the 69 * documentation and/or other materials provided with the distribution. 70 * 3. All advertising materials mentioning features or use of this software 71 * must display the following acknowledgement: 72 * This product includes software developed by the University of 73 * California, Berkeley and its contributors. 74 * 4. Neither the name of the University nor the names of its contributors 75 * may be used to endorse or promote products derived from this software 76 * without specific prior written permission. 77 * 78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 88 * SUCH DAMAGE. 89 * 90 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 91 */ 92 93#include <sys/param.h> 94#include <sys/systm.h> 95#include <sys/malloc.h> 96#include <sys/mbuf.h> 97#include <sys/domain.h> 98#include <sys/protosw.h> 99#include <sys/socket.h> 100#include <sys/socketvar.h> 101#include <sys/errno.h> 102#include <sys/time.h> 103#include <sys/kernel.h> 104#include <sys/syslog.h> 105#include <sys/sysctl.h> 106#include <sys/proc.h> 107#include <sys/kauth.h> 108#include <sys/mcache.h> 109 110#include <mach/mach_time.h> 111#include <mach/sdt.h> 112#include <pexpert/pexpert.h> 113#include <dev/random/randomdev.h> 114 115#include <net/if.h> 116#include <net/if_var.h> 117#include <net/if_types.h> 118#include <net/if_dl.h> 119#include <net/route.h> 120#include <net/kpi_protocol.h> 121#include <net/ntstat.h> 122#include <net/init.h> 123#include <net/net_osdep.h> 124 125#include <netinet/in.h> 126#include <netinet/in_systm.h> 127#if INET 128#include <netinet/ip.h> 129#include <netinet/ip_icmp.h> 130#endif /* INET */ 131#include <netinet/kpi_ipfilter_var.h> 132#include <netinet/ip6.h> 133#include <netinet6/in6_var.h> 134#include <netinet6/ip6_var.h> 135#include <netinet/in_pcb.h> 136#include <netinet/icmp6.h> 137#include <netinet6/in6_ifattach.h> 138#include <netinet6/nd6.h> 139#include <netinet6/scope6_var.h> 140#include <netinet6/ip6protosw.h> 141 142#if IPSEC 143#include <netinet6/ipsec.h> 144#include <netinet6/ipsec6.h> 145extern int ipsec_bypass; 146#endif /* IPSEC */ 147 148#if IPFW2 149#include <netinet6/ip6_fw.h> 150#endif /* IPFW2 */ 151 152#if DUMMYNET 153#include <netinet/ip_fw.h> 154#include <netinet/ip_dummynet.h> 155#endif /* DUMMYNET */ 156 157/* we need it for NLOOP. */ 158#include "loop.h" 159 160#if PF 161#include <net/pfvar.h> 162#endif /* PF */ 163 164struct ip6protosw *ip6_protox[IPPROTO_MAX]; 165 166static lck_grp_attr_t *in6_ifaddr_rwlock_grp_attr; 167static lck_grp_t *in6_ifaddr_rwlock_grp; 168static lck_attr_t *in6_ifaddr_rwlock_attr; 169decl_lck_rw_data(, in6_ifaddr_rwlock); 170 171/* Protected by in6_ifaddr_rwlock */ 172struct in6_ifaddr *in6_ifaddrs = NULL; 173 174#define IN6_IFSTAT_REQUIRE_ALIGNED_64(f) \ 175 _CASSERT(!(offsetof(struct in6_ifstat, f) % sizeof (uint64_t))) 176 177#define ICMP6_IFSTAT_REQUIRE_ALIGNED_64(f) \ 178 _CASSERT(!(offsetof(struct icmp6_ifstat, f) % sizeof (uint64_t))) 179 180#if IPFW2 181/* firewall hooks */ 182ip6_fw_chk_t *ip6_fw_chk_ptr; 183ip6_fw_ctl_t *ip6_fw_ctl_ptr; 184int ip6_fw_enable = 1; 185#endif /* IPFW2 */ 186 187struct ip6stat ip6stat; 188 189decl_lck_mtx_data(, proxy6_lock); 190decl_lck_mtx_data(static, dad6_mutex_data); 191decl_lck_mtx_data(static, nd6_mutex_data); 192decl_lck_mtx_data(static, prefix6_mutex_data); 193lck_mtx_t *dad6_mutex = &dad6_mutex_data; 194lck_mtx_t *nd6_mutex = &nd6_mutex_data; 195lck_mtx_t *prefix6_mutex = &prefix6_mutex_data; 196#ifdef ENABLE_ADDRSEL 197decl_lck_mtx_data(static, addrsel_mutex_data); 198lck_mtx_t *addrsel_mutex = &addrsel_mutex_data; 199#endif 200static lck_attr_t *ip6_mutex_attr; 201static lck_grp_t *ip6_mutex_grp; 202static lck_grp_attr_t *ip6_mutex_grp_attr; 203 204extern int loopattach_done; 205extern void addrsel_policy_init(void); 206 207static void ip6_init_delayed(void); 208static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *); 209#if PULLDOWN_TEST 210static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int); 211#endif 212 213#if NSTF 214extern void stfattach(void); 215#endif /* NSTF */ 216 217SYSCTL_DECL(_net_inet6_ip6); 218 219int ip6_doscopedroute = 1; 220SYSCTL_INT(_net_inet6_ip6, OID_AUTO, scopedroute, 221 CTLFLAG_RD | CTLFLAG_LOCKED, &ip6_doscopedroute, 0, 222 "Enable IPv6 scoped routing"); 223 224static uint32_t ip6_adj_clear_hwcksum = 0; 225SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, adj_clear_hwcksum, 226 CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_adj_clear_hwcksum, 0, 227 "Invalidate hwcksum info when adjusting length"); 228 229/* 230 * On platforms which require strict alignment (currently for anything but 231 * i386 or x86_64), check if the IP header pointer is 32-bit aligned; if not, 232 * copy the contents of the mbuf chain into a new chain, and free the original 233 * one. Create some head room in the first mbuf of the new chain, in case 234 * it's needed later on. 235 * 236 * RFC 2460 says that IPv6 headers are 64-bit aligned, but network interfaces 237 * mostly align to 32-bit boundaries. Care should be taken never to use 64-bit 238 * load/store operations on the fields in IPv6 headers. 239 */ 240#if defined(__i386__) || defined(__x86_64__) 241#define IP6_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { } while (0) 242#else /* !__i386__ && !__x86_64__ */ 243#define IP6_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { \ 244 if (!IP6_HDR_ALIGNED_P(mtod(_m, caddr_t))) { \ 245 struct mbuf *_n; \ 246 struct ifnet *__ifp = (_ifp); \ 247 atomic_add_64(&(__ifp)->if_alignerrs, 1); \ 248 if (((_m)->m_flags & M_PKTHDR) && \ 249 (_m)->m_pkthdr.pkt_hdr != NULL) \ 250 (_m)->m_pkthdr.pkt_hdr = NULL; \ 251 _n = m_defrag_offset(_m, max_linkhdr, M_NOWAIT); \ 252 if (_n == NULL) { \ 253 ip6stat.ip6s_toosmall++; \ 254 m_freem(_m); \ 255 (_m) = NULL; \ 256 _action; \ 257 } else { \ 258 VERIFY(_n != (_m)); \ 259 (_m) = _n; \ 260 } \ 261 } \ 262} while (0) 263#endif /* !__i386__ && !__x86_64__ */ 264 265static void 266ip6_proto_input(protocol_family_t protocol, mbuf_t packet) 267{ 268#pragma unused(protocol) 269 ip6_input(packet); 270} 271 272/* 273 * IP6 initialization: fill in IP6 protocol switch table. 274 * All protocols not implemented in kernel go to raw IP6 protocol handler. 275 */ 276void 277ip6_init(struct ip6protosw *pp, struct domain *dp) 278{ 279 static int ip6_initialized = 0; 280 struct protosw *pr; 281 struct timeval tv; 282 int i; 283 domain_unguard_t unguard; 284 285 domain_proto_mtx_lock_assert_held(); 286 VERIFY((pp->pr_flags & (PR_INITIALIZED|PR_ATTACHED)) == PR_ATTACHED); 287 288 _CASSERT((sizeof (struct ip6_hdr) + 289 sizeof (struct icmp6_hdr)) <= _MHLEN); 290 291 if (ip6_initialized) 292 return; 293 ip6_initialized = 1; 294 295 PE_parse_boot_argn("net.inet6.ip6.scopedroute", &ip6_doscopedroute, 296 sizeof (ip6_doscopedroute)); 297 298 pr = pffindproto_locked(PF_INET6, IPPROTO_RAW, SOCK_RAW); 299 if (pr == NULL) { 300 panic("%s: Unable to find [PF_INET6,IPPROTO_RAW,SOCK_RAW]\n", 301 __func__); 302 /* NOTREACHED */ 303 } 304 305 /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */ 306 for (i = 0; i < IPPROTO_MAX; i++) 307 ip6_protox[i] = (struct ip6protosw *)pr; 308 /* 309 * Cycle through IP protocols and put them into the appropriate place 310 * in ip6_protox[], skipping protocols IPPROTO_{IP,RAW}. 311 */ 312 VERIFY(dp == inet6domain && dp->dom_family == PF_INET6); 313 TAILQ_FOREACH(pr, &dp->dom_protosw, pr_entry) { 314 VERIFY(pr->pr_domain == dp); 315 if (pr->pr_protocol != 0 && pr->pr_protocol != IPPROTO_RAW) { 316 /* Be careful to only index valid IP protocols. */ 317 if (pr->pr_protocol < IPPROTO_MAX) 318 ip6_protox[pr->pr_protocol] = 319 (struct ip6protosw *)pr; 320 } 321 } 322 323 ip6_mutex_grp_attr = lck_grp_attr_alloc_init(); 324 325 ip6_mutex_grp = lck_grp_alloc_init("ip6", ip6_mutex_grp_attr); 326 ip6_mutex_attr = lck_attr_alloc_init(); 327 328 lck_mtx_init(dad6_mutex, ip6_mutex_grp, ip6_mutex_attr); 329 lck_mtx_init(nd6_mutex, ip6_mutex_grp, ip6_mutex_attr); 330 lck_mtx_init(prefix6_mutex, ip6_mutex_grp, ip6_mutex_attr); 331 scope6_init(ip6_mutex_grp, ip6_mutex_attr); 332 333#ifdef ENABLE_ADDRSEL 334 lck_mtx_init(addrsel_mutex, ip6_mutex_grp, ip6_mutex_attr); 335#endif 336 337 lck_mtx_init(&proxy6_lock, ip6_mutex_grp, ip6_mutex_attr); 338 339 in6_ifaddr_rwlock_grp_attr = lck_grp_attr_alloc_init(); 340 in6_ifaddr_rwlock_grp = lck_grp_alloc_init("in6_ifaddr_rwlock", 341 in6_ifaddr_rwlock_grp_attr); 342 in6_ifaddr_rwlock_attr = lck_attr_alloc_init(); 343 lck_rw_init(&in6_ifaddr_rwlock, in6_ifaddr_rwlock_grp, 344 in6_ifaddr_rwlock_attr); 345 346 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_receive); 347 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_hdrerr); 348 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_toobig); 349 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_noroute); 350 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_addrerr); 351 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_protounknown); 352 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_truncated); 353 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_discard); 354 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_deliver); 355 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_forward); 356 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_request); 357 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_discard); 358 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragok); 359 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragfail); 360 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragcreat); 361 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_reqd); 362 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_ok); 363 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_fail); 364 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mcast); 365 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mcast); 366 367 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_msg); 368 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_error); 369 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_dstunreach); 370 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_adminprohib); 371 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_timeexceed); 372 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_paramprob); 373 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_pkttoobig); 374 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echo); 375 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echoreply); 376 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routersolicit); 377 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routeradvert); 378 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighborsolicit); 379 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighboradvert); 380 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_redirect); 381 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldquery); 382 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldreport); 383 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mlddone); 384 385 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_msg); 386 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_error); 387 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_dstunreach); 388 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_adminprohib); 389 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_timeexceed); 390 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_paramprob); 391 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_pkttoobig); 392 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echo); 393 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echoreply); 394 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routersolicit); 395 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routeradvert); 396 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighborsolicit); 397 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighboradvert); 398 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_redirect); 399 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldquery); 400 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldreport); 401 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mlddone); 402 403 getmicrotime(&tv); 404 ip6_desync_factor = 405 (RandomULong() ^ tv.tv_usec) % MAX_TEMP_DESYNC_FACTOR; 406 407 in6_ifaddr_init(); 408 ip6_moptions_init(); 409 nd6_init(); 410 frag6_init(); 411 icmp6_init(NULL, dp); 412 addrsel_policy_init(); 413 414 /* 415 * P2P interfaces often route the local address to the loopback 416 * interface. At this point, lo0 hasn't been initialized yet, which 417 * means that we need to delay the IPv6 configuration of lo0. 418 */ 419 net_init_add(ip6_init_delayed); 420 421 unguard = domain_unguard_deploy(); 422 i = proto_register_input(PF_INET6, ip6_proto_input, NULL, 0); 423 if (i != 0) { 424 panic("%s: failed to register PF_INET6 protocol: %d\n", 425 __func__, i); 426 /* NOTREACHED */ 427 } 428 domain_unguard_release(unguard); 429} 430 431static void 432ip6_init_delayed(void) 433{ 434 (void) in6_ifattach_prelim(lo_ifp); 435 436 /* timer for regeneranation of temporary addresses randomize ID */ 437 timeout(in6_tmpaddrtimer, NULL, 438 (ip6_temp_preferred_lifetime - ip6_desync_factor - 439 ip6_temp_regen_advance) * hz); 440 441#if NSTF 442 stfattach(); 443#endif /* NSTF */ 444} 445 446void 447ip6_input(struct mbuf *m) 448{ 449 struct ip6_hdr *ip6; 450 int off = sizeof (struct ip6_hdr), nest; 451 u_int32_t plen; 452 u_int32_t rtalert = ~0; 453 int nxt = 0, ours = 0; 454 struct ifnet *inifp, *deliverifp = NULL; 455 ipfilter_t inject_ipfref = NULL; 456 int seen; 457 struct in6_ifaddr *ia6 = NULL; 458 struct sockaddr_in6 *dst6; 459#if DUMMYNET 460 struct m_tag *tag; 461#endif /* DUMMYNET */ 462 struct { 463 struct route_in6 rin6; 464#if DUMMYNET 465 struct ip_fw_args args; 466#endif /* DUMMYNET */ 467 } ip6ibz; 468#define rin6 ip6ibz.rin6 469#define args ip6ibz.args 470 471 /* zero out {rin6, args} */ 472 bzero(&ip6ibz, sizeof (ip6ibz)); 473 474 /* 475 * Check if the packet we received is valid after interface filter 476 * processing 477 */ 478 MBUF_INPUT_CHECK(m, m->m_pkthdr.rcvif); 479 inifp = m->m_pkthdr.rcvif; 480 VERIFY(inifp != NULL); 481 482 /* Perform IP header alignment fixup, if needed */ 483 IP6_HDR_ALIGNMENT_FIXUP(m, inifp, return); 484 485 m->m_pkthdr.pkt_flags &= ~PKTF_FORWARDED; 486#if IPSEC 487 /* 488 * should the inner packet be considered authentic? 489 * see comment in ah4_input(). 490 */ 491 m->m_flags &= ~M_AUTHIPHDR; 492 m->m_flags &= ~M_AUTHIPDGM; 493#endif /* IPSEC */ 494 495 /* 496 * make sure we don't have onion peering information into m_aux. 497 */ 498 ip6_delaux(m); 499 500#if DUMMYNET 501 if ((tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, 502 KERNEL_TAG_TYPE_DUMMYNET, NULL)) != NULL) { 503 struct dn_pkt_tag *dn_tag; 504 505 dn_tag = (struct dn_pkt_tag *)(tag+1); 506 507 args.fwa_pf_rule = dn_tag->dn_pf_rule; 508 509 m_tag_delete(m, tag); 510 } 511 512 if (args.fwa_pf_rule) { 513 ip6 = mtod(m, struct ip6_hdr *); /* In case PF got disabled */ 514 515 goto check_with_pf; 516 } 517#endif /* DUMMYNET */ 518 519 /* 520 * No need to proccess packet twice if we've already seen it. 521 */ 522 inject_ipfref = ipf_get_inject_filter(m); 523 if (inject_ipfref != NULL) { 524 ip6 = mtod(m, struct ip6_hdr *); 525 nxt = ip6->ip6_nxt; 526 seen = 0; 527 goto injectit; 528 } else { 529 seen = 1; 530 } 531 532 /* 533 * mbuf statistics 534 */ 535 if (m->m_flags & M_EXT) { 536 if (m->m_next != NULL) 537 ip6stat.ip6s_mext2m++; 538 else 539 ip6stat.ip6s_mext1++; 540 } else { 541#define M2MMAX (sizeof (ip6stat.ip6s_m2m) / sizeof (ip6stat.ip6s_m2m[0])) 542 if (m->m_next != NULL) { 543 if (m->m_pkthdr.pkt_flags & PKTF_LOOP) { 544 /* XXX */ 545 ip6stat.ip6s_m2m[ifnet_index(lo_ifp)]++; 546 } else if (inifp->if_index < M2MMAX) { 547 ip6stat.ip6s_m2m[inifp->if_index]++; 548 } else { 549 ip6stat.ip6s_m2m[0]++; 550 } 551 } else { 552 ip6stat.ip6s_m1++; 553 } 554#undef M2MMAX 555 } 556 557 /* 558 * Drop the packet if IPv6 operation is disabled on the interface. 559 */ 560 if (inifp->if_eflags & IFEF_IPV6_DISABLED) 561 goto bad; 562 563 in6_ifstat_inc_na(inifp, ifs6_in_receive); 564 ip6stat.ip6s_total++; 565 566#ifndef PULLDOWN_TEST 567 /* 568 * L2 bridge code and some other code can return mbuf chain 569 * that does not conform to KAME requirement. too bad. 570 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 571 */ 572 if (m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 573 struct mbuf *n; 574 575 MGETHDR(n, M_DONTWAIT, MT_HEADER); /* MAC-OK */ 576 if (n) 577 M_COPY_PKTHDR(n, m); 578 if (n && m->m_pkthdr.len > MHLEN) { 579 MCLGET(n, M_DONTWAIT); 580 if ((n->m_flags & M_EXT) == 0) { 581 m_freem(n); 582 n = NULL; 583 } 584 } 585 if (n == NULL) 586 goto bad; 587 588 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t)); 589 n->m_len = m->m_pkthdr.len; 590 m_freem(m); 591 m = n; 592 } 593 IP6_EXTHDR_CHECK(m, 0, sizeof (struct ip6_hdr), { goto done; }); 594#endif 595 596 if (m->m_len < sizeof (struct ip6_hdr)) { 597 if ((m = m_pullup(m, sizeof (struct ip6_hdr))) == 0) { 598 ip6stat.ip6s_toosmall++; 599 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 600 goto done; 601 } 602 } 603 604 ip6 = mtod(m, struct ip6_hdr *); 605 606 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 607 ip6stat.ip6s_badvers++; 608 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 609 goto bad; 610 } 611 612 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; 613 614#if IPFW2 615 /* 616 * Check with the firewall... 617 */ 618 if (ip6_fw_enable && ip6_fw_chk_ptr) { 619 u_short port = 0; 620 /* If ipfw says divert, we have to just drop packet */ 621 /* use port as a dummy argument */ 622 if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) { 623 m_freem(m); 624 m = NULL; 625 } 626 if (!m) 627 goto done; 628 } 629#endif /* IPFW2 */ 630 631 /* 632 * Check against address spoofing/corruption. 633 */ 634 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 635 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 636 /* 637 * XXX: "badscope" is not very suitable for a multicast source. 638 */ 639 ip6stat.ip6s_badscope++; 640 in6_ifstat_inc(inifp, ifs6_in_addrerr); 641 goto bad; 642 } 643 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) && 644 !(m->m_pkthdr.pkt_flags & PKTF_LOOP)) { 645 /* 646 * In this case, the packet should come from the loopback 647 * interface. However, we cannot just check the if_flags, 648 * because ip6_mloopback() passes the "actual" interface 649 * as the outgoing/incoming interface. 650 */ 651 ip6stat.ip6s_badscope++; 652 in6_ifstat_inc(inifp, ifs6_in_addrerr); 653 goto bad; 654 } 655 656 /* 657 * The following check is not documented in specs. A malicious 658 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack 659 * and bypass security checks (act as if it was from 127.0.0.1 by using 660 * IPv6 src ::ffff:127.0.0.1). Be cautious. 661 * 662 * This check chokes if we are in an SIIT cloud. As none of BSDs 663 * support IPv4-less kernel compilation, we cannot support SIIT 664 * environment at all. So, it makes more sense for us to reject any 665 * malicious packets for non-SIIT environment, than try to do a 666 * partial support for SIIT environment. 667 */ 668 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 669 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 670 ip6stat.ip6s_badscope++; 671 in6_ifstat_inc(inifp, ifs6_in_addrerr); 672 goto bad; 673 } 674#if 0 675 /* 676 * Reject packets with IPv4 compatible addresses (auto tunnel). 677 * 678 * The code forbids auto tunnel relay case in RFC1933 (the check is 679 * stronger than RFC1933). We may want to re-enable it if mech-xx 680 * is revised to forbid relaying case. 681 */ 682 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) || 683 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) { 684 ip6stat.ip6s_badscope++; 685 in6_ifstat_inc(inifp, ifs6_in_addrerr); 686 goto bad; 687 } 688#endif 689 690 /* 691 * Naively assume we can attribute inbound data to the route we would 692 * use to send to this destination. Asymetric routing breaks this 693 * assumption, but it still allows us to account for traffic from 694 * a remote node in the routing table. 695 * this has a very significant performance impact so we bypass 696 * if nstat_collect is disabled. We may also bypass if the 697 * protocol is tcp in the future because tcp will have a route that 698 * we can use to attribute the data to. That does mean we would not 699 * account for forwarded tcp traffic. 700 */ 701 if (nstat_collect) { 702 struct rtentry *rte = 703 ifnet_cached_rtlookup_inet6(inifp, &ip6->ip6_src); 704 if (rte != NULL) { 705 nstat_route_rx(rte, 1, m->m_pkthdr.len, 0); 706 rtfree(rte); 707 } 708 } 709 710 /* for consistency */ 711 m->m_pkthdr.pkt_proto = ip6->ip6_nxt; 712 713#if DUMMYNET 714check_with_pf: 715#endif /* DUMMYNET */ 716#if PF 717 /* Invoke inbound packet filter */ 718 if (PF_IS_ENABLED) { 719 int error; 720#if DUMMYNET 721 error = pf_af_hook(inifp, NULL, &m, AF_INET6, TRUE, &args); 722#else /* !DUMMYNET */ 723 error = pf_af_hook(inifp, NULL, &m, AF_INET6, TRUE, NULL); 724#endif /* !DUMMYNET */ 725 if (error != 0 || m == NULL) { 726 if (m != NULL) { 727 panic("%s: unexpected packet %p\n", 728 __func__, m); 729 /* NOTREACHED */ 730 } 731 /* Already freed by callee */ 732 goto done; 733 } 734 ip6 = mtod(m, struct ip6_hdr *); 735 } 736#endif /* PF */ 737 738 /* drop packets if interface ID portion is already filled */ 739 if (!(inifp->if_flags & IFF_LOOPBACK) && 740 !(m->m_pkthdr.pkt_flags & PKTF_LOOP)) { 741 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) && 742 ip6->ip6_src.s6_addr16[1]) { 743 ip6stat.ip6s_badscope++; 744 goto bad; 745 } 746 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) && 747 ip6->ip6_dst.s6_addr16[1]) { 748 ip6stat.ip6s_badscope++; 749 goto bad; 750 } 751 } 752 753 if (m->m_pkthdr.pkt_flags & PKTF_IFAINFO) { 754 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 755 ip6->ip6_src.s6_addr16[1] = 756 htons(m->m_pkthdr.src_ifindex); 757 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 758 ip6->ip6_dst.s6_addr16[1] = 759 htons(m->m_pkthdr.dst_ifindex); 760 } else { 761 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 762 ip6->ip6_src.s6_addr16[1] = htons(inifp->if_index); 763 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 764 ip6->ip6_dst.s6_addr16[1] = htons(inifp->if_index); 765 } 766 767 /* 768 * Multicast check 769 */ 770 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 771 struct in6_multi *in6m = NULL; 772 773 in6_ifstat_inc_na(inifp, ifs6_in_mcast); 774 /* 775 * See if we belong to the destination multicast group on the 776 * arrival interface. 777 */ 778 in6_multihead_lock_shared(); 779 IN6_LOOKUP_MULTI(&ip6->ip6_dst, inifp, in6m); 780 in6_multihead_lock_done(); 781 if (in6m != NULL) { 782 IN6M_REMREF(in6m); 783 ours = 1; 784 } else if (!nd6_prproxy 785#if MROUTING 786 && !ip6_mrouter 787#endif /* MROUTING */ 788 ) { 789 ip6stat.ip6s_notmember++; 790 ip6stat.ip6s_cantforward++; 791 in6_ifstat_inc(inifp, ifs6_in_discard); 792 goto bad; 793 } 794 deliverifp = inifp; 795 VERIFY(ia6 == NULL); 796 goto hbhcheck; 797 } 798 799 /* 800 * Unicast check 801 * 802 * Fast path: see if the target is ourselves. 803 */ 804 lck_rw_lock_shared(&in6_ifaddr_rwlock); 805 for (ia6 = in6_ifaddrs; ia6 != NULL; ia6 = ia6->ia_next) { 806 /* 807 * No reference is held on the address, as we just need 808 * to test for a few things while holding the RW lock. 809 */ 810 if (IN6_ARE_ADDR_EQUAL(&ia6->ia_addr.sin6_addr, &ip6->ip6_dst)) 811 break; 812 } 813 814 if (ia6 != NULL) { 815 /* 816 * For performance, test without acquiring the address lock; 817 * a lot of things in the address are set once and never 818 * changed (e.g. ia_ifp.) 819 */ 820 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 821 /* this address is ready */ 822 ours = 1; 823 deliverifp = ia6->ia_ifp; 824 /* 825 * record dst address information into mbuf. 826 */ 827 (void) ip6_setdstifaddr_info(m, 0, ia6); 828 lck_rw_done(&in6_ifaddr_rwlock); 829 goto hbhcheck; 830 } 831 lck_rw_done(&in6_ifaddr_rwlock); 832 ia6 = NULL; 833 /* address is not ready, so discard the packet. */ 834 nd6log((LOG_INFO, "%s: packet to an unready address %s->%s\n", 835 __func__, ip6_sprintf(&ip6->ip6_src), 836 ip6_sprintf(&ip6->ip6_dst))); 837 goto bad; 838 } 839 lck_rw_done(&in6_ifaddr_rwlock); 840 841 /* 842 * Slow path: route lookup. 843 */ 844 dst6 = SIN6(&rin6.ro_dst); 845 dst6->sin6_len = sizeof (struct sockaddr_in6); 846 dst6->sin6_family = AF_INET6; 847 dst6->sin6_addr = ip6->ip6_dst; 848 849 rtalloc_scoped_ign((struct route *)&rin6, 850 RTF_PRCLONING, IFSCOPE_NONE); 851 if (rin6.ro_rt != NULL) 852 RT_LOCK_SPIN(rin6.ro_rt); 853 854#define rt6_key(r) (SIN6((r)->rt_nodes->rn_key)) 855 856 /* 857 * Accept the packet if the forwarding interface to the destination 858 * according to the routing table is the loopback interface, 859 * unless the associated route has a gateway. 860 * Note that this approach causes to accept a packet if there is a 861 * route to the loopback interface for the destination of the packet. 862 * But we think it's even useful in some situations, e.g. when using 863 * a special daemon which wants to intercept the packet. 864 * 865 * XXX: some OSes automatically make a cloned route for the destination 866 * of an outgoing packet. If the outgoing interface of the packet 867 * is a loopback one, the kernel would consider the packet to be 868 * accepted, even if we have no such address assinged on the interface. 869 * We check the cloned flag of the route entry to reject such cases, 870 * assuming that route entries for our own addresses are not made by 871 * cloning (it should be true because in6_addloop explicitly installs 872 * the host route). However, we might have to do an explicit check 873 * while it would be less efficient. Or, should we rather install a 874 * reject route for such a case? 875 */ 876 if (rin6.ro_rt != NULL && 877 (rin6.ro_rt->rt_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && 878#if RTF_WASCLONED 879 !(rin6.ro_rt->rt_flags & RTF_WASCLONED) && 880#endif 881 rin6.ro_rt->rt_ifp->if_type == IFT_LOOP) { 882 ia6 = (struct in6_ifaddr *)rin6.ro_rt->rt_ifa; 883 /* 884 * Packets to a tentative, duplicated, or somehow invalid 885 * address must not be accepted. 886 * 887 * For performance, test without acquiring the address lock; 888 * a lot of things in the address are set once and never 889 * changed (e.g. ia_ifp.) 890 */ 891 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 892 /* this address is ready */ 893 ours = 1; 894 deliverifp = ia6->ia_ifp; /* correct? */ 895 /* 896 * record dst address information into mbuf. 897 */ 898 (void) ip6_setdstifaddr_info(m, 0, ia6); 899 RT_UNLOCK(rin6.ro_rt); 900 goto hbhcheck; 901 } 902 RT_UNLOCK(rin6.ro_rt); 903 ia6 = NULL; 904 /* address is not ready, so discard the packet. */ 905 nd6log((LOG_INFO, "%s: packet to an unready address %s->%s\n", 906 __func__, ip6_sprintf(&ip6->ip6_src), 907 ip6_sprintf(&ip6->ip6_dst))); 908 goto bad; 909 } 910 911 if (rin6.ro_rt != NULL) 912 RT_UNLOCK(rin6.ro_rt); 913 914 /* 915 * Now there is no reason to process the packet if it's not our own 916 * and we're not a router. 917 */ 918 if (!ip6_forwarding) { 919 ip6stat.ip6s_cantforward++; 920 in6_ifstat_inc(inifp, ifs6_in_discard); 921 goto bad; 922 } 923 924hbhcheck: 925 /* 926 * record dst address information into mbuf, if we don't have one yet. 927 * note that we are unable to record it, if the address is not listed 928 * as our interface address (e.g. multicast addresses, etc.) 929 */ 930 if (deliverifp != NULL && ia6 == NULL) { 931 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); 932 if (ia6 != NULL) { 933 (void) ip6_setdstifaddr_info(m, 0, ia6); 934 IFA_REMREF(&ia6->ia_ifa); 935 } 936 } 937 938 /* 939 * Process Hop-by-Hop options header if it's contained. 940 * m may be modified in ip6_hopopts_input(). 941 * If a JumboPayload option is included, plen will also be modified. 942 */ 943 plen = (u_int32_t)ntohs(ip6->ip6_plen); 944 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 945 struct ip6_hbh *hbh; 946 947 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) { 948#if 0 /* touches NULL pointer */ 949 in6_ifstat_inc(inifp, ifs6_in_discard); 950#endif 951 goto done; /* m have already been freed */ 952 } 953 954 /* adjust pointer */ 955 ip6 = mtod(m, struct ip6_hdr *); 956 957 /* 958 * if the payload length field is 0 and the next header field 959 * indicates Hop-by-Hop Options header, then a Jumbo Payload 960 * option MUST be included. 961 */ 962 if (ip6->ip6_plen == 0 && plen == 0) { 963 /* 964 * Note that if a valid jumbo payload option is 965 * contained, ip6_hopopts_input() must set a valid 966 * (non-zero) payload length to the variable plen. 967 */ 968 ip6stat.ip6s_badoptions++; 969 in6_ifstat_inc(inifp, ifs6_in_discard); 970 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 971 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 972 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); 973 goto done; 974 } 975#ifndef PULLDOWN_TEST 976 /* ip6_hopopts_input() ensures that mbuf is contiguous */ 977 hbh = (struct ip6_hbh *)(ip6 + 1); 978#else 979 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, 980 sizeof (struct ip6_hdr), sizeof (struct ip6_hbh)); 981 if (hbh == NULL) { 982 ip6stat.ip6s_tooshort++; 983 goto done; 984 } 985#endif 986 nxt = hbh->ip6h_nxt; 987 988 /* 989 * If we are acting as a router and the packet contains a 990 * router alert option, see if we know the option value. 991 * Currently, we only support the option value for MLD, in which 992 * case we should pass the packet to the multicast routing 993 * daemon. 994 */ 995 if (rtalert != ~0 && ip6_forwarding) { 996 switch (rtalert) { 997 case IP6OPT_RTALERT_MLD: 998 ours = 1; 999 break; 1000 default: 1001 /* 1002 * RFC2711 requires unrecognized values must be 1003 * silently ignored. 1004 */ 1005 break; 1006 } 1007 } 1008 } else 1009 nxt = ip6->ip6_nxt; 1010 1011 /* 1012 * Check that the amount of data in the buffers 1013 * is as at least much as the IPv6 header would have us expect. 1014 * Trim mbufs if longer than we expect. 1015 * Drop packet if shorter than we expect. 1016 */ 1017 if (m->m_pkthdr.len - sizeof (struct ip6_hdr) < plen) { 1018 ip6stat.ip6s_tooshort++; 1019 in6_ifstat_inc(inifp, ifs6_in_truncated); 1020 goto bad; 1021 } 1022 if (m->m_pkthdr.len > sizeof (struct ip6_hdr) + plen) { 1023 /* 1024 * Invalidate hardware checksum info if ip6_adj_clear_hwcksum 1025 * is set; useful to handle buggy drivers. Note that this 1026 * should not be enabled by default, as we may get here due 1027 * to link-layer padding. 1028 */ 1029 if (ip6_adj_clear_hwcksum && 1030 (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) && 1031 !(inifp->if_flags & IFF_LOOPBACK) && 1032 !(m->m_pkthdr.pkt_flags & PKTF_LOOP)) { 1033 m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID; 1034 m->m_pkthdr.csum_data = 0; 1035 ip6stat.ip6s_adj_hwcsum_clr++; 1036 } 1037 1038 ip6stat.ip6s_adj++; 1039 if (m->m_len == m->m_pkthdr.len) { 1040 m->m_len = sizeof (struct ip6_hdr) + plen; 1041 m->m_pkthdr.len = sizeof (struct ip6_hdr) + plen; 1042 } else { 1043 m_adj(m, sizeof (struct ip6_hdr) + plen - 1044 m->m_pkthdr.len); 1045 } 1046 } 1047 1048 /* 1049 * Forward if desirable. 1050 */ 1051 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1052 /* 1053 * If we are acting as a multicast router, all 1054 * incoming multicast packets are passed to the 1055 * kernel-level multicast forwarding function. 1056 * The packet is returned (relatively) intact; if 1057 * ip6_mforward() returns a non-zero value, the packet 1058 * must be discarded, else it may be accepted below. 1059 */ 1060#if MROUTING 1061 if (ip6_mrouter && ip6_mforward(ip6, inifp, m)) { 1062 ip6stat.ip6s_cantforward++; 1063 goto bad; 1064 } 1065#endif /* MROUTING */ 1066 if (!ours && nd6_prproxy) { 1067 /* 1068 * If this isn't for us, this might be a Neighbor 1069 * Solicitation (dst is solicited-node multicast) 1070 * against an address in one of the proxied prefixes; 1071 * if so, claim the packet and let icmp6_input() 1072 * handle the rest. 1073 */ 1074 ours = nd6_prproxy_isours(m, ip6, NULL, IFSCOPE_NONE); 1075 VERIFY(!ours || 1076 (m->m_pkthdr.pkt_flags & PKTF_PROXY_DST)); 1077 } 1078 if (!ours) 1079 goto bad; 1080 } else if (!ours) { 1081 /* 1082 * The unicast forwarding function might return the packet 1083 * if we are proxying prefix(es), and if the packet is an 1084 * ICMPv6 packet that has failed the zone checks, but is 1085 * targetted towards a proxied address (this is optimized by 1086 * way of RTF_PROXY test.) If so, claim the packet as ours 1087 * and let icmp6_input() handle the rest. The packet's hop 1088 * limit value is kept intact (it's not decremented). This 1089 * is for supporting Neighbor Unreachability Detection between 1090 * proxied nodes on different links (src is link-local, dst 1091 * is target address.) 1092 */ 1093 if ((m = ip6_forward(m, &rin6, 0)) == NULL) 1094 goto done; 1095 VERIFY(rin6.ro_rt != NULL); 1096 VERIFY(m->m_pkthdr.pkt_flags & PKTF_PROXY_DST); 1097 deliverifp = rin6.ro_rt->rt_ifp; 1098 ours = 1; 1099 } 1100 1101 ip6 = mtod(m, struct ip6_hdr *); 1102 1103 /* 1104 * Malicious party may be able to use IPv4 mapped addr to confuse 1105 * tcp/udp stack and bypass security checks (act as if it was from 1106 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 1107 * 1108 * For SIIT end node behavior, you may want to disable the check. 1109 * However, you will become vulnerable to attacks using IPv4 mapped 1110 * source. 1111 */ 1112 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 1113 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 1114 ip6stat.ip6s_badscope++; 1115 in6_ifstat_inc(inifp, ifs6_in_addrerr); 1116 goto bad; 1117 } 1118 1119 /* 1120 * Tell launch routine the next header 1121 */ 1122 ip6stat.ip6s_delivered++; 1123 in6_ifstat_inc_na(deliverifp, ifs6_in_deliver); 1124 1125injectit: 1126 nest = 0; 1127 1128 /* 1129 * Perform IP header alignment fixup again, if needed. Note that 1130 * we do it once for the outermost protocol, and we assume each 1131 * protocol handler wouldn't mess with the alignment afterwards. 1132 */ 1133 IP6_HDR_ALIGNMENT_FIXUP(m, inifp, return); 1134 1135 while (nxt != IPPROTO_DONE) { 1136 struct ipfilter *filter; 1137 int (*pr_input)(struct mbuf **, int *, int); 1138 1139 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { 1140 ip6stat.ip6s_toomanyhdr++; 1141 goto bad; 1142 } 1143 1144 /* 1145 * protection against faulty packet - there should be 1146 * more sanity checks in header chain processing. 1147 */ 1148 if (m->m_pkthdr.len < off) { 1149 ip6stat.ip6s_tooshort++; 1150 in6_ifstat_inc(inifp, ifs6_in_truncated); 1151 goto bad; 1152 } 1153 1154 1155#if IPSEC 1156 /* 1157 * enforce IPsec policy checking if we are seeing last header. 1158 * note that we do not visit this with protocols with pcb layer 1159 * code - like udp/tcp/raw ip. 1160 */ 1161 if ((ipsec_bypass == 0) && 1162 (ip6_protox[nxt]->pr_flags & PR_LASTHDR) != 0) { 1163 if (ipsec6_in_reject(m, NULL)) { 1164 IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio); 1165 goto bad; 1166 } 1167 } 1168#endif /* IPSEC */ 1169 1170 /* 1171 * Call IP filter 1172 */ 1173 if (!TAILQ_EMPTY(&ipv6_filters)) { 1174 ipf_ref(); 1175 TAILQ_FOREACH(filter, &ipv6_filters, ipf_link) { 1176 if (seen == 0) { 1177 if ((struct ipfilter *)inject_ipfref == 1178 filter) 1179 seen = 1; 1180 } else if (filter->ipf_filter.ipf_input) { 1181 errno_t result; 1182 1183 result = filter->ipf_filter.ipf_input( 1184 filter->ipf_filter.cookie, 1185 (mbuf_t *)&m, off, nxt); 1186 if (result == EJUSTRETURN) { 1187 ipf_unref(); 1188 goto done; 1189 } 1190 if (result != 0) { 1191 ipf_unref(); 1192 goto bad; 1193 } 1194 } 1195 } 1196 ipf_unref(); 1197 } 1198 1199 DTRACE_IP6(receive, struct mbuf *, m, struct inpcb *, NULL, 1200 struct ip6_hdr *, ip6, struct ifnet *, inifp, 1201 struct ip *, NULL, struct ip6_hdr *, ip6); 1202 1203 if ((pr_input = ip6_protox[nxt]->pr_input) == NULL) { 1204 m_freem(m); 1205 m = NULL; 1206 nxt = IPPROTO_DONE; 1207 } else if (!(ip6_protox[nxt]->pr_flags & PR_PROTOLOCK)) { 1208 lck_mtx_lock(inet6_domain_mutex); 1209 nxt = pr_input(&m, &off, nxt); 1210 lck_mtx_unlock(inet6_domain_mutex); 1211 } else { 1212 nxt = pr_input(&m, &off, nxt); 1213 } 1214 } 1215done: 1216 ROUTE_RELEASE(&rin6); 1217 return; 1218bad: 1219 m_freem(m); 1220 goto done; 1221} 1222 1223void 1224ip6_setsrcifaddr_info(struct mbuf *m, uint32_t src_idx, struct in6_ifaddr *ia6) 1225{ 1226 VERIFY(m->m_flags & M_PKTHDR); 1227 1228 /* 1229 * If the source ifaddr is specified, pick up the information 1230 * from there; otherwise just grab the passed-in ifindex as the 1231 * caller may not have the ifaddr available. 1232 */ 1233 if (ia6 != NULL) { 1234 m->m_pkthdr.pkt_flags |= PKTF_IFAINFO; 1235 m->m_pkthdr.src_ifindex = ia6->ia_ifp->if_index; 1236 1237 /* See IN6_IFF comments in in6_var.h */ 1238 m->m_pkthdr.src_iff = (ia6->ia6_flags & 0xffff); 1239 } else { 1240 m->m_pkthdr.src_iff = 0; 1241 m->m_pkthdr.src_ifindex = src_idx; 1242 if (src_idx != 0) 1243 m->m_pkthdr.pkt_flags |= PKTF_IFAINFO; 1244 } 1245} 1246 1247void 1248ip6_setdstifaddr_info(struct mbuf *m, uint32_t dst_idx, struct in6_ifaddr *ia6) 1249{ 1250 VERIFY(m->m_flags & M_PKTHDR); 1251 1252 /* 1253 * If the destination ifaddr is specified, pick up the information 1254 * from there; otherwise just grab the passed-in ifindex as the 1255 * caller may not have the ifaddr available. 1256 */ 1257 if (ia6 != NULL) { 1258 m->m_pkthdr.pkt_flags |= PKTF_IFAINFO; 1259 m->m_pkthdr.dst_ifindex = ia6->ia_ifp->if_index; 1260 1261 /* See IN6_IFF comments in in6_var.h */ 1262 m->m_pkthdr.dst_iff = (ia6->ia6_flags & 0xffff); 1263 } else { 1264 m->m_pkthdr.dst_iff = 0; 1265 m->m_pkthdr.dst_ifindex = dst_idx; 1266 if (dst_idx != 0) 1267 m->m_pkthdr.pkt_flags |= PKTF_IFAINFO; 1268 } 1269} 1270 1271int 1272ip6_getsrcifaddr_info(struct mbuf *m, uint32_t *src_idx, uint32_t *ia6f) 1273{ 1274 VERIFY(m->m_flags & M_PKTHDR); 1275 1276 if (!(m->m_pkthdr.pkt_flags & PKTF_IFAINFO)) 1277 return (-1); 1278 1279 if (src_idx != NULL) 1280 *src_idx = m->m_pkthdr.src_ifindex; 1281 1282 if (ia6f != NULL) 1283 *ia6f = m->m_pkthdr.src_iff; 1284 1285 return (0); 1286} 1287 1288int 1289ip6_getdstifaddr_info(struct mbuf *m, uint32_t *dst_idx, uint32_t *ia6f) 1290{ 1291 VERIFY(m->m_flags & M_PKTHDR); 1292 1293 if (!(m->m_pkthdr.pkt_flags & PKTF_IFAINFO)) 1294 return (-1); 1295 1296 if (dst_idx != NULL) 1297 *dst_idx = m->m_pkthdr.dst_ifindex; 1298 1299 if (ia6f != NULL) 1300 *ia6f = m->m_pkthdr.dst_iff; 1301 1302 return (0); 1303} 1304 1305/* 1306 * Hop-by-Hop options header processing. If a valid jumbo payload option is 1307 * included, the real payload length will be stored in plenp. 1308 */ 1309static int 1310ip6_hopopts_input(uint32_t *plenp, uint32_t *rtalertp, struct mbuf **mp, 1311 int *offp) 1312{ 1313 struct mbuf *m = *mp; 1314 int off = *offp, hbhlen; 1315 struct ip6_hbh *hbh; 1316 u_int8_t *opt; 1317 1318 /* validation of the length of the header */ 1319#ifndef PULLDOWN_TEST 1320 IP6_EXTHDR_CHECK(m, off, sizeof (*hbh), return (-1)); 1321 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 1322 hbhlen = (hbh->ip6h_len + 1) << 3; 1323 1324 IP6_EXTHDR_CHECK(m, off, hbhlen, return (-1)); 1325 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 1326#else 1327 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof (struct ip6_hdr), 1328 sizeof (struct ip6_hbh)); 1329 if (hbh == NULL) { 1330 ip6stat.ip6s_tooshort++; 1331 return (-1); 1332 } 1333 hbhlen = (hbh->ip6h_len + 1) << 3; 1334 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof (struct ip6_hdr), 1335 hbhlen); 1336 if (hbh == NULL) { 1337 ip6stat.ip6s_tooshort++; 1338 return (-1); 1339 } 1340#endif 1341 off += hbhlen; 1342 hbhlen -= sizeof (struct ip6_hbh); 1343 opt = (u_int8_t *)hbh + sizeof (struct ip6_hbh); 1344 1345 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof (struct ip6_hbh), 1346 hbhlen, rtalertp, plenp) < 0) 1347 return (-1); 1348 1349 *offp = off; 1350 *mp = m; 1351 return (0); 1352} 1353 1354/* 1355 * Search header for all Hop-by-hop options and process each option. 1356 * This function is separate from ip6_hopopts_input() in order to 1357 * handle a case where the sending node itself process its hop-by-hop 1358 * options header. In such a case, the function is called from ip6_output(). 1359 * 1360 * The function assumes that hbh header is located right after the IPv6 header 1361 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 1362 * opthead + hbhlen is located in continuous memory region. 1363 */ 1364int 1365ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp) 1366 struct mbuf *m; 1367 u_int8_t *opthead; 1368 int hbhlen; 1369 u_int32_t *rtalertp; 1370 u_int32_t *plenp; 1371{ 1372 struct ip6_hdr *ip6; 1373 int optlen = 0; 1374 u_int8_t *opt = opthead; 1375 u_int16_t rtalert_val; 1376 u_int32_t jumboplen; 1377 const int erroff = sizeof (struct ip6_hdr) + sizeof (struct ip6_hbh); 1378 1379 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 1380 switch (*opt) { 1381 case IP6OPT_PAD1: 1382 optlen = 1; 1383 break; 1384 case IP6OPT_PADN: 1385 if (hbhlen < IP6OPT_MINLEN) { 1386 ip6stat.ip6s_toosmall++; 1387 goto bad; 1388 } 1389 optlen = *(opt + 1) + 2; 1390 break; 1391 case IP6OPT_ROUTER_ALERT: 1392 /* XXX may need check for alignment */ 1393 if (hbhlen < IP6OPT_RTALERT_LEN) { 1394 ip6stat.ip6s_toosmall++; 1395 goto bad; 1396 } 1397 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 1398 /* XXX stat */ 1399 icmp6_error(m, ICMP6_PARAM_PROB, 1400 ICMP6_PARAMPROB_HEADER, 1401 erroff + opt + 1 - opthead); 1402 return (-1); 1403 } 1404 optlen = IP6OPT_RTALERT_LEN; 1405 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 1406 *rtalertp = ntohs(rtalert_val); 1407 break; 1408 case IP6OPT_JUMBO: 1409 /* XXX may need check for alignment */ 1410 if (hbhlen < IP6OPT_JUMBO_LEN) { 1411 ip6stat.ip6s_toosmall++; 1412 goto bad; 1413 } 1414 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 1415 /* XXX stat */ 1416 icmp6_error(m, ICMP6_PARAM_PROB, 1417 ICMP6_PARAMPROB_HEADER, 1418 erroff + opt + 1 - opthead); 1419 return (-1); 1420 } 1421 optlen = IP6OPT_JUMBO_LEN; 1422 1423 /* 1424 * IPv6 packets that have non 0 payload length 1425 * must not contain a jumbo payload option. 1426 */ 1427 ip6 = mtod(m, struct ip6_hdr *); 1428 if (ip6->ip6_plen) { 1429 ip6stat.ip6s_badoptions++; 1430 icmp6_error(m, ICMP6_PARAM_PROB, 1431 ICMP6_PARAMPROB_HEADER, 1432 erroff + opt - opthead); 1433 return (-1); 1434 } 1435 1436 /* 1437 * We may see jumbolen in unaligned location, so 1438 * we'd need to perform bcopy(). 1439 */ 1440 bcopy(opt + 2, &jumboplen, sizeof (jumboplen)); 1441 jumboplen = (u_int32_t)htonl(jumboplen); 1442 1443#if 1 1444 /* 1445 * if there are multiple jumbo payload options, 1446 * *plenp will be non-zero and the packet will be 1447 * rejected. 1448 * the behavior may need some debate in ipngwg - 1449 * multiple options does not make sense, however, 1450 * there's no explicit mention in specification. 1451 */ 1452 if (*plenp != 0) { 1453 ip6stat.ip6s_badoptions++; 1454 icmp6_error(m, ICMP6_PARAM_PROB, 1455 ICMP6_PARAMPROB_HEADER, 1456 erroff + opt + 2 - opthead); 1457 return (-1); 1458 } 1459#endif 1460 1461 /* 1462 * jumbo payload length must be larger than 65535. 1463 */ 1464 if (jumboplen <= IPV6_MAXPACKET) { 1465 ip6stat.ip6s_badoptions++; 1466 icmp6_error(m, ICMP6_PARAM_PROB, 1467 ICMP6_PARAMPROB_HEADER, 1468 erroff + opt + 2 - opthead); 1469 return (-1); 1470 } 1471 *plenp = jumboplen; 1472 1473 break; 1474 default: /* unknown option */ 1475 if (hbhlen < IP6OPT_MINLEN) { 1476 ip6stat.ip6s_toosmall++; 1477 goto bad; 1478 } 1479 optlen = ip6_unknown_opt(opt, m, 1480 erroff + opt - opthead); 1481 if (optlen == -1) { 1482 return (-1); 1483 } 1484 optlen += 2; 1485 break; 1486 } 1487 } 1488 1489 return (0); 1490 1491bad: 1492 m_freem(m); 1493 return (-1); 1494} 1495 1496/* 1497 * Unknown option processing. 1498 * The third argument `off' is the offset from the IPv6 header to the option, 1499 * which is necessary if the IPv6 header the and option header and IPv6 header 1500 * is not continuous in order to return an ICMPv6 error. 1501 */ 1502int 1503ip6_unknown_opt(uint8_t *optp, struct mbuf *m, int off) 1504{ 1505 struct ip6_hdr *ip6; 1506 1507 switch (IP6OPT_TYPE(*optp)) { 1508 case IP6OPT_TYPE_SKIP: /* ignore the option */ 1509 return ((int)*(optp + 1)); 1510 1511 case IP6OPT_TYPE_DISCARD: /* silently discard */ 1512 m_freem(m); 1513 return (-1); 1514 1515 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 1516 ip6stat.ip6s_badoptions++; 1517 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 1518 return (-1); 1519 1520 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 1521 ip6stat.ip6s_badoptions++; 1522 ip6 = mtod(m, struct ip6_hdr *); 1523 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1524 (m->m_flags & (M_BCAST|M_MCAST))) { 1525 m_freem(m); 1526 } else { 1527 icmp6_error(m, ICMP6_PARAM_PROB, 1528 ICMP6_PARAMPROB_OPTION, off); 1529 } 1530 return (-1); 1531 } 1532 1533 m_freem(m); /* XXX: NOTREACHED */ 1534 return (-1); 1535} 1536 1537/* 1538 * Create the "control" list for this pcb. 1539 * These functions will not modify mbuf chain at all. 1540 * 1541 * With KAME mbuf chain restriction: 1542 * The routine will be called from upper layer handlers like tcp6_input(). 1543 * Thus the routine assumes that the caller (tcp6_input) have already 1544 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 1545 * very first mbuf on the mbuf chain. 1546 * 1547 * ip6_savecontrol_v4 will handle those options that are possible to be 1548 * set on a v4-mapped socket. 1549 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those 1550 * options and handle the v6-only ones itself. 1551 */ 1552struct mbuf ** 1553ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp, 1554 int *v4only) 1555{ 1556 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1557 1558 if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) { 1559 struct timeval tv; 1560 1561 getmicrotime(&tv); 1562 mp = sbcreatecontrol_mbuf((caddr_t)&tv, sizeof (tv), 1563 SCM_TIMESTAMP, SOL_SOCKET, mp); 1564 if (*mp == NULL) 1565 return (NULL); 1566 } 1567 if ((inp->inp_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) { 1568 uint64_t time; 1569 1570 time = mach_absolute_time(); 1571 mp = sbcreatecontrol_mbuf((caddr_t)&time, sizeof (time), 1572 SCM_TIMESTAMP_MONOTONIC, SOL_SOCKET, mp); 1573 if (*mp == NULL) 1574 return (NULL); 1575 } 1576 if ((inp->inp_socket->so_flags & SOF_RECV_TRAFFIC_CLASS) != 0) { 1577 int tc = m_get_traffic_class(m); 1578 1579 mp = sbcreatecontrol_mbuf((caddr_t)&tc, sizeof (tc), 1580 SO_TRAFFIC_CLASS, SOL_SOCKET, mp); 1581 if (*mp == NULL) 1582 return (NULL); 1583 } 1584 1585 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 1586 if (v4only != NULL) 1587 *v4only = 1; 1588 return (mp); 1589 } 1590 1591#define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y)) 1592 /* RFC 2292 sec. 5 */ 1593 if ((inp->inp_flags & IN6P_PKTINFO) != 0) { 1594 struct in6_pktinfo pi6; 1595 1596 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof (struct in6_addr)); 1597 in6_clearscope(&pi6.ipi6_addr); /* XXX */ 1598 pi6.ipi6_ifindex = 1599 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0; 1600 1601 mp = sbcreatecontrol_mbuf((caddr_t)&pi6, 1602 sizeof (struct in6_pktinfo), 1603 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), 1604 IPPROTO_IPV6, mp); 1605 if (*mp == NULL) 1606 return (NULL); 1607 } 1608 1609 if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) { 1610 int hlim = ip6->ip6_hlim & 0xff; 1611 1612 mp = sbcreatecontrol_mbuf((caddr_t)&hlim, sizeof (int), 1613 IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), 1614 IPPROTO_IPV6, mp); 1615 if (*mp == NULL) 1616 return (NULL); 1617 } 1618 1619 if (v4only != NULL) 1620 *v4only = 0; 1621 return (mp); 1622} 1623 1624int 1625ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp) 1626{ 1627 struct mbuf **np; 1628 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1629 int v4only = 0; 1630 1631 *mp = NULL; 1632 np = ip6_savecontrol_v4(in6p, m, mp, &v4only); 1633 if (np == NULL) 1634 goto no_mbufs; 1635 1636 mp = np; 1637 if (v4only) 1638 return (0); 1639 1640 if ((in6p->inp_flags & IN6P_TCLASS) != 0) { 1641 u_int32_t flowinfo; 1642 int tclass; 1643 1644 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK); 1645 flowinfo >>= 20; 1646 1647 tclass = flowinfo & 0xff; 1648 mp = sbcreatecontrol_mbuf((caddr_t)&tclass, sizeof (tclass), 1649 IPV6_TCLASS, IPPROTO_IPV6, mp); 1650 if (*mp == NULL) 1651 goto no_mbufs; 1652 } 1653 1654 /* 1655 * IPV6_HOPOPTS socket option. Recall that we required super-user 1656 * privilege for the option (see ip6_ctloutput), but it might be too 1657 * strict, since there might be some hop-by-hop options which can be 1658 * returned to normal user. 1659 * See also RFC 2292 section 6 (or RFC 3542 section 8). 1660 */ 1661 if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) { 1662 /* 1663 * Check if a hop-by-hop options header is contatined in the 1664 * received packet, and if so, store the options as ancillary 1665 * data. Note that a hop-by-hop options header must be 1666 * just after the IPv6 header, which is assured through the 1667 * IPv6 input processing. 1668 */ 1669 ip6 = mtod(m, struct ip6_hdr *); 1670 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 1671 struct ip6_hbh *hbh; 1672 int hbhlen = 0; 1673#if PULLDOWN_TEST 1674 struct mbuf *ext; 1675#endif 1676 1677#ifndef PULLDOWN_TEST 1678 hbh = (struct ip6_hbh *)(ip6 + 1); 1679 hbhlen = (hbh->ip6h_len + 1) << 3; 1680#else 1681 ext = ip6_pullexthdr(m, sizeof (struct ip6_hdr), 1682 ip6->ip6_nxt); 1683 if (ext == NULL) { 1684 ip6stat.ip6s_tooshort++; 1685 return (0); 1686 } 1687 hbh = mtod(ext, struct ip6_hbh *); 1688 hbhlen = (hbh->ip6h_len + 1) << 3; 1689 if (hbhlen != ext->m_len) { 1690 m_freem(ext); 1691 ip6stat.ip6s_tooshort++; 1692 return (0); 1693 } 1694#endif 1695 1696 /* 1697 * XXX: We copy the whole header even if a 1698 * jumbo payload option is included, the option which 1699 * is to be removed before returning according to 1700 * RFC2292. 1701 * Note: this constraint is removed in RFC3542 1702 */ 1703 mp = sbcreatecontrol_mbuf((caddr_t)hbh, hbhlen, 1704 IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS), 1705 IPPROTO_IPV6, mp); 1706 1707#if PULLDOWN_TEST 1708 m_freem(ext); 1709#endif 1710 if (*mp == NULL) { 1711 goto no_mbufs; 1712 } 1713 } 1714 } 1715 1716 if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) { 1717 int nxt = ip6->ip6_nxt, off = sizeof (struct ip6_hdr); 1718 1719 /* 1720 * Search for destination options headers or routing 1721 * header(s) through the header chain, and stores each 1722 * header as ancillary data. 1723 * Note that the order of the headers remains in 1724 * the chain of ancillary data. 1725 */ 1726 while (1) { /* is explicit loop prevention necessary? */ 1727 struct ip6_ext *ip6e = NULL; 1728 int elen; 1729#if PULLDOWN_TEST 1730 struct mbuf *ext = NULL; 1731#endif 1732 1733 /* 1734 * if it is not an extension header, don't try to 1735 * pull it from the chain. 1736 */ 1737 switch (nxt) { 1738 case IPPROTO_DSTOPTS: 1739 case IPPROTO_ROUTING: 1740 case IPPROTO_HOPOPTS: 1741 case IPPROTO_AH: /* is it possible? */ 1742 break; 1743 default: 1744 goto loopend; 1745 } 1746 1747#ifndef PULLDOWN_TEST 1748 if (off + sizeof (*ip6e) > m->m_len) 1749 goto loopend; 1750 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 1751 if (nxt == IPPROTO_AH) 1752 elen = (ip6e->ip6e_len + 2) << 2; 1753 else 1754 elen = (ip6e->ip6e_len + 1) << 3; 1755 if (off + elen > m->m_len) 1756 goto loopend; 1757#else 1758 ext = ip6_pullexthdr(m, off, nxt); 1759 if (ext == NULL) { 1760 ip6stat.ip6s_tooshort++; 1761 return (0); 1762 } 1763 ip6e = mtod(ext, struct ip6_ext *); 1764 if (nxt == IPPROTO_AH) 1765 elen = (ip6e->ip6e_len + 2) << 2; 1766 else 1767 elen = (ip6e->ip6e_len + 1) << 3; 1768 if (elen != ext->m_len) { 1769 m_freem(ext); 1770 ip6stat.ip6s_tooshort++; 1771 return (0); 1772 } 1773#endif 1774 1775 switch (nxt) { 1776 case IPPROTO_DSTOPTS: 1777 if (!(in6p->inp_flags & IN6P_DSTOPTS)) 1778 break; 1779 1780 mp = sbcreatecontrol_mbuf((caddr_t)ip6e, elen, 1781 IS2292(in6p, IPV6_2292DSTOPTS, 1782 IPV6_DSTOPTS), IPPROTO_IPV6, mp); 1783 if (*mp == NULL) { 1784#if PULLDOWN_TEST 1785 m_freem(ext); 1786#endif 1787 goto no_mbufs; 1788 } 1789 break; 1790 case IPPROTO_ROUTING: 1791 if (!in6p->inp_flags & IN6P_RTHDR) 1792 break; 1793 1794 mp = sbcreatecontrol_mbuf((caddr_t)ip6e, elen, 1795 IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR), 1796 IPPROTO_IPV6, mp); 1797 if (*mp == NULL) { 1798#if PULLDOWN_TEST 1799 m_freem(ext); 1800#endif 1801 goto no_mbufs; 1802 } 1803 break; 1804 case IPPROTO_HOPOPTS: 1805 case IPPROTO_AH: /* is it possible? */ 1806 break; 1807 1808 default: 1809 /* 1810 * other cases have been filtered in the above. 1811 * none will visit this case. here we supply 1812 * the code just in case (nxt overwritten or 1813 * other cases). 1814 */ 1815#if PULLDOWN_TEST 1816 m_freem(ext); 1817#endif 1818 goto loopend; 1819 1820 } 1821 1822 /* proceed with the next header. */ 1823 off += elen; 1824 nxt = ip6e->ip6e_nxt; 1825 ip6e = NULL; 1826#if PULLDOWN_TEST 1827 m_freem(ext); 1828 ext = NULL; 1829#endif 1830 } 1831loopend: 1832 ; 1833 } 1834 return (0); 1835no_mbufs: 1836 ip6stat.ip6s_pktdropcntrl++; 1837 /* XXX increment a stat to show the failure */ 1838 return (ENOBUFS); 1839} 1840#undef IS2292 1841 1842void 1843ip6_notify_pmtu(struct inpcb *in6p, struct sockaddr_in6 *dst, u_int32_t *mtu) 1844{ 1845 struct socket *so; 1846 struct mbuf *m_mtu; 1847 struct ip6_mtuinfo mtuctl; 1848 1849 so = in6p->inp_socket; 1850 1851 if (mtu == NULL) 1852 return; 1853 1854#ifdef DIAGNOSTIC 1855 if (so == NULL) { /* I believe this is impossible */ 1856 panic("ip6_notify_pmtu: socket is NULL"); 1857 /* NOTREACHED */ 1858 } 1859#endif 1860 1861 bzero(&mtuctl, sizeof (mtuctl)); /* zero-clear for safety */ 1862 mtuctl.ip6m_mtu = *mtu; 1863 mtuctl.ip6m_addr = *dst; 1864 if (sa6_recoverscope(&mtuctl.ip6m_addr, TRUE)) 1865 return; 1866 1867 if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof (mtuctl), 1868 IPV6_PATHMTU, IPPROTO_IPV6)) == NULL) 1869 return; 1870 1871 if (sbappendaddr(&so->so_rcv, SA(dst), NULL, m_mtu, NULL) == 0) { 1872 m_freem(m_mtu); 1873 /* XXX: should count statistics */ 1874 } else { 1875 sorwakeup(so); 1876 } 1877} 1878 1879#if PULLDOWN_TEST 1880/* 1881 * pull single extension header from mbuf chain. returns single mbuf that 1882 * contains the result, or NULL on error. 1883 */ 1884static struct mbuf * 1885ip6_pullexthdr(m, off, nxt) 1886 struct mbuf *m; 1887 size_t off; 1888 int nxt; 1889{ 1890 struct ip6_ext ip6e; 1891 size_t elen; 1892 struct mbuf *n; 1893 1894#if DIAGNOSTIC 1895 switch (nxt) { 1896 case IPPROTO_DSTOPTS: 1897 case IPPROTO_ROUTING: 1898 case IPPROTO_HOPOPTS: 1899 case IPPROTO_AH: /* is it possible? */ 1900 break; 1901 default: 1902 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 1903 } 1904#endif 1905 1906 m_copydata(m, off, sizeof (ip6e), (caddr_t)&ip6e); 1907 if (nxt == IPPROTO_AH) 1908 elen = (ip6e.ip6e_len + 2) << 2; 1909 else 1910 elen = (ip6e.ip6e_len + 1) << 3; 1911 1912 MGET(n, M_DONTWAIT, MT_DATA); 1913 if (n && elen >= MLEN) { 1914 MCLGET(n, M_DONTWAIT); 1915 if ((n->m_flags & M_EXT) == 0) { 1916 m_free(n); 1917 n = NULL; 1918 } 1919 } 1920 if (!n) 1921 return (NULL); 1922 1923 n->m_len = 0; 1924 if (elen >= M_TRAILINGSPACE(n)) { 1925 m_free(n); 1926 return (NULL); 1927 } 1928 1929 m_copydata(m, off, elen, mtod(n, caddr_t)); 1930 n->m_len = elen; 1931 return (n); 1932} 1933#endif 1934 1935/* 1936 * Get pointer to the previous header followed by the header 1937 * currently processed. 1938 * XXX: This function supposes that 1939 * M includes all headers, 1940 * the next header field and the header length field of each header 1941 * are valid, and 1942 * the sum of each header length equals to OFF. 1943 * Because of these assumptions, this function must be called very 1944 * carefully. Moreover, it will not be used in the near future when 1945 * we develop `neater' mechanism to process extension headers. 1946 */ 1947char * 1948ip6_get_prevhdr(m, off) 1949 struct mbuf *m; 1950 int off; 1951{ 1952 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1953 1954 if (off == sizeof (struct ip6_hdr)) { 1955 return ((char *)&ip6->ip6_nxt); 1956 } else { 1957 int len, nxt; 1958 struct ip6_ext *ip6e = NULL; 1959 1960 nxt = ip6->ip6_nxt; 1961 len = sizeof (struct ip6_hdr); 1962 while (len < off) { 1963 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 1964 1965 switch (nxt) { 1966 case IPPROTO_FRAGMENT: 1967 len += sizeof (struct ip6_frag); 1968 break; 1969 case IPPROTO_AH: 1970 len += (ip6e->ip6e_len + 2) << 2; 1971 break; 1972 default: 1973 len += (ip6e->ip6e_len + 1) << 3; 1974 break; 1975 } 1976 nxt = ip6e->ip6e_nxt; 1977 } 1978 if (ip6e) 1979 return ((char *)&ip6e->ip6e_nxt); 1980 else 1981 return (NULL); 1982 } 1983} 1984 1985/* 1986 * get next header offset. m will be retained. 1987 */ 1988int 1989ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp) 1990{ 1991 struct ip6_hdr ip6; 1992 struct ip6_ext ip6e; 1993 struct ip6_frag fh; 1994 1995 /* just in case */ 1996 VERIFY(m != NULL); 1997 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 1998 return (-1); 1999 2000 switch (proto) { 2001 case IPPROTO_IPV6: 2002 if (m->m_pkthdr.len < off + sizeof (ip6)) 2003 return (-1); 2004 m_copydata(m, off, sizeof (ip6), (caddr_t)&ip6); 2005 if (nxtp) 2006 *nxtp = ip6.ip6_nxt; 2007 off += sizeof (ip6); 2008 return (off); 2009 2010 case IPPROTO_FRAGMENT: 2011 /* 2012 * terminate parsing if it is not the first fragment, 2013 * it does not make sense to parse through it. 2014 */ 2015 if (m->m_pkthdr.len < off + sizeof (fh)) 2016 return (-1); 2017 m_copydata(m, off, sizeof (fh), (caddr_t)&fh); 2018 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */ 2019 if (fh.ip6f_offlg & IP6F_OFF_MASK) 2020 return (-1); 2021 if (nxtp) 2022 *nxtp = fh.ip6f_nxt; 2023 off += sizeof (struct ip6_frag); 2024 return (off); 2025 2026 case IPPROTO_AH: 2027 if (m->m_pkthdr.len < off + sizeof (ip6e)) 2028 return (-1); 2029 m_copydata(m, off, sizeof (ip6e), (caddr_t)&ip6e); 2030 if (nxtp) 2031 *nxtp = ip6e.ip6e_nxt; 2032 off += (ip6e.ip6e_len + 2) << 2; 2033 return (off); 2034 2035 case IPPROTO_HOPOPTS: 2036 case IPPROTO_ROUTING: 2037 case IPPROTO_DSTOPTS: 2038 if (m->m_pkthdr.len < off + sizeof (ip6e)) 2039 return (-1); 2040 m_copydata(m, off, sizeof (ip6e), (caddr_t)&ip6e); 2041 if (nxtp) 2042 *nxtp = ip6e.ip6e_nxt; 2043 off += (ip6e.ip6e_len + 1) << 3; 2044 return (off); 2045 2046 case IPPROTO_NONE: 2047 case IPPROTO_ESP: 2048 case IPPROTO_IPCOMP: 2049 /* give up */ 2050 return (-1); 2051 2052 default: 2053 return (-1); 2054 } 2055 2056 return (-1); 2057} 2058 2059/* 2060 * get offset for the last header in the chain. m will be kept untainted. 2061 */ 2062int 2063ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp) 2064{ 2065 int newoff; 2066 int nxt; 2067 2068 if (!nxtp) { 2069 nxt = -1; 2070 nxtp = &nxt; 2071 } 2072 while (1) { 2073 newoff = ip6_nexthdr(m, off, proto, nxtp); 2074 if (newoff < 0) 2075 return (off); 2076 else if (newoff < off) 2077 return (-1); /* invalid */ 2078 else if (newoff == off) 2079 return (newoff); 2080 2081 off = newoff; 2082 proto = *nxtp; 2083 } 2084} 2085 2086struct ip6aux * 2087ip6_addaux(struct mbuf *m) 2088{ 2089 struct m_tag *tag; 2090 2091 /* Check if one is already allocated */ 2092 tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, 2093 KERNEL_TAG_TYPE_INET6, NULL); 2094 if (tag == NULL) { 2095 /* Allocate a tag */ 2096 tag = m_tag_create(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_INET6, 2097 sizeof (struct ip6aux), M_DONTWAIT, m); 2098 2099 /* Attach it to the mbuf */ 2100 if (tag) { 2101 m_tag_prepend(m, tag); 2102 } 2103 } 2104 2105 return (tag ? (struct ip6aux *)(tag + 1) : NULL); 2106} 2107 2108struct ip6aux * 2109ip6_findaux(struct mbuf *m) 2110{ 2111 struct m_tag *tag; 2112 2113 tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, 2114 KERNEL_TAG_TYPE_INET6, NULL); 2115 2116 return (tag ? (struct ip6aux *)(tag + 1) : NULL); 2117} 2118 2119void 2120ip6_delaux(struct mbuf *m) 2121{ 2122 struct m_tag *tag; 2123 2124 tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, 2125 KERNEL_TAG_TYPE_INET6, NULL); 2126 if (tag) { 2127 m_tag_delete(m, tag); 2128 } 2129} 2130 2131/* 2132 * Drain callback 2133 */ 2134void 2135ip6_drain(void) 2136{ 2137 frag6_drain(); /* fragments */ 2138 in6_rtqdrain(); /* protocol cloned routes */ 2139 nd6_drain(NULL); /* cloned routes: ND6 */ 2140} 2141 2142/* 2143 * System control for IP6 2144 */ 2145 2146u_char inet6ctlerrmap[PRC_NCMDS] = { 2147 0, 0, 0, 0, 2148 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 2149 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 2150 EMSGSIZE, EHOSTUNREACH, 0, 0, 2151 0, 0, 0, 0, 2152 ENOPROTOOPT 2153}; 2154