icmp6.c revision 122922
162587Sitojun/* $FreeBSD: head/sys/netinet6/icmp6.c 122922 2003-11-20 20:07:39Z andre $ */ 278064Sume/* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ */ 362587Sitojun 453541Sshin/* 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 3353541Sshin/* 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 * 3. All advertising materials mentioning features or use of this software 4653541Sshin * must display the following acknowledgement: 4753541Sshin * This product includes software developed by the University of 4853541Sshin * California, Berkeley and its contributors. 4953541Sshin * 4. Neither the name of the University nor the names of its contributors 5053541Sshin * may be used to endorse or promote products derived from this software 5153541Sshin * without specific prior written permission. 5253541Sshin * 5353541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5453541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5553541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5653541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5753541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5853541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5953541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 6053541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 6153541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 6253541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 6353541Sshin * SUCH DAMAGE. 6453541Sshin * 6553541Sshin * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 6653541Sshin */ 6753541Sshin 6862587Sitojun#include "opt_inet.h" 6962587Sitojun#include "opt_inet6.h" 7055009Sshin#include "opt_ipsec.h" 7153541Sshin 7253541Sshin#include <sys/param.h> 7395759Stanimura#include <sys/domain.h> 7495759Stanimura#include <sys/kernel.h> 7595759Stanimura#include <sys/lock.h> 7678064Sume#include <sys/malloc.h> 7753541Sshin#include <sys/mbuf.h> 7853541Sshin#include <sys/protosw.h> 7995759Stanimura#include <sys/signalvar.h> 8053541Sshin#include <sys/socket.h> 8153541Sshin#include <sys/socketvar.h> 8295759Stanimura#include <sys/sx.h> 8395759Stanimura#include <sys/syslog.h> 8495759Stanimura#include <sys/systm.h> 8553541Sshin#include <sys/time.h> 8653541Sshin 8753541Sshin#include <net/if.h> 8853541Sshin#include <net/if_dl.h> 8953541Sshin#include <net/if_types.h> 9095759Stanimura#include <net/route.h> 9153541Sshin 9253541Sshin#include <netinet/in.h> 9395759Stanimura#include <netinet/in_pcb.h> 9453541Sshin#include <netinet/in_var.h> 9562587Sitojun#include <netinet/ip6.h> 9695759Stanimura#include <netinet/icmp6.h> 97122922Sandre#include <netinet/tcp_var.h> 9895759Stanimura#include <netinet6/in6_ifattach.h> 9995759Stanimura#include <netinet6/in6_pcb.h> 10095759Stanimura#include <netinet6/ip6protosw.h> 10153541Sshin#include <netinet6/ip6_var.h> 10253541Sshin#include <netinet6/mld6_var.h> 10353541Sshin#include <netinet6/nd6.h> 10453541Sshin 10553541Sshin#ifdef IPSEC 10653541Sshin#include <netinet6/ipsec.h> 10753541Sshin#include <netkey/key.h> 10855009Sshin#endif 10953541Sshin 110105199Ssam#ifdef FAST_IPSEC 111105199Ssam#include <netipsec/ipsec.h> 112105199Ssam#include <netipsec/key.h> 113105199Ssam#endif 114105199Ssam 11553541Sshin#include <net/net_osdep.h> 11653541Sshin 11762587Sitojunextern struct domain inet6domain; 11853541Sshin 11962587Sitojunstruct icmp6stat icmp6stat; 12053541Sshin 12162587Sitojunextern struct inpcbhead ripcb; 12262587Sitojunextern int icmp6errppslim; 12362587Sitojunstatic int icmp6errpps_count = 0; 12478064Sumestatic struct timeval icmp6errppslim_last; 12562587Sitojunextern int icmp6_nodeinfo; 12653541Sshin 12762587Sitojunstatic void icmp6_errcount __P((struct icmp6errstat *, int, int)); 12862587Sitojunstatic int icmp6_rip6_input __P((struct mbuf **, int)); 12962587Sitojunstatic int icmp6_ratelimit __P((const struct in6_addr *, const int, const int)); 13062587Sitojunstatic const char *icmp6_redirect_diag __P((struct in6_addr *, 13162587Sitojun struct in6_addr *, struct in6_addr *)); 13262587Sitojunstatic struct mbuf *ni6_input __P((struct mbuf *, int)); 13362587Sitojunstatic struct mbuf *ni6_nametodns __P((const char *, int, int)); 13462587Sitojunstatic int ni6_dnsmatch __P((const char *, int, const char *, int)); 13562587Sitojunstatic int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *, 13678064Sume struct ifnet **, char *)); 13762587Sitojunstatic int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, 13862587Sitojun struct ifnet *, int)); 13978064Sumestatic int icmp6_notify_error __P((struct mbuf *, int, int, int)); 14053541Sshin 14153541Sshin#ifdef COMPAT_RFC1885 14262587Sitojunstatic struct route_in6 icmp6_reflect_rt; 14353541Sshin#endif 14453541Sshin 14578064Sume 14653541Sshinvoid 14753541Sshinicmp6_init() 14853541Sshin{ 14953541Sshin mld6_init(); 15053541Sshin} 15153541Sshin 15262587Sitojunstatic void 15362587Sitojunicmp6_errcount(stat, type, code) 15462587Sitojun struct icmp6errstat *stat; 15562587Sitojun int type, code; 15662587Sitojun{ 15778064Sume switch (type) { 15862587Sitojun case ICMP6_DST_UNREACH: 15962587Sitojun switch (code) { 16062587Sitojun case ICMP6_DST_UNREACH_NOROUTE: 16162587Sitojun stat->icp6errs_dst_unreach_noroute++; 16262587Sitojun return; 16362587Sitojun case ICMP6_DST_UNREACH_ADMIN: 16462587Sitojun stat->icp6errs_dst_unreach_admin++; 16562587Sitojun return; 16662587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 16762587Sitojun stat->icp6errs_dst_unreach_beyondscope++; 16862587Sitojun return; 16962587Sitojun case ICMP6_DST_UNREACH_ADDR: 17062587Sitojun stat->icp6errs_dst_unreach_addr++; 17162587Sitojun return; 17262587Sitojun case ICMP6_DST_UNREACH_NOPORT: 17362587Sitojun stat->icp6errs_dst_unreach_noport++; 17462587Sitojun return; 17562587Sitojun } 17662587Sitojun break; 17762587Sitojun case ICMP6_PACKET_TOO_BIG: 17862587Sitojun stat->icp6errs_packet_too_big++; 17962587Sitojun return; 18062587Sitojun case ICMP6_TIME_EXCEEDED: 18178064Sume switch (code) { 18262587Sitojun case ICMP6_TIME_EXCEED_TRANSIT: 18362587Sitojun stat->icp6errs_time_exceed_transit++; 18462587Sitojun return; 18562587Sitojun case ICMP6_TIME_EXCEED_REASSEMBLY: 18662587Sitojun stat->icp6errs_time_exceed_reassembly++; 18762587Sitojun return; 18862587Sitojun } 18962587Sitojun break; 19062587Sitojun case ICMP6_PARAM_PROB: 19178064Sume switch (code) { 19262587Sitojun case ICMP6_PARAMPROB_HEADER: 19362587Sitojun stat->icp6errs_paramprob_header++; 19462587Sitojun return; 19562587Sitojun case ICMP6_PARAMPROB_NEXTHEADER: 19662587Sitojun stat->icp6errs_paramprob_nextheader++; 19762587Sitojun return; 19862587Sitojun case ICMP6_PARAMPROB_OPTION: 19962587Sitojun stat->icp6errs_paramprob_option++; 20062587Sitojun return; 20162587Sitojun } 20262587Sitojun break; 20362587Sitojun case ND_REDIRECT: 20462587Sitojun stat->icp6errs_redirect++; 20562587Sitojun return; 20662587Sitojun } 20762587Sitojun stat->icp6errs_unknown++; 20862587Sitojun} 20962587Sitojun 21053541Sshin/* 21153541Sshin * Generate an error packet of type error in response to bad IP6 packet. 21253541Sshin */ 21353541Sshinvoid 21453541Sshinicmp6_error(m, type, code, param) 21553541Sshin struct mbuf *m; 21653541Sshin int type, code, param; 21753541Sshin{ 21853541Sshin struct ip6_hdr *oip6, *nip6; 21953541Sshin struct icmp6_hdr *icmp6; 22062587Sitojun u_int preplen; 22153541Sshin int off; 22262587Sitojun int nxt; 22353541Sshin 22453541Sshin icmp6stat.icp6s_error++; 22553541Sshin 22662587Sitojun /* count per-type-code statistics */ 22762587Sitojun icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code); 22862587Sitojun 22962587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 23062587Sitojun if (m->m_flags & M_DECRYPTED) { 23162587Sitojun icmp6stat.icp6s_canterror++; 23253541Sshin goto freeit; 23362587Sitojun } 23462587Sitojun#endif 23553541Sshin 23662587Sitojun#ifndef PULLDOWN_TEST 23762587Sitojun IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 23862587Sitojun#else 23962587Sitojun if (m->m_len < sizeof(struct ip6_hdr)) { 24062587Sitojun m = m_pullup(m, sizeof(struct ip6_hdr)); 24162587Sitojun if (m == NULL) 24262587Sitojun return; 24362587Sitojun } 24462587Sitojun#endif 24553541Sshin oip6 = mtod(m, struct ip6_hdr *); 24653541Sshin 24753541Sshin /* 248121472Sume * If the destination address of the erroneous packet is a multicast 249121472Sume * address, or the packet was sent using link-layer multicast, 250121472Sume * we should basically suppress sending an error (RFC 2463, Section 251121472Sume * 2.4). 252121472Sume * We have two exceptions (the item e.2 in that section): 253121472Sume * - the Pakcet Too Big message can be sent for path MTU discovery. 254121472Sume * - the Parameter Problem Message that can be allowed an icmp6 error 255121472Sume * in the option type field. This check has been done in 256121472Sume * ip6_unknown_opt(), so we can just check the type and code. 25753541Sshin */ 25853541Sshin if ((m->m_flags & (M_BCAST|M_MCAST) || 25953541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && 26053541Sshin (type != ICMP6_PACKET_TOO_BIG && 26153541Sshin (type != ICMP6_PARAM_PROB || 26253541Sshin code != ICMP6_PARAMPROB_OPTION))) 26353541Sshin goto freeit; 26453541Sshin 265121472Sume /* 266121472Sume * RFC 2463, 2.4 (e.5): source address check. 267121472Sume * XXX: the case of anycast source? 268121472Sume */ 26953541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || 27053541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) 27153541Sshin goto freeit; 27253541Sshin 27353541Sshin /* 27462587Sitojun * If we are about to send ICMPv6 against ICMPv6 error/redirect, 27562587Sitojun * don't do it. 27653541Sshin */ 27762587Sitojun nxt = -1; 27862587Sitojun off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 27962587Sitojun if (off >= 0 && nxt == IPPROTO_ICMPV6) { 28053541Sshin struct icmp6_hdr *icp; 28153541Sshin 28262587Sitojun#ifndef PULLDOWN_TEST 28362587Sitojun IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), ); 28462587Sitojun icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 28562587Sitojun#else 28662587Sitojun IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, 28762587Sitojun sizeof(*icp)); 28862587Sitojun if (icp == NULL) { 28962587Sitojun icmp6stat.icp6s_tooshort++; 29062587Sitojun return; 29162587Sitojun } 29262587Sitojun#endif 29362587Sitojun if (icp->icmp6_type < ICMP6_ECHO_REQUEST || 29462587Sitojun icp->icmp6_type == ND_REDIRECT) { 29553541Sshin /* 29662587Sitojun * ICMPv6 error 29762587Sitojun * Special case: for redirect (which is 29862587Sitojun * informational) we must not send icmp6 error. 29953541Sshin */ 30062587Sitojun icmp6stat.icp6s_canterror++; 30162587Sitojun goto freeit; 30262587Sitojun } else { 30362587Sitojun /* ICMPv6 informational - send the error */ 30453541Sshin } 30562587Sitojun } else { 30662587Sitojun /* non-ICMPv6 - send the error */ 30753541Sshin } 30853541Sshin 30953541Sshin oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */ 31053541Sshin 31153541Sshin /* Finally, do rate limitation check. */ 31253541Sshin if (icmp6_ratelimit(&oip6->ip6_src, type, code)) { 31353541Sshin icmp6stat.icp6s_toofreq++; 31453541Sshin goto freeit; 31553541Sshin } 31653541Sshin 31753541Sshin /* 31853541Sshin * OK, ICMP6 can be generated. 31953541Sshin */ 32053541Sshin 32153541Sshin if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) 32253541Sshin m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); 32353541Sshin 32462587Sitojun preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 325111119Simp M_PREPEND(m, preplen, M_DONTWAIT); 32662587Sitojun if (m && m->m_len < preplen) 32762587Sitojun m = m_pullup(m, preplen); 32853541Sshin if (m == NULL) { 32978064Sume nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__)); 33053541Sshin return; 33153541Sshin } 33253541Sshin 33353541Sshin nip6 = mtod(m, struct ip6_hdr *); 33453541Sshin nip6->ip6_src = oip6->ip6_src; 33553541Sshin nip6->ip6_dst = oip6->ip6_dst; 33653541Sshin 337121315Sume in6_clearscope(&oip6->ip6_src); 338121315Sume in6_clearscope(&oip6->ip6_dst); 33953541Sshin 34053541Sshin icmp6 = (struct icmp6_hdr *)(nip6 + 1); 34153541Sshin icmp6->icmp6_type = type; 34253541Sshin icmp6->icmp6_code = code; 34353541Sshin icmp6->icmp6_pptr = htonl((u_int32_t)param); 34453541Sshin 34578064Sume /* 34678064Sume * icmp6_reflect() is designed to be in the input path. 34778064Sume * icmp6_error() can be called from both input and outut path, 34878064Sume * and if we are in output path rcvif could contain bogus value. 34978064Sume * clear m->m_pkthdr.rcvif for safety, we should have enough scope 35078064Sume * information in ip header (nip6). 35178064Sume */ 35278064Sume m->m_pkthdr.rcvif = NULL; 35378064Sume 35453541Sshin icmp6stat.icp6s_outhist[type]++; 35595023Ssuz icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ 35662587Sitojun 35762587Sitojun return; 35862587Sitojun 35962587Sitojun freeit: 36062587Sitojun /* 36162587Sitojun * If we can't tell wheter or not we can generate ICMP6, free it. 36262587Sitojun */ 36362587Sitojun m_freem(m); 36453541Sshin} 36553541Sshin 36653541Sshin/* 36753541Sshin * Process a received ICMP6 message. 36853541Sshin */ 36953541Sshinint 37053541Sshinicmp6_input(mp, offp, proto) 37153541Sshin struct mbuf **mp; 37253541Sshin int *offp, proto; 37353541Sshin{ 37453541Sshin struct mbuf *m = *mp, *n; 37553541Sshin struct ip6_hdr *ip6, *nip6; 37653541Sshin struct icmp6_hdr *icmp6, *nicmp6; 37753541Sshin int off = *offp; 37853541Sshin int icmp6len = m->m_pkthdr.len - *offp; 37953541Sshin int code, sum, noff; 38053541Sshin 38162587Sitojun#ifndef PULLDOWN_TEST 38253541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE); 38395023Ssuz /* m might change if M_LOOP. So, call mtod after this */ 38462587Sitojun#endif 38553541Sshin 38653541Sshin /* 38753541Sshin * Locate icmp6 structure in mbuf, and check 38853541Sshin * that not corrupted and of at least minimum length 38953541Sshin */ 39053541Sshin 39153541Sshin ip6 = mtod(m, struct ip6_hdr *); 39253541Sshin if (icmp6len < sizeof(struct icmp6_hdr)) { 39353541Sshin icmp6stat.icp6s_tooshort++; 39453541Sshin goto freeit; 39553541Sshin } 39653541Sshin 39753541Sshin /* 39853541Sshin * calculate the checksum 39953541Sshin */ 40062587Sitojun#ifndef PULLDOWN_TEST 40153541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 40262587Sitojun#else 40362587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 40462587Sitojun if (icmp6 == NULL) { 40562587Sitojun icmp6stat.icp6s_tooshort++; 40662587Sitojun return IPPROTO_DONE; 40762587Sitojun } 40862587Sitojun#endif 40953541Sshin code = icmp6->icmp6_code; 41053541Sshin 41153541Sshin if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { 41278064Sume nd6log((LOG_ERR, 41353541Sshin "ICMP6 checksum error(%d|%x) %s\n", 41478064Sume icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src))); 41553541Sshin icmp6stat.icp6s_checksum++; 41653541Sshin goto freeit; 41753541Sshin } 41853541Sshin 41983934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 42053541Sshin /* 42153541Sshin * Deliver very specific ICMP6 type only. 42253541Sshin * This is important to deilver TOOBIG. Otherwise PMTUD 42353541Sshin * will not work. 42453541Sshin */ 42553541Sshin switch (icmp6->icmp6_type) { 42653541Sshin case ICMP6_DST_UNREACH: 42753541Sshin case ICMP6_PACKET_TOO_BIG: 42853541Sshin case ICMP6_TIME_EXCEEDED: 42953541Sshin break; 43053541Sshin default: 43153541Sshin goto freeit; 43253541Sshin } 43353541Sshin } 43453541Sshin 43553541Sshin icmp6stat.icp6s_inhist[icmp6->icmp6_type]++; 43653541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg); 43753541Sshin if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) 43853541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error); 43953541Sshin 44053541Sshin switch (icmp6->icmp6_type) { 44153541Sshin case ICMP6_DST_UNREACH: 44253541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach); 44353541Sshin switch (code) { 44453541Sshin case ICMP6_DST_UNREACH_NOROUTE: 44553541Sshin code = PRC_UNREACH_NET; 44653541Sshin break; 44753541Sshin case ICMP6_DST_UNREACH_ADMIN: 44853541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib); 44962587Sitojun code = PRC_UNREACH_PROTOCOL; /* is this a good code? */ 45062587Sitojun break; 45153541Sshin case ICMP6_DST_UNREACH_ADDR: 45262587Sitojun code = PRC_HOSTDEAD; 45353541Sshin break; 45462587Sitojun#ifdef COMPAT_RFC1885 45553541Sshin case ICMP6_DST_UNREACH_NOTNEIGHBOR: 45653541Sshin code = PRC_UNREACH_SRCFAIL; 45753541Sshin break; 45862587Sitojun#else 45962587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 46062587Sitojun /* I mean "source address was incorrect." */ 46162587Sitojun code = PRC_PARAMPROB; 46262587Sitojun break; 46362587Sitojun#endif 46453541Sshin case ICMP6_DST_UNREACH_NOPORT: 46553541Sshin code = PRC_UNREACH_PORT; 46653541Sshin break; 46753541Sshin default: 46853541Sshin goto badcode; 46953541Sshin } 47053541Sshin goto deliver; 47153541Sshin break; 47253541Sshin 47353541Sshin case ICMP6_PACKET_TOO_BIG: 47453541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig); 47553541Sshin if (code != 0) 47653541Sshin goto badcode; 47753541Sshin 47853541Sshin code = PRC_MSGSIZE; 47953541Sshin 48062587Sitojun /* 48162587Sitojun * Updating the path MTU will be done after examining 48262587Sitojun * intermediate extension headers. 48362587Sitojun */ 48453541Sshin goto deliver; 48553541Sshin break; 48653541Sshin 48753541Sshin case ICMP6_TIME_EXCEEDED: 48853541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed); 48953541Sshin switch (code) { 49053541Sshin case ICMP6_TIME_EXCEED_TRANSIT: 49153541Sshin case ICMP6_TIME_EXCEED_REASSEMBLY: 49253541Sshin code += PRC_TIMXCEED_INTRANS; 49353541Sshin break; 49453541Sshin default: 49553541Sshin goto badcode; 49653541Sshin } 49753541Sshin goto deliver; 49853541Sshin break; 49953541Sshin 50053541Sshin case ICMP6_PARAM_PROB: 50153541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob); 50253541Sshin switch (code) { 50353541Sshin case ICMP6_PARAMPROB_NEXTHEADER: 50453541Sshin code = PRC_UNREACH_PROTOCOL; 50553541Sshin break; 50653541Sshin case ICMP6_PARAMPROB_HEADER: 50753541Sshin case ICMP6_PARAMPROB_OPTION: 50853541Sshin code = PRC_PARAMPROB; 50953541Sshin break; 51053541Sshin default: 51153541Sshin goto badcode; 51253541Sshin } 51353541Sshin goto deliver; 51453541Sshin break; 51553541Sshin 51653541Sshin case ICMP6_ECHO_REQUEST: 51753541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo); 51853541Sshin if (code != 0) 51953541Sshin goto badcode; 52053541Sshin if ((n = m_copy(m, 0, M_COPYALL)) == NULL) { 52153541Sshin /* Give up remote */ 52253541Sshin break; 52353541Sshin } 52462587Sitojun if ((n->m_flags & M_EXT) != 0 52562587Sitojun || n->m_len < off + sizeof(struct icmp6_hdr)) { 52653541Sshin struct mbuf *n0 = n; 52762587Sitojun const int maxlen = sizeof(*nip6) + sizeof(*nicmp6); 528108741Ssam int n0len; 52953541Sshin 53053541Sshin /* 531120891Sume * Prepare an internal mbuf. m_pullup() doesn't 53253541Sshin * always copy the length we specified. 53353541Sshin */ 53462587Sitojun if (maxlen >= MCLBYTES) { 53562587Sitojun /* Give up remote */ 53662587Sitojun m_freem(n0); 53762587Sitojun break; 53862587Sitojun } 539111119Simp MGETHDR(n, M_DONTWAIT, n0->m_type); 540111070Ssam n0len = n0->m_pkthdr.len; /* save for use below */ 541111070Ssam if (n) 542111070Ssam M_MOVE_PKTHDR(n, n0); 54362587Sitojun if (n && maxlen >= MHLEN) { 544111119Simp MCLGET(n, M_DONTWAIT); 54562587Sitojun if ((n->m_flags & M_EXT) == 0) { 54662587Sitojun m_free(n); 54762587Sitojun n = NULL; 54862587Sitojun } 54962587Sitojun } 55053541Sshin if (n == NULL) { 55153541Sshin /* Give up remote */ 55253541Sshin m_freem(n0); 55353541Sshin break; 55453541Sshin } 55553541Sshin /* 55653541Sshin * Copy IPv6 and ICMPv6 only. 55753541Sshin */ 55853541Sshin nip6 = mtod(n, struct ip6_hdr *); 55953541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 56053541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 56153541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 56262587Sitojun noff = sizeof(struct ip6_hdr); 563108741Ssam /* new mbuf contains only ipv6+icmpv6 headers */ 564108741Ssam n->m_len = noff + sizeof(struct icmp6_hdr); 56553541Sshin /* 566120891Sume * Adjust mbuf. ip6_plen will be adjusted in 56762587Sitojun * ip6_output(). 56853541Sshin */ 56962587Sitojun m_adj(n0, off + sizeof(struct icmp6_hdr)); 570108741Ssam /* recalculate complete packet size */ 571108741Ssam n->m_pkthdr.len = n0len + (noff - off); 57262587Sitojun n->m_next = n0; 57353541Sshin } else { 57453541Sshin nip6 = mtod(n, struct ip6_hdr *); 57553541Sshin nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off); 57653541Sshin noff = off; 57753541Sshin } 57853541Sshin nicmp6->icmp6_type = ICMP6_ECHO_REPLY; 57953541Sshin nicmp6->icmp6_code = 0; 58053541Sshin if (n) { 58153541Sshin icmp6stat.icp6s_reflect++; 58253541Sshin icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++; 58353541Sshin icmp6_reflect(n, noff); 58453541Sshin } 58553541Sshin break; 58653541Sshin 58753541Sshin case ICMP6_ECHO_REPLY: 58853541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply); 58953541Sshin if (code != 0) 59053541Sshin goto badcode; 59153541Sshin break; 59253541Sshin 59396116Sume case MLD_LISTENER_QUERY: 59496116Sume case MLD_LISTENER_REPORT: 59596116Sume if (icmp6len < sizeof(struct mld_hdr)) 59653541Sshin goto badlen; 59796116Sume if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */ 59853541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery); 59953541Sshin else 60053541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport); 601111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 60262587Sitojun /* give up local */ 60362587Sitojun mld6_input(m, off); 60462587Sitojun m = NULL; 60562587Sitojun goto freeit; 60662587Sitojun } 60762587Sitojun mld6_input(n, off); 60853541Sshin /* m stays. */ 60953541Sshin break; 61053541Sshin 61196116Sume case MLD_LISTENER_DONE: 61253541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone); 61396116Sume if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */ 61453541Sshin goto badlen; 61553541Sshin break; /* nothing to be done in kernel */ 61653541Sshin 61796116Sume case MLD_MTRACE_RESP: 61896116Sume case MLD_MTRACE: 619120891Sume /* XXX: these two are experimental. not officially defined. */ 62053541Sshin /* XXX: per-interface statistics? */ 62162587Sitojun break; /* just pass it to applications */ 62253541Sshin 62353541Sshin case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ 62453541Sshin { 62553541Sshin enum { WRU, FQDN } mode; 62653541Sshin 62762587Sitojun if (!icmp6_nodeinfo) 62862587Sitojun break; 62962587Sitojun 63053541Sshin if (icmp6len == sizeof(struct icmp6_hdr) + 4) 63153541Sshin mode = WRU; 63262587Sitojun else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) 63353541Sshin mode = FQDN; 63453541Sshin else 63553541Sshin goto badlen; 63653541Sshin 63753541Sshin#define hostnamelen strlen(hostname) 63853541Sshin if (mode == FQDN) { 63962587Sitojun#ifndef PULLDOWN_TEST 64053541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), 641120891Sume IPPROTO_DONE); 64262587Sitojun#endif 64362587Sitojun n = m_copy(m, 0, M_COPYALL); 64462587Sitojun if (n) 64562587Sitojun n = ni6_input(n, off); 64662587Sitojun /* XXX meaningless if n == NULL */ 64753541Sshin noff = sizeof(struct ip6_hdr); 64853541Sshin } else { 64953541Sshin u_char *p; 65062587Sitojun int maxlen, maxhlen; 65153541Sshin 652120891Sume if ((icmp6_nodeinfo & 5) != 5) 65378064Sume break; 65478064Sume 65562587Sitojun if (code != 0) 65662587Sitojun goto badcode; 65762587Sitojun maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4; 65862587Sitojun if (maxlen >= MCLBYTES) { 65962587Sitojun /* Give up remote */ 66062587Sitojun break; 66162587Sitojun } 662111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 66362587Sitojun if (n && maxlen > MHLEN) { 664111119Simp MCLGET(n, M_DONTWAIT); 66562587Sitojun if ((n->m_flags & M_EXT) == 0) { 66662587Sitojun m_free(n); 66762587Sitojun n = NULL; 66862587Sitojun } 66962587Sitojun } 670111119Simp if (!m_dup_pkthdr(n, m, M_DONTWAIT)) { 671108466Ssam /* 672108466Ssam * Previous code did a blind M_COPY_PKTHDR 673108466Ssam * and said "just for rcvif". If true, then 674108466Ssam * we could tolerate the dup failing (due to 675108466Ssam * the deep copy of the tag chain). For now 676108466Ssam * be conservative and just fail. 677108466Ssam */ 678108466Ssam m_free(n); 679108466Ssam n = NULL; 680108466Ssam } 68153541Sshin if (n == NULL) { 68253541Sshin /* Give up remote */ 68353541Sshin break; 68453541Sshin } 68578064Sume n->m_pkthdr.rcvif = NULL; 68662587Sitojun n->m_len = 0; 68762587Sitojun maxhlen = M_TRAILINGSPACE(n) - maxlen; 68862587Sitojun if (maxhlen > hostnamelen) 68962587Sitojun maxhlen = hostnamelen; 69053541Sshin /* 69153541Sshin * Copy IPv6 and ICMPv6 only. 69253541Sshin */ 69353541Sshin nip6 = mtod(n, struct ip6_hdr *); 69453541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 69553541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 69653541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 69753541Sshin p = (u_char *)(nicmp6 + 1); 69853541Sshin bzero(p, 4); 69995023Ssuz bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */ 70053541Sshin noff = sizeof(struct ip6_hdr); 70153541Sshin n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 70262587Sitojun sizeof(struct icmp6_hdr) + 4 + maxhlen; 70353541Sshin nicmp6->icmp6_type = ICMP6_WRUREPLY; 70453541Sshin nicmp6->icmp6_code = 0; 70553541Sshin } 70653541Sshin#undef hostnamelen 70753541Sshin if (n) { 70853541Sshin icmp6stat.icp6s_reflect++; 70953541Sshin icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++; 71053541Sshin icmp6_reflect(n, noff); 71153541Sshin } 71253541Sshin break; 71353541Sshin } 71453541Sshin 71553541Sshin case ICMP6_WRUREPLY: 71653541Sshin if (code != 0) 71753541Sshin goto badcode; 71853541Sshin break; 71953541Sshin 72053541Sshin case ND_ROUTER_SOLICIT: 72153541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit); 72253541Sshin if (code != 0) 72353541Sshin goto badcode; 72453541Sshin if (icmp6len < sizeof(struct nd_router_solicit)) 72553541Sshin goto badlen; 726111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 72762587Sitojun /* give up local */ 72862587Sitojun nd6_rs_input(m, off, icmp6len); 72962587Sitojun m = NULL; 73062587Sitojun goto freeit; 73162587Sitojun } 73262587Sitojun nd6_rs_input(n, off, icmp6len); 73353541Sshin /* m stays. */ 73453541Sshin break; 73553541Sshin 73653541Sshin case ND_ROUTER_ADVERT: 73753541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert); 73853541Sshin if (code != 0) 73953541Sshin goto badcode; 74053541Sshin if (icmp6len < sizeof(struct nd_router_advert)) 74153541Sshin goto badlen; 742111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 74362587Sitojun /* give up local */ 74462587Sitojun nd6_ra_input(m, off, icmp6len); 74562587Sitojun m = NULL; 74662587Sitojun goto freeit; 74762587Sitojun } 74862587Sitojun nd6_ra_input(n, off, icmp6len); 74953541Sshin /* m stays. */ 75053541Sshin break; 75153541Sshin 75253541Sshin case ND_NEIGHBOR_SOLICIT: 75353541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit); 75453541Sshin if (code != 0) 75553541Sshin goto badcode; 75653541Sshin if (icmp6len < sizeof(struct nd_neighbor_solicit)) 75753541Sshin goto badlen; 758111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 75962587Sitojun /* give up local */ 76062587Sitojun nd6_ns_input(m, off, icmp6len); 76162587Sitojun m = NULL; 76262587Sitojun goto freeit; 76362587Sitojun } 76462587Sitojun nd6_ns_input(n, off, icmp6len); 76553541Sshin /* m stays. */ 76653541Sshin break; 76753541Sshin 76853541Sshin case ND_NEIGHBOR_ADVERT: 76953541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert); 77053541Sshin if (code != 0) 77153541Sshin goto badcode; 77253541Sshin if (icmp6len < sizeof(struct nd_neighbor_advert)) 77353541Sshin goto badlen; 774111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 77562587Sitojun /* give up local */ 77662587Sitojun nd6_na_input(m, off, icmp6len); 77762587Sitojun m = NULL; 77862587Sitojun goto freeit; 77962587Sitojun } 78062587Sitojun nd6_na_input(n, off, icmp6len); 78153541Sshin /* m stays. */ 78253541Sshin break; 78353541Sshin 78453541Sshin case ND_REDIRECT: 78553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect); 78653541Sshin if (code != 0) 78753541Sshin goto badcode; 78853541Sshin if (icmp6len < sizeof(struct nd_redirect)) 78953541Sshin goto badlen; 790111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 79162587Sitojun /* give up local */ 79262587Sitojun icmp6_redirect_input(m, off); 79362587Sitojun m = NULL; 79462587Sitojun goto freeit; 79562587Sitojun } 79662587Sitojun icmp6_redirect_input(n, off); 79753541Sshin /* m stays. */ 79853541Sshin break; 79953541Sshin 80053541Sshin case ICMP6_ROUTER_RENUMBERING: 80153541Sshin if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && 80253541Sshin code != ICMP6_ROUTER_RENUMBERING_RESULT) 80353541Sshin goto badcode; 80453541Sshin if (icmp6len < sizeof(struct icmp6_router_renum)) 80553541Sshin goto badlen; 80653541Sshin break; 80753541Sshin 80853541Sshin default: 80978064Sume nd6log((LOG_DEBUG, 81078064Sume "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", 81178064Sume icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src), 81278064Sume ip6_sprintf(&ip6->ip6_dst), 81378064Sume m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0)); 81453541Sshin if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { 81553541Sshin /* ICMPv6 error: MUST deliver it by spec... */ 81653541Sshin code = PRC_NCMDS; 81753541Sshin /* deliver */ 81853541Sshin } else { 81953541Sshin /* ICMPv6 informational: MUST not deliver */ 82053541Sshin break; 82153541Sshin } 82253541Sshin deliver: 82378064Sume if (icmp6_notify_error(m, off, icmp6len, code)) { 82478064Sume /* In this case, m should've been freed. */ 825120856Sume return (IPPROTO_DONE); 82653541Sshin } 82778064Sume break; 82878064Sume 82978064Sume badcode: 83078064Sume icmp6stat.icp6s_badcode++; 83178064Sume break; 83278064Sume 83378064Sume badlen: 83478064Sume icmp6stat.icp6s_badlen++; 83578064Sume break; 83678064Sume } 83778064Sume 83878064Sume /* deliver the packet to appropriate sockets */ 83978064Sume icmp6_rip6_input(&m, *offp); 84078064Sume 84178064Sume return IPPROTO_DONE; 84278064Sume 84378064Sume freeit: 84478064Sume m_freem(m); 84578064Sume return IPPROTO_DONE; 84678064Sume} 84778064Sume 84878064Sumestatic int 84978064Sumeicmp6_notify_error(m, off, icmp6len, code) 85078064Sume struct mbuf *m; 851113799Sobrien int off, icmp6len, code; 85278064Sume{ 85378064Sume struct icmp6_hdr *icmp6; 85478064Sume struct ip6_hdr *eip6; 85578064Sume u_int32_t notifymtu; 85678064Sume struct sockaddr_in6 icmp6src, icmp6dst; 85778064Sume 85878064Sume if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { 85978064Sume icmp6stat.icp6s_tooshort++; 86078064Sume goto freeit; 86178064Sume } 86262587Sitojun#ifndef PULLDOWN_TEST 86378064Sume IP6_EXTHDR_CHECK(m, off, 864120891Sume sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1); 86578064Sume icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 86662587Sitojun#else 86778064Sume IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 868120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 86978064Sume if (icmp6 == NULL) { 87078064Sume icmp6stat.icp6s_tooshort++; 871120856Sume return (-1); 87278064Sume } 87362587Sitojun#endif 87478064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 87553541Sshin 87678064Sume /* Detect the upper level protocol */ 87778064Sume { 87853541Sshin void (*ctlfunc) __P((int, struct sockaddr *, void *)); 87953541Sshin u_int8_t nxt = eip6->ip6_nxt; 88053541Sshin int eoff = off + sizeof(struct icmp6_hdr) + 881120891Sume sizeof(struct ip6_hdr); 88253541Sshin struct ip6ctlparam ip6cp; 88362587Sitojun struct in6_addr *finaldst = NULL; 88462587Sitojun int icmp6type = icmp6->icmp6_type; 88562587Sitojun struct ip6_frag *fh; 88662587Sitojun struct ip6_rthdr *rth; 88762587Sitojun struct ip6_rthdr0 *rth0; 88862587Sitojun int rthlen; 88953541Sshin 89095023Ssuz while (1) { /* XXX: should avoid infinite loop explicitly? */ 89153541Sshin struct ip6_ext *eh; 89253541Sshin 89378064Sume switch (nxt) { 89453541Sshin case IPPROTO_HOPOPTS: 89553541Sshin case IPPROTO_DSTOPTS: 89653541Sshin case IPPROTO_AH: 89762587Sitojun#ifndef PULLDOWN_TEST 898120891Sume IP6_EXTHDR_CHECK(m, 0, 899120891Sume eoff + sizeof(struct ip6_ext), -1); 900120892Sume eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff); 90162587Sitojun#else 90262587Sitojun IP6_EXTHDR_GET(eh, struct ip6_ext *, m, 903120891Sume eoff, sizeof(*eh)); 90462587Sitojun if (eh == NULL) { 90562587Sitojun icmp6stat.icp6s_tooshort++; 906120856Sume return (-1); 90762587Sitojun } 90862587Sitojun#endif 909120891Sume 91053541Sshin if (nxt == IPPROTO_AH) 91153541Sshin eoff += (eh->ip6e_len + 2) << 2; 91253541Sshin else 91353541Sshin eoff += (eh->ip6e_len + 1) << 3; 91453541Sshin nxt = eh->ip6e_nxt; 91553541Sshin break; 91662587Sitojun case IPPROTO_ROUTING: 91762587Sitojun /* 91862587Sitojun * When the erroneous packet contains a 91962587Sitojun * routing header, we should examine the 92062587Sitojun * header to determine the final destination. 92162587Sitojun * Otherwise, we can't properly update 92262587Sitojun * information that depends on the final 92362587Sitojun * destination (e.g. path MTU). 92462587Sitojun */ 92562587Sitojun#ifndef PULLDOWN_TEST 926120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1); 927120891Sume rth = (struct ip6_rthdr *) 928120891Sume (mtod(m, caddr_t) + eoff); 92962587Sitojun#else 93062587Sitojun IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m, 931120891Sume eoff, sizeof(*rth)); 93262587Sitojun if (rth == NULL) { 93362587Sitojun icmp6stat.icp6s_tooshort++; 934120856Sume return (-1); 93562587Sitojun } 93662587Sitojun#endif 93762587Sitojun rthlen = (rth->ip6r_len + 1) << 3; 93862587Sitojun /* 93962587Sitojun * XXX: currently there is no 94062587Sitojun * officially defined type other 94162587Sitojun * than type-0. 94262587Sitojun * Note that if the segment left field 94362587Sitojun * is 0, all intermediate hops must 94462587Sitojun * have been passed. 94562587Sitojun */ 94662587Sitojun if (rth->ip6r_segleft && 94762587Sitojun rth->ip6r_type == IPV6_RTHDR_TYPE_0) { 94862587Sitojun int hops; 94962587Sitojun 95062587Sitojun#ifndef PULLDOWN_TEST 951120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1); 952120891Sume rth0 = (struct ip6_rthdr0 *) 953120891Sume (mtod(m, caddr_t) + eoff); 95462587Sitojun#else 95562587Sitojun IP6_EXTHDR_GET(rth0, 956120891Sume struct ip6_rthdr0 *, m, 957120891Sume eoff, rthlen); 95862587Sitojun if (rth0 == NULL) { 95962587Sitojun icmp6stat.icp6s_tooshort++; 960120856Sume return (-1); 96162587Sitojun } 96262587Sitojun#endif 96362587Sitojun /* just ignore a bogus header */ 96462587Sitojun if ((rth0->ip6r0_len % 2) == 0 && 96562587Sitojun (hops = rth0->ip6r0_len/2)) 96662587Sitojun finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1); 96762587Sitojun } 96862587Sitojun eoff += rthlen; 96962587Sitojun nxt = rth->ip6r_nxt; 97062587Sitojun break; 97162587Sitojun case IPPROTO_FRAGMENT: 97262587Sitojun#ifndef PULLDOWN_TEST 97362587Sitojun IP6_EXTHDR_CHECK(m, 0, eoff + 974120891Sume sizeof(struct ip6_frag), -1); 975120891Sume fh = (struct ip6_frag *)(mtod(m, caddr_t) + 976120891Sume eoff); 97762587Sitojun#else 97862587Sitojun IP6_EXTHDR_GET(fh, struct ip6_frag *, m, 979120891Sume eoff, sizeof(*fh)); 98062587Sitojun if (fh == NULL) { 98162587Sitojun icmp6stat.icp6s_tooshort++; 982120856Sume return (-1); 98362587Sitojun } 98462587Sitojun#endif 98562587Sitojun /* 98662587Sitojun * Data after a fragment header is meaningless 98762587Sitojun * unless it is the first fragment, but 98862587Sitojun * we'll go to the notify label for path MTU 98962587Sitojun * discovery. 99062587Sitojun */ 99162587Sitojun if (fh->ip6f_offlg & IP6F_OFF_MASK) 99262587Sitojun goto notify; 99362587Sitojun 99462587Sitojun eoff += sizeof(struct ip6_frag); 99562587Sitojun nxt = fh->ip6f_nxt; 99662587Sitojun break; 99753541Sshin default: 99862587Sitojun /* 99962587Sitojun * This case includes ESP and the No Next 100095023Ssuz * Header. In such cases going to the notify 100162587Sitojun * label does not have any meaning 100262587Sitojun * (i.e. ctlfunc will be NULL), but we go 100362587Sitojun * anyway since we might have to update 100462587Sitojun * path MTU information. 100562587Sitojun */ 100653541Sshin goto notify; 100753541Sshin } 100853541Sshin } 100978064Sume notify: 101062587Sitojun#ifndef PULLDOWN_TEST 101153541Sshin icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 101262587Sitojun#else 101362587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 1014120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 101562587Sitojun if (icmp6 == NULL) { 101662587Sitojun icmp6stat.icp6s_tooshort++; 1017120856Sume return (-1); 101862587Sitojun } 101962587Sitojun#endif 102078064Sume 1021120891Sume /* 1022120891Sume * retrieve parameters from the inner IPv6 header, and convert 1023120891Sume * them into sockaddr structures. 1024120891Sume * XXX: there is no guarantee that the source or destination 1025120891Sume * addresses of the inner packet are in the same scope as 1026120891Sume * the addresses of the icmp packet. But there is no other 1027120891Sume * way to determine the zone. 1028120891Sume */ 102978064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 1030120891Sume 103178064Sume bzero(&icmp6dst, sizeof(icmp6dst)); 103278064Sume icmp6dst.sin6_len = sizeof(struct sockaddr_in6); 103378064Sume icmp6dst.sin6_family = AF_INET6; 103478064Sume if (finaldst == NULL) 103578064Sume icmp6dst.sin6_addr = eip6->ip6_dst; 103678064Sume else 103778064Sume icmp6dst.sin6_addr = *finaldst; 1038121315Sume if (in6_addr2zoneid(m->m_pkthdr.rcvif, &icmp6dst.sin6_addr, 1039121315Sume &icmp6dst.sin6_scope_id)) 1040121315Sume goto freeit; 104178064Sume if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst, 104278064Sume NULL, NULL)) { 104378064Sume /* should be impossbile */ 104478064Sume nd6log((LOG_DEBUG, 104578064Sume "icmp6_notify_error: in6_embedscope failed\n")); 104678064Sume goto freeit; 104778064Sume } 104878064Sume 104978064Sume /* 105078064Sume * retrieve parameters from the inner IPv6 header, and convert 105178064Sume * them into sockaddr structures. 105278064Sume */ 105378064Sume bzero(&icmp6src, sizeof(icmp6src)); 105478064Sume icmp6src.sin6_len = sizeof(struct sockaddr_in6); 105578064Sume icmp6src.sin6_family = AF_INET6; 105678064Sume icmp6src.sin6_addr = eip6->ip6_src; 1057121315Sume if (in6_addr2zoneid(m->m_pkthdr.rcvif, &icmp6src.sin6_addr, 1058121315Sume &icmp6src.sin6_scope_id)) { 1059121315Sume goto freeit; 1060121315Sume } 106178064Sume if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src, 106278064Sume NULL, NULL)) { 106378064Sume /* should be impossbile */ 106478064Sume nd6log((LOG_DEBUG, 106578064Sume "icmp6_notify_error: in6_embedscope failed\n")); 106678064Sume goto freeit; 106778064Sume } 1068120891Sume icmp6src.sin6_flowinfo = (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); 106978064Sume 107078064Sume if (finaldst == NULL) 107178064Sume finaldst = &eip6->ip6_dst; 107278064Sume ip6cp.ip6c_m = m; 107378064Sume ip6cp.ip6c_icmp6 = icmp6; 107478064Sume ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1); 107578064Sume ip6cp.ip6c_off = eoff; 107678064Sume ip6cp.ip6c_finaldst = finaldst; 107778064Sume ip6cp.ip6c_src = &icmp6src; 107878064Sume ip6cp.ip6c_nxt = nxt; 107978064Sume 108062587Sitojun if (icmp6type == ICMP6_PACKET_TOO_BIG) { 108178064Sume notifymtu = ntohl(icmp6->icmp6_mtu); 108278064Sume ip6cp.ip6c_cmdarg = (void *)¬ifymtu; 108378064Sume icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ 108462587Sitojun } 108562587Sitojun 108653541Sshin ctlfunc = (void (*) __P((int, struct sockaddr *, void *))) 1087120891Sume (inet6sw[ip6_protox[nxt]].pr_ctlinput); 108853541Sshin if (ctlfunc) { 108978064Sume (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, 1090120891Sume &ip6cp); 109153541Sshin } 109253541Sshin } 1093120856Sume return (0); 109453541Sshin 109578064Sume freeit: 109653541Sshin m_freem(m); 1097120856Sume return (-1); 109853541Sshin} 109953541Sshin 110078064Sumevoid 110178064Sumeicmp6_mtudisc_update(ip6cp, validated) 110278064Sume struct ip6ctlparam *ip6cp; 110378064Sume int validated; 110462587Sitojun{ 110578064Sume struct in6_addr *dst = ip6cp->ip6c_finaldst; 110678064Sume struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; 110778064Sume struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ 110862587Sitojun u_int mtu = ntohl(icmp6->icmp6_mtu); 1109122922Sandre struct in_conninfo inc; 111062587Sitojun 1111121472Sume#if 0 1112121472Sume /* 1113121472Sume * RFC2460 section 5, last paragraph. 1114121472Sume * even though minimum link MTU for IPv6 is IPV6_MMTU, 1115121472Sume * we may see ICMPv6 too big with mtu < IPV6_MMTU 1116121472Sume * due to packet translator in the middle. 1117121472Sume * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for 1118121472Sume * special handling. 1119121472Sume */ 1120121472Sume if (mtu < IPV6_MMTU) 1121121472Sume return; 1122121472Sume#endif 1123121472Sume 1124121472Sume /* 1125121472Sume * we reject ICMPv6 too big with abnormally small value. 1126121472Sume * XXX what is the good definition of "abnormally small"? 1127121472Sume */ 1128121472Sume if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) 1129121472Sume return; 1130121472Sume 113178064Sume if (!validated) 113278064Sume return; 113378064Sume 1134122922Sandre bzero(&inc, sizeof(inc)); 1135122922Sandre inc.inc_flags = 1; /* IPv6 */ 1136122922Sandre inc.inc6_faddr = *dst; 113778064Sume /* XXX normally, this won't happen */ 113878064Sume if (IN6_IS_ADDR_LINKLOCAL(dst)) { 1139122922Sandre inc.inc6_faddr.s6_addr16[1] = 114078064Sume htons(m->m_pkthdr.rcvif->if_index); 114178064Sume } 114262587Sitojun 1143122922Sandre if (mtu >= IPV6_MMTU) { 1144122922Sandre tcp_hc_updatemtu(&inc, mtu); 1145122922Sandre icmp6stat.icp6s_pmtuchg++; 114662587Sitojun } 114762587Sitojun} 114862587Sitojun 114953541Sshin/* 115078064Sume * Process a Node Information Query packet, based on 115178064Sume * draft-ietf-ipngwg-icmp-name-lookups-07. 1152120891Sume * 115362587Sitojun * Spec incompatibilities: 115462587Sitojun * - IPv6 Subject address handling 115562587Sitojun * - IPv4 Subject address handling support missing 115662587Sitojun * - Proxy reply (answer even if it's not for me) 115762587Sitojun * - joins NI group address at in6_ifattach() time only, does not cope 115862587Sitojun * with hostname changes by sethostname(3) 115953541Sshin */ 116062587Sitojun#define hostnamelen strlen(hostname) 116153541Sshinstatic struct mbuf * 116253541Sshinni6_input(m, off) 116353541Sshin struct mbuf *m; 116453541Sshin int off; 116553541Sshin{ 116662587Sitojun struct icmp6_nodeinfo *ni6, *nni6; 116753541Sshin struct mbuf *n = NULL; 116862587Sitojun u_int16_t qtype; 116962587Sitojun int subjlen; 117053541Sshin int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 117153541Sshin struct ni_reply_fqdn *fqdn; 117253541Sshin int addrs; /* for NI_QTYPE_NODEADDR */ 117353541Sshin struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ 1174121630Sume struct sockaddr_in6 sin6_sbj; /* subject address */ 1175121630Sume struct sockaddr_in6 sin6_d; 117662587Sitojun struct ip6_hdr *ip6; 117762587Sitojun int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ 117878064Sume char *subj = NULL; 117978064Sume struct in6_ifaddr *ia6 = NULL; 118053541Sshin 118162587Sitojun ip6 = mtod(m, struct ip6_hdr *); 118262587Sitojun#ifndef PULLDOWN_TEST 118362587Sitojun ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); 118462587Sitojun#else 118562587Sitojun IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); 118662587Sitojun if (ni6 == NULL) { 118762587Sitojun /* m is already reclaimed */ 1188120891Sume return (NULL); 118962587Sitojun } 119062587Sitojun#endif 119153541Sshin 1192121630Sume bzero(&sin6_d, sizeof(sin6_d)); 1193121630Sume sin6_d.sin6_family = AF_INET6; /* not used, actually */ 1194121630Sume sin6_d.sin6_len = sizeof(sin6_d); /* ditto */ 1195121630Sume sin6_d.sin6_addr = ip6->ip6_dst; 1196121630Sume if (in6_addr2zoneid(m->m_pkthdr.rcvif, &ip6->ip6_dst, 1197121630Sume &sin6_d.sin6_scope_id)) { 1198121630Sume goto bad; 1199121630Sume } 1200121630Sume if (in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL)) 1201121630Sume goto bad; /* XXX should not happen */ 1202121630Sume 120362587Sitojun /* 120462587Sitojun * Validate IPv6 destination address. 120562587Sitojun * 120678064Sume * The Responder must discard the Query without further processing 120778064Sume * unless it is one of the Responder's unicast or anycast addresses, or 120878064Sume * a link-local scope multicast address which the Responder has joined. 1209121630Sume * [icmp-name-lookups-08, Section 4.] 121062587Sitojun */ 1211121630Sume if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1212121630Sume if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) 1213121630Sume goto bad; 1214121630Sume /* else it's a link-local multicast, fine */ 1215121630Sume } else { /* unicast or anycast */ 1216121630Sume if ((ia6 = ip6_getdstifaddr(m)) == NULL) 1217121630Sume goto bad; /* XXX impossible */ 1218121630Sume 1219121630Sume if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) && 1220121630Sume !(icmp6_nodeinfo & 4)) { 122178064Sume nd6log((LOG_DEBUG, "ni6_input: ignore node info to " 122278064Sume "a temporary address in %s:%d", 122378064Sume __FILE__, __LINE__)); 122478064Sume goto bad; 122578064Sume } 1226121630Sume } 122762587Sitojun 122878064Sume /* validate query Subject field. */ 122962587Sitojun qtype = ntohs(ni6->ni_qtype); 123062587Sitojun subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); 123162587Sitojun switch (qtype) { 123262587Sitojun case NI_QTYPE_NOOP: 123362587Sitojun case NI_QTYPE_SUPTYPES: 123478064Sume /* 07 draft */ 123578064Sume if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) 123678064Sume break; 123778064Sume /* FALLTHROUGH */ 123862587Sitojun case NI_QTYPE_FQDN: 123962587Sitojun case NI_QTYPE_NODEADDR: 124062587Sitojun switch (ni6->ni_code) { 124162587Sitojun case ICMP6_NI_SUBJ_IPV6: 124262587Sitojun#if ICMP6_NI_SUBJ_IPV6 != 0 124362587Sitojun case 0: 124462587Sitojun#endif 124562587Sitojun /* 124662587Sitojun * backward compatibility - try to accept 03 draft 124762587Sitojun * format, where no Subject is present. 124862587Sitojun */ 124978064Sume if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && 125078064Sume subjlen == 0) { 125162587Sitojun oldfqdn++; 125262587Sitojun break; 125362587Sitojun } 125478064Sume#if ICMP6_NI_SUBJ_IPV6 != 0 125578064Sume if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) 125678064Sume goto bad; 125778064Sume#endif 125862587Sitojun 1259121630Sume if (subjlen != sizeof(struct in6_addr)) 126062587Sitojun goto bad; 126162587Sitojun 126262587Sitojun /* 126362587Sitojun * Validate Subject address. 126462587Sitojun * 126578064Sume * Not sure what exactly "address belongs to the node" 126678064Sume * means in the spec, is it just unicast, or what? 126762587Sitojun * 126862587Sitojun * At this moment we consider Subject address as 126962587Sitojun * "belong to the node" if the Subject address equals 127062587Sitojun * to the IPv6 destination address; validation for 127162587Sitojun * IPv6 destination address should have done enough 127262587Sitojun * check for us. 127362587Sitojun * 127462587Sitojun * We do not do proxy at this moment. 127562587Sitojun */ 127662587Sitojun /* m_pulldown instead of copy? */ 1277121630Sume bzero(&sin6_sbj, sizeof(sin6_sbj)); 1278121630Sume sin6_sbj.sin6_family = AF_INET6; 1279121630Sume sin6_sbj.sin6_len = sizeof(sin6_sbj); 128062587Sitojun m_copydata(m, off + sizeof(struct icmp6_nodeinfo), 1281121630Sume subjlen, (caddr_t)&sin6_sbj.sin6_addr); 1282121315Sume if (in6_addr2zoneid(m->m_pkthdr.rcvif, 1283121630Sume &sin6_sbj.sin6_addr, &sin6_sbj.sin6_scope_id)) { 1284121315Sume goto bad; 1285121315Sume } 1286121630Sume if (in6_embedscope(&sin6_sbj.sin6_addr, &sin6_sbj, 1287121630Sume NULL, NULL)) 1288121630Sume goto bad; /* XXX should not happen */ 1289121630Sume 1290121630Sume subj = (char *)&sin6_sbj; 1291121630Sume if (SA6_ARE_ADDR_EQUAL(&sin6_sbj, &sin6_d)) 129262587Sitojun break; 129378064Sume 129462587Sitojun /* 129562587Sitojun * XXX if we are to allow other cases, we should really 129662587Sitojun * be careful about scope here. 129762587Sitojun * basically, we should disallow queries toward IPv6 1298120891Sume * destination X with subject Y, 1299120891Sume * if scope(X) > scope(Y). 130062587Sitojun * if we allow scope(X) > scope(Y), it will result in 130162587Sitojun * information leakage across scope boundary. 130262587Sitojun */ 130362587Sitojun goto bad; 130462587Sitojun 130562587Sitojun case ICMP6_NI_SUBJ_FQDN: 130662587Sitojun /* 130762587Sitojun * Validate Subject name with gethostname(3). 130862587Sitojun * 130962587Sitojun * The behavior may need some debate, since: 131062587Sitojun * - we are not sure if the node has FQDN as 131162587Sitojun * hostname (returned by gethostname(3)). 131262587Sitojun * - the code does wildcard match for truncated names. 131362587Sitojun * however, we are not sure if we want to perform 131462587Sitojun * wildcard match, if gethostname(3) side has 131562587Sitojun * truncated hostname. 131662587Sitojun */ 131762587Sitojun n = ni6_nametodns(hostname, hostnamelen, 0); 131862587Sitojun if (!n || n->m_next || n->m_len == 0) 131962587Sitojun goto bad; 132062587Sitojun IP6_EXTHDR_GET(subj, char *, m, 132162587Sitojun off + sizeof(struct icmp6_nodeinfo), subjlen); 132262587Sitojun if (subj == NULL) 132362587Sitojun goto bad; 132462587Sitojun if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 1325120891Sume n->m_len)) { 132662587Sitojun goto bad; 132762587Sitojun } 132862587Sitojun m_freem(n); 132962587Sitojun n = NULL; 133062587Sitojun break; 133162587Sitojun 133278064Sume case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 133362587Sitojun default: 133462587Sitojun goto bad; 133562587Sitojun } 133662587Sitojun break; 133778064Sume } 133862587Sitojun 133978064Sume /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 134078064Sume switch (qtype) { 134178064Sume case NI_QTYPE_FQDN: 134278064Sume if ((icmp6_nodeinfo & 1) == 0) 134378064Sume goto bad; 134478064Sume break; 134578064Sume case NI_QTYPE_NODEADDR: 134678064Sume if ((icmp6_nodeinfo & 2) == 0) 134778064Sume goto bad; 134878064Sume break; 134978064Sume } 135078064Sume 135178064Sume /* guess reply length */ 135278064Sume switch (qtype) { 135378064Sume case NI_QTYPE_NOOP: 135478064Sume break; /* no reply data */ 135578064Sume case NI_QTYPE_SUPTYPES: 135678064Sume replylen += sizeof(u_int32_t); 135778064Sume break; 135878064Sume case NI_QTYPE_FQDN: 135978064Sume /* XXX will append an mbuf */ 136078064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 136178064Sume break; 136278064Sume case NI_QTYPE_NODEADDR: 136378064Sume addrs = ni6_addrs(ni6, m, &ifp, subj); 136478064Sume if ((replylen += addrs * (sizeof(struct in6_addr) + 1365120891Sume sizeof(u_int32_t))) > MCLBYTES) 136678064Sume replylen = MCLBYTES; /* XXX: will truncate pkt later */ 136778064Sume break; 136862587Sitojun default: 136978064Sume /* 137078064Sume * XXX: We must return a reply with the ICMP6 code 1371120891Sume * `unknown Qtype' in this case. However we regard the case 137278064Sume * as an FQDN query for backward compatibility. 137378064Sume * Older versions set a random value to this field, 137478064Sume * so it rarely varies in the defined qtypes. 137578064Sume * But the mechanism is not reliable... 137678064Sume * maybe we should obsolete older versions. 137778064Sume */ 137878064Sume qtype = NI_QTYPE_FQDN; 137978064Sume /* XXX will append an mbuf */ 138078064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 138178064Sume oldfqdn++; 138278064Sume break; 138362587Sitojun } 138462587Sitojun 138578064Sume /* allocate an mbuf to reply. */ 1386111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 138762587Sitojun if (n == NULL) { 138862587Sitojun m_freem(m); 1389120856Sume return (NULL); 139062587Sitojun } 1391108466Ssam M_MOVE_PKTHDR(n, m); /* just for recvif */ 139253541Sshin if (replylen > MHLEN) { 139362587Sitojun if (replylen > MCLBYTES) { 139478064Sume /* 139578064Sume * XXX: should we try to allocate more? But MCLBYTES 139678064Sume * is probably much larger than IPV6_MMTU... 139778064Sume */ 139853541Sshin goto bad; 139962587Sitojun } 1400111119Simp MCLGET(n, M_DONTWAIT); 140153541Sshin if ((n->m_flags & M_EXT) == 0) { 140253541Sshin goto bad; 140353541Sshin } 140453541Sshin } 140553541Sshin n->m_pkthdr.len = n->m_len = replylen; 140653541Sshin 140753541Sshin /* copy mbuf header and IPv6 + Node Information base headers */ 140853541Sshin bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); 140953541Sshin nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 141062587Sitojun bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); 141153541Sshin 141253541Sshin /* qtype dependent procedure */ 141353541Sshin switch (qtype) { 141462587Sitojun case NI_QTYPE_NOOP: 141578064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 141662587Sitojun nni6->ni_flags = 0; 141762587Sitojun break; 141862587Sitojun case NI_QTYPE_SUPTYPES: 141978064Sume { 142078064Sume u_int32_t v; 142178064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 142278064Sume nni6->ni_flags = htons(0x0000); /* raw bitmap */ 142378064Sume /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 142478064Sume v = (u_int32_t)htonl(0x0000000f); 142578064Sume bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 142662587Sitojun break; 142778064Sume } 142862587Sitojun case NI_QTYPE_FQDN: 142978064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 143062587Sitojun fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + 1431120891Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo)); 143262587Sitojun nni6->ni_flags = 0; /* XXX: meaningless TTL */ 143362587Sitojun fqdn->ni_fqdn_ttl = 0; /* ditto. */ 143462587Sitojun /* 143562587Sitojun * XXX do we really have FQDN in variable "hostname"? 143662587Sitojun */ 143762587Sitojun n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn); 143862587Sitojun if (n->m_next == NULL) 143962587Sitojun goto bad; 144062587Sitojun /* XXX we assume that n->m_next is not a chain */ 144162587Sitojun if (n->m_next->m_next != NULL) 144262587Sitojun goto bad; 144362587Sitojun n->m_pkthdr.len += n->m_next->m_len; 144462587Sitojun break; 144562587Sitojun case NI_QTYPE_NODEADDR: 144662587Sitojun { 144762587Sitojun int lenlim, copied; 144853541Sshin 144978064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 145078064Sume n->m_pkthdr.len = n->m_len = 145178064Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 145278064Sume lenlim = M_TRAILINGSPACE(n); 145362587Sitojun copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 145462587Sitojun /* XXX: reset mbuf length */ 145562587Sitojun n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 1456120891Sume sizeof(struct icmp6_nodeinfo) + copied; 145762587Sitojun break; 145853541Sshin } 145962587Sitojun default: 146062587Sitojun break; /* XXX impossible! */ 146162587Sitojun } 146253541Sshin 146353541Sshin nni6->ni_type = ICMP6_NI_REPLY; 146462587Sitojun m_freem(m); 1465120856Sume return (n); 146653541Sshin 146753541Sshin bad: 146862587Sitojun m_freem(m); 146953541Sshin if (n) 147053541Sshin m_freem(n); 1471120856Sume return (NULL); 147253541Sshin} 147353541Sshin#undef hostnamelen 147453541Sshin 147553541Sshin/* 147662587Sitojun * make a mbuf with DNS-encoded string. no compression support. 147762587Sitojun * 147862587Sitojun * XXX names with less than 2 dots (like "foo" or "foo.section") will be 147962587Sitojun * treated as truncated name (two \0 at the end). this is a wild guess. 148062587Sitojun */ 148162587Sitojunstatic struct mbuf * 148262587Sitojunni6_nametodns(name, namelen, old) 148362587Sitojun const char *name; 148462587Sitojun int namelen; 148562587Sitojun int old; /* return pascal string if non-zero */ 148662587Sitojun{ 148762587Sitojun struct mbuf *m; 148862587Sitojun char *cp, *ep; 148962587Sitojun const char *p, *q; 149062587Sitojun int i, len, nterm; 149162587Sitojun 149262587Sitojun if (old) 149362587Sitojun len = namelen + 1; 149462587Sitojun else 149562587Sitojun len = MCLBYTES; 149662587Sitojun 149762587Sitojun /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ 1498111119Simp MGET(m, M_DONTWAIT, MT_DATA); 149962587Sitojun if (m && len > MLEN) { 1500111119Simp MCLGET(m, M_DONTWAIT); 150162587Sitojun if ((m->m_flags & M_EXT) == 0) 150262587Sitojun goto fail; 150362587Sitojun } 150462587Sitojun if (!m) 150562587Sitojun goto fail; 150662587Sitojun m->m_next = NULL; 150762587Sitojun 150862587Sitojun if (old) { 150962587Sitojun m->m_len = len; 151062587Sitojun *mtod(m, char *) = namelen; 151162587Sitojun bcopy(name, mtod(m, char *) + 1, namelen); 151262587Sitojun return m; 151362587Sitojun } else { 151462587Sitojun m->m_len = 0; 151562587Sitojun cp = mtod(m, char *); 151662587Sitojun ep = mtod(m, char *) + M_TRAILINGSPACE(m); 151762587Sitojun 151862587Sitojun /* if not certain about my name, return empty buffer */ 151962587Sitojun if (namelen == 0) 152062587Sitojun return m; 152162587Sitojun 152262587Sitojun /* 152362587Sitojun * guess if it looks like shortened hostname, or FQDN. 152462587Sitojun * shortened hostname needs two trailing "\0". 152562587Sitojun */ 152662587Sitojun i = 0; 152762587Sitojun for (p = name; p < name + namelen; p++) { 152862587Sitojun if (*p && *p == '.') 152962587Sitojun i++; 153062587Sitojun } 153162587Sitojun if (i < 2) 153262587Sitojun nterm = 2; 153362587Sitojun else 153462587Sitojun nterm = 1; 153562587Sitojun 153662587Sitojun p = name; 153762587Sitojun while (cp < ep && p < name + namelen) { 153862587Sitojun i = 0; 153962587Sitojun for (q = p; q < name + namelen && *q && *q != '.'; q++) 154062587Sitojun i++; 154162587Sitojun /* result does not fit into mbuf */ 154262587Sitojun if (cp + i + 1 >= ep) 154362587Sitojun goto fail; 154478704Sume /* 154578704Sume * DNS label length restriction, RFC1035 page 8. 154678704Sume * "i == 0" case is included here to avoid returning 154778704Sume * 0-length label on "foo..bar". 154878704Sume */ 154978704Sume if (i <= 0 || i >= 64) 155062587Sitojun goto fail; 155162587Sitojun *cp++ = i; 155262587Sitojun bcopy(p, cp, i); 155362587Sitojun cp += i; 155462587Sitojun p = q; 155562587Sitojun if (p < name + namelen && *p == '.') 155662587Sitojun p++; 155762587Sitojun } 155862587Sitojun /* termination */ 155962587Sitojun if (cp + nterm >= ep) 156062587Sitojun goto fail; 156162587Sitojun while (nterm-- > 0) 156262587Sitojun *cp++ = '\0'; 156362587Sitojun m->m_len = cp - mtod(m, char *); 156462587Sitojun return m; 156562587Sitojun } 156662587Sitojun 156762587Sitojun panic("should not reach here"); 156895023Ssuz /* NOTREACHED */ 156962587Sitojun 157062587Sitojun fail: 157162587Sitojun if (m) 157262587Sitojun m_freem(m); 157362587Sitojun return NULL; 157462587Sitojun} 157562587Sitojun 157662587Sitojun/* 157762587Sitojun * check if two DNS-encoded string matches. takes care of truncated 157862587Sitojun * form (with \0\0 at the end). no compression support. 157978064Sume * XXX upper/lowercase match (see RFC2065) 158062587Sitojun */ 158162587Sitojunstatic int 158262587Sitojunni6_dnsmatch(a, alen, b, blen) 158362587Sitojun const char *a; 158462587Sitojun int alen; 158562587Sitojun const char *b; 158662587Sitojun int blen; 158762587Sitojun{ 158862587Sitojun const char *a0, *b0; 158962587Sitojun int l; 159062587Sitojun 159162587Sitojun /* simplest case - need validation? */ 159262587Sitojun if (alen == blen && bcmp(a, b, alen) == 0) 159362587Sitojun return 1; 159462587Sitojun 159562587Sitojun a0 = a; 159662587Sitojun b0 = b; 159762587Sitojun 159862587Sitojun /* termination is mandatory */ 159962587Sitojun if (alen < 2 || blen < 2) 160062587Sitojun return 0; 160162587Sitojun if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 160262587Sitojun return 0; 160362587Sitojun alen--; 160462587Sitojun blen--; 160562587Sitojun 160662587Sitojun while (a - a0 < alen && b - b0 < blen) { 160762587Sitojun if (a - a0 + 1 > alen || b - b0 + 1 > blen) 160862587Sitojun return 0; 160962587Sitojun 161062587Sitojun if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 161162587Sitojun return 0; 161262587Sitojun /* we don't support compression yet */ 161362587Sitojun if (a[0] >= 64 || b[0] >= 64) 161462587Sitojun return 0; 161562587Sitojun 161662587Sitojun /* truncated case */ 161762587Sitojun if (a[0] == 0 && a - a0 == alen - 1) 161862587Sitojun return 1; 161962587Sitojun if (b[0] == 0 && b - b0 == blen - 1) 162062587Sitojun return 1; 162162587Sitojun if (a[0] == 0 || b[0] == 0) 162262587Sitojun return 0; 162362587Sitojun 162462587Sitojun if (a[0] != b[0]) 162562587Sitojun return 0; 162662587Sitojun l = a[0]; 162762587Sitojun if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 162862587Sitojun return 0; 162962587Sitojun if (bcmp(a + 1, b + 1, l) != 0) 163062587Sitojun return 0; 163162587Sitojun 163262587Sitojun a += 1 + l; 163362587Sitojun b += 1 + l; 163462587Sitojun } 163562587Sitojun 163662587Sitojun if (a - a0 == alen && b - b0 == blen) 163762587Sitojun return 1; 163862587Sitojun else 163962587Sitojun return 0; 164062587Sitojun} 164162587Sitojun 164262587Sitojun/* 164353541Sshin * calculate the number of addresses to be returned in the node info reply. 164453541Sshin */ 164553541Sshinstatic int 164678064Sumeni6_addrs(ni6, m, ifpp, subj) 164753541Sshin struct icmp6_nodeinfo *ni6; 164853541Sshin struct mbuf *m; 164953541Sshin struct ifnet **ifpp; 165078064Sume char *subj; 165153541Sshin{ 165278064Sume struct ifnet *ifp; 165378064Sume struct in6_ifaddr *ifa6; 165478064Sume struct ifaddr *ifa; 165578064Sume struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */ 165653541Sshin int addrs = 0, addrsofif, iffound = 0; 165778064Sume int niflags = ni6->ni_flags; 165853541Sshin 165978064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 166078064Sume switch (ni6->ni_code) { 166178064Sume case ICMP6_NI_SUBJ_IPV6: 166278064Sume if (subj == NULL) /* must be impossible... */ 1663120856Sume return (0); 166478064Sume subj_ip6 = (struct sockaddr_in6 *)subj; 166578064Sume break; 166678064Sume default: 166778064Sume /* 166878064Sume * XXX: we only support IPv6 subject address for 166978064Sume * this Qtype. 167078064Sume */ 1671120856Sume return (0); 167278064Sume } 167378064Sume } 167478064Sume 1675108172Shsu IFNET_RLOCK(); 1676120891Sume for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 167753541Sshin addrsofif = 0; 1678120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 167953541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 168053541Sshin continue; 168153541Sshin ifa6 = (struct in6_ifaddr *)ifa; 168253541Sshin 168378064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 168478064Sume IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr, 1685120891Sume &ifa6->ia_addr.sin6_addr)) 168653541Sshin iffound = 1; 168753541Sshin 168862587Sitojun /* 168962587Sitojun * IPv4-mapped addresses can only be returned by a 169062587Sitojun * Node Information proxy, since they represent 169162587Sitojun * addresses of IPv4-only nodes, which perforce do 169262587Sitojun * not implement this protocol. 169378064Sume * [icmp-name-lookups-07, Section 5.4] 169462587Sitojun * So we don't support NI_NODEADDR_FLAG_COMPAT in 169562587Sitojun * this function at this moment. 169662587Sitojun */ 169762587Sitojun 169853541Sshin /* What do we have to do about ::1? */ 169978064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 170078064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 170178064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 170278064Sume continue; 170353541Sshin break; 170478064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 170578064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 170678064Sume continue; 170753541Sshin break; 170878064Sume case IPV6_ADDR_SCOPE_GLOBAL: 170978064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 171078064Sume continue; 171178064Sume break; 171278064Sume default: 171378064Sume continue; 171453541Sshin } 171578064Sume 171678064Sume /* 171778064Sume * check if anycast is okay. 171895023Ssuz * XXX: just experimental. not in the spec. 171978064Sume */ 172078064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 172178064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 172278064Sume continue; /* we need only unicast addresses */ 172378064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 172478064Sume (icmp6_nodeinfo & 4) == 0) { 172578064Sume continue; 172678064Sume } 172778064Sume addrsofif++; /* count the address */ 172853541Sshin } 172953541Sshin if (iffound) { 173053541Sshin *ifpp = ifp; 1731108172Shsu IFNET_RUNLOCK(); 1732120856Sume return (addrsofif); 173353541Sshin } 173453541Sshin 173553541Sshin addrs += addrsofif; 173653541Sshin } 1737108172Shsu IFNET_RUNLOCK(); 173853541Sshin 1739120856Sume return (addrs); 174053541Sshin} 174153541Sshin 174253541Sshinstatic int 174353541Sshinni6_store_addrs(ni6, nni6, ifp0, resid) 174453541Sshin struct icmp6_nodeinfo *ni6, *nni6; 174553541Sshin struct ifnet *ifp0; 174653541Sshin int resid; 174753541Sshin{ 174878064Sume struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet); 174978064Sume struct in6_ifaddr *ifa6; 175078064Sume struct ifaddr *ifa; 175178064Sume struct ifnet *ifp_dep = NULL; 175278064Sume int copied = 0, allow_deprecated = 0; 175353541Sshin u_char *cp = (u_char *)(nni6 + 1); 175478064Sume int niflags = ni6->ni_flags; 175578064Sume u_int32_t ltime; 175653541Sshin 175778064Sume if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 1758120856Sume return (0); /* needless to copy */ 1759120891Sume 1760108172Shsu IFNET_RLOCK(); 176178064Sume again: 1762120892Sume 1763120891Sume for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 176462587Sitojun for (ifa = ifp->if_addrlist.tqh_first; ifa; 1765120891Sume ifa = ifa->ifa_list.tqe_next) { 176653541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 176753541Sshin continue; 176853541Sshin ifa6 = (struct in6_ifaddr *)ifa; 176953541Sshin 177078064Sume if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && 177178064Sume allow_deprecated == 0) { 177278064Sume /* 177378064Sume * prefererred address should be put before 177478064Sume * deprecated addresses. 177578064Sume */ 177678064Sume 177778064Sume /* record the interface for later search */ 177878064Sume if (ifp_dep == NULL) 177978064Sume ifp_dep = ifp; 178078064Sume 178178064Sume continue; 1782120891Sume } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && 1783120891Sume allow_deprecated != 0) 178478064Sume continue; /* we now collect deprecated addrs */ 178553541Sshin 178653541Sshin /* What do we have to do about ::1? */ 178778064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 178878064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 178978064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 179078064Sume continue; 179153541Sshin break; 179278064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 179378064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 179478064Sume continue; 179553541Sshin break; 179678064Sume case IPV6_ADDR_SCOPE_GLOBAL: 179778064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 179878064Sume continue; 179978064Sume break; 180078064Sume default: 180178064Sume continue; 180253541Sshin } 180353541Sshin 180478064Sume /* 180578064Sume * check if anycast is okay. 1806120891Sume * XXX: just experimental. not in the spec. 180778064Sume */ 180878064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 180978064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 181078064Sume continue; 181178064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 181278064Sume (icmp6_nodeinfo & 4) == 0) { 181378064Sume continue; 181453541Sshin } 181578064Sume 181678064Sume /* now we can copy the address */ 181778064Sume if (resid < sizeof(struct in6_addr) + 181878064Sume sizeof(u_int32_t)) { 181978064Sume /* 182078064Sume * We give up much more copy. 182178064Sume * Set the truncate flag and return. 182278064Sume */ 1823120891Sume nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE; 1824108172Shsu IFNET_RUNLOCK(); 1825120856Sume return (copied); 182678064Sume } 182778064Sume 182878064Sume /* 182978064Sume * Set the TTL of the address. 183078064Sume * The TTL value should be one of the following 183178064Sume * according to the specification: 183278064Sume * 183378064Sume * 1. The remaining lifetime of a DHCP lease on the 183478064Sume * address, or 183578064Sume * 2. The remaining Valid Lifetime of a prefix from 183678064Sume * which the address was derived through Stateless 183778064Sume * Autoconfiguration. 183878064Sume * 183978064Sume * Note that we currently do not support stateful 184078064Sume * address configuration by DHCPv6, so the former 184178064Sume * case can't happen. 184278064Sume */ 184378064Sume if (ifa6->ia6_lifetime.ia6t_expire == 0) 184478064Sume ltime = ND6_INFINITE_LIFETIME; 184578064Sume else { 184678064Sume if (ifa6->ia6_lifetime.ia6t_expire > 184778064Sume time_second) 184878064Sume ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second); 184978064Sume else 185078064Sume ltime = 0; 185178064Sume } 1852120891Sume 185378064Sume bcopy(<ime, cp, sizeof(u_int32_t)); 185478064Sume cp += sizeof(u_int32_t); 185578064Sume 185678064Sume /* copy the address itself */ 185778064Sume bcopy(&ifa6->ia_addr.sin6_addr, cp, 1858120891Sume sizeof(struct in6_addr)); 1859121315Sume in6_clearscope((struct in6_addr *)cp); /* XXX */ 186078064Sume cp += sizeof(struct in6_addr); 1861120891Sume 186278064Sume resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1863120891Sume copied += (sizeof(struct in6_addr) + sizeof(u_int32_t)); 186453541Sshin } 186553541Sshin if (ifp0) /* we need search only on the specified IF */ 186653541Sshin break; 186753541Sshin } 186853541Sshin 186978064Sume if (allow_deprecated == 0 && ifp_dep != NULL) { 187078064Sume ifp = ifp_dep; 187178064Sume allow_deprecated = 1; 187278064Sume 187378064Sume goto again; 187478064Sume } 187578064Sume 1876108172Shsu IFNET_RUNLOCK(); 1877108172Shsu 1878120856Sume return (copied); 187953541Sshin} 188053541Sshin 188153541Sshin/* 188253541Sshin * XXX almost dup'ed code with rip6_input. 188353541Sshin */ 188453541Sshinstatic int 188553541Sshinicmp6_rip6_input(mp, off) 188653541Sshin struct mbuf **mp; 188753541Sshin int off; 188853541Sshin{ 188953541Sshin struct mbuf *m = *mp; 189078064Sume struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 189178064Sume struct in6pcb *in6p; 189253541Sshin struct in6pcb *last = NULL; 1893121315Sume struct sockaddr_in6 fromsa; 189453541Sshin struct icmp6_hdr *icmp6; 189553541Sshin struct mbuf *opts = NULL; 189653541Sshin 189762587Sitojun#ifndef PULLDOWN_TEST 189853541Sshin /* this is assumed to be safe. */ 189953541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 190062587Sitojun#else 190162587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 190262587Sitojun if (icmp6 == NULL) { 190362587Sitojun /* m is already reclaimed */ 1904120891Sume return (IPPROTO_DONE); 190562587Sitojun } 190662587Sitojun#endif 190753541Sshin 1908121315Sume bzero(&fromsa, sizeof(fromsa)); 1909121315Sume fromsa.sin6_len = sizeof(struct sockaddr_in6); 1910121315Sume fromsa.sin6_family = AF_INET6; 191162587Sitojun /* KAME hack: recover scopeid */ 1912121315Sume (void)in6_recoverscope(&fromsa, &ip6->ip6_src, m->m_pkthdr.rcvif); 191353541Sshin 1914120891Sume LIST_FOREACH(in6p, &ripcb, inp_list) { 191578064Sume if ((in6p->inp_vflag & INP_IPV6) == 0) 191653541Sshin continue; 191753541Sshin if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6) 191853541Sshin continue; 191953541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 192053541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 192153541Sshin continue; 192253541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 192353541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 192453541Sshin continue; 192553541Sshin if (in6p->in6p_icmp6filt 192653541Sshin && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, 192753541Sshin in6p->in6p_icmp6filt)) 192853541Sshin continue; 192953541Sshin if (last) { 1930121809Sume struct mbuf *n = NULL; 1931121809Sume 1932121809Sume /* 1933121809Sume * Recent network drivers tend to allocate a single 1934121809Sume * mbuf cluster, rather than to make a couple of 1935121809Sume * mbufs without clusters. Also, since the IPv6 code 1936121809Sume * path tries to avoid m_pullup(), it is highly 1937121809Sume * probable that we still have an mbuf cluster here 1938121809Sume * even though the necessary length can be stored in an 1939121809Sume * mbuf's internal buffer. 1940121809Sume * Meanwhile, the default size of the receive socket 1941121809Sume * buffer for raw sockets is not so large. This means 1942121809Sume * the possibility of packet loss is relatively higher 1943121809Sume * than before. To avoid this scenario, we copy the 1944121809Sume * received data to a separate mbuf that does not use 1945121809Sume * a cluster, if possible. 1946121809Sume * XXX: it is better to copy the data after stripping 1947121809Sume * intermediate headers. 1948121809Sume */ 1949121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 1950121809Sume m->m_len <= MHLEN) { 1951121809Sume MGET(n, M_DONTWAIT, m->m_type); 1952121809Sume if (n != NULL) { 1953121809Sume m_dup_pkthdr(n, m, M_NOWAIT); 1954121809Sume bcopy(m->m_data, n->m_data, m->m_len); 1955121809Sume n->m_len = m->m_len; 1956121809Sume } 1957121809Sume } 1958121809Sume if (n != NULL || 1959121809Sume (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 196053541Sshin if (last->in6p_flags & IN6P_CONTROLOPTS) 1961121674Sume ip6_savecontrol(last, n, &opts); 196253541Sshin /* strip intermediate headers */ 196353541Sshin m_adj(n, off); 196453541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 1965121315Sume (struct sockaddr *)&fromsa, n, opts) 1966120891Sume == 0) { 196753541Sshin /* should notify about lost packet */ 196853541Sshin m_freem(n); 196978064Sume if (opts) { 197053541Sshin m_freem(opts); 197178064Sume } 197297658Stanimura } else 197353541Sshin sorwakeup(last->in6p_socket); 197453541Sshin opts = NULL; 197553541Sshin } 197653541Sshin } 197753541Sshin last = in6p; 197853541Sshin } 197953541Sshin if (last) { 198053541Sshin if (last->in6p_flags & IN6P_CONTROLOPTS) 1981121674Sume ip6_savecontrol(last, m, &opts); 198253541Sshin /* strip intermediate headers */ 198353541Sshin m_adj(m, off); 1984121809Sume 1985121809Sume /* avoid using mbuf clusters if possible (see above) */ 1986121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 1987121809Sume m->m_len <= MHLEN) { 1988121809Sume struct mbuf *n; 1989121809Sume 1990121809Sume MGET(n, M_DONTWAIT, m->m_type); 1991121809Sume if (n != NULL) { 1992121809Sume m_dup_pkthdr(n, m, M_NOWAIT); 1993121809Sume bcopy(m->m_data, n->m_data, m->m_len); 1994121809Sume n->m_len = m->m_len; 1995121809Sume 1996121809Sume m_freem(m); 1997121809Sume m = n; 1998121809Sume } 1999121809Sume } 200053541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 2001121315Sume (struct sockaddr *)&fromsa, m, opts) == 0) { 200253541Sshin m_freem(m); 200353541Sshin if (opts) 200453541Sshin m_freem(opts); 200597658Stanimura } else 200653541Sshin sorwakeup(last->in6p_socket); 200753541Sshin } else { 200853541Sshin m_freem(m); 200953541Sshin ip6stat.ip6s_delivered--; 201053541Sshin } 201153541Sshin return IPPROTO_DONE; 201253541Sshin} 201353541Sshin 201453541Sshin/* 201553541Sshin * Reflect the ip6 packet back to the source. 201662587Sitojun * OFF points to the icmp6 header, counted from the top of the mbuf. 201753541Sshin */ 201853541Sshinvoid 201953541Sshinicmp6_reflect(m, off) 202053541Sshin struct mbuf *m; 202153541Sshin size_t off; 202253541Sshin{ 202362587Sitojun struct ip6_hdr *ip6; 202453541Sshin struct icmp6_hdr *icmp6; 202553541Sshin struct in6_ifaddr *ia; 202653541Sshin struct in6_addr t, *src = 0; 202762587Sitojun int plen; 202853541Sshin int type, code; 202953541Sshin struct ifnet *outif = NULL; 203078064Sume struct sockaddr_in6 sa6_src, sa6_dst; 203153541Sshin#ifdef COMPAT_RFC1885 203253541Sshin int mtu = IPV6_MMTU; 203353541Sshin struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst; 203453541Sshin#endif 203553541Sshin 203662587Sitojun /* too short to reflect */ 203762587Sitojun if (off < sizeof(struct ip6_hdr)) { 203878064Sume nd6log((LOG_DEBUG, 203978064Sume "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 204078064Sume (u_long)off, (u_long)sizeof(struct ip6_hdr), 204178064Sume __FILE__, __LINE__)); 204262587Sitojun goto bad; 204362587Sitojun } 204462587Sitojun 204553541Sshin /* 204653541Sshin * If there are extra headers between IPv6 and ICMPv6, strip 204753541Sshin * off that header first. 204853541Sshin */ 204962587Sitojun#ifdef DIAGNOSTIC 205062587Sitojun if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) 205162587Sitojun panic("assumption failed in icmp6_reflect"); 205262587Sitojun#endif 205362587Sitojun if (off > sizeof(struct ip6_hdr)) { 205462587Sitojun size_t l; 205562587Sitojun struct ip6_hdr nip6; 205653541Sshin 205762587Sitojun l = off - sizeof(struct ip6_hdr); 205862587Sitojun m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); 205962587Sitojun m_adj(m, l); 206062587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 206162587Sitojun if (m->m_len < l) { 206262587Sitojun if ((m = m_pullup(m, l)) == NULL) 206362587Sitojun return; 206453541Sshin } 206562587Sitojun bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); 206662587Sitojun } else /* off == sizeof(struct ip6_hdr) */ { 206762587Sitojun size_t l; 206862587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 206962587Sitojun if (m->m_len < l) { 207062587Sitojun if ((m = m_pullup(m, l)) == NULL) 207162587Sitojun return; 207253541Sshin } 207353541Sshin } 207462587Sitojun plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 207562587Sitojun ip6 = mtod(m, struct ip6_hdr *); 207662587Sitojun ip6->ip6_nxt = IPPROTO_ICMPV6; 207753541Sshin icmp6 = (struct icmp6_hdr *)(ip6 + 1); 207853541Sshin type = icmp6->icmp6_type; /* keep type for statistics */ 207953541Sshin code = icmp6->icmp6_code; /* ditto. */ 208053541Sshin 208153541Sshin t = ip6->ip6_dst; 208253541Sshin /* 208353541Sshin * ip6_input() drops a packet if its src is multicast. 208453541Sshin * So, the src is never multicast. 208553541Sshin */ 208653541Sshin ip6->ip6_dst = ip6->ip6_src; 208753541Sshin 208878064Sume /* 208978064Sume * XXX: make sure to embed scope zone information, using 209078064Sume * already embedded IDs or the received interface (if any). 209178064Sume * Note that rcvif may be NULL. 209278064Sume * TODO: scoped routing case (XXX). 209378064Sume */ 209478064Sume bzero(&sa6_src, sizeof(sa6_src)); 209578064Sume sa6_src.sin6_family = AF_INET6; 209678064Sume sa6_src.sin6_len = sizeof(sa6_src); 209778064Sume sa6_src.sin6_addr = ip6->ip6_dst; 209878064Sume in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif); 209978064Sume in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL); 210078064Sume bzero(&sa6_dst, sizeof(sa6_dst)); 210178064Sume sa6_dst.sin6_family = AF_INET6; 210278064Sume sa6_dst.sin6_len = sizeof(sa6_dst); 210378064Sume sa6_dst.sin6_addr = t; 210478064Sume in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif); 210578064Sume in6_embedscope(&t, &sa6_dst, NULL, NULL); 210653541Sshin 210753541Sshin#ifdef COMPAT_RFC1885 210853541Sshin /* 210953541Sshin * xxx guess MTU 211053541Sshin * RFC 1885 requires that echo reply should be truncated if it 211153541Sshin * does not fit in with (return) path MTU, but the description was 211253541Sshin * removed in the new spec. 211353541Sshin */ 211453541Sshin if (icmp6_reflect_rt.ro_rt == 0 || 211553541Sshin ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) { 211653541Sshin if (icmp6_reflect_rt.ro_rt) { 211753541Sshin RTFREE(icmp6_reflect_rt.ro_rt); 211853541Sshin icmp6_reflect_rt.ro_rt = 0; 211953541Sshin } 212053541Sshin bzero(sin6, sizeof(*sin6)); 212153541Sshin sin6->sin6_family = PF_INET6; 212253541Sshin sin6->sin6_len = sizeof(struct sockaddr_in6); 212353541Sshin sin6->sin6_addr = ip6->ip6_dst; 212453541Sshin 2125122921Sandre rtalloc((struct route *)&icmp6_reflect_rt.ro_rt); 212653541Sshin } 212753541Sshin 212853541Sshin if (icmp6_reflect_rt.ro_rt == 0) 212953541Sshin goto bad; 213053541Sshin 213153541Sshin if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST) 213253541Sshin && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu) 213353541Sshin mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu; 213453541Sshin 213553541Sshin if (mtu < m->m_pkthdr.len) { 213653541Sshin plen -= (m->m_pkthdr.len - mtu); 213753541Sshin m_adj(m, mtu - m->m_pkthdr.len); 213853541Sshin } 213953541Sshin#endif 214053541Sshin /* 2141120891Sume * If the incoming packet was addressed directly to us (i.e. unicast), 214253541Sshin * use dst as the src for the reply. 2143120891Sume * The IN6_IFF_NOTREADY case should be VERY rare, but is possible 214462587Sitojun * (for example) when we encounter an error while forwarding procedure 214562587Sitojun * destined to a duplicated address of ours. 214653541Sshin */ 214753541Sshin for (ia = in6_ifaddr; ia; ia = ia->ia_next) 214853541Sshin if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) && 214962587Sitojun (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) { 215053541Sshin src = &t; 215153541Sshin break; 215253541Sshin } 215353541Sshin if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) { 215453541Sshin /* 215553541Sshin * This is the case if the dst is our link-local address 215695023Ssuz * and the sender is also ourselves. 215753541Sshin */ 215853541Sshin src = &t; 215953541Sshin } 216053541Sshin 216178064Sume if (src == 0) { 216278064Sume int e; 216378064Sume struct route_in6 ro; 216478064Sume 216553541Sshin /* 216662587Sitojun * This case matches to multicasts, our anycast, or unicasts 216795023Ssuz * that we do not own. Select a source address based on the 216878064Sume * source address of the erroneous packet. 216953541Sshin */ 217078064Sume bzero(&ro, sizeof(ro)); 217178064Sume src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e); 217278064Sume if (ro.ro_rt) 217378064Sume RTFREE(ro.ro_rt); /* XXX: we could use this */ 217478064Sume if (src == NULL) { 217578064Sume nd6log((LOG_DEBUG, 217678064Sume "icmp6_reflect: source can't be determined: " 217778064Sume "dst=%s, error=%d\n", 217878064Sume ip6_sprintf(&sa6_src.sin6_addr), e)); 217978064Sume goto bad; 218078064Sume } 218178064Sume } 218253541Sshin 218353541Sshin ip6->ip6_src = *src; 218453541Sshin 218553541Sshin ip6->ip6_flow = 0; 218662587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 218762587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 218853541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 2189121472Sume if (outif) 2190121472Sume ip6->ip6_hlim = ND_IFINFO(outif)->chlim; 2191121472Sume else if (m->m_pkthdr.rcvif) { 219253541Sshin /* XXX: This may not be the outgoing interface */ 2193121161Sume ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim; 219478064Sume } else 219578064Sume ip6->ip6_hlim = ip6_defhlim; 219653541Sshin 219753541Sshin icmp6->icmp6_cksum = 0; 219853541Sshin icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2199120891Sume sizeof(struct ip6_hdr), plen); 220053541Sshin 220153541Sshin /* 220278064Sume * XXX option handling 220353541Sshin */ 220453541Sshin 220553541Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 2206122062Sume#ifdef IPSEC 2207122062Sume /* Don't lookup socket */ 2208122062Sume (void)ipsec_setsocket(m, NULL); 2209122062Sume#endif /* IPSEC */ 221053541Sshin 221153541Sshin#ifdef COMPAT_RFC1885 2212105194Ssam ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL); 221353541Sshin#else 2214105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 221553541Sshin#endif 221653541Sshin if (outif) 221753541Sshin icmp6_ifoutstat_inc(outif, type, code); 221853541Sshin 221953541Sshin return; 222053541Sshin 222153541Sshin bad: 222253541Sshin m_freem(m); 222353541Sshin return; 222453541Sshin} 222553541Sshin 222653541Sshinvoid 222753541Sshinicmp6_fasttimo() 222853541Sshin{ 222962587Sitojun 223053541Sshin mld6_fasttimeo(); 223153541Sshin} 223253541Sshin 223353541Sshinstatic const char * 223453541Sshinicmp6_redirect_diag(src6, dst6, tgt6) 223553541Sshin struct in6_addr *src6; 223653541Sshin struct in6_addr *dst6; 223753541Sshin struct in6_addr *tgt6; 223853541Sshin{ 223953541Sshin static char buf[1024]; 224053541Sshin snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", 2241120891Sume ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6)); 224253541Sshin return buf; 224353541Sshin} 224453541Sshin 224553541Sshinvoid 224653541Sshinicmp6_redirect_input(m, off) 224778064Sume struct mbuf *m; 224853541Sshin int off; 224953541Sshin{ 225053541Sshin struct ifnet *ifp = m->m_pkthdr.rcvif; 225153541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 225262587Sitojun struct nd_redirect *nd_rd; 225353541Sshin int icmp6len = ntohs(ip6->ip6_plen); 225453541Sshin char *lladdr = NULL; 225553541Sshin int lladdrlen = 0; 225653541Sshin u_char *redirhdr = NULL; 225753541Sshin int redirhdrlen = 0; 225853541Sshin struct rtentry *rt = NULL; 225953541Sshin int is_router; 226053541Sshin int is_onlink; 226153541Sshin struct in6_addr src6 = ip6->ip6_src; 226262587Sitojun struct in6_addr redtgt6; 226362587Sitojun struct in6_addr reddst6; 226453541Sshin union nd_opts ndopts; 226553541Sshin 226653541Sshin if (!m || !ifp) 226753541Sshin return; 226853541Sshin 226953541Sshin /* XXX if we are router, we don't update route by icmp6 redirect */ 227053541Sshin if (ip6_forwarding) 227162587Sitojun goto freeit; 227253541Sshin if (!icmp6_rediraccept) 227362587Sitojun goto freeit; 227462587Sitojun 227562587Sitojun#ifndef PULLDOWN_TEST 227662587Sitojun IP6_EXTHDR_CHECK(m, off, icmp6len,); 227762587Sitojun nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); 227862587Sitojun#else 227962587Sitojun IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); 228062587Sitojun if (nd_rd == NULL) { 228162587Sitojun icmp6stat.icp6s_tooshort++; 228253541Sshin return; 228362587Sitojun } 228462587Sitojun#endif 228562587Sitojun redtgt6 = nd_rd->nd_rd_target; 228662587Sitojun reddst6 = nd_rd->nd_rd_dst; 228753541Sshin 228853541Sshin if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 228953541Sshin redtgt6.s6_addr16[1] = htons(ifp->if_index); 229053541Sshin if (IN6_IS_ADDR_LINKLOCAL(&reddst6)) 229153541Sshin reddst6.s6_addr16[1] = htons(ifp->if_index); 229253541Sshin 229353541Sshin /* validation */ 229453541Sshin if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 229578064Sume nd6log((LOG_ERR, 2296120891Sume "ICMP6 redirect sent from %s rejected; " 2297120891Sume "must be from linklocal\n", 2298120891Sume ip6_sprintf(&src6))); 229978064Sume goto bad; 230053541Sshin } 230153541Sshin if (ip6->ip6_hlim != 255) { 230278064Sume nd6log((LOG_ERR, 2303120891Sume "ICMP6 redirect sent from %s rejected; " 2304120891Sume "hlim=%d (must be 255)\n", 2305120891Sume ip6_sprintf(&src6), ip6->ip6_hlim)); 230678064Sume goto bad; 230753541Sshin } 230853541Sshin { 230953541Sshin /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 231053541Sshin struct sockaddr_in6 sin6; 231153541Sshin struct in6_addr *gw6; 231253541Sshin 231353541Sshin bzero(&sin6, sizeof(sin6)); 231453541Sshin sin6.sin6_family = AF_INET6; 231553541Sshin sin6.sin6_len = sizeof(struct sockaddr_in6); 231653541Sshin bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); 231753541Sshin rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 231853541Sshin if (rt) { 231965895Sume if (rt->rt_gateway == NULL || 232065895Sume rt->rt_gateway->sa_family != AF_INET6) { 232178064Sume nd6log((LOG_ERR, 232265895Sume "ICMP6 redirect rejected; no route " 232365895Sume "with inet6 gateway found for redirect dst: %s\n", 232478064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2325120727Ssam RTFREE_LOCKED(rt); 232678064Sume goto bad; 232765895Sume } 232865895Sume 232953541Sshin gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); 233053541Sshin if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { 233178064Sume nd6log((LOG_ERR, 2332120891Sume "ICMP6 redirect rejected; " 2333120891Sume "not equal to gw-for-src=%s (must be same): " 2334120891Sume "%s\n", 2335120891Sume ip6_sprintf(gw6), 2336120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2337120727Ssam RTFREE_LOCKED(rt); 233878064Sume goto bad; 233953541Sshin } 234053541Sshin } else { 234178064Sume nd6log((LOG_ERR, 2342120891Sume "ICMP6 redirect rejected; " 2343120891Sume "no route found for redirect dst: %s\n", 2344120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 234578064Sume goto bad; 234653541Sshin } 2347120727Ssam RTFREE_LOCKED(rt); 234853541Sshin rt = NULL; 234953541Sshin } 235053541Sshin if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 235178064Sume nd6log((LOG_ERR, 2352120891Sume "ICMP6 redirect rejected; " 2353120891Sume "redirect dst must be unicast: %s\n", 2354120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 235578064Sume goto bad; 235653541Sshin } 235753541Sshin 235853541Sshin is_router = is_onlink = 0; 235953541Sshin if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 236053541Sshin is_router = 1; /* router case */ 236153541Sshin if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 236253541Sshin is_onlink = 1; /* on-link destination case */ 236353541Sshin if (!is_router && !is_onlink) { 236478064Sume nd6log((LOG_ERR, 2365120891Sume "ICMP6 redirect rejected; " 2366120891Sume "neither router case nor onlink case: %s\n", 2367120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 236878064Sume goto bad; 236953541Sshin } 237053541Sshin /* validation passed */ 237153541Sshin 237253541Sshin icmp6len -= sizeof(*nd_rd); 237353541Sshin nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 237453541Sshin if (nd6_options(&ndopts) < 0) { 237578064Sume nd6log((LOG_INFO, "icmp6_redirect_input: " 2376120891Sume "invalid ND option, rejected: %s\n", 2377120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 237878064Sume /* nd6_options have incremented stats */ 237962587Sitojun goto freeit; 238053541Sshin } 238153541Sshin 238253541Sshin if (ndopts.nd_opts_tgt_lladdr) { 238353541Sshin lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 238453541Sshin lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 238553541Sshin } 238653541Sshin 238753541Sshin if (ndopts.nd_opts_rh) { 238853541Sshin redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len; 238953541Sshin redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */ 239053541Sshin } 239153541Sshin 239253541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 239378064Sume nd6log((LOG_INFO, 2394120891Sume "icmp6_redirect_input: lladdrlen mismatch for %s " 2395120891Sume "(if %d, icmp6 packet %d): %s\n", 2396120891Sume ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2, 2397120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 239878064Sume goto bad; 239953541Sshin } 240053541Sshin 240153541Sshin /* RFC 2461 8.3 */ 240253541Sshin nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 2403120891Sume is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 240453541Sshin 240595023Ssuz if (!is_onlink) { /* better router case. perform rtredirect. */ 240653541Sshin /* perform rtredirect */ 240753541Sshin struct sockaddr_in6 sdst; 240853541Sshin struct sockaddr_in6 sgw; 240953541Sshin struct sockaddr_in6 ssrc; 241053541Sshin 241153541Sshin bzero(&sdst, sizeof(sdst)); 241253541Sshin bzero(&sgw, sizeof(sgw)); 241353541Sshin bzero(&ssrc, sizeof(ssrc)); 241453541Sshin sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; 241553541Sshin sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = 241653541Sshin sizeof(struct sockaddr_in6); 241753541Sshin bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); 241853541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 241953541Sshin bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 242053541Sshin rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw, 2421120891Sume (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST, 2422120891Sume (struct sockaddr *)&ssrc); 242353541Sshin } 242453541Sshin /* finally update cached route in each socket via pfctlinput */ 242553541Sshin { 242653541Sshin struct sockaddr_in6 sdst; 242753541Sshin 242853541Sshin bzero(&sdst, sizeof(sdst)); 242953541Sshin sdst.sin6_family = AF_INET6; 243053541Sshin sdst.sin6_len = sizeof(struct sockaddr_in6); 243153541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 243253541Sshin pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); 2433122062Sume#if defined(IPSEC) || defined(FAST_IPSEC) 243453541Sshin key_sa_routechange((struct sockaddr *)&sdst); 243553541Sshin#endif 243653541Sshin } 243762587Sitojun 243862587Sitojun freeit: 243962587Sitojun m_freem(m); 244078064Sume return; 244178064Sume 244278064Sume bad: 244378064Sume icmp6stat.icp6s_badredirect++; 244478064Sume m_freem(m); 244553541Sshin} 244653541Sshin 244753541Sshinvoid 244853541Sshinicmp6_redirect_output(m0, rt) 244953541Sshin struct mbuf *m0; 245053541Sshin struct rtentry *rt; 245153541Sshin{ 245253541Sshin struct ifnet *ifp; /* my outgoing interface */ 245353541Sshin struct in6_addr *ifp_ll6; 245453541Sshin struct in6_addr *router_ll6; 245553541Sshin struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 245653541Sshin struct mbuf *m = NULL; /* newly allocated one */ 245753541Sshin struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 245853541Sshin struct nd_redirect *nd_rd; 245953541Sshin size_t maxlen; 246053541Sshin u_char *p; 246153541Sshin struct ifnet *outif = NULL; 246262587Sitojun struct sockaddr_in6 src_sa; 246353541Sshin 246462587Sitojun icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0); 246562587Sitojun 246653541Sshin /* if we are not router, we don't send icmp6 redirect */ 246753541Sshin if (!ip6_forwarding || ip6_accept_rtadv) 246853541Sshin goto fail; 246953541Sshin 247053541Sshin /* sanity check */ 247153541Sshin if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) 247253541Sshin goto fail; 247353541Sshin 247453541Sshin /* 247553541Sshin * Address check: 247653541Sshin * the source address must identify a neighbor, and 247753541Sshin * the destination address must not be a multicast address 247853541Sshin * [RFC 2461, sec 8.2] 247953541Sshin */ 248053541Sshin sip6 = mtod(m0, struct ip6_hdr *); 248162587Sitojun bzero(&src_sa, sizeof(src_sa)); 248262587Sitojun src_sa.sin6_family = AF_INET6; 248362587Sitojun src_sa.sin6_len = sizeof(src_sa); 248462587Sitojun src_sa.sin6_addr = sip6->ip6_src; 248562587Sitojun /* we don't currently use sin6_scope_id, but eventually use it */ 2486121315Sume if (in6_addr2zoneid(ifp, &sip6->ip6_src, &src_sa.sin6_scope_id)) 2487121315Sume goto fail; 248862587Sitojun if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) 248953541Sshin goto fail; 249053541Sshin if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) 249153541Sshin goto fail; /* what should we do here? */ 249253541Sshin 249353541Sshin /* rate limit */ 249453541Sshin if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) 249553541Sshin goto fail; 249653541Sshin 249753541Sshin /* 249853541Sshin * Since we are going to append up to 1280 bytes (= IPV6_MMTU), 249953541Sshin * we almost always ask for an mbuf cluster for simplicity. 250053541Sshin * (MHLEN < IPV6_MMTU is almost always true) 250153541Sshin */ 250262587Sitojun#if IPV6_MMTU >= MCLBYTES 250362587Sitojun# error assumption failed about IPV6_MMTU and MCLBYTES 250462587Sitojun#endif 2505111119Simp MGETHDR(m, M_DONTWAIT, MT_HEADER); 250662587Sitojun if (m && IPV6_MMTU >= MHLEN) 2507111119Simp MCLGET(m, M_DONTWAIT); 250853541Sshin if (!m) 250953541Sshin goto fail; 251078064Sume m->m_pkthdr.rcvif = NULL; 251178064Sume m->m_len = 0; 251278064Sume maxlen = M_TRAILINGSPACE(m); 251353541Sshin maxlen = min(IPV6_MMTU, maxlen); 251453541Sshin /* just for safety */ 251562587Sitojun if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 251662587Sitojun ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { 251753541Sshin goto fail; 251862587Sitojun } 251953541Sshin 252053541Sshin { 252153541Sshin /* get ip6 linklocal address for ifp(my outgoing interface). */ 252262587Sitojun struct in6_ifaddr *ia; 252362587Sitojun if ((ia = in6ifa_ifpforlinklocal(ifp, 252462587Sitojun IN6_IFF_NOTREADY| 252562587Sitojun IN6_IFF_ANYCAST)) == NULL) 252653541Sshin goto fail; 252753541Sshin ifp_ll6 = &ia->ia_addr.sin6_addr; 252853541Sshin } 252953541Sshin 253053541Sshin /* get ip6 linklocal address for the router. */ 253153541Sshin if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { 253253541Sshin struct sockaddr_in6 *sin6; 253353541Sshin sin6 = (struct sockaddr_in6 *)rt->rt_gateway; 253453541Sshin router_ll6 = &sin6->sin6_addr; 253553541Sshin if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) 253653541Sshin router_ll6 = (struct in6_addr *)NULL; 253753541Sshin } else 253853541Sshin router_ll6 = (struct in6_addr *)NULL; 253953541Sshin 254053541Sshin /* ip6 */ 254153541Sshin ip6 = mtod(m, struct ip6_hdr *); 254253541Sshin ip6->ip6_flow = 0; 254362587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 254462587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 254553541Sshin /* ip6->ip6_plen will be set later */ 254653541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 254753541Sshin ip6->ip6_hlim = 255; 254853541Sshin /* ip6->ip6_src must be linklocal addr for my outgoing if. */ 254953541Sshin bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); 255053541Sshin bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); 255153541Sshin 255253541Sshin /* ND Redirect */ 255353541Sshin nd_rd = (struct nd_redirect *)(ip6 + 1); 255453541Sshin nd_rd->nd_rd_type = ND_REDIRECT; 255553541Sshin nd_rd->nd_rd_code = 0; 255653541Sshin nd_rd->nd_rd_reserved = 0; 255753541Sshin if (rt->rt_flags & RTF_GATEWAY) { 255853541Sshin /* 255953541Sshin * nd_rd->nd_rd_target must be a link-local address in 256053541Sshin * better router cases. 256153541Sshin */ 256253541Sshin if (!router_ll6) 256353541Sshin goto fail; 256453541Sshin bcopy(router_ll6, &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 } else { 256953541Sshin /* make sure redtgt == reddst */ 257053541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, 2571120892Sume sizeof(nd_rd->nd_rd_target)); 257253541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2573120892Sume sizeof(nd_rd->nd_rd_dst)); 257453541Sshin } 257553541Sshin 257653541Sshin p = (u_char *)(nd_rd + 1); 257753541Sshin 257853541Sshin if (!router_ll6) 257953541Sshin goto nolladdropt; 258053541Sshin 2581120892Sume { 2582120892Sume /* target lladdr option */ 2583120892Sume struct rtentry *rt_router = NULL; 2584120892Sume int len; 2585120892Sume struct sockaddr_dl *sdl; 2586120892Sume struct nd_opt_hdr *nd_opt; 2587120892Sume char *lladdr; 258853541Sshin 2589120892Sume rt_router = nd6_lookup(router_ll6, 0, ifp); 2590120892Sume if (!rt_router) 2591120892Sume goto nolladdropt; 2592120892Sume len = sizeof(*nd_opt) + ifp->if_addrlen; 2593120892Sume len = (len + 7) & ~7; /* round by 8 */ 2594120892Sume /* safety check */ 2595120892Sume if (len + (p - (u_char *)ip6) > maxlen) 2596120892Sume goto nolladdropt; 2597120892Sume if (!(rt_router->rt_flags & RTF_GATEWAY) && 2598120892Sume (rt_router->rt_flags & RTF_LLINFO) && 2599120892Sume (rt_router->rt_gateway->sa_family == AF_LINK) && 2600120892Sume (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) && 2601120892Sume sdl->sdl_alen) { 2602120892Sume nd_opt = (struct nd_opt_hdr *)p; 2603120892Sume nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 2604120892Sume nd_opt->nd_opt_len = len >> 3; 2605120892Sume lladdr = (char *)(nd_opt + 1); 2606120892Sume bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen); 2607120892Sume p += len; 2608120892Sume } 2609120893Sume } 261053541Sshinnolladdropt:; 261153541Sshin 261253541Sshin m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 261353541Sshin 261453541Sshin /* just to be safe */ 261562587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 261653541Sshin if (m0->m_flags & M_DECRYPTED) 261753541Sshin goto noredhdropt; 261862587Sitojun#endif 261962587Sitojun if (p - (u_char *)ip6 > maxlen) 262062587Sitojun goto noredhdropt; 262153541Sshin 2622120891Sume { 2623120891Sume /* redirected header option */ 2624120891Sume int len; 2625120891Sume struct nd_opt_rd_hdr *nd_opt_rh; 262653541Sshin 2627120891Sume /* 2628120891Sume * compute the maximum size for icmp6 redirect header option. 2629120891Sume * XXX room for auth header? 2630120891Sume */ 2631120891Sume len = maxlen - (p - (u_char *)ip6); 2632120891Sume len &= ~7; 263353541Sshin 2634120891Sume /* This is just for simplicity. */ 2635120891Sume if (m0->m_pkthdr.len != m0->m_len) { 2636120891Sume if (m0->m_next) { 2637120891Sume m_freem(m0->m_next); 2638120891Sume m0->m_next = NULL; 2639120891Sume } 2640120891Sume m0->m_pkthdr.len = m0->m_len; 264153541Sshin } 264253541Sshin 2643120891Sume /* 2644120891Sume * Redirected header option spec (RFC2461 4.6.3) talks nothing 2645120891Sume * about padding/truncate rule for the original IP packet. 2646120891Sume * From the discussion on IPv6imp in Feb 1999, 2647120891Sume * the consensus was: 2648120891Sume * - "attach as much as possible" is the goal 2649120891Sume * - pad if not aligned (original size can be guessed by 2650120891Sume * original ip6 header) 2651120891Sume * Following code adds the padding if it is simple enough, 2652120891Sume * and truncates if not. 2653120891Sume */ 2654120891Sume if (m0->m_next || m0->m_pkthdr.len != m0->m_len) 2655120891Sume panic("assumption failed in %s:%d", __FILE__, 2656120891Sume __LINE__); 265753541Sshin 2658120891Sume if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { 2659120891Sume /* not enough room, truncate */ 2660120891Sume m0->m_pkthdr.len = m0->m_len = len - 2661120891Sume sizeof(*nd_opt_rh); 2662120891Sume } else { 2663120891Sume /* enough room, pad or truncate */ 2664120891Sume size_t extra; 266553541Sshin 2666120891Sume extra = m0->m_pkthdr.len % 8; 2667120891Sume if (extra) { 2668120891Sume /* pad if easy enough, truncate if not */ 2669120891Sume if (8 - extra <= M_TRAILINGSPACE(m0)) { 2670120891Sume /* pad */ 2671120891Sume m0->m_len += (8 - extra); 2672120891Sume m0->m_pkthdr.len += (8 - extra); 2673120891Sume } else { 2674120891Sume /* truncate */ 2675120891Sume m0->m_pkthdr.len -= extra; 2676120891Sume m0->m_len -= extra; 2677120891Sume } 267853541Sshin } 2679120891Sume len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); 2680120891Sume m0->m_pkthdr.len = m0->m_len = len - 2681120891Sume sizeof(*nd_opt_rh); 268253541Sshin } 268353541Sshin 2684120891Sume nd_opt_rh = (struct nd_opt_rd_hdr *)p; 2685120891Sume bzero(nd_opt_rh, sizeof(*nd_opt_rh)); 2686120891Sume nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; 2687120891Sume nd_opt_rh->nd_opt_rh_len = len >> 3; 2688120891Sume p += sizeof(*nd_opt_rh); 2689120891Sume m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 269053541Sshin 2691120891Sume /* connect m0 to m */ 2692120891Sume m_tag_delete_chain(m0, NULL); 2693120891Sume m0->m_flags &= ~M_PKTHDR; 2694120891Sume m->m_next = m0; 2695120891Sume m->m_pkthdr.len = m->m_len + m0->m_len; 2696120891Sume m0 = NULL; 2697120891Sume } 269853541Sshinnoredhdropt:; 2699112781Ssuz if (m0) { 2700112781Ssuz m_freem(m0); 2701112781Ssuz m0 = NULL; 2702112781Ssuz } 270353541Sshin 2704121315Sume /* XXX: clear embedded link IDs in the inner header */ 2705121315Sume in6_clearscope(&sip6->ip6_src); 2706121315Sume in6_clearscope(&sip6->ip6_dst); 2707121315Sume in6_clearscope(&nd_rd->nd_rd_target); 2708121315Sume in6_clearscope(&nd_rd->nd_rd_dst); 270953541Sshin 271053541Sshin ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 271153541Sshin 271253541Sshin nd_rd->nd_rd_cksum = 0; 2713120891Sume nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2714120891Sume sizeof(*ip6), ntohs(ip6->ip6_plen)); 271553541Sshin 271653541Sshin /* send the packet to outside... */ 2717122062Sume#ifdef IPSEC 2718122062Sume /* Don't lookup socket */ 2719122062Sume (void)ipsec_setsocket(m, NULL); 2720122062Sume#endif /* IPSEC */ 2721105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 272253541Sshin if (outif) { 272353541Sshin icmp6_ifstat_inc(outif, ifs6_out_msg); 272453541Sshin icmp6_ifstat_inc(outif, ifs6_out_redirect); 272553541Sshin } 272653541Sshin icmp6stat.icp6s_outhist[ND_REDIRECT]++; 272753541Sshin 272853541Sshin return; 272953541Sshin 273053541Sshinfail: 273153541Sshin if (m) 273253541Sshin m_freem(m); 273353541Sshin if (m0) 273453541Sshin m_freem(m0); 273553541Sshin} 273653541Sshin 273753541Sshin/* 273853541Sshin * ICMPv6 socket option processing. 273953541Sshin */ 274053541Sshinint 274153541Sshinicmp6_ctloutput(so, sopt) 274253541Sshin struct socket *so; 274353541Sshin struct sockopt *sopt; 274453541Sshin{ 274553541Sshin int error = 0; 274653541Sshin int optlen; 274778064Sume struct inpcb *inp = sotoinpcb(so); 274853541Sshin int level, op, optname; 274953541Sshin 275053541Sshin if (sopt) { 275153541Sshin level = sopt->sopt_level; 275253541Sshin op = sopt->sopt_dir; 275353541Sshin optname = sopt->sopt_name; 275453541Sshin optlen = sopt->sopt_valsize; 275553541Sshin } else 275653541Sshin level = op = optname = optlen = 0; 275778064Sume 275853541Sshin if (level != IPPROTO_ICMPV6) { 275953541Sshin return EINVAL; 276053541Sshin } 276153541Sshin 276278064Sume switch (op) { 276353541Sshin case PRCO_SETOPT: 276453541Sshin switch (optname) { 276553541Sshin case ICMP6_FILTER: 276653541Sshin { 276753541Sshin struct icmp6_filter *p; 276853541Sshin 276953541Sshin if (optlen != sizeof(*p)) { 277053541Sshin error = EMSGSIZE; 277153541Sshin break; 277253541Sshin } 277353541Sshin if (inp->in6p_icmp6filt == NULL) { 277453541Sshin error = EINVAL; 277553541Sshin break; 277653541Sshin } 277753541Sshin error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen, 277853541Sshin optlen); 277953541Sshin break; 278053541Sshin } 278153541Sshin 278253541Sshin default: 278353541Sshin error = ENOPROTOOPT; 278453541Sshin break; 278553541Sshin } 278653541Sshin break; 278753541Sshin 278853541Sshin case PRCO_GETOPT: 278953541Sshin switch (optname) { 279053541Sshin case ICMP6_FILTER: 279153541Sshin { 279253541Sshin if (inp->in6p_icmp6filt == NULL) { 279353541Sshin error = EINVAL; 279453541Sshin break; 279553541Sshin } 279653541Sshin error = sooptcopyout(sopt, inp->in6p_icmp6filt, 279753541Sshin sizeof(struct icmp6_filter)); 279853541Sshin break; 279953541Sshin } 280053541Sshin 280153541Sshin default: 280253541Sshin error = ENOPROTOOPT; 280353541Sshin break; 280453541Sshin } 280553541Sshin break; 280653541Sshin } 280753541Sshin 2808120856Sume return (error); 280953541Sshin} 281053541Sshin 281153541Sshin/* 281253541Sshin * Perform rate limit check. 281353541Sshin * Returns 0 if it is okay to send the icmp6 packet. 281453541Sshin * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 281553541Sshin * limitation. 281653541Sshin * 281753541Sshin * XXX per-destination/type check necessary? 281853541Sshin */ 281953541Sshinstatic int 282053541Sshinicmp6_ratelimit(dst, type, code) 282153541Sshin const struct in6_addr *dst; /* not used at this moment */ 282253541Sshin const int type; /* not used at this moment */ 282353541Sshin const int code; /* not used at this moment */ 282453541Sshin{ 282562587Sitojun int ret; 282653541Sshin 282795023Ssuz ret = 0; /* okay to send */ 282853541Sshin 282962587Sitojun /* PPS limit */ 283078064Sume if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count, 283178064Sume icmp6errppslim)) { 283253541Sshin /* The packet is subject to rate limit */ 283362587Sitojun ret++; 283453541Sshin } 283553541Sshin 283662587Sitojun return ret; 283753541Sshin} 2838