icmp6.c revision 191672
1139826Simp/*- 253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 353541Sshin * All rights reserved. 453541Sshin * 553541Sshin * Redistribution and use in source and binary forms, with or without 653541Sshin * modification, are permitted provided that the following conditions 753541Sshin * are met: 853541Sshin * 1. Redistributions of source code must retain the above copyright 953541Sshin * notice, this list of conditions and the following disclaimer. 1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1153541Sshin * notice, this list of conditions and the following disclaimer in the 1253541Sshin * documentation and/or other materials provided with the distribution. 1353541Sshin * 3. Neither the name of the project nor the names of its contributors 1453541Sshin * may be used to endorse or promote products derived from this software 1553541Sshin * without specific prior written permission. 1653541Sshin * 1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2053541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2753541Sshin * SUCH DAMAGE. 28174510Sobrien * 29174510Sobrien * $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ 3053541Sshin */ 3153541Sshin 32139826Simp/*- 3353541Sshin * Copyright (c) 1982, 1986, 1988, 1993 3453541Sshin * The Regents of the University of California. All rights reserved. 3553541Sshin * 3653541Sshin * Redistribution and use in source and binary forms, with or without 3753541Sshin * modification, are permitted provided that the following conditions 3853541Sshin * are met: 3953541Sshin * 1. Redistributions of source code must retain the above copyright 4053541Sshin * notice, this list of conditions and the following disclaimer. 4153541Sshin * 2. Redistributions in binary form must reproduce the above copyright 4253541Sshin * notice, this list of conditions and the following disclaimer in the 4353541Sshin * documentation and/or other materials provided with the distribution. 4453541Sshin * 4. Neither the name of the University nor the names of its contributors 4553541Sshin * may be used to endorse or promote products derived from this software 4653541Sshin * without specific prior written permission. 4753541Sshin * 4853541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 4953541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5053541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5153541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5253541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5353541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5453541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5553541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5653541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5753541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5853541Sshin * SUCH DAMAGE. 5953541Sshin * 6053541Sshin * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 6153541Sshin */ 6253541Sshin 63174510Sobrien#include <sys/cdefs.h> 64174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/icmp6.c 191672 2009-04-29 19:19:13Z bms $"); 65174510Sobrien 6662587Sitojun#include "opt_inet.h" 6762587Sitojun#include "opt_inet6.h" 6855009Sshin#include "opt_ipsec.h" 69189106Sbz#include "opt_route.h" 7053541Sshin 7153541Sshin#include <sys/param.h> 7295759Stanimura#include <sys/domain.h> 7395759Stanimura#include <sys/kernel.h> 7495759Stanimura#include <sys/lock.h> 7578064Sume#include <sys/malloc.h> 7653541Sshin#include <sys/mbuf.h> 7753541Sshin#include <sys/protosw.h> 7895759Stanimura#include <sys/signalvar.h> 7953541Sshin#include <sys/socket.h> 8053541Sshin#include <sys/socketvar.h> 8195759Stanimura#include <sys/sx.h> 8295759Stanimura#include <sys/syslog.h> 8395759Stanimura#include <sys/systm.h> 8453541Sshin#include <sys/time.h> 85181803Sbz#include <sys/vimage.h> 8653541Sshin 8753541Sshin#include <net/if.h> 8853541Sshin#include <net/if_dl.h> 89186119Sqingli#include <net/if_llatbl.h> 9053541Sshin#include <net/if_types.h> 9195759Stanimura#include <net/route.h> 92185571Sbz#include <net/vnet.h> 9353541Sshin 9453541Sshin#include <netinet/in.h> 9595759Stanimura#include <netinet/in_pcb.h> 9653541Sshin#include <netinet/in_var.h> 9762587Sitojun#include <netinet/ip6.h> 9895759Stanimura#include <netinet/icmp6.h> 99122922Sandre#include <netinet/tcp_var.h> 100185571Sbz#include <netinet/vinet.h> 101185571Sbz 10295759Stanimura#include <netinet6/in6_ifattach.h> 10395759Stanimura#include <netinet6/in6_pcb.h> 10495759Stanimura#include <netinet6/ip6protosw.h> 10553541Sshin#include <netinet6/ip6_var.h> 106148385Sume#include <netinet6/scope6_var.h> 10753541Sshin#include <netinet6/mld6_var.h> 10853541Sshin#include <netinet6/nd6.h> 109185571Sbz#include <netinet6/vinet6.h> 11053541Sshin 111171167Sgnn#ifdef IPSEC 112105199Ssam#include <netipsec/ipsec.h> 113105199Ssam#include <netipsec/key.h> 114105199Ssam#endif 115105199Ssam 11662587Sitojunextern struct domain inet6domain; 117185348Szec 118185348Szec#ifdef VIMAGE_GLOBALS 119141553Srwatsonextern struct inpcbinfo ripcbinfo; 12062587Sitojunextern struct inpcbhead ripcb; 12162587Sitojunextern int icmp6errppslim; 12262587Sitojunextern int icmp6_nodeinfo; 12353541Sshin 124185088Szecstruct icmp6stat icmp6stat; 125185088Szecstatic int icmp6errpps_count; 126185088Szecstatic struct timeval icmp6errppslim_last; 127185088Szec#endif 128185088Szec 129175162Sobrienstatic void icmp6_errcount(struct icmp6errstat *, int, int); 130175162Sobrienstatic int icmp6_rip6_input(struct mbuf **, int); 131175162Sobrienstatic int icmp6_ratelimit(const struct in6_addr *, const int, const int); 13262587Sitojunstatic const char *icmp6_redirect_diag __P((struct in6_addr *, 13362587Sitojun struct in6_addr *, struct in6_addr *)); 134175162Sobrienstatic struct mbuf *ni6_input(struct mbuf *, int); 135175162Sobrienstatic struct mbuf *ni6_nametodns(const char *, int, int); 136175162Sobrienstatic int ni6_dnsmatch(const char *, int, const char *, int); 13762587Sitojunstatic int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *, 138148892Sume struct ifnet **, struct in6_addr *)); 13962587Sitojunstatic int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, 14062587Sitojun struct ifnet *, int)); 141175162Sobrienstatic int icmp6_notify_error(struct mbuf **, int, int, int); 14253541Sshin 14353541Sshin 14453541Sshinvoid 145171259Sdelphijicmp6_init(void) 14653541Sshin{ 147183550Szec INIT_VNET_INET6(curvnet); 148171259Sdelphij 149185088Szec V_icmp6errpps_count = 0; 15053541Sshin} 15153541Sshin 15262587Sitojunstatic void 153171259Sdelphijicmp6_errcount(struct icmp6errstat *stat, int type, int code) 15462587Sitojun{ 15578064Sume switch (type) { 15662587Sitojun case ICMP6_DST_UNREACH: 15762587Sitojun switch (code) { 15862587Sitojun case ICMP6_DST_UNREACH_NOROUTE: 15962587Sitojun stat->icp6errs_dst_unreach_noroute++; 16062587Sitojun return; 16162587Sitojun case ICMP6_DST_UNREACH_ADMIN: 16262587Sitojun stat->icp6errs_dst_unreach_admin++; 16362587Sitojun return; 16462587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 16562587Sitojun stat->icp6errs_dst_unreach_beyondscope++; 16662587Sitojun return; 16762587Sitojun case ICMP6_DST_UNREACH_ADDR: 16862587Sitojun stat->icp6errs_dst_unreach_addr++; 16962587Sitojun return; 17062587Sitojun case ICMP6_DST_UNREACH_NOPORT: 17162587Sitojun stat->icp6errs_dst_unreach_noport++; 17262587Sitojun return; 17362587Sitojun } 17462587Sitojun break; 17562587Sitojun case ICMP6_PACKET_TOO_BIG: 17662587Sitojun stat->icp6errs_packet_too_big++; 17762587Sitojun return; 17862587Sitojun case ICMP6_TIME_EXCEEDED: 17978064Sume switch (code) { 18062587Sitojun case ICMP6_TIME_EXCEED_TRANSIT: 18162587Sitojun stat->icp6errs_time_exceed_transit++; 18262587Sitojun return; 18362587Sitojun case ICMP6_TIME_EXCEED_REASSEMBLY: 18462587Sitojun stat->icp6errs_time_exceed_reassembly++; 18562587Sitojun return; 18662587Sitojun } 18762587Sitojun break; 18862587Sitojun case ICMP6_PARAM_PROB: 18978064Sume switch (code) { 19062587Sitojun case ICMP6_PARAMPROB_HEADER: 19162587Sitojun stat->icp6errs_paramprob_header++; 19262587Sitojun return; 19362587Sitojun case ICMP6_PARAMPROB_NEXTHEADER: 19462587Sitojun stat->icp6errs_paramprob_nextheader++; 19562587Sitojun return; 19662587Sitojun case ICMP6_PARAMPROB_OPTION: 19762587Sitojun stat->icp6errs_paramprob_option++; 19862587Sitojun return; 19962587Sitojun } 20062587Sitojun break; 20162587Sitojun case ND_REDIRECT: 20262587Sitojun stat->icp6errs_redirect++; 20362587Sitojun return; 20462587Sitojun } 20562587Sitojun stat->icp6errs_unknown++; 20662587Sitojun} 20762587Sitojun 20853541Sshin/* 209148385Sume * A wrapper function for icmp6_error() necessary when the erroneous packet 210148385Sume * may not contain enough scope zone information. 211148385Sume */ 212148385Sumevoid 213171259Sdelphijicmp6_error2(struct mbuf *m, int type, int code, int param, 214171259Sdelphij struct ifnet *ifp) 215148385Sume{ 216183550Szec INIT_VNET_INET6(curvnet); 217148385Sume struct ip6_hdr *ip6; 218148385Sume 219148385Sume if (ifp == NULL) 220148385Sume return; 221148385Sume 222148385Sume#ifndef PULLDOWN_TEST 223148385Sume IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 224148385Sume#else 225148385Sume if (m->m_len < sizeof(struct ip6_hdr)) { 226148385Sume m = m_pullup(m, sizeof(struct ip6_hdr)); 227148385Sume if (m == NULL) 228148385Sume return; 229148385Sume } 230148385Sume#endif 231148385Sume 232148385Sume ip6 = mtod(m, struct ip6_hdr *); 233148385Sume 234148385Sume if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0) 235148385Sume return; 236148385Sume if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) 237148385Sume return; 238148385Sume 239148385Sume icmp6_error(m, type, code, param); 240148385Sume} 241148385Sume 242148385Sume/* 24353541Sshin * Generate an error packet of type error in response to bad IP6 packet. 24453541Sshin */ 24553541Sshinvoid 246171259Sdelphijicmp6_error(struct mbuf *m, int type, int code, int param) 24753541Sshin{ 248183550Szec INIT_VNET_INET6(curvnet); 24953541Sshin struct ip6_hdr *oip6, *nip6; 25053541Sshin struct icmp6_hdr *icmp6; 25162587Sitojun u_int preplen; 25253541Sshin int off; 25362587Sitojun int nxt; 25453541Sshin 255190964Srwatson ICMP6STAT_INC(icp6s_error); 25653541Sshin 25762587Sitojun /* count per-type-code statistics */ 258181803Sbz icmp6_errcount(&V_icmp6stat.icp6s_outerrhist, type, code); 25962587Sitojun 26062587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 26162587Sitojun if (m->m_flags & M_DECRYPTED) { 262190964Srwatson ICMP6STAT_INC(icp6s_canterror); 26353541Sshin goto freeit; 26462587Sitojun } 26562587Sitojun#endif 26653541Sshin 26762587Sitojun#ifndef PULLDOWN_TEST 26862587Sitojun IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 26962587Sitojun#else 27062587Sitojun if (m->m_len < sizeof(struct ip6_hdr)) { 27162587Sitojun m = m_pullup(m, sizeof(struct ip6_hdr)); 27262587Sitojun if (m == NULL) 27362587Sitojun return; 27462587Sitojun } 27562587Sitojun#endif 27653541Sshin oip6 = mtod(m, struct ip6_hdr *); 27753541Sshin 27853541Sshin /* 279121472Sume * If the destination address of the erroneous packet is a multicast 280121472Sume * address, or the packet was sent using link-layer multicast, 281121472Sume * we should basically suppress sending an error (RFC 2463, Section 282121472Sume * 2.4). 283121472Sume * We have two exceptions (the item e.2 in that section): 284121472Sume * - the Pakcet Too Big message can be sent for path MTU discovery. 285121472Sume * - the Parameter Problem Message that can be allowed an icmp6 error 286121472Sume * in the option type field. This check has been done in 287121472Sume * ip6_unknown_opt(), so we can just check the type and code. 28853541Sshin */ 28953541Sshin if ((m->m_flags & (M_BCAST|M_MCAST) || 29053541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && 29153541Sshin (type != ICMP6_PACKET_TOO_BIG && 29253541Sshin (type != ICMP6_PARAM_PROB || 29353541Sshin code != ICMP6_PARAMPROB_OPTION))) 29453541Sshin goto freeit; 29553541Sshin 296121472Sume /* 297121472Sume * RFC 2463, 2.4 (e.5): source address check. 298121472Sume * XXX: the case of anycast source? 299121472Sume */ 30053541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || 30153541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) 30253541Sshin goto freeit; 30353541Sshin 30453541Sshin /* 30562587Sitojun * If we are about to send ICMPv6 against ICMPv6 error/redirect, 30662587Sitojun * don't do it. 30753541Sshin */ 30862587Sitojun nxt = -1; 30962587Sitojun off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 31062587Sitojun if (off >= 0 && nxt == IPPROTO_ICMPV6) { 31153541Sshin struct icmp6_hdr *icp; 31253541Sshin 31362587Sitojun#ifndef PULLDOWN_TEST 31462587Sitojun IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), ); 31562587Sitojun icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 31662587Sitojun#else 31762587Sitojun IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, 31862587Sitojun sizeof(*icp)); 31962587Sitojun if (icp == NULL) { 320190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 32162587Sitojun return; 32262587Sitojun } 32362587Sitojun#endif 32462587Sitojun if (icp->icmp6_type < ICMP6_ECHO_REQUEST || 32562587Sitojun icp->icmp6_type == ND_REDIRECT) { 32653541Sshin /* 32762587Sitojun * ICMPv6 error 32862587Sitojun * Special case: for redirect (which is 32962587Sitojun * informational) we must not send icmp6 error. 33053541Sshin */ 331190964Srwatson ICMP6STAT_INC(icp6s_canterror); 33262587Sitojun goto freeit; 33362587Sitojun } else { 33462587Sitojun /* ICMPv6 informational - send the error */ 33553541Sshin } 33662587Sitojun } else { 33762587Sitojun /* non-ICMPv6 - send the error */ 33853541Sshin } 33953541Sshin 34053541Sshin oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */ 34153541Sshin 34253541Sshin /* Finally, do rate limitation check. */ 34353541Sshin if (icmp6_ratelimit(&oip6->ip6_src, type, code)) { 344190964Srwatson ICMP6STAT_INC(icp6s_toofreq); 34553541Sshin goto freeit; 34653541Sshin } 34753541Sshin 34853541Sshin /* 34953541Sshin * OK, ICMP6 can be generated. 35053541Sshin */ 35153541Sshin 35253541Sshin if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) 35353541Sshin m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); 35453541Sshin 35562587Sitojun preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 356111119Simp M_PREPEND(m, preplen, M_DONTWAIT); 35762587Sitojun if (m && m->m_len < preplen) 35862587Sitojun m = m_pullup(m, preplen); 35953541Sshin if (m == NULL) { 36078064Sume nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__)); 36153541Sshin return; 36253541Sshin } 36353541Sshin 36453541Sshin nip6 = mtod(m, struct ip6_hdr *); 36553541Sshin nip6->ip6_src = oip6->ip6_src; 36653541Sshin nip6->ip6_dst = oip6->ip6_dst; 36753541Sshin 368121315Sume in6_clearscope(&oip6->ip6_src); 369121315Sume in6_clearscope(&oip6->ip6_dst); 37053541Sshin 37153541Sshin icmp6 = (struct icmp6_hdr *)(nip6 + 1); 37253541Sshin icmp6->icmp6_type = type; 37353541Sshin icmp6->icmp6_code = code; 37453541Sshin icmp6->icmp6_pptr = htonl((u_int32_t)param); 37553541Sshin 37678064Sume /* 37778064Sume * icmp6_reflect() is designed to be in the input path. 378148987Sume * icmp6_error() can be called from both input and output path, 37978064Sume * and if we are in output path rcvif could contain bogus value. 38078064Sume * clear m->m_pkthdr.rcvif for safety, we should have enough scope 38178064Sume * information in ip header (nip6). 38278064Sume */ 38378064Sume m->m_pkthdr.rcvif = NULL; 38478064Sume 385190964Srwatson ICMP6STAT_INC(icp6s_outhist[type]); 38695023Ssuz icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ 38762587Sitojun 38862587Sitojun return; 38962587Sitojun 39062587Sitojun freeit: 39162587Sitojun /* 392148987Sume * If we can't tell whether or not we can generate ICMP6, free it. 39362587Sitojun */ 39462587Sitojun m_freem(m); 39553541Sshin} 39653541Sshin 39753541Sshin/* 39853541Sshin * Process a received ICMP6 message. 39953541Sshin */ 40053541Sshinint 401171259Sdelphijicmp6_input(struct mbuf **mp, int *offp, int proto) 40253541Sshin{ 403183550Szec INIT_VNET_INET6(curvnet); 404183550Szec INIT_VPROCG(TD_TO_VPROCG(curthread)); /* XXX V_hostname needs this */ 40553541Sshin struct mbuf *m = *mp, *n; 40653541Sshin struct ip6_hdr *ip6, *nip6; 40753541Sshin struct icmp6_hdr *icmp6, *nicmp6; 40853541Sshin int off = *offp; 40953541Sshin int icmp6len = m->m_pkthdr.len - *offp; 41053541Sshin int code, sum, noff; 411165118Sbz char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 41253541Sshin 41362587Sitojun#ifndef PULLDOWN_TEST 41453541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE); 41595023Ssuz /* m might change if M_LOOP. So, call mtod after this */ 41662587Sitojun#endif 41753541Sshin 41853541Sshin /* 41953541Sshin * Locate icmp6 structure in mbuf, and check 42053541Sshin * that not corrupted and of at least minimum length 42153541Sshin */ 42253541Sshin 42353541Sshin ip6 = mtod(m, struct ip6_hdr *); 42453541Sshin if (icmp6len < sizeof(struct icmp6_hdr)) { 425190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 42653541Sshin goto freeit; 42753541Sshin } 42853541Sshin 42953541Sshin /* 430191672Sbms * Check multicast group membership. 431191672Sbms * Note: SSM filters are not applied for ICMPv6 traffic. 432191672Sbms */ 433191672Sbms if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 434191672Sbms struct ifnet *ifp; 435191672Sbms struct in6_multi *inm; 436191672Sbms 437191672Sbms ifp = m->m_pkthdr.rcvif; 438191672Sbms inm = in6m_lookup(ifp, &ip6->ip6_dst); 439191672Sbms if (inm == NULL) { 440191672Sbms IP6STAT_INC(ip6s_notmember); 441191672Sbms in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 442191672Sbms goto freeit; 443191672Sbms } 444191672Sbms } 445191672Sbms 446191672Sbms /* 44753541Sshin * calculate the checksum 44853541Sshin */ 44962587Sitojun#ifndef PULLDOWN_TEST 45053541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 45162587Sitojun#else 45262587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 45362587Sitojun if (icmp6 == NULL) { 454190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 45562587Sitojun return IPPROTO_DONE; 45662587Sitojun } 45762587Sitojun#endif 45853541Sshin code = icmp6->icmp6_code; 45953541Sshin 46053541Sshin if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { 46178064Sume nd6log((LOG_ERR, 46253541Sshin "ICMP6 checksum error(%d|%x) %s\n", 463165118Sbz icmp6->icmp6_type, sum, 464165118Sbz ip6_sprintf(ip6bufs, &ip6->ip6_src))); 465190964Srwatson ICMP6STAT_INC(icp6s_checksum); 46653541Sshin goto freeit; 46753541Sshin } 46853541Sshin 46983934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 47053541Sshin /* 47153541Sshin * Deliver very specific ICMP6 type only. 472148987Sume * This is important to deliver TOOBIG. Otherwise PMTUD 47353541Sshin * will not work. 47453541Sshin */ 47553541Sshin switch (icmp6->icmp6_type) { 47653541Sshin case ICMP6_DST_UNREACH: 47753541Sshin case ICMP6_PACKET_TOO_BIG: 47853541Sshin case ICMP6_TIME_EXCEEDED: 47953541Sshin break; 48053541Sshin default: 48153541Sshin goto freeit; 48253541Sshin } 48353541Sshin } 48453541Sshin 485190964Srwatson ICMP6STAT_INC(icp6s_inhist[icmp6->icmp6_type]); 48653541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg); 48753541Sshin if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) 48853541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error); 48953541Sshin 49053541Sshin switch (icmp6->icmp6_type) { 49153541Sshin case ICMP6_DST_UNREACH: 49253541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach); 49353541Sshin switch (code) { 49453541Sshin case ICMP6_DST_UNREACH_NOROUTE: 49553541Sshin code = PRC_UNREACH_NET; 49653541Sshin break; 49753541Sshin case ICMP6_DST_UNREACH_ADMIN: 49853541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib); 49962587Sitojun code = PRC_UNREACH_PROTOCOL; /* is this a good code? */ 50062587Sitojun break; 50153541Sshin case ICMP6_DST_UNREACH_ADDR: 50262587Sitojun code = PRC_HOSTDEAD; 50353541Sshin break; 50462587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 50562587Sitojun /* I mean "source address was incorrect." */ 50662587Sitojun code = PRC_PARAMPROB; 50762587Sitojun break; 50853541Sshin case ICMP6_DST_UNREACH_NOPORT: 50953541Sshin code = PRC_UNREACH_PORT; 51053541Sshin break; 51153541Sshin default: 51253541Sshin goto badcode; 51353541Sshin } 51453541Sshin goto deliver; 51553541Sshin break; 51653541Sshin 51753541Sshin case ICMP6_PACKET_TOO_BIG: 51853541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig); 51953541Sshin 520126194Sume /* validation is made in icmp6_mtudisc_update */ 521126194Sume 52253541Sshin code = PRC_MSGSIZE; 52353541Sshin 52462587Sitojun /* 52562587Sitojun * Updating the path MTU will be done after examining 52662587Sitojun * intermediate extension headers. 52762587Sitojun */ 52853541Sshin goto deliver; 52953541Sshin break; 53053541Sshin 53153541Sshin case ICMP6_TIME_EXCEEDED: 53253541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed); 53353541Sshin switch (code) { 53453541Sshin case ICMP6_TIME_EXCEED_TRANSIT: 535151465Ssuz code = PRC_TIMXCEED_INTRANS; 536151465Ssuz break; 53753541Sshin case ICMP6_TIME_EXCEED_REASSEMBLY: 538151465Ssuz code = PRC_TIMXCEED_REASS; 53953541Sshin break; 54053541Sshin default: 54153541Sshin goto badcode; 54253541Sshin } 54353541Sshin goto deliver; 54453541Sshin break; 54553541Sshin 54653541Sshin case ICMP6_PARAM_PROB: 54753541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob); 54853541Sshin switch (code) { 54953541Sshin case ICMP6_PARAMPROB_NEXTHEADER: 55053541Sshin code = PRC_UNREACH_PROTOCOL; 55153541Sshin break; 55253541Sshin case ICMP6_PARAMPROB_HEADER: 55353541Sshin case ICMP6_PARAMPROB_OPTION: 55453541Sshin code = PRC_PARAMPROB; 55553541Sshin break; 55653541Sshin default: 55753541Sshin goto badcode; 55853541Sshin } 55953541Sshin goto deliver; 56053541Sshin break; 56153541Sshin 56253541Sshin case ICMP6_ECHO_REQUEST: 56353541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo); 56453541Sshin if (code != 0) 56553541Sshin goto badcode; 56653541Sshin if ((n = m_copy(m, 0, M_COPYALL)) == NULL) { 56753541Sshin /* Give up remote */ 56853541Sshin break; 56953541Sshin } 57062587Sitojun if ((n->m_flags & M_EXT) != 0 57162587Sitojun || n->m_len < off + sizeof(struct icmp6_hdr)) { 57253541Sshin struct mbuf *n0 = n; 57362587Sitojun const int maxlen = sizeof(*nip6) + sizeof(*nicmp6); 574108741Ssam int n0len; 57553541Sshin 576111119Simp MGETHDR(n, M_DONTWAIT, n0->m_type); 577111070Ssam n0len = n0->m_pkthdr.len; /* save for use below */ 578111070Ssam if (n) 579111070Ssam M_MOVE_PKTHDR(n, n0); 58062587Sitojun if (n && maxlen >= MHLEN) { 581111119Simp MCLGET(n, M_DONTWAIT); 58262587Sitojun if ((n->m_flags & M_EXT) == 0) { 58362587Sitojun m_free(n); 58462587Sitojun n = NULL; 58562587Sitojun } 58662587Sitojun } 58753541Sshin if (n == NULL) { 58853541Sshin /* Give up remote */ 58953541Sshin m_freem(n0); 59053541Sshin break; 59153541Sshin } 59253541Sshin /* 59353541Sshin * Copy IPv6 and ICMPv6 only. 59453541Sshin */ 59553541Sshin nip6 = mtod(n, struct ip6_hdr *); 59653541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 59753541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 59853541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 59962587Sitojun noff = sizeof(struct ip6_hdr); 600108741Ssam /* new mbuf contains only ipv6+icmpv6 headers */ 601108741Ssam n->m_len = noff + sizeof(struct icmp6_hdr); 60253541Sshin /* 603120891Sume * Adjust mbuf. ip6_plen will be adjusted in 60462587Sitojun * ip6_output(). 60553541Sshin */ 60662587Sitojun m_adj(n0, off + sizeof(struct icmp6_hdr)); 607108741Ssam /* recalculate complete packet size */ 608108741Ssam n->m_pkthdr.len = n0len + (noff - off); 60962587Sitojun n->m_next = n0; 61053541Sshin } else { 61153541Sshin nip6 = mtod(n, struct ip6_hdr *); 612166046Sume IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off, 613166046Sume sizeof(*nicmp6)); 61453541Sshin noff = off; 61553541Sshin } 61653541Sshin nicmp6->icmp6_type = ICMP6_ECHO_REPLY; 61753541Sshin nicmp6->icmp6_code = 0; 61853541Sshin if (n) { 619190964Srwatson ICMP6STAT_INC(icp6s_reflect); 620190964Srwatson ICMP6STAT_INC(icp6s_outhist[ICMP6_ECHO_REPLY]); 62153541Sshin icmp6_reflect(n, noff); 62253541Sshin } 62353541Sshin break; 62453541Sshin 62553541Sshin case ICMP6_ECHO_REPLY: 62653541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply); 62753541Sshin if (code != 0) 62853541Sshin goto badcode; 62953541Sshin break; 63053541Sshin 63196116Sume case MLD_LISTENER_QUERY: 63296116Sume case MLD_LISTENER_REPORT: 633191672Sbms case MLD_LISTENER_DONE: 634191672Sbms case MLDV2_LISTENER_REPORT: 635191672Sbms /* 636191672Sbms * Drop MLD traffic which is not link-local. 637191672Sbms * XXX Should we also sanity check that these messages 638191672Sbms * were directed to a link-local multicast prefix? 639191672Sbms */ 640191672Sbms if (ip6->ip6_hlim != 1) 64162587Sitojun goto freeit; 642191672Sbms if (mld_input(m, off, icmp6len) != 0) 643191672Sbms return (IPPROTO_DONE); 64453541Sshin /* m stays. */ 64553541Sshin break; 64653541Sshin 64753541Sshin case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ 64853541Sshin { 64953541Sshin enum { WRU, FQDN } mode; 65053541Sshin 651181803Sbz if (!V_icmp6_nodeinfo) 65262587Sitojun break; 65362587Sitojun 65453541Sshin if (icmp6len == sizeof(struct icmp6_hdr) + 4) 65553541Sshin mode = WRU; 65662587Sitojun else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) 65753541Sshin mode = FQDN; 65853541Sshin else 65953541Sshin goto badlen; 66053541Sshin 661181803Sbz#define hostnamelen strlen(V_hostname) 66253541Sshin if (mode == FQDN) { 66362587Sitojun#ifndef PULLDOWN_TEST 66453541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), 665120891Sume IPPROTO_DONE); 66662587Sitojun#endif 66762587Sitojun n = m_copy(m, 0, M_COPYALL); 66862587Sitojun if (n) 66962587Sitojun n = ni6_input(n, off); 67062587Sitojun /* XXX meaningless if n == NULL */ 67153541Sshin noff = sizeof(struct ip6_hdr); 67253541Sshin } else { 67353541Sshin u_char *p; 67462587Sitojun int maxlen, maxhlen; 67553541Sshin 676169664Sjinmei /* 677169664Sjinmei * XXX: this combination of flags is pointless, 678169664Sjinmei * but should we keep this for compatibility? 679169664Sjinmei */ 680181803Sbz if ((V_icmp6_nodeinfo & 5) != 5) 68178064Sume break; 68278064Sume 68362587Sitojun if (code != 0) 68462587Sitojun goto badcode; 68562587Sitojun maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4; 68662587Sitojun if (maxlen >= MCLBYTES) { 68762587Sitojun /* Give up remote */ 68862587Sitojun break; 68962587Sitojun } 690111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 69162587Sitojun if (n && maxlen > MHLEN) { 692111119Simp MCLGET(n, M_DONTWAIT); 69362587Sitojun if ((n->m_flags & M_EXT) == 0) { 69462587Sitojun m_free(n); 69562587Sitojun n = NULL; 69662587Sitojun } 69762587Sitojun } 698166619Sbms if (n && !m_dup_pkthdr(n, m, M_DONTWAIT)) { 699108466Ssam /* 700108466Ssam * Previous code did a blind M_COPY_PKTHDR 701108466Ssam * and said "just for rcvif". If true, then 702108466Ssam * we could tolerate the dup failing (due to 703108466Ssam * the deep copy of the tag chain). For now 704108466Ssam * be conservative and just fail. 705108466Ssam */ 706108466Ssam m_free(n); 707108466Ssam n = NULL; 708108466Ssam } 70953541Sshin if (n == NULL) { 71053541Sshin /* Give up remote */ 71153541Sshin break; 71253541Sshin } 71378064Sume n->m_pkthdr.rcvif = NULL; 71462587Sitojun n->m_len = 0; 71562587Sitojun maxhlen = M_TRAILINGSPACE(n) - maxlen; 716180291Srwatson mtx_lock(&hostname_mtx); 71762587Sitojun if (maxhlen > hostnamelen) 71862587Sitojun maxhlen = hostnamelen; 71953541Sshin /* 72053541Sshin * Copy IPv6 and ICMPv6 only. 72153541Sshin */ 72253541Sshin nip6 = mtod(n, struct ip6_hdr *); 72353541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 72453541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 72553541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 72653541Sshin p = (u_char *)(nicmp6 + 1); 72753541Sshin bzero(p, 4); 728181803Sbz bcopy(V_hostname, p + 4, maxhlen); /* meaningless TTL */ 729180291Srwatson mtx_unlock(&hostname_mtx); 73053541Sshin noff = sizeof(struct ip6_hdr); 73153541Sshin n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 73262587Sitojun sizeof(struct icmp6_hdr) + 4 + maxhlen; 73353541Sshin nicmp6->icmp6_type = ICMP6_WRUREPLY; 73453541Sshin nicmp6->icmp6_code = 0; 73553541Sshin } 73653541Sshin#undef hostnamelen 73753541Sshin if (n) { 738190964Srwatson ICMP6STAT_INC(icp6s_reflect); 739190964Srwatson ICMP6STAT_INC(icp6s_outhist[ICMP6_WRUREPLY]); 74053541Sshin icmp6_reflect(n, noff); 74153541Sshin } 74253541Sshin break; 74353541Sshin } 74453541Sshin 74553541Sshin case ICMP6_WRUREPLY: 74653541Sshin if (code != 0) 74753541Sshin goto badcode; 74853541Sshin break; 74953541Sshin 75053541Sshin case ND_ROUTER_SOLICIT: 75153541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit); 75253541Sshin if (code != 0) 75353541Sshin goto badcode; 75453541Sshin if (icmp6len < sizeof(struct nd_router_solicit)) 75553541Sshin goto badlen; 756111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 75762587Sitojun /* give up local */ 75862587Sitojun nd6_rs_input(m, off, icmp6len); 75962587Sitojun m = NULL; 76062587Sitojun goto freeit; 76162587Sitojun } 76262587Sitojun nd6_rs_input(n, off, icmp6len); 76353541Sshin /* m stays. */ 76453541Sshin break; 76553541Sshin 76653541Sshin case ND_ROUTER_ADVERT: 76753541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert); 76853541Sshin if (code != 0) 76953541Sshin goto badcode; 77053541Sshin if (icmp6len < sizeof(struct nd_router_advert)) 77153541Sshin goto badlen; 772111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 77362587Sitojun /* give up local */ 77462587Sitojun nd6_ra_input(m, off, icmp6len); 77562587Sitojun m = NULL; 77662587Sitojun goto freeit; 77762587Sitojun } 77862587Sitojun nd6_ra_input(n, off, icmp6len); 77953541Sshin /* m stays. */ 78053541Sshin break; 78153541Sshin 78253541Sshin case ND_NEIGHBOR_SOLICIT: 78353541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit); 78453541Sshin if (code != 0) 78553541Sshin goto badcode; 78653541Sshin if (icmp6len < sizeof(struct nd_neighbor_solicit)) 78753541Sshin goto badlen; 788111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 78962587Sitojun /* give up local */ 79062587Sitojun nd6_ns_input(m, off, icmp6len); 79162587Sitojun m = NULL; 79262587Sitojun goto freeit; 79362587Sitojun } 79462587Sitojun nd6_ns_input(n, off, icmp6len); 79553541Sshin /* m stays. */ 79653541Sshin break; 79753541Sshin 79853541Sshin case ND_NEIGHBOR_ADVERT: 79953541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert); 80053541Sshin if (code != 0) 80153541Sshin goto badcode; 80253541Sshin if (icmp6len < sizeof(struct nd_neighbor_advert)) 80353541Sshin goto badlen; 804111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 80562587Sitojun /* give up local */ 80662587Sitojun nd6_na_input(m, off, icmp6len); 80762587Sitojun m = NULL; 80862587Sitojun goto freeit; 80962587Sitojun } 81062587Sitojun nd6_na_input(n, off, icmp6len); 81153541Sshin /* m stays. */ 81253541Sshin break; 81353541Sshin 81453541Sshin case ND_REDIRECT: 81553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect); 81653541Sshin if (code != 0) 81753541Sshin goto badcode; 81853541Sshin if (icmp6len < sizeof(struct nd_redirect)) 81953541Sshin goto badlen; 820111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 82162587Sitojun /* give up local */ 82262587Sitojun icmp6_redirect_input(m, off); 82362587Sitojun m = NULL; 82462587Sitojun goto freeit; 82562587Sitojun } 82662587Sitojun icmp6_redirect_input(n, off); 82753541Sshin /* m stays. */ 82853541Sshin break; 82953541Sshin 83053541Sshin case ICMP6_ROUTER_RENUMBERING: 83153541Sshin if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && 83253541Sshin code != ICMP6_ROUTER_RENUMBERING_RESULT) 83353541Sshin goto badcode; 83453541Sshin if (icmp6len < sizeof(struct icmp6_router_renum)) 83553541Sshin goto badlen; 83653541Sshin break; 83753541Sshin 83853541Sshin default: 83978064Sume nd6log((LOG_DEBUG, 84078064Sume "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", 841165118Sbz icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src), 842165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst), 84378064Sume m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0)); 84453541Sshin if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { 84553541Sshin /* ICMPv6 error: MUST deliver it by spec... */ 84653541Sshin code = PRC_NCMDS; 84753541Sshin /* deliver */ 84853541Sshin } else { 84953541Sshin /* ICMPv6 informational: MUST not deliver */ 85053541Sshin break; 85153541Sshin } 85253541Sshin deliver: 853142681Sume if (icmp6_notify_error(&m, off, icmp6len, code)) { 85478064Sume /* In this case, m should've been freed. */ 855120856Sume return (IPPROTO_DONE); 85653541Sshin } 85778064Sume break; 85878064Sume 85978064Sume badcode: 860190964Srwatson ICMP6STAT_INC(icp6s_badcode); 86178064Sume break; 86278064Sume 86378064Sume badlen: 864190964Srwatson ICMP6STAT_INC(icp6s_badlen); 86578064Sume break; 86678064Sume } 86778064Sume 86878064Sume /* deliver the packet to appropriate sockets */ 86978064Sume icmp6_rip6_input(&m, *offp); 87078064Sume 87178064Sume return IPPROTO_DONE; 87278064Sume 87378064Sume freeit: 87478064Sume m_freem(m); 87578064Sume return IPPROTO_DONE; 87678064Sume} 87778064Sume 87878064Sumestatic int 879171259Sdelphijicmp6_notify_error(struct mbuf **mp, int off, int icmp6len, int code) 88078064Sume{ 881183550Szec INIT_VNET_INET6(curvnet); 882142681Sume struct mbuf *m = *mp; 88378064Sume struct icmp6_hdr *icmp6; 88478064Sume struct ip6_hdr *eip6; 88578064Sume u_int32_t notifymtu; 88678064Sume struct sockaddr_in6 icmp6src, icmp6dst; 88778064Sume 88878064Sume if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { 889190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 89078064Sume goto freeit; 89178064Sume } 89262587Sitojun#ifndef PULLDOWN_TEST 89378064Sume IP6_EXTHDR_CHECK(m, off, 894120891Sume sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1); 89578064Sume icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 89662587Sitojun#else 89778064Sume IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 898120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 89978064Sume if (icmp6 == NULL) { 900190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 901120856Sume return (-1); 90278064Sume } 90362587Sitojun#endif 90478064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 90553541Sshin 90678064Sume /* Detect the upper level protocol */ 90778064Sume { 908175162Sobrien void (*ctlfunc)(int, struct sockaddr *, void *); 90953541Sshin u_int8_t nxt = eip6->ip6_nxt; 91053541Sshin int eoff = off + sizeof(struct icmp6_hdr) + 911120891Sume sizeof(struct ip6_hdr); 91253541Sshin struct ip6ctlparam ip6cp; 91362587Sitojun struct in6_addr *finaldst = NULL; 91462587Sitojun int icmp6type = icmp6->icmp6_type; 91562587Sitojun struct ip6_frag *fh; 91662587Sitojun struct ip6_rthdr *rth; 91762587Sitojun struct ip6_rthdr0 *rth0; 91862587Sitojun int rthlen; 91953541Sshin 92095023Ssuz while (1) { /* XXX: should avoid infinite loop explicitly? */ 92153541Sshin struct ip6_ext *eh; 92253541Sshin 92378064Sume switch (nxt) { 92453541Sshin case IPPROTO_HOPOPTS: 92553541Sshin case IPPROTO_DSTOPTS: 92653541Sshin case IPPROTO_AH: 92762587Sitojun#ifndef PULLDOWN_TEST 928120891Sume IP6_EXTHDR_CHECK(m, 0, 929120891Sume eoff + sizeof(struct ip6_ext), -1); 930120892Sume eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff); 93162587Sitojun#else 93262587Sitojun IP6_EXTHDR_GET(eh, struct ip6_ext *, m, 933120891Sume eoff, sizeof(*eh)); 93462587Sitojun if (eh == NULL) { 935190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 936120856Sume return (-1); 93762587Sitojun } 93862587Sitojun#endif 939120891Sume 94053541Sshin if (nxt == IPPROTO_AH) 94153541Sshin eoff += (eh->ip6e_len + 2) << 2; 94253541Sshin else 94353541Sshin eoff += (eh->ip6e_len + 1) << 3; 94453541Sshin nxt = eh->ip6e_nxt; 94553541Sshin break; 94662587Sitojun case IPPROTO_ROUTING: 94762587Sitojun /* 94862587Sitojun * When the erroneous packet contains a 94962587Sitojun * routing header, we should examine the 95062587Sitojun * header to determine the final destination. 95162587Sitojun * Otherwise, we can't properly update 95262587Sitojun * information that depends on the final 95362587Sitojun * destination (e.g. path MTU). 95462587Sitojun */ 95562587Sitojun#ifndef PULLDOWN_TEST 956120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1); 957120891Sume rth = (struct ip6_rthdr *) 958120891Sume (mtod(m, caddr_t) + eoff); 95962587Sitojun#else 96062587Sitojun IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m, 961120891Sume eoff, sizeof(*rth)); 96262587Sitojun if (rth == NULL) { 963190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 964120856Sume return (-1); 96562587Sitojun } 96662587Sitojun#endif 96762587Sitojun rthlen = (rth->ip6r_len + 1) << 3; 96862587Sitojun /* 96962587Sitojun * XXX: currently there is no 97062587Sitojun * officially defined type other 97162587Sitojun * than type-0. 97262587Sitojun * Note that if the segment left field 97362587Sitojun * is 0, all intermediate hops must 97462587Sitojun * have been passed. 97562587Sitojun */ 97662587Sitojun if (rth->ip6r_segleft && 97762587Sitojun rth->ip6r_type == IPV6_RTHDR_TYPE_0) { 97862587Sitojun int hops; 97962587Sitojun 98062587Sitojun#ifndef PULLDOWN_TEST 981120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1); 982120891Sume rth0 = (struct ip6_rthdr0 *) 983120891Sume (mtod(m, caddr_t) + eoff); 98462587Sitojun#else 98562587Sitojun IP6_EXTHDR_GET(rth0, 986120891Sume struct ip6_rthdr0 *, m, 987120891Sume eoff, rthlen); 98862587Sitojun if (rth0 == NULL) { 989190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 990120856Sume return (-1); 99162587Sitojun } 99262587Sitojun#endif 99362587Sitojun /* just ignore a bogus header */ 99462587Sitojun if ((rth0->ip6r0_len % 2) == 0 && 99562587Sitojun (hops = rth0->ip6r0_len/2)) 99662587Sitojun finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1); 99762587Sitojun } 99862587Sitojun eoff += rthlen; 99962587Sitojun nxt = rth->ip6r_nxt; 100062587Sitojun break; 100162587Sitojun case IPPROTO_FRAGMENT: 100262587Sitojun#ifndef PULLDOWN_TEST 100362587Sitojun IP6_EXTHDR_CHECK(m, 0, eoff + 1004120891Sume sizeof(struct ip6_frag), -1); 1005120891Sume fh = (struct ip6_frag *)(mtod(m, caddr_t) + 1006120891Sume eoff); 100762587Sitojun#else 100862587Sitojun IP6_EXTHDR_GET(fh, struct ip6_frag *, m, 1009120891Sume eoff, sizeof(*fh)); 101062587Sitojun if (fh == NULL) { 1011190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 1012120856Sume return (-1); 101362587Sitojun } 101462587Sitojun#endif 101562587Sitojun /* 101662587Sitojun * Data after a fragment header is meaningless 101762587Sitojun * unless it is the first fragment, but 101862587Sitojun * we'll go to the notify label for path MTU 101962587Sitojun * discovery. 102062587Sitojun */ 102162587Sitojun if (fh->ip6f_offlg & IP6F_OFF_MASK) 102262587Sitojun goto notify; 102362587Sitojun 102462587Sitojun eoff += sizeof(struct ip6_frag); 102562587Sitojun nxt = fh->ip6f_nxt; 102662587Sitojun break; 102753541Sshin default: 102862587Sitojun /* 102962587Sitojun * This case includes ESP and the No Next 103095023Ssuz * Header. In such cases going to the notify 103162587Sitojun * label does not have any meaning 103262587Sitojun * (i.e. ctlfunc will be NULL), but we go 103362587Sitojun * anyway since we might have to update 103462587Sitojun * path MTU information. 103562587Sitojun */ 103653541Sshin goto notify; 103753541Sshin } 103853541Sshin } 103978064Sume notify: 104062587Sitojun#ifndef PULLDOWN_TEST 104153541Sshin icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 104262587Sitojun#else 104362587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 1044120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 104562587Sitojun if (icmp6 == NULL) { 1046190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 1047120856Sume return (-1); 104862587Sitojun } 104962587Sitojun#endif 105078064Sume 1051120891Sume /* 1052120891Sume * retrieve parameters from the inner IPv6 header, and convert 1053120891Sume * them into sockaddr structures. 1054120891Sume * XXX: there is no guarantee that the source or destination 1055120891Sume * addresses of the inner packet are in the same scope as 1056120891Sume * the addresses of the icmp packet. But there is no other 1057120891Sume * way to determine the zone. 1058120891Sume */ 105978064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 1060120891Sume 106178064Sume bzero(&icmp6dst, sizeof(icmp6dst)); 106278064Sume icmp6dst.sin6_len = sizeof(struct sockaddr_in6); 106378064Sume icmp6dst.sin6_family = AF_INET6; 106478064Sume if (finaldst == NULL) 106578064Sume icmp6dst.sin6_addr = eip6->ip6_dst; 106678064Sume else 106778064Sume icmp6dst.sin6_addr = *finaldst; 1068148385Sume if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL)) 1069121315Sume goto freeit; 107078064Sume bzero(&icmp6src, sizeof(icmp6src)); 107178064Sume icmp6src.sin6_len = sizeof(struct sockaddr_in6); 107278064Sume icmp6src.sin6_family = AF_INET6; 107378064Sume icmp6src.sin6_addr = eip6->ip6_src; 1074148385Sume if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL)) 1075121315Sume goto freeit; 1076148385Sume icmp6src.sin6_flowinfo = 1077148385Sume (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); 107878064Sume 107978064Sume if (finaldst == NULL) 108078064Sume finaldst = &eip6->ip6_dst; 108178064Sume ip6cp.ip6c_m = m; 108278064Sume ip6cp.ip6c_icmp6 = icmp6; 108378064Sume ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1); 108478064Sume ip6cp.ip6c_off = eoff; 108578064Sume ip6cp.ip6c_finaldst = finaldst; 108678064Sume ip6cp.ip6c_src = &icmp6src; 108778064Sume ip6cp.ip6c_nxt = nxt; 108878064Sume 108962587Sitojun if (icmp6type == ICMP6_PACKET_TOO_BIG) { 109078064Sume notifymtu = ntohl(icmp6->icmp6_mtu); 109178064Sume ip6cp.ip6c_cmdarg = (void *)¬ifymtu; 109278064Sume icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ 109362587Sitojun } 109462587Sitojun 1095175162Sobrien ctlfunc = (void (*)(int, struct sockaddr *, void *)) 1096120891Sume (inet6sw[ip6_protox[nxt]].pr_ctlinput); 109753541Sshin if (ctlfunc) { 109878064Sume (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, 1099120891Sume &ip6cp); 110053541Sshin } 110153541Sshin } 1102142681Sume *mp = m; 1103120856Sume return (0); 110453541Sshin 110578064Sume freeit: 110653541Sshin m_freem(m); 1107120856Sume return (-1); 110853541Sshin} 110953541Sshin 111078064Sumevoid 1111171259Sdelphijicmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated) 111262587Sitojun{ 1113183550Szec INIT_VNET_INET6(curvnet); 111478064Sume struct in6_addr *dst = ip6cp->ip6c_finaldst; 111578064Sume struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; 111678064Sume struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ 111762587Sitojun u_int mtu = ntohl(icmp6->icmp6_mtu); 1118122922Sandre struct in_conninfo inc; 111962587Sitojun 1120121472Sume#if 0 1121121472Sume /* 1122121472Sume * RFC2460 section 5, last paragraph. 1123121472Sume * even though minimum link MTU for IPv6 is IPV6_MMTU, 1124121472Sume * we may see ICMPv6 too big with mtu < IPV6_MMTU 1125121472Sume * due to packet translator in the middle. 1126121472Sume * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for 1127121472Sume * special handling. 1128121472Sume */ 1129121472Sume if (mtu < IPV6_MMTU) 1130121472Sume return; 1131121472Sume#endif 1132121472Sume 1133121472Sume /* 1134121472Sume * we reject ICMPv6 too big with abnormally small value. 1135121472Sume * XXX what is the good definition of "abnormally small"? 1136121472Sume */ 1137121472Sume if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) 1138121472Sume return; 1139121472Sume 114078064Sume if (!validated) 114178064Sume return; 114278064Sume 1143182740Ssimon /* 1144182740Ssimon * In case the suggested mtu is less than IPV6_MMTU, we 1145182740Ssimon * only need to remember that it was for above mentioned 1146182740Ssimon * "alwaysfrag" case. 1147182740Ssimon * Try to be as close to the spec as possible. 1148182740Ssimon */ 1149182740Ssimon if (mtu < IPV6_MMTU) 1150182740Ssimon mtu = IPV6_MMTU - 8; 1151182740Ssimon 1152122922Sandre bzero(&inc, sizeof(inc)); 1153186222Sbz inc.inc_flags |= INC_ISIPV6; 1154122922Sandre inc.inc6_faddr = *dst; 1155148385Sume if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL)) 1156148385Sume return; 115762587Sitojun 1158162084Sandre if (mtu < tcp_maxmtu6(&inc, NULL)) { 1159122922Sandre tcp_hc_updatemtu(&inc, mtu); 1160190964Srwatson ICMP6STAT_INC(icp6s_pmtuchg); 116162587Sitojun } 116262587Sitojun} 116362587Sitojun 116453541Sshin/* 116578064Sume * Process a Node Information Query packet, based on 116678064Sume * draft-ietf-ipngwg-icmp-name-lookups-07. 1167120891Sume * 116862587Sitojun * Spec incompatibilities: 116962587Sitojun * - IPv6 Subject address handling 117062587Sitojun * - IPv4 Subject address handling support missing 117162587Sitojun * - Proxy reply (answer even if it's not for me) 117262587Sitojun * - joins NI group address at in6_ifattach() time only, does not cope 117362587Sitojun * with hostname changes by sethostname(3) 117453541Sshin */ 1175181803Sbz#define hostnamelen strlen(V_hostname) 117653541Sshinstatic struct mbuf * 1177171259Sdelphijni6_input(struct mbuf *m, int off) 117853541Sshin{ 1179183550Szec INIT_VNET_INET6(curvnet); 1180183550Szec INIT_VPROCG(TD_TO_VPROCG(curthread)); /* XXX V_hostname needs this */ 118162587Sitojun struct icmp6_nodeinfo *ni6, *nni6; 118253541Sshin struct mbuf *n = NULL; 118362587Sitojun u_int16_t qtype; 118462587Sitojun int subjlen; 118553541Sshin int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 118653541Sshin struct ni_reply_fqdn *fqdn; 118753541Sshin int addrs; /* for NI_QTYPE_NODEADDR */ 118853541Sshin struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ 1189148385Sume struct in6_addr in6_subj; /* subject address */ 119062587Sitojun struct ip6_hdr *ip6; 119162587Sitojun int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ 119278064Sume char *subj = NULL; 119378064Sume struct in6_ifaddr *ia6 = NULL; 119453541Sshin 119562587Sitojun ip6 = mtod(m, struct ip6_hdr *); 119662587Sitojun#ifndef PULLDOWN_TEST 119762587Sitojun ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); 119862587Sitojun#else 119962587Sitojun IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); 120062587Sitojun if (ni6 == NULL) { 120162587Sitojun /* m is already reclaimed */ 1202120891Sume return (NULL); 120362587Sitojun } 120462587Sitojun#endif 120553541Sshin 120662587Sitojun /* 1207169664Sjinmei * Validate IPv6 source address. 1208169664Sjinmei * The default configuration MUST be to refuse answering queries from 1209169664Sjinmei * global-scope addresses according to RFC4602. 1210169664Sjinmei * Notes: 1211169664Sjinmei * - it's not very clear what "refuse" means; this implementation 1212169664Sjinmei * simply drops it. 1213169664Sjinmei * - it's not very easy to identify global-scope (unicast) addresses 1214169664Sjinmei * since there are many prefixes for them. It should be safer 1215169664Sjinmei * and in practice sufficient to check "all" but loopback and 1216169664Sjinmei * link-local (note that site-local unicast was deprecated and 1217169664Sjinmei * ULA is defined as global scope-wise) 1218169664Sjinmei */ 1219181803Sbz if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 && 1220169664Sjinmei !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) && 1221169664Sjinmei !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) 1222169664Sjinmei goto bad; 1223169664Sjinmei 1224169664Sjinmei /* 122562587Sitojun * Validate IPv6 destination address. 122662587Sitojun * 122778064Sume * The Responder must discard the Query without further processing 122878064Sume * unless it is one of the Responder's unicast or anycast addresses, or 122978064Sume * a link-local scope multicast address which the Responder has joined. 1230169664Sjinmei * [RFC4602, Section 5.] 123162587Sitojun */ 1232121630Sume if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1233121630Sume if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) 1234121630Sume goto bad; 1235121630Sume /* else it's a link-local multicast, fine */ 1236121630Sume } else { /* unicast or anycast */ 1237121630Sume if ((ia6 = ip6_getdstifaddr(m)) == NULL) 1238121630Sume goto bad; /* XXX impossible */ 1239121630Sume 1240121630Sume if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) && 1241181803Sbz !(V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) { 124278064Sume nd6log((LOG_DEBUG, "ni6_input: ignore node info to " 124378064Sume "a temporary address in %s:%d", 124478064Sume __FILE__, __LINE__)); 124578064Sume goto bad; 124678064Sume } 1247121630Sume } 124862587Sitojun 124978064Sume /* validate query Subject field. */ 125062587Sitojun qtype = ntohs(ni6->ni_qtype); 125162587Sitojun subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); 125262587Sitojun switch (qtype) { 125362587Sitojun case NI_QTYPE_NOOP: 125462587Sitojun case NI_QTYPE_SUPTYPES: 125578064Sume /* 07 draft */ 125678064Sume if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) 125778064Sume break; 125878064Sume /* FALLTHROUGH */ 125962587Sitojun case NI_QTYPE_FQDN: 126062587Sitojun case NI_QTYPE_NODEADDR: 1261151475Ssuz case NI_QTYPE_IPV4ADDR: 126262587Sitojun switch (ni6->ni_code) { 126362587Sitojun case ICMP6_NI_SUBJ_IPV6: 126462587Sitojun#if ICMP6_NI_SUBJ_IPV6 != 0 126562587Sitojun case 0: 126662587Sitojun#endif 126762587Sitojun /* 126862587Sitojun * backward compatibility - try to accept 03 draft 126962587Sitojun * format, where no Subject is present. 127062587Sitojun */ 127178064Sume if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && 127278064Sume subjlen == 0) { 127362587Sitojun oldfqdn++; 127462587Sitojun break; 127562587Sitojun } 127678064Sume#if ICMP6_NI_SUBJ_IPV6 != 0 127778064Sume if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) 127878064Sume goto bad; 127978064Sume#endif 128062587Sitojun 1281121630Sume if (subjlen != sizeof(struct in6_addr)) 128262587Sitojun goto bad; 128362587Sitojun 128462587Sitojun /* 128562587Sitojun * Validate Subject address. 128662587Sitojun * 128778064Sume * Not sure what exactly "address belongs to the node" 128878064Sume * means in the spec, is it just unicast, or what? 128962587Sitojun * 129062587Sitojun * At this moment we consider Subject address as 129162587Sitojun * "belong to the node" if the Subject address equals 129262587Sitojun * to the IPv6 destination address; validation for 129362587Sitojun * IPv6 destination address should have done enough 129462587Sitojun * check for us. 129562587Sitojun * 129662587Sitojun * We do not do proxy at this moment. 129762587Sitojun */ 129862587Sitojun /* m_pulldown instead of copy? */ 129962587Sitojun m_copydata(m, off + sizeof(struct icmp6_nodeinfo), 1300148385Sume subjlen, (caddr_t)&in6_subj); 1301148385Sume if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL)) 1302121315Sume goto bad; 1303121630Sume 1304148385Sume subj = (char *)&in6_subj; 1305148385Sume if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj)) 130662587Sitojun break; 130778064Sume 130862587Sitojun /* 130962587Sitojun * XXX if we are to allow other cases, we should really 131062587Sitojun * be careful about scope here. 131162587Sitojun * basically, we should disallow queries toward IPv6 1312120891Sume * destination X with subject Y, 1313120891Sume * if scope(X) > scope(Y). 131462587Sitojun * if we allow scope(X) > scope(Y), it will result in 131562587Sitojun * information leakage across scope boundary. 131662587Sitojun */ 131762587Sitojun goto bad; 131862587Sitojun 131962587Sitojun case ICMP6_NI_SUBJ_FQDN: 132062587Sitojun /* 132162587Sitojun * Validate Subject name with gethostname(3). 132262587Sitojun * 132362587Sitojun * The behavior may need some debate, since: 132462587Sitojun * - we are not sure if the node has FQDN as 132562587Sitojun * hostname (returned by gethostname(3)). 132662587Sitojun * - the code does wildcard match for truncated names. 132762587Sitojun * however, we are not sure if we want to perform 132862587Sitojun * wildcard match, if gethostname(3) side has 132962587Sitojun * truncated hostname. 133062587Sitojun */ 1331180291Srwatson mtx_lock(&hostname_mtx); 1332181803Sbz n = ni6_nametodns(V_hostname, hostnamelen, 0); 1333180291Srwatson mtx_unlock(&hostname_mtx); 133462587Sitojun if (!n || n->m_next || n->m_len == 0) 133562587Sitojun goto bad; 133662587Sitojun IP6_EXTHDR_GET(subj, char *, m, 133762587Sitojun off + sizeof(struct icmp6_nodeinfo), subjlen); 133862587Sitojun if (subj == NULL) 133962587Sitojun goto bad; 134062587Sitojun if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 1341120891Sume n->m_len)) { 134262587Sitojun goto bad; 134362587Sitojun } 134462587Sitojun m_freem(n); 134562587Sitojun n = NULL; 134662587Sitojun break; 134762587Sitojun 134878064Sume case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 134962587Sitojun default: 135062587Sitojun goto bad; 135162587Sitojun } 135262587Sitojun break; 135378064Sume } 135462587Sitojun 135578064Sume /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 135678064Sume switch (qtype) { 135778064Sume case NI_QTYPE_FQDN: 1358181803Sbz if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0) 135978064Sume goto bad; 136078064Sume break; 136178064Sume case NI_QTYPE_NODEADDR: 1362151475Ssuz case NI_QTYPE_IPV4ADDR: 1363181803Sbz if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0) 136478064Sume goto bad; 136578064Sume break; 136678064Sume } 136778064Sume 136878064Sume /* guess reply length */ 136978064Sume switch (qtype) { 137078064Sume case NI_QTYPE_NOOP: 137178064Sume break; /* no reply data */ 137278064Sume case NI_QTYPE_SUPTYPES: 137378064Sume replylen += sizeof(u_int32_t); 137478064Sume break; 137578064Sume case NI_QTYPE_FQDN: 137678064Sume /* XXX will append an mbuf */ 137778064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 137878064Sume break; 137978064Sume case NI_QTYPE_NODEADDR: 1380148892Sume addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj); 138178064Sume if ((replylen += addrs * (sizeof(struct in6_addr) + 1382120891Sume sizeof(u_int32_t))) > MCLBYTES) 138378064Sume replylen = MCLBYTES; /* XXX: will truncate pkt later */ 138478064Sume break; 1385151475Ssuz case NI_QTYPE_IPV4ADDR: 1386151475Ssuz /* unsupported - should respond with unknown Qtype? */ 1387151475Ssuz break; 138862587Sitojun default: 138978064Sume /* 139078064Sume * XXX: We must return a reply with the ICMP6 code 1391120891Sume * `unknown Qtype' in this case. However we regard the case 139278064Sume * as an FQDN query for backward compatibility. 139378064Sume * Older versions set a random value to this field, 139478064Sume * so it rarely varies in the defined qtypes. 139578064Sume * But the mechanism is not reliable... 139678064Sume * maybe we should obsolete older versions. 139778064Sume */ 139878064Sume qtype = NI_QTYPE_FQDN; 139978064Sume /* XXX will append an mbuf */ 140078064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 140178064Sume oldfqdn++; 140278064Sume break; 140362587Sitojun } 140462587Sitojun 140578064Sume /* allocate an mbuf to reply. */ 1406111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 140762587Sitojun if (n == NULL) { 140862587Sitojun m_freem(m); 1409120856Sume return (NULL); 141062587Sitojun } 1411108466Ssam M_MOVE_PKTHDR(n, m); /* just for recvif */ 141253541Sshin if (replylen > MHLEN) { 141362587Sitojun if (replylen > MCLBYTES) { 141478064Sume /* 141578064Sume * XXX: should we try to allocate more? But MCLBYTES 141678064Sume * is probably much larger than IPV6_MMTU... 141778064Sume */ 141853541Sshin goto bad; 141962587Sitojun } 1420111119Simp MCLGET(n, M_DONTWAIT); 142153541Sshin if ((n->m_flags & M_EXT) == 0) { 142253541Sshin goto bad; 142353541Sshin } 142453541Sshin } 142553541Sshin n->m_pkthdr.len = n->m_len = replylen; 142653541Sshin 142753541Sshin /* copy mbuf header and IPv6 + Node Information base headers */ 142853541Sshin bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); 142953541Sshin nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 143062587Sitojun bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); 143153541Sshin 143253541Sshin /* qtype dependent procedure */ 143353541Sshin switch (qtype) { 143462587Sitojun case NI_QTYPE_NOOP: 143578064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 143662587Sitojun nni6->ni_flags = 0; 143762587Sitojun break; 143862587Sitojun case NI_QTYPE_SUPTYPES: 143978064Sume { 144078064Sume u_int32_t v; 144178064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 144278064Sume nni6->ni_flags = htons(0x0000); /* raw bitmap */ 144378064Sume /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 144478064Sume v = (u_int32_t)htonl(0x0000000f); 144578064Sume bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 144662587Sitojun break; 144778064Sume } 144862587Sitojun case NI_QTYPE_FQDN: 144978064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 145062587Sitojun fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + 1451120891Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo)); 145262587Sitojun nni6->ni_flags = 0; /* XXX: meaningless TTL */ 145362587Sitojun fqdn->ni_fqdn_ttl = 0; /* ditto. */ 145462587Sitojun /* 145562587Sitojun * XXX do we really have FQDN in variable "hostname"? 145662587Sitojun */ 1457180291Srwatson mtx_lock(&hostname_mtx); 1458181803Sbz n->m_next = ni6_nametodns(V_hostname, hostnamelen, oldfqdn); 1459180291Srwatson mtx_unlock(&hostname_mtx); 146062587Sitojun if (n->m_next == NULL) 146162587Sitojun goto bad; 146262587Sitojun /* XXX we assume that n->m_next is not a chain */ 146362587Sitojun if (n->m_next->m_next != NULL) 146462587Sitojun goto bad; 146562587Sitojun n->m_pkthdr.len += n->m_next->m_len; 146662587Sitojun break; 146762587Sitojun case NI_QTYPE_NODEADDR: 146862587Sitojun { 146962587Sitojun int lenlim, copied; 147053541Sshin 147178064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 147278064Sume n->m_pkthdr.len = n->m_len = 147378064Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 147478064Sume lenlim = M_TRAILINGSPACE(n); 147562587Sitojun copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 147662587Sitojun /* XXX: reset mbuf length */ 147762587Sitojun n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 1478120891Sume sizeof(struct icmp6_nodeinfo) + copied; 147962587Sitojun break; 148053541Sshin } 148162587Sitojun default: 148262587Sitojun break; /* XXX impossible! */ 148362587Sitojun } 148453541Sshin 148553541Sshin nni6->ni_type = ICMP6_NI_REPLY; 148662587Sitojun m_freem(m); 1487120856Sume return (n); 148853541Sshin 148953541Sshin bad: 149062587Sitojun m_freem(m); 149153541Sshin if (n) 149253541Sshin m_freem(n); 1493120856Sume return (NULL); 149453541Sshin} 149553541Sshin#undef hostnamelen 149653541Sshin 149753541Sshin/* 149862587Sitojun * make a mbuf with DNS-encoded string. no compression support. 149962587Sitojun * 150062587Sitojun * XXX names with less than 2 dots (like "foo" or "foo.section") will be 150162587Sitojun * treated as truncated name (two \0 at the end). this is a wild guess. 1502171259Sdelphij * 1503171259Sdelphij * old - return pascal string if non-zero 150462587Sitojun */ 150562587Sitojunstatic struct mbuf * 1506171259Sdelphijni6_nametodns(const char *name, int namelen, int old) 150762587Sitojun{ 150862587Sitojun struct mbuf *m; 150962587Sitojun char *cp, *ep; 151062587Sitojun const char *p, *q; 151162587Sitojun int i, len, nterm; 151262587Sitojun 151362587Sitojun if (old) 151462587Sitojun len = namelen + 1; 151562587Sitojun else 151662587Sitojun len = MCLBYTES; 151762587Sitojun 151862587Sitojun /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ 1519111119Simp MGET(m, M_DONTWAIT, MT_DATA); 152062587Sitojun if (m && len > MLEN) { 1521111119Simp MCLGET(m, M_DONTWAIT); 152262587Sitojun if ((m->m_flags & M_EXT) == 0) 152362587Sitojun goto fail; 152462587Sitojun } 152562587Sitojun if (!m) 152662587Sitojun goto fail; 152762587Sitojun m->m_next = NULL; 152862587Sitojun 152962587Sitojun if (old) { 153062587Sitojun m->m_len = len; 153162587Sitojun *mtod(m, char *) = namelen; 153262587Sitojun bcopy(name, mtod(m, char *) + 1, namelen); 153362587Sitojun return m; 153462587Sitojun } else { 153562587Sitojun m->m_len = 0; 153662587Sitojun cp = mtod(m, char *); 153762587Sitojun ep = mtod(m, char *) + M_TRAILINGSPACE(m); 153862587Sitojun 153962587Sitojun /* if not certain about my name, return empty buffer */ 154062587Sitojun if (namelen == 0) 154162587Sitojun return m; 154262587Sitojun 154362587Sitojun /* 154462587Sitojun * guess if it looks like shortened hostname, or FQDN. 154562587Sitojun * shortened hostname needs two trailing "\0". 154662587Sitojun */ 154762587Sitojun i = 0; 154862587Sitojun for (p = name; p < name + namelen; p++) { 154962587Sitojun if (*p && *p == '.') 155062587Sitojun i++; 155162587Sitojun } 155262587Sitojun if (i < 2) 155362587Sitojun nterm = 2; 155462587Sitojun else 155562587Sitojun nterm = 1; 155662587Sitojun 155762587Sitojun p = name; 155862587Sitojun while (cp < ep && p < name + namelen) { 155962587Sitojun i = 0; 156062587Sitojun for (q = p; q < name + namelen && *q && *q != '.'; q++) 156162587Sitojun i++; 156262587Sitojun /* result does not fit into mbuf */ 156362587Sitojun if (cp + i + 1 >= ep) 156462587Sitojun goto fail; 156578704Sume /* 156678704Sume * DNS label length restriction, RFC1035 page 8. 156778704Sume * "i == 0" case is included here to avoid returning 156878704Sume * 0-length label on "foo..bar". 156978704Sume */ 157078704Sume if (i <= 0 || i >= 64) 157162587Sitojun goto fail; 157262587Sitojun *cp++ = i; 157362587Sitojun bcopy(p, cp, i); 157462587Sitojun cp += i; 157562587Sitojun p = q; 157662587Sitojun if (p < name + namelen && *p == '.') 157762587Sitojun p++; 157862587Sitojun } 157962587Sitojun /* termination */ 158062587Sitojun if (cp + nterm >= ep) 158162587Sitojun goto fail; 158262587Sitojun while (nterm-- > 0) 158362587Sitojun *cp++ = '\0'; 158462587Sitojun m->m_len = cp - mtod(m, char *); 158562587Sitojun return m; 158662587Sitojun } 158762587Sitojun 158862587Sitojun panic("should not reach here"); 158995023Ssuz /* NOTREACHED */ 159062587Sitojun 159162587Sitojun fail: 159262587Sitojun if (m) 159362587Sitojun m_freem(m); 159462587Sitojun return NULL; 159562587Sitojun} 159662587Sitojun 159762587Sitojun/* 159862587Sitojun * check if two DNS-encoded string matches. takes care of truncated 159962587Sitojun * form (with \0\0 at the end). no compression support. 160078064Sume * XXX upper/lowercase match (see RFC2065) 160162587Sitojun */ 160262587Sitojunstatic int 1603171259Sdelphijni6_dnsmatch(const char *a, int alen, const char *b, int blen) 160462587Sitojun{ 160562587Sitojun const char *a0, *b0; 160662587Sitojun int l; 160762587Sitojun 160862587Sitojun /* simplest case - need validation? */ 160962587Sitojun if (alen == blen && bcmp(a, b, alen) == 0) 161062587Sitojun return 1; 161162587Sitojun 161262587Sitojun a0 = a; 161362587Sitojun b0 = b; 161462587Sitojun 161562587Sitojun /* termination is mandatory */ 161662587Sitojun if (alen < 2 || blen < 2) 161762587Sitojun return 0; 161862587Sitojun if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 161962587Sitojun return 0; 162062587Sitojun alen--; 162162587Sitojun blen--; 162262587Sitojun 162362587Sitojun while (a - a0 < alen && b - b0 < blen) { 162462587Sitojun if (a - a0 + 1 > alen || b - b0 + 1 > blen) 162562587Sitojun return 0; 162662587Sitojun 162762587Sitojun if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 162862587Sitojun return 0; 162962587Sitojun /* we don't support compression yet */ 163062587Sitojun if (a[0] >= 64 || b[0] >= 64) 163162587Sitojun return 0; 163262587Sitojun 163362587Sitojun /* truncated case */ 163462587Sitojun if (a[0] == 0 && a - a0 == alen - 1) 163562587Sitojun return 1; 163662587Sitojun if (b[0] == 0 && b - b0 == blen - 1) 163762587Sitojun return 1; 163862587Sitojun if (a[0] == 0 || b[0] == 0) 163962587Sitojun return 0; 164062587Sitojun 164162587Sitojun if (a[0] != b[0]) 164262587Sitojun return 0; 164362587Sitojun l = a[0]; 164462587Sitojun if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 164562587Sitojun return 0; 164662587Sitojun if (bcmp(a + 1, b + 1, l) != 0) 164762587Sitojun return 0; 164862587Sitojun 164962587Sitojun a += 1 + l; 165062587Sitojun b += 1 + l; 165162587Sitojun } 165262587Sitojun 165362587Sitojun if (a - a0 == alen && b - b0 == blen) 165462587Sitojun return 1; 165562587Sitojun else 165662587Sitojun return 0; 165762587Sitojun} 165862587Sitojun 165962587Sitojun/* 166053541Sshin * calculate the number of addresses to be returned in the node info reply. 166153541Sshin */ 166253541Sshinstatic int 1663171259Sdelphijni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp, 1664171259Sdelphij struct in6_addr *subj) 166553541Sshin{ 1666183550Szec INIT_VNET_NET(curvnet); 1667183550Szec INIT_VNET_INET6(curvnet); 166878064Sume struct ifnet *ifp; 166978064Sume struct in6_ifaddr *ifa6; 167078064Sume struct ifaddr *ifa; 167153541Sshin int addrs = 0, addrsofif, iffound = 0; 167278064Sume int niflags = ni6->ni_flags; 167353541Sshin 167478064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 167578064Sume switch (ni6->ni_code) { 167678064Sume case ICMP6_NI_SUBJ_IPV6: 167778064Sume if (subj == NULL) /* must be impossible... */ 1678120856Sume return (0); 167978064Sume break; 168078064Sume default: 168178064Sume /* 168278064Sume * XXX: we only support IPv6 subject address for 168378064Sume * this Qtype. 168478064Sume */ 1685120856Sume return (0); 168678064Sume } 168778064Sume } 168878064Sume 1689108172Shsu IFNET_RLOCK(); 1690181803Sbz for (ifp = TAILQ_FIRST(&V_ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 169153541Sshin addrsofif = 0; 1692191317Srwatson IF_ADDR_LOCK(ifp); 1693191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 169453541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 169553541Sshin continue; 169653541Sshin ifa6 = (struct in6_ifaddr *)ifa; 169753541Sshin 169878064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 1699148892Sume IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr)) 170053541Sshin iffound = 1; 170153541Sshin 170262587Sitojun /* 170362587Sitojun * IPv4-mapped addresses can only be returned by a 170462587Sitojun * Node Information proxy, since they represent 170562587Sitojun * addresses of IPv4-only nodes, which perforce do 170662587Sitojun * not implement this protocol. 170778064Sume * [icmp-name-lookups-07, Section 5.4] 170862587Sitojun * So we don't support NI_NODEADDR_FLAG_COMPAT in 170962587Sitojun * this function at this moment. 171062587Sitojun */ 171162587Sitojun 171253541Sshin /* What do we have to do about ::1? */ 171378064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 171478064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 171578064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 171678064Sume continue; 171753541Sshin break; 171878064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 171978064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 172078064Sume continue; 172153541Sshin break; 172278064Sume case IPV6_ADDR_SCOPE_GLOBAL: 172378064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 172478064Sume continue; 172578064Sume break; 172678064Sume default: 172778064Sume continue; 172853541Sshin } 172978064Sume 173078064Sume /* 173178064Sume * check if anycast is okay. 173295023Ssuz * XXX: just experimental. not in the spec. 173378064Sume */ 173478064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 173578064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 173678064Sume continue; /* we need only unicast addresses */ 173778064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1738181803Sbz (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { 173978064Sume continue; 174078064Sume } 174178064Sume addrsofif++; /* count the address */ 174253541Sshin } 1743191317Srwatson IF_ADDR_UNLOCK(ifp); 174453541Sshin if (iffound) { 174553541Sshin *ifpp = ifp; 1746108172Shsu IFNET_RUNLOCK(); 1747120856Sume return (addrsofif); 174853541Sshin } 174953541Sshin 175053541Sshin addrs += addrsofif; 175153541Sshin } 1752108172Shsu IFNET_RUNLOCK(); 175353541Sshin 1754120856Sume return (addrs); 175553541Sshin} 175653541Sshin 175753541Sshinstatic int 1758171259Sdelphijni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6, 1759171259Sdelphij struct ifnet *ifp0, int resid) 176053541Sshin{ 1761183550Szec INIT_VNET_NET(curvnet); 1762183550Szec INIT_VNET_INET6(curvnet); 1763181803Sbz struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&V_ifnet); 176478064Sume struct in6_ifaddr *ifa6; 176578064Sume struct ifaddr *ifa; 176678064Sume struct ifnet *ifp_dep = NULL; 176778064Sume int copied = 0, allow_deprecated = 0; 176853541Sshin u_char *cp = (u_char *)(nni6 + 1); 176978064Sume int niflags = ni6->ni_flags; 177078064Sume u_int32_t ltime; 177153541Sshin 177278064Sume if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 1773120856Sume return (0); /* needless to copy */ 1774120891Sume 1775108172Shsu IFNET_RLOCK(); 177678064Sume again: 1777120892Sume 1778120891Sume for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 1779191317Srwatson IF_ADDR_LOCK(ifp); 1780191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 178153541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 178253541Sshin continue; 178353541Sshin ifa6 = (struct in6_ifaddr *)ifa; 178453541Sshin 178578064Sume if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && 178678064Sume allow_deprecated == 0) { 178778064Sume /* 178878064Sume * prefererred address should be put before 178978064Sume * deprecated addresses. 179078064Sume */ 179178064Sume 179278064Sume /* record the interface for later search */ 179378064Sume if (ifp_dep == NULL) 179478064Sume ifp_dep = ifp; 179578064Sume 179678064Sume continue; 1797120891Sume } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && 1798120891Sume allow_deprecated != 0) 179978064Sume continue; /* we now collect deprecated addrs */ 180053541Sshin 180153541Sshin /* What do we have to do about ::1? */ 180278064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 180378064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 180478064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 180578064Sume continue; 180653541Sshin break; 180778064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 180878064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 180978064Sume continue; 181053541Sshin break; 181178064Sume case IPV6_ADDR_SCOPE_GLOBAL: 181278064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 181378064Sume continue; 181478064Sume break; 181578064Sume default: 181678064Sume continue; 181753541Sshin } 181853541Sshin 181978064Sume /* 182078064Sume * check if anycast is okay. 1821120891Sume * XXX: just experimental. not in the spec. 182278064Sume */ 182378064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 182478064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 182578064Sume continue; 182678064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1827181803Sbz (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { 182878064Sume continue; 182953541Sshin } 183078064Sume 183178064Sume /* now we can copy the address */ 183278064Sume if (resid < sizeof(struct in6_addr) + 183378064Sume sizeof(u_int32_t)) { 1834191317Srwatson IF_ADDR_UNLOCK(ifp); 183578064Sume /* 183678064Sume * We give up much more copy. 183778064Sume * Set the truncate flag and return. 183878064Sume */ 1839120891Sume nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE; 1840108172Shsu IFNET_RUNLOCK(); 1841120856Sume return (copied); 184278064Sume } 184378064Sume 184478064Sume /* 184578064Sume * Set the TTL of the address. 184678064Sume * The TTL value should be one of the following 184778064Sume * according to the specification: 184878064Sume * 184978064Sume * 1. The remaining lifetime of a DHCP lease on the 185078064Sume * address, or 185178064Sume * 2. The remaining Valid Lifetime of a prefix from 185278064Sume * which the address was derived through Stateless 185378064Sume * Autoconfiguration. 185478064Sume * 185578064Sume * Note that we currently do not support stateful 185678064Sume * address configuration by DHCPv6, so the former 185778064Sume * case can't happen. 185878064Sume */ 185978064Sume if (ifa6->ia6_lifetime.ia6t_expire == 0) 186078064Sume ltime = ND6_INFINITE_LIFETIME; 186178064Sume else { 186278064Sume if (ifa6->ia6_lifetime.ia6t_expire > 186378064Sume time_second) 186478064Sume ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second); 186578064Sume else 186678064Sume ltime = 0; 186778064Sume } 1868120891Sume 186978064Sume bcopy(<ime, cp, sizeof(u_int32_t)); 187078064Sume cp += sizeof(u_int32_t); 187178064Sume 187278064Sume /* copy the address itself */ 187378064Sume bcopy(&ifa6->ia_addr.sin6_addr, cp, 1874120891Sume sizeof(struct in6_addr)); 1875121315Sume in6_clearscope((struct in6_addr *)cp); /* XXX */ 187678064Sume cp += sizeof(struct in6_addr); 1877120891Sume 187878064Sume resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1879120891Sume copied += (sizeof(struct in6_addr) + sizeof(u_int32_t)); 188053541Sshin } 1881191317Srwatson IF_ADDR_UNLOCK(ifp); 188253541Sshin if (ifp0) /* we need search only on the specified IF */ 188353541Sshin break; 188453541Sshin } 188553541Sshin 188678064Sume if (allow_deprecated == 0 && ifp_dep != NULL) { 188778064Sume ifp = ifp_dep; 188878064Sume allow_deprecated = 1; 188978064Sume 189078064Sume goto again; 189178064Sume } 189278064Sume 1893108172Shsu IFNET_RUNLOCK(); 1894108172Shsu 1895120856Sume return (copied); 189653541Sshin} 189753541Sshin 189853541Sshin/* 189953541Sshin * XXX almost dup'ed code with rip6_input. 190053541Sshin */ 190153541Sshinstatic int 1902171259Sdelphijicmp6_rip6_input(struct mbuf **mp, int off) 190353541Sshin{ 1904183550Szec INIT_VNET_INET(curvnet); 1905183550Szec INIT_VNET_INET6(curvnet); 190653541Sshin struct mbuf *m = *mp; 190778064Sume struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1908186141Sbz struct inpcb *in6p; 1909186141Sbz struct inpcb *last = NULL; 1910121315Sume struct sockaddr_in6 fromsa; 191153541Sshin struct icmp6_hdr *icmp6; 191253541Sshin struct mbuf *opts = NULL; 191353541Sshin 191462587Sitojun#ifndef PULLDOWN_TEST 191553541Sshin /* this is assumed to be safe. */ 191653541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 191762587Sitojun#else 191862587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 191962587Sitojun if (icmp6 == NULL) { 192062587Sitojun /* m is already reclaimed */ 1921120891Sume return (IPPROTO_DONE); 192262587Sitojun } 192362587Sitojun#endif 192453541Sshin 1925148385Sume /* 1926148385Sume * XXX: the address may have embedded scope zone ID, which should be 1927148385Sume * hidden from applications. 1928148385Sume */ 1929121315Sume bzero(&fromsa, sizeof(fromsa)); 1930148385Sume fromsa.sin6_family = AF_INET6; 1931121315Sume fromsa.sin6_len = sizeof(struct sockaddr_in6); 1932148385Sume fromsa.sin6_addr = ip6->ip6_src; 1933148385Sume if (sa6_recoverscope(&fromsa)) { 1934148385Sume m_freem(m); 1935148385Sume return (IPPROTO_DONE); 1936148385Sume } 193753541Sshin 1938181803Sbz INP_INFO_RLOCK(&V_ripcbinfo); 1939181803Sbz LIST_FOREACH(in6p, &V_ripcb, inp_list) { 1940180850Smav if ((in6p->inp_vflag & INP_IPV6) == 0) 194153541Sshin continue; 1942186141Sbz if (in6p->inp_ip_p != IPPROTO_ICMPV6) 1943180850Smav continue; 194453541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 194553541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 1946180850Smav continue; 194753541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 194853541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 1949180850Smav continue; 1950180932Smav INP_RLOCK(in6p); 1951180965Srwatson if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, 1952180965Srwatson in6p->in6p_icmp6filt)) { 1953180932Smav INP_RUNLOCK(in6p); 1954180850Smav continue; 1955180932Smav } 1956186163Skmacy if (last != NULL) { 1957121809Sume struct mbuf *n = NULL; 1958121809Sume 1959121809Sume /* 1960121809Sume * Recent network drivers tend to allocate a single 1961121809Sume * mbuf cluster, rather than to make a couple of 1962121809Sume * mbufs without clusters. Also, since the IPv6 code 1963121809Sume * path tries to avoid m_pullup(), it is highly 1964121809Sume * probable that we still have an mbuf cluster here 1965121809Sume * even though the necessary length can be stored in an 1966121809Sume * mbuf's internal buffer. 1967121809Sume * Meanwhile, the default size of the receive socket 1968121809Sume * buffer for raw sockets is not so large. This means 1969121809Sume * the possibility of packet loss is relatively higher 1970121809Sume * than before. To avoid this scenario, we copy the 1971121809Sume * received data to a separate mbuf that does not use 1972121809Sume * a cluster, if possible. 1973121809Sume * XXX: it is better to copy the data after stripping 1974121809Sume * intermediate headers. 1975121809Sume */ 1976121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 1977121809Sume m->m_len <= MHLEN) { 1978121809Sume MGET(n, M_DONTWAIT, m->m_type); 1979121809Sume if (n != NULL) { 1980145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 1981171260Sdelphij bcopy(m->m_data, n->m_data, 1982145065Sgnn m->m_len); 1983145065Sgnn n->m_len = m->m_len; 1984145065Sgnn } else { 1985145065Sgnn m_free(n); 1986145065Sgnn n = NULL; 1987145065Sgnn } 1988121809Sume } 1989121809Sume } 1990121809Sume if (n != NULL || 1991121809Sume (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 1992186223Sbz if (last->inp_flags & INP_CONTROLOPTS) 1993121674Sume ip6_savecontrol(last, n, &opts); 199453541Sshin /* strip intermediate headers */ 199553541Sshin m_adj(n, off); 1996186141Sbz SOCKBUF_LOCK(&last->inp_socket->so_rcv); 1997160591Srwatson if (sbappendaddr_locked( 1998186141Sbz &last->inp_socket->so_rcv, 1999121315Sume (struct sockaddr *)&fromsa, n, opts) 2000120891Sume == 0) { 200153541Sshin /* should notify about lost packet */ 200253541Sshin m_freem(n); 200378064Sume if (opts) { 200453541Sshin m_freem(opts); 200578064Sume } 2006160591Srwatson SOCKBUF_UNLOCK( 2007186141Sbz &last->inp_socket->so_rcv); 200897658Stanimura } else 2009186141Sbz sorwakeup_locked(last->inp_socket); 201053541Sshin opts = NULL; 201153541Sshin } 2012178378Srwatson INP_RUNLOCK(last); 201353541Sshin } 201453541Sshin last = in6p; 201553541Sshin } 2016181803Sbz INP_INFO_RUNLOCK(&V_ripcbinfo); 2017186163Skmacy if (last != NULL) { 2018186223Sbz if (last->inp_flags & INP_CONTROLOPTS) 2019121674Sume ip6_savecontrol(last, m, &opts); 202053541Sshin /* strip intermediate headers */ 202153541Sshin m_adj(m, off); 2022121809Sume 2023121809Sume /* avoid using mbuf clusters if possible (see above) */ 2024121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 2025121809Sume m->m_len <= MHLEN) { 2026121809Sume struct mbuf *n; 2027121809Sume 2028121809Sume MGET(n, M_DONTWAIT, m->m_type); 2029121809Sume if (n != NULL) { 2030145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 2031145065Sgnn bcopy(m->m_data, n->m_data, m->m_len); 2032145065Sgnn n->m_len = m->m_len; 2033171260Sdelphij 2034145065Sgnn m_freem(m); 2035145065Sgnn m = n; 2036145065Sgnn } else { 2037145065Sgnn m_freem(n); 2038145065Sgnn n = NULL; 2039145065Sgnn } 2040121809Sume } 2041121809Sume } 2042186141Sbz SOCKBUF_LOCK(&last->inp_socket->so_rcv); 2043186141Sbz if (sbappendaddr_locked(&last->inp_socket->so_rcv, 2044121315Sume (struct sockaddr *)&fromsa, m, opts) == 0) { 204553541Sshin m_freem(m); 204653541Sshin if (opts) 204753541Sshin m_freem(opts); 2048186141Sbz SOCKBUF_UNLOCK(&last->inp_socket->so_rcv); 204997658Stanimura } else 2050186141Sbz sorwakeup_locked(last->inp_socket); 2051178378Srwatson INP_RUNLOCK(last); 205253541Sshin } else { 205353541Sshin m_freem(m); 2054191672Sbms IP6STAT_DEC(ip6s_delivered); 205553541Sshin } 205653541Sshin return IPPROTO_DONE; 205753541Sshin} 205853541Sshin 205953541Sshin/* 206053541Sshin * Reflect the ip6 packet back to the source. 206162587Sitojun * OFF points to the icmp6 header, counted from the top of the mbuf. 206253541Sshin */ 206353541Sshinvoid 2064171259Sdelphijicmp6_reflect(struct mbuf *m, size_t off) 206553541Sshin{ 2066183550Szec INIT_VNET_INET6(curvnet); 206762587Sitojun struct ip6_hdr *ip6; 206853541Sshin struct icmp6_hdr *icmp6; 206953541Sshin struct in6_ifaddr *ia; 207062587Sitojun int plen; 207153541Sshin int type, code; 207253541Sshin struct ifnet *outif = NULL; 2073148385Sume struct in6_addr origdst, *src = NULL; 207453541Sshin 207562587Sitojun /* too short to reflect */ 207662587Sitojun if (off < sizeof(struct ip6_hdr)) { 207778064Sume nd6log((LOG_DEBUG, 207878064Sume "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 207978064Sume (u_long)off, (u_long)sizeof(struct ip6_hdr), 208078064Sume __FILE__, __LINE__)); 208162587Sitojun goto bad; 208262587Sitojun } 208362587Sitojun 208453541Sshin /* 208553541Sshin * If there are extra headers between IPv6 and ICMPv6, strip 208653541Sshin * off that header first. 208753541Sshin */ 208862587Sitojun#ifdef DIAGNOSTIC 208962587Sitojun if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) 209062587Sitojun panic("assumption failed in icmp6_reflect"); 209162587Sitojun#endif 209262587Sitojun if (off > sizeof(struct ip6_hdr)) { 209362587Sitojun size_t l; 209462587Sitojun struct ip6_hdr nip6; 209553541Sshin 209662587Sitojun l = off - sizeof(struct ip6_hdr); 209762587Sitojun m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); 209862587Sitojun m_adj(m, l); 209962587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 210062587Sitojun if (m->m_len < l) { 210162587Sitojun if ((m = m_pullup(m, l)) == NULL) 210262587Sitojun return; 210353541Sshin } 210462587Sitojun bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); 210562587Sitojun } else /* off == sizeof(struct ip6_hdr) */ { 210662587Sitojun size_t l; 210762587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 210862587Sitojun if (m->m_len < l) { 210962587Sitojun if ((m = m_pullup(m, l)) == NULL) 211062587Sitojun return; 211153541Sshin } 211253541Sshin } 211362587Sitojun plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 211462587Sitojun ip6 = mtod(m, struct ip6_hdr *); 211562587Sitojun ip6->ip6_nxt = IPPROTO_ICMPV6; 211653541Sshin icmp6 = (struct icmp6_hdr *)(ip6 + 1); 211753541Sshin type = icmp6->icmp6_type; /* keep type for statistics */ 211853541Sshin code = icmp6->icmp6_code; /* ditto. */ 211953541Sshin 2120148385Sume origdst = ip6->ip6_dst; 212153541Sshin /* 212253541Sshin * ip6_input() drops a packet if its src is multicast. 212353541Sshin * So, the src is never multicast. 212453541Sshin */ 212553541Sshin ip6->ip6_dst = ip6->ip6_src; 212653541Sshin 212753541Sshin /* 2128120891Sume * If the incoming packet was addressed directly to us (i.e. unicast), 212953541Sshin * use dst as the src for the reply. 2130120891Sume * The IN6_IFF_NOTREADY case should be VERY rare, but is possible 213162587Sitojun * (for example) when we encounter an error while forwarding procedure 213262587Sitojun * destined to a duplicated address of ours. 2133148385Sume * Note that ip6_getdstifaddr() may fail if we are in an error handling 2134148385Sume * procedure of an outgoing packet of our own, in which case we need 2135148385Sume * to search in the ifaddr list. 213653541Sshin */ 2137148385Sume if (!IN6_IS_ADDR_MULTICAST(&origdst)) { 2138148385Sume if ((ia = ip6_getdstifaddr(m))) { 2139148385Sume if (!(ia->ia6_flags & 2140148385Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) 2141148385Sume src = &ia->ia_addr.sin6_addr; 2142148385Sume } else { 2143148385Sume struct sockaddr_in6 d; 2144148385Sume 2145148385Sume bzero(&d, sizeof(d)); 2146148385Sume d.sin6_family = AF_INET6; 2147148385Sume d.sin6_len = sizeof(d); 2148148385Sume d.sin6_addr = origdst; 2149148385Sume ia = (struct in6_ifaddr *) 2150148385Sume ifa_ifwithaddr((struct sockaddr *)&d); 2151148385Sume if (ia && 2152148385Sume !(ia->ia6_flags & 2153148385Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) { 2154148385Sume src = &ia->ia_addr.sin6_addr; 2155148385Sume } 215653541Sshin } 215753541Sshin } 215853541Sshin 2159148385Sume if (src == NULL) { 216078064Sume int e; 2161148385Sume struct sockaddr_in6 sin6; 216278064Sume struct route_in6 ro; 216378064Sume 216453541Sshin /* 216562587Sitojun * This case matches to multicasts, our anycast, or unicasts 216695023Ssuz * that we do not own. Select a source address based on the 216778064Sume * source address of the erroneous packet. 216853541Sshin */ 2169148385Sume bzero(&sin6, sizeof(sin6)); 2170148385Sume sin6.sin6_family = AF_INET6; 2171148385Sume sin6.sin6_len = sizeof(sin6); 2172148385Sume sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */ 2173148385Sume 217478064Sume bzero(&ro, sizeof(ro)); 2175148385Sume src = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &e); 217678064Sume if (ro.ro_rt) 217778064Sume RTFREE(ro.ro_rt); /* XXX: we could use this */ 217878064Sume if (src == NULL) { 2179165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 218078064Sume nd6log((LOG_DEBUG, 218178064Sume "icmp6_reflect: source can't be determined: " 218278064Sume "dst=%s, error=%d\n", 2183165118Sbz ip6_sprintf(ip6buf, &sin6.sin6_addr), e)); 218478064Sume goto bad; 218578064Sume } 218678064Sume } 218753541Sshin 218853541Sshin ip6->ip6_src = *src; 218953541Sshin ip6->ip6_flow = 0; 219062587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 219162587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 219253541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 2193121472Sume if (outif) 2194121472Sume ip6->ip6_hlim = ND_IFINFO(outif)->chlim; 2195121472Sume else if (m->m_pkthdr.rcvif) { 219653541Sshin /* XXX: This may not be the outgoing interface */ 2197121161Sume ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim; 219878064Sume } else 2199181803Sbz ip6->ip6_hlim = V_ip6_defhlim; 220053541Sshin 220153541Sshin icmp6->icmp6_cksum = 0; 220253541Sshin icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2203120891Sume sizeof(struct ip6_hdr), plen); 220453541Sshin 220553541Sshin /* 220678064Sume * XXX option handling 220753541Sshin */ 220853541Sshin 220953541Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 221053541Sshin 2211105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 221253541Sshin if (outif) 221353541Sshin icmp6_ifoutstat_inc(outif, type, code); 221453541Sshin 221553541Sshin return; 221653541Sshin 221753541Sshin bad: 221853541Sshin m_freem(m); 221953541Sshin return; 222053541Sshin} 222153541Sshin 222253541Sshinvoid 2223171259Sdelphijicmp6_fasttimo(void) 222453541Sshin{ 222562587Sitojun 2226191672Sbms mld_fasttimo(); 222753541Sshin} 222853541Sshin 2229191672Sbmsvoid 2230191672Sbmsicmp6_slowtimo(void) 2231191672Sbms{ 2232191672Sbms 2233191672Sbms mld_slowtimo(); 2234191672Sbms} 2235191672Sbms 223653541Sshinstatic const char * 2237171259Sdelphijicmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6, 2238171259Sdelphij struct in6_addr *tgt6) 223953541Sshin{ 224053541Sshin static char buf[1024]; 2241165118Sbz char ip6bufs[INET6_ADDRSTRLEN]; 2242165118Sbz char ip6bufd[INET6_ADDRSTRLEN]; 2243165118Sbz char ip6buft[INET6_ADDRSTRLEN]; 224453541Sshin snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", 2245165118Sbz ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6), 2246165118Sbz ip6_sprintf(ip6buft, tgt6)); 224753541Sshin return buf; 224853541Sshin} 224953541Sshin 225053541Sshinvoid 2251171259Sdelphijicmp6_redirect_input(struct mbuf *m, int off) 225253541Sshin{ 2253183550Szec INIT_VNET_INET6(curvnet); 2254171133Sgnn struct ifnet *ifp; 225553541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 225662587Sitojun struct nd_redirect *nd_rd; 225753541Sshin int icmp6len = ntohs(ip6->ip6_plen); 225853541Sshin char *lladdr = NULL; 225953541Sshin int lladdrlen = 0; 226053541Sshin u_char *redirhdr = NULL; 226153541Sshin int redirhdrlen = 0; 226253541Sshin struct rtentry *rt = NULL; 226353541Sshin int is_router; 226453541Sshin int is_onlink; 226553541Sshin struct in6_addr src6 = ip6->ip6_src; 226662587Sitojun struct in6_addr redtgt6; 226762587Sitojun struct in6_addr reddst6; 226853541Sshin union nd_opts ndopts; 2269165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 227053541Sshin 2271171133Sgnn if (!m) 227253541Sshin return; 227353541Sshin 2274171133Sgnn ifp = m->m_pkthdr.rcvif; 2275171133Sgnn 2276171133Sgnn if (!ifp) 2277171133Sgnn return; 2278171133Sgnn 227953541Sshin /* XXX if we are router, we don't update route by icmp6 redirect */ 2280181803Sbz if (V_ip6_forwarding) 228162587Sitojun goto freeit; 2282181803Sbz if (!V_icmp6_rediraccept) 228362587Sitojun goto freeit; 228462587Sitojun 228562587Sitojun#ifndef PULLDOWN_TEST 228662587Sitojun IP6_EXTHDR_CHECK(m, off, icmp6len,); 228762587Sitojun nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); 228862587Sitojun#else 228962587Sitojun IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); 229062587Sitojun if (nd_rd == NULL) { 2291190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 229253541Sshin return; 229362587Sitojun } 229462587Sitojun#endif 229562587Sitojun redtgt6 = nd_rd->nd_rd_target; 229662587Sitojun reddst6 = nd_rd->nd_rd_dst; 229753541Sshin 2298148385Sume if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) || 2299148385Sume in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) { 2300148385Sume goto freeit; 2301148385Sume } 230253541Sshin 230353541Sshin /* validation */ 230453541Sshin if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 230578064Sume nd6log((LOG_ERR, 2306120891Sume "ICMP6 redirect sent from %s rejected; " 2307120891Sume "must be from linklocal\n", 2308165118Sbz ip6_sprintf(ip6buf, &src6))); 230978064Sume goto bad; 231053541Sshin } 231153541Sshin if (ip6->ip6_hlim != 255) { 231278064Sume nd6log((LOG_ERR, 2313120891Sume "ICMP6 redirect sent from %s rejected; " 2314120891Sume "hlim=%d (must be 255)\n", 2315165118Sbz ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim)); 231678064Sume goto bad; 231753541Sshin } 231853541Sshin { 231953541Sshin /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 232053541Sshin struct sockaddr_in6 sin6; 232153541Sshin struct in6_addr *gw6; 232253541Sshin 232353541Sshin bzero(&sin6, sizeof(sin6)); 232453541Sshin sin6.sin6_family = AF_INET6; 232553541Sshin sin6.sin6_len = sizeof(struct sockaddr_in6); 232653541Sshin bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); 232753541Sshin rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 232853541Sshin if (rt) { 232965895Sume if (rt->rt_gateway == NULL || 233065895Sume rt->rt_gateway->sa_family != AF_INET6) { 233178064Sume nd6log((LOG_ERR, 233265895Sume "ICMP6 redirect rejected; no route " 233365895Sume "with inet6 gateway found for redirect dst: %s\n", 233478064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2335120727Ssam RTFREE_LOCKED(rt); 233678064Sume goto bad; 233765895Sume } 233865895Sume 233953541Sshin gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); 234053541Sshin if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { 234178064Sume nd6log((LOG_ERR, 2342120891Sume "ICMP6 redirect rejected; " 2343120891Sume "not equal to gw-for-src=%s (must be same): " 2344120891Sume "%s\n", 2345165118Sbz ip6_sprintf(ip6buf, gw6), 2346120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2347120727Ssam RTFREE_LOCKED(rt); 234878064Sume goto bad; 234953541Sshin } 235053541Sshin } else { 235178064Sume nd6log((LOG_ERR, 2352120891Sume "ICMP6 redirect rejected; " 2353120891Sume "no route found for redirect dst: %s\n", 2354120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 235578064Sume goto bad; 235653541Sshin } 2357120727Ssam RTFREE_LOCKED(rt); 235853541Sshin rt = NULL; 235953541Sshin } 236053541Sshin if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 236178064Sume nd6log((LOG_ERR, 2362120891Sume "ICMP6 redirect rejected; " 2363120891Sume "redirect dst must be unicast: %s\n", 2364120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 236578064Sume goto bad; 236653541Sshin } 236753541Sshin 236853541Sshin is_router = is_onlink = 0; 236953541Sshin if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 237053541Sshin is_router = 1; /* router case */ 237153541Sshin if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 237253541Sshin is_onlink = 1; /* on-link destination case */ 237353541Sshin if (!is_router && !is_onlink) { 237478064Sume nd6log((LOG_ERR, 2375120891Sume "ICMP6 redirect rejected; " 2376120891Sume "neither router case nor onlink case: %s\n", 2377120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 237878064Sume goto bad; 237953541Sshin } 238053541Sshin /* validation passed */ 238153541Sshin 238253541Sshin icmp6len -= sizeof(*nd_rd); 238353541Sshin nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 238453541Sshin if (nd6_options(&ndopts) < 0) { 238578064Sume nd6log((LOG_INFO, "icmp6_redirect_input: " 2386120891Sume "invalid ND option, rejected: %s\n", 2387120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 238878064Sume /* nd6_options have incremented stats */ 238962587Sitojun goto freeit; 239053541Sshin } 239153541Sshin 239253541Sshin if (ndopts.nd_opts_tgt_lladdr) { 239353541Sshin lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 239453541Sshin lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 239553541Sshin } 239653541Sshin 239753541Sshin if (ndopts.nd_opts_rh) { 239853541Sshin redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len; 239953541Sshin redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */ 240053541Sshin } 240153541Sshin 240253541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 240378064Sume nd6log((LOG_INFO, 2404120891Sume "icmp6_redirect_input: lladdrlen mismatch for %s " 2405120891Sume "(if %d, icmp6 packet %d): %s\n", 2406165118Sbz ip6_sprintf(ip6buf, &redtgt6), 2407165118Sbz ifp->if_addrlen, lladdrlen - 2, 2408120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 240978064Sume goto bad; 241053541Sshin } 241153541Sshin 241253541Sshin /* RFC 2461 8.3 */ 241353541Sshin nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 2414120891Sume is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 241553541Sshin 241695023Ssuz if (!is_onlink) { /* better router case. perform rtredirect. */ 241753541Sshin /* perform rtredirect */ 241853541Sshin struct sockaddr_in6 sdst; 241953541Sshin struct sockaddr_in6 sgw; 242053541Sshin struct sockaddr_in6 ssrc; 242153541Sshin 242253541Sshin bzero(&sdst, sizeof(sdst)); 242353541Sshin bzero(&sgw, sizeof(sgw)); 242453541Sshin bzero(&ssrc, sizeof(ssrc)); 242553541Sshin sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; 242653541Sshin sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = 242753541Sshin sizeof(struct sockaddr_in6); 242853541Sshin bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); 242953541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 243053541Sshin bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 243153541Sshin rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw, 2432120891Sume (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST, 2433120891Sume (struct sockaddr *)&ssrc); 243453541Sshin } 243553541Sshin /* finally update cached route in each socket via pfctlinput */ 243653541Sshin { 243753541Sshin struct sockaddr_in6 sdst; 243853541Sshin 243953541Sshin bzero(&sdst, sizeof(sdst)); 244053541Sshin sdst.sin6_family = AF_INET6; 244153541Sshin sdst.sin6_len = sizeof(struct sockaddr_in6); 244253541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 244353541Sshin pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); 2444171167Sgnn#ifdef IPSEC 244553541Sshin key_sa_routechange((struct sockaddr *)&sdst); 2446171167Sgnn#endif /* IPSEC */ 244753541Sshin } 244862587Sitojun 244962587Sitojun freeit: 245062587Sitojun m_freem(m); 245178064Sume return; 245278064Sume 245378064Sume bad: 2454190964Srwatson ICMP6STAT_INC(icp6s_badredirect); 245578064Sume m_freem(m); 245653541Sshin} 245753541Sshin 245853541Sshinvoid 2459171259Sdelphijicmp6_redirect_output(struct mbuf *m0, struct rtentry *rt) 246053541Sshin{ 2461183550Szec INIT_VNET_INET6(curvnet); 246253541Sshin struct ifnet *ifp; /* my outgoing interface */ 246353541Sshin struct in6_addr *ifp_ll6; 246453541Sshin struct in6_addr *router_ll6; 246553541Sshin struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 246653541Sshin struct mbuf *m = NULL; /* newly allocated one */ 246753541Sshin struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 246853541Sshin struct nd_redirect *nd_rd; 2469186157Skmacy struct llentry *ln = NULL; 247053541Sshin size_t maxlen; 247153541Sshin u_char *p; 247253541Sshin struct ifnet *outif = NULL; 247362587Sitojun struct sockaddr_in6 src_sa; 247453541Sshin 2475181803Sbz icmp6_errcount(&V_icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0); 247662587Sitojun 247753541Sshin /* if we are not router, we don't send icmp6 redirect */ 2478181803Sbz if (!V_ip6_forwarding) 247953541Sshin goto fail; 248053541Sshin 248153541Sshin /* sanity check */ 248253541Sshin if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) 248353541Sshin goto fail; 248453541Sshin 248553541Sshin /* 248653541Sshin * Address check: 248753541Sshin * the source address must identify a neighbor, and 248853541Sshin * the destination address must not be a multicast address 248953541Sshin * [RFC 2461, sec 8.2] 249053541Sshin */ 249153541Sshin sip6 = mtod(m0, struct ip6_hdr *); 249262587Sitojun bzero(&src_sa, sizeof(src_sa)); 249362587Sitojun src_sa.sin6_family = AF_INET6; 249462587Sitojun src_sa.sin6_len = sizeof(src_sa); 249562587Sitojun src_sa.sin6_addr = sip6->ip6_src; 249662587Sitojun if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) 249753541Sshin goto fail; 249853541Sshin if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) 249953541Sshin goto fail; /* what should we do here? */ 250053541Sshin 250153541Sshin /* rate limit */ 250253541Sshin if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) 250353541Sshin goto fail; 250453541Sshin 250553541Sshin /* 250653541Sshin * Since we are going to append up to 1280 bytes (= IPV6_MMTU), 250753541Sshin * we almost always ask for an mbuf cluster for simplicity. 250853541Sshin * (MHLEN < IPV6_MMTU is almost always true) 250953541Sshin */ 251062587Sitojun#if IPV6_MMTU >= MCLBYTES 251162587Sitojun# error assumption failed about IPV6_MMTU and MCLBYTES 251262587Sitojun#endif 2513111119Simp MGETHDR(m, M_DONTWAIT, MT_HEADER); 251462587Sitojun if (m && IPV6_MMTU >= MHLEN) 2515111119Simp MCLGET(m, M_DONTWAIT); 251653541Sshin if (!m) 251753541Sshin goto fail; 251878064Sume m->m_pkthdr.rcvif = NULL; 251978064Sume m->m_len = 0; 252078064Sume maxlen = M_TRAILINGSPACE(m); 252153541Sshin maxlen = min(IPV6_MMTU, maxlen); 252253541Sshin /* just for safety */ 252362587Sitojun if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 252462587Sitojun ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { 252553541Sshin goto fail; 252662587Sitojun } 252753541Sshin 252853541Sshin { 252953541Sshin /* get ip6 linklocal address for ifp(my outgoing interface). */ 253062587Sitojun struct in6_ifaddr *ia; 253162587Sitojun if ((ia = in6ifa_ifpforlinklocal(ifp, 253262587Sitojun IN6_IFF_NOTREADY| 253362587Sitojun IN6_IFF_ANYCAST)) == NULL) 253453541Sshin goto fail; 253553541Sshin ifp_ll6 = &ia->ia_addr.sin6_addr; 253653541Sshin } 253753541Sshin 253853541Sshin /* get ip6 linklocal address for the router. */ 253953541Sshin if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { 254053541Sshin struct sockaddr_in6 *sin6; 254153541Sshin sin6 = (struct sockaddr_in6 *)rt->rt_gateway; 254253541Sshin router_ll6 = &sin6->sin6_addr; 254353541Sshin if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) 254453541Sshin router_ll6 = (struct in6_addr *)NULL; 254553541Sshin } else 254653541Sshin router_ll6 = (struct in6_addr *)NULL; 254753541Sshin 254853541Sshin /* ip6 */ 254953541Sshin ip6 = mtod(m, struct ip6_hdr *); 255053541Sshin ip6->ip6_flow = 0; 255162587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 255262587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 255353541Sshin /* ip6->ip6_plen will be set later */ 255453541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 255553541Sshin ip6->ip6_hlim = 255; 255653541Sshin /* ip6->ip6_src must be linklocal addr for my outgoing if. */ 255753541Sshin bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); 255853541Sshin bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); 255953541Sshin 256053541Sshin /* ND Redirect */ 256153541Sshin nd_rd = (struct nd_redirect *)(ip6 + 1); 256253541Sshin nd_rd->nd_rd_type = ND_REDIRECT; 256353541Sshin nd_rd->nd_rd_code = 0; 256453541Sshin nd_rd->nd_rd_reserved = 0; 256553541Sshin if (rt->rt_flags & RTF_GATEWAY) { 256653541Sshin /* 256753541Sshin * nd_rd->nd_rd_target must be a link-local address in 256853541Sshin * better router cases. 256953541Sshin */ 257053541Sshin if (!router_ll6) 257153541Sshin goto fail; 257253541Sshin bcopy(router_ll6, &nd_rd->nd_rd_target, 2573120892Sume sizeof(nd_rd->nd_rd_target)); 257453541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2575120892Sume sizeof(nd_rd->nd_rd_dst)); 257653541Sshin } else { 257753541Sshin /* make sure redtgt == reddst */ 257853541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, 2579120892Sume sizeof(nd_rd->nd_rd_target)); 258053541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2581120892Sume sizeof(nd_rd->nd_rd_dst)); 258253541Sshin } 258353541Sshin 258453541Sshin p = (u_char *)(nd_rd + 1); 258553541Sshin 258653541Sshin if (!router_ll6) 258753541Sshin goto nolladdropt; 258853541Sshin 2589120892Sume { 2590120892Sume /* target lladdr option */ 2591120892Sume int len; 2592120892Sume struct nd_opt_hdr *nd_opt; 2593120892Sume char *lladdr; 259453541Sshin 2595186119Sqingli IF_AFDATA_LOCK(ifp); 2596186119Sqingli ln = nd6_lookup(router_ll6, 0, ifp); 2597186119Sqingli IF_AFDATA_UNLOCK(ifp); 2598186157Skmacy if (ln == NULL) 2599120892Sume goto nolladdropt; 2600186119Sqingli 2601120892Sume len = sizeof(*nd_opt) + ifp->if_addrlen; 2602120892Sume len = (len + 7) & ~7; /* round by 8 */ 2603120892Sume /* safety check */ 2604186157Skmacy if (len + (p - (u_char *)ip6) > maxlen) 2605120892Sume goto nolladdropt; 2606186119Sqingli 2607186119Sqingli if (ln->la_flags & LLE_VALID) { 2608120892Sume nd_opt = (struct nd_opt_hdr *)p; 2609120892Sume nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 2610120892Sume nd_opt->nd_opt_len = len >> 3; 2611120892Sume lladdr = (char *)(nd_opt + 1); 2612186119Sqingli bcopy(&ln->ll_addr, lladdr, ifp->if_addrlen); 2613120892Sume p += len; 2614120892Sume } 2615186157Skmacy } 2616186157Skmacynolladdropt: 2617186157Skmacy if (ln != NULL) 2618186119Sqingli LLE_RUNLOCK(ln); 2619186157Skmacy 262053541Sshin m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 262153541Sshin 262253541Sshin /* just to be safe */ 262362587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 262453541Sshin if (m0->m_flags & M_DECRYPTED) 262553541Sshin goto noredhdropt; 262662587Sitojun#endif 262762587Sitojun if (p - (u_char *)ip6 > maxlen) 262862587Sitojun goto noredhdropt; 262953541Sshin 2630120891Sume { 2631120891Sume /* redirected header option */ 2632120891Sume int len; 2633120891Sume struct nd_opt_rd_hdr *nd_opt_rh; 263453541Sshin 2635120891Sume /* 2636120891Sume * compute the maximum size for icmp6 redirect header option. 2637120891Sume * XXX room for auth header? 2638120891Sume */ 2639120891Sume len = maxlen - (p - (u_char *)ip6); 2640120891Sume len &= ~7; 264153541Sshin 2642120891Sume /* This is just for simplicity. */ 2643120891Sume if (m0->m_pkthdr.len != m0->m_len) { 2644120891Sume if (m0->m_next) { 2645120891Sume m_freem(m0->m_next); 2646120891Sume m0->m_next = NULL; 2647120891Sume } 2648120891Sume m0->m_pkthdr.len = m0->m_len; 264953541Sshin } 265053541Sshin 2651120891Sume /* 2652120891Sume * Redirected header option spec (RFC2461 4.6.3) talks nothing 2653120891Sume * about padding/truncate rule for the original IP packet. 2654120891Sume * From the discussion on IPv6imp in Feb 1999, 2655120891Sume * the consensus was: 2656120891Sume * - "attach as much as possible" is the goal 2657120891Sume * - pad if not aligned (original size can be guessed by 2658120891Sume * original ip6 header) 2659120891Sume * Following code adds the padding if it is simple enough, 2660120891Sume * and truncates if not. 2661120891Sume */ 2662120891Sume if (m0->m_next || m0->m_pkthdr.len != m0->m_len) 2663120891Sume panic("assumption failed in %s:%d", __FILE__, 2664120891Sume __LINE__); 266553541Sshin 2666120891Sume if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { 2667120891Sume /* not enough room, truncate */ 2668120891Sume m0->m_pkthdr.len = m0->m_len = len - 2669120891Sume sizeof(*nd_opt_rh); 2670120891Sume } else { 2671120891Sume /* enough room, pad or truncate */ 2672120891Sume size_t extra; 267353541Sshin 2674120891Sume extra = m0->m_pkthdr.len % 8; 2675120891Sume if (extra) { 2676120891Sume /* pad if easy enough, truncate if not */ 2677120891Sume if (8 - extra <= M_TRAILINGSPACE(m0)) { 2678120891Sume /* pad */ 2679120891Sume m0->m_len += (8 - extra); 2680120891Sume m0->m_pkthdr.len += (8 - extra); 2681120891Sume } else { 2682120891Sume /* truncate */ 2683120891Sume m0->m_pkthdr.len -= extra; 2684120891Sume m0->m_len -= extra; 2685120891Sume } 268653541Sshin } 2687120891Sume len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); 2688120891Sume m0->m_pkthdr.len = m0->m_len = len - 2689120891Sume sizeof(*nd_opt_rh); 269053541Sshin } 269153541Sshin 2692120891Sume nd_opt_rh = (struct nd_opt_rd_hdr *)p; 2693120891Sume bzero(nd_opt_rh, sizeof(*nd_opt_rh)); 2694120891Sume nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; 2695120891Sume nd_opt_rh->nd_opt_rh_len = len >> 3; 2696120891Sume p += sizeof(*nd_opt_rh); 2697120891Sume m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 269853541Sshin 2699120891Sume /* connect m0 to m */ 2700120891Sume m_tag_delete_chain(m0, NULL); 2701120891Sume m0->m_flags &= ~M_PKTHDR; 2702120891Sume m->m_next = m0; 2703120891Sume m->m_pkthdr.len = m->m_len + m0->m_len; 2704120891Sume m0 = NULL; 2705120891Sume } 270653541Sshinnoredhdropt:; 2707112781Ssuz if (m0) { 2708112781Ssuz m_freem(m0); 2709112781Ssuz m0 = NULL; 2710112781Ssuz } 271153541Sshin 2712121315Sume /* XXX: clear embedded link IDs in the inner header */ 2713121315Sume in6_clearscope(&sip6->ip6_src); 2714121315Sume in6_clearscope(&sip6->ip6_dst); 2715121315Sume in6_clearscope(&nd_rd->nd_rd_target); 2716121315Sume in6_clearscope(&nd_rd->nd_rd_dst); 271753541Sshin 271853541Sshin ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 271953541Sshin 272053541Sshin nd_rd->nd_rd_cksum = 0; 2721120891Sume nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2722120891Sume sizeof(*ip6), ntohs(ip6->ip6_plen)); 272353541Sshin 272453541Sshin /* send the packet to outside... */ 2725105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 272653541Sshin if (outif) { 272753541Sshin icmp6_ifstat_inc(outif, ifs6_out_msg); 272853541Sshin icmp6_ifstat_inc(outif, ifs6_out_redirect); 272953541Sshin } 2730190964Srwatson ICMP6STAT_INC(icp6s_outhist[ND_REDIRECT]); 273153541Sshin 273253541Sshin return; 273353541Sshin 273453541Sshinfail: 273553541Sshin if (m) 273653541Sshin m_freem(m); 273753541Sshin if (m0) 273853541Sshin m_freem(m0); 273953541Sshin} 274053541Sshin 274153541Sshin/* 274253541Sshin * ICMPv6 socket option processing. 274353541Sshin */ 274453541Sshinint 2745171259Sdelphijicmp6_ctloutput(struct socket *so, struct sockopt *sopt) 274653541Sshin{ 274753541Sshin int error = 0; 274853541Sshin int optlen; 274978064Sume struct inpcb *inp = sotoinpcb(so); 275053541Sshin int level, op, optname; 275153541Sshin 275253541Sshin if (sopt) { 275353541Sshin level = sopt->sopt_level; 275453541Sshin op = sopt->sopt_dir; 275553541Sshin optname = sopt->sopt_name; 275653541Sshin optlen = sopt->sopt_valsize; 275753541Sshin } else 275853541Sshin level = op = optname = optlen = 0; 275978064Sume 276053541Sshin if (level != IPPROTO_ICMPV6) { 276153541Sshin return EINVAL; 276253541Sshin } 276353541Sshin 276478064Sume switch (op) { 276553541Sshin case PRCO_SETOPT: 276653541Sshin switch (optname) { 276753541Sshin case ICMP6_FILTER: 276853541Sshin { 2769180968Srwatson struct icmp6_filter ic6f; 277053541Sshin 2771180968Srwatson if (optlen != sizeof(ic6f)) { 277253541Sshin error = EMSGSIZE; 277353541Sshin break; 277453541Sshin } 2775180968Srwatson error = sooptcopyin(sopt, &ic6f, optlen, optlen); 2776180968Srwatson if (error == 0) { 2777180968Srwatson INP_WLOCK(inp); 2778180968Srwatson *inp->in6p_icmp6filt = ic6f; 2779180968Srwatson INP_WUNLOCK(inp); 2780180968Srwatson } 278153541Sshin break; 278253541Sshin } 278353541Sshin 278453541Sshin default: 278553541Sshin error = ENOPROTOOPT; 278653541Sshin break; 278753541Sshin } 278853541Sshin break; 278953541Sshin 279053541Sshin case PRCO_GETOPT: 279153541Sshin switch (optname) { 279253541Sshin case ICMP6_FILTER: 279353541Sshin { 2794180968Srwatson struct icmp6_filter ic6f; 2795180968Srwatson 2796180968Srwatson INP_RLOCK(inp); 2797180968Srwatson ic6f = *inp->in6p_icmp6filt; 2798180968Srwatson INP_RUNLOCK(inp); 2799180968Srwatson error = sooptcopyout(sopt, &ic6f, sizeof(ic6f)); 280053541Sshin break; 280153541Sshin } 280253541Sshin 280353541Sshin default: 280453541Sshin error = ENOPROTOOPT; 280553541Sshin break; 280653541Sshin } 280753541Sshin break; 280853541Sshin } 280953541Sshin 2810120856Sume return (error); 281153541Sshin} 281253541Sshin 281353541Sshin/* 281453541Sshin * Perform rate limit check. 281553541Sshin * Returns 0 if it is okay to send the icmp6 packet. 281653541Sshin * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 281753541Sshin * limitation. 281853541Sshin * 281953541Sshin * XXX per-destination/type check necessary? 2820171259Sdelphij * 2821171259Sdelphij * dst - not used at this moment 2822171259Sdelphij * type - not used at this moment 2823171259Sdelphij * code - not used at this moment 282453541Sshin */ 282553541Sshinstatic int 2826171259Sdelphijicmp6_ratelimit(const struct in6_addr *dst, const int type, 2827171259Sdelphij const int code) 282853541Sshin{ 2829183550Szec INIT_VNET_INET6(curvnet); 283062587Sitojun int ret; 283153541Sshin 283295023Ssuz ret = 0; /* okay to send */ 283353541Sshin 283462587Sitojun /* PPS limit */ 2835181803Sbz if (!ppsratecheck(&V_icmp6errppslim_last, &V_icmp6errpps_count, 2836181803Sbz V_icmp6errppslim)) { 283753541Sshin /* The packet is subject to rate limit */ 283862587Sitojun ret++; 283953541Sshin } 284053541Sshin 284162587Sitojun return ret; 284253541Sshin} 2843