icmp6.c revision 215701
1139826Simp/*- 253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 353541Sshin * All rights reserved. 453541Sshin * 553541Sshin * Redistribution and use in source and binary forms, with or without 653541Sshin * modification, are permitted provided that the following conditions 753541Sshin * are met: 853541Sshin * 1. Redistributions of source code must retain the above copyright 953541Sshin * notice, this list of conditions and the following disclaimer. 1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1153541Sshin * notice, this list of conditions and the following disclaimer in the 1253541Sshin * documentation and/or other materials provided with the distribution. 1353541Sshin * 3. Neither the name of the project nor the names of its contributors 1453541Sshin * may be used to endorse or promote products derived from this software 1553541Sshin * without specific prior written permission. 1653541Sshin * 1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2053541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2753541Sshin * SUCH DAMAGE. 28174510Sobrien * 29174510Sobrien * $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ 3053541Sshin */ 3153541Sshin 32139826Simp/*- 3353541Sshin * Copyright (c) 1982, 1986, 1988, 1993 3453541Sshin * The Regents of the University of California. All rights reserved. 3553541Sshin * 3653541Sshin * Redistribution and use in source and binary forms, with or without 3753541Sshin * modification, are permitted provided that the following conditions 3853541Sshin * are met: 3953541Sshin * 1. Redistributions of source code must retain the above copyright 4053541Sshin * notice, this list of conditions and the following disclaimer. 4153541Sshin * 2. Redistributions in binary form must reproduce the above copyright 4253541Sshin * notice, this list of conditions and the following disclaimer in the 4353541Sshin * documentation and/or other materials provided with the distribution. 4453541Sshin * 4. Neither the name of the University nor the names of its contributors 4553541Sshin * may be used to endorse or promote products derived from this software 4653541Sshin * without specific prior written permission. 4753541Sshin * 4853541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 4953541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5053541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5153541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5253541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5353541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5453541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5553541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5653541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5753541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5853541Sshin * SUCH DAMAGE. 5953541Sshin * 6053541Sshin * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 6153541Sshin */ 6253541Sshin 63174510Sobrien#include <sys/cdefs.h> 64174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/icmp6.c 215701 2010-11-22 19:32:54Z dim $"); 65174510Sobrien 6662587Sitojun#include "opt_inet.h" 6762587Sitojun#include "opt_inet6.h" 6855009Sshin#include "opt_ipsec.h" 6953541Sshin 7053541Sshin#include <sys/param.h> 7195759Stanimura#include <sys/domain.h> 72193066Sjamie#include <sys/jail.h> 7395759Stanimura#include <sys/kernel.h> 7495759Stanimura#include <sys/lock.h> 7578064Sume#include <sys/malloc.h> 7653541Sshin#include <sys/mbuf.h> 77196019Srwatson#include <sys/proc.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> 89186119Sqingli#include <net/if_llatbl.h> 9053541Sshin#include <net/if_types.h> 9195759Stanimura#include <net/route.h> 92185571Sbz#include <net/vnet.h> 9353541Sshin 9453541Sshin#include <netinet/in.h> 9595759Stanimura#include <netinet/in_pcb.h> 9653541Sshin#include <netinet/in_var.h> 9762587Sitojun#include <netinet/ip6.h> 9895759Stanimura#include <netinet/icmp6.h> 99122922Sandre#include <netinet/tcp_var.h> 100185571Sbz 10195759Stanimura#include <netinet6/in6_ifattach.h> 10295759Stanimura#include <netinet6/in6_pcb.h> 10395759Stanimura#include <netinet6/ip6protosw.h> 10453541Sshin#include <netinet6/ip6_var.h> 105148385Sume#include <netinet6/scope6_var.h> 10653541Sshin#include <netinet6/mld6_var.h> 10753541Sshin#include <netinet6/nd6.h> 108211501Sanchie#include <netinet6/send.h> 10953541Sshin 110171167Sgnn#ifdef IPSEC 111105199Ssam#include <netipsec/ipsec.h> 112105199Ssam#include <netipsec/key.h> 113105199Ssam#endif 114105199Ssam 11562587Sitojunextern struct domain inet6domain; 116185348Szec 117207369SbzVNET_DEFINE(struct icmp6stat, icmp6stat); 118207369Sbz 119195699SrwatsonVNET_DECLARE(struct inpcbinfo, ripcbinfo); 120195699SrwatsonVNET_DECLARE(struct inpcbhead, ripcb); 121195699SrwatsonVNET_DECLARE(int, icmp6errppslim); 122215701Sdimstatic VNET_DEFINE(int, icmp6errpps_count) = 0; 123215701Sdimstatic VNET_DEFINE(struct timeval, icmp6errppslim_last); 124195699SrwatsonVNET_DECLARE(int, icmp6_nodeinfo); 12553541Sshin 126195727Srwatson#define V_ripcbinfo VNET(ripcbinfo) 127195727Srwatson#define V_ripcb VNET(ripcb) 128195727Srwatson#define V_icmp6errppslim VNET(icmp6errppslim) 129207369Sbz#define V_icmp6errpps_count VNET(icmp6errpps_count) 130207369Sbz#define V_icmp6errppslim_last VNET(icmp6errppslim_last) 131195727Srwatson#define V_icmp6_nodeinfo VNET(icmp6_nodeinfo) 132185088Szec 133175162Sobrienstatic void icmp6_errcount(struct icmp6errstat *, int, int); 134175162Sobrienstatic int icmp6_rip6_input(struct mbuf **, int); 135175162Sobrienstatic int icmp6_ratelimit(const struct in6_addr *, const int, const int); 13662587Sitojunstatic const char *icmp6_redirect_diag __P((struct in6_addr *, 13762587Sitojun struct in6_addr *, struct in6_addr *)); 138175162Sobrienstatic struct mbuf *ni6_input(struct mbuf *, int); 139175162Sobrienstatic struct mbuf *ni6_nametodns(const char *, int, int); 140175162Sobrienstatic int ni6_dnsmatch(const char *, int, const char *, int); 14162587Sitojunstatic int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *, 142148892Sume struct ifnet **, struct in6_addr *)); 14362587Sitojunstatic int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, 14462587Sitojun struct ifnet *, int)); 145175162Sobrienstatic int icmp6_notify_error(struct mbuf **, int, int, int); 14653541Sshin 147196039Srwatson/* 148196039Srwatson * Kernel module interface for updating icmp6stat. The argument is an index 149196039Srwatson * into icmp6stat treated as an array of u_quad_t. While this encodes the 150196039Srwatson * general layout of icmp6stat into the caller, it doesn't encode its 151196039Srwatson * location, so that future changes to add, for example, per-CPU stats 152196039Srwatson * support won't cause binary compatibility problems for kernel modules. 153196039Srwatson */ 154196039Srwatsonvoid 155196039Srwatsonkmod_icmp6stat_inc(int statnum) 156196039Srwatson{ 157196039Srwatson 158196039Srwatson (*((u_quad_t *)&V_icmp6stat + statnum))++; 159196039Srwatson} 160196039Srwatson 16162587Sitojunstatic void 162171259Sdelphijicmp6_errcount(struct icmp6errstat *stat, int type, int code) 16362587Sitojun{ 16478064Sume switch (type) { 16562587Sitojun case ICMP6_DST_UNREACH: 16662587Sitojun switch (code) { 16762587Sitojun case ICMP6_DST_UNREACH_NOROUTE: 16862587Sitojun stat->icp6errs_dst_unreach_noroute++; 16962587Sitojun return; 17062587Sitojun case ICMP6_DST_UNREACH_ADMIN: 17162587Sitojun stat->icp6errs_dst_unreach_admin++; 17262587Sitojun return; 17362587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 17462587Sitojun stat->icp6errs_dst_unreach_beyondscope++; 17562587Sitojun return; 17662587Sitojun case ICMP6_DST_UNREACH_ADDR: 17762587Sitojun stat->icp6errs_dst_unreach_addr++; 17862587Sitojun return; 17962587Sitojun case ICMP6_DST_UNREACH_NOPORT: 18062587Sitojun stat->icp6errs_dst_unreach_noport++; 18162587Sitojun return; 18262587Sitojun } 18362587Sitojun break; 18462587Sitojun case ICMP6_PACKET_TOO_BIG: 18562587Sitojun stat->icp6errs_packet_too_big++; 18662587Sitojun return; 18762587Sitojun case ICMP6_TIME_EXCEEDED: 18878064Sume switch (code) { 18962587Sitojun case ICMP6_TIME_EXCEED_TRANSIT: 19062587Sitojun stat->icp6errs_time_exceed_transit++; 19162587Sitojun return; 19262587Sitojun case ICMP6_TIME_EXCEED_REASSEMBLY: 19362587Sitojun stat->icp6errs_time_exceed_reassembly++; 19462587Sitojun return; 19562587Sitojun } 19662587Sitojun break; 19762587Sitojun case ICMP6_PARAM_PROB: 19878064Sume switch (code) { 19962587Sitojun case ICMP6_PARAMPROB_HEADER: 20062587Sitojun stat->icp6errs_paramprob_header++; 20162587Sitojun return; 20262587Sitojun case ICMP6_PARAMPROB_NEXTHEADER: 20362587Sitojun stat->icp6errs_paramprob_nextheader++; 20462587Sitojun return; 20562587Sitojun case ICMP6_PARAMPROB_OPTION: 20662587Sitojun stat->icp6errs_paramprob_option++; 20762587Sitojun return; 20862587Sitojun } 20962587Sitojun break; 21062587Sitojun case ND_REDIRECT: 21162587Sitojun stat->icp6errs_redirect++; 21262587Sitojun return; 21362587Sitojun } 21462587Sitojun stat->icp6errs_unknown++; 21562587Sitojun} 21662587Sitojun 21753541Sshin/* 218148385Sume * A wrapper function for icmp6_error() necessary when the erroneous packet 219148385Sume * may not contain enough scope zone information. 220148385Sume */ 221148385Sumevoid 222171259Sdelphijicmp6_error2(struct mbuf *m, int type, int code, int param, 223171259Sdelphij struct ifnet *ifp) 224148385Sume{ 225148385Sume struct ip6_hdr *ip6; 226148385Sume 227148385Sume if (ifp == NULL) 228148385Sume return; 229148385Sume 230148385Sume#ifndef PULLDOWN_TEST 231148385Sume IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 232148385Sume#else 233148385Sume if (m->m_len < sizeof(struct ip6_hdr)) { 234148385Sume m = m_pullup(m, sizeof(struct ip6_hdr)); 235148385Sume if (m == NULL) 236148385Sume return; 237148385Sume } 238148385Sume#endif 239148385Sume 240148385Sume ip6 = mtod(m, struct ip6_hdr *); 241148385Sume 242148385Sume if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0) 243148385Sume return; 244148385Sume if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) 245148385Sume return; 246148385Sume 247148385Sume icmp6_error(m, type, code, param); 248148385Sume} 249148385Sume 250148385Sume/* 25153541Sshin * Generate an error packet of type error in response to bad IP6 packet. 25253541Sshin */ 25353541Sshinvoid 254171259Sdelphijicmp6_error(struct mbuf *m, int type, int code, int param) 25553541Sshin{ 25653541Sshin struct ip6_hdr *oip6, *nip6; 25753541Sshin struct icmp6_hdr *icmp6; 25862587Sitojun u_int preplen; 25953541Sshin int off; 26062587Sitojun int nxt; 26153541Sshin 262190964Srwatson ICMP6STAT_INC(icp6s_error); 26353541Sshin 26462587Sitojun /* count per-type-code statistics */ 265181803Sbz icmp6_errcount(&V_icmp6stat.icp6s_outerrhist, type, code); 26662587Sitojun 26762587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 26862587Sitojun if (m->m_flags & M_DECRYPTED) { 269190964Srwatson ICMP6STAT_INC(icp6s_canterror); 27053541Sshin goto freeit; 27162587Sitojun } 27262587Sitojun#endif 27353541Sshin 27462587Sitojun#ifndef PULLDOWN_TEST 27562587Sitojun IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 27662587Sitojun#else 27762587Sitojun if (m->m_len < sizeof(struct ip6_hdr)) { 27862587Sitojun m = m_pullup(m, sizeof(struct ip6_hdr)); 27962587Sitojun if (m == NULL) 28062587Sitojun return; 28162587Sitojun } 28262587Sitojun#endif 28353541Sshin oip6 = mtod(m, struct ip6_hdr *); 28453541Sshin 28553541Sshin /* 286121472Sume * If the destination address of the erroneous packet is a multicast 287121472Sume * address, or the packet was sent using link-layer multicast, 288121472Sume * we should basically suppress sending an error (RFC 2463, Section 289121472Sume * 2.4). 290121472Sume * We have two exceptions (the item e.2 in that section): 291201688Sbz * - the Packet Too Big message can be sent for path MTU discovery. 292121472Sume * - the Parameter Problem Message that can be allowed an icmp6 error 293121472Sume * in the option type field. This check has been done in 294121472Sume * ip6_unknown_opt(), so we can just check the type and code. 29553541Sshin */ 29653541Sshin if ((m->m_flags & (M_BCAST|M_MCAST) || 29753541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && 29853541Sshin (type != ICMP6_PACKET_TOO_BIG && 29953541Sshin (type != ICMP6_PARAM_PROB || 30053541Sshin code != ICMP6_PARAMPROB_OPTION))) 30153541Sshin goto freeit; 30253541Sshin 303121472Sume /* 304121472Sume * RFC 2463, 2.4 (e.5): source address check. 305121472Sume * XXX: the case of anycast source? 306121472Sume */ 30753541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || 30853541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) 30953541Sshin goto freeit; 31053541Sshin 31153541Sshin /* 31262587Sitojun * If we are about to send ICMPv6 against ICMPv6 error/redirect, 31362587Sitojun * don't do it. 31453541Sshin */ 31562587Sitojun nxt = -1; 31662587Sitojun off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 31762587Sitojun if (off >= 0 && nxt == IPPROTO_ICMPV6) { 31853541Sshin struct icmp6_hdr *icp; 31953541Sshin 32062587Sitojun#ifndef PULLDOWN_TEST 32162587Sitojun IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), ); 32262587Sitojun icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 32362587Sitojun#else 32462587Sitojun IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, 32562587Sitojun sizeof(*icp)); 32662587Sitojun if (icp == NULL) { 327190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 32862587Sitojun return; 32962587Sitojun } 33062587Sitojun#endif 33162587Sitojun if (icp->icmp6_type < ICMP6_ECHO_REQUEST || 33262587Sitojun icp->icmp6_type == ND_REDIRECT) { 33353541Sshin /* 33462587Sitojun * ICMPv6 error 33562587Sitojun * Special case: for redirect (which is 33662587Sitojun * informational) we must not send icmp6 error. 33753541Sshin */ 338190964Srwatson ICMP6STAT_INC(icp6s_canterror); 33962587Sitojun goto freeit; 34062587Sitojun } else { 34162587Sitojun /* ICMPv6 informational - send the error */ 34253541Sshin } 34362587Sitojun } else { 34462587Sitojun /* non-ICMPv6 - send the error */ 34553541Sshin } 34653541Sshin 34753541Sshin oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */ 34853541Sshin 34953541Sshin /* Finally, do rate limitation check. */ 35053541Sshin if (icmp6_ratelimit(&oip6->ip6_src, type, code)) { 351190964Srwatson ICMP6STAT_INC(icp6s_toofreq); 35253541Sshin goto freeit; 35353541Sshin } 35453541Sshin 35553541Sshin /* 35653541Sshin * OK, ICMP6 can be generated. 35753541Sshin */ 35853541Sshin 35953541Sshin if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) 36053541Sshin m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); 36153541Sshin 36262587Sitojun preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 363111119Simp M_PREPEND(m, preplen, M_DONTWAIT); 36462587Sitojun if (m && m->m_len < preplen) 36562587Sitojun m = m_pullup(m, preplen); 36653541Sshin if (m == NULL) { 36778064Sume nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__)); 36853541Sshin return; 36953541Sshin } 37053541Sshin 37153541Sshin nip6 = mtod(m, struct ip6_hdr *); 37253541Sshin nip6->ip6_src = oip6->ip6_src; 37353541Sshin nip6->ip6_dst = oip6->ip6_dst; 37453541Sshin 375121315Sume in6_clearscope(&oip6->ip6_src); 376121315Sume in6_clearscope(&oip6->ip6_dst); 37753541Sshin 37853541Sshin icmp6 = (struct icmp6_hdr *)(nip6 + 1); 37953541Sshin icmp6->icmp6_type = type; 38053541Sshin icmp6->icmp6_code = code; 38153541Sshin icmp6->icmp6_pptr = htonl((u_int32_t)param); 38253541Sshin 38378064Sume /* 38478064Sume * icmp6_reflect() is designed to be in the input path. 385148987Sume * icmp6_error() can be called from both input and output path, 38678064Sume * and if we are in output path rcvif could contain bogus value. 38778064Sume * clear m->m_pkthdr.rcvif for safety, we should have enough scope 38878064Sume * information in ip header (nip6). 38978064Sume */ 39078064Sume m->m_pkthdr.rcvif = NULL; 39178064Sume 392190964Srwatson ICMP6STAT_INC(icp6s_outhist[type]); 39395023Ssuz icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ 39462587Sitojun 39562587Sitojun return; 39662587Sitojun 39762587Sitojun freeit: 39862587Sitojun /* 399148987Sume * If we can't tell whether or not we can generate ICMP6, free it. 40062587Sitojun */ 40162587Sitojun m_freem(m); 40253541Sshin} 40353541Sshin 40453541Sshin/* 40553541Sshin * Process a received ICMP6 message. 40653541Sshin */ 40753541Sshinint 408171259Sdelphijicmp6_input(struct mbuf **mp, int *offp, int proto) 40953541Sshin{ 41053541Sshin struct mbuf *m = *mp, *n; 411192923Sbms struct ifnet *ifp; 41253541Sshin struct ip6_hdr *ip6, *nip6; 41353541Sshin struct icmp6_hdr *icmp6, *nicmp6; 41453541Sshin int off = *offp; 41553541Sshin int icmp6len = m->m_pkthdr.len - *offp; 41653541Sshin int code, sum, noff; 417165118Sbz char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 418211501Sanchie int ip6len, error; 41953541Sshin 420192923Sbms ifp = m->m_pkthdr.rcvif; 421192923Sbms 42262587Sitojun#ifndef PULLDOWN_TEST 42353541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE); 42495023Ssuz /* m might change if M_LOOP. So, call mtod after this */ 42562587Sitojun#endif 42653541Sshin 42753541Sshin /* 42853541Sshin * Locate icmp6 structure in mbuf, and check 42953541Sshin * that not corrupted and of at least minimum length 43053541Sshin */ 43153541Sshin 43253541Sshin ip6 = mtod(m, struct ip6_hdr *); 433211501Sanchie ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen); 43453541Sshin if (icmp6len < sizeof(struct icmp6_hdr)) { 435190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 43653541Sshin goto freeit; 43753541Sshin } 43853541Sshin 43953541Sshin /* 440191672Sbms * Check multicast group membership. 441191672Sbms * Note: SSM filters are not applied for ICMPv6 traffic. 442191672Sbms */ 443191672Sbms if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 444192923Sbms struct in6_multi *inm; 445191672Sbms 446191672Sbms inm = in6m_lookup(ifp, &ip6->ip6_dst); 447191672Sbms if (inm == NULL) { 448191672Sbms IP6STAT_INC(ip6s_notmember); 449191672Sbms in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 450191672Sbms goto freeit; 451191672Sbms } 452191672Sbms } 453191672Sbms 454191672Sbms /* 45553541Sshin * calculate the checksum 45653541Sshin */ 45762587Sitojun#ifndef PULLDOWN_TEST 45853541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 45962587Sitojun#else 46062587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 46162587Sitojun if (icmp6 == NULL) { 462190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 46362587Sitojun return IPPROTO_DONE; 46462587Sitojun } 46562587Sitojun#endif 46653541Sshin code = icmp6->icmp6_code; 46753541Sshin 46853541Sshin if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { 46978064Sume nd6log((LOG_ERR, 47053541Sshin "ICMP6 checksum error(%d|%x) %s\n", 471165118Sbz icmp6->icmp6_type, sum, 472165118Sbz ip6_sprintf(ip6bufs, &ip6->ip6_src))); 473190964Srwatson ICMP6STAT_INC(icp6s_checksum); 47453541Sshin goto freeit; 47553541Sshin } 47653541Sshin 47783934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 47853541Sshin /* 47953541Sshin * Deliver very specific ICMP6 type only. 480148987Sume * This is important to deliver TOOBIG. Otherwise PMTUD 48153541Sshin * will not work. 48253541Sshin */ 48353541Sshin switch (icmp6->icmp6_type) { 48453541Sshin case ICMP6_DST_UNREACH: 48553541Sshin case ICMP6_PACKET_TOO_BIG: 48653541Sshin case ICMP6_TIME_EXCEEDED: 48753541Sshin break; 48853541Sshin default: 48953541Sshin goto freeit; 49053541Sshin } 49153541Sshin } 49253541Sshin 493190964Srwatson ICMP6STAT_INC(icp6s_inhist[icmp6->icmp6_type]); 494192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_msg); 49553541Sshin if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) 496192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_error); 49753541Sshin 49853541Sshin switch (icmp6->icmp6_type) { 49953541Sshin case ICMP6_DST_UNREACH: 500192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_dstunreach); 50153541Sshin switch (code) { 50253541Sshin case ICMP6_DST_UNREACH_NOROUTE: 50353541Sshin code = PRC_UNREACH_NET; 50453541Sshin break; 50553541Sshin case ICMP6_DST_UNREACH_ADMIN: 506192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_adminprohib); 50762587Sitojun code = PRC_UNREACH_PROTOCOL; /* is this a good code? */ 50862587Sitojun break; 50953541Sshin case ICMP6_DST_UNREACH_ADDR: 51062587Sitojun code = PRC_HOSTDEAD; 51153541Sshin break; 51262587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 51362587Sitojun /* I mean "source address was incorrect." */ 51462587Sitojun code = PRC_PARAMPROB; 51562587Sitojun break; 51653541Sshin case ICMP6_DST_UNREACH_NOPORT: 51753541Sshin code = PRC_UNREACH_PORT; 51853541Sshin break; 51953541Sshin default: 52053541Sshin goto badcode; 52153541Sshin } 52253541Sshin goto deliver; 52353541Sshin break; 52453541Sshin 52553541Sshin case ICMP6_PACKET_TOO_BIG: 526192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_pkttoobig); 52753541Sshin 528126194Sume /* validation is made in icmp6_mtudisc_update */ 529126194Sume 53053541Sshin code = PRC_MSGSIZE; 53153541Sshin 53262587Sitojun /* 53362587Sitojun * Updating the path MTU will be done after examining 53462587Sitojun * intermediate extension headers. 53562587Sitojun */ 53653541Sshin goto deliver; 53753541Sshin break; 53853541Sshin 53953541Sshin case ICMP6_TIME_EXCEEDED: 540192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_timeexceed); 54153541Sshin switch (code) { 54253541Sshin case ICMP6_TIME_EXCEED_TRANSIT: 543151465Ssuz code = PRC_TIMXCEED_INTRANS; 544151465Ssuz break; 54553541Sshin case ICMP6_TIME_EXCEED_REASSEMBLY: 546151465Ssuz code = PRC_TIMXCEED_REASS; 54753541Sshin break; 54853541Sshin default: 54953541Sshin goto badcode; 55053541Sshin } 55153541Sshin goto deliver; 55253541Sshin break; 55353541Sshin 55453541Sshin case ICMP6_PARAM_PROB: 555192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_paramprob); 55653541Sshin switch (code) { 55753541Sshin case ICMP6_PARAMPROB_NEXTHEADER: 55853541Sshin code = PRC_UNREACH_PROTOCOL; 55953541Sshin break; 56053541Sshin case ICMP6_PARAMPROB_HEADER: 56153541Sshin case ICMP6_PARAMPROB_OPTION: 56253541Sshin code = PRC_PARAMPROB; 56353541Sshin break; 56453541Sshin default: 56553541Sshin goto badcode; 56653541Sshin } 56753541Sshin goto deliver; 56853541Sshin break; 56953541Sshin 57053541Sshin case ICMP6_ECHO_REQUEST: 571192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_echo); 57253541Sshin if (code != 0) 57353541Sshin goto badcode; 57453541Sshin if ((n = m_copy(m, 0, M_COPYALL)) == NULL) { 57553541Sshin /* Give up remote */ 57653541Sshin break; 57753541Sshin } 57862587Sitojun if ((n->m_flags & M_EXT) != 0 57962587Sitojun || n->m_len < off + sizeof(struct icmp6_hdr)) { 58053541Sshin struct mbuf *n0 = n; 58162587Sitojun const int maxlen = sizeof(*nip6) + sizeof(*nicmp6); 582108741Ssam int n0len; 58353541Sshin 584111119Simp MGETHDR(n, M_DONTWAIT, n0->m_type); 585111070Ssam n0len = n0->m_pkthdr.len; /* save for use below */ 586111070Ssam if (n) 587111070Ssam M_MOVE_PKTHDR(n, n0); 58862587Sitojun if (n && maxlen >= MHLEN) { 589111119Simp MCLGET(n, M_DONTWAIT); 59062587Sitojun if ((n->m_flags & M_EXT) == 0) { 59162587Sitojun m_free(n); 59262587Sitojun n = NULL; 59362587Sitojun } 59462587Sitojun } 59553541Sshin if (n == NULL) { 59653541Sshin /* Give up remote */ 59753541Sshin m_freem(n0); 59853541Sshin break; 59953541Sshin } 60053541Sshin /* 60153541Sshin * Copy IPv6 and ICMPv6 only. 60253541Sshin */ 60353541Sshin nip6 = mtod(n, struct ip6_hdr *); 60453541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 60553541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 60653541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 60762587Sitojun noff = sizeof(struct ip6_hdr); 608108741Ssam /* new mbuf contains only ipv6+icmpv6 headers */ 609108741Ssam n->m_len = noff + sizeof(struct icmp6_hdr); 61053541Sshin /* 611120891Sume * Adjust mbuf. ip6_plen will be adjusted in 61262587Sitojun * ip6_output(). 61353541Sshin */ 61462587Sitojun m_adj(n0, off + sizeof(struct icmp6_hdr)); 615108741Ssam /* recalculate complete packet size */ 616108741Ssam n->m_pkthdr.len = n0len + (noff - off); 61762587Sitojun n->m_next = n0; 61853541Sshin } else { 61953541Sshin nip6 = mtod(n, struct ip6_hdr *); 620166046Sume IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off, 621166046Sume sizeof(*nicmp6)); 62253541Sshin noff = off; 62353541Sshin } 62453541Sshin nicmp6->icmp6_type = ICMP6_ECHO_REPLY; 62553541Sshin nicmp6->icmp6_code = 0; 62653541Sshin if (n) { 627190964Srwatson ICMP6STAT_INC(icp6s_reflect); 628190964Srwatson ICMP6STAT_INC(icp6s_outhist[ICMP6_ECHO_REPLY]); 62953541Sshin icmp6_reflect(n, noff); 63053541Sshin } 63153541Sshin break; 63253541Sshin 63353541Sshin case ICMP6_ECHO_REPLY: 634192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_echoreply); 63553541Sshin if (code != 0) 63653541Sshin goto badcode; 63753541Sshin break; 63853541Sshin 63996116Sume case MLD_LISTENER_QUERY: 64096116Sume case MLD_LISTENER_REPORT: 641191672Sbms case MLD_LISTENER_DONE: 642191672Sbms case MLDV2_LISTENER_REPORT: 643191672Sbms /* 644192923Sbms * Drop MLD traffic which is not link-local, has a hop limit 645192923Sbms * of greater than 1 hop, or which does not have the 646192923Sbms * IPv6 HBH Router Alert option. 647192923Sbms * As IPv6 HBH options are stripped in ip6_input() we must 648192923Sbms * check an mbuf header flag. 649191672Sbms * XXX Should we also sanity check that these messages 650191672Sbms * were directed to a link-local multicast prefix? 651191672Sbms */ 652192923Sbms if ((ip6->ip6_hlim != 1) || (m->m_flags & M_RTALERT_MLD) == 0) 65362587Sitojun goto freeit; 654191672Sbms if (mld_input(m, off, icmp6len) != 0) 655191672Sbms return (IPPROTO_DONE); 65653541Sshin /* m stays. */ 65753541Sshin break; 65853541Sshin 65953541Sshin case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ 66053541Sshin { 66153541Sshin enum { WRU, FQDN } mode; 66253541Sshin 663181803Sbz if (!V_icmp6_nodeinfo) 66462587Sitojun break; 66562587Sitojun 66653541Sshin if (icmp6len == sizeof(struct icmp6_hdr) + 4) 66753541Sshin mode = WRU; 66862587Sitojun else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) 66953541Sshin mode = FQDN; 67053541Sshin else 67153541Sshin goto badlen; 67253541Sshin 67353541Sshin if (mode == FQDN) { 67462587Sitojun#ifndef PULLDOWN_TEST 67553541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), 676120891Sume IPPROTO_DONE); 67762587Sitojun#endif 67862587Sitojun n = m_copy(m, 0, M_COPYALL); 67962587Sitojun if (n) 68062587Sitojun n = ni6_input(n, off); 68162587Sitojun /* XXX meaningless if n == NULL */ 68253541Sshin noff = sizeof(struct ip6_hdr); 68353541Sshin } else { 684193066Sjamie struct prison *pr; 68553541Sshin u_char *p; 686193066Sjamie int maxlen, maxhlen, hlen; 68753541Sshin 688169664Sjinmei /* 689169664Sjinmei * XXX: this combination of flags is pointless, 690169664Sjinmei * but should we keep this for compatibility? 691169664Sjinmei */ 692181803Sbz if ((V_icmp6_nodeinfo & 5) != 5) 69378064Sume break; 69478064Sume 69562587Sitojun if (code != 0) 69662587Sitojun goto badcode; 69762587Sitojun maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4; 69862587Sitojun if (maxlen >= MCLBYTES) { 69962587Sitojun /* Give up remote */ 70062587Sitojun break; 70162587Sitojun } 702111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 70362587Sitojun if (n && maxlen > MHLEN) { 704111119Simp MCLGET(n, M_DONTWAIT); 70562587Sitojun if ((n->m_flags & M_EXT) == 0) { 70662587Sitojun m_free(n); 70762587Sitojun n = NULL; 70862587Sitojun } 70962587Sitojun } 710166619Sbms if (n && !m_dup_pkthdr(n, m, M_DONTWAIT)) { 711108466Ssam /* 712108466Ssam * Previous code did a blind M_COPY_PKTHDR 713108466Ssam * and said "just for rcvif". If true, then 714108466Ssam * we could tolerate the dup failing (due to 715108466Ssam * the deep copy of the tag chain). For now 716108466Ssam * be conservative and just fail. 717108466Ssam */ 718108466Ssam m_free(n); 719108466Ssam n = NULL; 720108466Ssam } 72153541Sshin if (n == NULL) { 72253541Sshin /* Give up remote */ 72353541Sshin break; 72453541Sshin } 72578064Sume n->m_pkthdr.rcvif = NULL; 72662587Sitojun n->m_len = 0; 72762587Sitojun maxhlen = M_TRAILINGSPACE(n) - maxlen; 728193066Sjamie pr = curthread->td_ucred->cr_prison; 729193066Sjamie mtx_lock(&pr->pr_mtx); 730194118Sjamie hlen = strlen(pr->pr_hostname); 731193066Sjamie if (maxhlen > hlen) 732193066Sjamie maxhlen = hlen; 73353541Sshin /* 73453541Sshin * Copy IPv6 and ICMPv6 only. 73553541Sshin */ 73653541Sshin nip6 = mtod(n, struct ip6_hdr *); 73753541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 73853541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 73953541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 74053541Sshin p = (u_char *)(nicmp6 + 1); 74153541Sshin bzero(p, 4); 742194118Sjamie /* meaningless TTL */ 743194118Sjamie bcopy(pr->pr_hostname, p + 4, maxhlen); 744193066Sjamie mtx_unlock(&pr->pr_mtx); 74553541Sshin noff = sizeof(struct ip6_hdr); 74653541Sshin n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 74762587Sitojun sizeof(struct icmp6_hdr) + 4 + maxhlen; 74853541Sshin nicmp6->icmp6_type = ICMP6_WRUREPLY; 74953541Sshin nicmp6->icmp6_code = 0; 75053541Sshin } 75153541Sshin if (n) { 752190964Srwatson ICMP6STAT_INC(icp6s_reflect); 753190964Srwatson ICMP6STAT_INC(icp6s_outhist[ICMP6_WRUREPLY]); 75453541Sshin icmp6_reflect(n, noff); 75553541Sshin } 75653541Sshin break; 75753541Sshin } 75853541Sshin 75953541Sshin case ICMP6_WRUREPLY: 76053541Sshin if (code != 0) 76153541Sshin goto badcode; 76253541Sshin break; 76353541Sshin 76453541Sshin case ND_ROUTER_SOLICIT: 765192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_routersolicit); 76653541Sshin if (code != 0) 76753541Sshin goto badcode; 76853541Sshin if (icmp6len < sizeof(struct nd_router_solicit)) 76953541Sshin goto badlen; 770111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 77162587Sitojun /* give up local */ 772211501Sanchie 773211501Sanchie /* Send incoming SeND packet to user space. */ 774211501Sanchie if (send_sendso_input_hook != NULL) { 775211501Sanchie IP6_EXTHDR_CHECK(m, off, 776211501Sanchie icmp6len, IPPROTO_DONE); 777211501Sanchie error = send_sendso_input_hook(m, ifp, 778211501Sanchie SND_IN, ip6len); 779211501Sanchie /* -1 == no app on SEND socket */ 780211501Sanchie if (error == 0) 781211501Sanchie return (IPPROTO_DONE); 782211501Sanchie nd6_rs_input(m, off, icmp6len); 783211501Sanchie } else 784211501Sanchie nd6_rs_input(m, off, icmp6len); 78562587Sitojun m = NULL; 78662587Sitojun goto freeit; 78762587Sitojun } 788211501Sanchie if (send_sendso_input_hook != NULL) { 789211520Sanchie IP6_EXTHDR_CHECK(n, off, 790211501Sanchie icmp6len, IPPROTO_DONE); 791211501Sanchie error = send_sendso_input_hook(n, ifp, 792211501Sanchie SND_IN, ip6len); 793211520Sanchie if (error == 0) 794211520Sanchie goto freeit; 795211501Sanchie /* -1 == no app on SEND socket */ 796211501Sanchie nd6_rs_input(n, off, icmp6len); 797211501Sanchie } else 798211501Sanchie nd6_rs_input(n, off, icmp6len); 79953541Sshin /* m stays. */ 80053541Sshin break; 80153541Sshin 80253541Sshin case ND_ROUTER_ADVERT: 803192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_routeradvert); 80453541Sshin if (code != 0) 80553541Sshin goto badcode; 80653541Sshin if (icmp6len < sizeof(struct nd_router_advert)) 80753541Sshin goto badlen; 808111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 809211501Sanchie 810211501Sanchie /* Send incoming SeND-protected/ND packet to user space. */ 811211501Sanchie if (send_sendso_input_hook != NULL) { 812211501Sanchie error = send_sendso_input_hook(m, ifp, 813211501Sanchie SND_IN, ip6len); 814211501Sanchie if (error == 0) 815211501Sanchie return (IPPROTO_DONE); 816211501Sanchie nd6_ra_input(m, off, icmp6len); 817211501Sanchie } else 818211501Sanchie nd6_ra_input(m, off, icmp6len); 81962587Sitojun m = NULL; 82062587Sitojun goto freeit; 82162587Sitojun } 822211501Sanchie if (send_sendso_input_hook != NULL) { 823211501Sanchie error = send_sendso_input_hook(n, ifp, 824211501Sanchie SND_IN, ip6len); 825211501Sanchie if (error == 0) 826211520Sanchie goto freeit; 827211501Sanchie nd6_ra_input(n, off, icmp6len); 828211501Sanchie } else 829211501Sanchie nd6_ra_input(n, off, icmp6len); 83053541Sshin /* m stays. */ 83153541Sshin break; 83253541Sshin 83353541Sshin case ND_NEIGHBOR_SOLICIT: 834192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_neighborsolicit); 83553541Sshin if (code != 0) 83653541Sshin goto badcode; 83753541Sshin if (icmp6len < sizeof(struct nd_neighbor_solicit)) 83853541Sshin goto badlen; 839111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 840211501Sanchie if (send_sendso_input_hook != NULL) { 841211501Sanchie error = send_sendso_input_hook(m, ifp, 842211501Sanchie SND_IN, ip6len); 843211501Sanchie if (error == 0) 844211501Sanchie return (IPPROTO_DONE); 845211501Sanchie nd6_ns_input(m, off, icmp6len); 846211501Sanchie } else 847211501Sanchie nd6_ns_input(m, off, icmp6len); 84862587Sitojun m = NULL; 84962587Sitojun goto freeit; 85062587Sitojun } 851211501Sanchie if (send_sendso_input_hook != NULL) { 852211501Sanchie error = send_sendso_input_hook(n, ifp, 853211501Sanchie SND_IN, ip6len); 854211501Sanchie if (error == 0) 855211520Sanchie goto freeit; 856211501Sanchie nd6_ns_input(n, off, icmp6len); 857211501Sanchie } else 858211501Sanchie nd6_ns_input(n, off, icmp6len); 85953541Sshin /* m stays. */ 86053541Sshin break; 86153541Sshin 86253541Sshin case ND_NEIGHBOR_ADVERT: 863192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_neighboradvert); 86453541Sshin if (code != 0) 86553541Sshin goto badcode; 86653541Sshin if (icmp6len < sizeof(struct nd_neighbor_advert)) 86753541Sshin goto badlen; 868111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 869211501Sanchie 870211501Sanchie /* Send incoming SeND-protected/ND packet to user space. */ 871211501Sanchie if (send_sendso_input_hook != NULL) { 872211501Sanchie error = send_sendso_input_hook(m, ifp, 873211501Sanchie SND_IN, ip6len); 874211501Sanchie if (error == 0) 875211501Sanchie return (IPPROTO_DONE); 876211501Sanchie nd6_na_input(m, off, icmp6len); 877211501Sanchie } else 878211501Sanchie nd6_na_input(m, off, icmp6len); 87962587Sitojun m = NULL; 88062587Sitojun goto freeit; 88162587Sitojun } 882211501Sanchie if (send_sendso_input_hook != NULL) { 883211501Sanchie error = send_sendso_input_hook(n, ifp, 884211501Sanchie SND_IN, ip6len); 885211501Sanchie if (error == 0) 886211520Sanchie goto freeit; 887211501Sanchie nd6_na_input(n, off, icmp6len); 888211501Sanchie } else 889211501Sanchie nd6_na_input(n, off, icmp6len); 89053541Sshin /* m stays. */ 89153541Sshin break; 89253541Sshin 89353541Sshin case ND_REDIRECT: 894192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_redirect); 89553541Sshin if (code != 0) 89653541Sshin goto badcode; 89753541Sshin if (icmp6len < sizeof(struct nd_redirect)) 89853541Sshin goto badlen; 899111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 900211501Sanchie if (send_sendso_input_hook != NULL) { 901211501Sanchie error = send_sendso_input_hook(m, ifp, 902211501Sanchie SND_IN, ip6len); 903211501Sanchie if (error == 0) 904211501Sanchie return (IPPROTO_DONE); 905211501Sanchie icmp6_redirect_input(m, off); 906211501Sanchie } else 907211501Sanchie icmp6_redirect_input(m, off); 90862587Sitojun m = NULL; 90962587Sitojun goto freeit; 91062587Sitojun } 911211501Sanchie if (send_sendso_input_hook != NULL) { 912211501Sanchie error = send_sendso_input_hook(n, ifp, 913211501Sanchie SND_IN, ip6len); 914211501Sanchie if (error == 0) 915211520Sanchie goto freeit; 916211501Sanchie icmp6_redirect_input(n, off); 917211501Sanchie } else 918211501Sanchie icmp6_redirect_input(n, off); 91953541Sshin /* m stays. */ 92053541Sshin break; 92153541Sshin 92253541Sshin case ICMP6_ROUTER_RENUMBERING: 92353541Sshin if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && 92453541Sshin code != ICMP6_ROUTER_RENUMBERING_RESULT) 92553541Sshin goto badcode; 92653541Sshin if (icmp6len < sizeof(struct icmp6_router_renum)) 92753541Sshin goto badlen; 92853541Sshin break; 92953541Sshin 93053541Sshin default: 93178064Sume nd6log((LOG_DEBUG, 93278064Sume "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", 933165118Sbz icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src), 934165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst), 935192923Sbms ifp ? ifp->if_index : 0)); 93653541Sshin if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { 93753541Sshin /* ICMPv6 error: MUST deliver it by spec... */ 93853541Sshin code = PRC_NCMDS; 93953541Sshin /* deliver */ 94053541Sshin } else { 94153541Sshin /* ICMPv6 informational: MUST not deliver */ 94253541Sshin break; 94353541Sshin } 94453541Sshin deliver: 945198076Sbz if (icmp6_notify_error(&m, off, icmp6len, code) != 0) { 94678064Sume /* In this case, m should've been freed. */ 947120856Sume return (IPPROTO_DONE); 94853541Sshin } 94978064Sume break; 95078064Sume 95178064Sume badcode: 952190964Srwatson ICMP6STAT_INC(icp6s_badcode); 95378064Sume break; 95478064Sume 95578064Sume badlen: 956190964Srwatson ICMP6STAT_INC(icp6s_badlen); 95778064Sume break; 95878064Sume } 95978064Sume 96078064Sume /* deliver the packet to appropriate sockets */ 96178064Sume icmp6_rip6_input(&m, *offp); 96278064Sume 96378064Sume return IPPROTO_DONE; 96478064Sume 96578064Sume freeit: 96678064Sume m_freem(m); 96778064Sume return IPPROTO_DONE; 96878064Sume} 96978064Sume 97078064Sumestatic int 971171259Sdelphijicmp6_notify_error(struct mbuf **mp, int off, int icmp6len, int code) 97278064Sume{ 973142681Sume struct mbuf *m = *mp; 97478064Sume struct icmp6_hdr *icmp6; 97578064Sume struct ip6_hdr *eip6; 97678064Sume u_int32_t notifymtu; 97778064Sume struct sockaddr_in6 icmp6src, icmp6dst; 97878064Sume 97978064Sume if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { 980190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 98178064Sume goto freeit; 98278064Sume } 98362587Sitojun#ifndef PULLDOWN_TEST 98478064Sume IP6_EXTHDR_CHECK(m, off, 985120891Sume sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1); 98678064Sume icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 98762587Sitojun#else 98878064Sume IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 989120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 99078064Sume if (icmp6 == NULL) { 991190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 992120856Sume return (-1); 99378064Sume } 99462587Sitojun#endif 99578064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 99653541Sshin 99778064Sume /* Detect the upper level protocol */ 99878064Sume { 999175162Sobrien void (*ctlfunc)(int, struct sockaddr *, void *); 100053541Sshin u_int8_t nxt = eip6->ip6_nxt; 100153541Sshin int eoff = off + sizeof(struct icmp6_hdr) + 1002120891Sume sizeof(struct ip6_hdr); 100353541Sshin struct ip6ctlparam ip6cp; 100462587Sitojun struct in6_addr *finaldst = NULL; 100562587Sitojun int icmp6type = icmp6->icmp6_type; 100662587Sitojun struct ip6_frag *fh; 100762587Sitojun struct ip6_rthdr *rth; 100862587Sitojun struct ip6_rthdr0 *rth0; 100962587Sitojun int rthlen; 101053541Sshin 101195023Ssuz while (1) { /* XXX: should avoid infinite loop explicitly? */ 101253541Sshin struct ip6_ext *eh; 101353541Sshin 101478064Sume switch (nxt) { 101553541Sshin case IPPROTO_HOPOPTS: 101653541Sshin case IPPROTO_DSTOPTS: 101753541Sshin case IPPROTO_AH: 101862587Sitojun#ifndef PULLDOWN_TEST 1019120891Sume IP6_EXTHDR_CHECK(m, 0, 1020120891Sume eoff + sizeof(struct ip6_ext), -1); 1021120892Sume eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff); 102262587Sitojun#else 102362587Sitojun IP6_EXTHDR_GET(eh, struct ip6_ext *, m, 1024120891Sume eoff, sizeof(*eh)); 102562587Sitojun if (eh == NULL) { 1026190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 1027120856Sume return (-1); 102862587Sitojun } 102962587Sitojun#endif 1030120891Sume 103153541Sshin if (nxt == IPPROTO_AH) 103253541Sshin eoff += (eh->ip6e_len + 2) << 2; 103353541Sshin else 103453541Sshin eoff += (eh->ip6e_len + 1) << 3; 103553541Sshin nxt = eh->ip6e_nxt; 103653541Sshin break; 103762587Sitojun case IPPROTO_ROUTING: 103862587Sitojun /* 103962587Sitojun * When the erroneous packet contains a 104062587Sitojun * routing header, we should examine the 104162587Sitojun * header to determine the final destination. 104262587Sitojun * Otherwise, we can't properly update 104362587Sitojun * information that depends on the final 104462587Sitojun * destination (e.g. path MTU). 104562587Sitojun */ 104662587Sitojun#ifndef PULLDOWN_TEST 1047120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1); 1048120891Sume rth = (struct ip6_rthdr *) 1049120891Sume (mtod(m, caddr_t) + eoff); 105062587Sitojun#else 105162587Sitojun IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m, 1052120891Sume eoff, sizeof(*rth)); 105362587Sitojun if (rth == NULL) { 1054190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 1055120856Sume return (-1); 105662587Sitojun } 105762587Sitojun#endif 105862587Sitojun rthlen = (rth->ip6r_len + 1) << 3; 105962587Sitojun /* 106062587Sitojun * XXX: currently there is no 106162587Sitojun * officially defined type other 106262587Sitojun * than type-0. 106362587Sitojun * Note that if the segment left field 106462587Sitojun * is 0, all intermediate hops must 106562587Sitojun * have been passed. 106662587Sitojun */ 106762587Sitojun if (rth->ip6r_segleft && 106862587Sitojun rth->ip6r_type == IPV6_RTHDR_TYPE_0) { 106962587Sitojun int hops; 107062587Sitojun 107162587Sitojun#ifndef PULLDOWN_TEST 1072120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1); 1073120891Sume rth0 = (struct ip6_rthdr0 *) 1074120891Sume (mtod(m, caddr_t) + eoff); 107562587Sitojun#else 107662587Sitojun IP6_EXTHDR_GET(rth0, 1077120891Sume struct ip6_rthdr0 *, m, 1078120891Sume eoff, rthlen); 107962587Sitojun if (rth0 == NULL) { 1080190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 1081120856Sume return (-1); 108262587Sitojun } 108362587Sitojun#endif 108462587Sitojun /* just ignore a bogus header */ 108562587Sitojun if ((rth0->ip6r0_len % 2) == 0 && 108662587Sitojun (hops = rth0->ip6r0_len/2)) 108762587Sitojun finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1); 108862587Sitojun } 108962587Sitojun eoff += rthlen; 109062587Sitojun nxt = rth->ip6r_nxt; 109162587Sitojun break; 109262587Sitojun case IPPROTO_FRAGMENT: 109362587Sitojun#ifndef PULLDOWN_TEST 109462587Sitojun IP6_EXTHDR_CHECK(m, 0, eoff + 1095120891Sume sizeof(struct ip6_frag), -1); 1096120891Sume fh = (struct ip6_frag *)(mtod(m, caddr_t) + 1097120891Sume eoff); 109862587Sitojun#else 109962587Sitojun IP6_EXTHDR_GET(fh, struct ip6_frag *, m, 1100120891Sume eoff, sizeof(*fh)); 110162587Sitojun if (fh == NULL) { 1102190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 1103120856Sume return (-1); 110462587Sitojun } 110562587Sitojun#endif 110662587Sitojun /* 110762587Sitojun * Data after a fragment header is meaningless 110862587Sitojun * unless it is the first fragment, but 110962587Sitojun * we'll go to the notify label for path MTU 111062587Sitojun * discovery. 111162587Sitojun */ 111262587Sitojun if (fh->ip6f_offlg & IP6F_OFF_MASK) 111362587Sitojun goto notify; 111462587Sitojun 111562587Sitojun eoff += sizeof(struct ip6_frag); 111662587Sitojun nxt = fh->ip6f_nxt; 111762587Sitojun break; 111853541Sshin default: 111962587Sitojun /* 112062587Sitojun * This case includes ESP and the No Next 112195023Ssuz * Header. In such cases going to the notify 112262587Sitojun * label does not have any meaning 112362587Sitojun * (i.e. ctlfunc will be NULL), but we go 112462587Sitojun * anyway since we might have to update 112562587Sitojun * path MTU information. 112662587Sitojun */ 112753541Sshin goto notify; 112853541Sshin } 112953541Sshin } 113078064Sume notify: 113162587Sitojun#ifndef PULLDOWN_TEST 113253541Sshin icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 113362587Sitojun#else 113462587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 1135120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 113662587Sitojun if (icmp6 == NULL) { 1137190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 1138120856Sume return (-1); 113962587Sitojun } 114062587Sitojun#endif 114178064Sume 1142120891Sume /* 1143120891Sume * retrieve parameters from the inner IPv6 header, and convert 1144120891Sume * them into sockaddr structures. 1145120891Sume * XXX: there is no guarantee that the source or destination 1146120891Sume * addresses of the inner packet are in the same scope as 1147120891Sume * the addresses of the icmp packet. But there is no other 1148120891Sume * way to determine the zone. 1149120891Sume */ 115078064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 1151120891Sume 115278064Sume bzero(&icmp6dst, sizeof(icmp6dst)); 115378064Sume icmp6dst.sin6_len = sizeof(struct sockaddr_in6); 115478064Sume icmp6dst.sin6_family = AF_INET6; 115578064Sume if (finaldst == NULL) 115678064Sume icmp6dst.sin6_addr = eip6->ip6_dst; 115778064Sume else 115878064Sume icmp6dst.sin6_addr = *finaldst; 1159148385Sume if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL)) 1160121315Sume goto freeit; 116178064Sume bzero(&icmp6src, sizeof(icmp6src)); 116278064Sume icmp6src.sin6_len = sizeof(struct sockaddr_in6); 116378064Sume icmp6src.sin6_family = AF_INET6; 116478064Sume icmp6src.sin6_addr = eip6->ip6_src; 1165148385Sume if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL)) 1166121315Sume goto freeit; 1167148385Sume icmp6src.sin6_flowinfo = 1168148385Sume (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); 116978064Sume 117078064Sume if (finaldst == NULL) 117178064Sume finaldst = &eip6->ip6_dst; 117278064Sume ip6cp.ip6c_m = m; 117378064Sume ip6cp.ip6c_icmp6 = icmp6; 117478064Sume ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1); 117578064Sume ip6cp.ip6c_off = eoff; 117678064Sume ip6cp.ip6c_finaldst = finaldst; 117778064Sume ip6cp.ip6c_src = &icmp6src; 117878064Sume ip6cp.ip6c_nxt = nxt; 117978064Sume 118062587Sitojun if (icmp6type == ICMP6_PACKET_TOO_BIG) { 118178064Sume notifymtu = ntohl(icmp6->icmp6_mtu); 118278064Sume ip6cp.ip6c_cmdarg = (void *)¬ifymtu; 118378064Sume icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ 118462587Sitojun } 118562587Sitojun 1186175162Sobrien ctlfunc = (void (*)(int, struct sockaddr *, void *)) 1187120891Sume (inet6sw[ip6_protox[nxt]].pr_ctlinput); 118853541Sshin if (ctlfunc) { 118978064Sume (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, 1190120891Sume &ip6cp); 119153541Sshin } 119253541Sshin } 1193142681Sume *mp = m; 1194120856Sume return (0); 119553541Sshin 119678064Sume freeit: 119753541Sshin m_freem(m); 1198120856Sume return (-1); 119953541Sshin} 120053541Sshin 120178064Sumevoid 1202171259Sdelphijicmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated) 120362587Sitojun{ 120478064Sume struct in6_addr *dst = ip6cp->ip6c_finaldst; 120578064Sume struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; 120678064Sume struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ 120762587Sitojun u_int mtu = ntohl(icmp6->icmp6_mtu); 1208122922Sandre struct in_conninfo inc; 120962587Sitojun 1210121472Sume#if 0 1211121472Sume /* 1212121472Sume * RFC2460 section 5, last paragraph. 1213121472Sume * even though minimum link MTU for IPv6 is IPV6_MMTU, 1214121472Sume * we may see ICMPv6 too big with mtu < IPV6_MMTU 1215121472Sume * due to packet translator in the middle. 1216121472Sume * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for 1217121472Sume * special handling. 1218121472Sume */ 1219121472Sume if (mtu < IPV6_MMTU) 1220121472Sume return; 1221121472Sume#endif 1222121472Sume 1223121472Sume /* 1224121472Sume * we reject ICMPv6 too big with abnormally small value. 1225121472Sume * XXX what is the good definition of "abnormally small"? 1226121472Sume */ 1227121472Sume if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) 1228121472Sume return; 1229121472Sume 123078064Sume if (!validated) 123178064Sume return; 123278064Sume 1233182740Ssimon /* 1234182740Ssimon * In case the suggested mtu is less than IPV6_MMTU, we 1235182740Ssimon * only need to remember that it was for above mentioned 1236182740Ssimon * "alwaysfrag" case. 1237182740Ssimon * Try to be as close to the spec as possible. 1238182740Ssimon */ 1239182740Ssimon if (mtu < IPV6_MMTU) 1240182740Ssimon mtu = IPV6_MMTU - 8; 1241182740Ssimon 1242122922Sandre bzero(&inc, sizeof(inc)); 1243186222Sbz inc.inc_flags |= INC_ISIPV6; 1244122922Sandre inc.inc6_faddr = *dst; 1245148385Sume if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL)) 1246148385Sume return; 124762587Sitojun 1248162084Sandre if (mtu < tcp_maxmtu6(&inc, NULL)) { 1249122922Sandre tcp_hc_updatemtu(&inc, mtu); 1250190964Srwatson ICMP6STAT_INC(icp6s_pmtuchg); 125162587Sitojun } 125262587Sitojun} 125362587Sitojun 125453541Sshin/* 125578064Sume * Process a Node Information Query packet, based on 125678064Sume * draft-ietf-ipngwg-icmp-name-lookups-07. 1257120891Sume * 125862587Sitojun * Spec incompatibilities: 125962587Sitojun * - IPv6 Subject address handling 126062587Sitojun * - IPv4 Subject address handling support missing 126162587Sitojun * - Proxy reply (answer even if it's not for me) 126262587Sitojun * - joins NI group address at in6_ifattach() time only, does not cope 126362587Sitojun * with hostname changes by sethostname(3) 126453541Sshin */ 126553541Sshinstatic struct mbuf * 1266171259Sdelphijni6_input(struct mbuf *m, int off) 126753541Sshin{ 126862587Sitojun struct icmp6_nodeinfo *ni6, *nni6; 126953541Sshin struct mbuf *n = NULL; 1270193066Sjamie struct prison *pr; 127162587Sitojun u_int16_t qtype; 127262587Sitojun int subjlen; 127353541Sshin int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 127453541Sshin struct ni_reply_fqdn *fqdn; 127553541Sshin int addrs; /* for NI_QTYPE_NODEADDR */ 127653541Sshin struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ 1277148385Sume struct in6_addr in6_subj; /* subject address */ 127862587Sitojun struct ip6_hdr *ip6; 127962587Sitojun int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ 128078064Sume char *subj = NULL; 128178064Sume struct in6_ifaddr *ia6 = NULL; 128253541Sshin 128362587Sitojun ip6 = mtod(m, struct ip6_hdr *); 128462587Sitojun#ifndef PULLDOWN_TEST 128562587Sitojun ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); 128662587Sitojun#else 128762587Sitojun IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); 128862587Sitojun if (ni6 == NULL) { 128962587Sitojun /* m is already reclaimed */ 1290120891Sume return (NULL); 129162587Sitojun } 129262587Sitojun#endif 129353541Sshin 129462587Sitojun /* 1295169664Sjinmei * Validate IPv6 source address. 1296169664Sjinmei * The default configuration MUST be to refuse answering queries from 1297169664Sjinmei * global-scope addresses according to RFC4602. 1298169664Sjinmei * Notes: 1299169664Sjinmei * - it's not very clear what "refuse" means; this implementation 1300169664Sjinmei * simply drops it. 1301169664Sjinmei * - it's not very easy to identify global-scope (unicast) addresses 1302169664Sjinmei * since there are many prefixes for them. It should be safer 1303169664Sjinmei * and in practice sufficient to check "all" but loopback and 1304169664Sjinmei * link-local (note that site-local unicast was deprecated and 1305169664Sjinmei * ULA is defined as global scope-wise) 1306169664Sjinmei */ 1307181803Sbz if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 && 1308169664Sjinmei !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) && 1309169664Sjinmei !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) 1310169664Sjinmei goto bad; 1311169664Sjinmei 1312169664Sjinmei /* 131362587Sitojun * Validate IPv6 destination address. 131462587Sitojun * 131578064Sume * The Responder must discard the Query without further processing 131678064Sume * unless it is one of the Responder's unicast or anycast addresses, or 131778064Sume * a link-local scope multicast address which the Responder has joined. 1318169664Sjinmei * [RFC4602, Section 5.] 131962587Sitojun */ 1320121630Sume if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1321121630Sume if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) 1322121630Sume goto bad; 1323121630Sume /* else it's a link-local multicast, fine */ 1324121630Sume } else { /* unicast or anycast */ 1325121630Sume if ((ia6 = ip6_getdstifaddr(m)) == NULL) 1326121630Sume goto bad; /* XXX impossible */ 1327121630Sume 1328121630Sume if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) && 1329181803Sbz !(V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) { 1330194760Srwatson ifa_free(&ia6->ia_ifa); 133178064Sume nd6log((LOG_DEBUG, "ni6_input: ignore node info to " 133278064Sume "a temporary address in %s:%d", 133378064Sume __FILE__, __LINE__)); 133478064Sume goto bad; 133578064Sume } 1336194760Srwatson ifa_free(&ia6->ia_ifa); 1337121630Sume } 133862587Sitojun 133978064Sume /* validate query Subject field. */ 134062587Sitojun qtype = ntohs(ni6->ni_qtype); 134162587Sitojun subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); 134262587Sitojun switch (qtype) { 134362587Sitojun case NI_QTYPE_NOOP: 134462587Sitojun case NI_QTYPE_SUPTYPES: 134578064Sume /* 07 draft */ 134678064Sume if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) 134778064Sume break; 134878064Sume /* FALLTHROUGH */ 134962587Sitojun case NI_QTYPE_FQDN: 135062587Sitojun case NI_QTYPE_NODEADDR: 1351151475Ssuz case NI_QTYPE_IPV4ADDR: 135262587Sitojun switch (ni6->ni_code) { 135362587Sitojun case ICMP6_NI_SUBJ_IPV6: 135462587Sitojun#if ICMP6_NI_SUBJ_IPV6 != 0 135562587Sitojun case 0: 135662587Sitojun#endif 135762587Sitojun /* 135862587Sitojun * backward compatibility - try to accept 03 draft 135962587Sitojun * format, where no Subject is present. 136062587Sitojun */ 136178064Sume if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && 136278064Sume subjlen == 0) { 136362587Sitojun oldfqdn++; 136462587Sitojun break; 136562587Sitojun } 136678064Sume#if ICMP6_NI_SUBJ_IPV6 != 0 136778064Sume if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) 136878064Sume goto bad; 136978064Sume#endif 137062587Sitojun 1371121630Sume if (subjlen != sizeof(struct in6_addr)) 137262587Sitojun goto bad; 137362587Sitojun 137462587Sitojun /* 137562587Sitojun * Validate Subject address. 137662587Sitojun * 137778064Sume * Not sure what exactly "address belongs to the node" 137878064Sume * means in the spec, is it just unicast, or what? 137962587Sitojun * 138062587Sitojun * At this moment we consider Subject address as 138162587Sitojun * "belong to the node" if the Subject address equals 138262587Sitojun * to the IPv6 destination address; validation for 138362587Sitojun * IPv6 destination address should have done enough 138462587Sitojun * check for us. 138562587Sitojun * 138662587Sitojun * We do not do proxy at this moment. 138762587Sitojun */ 138862587Sitojun /* m_pulldown instead of copy? */ 138962587Sitojun m_copydata(m, off + sizeof(struct icmp6_nodeinfo), 1390148385Sume subjlen, (caddr_t)&in6_subj); 1391148385Sume if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL)) 1392121315Sume goto bad; 1393121630Sume 1394148385Sume subj = (char *)&in6_subj; 1395148385Sume if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj)) 139662587Sitojun break; 139778064Sume 139862587Sitojun /* 139962587Sitojun * XXX if we are to allow other cases, we should really 140062587Sitojun * be careful about scope here. 140162587Sitojun * basically, we should disallow queries toward IPv6 1402120891Sume * destination X with subject Y, 1403120891Sume * if scope(X) > scope(Y). 140462587Sitojun * if we allow scope(X) > scope(Y), it will result in 140562587Sitojun * information leakage across scope boundary. 140662587Sitojun */ 140762587Sitojun goto bad; 140862587Sitojun 140962587Sitojun case ICMP6_NI_SUBJ_FQDN: 141062587Sitojun /* 141162587Sitojun * Validate Subject name with gethostname(3). 141262587Sitojun * 141362587Sitojun * The behavior may need some debate, since: 141462587Sitojun * - we are not sure if the node has FQDN as 141562587Sitojun * hostname (returned by gethostname(3)). 141662587Sitojun * - the code does wildcard match for truncated names. 141762587Sitojun * however, we are not sure if we want to perform 141862587Sitojun * wildcard match, if gethostname(3) side has 141962587Sitojun * truncated hostname. 142062587Sitojun */ 1421193066Sjamie pr = curthread->td_ucred->cr_prison; 1422193066Sjamie mtx_lock(&pr->pr_mtx); 1423194118Sjamie n = ni6_nametodns(pr->pr_hostname, 1424194118Sjamie strlen(pr->pr_hostname), 0); 1425193066Sjamie mtx_unlock(&pr->pr_mtx); 142662587Sitojun if (!n || n->m_next || n->m_len == 0) 142762587Sitojun goto bad; 142862587Sitojun IP6_EXTHDR_GET(subj, char *, m, 142962587Sitojun off + sizeof(struct icmp6_nodeinfo), subjlen); 143062587Sitojun if (subj == NULL) 143162587Sitojun goto bad; 143262587Sitojun if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 1433120891Sume n->m_len)) { 143462587Sitojun goto bad; 143562587Sitojun } 143662587Sitojun m_freem(n); 143762587Sitojun n = NULL; 143862587Sitojun break; 143962587Sitojun 144078064Sume case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 144162587Sitojun default: 144262587Sitojun goto bad; 144362587Sitojun } 144462587Sitojun break; 144578064Sume } 144662587Sitojun 144778064Sume /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 144878064Sume switch (qtype) { 144978064Sume case NI_QTYPE_FQDN: 1450181803Sbz if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0) 145178064Sume goto bad; 145278064Sume break; 145378064Sume case NI_QTYPE_NODEADDR: 1454151475Ssuz case NI_QTYPE_IPV4ADDR: 1455181803Sbz if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0) 145678064Sume goto bad; 145778064Sume break; 145878064Sume } 145978064Sume 146078064Sume /* guess reply length */ 146178064Sume switch (qtype) { 146278064Sume case NI_QTYPE_NOOP: 146378064Sume break; /* no reply data */ 146478064Sume case NI_QTYPE_SUPTYPES: 146578064Sume replylen += sizeof(u_int32_t); 146678064Sume break; 146778064Sume case NI_QTYPE_FQDN: 146878064Sume /* XXX will append an mbuf */ 146978064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 147078064Sume break; 147178064Sume case NI_QTYPE_NODEADDR: 1472148892Sume addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj); 147378064Sume if ((replylen += addrs * (sizeof(struct in6_addr) + 1474120891Sume sizeof(u_int32_t))) > MCLBYTES) 147578064Sume replylen = MCLBYTES; /* XXX: will truncate pkt later */ 147678064Sume break; 1477151475Ssuz case NI_QTYPE_IPV4ADDR: 1478151475Ssuz /* unsupported - should respond with unknown Qtype? */ 1479151475Ssuz break; 148062587Sitojun default: 148178064Sume /* 148278064Sume * XXX: We must return a reply with the ICMP6 code 1483120891Sume * `unknown Qtype' in this case. However we regard the case 148478064Sume * as an FQDN query for backward compatibility. 148578064Sume * Older versions set a random value to this field, 148678064Sume * so it rarely varies in the defined qtypes. 148778064Sume * But the mechanism is not reliable... 148878064Sume * maybe we should obsolete older versions. 148978064Sume */ 149078064Sume qtype = NI_QTYPE_FQDN; 149178064Sume /* XXX will append an mbuf */ 149278064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 149378064Sume oldfqdn++; 149478064Sume break; 149562587Sitojun } 149662587Sitojun 149778064Sume /* allocate an mbuf to reply. */ 1498111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 149962587Sitojun if (n == NULL) { 150062587Sitojun m_freem(m); 1501120856Sume return (NULL); 150262587Sitojun } 1503108466Ssam M_MOVE_PKTHDR(n, m); /* just for recvif */ 150453541Sshin if (replylen > MHLEN) { 150562587Sitojun if (replylen > MCLBYTES) { 150678064Sume /* 150778064Sume * XXX: should we try to allocate more? But MCLBYTES 150878064Sume * is probably much larger than IPV6_MMTU... 150978064Sume */ 151053541Sshin goto bad; 151162587Sitojun } 1512111119Simp MCLGET(n, M_DONTWAIT); 151353541Sshin if ((n->m_flags & M_EXT) == 0) { 151453541Sshin goto bad; 151553541Sshin } 151653541Sshin } 151753541Sshin n->m_pkthdr.len = n->m_len = replylen; 151853541Sshin 151953541Sshin /* copy mbuf header and IPv6 + Node Information base headers */ 152053541Sshin bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); 152153541Sshin nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 152262587Sitojun bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); 152353541Sshin 152453541Sshin /* qtype dependent procedure */ 152553541Sshin switch (qtype) { 152662587Sitojun case NI_QTYPE_NOOP: 152778064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 152862587Sitojun nni6->ni_flags = 0; 152962587Sitojun break; 153062587Sitojun case NI_QTYPE_SUPTYPES: 153178064Sume { 153278064Sume u_int32_t v; 153378064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 153478064Sume nni6->ni_flags = htons(0x0000); /* raw bitmap */ 153578064Sume /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 153678064Sume v = (u_int32_t)htonl(0x0000000f); 153778064Sume bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 153862587Sitojun break; 153978064Sume } 154062587Sitojun case NI_QTYPE_FQDN: 154178064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 154262587Sitojun fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + 1543120891Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo)); 154462587Sitojun nni6->ni_flags = 0; /* XXX: meaningless TTL */ 154562587Sitojun fqdn->ni_fqdn_ttl = 0; /* ditto. */ 154662587Sitojun /* 1547193066Sjamie * XXX do we really have FQDN in hostname? 154862587Sitojun */ 1549193066Sjamie pr = curthread->td_ucred->cr_prison; 1550193066Sjamie mtx_lock(&pr->pr_mtx); 1551194118Sjamie n->m_next = ni6_nametodns(pr->pr_hostname, 1552194118Sjamie strlen(pr->pr_hostname), oldfqdn); 1553193066Sjamie mtx_unlock(&pr->pr_mtx); 155462587Sitojun if (n->m_next == NULL) 155562587Sitojun goto bad; 155662587Sitojun /* XXX we assume that n->m_next is not a chain */ 155762587Sitojun if (n->m_next->m_next != NULL) 155862587Sitojun goto bad; 155962587Sitojun n->m_pkthdr.len += n->m_next->m_len; 156062587Sitojun break; 156162587Sitojun case NI_QTYPE_NODEADDR: 156262587Sitojun { 156362587Sitojun int lenlim, copied; 156453541Sshin 156578064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 156678064Sume n->m_pkthdr.len = n->m_len = 156778064Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 156878064Sume lenlim = M_TRAILINGSPACE(n); 156962587Sitojun copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 157062587Sitojun /* XXX: reset mbuf length */ 157162587Sitojun n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 1572120891Sume sizeof(struct icmp6_nodeinfo) + copied; 157362587Sitojun break; 157453541Sshin } 157562587Sitojun default: 157662587Sitojun break; /* XXX impossible! */ 157762587Sitojun } 157853541Sshin 157953541Sshin nni6->ni_type = ICMP6_NI_REPLY; 158062587Sitojun m_freem(m); 1581120856Sume return (n); 158253541Sshin 158353541Sshin bad: 158462587Sitojun m_freem(m); 158553541Sshin if (n) 158653541Sshin m_freem(n); 1587120856Sume return (NULL); 158853541Sshin} 158953541Sshin 159053541Sshin/* 159162587Sitojun * make a mbuf with DNS-encoded string. no compression support. 159262587Sitojun * 159362587Sitojun * XXX names with less than 2 dots (like "foo" or "foo.section") will be 159462587Sitojun * treated as truncated name (two \0 at the end). this is a wild guess. 1595171259Sdelphij * 1596171259Sdelphij * old - return pascal string if non-zero 159762587Sitojun */ 159862587Sitojunstatic struct mbuf * 1599171259Sdelphijni6_nametodns(const char *name, int namelen, int old) 160062587Sitojun{ 160162587Sitojun struct mbuf *m; 160262587Sitojun char *cp, *ep; 160362587Sitojun const char *p, *q; 160462587Sitojun int i, len, nterm; 160562587Sitojun 160662587Sitojun if (old) 160762587Sitojun len = namelen + 1; 160862587Sitojun else 160962587Sitojun len = MCLBYTES; 161062587Sitojun 161162587Sitojun /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ 1612111119Simp MGET(m, M_DONTWAIT, MT_DATA); 161362587Sitojun if (m && len > MLEN) { 1614111119Simp MCLGET(m, M_DONTWAIT); 161562587Sitojun if ((m->m_flags & M_EXT) == 0) 161662587Sitojun goto fail; 161762587Sitojun } 161862587Sitojun if (!m) 161962587Sitojun goto fail; 162062587Sitojun m->m_next = NULL; 162162587Sitojun 162262587Sitojun if (old) { 162362587Sitojun m->m_len = len; 162462587Sitojun *mtod(m, char *) = namelen; 162562587Sitojun bcopy(name, mtod(m, char *) + 1, namelen); 162662587Sitojun return m; 162762587Sitojun } else { 162862587Sitojun m->m_len = 0; 162962587Sitojun cp = mtod(m, char *); 163062587Sitojun ep = mtod(m, char *) + M_TRAILINGSPACE(m); 163162587Sitojun 163262587Sitojun /* if not certain about my name, return empty buffer */ 163362587Sitojun if (namelen == 0) 163462587Sitojun return m; 163562587Sitojun 163662587Sitojun /* 163762587Sitojun * guess if it looks like shortened hostname, or FQDN. 163862587Sitojun * shortened hostname needs two trailing "\0". 163962587Sitojun */ 164062587Sitojun i = 0; 164162587Sitojun for (p = name; p < name + namelen; p++) { 164262587Sitojun if (*p && *p == '.') 164362587Sitojun i++; 164462587Sitojun } 164562587Sitojun if (i < 2) 164662587Sitojun nterm = 2; 164762587Sitojun else 164862587Sitojun nterm = 1; 164962587Sitojun 165062587Sitojun p = name; 165162587Sitojun while (cp < ep && p < name + namelen) { 165262587Sitojun i = 0; 165362587Sitojun for (q = p; q < name + namelen && *q && *q != '.'; q++) 165462587Sitojun i++; 165562587Sitojun /* result does not fit into mbuf */ 165662587Sitojun if (cp + i + 1 >= ep) 165762587Sitojun goto fail; 165878704Sume /* 165978704Sume * DNS label length restriction, RFC1035 page 8. 166078704Sume * "i == 0" case is included here to avoid returning 166178704Sume * 0-length label on "foo..bar". 166278704Sume */ 166378704Sume if (i <= 0 || i >= 64) 166462587Sitojun goto fail; 166562587Sitojun *cp++ = i; 166662587Sitojun bcopy(p, cp, i); 166762587Sitojun cp += i; 166862587Sitojun p = q; 166962587Sitojun if (p < name + namelen && *p == '.') 167062587Sitojun p++; 167162587Sitojun } 167262587Sitojun /* termination */ 167362587Sitojun if (cp + nterm >= ep) 167462587Sitojun goto fail; 167562587Sitojun while (nterm-- > 0) 167662587Sitojun *cp++ = '\0'; 167762587Sitojun m->m_len = cp - mtod(m, char *); 167862587Sitojun return m; 167962587Sitojun } 168062587Sitojun 168162587Sitojun panic("should not reach here"); 168295023Ssuz /* NOTREACHED */ 168362587Sitojun 168462587Sitojun fail: 168562587Sitojun if (m) 168662587Sitojun m_freem(m); 168762587Sitojun return NULL; 168862587Sitojun} 168962587Sitojun 169062587Sitojun/* 169162587Sitojun * check if two DNS-encoded string matches. takes care of truncated 169262587Sitojun * form (with \0\0 at the end). no compression support. 169378064Sume * XXX upper/lowercase match (see RFC2065) 169462587Sitojun */ 169562587Sitojunstatic int 1696171259Sdelphijni6_dnsmatch(const char *a, int alen, const char *b, int blen) 169762587Sitojun{ 169862587Sitojun const char *a0, *b0; 169962587Sitojun int l; 170062587Sitojun 170162587Sitojun /* simplest case - need validation? */ 170262587Sitojun if (alen == blen && bcmp(a, b, alen) == 0) 170362587Sitojun return 1; 170462587Sitojun 170562587Sitojun a0 = a; 170662587Sitojun b0 = b; 170762587Sitojun 170862587Sitojun /* termination is mandatory */ 170962587Sitojun if (alen < 2 || blen < 2) 171062587Sitojun return 0; 171162587Sitojun if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 171262587Sitojun return 0; 171362587Sitojun alen--; 171462587Sitojun blen--; 171562587Sitojun 171662587Sitojun while (a - a0 < alen && b - b0 < blen) { 171762587Sitojun if (a - a0 + 1 > alen || b - b0 + 1 > blen) 171862587Sitojun return 0; 171962587Sitojun 172062587Sitojun if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 172162587Sitojun return 0; 172262587Sitojun /* we don't support compression yet */ 172362587Sitojun if (a[0] >= 64 || b[0] >= 64) 172462587Sitojun return 0; 172562587Sitojun 172662587Sitojun /* truncated case */ 172762587Sitojun if (a[0] == 0 && a - a0 == alen - 1) 172862587Sitojun return 1; 172962587Sitojun if (b[0] == 0 && b - b0 == blen - 1) 173062587Sitojun return 1; 173162587Sitojun if (a[0] == 0 || b[0] == 0) 173262587Sitojun return 0; 173362587Sitojun 173462587Sitojun if (a[0] != b[0]) 173562587Sitojun return 0; 173662587Sitojun l = a[0]; 173762587Sitojun if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 173862587Sitojun return 0; 173962587Sitojun if (bcmp(a + 1, b + 1, l) != 0) 174062587Sitojun return 0; 174162587Sitojun 174262587Sitojun a += 1 + l; 174362587Sitojun b += 1 + l; 174462587Sitojun } 174562587Sitojun 174662587Sitojun if (a - a0 == alen && b - b0 == blen) 174762587Sitojun return 1; 174862587Sitojun else 174962587Sitojun return 0; 175062587Sitojun} 175162587Sitojun 175262587Sitojun/* 175353541Sshin * calculate the number of addresses to be returned in the node info reply. 175453541Sshin */ 175553541Sshinstatic int 1756171259Sdelphijni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp, 1757171259Sdelphij struct in6_addr *subj) 175853541Sshin{ 175978064Sume struct ifnet *ifp; 176078064Sume struct in6_ifaddr *ifa6; 176178064Sume struct ifaddr *ifa; 176253541Sshin int addrs = 0, addrsofif, iffound = 0; 176378064Sume int niflags = ni6->ni_flags; 176453541Sshin 176578064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 176678064Sume switch (ni6->ni_code) { 176778064Sume case ICMP6_NI_SUBJ_IPV6: 176878064Sume if (subj == NULL) /* must be impossible... */ 1769120856Sume return (0); 177078064Sume break; 177178064Sume default: 177278064Sume /* 177378064Sume * XXX: we only support IPv6 subject address for 177478064Sume * this Qtype. 177578064Sume */ 1776120856Sume return (0); 177778064Sume } 177878064Sume } 177978064Sume 1780196481Srwatson IFNET_RLOCK_NOSLEEP(); 1781181803Sbz for (ifp = TAILQ_FIRST(&V_ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 178253541Sshin addrsofif = 0; 1783191317Srwatson IF_ADDR_LOCK(ifp); 1784191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 178553541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 178653541Sshin continue; 178753541Sshin ifa6 = (struct in6_ifaddr *)ifa; 178853541Sshin 178978064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 1790148892Sume IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr)) 179153541Sshin iffound = 1; 179253541Sshin 179362587Sitojun /* 179462587Sitojun * IPv4-mapped addresses can only be returned by a 179562587Sitojun * Node Information proxy, since they represent 179662587Sitojun * addresses of IPv4-only nodes, which perforce do 179762587Sitojun * not implement this protocol. 179878064Sume * [icmp-name-lookups-07, Section 5.4] 179962587Sitojun * So we don't support NI_NODEADDR_FLAG_COMPAT in 180062587Sitojun * this function at this moment. 180162587Sitojun */ 180262587Sitojun 180353541Sshin /* What do we have to do about ::1? */ 180478064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 180578064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 180678064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 180778064Sume continue; 180853541Sshin break; 180978064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 181078064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 181178064Sume continue; 181253541Sshin break; 181378064Sume case IPV6_ADDR_SCOPE_GLOBAL: 181478064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 181578064Sume continue; 181678064Sume break; 181778064Sume default: 181878064Sume continue; 181953541Sshin } 182078064Sume 182178064Sume /* 182278064Sume * check if anycast is okay. 182395023Ssuz * XXX: just experimental. not in the spec. 182478064Sume */ 182578064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 182678064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 182778064Sume continue; /* we need only unicast addresses */ 182878064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1829181803Sbz (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { 183078064Sume continue; 183178064Sume } 183278064Sume addrsofif++; /* count the address */ 183353541Sshin } 1834191317Srwatson IF_ADDR_UNLOCK(ifp); 183553541Sshin if (iffound) { 183653541Sshin *ifpp = ifp; 1837196481Srwatson IFNET_RUNLOCK_NOSLEEP(); 1838120856Sume return (addrsofif); 183953541Sshin } 184053541Sshin 184153541Sshin addrs += addrsofif; 184253541Sshin } 1843196481Srwatson IFNET_RUNLOCK_NOSLEEP(); 184453541Sshin 1845120856Sume return (addrs); 184653541Sshin} 184753541Sshin 184853541Sshinstatic int 1849171259Sdelphijni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6, 1850171259Sdelphij struct ifnet *ifp0, int resid) 185153541Sshin{ 1852181803Sbz struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&V_ifnet); 185378064Sume struct in6_ifaddr *ifa6; 185478064Sume struct ifaddr *ifa; 185578064Sume struct ifnet *ifp_dep = NULL; 185678064Sume int copied = 0, allow_deprecated = 0; 185753541Sshin u_char *cp = (u_char *)(nni6 + 1); 185878064Sume int niflags = ni6->ni_flags; 185978064Sume u_int32_t ltime; 186053541Sshin 186178064Sume if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 1862120856Sume return (0); /* needless to copy */ 1863120891Sume 1864196481Srwatson IFNET_RLOCK_NOSLEEP(); 186578064Sume again: 1866120892Sume 1867120891Sume for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 1868191317Srwatson IF_ADDR_LOCK(ifp); 1869191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 187053541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 187153541Sshin continue; 187253541Sshin ifa6 = (struct in6_ifaddr *)ifa; 187353541Sshin 187478064Sume if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && 187578064Sume allow_deprecated == 0) { 187678064Sume /* 187778064Sume * prefererred address should be put before 187878064Sume * deprecated addresses. 187978064Sume */ 188078064Sume 188178064Sume /* record the interface for later search */ 188278064Sume if (ifp_dep == NULL) 188378064Sume ifp_dep = ifp; 188478064Sume 188578064Sume continue; 1886120891Sume } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && 1887120891Sume allow_deprecated != 0) 188878064Sume continue; /* we now collect deprecated addrs */ 188953541Sshin 189053541Sshin /* What do we have to do about ::1? */ 189178064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 189278064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 189378064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 189478064Sume continue; 189553541Sshin break; 189678064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 189778064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 189878064Sume continue; 189953541Sshin break; 190078064Sume case IPV6_ADDR_SCOPE_GLOBAL: 190178064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 190278064Sume continue; 190378064Sume break; 190478064Sume default: 190578064Sume continue; 190653541Sshin } 190753541Sshin 190878064Sume /* 190978064Sume * check if anycast is okay. 1910120891Sume * XXX: just experimental. not in the spec. 191178064Sume */ 191278064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 191378064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 191478064Sume continue; 191578064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1916181803Sbz (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { 191778064Sume continue; 191853541Sshin } 191978064Sume 192078064Sume /* now we can copy the address */ 192178064Sume if (resid < sizeof(struct in6_addr) + 192278064Sume sizeof(u_int32_t)) { 1923191317Srwatson IF_ADDR_UNLOCK(ifp); 192478064Sume /* 192578064Sume * We give up much more copy. 192678064Sume * Set the truncate flag and return. 192778064Sume */ 1928120891Sume nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE; 1929196481Srwatson IFNET_RUNLOCK_NOSLEEP(); 1930120856Sume return (copied); 193178064Sume } 193278064Sume 193378064Sume /* 193478064Sume * Set the TTL of the address. 193578064Sume * The TTL value should be one of the following 193678064Sume * according to the specification: 193778064Sume * 193878064Sume * 1. The remaining lifetime of a DHCP lease on the 193978064Sume * address, or 194078064Sume * 2. The remaining Valid Lifetime of a prefix from 194178064Sume * which the address was derived through Stateless 194278064Sume * Autoconfiguration. 194378064Sume * 194478064Sume * Note that we currently do not support stateful 194578064Sume * address configuration by DHCPv6, so the former 194678064Sume * case can't happen. 194778064Sume */ 194878064Sume if (ifa6->ia6_lifetime.ia6t_expire == 0) 194978064Sume ltime = ND6_INFINITE_LIFETIME; 195078064Sume else { 195178064Sume if (ifa6->ia6_lifetime.ia6t_expire > 195278064Sume time_second) 195378064Sume ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second); 195478064Sume else 195578064Sume ltime = 0; 195678064Sume } 1957120891Sume 195878064Sume bcopy(<ime, cp, sizeof(u_int32_t)); 195978064Sume cp += sizeof(u_int32_t); 196078064Sume 196178064Sume /* copy the address itself */ 196278064Sume bcopy(&ifa6->ia_addr.sin6_addr, cp, 1963120891Sume sizeof(struct in6_addr)); 1964121315Sume in6_clearscope((struct in6_addr *)cp); /* XXX */ 196578064Sume cp += sizeof(struct in6_addr); 1966120891Sume 196778064Sume resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1968120891Sume copied += (sizeof(struct in6_addr) + sizeof(u_int32_t)); 196953541Sshin } 1970191317Srwatson IF_ADDR_UNLOCK(ifp); 197153541Sshin if (ifp0) /* we need search only on the specified IF */ 197253541Sshin break; 197353541Sshin } 197453541Sshin 197578064Sume if (allow_deprecated == 0 && ifp_dep != NULL) { 197678064Sume ifp = ifp_dep; 197778064Sume allow_deprecated = 1; 197878064Sume 197978064Sume goto again; 198078064Sume } 198178064Sume 1982196481Srwatson IFNET_RUNLOCK_NOSLEEP(); 1983108172Shsu 1984120856Sume return (copied); 198553541Sshin} 198653541Sshin 198753541Sshin/* 198853541Sshin * XXX almost dup'ed code with rip6_input. 198953541Sshin */ 199053541Sshinstatic int 1991171259Sdelphijicmp6_rip6_input(struct mbuf **mp, int off) 199253541Sshin{ 199353541Sshin struct mbuf *m = *mp; 199478064Sume struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1995186141Sbz struct inpcb *in6p; 1996186141Sbz struct inpcb *last = NULL; 1997121315Sume struct sockaddr_in6 fromsa; 199853541Sshin struct icmp6_hdr *icmp6; 199953541Sshin struct mbuf *opts = NULL; 200053541Sshin 200162587Sitojun#ifndef PULLDOWN_TEST 200253541Sshin /* this is assumed to be safe. */ 200353541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 200462587Sitojun#else 200562587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 200662587Sitojun if (icmp6 == NULL) { 200762587Sitojun /* m is already reclaimed */ 2008120891Sume return (IPPROTO_DONE); 200962587Sitojun } 201062587Sitojun#endif 201153541Sshin 2012148385Sume /* 2013148385Sume * XXX: the address may have embedded scope zone ID, which should be 2014148385Sume * hidden from applications. 2015148385Sume */ 2016121315Sume bzero(&fromsa, sizeof(fromsa)); 2017148385Sume fromsa.sin6_family = AF_INET6; 2018121315Sume fromsa.sin6_len = sizeof(struct sockaddr_in6); 2019148385Sume fromsa.sin6_addr = ip6->ip6_src; 2020148385Sume if (sa6_recoverscope(&fromsa)) { 2021148385Sume m_freem(m); 2022148385Sume return (IPPROTO_DONE); 2023148385Sume } 202453541Sshin 2025181803Sbz INP_INFO_RLOCK(&V_ripcbinfo); 2026181803Sbz LIST_FOREACH(in6p, &V_ripcb, inp_list) { 2027180850Smav if ((in6p->inp_vflag & INP_IPV6) == 0) 202853541Sshin continue; 2029186141Sbz if (in6p->inp_ip_p != IPPROTO_ICMPV6) 2030180850Smav continue; 203153541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 203253541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 2033180850Smav continue; 203453541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 203553541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 2036180850Smav continue; 2037180932Smav INP_RLOCK(in6p); 2038180965Srwatson if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, 2039180965Srwatson in6p->in6p_icmp6filt)) { 2040180932Smav INP_RUNLOCK(in6p); 2041180850Smav continue; 2042180932Smav } 2043186163Skmacy if (last != NULL) { 2044121809Sume struct mbuf *n = NULL; 2045121809Sume 2046121809Sume /* 2047121809Sume * Recent network drivers tend to allocate a single 2048121809Sume * mbuf cluster, rather than to make a couple of 2049121809Sume * mbufs without clusters. Also, since the IPv6 code 2050121809Sume * path tries to avoid m_pullup(), it is highly 2051121809Sume * probable that we still have an mbuf cluster here 2052121809Sume * even though the necessary length can be stored in an 2053121809Sume * mbuf's internal buffer. 2054121809Sume * Meanwhile, the default size of the receive socket 2055121809Sume * buffer for raw sockets is not so large. This means 2056121809Sume * the possibility of packet loss is relatively higher 2057121809Sume * than before. To avoid this scenario, we copy the 2058121809Sume * received data to a separate mbuf that does not use 2059121809Sume * a cluster, if possible. 2060121809Sume * XXX: it is better to copy the data after stripping 2061121809Sume * intermediate headers. 2062121809Sume */ 2063121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 2064121809Sume m->m_len <= MHLEN) { 2065121809Sume MGET(n, M_DONTWAIT, m->m_type); 2066121809Sume if (n != NULL) { 2067145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 2068171260Sdelphij bcopy(m->m_data, n->m_data, 2069145065Sgnn m->m_len); 2070145065Sgnn n->m_len = m->m_len; 2071145065Sgnn } else { 2072145065Sgnn m_free(n); 2073145065Sgnn n = NULL; 2074145065Sgnn } 2075121809Sume } 2076121809Sume } 2077121809Sume if (n != NULL || 2078121809Sume (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 2079186223Sbz if (last->inp_flags & INP_CONTROLOPTS) 2080121674Sume ip6_savecontrol(last, n, &opts); 208153541Sshin /* strip intermediate headers */ 208253541Sshin m_adj(n, off); 2083186141Sbz SOCKBUF_LOCK(&last->inp_socket->so_rcv); 2084160591Srwatson if (sbappendaddr_locked( 2085186141Sbz &last->inp_socket->so_rcv, 2086121315Sume (struct sockaddr *)&fromsa, n, opts) 2087120891Sume == 0) { 208853541Sshin /* should notify about lost packet */ 208953541Sshin m_freem(n); 209078064Sume if (opts) { 209153541Sshin m_freem(opts); 209278064Sume } 2093160591Srwatson SOCKBUF_UNLOCK( 2094186141Sbz &last->inp_socket->so_rcv); 209597658Stanimura } else 2096186141Sbz sorwakeup_locked(last->inp_socket); 209753541Sshin opts = NULL; 209853541Sshin } 2099178378Srwatson INP_RUNLOCK(last); 210053541Sshin } 210153541Sshin last = in6p; 210253541Sshin } 2103181803Sbz INP_INFO_RUNLOCK(&V_ripcbinfo); 2104186163Skmacy if (last != NULL) { 2105186223Sbz if (last->inp_flags & INP_CONTROLOPTS) 2106121674Sume ip6_savecontrol(last, m, &opts); 210753541Sshin /* strip intermediate headers */ 210853541Sshin m_adj(m, off); 2109121809Sume 2110121809Sume /* avoid using mbuf clusters if possible (see above) */ 2111121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 2112121809Sume m->m_len <= MHLEN) { 2113121809Sume struct mbuf *n; 2114121809Sume 2115121809Sume MGET(n, M_DONTWAIT, m->m_type); 2116121809Sume if (n != NULL) { 2117145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 2118145065Sgnn bcopy(m->m_data, n->m_data, m->m_len); 2119145065Sgnn n->m_len = m->m_len; 2120171260Sdelphij 2121145065Sgnn m_freem(m); 2122145065Sgnn m = n; 2123145065Sgnn } else { 2124145065Sgnn m_freem(n); 2125145065Sgnn n = NULL; 2126145065Sgnn } 2127121809Sume } 2128121809Sume } 2129186141Sbz SOCKBUF_LOCK(&last->inp_socket->so_rcv); 2130186141Sbz if (sbappendaddr_locked(&last->inp_socket->so_rcv, 2131121315Sume (struct sockaddr *)&fromsa, m, opts) == 0) { 213253541Sshin m_freem(m); 213353541Sshin if (opts) 213453541Sshin m_freem(opts); 2135186141Sbz SOCKBUF_UNLOCK(&last->inp_socket->so_rcv); 213697658Stanimura } else 2137186141Sbz sorwakeup_locked(last->inp_socket); 2138178378Srwatson INP_RUNLOCK(last); 213953541Sshin } else { 214053541Sshin m_freem(m); 2141191672Sbms IP6STAT_DEC(ip6s_delivered); 214253541Sshin } 214353541Sshin return IPPROTO_DONE; 214453541Sshin} 214553541Sshin 214653541Sshin/* 214753541Sshin * Reflect the ip6 packet back to the source. 214862587Sitojun * OFF points to the icmp6 header, counted from the top of the mbuf. 214953541Sshin */ 215053541Sshinvoid 2151171259Sdelphijicmp6_reflect(struct mbuf *m, size_t off) 215253541Sshin{ 215362587Sitojun struct ip6_hdr *ip6; 215453541Sshin struct icmp6_hdr *icmp6; 2155194760Srwatson struct in6_ifaddr *ia = NULL; 215662587Sitojun int plen; 215753541Sshin int type, code; 215853541Sshin struct ifnet *outif = NULL; 2159194777Sbz struct in6_addr origdst, src, *srcp = NULL; 216053541Sshin 216162587Sitojun /* too short to reflect */ 216262587Sitojun if (off < sizeof(struct ip6_hdr)) { 216378064Sume nd6log((LOG_DEBUG, 216478064Sume "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 216578064Sume (u_long)off, (u_long)sizeof(struct ip6_hdr), 216678064Sume __FILE__, __LINE__)); 216762587Sitojun goto bad; 216862587Sitojun } 216962587Sitojun 217053541Sshin /* 217153541Sshin * If there are extra headers between IPv6 and ICMPv6, strip 217253541Sshin * off that header first. 217353541Sshin */ 217462587Sitojun#ifdef DIAGNOSTIC 217562587Sitojun if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) 217662587Sitojun panic("assumption failed in icmp6_reflect"); 217762587Sitojun#endif 217862587Sitojun if (off > sizeof(struct ip6_hdr)) { 217962587Sitojun size_t l; 218062587Sitojun struct ip6_hdr nip6; 218153541Sshin 218262587Sitojun l = off - sizeof(struct ip6_hdr); 218362587Sitojun m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); 218462587Sitojun m_adj(m, l); 218562587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 218662587Sitojun if (m->m_len < l) { 218762587Sitojun if ((m = m_pullup(m, l)) == NULL) 218862587Sitojun return; 218953541Sshin } 219062587Sitojun bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); 219162587Sitojun } else /* off == sizeof(struct ip6_hdr) */ { 219262587Sitojun size_t l; 219362587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 219462587Sitojun if (m->m_len < l) { 219562587Sitojun if ((m = m_pullup(m, l)) == NULL) 219662587Sitojun return; 219753541Sshin } 219853541Sshin } 219962587Sitojun plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 220062587Sitojun ip6 = mtod(m, struct ip6_hdr *); 220162587Sitojun ip6->ip6_nxt = IPPROTO_ICMPV6; 220253541Sshin icmp6 = (struct icmp6_hdr *)(ip6 + 1); 220353541Sshin type = icmp6->icmp6_type; /* keep type for statistics */ 220453541Sshin code = icmp6->icmp6_code; /* ditto. */ 220553541Sshin 2206148385Sume origdst = ip6->ip6_dst; 220753541Sshin /* 220853541Sshin * ip6_input() drops a packet if its src is multicast. 220953541Sshin * So, the src is never multicast. 221053541Sshin */ 221153541Sshin ip6->ip6_dst = ip6->ip6_src; 221253541Sshin 221353541Sshin /* 2214120891Sume * If the incoming packet was addressed directly to us (i.e. unicast), 221553541Sshin * use dst as the src for the reply. 2216120891Sume * The IN6_IFF_NOTREADY case should be VERY rare, but is possible 221762587Sitojun * (for example) when we encounter an error while forwarding procedure 221862587Sitojun * destined to a duplicated address of ours. 2219148385Sume * Note that ip6_getdstifaddr() may fail if we are in an error handling 2220148385Sume * procedure of an outgoing packet of our own, in which case we need 2221148385Sume * to search in the ifaddr list. 222253541Sshin */ 2223148385Sume if (!IN6_IS_ADDR_MULTICAST(&origdst)) { 2224148385Sume if ((ia = ip6_getdstifaddr(m))) { 2225148385Sume if (!(ia->ia6_flags & 2226148385Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) 2227194777Sbz srcp = &ia->ia_addr.sin6_addr; 2228148385Sume } else { 2229148385Sume struct sockaddr_in6 d; 2230148385Sume 2231148385Sume bzero(&d, sizeof(d)); 2232148385Sume d.sin6_family = AF_INET6; 2233148385Sume d.sin6_len = sizeof(d); 2234148385Sume d.sin6_addr = origdst; 2235148385Sume ia = (struct in6_ifaddr *) 2236148385Sume ifa_ifwithaddr((struct sockaddr *)&d); 2237148385Sume if (ia && 2238148385Sume !(ia->ia6_flags & 2239148385Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) { 2240194777Sbz srcp = &ia->ia_addr.sin6_addr; 2241148385Sume } 224253541Sshin } 224353541Sshin } 224453541Sshin 2245196865Sqingli if ((srcp != NULL) && 2246196865Sqingli (in6_addrscope(srcp) != in6_addrscope(&ip6->ip6_src))) 2247196865Sqingli srcp = NULL; 2248196865Sqingli 2249194777Sbz if (srcp == NULL) { 225078064Sume int e; 2251148385Sume struct sockaddr_in6 sin6; 225278064Sume struct route_in6 ro; 225378064Sume 225453541Sshin /* 225562587Sitojun * This case matches to multicasts, our anycast, or unicasts 225695023Ssuz * that we do not own. Select a source address based on the 225778064Sume * source address of the erroneous packet. 225853541Sshin */ 2259148385Sume bzero(&sin6, sizeof(sin6)); 2260148385Sume sin6.sin6_family = AF_INET6; 2261148385Sume sin6.sin6_len = sizeof(sin6); 2262148385Sume sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */ 2263148385Sume 226478064Sume bzero(&ro, sizeof(ro)); 2265194777Sbz e = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &src); 226678064Sume if (ro.ro_rt) 226778064Sume RTFREE(ro.ro_rt); /* XXX: we could use this */ 2268194777Sbz if (e) { 2269165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 227078064Sume nd6log((LOG_DEBUG, 227178064Sume "icmp6_reflect: source can't be determined: " 227278064Sume "dst=%s, error=%d\n", 2273165118Sbz ip6_sprintf(ip6buf, &sin6.sin6_addr), e)); 227478064Sume goto bad; 227578064Sume } 2276194777Sbz srcp = &src; 227778064Sume } 227853541Sshin 2279194777Sbz ip6->ip6_src = *srcp; 228053541Sshin ip6->ip6_flow = 0; 228162587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 228262587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 228353541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 2284121472Sume if (outif) 2285121472Sume ip6->ip6_hlim = ND_IFINFO(outif)->chlim; 2286121472Sume else if (m->m_pkthdr.rcvif) { 228753541Sshin /* XXX: This may not be the outgoing interface */ 2288121161Sume ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim; 228978064Sume } else 2290181803Sbz ip6->ip6_hlim = V_ip6_defhlim; 229153541Sshin 229253541Sshin icmp6->icmp6_cksum = 0; 229353541Sshin icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2294120891Sume sizeof(struct ip6_hdr), plen); 229553541Sshin 229653541Sshin /* 229778064Sume * XXX option handling 229853541Sshin */ 229953541Sshin 230053541Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 230153541Sshin 2302105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 230353541Sshin if (outif) 230453541Sshin icmp6_ifoutstat_inc(outif, type, code); 230553541Sshin 2306194760Srwatson if (ia != NULL) 2307194760Srwatson ifa_free(&ia->ia_ifa); 230853541Sshin return; 230953541Sshin 231053541Sshin bad: 2311194760Srwatson if (ia != NULL) 2312194760Srwatson ifa_free(&ia->ia_ifa); 231353541Sshin m_freem(m); 231453541Sshin return; 231553541Sshin} 231653541Sshin 231753541Sshinvoid 2318171259Sdelphijicmp6_fasttimo(void) 231953541Sshin{ 232062587Sitojun 2321191672Sbms mld_fasttimo(); 232253541Sshin} 232353541Sshin 2324191672Sbmsvoid 2325191672Sbmsicmp6_slowtimo(void) 2326191672Sbms{ 2327191672Sbms 2328191672Sbms mld_slowtimo(); 2329191672Sbms} 2330191672Sbms 233153541Sshinstatic const char * 2332171259Sdelphijicmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6, 2333171259Sdelphij struct in6_addr *tgt6) 233453541Sshin{ 233553541Sshin static char buf[1024]; 2336165118Sbz char ip6bufs[INET6_ADDRSTRLEN]; 2337165118Sbz char ip6bufd[INET6_ADDRSTRLEN]; 2338165118Sbz char ip6buft[INET6_ADDRSTRLEN]; 233953541Sshin snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", 2340165118Sbz ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6), 2341165118Sbz ip6_sprintf(ip6buft, tgt6)); 234253541Sshin return buf; 234353541Sshin} 234453541Sshin 234553541Sshinvoid 2346171259Sdelphijicmp6_redirect_input(struct mbuf *m, int off) 234753541Sshin{ 2348171133Sgnn struct ifnet *ifp; 234953541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 235062587Sitojun struct nd_redirect *nd_rd; 235153541Sshin int icmp6len = ntohs(ip6->ip6_plen); 235253541Sshin char *lladdr = NULL; 235353541Sshin int lladdrlen = 0; 235453541Sshin u_char *redirhdr = NULL; 235553541Sshin int redirhdrlen = 0; 235653541Sshin struct rtentry *rt = NULL; 235753541Sshin int is_router; 235853541Sshin int is_onlink; 235953541Sshin struct in6_addr src6 = ip6->ip6_src; 236062587Sitojun struct in6_addr redtgt6; 236162587Sitojun struct in6_addr reddst6; 236253541Sshin union nd_opts ndopts; 2363165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 236453541Sshin 2365171133Sgnn if (!m) 236653541Sshin return; 236753541Sshin 2368171133Sgnn ifp = m->m_pkthdr.rcvif; 2369171133Sgnn 2370171133Sgnn if (!ifp) 2371171133Sgnn return; 2372171133Sgnn 237353541Sshin /* XXX if we are router, we don't update route by icmp6 redirect */ 2374181803Sbz if (V_ip6_forwarding) 237562587Sitojun goto freeit; 2376181803Sbz if (!V_icmp6_rediraccept) 237762587Sitojun goto freeit; 237862587Sitojun 237962587Sitojun#ifndef PULLDOWN_TEST 238062587Sitojun IP6_EXTHDR_CHECK(m, off, icmp6len,); 238162587Sitojun nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); 238262587Sitojun#else 238362587Sitojun IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); 238462587Sitojun if (nd_rd == NULL) { 2385190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 238653541Sshin return; 238762587Sitojun } 238862587Sitojun#endif 238962587Sitojun redtgt6 = nd_rd->nd_rd_target; 239062587Sitojun reddst6 = nd_rd->nd_rd_dst; 239153541Sshin 2392148385Sume if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) || 2393148385Sume in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) { 2394148385Sume goto freeit; 2395148385Sume } 239653541Sshin 239753541Sshin /* validation */ 239853541Sshin if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 239978064Sume nd6log((LOG_ERR, 2400120891Sume "ICMP6 redirect sent from %s rejected; " 2401120891Sume "must be from linklocal\n", 2402165118Sbz ip6_sprintf(ip6buf, &src6))); 240378064Sume goto bad; 240453541Sshin } 240553541Sshin if (ip6->ip6_hlim != 255) { 240678064Sume nd6log((LOG_ERR, 2407120891Sume "ICMP6 redirect sent from %s rejected; " 2408120891Sume "hlim=%d (must be 255)\n", 2409165118Sbz ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim)); 241078064Sume goto bad; 241153541Sshin } 241253541Sshin { 241353541Sshin /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 241453541Sshin struct sockaddr_in6 sin6; 241553541Sshin struct in6_addr *gw6; 241653541Sshin 241753541Sshin bzero(&sin6, sizeof(sin6)); 241853541Sshin sin6.sin6_family = AF_INET6; 241953541Sshin sin6.sin6_len = sizeof(struct sockaddr_in6); 242053541Sshin bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); 242153541Sshin rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 242253541Sshin if (rt) { 242365895Sume if (rt->rt_gateway == NULL || 242465895Sume rt->rt_gateway->sa_family != AF_INET6) { 242578064Sume nd6log((LOG_ERR, 242665895Sume "ICMP6 redirect rejected; no route " 242765895Sume "with inet6 gateway found for redirect dst: %s\n", 242878064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2429120727Ssam RTFREE_LOCKED(rt); 243078064Sume goto bad; 243165895Sume } 243265895Sume 243353541Sshin gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); 243453541Sshin if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { 243578064Sume nd6log((LOG_ERR, 2436120891Sume "ICMP6 redirect rejected; " 2437120891Sume "not equal to gw-for-src=%s (must be same): " 2438120891Sume "%s\n", 2439165118Sbz ip6_sprintf(ip6buf, gw6), 2440120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2441120727Ssam RTFREE_LOCKED(rt); 244278064Sume goto bad; 244353541Sshin } 244453541Sshin } else { 244578064Sume nd6log((LOG_ERR, 2446120891Sume "ICMP6 redirect rejected; " 2447120891Sume "no route found for redirect dst: %s\n", 2448120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 244978064Sume goto bad; 245053541Sshin } 2451120727Ssam RTFREE_LOCKED(rt); 245253541Sshin rt = NULL; 245353541Sshin } 245453541Sshin if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 245578064Sume nd6log((LOG_ERR, 2456120891Sume "ICMP6 redirect rejected; " 2457120891Sume "redirect dst must be unicast: %s\n", 2458120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 245978064Sume goto bad; 246053541Sshin } 246153541Sshin 246253541Sshin is_router = is_onlink = 0; 246353541Sshin if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 246453541Sshin is_router = 1; /* router case */ 246553541Sshin if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 246653541Sshin is_onlink = 1; /* on-link destination case */ 246753541Sshin if (!is_router && !is_onlink) { 246878064Sume nd6log((LOG_ERR, 2469120891Sume "ICMP6 redirect rejected; " 2470120891Sume "neither router case nor onlink case: %s\n", 2471120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 247278064Sume goto bad; 247353541Sshin } 247453541Sshin /* validation passed */ 247553541Sshin 247653541Sshin icmp6len -= sizeof(*nd_rd); 247753541Sshin nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 247853541Sshin if (nd6_options(&ndopts) < 0) { 247978064Sume nd6log((LOG_INFO, "icmp6_redirect_input: " 2480120891Sume "invalid ND option, rejected: %s\n", 2481120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 248278064Sume /* nd6_options have incremented stats */ 248362587Sitojun goto freeit; 248453541Sshin } 248553541Sshin 248653541Sshin if (ndopts.nd_opts_tgt_lladdr) { 248753541Sshin lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 248853541Sshin lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 248953541Sshin } 249053541Sshin 249153541Sshin if (ndopts.nd_opts_rh) { 249253541Sshin redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len; 249353541Sshin redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */ 249453541Sshin } 249553541Sshin 249653541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 249778064Sume nd6log((LOG_INFO, 2498120891Sume "icmp6_redirect_input: lladdrlen mismatch for %s " 2499120891Sume "(if %d, icmp6 packet %d): %s\n", 2500165118Sbz ip6_sprintf(ip6buf, &redtgt6), 2501165118Sbz ifp->if_addrlen, lladdrlen - 2, 2502120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 250378064Sume goto bad; 250453541Sshin } 250553541Sshin 250653541Sshin /* RFC 2461 8.3 */ 250753541Sshin nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 2508120891Sume is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 250953541Sshin 251095023Ssuz if (!is_onlink) { /* better router case. perform rtredirect. */ 251153541Sshin /* perform rtredirect */ 251253541Sshin struct sockaddr_in6 sdst; 251353541Sshin struct sockaddr_in6 sgw; 251453541Sshin struct sockaddr_in6 ssrc; 251553541Sshin 251653541Sshin bzero(&sdst, sizeof(sdst)); 251753541Sshin bzero(&sgw, sizeof(sgw)); 251853541Sshin bzero(&ssrc, sizeof(ssrc)); 251953541Sshin sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; 252053541Sshin sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = 252153541Sshin sizeof(struct sockaddr_in6); 252253541Sshin bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); 252353541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 252453541Sshin bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 252553541Sshin rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw, 2526120891Sume (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST, 2527120891Sume (struct sockaddr *)&ssrc); 252853541Sshin } 252953541Sshin /* finally update cached route in each socket via pfctlinput */ 253053541Sshin { 253153541Sshin struct sockaddr_in6 sdst; 253253541Sshin 253353541Sshin bzero(&sdst, sizeof(sdst)); 253453541Sshin sdst.sin6_family = AF_INET6; 253553541Sshin sdst.sin6_len = sizeof(struct sockaddr_in6); 253653541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 253753541Sshin pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); 2538171167Sgnn#ifdef IPSEC 253953541Sshin key_sa_routechange((struct sockaddr *)&sdst); 2540171167Sgnn#endif /* IPSEC */ 254153541Sshin } 254262587Sitojun 254362587Sitojun freeit: 254462587Sitojun m_freem(m); 254578064Sume return; 254678064Sume 254778064Sume bad: 2548190964Srwatson ICMP6STAT_INC(icp6s_badredirect); 254978064Sume m_freem(m); 255053541Sshin} 255153541Sshin 255253541Sshinvoid 2553171259Sdelphijicmp6_redirect_output(struct mbuf *m0, struct rtentry *rt) 255453541Sshin{ 255553541Sshin struct ifnet *ifp; /* my outgoing interface */ 255653541Sshin struct in6_addr *ifp_ll6; 255753541Sshin struct in6_addr *router_ll6; 255853541Sshin struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 255953541Sshin struct mbuf *m = NULL; /* newly allocated one */ 2560211501Sanchie struct m_tag *mtag; 256153541Sshin struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 256253541Sshin struct nd_redirect *nd_rd; 2563186157Skmacy struct llentry *ln = NULL; 256453541Sshin size_t maxlen; 256553541Sshin u_char *p; 256653541Sshin struct ifnet *outif = NULL; 256762587Sitojun struct sockaddr_in6 src_sa; 256853541Sshin 2569181803Sbz icmp6_errcount(&V_icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0); 257062587Sitojun 257153541Sshin /* if we are not router, we don't send icmp6 redirect */ 2572181803Sbz if (!V_ip6_forwarding) 257353541Sshin goto fail; 257453541Sshin 257553541Sshin /* sanity check */ 257653541Sshin if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) 257753541Sshin goto fail; 257853541Sshin 257953541Sshin /* 258053541Sshin * Address check: 258153541Sshin * the source address must identify a neighbor, and 258253541Sshin * the destination address must not be a multicast address 258353541Sshin * [RFC 2461, sec 8.2] 258453541Sshin */ 258553541Sshin sip6 = mtod(m0, struct ip6_hdr *); 258662587Sitojun bzero(&src_sa, sizeof(src_sa)); 258762587Sitojun src_sa.sin6_family = AF_INET6; 258862587Sitojun src_sa.sin6_len = sizeof(src_sa); 258962587Sitojun src_sa.sin6_addr = sip6->ip6_src; 259062587Sitojun if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) 259153541Sshin goto fail; 259253541Sshin if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) 259353541Sshin goto fail; /* what should we do here? */ 259453541Sshin 259553541Sshin /* rate limit */ 259653541Sshin if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) 259753541Sshin goto fail; 259853541Sshin 259953541Sshin /* 260053541Sshin * Since we are going to append up to 1280 bytes (= IPV6_MMTU), 260153541Sshin * we almost always ask for an mbuf cluster for simplicity. 260253541Sshin * (MHLEN < IPV6_MMTU is almost always true) 260353541Sshin */ 260462587Sitojun#if IPV6_MMTU >= MCLBYTES 260562587Sitojun# error assumption failed about IPV6_MMTU and MCLBYTES 260662587Sitojun#endif 2607111119Simp MGETHDR(m, M_DONTWAIT, MT_HEADER); 260862587Sitojun if (m && IPV6_MMTU >= MHLEN) 2609111119Simp MCLGET(m, M_DONTWAIT); 261053541Sshin if (!m) 261153541Sshin goto fail; 261278064Sume m->m_pkthdr.rcvif = NULL; 261378064Sume m->m_len = 0; 261478064Sume maxlen = M_TRAILINGSPACE(m); 261553541Sshin maxlen = min(IPV6_MMTU, maxlen); 261653541Sshin /* just for safety */ 261762587Sitojun if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 261862587Sitojun ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { 261953541Sshin goto fail; 262062587Sitojun } 262153541Sshin 262253541Sshin { 262353541Sshin /* get ip6 linklocal address for ifp(my outgoing interface). */ 262462587Sitojun struct in6_ifaddr *ia; 262562587Sitojun if ((ia = in6ifa_ifpforlinklocal(ifp, 262662587Sitojun IN6_IFF_NOTREADY| 262762587Sitojun IN6_IFF_ANYCAST)) == NULL) 262853541Sshin goto fail; 262953541Sshin ifp_ll6 = &ia->ia_addr.sin6_addr; 2630194760Srwatson /* XXXRW: reference released prematurely. */ 2631194760Srwatson ifa_free(&ia->ia_ifa); 263253541Sshin } 263353541Sshin 263453541Sshin /* get ip6 linklocal address for the router. */ 263553541Sshin if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { 263653541Sshin struct sockaddr_in6 *sin6; 263753541Sshin sin6 = (struct sockaddr_in6 *)rt->rt_gateway; 263853541Sshin router_ll6 = &sin6->sin6_addr; 263953541Sshin if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) 264053541Sshin router_ll6 = (struct in6_addr *)NULL; 264153541Sshin } else 264253541Sshin router_ll6 = (struct in6_addr *)NULL; 264353541Sshin 264453541Sshin /* ip6 */ 264553541Sshin ip6 = mtod(m, struct ip6_hdr *); 264653541Sshin ip6->ip6_flow = 0; 264762587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 264862587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 264953541Sshin /* ip6->ip6_plen will be set later */ 265053541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 265153541Sshin ip6->ip6_hlim = 255; 265253541Sshin /* ip6->ip6_src must be linklocal addr for my outgoing if. */ 265353541Sshin bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); 265453541Sshin bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); 265553541Sshin 265653541Sshin /* ND Redirect */ 265753541Sshin nd_rd = (struct nd_redirect *)(ip6 + 1); 265853541Sshin nd_rd->nd_rd_type = ND_REDIRECT; 265953541Sshin nd_rd->nd_rd_code = 0; 266053541Sshin nd_rd->nd_rd_reserved = 0; 266153541Sshin if (rt->rt_flags & RTF_GATEWAY) { 266253541Sshin /* 266353541Sshin * nd_rd->nd_rd_target must be a link-local address in 266453541Sshin * better router cases. 266553541Sshin */ 266653541Sshin if (!router_ll6) 266753541Sshin goto fail; 266853541Sshin bcopy(router_ll6, &nd_rd->nd_rd_target, 2669120892Sume sizeof(nd_rd->nd_rd_target)); 267053541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2671120892Sume sizeof(nd_rd->nd_rd_dst)); 267253541Sshin } else { 267353541Sshin /* make sure redtgt == reddst */ 267453541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, 2675120892Sume sizeof(nd_rd->nd_rd_target)); 267653541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2677120892Sume sizeof(nd_rd->nd_rd_dst)); 267853541Sshin } 267953541Sshin 268053541Sshin p = (u_char *)(nd_rd + 1); 268153541Sshin 268253541Sshin if (!router_ll6) 268353541Sshin goto nolladdropt; 268453541Sshin 2685120892Sume { 2686120892Sume /* target lladdr option */ 2687120892Sume int len; 2688120892Sume struct nd_opt_hdr *nd_opt; 2689120892Sume char *lladdr; 269053541Sshin 2691186119Sqingli IF_AFDATA_LOCK(ifp); 2692186119Sqingli ln = nd6_lookup(router_ll6, 0, ifp); 2693186119Sqingli IF_AFDATA_UNLOCK(ifp); 2694186157Skmacy if (ln == NULL) 2695120892Sume goto nolladdropt; 2696186119Sqingli 2697120892Sume len = sizeof(*nd_opt) + ifp->if_addrlen; 2698120892Sume len = (len + 7) & ~7; /* round by 8 */ 2699120892Sume /* safety check */ 2700186157Skmacy if (len + (p - (u_char *)ip6) > maxlen) 2701120892Sume goto nolladdropt; 2702186119Sqingli 2703186119Sqingli if (ln->la_flags & LLE_VALID) { 2704120892Sume nd_opt = (struct nd_opt_hdr *)p; 2705120892Sume nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 2706120892Sume nd_opt->nd_opt_len = len >> 3; 2707120892Sume lladdr = (char *)(nd_opt + 1); 2708186119Sqingli bcopy(&ln->ll_addr, lladdr, ifp->if_addrlen); 2709120892Sume p += len; 2710120892Sume } 2711186157Skmacy } 2712186157Skmacynolladdropt: 2713186157Skmacy if (ln != NULL) 2714186119Sqingli LLE_RUNLOCK(ln); 2715186157Skmacy 271653541Sshin m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 271753541Sshin 271853541Sshin /* just to be safe */ 271962587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 272053541Sshin if (m0->m_flags & M_DECRYPTED) 272153541Sshin goto noredhdropt; 272262587Sitojun#endif 272362587Sitojun if (p - (u_char *)ip6 > maxlen) 272462587Sitojun goto noredhdropt; 272553541Sshin 2726120891Sume { 2727120891Sume /* redirected header option */ 2728120891Sume int len; 2729120891Sume struct nd_opt_rd_hdr *nd_opt_rh; 273053541Sshin 2731120891Sume /* 2732120891Sume * compute the maximum size for icmp6 redirect header option. 2733120891Sume * XXX room for auth header? 2734120891Sume */ 2735120891Sume len = maxlen - (p - (u_char *)ip6); 2736120891Sume len &= ~7; 273753541Sshin 2738120891Sume /* This is just for simplicity. */ 2739120891Sume if (m0->m_pkthdr.len != m0->m_len) { 2740120891Sume if (m0->m_next) { 2741120891Sume m_freem(m0->m_next); 2742120891Sume m0->m_next = NULL; 2743120891Sume } 2744120891Sume m0->m_pkthdr.len = m0->m_len; 274553541Sshin } 274653541Sshin 2747120891Sume /* 2748120891Sume * Redirected header option spec (RFC2461 4.6.3) talks nothing 2749120891Sume * about padding/truncate rule for the original IP packet. 2750120891Sume * From the discussion on IPv6imp in Feb 1999, 2751120891Sume * the consensus was: 2752120891Sume * - "attach as much as possible" is the goal 2753120891Sume * - pad if not aligned (original size can be guessed by 2754120891Sume * original ip6 header) 2755120891Sume * Following code adds the padding if it is simple enough, 2756120891Sume * and truncates if not. 2757120891Sume */ 2758120891Sume if (m0->m_next || m0->m_pkthdr.len != m0->m_len) 2759120891Sume panic("assumption failed in %s:%d", __FILE__, 2760120891Sume __LINE__); 276153541Sshin 2762120891Sume if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { 2763120891Sume /* not enough room, truncate */ 2764120891Sume m0->m_pkthdr.len = m0->m_len = len - 2765120891Sume sizeof(*nd_opt_rh); 2766120891Sume } else { 2767120891Sume /* enough room, pad or truncate */ 2768120891Sume size_t extra; 276953541Sshin 2770120891Sume extra = m0->m_pkthdr.len % 8; 2771120891Sume if (extra) { 2772120891Sume /* pad if easy enough, truncate if not */ 2773120891Sume if (8 - extra <= M_TRAILINGSPACE(m0)) { 2774120891Sume /* pad */ 2775120891Sume m0->m_len += (8 - extra); 2776120891Sume m0->m_pkthdr.len += (8 - extra); 2777120891Sume } else { 2778120891Sume /* truncate */ 2779120891Sume m0->m_pkthdr.len -= extra; 2780120891Sume m0->m_len -= extra; 2781120891Sume } 278253541Sshin } 2783120891Sume len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); 2784120891Sume m0->m_pkthdr.len = m0->m_len = len - 2785120891Sume sizeof(*nd_opt_rh); 278653541Sshin } 278753541Sshin 2788120891Sume nd_opt_rh = (struct nd_opt_rd_hdr *)p; 2789120891Sume bzero(nd_opt_rh, sizeof(*nd_opt_rh)); 2790120891Sume nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; 2791120891Sume nd_opt_rh->nd_opt_rh_len = len >> 3; 2792120891Sume p += sizeof(*nd_opt_rh); 2793120891Sume m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 279453541Sshin 2795120891Sume /* connect m0 to m */ 2796120891Sume m_tag_delete_chain(m0, NULL); 2797120891Sume m0->m_flags &= ~M_PKTHDR; 2798120891Sume m->m_next = m0; 2799120891Sume m->m_pkthdr.len = m->m_len + m0->m_len; 2800120891Sume m0 = NULL; 2801120891Sume } 280253541Sshinnoredhdropt:; 2803112781Ssuz if (m0) { 2804112781Ssuz m_freem(m0); 2805112781Ssuz m0 = NULL; 2806112781Ssuz } 280753541Sshin 2808121315Sume /* XXX: clear embedded link IDs in the inner header */ 2809121315Sume in6_clearscope(&sip6->ip6_src); 2810121315Sume in6_clearscope(&sip6->ip6_dst); 2811121315Sume in6_clearscope(&nd_rd->nd_rd_target); 2812121315Sume in6_clearscope(&nd_rd->nd_rd_dst); 281353541Sshin 281453541Sshin ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 281553541Sshin 281653541Sshin nd_rd->nd_rd_cksum = 0; 2817120891Sume nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2818120891Sume sizeof(*ip6), ntohs(ip6->ip6_plen)); 281953541Sshin 2820211501Sanchie if (send_sendso_input_hook != NULL) { 2821211501Sanchie mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, sizeof(unsigned short), 2822211501Sanchie M_NOWAIT); 2823211501Sanchie if (mtag == NULL) 2824211501Sanchie goto fail; 2825211501Sanchie *(unsigned short *)(mtag + 1) = nd_rd->nd_rd_type; 2826211501Sanchie m_tag_prepend(m, mtag); 2827211501Sanchie } 2828211501Sanchie 282953541Sshin /* send the packet to outside... */ 2830105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 283153541Sshin if (outif) { 283253541Sshin icmp6_ifstat_inc(outif, ifs6_out_msg); 283353541Sshin icmp6_ifstat_inc(outif, ifs6_out_redirect); 283453541Sshin } 2835190964Srwatson ICMP6STAT_INC(icp6s_outhist[ND_REDIRECT]); 283653541Sshin 283753541Sshin return; 283853541Sshin 283953541Sshinfail: 284053541Sshin if (m) 284153541Sshin m_freem(m); 284253541Sshin if (m0) 284353541Sshin m_freem(m0); 284453541Sshin} 284553541Sshin 284653541Sshin/* 284753541Sshin * ICMPv6 socket option processing. 284853541Sshin */ 284953541Sshinint 2850171259Sdelphijicmp6_ctloutput(struct socket *so, struct sockopt *sopt) 285153541Sshin{ 285253541Sshin int error = 0; 285353541Sshin int optlen; 285478064Sume struct inpcb *inp = sotoinpcb(so); 285553541Sshin int level, op, optname; 285653541Sshin 285753541Sshin if (sopt) { 285853541Sshin level = sopt->sopt_level; 285953541Sshin op = sopt->sopt_dir; 286053541Sshin optname = sopt->sopt_name; 286153541Sshin optlen = sopt->sopt_valsize; 286253541Sshin } else 286353541Sshin level = op = optname = optlen = 0; 286478064Sume 286553541Sshin if (level != IPPROTO_ICMPV6) { 286653541Sshin return EINVAL; 286753541Sshin } 286853541Sshin 286978064Sume switch (op) { 287053541Sshin case PRCO_SETOPT: 287153541Sshin switch (optname) { 287253541Sshin case ICMP6_FILTER: 287353541Sshin { 2874180968Srwatson struct icmp6_filter ic6f; 287553541Sshin 2876180968Srwatson if (optlen != sizeof(ic6f)) { 287753541Sshin error = EMSGSIZE; 287853541Sshin break; 287953541Sshin } 2880180968Srwatson error = sooptcopyin(sopt, &ic6f, optlen, optlen); 2881180968Srwatson if (error == 0) { 2882180968Srwatson INP_WLOCK(inp); 2883180968Srwatson *inp->in6p_icmp6filt = ic6f; 2884180968Srwatson INP_WUNLOCK(inp); 2885180968Srwatson } 288653541Sshin break; 288753541Sshin } 288853541Sshin 288953541Sshin default: 289053541Sshin error = ENOPROTOOPT; 289153541Sshin break; 289253541Sshin } 289353541Sshin break; 289453541Sshin 289553541Sshin case PRCO_GETOPT: 289653541Sshin switch (optname) { 289753541Sshin case ICMP6_FILTER: 289853541Sshin { 2899180968Srwatson struct icmp6_filter ic6f; 2900180968Srwatson 2901180968Srwatson INP_RLOCK(inp); 2902180968Srwatson ic6f = *inp->in6p_icmp6filt; 2903180968Srwatson INP_RUNLOCK(inp); 2904180968Srwatson error = sooptcopyout(sopt, &ic6f, sizeof(ic6f)); 290553541Sshin break; 290653541Sshin } 290753541Sshin 290853541Sshin default: 290953541Sshin error = ENOPROTOOPT; 291053541Sshin break; 291153541Sshin } 291253541Sshin break; 291353541Sshin } 291453541Sshin 2915120856Sume return (error); 291653541Sshin} 291753541Sshin 291853541Sshin/* 291953541Sshin * Perform rate limit check. 292053541Sshin * Returns 0 if it is okay to send the icmp6 packet. 292153541Sshin * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 292253541Sshin * limitation. 292353541Sshin * 292453541Sshin * XXX per-destination/type check necessary? 2925171259Sdelphij * 2926171259Sdelphij * dst - not used at this moment 2927171259Sdelphij * type - not used at this moment 2928171259Sdelphij * code - not used at this moment 292953541Sshin */ 293053541Sshinstatic int 2931171259Sdelphijicmp6_ratelimit(const struct in6_addr *dst, const int type, 2932171259Sdelphij const int code) 293353541Sshin{ 293462587Sitojun int ret; 293553541Sshin 293695023Ssuz ret = 0; /* okay to send */ 293753541Sshin 293862587Sitojun /* PPS limit */ 2939181803Sbz if (!ppsratecheck(&V_icmp6errppslim_last, &V_icmp6errpps_count, 2940181803Sbz V_icmp6errppslim)) { 294153541Sshin /* The packet is subject to rate limit */ 294262587Sitojun ret++; 294353541Sshin } 294453541Sshin 294562587Sitojun return ret; 294653541Sshin} 2947