X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/55e303ae13a4cf49d70f2294092726f2fffb9ef2..316670eb35587141e969394ae8537d66b9211e80:/bsd/netinet6/icmp6.c diff --git a/bsd/netinet6/icmp6.c b/bsd/netinet6/icmp6.c index 654dc8b15..cdf92a564 100644 --- a/bsd/netinet6/icmp6.c +++ b/bsd/netinet6/icmp6.c @@ -1,3 +1,31 @@ +/* + * Copyright (c) 2000-2012 Apple Inc. All rights reserved. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. + * + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ + */ + /* $FreeBSD: src/sys/netinet6/icmp6.c,v 1.6.2.6 2001/07/10 09:44:16 ume Exp $ */ /* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ */ @@ -68,7 +96,9 @@ #include #include +#include #include +#include #include #include #include @@ -77,6 +107,7 @@ #include #include #include +#include #include #include @@ -94,6 +125,7 @@ #include #include #include +#include #if IPSEC #include @@ -102,46 +134,15 @@ extern int ipsec_bypass; #endif -#include "faith.h" -#if defined(NFAITH) && 0 < NFAITH -#include -#endif - #include -#if HAVE_NRL_INPCB -/* inpcb members */ -#define in6pcb inpcb -#define in6p_laddr inp_laddr6 -#define in6p_faddr inp_faddr6 -#define in6p_icmp6filt inp_icmp6filt -#define in6p_route inp_route -#define in6p_socket inp_socket -#define in6p_flags inp_flags -#define in6p_moptions inp_moptions6 -#define in6p_outputopts inp_outputopts6 -#define in6p_ip6 inp_ipv6 -#define in6p_flowinfo inp_flowinfo -#define in6p_sp inp_sp -#define in6p_next inp_next -#define in6p_prev inp_prev -/* macro names */ -#define sotoin6pcb sotoinpcb -/* function names */ -#define in6_pcbdetach in_pcbdetach -#define in6_rtchange in_rtchange - -/* - * for KAME src sync over BSD*'s. XXX: FreeBSD (>=3) are VERY different from - * others... - */ -#define in6p_ip6_nxt inp_ipv6.ip6_nxt -#endif - extern struct domain inet6domain; extern struct ip6protosw inet6sw[]; extern struct ip6protosw *ip6_protox[]; +extern uint32_t rip_sendspace; +extern uint32_t rip_recvspace; + struct icmp6stat icmp6stat; extern struct inpcbhead ripcb; @@ -149,33 +150,32 @@ extern int icmp6errppslim; static int icmp6errpps_count = 0; static struct timeval icmp6errppslim_last; extern int icmp6_nodeinfo; - -static void icmp6_errcount __P((struct icmp6errstat *, int, int)); -static int icmp6_rip6_input __P((struct mbuf **, int)); -static int icmp6_ratelimit __P((const struct in6_addr *, const int, const int)); -static const char *icmp6_redirect_diag __P((struct in6_addr *, - struct in6_addr *, struct in6_addr *)); +extern struct inpcbinfo ripcbinfo; +extern lck_mtx_t *inet6_domain_mutex; + +static void icmp6_errcount(struct icmp6errstat *, int, int); +static int icmp6_rip6_input(struct mbuf **, int); +static int icmp6_ratelimit(const struct in6_addr *, const int, const int); +static const char *icmp6_redirect_diag(struct in6_addr *, + struct in6_addr *, struct in6_addr *); #ifndef HAVE_PPSRATECHECK -static int ppsratecheck __P((struct timeval *, int *, int)); -#endif -static struct mbuf *ni6_input __P((struct mbuf *, int)); -static struct mbuf *ni6_nametodns __P((const char *, int, int)); -static int ni6_dnsmatch __P((const char *, int, const char *, int)); -static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *, - struct ifnet **, char *)); -static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, - struct ifnet *, int)); -static int icmp6_notify_error __P((struct mbuf *, int, int, int)); - -#ifdef COMPAT_RFC1885 -static struct route_in6 icmp6_reflect_rt; +static int ppsratecheck(struct timeval *, int *, int); #endif +static struct mbuf *ni6_input(struct mbuf *, int); +static struct mbuf *ni6_nametodns(const char *, int, int); +static int ni6_dnsmatch(const char *, int, const char *, int); +static int ni6_addrs(struct icmp6_nodeinfo *, + struct ifnet **, char *); +static int ni6_store_addrs(struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, + struct ifnet *, int); +static int icmp6_notify_error(struct mbuf *, int, int, int); + void icmp6_init() { - mld6_init(); + mld_init(); } static void @@ -236,13 +236,44 @@ icmp6_errcount(stat, type, code) stat->icp6errs_unknown++; } +/* + * A wrapper function for icmp6_error() necessary when the erroneous packet + * may not contain enough scope zone information. + */ +void +icmp6_error2(struct mbuf *m, int type, int code, int param, + struct ifnet *ifp) +{ + struct ip6_hdr *ip6; + + if (ifp == NULL) + return; + +#ifndef PULLDOWN_TEST + IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr),return ); +#else + if (m->m_len < sizeof(struct ip6_hdr)) { + m = m_pullup(m, sizeof(struct ip6_hdr)); + if (m == NULL) + return; + } +#endif + + ip6 = mtod(m, struct ip6_hdr *); + + if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0) + return; + if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) + return; + + icmp6_error(m, type, code, param); +} + /* * Generate an error packet of type error in response to bad IP6 packet. */ void -icmp6_error(m, type, code, param) - struct mbuf *m; - int type, code, param; +icmp6_error(struct mbuf *m, int type, int code, int param) { struct ip6_hdr *oip6, *nip6; struct icmp6_hdr *icmp6; @@ -263,7 +294,7 @@ icmp6_error(m, type, code, param) #endif #ifndef PULLDOWN_TEST - IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); + IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), return); #else if (m->m_len < sizeof(struct ip6_hdr)) { m = m_pullup(m, sizeof(struct ip6_hdr)); @@ -274,9 +305,15 @@ icmp6_error(m, type, code, param) oip6 = mtod(m, struct ip6_hdr *); /* - * Multicast destination check. For unrecognized option errors, - * this check has already done in ip6_unknown_opt(), so we can - * check only for other errors. + * If the destination address of the erroneous packet is a multicast + * address, or the packet was sent using link-layer multicast, + * we should basically suppress sending an error (RFC 2463, Section + * 2.4). + * We have two exceptions (the item e.2 in that section): + * - the Pakcet Too Big message can be sent for path MTU discovery. + * - the Parameter Problem Message that can be allowed an icmp6 error + * in the option type field. This check has been done in + * ip6_unknown_opt(), so we can just check the type and code. */ if ((m->m_flags & (M_BCAST|M_MCAST) || IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && @@ -285,7 +322,10 @@ icmp6_error(m, type, code, param) code != ICMP6_PARAMPROB_OPTION))) goto freeit; - /* Source address check. XXX: the case of anycast source? */ + /* + * RFC 2463, 2.4 (e.5): source address check. + * XXX: the case of anycast source? + */ if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) goto freeit; @@ -300,7 +340,7 @@ icmp6_error(m, type, code, param) struct icmp6_hdr *icp; #ifndef PULLDOWN_TEST - IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), ); + IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), return); icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); #else IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, @@ -354,10 +394,8 @@ icmp6_error(m, type, code, param) nip6->ip6_src = oip6->ip6_src; nip6->ip6_dst = oip6->ip6_dst; - if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src)) - oip6->ip6_src.s6_addr16[1] = 0; - if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst)) - oip6->ip6_dst.s6_addr16[1] = 0; + in6_clearscope(&oip6->ip6_src); + in6_clearscope(&oip6->ip6_dst); icmp6 = (struct icmp6_hdr *)(nip6 + 1); icmp6->icmp6_type = type; @@ -366,7 +404,7 @@ icmp6_error(m, type, code, param) /* * icmp6_reflect() is designed to be in the input path. - * icmp6_error() can be called from both input and outut path, + * icmp6_error() can be called from both input and output path, * and if we are in output path rcvif could contain bogus value. * clear m->m_pkthdr.rcvif for safety, we should have enough scope * information in ip header (nip6). @@ -380,7 +418,7 @@ icmp6_error(m, type, code, param) freeit: /* - * If we can't tell wheter or not we can generate ICMP6, free it. + * If we can't tell whether or not we can generate ICMP6, free it. */ m_freem(m); } @@ -389,33 +427,64 @@ icmp6_error(m, type, code, param) * Process a received ICMP6 message. */ int -icmp6_input(mp, offp) - struct mbuf **mp; - int *offp; +icmp6_input(struct mbuf **mp, int *offp, int proto) { +#pragma unused(proto) struct mbuf *m = *mp, *n; + struct ifnet *ifp; struct ip6_hdr *ip6, *nip6; struct icmp6_hdr *icmp6, *nicmp6; int off = *offp; int icmp6len = m->m_pkthdr.len - *offp; - int code, sum, noff; + int code, sum, noff, proxy = 0; + + ifp = m->m_pkthdr.rcvif; #ifndef PULLDOWN_TEST - IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE); + IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), return IPPROTO_DONE); /* m might change if M_LOOP. So, call mtod after this */ #endif + /* Expect 32-bit aligned data pointer on strict-align platforms */ + MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); + /* * Locate icmp6 structure in mbuf, and check * that not corrupted and of at least minimum length */ - ip6 = mtod(m, struct ip6_hdr *); if (icmp6len < sizeof(struct icmp6_hdr)) { icmp6stat.icp6s_tooshort++; goto freeit; } + /* + * Check multicast group membership. + * Note: SSM filters are not applied for ICMPv6 traffic. + */ + if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { + struct in6_multi *inm; + + in6_multihead_lock_shared(); + IN6_LOOKUP_MULTI(&ip6->ip6_dst, ifp, inm); + in6_multihead_lock_done(); + + if (inm == NULL) { + /* + * Don't discard if this is a Neighbor Solicitation + * that needs to be proxied (see check down below.) + */ + if (!(m->m_pkthdr.aux_flags & MAUXF_PROXY_DST)) { + ip6stat.ip6s_notmember++; + in6_ifstat_inc(m->m_pkthdr.rcvif, + ifs6_in_discard); + goto freeit; + } + } else { + IN6M_REMREF(inm); + } + } + /* * calculate the checksum */ @@ -438,23 +507,22 @@ icmp6_input(mp, offp) goto freeit; } -#if defined(NFAITH) && 0 < NFAITH - if (faithprefix(&ip6->ip6_dst)) { + if (m->m_pkthdr.aux_flags & MAUXF_PROXY_DST) { /* - * Deliver very specific ICMP6 type only. - * This is important to deilver TOOBIG. Otherwise PMTUD - * will not work. + * This is the special case of proxying NS (dst is either + * solicited-node multicast or unicast); process it locally + * but don't deliver it to sockets. It practically lets us + * steer the packet to nd6_prproxy_ns_input, where more + * specific tests and actions will be taken. */ switch (icmp6->icmp6_type) { - case ICMP6_DST_UNREACH: - case ICMP6_PACKET_TOO_BIG: - case ICMP6_TIME_EXCEEDED: + case ND_NEIGHBOR_SOLICIT: + proxy = 1; break; default: goto freeit; } } -#endif icmp6stat.icp6s_inhist[icmp6->icmp6_type]++; icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg); @@ -475,16 +543,10 @@ icmp6_input(mp, offp) case ICMP6_DST_UNREACH_ADDR: code = PRC_HOSTDEAD; break; -#ifdef COMPAT_RFC1885 - case ICMP6_DST_UNREACH_NOTNEIGHBOR: - code = PRC_UNREACH_SRCFAIL; - break; -#else case ICMP6_DST_UNREACH_BEYONDSCOPE: /* I mean "source address was incorrect." */ code = PRC_PARAMPROB; break; -#endif case ICMP6_DST_UNREACH_NOPORT: code = PRC_UNREACH_PORT; break; @@ -496,8 +558,6 @@ icmp6_input(mp, offp) case ICMP6_PACKET_TOO_BIG: icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig); - if (code != 0) - goto badcode; code = PRC_MSGSIZE; @@ -512,8 +572,10 @@ icmp6_input(mp, offp) icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed); switch (code) { case ICMP6_TIME_EXCEED_TRANSIT: + code = PRC_TIMXCEED_INTRANS; + break; case ICMP6_TIME_EXCEED_REASSEMBLY: - code += PRC_TIMXCEED_INTRANS; + code = PRC_TIMXCEED_REASS; break; default: goto badcode; @@ -541,8 +603,15 @@ icmp6_input(mp, offp) icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo); if (code != 0) goto badcode; + + if (icmp6_ratelimit(&ip6->ip6_dst, icmp6->icmp6_type, code)) { + icmp6stat.icp6s_toofreq++; + goto freeit; + } + if ((n = m_copy(m, 0, M_COPYALL)) == NULL) { /* Give up remote */ + goto rate_limit_checked; break; } if ((n->m_flags & M_EXT) != 0 @@ -557,9 +626,10 @@ icmp6_input(mp, offp) if (maxlen >= MCLBYTES) { /* Give up remote */ m_freem(n0); + goto rate_limit_checked; break; } - MGETHDR(n, M_DONTWAIT, n0->m_type); + MGETHDR(n, M_DONTWAIT, n0->m_type); /* MAC-OK */ if (n && maxlen >= MHLEN) { MCLGET(n, M_DONTWAIT); if ((n->m_flags & M_EXT) == 0) { @@ -570,6 +640,7 @@ icmp6_input(mp, offp) if (n == NULL) { /* Give up remote */ m_freem(n0); + goto rate_limit_checked; break; } M_COPY_PKTHDR(n, n0); @@ -593,16 +664,22 @@ icmp6_input(mp, offp) n0->m_flags &= ~M_PKTHDR; } else { nip6 = mtod(n, struct ip6_hdr *); - nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off); + IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off, + sizeof(*nicmp6)); noff = off; } - nicmp6->icmp6_type = ICMP6_ECHO_REPLY; - nicmp6->icmp6_code = 0; + if(nicmp6 == NULL) + panic("nicmp6 is NULL in %s, which isn't good!\n", __FUNCTION__); + else { + nicmp6->icmp6_type = ICMP6_ECHO_REPLY; + nicmp6->icmp6_code = 0; + } if (n) { icmp6stat.icp6s_reflect++; icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++; icmp6_reflect(n, noff); } + goto rate_limit_checked; break; case ICMP6_ECHO_REPLY: @@ -611,117 +688,76 @@ icmp6_input(mp, offp) goto badcode; break; - case MLD6_LISTENER_QUERY: - case MLD6_LISTENER_REPORT: - if (icmp6len < sizeof(struct mld6_hdr)) + case MLD_LISTENER_QUERY: + case MLD_LISTENER_REPORT: + + if (icmp6len < sizeof(struct mld_hdr)) goto badlen; - if (icmp6->icmp6_type == MLD6_LISTENER_QUERY) /* XXX: ugly... */ + if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */ icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery); else icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport); + + if (icmp6_ratelimit(&ip6->ip6_dst, icmp6->icmp6_type, code)) { + icmp6stat.icp6s_toofreq++; + goto freeit; + } + if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { /* give up local */ - mld6_input(m, off); - m = NULL; + if (mld_input(m, off, icmp6len) == IPPROTO_DONE) + m = NULL; goto freeit; } - mld6_input(n, off); + if (mld_input(n, off, icmp6len) != IPPROTO_DONE) + m_freem(n); /* m stays. */ + goto rate_limit_checked; break; - case MLD6_LISTENER_DONE: + case MLD_LISTENER_DONE: icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone); - if (icmp6len < sizeof(struct mld6_hdr)) /* necessary? */ + if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */ goto badlen; break; /* nothing to be done in kernel */ - case MLD6_MTRACE_RESP: - case MLD6_MTRACE: - /* XXX: these two are experimental. not officially defind. */ + case MLD_MTRACE_RESP: + case MLD_MTRACE: + /* XXX: these two are experimental. not officially defined. */ /* XXX: per-interface statistics? */ break; /* just pass it to applications */ - case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ - { - enum { WRU, FQDN } mode; - + case ICMP6_NI_QUERY: if (!icmp6_nodeinfo) break; +//### LD 10/20 Check fbsd differences here. Not sure we're more advanced or not. + /* By RFC 4620 refuse to answer queries from global scope addresses */ + if ((icmp6_nodeinfo & 8) != 8 && in6_addrscope(&ip6->ip6_src) == IPV6_ADDR_SCOPE_GLOBAL) + break; - if (icmp6len == sizeof(struct icmp6_hdr) + 4) - mode = WRU; - else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) - mode = FQDN; - else + if (icmp6len < sizeof(struct icmp6_nodeinfo)) goto badlen; -#define hostnamelen strlen(hostname) - if (mode == FQDN) { #ifndef PULLDOWN_TEST - IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), - IPPROTO_DONE); + IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), + return IPPROTO_DONE); #endif - n = m_copy(m, 0, M_COPYALL); - if (n) - n = ni6_input(n, off); - /* XXX meaningless if n == NULL */ - noff = sizeof(struct ip6_hdr); - } else { - u_char *p; - int maxlen, maxhlen; - - if ((icmp6_nodeinfo & 5) != 5) - break; - - if (code != 0) - goto badcode; - maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4; - if (maxlen >= MCLBYTES) { - /* Give up remote */ - break; - } - MGETHDR(n, M_DONTWAIT, m->m_type); - if (n && maxlen > MHLEN) { - MCLGET(n, M_DONTWAIT); - if ((n->m_flags & M_EXT) == 0) { - m_free(n); - n = NULL; - } - } - if (n == NULL) { - /* Give up remote */ - break; - } - n->m_pkthdr.rcvif = NULL; - n->m_len = 0; - maxhlen = M_TRAILINGSPACE(n) - maxlen; - if (maxhlen > hostnamelen) - maxhlen = hostnamelen; - /* - * Copy IPv6 and ICMPv6 only. - */ - nip6 = mtod(n, struct ip6_hdr *); - bcopy(ip6, nip6, sizeof(struct ip6_hdr)); - nicmp6 = (struct icmp6_hdr *)(nip6 + 1); - bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); - p = (u_char *)(nicmp6 + 1); - bzero(p, 4); - bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */ - noff = sizeof(struct ip6_hdr); - M_COPY_PKTHDR(n, m); /* just for rcvif */ - n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + - sizeof(struct icmp6_hdr) + 4 + maxhlen; - nicmp6->icmp6_type = ICMP6_WRUREPLY; - nicmp6->icmp6_code = 0; + if (icmp6_ratelimit(&ip6->ip6_dst, icmp6->icmp6_type, code)) { + icmp6stat.icp6s_toofreq++; + goto freeit; } -#undef hostnamelen + + n = m_copy(m, 0, M_COPYALL); + if (n) + n = ni6_input(n, off); if (n) { + noff = sizeof(struct ip6_hdr); icmp6stat.icp6s_reflect++; icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++; icmp6_reflect(n, noff); } + goto rate_limit_checked; break; - } case ICMP6_WRUREPLY: if (code != 0) @@ -734,6 +770,12 @@ icmp6_input(mp, offp) goto badcode; if (icmp6len < sizeof(struct nd_router_solicit)) goto badlen; + + if (icmp6_ratelimit(&ip6->ip6_dst, icmp6->icmp6_type, code)) { + icmp6stat.icp6s_toofreq++; + goto freeit; + } + if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { /* give up local */ nd6_rs_input(m, off, icmp6len); @@ -742,6 +784,7 @@ icmp6_input(mp, offp) } nd6_rs_input(n, off, icmp6len); /* m stays. */ + goto rate_limit_checked; break; case ND_ROUTER_ADVERT: @@ -750,6 +793,12 @@ icmp6_input(mp, offp) goto badcode; if (icmp6len < sizeof(struct nd_router_advert)) goto badlen; + + if (icmp6_ratelimit(&ip6->ip6_dst, icmp6->icmp6_type, code)) { + icmp6stat.icp6s_toofreq++; + goto freeit; + } + if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { /* give up local */ nd6_ra_input(m, off, icmp6len); @@ -758,6 +807,7 @@ icmp6_input(mp, offp) } nd6_ra_input(n, off, icmp6len); /* m stays. */ + goto rate_limit_checked; break; case ND_NEIGHBOR_SOLICIT: @@ -766,6 +816,12 @@ icmp6_input(mp, offp) goto badcode; if (icmp6len < sizeof(struct nd_neighbor_solicit)) goto badlen; + + if (icmp6_ratelimit(&ip6->ip6_dst, icmp6->icmp6_type, code)) { + icmp6stat.icp6s_toofreq++; + goto freeit; + } + if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { /* give up local */ nd6_ns_input(m, off, icmp6len); @@ -774,6 +830,7 @@ icmp6_input(mp, offp) } nd6_ns_input(n, off, icmp6len); /* m stays. */ + goto rate_limit_checked; break; case ND_NEIGHBOR_ADVERT: @@ -782,6 +839,12 @@ icmp6_input(mp, offp) goto badcode; if (icmp6len < sizeof(struct nd_neighbor_advert)) goto badlen; + + if (icmp6_ratelimit(&ip6->ip6_dst, icmp6->icmp6_type, code)) { + icmp6stat.icp6s_toofreq++; + goto freeit; + } + if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { /* give up local */ nd6_na_input(m, off, icmp6len); @@ -790,6 +853,7 @@ icmp6_input(mp, offp) } nd6_na_input(n, off, icmp6len); /* m stays. */ + goto rate_limit_checked; break; case ND_REDIRECT: @@ -798,6 +862,12 @@ icmp6_input(mp, offp) goto badcode; if (icmp6len < sizeof(struct nd_redirect)) goto badlen; + + if (icmp6_ratelimit(&ip6->ip6_dst, icmp6->icmp6_type, code)) { + icmp6stat.icp6s_toofreq++; + goto freeit; + } + if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { /* give up local */ icmp6_redirect_input(m, off); @@ -806,6 +876,7 @@ icmp6_input(mp, offp) } icmp6_redirect_input(n, off); /* m stays. */ + goto rate_limit_checked; break; case ICMP6_ROUTER_RENUMBERING: @@ -817,6 +888,11 @@ icmp6_input(mp, offp) break; default: + if (icmp6_ratelimit(&ip6->ip6_dst, icmp6->icmp6_type, code)) { + icmp6stat.icp6s_toofreq++; + goto freeit; + } + nd6log((LOG_DEBUG, "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src), @@ -828,9 +904,15 @@ icmp6_input(mp, offp) /* deliver */ } else { /* ICMPv6 informational: MUST not deliver */ + goto rate_limit_checked; break; } deliver: + if (icmp6_ratelimit(&ip6->ip6_dst, icmp6->icmp6_type, code)) { + icmp6stat.icp6s_toofreq++; + goto freeit; + } + if (icmp6_notify_error(m, off, icmp6len, code)) { /* In this case, m should've been freed. */ return(IPPROTO_DONE); @@ -846,12 +928,18 @@ icmp6_input(mp, offp) break; } - /* deliver the packet to appropriate sockets */ - icmp6_rip6_input(&m, *offp); - - return IPPROTO_DONE; + if (icmp6_ratelimit(&ip6->ip6_dst, icmp6->icmp6_type, code)) { + icmp6stat.icp6s_toofreq++; + goto freeit; + } +rate_limit_checked: + /* deliver the packet to appropriate sockets (unless proxying) */ + if (!proxy) { + icmp6_rip6_input(&m, *offp); + return IPPROTO_DONE; + } - freeit: +freeit: m_freem(m); return IPPROTO_DONE; } @@ -873,7 +961,7 @@ icmp6_notify_error(m, off, icmp6len, code) #ifndef PULLDOWN_TEST IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), - -1); + return -1); icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); #else IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, @@ -887,7 +975,7 @@ icmp6_notify_error(m, off, icmp6len, code) /* Detect the upper level protocol */ { - void (*ctlfunc) __P((int, struct sockaddr *, void *)); + void (*ctlfunc)(int, struct sockaddr *, void *); u_int8_t nxt = eip6->ip6_nxt; int eoff = off + sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr); @@ -909,7 +997,7 @@ icmp6_notify_error(m, off, icmp6len, code) #ifndef PULLDOWN_TEST IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(struct ip6_ext), - -1); + return -1); eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff); #else @@ -920,7 +1008,7 @@ icmp6_notify_error(m, off, icmp6len, code) return(-1); } #endif - + if (nxt == IPPROTO_AH) eoff += (eh->ip6e_len + 2) << 2; else @@ -938,7 +1026,7 @@ icmp6_notify_error(m, off, icmp6len, code) */ #ifndef PULLDOWN_TEST IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), - -1); + return -1); rth = (struct ip6_rthdr *)(mtod(m, caddr_t) + eoff); #else @@ -964,7 +1052,7 @@ icmp6_notify_error(m, off, icmp6len, code) #ifndef PULLDOWN_TEST IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, - -1); + return -1); rth0 = (struct ip6_rthdr0 *)(mtod(m, caddr_t) + eoff); #else IP6_EXTHDR_GET(rth0, @@ -978,7 +1066,7 @@ icmp6_notify_error(m, off, icmp6len, code) /* just ignore a bogus header */ if ((rth0->ip6r0_len % 2) == 0 && (hops = rth0->ip6r0_len/2)) - finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1); + finaldst = (struct in6_addr *)(void *)(rth0 + 1) + (hops - 1); } eoff += rthlen; nxt = rth->ip6r_nxt; @@ -987,7 +1075,7 @@ icmp6_notify_error(m, off, icmp6len, code) #ifndef PULLDOWN_TEST IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(struct ip6_frag), - -1); + return -1); fh = (struct ip6_frag *)(mtod(m, caddr_t) + eoff); #else @@ -995,7 +1083,7 @@ icmp6_notify_error(m, off, icmp6len, code) eoff, sizeof(*fh)); if (fh == NULL) { icmp6stat.icp6s_tooshort++; - return(-1); + return (-1); } #endif /* @@ -1027,14 +1115,23 @@ icmp6_notify_error(m, off, icmp6len, code) icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); #else IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, - sizeof(*icmp6) + sizeof(struct ip6_hdr)); + sizeof(*icmp6) + sizeof(struct ip6_hdr)); if (icmp6 == NULL) { icmp6stat.icp6s_tooshort++; - return(-1); + return (-1); } #endif + /* + * retrieve parameters from the inner IPv6 header, and convert + * them into sockaddr structures. + * XXX: there is no guarantee that the source or destination + * addresses of the inner packet are in the same scope as + * the addresses of the icmp packet. But there is no other + * way to determine the zone. + */ eip6 = (struct ip6_hdr *)(icmp6 + 1); + bzero(&icmp6dst, sizeof(icmp6dst)); icmp6dst.sin6_len = sizeof(struct sockaddr_in6); icmp6dst.sin6_family = AF_INET6; @@ -1042,39 +1139,16 @@ icmp6_notify_error(m, off, icmp6len, code) icmp6dst.sin6_addr = eip6->ip6_dst; else icmp6dst.sin6_addr = *finaldst; - icmp6dst.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif, - &icmp6dst.sin6_addr); -#ifndef SCOPEDROUTING - if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst, - NULL, NULL)) { - /* should be impossbile */ - nd6log((LOG_DEBUG, - "icmp6_notify_error: in6_embedscope failed\n")); + if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL)) goto freeit; - } -#endif - - /* - * retrieve parameters from the inner IPv6 header, and convert - * them into sockaddr structures. - */ bzero(&icmp6src, sizeof(icmp6src)); icmp6src.sin6_len = sizeof(struct sockaddr_in6); icmp6src.sin6_family = AF_INET6; icmp6src.sin6_addr = eip6->ip6_src; - icmp6src.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif, - &icmp6src.sin6_addr); -#ifndef SCOPEDROUTING - if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src, - NULL, NULL)) { - /* should be impossbile */ - nd6log((LOG_DEBUG, - "icmp6_notify_error: in6_embedscope failed\n")); + if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL)) goto freeit; - } -#endif icmp6src.sin6_flowinfo = - (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); + (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); if (finaldst == NULL) finaldst = &eip6->ip6_dst; @@ -1092,7 +1166,7 @@ icmp6_notify_error(m, off, icmp6len, code) icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ } - ctlfunc = (void (*) __P((int, struct sockaddr *, void *))) + ctlfunc = (void (*)(int, struct sockaddr *, void *)) (ip6_protox[nxt]->pr_ctlinput); if (ctlfunc) { (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, @@ -1101,7 +1175,7 @@ icmp6_notify_error(m, off, icmp6len, code) } return(0); - freeit: +freeit: m_freem(m); return(-1); } @@ -1117,10 +1191,25 @@ icmp6_mtudisc_update(ip6cp, validated) u_int mtu = ntohl(icmp6->icmp6_mtu); struct rtentry *rt = NULL; struct sockaddr_in6 sin6; + /* + * we reject ICMPv6 too big with abnormally small value. + * XXX what is the good definition of "abnormally small"? + */ + if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) + return; if (!validated) return; + /* + * In case the suggested mtu is less than IPV6_MMTU, we + * only need to remember that it was for above mentioned + * "alwaysfrag" case. + * Try to be as close to the spec as possible. + */ + if (mtu < IPV6_MMTU) + mtu = IPV6_MMTU - 8; + bzero(&sin6, sizeof(sin6)); sin6.sin6_family = PF_INET6; sin6.sin6_len = sizeof(struct sockaddr_in6); @@ -1131,28 +1220,26 @@ icmp6_mtudisc_update(ip6cp, validated) htons(m->m_pkthdr.rcvif->if_index); } /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */ - rt = rtalloc1((struct sockaddr *)&sin6, 0, - RTF_CLONING | RTF_PRCLONING); - - if (rt && (rt->rt_flags & RTF_HOST) - && !(rt->rt_rmx.rmx_locks & RTV_MTU)) { - if (mtu < IPV6_MMTU) { - /* xxx */ - rt->rt_rmx.rmx_locks |= RTV_MTU; - } else if (mtu < rt->rt_ifp->if_mtu && - rt->rt_rmx.rmx_mtu > mtu) { + rt = rtalloc1_scoped((struct sockaddr *)&sin6, 0, + RTF_CLONING | RTF_PRCLONING, m->m_pkthdr.rcvif->if_index); + if (rt != NULL) { + RT_LOCK(rt); + if ((rt->rt_flags & RTF_HOST) && + !(rt->rt_rmx.rmx_locks & RTV_MTU) && + mtu < IN6_LINKMTU(rt->rt_ifp) && + rt->rt_rmx.rmx_mtu > mtu) { icmp6stat.icp6s_pmtuchg++; rt->rt_rmx.rmx_mtu = mtu; } - } - if (rt) + RT_UNLOCK(rt); rtfree(rt); + } } /* * Process a Node Information Query packet, based on * draft-ietf-ipngwg-icmp-name-lookups-07. - * + * * Spec incompatibilities: * - IPv6 Subject address handling * - IPv4 Subject address handling support missing @@ -1179,7 +1266,6 @@ ni6_input(m, off) struct ip6_hdr *ip6; int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ char *subj = NULL; - struct in6_ifaddr *ia6 = NULL; ip6 = mtod(m, struct ip6_hdr *); #ifndef PULLDOWN_TEST @@ -1188,36 +1274,59 @@ ni6_input(m, off) IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); if (ni6 == NULL) { /* m is already reclaimed */ - return NULL; + return (NULL); } #endif + /* + * Validate IPv6 source address. + * The default configuration MUST be to refuse answering queries from + * global-scope addresses according to RFC4602. + * Notes: + * - it's not very clear what "refuse" means; this implementation + * simply drops it. + * - it's not very easy to identify global-scope (unicast) addresses + * since there are many prefixes for them. It should be safer + * and in practice sufficient to check "all" but loopback and + * link-local (note that site-local unicast was deprecated and + * ULA is defined as global scope-wise) + */ + if ((icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 && + !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) && + !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) + goto bad; + /* * Validate IPv6 destination address. * * The Responder must discard the Query without further processing * unless it is one of the Responder's unicast or anycast addresses, or * a link-local scope multicast address which the Responder has joined. - * [icmp-name-lookups-07, Section 4.] + * [RFC4602, Section 5.] */ - bzero(&sin6, sizeof(sin6)); - sin6.sin6_family = AF_INET6; - sin6.sin6_len = sizeof(struct sockaddr_in6); - bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr)); - /* XXX scopeid */ - if ((ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)&sin6)) != NULL) { - /* unicast/anycast, fine */ - if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && - (icmp6_nodeinfo & 4) == 0) { + if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { + if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) + goto bad; + /* else it's a link-local multicast, fine */ + } else { /* unicast or anycast */ + struct in6_ifaddr *ia6; + + if ((ia6 = ip6_getdstifaddr(m)) == NULL) + goto bad; /* XXX impossible */ + + IFA_LOCK(&ia6->ia_ifa); + if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) && + !(icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) { nd6log((LOG_DEBUG, "ni6_input: ignore node info to " "a temporary address in %s:%d", __FILE__, __LINE__)); + IFA_UNLOCK(&ia6->ia_ifa); + IFA_REMREF(&ia6->ia_ifa); goto bad; } - } else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) - ; /* link-local multicast, fine */ - else - goto bad; + IFA_UNLOCK(&ia6->ia_ifa); + IFA_REMREF(&ia6->ia_ifa); + } /* validate query Subject field. */ qtype = ntohs(ni6->ni_qtype); @@ -1231,6 +1340,7 @@ ni6_input(m, off) /* FALLTHROUGH */ case NI_QTYPE_FQDN: case NI_QTYPE_NODEADDR: + case NI_QTYPE_IPV4ADDR: switch (ni6->ni_code) { case ICMP6_NI_SUBJ_IPV6: #if ICMP6_NI_SUBJ_IPV6 != 0 @@ -1250,7 +1360,7 @@ ni6_input(m, off) goto bad; #endif - if (subjlen != sizeof(sin6.sin6_addr)) + if (subjlen != sizeof(struct in6_addr)) goto bad; /* @@ -1272,18 +1382,16 @@ ni6_input(m, off) subjlen, (caddr_t)&sin6.sin6_addr); sin6.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif, &sin6.sin6_addr); -#ifndef SCOPEDROUTING - in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL); -#endif + in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL, + NULL); bzero(&sin6_d, sizeof(sin6_d)); sin6_d.sin6_family = AF_INET6; /* not used, actually */ sin6_d.sin6_len = sizeof(sin6_d); /* ditto */ sin6_d.sin6_addr = ip6->ip6_dst; sin6_d.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_dst); -#ifndef SCOPEDROUTING - in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL); -#endif + in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL, + NULL); subj = (char *)&sin6; if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d)) break; @@ -1292,7 +1400,8 @@ ni6_input(m, off) * XXX if we are to allow other cases, we should really * be careful about scope here. * basically, we should disallow queries toward IPv6 - * destination X with subject Y, if scope(X) > scope(Y). + * destination X with subject Y, + * if scope(X) > scope(Y). * if we allow scope(X) > scope(Y), it will result in * information leakage across scope boundary. */ @@ -1335,11 +1444,12 @@ ni6_input(m, off) /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ switch (qtype) { case NI_QTYPE_FQDN: - if ((icmp6_nodeinfo & 1) == 0) + if ((icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0) goto bad; break; case NI_QTYPE_NODEADDR: - if ((icmp6_nodeinfo & 2) == 0) + case NI_QTYPE_IPV4ADDR: + if ((icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0) goto bad; break; } @@ -1356,15 +1466,18 @@ ni6_input(m, off) replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); break; case NI_QTYPE_NODEADDR: - addrs = ni6_addrs(ni6, m, &ifp, subj); + addrs = ni6_addrs(ni6, &ifp, subj); if ((replylen += addrs * (sizeof(struct in6_addr) + - sizeof(u_int32_t))) > MCLBYTES) + sizeof(u_int32_t))) > MCLBYTES) replylen = MCLBYTES; /* XXX: will truncate pkt later */ break; + case NI_QTYPE_IPV4ADDR: + /* unsupported - should respond with unknown Qtype? */ + break; default: /* * XXX: We must return a reply with the ICMP6 code - * `unknown Qtype' in this case. However we regard the case + * `unknown Qtype' in this case. However we regard the case * as an FQDN query for backward compatibility. * Older versions set a random value to this field, * so it rarely varies in the defined qtypes. @@ -1379,10 +1492,12 @@ ni6_input(m, off) } /* allocate an mbuf to reply. */ - MGETHDR(n, M_DONTWAIT, m->m_type); + MGETHDR(n, M_DONTWAIT, m->m_type); /* MAC-OK */ if (n == NULL) { m_freem(m); - return(NULL); + if (ifp != NULL) + ifnet_release(ifp); + return (NULL); } M_COPY_PKTHDR(n, m); /* just for recvif */ if (replylen > MHLEN) { @@ -1459,13 +1574,17 @@ ni6_input(m, off) nni6->ni_type = ICMP6_NI_REPLY; m_freem(m); - return(n); + if (ifp != NULL) + ifnet_release(ifp); + return (n); - bad: +bad: m_freem(m); if (n) m_freem(n); - return(NULL); + if (ifp != NULL) + ifnet_release(ifp); + return (NULL); } #undef hostnamelen @@ -1640,9 +1759,8 @@ ni6_dnsmatch(a, alen, b, blen) * calculate the number of addresses to be returned in the node info reply. */ static int -ni6_addrs(ni6, m, ifpp, subj) +ni6_addrs(ni6, ifpp, subj) struct icmp6_nodeinfo *ni6; - struct mbuf *m; struct ifnet **ifpp; char *subj; { @@ -1653,29 +1771,36 @@ ni6_addrs(ni6, m, ifpp, subj) int addrs = 0, addrsofif, iffound = 0; int niflags = ni6->ni_flags; + if (ifpp != NULL) + *ifpp = NULL; + if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { switch (ni6->ni_code) { case ICMP6_NI_SUBJ_IPV6: if (subj == NULL) /* must be impossible... */ return(0); - subj_ip6 = (struct sockaddr_in6 *)subj; + subj_ip6 = (struct sockaddr_in6 *)(void *)subj; break; default: /* * XXX: we only support IPv6 subject address for * this Qtype. */ - return(0); + return (0); } } - for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) - { + ifnet_head_lock_shared(); + TAILQ_FOREACH(ifp, &ifnet_head, if_list) { addrsofif = 0; + ifnet_lock_shared(ifp); TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { - if (ifa->ifa_addr->sa_family != AF_INET6) + IFA_LOCK(ifa); + if (ifa->ifa_addr->sa_family != AF_INET6) { + IFA_UNLOCK(ifa); continue; + } ifa6 = (struct in6_ifaddr *)ifa; if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && @@ -1696,18 +1821,25 @@ ni6_addrs(ni6, m, ifpp, subj) /* What do we have to do about ::1? */ switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { case IPV6_ADDR_SCOPE_LINKLOCAL: - if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) + if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) { + IFA_UNLOCK(ifa); continue; + } break; case IPV6_ADDR_SCOPE_SITELOCAL: - if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) + if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) { + IFA_UNLOCK(ifa); continue; + } break; case IPV6_ADDR_SCOPE_GLOBAL: - if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) + if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) { + IFA_UNLOCK(ifa); continue; + } break; default: + IFA_UNLOCK(ifa); continue; } @@ -1716,23 +1848,33 @@ ni6_addrs(ni6, m, ifpp, subj) * XXX: just experimental. not in the spec. */ if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && - (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) + (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) { + IFA_UNLOCK(ifa); continue; /* we need only unicast addresses */ + } if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && - (icmp6_nodeinfo & 4) == 0) { + (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { + IFA_UNLOCK(ifa); continue; } addrsofif++; /* count the address */ + IFA_UNLOCK(ifa); } + ifnet_lock_done(ifp); if (iffound) { - *ifpp = ifp; + if (ifpp != NULL) { + *ifpp = ifp; + ifnet_reference(ifp); + } + ifnet_head_done(); return(addrsofif); } addrs += addrsofif; } + ifnet_head_done(); - return(addrs); + return (addrs); } static int @@ -1741,7 +1883,7 @@ ni6_store_addrs(ni6, nni6, ifp0, resid) struct ifnet *ifp0; int resid; { - struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet); + struct ifnet *ifp = ifp0; struct in6_ifaddr *ifa6; struct ifaddr *ifa; struct ifnet *ifp_dep = NULL; @@ -1749,19 +1891,28 @@ ni6_store_addrs(ni6, nni6, ifp0, resid) u_char *cp = (u_char *)(nni6 + 1); int niflags = ni6->ni_flags; u_int32_t ltime; + struct timeval timenow; + + getmicrotime(&timenow); if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) - return(0); /* needless to copy */ + return (0); /* needless to copy */ again: - for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) - { + ifnet_head_lock_shared(); + if (ifp == NULL) + ifp = TAILQ_FIRST(&ifnet_head); + + for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) { + ifnet_lock_shared(ifp); for (ifa = ifp->if_addrlist.tqh_first; ifa; - ifa = ifa->ifa_list.tqe_next) - { - if (ifa->ifa_addr->sa_family != AF_INET6) + ifa = ifa->ifa_list.tqe_next) { + IFA_LOCK(ifa); + if (ifa->ifa_addr->sa_family != AF_INET6) { + IFA_UNLOCK(ifa); continue; + } ifa6 = (struct in6_ifaddr *)ifa; if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && @@ -1775,51 +1926,65 @@ ni6_store_addrs(ni6, nni6, ifp0, resid) if (ifp_dep == NULL) ifp_dep = ifp; + IFA_UNLOCK(ifa); continue; - } - else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && - allow_deprecated != 0) + } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && + allow_deprecated != 0) { + IFA_UNLOCK(ifa); continue; /* we now collect deprecated addrs */ - + } /* What do we have to do about ::1? */ switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { case IPV6_ADDR_SCOPE_LINKLOCAL: - if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) + if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) { + IFA_UNLOCK(ifa); continue; + } break; case IPV6_ADDR_SCOPE_SITELOCAL: - if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) + if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) { + IFA_UNLOCK(ifa); continue; + } break; case IPV6_ADDR_SCOPE_GLOBAL: - if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) + if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) { + IFA_UNLOCK(ifa); continue; + } break; default: + IFA_UNLOCK(ifa); continue; } /* * check if anycast is okay. - * XXX: just experimental. not in the spec. + * XXX: just experimental. not in the spec. */ if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && - (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) + (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) { + IFA_UNLOCK(ifa); continue; + } if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && - (icmp6_nodeinfo & 4) == 0) { + (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { + IFA_UNLOCK(ifa); continue; } /* now we can copy the address */ if (resid < sizeof(struct in6_addr) + sizeof(u_int32_t)) { + IFA_UNLOCK(ifa); /* * We give up much more copy. * Set the truncate flag and return. */ nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE; + ifnet_lock_done(ifp); + ifnet_head_done(); return(copied); } @@ -1838,16 +2003,17 @@ ni6_store_addrs(ni6, nni6, ifp0, resid) * address configuration by DHCPv6, so the former * case can't happen. */ - if (ifa6->ia6_lifetime.ia6t_expire == 0) + if (ifa6->ia6_lifetime.ia6t_expire == 0 && + (ifa6->ia6_flags & IN6_IFF_TEMPORARY) == 0) ltime = ND6_INFINITE_LIFETIME; else { if (ifa6->ia6_lifetime.ia6t_expire > - time_second) - ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second); + timenow.tv_sec) + ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - timenow.tv_sec); else ltime = 0; } - + bcopy(<ime, cp, sizeof(u_int32_t)); cp += sizeof(u_int32_t); @@ -1856,16 +2022,19 @@ ni6_store_addrs(ni6, nni6, ifp0, resid) sizeof(struct in6_addr)); /* XXX: KAME link-local hack; remove ifindex */ if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr)) - ((struct in6_addr *)cp)->s6_addr16[1] = 0; + ((struct in6_addr *)(void *)cp)->s6_addr16[1] = 0; cp += sizeof(struct in6_addr); - + resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); copied += (sizeof(struct in6_addr) + sizeof(u_int32_t)); + IFA_UNLOCK(ifa); } + ifnet_lock_done(ifp); if (ifp0) /* we need search only on the specified IF */ break; } + ifnet_head_done(); if (allow_deprecated == 0 && ifp_dep != NULL) { ifp = ifp_dep; @@ -1892,6 +2061,7 @@ icmp6_rip6_input(mp, off) struct sockaddr_in6 rip6src; struct icmp6_hdr *icmp6; struct mbuf *opts = NULL; + int ret = 0; #ifndef PULLDOWN_TEST /* this is assumed to be safe. */ @@ -1904,20 +2074,22 @@ icmp6_rip6_input(mp, off) } #endif + /* + * XXX: the address may have embedded scope zone ID, which should be + * hidden from applications. + */ bzero(&rip6src, sizeof(rip6src)); - rip6src.sin6_len = sizeof(struct sockaddr_in6); rip6src.sin6_family = AF_INET6; - /* KAME hack: recover scopeid */ - (void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif); + rip6src.sin6_len = sizeof(struct sockaddr_in6); + rip6src.sin6_addr = ip6->ip6_src; + if (sa6_recoverscope(&rip6src, TRUE)) + return (IPPROTO_DONE); + lck_rw_lock_shared(ripcbinfo.mtx); LIST_FOREACH(in6p, &ripcb, inp_list) { if ((in6p->inp_vflag & INP_IPV6) == 0) continue; -#if HAVE_NRL_INPCB - if (!(in6p->in6p_flags & INP_IPV6)) - continue; -#endif if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6) continue; if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && @@ -1933,41 +2105,57 @@ icmp6_rip6_input(mp, off) if (last) { struct mbuf *n; if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { - if (last->in6p_flags & IN6P_CONTROLOPTS) - ip6_savecontrol(last, &opts, ip6, n); + if ((last->in6p_flags & IN6P_CONTROLOPTS) != 0 || + (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 || + (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) { + ret = ip6_savecontrol(last, n, &opts); + if (ret != 0) { + m_freem(n); + m_freem(opts); + last = in6p; + continue; + } + } /* strip intermediate headers */ m_adj(n, off); + so_recv_data_stat(last->in6p_socket, m, 0); if (sbappendaddr(&last->in6p_socket->so_rcv, (struct sockaddr *)&rip6src, - n, opts) == 0) { - /* should notify about lost packet */ - m_freem(n); - if (opts) { - m_freem(opts); - } - } else + n, opts, NULL) != 0) { sorwakeup(last->in6p_socket); + } opts = NULL; } } last = in6p; } if (last) { - if (last->in6p_flags & IN6P_CONTROLOPTS) - ip6_savecontrol(last, &opts, ip6, m); + if ((last->in6p_flags & INP_CONTROLOPTS) != 0 || + (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 || + (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) { + ret = ip6_savecontrol(last, m, &opts); + if (ret != 0) { + goto error; + } + } /* strip intermediate headers */ m_adj(m, off); + so_recv_data_stat(last->in6p_socket, m, 0); if (sbappendaddr(&last->in6p_socket->so_rcv, - (struct sockaddr *)&rip6src, m, opts) == 0) { - m_freem(m); - if (opts) - m_freem(opts); - } else + (struct sockaddr *)&rip6src, m, opts, NULL) != 0) { sorwakeup(last->in6p_socket); + } } else { - m_freem(m); - ip6stat.ip6s_delivered--; + goto error; } + lck_rw_done(ripcbinfo.mtx); + return IPPROTO_DONE; + +error: + lck_rw_done(ripcbinfo.mtx); + m_freem(m); + m_freem(opts); + ip6stat.ip6s_delivered--; return IPPROTO_DONE; } @@ -1983,21 +2171,26 @@ icmp6_reflect(m, off) struct ip6_hdr *ip6; struct icmp6_hdr *icmp6; struct in6_ifaddr *ia; - struct in6_addr t, *src = 0; + struct in6_addr t, src_storage, *src = 0; int plen; int type, code; struct ifnet *outif = NULL; struct sockaddr_in6 sa6_src, sa6_dst; -#ifdef COMPAT_RFC1885 - int mtu = IPV6_MMTU; - struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst; -#endif + struct nd_ifinfo *ndi; + u_int32_t oflow; + struct ip6_out_args ip6oa = + { IFSCOPE_NONE, { 0 }, IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR }; + + if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.rcvif != NULL) { + ip6oa.ip6oa_boundif = m->m_pkthdr.rcvif->if_index; + ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF; + } /* too short to reflect */ if (off < sizeof(struct ip6_hdr)) { nd6log((LOG_DEBUG, "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", - (u_long)off, (u_long)sizeof(struct ip6_hdr), + (u_int32_t)off, (u_int32_t)sizeof(struct ip6_hdr), __FILE__, __LINE__)); goto bad; } @@ -2049,68 +2242,42 @@ icmp6_reflect(m, off) * XXX: make sure to embed scope zone information, using * already embedded IDs or the received interface (if any). * Note that rcvif may be NULL. - * TODO: scoped routing case (XXX). */ bzero(&sa6_src, sizeof(sa6_src)); sa6_src.sin6_family = AF_INET6; sa6_src.sin6_len = sizeof(sa6_src); sa6_src.sin6_addr = ip6->ip6_dst; in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif); - in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL); + in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL, NULL); bzero(&sa6_dst, sizeof(sa6_dst)); sa6_dst.sin6_family = AF_INET6; sa6_dst.sin6_len = sizeof(sa6_dst); sa6_dst.sin6_addr = t; in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif); - in6_embedscope(&t, &sa6_dst, NULL, NULL); + in6_embedscope(&t, &sa6_dst, NULL, NULL, NULL); -#ifdef COMPAT_RFC1885 - /* - * xxx guess MTU - * RFC 1885 requires that echo reply should be truncated if it - * does not fit in with (return) path MTU, but the description was - * removed in the new spec. - */ - if (icmp6_reflect_rt.ro_rt == 0 || - ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) { - if (icmp6_reflect_rt.ro_rt) { - rtfree(icmp6_reflect_rt.ro_rt); - icmp6_reflect_rt.ro_rt = 0; - } - bzero(sin6, sizeof(*sin6)); - sin6->sin6_family = PF_INET6; - sin6->sin6_len = sizeof(struct sockaddr_in6); - sin6->sin6_addr = ip6->ip6_dst; - - rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt, - RTF_PRCLONING); - } - - if (icmp6_reflect_rt.ro_rt == 0) - goto bad; - - if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST) - && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu) - mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu; - - if (mtu < m->m_pkthdr.len) { - plen -= (m->m_pkthdr.len - mtu); - m_adj(m, mtu - m->m_pkthdr.len); - } -#endif /* * If the incoming packet was addressed directly to us(i.e. unicast), * use dst as the src for the reply. - * The IN6_IFF_NOTREADY case would be VERY rare, but is possible + * The IN6_IFF_NOTREADY case should be VERY rare, but is possible * (for example) when we encounter an error while forwarding procedure * destined to a duplicated address of ours. + * Note that ip6_getdstifaddr() may fail if we are in an error handling + * procedure of an outgoing packet of our own, in which case we need + * to search in the ifaddr list. */ - for (ia = in6_ifaddr; ia; ia = ia->ia_next) + lck_rw_lock_shared(&in6_ifaddr_rwlock); + for (ia = in6_ifaddrs; ia; ia = ia->ia_next) { + IFA_LOCK(&ia->ia_ifa); if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) && (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) { + IFA_UNLOCK(&ia->ia_ifa); src = &t; break; } + IFA_UNLOCK(&ia->ia_ifa); + } + lck_rw_done(&in6_ifaddr_rwlock); if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) { /* * This is the case if the dst is our link-local address @@ -2119,8 +2286,9 @@ icmp6_reflect(m, off) src = &t; } - if (src == 0) { + if (src == NULL) { int e; + struct sockaddr_in6 sin6; struct route_in6 ro; /* @@ -2128,8 +2296,19 @@ icmp6_reflect(m, off) * that we do not own. Select a source address based on the * source address of the erroneous packet. */ + bzero(&sin6, sizeof(sin6)); + sin6.sin6_family = AF_INET6; + sin6.sin6_len = sizeof(sin6); + sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */ + bzero(&ro, sizeof(ro)); - src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e); + /* + * in6_selectsrc() might return outif with its reference held + * even in the error case, so we always need to release it + * if non-NULL. + */ + src = in6_selectsrc(&sin6, NULL, NULL, &ro, &outif, + &src_storage, ip6oa.ip6oa_boundif, &e); if (ro.ro_rt) rtfree(ro.ro_rt); /* XXX: we could use this */ if (src == NULL) { @@ -2140,19 +2319,34 @@ icmp6_reflect(m, off) goto bad; } } - + oflow = ip6->ip6_flow; /* Save for later */ ip6->ip6_src = *src; - ip6->ip6_flow = 0; ip6->ip6_vfc &= ~IPV6_VERSION_MASK; ip6->ip6_vfc |= IPV6_VERSION; + if (icmp6->icmp6_type == ICMP6_ECHO_REPLY && icmp6->icmp6_code == 0) { + ip6->ip6_flow |= (oflow & htonl(0x0ff00000)); + } ip6->ip6_nxt = IPPROTO_ICMPV6; - if (m->m_pkthdr.rcvif) { + lck_rw_lock_shared(nd_if_rwlock); + if (outif != NULL && (ndi = ND_IFINFO(outif)) != NULL && + ndi->initialized) { + lck_mtx_lock(&ndi->lock); + ip6->ip6_hlim = ndi->chlim; + lck_mtx_unlock(&ndi->lock); + } + if (m->m_pkthdr.rcvif != NULL && + (ndi = ND_IFINFO(m->m_pkthdr.rcvif)) != NULL && + ndi->initialized) { /* XXX: This may not be the outgoing interface */ - ip6->ip6_hlim = nd_ifinfo[m->m_pkthdr.rcvif->if_index].chlim; - } else + lck_mtx_lock(&ndi->lock); + ip6->ip6_hlim = ndi->chlim; + lck_mtx_unlock(&ndi->lock); + } else { ip6->ip6_hlim = ip6_defhlim; - + } + lck_rw_done(nd_if_rwlock); + /* Use the same traffic class as in the request to match IPv4 */ icmp6->icmp6_cksum = 0; icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), plen); @@ -2168,28 +2362,27 @@ icmp6_reflect(m, off) (void)ipsec_setsocket(m, NULL); #endif /*IPSEC*/ -#ifdef COMPAT_RFC1885 - ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif); -#else - ip6_output(m, NULL, NULL, 0, NULL, &outif); -#endif - if (outif) + if (outif != NULL) { + ifnet_release(outif); + outif = NULL; + } + m->m_pkthdr.rcvif = NULL; + m->m_pkthdr.csum_data = 0; + m->m_pkthdr.csum_flags = 0; + ip6_output(m, NULL, NULL, IPV6_OUTARGS, NULL, &outif, &ip6oa); + if (outif != NULL) { icmp6_ifoutstat_inc(outif, type, code); - + ifnet_release(outif); + } return; - bad: +bad: m_freem(m); + if (outif != NULL) + ifnet_release(outif); return; } -void -icmp6_fasttimo() -{ - - mld6_fasttimeo(); -} - static const char * icmp6_redirect_diag(src6, dst6, tgt6) struct in6_addr *src6; @@ -2226,14 +2419,17 @@ icmp6_redirect_input(m, off) if (!m || !ifp) return; - /* XXX if we are router, we don't update route by icmp6 redirect */ - if (ip6_forwarding) + /* + * If we are an advertising router on this interface, + * don't update route by icmp6 redirect. + */ + if (ifp->if_eflags & IFEF_IPV6_ROUTER) goto freeit; if (!icmp6_rediraccept) goto freeit; #ifndef PULLDOWN_TEST - IP6_EXTHDR_CHECK(m, off, icmp6len,); + IP6_EXTHDR_CHECK(m, off, icmp6len, return); nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); #else IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); @@ -2245,10 +2441,10 @@ icmp6_redirect_input(m, off) redtgt6 = nd_rd->nd_rd_target; reddst6 = nd_rd->nd_rd_dst; - if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) - redtgt6.s6_addr16[1] = htons(ifp->if_index); - if (IN6_IS_ADDR_LINKLOCAL(&reddst6)) - reddst6.s6_addr16[1] = htons(ifp->if_index); + if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) || + in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) { + goto freeit; + } /* validation */ if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { @@ -2273,19 +2469,22 @@ icmp6_redirect_input(m, off) sin6.sin6_family = AF_INET6; sin6.sin6_len = sizeof(struct sockaddr_in6); bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); - rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); + rt = rtalloc1_scoped((struct sockaddr *)&sin6, 0, 0, ifp->if_index); if (rt) { + RT_LOCK(rt); if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6) { nd6log((LOG_ERR, "ICMP6 redirect rejected; no route " "with inet6 gateway found for redirect dst: %s\n", icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); + RT_UNLOCK(rt); rtfree(rt); goto bad; } - gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); + gw6 = &(((struct sockaddr_in6 *)(void *) + rt->rt_gateway)->sin6_addr); if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { nd6log((LOG_ERR, "ICMP6 redirect rejected; " @@ -2293,6 +2492,7 @@ icmp6_redirect_input(m, off) "%s\n", ip6_sprintf(gw6), icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); + RT_UNLOCK(rt); rtfree(rt); goto bad; } @@ -2303,6 +2503,7 @@ icmp6_redirect_input(m, off) icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); goto bad; } + RT_UNLOCK(rt); rtfree(rt); rt = NULL; } @@ -2376,10 +2577,9 @@ icmp6_redirect_input(m, off) bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); - rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw, - (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST, - (struct sockaddr *)&ssrc, - (struct rtentry **)NULL); + rtredirect(ifp, (struct sockaddr *)&sdst, + (struct sockaddr *)&sgw, NULL, RTF_GATEWAY | RTF_HOST, + (struct sockaddr *)&ssrc, NULL); } /* finally update cached route in each socket via pfctlinput */ { @@ -2389,7 +2589,16 @@ icmp6_redirect_input(m, off) sdst.sin6_family = AF_INET6; sdst.sin6_len = sizeof(struct sockaddr_in6); bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); + + /* + * Radar 6843900 + * Release the IPv6 domain lock because we are going to take domain_proto_mtx + * and could otherwise cause a deadlock with other threads taking these locks + * in the reverse order -- e.g. frag6_slowtimo() from pfslowtimo() + */ + lck_mtx_unlock(inet6_domain_mutex); pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); + lck_mtx_lock(inet6_domain_mutex); #if IPSEC key_sa_routechange((struct sockaddr *)&sdst); #endif @@ -2410,7 +2619,7 @@ icmp6_redirect_output(m0, rt) struct rtentry *rt; { struct ifnet *ifp; /* my outgoing interface */ - struct in6_addr *ifp_ll6; + struct in6_addr ifp_ll6; struct in6_addr *router_ll6; struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ struct mbuf *m = NULL; /* newly allocated one */ @@ -2420,15 +2629,23 @@ icmp6_redirect_output(m0, rt) u_char *p; struct ifnet *outif = NULL; struct sockaddr_in6 src_sa; + struct ip6_out_args ip6oa = + { IFSCOPE_NONE, { 0 }, IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR }; icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0); + if (rt != NULL) + RT_LOCK(rt); + /* sanity check */ if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) goto fail; - /* if we are not router, we don't send icmp6 redirect */ - if (!ip6_forwarding || ip6_accept_rtadv || (ifp->if_eflags & IFEF_ACCEPT_RTADVD)) + /* + * If we are not a router to begin with, or not an advertising + * router on this interface, don't send icmp6 redirect. + */ + if (!ip6_forwarding || !(ifp->if_eflags & IFEF_IPV6_ROUTER)) goto fail; /* @@ -2444,8 +2661,13 @@ icmp6_redirect_output(m0, rt) src_sa.sin6_addr = sip6->ip6_src; /* we don't currently use sin6_scope_id, but eventually use it */ src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src); - if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) + RT_UNLOCK(rt); + if (nd6_is_addr_neighbor(&src_sa, ifp, 0) == 0) { + /* already unlocked */ + rt = NULL; goto fail; + } + RT_LOCK(rt); if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) goto fail; /* what should we do here? */ @@ -2461,7 +2683,7 @@ icmp6_redirect_output(m0, rt) #if IPV6_MMTU >= MCLBYTES # error assumption failed about IPV6_MMTU and MCLBYTES #endif - MGETHDR(m, M_DONTWAIT, MT_HEADER); + MGETHDR(m, M_DONTWAIT, MT_HEADER); /* MAC-OK */ if (m && IPV6_MMTU >= MHLEN) MCLGET(m, M_DONTWAIT); if (!m) @@ -2483,13 +2705,16 @@ icmp6_redirect_output(m0, rt) IN6_IFF_NOTREADY| IN6_IFF_ANYCAST)) == NULL) goto fail; - ifp_ll6 = &ia->ia_addr.sin6_addr; + IFA_LOCK(&ia->ia_ifa); + ifp_ll6 = ia->ia_addr.sin6_addr; + IFA_UNLOCK(&ia->ia_ifa); + IFA_REMREF(&ia->ia_ifa); } /* get ip6 linklocal address for the router. */ if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { struct sockaddr_in6 *sin6; - sin6 = (struct sockaddr_in6 *)rt->rt_gateway; + sin6 = (struct sockaddr_in6 *)(void *)rt->rt_gateway; router_ll6 = &sin6->sin6_addr; if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) router_ll6 = (struct in6_addr *)NULL; @@ -2505,7 +2730,7 @@ icmp6_redirect_output(m0, rt) ip6->ip6_nxt = IPPROTO_ICMPV6; ip6->ip6_hlim = 255; /* ip6->ip6_src must be linklocal addr for my outgoing if. */ - bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); + bcopy(&ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); /* ND Redirect */ @@ -2531,41 +2756,52 @@ icmp6_redirect_output(m0, rt) bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, sizeof(nd_rd->nd_rd_dst)); } + RT_UNLOCK(rt); + rt = NULL; p = (u_char *)(nd_rd + 1); if (!router_ll6) goto nolladdropt; - { - /* target lladdr option */ - struct rtentry *rt_router = NULL; - int len; - struct sockaddr_dl *sdl; - struct nd_opt_hdr *nd_opt; - char *lladdr; + { + /* target lladdr option */ + struct rtentry *rt_router = NULL; + int len; + struct sockaddr_dl *sdl; + struct nd_opt_hdr *nd_opt; + char *lladdr; + + /* Callee returns a locked route upon success */ + rt_router = nd6_lookup(router_ll6, 0, ifp, 0); + if (!rt_router) + goto nolladdropt; + RT_LOCK_ASSERT_HELD(rt_router); + len = sizeof(*nd_opt) + ifp->if_addrlen; + len = (len + 7) & ~7; /* round by 8 */ + /* safety check */ + if (len + (p - (u_char *)ip6) > maxlen) { + RT_REMREF_LOCKED(rt_router); + RT_UNLOCK(rt_router); + goto nolladdropt; + } - rt_router = nd6_lookup(router_ll6, 0, ifp); - if (!rt_router) - goto nolladdropt; - len = sizeof(*nd_opt) + ifp->if_addrlen; - len = (len + 7) & ~7; /* round by 8 */ - /* safety check */ - if (len + (p - (u_char *)ip6) > maxlen) - goto nolladdropt; - if (!(rt_router->rt_flags & RTF_GATEWAY) && - (rt_router->rt_flags & RTF_LLINFO) && - (rt_router->rt_gateway->sa_family == AF_LINK) && - (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) && - sdl->sdl_alen) { - nd_opt = (struct nd_opt_hdr *)p; - nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; - nd_opt->nd_opt_len = len >> 3; - lladdr = (char *)(nd_opt + 1); - bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen); - p += len; + if (!(rt_router->rt_flags & RTF_GATEWAY) && + (rt_router->rt_flags & RTF_LLINFO) && + (rt_router->rt_gateway->sa_family == AF_LINK) && + (sdl = (struct sockaddr_dl *)(void *) + rt_router->rt_gateway) && sdl->sdl_alen) { + nd_opt = (struct nd_opt_hdr *)p; + nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; + nd_opt->nd_opt_len = len >> 3; + lladdr = (char *)(nd_opt + 1); + bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen); + p += len; + } + RT_REMREF_LOCKED(rt_router); + RT_UNLOCK(rt_router); } - } + nolladdropt:; m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; @@ -2649,20 +2885,11 @@ nolladdropt:; } noredhdropt:; - if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src)) - sip6->ip6_src.s6_addr16[1] = 0; - if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst)) - sip6->ip6_dst.s6_addr16[1] = 0; -#if 0 - if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) - ip6->ip6_src.s6_addr16[1] = 0; - if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) - ip6->ip6_dst.s6_addr16[1] = 0; -#endif - if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target)) - nd_rd->nd_rd_target.s6_addr16[1] = 0; - if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst)) - nd_rd->nd_rd_dst.s6_addr16[1] = 0; + /* XXX: clear embedded link IDs in the inner header */ + in6_clearscope(&sip6->ip6_src); + in6_clearscope(&sip6->ip6_dst); + in6_clearscope(&nd_rd->nd_rd_target); + in6_clearscope(&nd_rd->nd_rd_dst); ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); @@ -2676,27 +2903,29 @@ noredhdropt:; if (ipsec_bypass == 0) (void)ipsec_setsocket(m, NULL); #endif /*IPSEC*/ - ip6_output(m, NULL, NULL, 0, NULL, &outif); + + ip6oa.ip6oa_boundif = ifp->if_index; + ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF; + + ip6_output(m, NULL, NULL, IPV6_OUTARGS, NULL, &outif, &ip6oa); if (outif) { icmp6_ifstat_inc(outif, ifs6_out_msg); icmp6_ifstat_inc(outif, ifs6_out_redirect); + ifnet_release(outif); } icmp6stat.icp6s_outhist[ND_REDIRECT]++; return; fail: + if (rt != NULL) + RT_UNLOCK(rt); if (m) m_freem(m); if (m0) m_freem(m0); } -#if HAVE_NRL_INPCB -#define sotoin6pcb sotoinpcb -#define in6pcb inpcb -#define in6p_icmp6filt inp_icmp6filt -#endif /* * ICMPv6 socket option processing. */ @@ -2729,7 +2958,7 @@ icmp6_ctloutput(so, sopt) { struct icmp6_filter *p; - if (optlen != sizeof(*p)) { + if (optlen != 0 && optlen != sizeof(*p)) { error = EMSGSIZE; break; } @@ -2737,8 +2966,17 @@ icmp6_ctloutput(so, sopt) error = EINVAL; break; } - error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen, - optlen); + + if (optlen == 0) { + /* According to RFC 3542, an installed filter can be + * cleared by issuing a setsockopt for ICMP6_FILTER + * with a zero length. + */ + ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); + } else { + error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen, + optlen); + } break; } @@ -2757,7 +2995,7 @@ icmp6_ctloutput(so, sopt) break; } error = sooptcopyout(sopt, inp->in6p_icmp6filt, - sizeof(struct icmp6_filter)); + min(sizeof(struct icmp6_filter), optlen)); break; } @@ -2770,12 +3008,178 @@ icmp6_ctloutput(so, sopt) return(error); } -#if HAVE_NRL_INPCB -#undef sotoin6pcb -#undef in6pcb -#undef in6p_icmp6filt + +/* + * ICMPv6 socket datagram option processing. + */ +int +icmp6_dgram_ctloutput(struct socket *so, struct sockopt *sopt) +{ + if (kauth_cred_issuser(so->so_cred)) + return icmp6_ctloutput(so, sopt); + + if (sopt->sopt_level == IPPROTO_ICMPV6) { + switch (sopt->sopt_name) { + case ICMP6_FILTER: + return icmp6_ctloutput(so, sopt); + default: + return EPERM; + } + } + + if (sopt->sopt_level != IPPROTO_IPV6) + return EINVAL; + + switch (sopt->sopt_name) { + case IPV6_UNICAST_HOPS: + case IPV6_CHECKSUM: + case IPV6_V6ONLY: + case IPV6_USE_MIN_MTU: + case IPV6_RECVRTHDR: + case IPV6_RECVPKTINFO: + case IPV6_RECVHOPLIMIT: + case IPV6_PATHMTU: + case IPV6_PKTINFO: + case IPV6_HOPLIMIT: + case IPV6_HOPOPTS: + case IPV6_DSTOPTS: + case IPV6_MULTICAST_IF: + case IPV6_MULTICAST_HOPS: + case IPV6_MULTICAST_LOOP: + case IPV6_JOIN_GROUP: + case IPV6_LEAVE_GROUP: + case IPV6_PORTRANGE: + case IPV6_IPSEC_POLICY: + case IPV6_RECVTCLASS: + case IPV6_TCLASS: + case IPV6_2292PKTOPTIONS: + case IPV6_2292PKTINFO: + case IPV6_2292HOPLIMIT: + case IPV6_2292HOPOPTS: + case IPV6_2292DSTOPTS: + case IPV6_2292RTHDR: + case IPV6_BOUND_IF: + case IPV6_NO_IFT_CELLULAR: + + return ip6_ctloutput(so, sopt); + + default: + return EPERM; + } +} + +__private_extern__ int +icmp6_dgram_send(struct socket *so, int flags, struct mbuf *m, + struct sockaddr *nam, struct mbuf *control, struct proc *p) +{ +#pragma unused(flags, p) + int error = 0; + struct inpcb *inp = sotoinpcb(so); + struct sockaddr_in6 tmp; + struct sockaddr_in6 *dst = (struct sockaddr_in6 *)(void *)nam; + struct icmp6_hdr *icmp6; + + if (kauth_cred_issuser(so->so_cred)) + return rip6_output(m, so, (struct sockaddr_in6 *)(void *)nam, + control, 0); + + /* always copy sockaddr to avoid overwrites */ + if (so->so_state & SS_ISCONNECTED) { + if (nam) { + m_freem(m); + return EISCONN; + } + /* XXX */ + bzero(&tmp, sizeof(tmp)); + tmp.sin6_family = AF_INET6; + tmp.sin6_len = sizeof(struct sockaddr_in6); + bcopy(&inp->in6p_faddr, &tmp.sin6_addr, + sizeof(struct in6_addr)); + dst = &tmp; + } else { + if (nam == NULL) { + m_freem(m); + return ENOTCONN; + } + tmp = *(struct sockaddr_in6 *)(void *)nam; + dst = &tmp; + } + + /* + * For an ICMPv6 packet, we should know its type and code + */ + if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { + if (m->m_len < sizeof(struct icmp6_hdr) && + (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { + error = ENOBUFS; + goto bad; + } + icmp6 = mtod(m, struct icmp6_hdr *); + + /* + * Allow only to send echo request and node information request + * See RFC 2463 for Echo Request Message format + */ + if ((icmp6->icmp6_type == ICMP6_ECHO_REQUEST && icmp6->icmp6_code == 0) || + (icmp6->icmp6_type == ICMP6_NI_QUERY && + (icmp6->icmp6_code == ICMP6_NI_SUBJ_IPV6 || + icmp6->icmp6_code == ICMP6_NI_SUBJ_FQDN))) { + /* Good */ + ; + } else { + error = EPERM; + goto bad; + } + } + +#if ENABLE_DEFAULT_SCOPE + if (dst->sin6_scope_id == 0) { /* not change if specified */ + dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr); + } #endif + return rip6_output(m, so, (struct sockaddr_in6 *)(void *)nam, + control, 0); +bad: + m_freem(m); + return error; +} + +/* Like rip6_attach but without root privilege enforcement */ +__private_extern__ int +icmp6_dgram_attach(struct socket *so, int proto, struct proc *p) +{ + struct inpcb *inp; + int error; + + inp = sotoinpcb(so); + if (inp) + panic("icmp6_dgram_attach"); + + if (proto != IPPROTO_ICMPV6) + return EINVAL; + + error = soreserve(so, rip_sendspace, rip_recvspace); + if (error) + return error; + error = in_pcballoc(so, &ripcbinfo, p); + if (error) + return error; + inp = (struct inpcb *)so->so_pcb; + inp->inp_vflag |= INP_IPV6; + inp->in6p_ip6_nxt = IPPROTO_ICMPV6; + inp->in6p_hops = -1; /* use kernel default */ + inp->in6p_cksum = -1; + MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *, + sizeof(struct icmp6_filter), M_PCB, M_WAITOK); + if (inp->in6p_icmp6filt == NULL) + return (ENOMEM); + ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); + return 0; +} + + + #ifndef HAVE_PPSRATECHECK #ifndef timersub #define timersub(tvp, uvp, vvp) \ @@ -2799,11 +3203,9 @@ ppsratecheck(lasttime, curpps, maxpps) int maxpps; /* maximum pps allowed */ { struct timeval tv, delta; - int s, rv; + int rv; - s = splclock(); microtime(&tv); - splx(s); timersub(&tv, lasttime, &delta); @@ -2829,7 +3231,7 @@ ppsratecheck(lasttime, curpps, maxpps) #if 1 /* DIAGNOSTIC? */ /* be careful about wrap-around */ - if (*curpps + 1 > *curpps) + if (*curpps + 1 > 0) *curpps = *curpps + 1; #else /* @@ -2855,10 +3257,10 @@ ppsratecheck(lasttime, curpps, maxpps) * XXX per-destination/type check necessary? */ static int -icmp6_ratelimit(dst, type, code) - const struct in6_addr *dst; /* not used at this moment */ - const int type; /* not used at this moment */ - const int code; /* not used at this moment */ +icmp6_ratelimit( + __unused const struct in6_addr *dst, /* not used at this moment */ + __unused const int type, /* not used at this moment */ + __unused const int code) /* not used at this moment */ { int ret;