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