icmp6.c revision 108466
162587Sitojun/* $FreeBSD: head/sys/netinet6/icmp6.c 108466 2002-12-30 20:22:40Z sam $ */ 278064Sume/* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ */ 362587Sitojun 453541Sshin/* 553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 653541Sshin * All rights reserved. 753541Sshin * 853541Sshin * Redistribution and use in source and binary forms, with or without 953541Sshin * modification, are permitted provided that the following conditions 1053541Sshin * are met: 1153541Sshin * 1. Redistributions of source code must retain the above copyright 1253541Sshin * notice, this list of conditions and the following disclaimer. 1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1453541Sshin * notice, this list of conditions and the following disclaimer in the 1553541Sshin * documentation and/or other materials provided with the distribution. 1653541Sshin * 3. Neither the name of the project nor the names of its contributors 1753541Sshin * may be used to endorse or promote products derived from this software 1853541Sshin * without specific prior written permission. 1953541Sshin * 2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2353541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3053541Sshin * SUCH DAMAGE. 3153541Sshin */ 3253541Sshin 3353541Sshin/* 3453541Sshin * Copyright (c) 1982, 1986, 1988, 1993 3553541Sshin * The Regents of the University of California. All rights reserved. 3653541Sshin * 3753541Sshin * Redistribution and use in source and binary forms, with or without 3853541Sshin * modification, are permitted provided that the following conditions 3953541Sshin * are met: 4053541Sshin * 1. Redistributions of source code must retain the above copyright 4153541Sshin * notice, this list of conditions and the following disclaimer. 4253541Sshin * 2. Redistributions in binary form must reproduce the above copyright 4353541Sshin * notice, this list of conditions and the following disclaimer in the 4453541Sshin * documentation and/or other materials provided with the distribution. 4553541Sshin * 3. All advertising materials mentioning features or use of this software 4653541Sshin * must display the following acknowledgement: 4753541Sshin * This product includes software developed by the University of 4853541Sshin * California, Berkeley and its contributors. 4953541Sshin * 4. Neither the name of the University nor the names of its contributors 5053541Sshin * may be used to endorse or promote products derived from this software 5153541Sshin * without specific prior written permission. 5253541Sshin * 5353541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5453541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5553541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5653541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5753541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5853541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5953541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 6053541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 6153541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 6253541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 6353541Sshin * SUCH DAMAGE. 6453541Sshin * 6553541Sshin * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 6653541Sshin */ 6753541Sshin 6862587Sitojun#include "opt_inet.h" 6962587Sitojun#include "opt_inet6.h" 7055009Sshin#include "opt_ipsec.h" 7153541Sshin 7253541Sshin#include <sys/param.h> 7395759Stanimura#include <sys/domain.h> 7495759Stanimura#include <sys/kernel.h> 7595759Stanimura#include <sys/lock.h> 7678064Sume#include <sys/malloc.h> 7753541Sshin#include <sys/mbuf.h> 7853541Sshin#include <sys/protosw.h> 7995759Stanimura#include <sys/signalvar.h> 8053541Sshin#include <sys/socket.h> 8153541Sshin#include <sys/socketvar.h> 8295759Stanimura#include <sys/sx.h> 8395759Stanimura#include <sys/syslog.h> 8495759Stanimura#include <sys/systm.h> 8553541Sshin#include <sys/time.h> 8653541Sshin 8753541Sshin#include <net/if.h> 8853541Sshin#include <net/if_dl.h> 8953541Sshin#include <net/if_types.h> 9095759Stanimura#include <net/route.h> 9153541Sshin 9253541Sshin#include <netinet/in.h> 9395759Stanimura#include <netinet/in_pcb.h> 9453541Sshin#include <netinet/in_var.h> 9562587Sitojun#include <netinet/ip6.h> 9695759Stanimura#include <netinet/icmp6.h> 9795759Stanimura#include <netinet6/in6_ifattach.h> 9895759Stanimura#include <netinet6/in6_pcb.h> 9995759Stanimura#include <netinet6/ip6protosw.h> 10053541Sshin#include <netinet6/ip6_var.h> 10153541Sshin#include <netinet6/mld6_var.h> 10253541Sshin#include <netinet6/nd6.h> 10353541Sshin 10453541Sshin#ifdef IPSEC 10553541Sshin#include <netinet6/ipsec.h> 10653541Sshin#include <netkey/key.h> 10755009Sshin#endif 10853541Sshin 109105199Ssam#ifdef FAST_IPSEC 110105199Ssam#include <netipsec/ipsec.h> 111105199Ssam#include <netipsec/key.h> 112105199Ssam#define IPSEC 113105199Ssam#endif 114105199Ssam 11553541Sshin#include <net/net_osdep.h> 11653541Sshin 11778064Sume#ifdef HAVE_NRL_INPCB 11878064Sume/* inpcb members */ 11978064Sume#define in6pcb inpcb 12078064Sume#define in6p_laddr inp_laddr6 12178064Sume#define in6p_faddr inp_faddr6 12278064Sume#define in6p_icmp6filt inp_icmp6filt 12378064Sume#define in6p_route inp_route 12478064Sume#define in6p_socket inp_socket 12578064Sume#define in6p_flags inp_flags 12678064Sume#define in6p_moptions inp_moptions6 12778064Sume#define in6p_outputopts inp_outputopts6 12878064Sume#define in6p_ip6 inp_ipv6 12978064Sume#define in6p_flowinfo inp_flowinfo 13078064Sume#define in6p_sp inp_sp 13178064Sume#define in6p_next inp_next 13278064Sume#define in6p_prev inp_prev 13378064Sume/* macro names */ 13478064Sume#define sotoin6pcb sotoinpcb 13578064Sume/* function names */ 13678064Sume#define in6_pcbdetach in_pcbdetach 13778064Sume#define in6_rtchange in_rtchange 13878064Sume 13978064Sume/* 14078064Sume * for KAME src sync over BSD*'s. XXX: FreeBSD (>=3) are VERY different from 14178064Sume * others... 14278064Sume */ 14378064Sume#define in6p_ip6_nxt inp_ipv6.ip6_nxt 14478064Sume#endif 14578064Sume 14662587Sitojunextern struct domain inet6domain; 14753541Sshin 14862587Sitojunstruct icmp6stat icmp6stat; 14953541Sshin 15062587Sitojunextern struct inpcbhead ripcb; 15162587Sitojunextern int icmp6errppslim; 15262587Sitojunstatic int icmp6errpps_count = 0; 15378064Sumestatic struct timeval icmp6errppslim_last; 15462587Sitojunextern int icmp6_nodeinfo; 15553541Sshin 15662587Sitojunstatic void icmp6_errcount __P((struct icmp6errstat *, int, int)); 15762587Sitojunstatic int icmp6_rip6_input __P((struct mbuf **, int)); 15862587Sitojunstatic int icmp6_ratelimit __P((const struct in6_addr *, const int, const int)); 15962587Sitojunstatic const char *icmp6_redirect_diag __P((struct in6_addr *, 16062587Sitojun struct in6_addr *, struct in6_addr *)); 161108143Ssam#define HAVE_PPSRATECHECK 16278064Sume#ifndef HAVE_PPSRATECHECK 16378064Sumestatic int ppsratecheck __P((struct timeval *, int *, int)); 16462587Sitojun#endif 16562587Sitojunstatic struct mbuf *ni6_input __P((struct mbuf *, int)); 16662587Sitojunstatic struct mbuf *ni6_nametodns __P((const char *, int, int)); 16762587Sitojunstatic int ni6_dnsmatch __P((const char *, int, const char *, int)); 16862587Sitojunstatic int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *, 16978064Sume struct ifnet **, char *)); 17062587Sitojunstatic int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, 17162587Sitojun struct ifnet *, int)); 17278064Sumestatic int icmp6_notify_error __P((struct mbuf *, int, int, int)); 17353541Sshin 17453541Sshin#ifdef COMPAT_RFC1885 17562587Sitojunstatic struct route_in6 icmp6_reflect_rt; 17653541Sshin#endif 17753541Sshin 17878064Sume 17953541Sshinvoid 18053541Sshinicmp6_init() 18153541Sshin{ 18253541Sshin mld6_init(); 18353541Sshin} 18453541Sshin 18562587Sitojunstatic void 18662587Sitojunicmp6_errcount(stat, type, code) 18762587Sitojun struct icmp6errstat *stat; 18862587Sitojun int type, code; 18962587Sitojun{ 19078064Sume switch (type) { 19162587Sitojun case ICMP6_DST_UNREACH: 19262587Sitojun switch (code) { 19362587Sitojun case ICMP6_DST_UNREACH_NOROUTE: 19462587Sitojun stat->icp6errs_dst_unreach_noroute++; 19562587Sitojun return; 19662587Sitojun case ICMP6_DST_UNREACH_ADMIN: 19762587Sitojun stat->icp6errs_dst_unreach_admin++; 19862587Sitojun return; 19962587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 20062587Sitojun stat->icp6errs_dst_unreach_beyondscope++; 20162587Sitojun return; 20262587Sitojun case ICMP6_DST_UNREACH_ADDR: 20362587Sitojun stat->icp6errs_dst_unreach_addr++; 20462587Sitojun return; 20562587Sitojun case ICMP6_DST_UNREACH_NOPORT: 20662587Sitojun stat->icp6errs_dst_unreach_noport++; 20762587Sitojun return; 20862587Sitojun } 20962587Sitojun break; 21062587Sitojun case ICMP6_PACKET_TOO_BIG: 21162587Sitojun stat->icp6errs_packet_too_big++; 21262587Sitojun return; 21362587Sitojun case ICMP6_TIME_EXCEEDED: 21478064Sume switch (code) { 21562587Sitojun case ICMP6_TIME_EXCEED_TRANSIT: 21662587Sitojun stat->icp6errs_time_exceed_transit++; 21762587Sitojun return; 21862587Sitojun case ICMP6_TIME_EXCEED_REASSEMBLY: 21962587Sitojun stat->icp6errs_time_exceed_reassembly++; 22062587Sitojun return; 22162587Sitojun } 22262587Sitojun break; 22362587Sitojun case ICMP6_PARAM_PROB: 22478064Sume switch (code) { 22562587Sitojun case ICMP6_PARAMPROB_HEADER: 22662587Sitojun stat->icp6errs_paramprob_header++; 22762587Sitojun return; 22862587Sitojun case ICMP6_PARAMPROB_NEXTHEADER: 22962587Sitojun stat->icp6errs_paramprob_nextheader++; 23062587Sitojun return; 23162587Sitojun case ICMP6_PARAMPROB_OPTION: 23262587Sitojun stat->icp6errs_paramprob_option++; 23362587Sitojun return; 23462587Sitojun } 23562587Sitojun break; 23662587Sitojun case ND_REDIRECT: 23762587Sitojun stat->icp6errs_redirect++; 23862587Sitojun return; 23962587Sitojun } 24062587Sitojun stat->icp6errs_unknown++; 24162587Sitojun} 24262587Sitojun 24353541Sshin/* 24453541Sshin * Generate an error packet of type error in response to bad IP6 packet. 24553541Sshin */ 24653541Sshinvoid 24753541Sshinicmp6_error(m, type, code, param) 24853541Sshin struct mbuf *m; 24953541Sshin int type, code, param; 25053541Sshin{ 25153541Sshin struct ip6_hdr *oip6, *nip6; 25253541Sshin struct icmp6_hdr *icmp6; 25362587Sitojun u_int preplen; 25453541Sshin int off; 25562587Sitojun int nxt; 25653541Sshin 25753541Sshin icmp6stat.icp6s_error++; 25853541Sshin 25962587Sitojun /* count per-type-code statistics */ 26062587Sitojun icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code); 26162587Sitojun 26262587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 26362587Sitojun if (m->m_flags & M_DECRYPTED) { 26462587Sitojun icmp6stat.icp6s_canterror++; 26553541Sshin goto freeit; 26662587Sitojun } 26762587Sitojun#endif 26853541Sshin 26962587Sitojun#ifndef PULLDOWN_TEST 27062587Sitojun IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 27162587Sitojun#else 27262587Sitojun if (m->m_len < sizeof(struct ip6_hdr)) { 27362587Sitojun m = m_pullup(m, sizeof(struct ip6_hdr)); 27462587Sitojun if (m == NULL) 27562587Sitojun return; 27662587Sitojun } 27762587Sitojun#endif 27853541Sshin oip6 = mtod(m, struct ip6_hdr *); 27953541Sshin 28053541Sshin /* 28153541Sshin * Multicast destination check. For unrecognized option errors, 28253541Sshin * this check has already done in ip6_unknown_opt(), so we can 28353541Sshin * check only for other errors. 28453541Sshin */ 28553541Sshin if ((m->m_flags & (M_BCAST|M_MCAST) || 28653541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && 28753541Sshin (type != ICMP6_PACKET_TOO_BIG && 28853541Sshin (type != ICMP6_PARAM_PROB || 28953541Sshin code != ICMP6_PARAMPROB_OPTION))) 29053541Sshin goto freeit; 29153541Sshin 29253541Sshin /* Source address check. XXX: the case of anycast source? */ 29353541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || 29453541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) 29553541Sshin goto freeit; 29653541Sshin 29753541Sshin /* 29862587Sitojun * If we are about to send ICMPv6 against ICMPv6 error/redirect, 29962587Sitojun * don't do it. 30053541Sshin */ 30162587Sitojun nxt = -1; 30262587Sitojun off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 30362587Sitojun if (off >= 0 && nxt == IPPROTO_ICMPV6) { 30453541Sshin struct icmp6_hdr *icp; 30553541Sshin 30662587Sitojun#ifndef PULLDOWN_TEST 30762587Sitojun IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), ); 30862587Sitojun icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 30962587Sitojun#else 31062587Sitojun IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, 31162587Sitojun sizeof(*icp)); 31262587Sitojun if (icp == NULL) { 31362587Sitojun icmp6stat.icp6s_tooshort++; 31462587Sitojun return; 31562587Sitojun } 31662587Sitojun#endif 31762587Sitojun if (icp->icmp6_type < ICMP6_ECHO_REQUEST || 31862587Sitojun icp->icmp6_type == ND_REDIRECT) { 31953541Sshin /* 32062587Sitojun * ICMPv6 error 32162587Sitojun * Special case: for redirect (which is 32262587Sitojun * informational) we must not send icmp6 error. 32353541Sshin */ 32462587Sitojun icmp6stat.icp6s_canterror++; 32562587Sitojun goto freeit; 32662587Sitojun } else { 32762587Sitojun /* ICMPv6 informational - send the error */ 32853541Sshin } 32962587Sitojun } else { 33062587Sitojun /* non-ICMPv6 - send the error */ 33153541Sshin } 33253541Sshin 33353541Sshin oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */ 33453541Sshin 33553541Sshin /* Finally, do rate limitation check. */ 33653541Sshin if (icmp6_ratelimit(&oip6->ip6_src, type, code)) { 33753541Sshin icmp6stat.icp6s_toofreq++; 33853541Sshin goto freeit; 33953541Sshin } 34053541Sshin 34153541Sshin /* 34253541Sshin * OK, ICMP6 can be generated. 34353541Sshin */ 34453541Sshin 34553541Sshin if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) 34653541Sshin m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); 34753541Sshin 34862587Sitojun preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 34962587Sitojun M_PREPEND(m, preplen, M_DONTWAIT); 35062587Sitojun if (m && m->m_len < preplen) 35162587Sitojun m = m_pullup(m, preplen); 35253541Sshin if (m == NULL) { 35378064Sume nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__)); 35453541Sshin return; 35553541Sshin } 35653541Sshin 35753541Sshin nip6 = mtod(m, struct ip6_hdr *); 35853541Sshin nip6->ip6_src = oip6->ip6_src; 35953541Sshin nip6->ip6_dst = oip6->ip6_dst; 36053541Sshin 36153541Sshin if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src)) 36253541Sshin oip6->ip6_src.s6_addr16[1] = 0; 36353541Sshin if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst)) 36453541Sshin oip6->ip6_dst.s6_addr16[1] = 0; 36553541Sshin 36653541Sshin icmp6 = (struct icmp6_hdr *)(nip6 + 1); 36753541Sshin icmp6->icmp6_type = type; 36853541Sshin icmp6->icmp6_code = code; 36953541Sshin icmp6->icmp6_pptr = htonl((u_int32_t)param); 37053541Sshin 37178064Sume /* 37278064Sume * icmp6_reflect() is designed to be in the input path. 37378064Sume * icmp6_error() can be called from both input and outut path, 37478064Sume * and if we are in output path rcvif could contain bogus value. 37578064Sume * clear m->m_pkthdr.rcvif for safety, we should have enough scope 37678064Sume * information in ip header (nip6). 37778064Sume */ 37878064Sume m->m_pkthdr.rcvif = NULL; 37978064Sume 38053541Sshin icmp6stat.icp6s_outhist[type]++; 38195023Ssuz icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ 38262587Sitojun 38362587Sitojun return; 38462587Sitojun 38562587Sitojun freeit: 38662587Sitojun /* 38762587Sitojun * If we can't tell wheter or not we can generate ICMP6, free it. 38862587Sitojun */ 38962587Sitojun m_freem(m); 39053541Sshin} 39153541Sshin 39253541Sshin/* 39353541Sshin * Process a received ICMP6 message. 39453541Sshin */ 39553541Sshinint 39653541Sshinicmp6_input(mp, offp, proto) 39753541Sshin struct mbuf **mp; 39853541Sshin int *offp, proto; 39953541Sshin{ 40053541Sshin struct mbuf *m = *mp, *n; 40153541Sshin struct ip6_hdr *ip6, *nip6; 40253541Sshin struct icmp6_hdr *icmp6, *nicmp6; 40353541Sshin int off = *offp; 40453541Sshin int icmp6len = m->m_pkthdr.len - *offp; 40553541Sshin int code, sum, noff; 40653541Sshin 40762587Sitojun#ifndef PULLDOWN_TEST 40853541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE); 40995023Ssuz /* m might change if M_LOOP. So, call mtod after this */ 41062587Sitojun#endif 41153541Sshin 41253541Sshin /* 41353541Sshin * Locate icmp6 structure in mbuf, and check 41453541Sshin * that not corrupted and of at least minimum length 41553541Sshin */ 41653541Sshin 41753541Sshin ip6 = mtod(m, struct ip6_hdr *); 41853541Sshin if (icmp6len < sizeof(struct icmp6_hdr)) { 41953541Sshin icmp6stat.icp6s_tooshort++; 42053541Sshin goto freeit; 42153541Sshin } 42253541Sshin 42353541Sshin /* 42453541Sshin * calculate the checksum 42553541Sshin */ 42662587Sitojun#ifndef PULLDOWN_TEST 42753541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 42862587Sitojun#else 42962587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 43062587Sitojun if (icmp6 == NULL) { 43162587Sitojun icmp6stat.icp6s_tooshort++; 43262587Sitojun return IPPROTO_DONE; 43362587Sitojun } 43462587Sitojun#endif 43553541Sshin code = icmp6->icmp6_code; 43653541Sshin 43753541Sshin if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { 43878064Sume nd6log((LOG_ERR, 43953541Sshin "ICMP6 checksum error(%d|%x) %s\n", 44078064Sume icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src))); 44153541Sshin icmp6stat.icp6s_checksum++; 44253541Sshin goto freeit; 44353541Sshin } 44453541Sshin 44583934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 44653541Sshin /* 44753541Sshin * Deliver very specific ICMP6 type only. 44853541Sshin * This is important to deilver TOOBIG. Otherwise PMTUD 44953541Sshin * will not work. 45053541Sshin */ 45153541Sshin switch (icmp6->icmp6_type) { 45253541Sshin case ICMP6_DST_UNREACH: 45353541Sshin case ICMP6_PACKET_TOO_BIG: 45453541Sshin case ICMP6_TIME_EXCEEDED: 45553541Sshin break; 45653541Sshin default: 45753541Sshin goto freeit; 45853541Sshin } 45953541Sshin } 46053541Sshin 46153541Sshin icmp6stat.icp6s_inhist[icmp6->icmp6_type]++; 46253541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg); 46353541Sshin if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) 46453541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error); 46553541Sshin 46653541Sshin switch (icmp6->icmp6_type) { 46753541Sshin case ICMP6_DST_UNREACH: 46853541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach); 46953541Sshin switch (code) { 47053541Sshin case ICMP6_DST_UNREACH_NOROUTE: 47153541Sshin code = PRC_UNREACH_NET; 47253541Sshin break; 47353541Sshin case ICMP6_DST_UNREACH_ADMIN: 47453541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib); 47562587Sitojun code = PRC_UNREACH_PROTOCOL; /* is this a good code? */ 47662587Sitojun break; 47753541Sshin case ICMP6_DST_UNREACH_ADDR: 47862587Sitojun code = PRC_HOSTDEAD; 47953541Sshin break; 48062587Sitojun#ifdef COMPAT_RFC1885 48153541Sshin case ICMP6_DST_UNREACH_NOTNEIGHBOR: 48253541Sshin code = PRC_UNREACH_SRCFAIL; 48353541Sshin break; 48462587Sitojun#else 48562587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 48662587Sitojun /* I mean "source address was incorrect." */ 48762587Sitojun code = PRC_PARAMPROB; 48862587Sitojun break; 48962587Sitojun#endif 49053541Sshin case ICMP6_DST_UNREACH_NOPORT: 49153541Sshin code = PRC_UNREACH_PORT; 49253541Sshin break; 49353541Sshin default: 49453541Sshin goto badcode; 49553541Sshin } 49653541Sshin goto deliver; 49753541Sshin break; 49853541Sshin 49953541Sshin case ICMP6_PACKET_TOO_BIG: 50053541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig); 50153541Sshin if (code != 0) 50253541Sshin goto badcode; 50353541Sshin 50453541Sshin code = PRC_MSGSIZE; 50553541Sshin 50662587Sitojun /* 50762587Sitojun * Updating the path MTU will be done after examining 50862587Sitojun * intermediate extension headers. 50962587Sitojun */ 51053541Sshin goto deliver; 51153541Sshin break; 51253541Sshin 51353541Sshin case ICMP6_TIME_EXCEEDED: 51453541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed); 51553541Sshin switch (code) { 51653541Sshin case ICMP6_TIME_EXCEED_TRANSIT: 51753541Sshin case ICMP6_TIME_EXCEED_REASSEMBLY: 51853541Sshin code += PRC_TIMXCEED_INTRANS; 51953541Sshin break; 52053541Sshin default: 52153541Sshin goto badcode; 52253541Sshin } 52353541Sshin goto deliver; 52453541Sshin break; 52553541Sshin 52653541Sshin case ICMP6_PARAM_PROB: 52753541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob); 52853541Sshin switch (code) { 52953541Sshin case ICMP6_PARAMPROB_NEXTHEADER: 53053541Sshin code = PRC_UNREACH_PROTOCOL; 53153541Sshin break; 53253541Sshin case ICMP6_PARAMPROB_HEADER: 53353541Sshin case ICMP6_PARAMPROB_OPTION: 53453541Sshin code = PRC_PARAMPROB; 53553541Sshin break; 53653541Sshin default: 53753541Sshin goto badcode; 53853541Sshin } 53953541Sshin goto deliver; 54053541Sshin break; 54153541Sshin 54253541Sshin case ICMP6_ECHO_REQUEST: 54353541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo); 54453541Sshin if (code != 0) 54553541Sshin goto badcode; 54653541Sshin if ((n = m_copy(m, 0, M_COPYALL)) == NULL) { 54753541Sshin /* Give up remote */ 54853541Sshin break; 54953541Sshin } 55062587Sitojun if ((n->m_flags & M_EXT) != 0 55162587Sitojun || n->m_len < off + sizeof(struct icmp6_hdr)) { 55253541Sshin struct mbuf *n0 = n; 55362587Sitojun const int maxlen = sizeof(*nip6) + sizeof(*nicmp6); 55453541Sshin 55553541Sshin /* 55653541Sshin * Prepare an internal mbuf. m_pullup() doesn't 55753541Sshin * always copy the length we specified. 55853541Sshin */ 55962587Sitojun if (maxlen >= MCLBYTES) { 56062587Sitojun /* Give up remote */ 56162587Sitojun m_freem(n0); 56262587Sitojun break; 56362587Sitojun } 56453541Sshin MGETHDR(n, M_DONTWAIT, n0->m_type); 56562587Sitojun if (n && maxlen >= MHLEN) { 56662587Sitojun MCLGET(n, M_DONTWAIT); 56762587Sitojun if ((n->m_flags & M_EXT) == 0) { 56862587Sitojun m_free(n); 56962587Sitojun n = NULL; 57062587Sitojun } 57162587Sitojun } 57253541Sshin if (n == NULL) { 57353541Sshin /* Give up remote */ 57453541Sshin m_freem(n0); 57553541Sshin break; 57653541Sshin } 577108466Ssam M_MOVE_PKTHDR(n, n0); 57853541Sshin /* 57953541Sshin * Copy IPv6 and ICMPv6 only. 58053541Sshin */ 58153541Sshin nip6 = mtod(n, struct ip6_hdr *); 58253541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 58353541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 58453541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 58562587Sitojun noff = sizeof(struct ip6_hdr); 58662587Sitojun n->m_pkthdr.len = n->m_len = 58762587Sitojun noff + sizeof(struct icmp6_hdr); 58853541Sshin /* 58962587Sitojun * Adjust mbuf. ip6_plen will be adjusted in 59062587Sitojun * ip6_output(). 59153541Sshin */ 59262587Sitojun m_adj(n0, off + sizeof(struct icmp6_hdr)); 59362587Sitojun n->m_pkthdr.len += n0->m_pkthdr.len; 59462587Sitojun n->m_next = n0; 59553541Sshin } else { 59653541Sshin nip6 = mtod(n, struct ip6_hdr *); 59753541Sshin nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off); 59853541Sshin noff = off; 59953541Sshin } 60053541Sshin nicmp6->icmp6_type = ICMP6_ECHO_REPLY; 60153541Sshin nicmp6->icmp6_code = 0; 60253541Sshin if (n) { 60353541Sshin icmp6stat.icp6s_reflect++; 60453541Sshin icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++; 60553541Sshin icmp6_reflect(n, noff); 60653541Sshin } 60753541Sshin break; 60853541Sshin 60953541Sshin case ICMP6_ECHO_REPLY: 61053541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply); 61153541Sshin if (code != 0) 61253541Sshin goto badcode; 61353541Sshin break; 61453541Sshin 61596116Sume case MLD_LISTENER_QUERY: 61696116Sume case MLD_LISTENER_REPORT: 61796116Sume if (icmp6len < sizeof(struct mld_hdr)) 61853541Sshin goto badlen; 61996116Sume if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */ 62053541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery); 62153541Sshin else 62253541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport); 62362587Sitojun if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 62462587Sitojun /* give up local */ 62562587Sitojun mld6_input(m, off); 62662587Sitojun m = NULL; 62762587Sitojun goto freeit; 62862587Sitojun } 62962587Sitojun mld6_input(n, off); 63053541Sshin /* m stays. */ 63153541Sshin break; 63253541Sshin 63396116Sume case MLD_LISTENER_DONE: 63453541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone); 63596116Sume if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */ 63653541Sshin goto badlen; 63753541Sshin break; /* nothing to be done in kernel */ 63853541Sshin 63996116Sume case MLD_MTRACE_RESP: 64096116Sume case MLD_MTRACE: 64195023Ssuz /* XXX: these two are experimental. not officially defind. */ 64253541Sshin /* XXX: per-interface statistics? */ 64362587Sitojun break; /* just pass it to applications */ 64453541Sshin 64553541Sshin case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ 64653541Sshin { 64753541Sshin enum { WRU, FQDN } mode; 64853541Sshin 64962587Sitojun if (!icmp6_nodeinfo) 65062587Sitojun break; 65162587Sitojun 65253541Sshin if (icmp6len == sizeof(struct icmp6_hdr) + 4) 65353541Sshin mode = WRU; 65462587Sitojun else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) 65553541Sshin mode = FQDN; 65653541Sshin else 65753541Sshin goto badlen; 65853541Sshin 65953541Sshin#define hostnamelen strlen(hostname) 66053541Sshin if (mode == FQDN) { 66162587Sitojun#ifndef PULLDOWN_TEST 66253541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), 66353541Sshin IPPROTO_DONE); 66462587Sitojun#endif 66562587Sitojun n = m_copy(m, 0, M_COPYALL); 66662587Sitojun if (n) 66762587Sitojun n = ni6_input(n, off); 66862587Sitojun /* XXX meaningless if n == NULL */ 66953541Sshin noff = sizeof(struct ip6_hdr); 67053541Sshin } else { 67153541Sshin u_char *p; 67262587Sitojun int maxlen, maxhlen; 67353541Sshin 67478064Sume if ((icmp6_nodeinfo & 5) != 5) 67578064Sume break; 67678064Sume 67762587Sitojun if (code != 0) 67862587Sitojun goto badcode; 67962587Sitojun maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4; 68062587Sitojun if (maxlen >= MCLBYTES) { 68162587Sitojun /* Give up remote */ 68262587Sitojun break; 68362587Sitojun } 68453541Sshin MGETHDR(n, M_DONTWAIT, m->m_type); 68562587Sitojun if (n && maxlen > MHLEN) { 68662587Sitojun MCLGET(n, M_DONTWAIT); 68762587Sitojun if ((n->m_flags & M_EXT) == 0) { 68862587Sitojun m_free(n); 68962587Sitojun n = NULL; 69062587Sitojun } 69162587Sitojun } 692108466Ssam if (!m_dup_pkthdr(n, m, M_DONTWAIT)) { 693108466Ssam /* 694108466Ssam * Previous code did a blind M_COPY_PKTHDR 695108466Ssam * and said "just for rcvif". If true, then 696108466Ssam * we could tolerate the dup failing (due to 697108466Ssam * the deep copy of the tag chain). For now 698108466Ssam * be conservative and just fail. 699108466Ssam */ 700108466Ssam m_free(n); 701108466Ssam n = NULL; 702108466Ssam } 70353541Sshin if (n == NULL) { 70453541Sshin /* Give up remote */ 70553541Sshin break; 70653541Sshin } 70778064Sume n->m_pkthdr.rcvif = NULL; 70862587Sitojun n->m_len = 0; 70962587Sitojun maxhlen = M_TRAILINGSPACE(n) - maxlen; 71062587Sitojun if (maxhlen > hostnamelen) 71162587Sitojun maxhlen = hostnamelen; 71253541Sshin /* 71353541Sshin * Copy IPv6 and ICMPv6 only. 71453541Sshin */ 71553541Sshin nip6 = mtod(n, struct ip6_hdr *); 71653541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 71753541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 71853541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 71953541Sshin p = (u_char *)(nicmp6 + 1); 72053541Sshin bzero(p, 4); 72195023Ssuz bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */ 72253541Sshin noff = sizeof(struct ip6_hdr); 72353541Sshin n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 72462587Sitojun sizeof(struct icmp6_hdr) + 4 + maxhlen; 72553541Sshin nicmp6->icmp6_type = ICMP6_WRUREPLY; 72653541Sshin nicmp6->icmp6_code = 0; 72753541Sshin } 72853541Sshin#undef hostnamelen 72953541Sshin if (n) { 73053541Sshin icmp6stat.icp6s_reflect++; 73153541Sshin icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++; 73253541Sshin icmp6_reflect(n, noff); 73353541Sshin } 73453541Sshin break; 73553541Sshin } 73653541Sshin 73753541Sshin case ICMP6_WRUREPLY: 73853541Sshin if (code != 0) 73953541Sshin goto badcode; 74053541Sshin break; 74153541Sshin 74253541Sshin case ND_ROUTER_SOLICIT: 74353541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit); 74453541Sshin if (code != 0) 74553541Sshin goto badcode; 74653541Sshin if (icmp6len < sizeof(struct nd_router_solicit)) 74753541Sshin goto badlen; 74862587Sitojun if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 74962587Sitojun /* give up local */ 75062587Sitojun nd6_rs_input(m, off, icmp6len); 75162587Sitojun m = NULL; 75262587Sitojun goto freeit; 75362587Sitojun } 75462587Sitojun nd6_rs_input(n, off, icmp6len); 75553541Sshin /* m stays. */ 75653541Sshin break; 75753541Sshin 75853541Sshin case ND_ROUTER_ADVERT: 75953541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert); 76053541Sshin if (code != 0) 76153541Sshin goto badcode; 76253541Sshin if (icmp6len < sizeof(struct nd_router_advert)) 76353541Sshin goto badlen; 76462587Sitojun if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 76562587Sitojun /* give up local */ 76662587Sitojun nd6_ra_input(m, off, icmp6len); 76762587Sitojun m = NULL; 76862587Sitojun goto freeit; 76962587Sitojun } 77062587Sitojun nd6_ra_input(n, off, icmp6len); 77153541Sshin /* m stays. */ 77253541Sshin break; 77353541Sshin 77453541Sshin case ND_NEIGHBOR_SOLICIT: 77553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit); 77653541Sshin if (code != 0) 77753541Sshin goto badcode; 77853541Sshin if (icmp6len < sizeof(struct nd_neighbor_solicit)) 77953541Sshin goto badlen; 78062587Sitojun if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 78162587Sitojun /* give up local */ 78262587Sitojun nd6_ns_input(m, off, icmp6len); 78362587Sitojun m = NULL; 78462587Sitojun goto freeit; 78562587Sitojun } 78662587Sitojun nd6_ns_input(n, off, icmp6len); 78753541Sshin /* m stays. */ 78853541Sshin break; 78953541Sshin 79053541Sshin case ND_NEIGHBOR_ADVERT: 79153541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert); 79253541Sshin if (code != 0) 79353541Sshin goto badcode; 79453541Sshin if (icmp6len < sizeof(struct nd_neighbor_advert)) 79553541Sshin goto badlen; 79662587Sitojun if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 79762587Sitojun /* give up local */ 79862587Sitojun nd6_na_input(m, off, icmp6len); 79962587Sitojun m = NULL; 80062587Sitojun goto freeit; 80162587Sitojun } 80262587Sitojun nd6_na_input(n, off, icmp6len); 80353541Sshin /* m stays. */ 80453541Sshin break; 80553541Sshin 80653541Sshin case ND_REDIRECT: 80753541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect); 80853541Sshin if (code != 0) 80953541Sshin goto badcode; 81053541Sshin if (icmp6len < sizeof(struct nd_redirect)) 81153541Sshin goto badlen; 81262587Sitojun if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 81362587Sitojun /* give up local */ 81462587Sitojun icmp6_redirect_input(m, off); 81562587Sitojun m = NULL; 81662587Sitojun goto freeit; 81762587Sitojun } 81862587Sitojun icmp6_redirect_input(n, off); 81953541Sshin /* m stays. */ 82053541Sshin break; 82153541Sshin 82253541Sshin case ICMP6_ROUTER_RENUMBERING: 82353541Sshin if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && 82453541Sshin code != ICMP6_ROUTER_RENUMBERING_RESULT) 82553541Sshin goto badcode; 82653541Sshin if (icmp6len < sizeof(struct icmp6_router_renum)) 82753541Sshin goto badlen; 82853541Sshin break; 82953541Sshin 83053541Sshin default: 83178064Sume nd6log((LOG_DEBUG, 83278064Sume "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", 83378064Sume icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src), 83478064Sume ip6_sprintf(&ip6->ip6_dst), 83578064Sume m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0)); 83653541Sshin if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { 83753541Sshin /* ICMPv6 error: MUST deliver it by spec... */ 83853541Sshin code = PRC_NCMDS; 83953541Sshin /* deliver */ 84053541Sshin } else { 84153541Sshin /* ICMPv6 informational: MUST not deliver */ 84253541Sshin break; 84353541Sshin } 84453541Sshin deliver: 84578064Sume if (icmp6_notify_error(m, off, icmp6len, code)) { 84678064Sume /* In this case, m should've been freed. */ 84778064Sume return(IPPROTO_DONE); 84853541Sshin } 84978064Sume break; 85078064Sume 85178064Sume badcode: 85278064Sume icmp6stat.icp6s_badcode++; 85378064Sume break; 85478064Sume 85578064Sume badlen: 85678064Sume icmp6stat.icp6s_badlen++; 85778064Sume break; 85878064Sume } 85978064Sume 86078064Sume /* deliver the packet to appropriate sockets */ 86178064Sume icmp6_rip6_input(&m, *offp); 86278064Sume 86378064Sume return IPPROTO_DONE; 86478064Sume 86578064Sume freeit: 86678064Sume m_freem(m); 86778064Sume return IPPROTO_DONE; 86878064Sume} 86978064Sume 87078064Sumestatic int 87178064Sumeicmp6_notify_error(m, off, icmp6len, code) 87278064Sume struct mbuf *m; 87378064Sume int off, icmp6len; 87478064Sume{ 87578064Sume struct icmp6_hdr *icmp6; 87678064Sume struct ip6_hdr *eip6; 87778064Sume u_int32_t notifymtu; 87878064Sume struct sockaddr_in6 icmp6src, icmp6dst; 87978064Sume 88078064Sume if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { 88178064Sume icmp6stat.icp6s_tooshort++; 88278064Sume goto freeit; 88378064Sume } 88462587Sitojun#ifndef PULLDOWN_TEST 88578064Sume IP6_EXTHDR_CHECK(m, off, 88678064Sume sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), 88778064Sume -1); 88878064Sume icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 88962587Sitojun#else 89078064Sume IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 89178064Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 89278064Sume if (icmp6 == NULL) { 89378064Sume icmp6stat.icp6s_tooshort++; 89478064Sume return(-1); 89578064Sume } 89662587Sitojun#endif 89778064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 89853541Sshin 89978064Sume /* Detect the upper level protocol */ 90078064Sume { 90153541Sshin void (*ctlfunc) __P((int, struct sockaddr *, void *)); 90253541Sshin u_int8_t nxt = eip6->ip6_nxt; 90353541Sshin int eoff = off + sizeof(struct icmp6_hdr) + 90453541Sshin sizeof(struct ip6_hdr); 90553541Sshin struct ip6ctlparam ip6cp; 90662587Sitojun struct in6_addr *finaldst = NULL; 90762587Sitojun int icmp6type = icmp6->icmp6_type; 90862587Sitojun struct ip6_frag *fh; 90962587Sitojun struct ip6_rthdr *rth; 91062587Sitojun struct ip6_rthdr0 *rth0; 91162587Sitojun int rthlen; 91253541Sshin 91395023Ssuz while (1) { /* XXX: should avoid infinite loop explicitly? */ 91453541Sshin struct ip6_ext *eh; 91553541Sshin 91678064Sume switch (nxt) { 91753541Sshin case IPPROTO_HOPOPTS: 91853541Sshin case IPPROTO_DSTOPTS: 91953541Sshin case IPPROTO_AH: 92062587Sitojun#ifndef PULLDOWN_TEST 92153541Sshin IP6_EXTHDR_CHECK(m, 0, eoff + 92253541Sshin sizeof(struct ip6_ext), 92378064Sume -1); 92453541Sshin eh = (struct ip6_ext *)(mtod(m, caddr_t) 92553541Sshin + eoff); 92662587Sitojun#else 92762587Sitojun IP6_EXTHDR_GET(eh, struct ip6_ext *, m, 92878064Sume eoff, sizeof(*eh)); 92962587Sitojun if (eh == NULL) { 93062587Sitojun icmp6stat.icp6s_tooshort++; 93178064Sume return(-1); 93262587Sitojun } 93362587Sitojun#endif 93462587Sitojun 93553541Sshin if (nxt == IPPROTO_AH) 93653541Sshin eoff += (eh->ip6e_len + 2) << 2; 93753541Sshin else 93853541Sshin eoff += (eh->ip6e_len + 1) << 3; 93953541Sshin nxt = eh->ip6e_nxt; 94053541Sshin break; 94162587Sitojun case IPPROTO_ROUTING: 94262587Sitojun /* 94362587Sitojun * When the erroneous packet contains a 94462587Sitojun * routing header, we should examine the 94562587Sitojun * header to determine the final destination. 94662587Sitojun * Otherwise, we can't properly update 94762587Sitojun * information that depends on the final 94862587Sitojun * destination (e.g. path MTU). 94962587Sitojun */ 95062587Sitojun#ifndef PULLDOWN_TEST 95162587Sitojun IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), 95278064Sume -1); 95362587Sitojun rth = (struct ip6_rthdr *)(mtod(m, caddr_t) 95462587Sitojun + eoff); 95562587Sitojun#else 95662587Sitojun IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m, 95778064Sume eoff, sizeof(*rth)); 95862587Sitojun if (rth == NULL) { 95962587Sitojun icmp6stat.icp6s_tooshort++; 96078064Sume return(-1); 96162587Sitojun } 96262587Sitojun#endif 96362587Sitojun rthlen = (rth->ip6r_len + 1) << 3; 96462587Sitojun /* 96562587Sitojun * XXX: currently there is no 96662587Sitojun * officially defined type other 96762587Sitojun * than type-0. 96862587Sitojun * Note that if the segment left field 96962587Sitojun * is 0, all intermediate hops must 97062587Sitojun * have been passed. 97162587Sitojun */ 97262587Sitojun if (rth->ip6r_segleft && 97362587Sitojun rth->ip6r_type == IPV6_RTHDR_TYPE_0) { 97462587Sitojun int hops; 97562587Sitojun 97662587Sitojun#ifndef PULLDOWN_TEST 97762587Sitojun IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, 97878064Sume -1); 97962587Sitojun rth0 = (struct ip6_rthdr0 *)(mtod(m, caddr_t) + eoff); 98062587Sitojun#else 98162587Sitojun IP6_EXTHDR_GET(rth0, 98262587Sitojun struct ip6_rthdr0 *, m, 98362587Sitojun eoff, rthlen); 98462587Sitojun if (rth0 == NULL) { 98562587Sitojun icmp6stat.icp6s_tooshort++; 98678064Sume return(-1); 98762587Sitojun } 98862587Sitojun#endif 98962587Sitojun /* just ignore a bogus header */ 99062587Sitojun if ((rth0->ip6r0_len % 2) == 0 && 99162587Sitojun (hops = rth0->ip6r0_len/2)) 99262587Sitojun finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1); 99362587Sitojun } 99462587Sitojun eoff += rthlen; 99562587Sitojun nxt = rth->ip6r_nxt; 99662587Sitojun break; 99762587Sitojun case IPPROTO_FRAGMENT: 99862587Sitojun#ifndef PULLDOWN_TEST 99962587Sitojun IP6_EXTHDR_CHECK(m, 0, eoff + 100062587Sitojun sizeof(struct ip6_frag), 100178064Sume -1); 100262587Sitojun fh = (struct ip6_frag *)(mtod(m, caddr_t) 100362587Sitojun + eoff); 100462587Sitojun#else 100562587Sitojun IP6_EXTHDR_GET(fh, struct ip6_frag *, m, 100678064Sume eoff, sizeof(*fh)); 100762587Sitojun if (fh == NULL) { 100862587Sitojun icmp6stat.icp6s_tooshort++; 100978064Sume return(-1); 101062587Sitojun } 101162587Sitojun#endif 101262587Sitojun /* 101362587Sitojun * Data after a fragment header is meaningless 101462587Sitojun * unless it is the first fragment, but 101562587Sitojun * we'll go to the notify label for path MTU 101662587Sitojun * discovery. 101762587Sitojun */ 101862587Sitojun if (fh->ip6f_offlg & IP6F_OFF_MASK) 101962587Sitojun goto notify; 102062587Sitojun 102162587Sitojun eoff += sizeof(struct ip6_frag); 102262587Sitojun nxt = fh->ip6f_nxt; 102362587Sitojun break; 102453541Sshin default: 102562587Sitojun /* 102662587Sitojun * This case includes ESP and the No Next 102795023Ssuz * Header. In such cases going to the notify 102862587Sitojun * label does not have any meaning 102962587Sitojun * (i.e. ctlfunc will be NULL), but we go 103062587Sitojun * anyway since we might have to update 103162587Sitojun * path MTU information. 103262587Sitojun */ 103353541Sshin goto notify; 103453541Sshin } 103553541Sshin } 103678064Sume notify: 103762587Sitojun#ifndef PULLDOWN_TEST 103853541Sshin icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 103962587Sitojun#else 104062587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 104178064Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 104262587Sitojun if (icmp6 == NULL) { 104362587Sitojun icmp6stat.icp6s_tooshort++; 104478064Sume return(-1); 104562587Sitojun } 104662587Sitojun#endif 104778064Sume 104878064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 104978064Sume bzero(&icmp6dst, sizeof(icmp6dst)); 105078064Sume icmp6dst.sin6_len = sizeof(struct sockaddr_in6); 105178064Sume icmp6dst.sin6_family = AF_INET6; 105278064Sume if (finaldst == NULL) 105378064Sume icmp6dst.sin6_addr = eip6->ip6_dst; 105478064Sume else 105578064Sume icmp6dst.sin6_addr = *finaldst; 105678064Sume icmp6dst.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif, 105778064Sume &icmp6dst.sin6_addr); 105878064Sume#ifndef SCOPEDROUTING 105978064Sume if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst, 106078064Sume NULL, NULL)) { 106178064Sume /* should be impossbile */ 106278064Sume nd6log((LOG_DEBUG, 106378064Sume "icmp6_notify_error: in6_embedscope failed\n")); 106478064Sume goto freeit; 106578064Sume } 106678064Sume#endif 106778064Sume 106878064Sume /* 106978064Sume * retrieve parameters from the inner IPv6 header, and convert 107078064Sume * them into sockaddr structures. 107178064Sume */ 107278064Sume bzero(&icmp6src, sizeof(icmp6src)); 107378064Sume icmp6src.sin6_len = sizeof(struct sockaddr_in6); 107478064Sume icmp6src.sin6_family = AF_INET6; 107578064Sume icmp6src.sin6_addr = eip6->ip6_src; 107678064Sume icmp6src.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif, 107778064Sume &icmp6src.sin6_addr); 107878064Sume#ifndef SCOPEDROUTING 107978064Sume if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src, 108078064Sume NULL, NULL)) { 108178064Sume /* should be impossbile */ 108278064Sume nd6log((LOG_DEBUG, 108378064Sume "icmp6_notify_error: in6_embedscope failed\n")); 108478064Sume goto freeit; 108578064Sume } 108678064Sume#endif 108778064Sume icmp6src.sin6_flowinfo = 108878064Sume (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); 108978064Sume 109078064Sume if (finaldst == NULL) 109178064Sume finaldst = &eip6->ip6_dst; 109278064Sume ip6cp.ip6c_m = m; 109378064Sume ip6cp.ip6c_icmp6 = icmp6; 109478064Sume ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1); 109578064Sume ip6cp.ip6c_off = eoff; 109678064Sume ip6cp.ip6c_finaldst = finaldst; 109778064Sume ip6cp.ip6c_src = &icmp6src; 109878064Sume ip6cp.ip6c_nxt = nxt; 109978064Sume 110062587Sitojun if (icmp6type == ICMP6_PACKET_TOO_BIG) { 110178064Sume notifymtu = ntohl(icmp6->icmp6_mtu); 110278064Sume ip6cp.ip6c_cmdarg = (void *)¬ifymtu; 110378064Sume icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ 110462587Sitojun } 110562587Sitojun 110653541Sshin ctlfunc = (void (*) __P((int, struct sockaddr *, void *))) 110753541Sshin (inet6sw[ip6_protox[nxt]].pr_ctlinput); 110853541Sshin if (ctlfunc) { 110978064Sume (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, 111078064Sume &ip6cp); 111153541Sshin } 111253541Sshin } 111378064Sume return(0); 111453541Sshin 111578064Sume freeit: 111653541Sshin m_freem(m); 111778064Sume return(-1); 111853541Sshin} 111953541Sshin 112078064Sumevoid 112178064Sumeicmp6_mtudisc_update(ip6cp, validated) 112278064Sume struct ip6ctlparam *ip6cp; 112378064Sume int validated; 112462587Sitojun{ 112578064Sume struct in6_addr *dst = ip6cp->ip6c_finaldst; 112678064Sume struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; 112778064Sume struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ 112862587Sitojun u_int mtu = ntohl(icmp6->icmp6_mtu); 112962587Sitojun struct rtentry *rt = NULL; 113062587Sitojun struct sockaddr_in6 sin6; 113162587Sitojun 113278064Sume if (!validated) 113378064Sume return; 113478064Sume 113562587Sitojun bzero(&sin6, sizeof(sin6)); 113662587Sitojun sin6.sin6_family = PF_INET6; 113762587Sitojun sin6.sin6_len = sizeof(struct sockaddr_in6); 113862587Sitojun sin6.sin6_addr = *dst; 113978064Sume /* XXX normally, this won't happen */ 114078064Sume if (IN6_IS_ADDR_LINKLOCAL(dst)) { 114178064Sume sin6.sin6_addr.s6_addr16[1] = 114278064Sume htons(m->m_pkthdr.rcvif->if_index); 114378064Sume } 114462587Sitojun /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */ 114562587Sitojun rt = rtalloc1((struct sockaddr *)&sin6, 0, 114662587Sitojun RTF_CLONING | RTF_PRCLONING); 114762587Sitojun 114862587Sitojun if (rt && (rt->rt_flags & RTF_HOST) 114962587Sitojun && !(rt->rt_rmx.rmx_locks & RTV_MTU)) { 115062587Sitojun if (mtu < IPV6_MMTU) { 115162587Sitojun /* xxx */ 115262587Sitojun rt->rt_rmx.rmx_locks |= RTV_MTU; 115362587Sitojun } else if (mtu < rt->rt_ifp->if_mtu && 115462587Sitojun rt->rt_rmx.rmx_mtu > mtu) { 115578064Sume icmp6stat.icp6s_pmtuchg++; 115662587Sitojun rt->rt_rmx.rmx_mtu = mtu; 115762587Sitojun } 115862587Sitojun } 115995023Ssuz if (rt) { /* XXX: need braces to avoid conflict with else in RTFREE. */ 116062587Sitojun RTFREE(rt); 116195023Ssuz } 116262587Sitojun} 116362587Sitojun 116453541Sshin/* 116578064Sume * Process a Node Information Query packet, based on 116678064Sume * draft-ietf-ipngwg-icmp-name-lookups-07. 116762587Sitojun * 116862587Sitojun * Spec incompatibilities: 116962587Sitojun * - IPv6 Subject address handling 117062587Sitojun * - IPv4 Subject address handling support missing 117162587Sitojun * - Proxy reply (answer even if it's not for me) 117262587Sitojun * - joins NI group address at in6_ifattach() time only, does not cope 117362587Sitojun * with hostname changes by sethostname(3) 117453541Sshin */ 117562587Sitojun#define hostnamelen strlen(hostname) 117653541Sshinstatic struct mbuf * 117753541Sshinni6_input(m, off) 117853541Sshin struct mbuf *m; 117953541Sshin int off; 118053541Sshin{ 118162587Sitojun struct icmp6_nodeinfo *ni6, *nni6; 118253541Sshin struct mbuf *n = NULL; 118362587Sitojun u_int16_t qtype; 118462587Sitojun int subjlen; 118553541Sshin int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 118653541Sshin struct ni_reply_fqdn *fqdn; 118753541Sshin int addrs; /* for NI_QTYPE_NODEADDR */ 118853541Sshin struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ 118978064Sume struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */ 119078064Sume struct sockaddr_in6 sin6_d; /* XXX: we should retrieve this from m_aux */ 119162587Sitojun struct ip6_hdr *ip6; 119262587Sitojun int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ 119378064Sume char *subj = NULL; 119478064Sume struct in6_ifaddr *ia6 = NULL; 119553541Sshin 119662587Sitojun ip6 = mtod(m, struct ip6_hdr *); 119762587Sitojun#ifndef PULLDOWN_TEST 119862587Sitojun ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); 119962587Sitojun#else 120062587Sitojun IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); 120162587Sitojun if (ni6 == NULL) { 120262587Sitojun /* m is already reclaimed */ 120362587Sitojun return NULL; 120462587Sitojun } 120562587Sitojun#endif 120653541Sshin 120762587Sitojun /* 120862587Sitojun * Validate IPv6 destination address. 120962587Sitojun * 121078064Sume * The Responder must discard the Query without further processing 121178064Sume * unless it is one of the Responder's unicast or anycast addresses, or 121278064Sume * a link-local scope multicast address which the Responder has joined. 121378064Sume * [icmp-name-lookups-07, Section 4.] 121462587Sitojun */ 121562587Sitojun bzero(&sin6, sizeof(sin6)); 121662587Sitojun sin6.sin6_family = AF_INET6; 121762587Sitojun sin6.sin6_len = sizeof(struct sockaddr_in6); 121862587Sitojun bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr)); 121962587Sitojun /* XXX scopeid */ 122078064Sume if ((ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)&sin6)) != NULL) { 122178064Sume /* unicast/anycast, fine */ 122278064Sume if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 122378064Sume (icmp6_nodeinfo & 4) == 0) { 122478064Sume nd6log((LOG_DEBUG, "ni6_input: ignore node info to " 122578064Sume "a temporary address in %s:%d", 122678064Sume __FILE__, __LINE__)); 122778064Sume goto bad; 122878064Sume } 122978064Sume } else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) 123078064Sume ; /* link-local multicast, fine */ 123162587Sitojun else 123262587Sitojun goto bad; 123362587Sitojun 123478064Sume /* validate query Subject field. */ 123562587Sitojun qtype = ntohs(ni6->ni_qtype); 123662587Sitojun subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); 123762587Sitojun switch (qtype) { 123862587Sitojun case NI_QTYPE_NOOP: 123962587Sitojun case NI_QTYPE_SUPTYPES: 124078064Sume /* 07 draft */ 124178064Sume if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) 124278064Sume break; 124378064Sume /* FALLTHROUGH */ 124462587Sitojun case NI_QTYPE_FQDN: 124562587Sitojun case NI_QTYPE_NODEADDR: 124662587Sitojun switch (ni6->ni_code) { 124762587Sitojun case ICMP6_NI_SUBJ_IPV6: 124862587Sitojun#if ICMP6_NI_SUBJ_IPV6 != 0 124962587Sitojun case 0: 125062587Sitojun#endif 125162587Sitojun /* 125262587Sitojun * backward compatibility - try to accept 03 draft 125362587Sitojun * format, where no Subject is present. 125462587Sitojun */ 125578064Sume if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && 125678064Sume subjlen == 0) { 125762587Sitojun oldfqdn++; 125862587Sitojun break; 125962587Sitojun } 126078064Sume#if ICMP6_NI_SUBJ_IPV6 != 0 126178064Sume if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) 126278064Sume goto bad; 126378064Sume#endif 126462587Sitojun 126562587Sitojun if (subjlen != sizeof(sin6.sin6_addr)) 126662587Sitojun goto bad; 126762587Sitojun 126862587Sitojun /* 126962587Sitojun * Validate Subject address. 127062587Sitojun * 127178064Sume * Not sure what exactly "address belongs to the node" 127278064Sume * means in the spec, is it just unicast, or what? 127362587Sitojun * 127462587Sitojun * At this moment we consider Subject address as 127562587Sitojun * "belong to the node" if the Subject address equals 127662587Sitojun * to the IPv6 destination address; validation for 127762587Sitojun * IPv6 destination address should have done enough 127862587Sitojun * check for us. 127962587Sitojun * 128062587Sitojun * We do not do proxy at this moment. 128162587Sitojun */ 128262587Sitojun /* m_pulldown instead of copy? */ 128362587Sitojun m_copydata(m, off + sizeof(struct icmp6_nodeinfo), 128462587Sitojun subjlen, (caddr_t)&sin6.sin6_addr); 128578064Sume sin6.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif, 128678064Sume &sin6.sin6_addr); 128778064Sume#ifndef SCOPEDROUTING 128878064Sume in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL); 128962587Sitojun#endif 129078064Sume bzero(&sin6_d, sizeof(sin6_d)); 129178064Sume sin6_d.sin6_family = AF_INET6; /* not used, actually */ 129278064Sume sin6_d.sin6_len = sizeof(sin6_d); /* ditto */ 129378064Sume sin6_d.sin6_addr = ip6->ip6_dst; 129478064Sume sin6_d.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif, 129578064Sume &ip6->ip6_dst); 129678064Sume#ifndef SCOPEDROUTING 129778064Sume in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL); 129878064Sume#endif 129978064Sume subj = (char *)&sin6; 130078064Sume if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d)) 130162587Sitojun break; 130278064Sume 130362587Sitojun /* 130462587Sitojun * XXX if we are to allow other cases, we should really 130562587Sitojun * be careful about scope here. 130662587Sitojun * basically, we should disallow queries toward IPv6 130762587Sitojun * destination X with subject Y, if scope(X) > scope(Y). 130862587Sitojun * if we allow scope(X) > scope(Y), it will result in 130962587Sitojun * information leakage across scope boundary. 131062587Sitojun */ 131162587Sitojun goto bad; 131262587Sitojun 131362587Sitojun case ICMP6_NI_SUBJ_FQDN: 131462587Sitojun /* 131562587Sitojun * Validate Subject name with gethostname(3). 131662587Sitojun * 131762587Sitojun * The behavior may need some debate, since: 131862587Sitojun * - we are not sure if the node has FQDN as 131962587Sitojun * hostname (returned by gethostname(3)). 132062587Sitojun * - the code does wildcard match for truncated names. 132162587Sitojun * however, we are not sure if we want to perform 132262587Sitojun * wildcard match, if gethostname(3) side has 132362587Sitojun * truncated hostname. 132462587Sitojun */ 132562587Sitojun n = ni6_nametodns(hostname, hostnamelen, 0); 132662587Sitojun if (!n || n->m_next || n->m_len == 0) 132762587Sitojun goto bad; 132862587Sitojun IP6_EXTHDR_GET(subj, char *, m, 132962587Sitojun off + sizeof(struct icmp6_nodeinfo), subjlen); 133062587Sitojun if (subj == NULL) 133162587Sitojun goto bad; 133262587Sitojun if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 133362587Sitojun n->m_len)) { 133462587Sitojun goto bad; 133562587Sitojun } 133662587Sitojun m_freem(n); 133762587Sitojun n = NULL; 133862587Sitojun break; 133962587Sitojun 134078064Sume case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 134162587Sitojun default: 134262587Sitojun goto bad; 134362587Sitojun } 134462587Sitojun break; 134578064Sume } 134662587Sitojun 134778064Sume /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 134878064Sume switch (qtype) { 134978064Sume case NI_QTYPE_FQDN: 135078064Sume if ((icmp6_nodeinfo & 1) == 0) 135178064Sume goto bad; 135278064Sume break; 135378064Sume case NI_QTYPE_NODEADDR: 135478064Sume if ((icmp6_nodeinfo & 2) == 0) 135578064Sume goto bad; 135678064Sume break; 135778064Sume } 135878064Sume 135978064Sume /* guess reply length */ 136078064Sume switch (qtype) { 136178064Sume case NI_QTYPE_NOOP: 136278064Sume break; /* no reply data */ 136378064Sume case NI_QTYPE_SUPTYPES: 136478064Sume replylen += sizeof(u_int32_t); 136578064Sume break; 136678064Sume case NI_QTYPE_FQDN: 136778064Sume /* XXX will append an mbuf */ 136878064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 136978064Sume break; 137078064Sume case NI_QTYPE_NODEADDR: 137178064Sume addrs = ni6_addrs(ni6, m, &ifp, subj); 137278064Sume if ((replylen += addrs * (sizeof(struct in6_addr) + 137378064Sume sizeof(u_int32_t))) > MCLBYTES) 137478064Sume replylen = MCLBYTES; /* XXX: will truncate pkt later */ 137578064Sume break; 137662587Sitojun default: 137778064Sume /* 137878064Sume * XXX: We must return a reply with the ICMP6 code 137978064Sume * `unknown Qtype' in this case. However we regard the case 138078064Sume * as an FQDN query for backward compatibility. 138178064Sume * Older versions set a random value to this field, 138278064Sume * so it rarely varies in the defined qtypes. 138378064Sume * But the mechanism is not reliable... 138478064Sume * maybe we should obsolete older versions. 138578064Sume */ 138678064Sume qtype = NI_QTYPE_FQDN; 138778064Sume /* XXX will append an mbuf */ 138878064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 138978064Sume oldfqdn++; 139078064Sume break; 139162587Sitojun } 139262587Sitojun 139378064Sume /* allocate an mbuf to reply. */ 139453541Sshin MGETHDR(n, M_DONTWAIT, m->m_type); 139562587Sitojun if (n == NULL) { 139662587Sitojun m_freem(m); 139753541Sshin return(NULL); 139862587Sitojun } 1399108466Ssam M_MOVE_PKTHDR(n, m); /* just for recvif */ 140053541Sshin if (replylen > MHLEN) { 140162587Sitojun if (replylen > MCLBYTES) { 140278064Sume /* 140378064Sume * XXX: should we try to allocate more? But MCLBYTES 140478064Sume * is probably much larger than IPV6_MMTU... 140578064Sume */ 140653541Sshin goto bad; 140762587Sitojun } 140853541Sshin MCLGET(n, M_DONTWAIT); 140953541Sshin if ((n->m_flags & M_EXT) == 0) { 141053541Sshin goto bad; 141153541Sshin } 141253541Sshin } 141353541Sshin n->m_pkthdr.len = n->m_len = replylen; 141453541Sshin 141553541Sshin /* copy mbuf header and IPv6 + Node Information base headers */ 141653541Sshin bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); 141753541Sshin nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 141862587Sitojun bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); 141953541Sshin 142053541Sshin /* qtype dependent procedure */ 142153541Sshin switch (qtype) { 142262587Sitojun case NI_QTYPE_NOOP: 142378064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 142462587Sitojun nni6->ni_flags = 0; 142562587Sitojun break; 142662587Sitojun case NI_QTYPE_SUPTYPES: 142778064Sume { 142878064Sume u_int32_t v; 142978064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 143078064Sume nni6->ni_flags = htons(0x0000); /* raw bitmap */ 143178064Sume /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 143278064Sume v = (u_int32_t)htonl(0x0000000f); 143378064Sume bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 143462587Sitojun break; 143578064Sume } 143662587Sitojun case NI_QTYPE_FQDN: 143778064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 143862587Sitojun fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + 143962587Sitojun sizeof(struct ip6_hdr) + 144062587Sitojun sizeof(struct icmp6_nodeinfo)); 144162587Sitojun nni6->ni_flags = 0; /* XXX: meaningless TTL */ 144262587Sitojun fqdn->ni_fqdn_ttl = 0; /* ditto. */ 144362587Sitojun /* 144462587Sitojun * XXX do we really have FQDN in variable "hostname"? 144562587Sitojun */ 144662587Sitojun n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn); 144762587Sitojun if (n->m_next == NULL) 144862587Sitojun goto bad; 144962587Sitojun /* XXX we assume that n->m_next is not a chain */ 145062587Sitojun if (n->m_next->m_next != NULL) 145162587Sitojun goto bad; 145262587Sitojun n->m_pkthdr.len += n->m_next->m_len; 145362587Sitojun break; 145462587Sitojun case NI_QTYPE_NODEADDR: 145562587Sitojun { 145662587Sitojun int lenlim, copied; 145753541Sshin 145878064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 145978064Sume n->m_pkthdr.len = n->m_len = 146078064Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 146178064Sume lenlim = M_TRAILINGSPACE(n); 146262587Sitojun copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 146362587Sitojun /* XXX: reset mbuf length */ 146462587Sitojun n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 146562587Sitojun sizeof(struct icmp6_nodeinfo) + copied; 146662587Sitojun break; 146753541Sshin } 146862587Sitojun default: 146962587Sitojun break; /* XXX impossible! */ 147062587Sitojun } 147153541Sshin 147253541Sshin nni6->ni_type = ICMP6_NI_REPLY; 147362587Sitojun m_freem(m); 147453541Sshin return(n); 147553541Sshin 147653541Sshin bad: 147762587Sitojun m_freem(m); 147853541Sshin if (n) 147953541Sshin m_freem(n); 148053541Sshin return(NULL); 148153541Sshin} 148253541Sshin#undef hostnamelen 148353541Sshin 148453541Sshin/* 148562587Sitojun * make a mbuf with DNS-encoded string. no compression support. 148662587Sitojun * 148762587Sitojun * XXX names with less than 2 dots (like "foo" or "foo.section") will be 148862587Sitojun * treated as truncated name (two \0 at the end). this is a wild guess. 148962587Sitojun */ 149062587Sitojunstatic struct mbuf * 149162587Sitojunni6_nametodns(name, namelen, old) 149262587Sitojun const char *name; 149362587Sitojun int namelen; 149462587Sitojun int old; /* return pascal string if non-zero */ 149562587Sitojun{ 149662587Sitojun struct mbuf *m; 149762587Sitojun char *cp, *ep; 149862587Sitojun const char *p, *q; 149962587Sitojun int i, len, nterm; 150062587Sitojun 150162587Sitojun if (old) 150262587Sitojun len = namelen + 1; 150362587Sitojun else 150462587Sitojun len = MCLBYTES; 150562587Sitojun 150662587Sitojun /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ 150762587Sitojun MGET(m, M_DONTWAIT, MT_DATA); 150862587Sitojun if (m && len > MLEN) { 150962587Sitojun MCLGET(m, M_DONTWAIT); 151062587Sitojun if ((m->m_flags & M_EXT) == 0) 151162587Sitojun goto fail; 151262587Sitojun } 151362587Sitojun if (!m) 151462587Sitojun goto fail; 151562587Sitojun m->m_next = NULL; 151662587Sitojun 151762587Sitojun if (old) { 151862587Sitojun m->m_len = len; 151962587Sitojun *mtod(m, char *) = namelen; 152062587Sitojun bcopy(name, mtod(m, char *) + 1, namelen); 152162587Sitojun return m; 152262587Sitojun } else { 152362587Sitojun m->m_len = 0; 152462587Sitojun cp = mtod(m, char *); 152562587Sitojun ep = mtod(m, char *) + M_TRAILINGSPACE(m); 152662587Sitojun 152762587Sitojun /* if not certain about my name, return empty buffer */ 152862587Sitojun if (namelen == 0) 152962587Sitojun return m; 153062587Sitojun 153162587Sitojun /* 153262587Sitojun * guess if it looks like shortened hostname, or FQDN. 153362587Sitojun * shortened hostname needs two trailing "\0". 153462587Sitojun */ 153562587Sitojun i = 0; 153662587Sitojun for (p = name; p < name + namelen; p++) { 153762587Sitojun if (*p && *p == '.') 153862587Sitojun i++; 153962587Sitojun } 154062587Sitojun if (i < 2) 154162587Sitojun nterm = 2; 154262587Sitojun else 154362587Sitojun nterm = 1; 154462587Sitojun 154562587Sitojun p = name; 154662587Sitojun while (cp < ep && p < name + namelen) { 154762587Sitojun i = 0; 154862587Sitojun for (q = p; q < name + namelen && *q && *q != '.'; q++) 154962587Sitojun i++; 155062587Sitojun /* result does not fit into mbuf */ 155162587Sitojun if (cp + i + 1 >= ep) 155262587Sitojun goto fail; 155378704Sume /* 155478704Sume * DNS label length restriction, RFC1035 page 8. 155578704Sume * "i == 0" case is included here to avoid returning 155678704Sume * 0-length label on "foo..bar". 155778704Sume */ 155878704Sume if (i <= 0 || i >= 64) 155962587Sitojun goto fail; 156062587Sitojun *cp++ = i; 156162587Sitojun bcopy(p, cp, i); 156262587Sitojun cp += i; 156362587Sitojun p = q; 156462587Sitojun if (p < name + namelen && *p == '.') 156562587Sitojun p++; 156662587Sitojun } 156762587Sitojun /* termination */ 156862587Sitojun if (cp + nterm >= ep) 156962587Sitojun goto fail; 157062587Sitojun while (nterm-- > 0) 157162587Sitojun *cp++ = '\0'; 157262587Sitojun m->m_len = cp - mtod(m, char *); 157362587Sitojun return m; 157462587Sitojun } 157562587Sitojun 157662587Sitojun panic("should not reach here"); 157795023Ssuz /* NOTREACHED */ 157862587Sitojun 157962587Sitojun fail: 158062587Sitojun if (m) 158162587Sitojun m_freem(m); 158262587Sitojun return NULL; 158362587Sitojun} 158462587Sitojun 158562587Sitojun/* 158662587Sitojun * check if two DNS-encoded string matches. takes care of truncated 158762587Sitojun * form (with \0\0 at the end). no compression support. 158878064Sume * XXX upper/lowercase match (see RFC2065) 158962587Sitojun */ 159062587Sitojunstatic int 159162587Sitojunni6_dnsmatch(a, alen, b, blen) 159262587Sitojun const char *a; 159362587Sitojun int alen; 159462587Sitojun const char *b; 159562587Sitojun int blen; 159662587Sitojun{ 159762587Sitojun const char *a0, *b0; 159862587Sitojun int l; 159962587Sitojun 160062587Sitojun /* simplest case - need validation? */ 160162587Sitojun if (alen == blen && bcmp(a, b, alen) == 0) 160262587Sitojun return 1; 160362587Sitojun 160462587Sitojun a0 = a; 160562587Sitojun b0 = b; 160662587Sitojun 160762587Sitojun /* termination is mandatory */ 160862587Sitojun if (alen < 2 || blen < 2) 160962587Sitojun return 0; 161062587Sitojun if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 161162587Sitojun return 0; 161262587Sitojun alen--; 161362587Sitojun blen--; 161462587Sitojun 161562587Sitojun while (a - a0 < alen && b - b0 < blen) { 161662587Sitojun if (a - a0 + 1 > alen || b - b0 + 1 > blen) 161762587Sitojun return 0; 161862587Sitojun 161962587Sitojun if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 162062587Sitojun return 0; 162162587Sitojun /* we don't support compression yet */ 162262587Sitojun if (a[0] >= 64 || b[0] >= 64) 162362587Sitojun return 0; 162462587Sitojun 162562587Sitojun /* truncated case */ 162662587Sitojun if (a[0] == 0 && a - a0 == alen - 1) 162762587Sitojun return 1; 162862587Sitojun if (b[0] == 0 && b - b0 == blen - 1) 162962587Sitojun return 1; 163062587Sitojun if (a[0] == 0 || b[0] == 0) 163162587Sitojun return 0; 163262587Sitojun 163362587Sitojun if (a[0] != b[0]) 163462587Sitojun return 0; 163562587Sitojun l = a[0]; 163662587Sitojun if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 163762587Sitojun return 0; 163862587Sitojun if (bcmp(a + 1, b + 1, l) != 0) 163962587Sitojun return 0; 164062587Sitojun 164162587Sitojun a += 1 + l; 164262587Sitojun b += 1 + l; 164362587Sitojun } 164462587Sitojun 164562587Sitojun if (a - a0 == alen && b - b0 == blen) 164662587Sitojun return 1; 164762587Sitojun else 164862587Sitojun return 0; 164962587Sitojun} 165062587Sitojun 165162587Sitojun/* 165253541Sshin * calculate the number of addresses to be returned in the node info reply. 165353541Sshin */ 165453541Sshinstatic int 165578064Sumeni6_addrs(ni6, m, ifpp, subj) 165653541Sshin struct icmp6_nodeinfo *ni6; 165753541Sshin struct mbuf *m; 165853541Sshin struct ifnet **ifpp; 165978064Sume char *subj; 166053541Sshin{ 166178064Sume struct ifnet *ifp; 166278064Sume struct in6_ifaddr *ifa6; 166378064Sume struct ifaddr *ifa; 166478064Sume struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */ 166553541Sshin int addrs = 0, addrsofif, iffound = 0; 166678064Sume int niflags = ni6->ni_flags; 166753541Sshin 166878064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 166978064Sume switch (ni6->ni_code) { 167078064Sume case ICMP6_NI_SUBJ_IPV6: 167178064Sume if (subj == NULL) /* must be impossible... */ 167278064Sume return(0); 167378064Sume subj_ip6 = (struct sockaddr_in6 *)subj; 167478064Sume break; 167578064Sume default: 167678064Sume /* 167778064Sume * XXX: we only support IPv6 subject address for 167878064Sume * this Qtype. 167978064Sume */ 168078064Sume return(0); 168178064Sume } 168278064Sume } 168378064Sume 1684108172Shsu IFNET_RLOCK(); 168553541Sshin for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) 168653541Sshin { 168753541Sshin addrsofif = 0; 168879139Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 168953541Sshin { 169053541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 169153541Sshin continue; 169253541Sshin ifa6 = (struct in6_ifaddr *)ifa; 169353541Sshin 169478064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 169578064Sume IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr, 169653541Sshin &ifa6->ia_addr.sin6_addr)) 169753541Sshin iffound = 1; 169853541Sshin 169962587Sitojun /* 170062587Sitojun * IPv4-mapped addresses can only be returned by a 170162587Sitojun * Node Information proxy, since they represent 170262587Sitojun * addresses of IPv4-only nodes, which perforce do 170362587Sitojun * not implement this protocol. 170478064Sume * [icmp-name-lookups-07, Section 5.4] 170562587Sitojun * So we don't support NI_NODEADDR_FLAG_COMPAT in 170662587Sitojun * this function at this moment. 170762587Sitojun */ 170862587Sitojun 170953541Sshin /* What do we have to do about ::1? */ 171078064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 171178064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 171278064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 171378064Sume continue; 171453541Sshin break; 171578064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 171678064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 171778064Sume continue; 171853541Sshin break; 171978064Sume case IPV6_ADDR_SCOPE_GLOBAL: 172078064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 172178064Sume continue; 172278064Sume break; 172378064Sume default: 172478064Sume continue; 172553541Sshin } 172678064Sume 172778064Sume /* 172878064Sume * check if anycast is okay. 172995023Ssuz * XXX: just experimental. not in the spec. 173078064Sume */ 173178064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 173278064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 173378064Sume continue; /* we need only unicast addresses */ 173478064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 173578064Sume (icmp6_nodeinfo & 4) == 0) { 173678064Sume continue; 173778064Sume } 173878064Sume addrsofif++; /* count the address */ 173953541Sshin } 174053541Sshin if (iffound) { 174153541Sshin *ifpp = ifp; 1742108172Shsu IFNET_RUNLOCK(); 174353541Sshin return(addrsofif); 174453541Sshin } 174553541Sshin 174653541Sshin addrs += addrsofif; 174753541Sshin } 1748108172Shsu IFNET_RUNLOCK(); 174953541Sshin 175053541Sshin return(addrs); 175153541Sshin} 175253541Sshin 175353541Sshinstatic int 175453541Sshinni6_store_addrs(ni6, nni6, ifp0, resid) 175553541Sshin struct icmp6_nodeinfo *ni6, *nni6; 175653541Sshin struct ifnet *ifp0; 175753541Sshin int resid; 175853541Sshin{ 175978064Sume struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet); 176078064Sume struct in6_ifaddr *ifa6; 176178064Sume struct ifaddr *ifa; 176278064Sume struct ifnet *ifp_dep = NULL; 176378064Sume int copied = 0, allow_deprecated = 0; 176453541Sshin u_char *cp = (u_char *)(nni6 + 1); 176578064Sume int niflags = ni6->ni_flags; 176678064Sume u_int32_t ltime; 176753541Sshin 176878064Sume if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 176953541Sshin return(0); /* needless to copy */ 1770108172Shsu 1771108172Shsu IFNET_RLOCK(); 177278064Sume again: 177353541Sshin for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) 177453541Sshin { 177562587Sitojun for (ifa = ifp->if_addrlist.tqh_first; ifa; 177662587Sitojun ifa = ifa->ifa_list.tqe_next) 177753541Sshin { 177853541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 177953541Sshin continue; 178053541Sshin ifa6 = (struct in6_ifaddr *)ifa; 178153541Sshin 178278064Sume if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && 178378064Sume allow_deprecated == 0) { 178478064Sume /* 178578064Sume * prefererred address should be put before 178678064Sume * deprecated addresses. 178778064Sume */ 178878064Sume 178978064Sume /* record the interface for later search */ 179078064Sume if (ifp_dep == NULL) 179178064Sume ifp_dep = ifp; 179278064Sume 179378064Sume continue; 179462587Sitojun } 179578064Sume else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && 179678064Sume allow_deprecated != 0) 179778064Sume continue; /* we now collect deprecated addrs */ 179853541Sshin 179953541Sshin /* What do we have to do about ::1? */ 180078064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 180178064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 180278064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 180378064Sume continue; 180453541Sshin break; 180578064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 180678064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 180778064Sume continue; 180853541Sshin break; 180978064Sume case IPV6_ADDR_SCOPE_GLOBAL: 181078064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 181178064Sume continue; 181278064Sume break; 181378064Sume default: 181478064Sume continue; 181553541Sshin } 181653541Sshin 181778064Sume /* 181878064Sume * check if anycast is okay. 181978064Sume * XXX: just experimental. not in the spec. 182078064Sume */ 182178064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 182278064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 182378064Sume continue; 182478064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 182578064Sume (icmp6_nodeinfo & 4) == 0) { 182678064Sume continue; 182753541Sshin } 182878064Sume 182978064Sume /* now we can copy the address */ 183078064Sume if (resid < sizeof(struct in6_addr) + 183178064Sume sizeof(u_int32_t)) { 183278064Sume /* 183378064Sume * We give up much more copy. 183478064Sume * Set the truncate flag and return. 183578064Sume */ 183678064Sume nni6->ni_flags |= 183778064Sume NI_NODEADDR_FLAG_TRUNCATE; 1838108172Shsu IFNET_RUNLOCK(); 183978064Sume return(copied); 184078064Sume } 184178064Sume 184278064Sume /* 184378064Sume * Set the TTL of the address. 184478064Sume * The TTL value should be one of the following 184578064Sume * according to the specification: 184678064Sume * 184778064Sume * 1. The remaining lifetime of a DHCP lease on the 184878064Sume * address, or 184978064Sume * 2. The remaining Valid Lifetime of a prefix from 185078064Sume * which the address was derived through Stateless 185178064Sume * Autoconfiguration. 185278064Sume * 185378064Sume * Note that we currently do not support stateful 185478064Sume * address configuration by DHCPv6, so the former 185578064Sume * case can't happen. 185678064Sume */ 185778064Sume if (ifa6->ia6_lifetime.ia6t_expire == 0) 185878064Sume ltime = ND6_INFINITE_LIFETIME; 185978064Sume else { 186078064Sume if (ifa6->ia6_lifetime.ia6t_expire > 186178064Sume time_second) 186278064Sume ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second); 186378064Sume else 186478064Sume ltime = 0; 186578064Sume } 186678064Sume 186778064Sume bcopy(<ime, cp, sizeof(u_int32_t)); 186878064Sume cp += sizeof(u_int32_t); 186978064Sume 187078064Sume /* copy the address itself */ 187178064Sume bcopy(&ifa6->ia_addr.sin6_addr, cp, 187278064Sume sizeof(struct in6_addr)); 187378064Sume /* XXX: KAME link-local hack; remove ifindex */ 187478064Sume if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr)) 187578064Sume ((struct in6_addr *)cp)->s6_addr16[1] = 0; 187678064Sume cp += sizeof(struct in6_addr); 187778064Sume 187878064Sume resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); 187978064Sume copied += (sizeof(struct in6_addr) + 188078064Sume sizeof(u_int32_t)); 188153541Sshin } 188253541Sshin if (ifp0) /* we need search only on the specified IF */ 188353541Sshin break; 188453541Sshin } 188553541Sshin 188678064Sume if (allow_deprecated == 0 && ifp_dep != NULL) { 188778064Sume ifp = ifp_dep; 188878064Sume allow_deprecated = 1; 188978064Sume 189078064Sume goto again; 189178064Sume } 189278064Sume 1893108172Shsu IFNET_RUNLOCK(); 1894108172Shsu 189553541Sshin return(copied); 189653541Sshin} 189753541Sshin 189853541Sshin/* 189953541Sshin * XXX almost dup'ed code with rip6_input. 190053541Sshin */ 190153541Sshinstatic int 190253541Sshinicmp6_rip6_input(mp, off) 190353541Sshin struct mbuf **mp; 190453541Sshin int off; 190553541Sshin{ 190653541Sshin struct mbuf *m = *mp; 190778064Sume struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 190878064Sume struct in6pcb *in6p; 190953541Sshin struct in6pcb *last = NULL; 191053541Sshin struct sockaddr_in6 rip6src; 191153541Sshin struct icmp6_hdr *icmp6; 191253541Sshin struct mbuf *opts = NULL; 191353541Sshin 191462587Sitojun#ifndef PULLDOWN_TEST 191553541Sshin /* this is assumed to be safe. */ 191653541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 191762587Sitojun#else 191862587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 191962587Sitojun if (icmp6 == NULL) { 192062587Sitojun /* m is already reclaimed */ 192162587Sitojun return IPPROTO_DONE; 192262587Sitojun } 192362587Sitojun#endif 192453541Sshin 192553541Sshin bzero(&rip6src, sizeof(rip6src)); 192653541Sshin rip6src.sin6_len = sizeof(struct sockaddr_in6); 192753541Sshin rip6src.sin6_family = AF_INET6; 192862587Sitojun /* KAME hack: recover scopeid */ 192962587Sitojun (void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif); 193053541Sshin 193153541Sshin LIST_FOREACH(in6p, &ripcb, inp_list) 193253541Sshin { 193378064Sume if ((in6p->inp_vflag & INP_IPV6) == 0) 193453541Sshin continue; 193578064Sume#ifdef HAVE_NRL_INPCB 193678064Sume if (!(in6p->in6p_flags & INP_IPV6)) 193778064Sume continue; 193878064Sume#endif 193953541Sshin if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6) 194053541Sshin continue; 194153541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 194253541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 194353541Sshin continue; 194453541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 194553541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 194653541Sshin continue; 194753541Sshin if (in6p->in6p_icmp6filt 194853541Sshin && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, 194953541Sshin in6p->in6p_icmp6filt)) 195053541Sshin continue; 195153541Sshin if (last) { 195253541Sshin struct mbuf *n; 195353541Sshin if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 195453541Sshin if (last->in6p_flags & IN6P_CONTROLOPTS) 195553541Sshin ip6_savecontrol(last, &opts, ip6, n); 195653541Sshin /* strip intermediate headers */ 195753541Sshin m_adj(n, off); 195853541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 195953541Sshin (struct sockaddr *)&rip6src, 196053541Sshin n, opts) == 0) { 196153541Sshin /* should notify about lost packet */ 196253541Sshin m_freem(n); 196378064Sume if (opts) { 196453541Sshin m_freem(opts); 196578064Sume } 196697658Stanimura } else 196753541Sshin sorwakeup(last->in6p_socket); 196853541Sshin opts = NULL; 196953541Sshin } 197053541Sshin } 197153541Sshin last = in6p; 197253541Sshin } 197353541Sshin if (last) { 197453541Sshin if (last->in6p_flags & IN6P_CONTROLOPTS) 197553541Sshin ip6_savecontrol(last, &opts, ip6, m); 197653541Sshin /* strip intermediate headers */ 197753541Sshin m_adj(m, off); 197853541Sshin if (sbappendaddr(&last->in6p_socket->so_rcv, 197978064Sume (struct sockaddr *)&rip6src, m, opts) == 0) { 198053541Sshin m_freem(m); 198153541Sshin if (opts) 198253541Sshin m_freem(opts); 198397658Stanimura } else 198453541Sshin sorwakeup(last->in6p_socket); 198553541Sshin } else { 198653541Sshin m_freem(m); 198753541Sshin ip6stat.ip6s_delivered--; 198853541Sshin } 198953541Sshin return IPPROTO_DONE; 199053541Sshin} 199153541Sshin 199253541Sshin/* 199353541Sshin * Reflect the ip6 packet back to the source. 199462587Sitojun * OFF points to the icmp6 header, counted from the top of the mbuf. 199553541Sshin */ 199653541Sshinvoid 199753541Sshinicmp6_reflect(m, off) 199853541Sshin struct mbuf *m; 199953541Sshin size_t off; 200053541Sshin{ 200162587Sitojun struct ip6_hdr *ip6; 200253541Sshin struct icmp6_hdr *icmp6; 200353541Sshin struct in6_ifaddr *ia; 200453541Sshin struct in6_addr t, *src = 0; 200562587Sitojun int plen; 200653541Sshin int type, code; 200753541Sshin struct ifnet *outif = NULL; 200878064Sume struct sockaddr_in6 sa6_src, sa6_dst; 200953541Sshin#ifdef COMPAT_RFC1885 201053541Sshin int mtu = IPV6_MMTU; 201153541Sshin struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst; 201253541Sshin#endif 201353541Sshin 201462587Sitojun /* too short to reflect */ 201562587Sitojun if (off < sizeof(struct ip6_hdr)) { 201678064Sume nd6log((LOG_DEBUG, 201778064Sume "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 201878064Sume (u_long)off, (u_long)sizeof(struct ip6_hdr), 201978064Sume __FILE__, __LINE__)); 202062587Sitojun goto bad; 202162587Sitojun } 202262587Sitojun 202353541Sshin /* 202453541Sshin * If there are extra headers between IPv6 and ICMPv6, strip 202553541Sshin * off that header first. 202653541Sshin */ 202762587Sitojun#ifdef DIAGNOSTIC 202862587Sitojun if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) 202962587Sitojun panic("assumption failed in icmp6_reflect"); 203062587Sitojun#endif 203162587Sitojun if (off > sizeof(struct ip6_hdr)) { 203262587Sitojun size_t l; 203362587Sitojun struct ip6_hdr nip6; 203453541Sshin 203562587Sitojun l = off - sizeof(struct ip6_hdr); 203662587Sitojun m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); 203762587Sitojun m_adj(m, l); 203862587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 203962587Sitojun if (m->m_len < l) { 204062587Sitojun if ((m = m_pullup(m, l)) == NULL) 204162587Sitojun return; 204253541Sshin } 204362587Sitojun bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); 204462587Sitojun } else /* off == sizeof(struct ip6_hdr) */ { 204562587Sitojun size_t l; 204662587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 204762587Sitojun if (m->m_len < l) { 204862587Sitojun if ((m = m_pullup(m, l)) == NULL) 204962587Sitojun return; 205053541Sshin } 205153541Sshin } 205262587Sitojun plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 205362587Sitojun ip6 = mtod(m, struct ip6_hdr *); 205462587Sitojun ip6->ip6_nxt = IPPROTO_ICMPV6; 205553541Sshin icmp6 = (struct icmp6_hdr *)(ip6 + 1); 205653541Sshin type = icmp6->icmp6_type; /* keep type for statistics */ 205753541Sshin code = icmp6->icmp6_code; /* ditto. */ 205853541Sshin 205953541Sshin t = ip6->ip6_dst; 206053541Sshin /* 206153541Sshin * ip6_input() drops a packet if its src is multicast. 206253541Sshin * So, the src is never multicast. 206353541Sshin */ 206453541Sshin ip6->ip6_dst = ip6->ip6_src; 206553541Sshin 206678064Sume /* 206778064Sume * XXX: make sure to embed scope zone information, using 206878064Sume * already embedded IDs or the received interface (if any). 206978064Sume * Note that rcvif may be NULL. 207078064Sume * TODO: scoped routing case (XXX). 207178064Sume */ 207278064Sume bzero(&sa6_src, sizeof(sa6_src)); 207378064Sume sa6_src.sin6_family = AF_INET6; 207478064Sume sa6_src.sin6_len = sizeof(sa6_src); 207578064Sume sa6_src.sin6_addr = ip6->ip6_dst; 207678064Sume in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif); 207778064Sume in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL); 207878064Sume bzero(&sa6_dst, sizeof(sa6_dst)); 207978064Sume sa6_dst.sin6_family = AF_INET6; 208078064Sume sa6_dst.sin6_len = sizeof(sa6_dst); 208178064Sume sa6_dst.sin6_addr = t; 208278064Sume in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif); 208378064Sume in6_embedscope(&t, &sa6_dst, NULL, NULL); 208453541Sshin 208553541Sshin#ifdef COMPAT_RFC1885 208653541Sshin /* 208753541Sshin * xxx guess MTU 208853541Sshin * RFC 1885 requires that echo reply should be truncated if it 208953541Sshin * does not fit in with (return) path MTU, but the description was 209053541Sshin * removed in the new spec. 209153541Sshin */ 209253541Sshin if (icmp6_reflect_rt.ro_rt == 0 || 209353541Sshin ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) { 209453541Sshin if (icmp6_reflect_rt.ro_rt) { 209553541Sshin RTFREE(icmp6_reflect_rt.ro_rt); 209653541Sshin icmp6_reflect_rt.ro_rt = 0; 209753541Sshin } 209853541Sshin bzero(sin6, sizeof(*sin6)); 209953541Sshin sin6->sin6_family = PF_INET6; 210053541Sshin sin6->sin6_len = sizeof(struct sockaddr_in6); 210153541Sshin sin6->sin6_addr = ip6->ip6_dst; 210253541Sshin 210353541Sshin rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt, 210453541Sshin RTF_PRCLONING); 210553541Sshin } 210653541Sshin 210753541Sshin if (icmp6_reflect_rt.ro_rt == 0) 210853541Sshin goto bad; 210953541Sshin 211053541Sshin if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST) 211153541Sshin && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu) 211253541Sshin mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu; 211353541Sshin 211453541Sshin if (mtu < m->m_pkthdr.len) { 211553541Sshin plen -= (m->m_pkthdr.len - mtu); 211653541Sshin m_adj(m, mtu - m->m_pkthdr.len); 211753541Sshin } 211853541Sshin#endif 211953541Sshin /* 212053541Sshin * If the incoming packet was addressed directly to us(i.e. unicast), 212153541Sshin * use dst as the src for the reply. 212262587Sitojun * The IN6_IFF_NOTREADY case would be VERY rare, but is possible 212362587Sitojun * (for example) when we encounter an error while forwarding procedure 212462587Sitojun * destined to a duplicated address of ours. 212553541Sshin */ 212653541Sshin for (ia = in6_ifaddr; ia; ia = ia->ia_next) 212753541Sshin if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) && 212862587Sitojun (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) { 212953541Sshin src = &t; 213053541Sshin break; 213153541Sshin } 213253541Sshin if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) { 213353541Sshin /* 213453541Sshin * This is the case if the dst is our link-local address 213595023Ssuz * and the sender is also ourselves. 213653541Sshin */ 213753541Sshin src = &t; 213853541Sshin } 213953541Sshin 214078064Sume if (src == 0) { 214178064Sume int e; 214278064Sume struct route_in6 ro; 214378064Sume 214453541Sshin /* 214562587Sitojun * This case matches to multicasts, our anycast, or unicasts 214695023Ssuz * that we do not own. Select a source address based on the 214778064Sume * source address of the erroneous packet. 214853541Sshin */ 214978064Sume bzero(&ro, sizeof(ro)); 215078064Sume src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e); 215178064Sume if (ro.ro_rt) 215278064Sume RTFREE(ro.ro_rt); /* XXX: we could use this */ 215378064Sume if (src == NULL) { 215478064Sume nd6log((LOG_DEBUG, 215578064Sume "icmp6_reflect: source can't be determined: " 215678064Sume "dst=%s, error=%d\n", 215778064Sume ip6_sprintf(&sa6_src.sin6_addr), e)); 215878064Sume goto bad; 215978064Sume } 216078064Sume } 216153541Sshin 216253541Sshin ip6->ip6_src = *src; 216353541Sshin 216453541Sshin ip6->ip6_flow = 0; 216562587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 216662587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 216753541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 216853541Sshin if (m->m_pkthdr.rcvif) { 216953541Sshin /* XXX: This may not be the outgoing interface */ 217053541Sshin ip6->ip6_hlim = nd_ifinfo[m->m_pkthdr.rcvif->if_index].chlim; 217178064Sume } else 217278064Sume ip6->ip6_hlim = ip6_defhlim; 217353541Sshin 217453541Sshin icmp6->icmp6_cksum = 0; 217553541Sshin icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, 217653541Sshin sizeof(struct ip6_hdr), plen); 217753541Sshin 217853541Sshin /* 217978064Sume * XXX option handling 218053541Sshin */ 218153541Sshin 218253541Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 218353541Sshin 218453541Sshin#ifdef COMPAT_RFC1885 2185105194Ssam ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL); 218653541Sshin#else 2187105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 218853541Sshin#endif 218953541Sshin if (outif) 219053541Sshin icmp6_ifoutstat_inc(outif, type, code); 219153541Sshin 219253541Sshin return; 219353541Sshin 219453541Sshin bad: 219553541Sshin m_freem(m); 219653541Sshin return; 219753541Sshin} 219853541Sshin 219953541Sshinvoid 220053541Sshinicmp6_fasttimo() 220153541Sshin{ 220262587Sitojun 220353541Sshin mld6_fasttimeo(); 220453541Sshin} 220553541Sshin 220653541Sshinstatic const char * 220753541Sshinicmp6_redirect_diag(src6, dst6, tgt6) 220853541Sshin struct in6_addr *src6; 220953541Sshin struct in6_addr *dst6; 221053541Sshin struct in6_addr *tgt6; 221153541Sshin{ 221253541Sshin static char buf[1024]; 221353541Sshin snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", 221453541Sshin ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6)); 221553541Sshin return buf; 221653541Sshin} 221753541Sshin 221853541Sshinvoid 221953541Sshinicmp6_redirect_input(m, off) 222078064Sume struct mbuf *m; 222153541Sshin int off; 222253541Sshin{ 222353541Sshin struct ifnet *ifp = m->m_pkthdr.rcvif; 222453541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 222562587Sitojun struct nd_redirect *nd_rd; 222653541Sshin int icmp6len = ntohs(ip6->ip6_plen); 222753541Sshin char *lladdr = NULL; 222853541Sshin int lladdrlen = 0; 222953541Sshin u_char *redirhdr = NULL; 223053541Sshin int redirhdrlen = 0; 223153541Sshin struct rtentry *rt = NULL; 223253541Sshin int is_router; 223353541Sshin int is_onlink; 223453541Sshin struct in6_addr src6 = ip6->ip6_src; 223562587Sitojun struct in6_addr redtgt6; 223662587Sitojun struct in6_addr reddst6; 223753541Sshin union nd_opts ndopts; 223853541Sshin 223953541Sshin if (!m || !ifp) 224053541Sshin return; 224153541Sshin 224253541Sshin /* XXX if we are router, we don't update route by icmp6 redirect */ 224353541Sshin if (ip6_forwarding) 224462587Sitojun goto freeit; 224553541Sshin if (!icmp6_rediraccept) 224662587Sitojun goto freeit; 224762587Sitojun 224862587Sitojun#ifndef PULLDOWN_TEST 224962587Sitojun IP6_EXTHDR_CHECK(m, off, icmp6len,); 225062587Sitojun nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); 225162587Sitojun#else 225262587Sitojun IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); 225362587Sitojun if (nd_rd == NULL) { 225462587Sitojun icmp6stat.icp6s_tooshort++; 225553541Sshin return; 225662587Sitojun } 225762587Sitojun#endif 225862587Sitojun redtgt6 = nd_rd->nd_rd_target; 225962587Sitojun reddst6 = nd_rd->nd_rd_dst; 226053541Sshin 226153541Sshin if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 226253541Sshin redtgt6.s6_addr16[1] = htons(ifp->if_index); 226353541Sshin if (IN6_IS_ADDR_LINKLOCAL(&reddst6)) 226453541Sshin reddst6.s6_addr16[1] = htons(ifp->if_index); 226553541Sshin 226653541Sshin /* validation */ 226753541Sshin if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 226878064Sume nd6log((LOG_ERR, 226953541Sshin "ICMP6 redirect sent from %s rejected; " 227078064Sume "must be from linklocal\n", ip6_sprintf(&src6))); 227178064Sume goto bad; 227253541Sshin } 227353541Sshin if (ip6->ip6_hlim != 255) { 227478064Sume nd6log((LOG_ERR, 227553541Sshin "ICMP6 redirect sent from %s rejected; " 227653541Sshin "hlim=%d (must be 255)\n", 227778064Sume ip6_sprintf(&src6), ip6->ip6_hlim)); 227878064Sume goto bad; 227953541Sshin } 228053541Sshin { 228153541Sshin /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 228253541Sshin struct sockaddr_in6 sin6; 228353541Sshin struct in6_addr *gw6; 228453541Sshin 228553541Sshin bzero(&sin6, sizeof(sin6)); 228653541Sshin sin6.sin6_family = AF_INET6; 228753541Sshin sin6.sin6_len = sizeof(struct sockaddr_in6); 228853541Sshin bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); 228953541Sshin rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 229053541Sshin if (rt) { 229165895Sume if (rt->rt_gateway == NULL || 229265895Sume rt->rt_gateway->sa_family != AF_INET6) { 229378064Sume nd6log((LOG_ERR, 229465895Sume "ICMP6 redirect rejected; no route " 229565895Sume "with inet6 gateway found for redirect dst: %s\n", 229678064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 229765895Sume RTFREE(rt); 229878064Sume goto bad; 229965895Sume } 230065895Sume 230153541Sshin gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); 230253541Sshin if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { 230378064Sume nd6log((LOG_ERR, 230453541Sshin "ICMP6 redirect rejected; " 230553541Sshin "not equal to gw-for-src=%s (must be same): " 230653541Sshin "%s\n", 230753541Sshin ip6_sprintf(gw6), 230878064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 230953541Sshin RTFREE(rt); 231078064Sume goto bad; 231153541Sshin } 231253541Sshin } else { 231378064Sume nd6log((LOG_ERR, 231453541Sshin "ICMP6 redirect rejected; " 231553541Sshin "no route found for redirect dst: %s\n", 231678064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 231778064Sume goto bad; 231853541Sshin } 231953541Sshin RTFREE(rt); 232053541Sshin rt = NULL; 232153541Sshin } 232253541Sshin if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 232378064Sume nd6log((LOG_ERR, 232453541Sshin "ICMP6 redirect rejected; " 232553541Sshin "redirect dst must be unicast: %s\n", 232678064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 232778064Sume goto bad; 232853541Sshin } 232953541Sshin 233053541Sshin is_router = is_onlink = 0; 233153541Sshin if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 233253541Sshin is_router = 1; /* router case */ 233353541Sshin if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 233453541Sshin is_onlink = 1; /* on-link destination case */ 233553541Sshin if (!is_router && !is_onlink) { 233678064Sume nd6log((LOG_ERR, 233753541Sshin "ICMP6 redirect rejected; " 233853541Sshin "neither router case nor onlink case: %s\n", 233978064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 234078064Sume goto bad; 234153541Sshin } 234253541Sshin /* validation passed */ 234353541Sshin 234453541Sshin icmp6len -= sizeof(*nd_rd); 234553541Sshin nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 234653541Sshin if (nd6_options(&ndopts) < 0) { 234778064Sume nd6log((LOG_INFO, "icmp6_redirect_input: " 234853541Sshin "invalid ND option, rejected: %s\n", 234978064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 235078064Sume /* nd6_options have incremented stats */ 235162587Sitojun goto freeit; 235253541Sshin } 235353541Sshin 235453541Sshin if (ndopts.nd_opts_tgt_lladdr) { 235553541Sshin lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 235653541Sshin lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 235753541Sshin } 235853541Sshin 235953541Sshin if (ndopts.nd_opts_rh) { 236053541Sshin redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len; 236153541Sshin redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */ 236253541Sshin } 236353541Sshin 236453541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 236578064Sume nd6log((LOG_INFO, 236653541Sshin "icmp6_redirect_input: lladdrlen mismatch for %s " 236753541Sshin "(if %d, icmp6 packet %d): %s\n", 236853541Sshin ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2, 236978064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 237078064Sume goto bad; 237153541Sshin } 237253541Sshin 237353541Sshin /* RFC 2461 8.3 */ 237453541Sshin nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 237553541Sshin is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 237653541Sshin 237795023Ssuz if (!is_onlink) { /* better router case. perform rtredirect. */ 237853541Sshin /* perform rtredirect */ 237953541Sshin struct sockaddr_in6 sdst; 238053541Sshin struct sockaddr_in6 sgw; 238153541Sshin struct sockaddr_in6 ssrc; 238253541Sshin 238353541Sshin bzero(&sdst, sizeof(sdst)); 238453541Sshin bzero(&sgw, sizeof(sgw)); 238553541Sshin bzero(&ssrc, sizeof(ssrc)); 238653541Sshin sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; 238753541Sshin sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = 238853541Sshin sizeof(struct sockaddr_in6); 238953541Sshin bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); 239053541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 239153541Sshin bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 239253541Sshin rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw, 239353541Sshin (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST, 239453541Sshin (struct sockaddr *)&ssrc, 239553541Sshin (struct rtentry **)NULL); 239653541Sshin } 239753541Sshin /* finally update cached route in each socket via pfctlinput */ 239853541Sshin { 239953541Sshin struct sockaddr_in6 sdst; 240053541Sshin 240153541Sshin bzero(&sdst, sizeof(sdst)); 240253541Sshin sdst.sin6_family = AF_INET6; 240353541Sshin sdst.sin6_len = sizeof(struct sockaddr_in6); 240453541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 240553541Sshin pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); 240653541Sshin#ifdef IPSEC 240753541Sshin key_sa_routechange((struct sockaddr *)&sdst); 240853541Sshin#endif 240953541Sshin } 241062587Sitojun 241162587Sitojun freeit: 241262587Sitojun m_freem(m); 241378064Sume return; 241478064Sume 241578064Sume bad: 241678064Sume icmp6stat.icp6s_badredirect++; 241778064Sume m_freem(m); 241853541Sshin} 241953541Sshin 242053541Sshinvoid 242153541Sshinicmp6_redirect_output(m0, rt) 242253541Sshin struct mbuf *m0; 242353541Sshin struct rtentry *rt; 242453541Sshin{ 242553541Sshin struct ifnet *ifp; /* my outgoing interface */ 242653541Sshin struct in6_addr *ifp_ll6; 242753541Sshin struct in6_addr *router_ll6; 242853541Sshin struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 242953541Sshin struct mbuf *m = NULL; /* newly allocated one */ 243053541Sshin struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 243153541Sshin struct nd_redirect *nd_rd; 243253541Sshin size_t maxlen; 243353541Sshin u_char *p; 243453541Sshin struct ifnet *outif = NULL; 243562587Sitojun struct sockaddr_in6 src_sa; 243653541Sshin 243762587Sitojun icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0); 243862587Sitojun 243953541Sshin /* if we are not router, we don't send icmp6 redirect */ 244053541Sshin if (!ip6_forwarding || ip6_accept_rtadv) 244153541Sshin goto fail; 244253541Sshin 244353541Sshin /* sanity check */ 244453541Sshin if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) 244553541Sshin goto fail; 244653541Sshin 244753541Sshin /* 244853541Sshin * Address check: 244953541Sshin * the source address must identify a neighbor, and 245053541Sshin * the destination address must not be a multicast address 245153541Sshin * [RFC 2461, sec 8.2] 245253541Sshin */ 245353541Sshin sip6 = mtod(m0, struct ip6_hdr *); 245462587Sitojun bzero(&src_sa, sizeof(src_sa)); 245562587Sitojun src_sa.sin6_family = AF_INET6; 245662587Sitojun src_sa.sin6_len = sizeof(src_sa); 245762587Sitojun src_sa.sin6_addr = sip6->ip6_src; 245862587Sitojun /* we don't currently use sin6_scope_id, but eventually use it */ 245962587Sitojun src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src); 246062587Sitojun if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) 246153541Sshin goto fail; 246253541Sshin if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) 246353541Sshin goto fail; /* what should we do here? */ 246453541Sshin 246553541Sshin /* rate limit */ 246653541Sshin if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) 246753541Sshin goto fail; 246853541Sshin 246953541Sshin /* 247053541Sshin * Since we are going to append up to 1280 bytes (= IPV6_MMTU), 247153541Sshin * we almost always ask for an mbuf cluster for simplicity. 247253541Sshin * (MHLEN < IPV6_MMTU is almost always true) 247353541Sshin */ 247462587Sitojun#if IPV6_MMTU >= MCLBYTES 247562587Sitojun# error assumption failed about IPV6_MMTU and MCLBYTES 247662587Sitojun#endif 247753541Sshin MGETHDR(m, M_DONTWAIT, MT_HEADER); 247862587Sitojun if (m && IPV6_MMTU >= MHLEN) 247962587Sitojun MCLGET(m, M_DONTWAIT); 248053541Sshin if (!m) 248153541Sshin goto fail; 248278064Sume m->m_pkthdr.rcvif = NULL; 248378064Sume m->m_len = 0; 248478064Sume maxlen = M_TRAILINGSPACE(m); 248553541Sshin maxlen = min(IPV6_MMTU, maxlen); 248653541Sshin /* just for safety */ 248762587Sitojun if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 248862587Sitojun ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { 248953541Sshin goto fail; 249062587Sitojun } 249153541Sshin 249253541Sshin { 249353541Sshin /* get ip6 linklocal address for ifp(my outgoing interface). */ 249462587Sitojun struct in6_ifaddr *ia; 249562587Sitojun if ((ia = in6ifa_ifpforlinklocal(ifp, 249662587Sitojun IN6_IFF_NOTREADY| 249762587Sitojun IN6_IFF_ANYCAST)) == NULL) 249853541Sshin goto fail; 249953541Sshin ifp_ll6 = &ia->ia_addr.sin6_addr; 250053541Sshin } 250153541Sshin 250253541Sshin /* get ip6 linklocal address for the router. */ 250353541Sshin if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { 250453541Sshin struct sockaddr_in6 *sin6; 250553541Sshin sin6 = (struct sockaddr_in6 *)rt->rt_gateway; 250653541Sshin router_ll6 = &sin6->sin6_addr; 250753541Sshin if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) 250853541Sshin router_ll6 = (struct in6_addr *)NULL; 250953541Sshin } else 251053541Sshin router_ll6 = (struct in6_addr *)NULL; 251153541Sshin 251253541Sshin /* ip6 */ 251353541Sshin ip6 = mtod(m, struct ip6_hdr *); 251453541Sshin ip6->ip6_flow = 0; 251562587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 251662587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 251753541Sshin /* ip6->ip6_plen will be set later */ 251853541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 251953541Sshin ip6->ip6_hlim = 255; 252053541Sshin /* ip6->ip6_src must be linklocal addr for my outgoing if. */ 252153541Sshin bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); 252253541Sshin bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); 252353541Sshin 252453541Sshin /* ND Redirect */ 252553541Sshin nd_rd = (struct nd_redirect *)(ip6 + 1); 252653541Sshin nd_rd->nd_rd_type = ND_REDIRECT; 252753541Sshin nd_rd->nd_rd_code = 0; 252853541Sshin nd_rd->nd_rd_reserved = 0; 252953541Sshin if (rt->rt_flags & RTF_GATEWAY) { 253053541Sshin /* 253153541Sshin * nd_rd->nd_rd_target must be a link-local address in 253253541Sshin * better router cases. 253353541Sshin */ 253453541Sshin if (!router_ll6) 253553541Sshin goto fail; 253653541Sshin bcopy(router_ll6, &nd_rd->nd_rd_target, 253753541Sshin sizeof(nd_rd->nd_rd_target)); 253853541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 253953541Sshin sizeof(nd_rd->nd_rd_dst)); 254053541Sshin } else { 254153541Sshin /* make sure redtgt == reddst */ 254253541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, 254353541Sshin sizeof(nd_rd->nd_rd_target)); 254453541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 254553541Sshin sizeof(nd_rd->nd_rd_dst)); 254653541Sshin } 254753541Sshin 254853541Sshin p = (u_char *)(nd_rd + 1); 254953541Sshin 255053541Sshin if (!router_ll6) 255153541Sshin goto nolladdropt; 255253541Sshin 255353541Sshin { 255453541Sshin /* target lladdr option */ 255553541Sshin struct rtentry *rt_router = NULL; 255653541Sshin int len; 255753541Sshin struct sockaddr_dl *sdl; 255853541Sshin struct nd_opt_hdr *nd_opt; 255953541Sshin char *lladdr; 256053541Sshin 256153541Sshin rt_router = nd6_lookup(router_ll6, 0, ifp); 256253541Sshin if (!rt_router) 256353541Sshin goto nolladdropt; 256462587Sitojun len = sizeof(*nd_opt) + ifp->if_addrlen; 256595023Ssuz len = (len + 7) & ~7; /* round by 8 */ 256662587Sitojun /* safety check */ 256762587Sitojun if (len + (p - (u_char *)ip6) > maxlen) 256862587Sitojun goto nolladdropt; 256962587Sitojun if (!(rt_router->rt_flags & RTF_GATEWAY) && 257062587Sitojun (rt_router->rt_flags & RTF_LLINFO) && 257162587Sitojun (rt_router->rt_gateway->sa_family == AF_LINK) && 257262587Sitojun (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) && 257362587Sitojun sdl->sdl_alen) { 257453541Sshin nd_opt = (struct nd_opt_hdr *)p; 257553541Sshin nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 257653541Sshin nd_opt->nd_opt_len = len >> 3; 257753541Sshin lladdr = (char *)(nd_opt + 1); 257853541Sshin bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen); 257962587Sitojun p += len; 258053541Sshin } 258153541Sshin } 258253541Sshinnolladdropt:; 258353541Sshin 258453541Sshin m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 258553541Sshin 258653541Sshin /* just to be safe */ 258762587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 258853541Sshin if (m0->m_flags & M_DECRYPTED) 258953541Sshin goto noredhdropt; 259062587Sitojun#endif 259162587Sitojun if (p - (u_char *)ip6 > maxlen) 259262587Sitojun goto noredhdropt; 259353541Sshin 259453541Sshin { 259553541Sshin /* redirected header option */ 259653541Sshin int len; 259753541Sshin struct nd_opt_rd_hdr *nd_opt_rh; 259853541Sshin 259953541Sshin /* 260053541Sshin * compute the maximum size for icmp6 redirect header option. 260153541Sshin * XXX room for auth header? 260253541Sshin */ 260353541Sshin len = maxlen - (p - (u_char *)ip6); 260453541Sshin len &= ~7; 260553541Sshin 260653541Sshin /* This is just for simplicity. */ 260753541Sshin if (m0->m_pkthdr.len != m0->m_len) { 260853541Sshin if (m0->m_next) { 260953541Sshin m_freem(m0->m_next); 261053541Sshin m0->m_next = NULL; 261153541Sshin } 261253541Sshin m0->m_pkthdr.len = m0->m_len; 261353541Sshin } 261453541Sshin 261553541Sshin /* 261653541Sshin * Redirected header option spec (RFC2461 4.6.3) talks nothing 261753541Sshin * about padding/truncate rule for the original IP packet. 261853541Sshin * From the discussion on IPv6imp in Feb 1999, the consensus was: 261953541Sshin * - "attach as much as possible" is the goal 262053541Sshin * - pad if not aligned (original size can be guessed by original 262153541Sshin * ip6 header) 262253541Sshin * Following code adds the padding if it is simple enough, 262353541Sshin * and truncates if not. 262453541Sshin */ 262553541Sshin if (m0->m_next || m0->m_pkthdr.len != m0->m_len) 262653541Sshin panic("assumption failed in %s:%d\n", __FILE__, __LINE__); 262753541Sshin 262853541Sshin if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { 262953541Sshin /* not enough room, truncate */ 263053541Sshin m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh); 263153541Sshin } else { 263253541Sshin /* enough room, pad or truncate */ 263353541Sshin size_t extra; 263453541Sshin 263553541Sshin extra = m0->m_pkthdr.len % 8; 263653541Sshin if (extra) { 263753541Sshin /* pad if easy enough, truncate if not */ 263853541Sshin if (8 - extra <= M_TRAILINGSPACE(m0)) { 263953541Sshin /* pad */ 264053541Sshin m0->m_len += (8 - extra); 264153541Sshin m0->m_pkthdr.len += (8 - extra); 264253541Sshin } else { 264353541Sshin /* truncate */ 264453541Sshin m0->m_pkthdr.len -= extra; 264553541Sshin m0->m_len -= extra; 264653541Sshin } 264753541Sshin } 264853541Sshin len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); 264953541Sshin m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh); 265053541Sshin } 265153541Sshin 265253541Sshin nd_opt_rh = (struct nd_opt_rd_hdr *)p; 265353541Sshin bzero(nd_opt_rh, sizeof(*nd_opt_rh)); 265453541Sshin nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; 265553541Sshin nd_opt_rh->nd_opt_rh_len = len >> 3; 265653541Sshin p += sizeof(*nd_opt_rh); 265753541Sshin m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 265853541Sshin 265953541Sshin /* connect m0 to m */ 266053541Sshin m->m_next = m0; 266153541Sshin m->m_pkthdr.len = m->m_len + m0->m_len; 266253541Sshin } 266353541Sshinnoredhdropt:; 266453541Sshin 266553541Sshin if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src)) 266653541Sshin sip6->ip6_src.s6_addr16[1] = 0; 266753541Sshin if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst)) 266853541Sshin sip6->ip6_dst.s6_addr16[1] = 0; 266962587Sitojun#if 0 267062587Sitojun if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) 267162587Sitojun ip6->ip6_src.s6_addr16[1] = 0; 267262587Sitojun if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) 267362587Sitojun ip6->ip6_dst.s6_addr16[1] = 0; 267462587Sitojun#endif 267553541Sshin if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target)) 267653541Sshin nd_rd->nd_rd_target.s6_addr16[1] = 0; 267753541Sshin if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst)) 267853541Sshin nd_rd->nd_rd_dst.s6_addr16[1] = 0; 267953541Sshin 268053541Sshin ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 268153541Sshin 268253541Sshin nd_rd->nd_rd_cksum = 0; 268353541Sshin nd_rd->nd_rd_cksum 268453541Sshin = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen)); 268553541Sshin 268653541Sshin /* send the packet to outside... */ 2687105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 268853541Sshin if (outif) { 268953541Sshin icmp6_ifstat_inc(outif, ifs6_out_msg); 269053541Sshin icmp6_ifstat_inc(outif, ifs6_out_redirect); 269153541Sshin } 269253541Sshin icmp6stat.icp6s_outhist[ND_REDIRECT]++; 269353541Sshin 269453541Sshin return; 269553541Sshin 269653541Sshinfail: 269753541Sshin if (m) 269853541Sshin m_freem(m); 269953541Sshin if (m0) 270053541Sshin m_freem(m0); 270153541Sshin} 270253541Sshin 270378064Sume#ifdef HAVE_NRL_INPCB 270478064Sume#define sotoin6pcb sotoinpcb 270578064Sume#define in6pcb inpcb 270678064Sume#define in6p_icmp6filt inp_icmp6filt 270778064Sume#endif 270853541Sshin/* 270953541Sshin * ICMPv6 socket option processing. 271053541Sshin */ 271153541Sshinint 271253541Sshinicmp6_ctloutput(so, sopt) 271353541Sshin struct socket *so; 271453541Sshin struct sockopt *sopt; 271553541Sshin{ 271653541Sshin int error = 0; 271753541Sshin int optlen; 271878064Sume struct inpcb *inp = sotoinpcb(so); 271953541Sshin int level, op, optname; 272053541Sshin 272153541Sshin if (sopt) { 272253541Sshin level = sopt->sopt_level; 272353541Sshin op = sopt->sopt_dir; 272453541Sshin optname = sopt->sopt_name; 272553541Sshin optlen = sopt->sopt_valsize; 272653541Sshin } else 272753541Sshin level = op = optname = optlen = 0; 272878064Sume 272953541Sshin if (level != IPPROTO_ICMPV6) { 273053541Sshin return EINVAL; 273153541Sshin } 273253541Sshin 273378064Sume switch (op) { 273453541Sshin case PRCO_SETOPT: 273553541Sshin switch (optname) { 273653541Sshin case ICMP6_FILTER: 273753541Sshin { 273853541Sshin struct icmp6_filter *p; 273953541Sshin 274053541Sshin if (optlen != sizeof(*p)) { 274153541Sshin error = EMSGSIZE; 274253541Sshin break; 274353541Sshin } 274453541Sshin if (inp->in6p_icmp6filt == NULL) { 274553541Sshin error = EINVAL; 274653541Sshin break; 274753541Sshin } 274853541Sshin error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen, 274953541Sshin optlen); 275053541Sshin break; 275153541Sshin } 275253541Sshin 275353541Sshin default: 275453541Sshin error = ENOPROTOOPT; 275553541Sshin break; 275653541Sshin } 275753541Sshin break; 275853541Sshin 275953541Sshin case PRCO_GETOPT: 276053541Sshin switch (optname) { 276153541Sshin case ICMP6_FILTER: 276253541Sshin { 276353541Sshin if (inp->in6p_icmp6filt == NULL) { 276453541Sshin error = EINVAL; 276553541Sshin break; 276653541Sshin } 276753541Sshin error = sooptcopyout(sopt, inp->in6p_icmp6filt, 276853541Sshin sizeof(struct icmp6_filter)); 276953541Sshin break; 277053541Sshin } 277153541Sshin 277253541Sshin default: 277353541Sshin error = ENOPROTOOPT; 277453541Sshin break; 277553541Sshin } 277653541Sshin break; 277753541Sshin } 277853541Sshin 277953541Sshin return(error); 278053541Sshin} 278178064Sume#ifdef HAVE_NRL_INPCB 278278064Sume#undef sotoin6pcb 278378064Sume#undef in6pcb 278478064Sume#undef in6p_icmp6filt 278578064Sume#endif 278653541Sshin 278778064Sume#ifndef HAVE_PPSRATECHECK 278878064Sume#ifndef timersub 278978064Sume#define timersub(tvp, uvp, vvp) \ 279078064Sume do { \ 279178064Sume (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 279278064Sume (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 279378064Sume if ((vvp)->tv_usec < 0) { \ 279478064Sume (vvp)->tv_sec--; \ 279578064Sume (vvp)->tv_usec += 1000000; \ 279678064Sume } \ 279778064Sume } while (0) 279878064Sume#endif 279978064Sume 280053541Sshin/* 280178064Sume * ppsratecheck(): packets (or events) per second limitation. 280262587Sitojun */ 280362587Sitojunstatic int 280478064Sumeppsratecheck(lasttime, curpps, maxpps) 280578064Sume struct timeval *lasttime; 280678064Sume int *curpps; 280778064Sume int maxpps; /* maximum pps allowed */ 280862587Sitojun{ 280978064Sume struct timeval tv, delta; 281078064Sume int s, rv; 281162587Sitojun 281278064Sume s = splclock(); 281378064Sume microtime(&tv); 281478064Sume splx(s); 281562587Sitojun 281678064Sume timersub(&tv, lasttime, &delta); 281762587Sitojun 281878064Sume /* 281995023Ssuz * Check for 0,0 so that the message will be seen at least once. 282095023Ssuz * If more than one second has passed since the last update of 282178064Sume * lasttime, reset the counter. 282278064Sume * 282395023Ssuz * We do increment *curpps even in *curpps < maxpps case, as some may 282478064Sume * try to use *curpps for stat purposes as well. 282578064Sume */ 282678064Sume if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) || 282778064Sume delta.tv_sec >= 1) { 282878064Sume *lasttime = tv; 282978064Sume *curpps = 0; 283078064Sume rv = 1; 283178064Sume } else if (maxpps < 0) 283278064Sume rv = 1; 283378064Sume else if (*curpps < maxpps) 283478064Sume rv = 1; 283578064Sume else 283678064Sume rv = 0; 283762587Sitojun 283895023Ssuz#if 1 /* DIAGNOSTIC? */ 283978064Sume /* be careful about wrap-around */ 284078064Sume if (*curpps + 1 > *curpps) 284178064Sume *curpps = *curpps + 1; 284278064Sume#else 284378064Sume /* 284478064Sume * assume that there's not too many calls to this function. 284578064Sume * not sure if the assumption holds, as it depends on *caller's* 284678064Sume * behavior, not the behavior of this function. 284778064Sume * IMHO it is wrong to make assumption on the caller's behavior, 284878064Sume * so the above #if is #if 1, not #ifdef DIAGNOSTIC. 284978064Sume */ 285078064Sume *curpps = *curpps + 1; 285178064Sume#endif 285278064Sume 285378064Sume return (rv); 285462587Sitojun} 285562587Sitojun#endif 285662587Sitojun 285762587Sitojun/* 285853541Sshin * Perform rate limit check. 285953541Sshin * Returns 0 if it is okay to send the icmp6 packet. 286053541Sshin * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 286153541Sshin * limitation. 286253541Sshin * 286353541Sshin * XXX per-destination/type check necessary? 286453541Sshin */ 286553541Sshinstatic int 286653541Sshinicmp6_ratelimit(dst, type, code) 286753541Sshin const struct in6_addr *dst; /* not used at this moment */ 286853541Sshin const int type; /* not used at this moment */ 286953541Sshin const int code; /* not used at this moment */ 287053541Sshin{ 287162587Sitojun int ret; 287253541Sshin 287395023Ssuz ret = 0; /* okay to send */ 287453541Sshin 287562587Sitojun /* PPS limit */ 287678064Sume if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count, 287778064Sume icmp6errppslim)) { 287853541Sshin /* The packet is subject to rate limit */ 287962587Sitojun ret++; 288053541Sshin } 288153541Sshin 288262587Sitojun return ret; 288353541Sshin} 2884