icmp6.c revision 139826
162587Sitojun/* $FreeBSD: head/sys/netinet6/icmp6.c 139826 2005-01-07 02:30:35Z imp $ */ 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 11762587Sitojunextern struct inpcbhead ripcb; 11862587Sitojunextern int icmp6errppslim; 11962587Sitojunstatic int icmp6errpps_count = 0; 12078064Sumestatic struct timeval icmp6errppslim_last; 12162587Sitojunextern int icmp6_nodeinfo; 12253541Sshin 12362587Sitojunstatic void icmp6_errcount __P((struct icmp6errstat *, int, int)); 12462587Sitojunstatic int icmp6_rip6_input __P((struct mbuf **, int)); 12562587Sitojunstatic int icmp6_ratelimit __P((const struct in6_addr *, const int, const int)); 12662587Sitojunstatic const char *icmp6_redirect_diag __P((struct in6_addr *, 12762587Sitojun struct in6_addr *, struct in6_addr *)); 12862587Sitojunstatic struct mbuf *ni6_input __P((struct mbuf *, int)); 12962587Sitojunstatic struct mbuf *ni6_nametodns __P((const char *, int, int)); 13062587Sitojunstatic int ni6_dnsmatch __P((const char *, int, const char *, int)); 13162587Sitojunstatic int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *, 13278064Sume struct ifnet **, char *)); 13362587Sitojunstatic int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, 13462587Sitojun struct ifnet *, int)); 13578064Sumestatic int icmp6_notify_error __P((struct mbuf *, int, int, int)); 13653541Sshin 13753541Sshin#ifdef COMPAT_RFC1885 13862587Sitojunstatic struct route_in6 icmp6_reflect_rt; 13953541Sshin#endif 14053541Sshin 14178064Sume 14253541Sshinvoid 14353541Sshinicmp6_init() 14453541Sshin{ 14553541Sshin mld6_init(); 14653541Sshin} 14753541Sshin 14862587Sitojunstatic void 14962587Sitojunicmp6_errcount(stat, type, code) 15062587Sitojun struct icmp6errstat *stat; 15162587Sitojun int type, code; 15262587Sitojun{ 15378064Sume switch (type) { 15462587Sitojun case ICMP6_DST_UNREACH: 15562587Sitojun switch (code) { 15662587Sitojun case ICMP6_DST_UNREACH_NOROUTE: 15762587Sitojun stat->icp6errs_dst_unreach_noroute++; 15862587Sitojun return; 15962587Sitojun case ICMP6_DST_UNREACH_ADMIN: 16062587Sitojun stat->icp6errs_dst_unreach_admin++; 16162587Sitojun return; 16262587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 16362587Sitojun stat->icp6errs_dst_unreach_beyondscope++; 16462587Sitojun return; 16562587Sitojun case ICMP6_DST_UNREACH_ADDR: 16662587Sitojun stat->icp6errs_dst_unreach_addr++; 16762587Sitojun return; 16862587Sitojun case ICMP6_DST_UNREACH_NOPORT: 16962587Sitojun stat->icp6errs_dst_unreach_noport++; 17062587Sitojun return; 17162587Sitojun } 17262587Sitojun break; 17362587Sitojun case ICMP6_PACKET_TOO_BIG: 17462587Sitojun stat->icp6errs_packet_too_big++; 17562587Sitojun return; 17662587Sitojun case ICMP6_TIME_EXCEEDED: 17778064Sume switch (code) { 17862587Sitojun case ICMP6_TIME_EXCEED_TRANSIT: 17962587Sitojun stat->icp6errs_time_exceed_transit++; 18062587Sitojun return; 18162587Sitojun case ICMP6_TIME_EXCEED_REASSEMBLY: 18262587Sitojun stat->icp6errs_time_exceed_reassembly++; 18362587Sitojun return; 18462587Sitojun } 18562587Sitojun break; 18662587Sitojun case ICMP6_PARAM_PROB: 18778064Sume switch (code) { 18862587Sitojun case ICMP6_PARAMPROB_HEADER: 18962587Sitojun stat->icp6errs_paramprob_header++; 19062587Sitojun return; 19162587Sitojun case ICMP6_PARAMPROB_NEXTHEADER: 19262587Sitojun stat->icp6errs_paramprob_nextheader++; 19362587Sitojun return; 19462587Sitojun case ICMP6_PARAMPROB_OPTION: 19562587Sitojun stat->icp6errs_paramprob_option++; 19662587Sitojun return; 19762587Sitojun } 19862587Sitojun break; 19962587Sitojun case ND_REDIRECT: 20062587Sitojun stat->icp6errs_redirect++; 20162587Sitojun return; 20262587Sitojun } 20362587Sitojun stat->icp6errs_unknown++; 20462587Sitojun} 20562587Sitojun 20653541Sshin/* 20753541Sshin * Generate an error packet of type error in response to bad IP6 packet. 20853541Sshin */ 20953541Sshinvoid 21053541Sshinicmp6_error(m, type, code, param) 21153541Sshin struct mbuf *m; 21253541Sshin int type, code, param; 21353541Sshin{ 21453541Sshin struct ip6_hdr *oip6, *nip6; 21553541Sshin struct icmp6_hdr *icmp6; 21662587Sitojun u_int preplen; 21753541Sshin int off; 21862587Sitojun int nxt; 21953541Sshin 22053541Sshin icmp6stat.icp6s_error++; 22153541Sshin 22262587Sitojun /* count per-type-code statistics */ 22362587Sitojun icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code); 22462587Sitojun 22562587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 22662587Sitojun if (m->m_flags & M_DECRYPTED) { 22762587Sitojun icmp6stat.icp6s_canterror++; 22853541Sshin goto freeit; 22962587Sitojun } 23062587Sitojun#endif 23153541Sshin 23262587Sitojun#ifndef PULLDOWN_TEST 23362587Sitojun IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 23462587Sitojun#else 23562587Sitojun if (m->m_len < sizeof(struct ip6_hdr)) { 23662587Sitojun m = m_pullup(m, sizeof(struct ip6_hdr)); 23762587Sitojun if (m == NULL) 23862587Sitojun return; 23962587Sitojun } 24062587Sitojun#endif 24153541Sshin oip6 = mtod(m, struct ip6_hdr *); 24253541Sshin 24353541Sshin /* 244121472Sume * If the destination address of the erroneous packet is a multicast 245121472Sume * address, or the packet was sent using link-layer multicast, 246121472Sume * we should basically suppress sending an error (RFC 2463, Section 247121472Sume * 2.4). 248121472Sume * We have two exceptions (the item e.2 in that section): 249121472Sume * - the Pakcet Too Big message can be sent for path MTU discovery. 250121472Sume * - the Parameter Problem Message that can be allowed an icmp6 error 251121472Sume * in the option type field. This check has been done in 252121472Sume * ip6_unknown_opt(), so we can just check the type and code. 25353541Sshin */ 25453541Sshin if ((m->m_flags & (M_BCAST|M_MCAST) || 25553541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && 25653541Sshin (type != ICMP6_PACKET_TOO_BIG && 25753541Sshin (type != ICMP6_PARAM_PROB || 25853541Sshin code != ICMP6_PARAMPROB_OPTION))) 25953541Sshin goto freeit; 26053541Sshin 261121472Sume /* 262121472Sume * RFC 2463, 2.4 (e.5): source address check. 263121472Sume * XXX: the case of anycast source? 264121472Sume */ 26553541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || 26653541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) 26753541Sshin goto freeit; 26853541Sshin 26953541Sshin /* 27062587Sitojun * If we are about to send ICMPv6 against ICMPv6 error/redirect, 27162587Sitojun * don't do it. 27253541Sshin */ 27362587Sitojun nxt = -1; 27462587Sitojun off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 27562587Sitojun if (off >= 0 && nxt == IPPROTO_ICMPV6) { 27653541Sshin struct icmp6_hdr *icp; 27753541Sshin 27862587Sitojun#ifndef PULLDOWN_TEST 27962587Sitojun IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), ); 28062587Sitojun icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 28162587Sitojun#else 28262587Sitojun IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, 28362587Sitojun sizeof(*icp)); 28462587Sitojun if (icp == NULL) { 28562587Sitojun icmp6stat.icp6s_tooshort++; 28662587Sitojun return; 28762587Sitojun } 28862587Sitojun#endif 28962587Sitojun if (icp->icmp6_type < ICMP6_ECHO_REQUEST || 29062587Sitojun icp->icmp6_type == ND_REDIRECT) { 29153541Sshin /* 29262587Sitojun * ICMPv6 error 29362587Sitojun * Special case: for redirect (which is 29462587Sitojun * informational) we must not send icmp6 error. 29553541Sshin */ 29662587Sitojun icmp6stat.icp6s_canterror++; 29762587Sitojun goto freeit; 29862587Sitojun } else { 29962587Sitojun /* ICMPv6 informational - send the error */ 30053541Sshin } 30162587Sitojun } else { 30262587Sitojun /* non-ICMPv6 - send the error */ 30353541Sshin } 30453541Sshin 30553541Sshin oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */ 30653541Sshin 30753541Sshin /* Finally, do rate limitation check. */ 30853541Sshin if (icmp6_ratelimit(&oip6->ip6_src, type, code)) { 30953541Sshin icmp6stat.icp6s_toofreq++; 31053541Sshin goto freeit; 31153541Sshin } 31253541Sshin 31353541Sshin /* 31453541Sshin * OK, ICMP6 can be generated. 31553541Sshin */ 31653541Sshin 31753541Sshin if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) 31853541Sshin m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); 31953541Sshin 32062587Sitojun preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 321111119Simp M_PREPEND(m, preplen, M_DONTWAIT); 32262587Sitojun if (m && m->m_len < preplen) 32362587Sitojun m = m_pullup(m, preplen); 32453541Sshin if (m == NULL) { 32578064Sume nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__)); 32653541Sshin return; 32753541Sshin } 32853541Sshin 32953541Sshin nip6 = mtod(m, struct ip6_hdr *); 33053541Sshin nip6->ip6_src = oip6->ip6_src; 33153541Sshin nip6->ip6_dst = oip6->ip6_dst; 33253541Sshin 333121315Sume in6_clearscope(&oip6->ip6_src); 334121315Sume in6_clearscope(&oip6->ip6_dst); 33553541Sshin 33653541Sshin icmp6 = (struct icmp6_hdr *)(nip6 + 1); 33753541Sshin icmp6->icmp6_type = type; 33853541Sshin icmp6->icmp6_code = code; 33953541Sshin icmp6->icmp6_pptr = htonl((u_int32_t)param); 34053541Sshin 34178064Sume /* 34278064Sume * icmp6_reflect() is designed to be in the input path. 34378064Sume * icmp6_error() can be called from both input and outut path, 34478064Sume * and if we are in output path rcvif could contain bogus value. 34578064Sume * clear m->m_pkthdr.rcvif for safety, we should have enough scope 34678064Sume * information in ip header (nip6). 34778064Sume */ 34878064Sume m->m_pkthdr.rcvif = NULL; 34978064Sume 35053541Sshin icmp6stat.icp6s_outhist[type]++; 35195023Ssuz icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ 35262587Sitojun 35362587Sitojun return; 35462587Sitojun 35562587Sitojun freeit: 35662587Sitojun /* 35762587Sitojun * If we can't tell wheter or not we can generate ICMP6, free it. 35862587Sitojun */ 35962587Sitojun m_freem(m); 36053541Sshin} 36153541Sshin 36253541Sshin/* 36353541Sshin * Process a received ICMP6 message. 36453541Sshin */ 36553541Sshinint 36653541Sshinicmp6_input(mp, offp, proto) 36753541Sshin struct mbuf **mp; 36853541Sshin int *offp, proto; 36953541Sshin{ 37053541Sshin struct mbuf *m = *mp, *n; 37153541Sshin struct ip6_hdr *ip6, *nip6; 37253541Sshin struct icmp6_hdr *icmp6, *nicmp6; 37353541Sshin int off = *offp; 37453541Sshin int icmp6len = m->m_pkthdr.len - *offp; 37553541Sshin int code, sum, noff; 37653541Sshin 37762587Sitojun#ifndef PULLDOWN_TEST 37853541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE); 37995023Ssuz /* m might change if M_LOOP. So, call mtod after this */ 38062587Sitojun#endif 38153541Sshin 38253541Sshin /* 38353541Sshin * Locate icmp6 structure in mbuf, and check 38453541Sshin * that not corrupted and of at least minimum length 38553541Sshin */ 38653541Sshin 38753541Sshin ip6 = mtod(m, struct ip6_hdr *); 38853541Sshin if (icmp6len < sizeof(struct icmp6_hdr)) { 38953541Sshin icmp6stat.icp6s_tooshort++; 39053541Sshin goto freeit; 39153541Sshin } 39253541Sshin 39353541Sshin /* 39453541Sshin * calculate the checksum 39553541Sshin */ 39662587Sitojun#ifndef PULLDOWN_TEST 39753541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 39862587Sitojun#else 39962587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 40062587Sitojun if (icmp6 == NULL) { 40162587Sitojun icmp6stat.icp6s_tooshort++; 40262587Sitojun return IPPROTO_DONE; 40362587Sitojun } 40462587Sitojun#endif 40553541Sshin code = icmp6->icmp6_code; 40653541Sshin 40753541Sshin if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { 40878064Sume nd6log((LOG_ERR, 40953541Sshin "ICMP6 checksum error(%d|%x) %s\n", 41078064Sume icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src))); 41153541Sshin icmp6stat.icp6s_checksum++; 41253541Sshin goto freeit; 41353541Sshin } 41453541Sshin 41583934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 41653541Sshin /* 41753541Sshin * Deliver very specific ICMP6 type only. 41853541Sshin * This is important to deilver TOOBIG. Otherwise PMTUD 41953541Sshin * will not work. 42053541Sshin */ 42153541Sshin switch (icmp6->icmp6_type) { 42253541Sshin case ICMP6_DST_UNREACH: 42353541Sshin case ICMP6_PACKET_TOO_BIG: 42453541Sshin case ICMP6_TIME_EXCEEDED: 42553541Sshin break; 42653541Sshin default: 42753541Sshin goto freeit; 42853541Sshin } 42953541Sshin } 43053541Sshin 43153541Sshin icmp6stat.icp6s_inhist[icmp6->icmp6_type]++; 43253541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg); 43353541Sshin if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) 43453541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error); 43553541Sshin 43653541Sshin switch (icmp6->icmp6_type) { 43753541Sshin case ICMP6_DST_UNREACH: 43853541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach); 43953541Sshin switch (code) { 44053541Sshin case ICMP6_DST_UNREACH_NOROUTE: 44153541Sshin code = PRC_UNREACH_NET; 44253541Sshin break; 44353541Sshin case ICMP6_DST_UNREACH_ADMIN: 44453541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib); 44562587Sitojun code = PRC_UNREACH_PROTOCOL; /* is this a good code? */ 44662587Sitojun break; 44753541Sshin case ICMP6_DST_UNREACH_ADDR: 44862587Sitojun code = PRC_HOSTDEAD; 44953541Sshin break; 45062587Sitojun#ifdef COMPAT_RFC1885 45153541Sshin case ICMP6_DST_UNREACH_NOTNEIGHBOR: 45253541Sshin code = PRC_UNREACH_SRCFAIL; 45353541Sshin break; 45462587Sitojun#else 45562587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 45662587Sitojun /* I mean "source address was incorrect." */ 45762587Sitojun code = PRC_PARAMPROB; 45862587Sitojun break; 45962587Sitojun#endif 46053541Sshin case ICMP6_DST_UNREACH_NOPORT: 46153541Sshin code = PRC_UNREACH_PORT; 46253541Sshin break; 46353541Sshin default: 46453541Sshin goto badcode; 46553541Sshin } 46653541Sshin goto deliver; 46753541Sshin break; 46853541Sshin 46953541Sshin case ICMP6_PACKET_TOO_BIG: 47053541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig); 47153541Sshin if (code != 0) 47253541Sshin goto badcode; 47353541Sshin 474126194Sume /* validation is made in icmp6_mtudisc_update */ 475126194Sume 47653541Sshin code = PRC_MSGSIZE; 47753541Sshin 47862587Sitojun /* 47962587Sitojun * Updating the path MTU will be done after examining 48062587Sitojun * intermediate extension headers. 48162587Sitojun */ 48253541Sshin goto deliver; 48353541Sshin break; 48453541Sshin 48553541Sshin case ICMP6_TIME_EXCEEDED: 48653541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed); 48753541Sshin switch (code) { 48853541Sshin case ICMP6_TIME_EXCEED_TRANSIT: 48953541Sshin case ICMP6_TIME_EXCEED_REASSEMBLY: 49053541Sshin code += PRC_TIMXCEED_INTRANS; 49153541Sshin break; 49253541Sshin default: 49353541Sshin goto badcode; 49453541Sshin } 49553541Sshin goto deliver; 49653541Sshin break; 49753541Sshin 49853541Sshin case ICMP6_PARAM_PROB: 49953541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob); 50053541Sshin switch (code) { 50153541Sshin case ICMP6_PARAMPROB_NEXTHEADER: 50253541Sshin code = PRC_UNREACH_PROTOCOL; 50353541Sshin break; 50453541Sshin case ICMP6_PARAMPROB_HEADER: 50553541Sshin case ICMP6_PARAMPROB_OPTION: 50653541Sshin code = PRC_PARAMPROB; 50753541Sshin break; 50853541Sshin default: 50953541Sshin goto badcode; 51053541Sshin } 51153541Sshin goto deliver; 51253541Sshin break; 51353541Sshin 51453541Sshin case ICMP6_ECHO_REQUEST: 51553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo); 51653541Sshin if (code != 0) 51753541Sshin goto badcode; 51853541Sshin if ((n = m_copy(m, 0, M_COPYALL)) == NULL) { 51953541Sshin /* Give up remote */ 52053541Sshin break; 52153541Sshin } 52262587Sitojun if ((n->m_flags & M_EXT) != 0 52362587Sitojun || n->m_len < off + sizeof(struct icmp6_hdr)) { 52453541Sshin struct mbuf *n0 = n; 52562587Sitojun const int maxlen = sizeof(*nip6) + sizeof(*nicmp6); 526108741Ssam int n0len; 52753541Sshin 52853541Sshin /* 529120891Sume * Prepare an internal mbuf. m_pullup() doesn't 53053541Sshin * always copy the length we specified. 53153541Sshin */ 53262587Sitojun if (maxlen >= MCLBYTES) { 53362587Sitojun /* Give up remote */ 53462587Sitojun m_freem(n0); 53562587Sitojun break; 53662587Sitojun } 537111119Simp MGETHDR(n, M_DONTWAIT, n0->m_type); 538111070Ssam n0len = n0->m_pkthdr.len; /* save for use below */ 539111070Ssam if (n) 540111070Ssam M_MOVE_PKTHDR(n, n0); 54162587Sitojun if (n && maxlen >= MHLEN) { 542111119Simp MCLGET(n, M_DONTWAIT); 54362587Sitojun if ((n->m_flags & M_EXT) == 0) { 54462587Sitojun m_free(n); 54562587Sitojun n = NULL; 54662587Sitojun } 54762587Sitojun } 54853541Sshin if (n == NULL) { 54953541Sshin /* Give up remote */ 55053541Sshin m_freem(n0); 55153541Sshin break; 55253541Sshin } 55353541Sshin /* 55453541Sshin * Copy IPv6 and ICMPv6 only. 55553541Sshin */ 55653541Sshin nip6 = mtod(n, struct ip6_hdr *); 55753541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 55853541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 55953541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 56062587Sitojun noff = sizeof(struct ip6_hdr); 561108741Ssam /* new mbuf contains only ipv6+icmpv6 headers */ 562108741Ssam n->m_len = noff + sizeof(struct icmp6_hdr); 56353541Sshin /* 564120891Sume * Adjust mbuf. ip6_plen will be adjusted in 56562587Sitojun * ip6_output(). 56653541Sshin */ 56762587Sitojun m_adj(n0, off + sizeof(struct icmp6_hdr)); 568108741Ssam /* recalculate complete packet size */ 569108741Ssam n->m_pkthdr.len = n0len + (noff - off); 57062587Sitojun n->m_next = n0; 57153541Sshin } else { 57253541Sshin nip6 = mtod(n, struct ip6_hdr *); 57353541Sshin nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off); 57453541Sshin noff = off; 57553541Sshin } 57653541Sshin nicmp6->icmp6_type = ICMP6_ECHO_REPLY; 57753541Sshin nicmp6->icmp6_code = 0; 57853541Sshin if (n) { 57953541Sshin icmp6stat.icp6s_reflect++; 58053541Sshin icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++; 58153541Sshin icmp6_reflect(n, noff); 58253541Sshin } 58353541Sshin break; 58453541Sshin 58553541Sshin case ICMP6_ECHO_REPLY: 58653541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply); 58753541Sshin if (code != 0) 58853541Sshin goto badcode; 58953541Sshin break; 59053541Sshin 59196116Sume case MLD_LISTENER_QUERY: 59296116Sume case MLD_LISTENER_REPORT: 59396116Sume if (icmp6len < sizeof(struct mld_hdr)) 59453541Sshin goto badlen; 59596116Sume if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */ 59653541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery); 59753541Sshin else 59853541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport); 599111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 60062587Sitojun /* give up local */ 60162587Sitojun mld6_input(m, off); 60262587Sitojun m = NULL; 60362587Sitojun goto freeit; 60462587Sitojun } 60562587Sitojun mld6_input(n, off); 60653541Sshin /* m stays. */ 60753541Sshin break; 60853541Sshin 60996116Sume case MLD_LISTENER_DONE: 61053541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone); 61196116Sume if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */ 61253541Sshin goto badlen; 61353541Sshin break; /* nothing to be done in kernel */ 61453541Sshin 61596116Sume case MLD_MTRACE_RESP: 61696116Sume case MLD_MTRACE: 617120891Sume /* XXX: these two are experimental. not officially defined. */ 61853541Sshin /* XXX: per-interface statistics? */ 61962587Sitojun break; /* just pass it to applications */ 62053541Sshin 62153541Sshin case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ 62253541Sshin { 62353541Sshin enum { WRU, FQDN } mode; 62453541Sshin 62562587Sitojun if (!icmp6_nodeinfo) 62662587Sitojun break; 62762587Sitojun 62853541Sshin if (icmp6len == sizeof(struct icmp6_hdr) + 4) 62953541Sshin mode = WRU; 63062587Sitojun else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) 63153541Sshin mode = FQDN; 63253541Sshin else 63353541Sshin goto badlen; 63453541Sshin 63553541Sshin#define hostnamelen strlen(hostname) 63653541Sshin if (mode == FQDN) { 63762587Sitojun#ifndef PULLDOWN_TEST 63853541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), 639120891Sume IPPROTO_DONE); 64062587Sitojun#endif 64162587Sitojun n = m_copy(m, 0, M_COPYALL); 64262587Sitojun if (n) 64362587Sitojun n = ni6_input(n, off); 64462587Sitojun /* XXX meaningless if n == NULL */ 64553541Sshin noff = sizeof(struct ip6_hdr); 64653541Sshin } else { 64753541Sshin u_char *p; 64862587Sitojun int maxlen, maxhlen; 64953541Sshin 650120891Sume if ((icmp6_nodeinfo & 5) != 5) 65178064Sume break; 65278064Sume 65362587Sitojun if (code != 0) 65462587Sitojun goto badcode; 65562587Sitojun maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4; 65662587Sitojun if (maxlen >= MCLBYTES) { 65762587Sitojun /* Give up remote */ 65862587Sitojun break; 65962587Sitojun } 660111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 66162587Sitojun if (n && maxlen > MHLEN) { 662111119Simp MCLGET(n, M_DONTWAIT); 66362587Sitojun if ((n->m_flags & M_EXT) == 0) { 66462587Sitojun m_free(n); 66562587Sitojun n = NULL; 66662587Sitojun } 66762587Sitojun } 668111119Simp if (!m_dup_pkthdr(n, m, M_DONTWAIT)) { 669108466Ssam /* 670108466Ssam * Previous code did a blind M_COPY_PKTHDR 671108466Ssam * and said "just for rcvif". If true, then 672108466Ssam * we could tolerate the dup failing (due to 673108466Ssam * the deep copy of the tag chain). For now 674108466Ssam * be conservative and just fail. 675108466Ssam */ 676108466Ssam m_free(n); 677108466Ssam n = NULL; 678108466Ssam } 67953541Sshin if (n == NULL) { 68053541Sshin /* Give up remote */ 68153541Sshin break; 68253541Sshin } 68378064Sume n->m_pkthdr.rcvif = NULL; 68462587Sitojun n->m_len = 0; 68562587Sitojun maxhlen = M_TRAILINGSPACE(n) - maxlen; 68662587Sitojun if (maxhlen > hostnamelen) 68762587Sitojun maxhlen = hostnamelen; 68853541Sshin /* 68953541Sshin * Copy IPv6 and ICMPv6 only. 69053541Sshin */ 69153541Sshin nip6 = mtod(n, struct ip6_hdr *); 69253541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 69353541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 69453541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 69553541Sshin p = (u_char *)(nicmp6 + 1); 69653541Sshin bzero(p, 4); 69795023Ssuz bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */ 69853541Sshin noff = sizeof(struct ip6_hdr); 69953541Sshin n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 70062587Sitojun sizeof(struct icmp6_hdr) + 4 + maxhlen; 70153541Sshin nicmp6->icmp6_type = ICMP6_WRUREPLY; 70253541Sshin nicmp6->icmp6_code = 0; 70353541Sshin } 70453541Sshin#undef hostnamelen 70553541Sshin if (n) { 70653541Sshin icmp6stat.icp6s_reflect++; 70753541Sshin icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++; 70853541Sshin icmp6_reflect(n, noff); 70953541Sshin } 71053541Sshin break; 71153541Sshin } 71253541Sshin 71353541Sshin case ICMP6_WRUREPLY: 71453541Sshin if (code != 0) 71553541Sshin goto badcode; 71653541Sshin break; 71753541Sshin 71853541Sshin case ND_ROUTER_SOLICIT: 71953541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit); 72053541Sshin if (code != 0) 72153541Sshin goto badcode; 72253541Sshin if (icmp6len < sizeof(struct nd_router_solicit)) 72353541Sshin goto badlen; 724111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 72562587Sitojun /* give up local */ 72662587Sitojun nd6_rs_input(m, off, icmp6len); 72762587Sitojun m = NULL; 72862587Sitojun goto freeit; 72962587Sitojun } 73062587Sitojun nd6_rs_input(n, off, icmp6len); 73153541Sshin /* m stays. */ 73253541Sshin break; 73353541Sshin 73453541Sshin case ND_ROUTER_ADVERT: 73553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert); 73653541Sshin if (code != 0) 73753541Sshin goto badcode; 73853541Sshin if (icmp6len < sizeof(struct nd_router_advert)) 73953541Sshin goto badlen; 740111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 74162587Sitojun /* give up local */ 74262587Sitojun nd6_ra_input(m, off, icmp6len); 74362587Sitojun m = NULL; 74462587Sitojun goto freeit; 74562587Sitojun } 74662587Sitojun nd6_ra_input(n, off, icmp6len); 74753541Sshin /* m stays. */ 74853541Sshin break; 74953541Sshin 75053541Sshin case ND_NEIGHBOR_SOLICIT: 75153541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit); 75253541Sshin if (code != 0) 75353541Sshin goto badcode; 75453541Sshin if (icmp6len < sizeof(struct nd_neighbor_solicit)) 75553541Sshin goto badlen; 756111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 75762587Sitojun /* give up local */ 75862587Sitojun nd6_ns_input(m, off, icmp6len); 75962587Sitojun m = NULL; 76062587Sitojun goto freeit; 76162587Sitojun } 76262587Sitojun nd6_ns_input(n, off, icmp6len); 76353541Sshin /* m stays. */ 76453541Sshin break; 76553541Sshin 76653541Sshin case ND_NEIGHBOR_ADVERT: 76753541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert); 76853541Sshin if (code != 0) 76953541Sshin goto badcode; 77053541Sshin if (icmp6len < sizeof(struct nd_neighbor_advert)) 77153541Sshin goto badlen; 772111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 77362587Sitojun /* give up local */ 77462587Sitojun nd6_na_input(m, off, icmp6len); 77562587Sitojun m = NULL; 77662587Sitojun goto freeit; 77762587Sitojun } 77862587Sitojun nd6_na_input(n, off, icmp6len); 77953541Sshin /* m stays. */ 78053541Sshin break; 78153541Sshin 78253541Sshin case ND_REDIRECT: 78353541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect); 78453541Sshin if (code != 0) 78553541Sshin goto badcode; 78653541Sshin if (icmp6len < sizeof(struct nd_redirect)) 78753541Sshin goto badlen; 788111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 78962587Sitojun /* give up local */ 79062587Sitojun icmp6_redirect_input(m, off); 79162587Sitojun m = NULL; 79262587Sitojun goto freeit; 79362587Sitojun } 79462587Sitojun icmp6_redirect_input(n, off); 79553541Sshin /* m stays. */ 79653541Sshin break; 79753541Sshin 79853541Sshin case ICMP6_ROUTER_RENUMBERING: 79953541Sshin if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && 80053541Sshin code != ICMP6_ROUTER_RENUMBERING_RESULT) 80153541Sshin goto badcode; 80253541Sshin if (icmp6len < sizeof(struct icmp6_router_renum)) 80353541Sshin goto badlen; 80453541Sshin break; 80553541Sshin 80653541Sshin default: 80778064Sume nd6log((LOG_DEBUG, 80878064Sume "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", 80978064Sume icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src), 81078064Sume ip6_sprintf(&ip6->ip6_dst), 81178064Sume m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0)); 81253541Sshin if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { 81353541Sshin /* ICMPv6 error: MUST deliver it by spec... */ 81453541Sshin code = PRC_NCMDS; 81553541Sshin /* deliver */ 81653541Sshin } else { 81753541Sshin /* ICMPv6 informational: MUST not deliver */ 81853541Sshin break; 81953541Sshin } 82053541Sshin deliver: 82178064Sume if (icmp6_notify_error(m, off, icmp6len, code)) { 82278064Sume /* In this case, m should've been freed. */ 823120856Sume return (IPPROTO_DONE); 82453541Sshin } 82578064Sume break; 82678064Sume 82778064Sume badcode: 82878064Sume icmp6stat.icp6s_badcode++; 82978064Sume break; 83078064Sume 83178064Sume badlen: 83278064Sume icmp6stat.icp6s_badlen++; 83378064Sume break; 83478064Sume } 83578064Sume 83678064Sume /* deliver the packet to appropriate sockets */ 83778064Sume icmp6_rip6_input(&m, *offp); 83878064Sume 83978064Sume return IPPROTO_DONE; 84078064Sume 84178064Sume freeit: 84278064Sume m_freem(m); 84378064Sume return IPPROTO_DONE; 84478064Sume} 84578064Sume 84678064Sumestatic int 84778064Sumeicmp6_notify_error(m, off, icmp6len, code) 84878064Sume struct mbuf *m; 849113799Sobrien int off, icmp6len, code; 85078064Sume{ 85178064Sume struct icmp6_hdr *icmp6; 85278064Sume struct ip6_hdr *eip6; 85378064Sume u_int32_t notifymtu; 85478064Sume struct sockaddr_in6 icmp6src, icmp6dst; 85578064Sume 85678064Sume if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { 85778064Sume icmp6stat.icp6s_tooshort++; 85878064Sume goto freeit; 85978064Sume } 86062587Sitojun#ifndef PULLDOWN_TEST 86178064Sume IP6_EXTHDR_CHECK(m, off, 862120891Sume sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1); 86378064Sume icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 86462587Sitojun#else 86578064Sume IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 866120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 86778064Sume if (icmp6 == NULL) { 86878064Sume icmp6stat.icp6s_tooshort++; 869120856Sume return (-1); 87078064Sume } 87162587Sitojun#endif 87278064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 87353541Sshin 87478064Sume /* Detect the upper level protocol */ 87578064Sume { 87653541Sshin void (*ctlfunc) __P((int, struct sockaddr *, void *)); 87753541Sshin u_int8_t nxt = eip6->ip6_nxt; 87853541Sshin int eoff = off + sizeof(struct icmp6_hdr) + 879120891Sume sizeof(struct ip6_hdr); 88053541Sshin struct ip6ctlparam ip6cp; 88162587Sitojun struct in6_addr *finaldst = NULL; 88262587Sitojun int icmp6type = icmp6->icmp6_type; 88362587Sitojun struct ip6_frag *fh; 88462587Sitojun struct ip6_rthdr *rth; 88562587Sitojun struct ip6_rthdr0 *rth0; 88662587Sitojun int rthlen; 88753541Sshin 88895023Ssuz while (1) { /* XXX: should avoid infinite loop explicitly? */ 88953541Sshin struct ip6_ext *eh; 89053541Sshin 89178064Sume switch (nxt) { 89253541Sshin case IPPROTO_HOPOPTS: 89353541Sshin case IPPROTO_DSTOPTS: 89453541Sshin case IPPROTO_AH: 89562587Sitojun#ifndef PULLDOWN_TEST 896120891Sume IP6_EXTHDR_CHECK(m, 0, 897120891Sume eoff + sizeof(struct ip6_ext), -1); 898120892Sume eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff); 89962587Sitojun#else 90062587Sitojun IP6_EXTHDR_GET(eh, struct ip6_ext *, m, 901120891Sume eoff, sizeof(*eh)); 90262587Sitojun if (eh == NULL) { 90362587Sitojun icmp6stat.icp6s_tooshort++; 904120856Sume return (-1); 90562587Sitojun } 90662587Sitojun#endif 907120891Sume 90853541Sshin if (nxt == IPPROTO_AH) 90953541Sshin eoff += (eh->ip6e_len + 2) << 2; 91053541Sshin else 91153541Sshin eoff += (eh->ip6e_len + 1) << 3; 91253541Sshin nxt = eh->ip6e_nxt; 91353541Sshin break; 91462587Sitojun case IPPROTO_ROUTING: 91562587Sitojun /* 91662587Sitojun * When the erroneous packet contains a 91762587Sitojun * routing header, we should examine the 91862587Sitojun * header to determine the final destination. 91962587Sitojun * Otherwise, we can't properly update 92062587Sitojun * information that depends on the final 92162587Sitojun * destination (e.g. path MTU). 92262587Sitojun */ 92362587Sitojun#ifndef PULLDOWN_TEST 924120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1); 925120891Sume rth = (struct ip6_rthdr *) 926120891Sume (mtod(m, caddr_t) + eoff); 92762587Sitojun#else 92862587Sitojun IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m, 929120891Sume eoff, sizeof(*rth)); 93062587Sitojun if (rth == NULL) { 93162587Sitojun icmp6stat.icp6s_tooshort++; 932120856Sume return (-1); 93362587Sitojun } 93462587Sitojun#endif 93562587Sitojun rthlen = (rth->ip6r_len + 1) << 3; 93662587Sitojun /* 93762587Sitojun * XXX: currently there is no 93862587Sitojun * officially defined type other 93962587Sitojun * than type-0. 94062587Sitojun * Note that if the segment left field 94162587Sitojun * is 0, all intermediate hops must 94262587Sitojun * have been passed. 94362587Sitojun */ 94462587Sitojun if (rth->ip6r_segleft && 94562587Sitojun rth->ip6r_type == IPV6_RTHDR_TYPE_0) { 94662587Sitojun int hops; 94762587Sitojun 94862587Sitojun#ifndef PULLDOWN_TEST 949120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1); 950120891Sume rth0 = (struct ip6_rthdr0 *) 951120891Sume (mtod(m, caddr_t) + eoff); 95262587Sitojun#else 95362587Sitojun IP6_EXTHDR_GET(rth0, 954120891Sume struct ip6_rthdr0 *, m, 955120891Sume eoff, rthlen); 95662587Sitojun if (rth0 == NULL) { 95762587Sitojun icmp6stat.icp6s_tooshort++; 958120856Sume return (-1); 95962587Sitojun } 96062587Sitojun#endif 96162587Sitojun /* just ignore a bogus header */ 96262587Sitojun if ((rth0->ip6r0_len % 2) == 0 && 96362587Sitojun (hops = rth0->ip6r0_len/2)) 96462587Sitojun finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1); 96562587Sitojun } 96662587Sitojun eoff += rthlen; 96762587Sitojun nxt = rth->ip6r_nxt; 96862587Sitojun break; 96962587Sitojun case IPPROTO_FRAGMENT: 97062587Sitojun#ifndef PULLDOWN_TEST 97162587Sitojun IP6_EXTHDR_CHECK(m, 0, eoff + 972120891Sume sizeof(struct ip6_frag), -1); 973120891Sume fh = (struct ip6_frag *)(mtod(m, caddr_t) + 974120891Sume eoff); 97562587Sitojun#else 97662587Sitojun IP6_EXTHDR_GET(fh, struct ip6_frag *, m, 977120891Sume eoff, sizeof(*fh)); 97862587Sitojun if (fh == NULL) { 97962587Sitojun icmp6stat.icp6s_tooshort++; 980120856Sume return (-1); 98162587Sitojun } 98262587Sitojun#endif 98362587Sitojun /* 98462587Sitojun * Data after a fragment header is meaningless 98562587Sitojun * unless it is the first fragment, but 98662587Sitojun * we'll go to the notify label for path MTU 98762587Sitojun * discovery. 98862587Sitojun */ 98962587Sitojun if (fh->ip6f_offlg & IP6F_OFF_MASK) 99062587Sitojun goto notify; 99162587Sitojun 99262587Sitojun eoff += sizeof(struct ip6_frag); 99362587Sitojun nxt = fh->ip6f_nxt; 99462587Sitojun break; 99553541Sshin default: 99662587Sitojun /* 99762587Sitojun * This case includes ESP and the No Next 99895023Ssuz * Header. In such cases going to the notify 99962587Sitojun * label does not have any meaning 100062587Sitojun * (i.e. ctlfunc will be NULL), but we go 100162587Sitojun * anyway since we might have to update 100262587Sitojun * path MTU information. 100362587Sitojun */ 100453541Sshin goto notify; 100553541Sshin } 100653541Sshin } 100778064Sume notify: 100862587Sitojun#ifndef PULLDOWN_TEST 100953541Sshin icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 101062587Sitojun#else 101162587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 1012120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 101362587Sitojun if (icmp6 == NULL) { 101462587Sitojun icmp6stat.icp6s_tooshort++; 1015120856Sume return (-1); 101662587Sitojun } 101762587Sitojun#endif 101878064Sume 1019120891Sume /* 1020120891Sume * retrieve parameters from the inner IPv6 header, and convert 1021120891Sume * them into sockaddr structures. 1022120891Sume * XXX: there is no guarantee that the source or destination 1023120891Sume * addresses of the inner packet are in the same scope as 1024120891Sume * the addresses of the icmp packet. But there is no other 1025120891Sume * way to determine the zone. 1026120891Sume */ 102778064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 1028120891Sume 102978064Sume bzero(&icmp6dst, sizeof(icmp6dst)); 103078064Sume icmp6dst.sin6_len = sizeof(struct sockaddr_in6); 103178064Sume icmp6dst.sin6_family = AF_INET6; 103278064Sume if (finaldst == NULL) 103378064Sume icmp6dst.sin6_addr = eip6->ip6_dst; 103478064Sume else 103578064Sume icmp6dst.sin6_addr = *finaldst; 1036121315Sume if (in6_addr2zoneid(m->m_pkthdr.rcvif, &icmp6dst.sin6_addr, 1037121315Sume &icmp6dst.sin6_scope_id)) 1038121315Sume goto freeit; 103978064Sume if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst, 104078064Sume NULL, NULL)) { 104178064Sume /* should be impossbile */ 104278064Sume nd6log((LOG_DEBUG, 104378064Sume "icmp6_notify_error: in6_embedscope failed\n")); 104478064Sume goto freeit; 104578064Sume } 104678064Sume 104778064Sume /* 104878064Sume * retrieve parameters from the inner IPv6 header, and convert 104978064Sume * them into sockaddr structures. 105078064Sume */ 105178064Sume bzero(&icmp6src, sizeof(icmp6src)); 105278064Sume icmp6src.sin6_len = sizeof(struct sockaddr_in6); 105378064Sume icmp6src.sin6_family = AF_INET6; 105478064Sume icmp6src.sin6_addr = eip6->ip6_src; 1055121315Sume if (in6_addr2zoneid(m->m_pkthdr.rcvif, &icmp6src.sin6_addr, 1056121315Sume &icmp6src.sin6_scope_id)) { 1057121315Sume goto freeit; 1058121315Sume } 105978064Sume if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src, 106078064Sume NULL, NULL)) { 106178064Sume /* should be impossbile */ 106278064Sume nd6log((LOG_DEBUG, 106378064Sume "icmp6_notify_error: in6_embedscope failed\n")); 106478064Sume goto freeit; 106578064Sume } 1066120891Sume icmp6src.sin6_flowinfo = (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); 106778064Sume 106878064Sume if (finaldst == NULL) 106978064Sume finaldst = &eip6->ip6_dst; 107078064Sume ip6cp.ip6c_m = m; 107178064Sume ip6cp.ip6c_icmp6 = icmp6; 107278064Sume ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1); 107378064Sume ip6cp.ip6c_off = eoff; 107478064Sume ip6cp.ip6c_finaldst = finaldst; 107578064Sume ip6cp.ip6c_src = &icmp6src; 107678064Sume ip6cp.ip6c_nxt = nxt; 107778064Sume 107862587Sitojun if (icmp6type == ICMP6_PACKET_TOO_BIG) { 107978064Sume notifymtu = ntohl(icmp6->icmp6_mtu); 108078064Sume ip6cp.ip6c_cmdarg = (void *)¬ifymtu; 108178064Sume icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ 108262587Sitojun } 108362587Sitojun 108453541Sshin ctlfunc = (void (*) __P((int, struct sockaddr *, void *))) 1085120891Sume (inet6sw[ip6_protox[nxt]].pr_ctlinput); 108653541Sshin if (ctlfunc) { 108778064Sume (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, 1088120891Sume &ip6cp); 108953541Sshin } 109053541Sshin } 1091120856Sume return (0); 109253541Sshin 109378064Sume freeit: 109453541Sshin m_freem(m); 1095120856Sume return (-1); 109653541Sshin} 109753541Sshin 109878064Sumevoid 109978064Sumeicmp6_mtudisc_update(ip6cp, validated) 110078064Sume struct ip6ctlparam *ip6cp; 110178064Sume int validated; 110262587Sitojun{ 110378064Sume struct in6_addr *dst = ip6cp->ip6c_finaldst; 110478064Sume struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; 110578064Sume struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ 110662587Sitojun u_int mtu = ntohl(icmp6->icmp6_mtu); 1107122922Sandre struct in_conninfo inc; 110862587Sitojun 1109121472Sume#if 0 1110121472Sume /* 1111121472Sume * RFC2460 section 5, last paragraph. 1112121472Sume * even though minimum link MTU for IPv6 is IPV6_MMTU, 1113121472Sume * we may see ICMPv6 too big with mtu < IPV6_MMTU 1114121472Sume * due to packet translator in the middle. 1115121472Sume * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for 1116121472Sume * special handling. 1117121472Sume */ 1118121472Sume if (mtu < IPV6_MMTU) 1119121472Sume return; 1120121472Sume#endif 1121121472Sume 1122121472Sume /* 1123121472Sume * we reject ICMPv6 too big with abnormally small value. 1124121472Sume * XXX what is the good definition of "abnormally small"? 1125121472Sume */ 1126121472Sume if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) 1127121472Sume return; 1128121472Sume 112978064Sume if (!validated) 113078064Sume return; 113178064Sume 1132122922Sandre bzero(&inc, sizeof(inc)); 1133122922Sandre inc.inc_flags = 1; /* IPv6 */ 1134122922Sandre inc.inc6_faddr = *dst; 113578064Sume /* XXX normally, this won't happen */ 113678064Sume if (IN6_IS_ADDR_LINKLOCAL(dst)) { 1137122922Sandre inc.inc6_faddr.s6_addr16[1] = 113878064Sume htons(m->m_pkthdr.rcvif->if_index); 113978064Sume } 114062587Sitojun 1141126194Sume if (mtu < tcp_maxmtu6(&inc)) { 1142122922Sandre tcp_hc_updatemtu(&inc, mtu); 1143122922Sandre icmp6stat.icp6s_pmtuchg++; 114462587Sitojun } 114562587Sitojun} 114662587Sitojun 114753541Sshin/* 114878064Sume * Process a Node Information Query packet, based on 114978064Sume * draft-ietf-ipngwg-icmp-name-lookups-07. 1150120891Sume * 115162587Sitojun * Spec incompatibilities: 115262587Sitojun * - IPv6 Subject address handling 115362587Sitojun * - IPv4 Subject address handling support missing 115462587Sitojun * - Proxy reply (answer even if it's not for me) 115562587Sitojun * - joins NI group address at in6_ifattach() time only, does not cope 115662587Sitojun * with hostname changes by sethostname(3) 115753541Sshin */ 115862587Sitojun#define hostnamelen strlen(hostname) 115953541Sshinstatic struct mbuf * 116053541Sshinni6_input(m, off) 116153541Sshin struct mbuf *m; 116253541Sshin int off; 116353541Sshin{ 116462587Sitojun struct icmp6_nodeinfo *ni6, *nni6; 116553541Sshin struct mbuf *n = NULL; 116662587Sitojun u_int16_t qtype; 116762587Sitojun int subjlen; 116853541Sshin int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 116953541Sshin struct ni_reply_fqdn *fqdn; 117053541Sshin int addrs; /* for NI_QTYPE_NODEADDR */ 117153541Sshin struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ 1172121630Sume struct sockaddr_in6 sin6_sbj; /* subject address */ 1173121630Sume struct sockaddr_in6 sin6_d; 117462587Sitojun struct ip6_hdr *ip6; 117562587Sitojun int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ 117678064Sume char *subj = NULL; 117778064Sume struct in6_ifaddr *ia6 = NULL; 117853541Sshin 117962587Sitojun ip6 = mtod(m, struct ip6_hdr *); 118062587Sitojun#ifndef PULLDOWN_TEST 118162587Sitojun ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); 118262587Sitojun#else 118362587Sitojun IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); 118462587Sitojun if (ni6 == NULL) { 118562587Sitojun /* m is already reclaimed */ 1186120891Sume return (NULL); 118762587Sitojun } 118862587Sitojun#endif 118953541Sshin 1190121630Sume bzero(&sin6_d, sizeof(sin6_d)); 1191121630Sume sin6_d.sin6_family = AF_INET6; /* not used, actually */ 1192121630Sume sin6_d.sin6_len = sizeof(sin6_d); /* ditto */ 1193121630Sume sin6_d.sin6_addr = ip6->ip6_dst; 1194121630Sume if (in6_addr2zoneid(m->m_pkthdr.rcvif, &ip6->ip6_dst, 1195121630Sume &sin6_d.sin6_scope_id)) { 1196121630Sume goto bad; 1197121630Sume } 1198121630Sume if (in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL)) 1199121630Sume goto bad; /* XXX should not happen */ 1200121630Sume 120162587Sitojun /* 120262587Sitojun * Validate IPv6 destination address. 120362587Sitojun * 120478064Sume * The Responder must discard the Query without further processing 120578064Sume * unless it is one of the Responder's unicast or anycast addresses, or 120678064Sume * a link-local scope multicast address which the Responder has joined. 1207121630Sume * [icmp-name-lookups-08, Section 4.] 120862587Sitojun */ 1209121630Sume if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1210121630Sume if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) 1211121630Sume goto bad; 1212121630Sume /* else it's a link-local multicast, fine */ 1213121630Sume } else { /* unicast or anycast */ 1214121630Sume if ((ia6 = ip6_getdstifaddr(m)) == NULL) 1215121630Sume goto bad; /* XXX impossible */ 1216121630Sume 1217121630Sume if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) && 1218121630Sume !(icmp6_nodeinfo & 4)) { 121978064Sume nd6log((LOG_DEBUG, "ni6_input: ignore node info to " 122078064Sume "a temporary address in %s:%d", 122178064Sume __FILE__, __LINE__)); 122278064Sume goto bad; 122378064Sume } 1224121630Sume } 122562587Sitojun 122678064Sume /* validate query Subject field. */ 122762587Sitojun qtype = ntohs(ni6->ni_qtype); 122862587Sitojun subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); 122962587Sitojun switch (qtype) { 123062587Sitojun case NI_QTYPE_NOOP: 123162587Sitojun case NI_QTYPE_SUPTYPES: 123278064Sume /* 07 draft */ 123378064Sume if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) 123478064Sume break; 123578064Sume /* FALLTHROUGH */ 123662587Sitojun case NI_QTYPE_FQDN: 123762587Sitojun case NI_QTYPE_NODEADDR: 123862587Sitojun switch (ni6->ni_code) { 123962587Sitojun case ICMP6_NI_SUBJ_IPV6: 124062587Sitojun#if ICMP6_NI_SUBJ_IPV6 != 0 124162587Sitojun case 0: 124262587Sitojun#endif 124362587Sitojun /* 124462587Sitojun * backward compatibility - try to accept 03 draft 124562587Sitojun * format, where no Subject is present. 124662587Sitojun */ 124778064Sume if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && 124878064Sume subjlen == 0) { 124962587Sitojun oldfqdn++; 125062587Sitojun break; 125162587Sitojun } 125278064Sume#if ICMP6_NI_SUBJ_IPV6 != 0 125378064Sume if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) 125478064Sume goto bad; 125578064Sume#endif 125662587Sitojun 1257121630Sume if (subjlen != sizeof(struct in6_addr)) 125862587Sitojun goto bad; 125962587Sitojun 126062587Sitojun /* 126162587Sitojun * Validate Subject address. 126262587Sitojun * 126378064Sume * Not sure what exactly "address belongs to the node" 126478064Sume * means in the spec, is it just unicast, or what? 126562587Sitojun * 126662587Sitojun * At this moment we consider Subject address as 126762587Sitojun * "belong to the node" if the Subject address equals 126862587Sitojun * to the IPv6 destination address; validation for 126962587Sitojun * IPv6 destination address should have done enough 127062587Sitojun * check for us. 127162587Sitojun * 127262587Sitojun * We do not do proxy at this moment. 127362587Sitojun */ 127462587Sitojun /* m_pulldown instead of copy? */ 1275121630Sume bzero(&sin6_sbj, sizeof(sin6_sbj)); 1276121630Sume sin6_sbj.sin6_family = AF_INET6; 1277121630Sume sin6_sbj.sin6_len = sizeof(sin6_sbj); 127862587Sitojun m_copydata(m, off + sizeof(struct icmp6_nodeinfo), 1279121630Sume subjlen, (caddr_t)&sin6_sbj.sin6_addr); 1280121315Sume if (in6_addr2zoneid(m->m_pkthdr.rcvif, 1281121630Sume &sin6_sbj.sin6_addr, &sin6_sbj.sin6_scope_id)) { 1282121315Sume goto bad; 1283121315Sume } 1284121630Sume if (in6_embedscope(&sin6_sbj.sin6_addr, &sin6_sbj, 1285121630Sume NULL, NULL)) 1286121630Sume goto bad; /* XXX should not happen */ 1287121630Sume 1288121630Sume subj = (char *)&sin6_sbj; 1289121630Sume if (SA6_ARE_ADDR_EQUAL(&sin6_sbj, &sin6_d)) 129062587Sitojun break; 129178064Sume 129262587Sitojun /* 129362587Sitojun * XXX if we are to allow other cases, we should really 129462587Sitojun * be careful about scope here. 129562587Sitojun * basically, we should disallow queries toward IPv6 1296120891Sume * destination X with subject Y, 1297120891Sume * if scope(X) > scope(Y). 129862587Sitojun * if we allow scope(X) > scope(Y), it will result in 129962587Sitojun * information leakage across scope boundary. 130062587Sitojun */ 130162587Sitojun goto bad; 130262587Sitojun 130362587Sitojun case ICMP6_NI_SUBJ_FQDN: 130462587Sitojun /* 130562587Sitojun * Validate Subject name with gethostname(3). 130662587Sitojun * 130762587Sitojun * The behavior may need some debate, since: 130862587Sitojun * - we are not sure if the node has FQDN as 130962587Sitojun * hostname (returned by gethostname(3)). 131062587Sitojun * - the code does wildcard match for truncated names. 131162587Sitojun * however, we are not sure if we want to perform 131262587Sitojun * wildcard match, if gethostname(3) side has 131362587Sitojun * truncated hostname. 131462587Sitojun */ 131562587Sitojun n = ni6_nametodns(hostname, hostnamelen, 0); 131662587Sitojun if (!n || n->m_next || n->m_len == 0) 131762587Sitojun goto bad; 131862587Sitojun IP6_EXTHDR_GET(subj, char *, m, 131962587Sitojun off + sizeof(struct icmp6_nodeinfo), subjlen); 132062587Sitojun if (subj == NULL) 132162587Sitojun goto bad; 132262587Sitojun if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 1323120891Sume n->m_len)) { 132462587Sitojun goto bad; 132562587Sitojun } 132662587Sitojun m_freem(n); 132762587Sitojun n = NULL; 132862587Sitojun break; 132962587Sitojun 133078064Sume case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 133162587Sitojun default: 133262587Sitojun goto bad; 133362587Sitojun } 133462587Sitojun break; 133578064Sume } 133662587Sitojun 133778064Sume /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 133878064Sume switch (qtype) { 133978064Sume case NI_QTYPE_FQDN: 134078064Sume if ((icmp6_nodeinfo & 1) == 0) 134178064Sume goto bad; 134278064Sume break; 134378064Sume case NI_QTYPE_NODEADDR: 134478064Sume if ((icmp6_nodeinfo & 2) == 0) 134578064Sume goto bad; 134678064Sume break; 134778064Sume } 134878064Sume 134978064Sume /* guess reply length */ 135078064Sume switch (qtype) { 135178064Sume case NI_QTYPE_NOOP: 135278064Sume break; /* no reply data */ 135378064Sume case NI_QTYPE_SUPTYPES: 135478064Sume replylen += sizeof(u_int32_t); 135578064Sume break; 135678064Sume case NI_QTYPE_FQDN: 135778064Sume /* XXX will append an mbuf */ 135878064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 135978064Sume break; 136078064Sume case NI_QTYPE_NODEADDR: 136178064Sume addrs = ni6_addrs(ni6, m, &ifp, subj); 136278064Sume if ((replylen += addrs * (sizeof(struct in6_addr) + 1363120891Sume sizeof(u_int32_t))) > MCLBYTES) 136478064Sume replylen = MCLBYTES; /* XXX: will truncate pkt later */ 136578064Sume break; 136662587Sitojun default: 136778064Sume /* 136878064Sume * XXX: We must return a reply with the ICMP6 code 1369120891Sume * `unknown Qtype' in this case. However we regard the case 137078064Sume * as an FQDN query for backward compatibility. 137178064Sume * Older versions set a random value to this field, 137278064Sume * so it rarely varies in the defined qtypes. 137378064Sume * But the mechanism is not reliable... 137478064Sume * maybe we should obsolete older versions. 137578064Sume */ 137678064Sume qtype = NI_QTYPE_FQDN; 137778064Sume /* XXX will append an mbuf */ 137878064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 137978064Sume oldfqdn++; 138078064Sume break; 138162587Sitojun } 138262587Sitojun 138378064Sume /* allocate an mbuf to reply. */ 1384111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 138562587Sitojun if (n == NULL) { 138662587Sitojun m_freem(m); 1387120856Sume return (NULL); 138862587Sitojun } 1389108466Ssam M_MOVE_PKTHDR(n, m); /* just for recvif */ 139053541Sshin if (replylen > MHLEN) { 139162587Sitojun if (replylen > MCLBYTES) { 139278064Sume /* 139378064Sume * XXX: should we try to allocate more? But MCLBYTES 139478064Sume * is probably much larger than IPV6_MMTU... 139578064Sume */ 139653541Sshin goto bad; 139762587Sitojun } 1398111119Simp MCLGET(n, M_DONTWAIT); 139953541Sshin if ((n->m_flags & M_EXT) == 0) { 140053541Sshin goto bad; 140153541Sshin } 140253541Sshin } 140353541Sshin n->m_pkthdr.len = n->m_len = replylen; 140453541Sshin 140553541Sshin /* copy mbuf header and IPv6 + Node Information base headers */ 140653541Sshin bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); 140753541Sshin nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 140862587Sitojun bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); 140953541Sshin 141053541Sshin /* qtype dependent procedure */ 141153541Sshin switch (qtype) { 141262587Sitojun case NI_QTYPE_NOOP: 141378064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 141462587Sitojun nni6->ni_flags = 0; 141562587Sitojun break; 141662587Sitojun case NI_QTYPE_SUPTYPES: 141778064Sume { 141878064Sume u_int32_t v; 141978064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 142078064Sume nni6->ni_flags = htons(0x0000); /* raw bitmap */ 142178064Sume /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 142278064Sume v = (u_int32_t)htonl(0x0000000f); 142378064Sume bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 142462587Sitojun break; 142578064Sume } 142662587Sitojun case NI_QTYPE_FQDN: 142778064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 142862587Sitojun fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + 1429120891Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo)); 143062587Sitojun nni6->ni_flags = 0; /* XXX: meaningless TTL */ 143162587Sitojun fqdn->ni_fqdn_ttl = 0; /* ditto. */ 143262587Sitojun /* 143362587Sitojun * XXX do we really have FQDN in variable "hostname"? 143462587Sitojun */ 143562587Sitojun n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn); 143662587Sitojun if (n->m_next == NULL) 143762587Sitojun goto bad; 143862587Sitojun /* XXX we assume that n->m_next is not a chain */ 143962587Sitojun if (n->m_next->m_next != NULL) 144062587Sitojun goto bad; 144162587Sitojun n->m_pkthdr.len += n->m_next->m_len; 144262587Sitojun break; 144362587Sitojun case NI_QTYPE_NODEADDR: 144462587Sitojun { 144562587Sitojun int lenlim, copied; 144653541Sshin 144778064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 144878064Sume n->m_pkthdr.len = n->m_len = 144978064Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 145078064Sume lenlim = M_TRAILINGSPACE(n); 145162587Sitojun copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 145262587Sitojun /* XXX: reset mbuf length */ 145362587Sitojun n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 1454120891Sume sizeof(struct icmp6_nodeinfo) + copied; 145562587Sitojun break; 145653541Sshin } 145762587Sitojun default: 145862587Sitojun break; /* XXX impossible! */ 145962587Sitojun } 146053541Sshin 146153541Sshin nni6->ni_type = ICMP6_NI_REPLY; 146262587Sitojun m_freem(m); 1463120856Sume return (n); 146453541Sshin 146553541Sshin bad: 146662587Sitojun m_freem(m); 146753541Sshin if (n) 146853541Sshin m_freem(n); 1469120856Sume return (NULL); 147053541Sshin} 147153541Sshin#undef hostnamelen 147253541Sshin 147353541Sshin/* 147462587Sitojun * make a mbuf with DNS-encoded string. no compression support. 147562587Sitojun * 147662587Sitojun * XXX names with less than 2 dots (like "foo" or "foo.section") will be 147762587Sitojun * treated as truncated name (two \0 at the end). this is a wild guess. 147862587Sitojun */ 147962587Sitojunstatic struct mbuf * 148062587Sitojunni6_nametodns(name, namelen, old) 148162587Sitojun const char *name; 148262587Sitojun int namelen; 148362587Sitojun int old; /* return pascal string if non-zero */ 148462587Sitojun{ 148562587Sitojun struct mbuf *m; 148662587Sitojun char *cp, *ep; 148762587Sitojun const char *p, *q; 148862587Sitojun int i, len, nterm; 148962587Sitojun 149062587Sitojun if (old) 149162587Sitojun len = namelen + 1; 149262587Sitojun else 149362587Sitojun len = MCLBYTES; 149462587Sitojun 149562587Sitojun /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ 1496111119Simp MGET(m, M_DONTWAIT, MT_DATA); 149762587Sitojun if (m && len > MLEN) { 1498111119Simp MCLGET(m, M_DONTWAIT); 149962587Sitojun if ((m->m_flags & M_EXT) == 0) 150062587Sitojun goto fail; 150162587Sitojun } 150262587Sitojun if (!m) 150362587Sitojun goto fail; 150462587Sitojun m->m_next = NULL; 150562587Sitojun 150662587Sitojun if (old) { 150762587Sitojun m->m_len = len; 150862587Sitojun *mtod(m, char *) = namelen; 150962587Sitojun bcopy(name, mtod(m, char *) + 1, namelen); 151062587Sitojun return m; 151162587Sitojun } else { 151262587Sitojun m->m_len = 0; 151362587Sitojun cp = mtod(m, char *); 151462587Sitojun ep = mtod(m, char *) + M_TRAILINGSPACE(m); 151562587Sitojun 151662587Sitojun /* if not certain about my name, return empty buffer */ 151762587Sitojun if (namelen == 0) 151862587Sitojun return m; 151962587Sitojun 152062587Sitojun /* 152162587Sitojun * guess if it looks like shortened hostname, or FQDN. 152262587Sitojun * shortened hostname needs two trailing "\0". 152362587Sitojun */ 152462587Sitojun i = 0; 152562587Sitojun for (p = name; p < name + namelen; p++) { 152662587Sitojun if (*p && *p == '.') 152762587Sitojun i++; 152862587Sitojun } 152962587Sitojun if (i < 2) 153062587Sitojun nterm = 2; 153162587Sitojun else 153262587Sitojun nterm = 1; 153362587Sitojun 153462587Sitojun p = name; 153562587Sitojun while (cp < ep && p < name + namelen) { 153662587Sitojun i = 0; 153762587Sitojun for (q = p; q < name + namelen && *q && *q != '.'; q++) 153862587Sitojun i++; 153962587Sitojun /* result does not fit into mbuf */ 154062587Sitojun if (cp + i + 1 >= ep) 154162587Sitojun goto fail; 154278704Sume /* 154378704Sume * DNS label length restriction, RFC1035 page 8. 154478704Sume * "i == 0" case is included here to avoid returning 154578704Sume * 0-length label on "foo..bar". 154678704Sume */ 154778704Sume if (i <= 0 || i >= 64) 154862587Sitojun goto fail; 154962587Sitojun *cp++ = i; 155062587Sitojun bcopy(p, cp, i); 155162587Sitojun cp += i; 155262587Sitojun p = q; 155362587Sitojun if (p < name + namelen && *p == '.') 155462587Sitojun p++; 155562587Sitojun } 155662587Sitojun /* termination */ 155762587Sitojun if (cp + nterm >= ep) 155862587Sitojun goto fail; 155962587Sitojun while (nterm-- > 0) 156062587Sitojun *cp++ = '\0'; 156162587Sitojun m->m_len = cp - mtod(m, char *); 156262587Sitojun return m; 156362587Sitojun } 156462587Sitojun 156562587Sitojun panic("should not reach here"); 156695023Ssuz /* NOTREACHED */ 156762587Sitojun 156862587Sitojun fail: 156962587Sitojun if (m) 157062587Sitojun m_freem(m); 157162587Sitojun return NULL; 157262587Sitojun} 157362587Sitojun 157462587Sitojun/* 157562587Sitojun * check if two DNS-encoded string matches. takes care of truncated 157662587Sitojun * form (with \0\0 at the end). no compression support. 157778064Sume * XXX upper/lowercase match (see RFC2065) 157862587Sitojun */ 157962587Sitojunstatic int 158062587Sitojunni6_dnsmatch(a, alen, b, blen) 158162587Sitojun const char *a; 158262587Sitojun int alen; 158362587Sitojun const char *b; 158462587Sitojun int blen; 158562587Sitojun{ 158662587Sitojun const char *a0, *b0; 158762587Sitojun int l; 158862587Sitojun 158962587Sitojun /* simplest case - need validation? */ 159062587Sitojun if (alen == blen && bcmp(a, b, alen) == 0) 159162587Sitojun return 1; 159262587Sitojun 159362587Sitojun a0 = a; 159462587Sitojun b0 = b; 159562587Sitojun 159662587Sitojun /* termination is mandatory */ 159762587Sitojun if (alen < 2 || blen < 2) 159862587Sitojun return 0; 159962587Sitojun if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 160062587Sitojun return 0; 160162587Sitojun alen--; 160262587Sitojun blen--; 160362587Sitojun 160462587Sitojun while (a - a0 < alen && b - b0 < blen) { 160562587Sitojun if (a - a0 + 1 > alen || b - b0 + 1 > blen) 160662587Sitojun return 0; 160762587Sitojun 160862587Sitojun if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 160962587Sitojun return 0; 161062587Sitojun /* we don't support compression yet */ 161162587Sitojun if (a[0] >= 64 || b[0] >= 64) 161262587Sitojun return 0; 161362587Sitojun 161462587Sitojun /* truncated case */ 161562587Sitojun if (a[0] == 0 && a - a0 == alen - 1) 161662587Sitojun return 1; 161762587Sitojun if (b[0] == 0 && b - b0 == blen - 1) 161862587Sitojun return 1; 161962587Sitojun if (a[0] == 0 || b[0] == 0) 162062587Sitojun return 0; 162162587Sitojun 162262587Sitojun if (a[0] != b[0]) 162362587Sitojun return 0; 162462587Sitojun l = a[0]; 162562587Sitojun if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 162662587Sitojun return 0; 162762587Sitojun if (bcmp(a + 1, b + 1, l) != 0) 162862587Sitojun return 0; 162962587Sitojun 163062587Sitojun a += 1 + l; 163162587Sitojun b += 1 + l; 163262587Sitojun } 163362587Sitojun 163462587Sitojun if (a - a0 == alen && b - b0 == blen) 163562587Sitojun return 1; 163662587Sitojun else 163762587Sitojun return 0; 163862587Sitojun} 163962587Sitojun 164062587Sitojun/* 164153541Sshin * calculate the number of addresses to be returned in the node info reply. 164253541Sshin */ 164353541Sshinstatic int 164478064Sumeni6_addrs(ni6, m, ifpp, subj) 164553541Sshin struct icmp6_nodeinfo *ni6; 164653541Sshin struct mbuf *m; 164753541Sshin struct ifnet **ifpp; 164878064Sume char *subj; 164953541Sshin{ 165078064Sume struct ifnet *ifp; 165178064Sume struct in6_ifaddr *ifa6; 165278064Sume struct ifaddr *ifa; 165378064Sume struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */ 165453541Sshin int addrs = 0, addrsofif, iffound = 0; 165578064Sume int niflags = ni6->ni_flags; 165653541Sshin 165778064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 165878064Sume switch (ni6->ni_code) { 165978064Sume case ICMP6_NI_SUBJ_IPV6: 166078064Sume if (subj == NULL) /* must be impossible... */ 1661120856Sume return (0); 166278064Sume subj_ip6 = (struct sockaddr_in6 *)subj; 166378064Sume break; 166478064Sume default: 166578064Sume /* 166678064Sume * XXX: we only support IPv6 subject address for 166778064Sume * this Qtype. 166878064Sume */ 1669120856Sume return (0); 167078064Sume } 167178064Sume } 167278064Sume 1673108172Shsu IFNET_RLOCK(); 1674120891Sume for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 167553541Sshin addrsofif = 0; 1676120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 167753541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 167853541Sshin continue; 167953541Sshin ifa6 = (struct in6_ifaddr *)ifa; 168053541Sshin 168178064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 168278064Sume IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr, 1683120891Sume &ifa6->ia_addr.sin6_addr)) 168453541Sshin iffound = 1; 168553541Sshin 168662587Sitojun /* 168762587Sitojun * IPv4-mapped addresses can only be returned by a 168862587Sitojun * Node Information proxy, since they represent 168962587Sitojun * addresses of IPv4-only nodes, which perforce do 169062587Sitojun * not implement this protocol. 169178064Sume * [icmp-name-lookups-07, Section 5.4] 169262587Sitojun * So we don't support NI_NODEADDR_FLAG_COMPAT in 169362587Sitojun * this function at this moment. 169462587Sitojun */ 169562587Sitojun 169653541Sshin /* What do we have to do about ::1? */ 169778064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 169878064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 169978064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 170078064Sume continue; 170153541Sshin break; 170278064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 170378064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 170478064Sume continue; 170553541Sshin break; 170678064Sume case IPV6_ADDR_SCOPE_GLOBAL: 170778064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 170878064Sume continue; 170978064Sume break; 171078064Sume default: 171178064Sume continue; 171253541Sshin } 171378064Sume 171478064Sume /* 171578064Sume * check if anycast is okay. 171695023Ssuz * XXX: just experimental. not in the spec. 171778064Sume */ 171878064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 171978064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 172078064Sume continue; /* we need only unicast addresses */ 172178064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 172278064Sume (icmp6_nodeinfo & 4) == 0) { 172378064Sume continue; 172478064Sume } 172578064Sume addrsofif++; /* count the address */ 172653541Sshin } 172753541Sshin if (iffound) { 172853541Sshin *ifpp = ifp; 1729108172Shsu IFNET_RUNLOCK(); 1730120856Sume return (addrsofif); 173153541Sshin } 173253541Sshin 173353541Sshin addrs += addrsofif; 173453541Sshin } 1735108172Shsu IFNET_RUNLOCK(); 173653541Sshin 1737120856Sume return (addrs); 173853541Sshin} 173953541Sshin 174053541Sshinstatic int 174153541Sshinni6_store_addrs(ni6, nni6, ifp0, resid) 174253541Sshin struct icmp6_nodeinfo *ni6, *nni6; 174353541Sshin struct ifnet *ifp0; 174453541Sshin int resid; 174553541Sshin{ 174678064Sume struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet); 174778064Sume struct in6_ifaddr *ifa6; 174878064Sume struct ifaddr *ifa; 174978064Sume struct ifnet *ifp_dep = NULL; 175078064Sume int copied = 0, allow_deprecated = 0; 175153541Sshin u_char *cp = (u_char *)(nni6 + 1); 175278064Sume int niflags = ni6->ni_flags; 175378064Sume u_int32_t ltime; 175453541Sshin 175578064Sume if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 1756120856Sume return (0); /* needless to copy */ 1757120891Sume 1758108172Shsu IFNET_RLOCK(); 175978064Sume again: 1760120892Sume 1761120891Sume for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 176262587Sitojun for (ifa = ifp->if_addrlist.tqh_first; ifa; 1763120891Sume ifa = ifa->ifa_list.tqe_next) { 176453541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 176553541Sshin continue; 176653541Sshin ifa6 = (struct in6_ifaddr *)ifa; 176753541Sshin 176878064Sume if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && 176978064Sume allow_deprecated == 0) { 177078064Sume /* 177178064Sume * prefererred address should be put before 177278064Sume * deprecated addresses. 177378064Sume */ 177478064Sume 177578064Sume /* record the interface for later search */ 177678064Sume if (ifp_dep == NULL) 177778064Sume ifp_dep = ifp; 177878064Sume 177978064Sume continue; 1780120891Sume } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && 1781120891Sume allow_deprecated != 0) 178278064Sume continue; /* we now collect deprecated addrs */ 178353541Sshin 178453541Sshin /* What do we have to do about ::1? */ 178578064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 178678064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 178778064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 178878064Sume continue; 178953541Sshin break; 179078064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 179178064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 179278064Sume continue; 179353541Sshin break; 179478064Sume case IPV6_ADDR_SCOPE_GLOBAL: 179578064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 179678064Sume continue; 179778064Sume break; 179878064Sume default: 179978064Sume continue; 180053541Sshin } 180153541Sshin 180278064Sume /* 180378064Sume * check if anycast is okay. 1804120891Sume * XXX: just experimental. not in the spec. 180578064Sume */ 180678064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 180778064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 180878064Sume continue; 180978064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 181078064Sume (icmp6_nodeinfo & 4) == 0) { 181178064Sume continue; 181253541Sshin } 181378064Sume 181478064Sume /* now we can copy the address */ 181578064Sume if (resid < sizeof(struct in6_addr) + 181678064Sume sizeof(u_int32_t)) { 181778064Sume /* 181878064Sume * We give up much more copy. 181978064Sume * Set the truncate flag and return. 182078064Sume */ 1821120891Sume nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE; 1822108172Shsu IFNET_RUNLOCK(); 1823120856Sume return (copied); 182478064Sume } 182578064Sume 182678064Sume /* 182778064Sume * Set the TTL of the address. 182878064Sume * The TTL value should be one of the following 182978064Sume * according to the specification: 183078064Sume * 183178064Sume * 1. The remaining lifetime of a DHCP lease on the 183278064Sume * address, or 183378064Sume * 2. The remaining Valid Lifetime of a prefix from 183478064Sume * which the address was derived through Stateless 183578064Sume * Autoconfiguration. 183678064Sume * 183778064Sume * Note that we currently do not support stateful 183878064Sume * address configuration by DHCPv6, so the former 183978064Sume * case can't happen. 184078064Sume */ 184178064Sume if (ifa6->ia6_lifetime.ia6t_expire == 0) 184278064Sume ltime = ND6_INFINITE_LIFETIME; 184378064Sume else { 184478064Sume if (ifa6->ia6_lifetime.ia6t_expire > 184578064Sume time_second) 184678064Sume ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second); 184778064Sume else 184878064Sume ltime = 0; 184978064Sume } 1850120891Sume 185178064Sume bcopy(<ime, cp, sizeof(u_int32_t)); 185278064Sume cp += sizeof(u_int32_t); 185378064Sume 185478064Sume /* copy the address itself */ 185578064Sume bcopy(&ifa6->ia_addr.sin6_addr, cp, 1856120891Sume sizeof(struct in6_addr)); 1857121315Sume in6_clearscope((struct in6_addr *)cp); /* XXX */ 185878064Sume cp += sizeof(struct in6_addr); 1859120891Sume 186078064Sume resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1861120891Sume copied += (sizeof(struct in6_addr) + sizeof(u_int32_t)); 186253541Sshin } 186353541Sshin if (ifp0) /* we need search only on the specified IF */ 186453541Sshin break; 186553541Sshin } 186653541Sshin 186778064Sume if (allow_deprecated == 0 && ifp_dep != NULL) { 186878064Sume ifp = ifp_dep; 186978064Sume allow_deprecated = 1; 187078064Sume 187178064Sume goto again; 187278064Sume } 187378064Sume 1874108172Shsu IFNET_RUNLOCK(); 1875108172Shsu 1876120856Sume return (copied); 187753541Sshin} 187853541Sshin 187953541Sshin/* 188053541Sshin * XXX almost dup'ed code with rip6_input. 188153541Sshin */ 188253541Sshinstatic int 188353541Sshinicmp6_rip6_input(mp, off) 188453541Sshin struct mbuf **mp; 188553541Sshin int off; 188653541Sshin{ 188753541Sshin struct mbuf *m = *mp; 188878064Sume struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 188978064Sume struct in6pcb *in6p; 189053541Sshin struct in6pcb *last = NULL; 1891121315Sume struct sockaddr_in6 fromsa; 189253541Sshin struct icmp6_hdr *icmp6; 189353541Sshin struct mbuf *opts = NULL; 189453541Sshin 189562587Sitojun#ifndef PULLDOWN_TEST 189653541Sshin /* this is assumed to be safe. */ 189753541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 189862587Sitojun#else 189962587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 190062587Sitojun if (icmp6 == NULL) { 190162587Sitojun /* m is already reclaimed */ 1902120891Sume return (IPPROTO_DONE); 190362587Sitojun } 190462587Sitojun#endif 190553541Sshin 1906121315Sume bzero(&fromsa, sizeof(fromsa)); 1907121315Sume fromsa.sin6_len = sizeof(struct sockaddr_in6); 1908121315Sume fromsa.sin6_family = AF_INET6; 190962587Sitojun /* KAME hack: recover scopeid */ 1910121315Sume (void)in6_recoverscope(&fromsa, &ip6->ip6_src, m->m_pkthdr.rcvif); 191153541Sshin 1912120891Sume LIST_FOREACH(in6p, &ripcb, inp_list) { 191378064Sume if ((in6p->inp_vflag & INP_IPV6) == 0) 191453541Sshin continue; 191553541Sshin if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6) 191653541Sshin continue; 191753541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 191853541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 191953541Sshin continue; 192053541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 192153541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 192253541Sshin continue; 192353541Sshin if (in6p->in6p_icmp6filt 192453541Sshin && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, 192553541Sshin in6p->in6p_icmp6filt)) 192653541Sshin continue; 192753541Sshin if (last) { 1928121809Sume struct mbuf *n = NULL; 1929121809Sume 1930121809Sume /* 1931121809Sume * Recent network drivers tend to allocate a single 1932121809Sume * mbuf cluster, rather than to make a couple of 1933121809Sume * mbufs without clusters. Also, since the IPv6 code 1934121809Sume * path tries to avoid m_pullup(), it is highly 1935121809Sume * probable that we still have an mbuf cluster here 1936121809Sume * even though the necessary length can be stored in an 1937121809Sume * mbuf's internal buffer. 1938121809Sume * Meanwhile, the default size of the receive socket 1939121809Sume * buffer for raw sockets is not so large. This means 1940121809Sume * the possibility of packet loss is relatively higher 1941121809Sume * than before. To avoid this scenario, we copy the 1942121809Sume * received data to a separate mbuf that does not use 1943121809Sume * a cluster, if possible. 1944121809Sume * XXX: it is better to copy the data after stripping 1945121809Sume * intermediate headers. 1946121809Sume */ 1947121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 1948121809Sume m->m_len <= MHLEN) { 1949121809Sume MGET(n, M_DONTWAIT, m->m_type); 1950121809Sume if (n != NULL) { 1951121809Sume m_dup_pkthdr(n, m, M_NOWAIT); 1952121809Sume bcopy(m->m_data, n->m_data, m->m_len); 1953121809Sume n->m_len = m->m_len; 1954121809Sume } 1955121809Sume } 1956121809Sume if (n != NULL || 1957121809Sume (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 195853541Sshin if (last->in6p_flags & IN6P_CONTROLOPTS) 1959121674Sume ip6_savecontrol(last, n, &opts); 196053541Sshin /* strip intermediate headers */ 196153541Sshin m_adj(n, off); 196253541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 1963121315Sume (struct sockaddr *)&fromsa, n, opts) 1964120891Sume == 0) { 196553541Sshin /* should notify about lost packet */ 196653541Sshin m_freem(n); 196778064Sume if (opts) { 196853541Sshin m_freem(opts); 196978064Sume } 197097658Stanimura } else 197153541Sshin sorwakeup(last->in6p_socket); 197253541Sshin opts = NULL; 197353541Sshin } 197453541Sshin } 197553541Sshin last = in6p; 197653541Sshin } 197753541Sshin if (last) { 197853541Sshin if (last->in6p_flags & IN6P_CONTROLOPTS) 1979121674Sume ip6_savecontrol(last, m, &opts); 198053541Sshin /* strip intermediate headers */ 198153541Sshin m_adj(m, off); 1982121809Sume 1983121809Sume /* avoid using mbuf clusters if possible (see above) */ 1984121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 1985121809Sume m->m_len <= MHLEN) { 1986121809Sume struct mbuf *n; 1987121809Sume 1988121809Sume MGET(n, M_DONTWAIT, m->m_type); 1989121809Sume if (n != NULL) { 1990121809Sume m_dup_pkthdr(n, m, M_NOWAIT); 1991121809Sume bcopy(m->m_data, n->m_data, m->m_len); 1992121809Sume n->m_len = m->m_len; 1993121809Sume 1994121809Sume m_freem(m); 1995121809Sume m = n; 1996121809Sume } 1997121809Sume } 199853541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 1999121315Sume (struct sockaddr *)&fromsa, m, opts) == 0) { 200053541Sshin m_freem(m); 200153541Sshin if (opts) 200253541Sshin m_freem(opts); 200397658Stanimura } else 200453541Sshin sorwakeup(last->in6p_socket); 200553541Sshin } else { 200653541Sshin m_freem(m); 200753541Sshin ip6stat.ip6s_delivered--; 200853541Sshin } 200953541Sshin return IPPROTO_DONE; 201053541Sshin} 201153541Sshin 201253541Sshin/* 201353541Sshin * Reflect the ip6 packet back to the source. 201462587Sitojun * OFF points to the icmp6 header, counted from the top of the mbuf. 201553541Sshin */ 201653541Sshinvoid 201753541Sshinicmp6_reflect(m, off) 201853541Sshin struct mbuf *m; 201953541Sshin size_t off; 202053541Sshin{ 202162587Sitojun struct ip6_hdr *ip6; 202253541Sshin struct icmp6_hdr *icmp6; 202353541Sshin struct in6_ifaddr *ia; 202453541Sshin struct in6_addr t, *src = 0; 202562587Sitojun int plen; 202653541Sshin int type, code; 202753541Sshin struct ifnet *outif = NULL; 202878064Sume struct sockaddr_in6 sa6_src, sa6_dst; 202953541Sshin#ifdef COMPAT_RFC1885 203053541Sshin int mtu = IPV6_MMTU; 203153541Sshin struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst; 203253541Sshin#endif 203353541Sshin 203462587Sitojun /* too short to reflect */ 203562587Sitojun if (off < sizeof(struct ip6_hdr)) { 203678064Sume nd6log((LOG_DEBUG, 203778064Sume "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 203878064Sume (u_long)off, (u_long)sizeof(struct ip6_hdr), 203978064Sume __FILE__, __LINE__)); 204062587Sitojun goto bad; 204162587Sitojun } 204262587Sitojun 204353541Sshin /* 204453541Sshin * If there are extra headers between IPv6 and ICMPv6, strip 204553541Sshin * off that header first. 204653541Sshin */ 204762587Sitojun#ifdef DIAGNOSTIC 204862587Sitojun if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) 204962587Sitojun panic("assumption failed in icmp6_reflect"); 205062587Sitojun#endif 205162587Sitojun if (off > sizeof(struct ip6_hdr)) { 205262587Sitojun size_t l; 205362587Sitojun struct ip6_hdr nip6; 205453541Sshin 205562587Sitojun l = off - sizeof(struct ip6_hdr); 205662587Sitojun m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); 205762587Sitojun m_adj(m, l); 205862587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 205962587Sitojun if (m->m_len < l) { 206062587Sitojun if ((m = m_pullup(m, l)) == NULL) 206162587Sitojun return; 206253541Sshin } 206362587Sitojun bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); 206462587Sitojun } else /* off == sizeof(struct ip6_hdr) */ { 206562587Sitojun size_t l; 206662587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 206762587Sitojun if (m->m_len < l) { 206862587Sitojun if ((m = m_pullup(m, l)) == NULL) 206962587Sitojun return; 207053541Sshin } 207153541Sshin } 207262587Sitojun plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 207362587Sitojun ip6 = mtod(m, struct ip6_hdr *); 207462587Sitojun ip6->ip6_nxt = IPPROTO_ICMPV6; 207553541Sshin icmp6 = (struct icmp6_hdr *)(ip6 + 1); 207653541Sshin type = icmp6->icmp6_type; /* keep type for statistics */ 207753541Sshin code = icmp6->icmp6_code; /* ditto. */ 207853541Sshin 207953541Sshin t = ip6->ip6_dst; 208053541Sshin /* 208153541Sshin * ip6_input() drops a packet if its src is multicast. 208253541Sshin * So, the src is never multicast. 208353541Sshin */ 208453541Sshin ip6->ip6_dst = ip6->ip6_src; 208553541Sshin 208678064Sume /* 208778064Sume * XXX: make sure to embed scope zone information, using 208878064Sume * already embedded IDs or the received interface (if any). 208978064Sume * Note that rcvif may be NULL. 209078064Sume * TODO: scoped routing case (XXX). 209178064Sume */ 209278064Sume bzero(&sa6_src, sizeof(sa6_src)); 209378064Sume sa6_src.sin6_family = AF_INET6; 209478064Sume sa6_src.sin6_len = sizeof(sa6_src); 209578064Sume sa6_src.sin6_addr = ip6->ip6_dst; 209678064Sume in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif); 209778064Sume in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL); 209878064Sume bzero(&sa6_dst, sizeof(sa6_dst)); 209978064Sume sa6_dst.sin6_family = AF_INET6; 210078064Sume sa6_dst.sin6_len = sizeof(sa6_dst); 210178064Sume sa6_dst.sin6_addr = t; 210278064Sume in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif); 210378064Sume in6_embedscope(&t, &sa6_dst, NULL, NULL); 210453541Sshin 210553541Sshin#ifdef COMPAT_RFC1885 210653541Sshin /* 210753541Sshin * xxx guess MTU 210853541Sshin * RFC 1885 requires that echo reply should be truncated if it 210953541Sshin * does not fit in with (return) path MTU, but the description was 211053541Sshin * removed in the new spec. 211153541Sshin */ 211253541Sshin if (icmp6_reflect_rt.ro_rt == 0 || 211353541Sshin ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) { 211453541Sshin if (icmp6_reflect_rt.ro_rt) { 211553541Sshin RTFREE(icmp6_reflect_rt.ro_rt); 211653541Sshin icmp6_reflect_rt.ro_rt = 0; 211753541Sshin } 211853541Sshin bzero(sin6, sizeof(*sin6)); 211953541Sshin sin6->sin6_family = PF_INET6; 212053541Sshin sin6->sin6_len = sizeof(struct sockaddr_in6); 212153541Sshin sin6->sin6_addr = ip6->ip6_dst; 212253541Sshin 2123122921Sandre rtalloc((struct route *)&icmp6_reflect_rt.ro_rt); 212453541Sshin } 212553541Sshin 212653541Sshin if (icmp6_reflect_rt.ro_rt == 0) 212753541Sshin goto bad; 212853541Sshin 212953541Sshin if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST) 213053541Sshin && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu) 213153541Sshin mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu; 213253541Sshin 213353541Sshin if (mtu < m->m_pkthdr.len) { 213453541Sshin plen -= (m->m_pkthdr.len - mtu); 213553541Sshin m_adj(m, mtu - m->m_pkthdr.len); 213653541Sshin } 213753541Sshin#endif 213853541Sshin /* 2139120891Sume * If the incoming packet was addressed directly to us (i.e. unicast), 214053541Sshin * use dst as the src for the reply. 2141120891Sume * The IN6_IFF_NOTREADY case should be VERY rare, but is possible 214262587Sitojun * (for example) when we encounter an error while forwarding procedure 214362587Sitojun * destined to a duplicated address of ours. 214453541Sshin */ 214553541Sshin for (ia = in6_ifaddr; ia; ia = ia->ia_next) 214653541Sshin if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) && 214762587Sitojun (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) { 214853541Sshin src = &t; 214953541Sshin break; 215053541Sshin } 215153541Sshin if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) { 215253541Sshin /* 215353541Sshin * This is the case if the dst is our link-local address 215495023Ssuz * and the sender is also ourselves. 215553541Sshin */ 215653541Sshin src = &t; 215753541Sshin } 215853541Sshin 215978064Sume if (src == 0) { 216078064Sume int e; 216178064Sume struct route_in6 ro; 216278064Sume 216353541Sshin /* 216462587Sitojun * This case matches to multicasts, our anycast, or unicasts 216595023Ssuz * that we do not own. Select a source address based on the 216678064Sume * source address of the erroneous packet. 216753541Sshin */ 216878064Sume bzero(&ro, sizeof(ro)); 216978064Sume src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e); 217078064Sume if (ro.ro_rt) 217178064Sume RTFREE(ro.ro_rt); /* XXX: we could use this */ 217278064Sume if (src == NULL) { 217378064Sume nd6log((LOG_DEBUG, 217478064Sume "icmp6_reflect: source can't be determined: " 217578064Sume "dst=%s, error=%d\n", 217678064Sume ip6_sprintf(&sa6_src.sin6_addr), e)); 217778064Sume goto bad; 217878064Sume } 217978064Sume } 218053541Sshin 218153541Sshin ip6->ip6_src = *src; 218253541Sshin 218353541Sshin ip6->ip6_flow = 0; 218462587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 218562587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 218653541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 2187121472Sume if (outif) 2188121472Sume ip6->ip6_hlim = ND_IFINFO(outif)->chlim; 2189121472Sume else if (m->m_pkthdr.rcvif) { 219053541Sshin /* XXX: This may not be the outgoing interface */ 2191121161Sume ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim; 219278064Sume } else 219378064Sume ip6->ip6_hlim = ip6_defhlim; 219453541Sshin 219553541Sshin icmp6->icmp6_cksum = 0; 219653541Sshin icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2197120891Sume sizeof(struct ip6_hdr), plen); 219853541Sshin 219953541Sshin /* 220078064Sume * XXX option handling 220153541Sshin */ 220253541Sshin 220353541Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 220453541Sshin 220553541Sshin#ifdef COMPAT_RFC1885 2206105194Ssam ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL); 220753541Sshin#else 2208105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 220953541Sshin#endif 221053541Sshin if (outif) 221153541Sshin icmp6_ifoutstat_inc(outif, type, code); 221253541Sshin 221353541Sshin return; 221453541Sshin 221553541Sshin bad: 221653541Sshin m_freem(m); 221753541Sshin return; 221853541Sshin} 221953541Sshin 222053541Sshinvoid 222153541Sshinicmp6_fasttimo() 222253541Sshin{ 222362587Sitojun 222453541Sshin mld6_fasttimeo(); 222553541Sshin} 222653541Sshin 222753541Sshinstatic const char * 222853541Sshinicmp6_redirect_diag(src6, dst6, tgt6) 222953541Sshin struct in6_addr *src6; 223053541Sshin struct in6_addr *dst6; 223153541Sshin struct in6_addr *tgt6; 223253541Sshin{ 223353541Sshin static char buf[1024]; 223453541Sshin snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", 2235120891Sume ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6)); 223653541Sshin return buf; 223753541Sshin} 223853541Sshin 223953541Sshinvoid 224053541Sshinicmp6_redirect_input(m, off) 224178064Sume struct mbuf *m; 224253541Sshin int off; 224353541Sshin{ 224453541Sshin struct ifnet *ifp = m->m_pkthdr.rcvif; 224553541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 224662587Sitojun struct nd_redirect *nd_rd; 224753541Sshin int icmp6len = ntohs(ip6->ip6_plen); 224853541Sshin char *lladdr = NULL; 224953541Sshin int lladdrlen = 0; 225053541Sshin u_char *redirhdr = NULL; 225153541Sshin int redirhdrlen = 0; 225253541Sshin struct rtentry *rt = NULL; 225353541Sshin int is_router; 225453541Sshin int is_onlink; 225553541Sshin struct in6_addr src6 = ip6->ip6_src; 225662587Sitojun struct in6_addr redtgt6; 225762587Sitojun struct in6_addr reddst6; 225853541Sshin union nd_opts ndopts; 225953541Sshin 226053541Sshin if (!m || !ifp) 226153541Sshin return; 226253541Sshin 226353541Sshin /* XXX if we are router, we don't update route by icmp6 redirect */ 226453541Sshin if (ip6_forwarding) 226562587Sitojun goto freeit; 226653541Sshin if (!icmp6_rediraccept) 226762587Sitojun goto freeit; 226862587Sitojun 226962587Sitojun#ifndef PULLDOWN_TEST 227062587Sitojun IP6_EXTHDR_CHECK(m, off, icmp6len,); 227162587Sitojun nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); 227262587Sitojun#else 227362587Sitojun IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); 227462587Sitojun if (nd_rd == NULL) { 227562587Sitojun icmp6stat.icp6s_tooshort++; 227653541Sshin return; 227762587Sitojun } 227862587Sitojun#endif 227962587Sitojun redtgt6 = nd_rd->nd_rd_target; 228062587Sitojun reddst6 = nd_rd->nd_rd_dst; 228153541Sshin 228253541Sshin if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 228353541Sshin redtgt6.s6_addr16[1] = htons(ifp->if_index); 228453541Sshin if (IN6_IS_ADDR_LINKLOCAL(&reddst6)) 228553541Sshin reddst6.s6_addr16[1] = htons(ifp->if_index); 228653541Sshin 228753541Sshin /* validation */ 228853541Sshin if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 228978064Sume nd6log((LOG_ERR, 2290120891Sume "ICMP6 redirect sent from %s rejected; " 2291120891Sume "must be from linklocal\n", 2292120891Sume ip6_sprintf(&src6))); 229378064Sume goto bad; 229453541Sshin } 229553541Sshin if (ip6->ip6_hlim != 255) { 229678064Sume nd6log((LOG_ERR, 2297120891Sume "ICMP6 redirect sent from %s rejected; " 2298120891Sume "hlim=%d (must be 255)\n", 2299120891Sume ip6_sprintf(&src6), ip6->ip6_hlim)); 230078064Sume goto bad; 230153541Sshin } 230253541Sshin { 230353541Sshin /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 230453541Sshin struct sockaddr_in6 sin6; 230553541Sshin struct in6_addr *gw6; 230653541Sshin 230753541Sshin bzero(&sin6, sizeof(sin6)); 230853541Sshin sin6.sin6_family = AF_INET6; 230953541Sshin sin6.sin6_len = sizeof(struct sockaddr_in6); 231053541Sshin bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); 231153541Sshin rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 231253541Sshin if (rt) { 231365895Sume if (rt->rt_gateway == NULL || 231465895Sume rt->rt_gateway->sa_family != AF_INET6) { 231578064Sume nd6log((LOG_ERR, 231665895Sume "ICMP6 redirect rejected; no route " 231765895Sume "with inet6 gateway found for redirect dst: %s\n", 231878064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2319120727Ssam RTFREE_LOCKED(rt); 232078064Sume goto bad; 232165895Sume } 232265895Sume 232353541Sshin gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); 232453541Sshin if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { 232578064Sume nd6log((LOG_ERR, 2326120891Sume "ICMP6 redirect rejected; " 2327120891Sume "not equal to gw-for-src=%s (must be same): " 2328120891Sume "%s\n", 2329120891Sume ip6_sprintf(gw6), 2330120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2331120727Ssam RTFREE_LOCKED(rt); 233278064Sume goto bad; 233353541Sshin } 233453541Sshin } else { 233578064Sume nd6log((LOG_ERR, 2336120891Sume "ICMP6 redirect rejected; " 2337120891Sume "no route found for redirect dst: %s\n", 2338120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 233978064Sume goto bad; 234053541Sshin } 2341120727Ssam RTFREE_LOCKED(rt); 234253541Sshin rt = NULL; 234353541Sshin } 234453541Sshin if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 234578064Sume nd6log((LOG_ERR, 2346120891Sume "ICMP6 redirect rejected; " 2347120891Sume "redirect dst must be unicast: %s\n", 2348120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 234978064Sume goto bad; 235053541Sshin } 235153541Sshin 235253541Sshin is_router = is_onlink = 0; 235353541Sshin if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 235453541Sshin is_router = 1; /* router case */ 235553541Sshin if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 235653541Sshin is_onlink = 1; /* on-link destination case */ 235753541Sshin if (!is_router && !is_onlink) { 235878064Sume nd6log((LOG_ERR, 2359120891Sume "ICMP6 redirect rejected; " 2360120891Sume "neither router case nor onlink case: %s\n", 2361120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 236278064Sume goto bad; 236353541Sshin } 236453541Sshin /* validation passed */ 236553541Sshin 236653541Sshin icmp6len -= sizeof(*nd_rd); 236753541Sshin nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 236853541Sshin if (nd6_options(&ndopts) < 0) { 236978064Sume nd6log((LOG_INFO, "icmp6_redirect_input: " 2370120891Sume "invalid ND option, rejected: %s\n", 2371120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 237278064Sume /* nd6_options have incremented stats */ 237362587Sitojun goto freeit; 237453541Sshin } 237553541Sshin 237653541Sshin if (ndopts.nd_opts_tgt_lladdr) { 237753541Sshin lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 237853541Sshin lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 237953541Sshin } 238053541Sshin 238153541Sshin if (ndopts.nd_opts_rh) { 238253541Sshin redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len; 238353541Sshin redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */ 238453541Sshin } 238553541Sshin 238653541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 238778064Sume nd6log((LOG_INFO, 2388120891Sume "icmp6_redirect_input: lladdrlen mismatch for %s " 2389120891Sume "(if %d, icmp6 packet %d): %s\n", 2390120891Sume ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2, 2391120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 239278064Sume goto bad; 239353541Sshin } 239453541Sshin 239553541Sshin /* RFC 2461 8.3 */ 239653541Sshin nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 2397120891Sume is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 239853541Sshin 239995023Ssuz if (!is_onlink) { /* better router case. perform rtredirect. */ 240053541Sshin /* perform rtredirect */ 240153541Sshin struct sockaddr_in6 sdst; 240253541Sshin struct sockaddr_in6 sgw; 240353541Sshin struct sockaddr_in6 ssrc; 240453541Sshin 240553541Sshin bzero(&sdst, sizeof(sdst)); 240653541Sshin bzero(&sgw, sizeof(sgw)); 240753541Sshin bzero(&ssrc, sizeof(ssrc)); 240853541Sshin sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; 240953541Sshin sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = 241053541Sshin sizeof(struct sockaddr_in6); 241153541Sshin bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); 241253541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 241353541Sshin bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 241453541Sshin rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw, 2415120891Sume (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST, 2416120891Sume (struct sockaddr *)&ssrc); 241753541Sshin } 241853541Sshin /* finally update cached route in each socket via pfctlinput */ 241953541Sshin { 242053541Sshin struct sockaddr_in6 sdst; 242153541Sshin 242253541Sshin bzero(&sdst, sizeof(sdst)); 242353541Sshin sdst.sin6_family = AF_INET6; 242453541Sshin sdst.sin6_len = sizeof(struct sockaddr_in6); 242553541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 242653541Sshin pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); 2427122062Sume#if defined(IPSEC) || defined(FAST_IPSEC) 242853541Sshin key_sa_routechange((struct sockaddr *)&sdst); 242953541Sshin#endif 243053541Sshin } 243162587Sitojun 243262587Sitojun freeit: 243362587Sitojun m_freem(m); 243478064Sume return; 243578064Sume 243678064Sume bad: 243778064Sume icmp6stat.icp6s_badredirect++; 243878064Sume m_freem(m); 243953541Sshin} 244053541Sshin 244153541Sshinvoid 244253541Sshinicmp6_redirect_output(m0, rt) 244353541Sshin struct mbuf *m0; 244453541Sshin struct rtentry *rt; 244553541Sshin{ 244653541Sshin struct ifnet *ifp; /* my outgoing interface */ 244753541Sshin struct in6_addr *ifp_ll6; 244853541Sshin struct in6_addr *router_ll6; 244953541Sshin struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 245053541Sshin struct mbuf *m = NULL; /* newly allocated one */ 245153541Sshin struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 245253541Sshin struct nd_redirect *nd_rd; 245353541Sshin size_t maxlen; 245453541Sshin u_char *p; 245553541Sshin struct ifnet *outif = NULL; 245662587Sitojun struct sockaddr_in6 src_sa; 245753541Sshin 245862587Sitojun icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0); 245962587Sitojun 246053541Sshin /* if we are not router, we don't send icmp6 redirect */ 246153541Sshin if (!ip6_forwarding || ip6_accept_rtadv) 246253541Sshin goto fail; 246353541Sshin 246453541Sshin /* sanity check */ 246553541Sshin if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) 246653541Sshin goto fail; 246753541Sshin 246853541Sshin /* 246953541Sshin * Address check: 247053541Sshin * the source address must identify a neighbor, and 247153541Sshin * the destination address must not be a multicast address 247253541Sshin * [RFC 2461, sec 8.2] 247353541Sshin */ 247453541Sshin sip6 = mtod(m0, struct ip6_hdr *); 247562587Sitojun bzero(&src_sa, sizeof(src_sa)); 247662587Sitojun src_sa.sin6_family = AF_INET6; 247762587Sitojun src_sa.sin6_len = sizeof(src_sa); 247862587Sitojun src_sa.sin6_addr = sip6->ip6_src; 247962587Sitojun /* we don't currently use sin6_scope_id, but eventually use it */ 2480121315Sume if (in6_addr2zoneid(ifp, &sip6->ip6_src, &src_sa.sin6_scope_id)) 2481121315Sume goto fail; 248262587Sitojun if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) 248353541Sshin goto fail; 248453541Sshin if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) 248553541Sshin goto fail; /* what should we do here? */ 248653541Sshin 248753541Sshin /* rate limit */ 248853541Sshin if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) 248953541Sshin goto fail; 249053541Sshin 249153541Sshin /* 249253541Sshin * Since we are going to append up to 1280 bytes (= IPV6_MMTU), 249353541Sshin * we almost always ask for an mbuf cluster for simplicity. 249453541Sshin * (MHLEN < IPV6_MMTU is almost always true) 249553541Sshin */ 249662587Sitojun#if IPV6_MMTU >= MCLBYTES 249762587Sitojun# error assumption failed about IPV6_MMTU and MCLBYTES 249862587Sitojun#endif 2499111119Simp MGETHDR(m, M_DONTWAIT, MT_HEADER); 250062587Sitojun if (m && IPV6_MMTU >= MHLEN) 2501111119Simp MCLGET(m, M_DONTWAIT); 250253541Sshin if (!m) 250353541Sshin goto fail; 250478064Sume m->m_pkthdr.rcvif = NULL; 250578064Sume m->m_len = 0; 250678064Sume maxlen = M_TRAILINGSPACE(m); 250753541Sshin maxlen = min(IPV6_MMTU, maxlen); 250853541Sshin /* just for safety */ 250962587Sitojun if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 251062587Sitojun ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { 251153541Sshin goto fail; 251262587Sitojun } 251353541Sshin 251453541Sshin { 251553541Sshin /* get ip6 linklocal address for ifp(my outgoing interface). */ 251662587Sitojun struct in6_ifaddr *ia; 251762587Sitojun if ((ia = in6ifa_ifpforlinklocal(ifp, 251862587Sitojun IN6_IFF_NOTREADY| 251962587Sitojun IN6_IFF_ANYCAST)) == NULL) 252053541Sshin goto fail; 252153541Sshin ifp_ll6 = &ia->ia_addr.sin6_addr; 252253541Sshin } 252353541Sshin 252453541Sshin /* get ip6 linklocal address for the router. */ 252553541Sshin if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { 252653541Sshin struct sockaddr_in6 *sin6; 252753541Sshin sin6 = (struct sockaddr_in6 *)rt->rt_gateway; 252853541Sshin router_ll6 = &sin6->sin6_addr; 252953541Sshin if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) 253053541Sshin router_ll6 = (struct in6_addr *)NULL; 253153541Sshin } else 253253541Sshin router_ll6 = (struct in6_addr *)NULL; 253353541Sshin 253453541Sshin /* ip6 */ 253553541Sshin ip6 = mtod(m, struct ip6_hdr *); 253653541Sshin ip6->ip6_flow = 0; 253762587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 253862587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 253953541Sshin /* ip6->ip6_plen will be set later */ 254053541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 254153541Sshin ip6->ip6_hlim = 255; 254253541Sshin /* ip6->ip6_src must be linklocal addr for my outgoing if. */ 254353541Sshin bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); 254453541Sshin bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); 254553541Sshin 254653541Sshin /* ND Redirect */ 254753541Sshin nd_rd = (struct nd_redirect *)(ip6 + 1); 254853541Sshin nd_rd->nd_rd_type = ND_REDIRECT; 254953541Sshin nd_rd->nd_rd_code = 0; 255053541Sshin nd_rd->nd_rd_reserved = 0; 255153541Sshin if (rt->rt_flags & RTF_GATEWAY) { 255253541Sshin /* 255353541Sshin * nd_rd->nd_rd_target must be a link-local address in 255453541Sshin * better router cases. 255553541Sshin */ 255653541Sshin if (!router_ll6) 255753541Sshin goto fail; 255853541Sshin bcopy(router_ll6, &nd_rd->nd_rd_target, 2559120892Sume sizeof(nd_rd->nd_rd_target)); 256053541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2561120892Sume sizeof(nd_rd->nd_rd_dst)); 256253541Sshin } else { 256353541Sshin /* make sure redtgt == reddst */ 256453541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, 2565120892Sume sizeof(nd_rd->nd_rd_target)); 256653541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2567120892Sume sizeof(nd_rd->nd_rd_dst)); 256853541Sshin } 256953541Sshin 257053541Sshin p = (u_char *)(nd_rd + 1); 257153541Sshin 257253541Sshin if (!router_ll6) 257353541Sshin goto nolladdropt; 257453541Sshin 2575120892Sume { 2576120892Sume /* target lladdr option */ 2577120892Sume struct rtentry *rt_router = NULL; 2578120892Sume int len; 2579120892Sume struct sockaddr_dl *sdl; 2580120892Sume struct nd_opt_hdr *nd_opt; 2581120892Sume char *lladdr; 258253541Sshin 2583120892Sume rt_router = nd6_lookup(router_ll6, 0, ifp); 2584120892Sume if (!rt_router) 2585120892Sume goto nolladdropt; 2586120892Sume len = sizeof(*nd_opt) + ifp->if_addrlen; 2587120892Sume len = (len + 7) & ~7; /* round by 8 */ 2588120892Sume /* safety check */ 2589120892Sume if (len + (p - (u_char *)ip6) > maxlen) 2590120892Sume goto nolladdropt; 2591120892Sume if (!(rt_router->rt_flags & RTF_GATEWAY) && 2592120892Sume (rt_router->rt_flags & RTF_LLINFO) && 2593120892Sume (rt_router->rt_gateway->sa_family == AF_LINK) && 2594120892Sume (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) && 2595120892Sume sdl->sdl_alen) { 2596120892Sume nd_opt = (struct nd_opt_hdr *)p; 2597120892Sume nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 2598120892Sume nd_opt->nd_opt_len = len >> 3; 2599120892Sume lladdr = (char *)(nd_opt + 1); 2600120892Sume bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen); 2601120892Sume p += len; 2602120892Sume } 2603120893Sume } 260453541Sshinnolladdropt:; 260553541Sshin 260653541Sshin m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 260753541Sshin 260853541Sshin /* just to be safe */ 260962587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 261053541Sshin if (m0->m_flags & M_DECRYPTED) 261153541Sshin goto noredhdropt; 261262587Sitojun#endif 261362587Sitojun if (p - (u_char *)ip6 > maxlen) 261462587Sitojun goto noredhdropt; 261553541Sshin 2616120891Sume { 2617120891Sume /* redirected header option */ 2618120891Sume int len; 2619120891Sume struct nd_opt_rd_hdr *nd_opt_rh; 262053541Sshin 2621120891Sume /* 2622120891Sume * compute the maximum size for icmp6 redirect header option. 2623120891Sume * XXX room for auth header? 2624120891Sume */ 2625120891Sume len = maxlen - (p - (u_char *)ip6); 2626120891Sume len &= ~7; 262753541Sshin 2628120891Sume /* This is just for simplicity. */ 2629120891Sume if (m0->m_pkthdr.len != m0->m_len) { 2630120891Sume if (m0->m_next) { 2631120891Sume m_freem(m0->m_next); 2632120891Sume m0->m_next = NULL; 2633120891Sume } 2634120891Sume m0->m_pkthdr.len = m0->m_len; 263553541Sshin } 263653541Sshin 2637120891Sume /* 2638120891Sume * Redirected header option spec (RFC2461 4.6.3) talks nothing 2639120891Sume * about padding/truncate rule for the original IP packet. 2640120891Sume * From the discussion on IPv6imp in Feb 1999, 2641120891Sume * the consensus was: 2642120891Sume * - "attach as much as possible" is the goal 2643120891Sume * - pad if not aligned (original size can be guessed by 2644120891Sume * original ip6 header) 2645120891Sume * Following code adds the padding if it is simple enough, 2646120891Sume * and truncates if not. 2647120891Sume */ 2648120891Sume if (m0->m_next || m0->m_pkthdr.len != m0->m_len) 2649120891Sume panic("assumption failed in %s:%d", __FILE__, 2650120891Sume __LINE__); 265153541Sshin 2652120891Sume if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { 2653120891Sume /* not enough room, truncate */ 2654120891Sume m0->m_pkthdr.len = m0->m_len = len - 2655120891Sume sizeof(*nd_opt_rh); 2656120891Sume } else { 2657120891Sume /* enough room, pad or truncate */ 2658120891Sume size_t extra; 265953541Sshin 2660120891Sume extra = m0->m_pkthdr.len % 8; 2661120891Sume if (extra) { 2662120891Sume /* pad if easy enough, truncate if not */ 2663120891Sume if (8 - extra <= M_TRAILINGSPACE(m0)) { 2664120891Sume /* pad */ 2665120891Sume m0->m_len += (8 - extra); 2666120891Sume m0->m_pkthdr.len += (8 - extra); 2667120891Sume } else { 2668120891Sume /* truncate */ 2669120891Sume m0->m_pkthdr.len -= extra; 2670120891Sume m0->m_len -= extra; 2671120891Sume } 267253541Sshin } 2673120891Sume len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); 2674120891Sume m0->m_pkthdr.len = m0->m_len = len - 2675120891Sume sizeof(*nd_opt_rh); 267653541Sshin } 267753541Sshin 2678120891Sume nd_opt_rh = (struct nd_opt_rd_hdr *)p; 2679120891Sume bzero(nd_opt_rh, sizeof(*nd_opt_rh)); 2680120891Sume nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; 2681120891Sume nd_opt_rh->nd_opt_rh_len = len >> 3; 2682120891Sume p += sizeof(*nd_opt_rh); 2683120891Sume m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 268453541Sshin 2685120891Sume /* connect m0 to m */ 2686120891Sume m_tag_delete_chain(m0, NULL); 2687120891Sume m0->m_flags &= ~M_PKTHDR; 2688120891Sume m->m_next = m0; 2689120891Sume m->m_pkthdr.len = m->m_len + m0->m_len; 2690120891Sume m0 = NULL; 2691120891Sume } 269253541Sshinnoredhdropt:; 2693112781Ssuz if (m0) { 2694112781Ssuz m_freem(m0); 2695112781Ssuz m0 = NULL; 2696112781Ssuz } 269753541Sshin 2698121315Sume /* XXX: clear embedded link IDs in the inner header */ 2699121315Sume in6_clearscope(&sip6->ip6_src); 2700121315Sume in6_clearscope(&sip6->ip6_dst); 2701121315Sume in6_clearscope(&nd_rd->nd_rd_target); 2702121315Sume in6_clearscope(&nd_rd->nd_rd_dst); 270353541Sshin 270453541Sshin ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 270553541Sshin 270653541Sshin nd_rd->nd_rd_cksum = 0; 2707120891Sume nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2708120891Sume sizeof(*ip6), ntohs(ip6->ip6_plen)); 270953541Sshin 271053541Sshin /* send the packet to outside... */ 2711105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 271253541Sshin if (outif) { 271353541Sshin icmp6_ifstat_inc(outif, ifs6_out_msg); 271453541Sshin icmp6_ifstat_inc(outif, ifs6_out_redirect); 271553541Sshin } 271653541Sshin icmp6stat.icp6s_outhist[ND_REDIRECT]++; 271753541Sshin 271853541Sshin return; 271953541Sshin 272053541Sshinfail: 272153541Sshin if (m) 272253541Sshin m_freem(m); 272353541Sshin if (m0) 272453541Sshin m_freem(m0); 272553541Sshin} 272653541Sshin 272753541Sshin/* 272853541Sshin * ICMPv6 socket option processing. 272953541Sshin */ 273053541Sshinint 273153541Sshinicmp6_ctloutput(so, sopt) 273253541Sshin struct socket *so; 273353541Sshin struct sockopt *sopt; 273453541Sshin{ 273553541Sshin int error = 0; 273653541Sshin int optlen; 273778064Sume struct inpcb *inp = sotoinpcb(so); 273853541Sshin int level, op, optname; 273953541Sshin 274053541Sshin if (sopt) { 274153541Sshin level = sopt->sopt_level; 274253541Sshin op = sopt->sopt_dir; 274353541Sshin optname = sopt->sopt_name; 274453541Sshin optlen = sopt->sopt_valsize; 274553541Sshin } else 274653541Sshin level = op = optname = optlen = 0; 274778064Sume 274853541Sshin if (level != IPPROTO_ICMPV6) { 274953541Sshin return EINVAL; 275053541Sshin } 275153541Sshin 275278064Sume switch (op) { 275353541Sshin case PRCO_SETOPT: 275453541Sshin switch (optname) { 275553541Sshin case ICMP6_FILTER: 275653541Sshin { 275753541Sshin struct icmp6_filter *p; 275853541Sshin 275953541Sshin if (optlen != sizeof(*p)) { 276053541Sshin error = EMSGSIZE; 276153541Sshin break; 276253541Sshin } 276353541Sshin if (inp->in6p_icmp6filt == NULL) { 276453541Sshin error = EINVAL; 276553541Sshin break; 276653541Sshin } 276753541Sshin error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen, 276853541Sshin optlen); 276953541Sshin break; 277053541Sshin } 277153541Sshin 277253541Sshin default: 277353541Sshin error = ENOPROTOOPT; 277453541Sshin break; 277553541Sshin } 277653541Sshin break; 277753541Sshin 277853541Sshin case PRCO_GETOPT: 277953541Sshin switch (optname) { 278053541Sshin case ICMP6_FILTER: 278153541Sshin { 278253541Sshin if (inp->in6p_icmp6filt == NULL) { 278353541Sshin error = EINVAL; 278453541Sshin break; 278553541Sshin } 278653541Sshin error = sooptcopyout(sopt, inp->in6p_icmp6filt, 278753541Sshin sizeof(struct icmp6_filter)); 278853541Sshin break; 278953541Sshin } 279053541Sshin 279153541Sshin default: 279253541Sshin error = ENOPROTOOPT; 279353541Sshin break; 279453541Sshin } 279553541Sshin break; 279653541Sshin } 279753541Sshin 2798120856Sume return (error); 279953541Sshin} 280053541Sshin 280153541Sshin/* 280253541Sshin * Perform rate limit check. 280353541Sshin * Returns 0 if it is okay to send the icmp6 packet. 280453541Sshin * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 280553541Sshin * limitation. 280653541Sshin * 280753541Sshin * XXX per-destination/type check necessary? 280853541Sshin */ 280953541Sshinstatic int 281053541Sshinicmp6_ratelimit(dst, type, code) 281153541Sshin const struct in6_addr *dst; /* not used at this moment */ 281253541Sshin const int type; /* not used at this moment */ 281353541Sshin const int code; /* not used at this moment */ 281453541Sshin{ 281562587Sitojun int ret; 281653541Sshin 281795023Ssuz ret = 0; /* okay to send */ 281853541Sshin 281962587Sitojun /* PPS limit */ 282078064Sume if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count, 282178064Sume icmp6errppslim)) { 282253541Sshin /* The packet is subject to rate limit */ 282362587Sitojun ret++; 282453541Sshin } 282553541Sshin 282662587Sitojun return ret; 282753541Sshin} 2828