icmp6.c revision 148892
162587Sitojun/* $FreeBSD: head/sys/netinet6/icmp6.c 148892 2005-08-09 12:24:11Z ume $ */ 278064Sume/* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ */ 362587Sitojun 4139826Simp/*- 553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 653541Sshin * All rights reserved. 753541Sshin * 853541Sshin * Redistribution and use in source and binary forms, with or without 953541Sshin * modification, are permitted provided that the following conditions 1053541Sshin * are met: 1153541Sshin * 1. Redistributions of source code must retain the above copyright 1253541Sshin * notice, this list of conditions and the following disclaimer. 1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1453541Sshin * notice, this list of conditions and the following disclaimer in the 1553541Sshin * documentation and/or other materials provided with the distribution. 1653541Sshin * 3. Neither the name of the project nor the names of its contributors 1753541Sshin * may be used to endorse or promote products derived from this software 1853541Sshin * without specific prior written permission. 1953541Sshin * 2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2353541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3053541Sshin * SUCH DAMAGE. 3153541Sshin */ 3253541Sshin 33139826Simp/*- 3453541Sshin * Copyright (c) 1982, 1986, 1988, 1993 3553541Sshin * The Regents of the University of California. All rights reserved. 3653541Sshin * 3753541Sshin * Redistribution and use in source and binary forms, with or without 3853541Sshin * modification, are permitted provided that the following conditions 3953541Sshin * are met: 4053541Sshin * 1. Redistributions of source code must retain the above copyright 4153541Sshin * notice, this list of conditions and the following disclaimer. 4253541Sshin * 2. Redistributions in binary form must reproduce the above copyright 4353541Sshin * notice, this list of conditions and the following disclaimer in the 4453541Sshin * documentation and/or other materials provided with the distribution. 4553541Sshin * 4. Neither the name of the University nor the names of its contributors 4653541Sshin * may be used to endorse or promote products derived from this software 4753541Sshin * without specific prior written permission. 4853541Sshin * 4953541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5053541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5153541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5253541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5353541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5453541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5553541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5653541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5753541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5853541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5953541Sshin * SUCH DAMAGE. 6053541Sshin * 6153541Sshin * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 6253541Sshin */ 6353541Sshin 6462587Sitojun#include "opt_inet.h" 6562587Sitojun#include "opt_inet6.h" 6655009Sshin#include "opt_ipsec.h" 6753541Sshin 6853541Sshin#include <sys/param.h> 6995759Stanimura#include <sys/domain.h> 7095759Stanimura#include <sys/kernel.h> 7195759Stanimura#include <sys/lock.h> 7278064Sume#include <sys/malloc.h> 7353541Sshin#include <sys/mbuf.h> 7453541Sshin#include <sys/protosw.h> 7595759Stanimura#include <sys/signalvar.h> 7653541Sshin#include <sys/socket.h> 7753541Sshin#include <sys/socketvar.h> 7895759Stanimura#include <sys/sx.h> 7995759Stanimura#include <sys/syslog.h> 8095759Stanimura#include <sys/systm.h> 8153541Sshin#include <sys/time.h> 8253541Sshin 8353541Sshin#include <net/if.h> 8453541Sshin#include <net/if_dl.h> 8553541Sshin#include <net/if_types.h> 8695759Stanimura#include <net/route.h> 8753541Sshin 8853541Sshin#include <netinet/in.h> 8995759Stanimura#include <netinet/in_pcb.h> 9053541Sshin#include <netinet/in_var.h> 9162587Sitojun#include <netinet/ip6.h> 9295759Stanimura#include <netinet/icmp6.h> 93122922Sandre#include <netinet/tcp_var.h> 9495759Stanimura#include <netinet6/in6_ifattach.h> 9595759Stanimura#include <netinet6/in6_pcb.h> 9695759Stanimura#include <netinet6/ip6protosw.h> 9753541Sshin#include <netinet6/ip6_var.h> 98148385Sume#include <netinet6/scope6_var.h> 9953541Sshin#include <netinet6/mld6_var.h> 10053541Sshin#include <netinet6/nd6.h> 10153541Sshin 10253541Sshin#ifdef IPSEC 10353541Sshin#include <netinet6/ipsec.h> 10453541Sshin#include <netkey/key.h> 10555009Sshin#endif 10653541Sshin 107105199Ssam#ifdef FAST_IPSEC 108105199Ssam#include <netipsec/ipsec.h> 109105199Ssam#include <netipsec/key.h> 110105199Ssam#endif 111105199Ssam 11253541Sshin#include <net/net_osdep.h> 11353541Sshin 11462587Sitojunextern struct domain inet6domain; 11553541Sshin 11662587Sitojunstruct icmp6stat icmp6stat; 11753541Sshin 118141553Srwatsonextern struct inpcbinfo ripcbinfo; 11962587Sitojunextern struct inpcbhead ripcb; 12062587Sitojunextern int icmp6errppslim; 12162587Sitojunstatic int icmp6errpps_count = 0; 12278064Sumestatic struct timeval icmp6errppslim_last; 12362587Sitojunextern int icmp6_nodeinfo; 12453541Sshin 12562587Sitojunstatic void icmp6_errcount __P((struct icmp6errstat *, int, int)); 12662587Sitojunstatic int icmp6_rip6_input __P((struct mbuf **, int)); 12762587Sitojunstatic int icmp6_ratelimit __P((const struct in6_addr *, const int, const int)); 12862587Sitojunstatic const char *icmp6_redirect_diag __P((struct in6_addr *, 12962587Sitojun struct in6_addr *, struct in6_addr *)); 13062587Sitojunstatic struct mbuf *ni6_input __P((struct mbuf *, int)); 13162587Sitojunstatic struct mbuf *ni6_nametodns __P((const char *, int, int)); 13262587Sitojunstatic int ni6_dnsmatch __P((const char *, int, const char *, int)); 13362587Sitojunstatic int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *, 134148892Sume struct ifnet **, struct in6_addr *)); 13562587Sitojunstatic int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, 13662587Sitojun struct ifnet *, int)); 137142681Sumestatic int icmp6_notify_error __P((struct mbuf **, int, int, int)); 13853541Sshin 13953541Sshin#ifdef COMPAT_RFC1885 14062587Sitojunstatic struct route_in6 icmp6_reflect_rt; 14153541Sshin#endif 14253541Sshin 14378064Sume 14453541Sshinvoid 14553541Sshinicmp6_init() 14653541Sshin{ 14753541Sshin mld6_init(); 14853541Sshin} 14953541Sshin 15062587Sitojunstatic void 15162587Sitojunicmp6_errcount(stat, type, code) 15262587Sitojun struct icmp6errstat *stat; 15362587Sitojun int type, code; 15462587Sitojun{ 15578064Sume switch (type) { 15662587Sitojun case ICMP6_DST_UNREACH: 15762587Sitojun switch (code) { 15862587Sitojun case ICMP6_DST_UNREACH_NOROUTE: 15962587Sitojun stat->icp6errs_dst_unreach_noroute++; 16062587Sitojun return; 16162587Sitojun case ICMP6_DST_UNREACH_ADMIN: 16262587Sitojun stat->icp6errs_dst_unreach_admin++; 16362587Sitojun return; 16462587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 16562587Sitojun stat->icp6errs_dst_unreach_beyondscope++; 16662587Sitojun return; 16762587Sitojun case ICMP6_DST_UNREACH_ADDR: 16862587Sitojun stat->icp6errs_dst_unreach_addr++; 16962587Sitojun return; 17062587Sitojun case ICMP6_DST_UNREACH_NOPORT: 17162587Sitojun stat->icp6errs_dst_unreach_noport++; 17262587Sitojun return; 17362587Sitojun } 17462587Sitojun break; 17562587Sitojun case ICMP6_PACKET_TOO_BIG: 17662587Sitojun stat->icp6errs_packet_too_big++; 17762587Sitojun return; 17862587Sitojun case ICMP6_TIME_EXCEEDED: 17978064Sume switch (code) { 18062587Sitojun case ICMP6_TIME_EXCEED_TRANSIT: 18162587Sitojun stat->icp6errs_time_exceed_transit++; 18262587Sitojun return; 18362587Sitojun case ICMP6_TIME_EXCEED_REASSEMBLY: 18462587Sitojun stat->icp6errs_time_exceed_reassembly++; 18562587Sitojun return; 18662587Sitojun } 18762587Sitojun break; 18862587Sitojun case ICMP6_PARAM_PROB: 18978064Sume switch (code) { 19062587Sitojun case ICMP6_PARAMPROB_HEADER: 19162587Sitojun stat->icp6errs_paramprob_header++; 19262587Sitojun return; 19362587Sitojun case ICMP6_PARAMPROB_NEXTHEADER: 19462587Sitojun stat->icp6errs_paramprob_nextheader++; 19562587Sitojun return; 19662587Sitojun case ICMP6_PARAMPROB_OPTION: 19762587Sitojun stat->icp6errs_paramprob_option++; 19862587Sitojun return; 19962587Sitojun } 20062587Sitojun break; 20162587Sitojun case ND_REDIRECT: 20262587Sitojun stat->icp6errs_redirect++; 20362587Sitojun return; 20462587Sitojun } 20562587Sitojun stat->icp6errs_unknown++; 20662587Sitojun} 20762587Sitojun 20853541Sshin/* 209148385Sume * A wrapper function for icmp6_error() necessary when the erroneous packet 210148385Sume * may not contain enough scope zone information. 211148385Sume */ 212148385Sumevoid 213148385Sumeicmp6_error2(m, type, code, param, ifp) 214148385Sume struct mbuf *m; 215148385Sume int type, code, param; 216148385Sume struct ifnet *ifp; 217148385Sume{ 218148385Sume struct ip6_hdr *ip6; 219148385Sume 220148385Sume if (ifp == NULL) 221148385Sume return; 222148385Sume 223148385Sume#ifndef PULLDOWN_TEST 224148385Sume IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 225148385Sume#else 226148385Sume if (m->m_len < sizeof(struct ip6_hdr)) { 227148385Sume m = m_pullup(m, sizeof(struct ip6_hdr)); 228148385Sume if (m == NULL) 229148385Sume return; 230148385Sume } 231148385Sume#endif 232148385Sume 233148385Sume ip6 = mtod(m, struct ip6_hdr *); 234148385Sume 235148385Sume if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0) 236148385Sume return; 237148385Sume if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) 238148385Sume return; 239148385Sume 240148385Sume icmp6_error(m, type, code, param); 241148385Sume} 242148385Sume 243148385Sume/* 24453541Sshin * Generate an error packet of type error in response to bad IP6 packet. 24553541Sshin */ 24653541Sshinvoid 24753541Sshinicmp6_error(m, type, code, param) 24853541Sshin struct mbuf *m; 24953541Sshin int type, code, param; 25053541Sshin{ 25153541Sshin struct ip6_hdr *oip6, *nip6; 25253541Sshin struct icmp6_hdr *icmp6; 25362587Sitojun u_int preplen; 25453541Sshin int off; 25562587Sitojun int nxt; 25653541Sshin 25753541Sshin icmp6stat.icp6s_error++; 25853541Sshin 25962587Sitojun /* count per-type-code statistics */ 26062587Sitojun icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code); 26162587Sitojun 26262587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 26362587Sitojun if (m->m_flags & M_DECRYPTED) { 26462587Sitojun icmp6stat.icp6s_canterror++; 26553541Sshin goto freeit; 26662587Sitojun } 26762587Sitojun#endif 26853541Sshin 26962587Sitojun#ifndef PULLDOWN_TEST 27062587Sitojun IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 27162587Sitojun#else 27262587Sitojun if (m->m_len < sizeof(struct ip6_hdr)) { 27362587Sitojun m = m_pullup(m, sizeof(struct ip6_hdr)); 27462587Sitojun if (m == NULL) 27562587Sitojun return; 27662587Sitojun } 27762587Sitojun#endif 27853541Sshin oip6 = mtod(m, struct ip6_hdr *); 27953541Sshin 28053541Sshin /* 281121472Sume * If the destination address of the erroneous packet is a multicast 282121472Sume * address, or the packet was sent using link-layer multicast, 283121472Sume * we should basically suppress sending an error (RFC 2463, Section 284121472Sume * 2.4). 285121472Sume * We have two exceptions (the item e.2 in that section): 286121472Sume * - the Pakcet Too Big message can be sent for path MTU discovery. 287121472Sume * - the Parameter Problem Message that can be allowed an icmp6 error 288121472Sume * in the option type field. This check has been done in 289121472Sume * ip6_unknown_opt(), so we can just check the type and code. 29053541Sshin */ 29153541Sshin if ((m->m_flags & (M_BCAST|M_MCAST) || 29253541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && 29353541Sshin (type != ICMP6_PACKET_TOO_BIG && 29453541Sshin (type != ICMP6_PARAM_PROB || 29553541Sshin code != ICMP6_PARAMPROB_OPTION))) 29653541Sshin goto freeit; 29753541Sshin 298121472Sume /* 299121472Sume * RFC 2463, 2.4 (e.5): source address check. 300121472Sume * XXX: the case of anycast source? 301121472Sume */ 30253541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || 30353541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) 30453541Sshin goto freeit; 30553541Sshin 30653541Sshin /* 30762587Sitojun * If we are about to send ICMPv6 against ICMPv6 error/redirect, 30862587Sitojun * don't do it. 30953541Sshin */ 31062587Sitojun nxt = -1; 31162587Sitojun off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 31262587Sitojun if (off >= 0 && nxt == IPPROTO_ICMPV6) { 31353541Sshin struct icmp6_hdr *icp; 31453541Sshin 31562587Sitojun#ifndef PULLDOWN_TEST 31662587Sitojun IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), ); 31762587Sitojun icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 31862587Sitojun#else 31962587Sitojun IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, 32062587Sitojun sizeof(*icp)); 32162587Sitojun if (icp == NULL) { 32262587Sitojun icmp6stat.icp6s_tooshort++; 32362587Sitojun return; 32462587Sitojun } 32562587Sitojun#endif 32662587Sitojun if (icp->icmp6_type < ICMP6_ECHO_REQUEST || 32762587Sitojun icp->icmp6_type == ND_REDIRECT) { 32853541Sshin /* 32962587Sitojun * ICMPv6 error 33062587Sitojun * Special case: for redirect (which is 33162587Sitojun * informational) we must not send icmp6 error. 33253541Sshin */ 33362587Sitojun icmp6stat.icp6s_canterror++; 33462587Sitojun goto freeit; 33562587Sitojun } else { 33662587Sitojun /* ICMPv6 informational - send the error */ 33753541Sshin } 33862587Sitojun } else { 33962587Sitojun /* non-ICMPv6 - send the error */ 34053541Sshin } 34153541Sshin 34253541Sshin oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */ 34353541Sshin 34453541Sshin /* Finally, do rate limitation check. */ 34553541Sshin if (icmp6_ratelimit(&oip6->ip6_src, type, code)) { 34653541Sshin icmp6stat.icp6s_toofreq++; 34753541Sshin goto freeit; 34853541Sshin } 34953541Sshin 35053541Sshin /* 35153541Sshin * OK, ICMP6 can be generated. 35253541Sshin */ 35353541Sshin 35453541Sshin if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) 35553541Sshin m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); 35653541Sshin 35762587Sitojun preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 358111119Simp M_PREPEND(m, preplen, M_DONTWAIT); 35962587Sitojun if (m && m->m_len < preplen) 36062587Sitojun m = m_pullup(m, preplen); 36153541Sshin if (m == NULL) { 36278064Sume nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__)); 36353541Sshin return; 36453541Sshin } 36553541Sshin 36653541Sshin nip6 = mtod(m, struct ip6_hdr *); 36753541Sshin nip6->ip6_src = oip6->ip6_src; 36853541Sshin nip6->ip6_dst = oip6->ip6_dst; 36953541Sshin 370121315Sume in6_clearscope(&oip6->ip6_src); 371121315Sume in6_clearscope(&oip6->ip6_dst); 37253541Sshin 37353541Sshin icmp6 = (struct icmp6_hdr *)(nip6 + 1); 37453541Sshin icmp6->icmp6_type = type; 37553541Sshin icmp6->icmp6_code = code; 37653541Sshin icmp6->icmp6_pptr = htonl((u_int32_t)param); 37753541Sshin 37878064Sume /* 37978064Sume * icmp6_reflect() is designed to be in the input path. 38078064Sume * icmp6_error() can be called from both input and outut path, 38178064Sume * and if we are in output path rcvif could contain bogus value. 38278064Sume * clear m->m_pkthdr.rcvif for safety, we should have enough scope 38378064Sume * information in ip header (nip6). 38478064Sume */ 38578064Sume m->m_pkthdr.rcvif = NULL; 38678064Sume 38753541Sshin icmp6stat.icp6s_outhist[type]++; 38895023Ssuz icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ 38962587Sitojun 39062587Sitojun return; 39162587Sitojun 39262587Sitojun freeit: 39362587Sitojun /* 39462587Sitojun * If we can't tell wheter or not we can generate ICMP6, free it. 39562587Sitojun */ 39662587Sitojun m_freem(m); 39753541Sshin} 39853541Sshin 39953541Sshin/* 40053541Sshin * Process a received ICMP6 message. 40153541Sshin */ 40253541Sshinint 40353541Sshinicmp6_input(mp, offp, proto) 40453541Sshin struct mbuf **mp; 40553541Sshin int *offp, proto; 40653541Sshin{ 40753541Sshin struct mbuf *m = *mp, *n; 40853541Sshin struct ip6_hdr *ip6, *nip6; 40953541Sshin struct icmp6_hdr *icmp6, *nicmp6; 41053541Sshin int off = *offp; 41153541Sshin int icmp6len = m->m_pkthdr.len - *offp; 41253541Sshin int code, sum, noff; 41353541Sshin 41462587Sitojun#ifndef PULLDOWN_TEST 41553541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE); 41695023Ssuz /* m might change if M_LOOP. So, call mtod after this */ 41762587Sitojun#endif 41853541Sshin 41953541Sshin /* 42053541Sshin * Locate icmp6 structure in mbuf, and check 42153541Sshin * that not corrupted and of at least minimum length 42253541Sshin */ 42353541Sshin 42453541Sshin ip6 = mtod(m, struct ip6_hdr *); 42553541Sshin if (icmp6len < sizeof(struct icmp6_hdr)) { 42653541Sshin icmp6stat.icp6s_tooshort++; 42753541Sshin goto freeit; 42853541Sshin } 42953541Sshin 43053541Sshin /* 43153541Sshin * calculate the checksum 43253541Sshin */ 43362587Sitojun#ifndef PULLDOWN_TEST 43453541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 43562587Sitojun#else 43662587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 43762587Sitojun if (icmp6 == NULL) { 43862587Sitojun icmp6stat.icp6s_tooshort++; 43962587Sitojun return IPPROTO_DONE; 44062587Sitojun } 44162587Sitojun#endif 44253541Sshin code = icmp6->icmp6_code; 44353541Sshin 44453541Sshin if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { 44578064Sume nd6log((LOG_ERR, 44653541Sshin "ICMP6 checksum error(%d|%x) %s\n", 44778064Sume icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src))); 44853541Sshin icmp6stat.icp6s_checksum++; 44953541Sshin goto freeit; 45053541Sshin } 45153541Sshin 45283934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 45353541Sshin /* 45453541Sshin * Deliver very specific ICMP6 type only. 45553541Sshin * This is important to deilver TOOBIG. Otherwise PMTUD 45653541Sshin * will not work. 45753541Sshin */ 45853541Sshin switch (icmp6->icmp6_type) { 45953541Sshin case ICMP6_DST_UNREACH: 46053541Sshin case ICMP6_PACKET_TOO_BIG: 46153541Sshin case ICMP6_TIME_EXCEEDED: 46253541Sshin break; 46353541Sshin default: 46453541Sshin goto freeit; 46553541Sshin } 46653541Sshin } 46753541Sshin 46853541Sshin icmp6stat.icp6s_inhist[icmp6->icmp6_type]++; 46953541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg); 47053541Sshin if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) 47153541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error); 47253541Sshin 47353541Sshin switch (icmp6->icmp6_type) { 47453541Sshin case ICMP6_DST_UNREACH: 47553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach); 47653541Sshin switch (code) { 47753541Sshin case ICMP6_DST_UNREACH_NOROUTE: 47853541Sshin code = PRC_UNREACH_NET; 47953541Sshin break; 48053541Sshin case ICMP6_DST_UNREACH_ADMIN: 48153541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib); 48262587Sitojun code = PRC_UNREACH_PROTOCOL; /* is this a good code? */ 48362587Sitojun break; 48453541Sshin case ICMP6_DST_UNREACH_ADDR: 48562587Sitojun code = PRC_HOSTDEAD; 48653541Sshin break; 48762587Sitojun#ifdef COMPAT_RFC1885 48853541Sshin case ICMP6_DST_UNREACH_NOTNEIGHBOR: 48953541Sshin code = PRC_UNREACH_SRCFAIL; 49053541Sshin break; 49162587Sitojun#else 49262587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 49362587Sitojun /* I mean "source address was incorrect." */ 49462587Sitojun code = PRC_PARAMPROB; 49562587Sitojun break; 49662587Sitojun#endif 49753541Sshin case ICMP6_DST_UNREACH_NOPORT: 49853541Sshin code = PRC_UNREACH_PORT; 49953541Sshin break; 50053541Sshin default: 50153541Sshin goto badcode; 50253541Sshin } 50353541Sshin goto deliver; 50453541Sshin break; 50553541Sshin 50653541Sshin case ICMP6_PACKET_TOO_BIG: 50753541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig); 50853541Sshin 509126194Sume /* validation is made in icmp6_mtudisc_update */ 510126194Sume 51153541Sshin code = PRC_MSGSIZE; 51253541Sshin 51362587Sitojun /* 51462587Sitojun * Updating the path MTU will be done after examining 51562587Sitojun * intermediate extension headers. 51662587Sitojun */ 51753541Sshin goto deliver; 51853541Sshin break; 51953541Sshin 52053541Sshin case ICMP6_TIME_EXCEEDED: 52153541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed); 52253541Sshin switch (code) { 52353541Sshin case ICMP6_TIME_EXCEED_TRANSIT: 52453541Sshin case ICMP6_TIME_EXCEED_REASSEMBLY: 52553541Sshin code += PRC_TIMXCEED_INTRANS; 52653541Sshin break; 52753541Sshin default: 52853541Sshin goto badcode; 52953541Sshin } 53053541Sshin goto deliver; 53153541Sshin break; 53253541Sshin 53353541Sshin case ICMP6_PARAM_PROB: 53453541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob); 53553541Sshin switch (code) { 53653541Sshin case ICMP6_PARAMPROB_NEXTHEADER: 53753541Sshin code = PRC_UNREACH_PROTOCOL; 53853541Sshin break; 53953541Sshin case ICMP6_PARAMPROB_HEADER: 54053541Sshin case ICMP6_PARAMPROB_OPTION: 54153541Sshin code = PRC_PARAMPROB; 54253541Sshin break; 54353541Sshin default: 54453541Sshin goto badcode; 54553541Sshin } 54653541Sshin goto deliver; 54753541Sshin break; 54853541Sshin 54953541Sshin case ICMP6_ECHO_REQUEST: 55053541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo); 55153541Sshin if (code != 0) 55253541Sshin goto badcode; 55353541Sshin if ((n = m_copy(m, 0, M_COPYALL)) == NULL) { 55453541Sshin /* Give up remote */ 55553541Sshin break; 55653541Sshin } 55762587Sitojun if ((n->m_flags & M_EXT) != 0 55862587Sitojun || n->m_len < off + sizeof(struct icmp6_hdr)) { 55953541Sshin struct mbuf *n0 = n; 56062587Sitojun const int maxlen = sizeof(*nip6) + sizeof(*nicmp6); 561108741Ssam int n0len; 56253541Sshin 563111119Simp MGETHDR(n, M_DONTWAIT, n0->m_type); 564111070Ssam n0len = n0->m_pkthdr.len; /* save for use below */ 565111070Ssam if (n) 566111070Ssam M_MOVE_PKTHDR(n, n0); 56762587Sitojun if (n && maxlen >= MHLEN) { 568111119Simp MCLGET(n, M_DONTWAIT); 56962587Sitojun if ((n->m_flags & M_EXT) == 0) { 57062587Sitojun m_free(n); 57162587Sitojun n = NULL; 57262587Sitojun } 57362587Sitojun } 57453541Sshin if (n == NULL) { 57553541Sshin /* Give up remote */ 57653541Sshin m_freem(n0); 57753541Sshin break; 57853541Sshin } 57953541Sshin /* 58053541Sshin * Copy IPv6 and ICMPv6 only. 58153541Sshin */ 58253541Sshin nip6 = mtod(n, struct ip6_hdr *); 58353541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 58453541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 58553541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 58662587Sitojun noff = sizeof(struct ip6_hdr); 587108741Ssam /* new mbuf contains only ipv6+icmpv6 headers */ 588108741Ssam n->m_len = noff + sizeof(struct icmp6_hdr); 58953541Sshin /* 590120891Sume * Adjust mbuf. ip6_plen will be adjusted in 59162587Sitojun * ip6_output(). 59253541Sshin */ 59362587Sitojun m_adj(n0, off + sizeof(struct icmp6_hdr)); 594108741Ssam /* recalculate complete packet size */ 595108741Ssam n->m_pkthdr.len = n0len + (noff - off); 59662587Sitojun n->m_next = n0; 59753541Sshin } else { 59853541Sshin nip6 = mtod(n, struct ip6_hdr *); 59953541Sshin nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off); 60053541Sshin noff = off; 60153541Sshin } 60253541Sshin nicmp6->icmp6_type = ICMP6_ECHO_REPLY; 60353541Sshin nicmp6->icmp6_code = 0; 60453541Sshin if (n) { 60553541Sshin icmp6stat.icp6s_reflect++; 60653541Sshin icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++; 60753541Sshin icmp6_reflect(n, noff); 60853541Sshin } 60953541Sshin break; 61053541Sshin 61153541Sshin case ICMP6_ECHO_REPLY: 61253541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply); 61353541Sshin if (code != 0) 61453541Sshin goto badcode; 61553541Sshin break; 61653541Sshin 61796116Sume case MLD_LISTENER_QUERY: 61896116Sume case MLD_LISTENER_REPORT: 61996116Sume if (icmp6len < sizeof(struct mld_hdr)) 62053541Sshin goto badlen; 62196116Sume if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */ 62253541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery); 62353541Sshin else 62453541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport); 625111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 62662587Sitojun /* give up local */ 62762587Sitojun mld6_input(m, off); 62862587Sitojun m = NULL; 62962587Sitojun goto freeit; 63062587Sitojun } 63162587Sitojun mld6_input(n, off); 63253541Sshin /* m stays. */ 63353541Sshin break; 63453541Sshin 63596116Sume case MLD_LISTENER_DONE: 63653541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone); 63796116Sume if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */ 63853541Sshin goto badlen; 63953541Sshin break; /* nothing to be done in kernel */ 64053541Sshin 64196116Sume case MLD_MTRACE_RESP: 64296116Sume case MLD_MTRACE: 643120891Sume /* XXX: these two are experimental. not officially defined. */ 64453541Sshin /* XXX: per-interface statistics? */ 64562587Sitojun break; /* just pass it to applications */ 64653541Sshin 64753541Sshin case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ 64853541Sshin { 64953541Sshin enum { WRU, FQDN } mode; 65053541Sshin 65162587Sitojun if (!icmp6_nodeinfo) 65262587Sitojun break; 65362587Sitojun 65453541Sshin if (icmp6len == sizeof(struct icmp6_hdr) + 4) 65553541Sshin mode = WRU; 65662587Sitojun else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) 65753541Sshin mode = FQDN; 65853541Sshin else 65953541Sshin goto badlen; 66053541Sshin 66153541Sshin#define hostnamelen strlen(hostname) 66253541Sshin if (mode == FQDN) { 66362587Sitojun#ifndef PULLDOWN_TEST 66453541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), 665120891Sume IPPROTO_DONE); 66662587Sitojun#endif 66762587Sitojun n = m_copy(m, 0, M_COPYALL); 66862587Sitojun if (n) 66962587Sitojun n = ni6_input(n, off); 67062587Sitojun /* XXX meaningless if n == NULL */ 67153541Sshin noff = sizeof(struct ip6_hdr); 67253541Sshin } else { 67353541Sshin u_char *p; 67462587Sitojun int maxlen, maxhlen; 67553541Sshin 676120891Sume if ((icmp6_nodeinfo & 5) != 5) 67778064Sume break; 67878064Sume 67962587Sitojun if (code != 0) 68062587Sitojun goto badcode; 68162587Sitojun maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4; 68262587Sitojun if (maxlen >= MCLBYTES) { 68362587Sitojun /* Give up remote */ 68462587Sitojun break; 68562587Sitojun } 686111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 68762587Sitojun if (n && maxlen > MHLEN) { 688111119Simp MCLGET(n, M_DONTWAIT); 68962587Sitojun if ((n->m_flags & M_EXT) == 0) { 69062587Sitojun m_free(n); 69162587Sitojun n = NULL; 69262587Sitojun } 69362587Sitojun } 694111119Simp if (!m_dup_pkthdr(n, m, M_DONTWAIT)) { 695108466Ssam /* 696108466Ssam * Previous code did a blind M_COPY_PKTHDR 697108466Ssam * and said "just for rcvif". If true, then 698108466Ssam * we could tolerate the dup failing (due to 699108466Ssam * the deep copy of the tag chain). For now 700108466Ssam * be conservative and just fail. 701108466Ssam */ 702108466Ssam m_free(n); 703108466Ssam n = NULL; 704108466Ssam } 70553541Sshin if (n == NULL) { 70653541Sshin /* Give up remote */ 70753541Sshin break; 70853541Sshin } 70978064Sume n->m_pkthdr.rcvif = NULL; 71062587Sitojun n->m_len = 0; 71162587Sitojun maxhlen = M_TRAILINGSPACE(n) - maxlen; 71262587Sitojun if (maxhlen > hostnamelen) 71362587Sitojun maxhlen = hostnamelen; 71453541Sshin /* 71553541Sshin * Copy IPv6 and ICMPv6 only. 71653541Sshin */ 71753541Sshin nip6 = mtod(n, struct ip6_hdr *); 71853541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 71953541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 72053541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 72153541Sshin p = (u_char *)(nicmp6 + 1); 72253541Sshin bzero(p, 4); 72395023Ssuz bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */ 72453541Sshin noff = sizeof(struct ip6_hdr); 72553541Sshin n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 72662587Sitojun sizeof(struct icmp6_hdr) + 4 + maxhlen; 72753541Sshin nicmp6->icmp6_type = ICMP6_WRUREPLY; 72853541Sshin nicmp6->icmp6_code = 0; 72953541Sshin } 73053541Sshin#undef hostnamelen 73153541Sshin if (n) { 73253541Sshin icmp6stat.icp6s_reflect++; 73353541Sshin icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++; 73453541Sshin icmp6_reflect(n, noff); 73553541Sshin } 73653541Sshin break; 73753541Sshin } 73853541Sshin 73953541Sshin case ICMP6_WRUREPLY: 74053541Sshin if (code != 0) 74153541Sshin goto badcode; 74253541Sshin break; 74353541Sshin 74453541Sshin case ND_ROUTER_SOLICIT: 74553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit); 74653541Sshin if (code != 0) 74753541Sshin goto badcode; 74853541Sshin if (icmp6len < sizeof(struct nd_router_solicit)) 74953541Sshin goto badlen; 750111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 75162587Sitojun /* give up local */ 75262587Sitojun nd6_rs_input(m, off, icmp6len); 75362587Sitojun m = NULL; 75462587Sitojun goto freeit; 75562587Sitojun } 75662587Sitojun nd6_rs_input(n, off, icmp6len); 75753541Sshin /* m stays. */ 75853541Sshin break; 75953541Sshin 76053541Sshin case ND_ROUTER_ADVERT: 76153541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert); 76253541Sshin if (code != 0) 76353541Sshin goto badcode; 76453541Sshin if (icmp6len < sizeof(struct nd_router_advert)) 76553541Sshin goto badlen; 766111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 76762587Sitojun /* give up local */ 76862587Sitojun nd6_ra_input(m, off, icmp6len); 76962587Sitojun m = NULL; 77062587Sitojun goto freeit; 77162587Sitojun } 77262587Sitojun nd6_ra_input(n, off, icmp6len); 77353541Sshin /* m stays. */ 77453541Sshin break; 77553541Sshin 77653541Sshin case ND_NEIGHBOR_SOLICIT: 77753541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit); 77853541Sshin if (code != 0) 77953541Sshin goto badcode; 78053541Sshin if (icmp6len < sizeof(struct nd_neighbor_solicit)) 78153541Sshin goto badlen; 782111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 78362587Sitojun /* give up local */ 78462587Sitojun nd6_ns_input(m, off, icmp6len); 78562587Sitojun m = NULL; 78662587Sitojun goto freeit; 78762587Sitojun } 78862587Sitojun nd6_ns_input(n, off, icmp6len); 78953541Sshin /* m stays. */ 79053541Sshin break; 79153541Sshin 79253541Sshin case ND_NEIGHBOR_ADVERT: 79353541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert); 79453541Sshin if (code != 0) 79553541Sshin goto badcode; 79653541Sshin if (icmp6len < sizeof(struct nd_neighbor_advert)) 79753541Sshin goto badlen; 798111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 79962587Sitojun /* give up local */ 80062587Sitojun nd6_na_input(m, off, icmp6len); 80162587Sitojun m = NULL; 80262587Sitojun goto freeit; 80362587Sitojun } 80462587Sitojun nd6_na_input(n, off, icmp6len); 80553541Sshin /* m stays. */ 80653541Sshin break; 80753541Sshin 80853541Sshin case ND_REDIRECT: 80953541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect); 81053541Sshin if (code != 0) 81153541Sshin goto badcode; 81253541Sshin if (icmp6len < sizeof(struct nd_redirect)) 81353541Sshin goto badlen; 814111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 81562587Sitojun /* give up local */ 81662587Sitojun icmp6_redirect_input(m, off); 81762587Sitojun m = NULL; 81862587Sitojun goto freeit; 81962587Sitojun } 82062587Sitojun icmp6_redirect_input(n, off); 82153541Sshin /* m stays. */ 82253541Sshin break; 82353541Sshin 82453541Sshin case ICMP6_ROUTER_RENUMBERING: 82553541Sshin if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && 82653541Sshin code != ICMP6_ROUTER_RENUMBERING_RESULT) 82753541Sshin goto badcode; 82853541Sshin if (icmp6len < sizeof(struct icmp6_router_renum)) 82953541Sshin goto badlen; 83053541Sshin break; 83153541Sshin 83253541Sshin default: 83378064Sume nd6log((LOG_DEBUG, 83478064Sume "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", 83578064Sume icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src), 83678064Sume ip6_sprintf(&ip6->ip6_dst), 83778064Sume m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0)); 83853541Sshin if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { 83953541Sshin /* ICMPv6 error: MUST deliver it by spec... */ 84053541Sshin code = PRC_NCMDS; 84153541Sshin /* deliver */ 84253541Sshin } else { 84353541Sshin /* ICMPv6 informational: MUST not deliver */ 84453541Sshin break; 84553541Sshin } 84653541Sshin deliver: 847142681Sume if (icmp6_notify_error(&m, off, icmp6len, code)) { 84878064Sume /* In this case, m should've been freed. */ 849120856Sume return (IPPROTO_DONE); 85053541Sshin } 85178064Sume break; 85278064Sume 85378064Sume badcode: 85478064Sume icmp6stat.icp6s_badcode++; 85578064Sume break; 85678064Sume 85778064Sume badlen: 85878064Sume icmp6stat.icp6s_badlen++; 85978064Sume break; 86078064Sume } 86178064Sume 86278064Sume /* deliver the packet to appropriate sockets */ 86378064Sume icmp6_rip6_input(&m, *offp); 86478064Sume 86578064Sume return IPPROTO_DONE; 86678064Sume 86778064Sume freeit: 86878064Sume m_freem(m); 86978064Sume return IPPROTO_DONE; 87078064Sume} 87178064Sume 87278064Sumestatic int 873142681Sumeicmp6_notify_error(mp, off, icmp6len, code) 874142681Sume struct mbuf **mp; 875113799Sobrien int off, icmp6len, code; 87678064Sume{ 877142681Sume struct mbuf *m = *mp; 87878064Sume struct icmp6_hdr *icmp6; 87978064Sume struct ip6_hdr *eip6; 88078064Sume u_int32_t notifymtu; 88178064Sume struct sockaddr_in6 icmp6src, icmp6dst; 88278064Sume 88378064Sume if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { 88478064Sume icmp6stat.icp6s_tooshort++; 88578064Sume goto freeit; 88678064Sume } 88762587Sitojun#ifndef PULLDOWN_TEST 88878064Sume IP6_EXTHDR_CHECK(m, off, 889120891Sume sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1); 89078064Sume icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 89162587Sitojun#else 89278064Sume IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 893120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 89478064Sume if (icmp6 == NULL) { 89578064Sume icmp6stat.icp6s_tooshort++; 896120856Sume return (-1); 89778064Sume } 89862587Sitojun#endif 89978064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 90053541Sshin 90178064Sume /* Detect the upper level protocol */ 90278064Sume { 90353541Sshin void (*ctlfunc) __P((int, struct sockaddr *, void *)); 90453541Sshin u_int8_t nxt = eip6->ip6_nxt; 90553541Sshin int eoff = off + sizeof(struct icmp6_hdr) + 906120891Sume sizeof(struct ip6_hdr); 90753541Sshin struct ip6ctlparam ip6cp; 90862587Sitojun struct in6_addr *finaldst = NULL; 90962587Sitojun int icmp6type = icmp6->icmp6_type; 91062587Sitojun struct ip6_frag *fh; 91162587Sitojun struct ip6_rthdr *rth; 91262587Sitojun struct ip6_rthdr0 *rth0; 91362587Sitojun int rthlen; 91453541Sshin 91595023Ssuz while (1) { /* XXX: should avoid infinite loop explicitly? */ 91653541Sshin struct ip6_ext *eh; 91753541Sshin 91878064Sume switch (nxt) { 91953541Sshin case IPPROTO_HOPOPTS: 92053541Sshin case IPPROTO_DSTOPTS: 92153541Sshin case IPPROTO_AH: 92262587Sitojun#ifndef PULLDOWN_TEST 923120891Sume IP6_EXTHDR_CHECK(m, 0, 924120891Sume eoff + sizeof(struct ip6_ext), -1); 925120892Sume eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff); 92662587Sitojun#else 92762587Sitojun IP6_EXTHDR_GET(eh, struct ip6_ext *, m, 928120891Sume eoff, sizeof(*eh)); 92962587Sitojun if (eh == NULL) { 93062587Sitojun icmp6stat.icp6s_tooshort++; 931120856Sume return (-1); 93262587Sitojun } 93362587Sitojun#endif 934120891Sume 93553541Sshin if (nxt == IPPROTO_AH) 93653541Sshin eoff += (eh->ip6e_len + 2) << 2; 93753541Sshin else 93853541Sshin eoff += (eh->ip6e_len + 1) << 3; 93953541Sshin nxt = eh->ip6e_nxt; 94053541Sshin break; 94162587Sitojun case IPPROTO_ROUTING: 94262587Sitojun /* 94362587Sitojun * When the erroneous packet contains a 94462587Sitojun * routing header, we should examine the 94562587Sitojun * header to determine the final destination. 94662587Sitojun * Otherwise, we can't properly update 94762587Sitojun * information that depends on the final 94862587Sitojun * destination (e.g. path MTU). 94962587Sitojun */ 95062587Sitojun#ifndef PULLDOWN_TEST 951120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1); 952120891Sume rth = (struct ip6_rthdr *) 953120891Sume (mtod(m, caddr_t) + eoff); 95462587Sitojun#else 95562587Sitojun IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m, 956120891Sume eoff, sizeof(*rth)); 95762587Sitojun if (rth == NULL) { 95862587Sitojun icmp6stat.icp6s_tooshort++; 959120856Sume return (-1); 96062587Sitojun } 96162587Sitojun#endif 96262587Sitojun rthlen = (rth->ip6r_len + 1) << 3; 96362587Sitojun /* 96462587Sitojun * XXX: currently there is no 96562587Sitojun * officially defined type other 96662587Sitojun * than type-0. 96762587Sitojun * Note that if the segment left field 96862587Sitojun * is 0, all intermediate hops must 96962587Sitojun * have been passed. 97062587Sitojun */ 97162587Sitojun if (rth->ip6r_segleft && 97262587Sitojun rth->ip6r_type == IPV6_RTHDR_TYPE_0) { 97362587Sitojun int hops; 97462587Sitojun 97562587Sitojun#ifndef PULLDOWN_TEST 976120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1); 977120891Sume rth0 = (struct ip6_rthdr0 *) 978120891Sume (mtod(m, caddr_t) + eoff); 97962587Sitojun#else 98062587Sitojun IP6_EXTHDR_GET(rth0, 981120891Sume struct ip6_rthdr0 *, m, 982120891Sume eoff, rthlen); 98362587Sitojun if (rth0 == NULL) { 98462587Sitojun icmp6stat.icp6s_tooshort++; 985120856Sume return (-1); 98662587Sitojun } 98762587Sitojun#endif 98862587Sitojun /* just ignore a bogus header */ 98962587Sitojun if ((rth0->ip6r0_len % 2) == 0 && 99062587Sitojun (hops = rth0->ip6r0_len/2)) 99162587Sitojun finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1); 99262587Sitojun } 99362587Sitojun eoff += rthlen; 99462587Sitojun nxt = rth->ip6r_nxt; 99562587Sitojun break; 99662587Sitojun case IPPROTO_FRAGMENT: 99762587Sitojun#ifndef PULLDOWN_TEST 99862587Sitojun IP6_EXTHDR_CHECK(m, 0, eoff + 999120891Sume sizeof(struct ip6_frag), -1); 1000120891Sume fh = (struct ip6_frag *)(mtod(m, caddr_t) + 1001120891Sume eoff); 100262587Sitojun#else 100362587Sitojun IP6_EXTHDR_GET(fh, struct ip6_frag *, m, 1004120891Sume eoff, sizeof(*fh)); 100562587Sitojun if (fh == NULL) { 100662587Sitojun icmp6stat.icp6s_tooshort++; 1007120856Sume return (-1); 100862587Sitojun } 100962587Sitojun#endif 101062587Sitojun /* 101162587Sitojun * Data after a fragment header is meaningless 101262587Sitojun * unless it is the first fragment, but 101362587Sitojun * we'll go to the notify label for path MTU 101462587Sitojun * discovery. 101562587Sitojun */ 101662587Sitojun if (fh->ip6f_offlg & IP6F_OFF_MASK) 101762587Sitojun goto notify; 101862587Sitojun 101962587Sitojun eoff += sizeof(struct ip6_frag); 102062587Sitojun nxt = fh->ip6f_nxt; 102162587Sitojun break; 102253541Sshin default: 102362587Sitojun /* 102462587Sitojun * This case includes ESP and the No Next 102595023Ssuz * Header. In such cases going to the notify 102662587Sitojun * label does not have any meaning 102762587Sitojun * (i.e. ctlfunc will be NULL), but we go 102862587Sitojun * anyway since we might have to update 102962587Sitojun * path MTU information. 103062587Sitojun */ 103153541Sshin goto notify; 103253541Sshin } 103353541Sshin } 103478064Sume notify: 103562587Sitojun#ifndef PULLDOWN_TEST 103653541Sshin icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 103762587Sitojun#else 103862587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 1039120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 104062587Sitojun if (icmp6 == NULL) { 104162587Sitojun icmp6stat.icp6s_tooshort++; 1042120856Sume return (-1); 104362587Sitojun } 104462587Sitojun#endif 104578064Sume 1046120891Sume /* 1047120891Sume * retrieve parameters from the inner IPv6 header, and convert 1048120891Sume * them into sockaddr structures. 1049120891Sume * XXX: there is no guarantee that the source or destination 1050120891Sume * addresses of the inner packet are in the same scope as 1051120891Sume * the addresses of the icmp packet. But there is no other 1052120891Sume * way to determine the zone. 1053120891Sume */ 105478064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 1055120891Sume 105678064Sume bzero(&icmp6dst, sizeof(icmp6dst)); 105778064Sume icmp6dst.sin6_len = sizeof(struct sockaddr_in6); 105878064Sume icmp6dst.sin6_family = AF_INET6; 105978064Sume if (finaldst == NULL) 106078064Sume icmp6dst.sin6_addr = eip6->ip6_dst; 106178064Sume else 106278064Sume icmp6dst.sin6_addr = *finaldst; 1063148385Sume if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL)) 1064121315Sume goto freeit; 106578064Sume bzero(&icmp6src, sizeof(icmp6src)); 106678064Sume icmp6src.sin6_len = sizeof(struct sockaddr_in6); 106778064Sume icmp6src.sin6_family = AF_INET6; 106878064Sume icmp6src.sin6_addr = eip6->ip6_src; 1069148385Sume if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL)) 1070121315Sume goto freeit; 1071148385Sume icmp6src.sin6_flowinfo = 1072148385Sume (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); 107378064Sume 107478064Sume if (finaldst == NULL) 107578064Sume finaldst = &eip6->ip6_dst; 107678064Sume ip6cp.ip6c_m = m; 107778064Sume ip6cp.ip6c_icmp6 = icmp6; 107878064Sume ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1); 107978064Sume ip6cp.ip6c_off = eoff; 108078064Sume ip6cp.ip6c_finaldst = finaldst; 108178064Sume ip6cp.ip6c_src = &icmp6src; 108278064Sume ip6cp.ip6c_nxt = nxt; 108378064Sume 108462587Sitojun if (icmp6type == ICMP6_PACKET_TOO_BIG) { 108578064Sume notifymtu = ntohl(icmp6->icmp6_mtu); 108678064Sume ip6cp.ip6c_cmdarg = (void *)¬ifymtu; 108778064Sume icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ 108862587Sitojun } 108962587Sitojun 109053541Sshin ctlfunc = (void (*) __P((int, struct sockaddr *, void *))) 1091120891Sume (inet6sw[ip6_protox[nxt]].pr_ctlinput); 109253541Sshin if (ctlfunc) { 109378064Sume (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, 1094120891Sume &ip6cp); 109553541Sshin } 109653541Sshin } 1097142681Sume *mp = m; 1098120856Sume return (0); 109953541Sshin 110078064Sume freeit: 110153541Sshin m_freem(m); 1102120856Sume return (-1); 110353541Sshin} 110453541Sshin 110578064Sumevoid 110678064Sumeicmp6_mtudisc_update(ip6cp, validated) 110778064Sume struct ip6ctlparam *ip6cp; 110878064Sume int validated; 110962587Sitojun{ 111078064Sume struct in6_addr *dst = ip6cp->ip6c_finaldst; 111178064Sume struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; 111278064Sume struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ 111362587Sitojun u_int mtu = ntohl(icmp6->icmp6_mtu); 1114122922Sandre struct in_conninfo inc; 111562587Sitojun 1116121472Sume#if 0 1117121472Sume /* 1118121472Sume * RFC2460 section 5, last paragraph. 1119121472Sume * even though minimum link MTU for IPv6 is IPV6_MMTU, 1120121472Sume * we may see ICMPv6 too big with mtu < IPV6_MMTU 1121121472Sume * due to packet translator in the middle. 1122121472Sume * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for 1123121472Sume * special handling. 1124121472Sume */ 1125121472Sume if (mtu < IPV6_MMTU) 1126121472Sume return; 1127121472Sume#endif 1128121472Sume 1129121472Sume /* 1130121472Sume * we reject ICMPv6 too big with abnormally small value. 1131121472Sume * XXX what is the good definition of "abnormally small"? 1132121472Sume */ 1133121472Sume if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) 1134121472Sume return; 1135121472Sume 113678064Sume if (!validated) 113778064Sume return; 113878064Sume 1139122922Sandre bzero(&inc, sizeof(inc)); 1140122922Sandre inc.inc_flags = 1; /* IPv6 */ 1141122922Sandre inc.inc6_faddr = *dst; 1142148385Sume if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL)) 1143148385Sume return; 114462587Sitojun 1145126194Sume if (mtu < tcp_maxmtu6(&inc)) { 1146122922Sandre tcp_hc_updatemtu(&inc, mtu); 1147122922Sandre icmp6stat.icp6s_pmtuchg++; 114862587Sitojun } 114962587Sitojun} 115062587Sitojun 115153541Sshin/* 115278064Sume * Process a Node Information Query packet, based on 115378064Sume * draft-ietf-ipngwg-icmp-name-lookups-07. 1154120891Sume * 115562587Sitojun * Spec incompatibilities: 115662587Sitojun * - IPv6 Subject address handling 115762587Sitojun * - IPv4 Subject address handling support missing 115862587Sitojun * - Proxy reply (answer even if it's not for me) 115962587Sitojun * - joins NI group address at in6_ifattach() time only, does not cope 116062587Sitojun * with hostname changes by sethostname(3) 116153541Sshin */ 116262587Sitojun#define hostnamelen strlen(hostname) 116353541Sshinstatic struct mbuf * 116453541Sshinni6_input(m, off) 116553541Sshin struct mbuf *m; 116653541Sshin int off; 116753541Sshin{ 116862587Sitojun struct icmp6_nodeinfo *ni6, *nni6; 116953541Sshin struct mbuf *n = NULL; 117062587Sitojun u_int16_t qtype; 117162587Sitojun int subjlen; 117253541Sshin int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 117353541Sshin struct ni_reply_fqdn *fqdn; 117453541Sshin int addrs; /* for NI_QTYPE_NODEADDR */ 117553541Sshin struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ 1176148385Sume struct in6_addr in6_subj; /* subject address */ 117762587Sitojun struct ip6_hdr *ip6; 117862587Sitojun int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ 117978064Sume char *subj = NULL; 118078064Sume struct in6_ifaddr *ia6 = NULL; 118153541Sshin 118262587Sitojun ip6 = mtod(m, struct ip6_hdr *); 118362587Sitojun#ifndef PULLDOWN_TEST 118462587Sitojun ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); 118562587Sitojun#else 118662587Sitojun IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); 118762587Sitojun if (ni6 == NULL) { 118862587Sitojun /* m is already reclaimed */ 1189120891Sume return (NULL); 119062587Sitojun } 119162587Sitojun#endif 119253541Sshin 119362587Sitojun /* 119462587Sitojun * Validate IPv6 destination address. 119562587Sitojun * 119678064Sume * The Responder must discard the Query without further processing 119778064Sume * unless it is one of the Responder's unicast or anycast addresses, or 119878064Sume * a link-local scope multicast address which the Responder has joined. 1199121630Sume * [icmp-name-lookups-08, Section 4.] 120062587Sitojun */ 1201121630Sume if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1202121630Sume if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) 1203121630Sume goto bad; 1204121630Sume /* else it's a link-local multicast, fine */ 1205121630Sume } else { /* unicast or anycast */ 1206121630Sume if ((ia6 = ip6_getdstifaddr(m)) == NULL) 1207121630Sume goto bad; /* XXX impossible */ 1208121630Sume 1209121630Sume if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) && 1210121630Sume !(icmp6_nodeinfo & 4)) { 121178064Sume nd6log((LOG_DEBUG, "ni6_input: ignore node info to " 121278064Sume "a temporary address in %s:%d", 121378064Sume __FILE__, __LINE__)); 121478064Sume goto bad; 121578064Sume } 1216121630Sume } 121762587Sitojun 121878064Sume /* validate query Subject field. */ 121962587Sitojun qtype = ntohs(ni6->ni_qtype); 122062587Sitojun subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); 122162587Sitojun switch (qtype) { 122262587Sitojun case NI_QTYPE_NOOP: 122362587Sitojun case NI_QTYPE_SUPTYPES: 122478064Sume /* 07 draft */ 122578064Sume if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) 122678064Sume break; 122778064Sume /* FALLTHROUGH */ 122862587Sitojun case NI_QTYPE_FQDN: 122962587Sitojun case NI_QTYPE_NODEADDR: 123062587Sitojun switch (ni6->ni_code) { 123162587Sitojun case ICMP6_NI_SUBJ_IPV6: 123262587Sitojun#if ICMP6_NI_SUBJ_IPV6 != 0 123362587Sitojun case 0: 123462587Sitojun#endif 123562587Sitojun /* 123662587Sitojun * backward compatibility - try to accept 03 draft 123762587Sitojun * format, where no Subject is present. 123862587Sitojun */ 123978064Sume if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && 124078064Sume subjlen == 0) { 124162587Sitojun oldfqdn++; 124262587Sitojun break; 124362587Sitojun } 124478064Sume#if ICMP6_NI_SUBJ_IPV6 != 0 124578064Sume if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) 124678064Sume goto bad; 124778064Sume#endif 124862587Sitojun 1249121630Sume if (subjlen != sizeof(struct in6_addr)) 125062587Sitojun goto bad; 125162587Sitojun 125262587Sitojun /* 125362587Sitojun * Validate Subject address. 125462587Sitojun * 125578064Sume * Not sure what exactly "address belongs to the node" 125678064Sume * means in the spec, is it just unicast, or what? 125762587Sitojun * 125862587Sitojun * At this moment we consider Subject address as 125962587Sitojun * "belong to the node" if the Subject address equals 126062587Sitojun * to the IPv6 destination address; validation for 126162587Sitojun * IPv6 destination address should have done enough 126262587Sitojun * check for us. 126362587Sitojun * 126462587Sitojun * We do not do proxy at this moment. 126562587Sitojun */ 126662587Sitojun /* m_pulldown instead of copy? */ 126762587Sitojun m_copydata(m, off + sizeof(struct icmp6_nodeinfo), 1268148385Sume subjlen, (caddr_t)&in6_subj); 1269148385Sume if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL)) 1270121315Sume goto bad; 1271121630Sume 1272148385Sume subj = (char *)&in6_subj; 1273148385Sume if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj)) 127462587Sitojun break; 127578064Sume 127662587Sitojun /* 127762587Sitojun * XXX if we are to allow other cases, we should really 127862587Sitojun * be careful about scope here. 127962587Sitojun * basically, we should disallow queries toward IPv6 1280120891Sume * destination X with subject Y, 1281120891Sume * if scope(X) > scope(Y). 128262587Sitojun * if we allow scope(X) > scope(Y), it will result in 128362587Sitojun * information leakage across scope boundary. 128462587Sitojun */ 128562587Sitojun goto bad; 128662587Sitojun 128762587Sitojun case ICMP6_NI_SUBJ_FQDN: 128862587Sitojun /* 128962587Sitojun * Validate Subject name with gethostname(3). 129062587Sitojun * 129162587Sitojun * The behavior may need some debate, since: 129262587Sitojun * - we are not sure if the node has FQDN as 129362587Sitojun * hostname (returned by gethostname(3)). 129462587Sitojun * - the code does wildcard match for truncated names. 129562587Sitojun * however, we are not sure if we want to perform 129662587Sitojun * wildcard match, if gethostname(3) side has 129762587Sitojun * truncated hostname. 129862587Sitojun */ 129962587Sitojun n = ni6_nametodns(hostname, hostnamelen, 0); 130062587Sitojun if (!n || n->m_next || n->m_len == 0) 130162587Sitojun goto bad; 130262587Sitojun IP6_EXTHDR_GET(subj, char *, m, 130362587Sitojun off + sizeof(struct icmp6_nodeinfo), subjlen); 130462587Sitojun if (subj == NULL) 130562587Sitojun goto bad; 130662587Sitojun if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 1307120891Sume n->m_len)) { 130862587Sitojun goto bad; 130962587Sitojun } 131062587Sitojun m_freem(n); 131162587Sitojun n = NULL; 131262587Sitojun break; 131362587Sitojun 131478064Sume case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 131562587Sitojun default: 131662587Sitojun goto bad; 131762587Sitojun } 131862587Sitojun break; 131978064Sume } 132062587Sitojun 132178064Sume /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 132278064Sume switch (qtype) { 132378064Sume case NI_QTYPE_FQDN: 132478064Sume if ((icmp6_nodeinfo & 1) == 0) 132578064Sume goto bad; 132678064Sume break; 132778064Sume case NI_QTYPE_NODEADDR: 132878064Sume if ((icmp6_nodeinfo & 2) == 0) 132978064Sume goto bad; 133078064Sume break; 133178064Sume } 133278064Sume 133378064Sume /* guess reply length */ 133478064Sume switch (qtype) { 133578064Sume case NI_QTYPE_NOOP: 133678064Sume break; /* no reply data */ 133778064Sume case NI_QTYPE_SUPTYPES: 133878064Sume replylen += sizeof(u_int32_t); 133978064Sume break; 134078064Sume case NI_QTYPE_FQDN: 134178064Sume /* XXX will append an mbuf */ 134278064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 134378064Sume break; 134478064Sume case NI_QTYPE_NODEADDR: 1345148892Sume addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj); 134678064Sume if ((replylen += addrs * (sizeof(struct in6_addr) + 1347120891Sume sizeof(u_int32_t))) > MCLBYTES) 134878064Sume replylen = MCLBYTES; /* XXX: will truncate pkt later */ 134978064Sume break; 135062587Sitojun default: 135178064Sume /* 135278064Sume * XXX: We must return a reply with the ICMP6 code 1353120891Sume * `unknown Qtype' in this case. However we regard the case 135478064Sume * as an FQDN query for backward compatibility. 135578064Sume * Older versions set a random value to this field, 135678064Sume * so it rarely varies in the defined qtypes. 135778064Sume * But the mechanism is not reliable... 135878064Sume * maybe we should obsolete older versions. 135978064Sume */ 136078064Sume qtype = NI_QTYPE_FQDN; 136178064Sume /* XXX will append an mbuf */ 136278064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 136378064Sume oldfqdn++; 136478064Sume break; 136562587Sitojun } 136662587Sitojun 136778064Sume /* allocate an mbuf to reply. */ 1368111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 136962587Sitojun if (n == NULL) { 137062587Sitojun m_freem(m); 1371120856Sume return (NULL); 137262587Sitojun } 1373108466Ssam M_MOVE_PKTHDR(n, m); /* just for recvif */ 137453541Sshin if (replylen > MHLEN) { 137562587Sitojun if (replylen > MCLBYTES) { 137678064Sume /* 137778064Sume * XXX: should we try to allocate more? But MCLBYTES 137878064Sume * is probably much larger than IPV6_MMTU... 137978064Sume */ 138053541Sshin goto bad; 138162587Sitojun } 1382111119Simp MCLGET(n, M_DONTWAIT); 138353541Sshin if ((n->m_flags & M_EXT) == 0) { 138453541Sshin goto bad; 138553541Sshin } 138653541Sshin } 138753541Sshin n->m_pkthdr.len = n->m_len = replylen; 138853541Sshin 138953541Sshin /* copy mbuf header and IPv6 + Node Information base headers */ 139053541Sshin bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); 139153541Sshin nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 139262587Sitojun bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); 139353541Sshin 139453541Sshin /* qtype dependent procedure */ 139553541Sshin switch (qtype) { 139662587Sitojun case NI_QTYPE_NOOP: 139778064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 139862587Sitojun nni6->ni_flags = 0; 139962587Sitojun break; 140062587Sitojun case NI_QTYPE_SUPTYPES: 140178064Sume { 140278064Sume u_int32_t v; 140378064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 140478064Sume nni6->ni_flags = htons(0x0000); /* raw bitmap */ 140578064Sume /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 140678064Sume v = (u_int32_t)htonl(0x0000000f); 140778064Sume bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 140862587Sitojun break; 140978064Sume } 141062587Sitojun case NI_QTYPE_FQDN: 141178064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 141262587Sitojun fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + 1413120891Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo)); 141462587Sitojun nni6->ni_flags = 0; /* XXX: meaningless TTL */ 141562587Sitojun fqdn->ni_fqdn_ttl = 0; /* ditto. */ 141662587Sitojun /* 141762587Sitojun * XXX do we really have FQDN in variable "hostname"? 141862587Sitojun */ 141962587Sitojun n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn); 142062587Sitojun if (n->m_next == NULL) 142162587Sitojun goto bad; 142262587Sitojun /* XXX we assume that n->m_next is not a chain */ 142362587Sitojun if (n->m_next->m_next != NULL) 142462587Sitojun goto bad; 142562587Sitojun n->m_pkthdr.len += n->m_next->m_len; 142662587Sitojun break; 142762587Sitojun case NI_QTYPE_NODEADDR: 142862587Sitojun { 142962587Sitojun int lenlim, copied; 143053541Sshin 143178064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 143278064Sume n->m_pkthdr.len = n->m_len = 143378064Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 143478064Sume lenlim = M_TRAILINGSPACE(n); 143562587Sitojun copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 143662587Sitojun /* XXX: reset mbuf length */ 143762587Sitojun n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 1438120891Sume sizeof(struct icmp6_nodeinfo) + copied; 143962587Sitojun break; 144053541Sshin } 144162587Sitojun default: 144262587Sitojun break; /* XXX impossible! */ 144362587Sitojun } 144453541Sshin 144553541Sshin nni6->ni_type = ICMP6_NI_REPLY; 144662587Sitojun m_freem(m); 1447120856Sume return (n); 144853541Sshin 144953541Sshin bad: 145062587Sitojun m_freem(m); 145153541Sshin if (n) 145253541Sshin m_freem(n); 1453120856Sume return (NULL); 145453541Sshin} 145553541Sshin#undef hostnamelen 145653541Sshin 145753541Sshin/* 145862587Sitojun * make a mbuf with DNS-encoded string. no compression support. 145962587Sitojun * 146062587Sitojun * XXX names with less than 2 dots (like "foo" or "foo.section") will be 146162587Sitojun * treated as truncated name (two \0 at the end). this is a wild guess. 146262587Sitojun */ 146362587Sitojunstatic struct mbuf * 146462587Sitojunni6_nametodns(name, namelen, old) 146562587Sitojun const char *name; 146662587Sitojun int namelen; 146762587Sitojun int old; /* return pascal string if non-zero */ 146862587Sitojun{ 146962587Sitojun struct mbuf *m; 147062587Sitojun char *cp, *ep; 147162587Sitojun const char *p, *q; 147262587Sitojun int i, len, nterm; 147362587Sitojun 147462587Sitojun if (old) 147562587Sitojun len = namelen + 1; 147662587Sitojun else 147762587Sitojun len = MCLBYTES; 147862587Sitojun 147962587Sitojun /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ 1480111119Simp MGET(m, M_DONTWAIT, MT_DATA); 148162587Sitojun if (m && len > MLEN) { 1482111119Simp MCLGET(m, M_DONTWAIT); 148362587Sitojun if ((m->m_flags & M_EXT) == 0) 148462587Sitojun goto fail; 148562587Sitojun } 148662587Sitojun if (!m) 148762587Sitojun goto fail; 148862587Sitojun m->m_next = NULL; 148962587Sitojun 149062587Sitojun if (old) { 149162587Sitojun m->m_len = len; 149262587Sitojun *mtod(m, char *) = namelen; 149362587Sitojun bcopy(name, mtod(m, char *) + 1, namelen); 149462587Sitojun return m; 149562587Sitojun } else { 149662587Sitojun m->m_len = 0; 149762587Sitojun cp = mtod(m, char *); 149862587Sitojun ep = mtod(m, char *) + M_TRAILINGSPACE(m); 149962587Sitojun 150062587Sitojun /* if not certain about my name, return empty buffer */ 150162587Sitojun if (namelen == 0) 150262587Sitojun return m; 150362587Sitojun 150462587Sitojun /* 150562587Sitojun * guess if it looks like shortened hostname, or FQDN. 150662587Sitojun * shortened hostname needs two trailing "\0". 150762587Sitojun */ 150862587Sitojun i = 0; 150962587Sitojun for (p = name; p < name + namelen; p++) { 151062587Sitojun if (*p && *p == '.') 151162587Sitojun i++; 151262587Sitojun } 151362587Sitojun if (i < 2) 151462587Sitojun nterm = 2; 151562587Sitojun else 151662587Sitojun nterm = 1; 151762587Sitojun 151862587Sitojun p = name; 151962587Sitojun while (cp < ep && p < name + namelen) { 152062587Sitojun i = 0; 152162587Sitojun for (q = p; q < name + namelen && *q && *q != '.'; q++) 152262587Sitojun i++; 152362587Sitojun /* result does not fit into mbuf */ 152462587Sitojun if (cp + i + 1 >= ep) 152562587Sitojun goto fail; 152678704Sume /* 152778704Sume * DNS label length restriction, RFC1035 page 8. 152878704Sume * "i == 0" case is included here to avoid returning 152978704Sume * 0-length label on "foo..bar". 153078704Sume */ 153178704Sume if (i <= 0 || i >= 64) 153262587Sitojun goto fail; 153362587Sitojun *cp++ = i; 153462587Sitojun bcopy(p, cp, i); 153562587Sitojun cp += i; 153662587Sitojun p = q; 153762587Sitojun if (p < name + namelen && *p == '.') 153862587Sitojun p++; 153962587Sitojun } 154062587Sitojun /* termination */ 154162587Sitojun if (cp + nterm >= ep) 154262587Sitojun goto fail; 154362587Sitojun while (nterm-- > 0) 154462587Sitojun *cp++ = '\0'; 154562587Sitojun m->m_len = cp - mtod(m, char *); 154662587Sitojun return m; 154762587Sitojun } 154862587Sitojun 154962587Sitojun panic("should not reach here"); 155095023Ssuz /* NOTREACHED */ 155162587Sitojun 155262587Sitojun fail: 155362587Sitojun if (m) 155462587Sitojun m_freem(m); 155562587Sitojun return NULL; 155662587Sitojun} 155762587Sitojun 155862587Sitojun/* 155962587Sitojun * check if two DNS-encoded string matches. takes care of truncated 156062587Sitojun * form (with \0\0 at the end). no compression support. 156178064Sume * XXX upper/lowercase match (see RFC2065) 156262587Sitojun */ 156362587Sitojunstatic int 156462587Sitojunni6_dnsmatch(a, alen, b, blen) 156562587Sitojun const char *a; 156662587Sitojun int alen; 156762587Sitojun const char *b; 156862587Sitojun int blen; 156962587Sitojun{ 157062587Sitojun const char *a0, *b0; 157162587Sitojun int l; 157262587Sitojun 157362587Sitojun /* simplest case - need validation? */ 157462587Sitojun if (alen == blen && bcmp(a, b, alen) == 0) 157562587Sitojun return 1; 157662587Sitojun 157762587Sitojun a0 = a; 157862587Sitojun b0 = b; 157962587Sitojun 158062587Sitojun /* termination is mandatory */ 158162587Sitojun if (alen < 2 || blen < 2) 158262587Sitojun return 0; 158362587Sitojun if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 158462587Sitojun return 0; 158562587Sitojun alen--; 158662587Sitojun blen--; 158762587Sitojun 158862587Sitojun while (a - a0 < alen && b - b0 < blen) { 158962587Sitojun if (a - a0 + 1 > alen || b - b0 + 1 > blen) 159062587Sitojun return 0; 159162587Sitojun 159262587Sitojun if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 159362587Sitojun return 0; 159462587Sitojun /* we don't support compression yet */ 159562587Sitojun if (a[0] >= 64 || b[0] >= 64) 159662587Sitojun return 0; 159762587Sitojun 159862587Sitojun /* truncated case */ 159962587Sitojun if (a[0] == 0 && a - a0 == alen - 1) 160062587Sitojun return 1; 160162587Sitojun if (b[0] == 0 && b - b0 == blen - 1) 160262587Sitojun return 1; 160362587Sitojun if (a[0] == 0 || b[0] == 0) 160462587Sitojun return 0; 160562587Sitojun 160662587Sitojun if (a[0] != b[0]) 160762587Sitojun return 0; 160862587Sitojun l = a[0]; 160962587Sitojun if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 161062587Sitojun return 0; 161162587Sitojun if (bcmp(a + 1, b + 1, l) != 0) 161262587Sitojun return 0; 161362587Sitojun 161462587Sitojun a += 1 + l; 161562587Sitojun b += 1 + l; 161662587Sitojun } 161762587Sitojun 161862587Sitojun if (a - a0 == alen && b - b0 == blen) 161962587Sitojun return 1; 162062587Sitojun else 162162587Sitojun return 0; 162262587Sitojun} 162362587Sitojun 162462587Sitojun/* 162553541Sshin * calculate the number of addresses to be returned in the node info reply. 162653541Sshin */ 162753541Sshinstatic int 162878064Sumeni6_addrs(ni6, m, ifpp, subj) 162953541Sshin struct icmp6_nodeinfo *ni6; 163053541Sshin struct mbuf *m; 163153541Sshin struct ifnet **ifpp; 1632148892Sume struct in6_addr *subj; 163353541Sshin{ 163478064Sume struct ifnet *ifp; 163578064Sume struct in6_ifaddr *ifa6; 163678064Sume struct ifaddr *ifa; 163753541Sshin int addrs = 0, addrsofif, iffound = 0; 163878064Sume int niflags = ni6->ni_flags; 163953541Sshin 164078064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 164178064Sume switch (ni6->ni_code) { 164278064Sume case ICMP6_NI_SUBJ_IPV6: 164378064Sume if (subj == NULL) /* must be impossible... */ 1644120856Sume return (0); 164578064Sume break; 164678064Sume default: 164778064Sume /* 164878064Sume * XXX: we only support IPv6 subject address for 164978064Sume * this Qtype. 165078064Sume */ 1651120856Sume return (0); 165278064Sume } 165378064Sume } 165478064Sume 1655108172Shsu IFNET_RLOCK(); 1656120891Sume for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 165753541Sshin addrsofif = 0; 1658120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 165953541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 166053541Sshin continue; 166153541Sshin ifa6 = (struct in6_ifaddr *)ifa; 166253541Sshin 166378064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 1664148892Sume IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr)) 166553541Sshin iffound = 1; 166653541Sshin 166762587Sitojun /* 166862587Sitojun * IPv4-mapped addresses can only be returned by a 166962587Sitojun * Node Information proxy, since they represent 167062587Sitojun * addresses of IPv4-only nodes, which perforce do 167162587Sitojun * not implement this protocol. 167278064Sume * [icmp-name-lookups-07, Section 5.4] 167362587Sitojun * So we don't support NI_NODEADDR_FLAG_COMPAT in 167462587Sitojun * this function at this moment. 167562587Sitojun */ 167662587Sitojun 167753541Sshin /* What do we have to do about ::1? */ 167878064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 167978064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 168078064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 168178064Sume continue; 168253541Sshin break; 168378064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 168478064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 168578064Sume continue; 168653541Sshin break; 168778064Sume case IPV6_ADDR_SCOPE_GLOBAL: 168878064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 168978064Sume continue; 169078064Sume break; 169178064Sume default: 169278064Sume continue; 169353541Sshin } 169478064Sume 169578064Sume /* 169678064Sume * check if anycast is okay. 169795023Ssuz * XXX: just experimental. not in the spec. 169878064Sume */ 169978064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 170078064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 170178064Sume continue; /* we need only unicast addresses */ 170278064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 170378064Sume (icmp6_nodeinfo & 4) == 0) { 170478064Sume continue; 170578064Sume } 170678064Sume addrsofif++; /* count the address */ 170753541Sshin } 170853541Sshin if (iffound) { 170953541Sshin *ifpp = ifp; 1710108172Shsu IFNET_RUNLOCK(); 1711120856Sume return (addrsofif); 171253541Sshin } 171353541Sshin 171453541Sshin addrs += addrsofif; 171553541Sshin } 1716108172Shsu IFNET_RUNLOCK(); 171753541Sshin 1718120856Sume return (addrs); 171953541Sshin} 172053541Sshin 172153541Sshinstatic int 172253541Sshinni6_store_addrs(ni6, nni6, ifp0, resid) 172353541Sshin struct icmp6_nodeinfo *ni6, *nni6; 172453541Sshin struct ifnet *ifp0; 172553541Sshin int resid; 172653541Sshin{ 172778064Sume struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet); 172878064Sume struct in6_ifaddr *ifa6; 172978064Sume struct ifaddr *ifa; 173078064Sume struct ifnet *ifp_dep = NULL; 173178064Sume int copied = 0, allow_deprecated = 0; 173253541Sshin u_char *cp = (u_char *)(nni6 + 1); 173378064Sume int niflags = ni6->ni_flags; 173478064Sume u_int32_t ltime; 173553541Sshin 173678064Sume if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 1737120856Sume return (0); /* needless to copy */ 1738120891Sume 1739108172Shsu IFNET_RLOCK(); 174078064Sume again: 1741120892Sume 1742120891Sume for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 174362587Sitojun for (ifa = ifp->if_addrlist.tqh_first; ifa; 1744120891Sume ifa = ifa->ifa_list.tqe_next) { 174553541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 174653541Sshin continue; 174753541Sshin ifa6 = (struct in6_ifaddr *)ifa; 174853541Sshin 174978064Sume if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && 175078064Sume allow_deprecated == 0) { 175178064Sume /* 175278064Sume * prefererred address should be put before 175378064Sume * deprecated addresses. 175478064Sume */ 175578064Sume 175678064Sume /* record the interface for later search */ 175778064Sume if (ifp_dep == NULL) 175878064Sume ifp_dep = ifp; 175978064Sume 176078064Sume continue; 1761120891Sume } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && 1762120891Sume allow_deprecated != 0) 176378064Sume continue; /* we now collect deprecated addrs */ 176453541Sshin 176553541Sshin /* What do we have to do about ::1? */ 176678064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 176778064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 176878064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 176978064Sume continue; 177053541Sshin break; 177178064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 177278064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 177378064Sume continue; 177453541Sshin break; 177578064Sume case IPV6_ADDR_SCOPE_GLOBAL: 177678064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 177778064Sume continue; 177878064Sume break; 177978064Sume default: 178078064Sume continue; 178153541Sshin } 178253541Sshin 178378064Sume /* 178478064Sume * check if anycast is okay. 1785120891Sume * XXX: just experimental. not in the spec. 178678064Sume */ 178778064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 178878064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 178978064Sume continue; 179078064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 179178064Sume (icmp6_nodeinfo & 4) == 0) { 179278064Sume continue; 179353541Sshin } 179478064Sume 179578064Sume /* now we can copy the address */ 179678064Sume if (resid < sizeof(struct in6_addr) + 179778064Sume sizeof(u_int32_t)) { 179878064Sume /* 179978064Sume * We give up much more copy. 180078064Sume * Set the truncate flag and return. 180178064Sume */ 1802120891Sume nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE; 1803108172Shsu IFNET_RUNLOCK(); 1804120856Sume return (copied); 180578064Sume } 180678064Sume 180778064Sume /* 180878064Sume * Set the TTL of the address. 180978064Sume * The TTL value should be one of the following 181078064Sume * according to the specification: 181178064Sume * 181278064Sume * 1. The remaining lifetime of a DHCP lease on the 181378064Sume * address, or 181478064Sume * 2. The remaining Valid Lifetime of a prefix from 181578064Sume * which the address was derived through Stateless 181678064Sume * Autoconfiguration. 181778064Sume * 181878064Sume * Note that we currently do not support stateful 181978064Sume * address configuration by DHCPv6, so the former 182078064Sume * case can't happen. 182178064Sume */ 182278064Sume if (ifa6->ia6_lifetime.ia6t_expire == 0) 182378064Sume ltime = ND6_INFINITE_LIFETIME; 182478064Sume else { 182578064Sume if (ifa6->ia6_lifetime.ia6t_expire > 182678064Sume time_second) 182778064Sume ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second); 182878064Sume else 182978064Sume ltime = 0; 183078064Sume } 1831120891Sume 183278064Sume bcopy(<ime, cp, sizeof(u_int32_t)); 183378064Sume cp += sizeof(u_int32_t); 183478064Sume 183578064Sume /* copy the address itself */ 183678064Sume bcopy(&ifa6->ia_addr.sin6_addr, cp, 1837120891Sume sizeof(struct in6_addr)); 1838121315Sume in6_clearscope((struct in6_addr *)cp); /* XXX */ 183978064Sume cp += sizeof(struct in6_addr); 1840120891Sume 184178064Sume resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1842120891Sume copied += (sizeof(struct in6_addr) + sizeof(u_int32_t)); 184353541Sshin } 184453541Sshin if (ifp0) /* we need search only on the specified IF */ 184553541Sshin break; 184653541Sshin } 184753541Sshin 184878064Sume if (allow_deprecated == 0 && ifp_dep != NULL) { 184978064Sume ifp = ifp_dep; 185078064Sume allow_deprecated = 1; 185178064Sume 185278064Sume goto again; 185378064Sume } 185478064Sume 1855108172Shsu IFNET_RUNLOCK(); 1856108172Shsu 1857120856Sume return (copied); 185853541Sshin} 185953541Sshin 186053541Sshin/* 186153541Sshin * XXX almost dup'ed code with rip6_input. 186253541Sshin */ 186353541Sshinstatic int 186453541Sshinicmp6_rip6_input(mp, off) 186553541Sshin struct mbuf **mp; 186653541Sshin int off; 186753541Sshin{ 186853541Sshin struct mbuf *m = *mp; 186978064Sume struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 187078064Sume struct in6pcb *in6p; 187153541Sshin struct in6pcb *last = NULL; 1872121315Sume struct sockaddr_in6 fromsa; 187353541Sshin struct icmp6_hdr *icmp6; 187453541Sshin struct mbuf *opts = NULL; 187553541Sshin 187662587Sitojun#ifndef PULLDOWN_TEST 187753541Sshin /* this is assumed to be safe. */ 187853541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 187962587Sitojun#else 188062587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 188162587Sitojun if (icmp6 == NULL) { 188262587Sitojun /* m is already reclaimed */ 1883120891Sume return (IPPROTO_DONE); 188462587Sitojun } 188562587Sitojun#endif 188653541Sshin 1887148385Sume /* 1888148385Sume * XXX: the address may have embedded scope zone ID, which should be 1889148385Sume * hidden from applications. 1890148385Sume */ 1891121315Sume bzero(&fromsa, sizeof(fromsa)); 1892148385Sume fromsa.sin6_family = AF_INET6; 1893121315Sume fromsa.sin6_len = sizeof(struct sockaddr_in6); 1894148385Sume fromsa.sin6_addr = ip6->ip6_src; 1895148385Sume if (sa6_recoverscope(&fromsa)) { 1896148385Sume m_freem(m); 1897148385Sume return (IPPROTO_DONE); 1898148385Sume } 189953541Sshin 1900141545Srwatson INP_INFO_RLOCK(&ripcbinfo); 1901120891Sume LIST_FOREACH(in6p, &ripcb, inp_list) { 1902141545Srwatson INP_LOCK(in6p); 1903141545Srwatson if ((in6p->inp_vflag & INP_IPV6) == 0) { 1904141545Srwatson docontinue: 1905141545Srwatson INP_UNLOCK(in6p); 190653541Sshin continue; 1907141545Srwatson } 190853541Sshin if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6) 1909141545Srwatson goto docontinue; 191053541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 191153541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 1912141545Srwatson goto docontinue; 191353541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 191453541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 1915141545Srwatson goto docontinue; 191653541Sshin if (in6p->in6p_icmp6filt 191753541Sshin && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, 191853541Sshin in6p->in6p_icmp6filt)) 1919141545Srwatson goto docontinue; 192053541Sshin if (last) { 1921121809Sume struct mbuf *n = NULL; 1922121809Sume 1923121809Sume /* 1924121809Sume * Recent network drivers tend to allocate a single 1925121809Sume * mbuf cluster, rather than to make a couple of 1926121809Sume * mbufs without clusters. Also, since the IPv6 code 1927121809Sume * path tries to avoid m_pullup(), it is highly 1928121809Sume * probable that we still have an mbuf cluster here 1929121809Sume * even though the necessary length can be stored in an 1930121809Sume * mbuf's internal buffer. 1931121809Sume * Meanwhile, the default size of the receive socket 1932121809Sume * buffer for raw sockets is not so large. This means 1933121809Sume * the possibility of packet loss is relatively higher 1934121809Sume * than before. To avoid this scenario, we copy the 1935121809Sume * received data to a separate mbuf that does not use 1936121809Sume * a cluster, if possible. 1937121809Sume * XXX: it is better to copy the data after stripping 1938121809Sume * intermediate headers. 1939121809Sume */ 1940121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 1941121809Sume m->m_len <= MHLEN) { 1942121809Sume MGET(n, M_DONTWAIT, m->m_type); 1943121809Sume if (n != NULL) { 1944145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 1945145065Sgnn bcopy(m->m_data, n->m_data, 1946145065Sgnn m->m_len); 1947145065Sgnn n->m_len = m->m_len; 1948145065Sgnn } else { 1949145065Sgnn m_free(n); 1950145065Sgnn n = NULL; 1951145065Sgnn } 1952121809Sume } 1953121809Sume } 1954121809Sume if (n != NULL || 1955121809Sume (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 195653541Sshin if (last->in6p_flags & IN6P_CONTROLOPTS) 1957121674Sume ip6_savecontrol(last, n, &opts); 195853541Sshin /* strip intermediate headers */ 195953541Sshin m_adj(n, off); 196053541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 1961121315Sume (struct sockaddr *)&fromsa, n, opts) 1962120891Sume == 0) { 196353541Sshin /* should notify about lost packet */ 196453541Sshin m_freem(n); 196578064Sume if (opts) { 196653541Sshin m_freem(opts); 196778064Sume } 196897658Stanimura } else 196953541Sshin sorwakeup(last->in6p_socket); 197053541Sshin opts = NULL; 197153541Sshin } 1972141545Srwatson INP_UNLOCK(last); 197353541Sshin } 197453541Sshin last = in6p; 197553541Sshin } 197653541Sshin if (last) { 197753541Sshin if (last->in6p_flags & IN6P_CONTROLOPTS) 1978121674Sume ip6_savecontrol(last, m, &opts); 197953541Sshin /* strip intermediate headers */ 198053541Sshin m_adj(m, off); 1981121809Sume 1982121809Sume /* avoid using mbuf clusters if possible (see above) */ 1983121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 1984121809Sume m->m_len <= MHLEN) { 1985121809Sume struct mbuf *n; 1986121809Sume 1987121809Sume MGET(n, M_DONTWAIT, m->m_type); 1988121809Sume if (n != NULL) { 1989145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 1990145065Sgnn bcopy(m->m_data, n->m_data, m->m_len); 1991145065Sgnn n->m_len = m->m_len; 1992145065Sgnn 1993145065Sgnn m_freem(m); 1994145065Sgnn m = n; 1995145065Sgnn } else { 1996145065Sgnn m_freem(n); 1997145065Sgnn n = NULL; 1998145065Sgnn } 1999121809Sume } 2000121809Sume } 200153541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 2002121315Sume (struct sockaddr *)&fromsa, m, opts) == 0) { 200353541Sshin m_freem(m); 200453541Sshin if (opts) 200553541Sshin m_freem(opts); 200697658Stanimura } else 200753541Sshin sorwakeup(last->in6p_socket); 2008141545Srwatson INP_UNLOCK(last); 200953541Sshin } else { 201053541Sshin m_freem(m); 201153541Sshin ip6stat.ip6s_delivered--; 201253541Sshin } 2013141545Srwatson INP_INFO_RUNLOCK(&ripcbinfo); 201453541Sshin return IPPROTO_DONE; 201553541Sshin} 201653541Sshin 201753541Sshin/* 201853541Sshin * Reflect the ip6 packet back to the source. 201962587Sitojun * OFF points to the icmp6 header, counted from the top of the mbuf. 202053541Sshin */ 202153541Sshinvoid 202253541Sshinicmp6_reflect(m, off) 202353541Sshin struct mbuf *m; 202453541Sshin size_t off; 202553541Sshin{ 202662587Sitojun struct ip6_hdr *ip6; 202753541Sshin struct icmp6_hdr *icmp6; 202853541Sshin struct in6_ifaddr *ia; 202962587Sitojun int plen; 203053541Sshin int type, code; 203153541Sshin struct ifnet *outif = NULL; 2032148385Sume struct in6_addr origdst, *src = NULL; 203353541Sshin#ifdef COMPAT_RFC1885 203453541Sshin int mtu = IPV6_MMTU; 203553541Sshin struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst; 203653541Sshin#endif 203753541Sshin 203862587Sitojun /* too short to reflect */ 203962587Sitojun if (off < sizeof(struct ip6_hdr)) { 204078064Sume nd6log((LOG_DEBUG, 204178064Sume "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 204278064Sume (u_long)off, (u_long)sizeof(struct ip6_hdr), 204378064Sume __FILE__, __LINE__)); 204462587Sitojun goto bad; 204562587Sitojun } 204662587Sitojun 204753541Sshin /* 204853541Sshin * If there are extra headers between IPv6 and ICMPv6, strip 204953541Sshin * off that header first. 205053541Sshin */ 205162587Sitojun#ifdef DIAGNOSTIC 205262587Sitojun if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) 205362587Sitojun panic("assumption failed in icmp6_reflect"); 205462587Sitojun#endif 205562587Sitojun if (off > sizeof(struct ip6_hdr)) { 205662587Sitojun size_t l; 205762587Sitojun struct ip6_hdr nip6; 205853541Sshin 205962587Sitojun l = off - sizeof(struct ip6_hdr); 206062587Sitojun m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); 206162587Sitojun m_adj(m, l); 206262587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 206362587Sitojun if (m->m_len < l) { 206462587Sitojun if ((m = m_pullup(m, l)) == NULL) 206562587Sitojun return; 206653541Sshin } 206762587Sitojun bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); 206862587Sitojun } else /* off == sizeof(struct ip6_hdr) */ { 206962587Sitojun size_t l; 207062587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 207162587Sitojun if (m->m_len < l) { 207262587Sitojun if ((m = m_pullup(m, l)) == NULL) 207362587Sitojun return; 207453541Sshin } 207553541Sshin } 207662587Sitojun plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 207762587Sitojun ip6 = mtod(m, struct ip6_hdr *); 207862587Sitojun ip6->ip6_nxt = IPPROTO_ICMPV6; 207953541Sshin icmp6 = (struct icmp6_hdr *)(ip6 + 1); 208053541Sshin type = icmp6->icmp6_type; /* keep type for statistics */ 208153541Sshin code = icmp6->icmp6_code; /* ditto. */ 208253541Sshin 2083148385Sume origdst = ip6->ip6_dst; 208453541Sshin /* 208553541Sshin * ip6_input() drops a packet if its src is multicast. 208653541Sshin * So, the src is never multicast. 208753541Sshin */ 208853541Sshin ip6->ip6_dst = ip6->ip6_src; 208953541Sshin 209053541Sshin#ifdef COMPAT_RFC1885 209153541Sshin /* 209253541Sshin * xxx guess MTU 209353541Sshin * RFC 1885 requires that echo reply should be truncated if it 209453541Sshin * does not fit in with (return) path MTU, but the description was 209553541Sshin * removed in the new spec. 209653541Sshin */ 209753541Sshin if (icmp6_reflect_rt.ro_rt == 0 || 209853541Sshin ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) { 209953541Sshin if (icmp6_reflect_rt.ro_rt) { 210053541Sshin RTFREE(icmp6_reflect_rt.ro_rt); 210153541Sshin icmp6_reflect_rt.ro_rt = 0; 210253541Sshin } 210353541Sshin bzero(sin6, sizeof(*sin6)); 210453541Sshin sin6->sin6_family = PF_INET6; 210553541Sshin sin6->sin6_len = sizeof(struct sockaddr_in6); 210653541Sshin sin6->sin6_addr = ip6->ip6_dst; 210753541Sshin 2108122921Sandre rtalloc((struct route *)&icmp6_reflect_rt.ro_rt); 210953541Sshin } 211053541Sshin 211153541Sshin if (icmp6_reflect_rt.ro_rt == 0) 211253541Sshin goto bad; 211353541Sshin 211453541Sshin if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST) 211553541Sshin && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu) 211653541Sshin mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu; 211753541Sshin 211853541Sshin if (mtu < m->m_pkthdr.len) { 211953541Sshin plen -= (m->m_pkthdr.len - mtu); 212053541Sshin m_adj(m, mtu - m->m_pkthdr.len); 212153541Sshin } 212253541Sshin#endif 2123148385Sume 212453541Sshin /* 2125120891Sume * If the incoming packet was addressed directly to us (i.e. unicast), 212653541Sshin * use dst as the src for the reply. 2127120891Sume * The IN6_IFF_NOTREADY case should be VERY rare, but is possible 212862587Sitojun * (for example) when we encounter an error while forwarding procedure 212962587Sitojun * destined to a duplicated address of ours. 2130148385Sume * Note that ip6_getdstifaddr() may fail if we are in an error handling 2131148385Sume * procedure of an outgoing packet of our own, in which case we need 2132148385Sume * to search in the ifaddr list. 213353541Sshin */ 2134148385Sume if (!IN6_IS_ADDR_MULTICAST(&origdst)) { 2135148385Sume if ((ia = ip6_getdstifaddr(m))) { 2136148385Sume if (!(ia->ia6_flags & 2137148385Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) 2138148385Sume src = &ia->ia_addr.sin6_addr; 2139148385Sume } else { 2140148385Sume struct sockaddr_in6 d; 2141148385Sume 2142148385Sume bzero(&d, sizeof(d)); 2143148385Sume d.sin6_family = AF_INET6; 2144148385Sume d.sin6_len = sizeof(d); 2145148385Sume d.sin6_addr = origdst; 2146148385Sume ia = (struct in6_ifaddr *) 2147148385Sume ifa_ifwithaddr((struct sockaddr *)&d); 2148148385Sume if (ia && 2149148385Sume !(ia->ia6_flags & 2150148385Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) { 2151148385Sume src = &ia->ia_addr.sin6_addr; 2152148385Sume } 215353541Sshin } 215453541Sshin } 215553541Sshin 2156148385Sume if (src == NULL) { 215778064Sume int e; 2158148385Sume struct sockaddr_in6 sin6; 215978064Sume struct route_in6 ro; 216078064Sume 216153541Sshin /* 216262587Sitojun * This case matches to multicasts, our anycast, or unicasts 216395023Ssuz * that we do not own. Select a source address based on the 216478064Sume * source address of the erroneous packet. 216553541Sshin */ 2166148385Sume bzero(&sin6, sizeof(sin6)); 2167148385Sume sin6.sin6_family = AF_INET6; 2168148385Sume sin6.sin6_len = sizeof(sin6); 2169148385Sume sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */ 2170148385Sume 217178064Sume bzero(&ro, sizeof(ro)); 2172148385Sume src = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &e); 217378064Sume if (ro.ro_rt) 217478064Sume RTFREE(ro.ro_rt); /* XXX: we could use this */ 217578064Sume if (src == NULL) { 217678064Sume nd6log((LOG_DEBUG, 217778064Sume "icmp6_reflect: source can't be determined: " 217878064Sume "dst=%s, error=%d\n", 2179148385Sume ip6_sprintf(&sin6.sin6_addr), e)); 218078064Sume goto bad; 218178064Sume } 218278064Sume } 218353541Sshin 218453541Sshin ip6->ip6_src = *src; 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); 220653541Sshin 220753541Sshin#ifdef COMPAT_RFC1885 2208105194Ssam ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL); 220953541Sshin#else 2210105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 221153541Sshin#endif 221253541Sshin if (outif) 221353541Sshin icmp6_ifoutstat_inc(outif, type, code); 221453541Sshin 221553541Sshin return; 221653541Sshin 221753541Sshin bad: 221853541Sshin m_freem(m); 221953541Sshin return; 222053541Sshin} 222153541Sshin 222253541Sshinvoid 222353541Sshinicmp6_fasttimo() 222453541Sshin{ 222562587Sitojun 222653541Sshin mld6_fasttimeo(); 222753541Sshin} 222853541Sshin 222953541Sshinstatic const char * 223053541Sshinicmp6_redirect_diag(src6, dst6, tgt6) 223153541Sshin struct in6_addr *src6; 223253541Sshin struct in6_addr *dst6; 223353541Sshin struct in6_addr *tgt6; 223453541Sshin{ 223553541Sshin static char buf[1024]; 223653541Sshin snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", 2237120891Sume ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6)); 223853541Sshin return buf; 223953541Sshin} 224053541Sshin 224153541Sshinvoid 224253541Sshinicmp6_redirect_input(m, off) 224378064Sume struct mbuf *m; 224453541Sshin int off; 224553541Sshin{ 224653541Sshin struct ifnet *ifp = m->m_pkthdr.rcvif; 224753541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 224862587Sitojun struct nd_redirect *nd_rd; 224953541Sshin int icmp6len = ntohs(ip6->ip6_plen); 225053541Sshin char *lladdr = NULL; 225153541Sshin int lladdrlen = 0; 225253541Sshin u_char *redirhdr = NULL; 225353541Sshin int redirhdrlen = 0; 225453541Sshin struct rtentry *rt = NULL; 225553541Sshin int is_router; 225653541Sshin int is_onlink; 225753541Sshin struct in6_addr src6 = ip6->ip6_src; 225862587Sitojun struct in6_addr redtgt6; 225962587Sitojun struct in6_addr reddst6; 226053541Sshin union nd_opts ndopts; 226153541Sshin 226253541Sshin if (!m || !ifp) 226353541Sshin return; 226453541Sshin 226553541Sshin /* XXX if we are router, we don't update route by icmp6 redirect */ 226653541Sshin if (ip6_forwarding) 226762587Sitojun goto freeit; 226853541Sshin if (!icmp6_rediraccept) 226962587Sitojun goto freeit; 227062587Sitojun 227162587Sitojun#ifndef PULLDOWN_TEST 227262587Sitojun IP6_EXTHDR_CHECK(m, off, icmp6len,); 227362587Sitojun nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); 227462587Sitojun#else 227562587Sitojun IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); 227662587Sitojun if (nd_rd == NULL) { 227762587Sitojun icmp6stat.icp6s_tooshort++; 227853541Sshin return; 227962587Sitojun } 228062587Sitojun#endif 228162587Sitojun redtgt6 = nd_rd->nd_rd_target; 228262587Sitojun reddst6 = nd_rd->nd_rd_dst; 228353541Sshin 2284148385Sume if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) || 2285148385Sume in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) { 2286148385Sume goto freeit; 2287148385Sume } 228853541Sshin 228953541Sshin /* validation */ 229053541Sshin if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 229178064Sume nd6log((LOG_ERR, 2292120891Sume "ICMP6 redirect sent from %s rejected; " 2293120891Sume "must be from linklocal\n", 2294120891Sume ip6_sprintf(&src6))); 229578064Sume goto bad; 229653541Sshin } 229753541Sshin if (ip6->ip6_hlim != 255) { 229878064Sume nd6log((LOG_ERR, 2299120891Sume "ICMP6 redirect sent from %s rejected; " 2300120891Sume "hlim=%d (must be 255)\n", 2301120891Sume ip6_sprintf(&src6), ip6->ip6_hlim)); 230278064Sume goto bad; 230353541Sshin } 230453541Sshin { 230553541Sshin /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 230653541Sshin struct sockaddr_in6 sin6; 230753541Sshin struct in6_addr *gw6; 230853541Sshin 230953541Sshin bzero(&sin6, sizeof(sin6)); 231053541Sshin sin6.sin6_family = AF_INET6; 231153541Sshin sin6.sin6_len = sizeof(struct sockaddr_in6); 231253541Sshin bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); 231353541Sshin rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 231453541Sshin if (rt) { 231565895Sume if (rt->rt_gateway == NULL || 231665895Sume rt->rt_gateway->sa_family != AF_INET6) { 231778064Sume nd6log((LOG_ERR, 231865895Sume "ICMP6 redirect rejected; no route " 231965895Sume "with inet6 gateway found for redirect dst: %s\n", 232078064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2321120727Ssam RTFREE_LOCKED(rt); 232278064Sume goto bad; 232365895Sume } 232465895Sume 232553541Sshin gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); 232653541Sshin if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { 232778064Sume nd6log((LOG_ERR, 2328120891Sume "ICMP6 redirect rejected; " 2329120891Sume "not equal to gw-for-src=%s (must be same): " 2330120891Sume "%s\n", 2331120891Sume ip6_sprintf(gw6), 2332120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2333120727Ssam RTFREE_LOCKED(rt); 233478064Sume goto bad; 233553541Sshin } 233653541Sshin } else { 233778064Sume nd6log((LOG_ERR, 2338120891Sume "ICMP6 redirect rejected; " 2339120891Sume "no route found for redirect dst: %s\n", 2340120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 234178064Sume goto bad; 234253541Sshin } 2343120727Ssam RTFREE_LOCKED(rt); 234453541Sshin rt = NULL; 234553541Sshin } 234653541Sshin if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 234778064Sume nd6log((LOG_ERR, 2348120891Sume "ICMP6 redirect rejected; " 2349120891Sume "redirect dst must be unicast: %s\n", 2350120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 235178064Sume goto bad; 235253541Sshin } 235353541Sshin 235453541Sshin is_router = is_onlink = 0; 235553541Sshin if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 235653541Sshin is_router = 1; /* router case */ 235753541Sshin if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 235853541Sshin is_onlink = 1; /* on-link destination case */ 235953541Sshin if (!is_router && !is_onlink) { 236078064Sume nd6log((LOG_ERR, 2361120891Sume "ICMP6 redirect rejected; " 2362120891Sume "neither router case nor onlink case: %s\n", 2363120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 236478064Sume goto bad; 236553541Sshin } 236653541Sshin /* validation passed */ 236753541Sshin 236853541Sshin icmp6len -= sizeof(*nd_rd); 236953541Sshin nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 237053541Sshin if (nd6_options(&ndopts) < 0) { 237178064Sume nd6log((LOG_INFO, "icmp6_redirect_input: " 2372120891Sume "invalid ND option, rejected: %s\n", 2373120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 237478064Sume /* nd6_options have incremented stats */ 237562587Sitojun goto freeit; 237653541Sshin } 237753541Sshin 237853541Sshin if (ndopts.nd_opts_tgt_lladdr) { 237953541Sshin lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 238053541Sshin lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 238153541Sshin } 238253541Sshin 238353541Sshin if (ndopts.nd_opts_rh) { 238453541Sshin redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len; 238553541Sshin redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */ 238653541Sshin } 238753541Sshin 238853541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 238978064Sume nd6log((LOG_INFO, 2390120891Sume "icmp6_redirect_input: lladdrlen mismatch for %s " 2391120891Sume "(if %d, icmp6 packet %d): %s\n", 2392120891Sume ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2, 2393120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 239478064Sume goto bad; 239553541Sshin } 239653541Sshin 239753541Sshin /* RFC 2461 8.3 */ 239853541Sshin nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 2399120891Sume is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 240053541Sshin 240195023Ssuz if (!is_onlink) { /* better router case. perform rtredirect. */ 240253541Sshin /* perform rtredirect */ 240353541Sshin struct sockaddr_in6 sdst; 240453541Sshin struct sockaddr_in6 sgw; 240553541Sshin struct sockaddr_in6 ssrc; 240653541Sshin 240753541Sshin bzero(&sdst, sizeof(sdst)); 240853541Sshin bzero(&sgw, sizeof(sgw)); 240953541Sshin bzero(&ssrc, sizeof(ssrc)); 241053541Sshin sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; 241153541Sshin sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = 241253541Sshin sizeof(struct sockaddr_in6); 241353541Sshin bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); 241453541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 241553541Sshin bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 241653541Sshin rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw, 2417120891Sume (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST, 2418120891Sume (struct sockaddr *)&ssrc); 241953541Sshin } 242053541Sshin /* finally update cached route in each socket via pfctlinput */ 242153541Sshin { 242253541Sshin struct sockaddr_in6 sdst; 242353541Sshin 242453541Sshin bzero(&sdst, sizeof(sdst)); 242553541Sshin sdst.sin6_family = AF_INET6; 242653541Sshin sdst.sin6_len = sizeof(struct sockaddr_in6); 242753541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 242853541Sshin pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); 2429122062Sume#if defined(IPSEC) || defined(FAST_IPSEC) 243053541Sshin key_sa_routechange((struct sockaddr *)&sdst); 243153541Sshin#endif 243253541Sshin } 243362587Sitojun 243462587Sitojun freeit: 243562587Sitojun m_freem(m); 243678064Sume return; 243778064Sume 243878064Sume bad: 243978064Sume icmp6stat.icp6s_badredirect++; 244078064Sume m_freem(m); 244153541Sshin} 244253541Sshin 244353541Sshinvoid 244453541Sshinicmp6_redirect_output(m0, rt) 244553541Sshin struct mbuf *m0; 244653541Sshin struct rtentry *rt; 244753541Sshin{ 244853541Sshin struct ifnet *ifp; /* my outgoing interface */ 244953541Sshin struct in6_addr *ifp_ll6; 245053541Sshin struct in6_addr *router_ll6; 245153541Sshin struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 245253541Sshin struct mbuf *m = NULL; /* newly allocated one */ 245353541Sshin struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 245453541Sshin struct nd_redirect *nd_rd; 245553541Sshin size_t maxlen; 245653541Sshin u_char *p; 245753541Sshin struct ifnet *outif = NULL; 245862587Sitojun struct sockaddr_in6 src_sa; 245953541Sshin 246062587Sitojun icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0); 246162587Sitojun 246253541Sshin /* if we are not router, we don't send icmp6 redirect */ 246353541Sshin if (!ip6_forwarding || ip6_accept_rtadv) 246453541Sshin goto fail; 246553541Sshin 246653541Sshin /* sanity check */ 246753541Sshin if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) 246853541Sshin goto fail; 246953541Sshin 247053541Sshin /* 247153541Sshin * Address check: 247253541Sshin * the source address must identify a neighbor, and 247353541Sshin * the destination address must not be a multicast address 247453541Sshin * [RFC 2461, sec 8.2] 247553541Sshin */ 247653541Sshin sip6 = mtod(m0, struct ip6_hdr *); 247762587Sitojun bzero(&src_sa, sizeof(src_sa)); 247862587Sitojun src_sa.sin6_family = AF_INET6; 247962587Sitojun src_sa.sin6_len = sizeof(src_sa); 248062587Sitojun src_sa.sin6_addr = sip6->ip6_src; 248162587Sitojun if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) 248253541Sshin goto fail; 248353541Sshin if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) 248453541Sshin goto fail; /* what should we do here? */ 248553541Sshin 248653541Sshin /* rate limit */ 248753541Sshin if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) 248853541Sshin goto fail; 248953541Sshin 249053541Sshin /* 249153541Sshin * Since we are going to append up to 1280 bytes (= IPV6_MMTU), 249253541Sshin * we almost always ask for an mbuf cluster for simplicity. 249353541Sshin * (MHLEN < IPV6_MMTU is almost always true) 249453541Sshin */ 249562587Sitojun#if IPV6_MMTU >= MCLBYTES 249662587Sitojun# error assumption failed about IPV6_MMTU and MCLBYTES 249762587Sitojun#endif 2498111119Simp MGETHDR(m, M_DONTWAIT, MT_HEADER); 249962587Sitojun if (m && IPV6_MMTU >= MHLEN) 2500111119Simp MCLGET(m, M_DONTWAIT); 250153541Sshin if (!m) 250253541Sshin goto fail; 250378064Sume m->m_pkthdr.rcvif = NULL; 250478064Sume m->m_len = 0; 250578064Sume maxlen = M_TRAILINGSPACE(m); 250653541Sshin maxlen = min(IPV6_MMTU, maxlen); 250753541Sshin /* just for safety */ 250862587Sitojun if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 250962587Sitojun ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { 251053541Sshin goto fail; 251162587Sitojun } 251253541Sshin 251353541Sshin { 251453541Sshin /* get ip6 linklocal address for ifp(my outgoing interface). */ 251562587Sitojun struct in6_ifaddr *ia; 251662587Sitojun if ((ia = in6ifa_ifpforlinklocal(ifp, 251762587Sitojun IN6_IFF_NOTREADY| 251862587Sitojun IN6_IFF_ANYCAST)) == NULL) 251953541Sshin goto fail; 252053541Sshin ifp_ll6 = &ia->ia_addr.sin6_addr; 252153541Sshin } 252253541Sshin 252353541Sshin /* get ip6 linklocal address for the router. */ 252453541Sshin if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { 252553541Sshin struct sockaddr_in6 *sin6; 252653541Sshin sin6 = (struct sockaddr_in6 *)rt->rt_gateway; 252753541Sshin router_ll6 = &sin6->sin6_addr; 252853541Sshin if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) 252953541Sshin router_ll6 = (struct in6_addr *)NULL; 253053541Sshin } else 253153541Sshin router_ll6 = (struct in6_addr *)NULL; 253253541Sshin 253353541Sshin /* ip6 */ 253453541Sshin ip6 = mtod(m, struct ip6_hdr *); 253553541Sshin ip6->ip6_flow = 0; 253662587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 253762587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 253853541Sshin /* ip6->ip6_plen will be set later */ 253953541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 254053541Sshin ip6->ip6_hlim = 255; 254153541Sshin /* ip6->ip6_src must be linklocal addr for my outgoing if. */ 254253541Sshin bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); 254353541Sshin bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); 254453541Sshin 254553541Sshin /* ND Redirect */ 254653541Sshin nd_rd = (struct nd_redirect *)(ip6 + 1); 254753541Sshin nd_rd->nd_rd_type = ND_REDIRECT; 254853541Sshin nd_rd->nd_rd_code = 0; 254953541Sshin nd_rd->nd_rd_reserved = 0; 255053541Sshin if (rt->rt_flags & RTF_GATEWAY) { 255153541Sshin /* 255253541Sshin * nd_rd->nd_rd_target must be a link-local address in 255353541Sshin * better router cases. 255453541Sshin */ 255553541Sshin if (!router_ll6) 255653541Sshin goto fail; 255753541Sshin bcopy(router_ll6, &nd_rd->nd_rd_target, 2558120892Sume sizeof(nd_rd->nd_rd_target)); 255953541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2560120892Sume sizeof(nd_rd->nd_rd_dst)); 256153541Sshin } else { 256253541Sshin /* make sure redtgt == reddst */ 256353541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, 2564120892Sume sizeof(nd_rd->nd_rd_target)); 256553541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2566120892Sume sizeof(nd_rd->nd_rd_dst)); 256753541Sshin } 256853541Sshin 256953541Sshin p = (u_char *)(nd_rd + 1); 257053541Sshin 257153541Sshin if (!router_ll6) 257253541Sshin goto nolladdropt; 257353541Sshin 2574120892Sume { 2575120892Sume /* target lladdr option */ 2576120892Sume struct rtentry *rt_router = NULL; 2577120892Sume int len; 2578120892Sume struct sockaddr_dl *sdl; 2579120892Sume struct nd_opt_hdr *nd_opt; 2580120892Sume char *lladdr; 258153541Sshin 2582120892Sume rt_router = nd6_lookup(router_ll6, 0, ifp); 2583120892Sume if (!rt_router) 2584120892Sume goto nolladdropt; 2585120892Sume len = sizeof(*nd_opt) + ifp->if_addrlen; 2586120892Sume len = (len + 7) & ~7; /* round by 8 */ 2587120892Sume /* safety check */ 2588120892Sume if (len + (p - (u_char *)ip6) > maxlen) 2589120892Sume goto nolladdropt; 2590120892Sume if (!(rt_router->rt_flags & RTF_GATEWAY) && 2591120892Sume (rt_router->rt_flags & RTF_LLINFO) && 2592120892Sume (rt_router->rt_gateway->sa_family == AF_LINK) && 2593120892Sume (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) && 2594120892Sume sdl->sdl_alen) { 2595120892Sume nd_opt = (struct nd_opt_hdr *)p; 2596120892Sume nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 2597120892Sume nd_opt->nd_opt_len = len >> 3; 2598120892Sume lladdr = (char *)(nd_opt + 1); 2599120892Sume bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen); 2600120892Sume p += len; 2601120892Sume } 2602120893Sume } 260353541Sshinnolladdropt:; 260453541Sshin 260553541Sshin m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 260653541Sshin 260753541Sshin /* just to be safe */ 260862587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 260953541Sshin if (m0->m_flags & M_DECRYPTED) 261053541Sshin goto noredhdropt; 261162587Sitojun#endif 261262587Sitojun if (p - (u_char *)ip6 > maxlen) 261362587Sitojun goto noredhdropt; 261453541Sshin 2615120891Sume { 2616120891Sume /* redirected header option */ 2617120891Sume int len; 2618120891Sume struct nd_opt_rd_hdr *nd_opt_rh; 261953541Sshin 2620120891Sume /* 2621120891Sume * compute the maximum size for icmp6 redirect header option. 2622120891Sume * XXX room for auth header? 2623120891Sume */ 2624120891Sume len = maxlen - (p - (u_char *)ip6); 2625120891Sume len &= ~7; 262653541Sshin 2627120891Sume /* This is just for simplicity. */ 2628120891Sume if (m0->m_pkthdr.len != m0->m_len) { 2629120891Sume if (m0->m_next) { 2630120891Sume m_freem(m0->m_next); 2631120891Sume m0->m_next = NULL; 2632120891Sume } 2633120891Sume m0->m_pkthdr.len = m0->m_len; 263453541Sshin } 263553541Sshin 2636120891Sume /* 2637120891Sume * Redirected header option spec (RFC2461 4.6.3) talks nothing 2638120891Sume * about padding/truncate rule for the original IP packet. 2639120891Sume * From the discussion on IPv6imp in Feb 1999, 2640120891Sume * the consensus was: 2641120891Sume * - "attach as much as possible" is the goal 2642120891Sume * - pad if not aligned (original size can be guessed by 2643120891Sume * original ip6 header) 2644120891Sume * Following code adds the padding if it is simple enough, 2645120891Sume * and truncates if not. 2646120891Sume */ 2647120891Sume if (m0->m_next || m0->m_pkthdr.len != m0->m_len) 2648120891Sume panic("assumption failed in %s:%d", __FILE__, 2649120891Sume __LINE__); 265053541Sshin 2651120891Sume if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { 2652120891Sume /* not enough room, truncate */ 2653120891Sume m0->m_pkthdr.len = m0->m_len = len - 2654120891Sume sizeof(*nd_opt_rh); 2655120891Sume } else { 2656120891Sume /* enough room, pad or truncate */ 2657120891Sume size_t extra; 265853541Sshin 2659120891Sume extra = m0->m_pkthdr.len % 8; 2660120891Sume if (extra) { 2661120891Sume /* pad if easy enough, truncate if not */ 2662120891Sume if (8 - extra <= M_TRAILINGSPACE(m0)) { 2663120891Sume /* pad */ 2664120891Sume m0->m_len += (8 - extra); 2665120891Sume m0->m_pkthdr.len += (8 - extra); 2666120891Sume } else { 2667120891Sume /* truncate */ 2668120891Sume m0->m_pkthdr.len -= extra; 2669120891Sume m0->m_len -= extra; 2670120891Sume } 267153541Sshin } 2672120891Sume len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); 2673120891Sume m0->m_pkthdr.len = m0->m_len = len - 2674120891Sume sizeof(*nd_opt_rh); 267553541Sshin } 267653541Sshin 2677120891Sume nd_opt_rh = (struct nd_opt_rd_hdr *)p; 2678120891Sume bzero(nd_opt_rh, sizeof(*nd_opt_rh)); 2679120891Sume nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; 2680120891Sume nd_opt_rh->nd_opt_rh_len = len >> 3; 2681120891Sume p += sizeof(*nd_opt_rh); 2682120891Sume m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 268353541Sshin 2684120891Sume /* connect m0 to m */ 2685120891Sume m_tag_delete_chain(m0, NULL); 2686120891Sume m0->m_flags &= ~M_PKTHDR; 2687120891Sume m->m_next = m0; 2688120891Sume m->m_pkthdr.len = m->m_len + m0->m_len; 2689120891Sume m0 = NULL; 2690120891Sume } 269153541Sshinnoredhdropt:; 2692112781Ssuz if (m0) { 2693112781Ssuz m_freem(m0); 2694112781Ssuz m0 = NULL; 2695112781Ssuz } 269653541Sshin 2697121315Sume /* XXX: clear embedded link IDs in the inner header */ 2698121315Sume in6_clearscope(&sip6->ip6_src); 2699121315Sume in6_clearscope(&sip6->ip6_dst); 2700121315Sume in6_clearscope(&nd_rd->nd_rd_target); 2701121315Sume in6_clearscope(&nd_rd->nd_rd_dst); 270253541Sshin 270353541Sshin ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 270453541Sshin 270553541Sshin nd_rd->nd_rd_cksum = 0; 2706120891Sume nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2707120891Sume sizeof(*ip6), ntohs(ip6->ip6_plen)); 270853541Sshin 270953541Sshin /* send the packet to outside... */ 2710105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 271153541Sshin if (outif) { 271253541Sshin icmp6_ifstat_inc(outif, ifs6_out_msg); 271353541Sshin icmp6_ifstat_inc(outif, ifs6_out_redirect); 271453541Sshin } 271553541Sshin icmp6stat.icp6s_outhist[ND_REDIRECT]++; 271653541Sshin 271753541Sshin return; 271853541Sshin 271953541Sshinfail: 272053541Sshin if (m) 272153541Sshin m_freem(m); 272253541Sshin if (m0) 272353541Sshin m_freem(m0); 272453541Sshin} 272553541Sshin 272653541Sshin/* 272753541Sshin * ICMPv6 socket option processing. 272853541Sshin */ 272953541Sshinint 273053541Sshinicmp6_ctloutput(so, sopt) 273153541Sshin struct socket *so; 273253541Sshin struct sockopt *sopt; 273353541Sshin{ 273453541Sshin int error = 0; 273553541Sshin int optlen; 273678064Sume struct inpcb *inp = sotoinpcb(so); 273753541Sshin int level, op, optname; 273853541Sshin 273953541Sshin if (sopt) { 274053541Sshin level = sopt->sopt_level; 274153541Sshin op = sopt->sopt_dir; 274253541Sshin optname = sopt->sopt_name; 274353541Sshin optlen = sopt->sopt_valsize; 274453541Sshin } else 274553541Sshin level = op = optname = optlen = 0; 274678064Sume 274753541Sshin if (level != IPPROTO_ICMPV6) { 274853541Sshin return EINVAL; 274953541Sshin } 275053541Sshin 275178064Sume switch (op) { 275253541Sshin case PRCO_SETOPT: 275353541Sshin switch (optname) { 275453541Sshin case ICMP6_FILTER: 275553541Sshin { 275653541Sshin struct icmp6_filter *p; 275753541Sshin 275853541Sshin if (optlen != sizeof(*p)) { 275953541Sshin error = EMSGSIZE; 276053541Sshin break; 276153541Sshin } 276253541Sshin if (inp->in6p_icmp6filt == NULL) { 276353541Sshin error = EINVAL; 276453541Sshin break; 276553541Sshin } 276653541Sshin error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen, 276753541Sshin optlen); 276853541Sshin break; 276953541Sshin } 277053541Sshin 277153541Sshin default: 277253541Sshin error = ENOPROTOOPT; 277353541Sshin break; 277453541Sshin } 277553541Sshin break; 277653541Sshin 277753541Sshin case PRCO_GETOPT: 277853541Sshin switch (optname) { 277953541Sshin case ICMP6_FILTER: 278053541Sshin { 278153541Sshin if (inp->in6p_icmp6filt == NULL) { 278253541Sshin error = EINVAL; 278353541Sshin break; 278453541Sshin } 278553541Sshin error = sooptcopyout(sopt, inp->in6p_icmp6filt, 278653541Sshin sizeof(struct icmp6_filter)); 278753541Sshin break; 278853541Sshin } 278953541Sshin 279053541Sshin default: 279153541Sshin error = ENOPROTOOPT; 279253541Sshin break; 279353541Sshin } 279453541Sshin break; 279553541Sshin } 279653541Sshin 2797120856Sume return (error); 279853541Sshin} 279953541Sshin 280053541Sshin/* 280153541Sshin * Perform rate limit check. 280253541Sshin * Returns 0 if it is okay to send the icmp6 packet. 280353541Sshin * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 280453541Sshin * limitation. 280553541Sshin * 280653541Sshin * XXX per-destination/type check necessary? 280753541Sshin */ 280853541Sshinstatic int 280953541Sshinicmp6_ratelimit(dst, type, code) 281053541Sshin const struct in6_addr *dst; /* not used at this moment */ 281153541Sshin const int type; /* not used at this moment */ 281253541Sshin const int code; /* not used at this moment */ 281353541Sshin{ 281462587Sitojun int ret; 281553541Sshin 281695023Ssuz ret = 0; /* okay to send */ 281753541Sshin 281862587Sitojun /* PPS limit */ 281978064Sume if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count, 282078064Sume icmp6errppslim)) { 282153541Sshin /* The packet is subject to rate limit */ 282262587Sitojun ret++; 282353541Sshin } 282453541Sshin 282562587Sitojun return ret; 282653541Sshin} 2827