icmp6.c revision 145065
162587Sitojun/* $FreeBSD: head/sys/netinet6/icmp6.c 145065 2005-04-14 11:41:23Z gnn $ */ 278064Sume/* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ */ 362587Sitojun 4139826Simp/*- 553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 653541Sshin * All rights reserved. 753541Sshin * 853541Sshin * Redistribution and use in source and binary forms, with or without 953541Sshin * modification, are permitted provided that the following conditions 1053541Sshin * are met: 1153541Sshin * 1. Redistributions of source code must retain the above copyright 1253541Sshin * notice, this list of conditions and the following disclaimer. 1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1453541Sshin * notice, this list of conditions and the following disclaimer in the 1553541Sshin * documentation and/or other materials provided with the distribution. 1653541Sshin * 3. Neither the name of the project nor the names of its contributors 1753541Sshin * may be used to endorse or promote products derived from this software 1853541Sshin * without specific prior written permission. 1953541Sshin * 2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2353541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3053541Sshin * SUCH DAMAGE. 3153541Sshin */ 3253541Sshin 33139826Simp/*- 3453541Sshin * Copyright (c) 1982, 1986, 1988, 1993 3553541Sshin * The Regents of the University of California. All rights reserved. 3653541Sshin * 3753541Sshin * Redistribution and use in source and binary forms, with or without 3853541Sshin * modification, are permitted provided that the following conditions 3953541Sshin * are met: 4053541Sshin * 1. Redistributions of source code must retain the above copyright 4153541Sshin * notice, this list of conditions and the following disclaimer. 4253541Sshin * 2. Redistributions in binary form must reproduce the above copyright 4353541Sshin * notice, this list of conditions and the following disclaimer in the 4453541Sshin * documentation and/or other materials provided with the distribution. 4553541Sshin * 4. Neither the name of the University nor the names of its contributors 4653541Sshin * may be used to endorse or promote products derived from this software 4753541Sshin * without specific prior written permission. 4853541Sshin * 4953541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5053541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5153541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5253541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5353541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5453541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5553541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5653541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5753541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5853541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5953541Sshin * SUCH DAMAGE. 6053541Sshin * 6153541Sshin * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 6253541Sshin */ 6353541Sshin 6462587Sitojun#include "opt_inet.h" 6562587Sitojun#include "opt_inet6.h" 6655009Sshin#include "opt_ipsec.h" 6753541Sshin 6853541Sshin#include <sys/param.h> 6995759Stanimura#include <sys/domain.h> 7095759Stanimura#include <sys/kernel.h> 7195759Stanimura#include <sys/lock.h> 7278064Sume#include <sys/malloc.h> 7353541Sshin#include <sys/mbuf.h> 7453541Sshin#include <sys/protosw.h> 7595759Stanimura#include <sys/signalvar.h> 7653541Sshin#include <sys/socket.h> 7753541Sshin#include <sys/socketvar.h> 7895759Stanimura#include <sys/sx.h> 7995759Stanimura#include <sys/syslog.h> 8095759Stanimura#include <sys/systm.h> 8153541Sshin#include <sys/time.h> 8253541Sshin 8353541Sshin#include <net/if.h> 8453541Sshin#include <net/if_dl.h> 8553541Sshin#include <net/if_types.h> 8695759Stanimura#include <net/route.h> 8753541Sshin 8853541Sshin#include <netinet/in.h> 8995759Stanimura#include <netinet/in_pcb.h> 9053541Sshin#include <netinet/in_var.h> 9162587Sitojun#include <netinet/ip6.h> 9295759Stanimura#include <netinet/icmp6.h> 93122922Sandre#include <netinet/tcp_var.h> 9495759Stanimura#include <netinet6/in6_ifattach.h> 9595759Stanimura#include <netinet6/in6_pcb.h> 9695759Stanimura#include <netinet6/ip6protosw.h> 9753541Sshin#include <netinet6/ip6_var.h> 9853541Sshin#include <netinet6/mld6_var.h> 9953541Sshin#include <netinet6/nd6.h> 10053541Sshin 10153541Sshin#ifdef IPSEC 10253541Sshin#include <netinet6/ipsec.h> 10353541Sshin#include <netkey/key.h> 10455009Sshin#endif 10553541Sshin 106105199Ssam#ifdef FAST_IPSEC 107105199Ssam#include <netipsec/ipsec.h> 108105199Ssam#include <netipsec/key.h> 109105199Ssam#endif 110105199Ssam 11153541Sshin#include <net/net_osdep.h> 11253541Sshin 11362587Sitojunextern struct domain inet6domain; 11453541Sshin 11562587Sitojunstruct icmp6stat icmp6stat; 11653541Sshin 117141553Srwatsonextern struct inpcbinfo ripcbinfo; 11862587Sitojunextern struct inpcbhead ripcb; 11962587Sitojunextern int icmp6errppslim; 12062587Sitojunstatic int icmp6errpps_count = 0; 12178064Sumestatic struct timeval icmp6errppslim_last; 12262587Sitojunextern int icmp6_nodeinfo; 12353541Sshin 12462587Sitojunstatic void icmp6_errcount __P((struct icmp6errstat *, int, int)); 12562587Sitojunstatic int icmp6_rip6_input __P((struct mbuf **, int)); 12662587Sitojunstatic int icmp6_ratelimit __P((const struct in6_addr *, const int, const int)); 12762587Sitojunstatic const char *icmp6_redirect_diag __P((struct in6_addr *, 12862587Sitojun struct in6_addr *, struct in6_addr *)); 12962587Sitojunstatic struct mbuf *ni6_input __P((struct mbuf *, int)); 13062587Sitojunstatic struct mbuf *ni6_nametodns __P((const char *, int, int)); 13162587Sitojunstatic int ni6_dnsmatch __P((const char *, int, const char *, int)); 13262587Sitojunstatic int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *, 13378064Sume struct ifnet **, char *)); 13462587Sitojunstatic int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, 13562587Sitojun struct ifnet *, int)); 136142681Sumestatic int icmp6_notify_error __P((struct mbuf **, int, int, int)); 13753541Sshin 13853541Sshin#ifdef COMPAT_RFC1885 13962587Sitojunstatic struct route_in6 icmp6_reflect_rt; 14053541Sshin#endif 14153541Sshin 14278064Sume 14353541Sshinvoid 14453541Sshinicmp6_init() 14553541Sshin{ 14653541Sshin mld6_init(); 14753541Sshin} 14853541Sshin 14962587Sitojunstatic void 15062587Sitojunicmp6_errcount(stat, type, code) 15162587Sitojun struct icmp6errstat *stat; 15262587Sitojun int type, code; 15362587Sitojun{ 15478064Sume switch (type) { 15562587Sitojun case ICMP6_DST_UNREACH: 15662587Sitojun switch (code) { 15762587Sitojun case ICMP6_DST_UNREACH_NOROUTE: 15862587Sitojun stat->icp6errs_dst_unreach_noroute++; 15962587Sitojun return; 16062587Sitojun case ICMP6_DST_UNREACH_ADMIN: 16162587Sitojun stat->icp6errs_dst_unreach_admin++; 16262587Sitojun return; 16362587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 16462587Sitojun stat->icp6errs_dst_unreach_beyondscope++; 16562587Sitojun return; 16662587Sitojun case ICMP6_DST_UNREACH_ADDR: 16762587Sitojun stat->icp6errs_dst_unreach_addr++; 16862587Sitojun return; 16962587Sitojun case ICMP6_DST_UNREACH_NOPORT: 17062587Sitojun stat->icp6errs_dst_unreach_noport++; 17162587Sitojun return; 17262587Sitojun } 17362587Sitojun break; 17462587Sitojun case ICMP6_PACKET_TOO_BIG: 17562587Sitojun stat->icp6errs_packet_too_big++; 17662587Sitojun return; 17762587Sitojun case ICMP6_TIME_EXCEEDED: 17878064Sume switch (code) { 17962587Sitojun case ICMP6_TIME_EXCEED_TRANSIT: 18062587Sitojun stat->icp6errs_time_exceed_transit++; 18162587Sitojun return; 18262587Sitojun case ICMP6_TIME_EXCEED_REASSEMBLY: 18362587Sitojun stat->icp6errs_time_exceed_reassembly++; 18462587Sitojun return; 18562587Sitojun } 18662587Sitojun break; 18762587Sitojun case ICMP6_PARAM_PROB: 18878064Sume switch (code) { 18962587Sitojun case ICMP6_PARAMPROB_HEADER: 19062587Sitojun stat->icp6errs_paramprob_header++; 19162587Sitojun return; 19262587Sitojun case ICMP6_PARAMPROB_NEXTHEADER: 19362587Sitojun stat->icp6errs_paramprob_nextheader++; 19462587Sitojun return; 19562587Sitojun case ICMP6_PARAMPROB_OPTION: 19662587Sitojun stat->icp6errs_paramprob_option++; 19762587Sitojun return; 19862587Sitojun } 19962587Sitojun break; 20062587Sitojun case ND_REDIRECT: 20162587Sitojun stat->icp6errs_redirect++; 20262587Sitojun return; 20362587Sitojun } 20462587Sitojun stat->icp6errs_unknown++; 20562587Sitojun} 20662587Sitojun 20753541Sshin/* 20853541Sshin * Generate an error packet of type error in response to bad IP6 packet. 20953541Sshin */ 21053541Sshinvoid 21153541Sshinicmp6_error(m, type, code, param) 21253541Sshin struct mbuf *m; 21353541Sshin int type, code, param; 21453541Sshin{ 21553541Sshin struct ip6_hdr *oip6, *nip6; 21653541Sshin struct icmp6_hdr *icmp6; 21762587Sitojun u_int preplen; 21853541Sshin int off; 21962587Sitojun int nxt; 22053541Sshin 22153541Sshin icmp6stat.icp6s_error++; 22253541Sshin 22362587Sitojun /* count per-type-code statistics */ 22462587Sitojun icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code); 22562587Sitojun 22662587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 22762587Sitojun if (m->m_flags & M_DECRYPTED) { 22862587Sitojun icmp6stat.icp6s_canterror++; 22953541Sshin goto freeit; 23062587Sitojun } 23162587Sitojun#endif 23253541Sshin 23362587Sitojun#ifndef PULLDOWN_TEST 23462587Sitojun IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 23562587Sitojun#else 23662587Sitojun if (m->m_len < sizeof(struct ip6_hdr)) { 23762587Sitojun m = m_pullup(m, sizeof(struct ip6_hdr)); 23862587Sitojun if (m == NULL) 23962587Sitojun return; 24062587Sitojun } 24162587Sitojun#endif 24253541Sshin oip6 = mtod(m, struct ip6_hdr *); 24353541Sshin 24453541Sshin /* 245121472Sume * If the destination address of the erroneous packet is a multicast 246121472Sume * address, or the packet was sent using link-layer multicast, 247121472Sume * we should basically suppress sending an error (RFC 2463, Section 248121472Sume * 2.4). 249121472Sume * We have two exceptions (the item e.2 in that section): 250121472Sume * - the Pakcet Too Big message can be sent for path MTU discovery. 251121472Sume * - the Parameter Problem Message that can be allowed an icmp6 error 252121472Sume * in the option type field. This check has been done in 253121472Sume * ip6_unknown_opt(), so we can just check the type and code. 25453541Sshin */ 25553541Sshin if ((m->m_flags & (M_BCAST|M_MCAST) || 25653541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && 25753541Sshin (type != ICMP6_PACKET_TOO_BIG && 25853541Sshin (type != ICMP6_PARAM_PROB || 25953541Sshin code != ICMP6_PARAMPROB_OPTION))) 26053541Sshin goto freeit; 26153541Sshin 262121472Sume /* 263121472Sume * RFC 2463, 2.4 (e.5): source address check. 264121472Sume * XXX: the case of anycast source? 265121472Sume */ 26653541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || 26753541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) 26853541Sshin goto freeit; 26953541Sshin 27053541Sshin /* 27162587Sitojun * If we are about to send ICMPv6 against ICMPv6 error/redirect, 27262587Sitojun * don't do it. 27353541Sshin */ 27462587Sitojun nxt = -1; 27562587Sitojun off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 27662587Sitojun if (off >= 0 && nxt == IPPROTO_ICMPV6) { 27753541Sshin struct icmp6_hdr *icp; 27853541Sshin 27962587Sitojun#ifndef PULLDOWN_TEST 28062587Sitojun IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), ); 28162587Sitojun icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 28262587Sitojun#else 28362587Sitojun IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, 28462587Sitojun sizeof(*icp)); 28562587Sitojun if (icp == NULL) { 28662587Sitojun icmp6stat.icp6s_tooshort++; 28762587Sitojun return; 28862587Sitojun } 28962587Sitojun#endif 29062587Sitojun if (icp->icmp6_type < ICMP6_ECHO_REQUEST || 29162587Sitojun icp->icmp6_type == ND_REDIRECT) { 29253541Sshin /* 29362587Sitojun * ICMPv6 error 29462587Sitojun * Special case: for redirect (which is 29562587Sitojun * informational) we must not send icmp6 error. 29653541Sshin */ 29762587Sitojun icmp6stat.icp6s_canterror++; 29862587Sitojun goto freeit; 29962587Sitojun } else { 30062587Sitojun /* ICMPv6 informational - send the error */ 30153541Sshin } 30262587Sitojun } else { 30362587Sitojun /* non-ICMPv6 - send the error */ 30453541Sshin } 30553541Sshin 30653541Sshin oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */ 30753541Sshin 30853541Sshin /* Finally, do rate limitation check. */ 30953541Sshin if (icmp6_ratelimit(&oip6->ip6_src, type, code)) { 31053541Sshin icmp6stat.icp6s_toofreq++; 31153541Sshin goto freeit; 31253541Sshin } 31353541Sshin 31453541Sshin /* 31553541Sshin * OK, ICMP6 can be generated. 31653541Sshin */ 31753541Sshin 31853541Sshin if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) 31953541Sshin m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); 32053541Sshin 32162587Sitojun preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 322111119Simp M_PREPEND(m, preplen, M_DONTWAIT); 32362587Sitojun if (m && m->m_len < preplen) 32462587Sitojun m = m_pullup(m, preplen); 32553541Sshin if (m == NULL) { 32678064Sume nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__)); 32753541Sshin return; 32853541Sshin } 32953541Sshin 33053541Sshin nip6 = mtod(m, struct ip6_hdr *); 33153541Sshin nip6->ip6_src = oip6->ip6_src; 33253541Sshin nip6->ip6_dst = oip6->ip6_dst; 33353541Sshin 334121315Sume in6_clearscope(&oip6->ip6_src); 335121315Sume in6_clearscope(&oip6->ip6_dst); 33653541Sshin 33753541Sshin icmp6 = (struct icmp6_hdr *)(nip6 + 1); 33853541Sshin icmp6->icmp6_type = type; 33953541Sshin icmp6->icmp6_code = code; 34053541Sshin icmp6->icmp6_pptr = htonl((u_int32_t)param); 34153541Sshin 34278064Sume /* 34378064Sume * icmp6_reflect() is designed to be in the input path. 34478064Sume * icmp6_error() can be called from both input and outut path, 34578064Sume * and if we are in output path rcvif could contain bogus value. 34678064Sume * clear m->m_pkthdr.rcvif for safety, we should have enough scope 34778064Sume * information in ip header (nip6). 34878064Sume */ 34978064Sume m->m_pkthdr.rcvif = NULL; 35078064Sume 35153541Sshin icmp6stat.icp6s_outhist[type]++; 35295023Ssuz icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ 35362587Sitojun 35462587Sitojun return; 35562587Sitojun 35662587Sitojun freeit: 35762587Sitojun /* 35862587Sitojun * If we can't tell wheter or not we can generate ICMP6, free it. 35962587Sitojun */ 36062587Sitojun m_freem(m); 36153541Sshin} 36253541Sshin 36353541Sshin/* 36453541Sshin * Process a received ICMP6 message. 36553541Sshin */ 36653541Sshinint 36753541Sshinicmp6_input(mp, offp, proto) 36853541Sshin struct mbuf **mp; 36953541Sshin int *offp, proto; 37053541Sshin{ 37153541Sshin struct mbuf *m = *mp, *n; 37253541Sshin struct ip6_hdr *ip6, *nip6; 37353541Sshin struct icmp6_hdr *icmp6, *nicmp6; 37453541Sshin int off = *offp; 37553541Sshin int icmp6len = m->m_pkthdr.len - *offp; 37653541Sshin int code, sum, noff; 37753541Sshin 37862587Sitojun#ifndef PULLDOWN_TEST 37953541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE); 38095023Ssuz /* m might change if M_LOOP. So, call mtod after this */ 38162587Sitojun#endif 38253541Sshin 38353541Sshin /* 38453541Sshin * Locate icmp6 structure in mbuf, and check 38553541Sshin * that not corrupted and of at least minimum length 38653541Sshin */ 38753541Sshin 38853541Sshin ip6 = mtod(m, struct ip6_hdr *); 38953541Sshin if (icmp6len < sizeof(struct icmp6_hdr)) { 39053541Sshin icmp6stat.icp6s_tooshort++; 39153541Sshin goto freeit; 39253541Sshin } 39353541Sshin 39453541Sshin /* 39553541Sshin * calculate the checksum 39653541Sshin */ 39762587Sitojun#ifndef PULLDOWN_TEST 39853541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 39962587Sitojun#else 40062587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 40162587Sitojun if (icmp6 == NULL) { 40262587Sitojun icmp6stat.icp6s_tooshort++; 40362587Sitojun return IPPROTO_DONE; 40462587Sitojun } 40562587Sitojun#endif 40653541Sshin code = icmp6->icmp6_code; 40753541Sshin 40853541Sshin if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { 40978064Sume nd6log((LOG_ERR, 41053541Sshin "ICMP6 checksum error(%d|%x) %s\n", 41178064Sume icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src))); 41253541Sshin icmp6stat.icp6s_checksum++; 41353541Sshin goto freeit; 41453541Sshin } 41553541Sshin 41683934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 41753541Sshin /* 41853541Sshin * Deliver very specific ICMP6 type only. 41953541Sshin * This is important to deilver TOOBIG. Otherwise PMTUD 42053541Sshin * will not work. 42153541Sshin */ 42253541Sshin switch (icmp6->icmp6_type) { 42353541Sshin case ICMP6_DST_UNREACH: 42453541Sshin case ICMP6_PACKET_TOO_BIG: 42553541Sshin case ICMP6_TIME_EXCEEDED: 42653541Sshin break; 42753541Sshin default: 42853541Sshin goto freeit; 42953541Sshin } 43053541Sshin } 43153541Sshin 43253541Sshin icmp6stat.icp6s_inhist[icmp6->icmp6_type]++; 43353541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg); 43453541Sshin if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) 43553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error); 43653541Sshin 43753541Sshin switch (icmp6->icmp6_type) { 43853541Sshin case ICMP6_DST_UNREACH: 43953541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach); 44053541Sshin switch (code) { 44153541Sshin case ICMP6_DST_UNREACH_NOROUTE: 44253541Sshin code = PRC_UNREACH_NET; 44353541Sshin break; 44453541Sshin case ICMP6_DST_UNREACH_ADMIN: 44553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib); 44662587Sitojun code = PRC_UNREACH_PROTOCOL; /* is this a good code? */ 44762587Sitojun break; 44853541Sshin case ICMP6_DST_UNREACH_ADDR: 44962587Sitojun code = PRC_HOSTDEAD; 45053541Sshin break; 45162587Sitojun#ifdef COMPAT_RFC1885 45253541Sshin case ICMP6_DST_UNREACH_NOTNEIGHBOR: 45353541Sshin code = PRC_UNREACH_SRCFAIL; 45453541Sshin break; 45562587Sitojun#else 45662587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 45762587Sitojun /* I mean "source address was incorrect." */ 45862587Sitojun code = PRC_PARAMPROB; 45962587Sitojun break; 46062587Sitojun#endif 46153541Sshin case ICMP6_DST_UNREACH_NOPORT: 46253541Sshin code = PRC_UNREACH_PORT; 46353541Sshin break; 46453541Sshin default: 46553541Sshin goto badcode; 46653541Sshin } 46753541Sshin goto deliver; 46853541Sshin break; 46953541Sshin 47053541Sshin case ICMP6_PACKET_TOO_BIG: 47153541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig); 47253541Sshin 473126194Sume /* validation is made in icmp6_mtudisc_update */ 474126194Sume 47553541Sshin code = PRC_MSGSIZE; 47653541Sshin 47762587Sitojun /* 47862587Sitojun * Updating the path MTU will be done after examining 47962587Sitojun * intermediate extension headers. 48062587Sitojun */ 48153541Sshin goto deliver; 48253541Sshin break; 48353541Sshin 48453541Sshin case ICMP6_TIME_EXCEEDED: 48553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed); 48653541Sshin switch (code) { 48753541Sshin case ICMP6_TIME_EXCEED_TRANSIT: 48853541Sshin case ICMP6_TIME_EXCEED_REASSEMBLY: 48953541Sshin code += PRC_TIMXCEED_INTRANS; 49053541Sshin break; 49153541Sshin default: 49253541Sshin goto badcode; 49353541Sshin } 49453541Sshin goto deliver; 49553541Sshin break; 49653541Sshin 49753541Sshin case ICMP6_PARAM_PROB: 49853541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob); 49953541Sshin switch (code) { 50053541Sshin case ICMP6_PARAMPROB_NEXTHEADER: 50153541Sshin code = PRC_UNREACH_PROTOCOL; 50253541Sshin break; 50353541Sshin case ICMP6_PARAMPROB_HEADER: 50453541Sshin case ICMP6_PARAMPROB_OPTION: 50553541Sshin code = PRC_PARAMPROB; 50653541Sshin break; 50753541Sshin default: 50853541Sshin goto badcode; 50953541Sshin } 51053541Sshin goto deliver; 51153541Sshin break; 51253541Sshin 51353541Sshin case ICMP6_ECHO_REQUEST: 51453541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo); 51553541Sshin if (code != 0) 51653541Sshin goto badcode; 51753541Sshin if ((n = m_copy(m, 0, M_COPYALL)) == NULL) { 51853541Sshin /* Give up remote */ 51953541Sshin break; 52053541Sshin } 52162587Sitojun if ((n->m_flags & M_EXT) != 0 52262587Sitojun || n->m_len < off + sizeof(struct icmp6_hdr)) { 52353541Sshin struct mbuf *n0 = n; 52462587Sitojun const int maxlen = sizeof(*nip6) + sizeof(*nicmp6); 525108741Ssam int n0len; 52653541Sshin 527111119Simp MGETHDR(n, M_DONTWAIT, n0->m_type); 528111070Ssam n0len = n0->m_pkthdr.len; /* save for use below */ 529111070Ssam if (n) 530111070Ssam M_MOVE_PKTHDR(n, n0); 53162587Sitojun if (n && maxlen >= MHLEN) { 532111119Simp MCLGET(n, M_DONTWAIT); 53362587Sitojun if ((n->m_flags & M_EXT) == 0) { 53462587Sitojun m_free(n); 53562587Sitojun n = NULL; 53662587Sitojun } 53762587Sitojun } 53853541Sshin if (n == NULL) { 53953541Sshin /* Give up remote */ 54053541Sshin m_freem(n0); 54153541Sshin break; 54253541Sshin } 54353541Sshin /* 54453541Sshin * Copy IPv6 and ICMPv6 only. 54553541Sshin */ 54653541Sshin nip6 = mtod(n, struct ip6_hdr *); 54753541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 54853541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 54953541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 55062587Sitojun noff = sizeof(struct ip6_hdr); 551108741Ssam /* new mbuf contains only ipv6+icmpv6 headers */ 552108741Ssam n->m_len = noff + sizeof(struct icmp6_hdr); 55353541Sshin /* 554120891Sume * Adjust mbuf. ip6_plen will be adjusted in 55562587Sitojun * ip6_output(). 55653541Sshin */ 55762587Sitojun m_adj(n0, off + sizeof(struct icmp6_hdr)); 558108741Ssam /* recalculate complete packet size */ 559108741Ssam n->m_pkthdr.len = n0len + (noff - off); 56062587Sitojun n->m_next = n0; 56153541Sshin } else { 56253541Sshin nip6 = mtod(n, struct ip6_hdr *); 56353541Sshin nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off); 56453541Sshin noff = off; 56553541Sshin } 56653541Sshin nicmp6->icmp6_type = ICMP6_ECHO_REPLY; 56753541Sshin nicmp6->icmp6_code = 0; 56853541Sshin if (n) { 56953541Sshin icmp6stat.icp6s_reflect++; 57053541Sshin icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++; 57153541Sshin icmp6_reflect(n, noff); 57253541Sshin } 57353541Sshin break; 57453541Sshin 57553541Sshin case ICMP6_ECHO_REPLY: 57653541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply); 57753541Sshin if (code != 0) 57853541Sshin goto badcode; 57953541Sshin break; 58053541Sshin 58196116Sume case MLD_LISTENER_QUERY: 58296116Sume case MLD_LISTENER_REPORT: 58396116Sume if (icmp6len < sizeof(struct mld_hdr)) 58453541Sshin goto badlen; 58596116Sume if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */ 58653541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery); 58753541Sshin else 58853541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport); 589111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 59062587Sitojun /* give up local */ 59162587Sitojun mld6_input(m, off); 59262587Sitojun m = NULL; 59362587Sitojun goto freeit; 59462587Sitojun } 59562587Sitojun mld6_input(n, off); 59653541Sshin /* m stays. */ 59753541Sshin break; 59853541Sshin 59996116Sume case MLD_LISTENER_DONE: 60053541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone); 60196116Sume if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */ 60253541Sshin goto badlen; 60353541Sshin break; /* nothing to be done in kernel */ 60453541Sshin 60596116Sume case MLD_MTRACE_RESP: 60696116Sume case MLD_MTRACE: 607120891Sume /* XXX: these two are experimental. not officially defined. */ 60853541Sshin /* XXX: per-interface statistics? */ 60962587Sitojun break; /* just pass it to applications */ 61053541Sshin 61153541Sshin case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ 61253541Sshin { 61353541Sshin enum { WRU, FQDN } mode; 61453541Sshin 61562587Sitojun if (!icmp6_nodeinfo) 61662587Sitojun break; 61762587Sitojun 61853541Sshin if (icmp6len == sizeof(struct icmp6_hdr) + 4) 61953541Sshin mode = WRU; 62062587Sitojun else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) 62153541Sshin mode = FQDN; 62253541Sshin else 62353541Sshin goto badlen; 62453541Sshin 62553541Sshin#define hostnamelen strlen(hostname) 62653541Sshin if (mode == FQDN) { 62762587Sitojun#ifndef PULLDOWN_TEST 62853541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), 629120891Sume IPPROTO_DONE); 63062587Sitojun#endif 63162587Sitojun n = m_copy(m, 0, M_COPYALL); 63262587Sitojun if (n) 63362587Sitojun n = ni6_input(n, off); 63462587Sitojun /* XXX meaningless if n == NULL */ 63553541Sshin noff = sizeof(struct ip6_hdr); 63653541Sshin } else { 63753541Sshin u_char *p; 63862587Sitojun int maxlen, maxhlen; 63953541Sshin 640120891Sume if ((icmp6_nodeinfo & 5) != 5) 64178064Sume break; 64278064Sume 64362587Sitojun if (code != 0) 64462587Sitojun goto badcode; 64562587Sitojun maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4; 64662587Sitojun if (maxlen >= MCLBYTES) { 64762587Sitojun /* Give up remote */ 64862587Sitojun break; 64962587Sitojun } 650111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 65162587Sitojun if (n && maxlen > MHLEN) { 652111119Simp MCLGET(n, M_DONTWAIT); 65362587Sitojun if ((n->m_flags & M_EXT) == 0) { 65462587Sitojun m_free(n); 65562587Sitojun n = NULL; 65662587Sitojun } 65762587Sitojun } 658111119Simp if (!m_dup_pkthdr(n, m, M_DONTWAIT)) { 659108466Ssam /* 660108466Ssam * Previous code did a blind M_COPY_PKTHDR 661108466Ssam * and said "just for rcvif". If true, then 662108466Ssam * we could tolerate the dup failing (due to 663108466Ssam * the deep copy of the tag chain). For now 664108466Ssam * be conservative and just fail. 665108466Ssam */ 666108466Ssam m_free(n); 667108466Ssam n = NULL; 668108466Ssam } 66953541Sshin if (n == NULL) { 67053541Sshin /* Give up remote */ 67153541Sshin break; 67253541Sshin } 67378064Sume n->m_pkthdr.rcvif = NULL; 67462587Sitojun n->m_len = 0; 67562587Sitojun maxhlen = M_TRAILINGSPACE(n) - maxlen; 67662587Sitojun if (maxhlen > hostnamelen) 67762587Sitojun maxhlen = hostnamelen; 67853541Sshin /* 67953541Sshin * Copy IPv6 and ICMPv6 only. 68053541Sshin */ 68153541Sshin nip6 = mtod(n, struct ip6_hdr *); 68253541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 68353541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 68453541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 68553541Sshin p = (u_char *)(nicmp6 + 1); 68653541Sshin bzero(p, 4); 68795023Ssuz bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */ 68853541Sshin noff = sizeof(struct ip6_hdr); 68953541Sshin n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 69062587Sitojun sizeof(struct icmp6_hdr) + 4 + maxhlen; 69153541Sshin nicmp6->icmp6_type = ICMP6_WRUREPLY; 69253541Sshin nicmp6->icmp6_code = 0; 69353541Sshin } 69453541Sshin#undef hostnamelen 69553541Sshin if (n) { 69653541Sshin icmp6stat.icp6s_reflect++; 69753541Sshin icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++; 69853541Sshin icmp6_reflect(n, noff); 69953541Sshin } 70053541Sshin break; 70153541Sshin } 70253541Sshin 70353541Sshin case ICMP6_WRUREPLY: 70453541Sshin if (code != 0) 70553541Sshin goto badcode; 70653541Sshin break; 70753541Sshin 70853541Sshin case ND_ROUTER_SOLICIT: 70953541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit); 71053541Sshin if (code != 0) 71153541Sshin goto badcode; 71253541Sshin if (icmp6len < sizeof(struct nd_router_solicit)) 71353541Sshin goto badlen; 714111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 71562587Sitojun /* give up local */ 71662587Sitojun nd6_rs_input(m, off, icmp6len); 71762587Sitojun m = NULL; 71862587Sitojun goto freeit; 71962587Sitojun } 72062587Sitojun nd6_rs_input(n, off, icmp6len); 72153541Sshin /* m stays. */ 72253541Sshin break; 72353541Sshin 72453541Sshin case ND_ROUTER_ADVERT: 72553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert); 72653541Sshin if (code != 0) 72753541Sshin goto badcode; 72853541Sshin if (icmp6len < sizeof(struct nd_router_advert)) 72953541Sshin goto badlen; 730111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 73162587Sitojun /* give up local */ 73262587Sitojun nd6_ra_input(m, off, icmp6len); 73362587Sitojun m = NULL; 73462587Sitojun goto freeit; 73562587Sitojun } 73662587Sitojun nd6_ra_input(n, off, icmp6len); 73753541Sshin /* m stays. */ 73853541Sshin break; 73953541Sshin 74053541Sshin case ND_NEIGHBOR_SOLICIT: 74153541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit); 74253541Sshin if (code != 0) 74353541Sshin goto badcode; 74453541Sshin if (icmp6len < sizeof(struct nd_neighbor_solicit)) 74553541Sshin goto badlen; 746111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 74762587Sitojun /* give up local */ 74862587Sitojun nd6_ns_input(m, off, icmp6len); 74962587Sitojun m = NULL; 75062587Sitojun goto freeit; 75162587Sitojun } 75262587Sitojun nd6_ns_input(n, off, icmp6len); 75353541Sshin /* m stays. */ 75453541Sshin break; 75553541Sshin 75653541Sshin case ND_NEIGHBOR_ADVERT: 75753541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert); 75853541Sshin if (code != 0) 75953541Sshin goto badcode; 76053541Sshin if (icmp6len < sizeof(struct nd_neighbor_advert)) 76153541Sshin goto badlen; 762111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 76362587Sitojun /* give up local */ 76462587Sitojun nd6_na_input(m, off, icmp6len); 76562587Sitojun m = NULL; 76662587Sitojun goto freeit; 76762587Sitojun } 76862587Sitojun nd6_na_input(n, off, icmp6len); 76953541Sshin /* m stays. */ 77053541Sshin break; 77153541Sshin 77253541Sshin case ND_REDIRECT: 77353541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect); 77453541Sshin if (code != 0) 77553541Sshin goto badcode; 77653541Sshin if (icmp6len < sizeof(struct nd_redirect)) 77753541Sshin goto badlen; 778111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 77962587Sitojun /* give up local */ 78062587Sitojun icmp6_redirect_input(m, off); 78162587Sitojun m = NULL; 78262587Sitojun goto freeit; 78362587Sitojun } 78462587Sitojun icmp6_redirect_input(n, off); 78553541Sshin /* m stays. */ 78653541Sshin break; 78753541Sshin 78853541Sshin case ICMP6_ROUTER_RENUMBERING: 78953541Sshin if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && 79053541Sshin code != ICMP6_ROUTER_RENUMBERING_RESULT) 79153541Sshin goto badcode; 79253541Sshin if (icmp6len < sizeof(struct icmp6_router_renum)) 79353541Sshin goto badlen; 79453541Sshin break; 79553541Sshin 79653541Sshin default: 79778064Sume nd6log((LOG_DEBUG, 79878064Sume "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", 79978064Sume icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src), 80078064Sume ip6_sprintf(&ip6->ip6_dst), 80178064Sume m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0)); 80253541Sshin if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { 80353541Sshin /* ICMPv6 error: MUST deliver it by spec... */ 80453541Sshin code = PRC_NCMDS; 80553541Sshin /* deliver */ 80653541Sshin } else { 80753541Sshin /* ICMPv6 informational: MUST not deliver */ 80853541Sshin break; 80953541Sshin } 81053541Sshin deliver: 811142681Sume if (icmp6_notify_error(&m, off, icmp6len, code)) { 81278064Sume /* In this case, m should've been freed. */ 813120856Sume return (IPPROTO_DONE); 81453541Sshin } 81578064Sume break; 81678064Sume 81778064Sume badcode: 81878064Sume icmp6stat.icp6s_badcode++; 81978064Sume break; 82078064Sume 82178064Sume badlen: 82278064Sume icmp6stat.icp6s_badlen++; 82378064Sume break; 82478064Sume } 82578064Sume 82678064Sume /* deliver the packet to appropriate sockets */ 82778064Sume icmp6_rip6_input(&m, *offp); 82878064Sume 82978064Sume return IPPROTO_DONE; 83078064Sume 83178064Sume freeit: 83278064Sume m_freem(m); 83378064Sume return IPPROTO_DONE; 83478064Sume} 83578064Sume 83678064Sumestatic int 837142681Sumeicmp6_notify_error(mp, off, icmp6len, code) 838142681Sume struct mbuf **mp; 839113799Sobrien int off, icmp6len, code; 84078064Sume{ 841142681Sume struct mbuf *m = *mp; 84278064Sume struct icmp6_hdr *icmp6; 84378064Sume struct ip6_hdr *eip6; 84478064Sume u_int32_t notifymtu; 84578064Sume struct sockaddr_in6 icmp6src, icmp6dst; 84678064Sume 84778064Sume if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { 84878064Sume icmp6stat.icp6s_tooshort++; 84978064Sume goto freeit; 85078064Sume } 85162587Sitojun#ifndef PULLDOWN_TEST 85278064Sume IP6_EXTHDR_CHECK(m, off, 853120891Sume sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1); 85478064Sume icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 85562587Sitojun#else 85678064Sume IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 857120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 85878064Sume if (icmp6 == NULL) { 85978064Sume icmp6stat.icp6s_tooshort++; 860120856Sume return (-1); 86178064Sume } 86262587Sitojun#endif 86378064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 86453541Sshin 86578064Sume /* Detect the upper level protocol */ 86678064Sume { 86753541Sshin void (*ctlfunc) __P((int, struct sockaddr *, void *)); 86853541Sshin u_int8_t nxt = eip6->ip6_nxt; 86953541Sshin int eoff = off + sizeof(struct icmp6_hdr) + 870120891Sume sizeof(struct ip6_hdr); 87153541Sshin struct ip6ctlparam ip6cp; 87262587Sitojun struct in6_addr *finaldst = NULL; 87362587Sitojun int icmp6type = icmp6->icmp6_type; 87462587Sitojun struct ip6_frag *fh; 87562587Sitojun struct ip6_rthdr *rth; 87662587Sitojun struct ip6_rthdr0 *rth0; 87762587Sitojun int rthlen; 87853541Sshin 87995023Ssuz while (1) { /* XXX: should avoid infinite loop explicitly? */ 88053541Sshin struct ip6_ext *eh; 88153541Sshin 88278064Sume switch (nxt) { 88353541Sshin case IPPROTO_HOPOPTS: 88453541Sshin case IPPROTO_DSTOPTS: 88553541Sshin case IPPROTO_AH: 88662587Sitojun#ifndef PULLDOWN_TEST 887120891Sume IP6_EXTHDR_CHECK(m, 0, 888120891Sume eoff + sizeof(struct ip6_ext), -1); 889120892Sume eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff); 89062587Sitojun#else 89162587Sitojun IP6_EXTHDR_GET(eh, struct ip6_ext *, m, 892120891Sume eoff, sizeof(*eh)); 89362587Sitojun if (eh == NULL) { 89462587Sitojun icmp6stat.icp6s_tooshort++; 895120856Sume return (-1); 89662587Sitojun } 89762587Sitojun#endif 898120891Sume 89953541Sshin if (nxt == IPPROTO_AH) 90053541Sshin eoff += (eh->ip6e_len + 2) << 2; 90153541Sshin else 90253541Sshin eoff += (eh->ip6e_len + 1) << 3; 90353541Sshin nxt = eh->ip6e_nxt; 90453541Sshin break; 90562587Sitojun case IPPROTO_ROUTING: 90662587Sitojun /* 90762587Sitojun * When the erroneous packet contains a 90862587Sitojun * routing header, we should examine the 90962587Sitojun * header to determine the final destination. 91062587Sitojun * Otherwise, we can't properly update 91162587Sitojun * information that depends on the final 91262587Sitojun * destination (e.g. path MTU). 91362587Sitojun */ 91462587Sitojun#ifndef PULLDOWN_TEST 915120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1); 916120891Sume rth = (struct ip6_rthdr *) 917120891Sume (mtod(m, caddr_t) + eoff); 91862587Sitojun#else 91962587Sitojun IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m, 920120891Sume eoff, sizeof(*rth)); 92162587Sitojun if (rth == NULL) { 92262587Sitojun icmp6stat.icp6s_tooshort++; 923120856Sume return (-1); 92462587Sitojun } 92562587Sitojun#endif 92662587Sitojun rthlen = (rth->ip6r_len + 1) << 3; 92762587Sitojun /* 92862587Sitojun * XXX: currently there is no 92962587Sitojun * officially defined type other 93062587Sitojun * than type-0. 93162587Sitojun * Note that if the segment left field 93262587Sitojun * is 0, all intermediate hops must 93362587Sitojun * have been passed. 93462587Sitojun */ 93562587Sitojun if (rth->ip6r_segleft && 93662587Sitojun rth->ip6r_type == IPV6_RTHDR_TYPE_0) { 93762587Sitojun int hops; 93862587Sitojun 93962587Sitojun#ifndef PULLDOWN_TEST 940120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1); 941120891Sume rth0 = (struct ip6_rthdr0 *) 942120891Sume (mtod(m, caddr_t) + eoff); 94362587Sitojun#else 94462587Sitojun IP6_EXTHDR_GET(rth0, 945120891Sume struct ip6_rthdr0 *, m, 946120891Sume eoff, rthlen); 94762587Sitojun if (rth0 == NULL) { 94862587Sitojun icmp6stat.icp6s_tooshort++; 949120856Sume return (-1); 95062587Sitojun } 95162587Sitojun#endif 95262587Sitojun /* just ignore a bogus header */ 95362587Sitojun if ((rth0->ip6r0_len % 2) == 0 && 95462587Sitojun (hops = rth0->ip6r0_len/2)) 95562587Sitojun finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1); 95662587Sitojun } 95762587Sitojun eoff += rthlen; 95862587Sitojun nxt = rth->ip6r_nxt; 95962587Sitojun break; 96062587Sitojun case IPPROTO_FRAGMENT: 96162587Sitojun#ifndef PULLDOWN_TEST 96262587Sitojun IP6_EXTHDR_CHECK(m, 0, eoff + 963120891Sume sizeof(struct ip6_frag), -1); 964120891Sume fh = (struct ip6_frag *)(mtod(m, caddr_t) + 965120891Sume eoff); 96662587Sitojun#else 96762587Sitojun IP6_EXTHDR_GET(fh, struct ip6_frag *, m, 968120891Sume eoff, sizeof(*fh)); 96962587Sitojun if (fh == NULL) { 97062587Sitojun icmp6stat.icp6s_tooshort++; 971120856Sume return (-1); 97262587Sitojun } 97362587Sitojun#endif 97462587Sitojun /* 97562587Sitojun * Data after a fragment header is meaningless 97662587Sitojun * unless it is the first fragment, but 97762587Sitojun * we'll go to the notify label for path MTU 97862587Sitojun * discovery. 97962587Sitojun */ 98062587Sitojun if (fh->ip6f_offlg & IP6F_OFF_MASK) 98162587Sitojun goto notify; 98262587Sitojun 98362587Sitojun eoff += sizeof(struct ip6_frag); 98462587Sitojun nxt = fh->ip6f_nxt; 98562587Sitojun break; 98653541Sshin default: 98762587Sitojun /* 98862587Sitojun * This case includes ESP and the No Next 98995023Ssuz * Header. In such cases going to the notify 99062587Sitojun * label does not have any meaning 99162587Sitojun * (i.e. ctlfunc will be NULL), but we go 99262587Sitojun * anyway since we might have to update 99362587Sitojun * path MTU information. 99462587Sitojun */ 99553541Sshin goto notify; 99653541Sshin } 99753541Sshin } 99878064Sume notify: 99962587Sitojun#ifndef PULLDOWN_TEST 100053541Sshin icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 100162587Sitojun#else 100262587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 1003120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 100462587Sitojun if (icmp6 == NULL) { 100562587Sitojun icmp6stat.icp6s_tooshort++; 1006120856Sume return (-1); 100762587Sitojun } 100862587Sitojun#endif 100978064Sume 1010120891Sume /* 1011120891Sume * retrieve parameters from the inner IPv6 header, and convert 1012120891Sume * them into sockaddr structures. 1013120891Sume * XXX: there is no guarantee that the source or destination 1014120891Sume * addresses of the inner packet are in the same scope as 1015120891Sume * the addresses of the icmp packet. But there is no other 1016120891Sume * way to determine the zone. 1017120891Sume */ 101878064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 1019120891Sume 102078064Sume bzero(&icmp6dst, sizeof(icmp6dst)); 102178064Sume icmp6dst.sin6_len = sizeof(struct sockaddr_in6); 102278064Sume icmp6dst.sin6_family = AF_INET6; 102378064Sume if (finaldst == NULL) 102478064Sume icmp6dst.sin6_addr = eip6->ip6_dst; 102578064Sume else 102678064Sume icmp6dst.sin6_addr = *finaldst; 1027121315Sume if (in6_addr2zoneid(m->m_pkthdr.rcvif, &icmp6dst.sin6_addr, 1028121315Sume &icmp6dst.sin6_scope_id)) 1029121315Sume goto freeit; 103078064Sume if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst, 103178064Sume NULL, NULL)) { 1032142679Sume /* should be impossible */ 103378064Sume nd6log((LOG_DEBUG, 103478064Sume "icmp6_notify_error: in6_embedscope failed\n")); 103578064Sume goto freeit; 103678064Sume } 103778064Sume 103878064Sume /* 103978064Sume * retrieve parameters from the inner IPv6 header, and convert 104078064Sume * them into sockaddr structures. 104178064Sume */ 104278064Sume bzero(&icmp6src, sizeof(icmp6src)); 104378064Sume icmp6src.sin6_len = sizeof(struct sockaddr_in6); 104478064Sume icmp6src.sin6_family = AF_INET6; 104578064Sume icmp6src.sin6_addr = eip6->ip6_src; 1046121315Sume if (in6_addr2zoneid(m->m_pkthdr.rcvif, &icmp6src.sin6_addr, 1047121315Sume &icmp6src.sin6_scope_id)) { 1048121315Sume goto freeit; 1049121315Sume } 105078064Sume if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src, 105178064Sume NULL, NULL)) { 1052142679Sume /* should be impossible */ 105378064Sume nd6log((LOG_DEBUG, 105478064Sume "icmp6_notify_error: in6_embedscope failed\n")); 105578064Sume goto freeit; 105678064Sume } 1057120891Sume icmp6src.sin6_flowinfo = (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); 105878064Sume 105978064Sume if (finaldst == NULL) 106078064Sume finaldst = &eip6->ip6_dst; 106178064Sume ip6cp.ip6c_m = m; 106278064Sume ip6cp.ip6c_icmp6 = icmp6; 106378064Sume ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1); 106478064Sume ip6cp.ip6c_off = eoff; 106578064Sume ip6cp.ip6c_finaldst = finaldst; 106678064Sume ip6cp.ip6c_src = &icmp6src; 106778064Sume ip6cp.ip6c_nxt = nxt; 106878064Sume 106962587Sitojun if (icmp6type == ICMP6_PACKET_TOO_BIG) { 107078064Sume notifymtu = ntohl(icmp6->icmp6_mtu); 107178064Sume ip6cp.ip6c_cmdarg = (void *)¬ifymtu; 107278064Sume icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ 107362587Sitojun } 107462587Sitojun 107553541Sshin ctlfunc = (void (*) __P((int, struct sockaddr *, void *))) 1076120891Sume (inet6sw[ip6_protox[nxt]].pr_ctlinput); 107753541Sshin if (ctlfunc) { 107878064Sume (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, 1079120891Sume &ip6cp); 108053541Sshin } 108153541Sshin } 1082142681Sume *mp = m; 1083120856Sume return (0); 108453541Sshin 108578064Sume freeit: 108653541Sshin m_freem(m); 1087120856Sume return (-1); 108853541Sshin} 108953541Sshin 109078064Sumevoid 109178064Sumeicmp6_mtudisc_update(ip6cp, validated) 109278064Sume struct ip6ctlparam *ip6cp; 109378064Sume int validated; 109462587Sitojun{ 109578064Sume struct in6_addr *dst = ip6cp->ip6c_finaldst; 109678064Sume struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; 109778064Sume struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ 109862587Sitojun u_int mtu = ntohl(icmp6->icmp6_mtu); 1099122922Sandre struct in_conninfo inc; 110062587Sitojun 1101121472Sume#if 0 1102121472Sume /* 1103121472Sume * RFC2460 section 5, last paragraph. 1104121472Sume * even though minimum link MTU for IPv6 is IPV6_MMTU, 1105121472Sume * we may see ICMPv6 too big with mtu < IPV6_MMTU 1106121472Sume * due to packet translator in the middle. 1107121472Sume * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for 1108121472Sume * special handling. 1109121472Sume */ 1110121472Sume if (mtu < IPV6_MMTU) 1111121472Sume return; 1112121472Sume#endif 1113121472Sume 1114121472Sume /* 1115121472Sume * we reject ICMPv6 too big with abnormally small value. 1116121472Sume * XXX what is the good definition of "abnormally small"? 1117121472Sume */ 1118121472Sume if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) 1119121472Sume return; 1120121472Sume 112178064Sume if (!validated) 112278064Sume return; 112378064Sume 1124122922Sandre bzero(&inc, sizeof(inc)); 1125122922Sandre inc.inc_flags = 1; /* IPv6 */ 1126122922Sandre inc.inc6_faddr = *dst; 112778064Sume /* XXX normally, this won't happen */ 112878064Sume if (IN6_IS_ADDR_LINKLOCAL(dst)) { 1129122922Sandre inc.inc6_faddr.s6_addr16[1] = 113078064Sume htons(m->m_pkthdr.rcvif->if_index); 113178064Sume } 113262587Sitojun 1133126194Sume if (mtu < tcp_maxmtu6(&inc)) { 1134122922Sandre tcp_hc_updatemtu(&inc, mtu); 1135122922Sandre icmp6stat.icp6s_pmtuchg++; 113662587Sitojun } 113762587Sitojun} 113862587Sitojun 113953541Sshin/* 114078064Sume * Process a Node Information Query packet, based on 114178064Sume * draft-ietf-ipngwg-icmp-name-lookups-07. 1142120891Sume * 114362587Sitojun * Spec incompatibilities: 114462587Sitojun * - IPv6 Subject address handling 114562587Sitojun * - IPv4 Subject address handling support missing 114662587Sitojun * - Proxy reply (answer even if it's not for me) 114762587Sitojun * - joins NI group address at in6_ifattach() time only, does not cope 114862587Sitojun * with hostname changes by sethostname(3) 114953541Sshin */ 115062587Sitojun#define hostnamelen strlen(hostname) 115153541Sshinstatic struct mbuf * 115253541Sshinni6_input(m, off) 115353541Sshin struct mbuf *m; 115453541Sshin int off; 115553541Sshin{ 115662587Sitojun struct icmp6_nodeinfo *ni6, *nni6; 115753541Sshin struct mbuf *n = NULL; 115862587Sitojun u_int16_t qtype; 115962587Sitojun int subjlen; 116053541Sshin int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 116153541Sshin struct ni_reply_fqdn *fqdn; 116253541Sshin int addrs; /* for NI_QTYPE_NODEADDR */ 116353541Sshin struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ 1164121630Sume struct sockaddr_in6 sin6_sbj; /* subject address */ 116562587Sitojun struct ip6_hdr *ip6; 116662587Sitojun int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ 116778064Sume char *subj = NULL; 116878064Sume struct in6_ifaddr *ia6 = NULL; 116953541Sshin 117062587Sitojun ip6 = mtod(m, struct ip6_hdr *); 117162587Sitojun#ifndef PULLDOWN_TEST 117262587Sitojun ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); 117362587Sitojun#else 117462587Sitojun IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); 117562587Sitojun if (ni6 == NULL) { 117662587Sitojun /* m is already reclaimed */ 1177120891Sume return (NULL); 117862587Sitojun } 117962587Sitojun#endif 118053541Sshin 118162587Sitojun /* 118262587Sitojun * Validate IPv6 destination address. 118362587Sitojun * 118478064Sume * The Responder must discard the Query without further processing 118578064Sume * unless it is one of the Responder's unicast or anycast addresses, or 118678064Sume * a link-local scope multicast address which the Responder has joined. 1187121630Sume * [icmp-name-lookups-08, Section 4.] 118862587Sitojun */ 1189121630Sume if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1190121630Sume if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) 1191121630Sume goto bad; 1192121630Sume /* else it's a link-local multicast, fine */ 1193121630Sume } else { /* unicast or anycast */ 1194121630Sume if ((ia6 = ip6_getdstifaddr(m)) == NULL) 1195121630Sume goto bad; /* XXX impossible */ 1196121630Sume 1197121630Sume if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) && 1198121630Sume !(icmp6_nodeinfo & 4)) { 119978064Sume nd6log((LOG_DEBUG, "ni6_input: ignore node info to " 120078064Sume "a temporary address in %s:%d", 120178064Sume __FILE__, __LINE__)); 120278064Sume goto bad; 120378064Sume } 1204121630Sume } 120562587Sitojun 120678064Sume /* validate query Subject field. */ 120762587Sitojun qtype = ntohs(ni6->ni_qtype); 120862587Sitojun subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); 120962587Sitojun switch (qtype) { 121062587Sitojun case NI_QTYPE_NOOP: 121162587Sitojun case NI_QTYPE_SUPTYPES: 121278064Sume /* 07 draft */ 121378064Sume if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) 121478064Sume break; 121578064Sume /* FALLTHROUGH */ 121662587Sitojun case NI_QTYPE_FQDN: 121762587Sitojun case NI_QTYPE_NODEADDR: 121862587Sitojun switch (ni6->ni_code) { 121962587Sitojun case ICMP6_NI_SUBJ_IPV6: 122062587Sitojun#if ICMP6_NI_SUBJ_IPV6 != 0 122162587Sitojun case 0: 122262587Sitojun#endif 122362587Sitojun /* 122462587Sitojun * backward compatibility - try to accept 03 draft 122562587Sitojun * format, where no Subject is present. 122662587Sitojun */ 122778064Sume if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && 122878064Sume subjlen == 0) { 122962587Sitojun oldfqdn++; 123062587Sitojun break; 123162587Sitojun } 123278064Sume#if ICMP6_NI_SUBJ_IPV6 != 0 123378064Sume if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) 123478064Sume goto bad; 123578064Sume#endif 123662587Sitojun 1237121630Sume if (subjlen != sizeof(struct in6_addr)) 123862587Sitojun goto bad; 123962587Sitojun 124062587Sitojun /* 124162587Sitojun * Validate Subject address. 124262587Sitojun * 124378064Sume * Not sure what exactly "address belongs to the node" 124478064Sume * means in the spec, is it just unicast, or what? 124562587Sitojun * 124662587Sitojun * At this moment we consider Subject address as 124762587Sitojun * "belong to the node" if the Subject address equals 124862587Sitojun * to the IPv6 destination address; validation for 124962587Sitojun * IPv6 destination address should have done enough 125062587Sitojun * check for us. 125162587Sitojun * 125262587Sitojun * We do not do proxy at this moment. 125362587Sitojun */ 125462587Sitojun /* m_pulldown instead of copy? */ 1255121630Sume bzero(&sin6_sbj, sizeof(sin6_sbj)); 1256121630Sume sin6_sbj.sin6_family = AF_INET6; 1257121630Sume sin6_sbj.sin6_len = sizeof(sin6_sbj); 125862587Sitojun m_copydata(m, off + sizeof(struct icmp6_nodeinfo), 1259121630Sume subjlen, (caddr_t)&sin6_sbj.sin6_addr); 1260121315Sume if (in6_addr2zoneid(m->m_pkthdr.rcvif, 1261121630Sume &sin6_sbj.sin6_addr, &sin6_sbj.sin6_scope_id)) { 1262121315Sume goto bad; 1263121315Sume } 1264121630Sume if (in6_embedscope(&sin6_sbj.sin6_addr, &sin6_sbj, 1265121630Sume NULL, NULL)) 1266121630Sume goto bad; /* XXX should not happen */ 1267121630Sume 1268121630Sume subj = (char *)&sin6_sbj; 1269140588Sume if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 1270140588Sume &sin6_sbj.sin6_addr)) 127162587Sitojun break; 127278064Sume 127362587Sitojun /* 127462587Sitojun * XXX if we are to allow other cases, we should really 127562587Sitojun * be careful about scope here. 127662587Sitojun * basically, we should disallow queries toward IPv6 1277120891Sume * destination X with subject Y, 1278120891Sume * if scope(X) > scope(Y). 127962587Sitojun * if we allow scope(X) > scope(Y), it will result in 128062587Sitojun * information leakage across scope boundary. 128162587Sitojun */ 128262587Sitojun goto bad; 128362587Sitojun 128462587Sitojun case ICMP6_NI_SUBJ_FQDN: 128562587Sitojun /* 128662587Sitojun * Validate Subject name with gethostname(3). 128762587Sitojun * 128862587Sitojun * The behavior may need some debate, since: 128962587Sitojun * - we are not sure if the node has FQDN as 129062587Sitojun * hostname (returned by gethostname(3)). 129162587Sitojun * - the code does wildcard match for truncated names. 129262587Sitojun * however, we are not sure if we want to perform 129362587Sitojun * wildcard match, if gethostname(3) side has 129462587Sitojun * truncated hostname. 129562587Sitojun */ 129662587Sitojun n = ni6_nametodns(hostname, hostnamelen, 0); 129762587Sitojun if (!n || n->m_next || n->m_len == 0) 129862587Sitojun goto bad; 129962587Sitojun IP6_EXTHDR_GET(subj, char *, m, 130062587Sitojun off + sizeof(struct icmp6_nodeinfo), subjlen); 130162587Sitojun if (subj == NULL) 130262587Sitojun goto bad; 130362587Sitojun if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 1304120891Sume n->m_len)) { 130562587Sitojun goto bad; 130662587Sitojun } 130762587Sitojun m_freem(n); 130862587Sitojun n = NULL; 130962587Sitojun break; 131062587Sitojun 131178064Sume case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 131262587Sitojun default: 131362587Sitojun goto bad; 131462587Sitojun } 131562587Sitojun break; 131678064Sume } 131762587Sitojun 131878064Sume /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 131978064Sume switch (qtype) { 132078064Sume case NI_QTYPE_FQDN: 132178064Sume if ((icmp6_nodeinfo & 1) == 0) 132278064Sume goto bad; 132378064Sume break; 132478064Sume case NI_QTYPE_NODEADDR: 132578064Sume if ((icmp6_nodeinfo & 2) == 0) 132678064Sume goto bad; 132778064Sume break; 132878064Sume } 132978064Sume 133078064Sume /* guess reply length */ 133178064Sume switch (qtype) { 133278064Sume case NI_QTYPE_NOOP: 133378064Sume break; /* no reply data */ 133478064Sume case NI_QTYPE_SUPTYPES: 133578064Sume replylen += sizeof(u_int32_t); 133678064Sume break; 133778064Sume case NI_QTYPE_FQDN: 133878064Sume /* XXX will append an mbuf */ 133978064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 134078064Sume break; 134178064Sume case NI_QTYPE_NODEADDR: 134278064Sume addrs = ni6_addrs(ni6, m, &ifp, subj); 134378064Sume if ((replylen += addrs * (sizeof(struct in6_addr) + 1344120891Sume sizeof(u_int32_t))) > MCLBYTES) 134578064Sume replylen = MCLBYTES; /* XXX: will truncate pkt later */ 134678064Sume break; 134762587Sitojun default: 134878064Sume /* 134978064Sume * XXX: We must return a reply with the ICMP6 code 1350120891Sume * `unknown Qtype' in this case. However we regard the case 135178064Sume * as an FQDN query for backward compatibility. 135278064Sume * Older versions set a random value to this field, 135378064Sume * so it rarely varies in the defined qtypes. 135478064Sume * But the mechanism is not reliable... 135578064Sume * maybe we should obsolete older versions. 135678064Sume */ 135778064Sume qtype = NI_QTYPE_FQDN; 135878064Sume /* XXX will append an mbuf */ 135978064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 136078064Sume oldfqdn++; 136178064Sume break; 136262587Sitojun } 136362587Sitojun 136478064Sume /* allocate an mbuf to reply. */ 1365111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 136662587Sitojun if (n == NULL) { 136762587Sitojun m_freem(m); 1368120856Sume return (NULL); 136962587Sitojun } 1370108466Ssam M_MOVE_PKTHDR(n, m); /* just for recvif */ 137153541Sshin if (replylen > MHLEN) { 137262587Sitojun if (replylen > MCLBYTES) { 137378064Sume /* 137478064Sume * XXX: should we try to allocate more? But MCLBYTES 137578064Sume * is probably much larger than IPV6_MMTU... 137678064Sume */ 137753541Sshin goto bad; 137862587Sitojun } 1379111119Simp MCLGET(n, M_DONTWAIT); 138053541Sshin if ((n->m_flags & M_EXT) == 0) { 138153541Sshin goto bad; 138253541Sshin } 138353541Sshin } 138453541Sshin n->m_pkthdr.len = n->m_len = replylen; 138553541Sshin 138653541Sshin /* copy mbuf header and IPv6 + Node Information base headers */ 138753541Sshin bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); 138853541Sshin nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 138962587Sitojun bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); 139053541Sshin 139153541Sshin /* qtype dependent procedure */ 139253541Sshin switch (qtype) { 139362587Sitojun case NI_QTYPE_NOOP: 139478064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 139562587Sitojun nni6->ni_flags = 0; 139662587Sitojun break; 139762587Sitojun case NI_QTYPE_SUPTYPES: 139878064Sume { 139978064Sume u_int32_t v; 140078064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 140178064Sume nni6->ni_flags = htons(0x0000); /* raw bitmap */ 140278064Sume /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 140378064Sume v = (u_int32_t)htonl(0x0000000f); 140478064Sume bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 140562587Sitojun break; 140678064Sume } 140762587Sitojun case NI_QTYPE_FQDN: 140878064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 140962587Sitojun fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + 1410120891Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo)); 141162587Sitojun nni6->ni_flags = 0; /* XXX: meaningless TTL */ 141262587Sitojun fqdn->ni_fqdn_ttl = 0; /* ditto. */ 141362587Sitojun /* 141462587Sitojun * XXX do we really have FQDN in variable "hostname"? 141562587Sitojun */ 141662587Sitojun n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn); 141762587Sitojun if (n->m_next == NULL) 141862587Sitojun goto bad; 141962587Sitojun /* XXX we assume that n->m_next is not a chain */ 142062587Sitojun if (n->m_next->m_next != NULL) 142162587Sitojun goto bad; 142262587Sitojun n->m_pkthdr.len += n->m_next->m_len; 142362587Sitojun break; 142462587Sitojun case NI_QTYPE_NODEADDR: 142562587Sitojun { 142662587Sitojun int lenlim, copied; 142753541Sshin 142878064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 142978064Sume n->m_pkthdr.len = n->m_len = 143078064Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 143178064Sume lenlim = M_TRAILINGSPACE(n); 143262587Sitojun copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 143362587Sitojun /* XXX: reset mbuf length */ 143462587Sitojun n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 1435120891Sume sizeof(struct icmp6_nodeinfo) + copied; 143662587Sitojun break; 143753541Sshin } 143862587Sitojun default: 143962587Sitojun break; /* XXX impossible! */ 144062587Sitojun } 144153541Sshin 144253541Sshin nni6->ni_type = ICMP6_NI_REPLY; 144362587Sitojun m_freem(m); 1444120856Sume return (n); 144553541Sshin 144653541Sshin bad: 144762587Sitojun m_freem(m); 144853541Sshin if (n) 144953541Sshin m_freem(n); 1450120856Sume return (NULL); 145153541Sshin} 145253541Sshin#undef hostnamelen 145353541Sshin 145453541Sshin/* 145562587Sitojun * make a mbuf with DNS-encoded string. no compression support. 145662587Sitojun * 145762587Sitojun * XXX names with less than 2 dots (like "foo" or "foo.section") will be 145862587Sitojun * treated as truncated name (two \0 at the end). this is a wild guess. 145962587Sitojun */ 146062587Sitojunstatic struct mbuf * 146162587Sitojunni6_nametodns(name, namelen, old) 146262587Sitojun const char *name; 146362587Sitojun int namelen; 146462587Sitojun int old; /* return pascal string if non-zero */ 146562587Sitojun{ 146662587Sitojun struct mbuf *m; 146762587Sitojun char *cp, *ep; 146862587Sitojun const char *p, *q; 146962587Sitojun int i, len, nterm; 147062587Sitojun 147162587Sitojun if (old) 147262587Sitojun len = namelen + 1; 147362587Sitojun else 147462587Sitojun len = MCLBYTES; 147562587Sitojun 147662587Sitojun /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ 1477111119Simp MGET(m, M_DONTWAIT, MT_DATA); 147862587Sitojun if (m && len > MLEN) { 1479111119Simp MCLGET(m, M_DONTWAIT); 148062587Sitojun if ((m->m_flags & M_EXT) == 0) 148162587Sitojun goto fail; 148262587Sitojun } 148362587Sitojun if (!m) 148462587Sitojun goto fail; 148562587Sitojun m->m_next = NULL; 148662587Sitojun 148762587Sitojun if (old) { 148862587Sitojun m->m_len = len; 148962587Sitojun *mtod(m, char *) = namelen; 149062587Sitojun bcopy(name, mtod(m, char *) + 1, namelen); 149162587Sitojun return m; 149262587Sitojun } else { 149362587Sitojun m->m_len = 0; 149462587Sitojun cp = mtod(m, char *); 149562587Sitojun ep = mtod(m, char *) + M_TRAILINGSPACE(m); 149662587Sitojun 149762587Sitojun /* if not certain about my name, return empty buffer */ 149862587Sitojun if (namelen == 0) 149962587Sitojun return m; 150062587Sitojun 150162587Sitojun /* 150262587Sitojun * guess if it looks like shortened hostname, or FQDN. 150362587Sitojun * shortened hostname needs two trailing "\0". 150462587Sitojun */ 150562587Sitojun i = 0; 150662587Sitojun for (p = name; p < name + namelen; p++) { 150762587Sitojun if (*p && *p == '.') 150862587Sitojun i++; 150962587Sitojun } 151062587Sitojun if (i < 2) 151162587Sitojun nterm = 2; 151262587Sitojun else 151362587Sitojun nterm = 1; 151462587Sitojun 151562587Sitojun p = name; 151662587Sitojun while (cp < ep && p < name + namelen) { 151762587Sitojun i = 0; 151862587Sitojun for (q = p; q < name + namelen && *q && *q != '.'; q++) 151962587Sitojun i++; 152062587Sitojun /* result does not fit into mbuf */ 152162587Sitojun if (cp + i + 1 >= ep) 152262587Sitojun goto fail; 152378704Sume /* 152478704Sume * DNS label length restriction, RFC1035 page 8. 152578704Sume * "i == 0" case is included here to avoid returning 152678704Sume * 0-length label on "foo..bar". 152778704Sume */ 152878704Sume if (i <= 0 || i >= 64) 152962587Sitojun goto fail; 153062587Sitojun *cp++ = i; 153162587Sitojun bcopy(p, cp, i); 153262587Sitojun cp += i; 153362587Sitojun p = q; 153462587Sitojun if (p < name + namelen && *p == '.') 153562587Sitojun p++; 153662587Sitojun } 153762587Sitojun /* termination */ 153862587Sitojun if (cp + nterm >= ep) 153962587Sitojun goto fail; 154062587Sitojun while (nterm-- > 0) 154162587Sitojun *cp++ = '\0'; 154262587Sitojun m->m_len = cp - mtod(m, char *); 154362587Sitojun return m; 154462587Sitojun } 154562587Sitojun 154662587Sitojun panic("should not reach here"); 154795023Ssuz /* NOTREACHED */ 154862587Sitojun 154962587Sitojun fail: 155062587Sitojun if (m) 155162587Sitojun m_freem(m); 155262587Sitojun return NULL; 155362587Sitojun} 155462587Sitojun 155562587Sitojun/* 155662587Sitojun * check if two DNS-encoded string matches. takes care of truncated 155762587Sitojun * form (with \0\0 at the end). no compression support. 155878064Sume * XXX upper/lowercase match (see RFC2065) 155962587Sitojun */ 156062587Sitojunstatic int 156162587Sitojunni6_dnsmatch(a, alen, b, blen) 156262587Sitojun const char *a; 156362587Sitojun int alen; 156462587Sitojun const char *b; 156562587Sitojun int blen; 156662587Sitojun{ 156762587Sitojun const char *a0, *b0; 156862587Sitojun int l; 156962587Sitojun 157062587Sitojun /* simplest case - need validation? */ 157162587Sitojun if (alen == blen && bcmp(a, b, alen) == 0) 157262587Sitojun return 1; 157362587Sitojun 157462587Sitojun a0 = a; 157562587Sitojun b0 = b; 157662587Sitojun 157762587Sitojun /* termination is mandatory */ 157862587Sitojun if (alen < 2 || blen < 2) 157962587Sitojun return 0; 158062587Sitojun if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 158162587Sitojun return 0; 158262587Sitojun alen--; 158362587Sitojun blen--; 158462587Sitojun 158562587Sitojun while (a - a0 < alen && b - b0 < blen) { 158662587Sitojun if (a - a0 + 1 > alen || b - b0 + 1 > blen) 158762587Sitojun return 0; 158862587Sitojun 158962587Sitojun if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 159062587Sitojun return 0; 159162587Sitojun /* we don't support compression yet */ 159262587Sitojun if (a[0] >= 64 || b[0] >= 64) 159362587Sitojun return 0; 159462587Sitojun 159562587Sitojun /* truncated case */ 159662587Sitojun if (a[0] == 0 && a - a0 == alen - 1) 159762587Sitojun return 1; 159862587Sitojun if (b[0] == 0 && b - b0 == blen - 1) 159962587Sitojun return 1; 160062587Sitojun if (a[0] == 0 || b[0] == 0) 160162587Sitojun return 0; 160262587Sitojun 160362587Sitojun if (a[0] != b[0]) 160462587Sitojun return 0; 160562587Sitojun l = a[0]; 160662587Sitojun if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 160762587Sitojun return 0; 160862587Sitojun if (bcmp(a + 1, b + 1, l) != 0) 160962587Sitojun return 0; 161062587Sitojun 161162587Sitojun a += 1 + l; 161262587Sitojun b += 1 + l; 161362587Sitojun } 161462587Sitojun 161562587Sitojun if (a - a0 == alen && b - b0 == blen) 161662587Sitojun return 1; 161762587Sitojun else 161862587Sitojun return 0; 161962587Sitojun} 162062587Sitojun 162162587Sitojun/* 162253541Sshin * calculate the number of addresses to be returned in the node info reply. 162353541Sshin */ 162453541Sshinstatic int 162578064Sumeni6_addrs(ni6, m, ifpp, subj) 162653541Sshin struct icmp6_nodeinfo *ni6; 162753541Sshin struct mbuf *m; 162853541Sshin struct ifnet **ifpp; 162978064Sume char *subj; 163053541Sshin{ 163178064Sume struct ifnet *ifp; 163278064Sume struct in6_ifaddr *ifa6; 163378064Sume struct ifaddr *ifa; 163478064Sume struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */ 163553541Sshin int addrs = 0, addrsofif, iffound = 0; 163678064Sume int niflags = ni6->ni_flags; 163753541Sshin 163878064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 163978064Sume switch (ni6->ni_code) { 164078064Sume case ICMP6_NI_SUBJ_IPV6: 164178064Sume if (subj == NULL) /* must be impossible... */ 1642120856Sume return (0); 164378064Sume subj_ip6 = (struct sockaddr_in6 *)subj; 164478064Sume break; 164578064Sume default: 164678064Sume /* 164778064Sume * XXX: we only support IPv6 subject address for 164878064Sume * this Qtype. 164978064Sume */ 1650120856Sume return (0); 165178064Sume } 165278064Sume } 165378064Sume 1654108172Shsu IFNET_RLOCK(); 1655120891Sume for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 165653541Sshin addrsofif = 0; 1657120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 165853541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 165953541Sshin continue; 166053541Sshin ifa6 = (struct in6_ifaddr *)ifa; 166153541Sshin 166278064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 166378064Sume IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr, 1664120891Sume &ifa6->ia_addr.sin6_addr)) 166553541Sshin iffound = 1; 166653541Sshin 166762587Sitojun /* 166862587Sitojun * IPv4-mapped addresses can only be returned by a 166962587Sitojun * Node Information proxy, since they represent 167062587Sitojun * addresses of IPv4-only nodes, which perforce do 167162587Sitojun * not implement this protocol. 167278064Sume * [icmp-name-lookups-07, Section 5.4] 167362587Sitojun * So we don't support NI_NODEADDR_FLAG_COMPAT in 167462587Sitojun * this function at this moment. 167562587Sitojun */ 167662587Sitojun 167753541Sshin /* What do we have to do about ::1? */ 167878064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 167978064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 168078064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 168178064Sume continue; 168253541Sshin break; 168378064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 168478064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 168578064Sume continue; 168653541Sshin break; 168778064Sume case IPV6_ADDR_SCOPE_GLOBAL: 168878064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 168978064Sume continue; 169078064Sume break; 169178064Sume default: 169278064Sume continue; 169353541Sshin } 169478064Sume 169578064Sume /* 169678064Sume * check if anycast is okay. 169795023Ssuz * XXX: just experimental. not in the spec. 169878064Sume */ 169978064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 170078064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 170178064Sume continue; /* we need only unicast addresses */ 170278064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 170378064Sume (icmp6_nodeinfo & 4) == 0) { 170478064Sume continue; 170578064Sume } 170678064Sume addrsofif++; /* count the address */ 170753541Sshin } 170853541Sshin if (iffound) { 170953541Sshin *ifpp = ifp; 1710108172Shsu IFNET_RUNLOCK(); 1711120856Sume return (addrsofif); 171253541Sshin } 171353541Sshin 171453541Sshin addrs += addrsofif; 171553541Sshin } 1716108172Shsu IFNET_RUNLOCK(); 171753541Sshin 1718120856Sume return (addrs); 171953541Sshin} 172053541Sshin 172153541Sshinstatic int 172253541Sshinni6_store_addrs(ni6, nni6, ifp0, resid) 172353541Sshin struct icmp6_nodeinfo *ni6, *nni6; 172453541Sshin struct ifnet *ifp0; 172553541Sshin int resid; 172653541Sshin{ 172778064Sume struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet); 172878064Sume struct in6_ifaddr *ifa6; 172978064Sume struct ifaddr *ifa; 173078064Sume struct ifnet *ifp_dep = NULL; 173178064Sume int copied = 0, allow_deprecated = 0; 173253541Sshin u_char *cp = (u_char *)(nni6 + 1); 173378064Sume int niflags = ni6->ni_flags; 173478064Sume u_int32_t ltime; 173553541Sshin 173678064Sume if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 1737120856Sume return (0); /* needless to copy */ 1738120891Sume 1739108172Shsu IFNET_RLOCK(); 174078064Sume again: 1741120892Sume 1742120891Sume for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 174362587Sitojun for (ifa = ifp->if_addrlist.tqh_first; ifa; 1744120891Sume ifa = ifa->ifa_list.tqe_next) { 174553541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 174653541Sshin continue; 174753541Sshin ifa6 = (struct in6_ifaddr *)ifa; 174853541Sshin 174978064Sume if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && 175078064Sume allow_deprecated == 0) { 175178064Sume /* 175278064Sume * prefererred address should be put before 175378064Sume * deprecated addresses. 175478064Sume */ 175578064Sume 175678064Sume /* record the interface for later search */ 175778064Sume if (ifp_dep == NULL) 175878064Sume ifp_dep = ifp; 175978064Sume 176078064Sume continue; 1761120891Sume } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && 1762120891Sume allow_deprecated != 0) 176378064Sume continue; /* we now collect deprecated addrs */ 176453541Sshin 176553541Sshin /* What do we have to do about ::1? */ 176678064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 176778064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 176878064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 176978064Sume continue; 177053541Sshin break; 177178064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 177278064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 177378064Sume continue; 177453541Sshin break; 177578064Sume case IPV6_ADDR_SCOPE_GLOBAL: 177678064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 177778064Sume continue; 177878064Sume break; 177978064Sume default: 178078064Sume continue; 178153541Sshin } 178253541Sshin 178378064Sume /* 178478064Sume * check if anycast is okay. 1785120891Sume * XXX: just experimental. not in the spec. 178678064Sume */ 178778064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 178878064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 178978064Sume continue; 179078064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 179178064Sume (icmp6_nodeinfo & 4) == 0) { 179278064Sume continue; 179353541Sshin } 179478064Sume 179578064Sume /* now we can copy the address */ 179678064Sume if (resid < sizeof(struct in6_addr) + 179778064Sume sizeof(u_int32_t)) { 179878064Sume /* 179978064Sume * We give up much more copy. 180078064Sume * Set the truncate flag and return. 180178064Sume */ 1802120891Sume nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE; 1803108172Shsu IFNET_RUNLOCK(); 1804120856Sume return (copied); 180578064Sume } 180678064Sume 180778064Sume /* 180878064Sume * Set the TTL of the address. 180978064Sume * The TTL value should be one of the following 181078064Sume * according to the specification: 181178064Sume * 181278064Sume * 1. The remaining lifetime of a DHCP lease on the 181378064Sume * address, or 181478064Sume * 2. The remaining Valid Lifetime of a prefix from 181578064Sume * which the address was derived through Stateless 181678064Sume * Autoconfiguration. 181778064Sume * 181878064Sume * Note that we currently do not support stateful 181978064Sume * address configuration by DHCPv6, so the former 182078064Sume * case can't happen. 182178064Sume */ 182278064Sume if (ifa6->ia6_lifetime.ia6t_expire == 0) 182378064Sume ltime = ND6_INFINITE_LIFETIME; 182478064Sume else { 182578064Sume if (ifa6->ia6_lifetime.ia6t_expire > 182678064Sume time_second) 182778064Sume ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second); 182878064Sume else 182978064Sume ltime = 0; 183078064Sume } 1831120891Sume 183278064Sume bcopy(<ime, cp, sizeof(u_int32_t)); 183378064Sume cp += sizeof(u_int32_t); 183478064Sume 183578064Sume /* copy the address itself */ 183678064Sume bcopy(&ifa6->ia_addr.sin6_addr, cp, 1837120891Sume sizeof(struct in6_addr)); 1838121315Sume in6_clearscope((struct in6_addr *)cp); /* XXX */ 183978064Sume cp += sizeof(struct in6_addr); 1840120891Sume 184178064Sume resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1842120891Sume copied += (sizeof(struct in6_addr) + sizeof(u_int32_t)); 184353541Sshin } 184453541Sshin if (ifp0) /* we need search only on the specified IF */ 184553541Sshin break; 184653541Sshin } 184753541Sshin 184878064Sume if (allow_deprecated == 0 && ifp_dep != NULL) { 184978064Sume ifp = ifp_dep; 185078064Sume allow_deprecated = 1; 185178064Sume 185278064Sume goto again; 185378064Sume } 185478064Sume 1855108172Shsu IFNET_RUNLOCK(); 1856108172Shsu 1857120856Sume return (copied); 185853541Sshin} 185953541Sshin 186053541Sshin/* 186153541Sshin * XXX almost dup'ed code with rip6_input. 186253541Sshin */ 186353541Sshinstatic int 186453541Sshinicmp6_rip6_input(mp, off) 186553541Sshin struct mbuf **mp; 186653541Sshin int off; 186753541Sshin{ 186853541Sshin struct mbuf *m = *mp; 186978064Sume struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 187078064Sume struct in6pcb *in6p; 187153541Sshin struct in6pcb *last = NULL; 1872121315Sume struct sockaddr_in6 fromsa; 187353541Sshin struct icmp6_hdr *icmp6; 187453541Sshin struct mbuf *opts = NULL; 187553541Sshin 187662587Sitojun#ifndef PULLDOWN_TEST 187753541Sshin /* this is assumed to be safe. */ 187853541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 187962587Sitojun#else 188062587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 188162587Sitojun if (icmp6 == NULL) { 188262587Sitojun /* m is already reclaimed */ 1883120891Sume return (IPPROTO_DONE); 188462587Sitojun } 188562587Sitojun#endif 188653541Sshin 1887121315Sume bzero(&fromsa, sizeof(fromsa)); 1888121315Sume fromsa.sin6_len = sizeof(struct sockaddr_in6); 1889121315Sume fromsa.sin6_family = AF_INET6; 189062587Sitojun /* KAME hack: recover scopeid */ 1891121315Sume (void)in6_recoverscope(&fromsa, &ip6->ip6_src, m->m_pkthdr.rcvif); 189253541Sshin 1893141545Srwatson INP_INFO_RLOCK(&ripcbinfo); 1894120891Sume LIST_FOREACH(in6p, &ripcb, inp_list) { 1895141545Srwatson INP_LOCK(in6p); 1896141545Srwatson if ((in6p->inp_vflag & INP_IPV6) == 0) { 1897141545Srwatson docontinue: 1898141545Srwatson INP_UNLOCK(in6p); 189953541Sshin continue; 1900141545Srwatson } 190153541Sshin if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6) 1902141545Srwatson goto docontinue; 190353541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 190453541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 1905141545Srwatson goto docontinue; 190653541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 190753541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 1908141545Srwatson goto docontinue; 190953541Sshin if (in6p->in6p_icmp6filt 191053541Sshin && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, 191153541Sshin in6p->in6p_icmp6filt)) 1912141545Srwatson goto docontinue; 191353541Sshin if (last) { 1914121809Sume struct mbuf *n = NULL; 1915121809Sume 1916121809Sume /* 1917121809Sume * Recent network drivers tend to allocate a single 1918121809Sume * mbuf cluster, rather than to make a couple of 1919121809Sume * mbufs without clusters. Also, since the IPv6 code 1920121809Sume * path tries to avoid m_pullup(), it is highly 1921121809Sume * probable that we still have an mbuf cluster here 1922121809Sume * even though the necessary length can be stored in an 1923121809Sume * mbuf's internal buffer. 1924121809Sume * Meanwhile, the default size of the receive socket 1925121809Sume * buffer for raw sockets is not so large. This means 1926121809Sume * the possibility of packet loss is relatively higher 1927121809Sume * than before. To avoid this scenario, we copy the 1928121809Sume * received data to a separate mbuf that does not use 1929121809Sume * a cluster, if possible. 1930121809Sume * XXX: it is better to copy the data after stripping 1931121809Sume * intermediate headers. 1932121809Sume */ 1933121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 1934121809Sume m->m_len <= MHLEN) { 1935121809Sume MGET(n, M_DONTWAIT, m->m_type); 1936121809Sume if (n != NULL) { 1937145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 1938145065Sgnn bcopy(m->m_data, n->m_data, 1939145065Sgnn m->m_len); 1940145065Sgnn n->m_len = m->m_len; 1941145065Sgnn } else { 1942145065Sgnn m_free(n); 1943145065Sgnn n = NULL; 1944145065Sgnn } 1945121809Sume } 1946121809Sume } 1947121809Sume if (n != NULL || 1948121809Sume (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 194953541Sshin if (last->in6p_flags & IN6P_CONTROLOPTS) 1950121674Sume ip6_savecontrol(last, n, &opts); 195153541Sshin /* strip intermediate headers */ 195253541Sshin m_adj(n, off); 195353541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 1954121315Sume (struct sockaddr *)&fromsa, n, opts) 1955120891Sume == 0) { 195653541Sshin /* should notify about lost packet */ 195753541Sshin m_freem(n); 195878064Sume if (opts) { 195953541Sshin m_freem(opts); 196078064Sume } 196197658Stanimura } else 196253541Sshin sorwakeup(last->in6p_socket); 196353541Sshin opts = NULL; 196453541Sshin } 1965141545Srwatson INP_UNLOCK(last); 196653541Sshin } 196753541Sshin last = in6p; 196853541Sshin } 196953541Sshin if (last) { 197053541Sshin if (last->in6p_flags & IN6P_CONTROLOPTS) 1971121674Sume ip6_savecontrol(last, m, &opts); 197253541Sshin /* strip intermediate headers */ 197353541Sshin m_adj(m, off); 1974121809Sume 1975121809Sume /* avoid using mbuf clusters if possible (see above) */ 1976121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 1977121809Sume m->m_len <= MHLEN) { 1978121809Sume struct mbuf *n; 1979121809Sume 1980121809Sume MGET(n, M_DONTWAIT, m->m_type); 1981121809Sume if (n != NULL) { 1982145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 1983145065Sgnn bcopy(m->m_data, n->m_data, m->m_len); 1984145065Sgnn n->m_len = m->m_len; 1985145065Sgnn 1986145065Sgnn m_freem(m); 1987145065Sgnn m = n; 1988145065Sgnn } else { 1989145065Sgnn m_freem(n); 1990145065Sgnn n = NULL; 1991145065Sgnn } 1992121809Sume } 1993121809Sume } 199453541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 1995121315Sume (struct sockaddr *)&fromsa, m, opts) == 0) { 199653541Sshin m_freem(m); 199753541Sshin if (opts) 199853541Sshin m_freem(opts); 199997658Stanimura } else 200053541Sshin sorwakeup(last->in6p_socket); 2001141545Srwatson INP_UNLOCK(last); 200253541Sshin } else { 200353541Sshin m_freem(m); 200453541Sshin ip6stat.ip6s_delivered--; 200553541Sshin } 2006141545Srwatson INP_INFO_RUNLOCK(&ripcbinfo); 200753541Sshin return IPPROTO_DONE; 200853541Sshin} 200953541Sshin 201053541Sshin/* 201153541Sshin * Reflect the ip6 packet back to the source. 201262587Sitojun * OFF points to the icmp6 header, counted from the top of the mbuf. 201353541Sshin */ 201453541Sshinvoid 201553541Sshinicmp6_reflect(m, off) 201653541Sshin struct mbuf *m; 201753541Sshin size_t off; 201853541Sshin{ 201962587Sitojun struct ip6_hdr *ip6; 202053541Sshin struct icmp6_hdr *icmp6; 202153541Sshin struct in6_ifaddr *ia; 202253541Sshin struct in6_addr t, *src = 0; 202362587Sitojun int plen; 202453541Sshin int type, code; 202553541Sshin struct ifnet *outif = NULL; 202678064Sume struct sockaddr_in6 sa6_src, sa6_dst; 202753541Sshin#ifdef COMPAT_RFC1885 202853541Sshin int mtu = IPV6_MMTU; 202953541Sshin struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst; 203053541Sshin#endif 203153541Sshin 203262587Sitojun /* too short to reflect */ 203362587Sitojun if (off < sizeof(struct ip6_hdr)) { 203478064Sume nd6log((LOG_DEBUG, 203578064Sume "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 203678064Sume (u_long)off, (u_long)sizeof(struct ip6_hdr), 203778064Sume __FILE__, __LINE__)); 203862587Sitojun goto bad; 203962587Sitojun } 204062587Sitojun 204153541Sshin /* 204253541Sshin * If there are extra headers between IPv6 and ICMPv6, strip 204353541Sshin * off that header first. 204453541Sshin */ 204562587Sitojun#ifdef DIAGNOSTIC 204662587Sitojun if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) 204762587Sitojun panic("assumption failed in icmp6_reflect"); 204862587Sitojun#endif 204962587Sitojun if (off > sizeof(struct ip6_hdr)) { 205062587Sitojun size_t l; 205162587Sitojun struct ip6_hdr nip6; 205253541Sshin 205362587Sitojun l = off - sizeof(struct ip6_hdr); 205462587Sitojun m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); 205562587Sitojun m_adj(m, l); 205662587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 205762587Sitojun if (m->m_len < l) { 205862587Sitojun if ((m = m_pullup(m, l)) == NULL) 205962587Sitojun return; 206053541Sshin } 206162587Sitojun bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); 206262587Sitojun } else /* off == sizeof(struct ip6_hdr) */ { 206362587Sitojun size_t l; 206462587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 206562587Sitojun if (m->m_len < l) { 206662587Sitojun if ((m = m_pullup(m, l)) == NULL) 206762587Sitojun return; 206853541Sshin } 206953541Sshin } 207062587Sitojun plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 207162587Sitojun ip6 = mtod(m, struct ip6_hdr *); 207262587Sitojun ip6->ip6_nxt = IPPROTO_ICMPV6; 207353541Sshin icmp6 = (struct icmp6_hdr *)(ip6 + 1); 207453541Sshin type = icmp6->icmp6_type; /* keep type for statistics */ 207553541Sshin code = icmp6->icmp6_code; /* ditto. */ 207653541Sshin 207753541Sshin t = ip6->ip6_dst; 207853541Sshin /* 207953541Sshin * ip6_input() drops a packet if its src is multicast. 208053541Sshin * So, the src is never multicast. 208153541Sshin */ 208253541Sshin ip6->ip6_dst = ip6->ip6_src; 208353541Sshin 208478064Sume /* 208578064Sume * XXX: make sure to embed scope zone information, using 208678064Sume * already embedded IDs or the received interface (if any). 208778064Sume * Note that rcvif may be NULL. 208878064Sume * TODO: scoped routing case (XXX). 208978064Sume */ 209078064Sume bzero(&sa6_src, sizeof(sa6_src)); 209178064Sume sa6_src.sin6_family = AF_INET6; 209278064Sume sa6_src.sin6_len = sizeof(sa6_src); 209378064Sume sa6_src.sin6_addr = ip6->ip6_dst; 209478064Sume in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif); 209578064Sume in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL); 209678064Sume bzero(&sa6_dst, sizeof(sa6_dst)); 209778064Sume sa6_dst.sin6_family = AF_INET6; 209878064Sume sa6_dst.sin6_len = sizeof(sa6_dst); 209978064Sume sa6_dst.sin6_addr = t; 210078064Sume in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif); 210178064Sume in6_embedscope(&t, &sa6_dst, NULL, NULL); 210253541Sshin 210353541Sshin#ifdef COMPAT_RFC1885 210453541Sshin /* 210553541Sshin * xxx guess MTU 210653541Sshin * RFC 1885 requires that echo reply should be truncated if it 210753541Sshin * does not fit in with (return) path MTU, but the description was 210853541Sshin * removed in the new spec. 210953541Sshin */ 211053541Sshin if (icmp6_reflect_rt.ro_rt == 0 || 211153541Sshin ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) { 211253541Sshin if (icmp6_reflect_rt.ro_rt) { 211353541Sshin RTFREE(icmp6_reflect_rt.ro_rt); 211453541Sshin icmp6_reflect_rt.ro_rt = 0; 211553541Sshin } 211653541Sshin bzero(sin6, sizeof(*sin6)); 211753541Sshin sin6->sin6_family = PF_INET6; 211853541Sshin sin6->sin6_len = sizeof(struct sockaddr_in6); 211953541Sshin sin6->sin6_addr = ip6->ip6_dst; 212053541Sshin 2121122921Sandre rtalloc((struct route *)&icmp6_reflect_rt.ro_rt); 212253541Sshin } 212353541Sshin 212453541Sshin if (icmp6_reflect_rt.ro_rt == 0) 212553541Sshin goto bad; 212653541Sshin 212753541Sshin if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST) 212853541Sshin && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu) 212953541Sshin mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu; 213053541Sshin 213153541Sshin if (mtu < m->m_pkthdr.len) { 213253541Sshin plen -= (m->m_pkthdr.len - mtu); 213353541Sshin m_adj(m, mtu - m->m_pkthdr.len); 213453541Sshin } 213553541Sshin#endif 213653541Sshin /* 2137120891Sume * If the incoming packet was addressed directly to us (i.e. unicast), 213853541Sshin * use dst as the src for the reply. 2139120891Sume * The IN6_IFF_NOTREADY case should be VERY rare, but is possible 214062587Sitojun * (for example) when we encounter an error while forwarding procedure 214162587Sitojun * destined to a duplicated address of ours. 214253541Sshin */ 214353541Sshin for (ia = in6_ifaddr; ia; ia = ia->ia_next) 214453541Sshin if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) && 214562587Sitojun (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) { 214653541Sshin src = &t; 214753541Sshin break; 214853541Sshin } 214953541Sshin if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) { 215053541Sshin /* 215153541Sshin * This is the case if the dst is our link-local address 215295023Ssuz * and the sender is also ourselves. 215353541Sshin */ 215453541Sshin src = &t; 215553541Sshin } 215653541Sshin 215778064Sume if (src == 0) { 215878064Sume int e; 215978064Sume struct route_in6 ro; 216078064Sume 216153541Sshin /* 216262587Sitojun * This case matches to multicasts, our anycast, or unicasts 216395023Ssuz * that we do not own. Select a source address based on the 216478064Sume * source address of the erroneous packet. 216553541Sshin */ 216678064Sume bzero(&ro, sizeof(ro)); 216778064Sume src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e); 216878064Sume if (ro.ro_rt) 216978064Sume RTFREE(ro.ro_rt); /* XXX: we could use this */ 217078064Sume if (src == NULL) { 217178064Sume nd6log((LOG_DEBUG, 217278064Sume "icmp6_reflect: source can't be determined: " 217378064Sume "dst=%s, error=%d\n", 217478064Sume ip6_sprintf(&sa6_src.sin6_addr), e)); 217578064Sume goto bad; 217678064Sume } 217778064Sume } 217853541Sshin 217953541Sshin ip6->ip6_src = *src; 218053541Sshin 218153541Sshin ip6->ip6_flow = 0; 218262587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 218362587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 218453541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 2185121472Sume if (outif) 2186121472Sume ip6->ip6_hlim = ND_IFINFO(outif)->chlim; 2187121472Sume else if (m->m_pkthdr.rcvif) { 218853541Sshin /* XXX: This may not be the outgoing interface */ 2189121161Sume ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim; 219078064Sume } else 219178064Sume ip6->ip6_hlim = ip6_defhlim; 219253541Sshin 219353541Sshin icmp6->icmp6_cksum = 0; 219453541Sshin icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2195120891Sume sizeof(struct ip6_hdr), plen); 219653541Sshin 219753541Sshin /* 219878064Sume * XXX option handling 219953541Sshin */ 220053541Sshin 220153541Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 220253541Sshin 220353541Sshin#ifdef COMPAT_RFC1885 2204105194Ssam ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL); 220553541Sshin#else 2206105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 220753541Sshin#endif 220853541Sshin if (outif) 220953541Sshin icmp6_ifoutstat_inc(outif, type, code); 221053541Sshin 221153541Sshin return; 221253541Sshin 221353541Sshin bad: 221453541Sshin m_freem(m); 221553541Sshin return; 221653541Sshin} 221753541Sshin 221853541Sshinvoid 221953541Sshinicmp6_fasttimo() 222053541Sshin{ 222162587Sitojun 222253541Sshin mld6_fasttimeo(); 222353541Sshin} 222453541Sshin 222553541Sshinstatic const char * 222653541Sshinicmp6_redirect_diag(src6, dst6, tgt6) 222753541Sshin struct in6_addr *src6; 222853541Sshin struct in6_addr *dst6; 222953541Sshin struct in6_addr *tgt6; 223053541Sshin{ 223153541Sshin static char buf[1024]; 223253541Sshin snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", 2233120891Sume ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6)); 223453541Sshin return buf; 223553541Sshin} 223653541Sshin 223753541Sshinvoid 223853541Sshinicmp6_redirect_input(m, off) 223978064Sume struct mbuf *m; 224053541Sshin int off; 224153541Sshin{ 224253541Sshin struct ifnet *ifp = m->m_pkthdr.rcvif; 224353541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 224462587Sitojun struct nd_redirect *nd_rd; 224553541Sshin int icmp6len = ntohs(ip6->ip6_plen); 224653541Sshin char *lladdr = NULL; 224753541Sshin int lladdrlen = 0; 224853541Sshin u_char *redirhdr = NULL; 224953541Sshin int redirhdrlen = 0; 225053541Sshin struct rtentry *rt = NULL; 225153541Sshin int is_router; 225253541Sshin int is_onlink; 225353541Sshin struct in6_addr src6 = ip6->ip6_src; 225462587Sitojun struct in6_addr redtgt6; 225562587Sitojun struct in6_addr reddst6; 225653541Sshin union nd_opts ndopts; 225753541Sshin 225853541Sshin if (!m || !ifp) 225953541Sshin return; 226053541Sshin 226153541Sshin /* XXX if we are router, we don't update route by icmp6 redirect */ 226253541Sshin if (ip6_forwarding) 226362587Sitojun goto freeit; 226453541Sshin if (!icmp6_rediraccept) 226562587Sitojun goto freeit; 226662587Sitojun 226762587Sitojun#ifndef PULLDOWN_TEST 226862587Sitojun IP6_EXTHDR_CHECK(m, off, icmp6len,); 226962587Sitojun nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); 227062587Sitojun#else 227162587Sitojun IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); 227262587Sitojun if (nd_rd == NULL) { 227362587Sitojun icmp6stat.icp6s_tooshort++; 227453541Sshin return; 227562587Sitojun } 227662587Sitojun#endif 227762587Sitojun redtgt6 = nd_rd->nd_rd_target; 227862587Sitojun reddst6 = nd_rd->nd_rd_dst; 227953541Sshin 228053541Sshin if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 228153541Sshin redtgt6.s6_addr16[1] = htons(ifp->if_index); 228253541Sshin if (IN6_IS_ADDR_LINKLOCAL(&reddst6)) 228353541Sshin reddst6.s6_addr16[1] = htons(ifp->if_index); 228453541Sshin 228553541Sshin /* validation */ 228653541Sshin if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 228778064Sume nd6log((LOG_ERR, 2288120891Sume "ICMP6 redirect sent from %s rejected; " 2289120891Sume "must be from linklocal\n", 2290120891Sume ip6_sprintf(&src6))); 229178064Sume goto bad; 229253541Sshin } 229353541Sshin if (ip6->ip6_hlim != 255) { 229478064Sume nd6log((LOG_ERR, 2295120891Sume "ICMP6 redirect sent from %s rejected; " 2296120891Sume "hlim=%d (must be 255)\n", 2297120891Sume ip6_sprintf(&src6), ip6->ip6_hlim)); 229878064Sume goto bad; 229953541Sshin } 230053541Sshin { 230153541Sshin /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 230253541Sshin struct sockaddr_in6 sin6; 230353541Sshin struct in6_addr *gw6; 230453541Sshin 230553541Sshin bzero(&sin6, sizeof(sin6)); 230653541Sshin sin6.sin6_family = AF_INET6; 230753541Sshin sin6.sin6_len = sizeof(struct sockaddr_in6); 230853541Sshin bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); 230953541Sshin rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 231053541Sshin if (rt) { 231165895Sume if (rt->rt_gateway == NULL || 231265895Sume rt->rt_gateway->sa_family != AF_INET6) { 231378064Sume nd6log((LOG_ERR, 231465895Sume "ICMP6 redirect rejected; no route " 231565895Sume "with inet6 gateway found for redirect dst: %s\n", 231678064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2317120727Ssam RTFREE_LOCKED(rt); 231878064Sume goto bad; 231965895Sume } 232065895Sume 232153541Sshin gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); 232253541Sshin if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { 232378064Sume nd6log((LOG_ERR, 2324120891Sume "ICMP6 redirect rejected; " 2325120891Sume "not equal to gw-for-src=%s (must be same): " 2326120891Sume "%s\n", 2327120891Sume ip6_sprintf(gw6), 2328120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2329120727Ssam RTFREE_LOCKED(rt); 233078064Sume goto bad; 233153541Sshin } 233253541Sshin } else { 233378064Sume nd6log((LOG_ERR, 2334120891Sume "ICMP6 redirect rejected; " 2335120891Sume "no route found for redirect dst: %s\n", 2336120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 233778064Sume goto bad; 233853541Sshin } 2339120727Ssam RTFREE_LOCKED(rt); 234053541Sshin rt = NULL; 234153541Sshin } 234253541Sshin if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 234378064Sume nd6log((LOG_ERR, 2344120891Sume "ICMP6 redirect rejected; " 2345120891Sume "redirect dst must be unicast: %s\n", 2346120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 234778064Sume goto bad; 234853541Sshin } 234953541Sshin 235053541Sshin is_router = is_onlink = 0; 235153541Sshin if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 235253541Sshin is_router = 1; /* router case */ 235353541Sshin if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 235453541Sshin is_onlink = 1; /* on-link destination case */ 235553541Sshin if (!is_router && !is_onlink) { 235678064Sume nd6log((LOG_ERR, 2357120891Sume "ICMP6 redirect rejected; " 2358120891Sume "neither router case nor onlink case: %s\n", 2359120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 236078064Sume goto bad; 236153541Sshin } 236253541Sshin /* validation passed */ 236353541Sshin 236453541Sshin icmp6len -= sizeof(*nd_rd); 236553541Sshin nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 236653541Sshin if (nd6_options(&ndopts) < 0) { 236778064Sume nd6log((LOG_INFO, "icmp6_redirect_input: " 2368120891Sume "invalid ND option, rejected: %s\n", 2369120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 237078064Sume /* nd6_options have incremented stats */ 237162587Sitojun goto freeit; 237253541Sshin } 237353541Sshin 237453541Sshin if (ndopts.nd_opts_tgt_lladdr) { 237553541Sshin lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 237653541Sshin lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 237753541Sshin } 237853541Sshin 237953541Sshin if (ndopts.nd_opts_rh) { 238053541Sshin redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len; 238153541Sshin redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */ 238253541Sshin } 238353541Sshin 238453541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 238578064Sume nd6log((LOG_INFO, 2386120891Sume "icmp6_redirect_input: lladdrlen mismatch for %s " 2387120891Sume "(if %d, icmp6 packet %d): %s\n", 2388120891Sume ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2, 2389120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 239078064Sume goto bad; 239153541Sshin } 239253541Sshin 239353541Sshin /* RFC 2461 8.3 */ 239453541Sshin nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 2395120891Sume is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 239653541Sshin 239795023Ssuz if (!is_onlink) { /* better router case. perform rtredirect. */ 239853541Sshin /* perform rtredirect */ 239953541Sshin struct sockaddr_in6 sdst; 240053541Sshin struct sockaddr_in6 sgw; 240153541Sshin struct sockaddr_in6 ssrc; 240253541Sshin 240353541Sshin bzero(&sdst, sizeof(sdst)); 240453541Sshin bzero(&sgw, sizeof(sgw)); 240553541Sshin bzero(&ssrc, sizeof(ssrc)); 240653541Sshin sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; 240753541Sshin sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = 240853541Sshin sizeof(struct sockaddr_in6); 240953541Sshin bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); 241053541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 241153541Sshin bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 241253541Sshin rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw, 2413120891Sume (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST, 2414120891Sume (struct sockaddr *)&ssrc); 241553541Sshin } 241653541Sshin /* finally update cached route in each socket via pfctlinput */ 241753541Sshin { 241853541Sshin struct sockaddr_in6 sdst; 241953541Sshin 242053541Sshin bzero(&sdst, sizeof(sdst)); 242153541Sshin sdst.sin6_family = AF_INET6; 242253541Sshin sdst.sin6_len = sizeof(struct sockaddr_in6); 242353541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 242453541Sshin pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); 2425122062Sume#if defined(IPSEC) || defined(FAST_IPSEC) 242653541Sshin key_sa_routechange((struct sockaddr *)&sdst); 242753541Sshin#endif 242853541Sshin } 242962587Sitojun 243062587Sitojun freeit: 243162587Sitojun m_freem(m); 243278064Sume return; 243378064Sume 243478064Sume bad: 243578064Sume icmp6stat.icp6s_badredirect++; 243678064Sume m_freem(m); 243753541Sshin} 243853541Sshin 243953541Sshinvoid 244053541Sshinicmp6_redirect_output(m0, rt) 244153541Sshin struct mbuf *m0; 244253541Sshin struct rtentry *rt; 244353541Sshin{ 244453541Sshin struct ifnet *ifp; /* my outgoing interface */ 244553541Sshin struct in6_addr *ifp_ll6; 244653541Sshin struct in6_addr *router_ll6; 244753541Sshin struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 244853541Sshin struct mbuf *m = NULL; /* newly allocated one */ 244953541Sshin struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 245053541Sshin struct nd_redirect *nd_rd; 245153541Sshin size_t maxlen; 245253541Sshin u_char *p; 245353541Sshin struct ifnet *outif = NULL; 245462587Sitojun struct sockaddr_in6 src_sa; 245553541Sshin 245662587Sitojun icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0); 245762587Sitojun 245853541Sshin /* if we are not router, we don't send icmp6 redirect */ 245953541Sshin if (!ip6_forwarding || ip6_accept_rtadv) 246053541Sshin goto fail; 246153541Sshin 246253541Sshin /* sanity check */ 246353541Sshin if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) 246453541Sshin goto fail; 246553541Sshin 246653541Sshin /* 246753541Sshin * Address check: 246853541Sshin * the source address must identify a neighbor, and 246953541Sshin * the destination address must not be a multicast address 247053541Sshin * [RFC 2461, sec 8.2] 247153541Sshin */ 247253541Sshin sip6 = mtod(m0, struct ip6_hdr *); 247362587Sitojun bzero(&src_sa, sizeof(src_sa)); 247462587Sitojun src_sa.sin6_family = AF_INET6; 247562587Sitojun src_sa.sin6_len = sizeof(src_sa); 247662587Sitojun src_sa.sin6_addr = sip6->ip6_src; 247762587Sitojun /* we don't currently use sin6_scope_id, but eventually use it */ 2478121315Sume if (in6_addr2zoneid(ifp, &sip6->ip6_src, &src_sa.sin6_scope_id)) 2479121315Sume goto fail; 248062587Sitojun if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) 248153541Sshin goto fail; 248253541Sshin if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) 248353541Sshin goto fail; /* what should we do here? */ 248453541Sshin 248553541Sshin /* rate limit */ 248653541Sshin if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) 248753541Sshin goto fail; 248853541Sshin 248953541Sshin /* 249053541Sshin * Since we are going to append up to 1280 bytes (= IPV6_MMTU), 249153541Sshin * we almost always ask for an mbuf cluster for simplicity. 249253541Sshin * (MHLEN < IPV6_MMTU is almost always true) 249353541Sshin */ 249462587Sitojun#if IPV6_MMTU >= MCLBYTES 249562587Sitojun# error assumption failed about IPV6_MMTU and MCLBYTES 249662587Sitojun#endif 2497111119Simp MGETHDR(m, M_DONTWAIT, MT_HEADER); 249862587Sitojun if (m && IPV6_MMTU >= MHLEN) 2499111119Simp MCLGET(m, M_DONTWAIT); 250053541Sshin if (!m) 250153541Sshin goto fail; 250278064Sume m->m_pkthdr.rcvif = NULL; 250378064Sume m->m_len = 0; 250478064Sume maxlen = M_TRAILINGSPACE(m); 250553541Sshin maxlen = min(IPV6_MMTU, maxlen); 250653541Sshin /* just for safety */ 250762587Sitojun if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 250862587Sitojun ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { 250953541Sshin goto fail; 251062587Sitojun } 251153541Sshin 251253541Sshin { 251353541Sshin /* get ip6 linklocal address for ifp(my outgoing interface). */ 251462587Sitojun struct in6_ifaddr *ia; 251562587Sitojun if ((ia = in6ifa_ifpforlinklocal(ifp, 251662587Sitojun IN6_IFF_NOTREADY| 251762587Sitojun IN6_IFF_ANYCAST)) == NULL) 251853541Sshin goto fail; 251953541Sshin ifp_ll6 = &ia->ia_addr.sin6_addr; 252053541Sshin } 252153541Sshin 252253541Sshin /* get ip6 linklocal address for the router. */ 252353541Sshin if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { 252453541Sshin struct sockaddr_in6 *sin6; 252553541Sshin sin6 = (struct sockaddr_in6 *)rt->rt_gateway; 252653541Sshin router_ll6 = &sin6->sin6_addr; 252753541Sshin if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) 252853541Sshin router_ll6 = (struct in6_addr *)NULL; 252953541Sshin } else 253053541Sshin router_ll6 = (struct in6_addr *)NULL; 253153541Sshin 253253541Sshin /* ip6 */ 253353541Sshin ip6 = mtod(m, struct ip6_hdr *); 253453541Sshin ip6->ip6_flow = 0; 253562587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 253662587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 253753541Sshin /* ip6->ip6_plen will be set later */ 253853541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 253953541Sshin ip6->ip6_hlim = 255; 254053541Sshin /* ip6->ip6_src must be linklocal addr for my outgoing if. */ 254153541Sshin bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); 254253541Sshin bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); 254353541Sshin 254453541Sshin /* ND Redirect */ 254553541Sshin nd_rd = (struct nd_redirect *)(ip6 + 1); 254653541Sshin nd_rd->nd_rd_type = ND_REDIRECT; 254753541Sshin nd_rd->nd_rd_code = 0; 254853541Sshin nd_rd->nd_rd_reserved = 0; 254953541Sshin if (rt->rt_flags & RTF_GATEWAY) { 255053541Sshin /* 255153541Sshin * nd_rd->nd_rd_target must be a link-local address in 255253541Sshin * better router cases. 255353541Sshin */ 255453541Sshin if (!router_ll6) 255553541Sshin goto fail; 255653541Sshin bcopy(router_ll6, &nd_rd->nd_rd_target, 2557120892Sume sizeof(nd_rd->nd_rd_target)); 255853541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2559120892Sume sizeof(nd_rd->nd_rd_dst)); 256053541Sshin } else { 256153541Sshin /* make sure redtgt == reddst */ 256253541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, 2563120892Sume sizeof(nd_rd->nd_rd_target)); 256453541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2565120892Sume sizeof(nd_rd->nd_rd_dst)); 256653541Sshin } 256753541Sshin 256853541Sshin p = (u_char *)(nd_rd + 1); 256953541Sshin 257053541Sshin if (!router_ll6) 257153541Sshin goto nolladdropt; 257253541Sshin 2573120892Sume { 2574120892Sume /* target lladdr option */ 2575120892Sume struct rtentry *rt_router = NULL; 2576120892Sume int len; 2577120892Sume struct sockaddr_dl *sdl; 2578120892Sume struct nd_opt_hdr *nd_opt; 2579120892Sume char *lladdr; 258053541Sshin 2581120892Sume rt_router = nd6_lookup(router_ll6, 0, ifp); 2582120892Sume if (!rt_router) 2583120892Sume goto nolladdropt; 2584120892Sume len = sizeof(*nd_opt) + ifp->if_addrlen; 2585120892Sume len = (len + 7) & ~7; /* round by 8 */ 2586120892Sume /* safety check */ 2587120892Sume if (len + (p - (u_char *)ip6) > maxlen) 2588120892Sume goto nolladdropt; 2589120892Sume if (!(rt_router->rt_flags & RTF_GATEWAY) && 2590120892Sume (rt_router->rt_flags & RTF_LLINFO) && 2591120892Sume (rt_router->rt_gateway->sa_family == AF_LINK) && 2592120892Sume (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) && 2593120892Sume sdl->sdl_alen) { 2594120892Sume nd_opt = (struct nd_opt_hdr *)p; 2595120892Sume nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 2596120892Sume nd_opt->nd_opt_len = len >> 3; 2597120892Sume lladdr = (char *)(nd_opt + 1); 2598120892Sume bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen); 2599120892Sume p += len; 2600120892Sume } 2601120893Sume } 260253541Sshinnolladdropt:; 260353541Sshin 260453541Sshin m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 260553541Sshin 260653541Sshin /* just to be safe */ 260762587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 260853541Sshin if (m0->m_flags & M_DECRYPTED) 260953541Sshin goto noredhdropt; 261062587Sitojun#endif 261162587Sitojun if (p - (u_char *)ip6 > maxlen) 261262587Sitojun goto noredhdropt; 261353541Sshin 2614120891Sume { 2615120891Sume /* redirected header option */ 2616120891Sume int len; 2617120891Sume struct nd_opt_rd_hdr *nd_opt_rh; 261853541Sshin 2619120891Sume /* 2620120891Sume * compute the maximum size for icmp6 redirect header option. 2621120891Sume * XXX room for auth header? 2622120891Sume */ 2623120891Sume len = maxlen - (p - (u_char *)ip6); 2624120891Sume len &= ~7; 262553541Sshin 2626120891Sume /* This is just for simplicity. */ 2627120891Sume if (m0->m_pkthdr.len != m0->m_len) { 2628120891Sume if (m0->m_next) { 2629120891Sume m_freem(m0->m_next); 2630120891Sume m0->m_next = NULL; 2631120891Sume } 2632120891Sume m0->m_pkthdr.len = m0->m_len; 263353541Sshin } 263453541Sshin 2635120891Sume /* 2636120891Sume * Redirected header option spec (RFC2461 4.6.3) talks nothing 2637120891Sume * about padding/truncate rule for the original IP packet. 2638120891Sume * From the discussion on IPv6imp in Feb 1999, 2639120891Sume * the consensus was: 2640120891Sume * - "attach as much as possible" is the goal 2641120891Sume * - pad if not aligned (original size can be guessed by 2642120891Sume * original ip6 header) 2643120891Sume * Following code adds the padding if it is simple enough, 2644120891Sume * and truncates if not. 2645120891Sume */ 2646120891Sume if (m0->m_next || m0->m_pkthdr.len != m0->m_len) 2647120891Sume panic("assumption failed in %s:%d", __FILE__, 2648120891Sume __LINE__); 264953541Sshin 2650120891Sume if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { 2651120891Sume /* not enough room, truncate */ 2652120891Sume m0->m_pkthdr.len = m0->m_len = len - 2653120891Sume sizeof(*nd_opt_rh); 2654120891Sume } else { 2655120891Sume /* enough room, pad or truncate */ 2656120891Sume size_t extra; 265753541Sshin 2658120891Sume extra = m0->m_pkthdr.len % 8; 2659120891Sume if (extra) { 2660120891Sume /* pad if easy enough, truncate if not */ 2661120891Sume if (8 - extra <= M_TRAILINGSPACE(m0)) { 2662120891Sume /* pad */ 2663120891Sume m0->m_len += (8 - extra); 2664120891Sume m0->m_pkthdr.len += (8 - extra); 2665120891Sume } else { 2666120891Sume /* truncate */ 2667120891Sume m0->m_pkthdr.len -= extra; 2668120891Sume m0->m_len -= extra; 2669120891Sume } 267053541Sshin } 2671120891Sume len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); 2672120891Sume m0->m_pkthdr.len = m0->m_len = len - 2673120891Sume sizeof(*nd_opt_rh); 267453541Sshin } 267553541Sshin 2676120891Sume nd_opt_rh = (struct nd_opt_rd_hdr *)p; 2677120891Sume bzero(nd_opt_rh, sizeof(*nd_opt_rh)); 2678120891Sume nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; 2679120891Sume nd_opt_rh->nd_opt_rh_len = len >> 3; 2680120891Sume p += sizeof(*nd_opt_rh); 2681120891Sume m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 268253541Sshin 2683120891Sume /* connect m0 to m */ 2684120891Sume m_tag_delete_chain(m0, NULL); 2685120891Sume m0->m_flags &= ~M_PKTHDR; 2686120891Sume m->m_next = m0; 2687120891Sume m->m_pkthdr.len = m->m_len + m0->m_len; 2688120891Sume m0 = NULL; 2689120891Sume } 269053541Sshinnoredhdropt:; 2691112781Ssuz if (m0) { 2692112781Ssuz m_freem(m0); 2693112781Ssuz m0 = NULL; 2694112781Ssuz } 269553541Sshin 2696121315Sume /* XXX: clear embedded link IDs in the inner header */ 2697121315Sume in6_clearscope(&sip6->ip6_src); 2698121315Sume in6_clearscope(&sip6->ip6_dst); 2699121315Sume in6_clearscope(&nd_rd->nd_rd_target); 2700121315Sume in6_clearscope(&nd_rd->nd_rd_dst); 270153541Sshin 270253541Sshin ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 270353541Sshin 270453541Sshin nd_rd->nd_rd_cksum = 0; 2705120891Sume nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2706120891Sume sizeof(*ip6), ntohs(ip6->ip6_plen)); 270753541Sshin 270853541Sshin /* send the packet to outside... */ 2709105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 271053541Sshin if (outif) { 271153541Sshin icmp6_ifstat_inc(outif, ifs6_out_msg); 271253541Sshin icmp6_ifstat_inc(outif, ifs6_out_redirect); 271353541Sshin } 271453541Sshin icmp6stat.icp6s_outhist[ND_REDIRECT]++; 271553541Sshin 271653541Sshin return; 271753541Sshin 271853541Sshinfail: 271953541Sshin if (m) 272053541Sshin m_freem(m); 272153541Sshin if (m0) 272253541Sshin m_freem(m0); 272353541Sshin} 272453541Sshin 272553541Sshin/* 272653541Sshin * ICMPv6 socket option processing. 272753541Sshin */ 272853541Sshinint 272953541Sshinicmp6_ctloutput(so, sopt) 273053541Sshin struct socket *so; 273153541Sshin struct sockopt *sopt; 273253541Sshin{ 273353541Sshin int error = 0; 273453541Sshin int optlen; 273578064Sume struct inpcb *inp = sotoinpcb(so); 273653541Sshin int level, op, optname; 273753541Sshin 273853541Sshin if (sopt) { 273953541Sshin level = sopt->sopt_level; 274053541Sshin op = sopt->sopt_dir; 274153541Sshin optname = sopt->sopt_name; 274253541Sshin optlen = sopt->sopt_valsize; 274353541Sshin } else 274453541Sshin level = op = optname = optlen = 0; 274578064Sume 274653541Sshin if (level != IPPROTO_ICMPV6) { 274753541Sshin return EINVAL; 274853541Sshin } 274953541Sshin 275078064Sume switch (op) { 275153541Sshin case PRCO_SETOPT: 275253541Sshin switch (optname) { 275353541Sshin case ICMP6_FILTER: 275453541Sshin { 275553541Sshin struct icmp6_filter *p; 275653541Sshin 275753541Sshin if (optlen != sizeof(*p)) { 275853541Sshin error = EMSGSIZE; 275953541Sshin break; 276053541Sshin } 276153541Sshin if (inp->in6p_icmp6filt == NULL) { 276253541Sshin error = EINVAL; 276353541Sshin break; 276453541Sshin } 276553541Sshin error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen, 276653541Sshin optlen); 276753541Sshin break; 276853541Sshin } 276953541Sshin 277053541Sshin default: 277153541Sshin error = ENOPROTOOPT; 277253541Sshin break; 277353541Sshin } 277453541Sshin break; 277553541Sshin 277653541Sshin case PRCO_GETOPT: 277753541Sshin switch (optname) { 277853541Sshin case ICMP6_FILTER: 277953541Sshin { 278053541Sshin if (inp->in6p_icmp6filt == NULL) { 278153541Sshin error = EINVAL; 278253541Sshin break; 278353541Sshin } 278453541Sshin error = sooptcopyout(sopt, inp->in6p_icmp6filt, 278553541Sshin sizeof(struct icmp6_filter)); 278653541Sshin break; 278753541Sshin } 278853541Sshin 278953541Sshin default: 279053541Sshin error = ENOPROTOOPT; 279153541Sshin break; 279253541Sshin } 279353541Sshin break; 279453541Sshin } 279553541Sshin 2796120856Sume return (error); 279753541Sshin} 279853541Sshin 279953541Sshin/* 280053541Sshin * Perform rate limit check. 280153541Sshin * Returns 0 if it is okay to send the icmp6 packet. 280253541Sshin * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 280353541Sshin * limitation. 280453541Sshin * 280553541Sshin * XXX per-destination/type check necessary? 280653541Sshin */ 280753541Sshinstatic int 280853541Sshinicmp6_ratelimit(dst, type, code) 280953541Sshin const struct in6_addr *dst; /* not used at this moment */ 281053541Sshin const int type; /* not used at this moment */ 281153541Sshin const int code; /* not used at this moment */ 281253541Sshin{ 281362587Sitojun int ret; 281453541Sshin 281595023Ssuz ret = 0; /* okay to send */ 281653541Sshin 281762587Sitojun /* PPS limit */ 281878064Sume if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count, 281978064Sume icmp6errppslim)) { 282053541Sshin /* The packet is subject to rate limit */ 282162587Sitojun ret++; 282253541Sshin } 282353541Sshin 282462587Sitojun return ret; 282553541Sshin} 2826