icmp6.c revision 171260
162587Sitojun/* $FreeBSD: head/sys/netinet6/icmp6.c 171260 2007-07-05 16:29:40Z delphij $ */ 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 102171167Sgnn#ifdef IPSEC 103105199Ssam#include <netipsec/ipsec.h> 104105199Ssam#include <netipsec/key.h> 105105199Ssam#endif 106105199Ssam 10762587Sitojunextern struct domain inet6domain; 10853541Sshin 10962587Sitojunstruct icmp6stat icmp6stat; 11053541Sshin 111141553Srwatsonextern struct inpcbinfo ripcbinfo; 11262587Sitojunextern struct inpcbhead ripcb; 11362587Sitojunextern int icmp6errppslim; 11462587Sitojunstatic int icmp6errpps_count = 0; 11578064Sumestatic struct timeval icmp6errppslim_last; 11662587Sitojunextern int icmp6_nodeinfo; 11753541Sshin 11862587Sitojunstatic void icmp6_errcount __P((struct icmp6errstat *, int, int)); 11962587Sitojunstatic int icmp6_rip6_input __P((struct mbuf **, int)); 12062587Sitojunstatic int icmp6_ratelimit __P((const struct in6_addr *, const int, const int)); 12162587Sitojunstatic const char *icmp6_redirect_diag __P((struct in6_addr *, 12262587Sitojun struct in6_addr *, struct in6_addr *)); 12362587Sitojunstatic struct mbuf *ni6_input __P((struct mbuf *, int)); 12462587Sitojunstatic struct mbuf *ni6_nametodns __P((const char *, int, int)); 12562587Sitojunstatic int ni6_dnsmatch __P((const char *, int, const char *, int)); 12662587Sitojunstatic int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *, 127148892Sume struct ifnet **, struct in6_addr *)); 12862587Sitojunstatic int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, 12962587Sitojun struct ifnet *, int)); 130142681Sumestatic int icmp6_notify_error __P((struct mbuf **, int, int, int)); 13153541Sshin 13253541Sshin 13353541Sshinvoid 134171259Sdelphijicmp6_init(void) 13553541Sshin{ 136171259Sdelphij 13753541Sshin mld6_init(); 13853541Sshin} 13953541Sshin 14062587Sitojunstatic void 141171259Sdelphijicmp6_errcount(struct icmp6errstat *stat, int type, int code) 14262587Sitojun{ 14378064Sume switch (type) { 14462587Sitojun case ICMP6_DST_UNREACH: 14562587Sitojun switch (code) { 14662587Sitojun case ICMP6_DST_UNREACH_NOROUTE: 14762587Sitojun stat->icp6errs_dst_unreach_noroute++; 14862587Sitojun return; 14962587Sitojun case ICMP6_DST_UNREACH_ADMIN: 15062587Sitojun stat->icp6errs_dst_unreach_admin++; 15162587Sitojun return; 15262587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 15362587Sitojun stat->icp6errs_dst_unreach_beyondscope++; 15462587Sitojun return; 15562587Sitojun case ICMP6_DST_UNREACH_ADDR: 15662587Sitojun stat->icp6errs_dst_unreach_addr++; 15762587Sitojun return; 15862587Sitojun case ICMP6_DST_UNREACH_NOPORT: 15962587Sitojun stat->icp6errs_dst_unreach_noport++; 16062587Sitojun return; 16162587Sitojun } 16262587Sitojun break; 16362587Sitojun case ICMP6_PACKET_TOO_BIG: 16462587Sitojun stat->icp6errs_packet_too_big++; 16562587Sitojun return; 16662587Sitojun case ICMP6_TIME_EXCEEDED: 16778064Sume switch (code) { 16862587Sitojun case ICMP6_TIME_EXCEED_TRANSIT: 16962587Sitojun stat->icp6errs_time_exceed_transit++; 17062587Sitojun return; 17162587Sitojun case ICMP6_TIME_EXCEED_REASSEMBLY: 17262587Sitojun stat->icp6errs_time_exceed_reassembly++; 17362587Sitojun return; 17462587Sitojun } 17562587Sitojun break; 17662587Sitojun case ICMP6_PARAM_PROB: 17778064Sume switch (code) { 17862587Sitojun case ICMP6_PARAMPROB_HEADER: 17962587Sitojun stat->icp6errs_paramprob_header++; 18062587Sitojun return; 18162587Sitojun case ICMP6_PARAMPROB_NEXTHEADER: 18262587Sitojun stat->icp6errs_paramprob_nextheader++; 18362587Sitojun return; 18462587Sitojun case ICMP6_PARAMPROB_OPTION: 18562587Sitojun stat->icp6errs_paramprob_option++; 18662587Sitojun return; 18762587Sitojun } 18862587Sitojun break; 18962587Sitojun case ND_REDIRECT: 19062587Sitojun stat->icp6errs_redirect++; 19162587Sitojun return; 19262587Sitojun } 19362587Sitojun stat->icp6errs_unknown++; 19462587Sitojun} 19562587Sitojun 19653541Sshin/* 197148385Sume * A wrapper function for icmp6_error() necessary when the erroneous packet 198148385Sume * may not contain enough scope zone information. 199148385Sume */ 200148385Sumevoid 201171259Sdelphijicmp6_error2(struct mbuf *m, int type, int code, int param, 202171259Sdelphij struct ifnet *ifp) 203148385Sume{ 204148385Sume struct ip6_hdr *ip6; 205148385Sume 206148385Sume if (ifp == NULL) 207148385Sume return; 208148385Sume 209148385Sume#ifndef PULLDOWN_TEST 210148385Sume IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 211148385Sume#else 212148385Sume if (m->m_len < sizeof(struct ip6_hdr)) { 213148385Sume m = m_pullup(m, sizeof(struct ip6_hdr)); 214148385Sume if (m == NULL) 215148385Sume return; 216148385Sume } 217148385Sume#endif 218148385Sume 219148385Sume ip6 = mtod(m, struct ip6_hdr *); 220148385Sume 221148385Sume if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0) 222148385Sume return; 223148385Sume if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) 224148385Sume return; 225148385Sume 226148385Sume icmp6_error(m, type, code, param); 227148385Sume} 228148385Sume 229148385Sume/* 23053541Sshin * Generate an error packet of type error in response to bad IP6 packet. 23153541Sshin */ 23253541Sshinvoid 233171259Sdelphijicmp6_error(struct mbuf *m, int type, int code, int param) 23453541Sshin{ 23553541Sshin struct ip6_hdr *oip6, *nip6; 23653541Sshin struct icmp6_hdr *icmp6; 23762587Sitojun u_int preplen; 23853541Sshin int off; 23962587Sitojun int nxt; 24053541Sshin 24153541Sshin icmp6stat.icp6s_error++; 24253541Sshin 24362587Sitojun /* count per-type-code statistics */ 24462587Sitojun icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code); 24562587Sitojun 24662587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 24762587Sitojun if (m->m_flags & M_DECRYPTED) { 24862587Sitojun icmp6stat.icp6s_canterror++; 24953541Sshin goto freeit; 25062587Sitojun } 25162587Sitojun#endif 25253541Sshin 25362587Sitojun#ifndef PULLDOWN_TEST 25462587Sitojun IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 25562587Sitojun#else 25662587Sitojun if (m->m_len < sizeof(struct ip6_hdr)) { 25762587Sitojun m = m_pullup(m, sizeof(struct ip6_hdr)); 25862587Sitojun if (m == NULL) 25962587Sitojun return; 26062587Sitojun } 26162587Sitojun#endif 26253541Sshin oip6 = mtod(m, struct ip6_hdr *); 26353541Sshin 26453541Sshin /* 265121472Sume * If the destination address of the erroneous packet is a multicast 266121472Sume * address, or the packet was sent using link-layer multicast, 267121472Sume * we should basically suppress sending an error (RFC 2463, Section 268121472Sume * 2.4). 269121472Sume * We have two exceptions (the item e.2 in that section): 270121472Sume * - the Pakcet Too Big message can be sent for path MTU discovery. 271121472Sume * - the Parameter Problem Message that can be allowed an icmp6 error 272121472Sume * in the option type field. This check has been done in 273121472Sume * ip6_unknown_opt(), so we can just check the type and code. 27453541Sshin */ 27553541Sshin if ((m->m_flags & (M_BCAST|M_MCAST) || 27653541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && 27753541Sshin (type != ICMP6_PACKET_TOO_BIG && 27853541Sshin (type != ICMP6_PARAM_PROB || 27953541Sshin code != ICMP6_PARAMPROB_OPTION))) 28053541Sshin goto freeit; 28153541Sshin 282121472Sume /* 283121472Sume * RFC 2463, 2.4 (e.5): source address check. 284121472Sume * XXX: the case of anycast source? 285121472Sume */ 28653541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || 28753541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) 28853541Sshin goto freeit; 28953541Sshin 29053541Sshin /* 29162587Sitojun * If we are about to send ICMPv6 against ICMPv6 error/redirect, 29262587Sitojun * don't do it. 29353541Sshin */ 29462587Sitojun nxt = -1; 29562587Sitojun off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 29662587Sitojun if (off >= 0 && nxt == IPPROTO_ICMPV6) { 29753541Sshin struct icmp6_hdr *icp; 29853541Sshin 29962587Sitojun#ifndef PULLDOWN_TEST 30062587Sitojun IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), ); 30162587Sitojun icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 30262587Sitojun#else 30362587Sitojun IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, 30462587Sitojun sizeof(*icp)); 30562587Sitojun if (icp == NULL) { 30662587Sitojun icmp6stat.icp6s_tooshort++; 30762587Sitojun return; 30862587Sitojun } 30962587Sitojun#endif 31062587Sitojun if (icp->icmp6_type < ICMP6_ECHO_REQUEST || 31162587Sitojun icp->icmp6_type == ND_REDIRECT) { 31253541Sshin /* 31362587Sitojun * ICMPv6 error 31462587Sitojun * Special case: for redirect (which is 31562587Sitojun * informational) we must not send icmp6 error. 31653541Sshin */ 31762587Sitojun icmp6stat.icp6s_canterror++; 31862587Sitojun goto freeit; 31962587Sitojun } else { 32062587Sitojun /* ICMPv6 informational - send the error */ 32153541Sshin } 32262587Sitojun } else { 32362587Sitojun /* non-ICMPv6 - send the error */ 32453541Sshin } 32553541Sshin 32653541Sshin oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */ 32753541Sshin 32853541Sshin /* Finally, do rate limitation check. */ 32953541Sshin if (icmp6_ratelimit(&oip6->ip6_src, type, code)) { 33053541Sshin icmp6stat.icp6s_toofreq++; 33153541Sshin goto freeit; 33253541Sshin } 33353541Sshin 33453541Sshin /* 33553541Sshin * OK, ICMP6 can be generated. 33653541Sshin */ 33753541Sshin 33853541Sshin if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) 33953541Sshin m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); 34053541Sshin 34162587Sitojun preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 342111119Simp M_PREPEND(m, preplen, M_DONTWAIT); 34362587Sitojun if (m && m->m_len < preplen) 34462587Sitojun m = m_pullup(m, preplen); 34553541Sshin if (m == NULL) { 34678064Sume nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__)); 34753541Sshin return; 34853541Sshin } 34953541Sshin 35053541Sshin nip6 = mtod(m, struct ip6_hdr *); 35153541Sshin nip6->ip6_src = oip6->ip6_src; 35253541Sshin nip6->ip6_dst = oip6->ip6_dst; 35353541Sshin 354121315Sume in6_clearscope(&oip6->ip6_src); 355121315Sume in6_clearscope(&oip6->ip6_dst); 35653541Sshin 35753541Sshin icmp6 = (struct icmp6_hdr *)(nip6 + 1); 35853541Sshin icmp6->icmp6_type = type; 35953541Sshin icmp6->icmp6_code = code; 36053541Sshin icmp6->icmp6_pptr = htonl((u_int32_t)param); 36153541Sshin 36278064Sume /* 36378064Sume * icmp6_reflect() is designed to be in the input path. 364148987Sume * icmp6_error() can be called from both input and output path, 36578064Sume * and if we are in output path rcvif could contain bogus value. 36678064Sume * clear m->m_pkthdr.rcvif for safety, we should have enough scope 36778064Sume * information in ip header (nip6). 36878064Sume */ 36978064Sume m->m_pkthdr.rcvif = NULL; 37078064Sume 37153541Sshin icmp6stat.icp6s_outhist[type]++; 37295023Ssuz icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ 37362587Sitojun 37462587Sitojun return; 37562587Sitojun 37662587Sitojun freeit: 37762587Sitojun /* 378148987Sume * If we can't tell whether or not we can generate ICMP6, free it. 37962587Sitojun */ 38062587Sitojun m_freem(m); 38153541Sshin} 38253541Sshin 38353541Sshin/* 38453541Sshin * Process a received ICMP6 message. 38553541Sshin */ 38653541Sshinint 387171259Sdelphijicmp6_input(struct mbuf **mp, int *offp, int proto) 38853541Sshin{ 38953541Sshin struct mbuf *m = *mp, *n; 39053541Sshin struct ip6_hdr *ip6, *nip6; 39153541Sshin struct icmp6_hdr *icmp6, *nicmp6; 39253541Sshin int off = *offp; 39353541Sshin int icmp6len = m->m_pkthdr.len - *offp; 39453541Sshin int code, sum, noff; 395165118Sbz char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 39653541Sshin 39762587Sitojun#ifndef PULLDOWN_TEST 39853541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE); 39995023Ssuz /* m might change if M_LOOP. So, call mtod after this */ 40062587Sitojun#endif 40153541Sshin 40253541Sshin /* 40353541Sshin * Locate icmp6 structure in mbuf, and check 40453541Sshin * that not corrupted and of at least minimum length 40553541Sshin */ 40653541Sshin 40753541Sshin ip6 = mtod(m, struct ip6_hdr *); 40853541Sshin if (icmp6len < sizeof(struct icmp6_hdr)) { 40953541Sshin icmp6stat.icp6s_tooshort++; 41053541Sshin goto freeit; 41153541Sshin } 41253541Sshin 41353541Sshin /* 41453541Sshin * calculate the checksum 41553541Sshin */ 41662587Sitojun#ifndef PULLDOWN_TEST 41753541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 41862587Sitojun#else 41962587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 42062587Sitojun if (icmp6 == NULL) { 42162587Sitojun icmp6stat.icp6s_tooshort++; 42262587Sitojun return IPPROTO_DONE; 42362587Sitojun } 42462587Sitojun#endif 42553541Sshin code = icmp6->icmp6_code; 42653541Sshin 42753541Sshin if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { 42878064Sume nd6log((LOG_ERR, 42953541Sshin "ICMP6 checksum error(%d|%x) %s\n", 430165118Sbz icmp6->icmp6_type, sum, 431165118Sbz ip6_sprintf(ip6bufs, &ip6->ip6_src))); 43253541Sshin icmp6stat.icp6s_checksum++; 43353541Sshin goto freeit; 43453541Sshin } 43553541Sshin 43683934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 43753541Sshin /* 43853541Sshin * Deliver very specific ICMP6 type only. 439148987Sume * This is important to deliver TOOBIG. Otherwise PMTUD 44053541Sshin * will not work. 44153541Sshin */ 44253541Sshin switch (icmp6->icmp6_type) { 44353541Sshin case ICMP6_DST_UNREACH: 44453541Sshin case ICMP6_PACKET_TOO_BIG: 44553541Sshin case ICMP6_TIME_EXCEEDED: 44653541Sshin break; 44753541Sshin default: 44853541Sshin goto freeit; 44953541Sshin } 45053541Sshin } 45153541Sshin 45253541Sshin icmp6stat.icp6s_inhist[icmp6->icmp6_type]++; 45353541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg); 45453541Sshin if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) 45553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error); 45653541Sshin 45753541Sshin switch (icmp6->icmp6_type) { 45853541Sshin case ICMP6_DST_UNREACH: 45953541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach); 46053541Sshin switch (code) { 46153541Sshin case ICMP6_DST_UNREACH_NOROUTE: 46253541Sshin code = PRC_UNREACH_NET; 46353541Sshin break; 46453541Sshin case ICMP6_DST_UNREACH_ADMIN: 46553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib); 46662587Sitojun code = PRC_UNREACH_PROTOCOL; /* is this a good code? */ 46762587Sitojun break; 46853541Sshin case ICMP6_DST_UNREACH_ADDR: 46962587Sitojun code = PRC_HOSTDEAD; 47053541Sshin break; 47162587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 47262587Sitojun /* I mean "source address was incorrect." */ 47362587Sitojun code = PRC_PARAMPROB; 47462587Sitojun break; 47553541Sshin case ICMP6_DST_UNREACH_NOPORT: 47653541Sshin code = PRC_UNREACH_PORT; 47753541Sshin break; 47853541Sshin default: 47953541Sshin goto badcode; 48053541Sshin } 48153541Sshin goto deliver; 48253541Sshin break; 48353541Sshin 48453541Sshin case ICMP6_PACKET_TOO_BIG: 48553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig); 48653541Sshin 487126194Sume /* validation is made in icmp6_mtudisc_update */ 488126194Sume 48953541Sshin code = PRC_MSGSIZE; 49053541Sshin 49162587Sitojun /* 49262587Sitojun * Updating the path MTU will be done after examining 49362587Sitojun * intermediate extension headers. 49462587Sitojun */ 49553541Sshin goto deliver; 49653541Sshin break; 49753541Sshin 49853541Sshin case ICMP6_TIME_EXCEEDED: 49953541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed); 50053541Sshin switch (code) { 50153541Sshin case ICMP6_TIME_EXCEED_TRANSIT: 502151465Ssuz code = PRC_TIMXCEED_INTRANS; 503151465Ssuz break; 50453541Sshin case ICMP6_TIME_EXCEED_REASSEMBLY: 505151465Ssuz code = PRC_TIMXCEED_REASS; 50653541Sshin break; 50753541Sshin default: 50853541Sshin goto badcode; 50953541Sshin } 51053541Sshin goto deliver; 51153541Sshin break; 51253541Sshin 51353541Sshin case ICMP6_PARAM_PROB: 51453541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob); 51553541Sshin switch (code) { 51653541Sshin case ICMP6_PARAMPROB_NEXTHEADER: 51753541Sshin code = PRC_UNREACH_PROTOCOL; 51853541Sshin break; 51953541Sshin case ICMP6_PARAMPROB_HEADER: 52053541Sshin case ICMP6_PARAMPROB_OPTION: 52153541Sshin code = PRC_PARAMPROB; 52253541Sshin break; 52353541Sshin default: 52453541Sshin goto badcode; 52553541Sshin } 52653541Sshin goto deliver; 52753541Sshin break; 52853541Sshin 52953541Sshin case ICMP6_ECHO_REQUEST: 53053541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo); 53153541Sshin if (code != 0) 53253541Sshin goto badcode; 53353541Sshin if ((n = m_copy(m, 0, M_COPYALL)) == NULL) { 53453541Sshin /* Give up remote */ 53553541Sshin break; 53653541Sshin } 53762587Sitojun if ((n->m_flags & M_EXT) != 0 53862587Sitojun || n->m_len < off + sizeof(struct icmp6_hdr)) { 53953541Sshin struct mbuf *n0 = n; 54062587Sitojun const int maxlen = sizeof(*nip6) + sizeof(*nicmp6); 541108741Ssam int n0len; 54253541Sshin 543111119Simp MGETHDR(n, M_DONTWAIT, n0->m_type); 544111070Ssam n0len = n0->m_pkthdr.len; /* save for use below */ 545111070Ssam if (n) 546111070Ssam M_MOVE_PKTHDR(n, n0); 54762587Sitojun if (n && maxlen >= MHLEN) { 548111119Simp MCLGET(n, M_DONTWAIT); 54962587Sitojun if ((n->m_flags & M_EXT) == 0) { 55062587Sitojun m_free(n); 55162587Sitojun n = NULL; 55262587Sitojun } 55362587Sitojun } 55453541Sshin if (n == NULL) { 55553541Sshin /* Give up remote */ 55653541Sshin m_freem(n0); 55753541Sshin break; 55853541Sshin } 55953541Sshin /* 56053541Sshin * Copy IPv6 and ICMPv6 only. 56153541Sshin */ 56253541Sshin nip6 = mtod(n, struct ip6_hdr *); 56353541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 56453541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 56553541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 56662587Sitojun noff = sizeof(struct ip6_hdr); 567108741Ssam /* new mbuf contains only ipv6+icmpv6 headers */ 568108741Ssam n->m_len = noff + sizeof(struct icmp6_hdr); 56953541Sshin /* 570120891Sume * Adjust mbuf. ip6_plen will be adjusted in 57162587Sitojun * ip6_output(). 57253541Sshin */ 57362587Sitojun m_adj(n0, off + sizeof(struct icmp6_hdr)); 574108741Ssam /* recalculate complete packet size */ 575108741Ssam n->m_pkthdr.len = n0len + (noff - off); 57662587Sitojun n->m_next = n0; 57753541Sshin } else { 57853541Sshin nip6 = mtod(n, struct ip6_hdr *); 579166046Sume IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off, 580166046Sume sizeof(*nicmp6)); 58153541Sshin noff = off; 58253541Sshin } 58353541Sshin nicmp6->icmp6_type = ICMP6_ECHO_REPLY; 58453541Sshin nicmp6->icmp6_code = 0; 58553541Sshin if (n) { 58653541Sshin icmp6stat.icp6s_reflect++; 58753541Sshin icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++; 58853541Sshin icmp6_reflect(n, noff); 58953541Sshin } 59053541Sshin break; 59153541Sshin 59253541Sshin case ICMP6_ECHO_REPLY: 59353541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply); 59453541Sshin if (code != 0) 59553541Sshin goto badcode; 59653541Sshin break; 59753541Sshin 59896116Sume case MLD_LISTENER_QUERY: 59996116Sume case MLD_LISTENER_REPORT: 60096116Sume if (icmp6len < sizeof(struct mld_hdr)) 60153541Sshin goto badlen; 60296116Sume if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */ 60353541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery); 60453541Sshin else 60553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport); 606111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 60762587Sitojun /* give up local */ 60862587Sitojun mld6_input(m, off); 60962587Sitojun m = NULL; 61062587Sitojun goto freeit; 61162587Sitojun } 61262587Sitojun mld6_input(n, off); 61353541Sshin /* m stays. */ 61453541Sshin break; 61553541Sshin 61696116Sume case MLD_LISTENER_DONE: 61753541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone); 61896116Sume if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */ 61953541Sshin goto badlen; 62053541Sshin break; /* nothing to be done in kernel */ 62153541Sshin 62296116Sume case MLD_MTRACE_RESP: 62396116Sume case MLD_MTRACE: 624120891Sume /* XXX: these two are experimental. not officially defined. */ 62553541Sshin /* XXX: per-interface statistics? */ 62662587Sitojun break; /* just pass it to applications */ 62753541Sshin 62853541Sshin case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ 62953541Sshin { 63053541Sshin enum { WRU, FQDN } mode; 63153541Sshin 63262587Sitojun if (!icmp6_nodeinfo) 63362587Sitojun break; 63462587Sitojun 63553541Sshin if (icmp6len == sizeof(struct icmp6_hdr) + 4) 63653541Sshin mode = WRU; 63762587Sitojun else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) 63853541Sshin mode = FQDN; 63953541Sshin else 64053541Sshin goto badlen; 64153541Sshin 64253541Sshin#define hostnamelen strlen(hostname) 64353541Sshin if (mode == FQDN) { 64462587Sitojun#ifndef PULLDOWN_TEST 64553541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), 646120891Sume IPPROTO_DONE); 64762587Sitojun#endif 64862587Sitojun n = m_copy(m, 0, M_COPYALL); 64962587Sitojun if (n) 65062587Sitojun n = ni6_input(n, off); 65162587Sitojun /* XXX meaningless if n == NULL */ 65253541Sshin noff = sizeof(struct ip6_hdr); 65353541Sshin } else { 65453541Sshin u_char *p; 65562587Sitojun int maxlen, maxhlen; 65653541Sshin 657169664Sjinmei /* 658169664Sjinmei * XXX: this combination of flags is pointless, 659169664Sjinmei * but should we keep this for compatibility? 660169664Sjinmei */ 661120891Sume if ((icmp6_nodeinfo & 5) != 5) 66278064Sume break; 66378064Sume 66462587Sitojun if (code != 0) 66562587Sitojun goto badcode; 66662587Sitojun maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4; 66762587Sitojun if (maxlen >= MCLBYTES) { 66862587Sitojun /* Give up remote */ 66962587Sitojun break; 67062587Sitojun } 671111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 67262587Sitojun if (n && maxlen > MHLEN) { 673111119Simp MCLGET(n, M_DONTWAIT); 67462587Sitojun if ((n->m_flags & M_EXT) == 0) { 67562587Sitojun m_free(n); 67662587Sitojun n = NULL; 67762587Sitojun } 67862587Sitojun } 679166619Sbms if (n && !m_dup_pkthdr(n, m, M_DONTWAIT)) { 680108466Ssam /* 681108466Ssam * Previous code did a blind M_COPY_PKTHDR 682108466Ssam * and said "just for rcvif". If true, then 683108466Ssam * we could tolerate the dup failing (due to 684108466Ssam * the deep copy of the tag chain). For now 685108466Ssam * be conservative and just fail. 686108466Ssam */ 687108466Ssam m_free(n); 688108466Ssam n = NULL; 689108466Ssam } 69053541Sshin if (n == NULL) { 69153541Sshin /* Give up remote */ 69253541Sshin break; 69353541Sshin } 69478064Sume n->m_pkthdr.rcvif = NULL; 69562587Sitojun n->m_len = 0; 69662587Sitojun maxhlen = M_TRAILINGSPACE(n) - maxlen; 69762587Sitojun if (maxhlen > hostnamelen) 69862587Sitojun maxhlen = hostnamelen; 69953541Sshin /* 70053541Sshin * Copy IPv6 and ICMPv6 only. 70153541Sshin */ 70253541Sshin nip6 = mtod(n, struct ip6_hdr *); 70353541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 70453541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 70553541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 70653541Sshin p = (u_char *)(nicmp6 + 1); 70753541Sshin bzero(p, 4); 70895023Ssuz bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */ 70953541Sshin noff = sizeof(struct ip6_hdr); 71053541Sshin n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 71162587Sitojun sizeof(struct icmp6_hdr) + 4 + maxhlen; 71253541Sshin nicmp6->icmp6_type = ICMP6_WRUREPLY; 71353541Sshin nicmp6->icmp6_code = 0; 71453541Sshin } 71553541Sshin#undef hostnamelen 71653541Sshin if (n) { 71753541Sshin icmp6stat.icp6s_reflect++; 71853541Sshin icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++; 71953541Sshin icmp6_reflect(n, noff); 72053541Sshin } 72153541Sshin break; 72253541Sshin } 72353541Sshin 72453541Sshin case ICMP6_WRUREPLY: 72553541Sshin if (code != 0) 72653541Sshin goto badcode; 72753541Sshin break; 72853541Sshin 72953541Sshin case ND_ROUTER_SOLICIT: 73053541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit); 73153541Sshin if (code != 0) 73253541Sshin goto badcode; 73353541Sshin if (icmp6len < sizeof(struct nd_router_solicit)) 73453541Sshin goto badlen; 735111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 73662587Sitojun /* give up local */ 73762587Sitojun nd6_rs_input(m, off, icmp6len); 73862587Sitojun m = NULL; 73962587Sitojun goto freeit; 74062587Sitojun } 74162587Sitojun nd6_rs_input(n, off, icmp6len); 74253541Sshin /* m stays. */ 74353541Sshin break; 74453541Sshin 74553541Sshin case ND_ROUTER_ADVERT: 74653541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert); 74753541Sshin if (code != 0) 74853541Sshin goto badcode; 74953541Sshin if (icmp6len < sizeof(struct nd_router_advert)) 75053541Sshin goto badlen; 751111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 75262587Sitojun /* give up local */ 75362587Sitojun nd6_ra_input(m, off, icmp6len); 75462587Sitojun m = NULL; 75562587Sitojun goto freeit; 75662587Sitojun } 75762587Sitojun nd6_ra_input(n, off, icmp6len); 75853541Sshin /* m stays. */ 75953541Sshin break; 76053541Sshin 76153541Sshin case ND_NEIGHBOR_SOLICIT: 76253541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit); 76353541Sshin if (code != 0) 76453541Sshin goto badcode; 76553541Sshin if (icmp6len < sizeof(struct nd_neighbor_solicit)) 76653541Sshin goto badlen; 767111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 76862587Sitojun /* give up local */ 76962587Sitojun nd6_ns_input(m, off, icmp6len); 77062587Sitojun m = NULL; 77162587Sitojun goto freeit; 77262587Sitojun } 77362587Sitojun nd6_ns_input(n, off, icmp6len); 77453541Sshin /* m stays. */ 77553541Sshin break; 77653541Sshin 77753541Sshin case ND_NEIGHBOR_ADVERT: 77853541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert); 77953541Sshin if (code != 0) 78053541Sshin goto badcode; 78153541Sshin if (icmp6len < sizeof(struct nd_neighbor_advert)) 78253541Sshin goto badlen; 783111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 78462587Sitojun /* give up local */ 78562587Sitojun nd6_na_input(m, off, icmp6len); 78662587Sitojun m = NULL; 78762587Sitojun goto freeit; 78862587Sitojun } 78962587Sitojun nd6_na_input(n, off, icmp6len); 79053541Sshin /* m stays. */ 79153541Sshin break; 79253541Sshin 79353541Sshin case ND_REDIRECT: 79453541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect); 79553541Sshin if (code != 0) 79653541Sshin goto badcode; 79753541Sshin if (icmp6len < sizeof(struct nd_redirect)) 79853541Sshin goto badlen; 799111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 80062587Sitojun /* give up local */ 80162587Sitojun icmp6_redirect_input(m, off); 80262587Sitojun m = NULL; 80362587Sitojun goto freeit; 80462587Sitojun } 80562587Sitojun icmp6_redirect_input(n, off); 80653541Sshin /* m stays. */ 80753541Sshin break; 80853541Sshin 80953541Sshin case ICMP6_ROUTER_RENUMBERING: 81053541Sshin if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && 81153541Sshin code != ICMP6_ROUTER_RENUMBERING_RESULT) 81253541Sshin goto badcode; 81353541Sshin if (icmp6len < sizeof(struct icmp6_router_renum)) 81453541Sshin goto badlen; 81553541Sshin break; 81653541Sshin 81753541Sshin default: 81878064Sume nd6log((LOG_DEBUG, 81978064Sume "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", 820165118Sbz icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src), 821165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst), 82278064Sume m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0)); 82353541Sshin if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { 82453541Sshin /* ICMPv6 error: MUST deliver it by spec... */ 82553541Sshin code = PRC_NCMDS; 82653541Sshin /* deliver */ 82753541Sshin } else { 82853541Sshin /* ICMPv6 informational: MUST not deliver */ 82953541Sshin break; 83053541Sshin } 83153541Sshin deliver: 832142681Sume if (icmp6_notify_error(&m, off, icmp6len, code)) { 83378064Sume /* In this case, m should've been freed. */ 834120856Sume return (IPPROTO_DONE); 83553541Sshin } 83678064Sume break; 83778064Sume 83878064Sume badcode: 83978064Sume icmp6stat.icp6s_badcode++; 84078064Sume break; 84178064Sume 84278064Sume badlen: 84378064Sume icmp6stat.icp6s_badlen++; 84478064Sume break; 84578064Sume } 84678064Sume 84778064Sume /* deliver the packet to appropriate sockets */ 84878064Sume icmp6_rip6_input(&m, *offp); 84978064Sume 85078064Sume return IPPROTO_DONE; 85178064Sume 85278064Sume freeit: 85378064Sume m_freem(m); 85478064Sume return IPPROTO_DONE; 85578064Sume} 85678064Sume 85778064Sumestatic int 858171259Sdelphijicmp6_notify_error(struct mbuf **mp, int off, int icmp6len, int code) 85978064Sume{ 860142681Sume struct mbuf *m = *mp; 86178064Sume struct icmp6_hdr *icmp6; 86278064Sume struct ip6_hdr *eip6; 86378064Sume u_int32_t notifymtu; 86478064Sume struct sockaddr_in6 icmp6src, icmp6dst; 86578064Sume 86678064Sume if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { 86778064Sume icmp6stat.icp6s_tooshort++; 86878064Sume goto freeit; 86978064Sume } 87062587Sitojun#ifndef PULLDOWN_TEST 87178064Sume IP6_EXTHDR_CHECK(m, off, 872120891Sume sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1); 87378064Sume icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 87462587Sitojun#else 87578064Sume IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 876120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 87778064Sume if (icmp6 == NULL) { 87878064Sume icmp6stat.icp6s_tooshort++; 879120856Sume return (-1); 88078064Sume } 88162587Sitojun#endif 88278064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 88353541Sshin 88478064Sume /* Detect the upper level protocol */ 88578064Sume { 88653541Sshin void (*ctlfunc) __P((int, struct sockaddr *, void *)); 88753541Sshin u_int8_t nxt = eip6->ip6_nxt; 88853541Sshin int eoff = off + sizeof(struct icmp6_hdr) + 889120891Sume sizeof(struct ip6_hdr); 89053541Sshin struct ip6ctlparam ip6cp; 89162587Sitojun struct in6_addr *finaldst = NULL; 89262587Sitojun int icmp6type = icmp6->icmp6_type; 89362587Sitojun struct ip6_frag *fh; 89462587Sitojun struct ip6_rthdr *rth; 89562587Sitojun struct ip6_rthdr0 *rth0; 89662587Sitojun int rthlen; 89753541Sshin 89895023Ssuz while (1) { /* XXX: should avoid infinite loop explicitly? */ 89953541Sshin struct ip6_ext *eh; 90053541Sshin 90178064Sume switch (nxt) { 90253541Sshin case IPPROTO_HOPOPTS: 90353541Sshin case IPPROTO_DSTOPTS: 90453541Sshin case IPPROTO_AH: 90562587Sitojun#ifndef PULLDOWN_TEST 906120891Sume IP6_EXTHDR_CHECK(m, 0, 907120891Sume eoff + sizeof(struct ip6_ext), -1); 908120892Sume eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff); 90962587Sitojun#else 91062587Sitojun IP6_EXTHDR_GET(eh, struct ip6_ext *, m, 911120891Sume eoff, sizeof(*eh)); 91262587Sitojun if (eh == NULL) { 91362587Sitojun icmp6stat.icp6s_tooshort++; 914120856Sume return (-1); 91562587Sitojun } 91662587Sitojun#endif 917120891Sume 91853541Sshin if (nxt == IPPROTO_AH) 91953541Sshin eoff += (eh->ip6e_len + 2) << 2; 92053541Sshin else 92153541Sshin eoff += (eh->ip6e_len + 1) << 3; 92253541Sshin nxt = eh->ip6e_nxt; 92353541Sshin break; 92462587Sitojun case IPPROTO_ROUTING: 92562587Sitojun /* 92662587Sitojun * When the erroneous packet contains a 92762587Sitojun * routing header, we should examine the 92862587Sitojun * header to determine the final destination. 92962587Sitojun * Otherwise, we can't properly update 93062587Sitojun * information that depends on the final 93162587Sitojun * destination (e.g. path MTU). 93262587Sitojun */ 93362587Sitojun#ifndef PULLDOWN_TEST 934120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1); 935120891Sume rth = (struct ip6_rthdr *) 936120891Sume (mtod(m, caddr_t) + eoff); 93762587Sitojun#else 93862587Sitojun IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m, 939120891Sume eoff, sizeof(*rth)); 94062587Sitojun if (rth == NULL) { 94162587Sitojun icmp6stat.icp6s_tooshort++; 942120856Sume return (-1); 94362587Sitojun } 94462587Sitojun#endif 94562587Sitojun rthlen = (rth->ip6r_len + 1) << 3; 94662587Sitojun /* 94762587Sitojun * XXX: currently there is no 94862587Sitojun * officially defined type other 94962587Sitojun * than type-0. 95062587Sitojun * Note that if the segment left field 95162587Sitojun * is 0, all intermediate hops must 95262587Sitojun * have been passed. 95362587Sitojun */ 95462587Sitojun if (rth->ip6r_segleft && 95562587Sitojun rth->ip6r_type == IPV6_RTHDR_TYPE_0) { 95662587Sitojun int hops; 95762587Sitojun 95862587Sitojun#ifndef PULLDOWN_TEST 959120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1); 960120891Sume rth0 = (struct ip6_rthdr0 *) 961120891Sume (mtod(m, caddr_t) + eoff); 96262587Sitojun#else 96362587Sitojun IP6_EXTHDR_GET(rth0, 964120891Sume struct ip6_rthdr0 *, m, 965120891Sume eoff, rthlen); 96662587Sitojun if (rth0 == NULL) { 96762587Sitojun icmp6stat.icp6s_tooshort++; 968120856Sume return (-1); 96962587Sitojun } 97062587Sitojun#endif 97162587Sitojun /* just ignore a bogus header */ 97262587Sitojun if ((rth0->ip6r0_len % 2) == 0 && 97362587Sitojun (hops = rth0->ip6r0_len/2)) 97462587Sitojun finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1); 97562587Sitojun } 97662587Sitojun eoff += rthlen; 97762587Sitojun nxt = rth->ip6r_nxt; 97862587Sitojun break; 97962587Sitojun case IPPROTO_FRAGMENT: 98062587Sitojun#ifndef PULLDOWN_TEST 98162587Sitojun IP6_EXTHDR_CHECK(m, 0, eoff + 982120891Sume sizeof(struct ip6_frag), -1); 983120891Sume fh = (struct ip6_frag *)(mtod(m, caddr_t) + 984120891Sume eoff); 98562587Sitojun#else 98662587Sitojun IP6_EXTHDR_GET(fh, struct ip6_frag *, m, 987120891Sume eoff, sizeof(*fh)); 98862587Sitojun if (fh == NULL) { 98962587Sitojun icmp6stat.icp6s_tooshort++; 990120856Sume return (-1); 99162587Sitojun } 99262587Sitojun#endif 99362587Sitojun /* 99462587Sitojun * Data after a fragment header is meaningless 99562587Sitojun * unless it is the first fragment, but 99662587Sitojun * we'll go to the notify label for path MTU 99762587Sitojun * discovery. 99862587Sitojun */ 99962587Sitojun if (fh->ip6f_offlg & IP6F_OFF_MASK) 100062587Sitojun goto notify; 100162587Sitojun 100262587Sitojun eoff += sizeof(struct ip6_frag); 100362587Sitojun nxt = fh->ip6f_nxt; 100462587Sitojun break; 100553541Sshin default: 100662587Sitojun /* 100762587Sitojun * This case includes ESP and the No Next 100895023Ssuz * Header. In such cases going to the notify 100962587Sitojun * label does not have any meaning 101062587Sitojun * (i.e. ctlfunc will be NULL), but we go 101162587Sitojun * anyway since we might have to update 101262587Sitojun * path MTU information. 101362587Sitojun */ 101453541Sshin goto notify; 101553541Sshin } 101653541Sshin } 101778064Sume notify: 101862587Sitojun#ifndef PULLDOWN_TEST 101953541Sshin icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 102062587Sitojun#else 102162587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 1022120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 102362587Sitojun if (icmp6 == NULL) { 102462587Sitojun icmp6stat.icp6s_tooshort++; 1025120856Sume return (-1); 102662587Sitojun } 102762587Sitojun#endif 102878064Sume 1029120891Sume /* 1030120891Sume * retrieve parameters from the inner IPv6 header, and convert 1031120891Sume * them into sockaddr structures. 1032120891Sume * XXX: there is no guarantee that the source or destination 1033120891Sume * addresses of the inner packet are in the same scope as 1034120891Sume * the addresses of the icmp packet. But there is no other 1035120891Sume * way to determine the zone. 1036120891Sume */ 103778064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 1038120891Sume 103978064Sume bzero(&icmp6dst, sizeof(icmp6dst)); 104078064Sume icmp6dst.sin6_len = sizeof(struct sockaddr_in6); 104178064Sume icmp6dst.sin6_family = AF_INET6; 104278064Sume if (finaldst == NULL) 104378064Sume icmp6dst.sin6_addr = eip6->ip6_dst; 104478064Sume else 104578064Sume icmp6dst.sin6_addr = *finaldst; 1046148385Sume if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL)) 1047121315Sume goto freeit; 104878064Sume bzero(&icmp6src, sizeof(icmp6src)); 104978064Sume icmp6src.sin6_len = sizeof(struct sockaddr_in6); 105078064Sume icmp6src.sin6_family = AF_INET6; 105178064Sume icmp6src.sin6_addr = eip6->ip6_src; 1052148385Sume if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL)) 1053121315Sume goto freeit; 1054148385Sume icmp6src.sin6_flowinfo = 1055148385Sume (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); 105678064Sume 105778064Sume if (finaldst == NULL) 105878064Sume finaldst = &eip6->ip6_dst; 105978064Sume ip6cp.ip6c_m = m; 106078064Sume ip6cp.ip6c_icmp6 = icmp6; 106178064Sume ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1); 106278064Sume ip6cp.ip6c_off = eoff; 106378064Sume ip6cp.ip6c_finaldst = finaldst; 106478064Sume ip6cp.ip6c_src = &icmp6src; 106578064Sume ip6cp.ip6c_nxt = nxt; 106678064Sume 106762587Sitojun if (icmp6type == ICMP6_PACKET_TOO_BIG) { 106878064Sume notifymtu = ntohl(icmp6->icmp6_mtu); 106978064Sume ip6cp.ip6c_cmdarg = (void *)¬ifymtu; 107078064Sume icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ 107162587Sitojun } 107262587Sitojun 107353541Sshin ctlfunc = (void (*) __P((int, struct sockaddr *, void *))) 1074120891Sume (inet6sw[ip6_protox[nxt]].pr_ctlinput); 107553541Sshin if (ctlfunc) { 107678064Sume (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, 1077120891Sume &ip6cp); 107853541Sshin } 107953541Sshin } 1080142681Sume *mp = m; 1081120856Sume return (0); 108253541Sshin 108378064Sume freeit: 108453541Sshin m_freem(m); 1085120856Sume return (-1); 108653541Sshin} 108753541Sshin 108878064Sumevoid 1089171259Sdelphijicmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated) 109062587Sitojun{ 109178064Sume struct in6_addr *dst = ip6cp->ip6c_finaldst; 109278064Sume struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; 109378064Sume struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ 109462587Sitojun u_int mtu = ntohl(icmp6->icmp6_mtu); 1095122922Sandre struct in_conninfo inc; 109662587Sitojun 1097121472Sume#if 0 1098121472Sume /* 1099121472Sume * RFC2460 section 5, last paragraph. 1100121472Sume * even though minimum link MTU for IPv6 is IPV6_MMTU, 1101121472Sume * we may see ICMPv6 too big with mtu < IPV6_MMTU 1102121472Sume * due to packet translator in the middle. 1103121472Sume * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for 1104121472Sume * special handling. 1105121472Sume */ 1106121472Sume if (mtu < IPV6_MMTU) 1107121472Sume return; 1108121472Sume#endif 1109121472Sume 1110121472Sume /* 1111121472Sume * we reject ICMPv6 too big with abnormally small value. 1112121472Sume * XXX what is the good definition of "abnormally small"? 1113121472Sume */ 1114121472Sume if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) 1115121472Sume return; 1116121472Sume 111778064Sume if (!validated) 111878064Sume return; 111978064Sume 1120122922Sandre bzero(&inc, sizeof(inc)); 1121122922Sandre inc.inc_flags = 1; /* IPv6 */ 1122122922Sandre inc.inc6_faddr = *dst; 1123148385Sume if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL)) 1124148385Sume return; 112562587Sitojun 1126162084Sandre if (mtu < tcp_maxmtu6(&inc, NULL)) { 1127122922Sandre tcp_hc_updatemtu(&inc, mtu); 1128122922Sandre icmp6stat.icp6s_pmtuchg++; 112962587Sitojun } 113062587Sitojun} 113162587Sitojun 113253541Sshin/* 113378064Sume * Process a Node Information Query packet, based on 113478064Sume * draft-ietf-ipngwg-icmp-name-lookups-07. 1135120891Sume * 113662587Sitojun * Spec incompatibilities: 113762587Sitojun * - IPv6 Subject address handling 113862587Sitojun * - IPv4 Subject address handling support missing 113962587Sitojun * - Proxy reply (answer even if it's not for me) 114062587Sitojun * - joins NI group address at in6_ifattach() time only, does not cope 114162587Sitojun * with hostname changes by sethostname(3) 114253541Sshin */ 114362587Sitojun#define hostnamelen strlen(hostname) 114453541Sshinstatic struct mbuf * 1145171259Sdelphijni6_input(struct mbuf *m, int off) 114653541Sshin{ 114762587Sitojun struct icmp6_nodeinfo *ni6, *nni6; 114853541Sshin struct mbuf *n = NULL; 114962587Sitojun u_int16_t qtype; 115062587Sitojun int subjlen; 115153541Sshin int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 115253541Sshin struct ni_reply_fqdn *fqdn; 115353541Sshin int addrs; /* for NI_QTYPE_NODEADDR */ 115453541Sshin struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ 1155148385Sume struct in6_addr in6_subj; /* subject address */ 115662587Sitojun struct ip6_hdr *ip6; 115762587Sitojun int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ 115878064Sume char *subj = NULL; 115978064Sume struct in6_ifaddr *ia6 = NULL; 116053541Sshin 116162587Sitojun ip6 = mtod(m, struct ip6_hdr *); 116262587Sitojun#ifndef PULLDOWN_TEST 116362587Sitojun ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); 116462587Sitojun#else 116562587Sitojun IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); 116662587Sitojun if (ni6 == NULL) { 116762587Sitojun /* m is already reclaimed */ 1168120891Sume return (NULL); 116962587Sitojun } 117062587Sitojun#endif 117153541Sshin 117262587Sitojun /* 1173169664Sjinmei * Validate IPv6 source address. 1174169664Sjinmei * The default configuration MUST be to refuse answering queries from 1175169664Sjinmei * global-scope addresses according to RFC4602. 1176169664Sjinmei * Notes: 1177169664Sjinmei * - it's not very clear what "refuse" means; this implementation 1178169664Sjinmei * simply drops it. 1179169664Sjinmei * - it's not very easy to identify global-scope (unicast) addresses 1180169664Sjinmei * since there are many prefixes for them. It should be safer 1181169664Sjinmei * and in practice sufficient to check "all" but loopback and 1182169664Sjinmei * link-local (note that site-local unicast was deprecated and 1183169664Sjinmei * ULA is defined as global scope-wise) 1184169664Sjinmei */ 1185169664Sjinmei if ((icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 && 1186169664Sjinmei !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) && 1187169664Sjinmei !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) 1188169664Sjinmei goto bad; 1189169664Sjinmei 1190169664Sjinmei /* 119162587Sitojun * Validate IPv6 destination address. 119262587Sitojun * 119378064Sume * The Responder must discard the Query without further processing 119478064Sume * unless it is one of the Responder's unicast or anycast addresses, or 119578064Sume * a link-local scope multicast address which the Responder has joined. 1196169664Sjinmei * [RFC4602, Section 5.] 119762587Sitojun */ 1198121630Sume if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1199121630Sume if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) 1200121630Sume goto bad; 1201121630Sume /* else it's a link-local multicast, fine */ 1202121630Sume } else { /* unicast or anycast */ 1203121630Sume if ((ia6 = ip6_getdstifaddr(m)) == NULL) 1204121630Sume goto bad; /* XXX impossible */ 1205121630Sume 1206121630Sume if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) && 1207169664Sjinmei !(icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) { 120878064Sume nd6log((LOG_DEBUG, "ni6_input: ignore node info to " 120978064Sume "a temporary address in %s:%d", 121078064Sume __FILE__, __LINE__)); 121178064Sume goto bad; 121278064Sume } 1213121630Sume } 121462587Sitojun 121578064Sume /* validate query Subject field. */ 121662587Sitojun qtype = ntohs(ni6->ni_qtype); 121762587Sitojun subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); 121862587Sitojun switch (qtype) { 121962587Sitojun case NI_QTYPE_NOOP: 122062587Sitojun case NI_QTYPE_SUPTYPES: 122178064Sume /* 07 draft */ 122278064Sume if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) 122378064Sume break; 122478064Sume /* FALLTHROUGH */ 122562587Sitojun case NI_QTYPE_FQDN: 122662587Sitojun case NI_QTYPE_NODEADDR: 1227151475Ssuz case NI_QTYPE_IPV4ADDR: 122862587Sitojun switch (ni6->ni_code) { 122962587Sitojun case ICMP6_NI_SUBJ_IPV6: 123062587Sitojun#if ICMP6_NI_SUBJ_IPV6 != 0 123162587Sitojun case 0: 123262587Sitojun#endif 123362587Sitojun /* 123462587Sitojun * backward compatibility - try to accept 03 draft 123562587Sitojun * format, where no Subject is present. 123662587Sitojun */ 123778064Sume if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && 123878064Sume subjlen == 0) { 123962587Sitojun oldfqdn++; 124062587Sitojun break; 124162587Sitojun } 124278064Sume#if ICMP6_NI_SUBJ_IPV6 != 0 124378064Sume if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) 124478064Sume goto bad; 124578064Sume#endif 124662587Sitojun 1247121630Sume if (subjlen != sizeof(struct in6_addr)) 124862587Sitojun goto bad; 124962587Sitojun 125062587Sitojun /* 125162587Sitojun * Validate Subject address. 125262587Sitojun * 125378064Sume * Not sure what exactly "address belongs to the node" 125478064Sume * means in the spec, is it just unicast, or what? 125562587Sitojun * 125662587Sitojun * At this moment we consider Subject address as 125762587Sitojun * "belong to the node" if the Subject address equals 125862587Sitojun * to the IPv6 destination address; validation for 125962587Sitojun * IPv6 destination address should have done enough 126062587Sitojun * check for us. 126162587Sitojun * 126262587Sitojun * We do not do proxy at this moment. 126362587Sitojun */ 126462587Sitojun /* m_pulldown instead of copy? */ 126562587Sitojun m_copydata(m, off + sizeof(struct icmp6_nodeinfo), 1266148385Sume subjlen, (caddr_t)&in6_subj); 1267148385Sume if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL)) 1268121315Sume goto bad; 1269121630Sume 1270148385Sume subj = (char *)&in6_subj; 1271148385Sume if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj)) 127262587Sitojun break; 127378064Sume 127462587Sitojun /* 127562587Sitojun * XXX if we are to allow other cases, we should really 127662587Sitojun * be careful about scope here. 127762587Sitojun * basically, we should disallow queries toward IPv6 1278120891Sume * destination X with subject Y, 1279120891Sume * if scope(X) > scope(Y). 128062587Sitojun * if we allow scope(X) > scope(Y), it will result in 128162587Sitojun * information leakage across scope boundary. 128262587Sitojun */ 128362587Sitojun goto bad; 128462587Sitojun 128562587Sitojun case ICMP6_NI_SUBJ_FQDN: 128662587Sitojun /* 128762587Sitojun * Validate Subject name with gethostname(3). 128862587Sitojun * 128962587Sitojun * The behavior may need some debate, since: 129062587Sitojun * - we are not sure if the node has FQDN as 129162587Sitojun * hostname (returned by gethostname(3)). 129262587Sitojun * - the code does wildcard match for truncated names. 129362587Sitojun * however, we are not sure if we want to perform 129462587Sitojun * wildcard match, if gethostname(3) side has 129562587Sitojun * truncated hostname. 129662587Sitojun */ 129762587Sitojun n = ni6_nametodns(hostname, hostnamelen, 0); 129862587Sitojun if (!n || n->m_next || n->m_len == 0) 129962587Sitojun goto bad; 130062587Sitojun IP6_EXTHDR_GET(subj, char *, m, 130162587Sitojun off + sizeof(struct icmp6_nodeinfo), subjlen); 130262587Sitojun if (subj == NULL) 130362587Sitojun goto bad; 130462587Sitojun if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 1305120891Sume n->m_len)) { 130662587Sitojun goto bad; 130762587Sitojun } 130862587Sitojun m_freem(n); 130962587Sitojun n = NULL; 131062587Sitojun break; 131162587Sitojun 131278064Sume case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 131362587Sitojun default: 131462587Sitojun goto bad; 131562587Sitojun } 131662587Sitojun break; 131778064Sume } 131862587Sitojun 131978064Sume /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 132078064Sume switch (qtype) { 132178064Sume case NI_QTYPE_FQDN: 1322169664Sjinmei if ((icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0) 132378064Sume goto bad; 132478064Sume break; 132578064Sume case NI_QTYPE_NODEADDR: 1326151475Ssuz case NI_QTYPE_IPV4ADDR: 1327169664Sjinmei if ((icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0) 132878064Sume goto bad; 132978064Sume break; 133078064Sume } 133178064Sume 133278064Sume /* guess reply length */ 133378064Sume switch (qtype) { 133478064Sume case NI_QTYPE_NOOP: 133578064Sume break; /* no reply data */ 133678064Sume case NI_QTYPE_SUPTYPES: 133778064Sume replylen += sizeof(u_int32_t); 133878064Sume break; 133978064Sume case NI_QTYPE_FQDN: 134078064Sume /* XXX will append an mbuf */ 134178064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 134278064Sume break; 134378064Sume case NI_QTYPE_NODEADDR: 1344148892Sume addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj); 134578064Sume if ((replylen += addrs * (sizeof(struct in6_addr) + 1346120891Sume sizeof(u_int32_t))) > MCLBYTES) 134778064Sume replylen = MCLBYTES; /* XXX: will truncate pkt later */ 134878064Sume break; 1349151475Ssuz case NI_QTYPE_IPV4ADDR: 1350151475Ssuz /* unsupported - should respond with unknown Qtype? */ 1351151475Ssuz break; 135262587Sitojun default: 135378064Sume /* 135478064Sume * XXX: We must return a reply with the ICMP6 code 1355120891Sume * `unknown Qtype' in this case. However we regard the case 135678064Sume * as an FQDN query for backward compatibility. 135778064Sume * Older versions set a random value to this field, 135878064Sume * so it rarely varies in the defined qtypes. 135978064Sume * But the mechanism is not reliable... 136078064Sume * maybe we should obsolete older versions. 136178064Sume */ 136278064Sume qtype = NI_QTYPE_FQDN; 136378064Sume /* XXX will append an mbuf */ 136478064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 136578064Sume oldfqdn++; 136678064Sume break; 136762587Sitojun } 136862587Sitojun 136978064Sume /* allocate an mbuf to reply. */ 1370111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 137162587Sitojun if (n == NULL) { 137262587Sitojun m_freem(m); 1373120856Sume return (NULL); 137462587Sitojun } 1375108466Ssam M_MOVE_PKTHDR(n, m); /* just for recvif */ 137653541Sshin if (replylen > MHLEN) { 137762587Sitojun if (replylen > MCLBYTES) { 137878064Sume /* 137978064Sume * XXX: should we try to allocate more? But MCLBYTES 138078064Sume * is probably much larger than IPV6_MMTU... 138178064Sume */ 138253541Sshin goto bad; 138362587Sitojun } 1384111119Simp MCLGET(n, M_DONTWAIT); 138553541Sshin if ((n->m_flags & M_EXT) == 0) { 138653541Sshin goto bad; 138753541Sshin } 138853541Sshin } 138953541Sshin n->m_pkthdr.len = n->m_len = replylen; 139053541Sshin 139153541Sshin /* copy mbuf header and IPv6 + Node Information base headers */ 139253541Sshin bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); 139353541Sshin nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 139462587Sitojun bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); 139553541Sshin 139653541Sshin /* qtype dependent procedure */ 139753541Sshin switch (qtype) { 139862587Sitojun case NI_QTYPE_NOOP: 139978064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 140062587Sitojun nni6->ni_flags = 0; 140162587Sitojun break; 140262587Sitojun case NI_QTYPE_SUPTYPES: 140378064Sume { 140478064Sume u_int32_t v; 140578064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 140678064Sume nni6->ni_flags = htons(0x0000); /* raw bitmap */ 140778064Sume /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 140878064Sume v = (u_int32_t)htonl(0x0000000f); 140978064Sume bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 141062587Sitojun break; 141178064Sume } 141262587Sitojun case NI_QTYPE_FQDN: 141378064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 141462587Sitojun fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + 1415120891Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo)); 141662587Sitojun nni6->ni_flags = 0; /* XXX: meaningless TTL */ 141762587Sitojun fqdn->ni_fqdn_ttl = 0; /* ditto. */ 141862587Sitojun /* 141962587Sitojun * XXX do we really have FQDN in variable "hostname"? 142062587Sitojun */ 142162587Sitojun n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn); 142262587Sitojun if (n->m_next == NULL) 142362587Sitojun goto bad; 142462587Sitojun /* XXX we assume that n->m_next is not a chain */ 142562587Sitojun if (n->m_next->m_next != NULL) 142662587Sitojun goto bad; 142762587Sitojun n->m_pkthdr.len += n->m_next->m_len; 142862587Sitojun break; 142962587Sitojun case NI_QTYPE_NODEADDR: 143062587Sitojun { 143162587Sitojun int lenlim, copied; 143253541Sshin 143378064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 143478064Sume n->m_pkthdr.len = n->m_len = 143578064Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 143678064Sume lenlim = M_TRAILINGSPACE(n); 143762587Sitojun copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 143862587Sitojun /* XXX: reset mbuf length */ 143962587Sitojun n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 1440120891Sume sizeof(struct icmp6_nodeinfo) + copied; 144162587Sitojun break; 144253541Sshin } 144362587Sitojun default: 144462587Sitojun break; /* XXX impossible! */ 144562587Sitojun } 144653541Sshin 144753541Sshin nni6->ni_type = ICMP6_NI_REPLY; 144862587Sitojun m_freem(m); 1449120856Sume return (n); 145053541Sshin 145153541Sshin bad: 145262587Sitojun m_freem(m); 145353541Sshin if (n) 145453541Sshin m_freem(n); 1455120856Sume return (NULL); 145653541Sshin} 145753541Sshin#undef hostnamelen 145853541Sshin 145953541Sshin/* 146062587Sitojun * make a mbuf with DNS-encoded string. no compression support. 146162587Sitojun * 146262587Sitojun * XXX names with less than 2 dots (like "foo" or "foo.section") will be 146362587Sitojun * treated as truncated name (two \0 at the end). this is a wild guess. 1464171259Sdelphij * 1465171259Sdelphij * old - return pascal string if non-zero 146662587Sitojun */ 146762587Sitojunstatic struct mbuf * 1468171259Sdelphijni6_nametodns(const char *name, int namelen, int old) 146962587Sitojun{ 147062587Sitojun struct mbuf *m; 147162587Sitojun char *cp, *ep; 147262587Sitojun const char *p, *q; 147362587Sitojun int i, len, nterm; 147462587Sitojun 147562587Sitojun if (old) 147662587Sitojun len = namelen + 1; 147762587Sitojun else 147862587Sitojun len = MCLBYTES; 147962587Sitojun 148062587Sitojun /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ 1481111119Simp MGET(m, M_DONTWAIT, MT_DATA); 148262587Sitojun if (m && len > MLEN) { 1483111119Simp MCLGET(m, M_DONTWAIT); 148462587Sitojun if ((m->m_flags & M_EXT) == 0) 148562587Sitojun goto fail; 148662587Sitojun } 148762587Sitojun if (!m) 148862587Sitojun goto fail; 148962587Sitojun m->m_next = NULL; 149062587Sitojun 149162587Sitojun if (old) { 149262587Sitojun m->m_len = len; 149362587Sitojun *mtod(m, char *) = namelen; 149462587Sitojun bcopy(name, mtod(m, char *) + 1, namelen); 149562587Sitojun return m; 149662587Sitojun } else { 149762587Sitojun m->m_len = 0; 149862587Sitojun cp = mtod(m, char *); 149962587Sitojun ep = mtod(m, char *) + M_TRAILINGSPACE(m); 150062587Sitojun 150162587Sitojun /* if not certain about my name, return empty buffer */ 150262587Sitojun if (namelen == 0) 150362587Sitojun return m; 150462587Sitojun 150562587Sitojun /* 150662587Sitojun * guess if it looks like shortened hostname, or FQDN. 150762587Sitojun * shortened hostname needs two trailing "\0". 150862587Sitojun */ 150962587Sitojun i = 0; 151062587Sitojun for (p = name; p < name + namelen; p++) { 151162587Sitojun if (*p && *p == '.') 151262587Sitojun i++; 151362587Sitojun } 151462587Sitojun if (i < 2) 151562587Sitojun nterm = 2; 151662587Sitojun else 151762587Sitojun nterm = 1; 151862587Sitojun 151962587Sitojun p = name; 152062587Sitojun while (cp < ep && p < name + namelen) { 152162587Sitojun i = 0; 152262587Sitojun for (q = p; q < name + namelen && *q && *q != '.'; q++) 152362587Sitojun i++; 152462587Sitojun /* result does not fit into mbuf */ 152562587Sitojun if (cp + i + 1 >= ep) 152662587Sitojun goto fail; 152778704Sume /* 152878704Sume * DNS label length restriction, RFC1035 page 8. 152978704Sume * "i == 0" case is included here to avoid returning 153078704Sume * 0-length label on "foo..bar". 153178704Sume */ 153278704Sume if (i <= 0 || i >= 64) 153362587Sitojun goto fail; 153462587Sitojun *cp++ = i; 153562587Sitojun bcopy(p, cp, i); 153662587Sitojun cp += i; 153762587Sitojun p = q; 153862587Sitojun if (p < name + namelen && *p == '.') 153962587Sitojun p++; 154062587Sitojun } 154162587Sitojun /* termination */ 154262587Sitojun if (cp + nterm >= ep) 154362587Sitojun goto fail; 154462587Sitojun while (nterm-- > 0) 154562587Sitojun *cp++ = '\0'; 154662587Sitojun m->m_len = cp - mtod(m, char *); 154762587Sitojun return m; 154862587Sitojun } 154962587Sitojun 155062587Sitojun panic("should not reach here"); 155195023Ssuz /* NOTREACHED */ 155262587Sitojun 155362587Sitojun fail: 155462587Sitojun if (m) 155562587Sitojun m_freem(m); 155662587Sitojun return NULL; 155762587Sitojun} 155862587Sitojun 155962587Sitojun/* 156062587Sitojun * check if two DNS-encoded string matches. takes care of truncated 156162587Sitojun * form (with \0\0 at the end). no compression support. 156278064Sume * XXX upper/lowercase match (see RFC2065) 156362587Sitojun */ 156462587Sitojunstatic int 1565171259Sdelphijni6_dnsmatch(const char *a, int alen, const char *b, int blen) 156662587Sitojun{ 156762587Sitojun const char *a0, *b0; 156862587Sitojun int l; 156962587Sitojun 157062587Sitojun /* simplest case - need validation? */ 157162587Sitojun if (alen == blen && bcmp(a, b, alen) == 0) 157262587Sitojun return 1; 157362587Sitojun 157462587Sitojun a0 = a; 157562587Sitojun b0 = b; 157662587Sitojun 157762587Sitojun /* termination is mandatory */ 157862587Sitojun if (alen < 2 || blen < 2) 157962587Sitojun return 0; 158062587Sitojun if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 158162587Sitojun return 0; 158262587Sitojun alen--; 158362587Sitojun blen--; 158462587Sitojun 158562587Sitojun while (a - a0 < alen && b - b0 < blen) { 158662587Sitojun if (a - a0 + 1 > alen || b - b0 + 1 > blen) 158762587Sitojun return 0; 158862587Sitojun 158962587Sitojun if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 159062587Sitojun return 0; 159162587Sitojun /* we don't support compression yet */ 159262587Sitojun if (a[0] >= 64 || b[0] >= 64) 159362587Sitojun return 0; 159462587Sitojun 159562587Sitojun /* truncated case */ 159662587Sitojun if (a[0] == 0 && a - a0 == alen - 1) 159762587Sitojun return 1; 159862587Sitojun if (b[0] == 0 && b - b0 == blen - 1) 159962587Sitojun return 1; 160062587Sitojun if (a[0] == 0 || b[0] == 0) 160162587Sitojun return 0; 160262587Sitojun 160362587Sitojun if (a[0] != b[0]) 160462587Sitojun return 0; 160562587Sitojun l = a[0]; 160662587Sitojun if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 160762587Sitojun return 0; 160862587Sitojun if (bcmp(a + 1, b + 1, l) != 0) 160962587Sitojun return 0; 161062587Sitojun 161162587Sitojun a += 1 + l; 161262587Sitojun b += 1 + l; 161362587Sitojun } 161462587Sitojun 161562587Sitojun if (a - a0 == alen && b - b0 == blen) 161662587Sitojun return 1; 161762587Sitojun else 161862587Sitojun return 0; 161962587Sitojun} 162062587Sitojun 162162587Sitojun/* 162253541Sshin * calculate the number of addresses to be returned in the node info reply. 162353541Sshin */ 162453541Sshinstatic int 1625171259Sdelphijni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp, 1626171259Sdelphij struct in6_addr *subj) 162753541Sshin{ 162878064Sume struct ifnet *ifp; 162978064Sume struct in6_ifaddr *ifa6; 163078064Sume struct ifaddr *ifa; 163153541Sshin int addrs = 0, addrsofif, iffound = 0; 163278064Sume int niflags = ni6->ni_flags; 163353541Sshin 163478064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 163578064Sume switch (ni6->ni_code) { 163678064Sume case ICMP6_NI_SUBJ_IPV6: 163778064Sume if (subj == NULL) /* must be impossible... */ 1638120856Sume return (0); 163978064Sume break; 164078064Sume default: 164178064Sume /* 164278064Sume * XXX: we only support IPv6 subject address for 164378064Sume * this Qtype. 164478064Sume */ 1645120856Sume return (0); 164678064Sume } 164778064Sume } 164878064Sume 1649108172Shsu IFNET_RLOCK(); 1650120891Sume for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 165153541Sshin addrsofif = 0; 1652120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 165353541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 165453541Sshin continue; 165553541Sshin ifa6 = (struct in6_ifaddr *)ifa; 165653541Sshin 165778064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 1658148892Sume IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr)) 165953541Sshin iffound = 1; 166053541Sshin 166162587Sitojun /* 166262587Sitojun * IPv4-mapped addresses can only be returned by a 166362587Sitojun * Node Information proxy, since they represent 166462587Sitojun * addresses of IPv4-only nodes, which perforce do 166562587Sitojun * not implement this protocol. 166678064Sume * [icmp-name-lookups-07, Section 5.4] 166762587Sitojun * So we don't support NI_NODEADDR_FLAG_COMPAT in 166862587Sitojun * this function at this moment. 166962587Sitojun */ 167062587Sitojun 167153541Sshin /* What do we have to do about ::1? */ 167278064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 167378064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 167478064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 167578064Sume continue; 167653541Sshin break; 167778064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 167878064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 167978064Sume continue; 168053541Sshin break; 168178064Sume case IPV6_ADDR_SCOPE_GLOBAL: 168278064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 168378064Sume continue; 168478064Sume break; 168578064Sume default: 168678064Sume continue; 168753541Sshin } 168878064Sume 168978064Sume /* 169078064Sume * check if anycast is okay. 169195023Ssuz * XXX: just experimental. not in the spec. 169278064Sume */ 169378064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 169478064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 169578064Sume continue; /* we need only unicast addresses */ 169678064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1697169664Sjinmei (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { 169878064Sume continue; 169978064Sume } 170078064Sume addrsofif++; /* count the address */ 170153541Sshin } 170253541Sshin if (iffound) { 170353541Sshin *ifpp = ifp; 1704108172Shsu IFNET_RUNLOCK(); 1705120856Sume return (addrsofif); 170653541Sshin } 170753541Sshin 170853541Sshin addrs += addrsofif; 170953541Sshin } 1710108172Shsu IFNET_RUNLOCK(); 171153541Sshin 1712120856Sume return (addrs); 171353541Sshin} 171453541Sshin 171553541Sshinstatic int 1716171259Sdelphijni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6, 1717171259Sdelphij struct ifnet *ifp0, int resid) 171853541Sshin{ 171978064Sume struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet); 172078064Sume struct in6_ifaddr *ifa6; 172178064Sume struct ifaddr *ifa; 172278064Sume struct ifnet *ifp_dep = NULL; 172378064Sume int copied = 0, allow_deprecated = 0; 172453541Sshin u_char *cp = (u_char *)(nni6 + 1); 172578064Sume int niflags = ni6->ni_flags; 172678064Sume u_int32_t ltime; 172753541Sshin 172878064Sume if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 1729120856Sume return (0); /* needless to copy */ 1730120891Sume 1731108172Shsu IFNET_RLOCK(); 173278064Sume again: 1733120892Sume 1734120891Sume for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 173562587Sitojun for (ifa = ifp->if_addrlist.tqh_first; ifa; 1736120891Sume ifa = ifa->ifa_list.tqe_next) { 173753541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 173853541Sshin continue; 173953541Sshin ifa6 = (struct in6_ifaddr *)ifa; 174053541Sshin 174178064Sume if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && 174278064Sume allow_deprecated == 0) { 174378064Sume /* 174478064Sume * prefererred address should be put before 174578064Sume * deprecated addresses. 174678064Sume */ 174778064Sume 174878064Sume /* record the interface for later search */ 174978064Sume if (ifp_dep == NULL) 175078064Sume ifp_dep = ifp; 175178064Sume 175278064Sume continue; 1753120891Sume } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && 1754120891Sume allow_deprecated != 0) 175578064Sume continue; /* we now collect deprecated addrs */ 175653541Sshin 175753541Sshin /* What do we have to do about ::1? */ 175878064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 175978064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 176078064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 176178064Sume continue; 176253541Sshin break; 176378064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 176478064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 176578064Sume continue; 176653541Sshin break; 176778064Sume case IPV6_ADDR_SCOPE_GLOBAL: 176878064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 176978064Sume continue; 177078064Sume break; 177178064Sume default: 177278064Sume continue; 177353541Sshin } 177453541Sshin 177578064Sume /* 177678064Sume * check if anycast is okay. 1777120891Sume * XXX: just experimental. not in the spec. 177878064Sume */ 177978064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 178078064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 178178064Sume continue; 178278064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1783169664Sjinmei (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { 178478064Sume continue; 178553541Sshin } 178678064Sume 178778064Sume /* now we can copy the address */ 178878064Sume if (resid < sizeof(struct in6_addr) + 178978064Sume sizeof(u_int32_t)) { 179078064Sume /* 179178064Sume * We give up much more copy. 179278064Sume * Set the truncate flag and return. 179378064Sume */ 1794120891Sume nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE; 1795108172Shsu IFNET_RUNLOCK(); 1796120856Sume return (copied); 179778064Sume } 179878064Sume 179978064Sume /* 180078064Sume * Set the TTL of the address. 180178064Sume * The TTL value should be one of the following 180278064Sume * according to the specification: 180378064Sume * 180478064Sume * 1. The remaining lifetime of a DHCP lease on the 180578064Sume * address, or 180678064Sume * 2. The remaining Valid Lifetime of a prefix from 180778064Sume * which the address was derived through Stateless 180878064Sume * Autoconfiguration. 180978064Sume * 181078064Sume * Note that we currently do not support stateful 181178064Sume * address configuration by DHCPv6, so the former 181278064Sume * case can't happen. 181378064Sume */ 181478064Sume if (ifa6->ia6_lifetime.ia6t_expire == 0) 181578064Sume ltime = ND6_INFINITE_LIFETIME; 181678064Sume else { 181778064Sume if (ifa6->ia6_lifetime.ia6t_expire > 181878064Sume time_second) 181978064Sume ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second); 182078064Sume else 182178064Sume ltime = 0; 182278064Sume } 1823120891Sume 182478064Sume bcopy(<ime, cp, sizeof(u_int32_t)); 182578064Sume cp += sizeof(u_int32_t); 182678064Sume 182778064Sume /* copy the address itself */ 182878064Sume bcopy(&ifa6->ia_addr.sin6_addr, cp, 1829120891Sume sizeof(struct in6_addr)); 1830121315Sume in6_clearscope((struct in6_addr *)cp); /* XXX */ 183178064Sume cp += sizeof(struct in6_addr); 1832120891Sume 183378064Sume resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1834120891Sume copied += (sizeof(struct in6_addr) + sizeof(u_int32_t)); 183553541Sshin } 183653541Sshin if (ifp0) /* we need search only on the specified IF */ 183753541Sshin break; 183853541Sshin } 183953541Sshin 184078064Sume if (allow_deprecated == 0 && ifp_dep != NULL) { 184178064Sume ifp = ifp_dep; 184278064Sume allow_deprecated = 1; 184378064Sume 184478064Sume goto again; 184578064Sume } 184678064Sume 1847108172Shsu IFNET_RUNLOCK(); 1848108172Shsu 1849120856Sume return (copied); 185053541Sshin} 185153541Sshin 185253541Sshin/* 185353541Sshin * XXX almost dup'ed code with rip6_input. 185453541Sshin */ 185553541Sshinstatic int 1856171259Sdelphijicmp6_rip6_input(struct mbuf **mp, int off) 185753541Sshin{ 185853541Sshin struct mbuf *m = *mp; 185978064Sume struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 186078064Sume struct in6pcb *in6p; 186153541Sshin struct in6pcb *last = NULL; 1862121315Sume struct sockaddr_in6 fromsa; 186353541Sshin struct icmp6_hdr *icmp6; 186453541Sshin struct mbuf *opts = NULL; 186553541Sshin 186662587Sitojun#ifndef PULLDOWN_TEST 186753541Sshin /* this is assumed to be safe. */ 186853541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 186962587Sitojun#else 187062587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 187162587Sitojun if (icmp6 == NULL) { 187262587Sitojun /* m is already reclaimed */ 1873120891Sume return (IPPROTO_DONE); 187462587Sitojun } 187562587Sitojun#endif 187653541Sshin 1877148385Sume /* 1878148385Sume * XXX: the address may have embedded scope zone ID, which should be 1879148385Sume * hidden from applications. 1880148385Sume */ 1881121315Sume bzero(&fromsa, sizeof(fromsa)); 1882148385Sume fromsa.sin6_family = AF_INET6; 1883121315Sume fromsa.sin6_len = sizeof(struct sockaddr_in6); 1884148385Sume fromsa.sin6_addr = ip6->ip6_src; 1885148385Sume if (sa6_recoverscope(&fromsa)) { 1886148385Sume m_freem(m); 1887148385Sume return (IPPROTO_DONE); 1888148385Sume } 188953541Sshin 1890141545Srwatson INP_INFO_RLOCK(&ripcbinfo); 1891120891Sume LIST_FOREACH(in6p, &ripcb, inp_list) { 1892141545Srwatson INP_LOCK(in6p); 1893141545Srwatson if ((in6p->inp_vflag & INP_IPV6) == 0) { 1894141545Srwatson docontinue: 1895141545Srwatson INP_UNLOCK(in6p); 189653541Sshin continue; 1897141545Srwatson } 189853541Sshin if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6) 1899141545Srwatson goto docontinue; 190053541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 190153541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 1902141545Srwatson goto docontinue; 190353541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 190453541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 1905141545Srwatson goto docontinue; 190653541Sshin if (in6p->in6p_icmp6filt 190753541Sshin && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, 190853541Sshin in6p->in6p_icmp6filt)) 1909141545Srwatson goto docontinue; 191053541Sshin if (last) { 1911121809Sume struct mbuf *n = NULL; 1912121809Sume 1913121809Sume /* 1914121809Sume * Recent network drivers tend to allocate a single 1915121809Sume * mbuf cluster, rather than to make a couple of 1916121809Sume * mbufs without clusters. Also, since the IPv6 code 1917121809Sume * path tries to avoid m_pullup(), it is highly 1918121809Sume * probable that we still have an mbuf cluster here 1919121809Sume * even though the necessary length can be stored in an 1920121809Sume * mbuf's internal buffer. 1921121809Sume * Meanwhile, the default size of the receive socket 1922121809Sume * buffer for raw sockets is not so large. This means 1923121809Sume * the possibility of packet loss is relatively higher 1924121809Sume * than before. To avoid this scenario, we copy the 1925121809Sume * received data to a separate mbuf that does not use 1926121809Sume * a cluster, if possible. 1927121809Sume * XXX: it is better to copy the data after stripping 1928121809Sume * intermediate headers. 1929121809Sume */ 1930121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 1931121809Sume m->m_len <= MHLEN) { 1932121809Sume MGET(n, M_DONTWAIT, m->m_type); 1933121809Sume if (n != NULL) { 1934145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 1935171260Sdelphij bcopy(m->m_data, n->m_data, 1936145065Sgnn m->m_len); 1937145065Sgnn n->m_len = m->m_len; 1938145065Sgnn } else { 1939145065Sgnn m_free(n); 1940145065Sgnn n = NULL; 1941145065Sgnn } 1942121809Sume } 1943121809Sume } 1944121809Sume if (n != NULL || 1945121809Sume (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 194653541Sshin if (last->in6p_flags & IN6P_CONTROLOPTS) 1947121674Sume ip6_savecontrol(last, n, &opts); 194853541Sshin /* strip intermediate headers */ 194953541Sshin m_adj(n, off); 1950160591Srwatson SOCKBUF_LOCK(&last->in6p_socket->so_rcv); 1951160591Srwatson if (sbappendaddr_locked( 1952160591Srwatson &last->in6p_socket->so_rcv, 1953121315Sume (struct sockaddr *)&fromsa, n, opts) 1954120891Sume == 0) { 195553541Sshin /* should notify about lost packet */ 195653541Sshin m_freem(n); 195778064Sume if (opts) { 195853541Sshin m_freem(opts); 195978064Sume } 1960160591Srwatson SOCKBUF_UNLOCK( 1961160591Srwatson &last->in6p_socket->so_rcv); 196297658Stanimura } else 1963160591Srwatson sorwakeup_locked(last->in6p_socket); 196453541Sshin opts = NULL; 196553541Sshin } 1966141545Srwatson INP_UNLOCK(last); 196753541Sshin } 196853541Sshin last = in6p; 196953541Sshin } 197053541Sshin if (last) { 197153541Sshin if (last->in6p_flags & IN6P_CONTROLOPTS) 1972121674Sume ip6_savecontrol(last, m, &opts); 197353541Sshin /* strip intermediate headers */ 197453541Sshin m_adj(m, off); 1975121809Sume 1976121809Sume /* avoid using mbuf clusters if possible (see above) */ 1977121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 1978121809Sume m->m_len <= MHLEN) { 1979121809Sume struct mbuf *n; 1980121809Sume 1981121809Sume MGET(n, M_DONTWAIT, m->m_type); 1982121809Sume if (n != NULL) { 1983145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 1984145065Sgnn bcopy(m->m_data, n->m_data, m->m_len); 1985145065Sgnn n->m_len = m->m_len; 1986171260Sdelphij 1987145065Sgnn m_freem(m); 1988145065Sgnn m = n; 1989145065Sgnn } else { 1990145065Sgnn m_freem(n); 1991145065Sgnn n = NULL; 1992145065Sgnn } 1993121809Sume } 1994121809Sume } 1995160591Srwatson SOCKBUF_LOCK(&last->in6p_socket->so_rcv); 1996160591Srwatson if (sbappendaddr_locked(&last->in6p_socket->so_rcv, 1997121315Sume (struct sockaddr *)&fromsa, m, opts) == 0) { 199853541Sshin m_freem(m); 199953541Sshin if (opts) 200053541Sshin m_freem(opts); 2001160591Srwatson SOCKBUF_UNLOCK(&last->in6p_socket->so_rcv); 200297658Stanimura } else 2003160591Srwatson sorwakeup_locked(last->in6p_socket); 2004141545Srwatson INP_UNLOCK(last); 200553541Sshin } else { 200653541Sshin m_freem(m); 200753541Sshin ip6stat.ip6s_delivered--; 200853541Sshin } 2009141545Srwatson INP_INFO_RUNLOCK(&ripcbinfo); 201053541Sshin return IPPROTO_DONE; 201153541Sshin} 201253541Sshin 201353541Sshin/* 201453541Sshin * Reflect the ip6 packet back to the source. 201562587Sitojun * OFF points to the icmp6 header, counted from the top of the mbuf. 201653541Sshin */ 201753541Sshinvoid 2018171259Sdelphijicmp6_reflect(struct mbuf *m, size_t off) 201953541Sshin{ 202062587Sitojun struct ip6_hdr *ip6; 202153541Sshin struct icmp6_hdr *icmp6; 202253541Sshin struct in6_ifaddr *ia; 202362587Sitojun int plen; 202453541Sshin int type, code; 202553541Sshin struct ifnet *outif = NULL; 2026148385Sume struct in6_addr origdst, *src = NULL; 202753541Sshin 202862587Sitojun /* too short to reflect */ 202962587Sitojun if (off < sizeof(struct ip6_hdr)) { 203078064Sume nd6log((LOG_DEBUG, 203178064Sume "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 203278064Sume (u_long)off, (u_long)sizeof(struct ip6_hdr), 203378064Sume __FILE__, __LINE__)); 203462587Sitojun goto bad; 203562587Sitojun } 203662587Sitojun 203753541Sshin /* 203853541Sshin * If there are extra headers between IPv6 and ICMPv6, strip 203953541Sshin * off that header first. 204053541Sshin */ 204162587Sitojun#ifdef DIAGNOSTIC 204262587Sitojun if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) 204362587Sitojun panic("assumption failed in icmp6_reflect"); 204462587Sitojun#endif 204562587Sitojun if (off > sizeof(struct ip6_hdr)) { 204662587Sitojun size_t l; 204762587Sitojun struct ip6_hdr nip6; 204853541Sshin 204962587Sitojun l = off - sizeof(struct ip6_hdr); 205062587Sitojun m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); 205162587Sitojun m_adj(m, l); 205262587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 205362587Sitojun if (m->m_len < l) { 205462587Sitojun if ((m = m_pullup(m, l)) == NULL) 205562587Sitojun return; 205653541Sshin } 205762587Sitojun bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); 205862587Sitojun } else /* off == sizeof(struct ip6_hdr) */ { 205962587Sitojun size_t l; 206062587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 206162587Sitojun if (m->m_len < l) { 206262587Sitojun if ((m = m_pullup(m, l)) == NULL) 206362587Sitojun return; 206453541Sshin } 206553541Sshin } 206662587Sitojun plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 206762587Sitojun ip6 = mtod(m, struct ip6_hdr *); 206862587Sitojun ip6->ip6_nxt = IPPROTO_ICMPV6; 206953541Sshin icmp6 = (struct icmp6_hdr *)(ip6 + 1); 207053541Sshin type = icmp6->icmp6_type; /* keep type for statistics */ 207153541Sshin code = icmp6->icmp6_code; /* ditto. */ 207253541Sshin 2073148385Sume origdst = ip6->ip6_dst; 207453541Sshin /* 207553541Sshin * ip6_input() drops a packet if its src is multicast. 207653541Sshin * So, the src is never multicast. 207753541Sshin */ 207853541Sshin ip6->ip6_dst = ip6->ip6_src; 207953541Sshin 208053541Sshin /* 2081120891Sume * If the incoming packet was addressed directly to us (i.e. unicast), 208253541Sshin * use dst as the src for the reply. 2083120891Sume * The IN6_IFF_NOTREADY case should be VERY rare, but is possible 208462587Sitojun * (for example) when we encounter an error while forwarding procedure 208562587Sitojun * destined to a duplicated address of ours. 2086148385Sume * Note that ip6_getdstifaddr() may fail if we are in an error handling 2087148385Sume * procedure of an outgoing packet of our own, in which case we need 2088148385Sume * to search in the ifaddr list. 208953541Sshin */ 2090148385Sume if (!IN6_IS_ADDR_MULTICAST(&origdst)) { 2091148385Sume if ((ia = ip6_getdstifaddr(m))) { 2092148385Sume if (!(ia->ia6_flags & 2093148385Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) 2094148385Sume src = &ia->ia_addr.sin6_addr; 2095148385Sume } else { 2096148385Sume struct sockaddr_in6 d; 2097148385Sume 2098148385Sume bzero(&d, sizeof(d)); 2099148385Sume d.sin6_family = AF_INET6; 2100148385Sume d.sin6_len = sizeof(d); 2101148385Sume d.sin6_addr = origdst; 2102148385Sume ia = (struct in6_ifaddr *) 2103148385Sume ifa_ifwithaddr((struct sockaddr *)&d); 2104148385Sume if (ia && 2105148385Sume !(ia->ia6_flags & 2106148385Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) { 2107148385Sume src = &ia->ia_addr.sin6_addr; 2108148385Sume } 210953541Sshin } 211053541Sshin } 211153541Sshin 2112148385Sume if (src == NULL) { 211378064Sume int e; 2114148385Sume struct sockaddr_in6 sin6; 211578064Sume struct route_in6 ro; 211678064Sume 211753541Sshin /* 211862587Sitojun * This case matches to multicasts, our anycast, or unicasts 211995023Ssuz * that we do not own. Select a source address based on the 212078064Sume * source address of the erroneous packet. 212153541Sshin */ 2122148385Sume bzero(&sin6, sizeof(sin6)); 2123148385Sume sin6.sin6_family = AF_INET6; 2124148385Sume sin6.sin6_len = sizeof(sin6); 2125148385Sume sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */ 2126148385Sume 212778064Sume bzero(&ro, sizeof(ro)); 2128148385Sume src = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &e); 212978064Sume if (ro.ro_rt) 213078064Sume RTFREE(ro.ro_rt); /* XXX: we could use this */ 213178064Sume if (src == NULL) { 2132165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 213378064Sume nd6log((LOG_DEBUG, 213478064Sume "icmp6_reflect: source can't be determined: " 213578064Sume "dst=%s, error=%d\n", 2136165118Sbz ip6_sprintf(ip6buf, &sin6.sin6_addr), e)); 213778064Sume goto bad; 213878064Sume } 213978064Sume } 214053541Sshin 214153541Sshin ip6->ip6_src = *src; 214253541Sshin ip6->ip6_flow = 0; 214362587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 214462587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 214553541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 2146121472Sume if (outif) 2147121472Sume ip6->ip6_hlim = ND_IFINFO(outif)->chlim; 2148121472Sume else if (m->m_pkthdr.rcvif) { 214953541Sshin /* XXX: This may not be the outgoing interface */ 2150121161Sume ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim; 215178064Sume } else 215278064Sume ip6->ip6_hlim = ip6_defhlim; 215353541Sshin 215453541Sshin icmp6->icmp6_cksum = 0; 215553541Sshin icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2156120891Sume sizeof(struct ip6_hdr), plen); 215753541Sshin 215853541Sshin /* 215978064Sume * XXX option handling 216053541Sshin */ 216153541Sshin 216253541Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 216353541Sshin 2164105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 216553541Sshin if (outif) 216653541Sshin icmp6_ifoutstat_inc(outif, type, code); 216753541Sshin 216853541Sshin return; 216953541Sshin 217053541Sshin bad: 217153541Sshin m_freem(m); 217253541Sshin return; 217353541Sshin} 217453541Sshin 217553541Sshinvoid 2176171259Sdelphijicmp6_fasttimo(void) 217753541Sshin{ 217862587Sitojun 2179151539Ssuz return; 218053541Sshin} 218153541Sshin 218253541Sshinstatic const char * 2183171259Sdelphijicmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6, 2184171259Sdelphij struct in6_addr *tgt6) 218553541Sshin{ 218653541Sshin static char buf[1024]; 2187165118Sbz char ip6bufs[INET6_ADDRSTRLEN]; 2188165118Sbz char ip6bufd[INET6_ADDRSTRLEN]; 2189165118Sbz char ip6buft[INET6_ADDRSTRLEN]; 219053541Sshin snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", 2191165118Sbz ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6), 2192165118Sbz ip6_sprintf(ip6buft, tgt6)); 219353541Sshin return buf; 219453541Sshin} 219553541Sshin 219653541Sshinvoid 2197171259Sdelphijicmp6_redirect_input(struct mbuf *m, int off) 219853541Sshin{ 2199171133Sgnn struct ifnet *ifp; 220053541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 220162587Sitojun struct nd_redirect *nd_rd; 220253541Sshin int icmp6len = ntohs(ip6->ip6_plen); 220353541Sshin char *lladdr = NULL; 220453541Sshin int lladdrlen = 0; 220553541Sshin u_char *redirhdr = NULL; 220653541Sshin int redirhdrlen = 0; 220753541Sshin struct rtentry *rt = NULL; 220853541Sshin int is_router; 220953541Sshin int is_onlink; 221053541Sshin struct in6_addr src6 = ip6->ip6_src; 221162587Sitojun struct in6_addr redtgt6; 221262587Sitojun struct in6_addr reddst6; 221353541Sshin union nd_opts ndopts; 2214165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 221553541Sshin 2216171133Sgnn if (!m) 221753541Sshin return; 221853541Sshin 2219171133Sgnn ifp = m->m_pkthdr.rcvif; 2220171133Sgnn 2221171133Sgnn if (!ifp) 2222171133Sgnn return; 2223171133Sgnn 222453541Sshin /* XXX if we are router, we don't update route by icmp6 redirect */ 222553541Sshin if (ip6_forwarding) 222662587Sitojun goto freeit; 222753541Sshin if (!icmp6_rediraccept) 222862587Sitojun goto freeit; 222962587Sitojun 223062587Sitojun#ifndef PULLDOWN_TEST 223162587Sitojun IP6_EXTHDR_CHECK(m, off, icmp6len,); 223262587Sitojun nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); 223362587Sitojun#else 223462587Sitojun IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); 223562587Sitojun if (nd_rd == NULL) { 223662587Sitojun icmp6stat.icp6s_tooshort++; 223753541Sshin return; 223862587Sitojun } 223962587Sitojun#endif 224062587Sitojun redtgt6 = nd_rd->nd_rd_target; 224162587Sitojun reddst6 = nd_rd->nd_rd_dst; 224253541Sshin 2243148385Sume if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) || 2244148385Sume in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) { 2245148385Sume goto freeit; 2246148385Sume } 224753541Sshin 224853541Sshin /* validation */ 224953541Sshin if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 225078064Sume nd6log((LOG_ERR, 2251120891Sume "ICMP6 redirect sent from %s rejected; " 2252120891Sume "must be from linklocal\n", 2253165118Sbz ip6_sprintf(ip6buf, &src6))); 225478064Sume goto bad; 225553541Sshin } 225653541Sshin if (ip6->ip6_hlim != 255) { 225778064Sume nd6log((LOG_ERR, 2258120891Sume "ICMP6 redirect sent from %s rejected; " 2259120891Sume "hlim=%d (must be 255)\n", 2260165118Sbz ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim)); 226178064Sume goto bad; 226253541Sshin } 226353541Sshin { 226453541Sshin /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 226553541Sshin struct sockaddr_in6 sin6; 226653541Sshin struct in6_addr *gw6; 226753541Sshin 226853541Sshin bzero(&sin6, sizeof(sin6)); 226953541Sshin sin6.sin6_family = AF_INET6; 227053541Sshin sin6.sin6_len = sizeof(struct sockaddr_in6); 227153541Sshin bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); 227253541Sshin rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 227353541Sshin if (rt) { 227465895Sume if (rt->rt_gateway == NULL || 227565895Sume rt->rt_gateway->sa_family != AF_INET6) { 227678064Sume nd6log((LOG_ERR, 227765895Sume "ICMP6 redirect rejected; no route " 227865895Sume "with inet6 gateway found for redirect dst: %s\n", 227978064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2280120727Ssam RTFREE_LOCKED(rt); 228178064Sume goto bad; 228265895Sume } 228365895Sume 228453541Sshin gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); 228553541Sshin if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { 228678064Sume nd6log((LOG_ERR, 2287120891Sume "ICMP6 redirect rejected; " 2288120891Sume "not equal to gw-for-src=%s (must be same): " 2289120891Sume "%s\n", 2290165118Sbz ip6_sprintf(ip6buf, gw6), 2291120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2292120727Ssam RTFREE_LOCKED(rt); 229378064Sume goto bad; 229453541Sshin } 229553541Sshin } else { 229678064Sume nd6log((LOG_ERR, 2297120891Sume "ICMP6 redirect rejected; " 2298120891Sume "no route found for redirect dst: %s\n", 2299120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 230078064Sume goto bad; 230153541Sshin } 2302120727Ssam RTFREE_LOCKED(rt); 230353541Sshin rt = NULL; 230453541Sshin } 230553541Sshin if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 230678064Sume nd6log((LOG_ERR, 2307120891Sume "ICMP6 redirect rejected; " 2308120891Sume "redirect dst must be unicast: %s\n", 2309120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 231078064Sume goto bad; 231153541Sshin } 231253541Sshin 231353541Sshin is_router = is_onlink = 0; 231453541Sshin if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 231553541Sshin is_router = 1; /* router case */ 231653541Sshin if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 231753541Sshin is_onlink = 1; /* on-link destination case */ 231853541Sshin if (!is_router && !is_onlink) { 231978064Sume nd6log((LOG_ERR, 2320120891Sume "ICMP6 redirect rejected; " 2321120891Sume "neither router case nor onlink case: %s\n", 2322120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 232378064Sume goto bad; 232453541Sshin } 232553541Sshin /* validation passed */ 232653541Sshin 232753541Sshin icmp6len -= sizeof(*nd_rd); 232853541Sshin nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 232953541Sshin if (nd6_options(&ndopts) < 0) { 233078064Sume nd6log((LOG_INFO, "icmp6_redirect_input: " 2331120891Sume "invalid ND option, rejected: %s\n", 2332120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 233378064Sume /* nd6_options have incremented stats */ 233462587Sitojun goto freeit; 233553541Sshin } 233653541Sshin 233753541Sshin if (ndopts.nd_opts_tgt_lladdr) { 233853541Sshin lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 233953541Sshin lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 234053541Sshin } 234153541Sshin 234253541Sshin if (ndopts.nd_opts_rh) { 234353541Sshin redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len; 234453541Sshin redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */ 234553541Sshin } 234653541Sshin 234753541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 234878064Sume nd6log((LOG_INFO, 2349120891Sume "icmp6_redirect_input: lladdrlen mismatch for %s " 2350120891Sume "(if %d, icmp6 packet %d): %s\n", 2351165118Sbz ip6_sprintf(ip6buf, &redtgt6), 2352165118Sbz ifp->if_addrlen, lladdrlen - 2, 2353120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 235478064Sume goto bad; 235553541Sshin } 235653541Sshin 235753541Sshin /* RFC 2461 8.3 */ 235853541Sshin nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 2359120891Sume is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 236053541Sshin 236195023Ssuz if (!is_onlink) { /* better router case. perform rtredirect. */ 236253541Sshin /* perform rtredirect */ 236353541Sshin struct sockaddr_in6 sdst; 236453541Sshin struct sockaddr_in6 sgw; 236553541Sshin struct sockaddr_in6 ssrc; 236653541Sshin 236753541Sshin bzero(&sdst, sizeof(sdst)); 236853541Sshin bzero(&sgw, sizeof(sgw)); 236953541Sshin bzero(&ssrc, sizeof(ssrc)); 237053541Sshin sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; 237153541Sshin sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = 237253541Sshin sizeof(struct sockaddr_in6); 237353541Sshin bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); 237453541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 237553541Sshin bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 237653541Sshin rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw, 2377120891Sume (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST, 2378120891Sume (struct sockaddr *)&ssrc); 237953541Sshin } 238053541Sshin /* finally update cached route in each socket via pfctlinput */ 238153541Sshin { 238253541Sshin struct sockaddr_in6 sdst; 238353541Sshin 238453541Sshin bzero(&sdst, sizeof(sdst)); 238553541Sshin sdst.sin6_family = AF_INET6; 238653541Sshin sdst.sin6_len = sizeof(struct sockaddr_in6); 238753541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 238853541Sshin pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); 2389171167Sgnn#ifdef IPSEC 239053541Sshin key_sa_routechange((struct sockaddr *)&sdst); 2391171167Sgnn#endif /* IPSEC */ 239253541Sshin } 239362587Sitojun 239462587Sitojun freeit: 239562587Sitojun m_freem(m); 239678064Sume return; 239778064Sume 239878064Sume bad: 239978064Sume icmp6stat.icp6s_badredirect++; 240078064Sume m_freem(m); 240153541Sshin} 240253541Sshin 240353541Sshinvoid 2404171259Sdelphijicmp6_redirect_output(struct mbuf *m0, struct rtentry *rt) 240553541Sshin{ 240653541Sshin struct ifnet *ifp; /* my outgoing interface */ 240753541Sshin struct in6_addr *ifp_ll6; 240853541Sshin struct in6_addr *router_ll6; 240953541Sshin struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 241053541Sshin struct mbuf *m = NULL; /* newly allocated one */ 241153541Sshin struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 241253541Sshin struct nd_redirect *nd_rd; 241353541Sshin size_t maxlen; 241453541Sshin u_char *p; 241553541Sshin struct ifnet *outif = NULL; 241662587Sitojun struct sockaddr_in6 src_sa; 241753541Sshin 241862587Sitojun icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0); 241962587Sitojun 242053541Sshin /* if we are not router, we don't send icmp6 redirect */ 2421151539Ssuz if (!ip6_forwarding) 242253541Sshin goto fail; 242353541Sshin 242453541Sshin /* sanity check */ 242553541Sshin if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) 242653541Sshin goto fail; 242753541Sshin 242853541Sshin /* 242953541Sshin * Address check: 243053541Sshin * the source address must identify a neighbor, and 243153541Sshin * the destination address must not be a multicast address 243253541Sshin * [RFC 2461, sec 8.2] 243353541Sshin */ 243453541Sshin sip6 = mtod(m0, struct ip6_hdr *); 243562587Sitojun bzero(&src_sa, sizeof(src_sa)); 243662587Sitojun src_sa.sin6_family = AF_INET6; 243762587Sitojun src_sa.sin6_len = sizeof(src_sa); 243862587Sitojun src_sa.sin6_addr = sip6->ip6_src; 243962587Sitojun if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) 244053541Sshin goto fail; 244153541Sshin if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) 244253541Sshin goto fail; /* what should we do here? */ 244353541Sshin 244453541Sshin /* rate limit */ 244553541Sshin if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) 244653541Sshin goto fail; 244753541Sshin 244853541Sshin /* 244953541Sshin * Since we are going to append up to 1280 bytes (= IPV6_MMTU), 245053541Sshin * we almost always ask for an mbuf cluster for simplicity. 245153541Sshin * (MHLEN < IPV6_MMTU is almost always true) 245253541Sshin */ 245362587Sitojun#if IPV6_MMTU >= MCLBYTES 245462587Sitojun# error assumption failed about IPV6_MMTU and MCLBYTES 245562587Sitojun#endif 2456111119Simp MGETHDR(m, M_DONTWAIT, MT_HEADER); 245762587Sitojun if (m && IPV6_MMTU >= MHLEN) 2458111119Simp MCLGET(m, M_DONTWAIT); 245953541Sshin if (!m) 246053541Sshin goto fail; 246178064Sume m->m_pkthdr.rcvif = NULL; 246278064Sume m->m_len = 0; 246378064Sume maxlen = M_TRAILINGSPACE(m); 246453541Sshin maxlen = min(IPV6_MMTU, maxlen); 246553541Sshin /* just for safety */ 246662587Sitojun if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 246762587Sitojun ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { 246853541Sshin goto fail; 246962587Sitojun } 247053541Sshin 247153541Sshin { 247253541Sshin /* get ip6 linklocal address for ifp(my outgoing interface). */ 247362587Sitojun struct in6_ifaddr *ia; 247462587Sitojun if ((ia = in6ifa_ifpforlinklocal(ifp, 247562587Sitojun IN6_IFF_NOTREADY| 247662587Sitojun IN6_IFF_ANYCAST)) == NULL) 247753541Sshin goto fail; 247853541Sshin ifp_ll6 = &ia->ia_addr.sin6_addr; 247953541Sshin } 248053541Sshin 248153541Sshin /* get ip6 linklocal address for the router. */ 248253541Sshin if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { 248353541Sshin struct sockaddr_in6 *sin6; 248453541Sshin sin6 = (struct sockaddr_in6 *)rt->rt_gateway; 248553541Sshin router_ll6 = &sin6->sin6_addr; 248653541Sshin if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) 248753541Sshin router_ll6 = (struct in6_addr *)NULL; 248853541Sshin } else 248953541Sshin router_ll6 = (struct in6_addr *)NULL; 249053541Sshin 249153541Sshin /* ip6 */ 249253541Sshin ip6 = mtod(m, struct ip6_hdr *); 249353541Sshin ip6->ip6_flow = 0; 249462587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 249562587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 249653541Sshin /* ip6->ip6_plen will be set later */ 249753541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 249853541Sshin ip6->ip6_hlim = 255; 249953541Sshin /* ip6->ip6_src must be linklocal addr for my outgoing if. */ 250053541Sshin bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); 250153541Sshin bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); 250253541Sshin 250353541Sshin /* ND Redirect */ 250453541Sshin nd_rd = (struct nd_redirect *)(ip6 + 1); 250553541Sshin nd_rd->nd_rd_type = ND_REDIRECT; 250653541Sshin nd_rd->nd_rd_code = 0; 250753541Sshin nd_rd->nd_rd_reserved = 0; 250853541Sshin if (rt->rt_flags & RTF_GATEWAY) { 250953541Sshin /* 251053541Sshin * nd_rd->nd_rd_target must be a link-local address in 251153541Sshin * better router cases. 251253541Sshin */ 251353541Sshin if (!router_ll6) 251453541Sshin goto fail; 251553541Sshin bcopy(router_ll6, &nd_rd->nd_rd_target, 2516120892Sume sizeof(nd_rd->nd_rd_target)); 251753541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2518120892Sume sizeof(nd_rd->nd_rd_dst)); 251953541Sshin } else { 252053541Sshin /* make sure redtgt == reddst */ 252153541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, 2522120892Sume sizeof(nd_rd->nd_rd_target)); 252353541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2524120892Sume sizeof(nd_rd->nd_rd_dst)); 252553541Sshin } 252653541Sshin 252753541Sshin p = (u_char *)(nd_rd + 1); 252853541Sshin 252953541Sshin if (!router_ll6) 253053541Sshin goto nolladdropt; 253153541Sshin 2532120892Sume { 2533120892Sume /* target lladdr option */ 2534120892Sume struct rtentry *rt_router = NULL; 2535120892Sume int len; 2536120892Sume struct sockaddr_dl *sdl; 2537120892Sume struct nd_opt_hdr *nd_opt; 2538120892Sume char *lladdr; 253953541Sshin 2540120892Sume rt_router = nd6_lookup(router_ll6, 0, ifp); 2541120892Sume if (!rt_router) 2542120892Sume goto nolladdropt; 2543120892Sume len = sizeof(*nd_opt) + ifp->if_addrlen; 2544120892Sume len = (len + 7) & ~7; /* round by 8 */ 2545120892Sume /* safety check */ 2546120892Sume if (len + (p - (u_char *)ip6) > maxlen) 2547120892Sume goto nolladdropt; 2548120892Sume if (!(rt_router->rt_flags & RTF_GATEWAY) && 2549120892Sume (rt_router->rt_flags & RTF_LLINFO) && 2550120892Sume (rt_router->rt_gateway->sa_family == AF_LINK) && 2551120892Sume (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) && 2552120892Sume sdl->sdl_alen) { 2553120892Sume nd_opt = (struct nd_opt_hdr *)p; 2554120892Sume nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 2555120892Sume nd_opt->nd_opt_len = len >> 3; 2556120892Sume lladdr = (char *)(nd_opt + 1); 2557120892Sume bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen); 2558120892Sume p += len; 2559120892Sume } 2560120893Sume } 256153541Sshinnolladdropt:; 256253541Sshin 256353541Sshin m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 256453541Sshin 256553541Sshin /* just to be safe */ 256662587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 256753541Sshin if (m0->m_flags & M_DECRYPTED) 256853541Sshin goto noredhdropt; 256962587Sitojun#endif 257062587Sitojun if (p - (u_char *)ip6 > maxlen) 257162587Sitojun goto noredhdropt; 257253541Sshin 2573120891Sume { 2574120891Sume /* redirected header option */ 2575120891Sume int len; 2576120891Sume struct nd_opt_rd_hdr *nd_opt_rh; 257753541Sshin 2578120891Sume /* 2579120891Sume * compute the maximum size for icmp6 redirect header option. 2580120891Sume * XXX room for auth header? 2581120891Sume */ 2582120891Sume len = maxlen - (p - (u_char *)ip6); 2583120891Sume len &= ~7; 258453541Sshin 2585120891Sume /* This is just for simplicity. */ 2586120891Sume if (m0->m_pkthdr.len != m0->m_len) { 2587120891Sume if (m0->m_next) { 2588120891Sume m_freem(m0->m_next); 2589120891Sume m0->m_next = NULL; 2590120891Sume } 2591120891Sume m0->m_pkthdr.len = m0->m_len; 259253541Sshin } 259353541Sshin 2594120891Sume /* 2595120891Sume * Redirected header option spec (RFC2461 4.6.3) talks nothing 2596120891Sume * about padding/truncate rule for the original IP packet. 2597120891Sume * From the discussion on IPv6imp in Feb 1999, 2598120891Sume * the consensus was: 2599120891Sume * - "attach as much as possible" is the goal 2600120891Sume * - pad if not aligned (original size can be guessed by 2601120891Sume * original ip6 header) 2602120891Sume * Following code adds the padding if it is simple enough, 2603120891Sume * and truncates if not. 2604120891Sume */ 2605120891Sume if (m0->m_next || m0->m_pkthdr.len != m0->m_len) 2606120891Sume panic("assumption failed in %s:%d", __FILE__, 2607120891Sume __LINE__); 260853541Sshin 2609120891Sume if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { 2610120891Sume /* not enough room, truncate */ 2611120891Sume m0->m_pkthdr.len = m0->m_len = len - 2612120891Sume sizeof(*nd_opt_rh); 2613120891Sume } else { 2614120891Sume /* enough room, pad or truncate */ 2615120891Sume size_t extra; 261653541Sshin 2617120891Sume extra = m0->m_pkthdr.len % 8; 2618120891Sume if (extra) { 2619120891Sume /* pad if easy enough, truncate if not */ 2620120891Sume if (8 - extra <= M_TRAILINGSPACE(m0)) { 2621120891Sume /* pad */ 2622120891Sume m0->m_len += (8 - extra); 2623120891Sume m0->m_pkthdr.len += (8 - extra); 2624120891Sume } else { 2625120891Sume /* truncate */ 2626120891Sume m0->m_pkthdr.len -= extra; 2627120891Sume m0->m_len -= extra; 2628120891Sume } 262953541Sshin } 2630120891Sume len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); 2631120891Sume m0->m_pkthdr.len = m0->m_len = len - 2632120891Sume sizeof(*nd_opt_rh); 263353541Sshin } 263453541Sshin 2635120891Sume nd_opt_rh = (struct nd_opt_rd_hdr *)p; 2636120891Sume bzero(nd_opt_rh, sizeof(*nd_opt_rh)); 2637120891Sume nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; 2638120891Sume nd_opt_rh->nd_opt_rh_len = len >> 3; 2639120891Sume p += sizeof(*nd_opt_rh); 2640120891Sume m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 264153541Sshin 2642120891Sume /* connect m0 to m */ 2643120891Sume m_tag_delete_chain(m0, NULL); 2644120891Sume m0->m_flags &= ~M_PKTHDR; 2645120891Sume m->m_next = m0; 2646120891Sume m->m_pkthdr.len = m->m_len + m0->m_len; 2647120891Sume m0 = NULL; 2648120891Sume } 264953541Sshinnoredhdropt:; 2650112781Ssuz if (m0) { 2651112781Ssuz m_freem(m0); 2652112781Ssuz m0 = NULL; 2653112781Ssuz } 265453541Sshin 2655121315Sume /* XXX: clear embedded link IDs in the inner header */ 2656121315Sume in6_clearscope(&sip6->ip6_src); 2657121315Sume in6_clearscope(&sip6->ip6_dst); 2658121315Sume in6_clearscope(&nd_rd->nd_rd_target); 2659121315Sume in6_clearscope(&nd_rd->nd_rd_dst); 266053541Sshin 266153541Sshin ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 266253541Sshin 266353541Sshin nd_rd->nd_rd_cksum = 0; 2664120891Sume nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2665120891Sume sizeof(*ip6), ntohs(ip6->ip6_plen)); 266653541Sshin 266753541Sshin /* send the packet to outside... */ 2668105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 266953541Sshin if (outif) { 267053541Sshin icmp6_ifstat_inc(outif, ifs6_out_msg); 267153541Sshin icmp6_ifstat_inc(outif, ifs6_out_redirect); 267253541Sshin } 267353541Sshin icmp6stat.icp6s_outhist[ND_REDIRECT]++; 267453541Sshin 267553541Sshin return; 267653541Sshin 267753541Sshinfail: 267853541Sshin if (m) 267953541Sshin m_freem(m); 268053541Sshin if (m0) 268153541Sshin m_freem(m0); 268253541Sshin} 268353541Sshin 268453541Sshin/* 268553541Sshin * ICMPv6 socket option processing. 268653541Sshin */ 268753541Sshinint 2688171259Sdelphijicmp6_ctloutput(struct socket *so, struct sockopt *sopt) 268953541Sshin{ 269053541Sshin int error = 0; 269153541Sshin int optlen; 269278064Sume struct inpcb *inp = sotoinpcb(so); 269353541Sshin int level, op, optname; 269453541Sshin 269553541Sshin if (sopt) { 269653541Sshin level = sopt->sopt_level; 269753541Sshin op = sopt->sopt_dir; 269853541Sshin optname = sopt->sopt_name; 269953541Sshin optlen = sopt->sopt_valsize; 270053541Sshin } else 270153541Sshin level = op = optname = optlen = 0; 270278064Sume 270353541Sshin if (level != IPPROTO_ICMPV6) { 270453541Sshin return EINVAL; 270553541Sshin } 270653541Sshin 270778064Sume switch (op) { 270853541Sshin case PRCO_SETOPT: 270953541Sshin switch (optname) { 271053541Sshin case ICMP6_FILTER: 271153541Sshin { 271253541Sshin struct icmp6_filter *p; 271353541Sshin 271453541Sshin if (optlen != sizeof(*p)) { 271553541Sshin error = EMSGSIZE; 271653541Sshin break; 271753541Sshin } 271853541Sshin if (inp->in6p_icmp6filt == NULL) { 271953541Sshin error = EINVAL; 272053541Sshin break; 272153541Sshin } 272253541Sshin error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen, 272353541Sshin optlen); 272453541Sshin break; 272553541Sshin } 272653541Sshin 272753541Sshin default: 272853541Sshin error = ENOPROTOOPT; 272953541Sshin break; 273053541Sshin } 273153541Sshin break; 273253541Sshin 273353541Sshin case PRCO_GETOPT: 273453541Sshin switch (optname) { 273553541Sshin case ICMP6_FILTER: 273653541Sshin { 273753541Sshin if (inp->in6p_icmp6filt == NULL) { 273853541Sshin error = EINVAL; 273953541Sshin break; 274053541Sshin } 274153541Sshin error = sooptcopyout(sopt, inp->in6p_icmp6filt, 274253541Sshin sizeof(struct icmp6_filter)); 274353541Sshin break; 274453541Sshin } 274553541Sshin 274653541Sshin default: 274753541Sshin error = ENOPROTOOPT; 274853541Sshin break; 274953541Sshin } 275053541Sshin break; 275153541Sshin } 275253541Sshin 2753120856Sume return (error); 275453541Sshin} 275553541Sshin 275653541Sshin/* 275753541Sshin * Perform rate limit check. 275853541Sshin * Returns 0 if it is okay to send the icmp6 packet. 275953541Sshin * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 276053541Sshin * limitation. 276153541Sshin * 276253541Sshin * XXX per-destination/type check necessary? 2763171259Sdelphij * 2764171259Sdelphij * dst - not used at this moment 2765171259Sdelphij * type - not used at this moment 2766171259Sdelphij * code - not used at this moment 276753541Sshin */ 276853541Sshinstatic int 2769171259Sdelphijicmp6_ratelimit(const struct in6_addr *dst, const int type, 2770171259Sdelphij const int code) 277153541Sshin{ 277262587Sitojun int ret; 277353541Sshin 277495023Ssuz ret = 0; /* okay to send */ 277553541Sshin 277662587Sitojun /* PPS limit */ 277778064Sume if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count, 277878064Sume icmp6errppslim)) { 277953541Sshin /* The packet is subject to rate limit */ 278062587Sitojun ret++; 278153541Sshin } 278253541Sshin 278362587Sitojun return ret; 278453541Sshin} 2785