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$"); 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 133252563Saestatic void icmp6_errcount(int, int); 134175162Sobrienstatic int icmp6_rip6_input(struct mbuf **, int); 135175162Sobrienstatic int icmp6_ratelimit(const struct in6_addr *, const int, const int); 136244524Sdelphijstatic const char *icmp6_redirect_diag(struct in6_addr *, 137244524Sdelphij 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); 141244524Sdelphijstatic int ni6_addrs(struct icmp6_nodeinfo *, struct mbuf *, 142244524Sdelphij struct ifnet **, struct in6_addr *); 143244524Sdelphijstatic int ni6_store_addrs(struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, 144244524Sdelphij 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 162252563Saeicmp6_errcount(int type, int code) 16362587Sitojun{ 16478064Sume switch (type) { 16562587Sitojun case ICMP6_DST_UNREACH: 16662587Sitojun switch (code) { 16762587Sitojun case ICMP6_DST_UNREACH_NOROUTE: 168252563Sae ICMP6STAT_INC(icp6s_odst_unreach_noroute); 16962587Sitojun return; 17062587Sitojun case ICMP6_DST_UNREACH_ADMIN: 171252563Sae ICMP6STAT_INC(icp6s_odst_unreach_admin); 17262587Sitojun return; 17362587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 174252563Sae ICMP6STAT_INC(icp6s_odst_unreach_beyondscope); 17562587Sitojun return; 17662587Sitojun case ICMP6_DST_UNREACH_ADDR: 177252563Sae ICMP6STAT_INC(icp6s_odst_unreach_addr); 17862587Sitojun return; 17962587Sitojun case ICMP6_DST_UNREACH_NOPORT: 180252563Sae ICMP6STAT_INC(icp6s_odst_unreach_noport); 18162587Sitojun return; 18262587Sitojun } 18362587Sitojun break; 18462587Sitojun case ICMP6_PACKET_TOO_BIG: 185252563Sae ICMP6STAT_INC(icp6s_opacket_too_big); 18662587Sitojun return; 18762587Sitojun case ICMP6_TIME_EXCEEDED: 18878064Sume switch (code) { 18962587Sitojun case ICMP6_TIME_EXCEED_TRANSIT: 190252563Sae ICMP6STAT_INC(icp6s_otime_exceed_transit); 19162587Sitojun return; 19262587Sitojun case ICMP6_TIME_EXCEED_REASSEMBLY: 193252563Sae ICMP6STAT_INC(icp6s_otime_exceed_reassembly); 19462587Sitojun return; 19562587Sitojun } 19662587Sitojun break; 19762587Sitojun case ICMP6_PARAM_PROB: 19878064Sume switch (code) { 19962587Sitojun case ICMP6_PARAMPROB_HEADER: 200252563Sae ICMP6STAT_INC(icp6s_oparamprob_header); 20162587Sitojun return; 20262587Sitojun case ICMP6_PARAMPROB_NEXTHEADER: 203252563Sae ICMP6STAT_INC(icp6s_oparamprob_nextheader); 20462587Sitojun return; 20562587Sitojun case ICMP6_PARAMPROB_OPTION: 206252563Sae ICMP6STAT_INC(icp6s_oparamprob_option); 20762587Sitojun return; 20862587Sitojun } 20962587Sitojun break; 21062587Sitojun case ND_REDIRECT: 211252563Sae ICMP6STAT_INC(icp6s_oredirect); 21262587Sitojun return; 21362587Sitojun } 214252563Sae ICMP6STAT_INC(icp6s_ounknown); 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 */ 265252563Sae icmp6_errcount(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); 363232292Sbz M_PREPEND(m, preplen, M_DONTWAIT); /* FIB is also copied over. */ 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) 587232292Sbz M_MOVE_PKTHDR(n, n0); /* FIB copied. */ 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 1180223637Sbz m_addr_changed(m); 1181223637Sbz 118262587Sitojun if (icmp6type == ICMP6_PACKET_TOO_BIG) { 118378064Sume notifymtu = ntohl(icmp6->icmp6_mtu); 118478064Sume ip6cp.ip6c_cmdarg = (void *)¬ifymtu; 118578064Sume icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ 118662587Sitojun } 118762587Sitojun 1188175162Sobrien ctlfunc = (void (*)(int, struct sockaddr *, void *)) 1189120891Sume (inet6sw[ip6_protox[nxt]].pr_ctlinput); 119053541Sshin if (ctlfunc) { 119178064Sume (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, 1192120891Sume &ip6cp); 119353541Sshin } 119453541Sshin } 1195142681Sume *mp = m; 1196120856Sume return (0); 119753541Sshin 119878064Sume freeit: 119953541Sshin m_freem(m); 1200120856Sume return (-1); 120153541Sshin} 120253541Sshin 120378064Sumevoid 1204171259Sdelphijicmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated) 120562587Sitojun{ 120678064Sume struct in6_addr *dst = ip6cp->ip6c_finaldst; 120778064Sume struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; 120878064Sume struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ 120962587Sitojun u_int mtu = ntohl(icmp6->icmp6_mtu); 1210122922Sandre struct in_conninfo inc; 121162587Sitojun 1212121472Sume#if 0 1213121472Sume /* 1214121472Sume * RFC2460 section 5, last paragraph. 1215121472Sume * even though minimum link MTU for IPv6 is IPV6_MMTU, 1216121472Sume * we may see ICMPv6 too big with mtu < IPV6_MMTU 1217121472Sume * due to packet translator in the middle. 1218121472Sume * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for 1219121472Sume * special handling. 1220121472Sume */ 1221121472Sume if (mtu < IPV6_MMTU) 1222121472Sume return; 1223121472Sume#endif 1224121472Sume 1225121472Sume /* 1226121472Sume * we reject ICMPv6 too big with abnormally small value. 1227121472Sume * XXX what is the good definition of "abnormally small"? 1228121472Sume */ 1229121472Sume if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) 1230121472Sume return; 1231121472Sume 123278064Sume if (!validated) 123378064Sume return; 123478064Sume 1235182740Ssimon /* 1236182740Ssimon * In case the suggested mtu is less than IPV6_MMTU, we 1237182740Ssimon * only need to remember that it was for above mentioned 1238182740Ssimon * "alwaysfrag" case. 1239182740Ssimon * Try to be as close to the spec as possible. 1240182740Ssimon */ 1241182740Ssimon if (mtu < IPV6_MMTU) 1242182740Ssimon mtu = IPV6_MMTU - 8; 1243182740Ssimon 1244122922Sandre bzero(&inc, sizeof(inc)); 1245257962Sae inc.inc_fibnum = M_GETFIB(m); 1246186222Sbz inc.inc_flags |= INC_ISIPV6; 1247122922Sandre inc.inc6_faddr = *dst; 1248148385Sume if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL)) 1249148385Sume return; 125062587Sitojun 1251162084Sandre if (mtu < tcp_maxmtu6(&inc, NULL)) { 1252122922Sandre tcp_hc_updatemtu(&inc, mtu); 1253190964Srwatson ICMP6STAT_INC(icp6s_pmtuchg); 125462587Sitojun } 125562587Sitojun} 125662587Sitojun 125753541Sshin/* 125878064Sume * Process a Node Information Query packet, based on 125978064Sume * draft-ietf-ipngwg-icmp-name-lookups-07. 1260120891Sume * 126162587Sitojun * Spec incompatibilities: 126262587Sitojun * - IPv6 Subject address handling 126362587Sitojun * - IPv4 Subject address handling support missing 126462587Sitojun * - Proxy reply (answer even if it's not for me) 126562587Sitojun * - joins NI group address at in6_ifattach() time only, does not cope 126662587Sitojun * with hostname changes by sethostname(3) 126753541Sshin */ 126853541Sshinstatic struct mbuf * 1269171259Sdelphijni6_input(struct mbuf *m, int off) 127053541Sshin{ 127162587Sitojun struct icmp6_nodeinfo *ni6, *nni6; 127253541Sshin struct mbuf *n = NULL; 1273193066Sjamie struct prison *pr; 127462587Sitojun u_int16_t qtype; 127562587Sitojun int subjlen; 127653541Sshin int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 127753541Sshin struct ni_reply_fqdn *fqdn; 127853541Sshin int addrs; /* for NI_QTYPE_NODEADDR */ 127953541Sshin struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ 1280148385Sume struct in6_addr in6_subj; /* subject address */ 128162587Sitojun struct ip6_hdr *ip6; 128262587Sitojun int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ 128378064Sume char *subj = NULL; 128478064Sume struct in6_ifaddr *ia6 = NULL; 128553541Sshin 128662587Sitojun ip6 = mtod(m, struct ip6_hdr *); 128762587Sitojun#ifndef PULLDOWN_TEST 128862587Sitojun ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); 128962587Sitojun#else 129062587Sitojun IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); 129162587Sitojun if (ni6 == NULL) { 129262587Sitojun /* m is already reclaimed */ 1293120891Sume return (NULL); 129462587Sitojun } 129562587Sitojun#endif 129653541Sshin 129762587Sitojun /* 1298169664Sjinmei * Validate IPv6 source address. 1299169664Sjinmei * The default configuration MUST be to refuse answering queries from 1300169664Sjinmei * global-scope addresses according to RFC4602. 1301169664Sjinmei * Notes: 1302169664Sjinmei * - it's not very clear what "refuse" means; this implementation 1303169664Sjinmei * simply drops it. 1304169664Sjinmei * - it's not very easy to identify global-scope (unicast) addresses 1305169664Sjinmei * since there are many prefixes for them. It should be safer 1306169664Sjinmei * and in practice sufficient to check "all" but loopback and 1307169664Sjinmei * link-local (note that site-local unicast was deprecated and 1308169664Sjinmei * ULA is defined as global scope-wise) 1309169664Sjinmei */ 1310181803Sbz if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 && 1311169664Sjinmei !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) && 1312169664Sjinmei !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) 1313169664Sjinmei goto bad; 1314169664Sjinmei 1315169664Sjinmei /* 131662587Sitojun * Validate IPv6 destination address. 131762587Sitojun * 131878064Sume * The Responder must discard the Query without further processing 131978064Sume * unless it is one of the Responder's unicast or anycast addresses, or 132078064Sume * a link-local scope multicast address which the Responder has joined. 1321169664Sjinmei * [RFC4602, Section 5.] 132262587Sitojun */ 1323121630Sume if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1324121630Sume if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) 1325121630Sume goto bad; 1326121630Sume /* else it's a link-local multicast, fine */ 1327121630Sume } else { /* unicast or anycast */ 1328121630Sume if ((ia6 = ip6_getdstifaddr(m)) == NULL) 1329121630Sume goto bad; /* XXX impossible */ 1330121630Sume 1331121630Sume if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) && 1332181803Sbz !(V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) { 1333194760Srwatson ifa_free(&ia6->ia_ifa); 133478064Sume nd6log((LOG_DEBUG, "ni6_input: ignore node info to " 133578064Sume "a temporary address in %s:%d", 133678064Sume __FILE__, __LINE__)); 133778064Sume goto bad; 133878064Sume } 1339194760Srwatson ifa_free(&ia6->ia_ifa); 1340121630Sume } 134162587Sitojun 134278064Sume /* validate query Subject field. */ 134362587Sitojun qtype = ntohs(ni6->ni_qtype); 134462587Sitojun subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); 134562587Sitojun switch (qtype) { 134662587Sitojun case NI_QTYPE_NOOP: 134762587Sitojun case NI_QTYPE_SUPTYPES: 134878064Sume /* 07 draft */ 134978064Sume if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) 135078064Sume break; 135178064Sume /* FALLTHROUGH */ 135262587Sitojun case NI_QTYPE_FQDN: 135362587Sitojun case NI_QTYPE_NODEADDR: 1354151475Ssuz case NI_QTYPE_IPV4ADDR: 135562587Sitojun switch (ni6->ni_code) { 135662587Sitojun case ICMP6_NI_SUBJ_IPV6: 135762587Sitojun#if ICMP6_NI_SUBJ_IPV6 != 0 135862587Sitojun case 0: 135962587Sitojun#endif 136062587Sitojun /* 136162587Sitojun * backward compatibility - try to accept 03 draft 136262587Sitojun * format, where no Subject is present. 136362587Sitojun */ 136478064Sume if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && 136578064Sume subjlen == 0) { 136662587Sitojun oldfqdn++; 136762587Sitojun break; 136862587Sitojun } 136978064Sume#if ICMP6_NI_SUBJ_IPV6 != 0 137078064Sume if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) 137178064Sume goto bad; 137278064Sume#endif 137362587Sitojun 1374121630Sume if (subjlen != sizeof(struct in6_addr)) 137562587Sitojun goto bad; 137662587Sitojun 137762587Sitojun /* 137862587Sitojun * Validate Subject address. 137962587Sitojun * 138078064Sume * Not sure what exactly "address belongs to the node" 138178064Sume * means in the spec, is it just unicast, or what? 138262587Sitojun * 138362587Sitojun * At this moment we consider Subject address as 138462587Sitojun * "belong to the node" if the Subject address equals 138562587Sitojun * to the IPv6 destination address; validation for 138662587Sitojun * IPv6 destination address should have done enough 138762587Sitojun * check for us. 138862587Sitojun * 138962587Sitojun * We do not do proxy at this moment. 139062587Sitojun */ 139162587Sitojun /* m_pulldown instead of copy? */ 139262587Sitojun m_copydata(m, off + sizeof(struct icmp6_nodeinfo), 1393148385Sume subjlen, (caddr_t)&in6_subj); 1394148385Sume if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL)) 1395121315Sume goto bad; 1396121630Sume 1397148385Sume subj = (char *)&in6_subj; 1398148385Sume if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj)) 139962587Sitojun break; 140078064Sume 140162587Sitojun /* 140262587Sitojun * XXX if we are to allow other cases, we should really 140362587Sitojun * be careful about scope here. 140462587Sitojun * basically, we should disallow queries toward IPv6 1405120891Sume * destination X with subject Y, 1406120891Sume * if scope(X) > scope(Y). 140762587Sitojun * if we allow scope(X) > scope(Y), it will result in 140862587Sitojun * information leakage across scope boundary. 140962587Sitojun */ 141062587Sitojun goto bad; 141162587Sitojun 141262587Sitojun case ICMP6_NI_SUBJ_FQDN: 141362587Sitojun /* 141462587Sitojun * Validate Subject name with gethostname(3). 141562587Sitojun * 141662587Sitojun * The behavior may need some debate, since: 141762587Sitojun * - we are not sure if the node has FQDN as 141862587Sitojun * hostname (returned by gethostname(3)). 141962587Sitojun * - the code does wildcard match for truncated names. 142062587Sitojun * however, we are not sure if we want to perform 142162587Sitojun * wildcard match, if gethostname(3) side has 142262587Sitojun * truncated hostname. 142362587Sitojun */ 1424193066Sjamie pr = curthread->td_ucred->cr_prison; 1425193066Sjamie mtx_lock(&pr->pr_mtx); 1426194118Sjamie n = ni6_nametodns(pr->pr_hostname, 1427194118Sjamie strlen(pr->pr_hostname), 0); 1428193066Sjamie mtx_unlock(&pr->pr_mtx); 142962587Sitojun if (!n || n->m_next || n->m_len == 0) 143062587Sitojun goto bad; 143162587Sitojun IP6_EXTHDR_GET(subj, char *, m, 143262587Sitojun off + sizeof(struct icmp6_nodeinfo), subjlen); 143362587Sitojun if (subj == NULL) 143462587Sitojun goto bad; 143562587Sitojun if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 1436120891Sume n->m_len)) { 143762587Sitojun goto bad; 143862587Sitojun } 143962587Sitojun m_freem(n); 144062587Sitojun n = NULL; 144162587Sitojun break; 144262587Sitojun 144378064Sume case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 144462587Sitojun default: 144562587Sitojun goto bad; 144662587Sitojun } 144762587Sitojun break; 144878064Sume } 144962587Sitojun 145078064Sume /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 145178064Sume switch (qtype) { 145278064Sume case NI_QTYPE_FQDN: 1453181803Sbz if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0) 145478064Sume goto bad; 145578064Sume break; 145678064Sume case NI_QTYPE_NODEADDR: 1457151475Ssuz case NI_QTYPE_IPV4ADDR: 1458181803Sbz if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0) 145978064Sume goto bad; 146078064Sume break; 146178064Sume } 146278064Sume 146378064Sume /* guess reply length */ 146478064Sume switch (qtype) { 146578064Sume case NI_QTYPE_NOOP: 146678064Sume break; /* no reply data */ 146778064Sume case NI_QTYPE_SUPTYPES: 146878064Sume replylen += sizeof(u_int32_t); 146978064Sume break; 147078064Sume case NI_QTYPE_FQDN: 147178064Sume /* XXX will append an mbuf */ 147278064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 147378064Sume break; 147478064Sume case NI_QTYPE_NODEADDR: 1475148892Sume addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj); 147678064Sume if ((replylen += addrs * (sizeof(struct in6_addr) + 1477120891Sume sizeof(u_int32_t))) > MCLBYTES) 147878064Sume replylen = MCLBYTES; /* XXX: will truncate pkt later */ 147978064Sume break; 1480151475Ssuz case NI_QTYPE_IPV4ADDR: 1481151475Ssuz /* unsupported - should respond with unknown Qtype? */ 1482151475Ssuz break; 148362587Sitojun default: 148478064Sume /* 148578064Sume * XXX: We must return a reply with the ICMP6 code 1486120891Sume * `unknown Qtype' in this case. However we regard the case 148778064Sume * as an FQDN query for backward compatibility. 148878064Sume * Older versions set a random value to this field, 148978064Sume * so it rarely varies in the defined qtypes. 149078064Sume * But the mechanism is not reliable... 149178064Sume * maybe we should obsolete older versions. 149278064Sume */ 149378064Sume qtype = NI_QTYPE_FQDN; 149478064Sume /* XXX will append an mbuf */ 149578064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 149678064Sume oldfqdn++; 149778064Sume break; 149862587Sitojun } 149962587Sitojun 150078064Sume /* allocate an mbuf to reply. */ 1501111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 150262587Sitojun if (n == NULL) { 150362587Sitojun m_freem(m); 1504120856Sume return (NULL); 150562587Sitojun } 1506232292Sbz M_MOVE_PKTHDR(n, m); /* just for recvif and FIB */ 150753541Sshin if (replylen > MHLEN) { 150862587Sitojun if (replylen > MCLBYTES) { 150978064Sume /* 151078064Sume * XXX: should we try to allocate more? But MCLBYTES 151178064Sume * is probably much larger than IPV6_MMTU... 151278064Sume */ 151353541Sshin goto bad; 151462587Sitojun } 1515111119Simp MCLGET(n, M_DONTWAIT); 151653541Sshin if ((n->m_flags & M_EXT) == 0) { 151753541Sshin goto bad; 151853541Sshin } 151953541Sshin } 152053541Sshin n->m_pkthdr.len = n->m_len = replylen; 152153541Sshin 152253541Sshin /* copy mbuf header and IPv6 + Node Information base headers */ 152353541Sshin bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); 152453541Sshin nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 152562587Sitojun bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); 152653541Sshin 152753541Sshin /* qtype dependent procedure */ 152853541Sshin switch (qtype) { 152962587Sitojun case NI_QTYPE_NOOP: 153078064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 153162587Sitojun nni6->ni_flags = 0; 153262587Sitojun break; 153362587Sitojun case NI_QTYPE_SUPTYPES: 153478064Sume { 153578064Sume u_int32_t v; 153678064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 153778064Sume nni6->ni_flags = htons(0x0000); /* raw bitmap */ 153878064Sume /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 153978064Sume v = (u_int32_t)htonl(0x0000000f); 154078064Sume bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 154162587Sitojun break; 154278064Sume } 154362587Sitojun case NI_QTYPE_FQDN: 154478064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 154562587Sitojun fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + 1546120891Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo)); 154762587Sitojun nni6->ni_flags = 0; /* XXX: meaningless TTL */ 154862587Sitojun fqdn->ni_fqdn_ttl = 0; /* ditto. */ 154962587Sitojun /* 1550193066Sjamie * XXX do we really have FQDN in hostname? 155162587Sitojun */ 1552193066Sjamie pr = curthread->td_ucred->cr_prison; 1553193066Sjamie mtx_lock(&pr->pr_mtx); 1554194118Sjamie n->m_next = ni6_nametodns(pr->pr_hostname, 1555194118Sjamie strlen(pr->pr_hostname), oldfqdn); 1556193066Sjamie mtx_unlock(&pr->pr_mtx); 155762587Sitojun if (n->m_next == NULL) 155862587Sitojun goto bad; 155962587Sitojun /* XXX we assume that n->m_next is not a chain */ 156062587Sitojun if (n->m_next->m_next != NULL) 156162587Sitojun goto bad; 156262587Sitojun n->m_pkthdr.len += n->m_next->m_len; 156362587Sitojun break; 156462587Sitojun case NI_QTYPE_NODEADDR: 156562587Sitojun { 156662587Sitojun int lenlim, copied; 156753541Sshin 156878064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 156978064Sume n->m_pkthdr.len = n->m_len = 157078064Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 157178064Sume lenlim = M_TRAILINGSPACE(n); 157262587Sitojun copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 157362587Sitojun /* XXX: reset mbuf length */ 157462587Sitojun n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 1575120891Sume sizeof(struct icmp6_nodeinfo) + copied; 157662587Sitojun break; 157753541Sshin } 157862587Sitojun default: 157962587Sitojun break; /* XXX impossible! */ 158062587Sitojun } 158153541Sshin 158253541Sshin nni6->ni_type = ICMP6_NI_REPLY; 158362587Sitojun m_freem(m); 1584120856Sume return (n); 158553541Sshin 158653541Sshin bad: 158762587Sitojun m_freem(m); 158853541Sshin if (n) 158953541Sshin m_freem(n); 1590120856Sume return (NULL); 159153541Sshin} 159253541Sshin 159353541Sshin/* 159462587Sitojun * make a mbuf with DNS-encoded string. no compression support. 159562587Sitojun * 159662587Sitojun * XXX names with less than 2 dots (like "foo" or "foo.section") will be 159762587Sitojun * treated as truncated name (two \0 at the end). this is a wild guess. 1598171259Sdelphij * 1599171259Sdelphij * old - return pascal string if non-zero 160062587Sitojun */ 160162587Sitojunstatic struct mbuf * 1602171259Sdelphijni6_nametodns(const char *name, int namelen, int old) 160362587Sitojun{ 160462587Sitojun struct mbuf *m; 160562587Sitojun char *cp, *ep; 160662587Sitojun const char *p, *q; 160762587Sitojun int i, len, nterm; 160862587Sitojun 160962587Sitojun if (old) 161062587Sitojun len = namelen + 1; 161162587Sitojun else 161262587Sitojun len = MCLBYTES; 161362587Sitojun 161462587Sitojun /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ 1615111119Simp MGET(m, M_DONTWAIT, MT_DATA); 161662587Sitojun if (m && len > MLEN) { 1617111119Simp MCLGET(m, M_DONTWAIT); 161862587Sitojun if ((m->m_flags & M_EXT) == 0) 161962587Sitojun goto fail; 162062587Sitojun } 162162587Sitojun if (!m) 162262587Sitojun goto fail; 162362587Sitojun m->m_next = NULL; 162462587Sitojun 162562587Sitojun if (old) { 162662587Sitojun m->m_len = len; 162762587Sitojun *mtod(m, char *) = namelen; 162862587Sitojun bcopy(name, mtod(m, char *) + 1, namelen); 162962587Sitojun return m; 163062587Sitojun } else { 163162587Sitojun m->m_len = 0; 163262587Sitojun cp = mtod(m, char *); 163362587Sitojun ep = mtod(m, char *) + M_TRAILINGSPACE(m); 163462587Sitojun 163562587Sitojun /* if not certain about my name, return empty buffer */ 163662587Sitojun if (namelen == 0) 163762587Sitojun return m; 163862587Sitojun 163962587Sitojun /* 164062587Sitojun * guess if it looks like shortened hostname, or FQDN. 164162587Sitojun * shortened hostname needs two trailing "\0". 164262587Sitojun */ 164362587Sitojun i = 0; 164462587Sitojun for (p = name; p < name + namelen; p++) { 164562587Sitojun if (*p && *p == '.') 164662587Sitojun i++; 164762587Sitojun } 164862587Sitojun if (i < 2) 164962587Sitojun nterm = 2; 165062587Sitojun else 165162587Sitojun nterm = 1; 165262587Sitojun 165362587Sitojun p = name; 165462587Sitojun while (cp < ep && p < name + namelen) { 165562587Sitojun i = 0; 165662587Sitojun for (q = p; q < name + namelen && *q && *q != '.'; q++) 165762587Sitojun i++; 165862587Sitojun /* result does not fit into mbuf */ 165962587Sitojun if (cp + i + 1 >= ep) 166062587Sitojun goto fail; 166178704Sume /* 166278704Sume * DNS label length restriction, RFC1035 page 8. 166378704Sume * "i == 0" case is included here to avoid returning 166478704Sume * 0-length label on "foo..bar". 166578704Sume */ 166678704Sume if (i <= 0 || i >= 64) 166762587Sitojun goto fail; 166862587Sitojun *cp++ = i; 166962587Sitojun bcopy(p, cp, i); 167062587Sitojun cp += i; 167162587Sitojun p = q; 167262587Sitojun if (p < name + namelen && *p == '.') 167362587Sitojun p++; 167462587Sitojun } 167562587Sitojun /* termination */ 167662587Sitojun if (cp + nterm >= ep) 167762587Sitojun goto fail; 167862587Sitojun while (nterm-- > 0) 167962587Sitojun *cp++ = '\0'; 168062587Sitojun m->m_len = cp - mtod(m, char *); 168162587Sitojun return m; 168262587Sitojun } 168362587Sitojun 168462587Sitojun panic("should not reach here"); 168595023Ssuz /* NOTREACHED */ 168662587Sitojun 168762587Sitojun fail: 168862587Sitojun if (m) 168962587Sitojun m_freem(m); 169062587Sitojun return NULL; 169162587Sitojun} 169262587Sitojun 169362587Sitojun/* 169462587Sitojun * check if two DNS-encoded string matches. takes care of truncated 169562587Sitojun * form (with \0\0 at the end). no compression support. 169678064Sume * XXX upper/lowercase match (see RFC2065) 169762587Sitojun */ 169862587Sitojunstatic int 1699171259Sdelphijni6_dnsmatch(const char *a, int alen, const char *b, int blen) 170062587Sitojun{ 170162587Sitojun const char *a0, *b0; 170262587Sitojun int l; 170362587Sitojun 170462587Sitojun /* simplest case - need validation? */ 170562587Sitojun if (alen == blen && bcmp(a, b, alen) == 0) 170662587Sitojun return 1; 170762587Sitojun 170862587Sitojun a0 = a; 170962587Sitojun b0 = b; 171062587Sitojun 171162587Sitojun /* termination is mandatory */ 171262587Sitojun if (alen < 2 || blen < 2) 171362587Sitojun return 0; 171462587Sitojun if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 171562587Sitojun return 0; 171662587Sitojun alen--; 171762587Sitojun blen--; 171862587Sitojun 171962587Sitojun while (a - a0 < alen && b - b0 < blen) { 172062587Sitojun if (a - a0 + 1 > alen || b - b0 + 1 > blen) 172162587Sitojun return 0; 172262587Sitojun 172362587Sitojun if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 172462587Sitojun return 0; 172562587Sitojun /* we don't support compression yet */ 172662587Sitojun if (a[0] >= 64 || b[0] >= 64) 172762587Sitojun return 0; 172862587Sitojun 172962587Sitojun /* truncated case */ 173062587Sitojun if (a[0] == 0 && a - a0 == alen - 1) 173162587Sitojun return 1; 173262587Sitojun if (b[0] == 0 && b - b0 == blen - 1) 173362587Sitojun return 1; 173462587Sitojun if (a[0] == 0 || b[0] == 0) 173562587Sitojun return 0; 173662587Sitojun 173762587Sitojun if (a[0] != b[0]) 173862587Sitojun return 0; 173962587Sitojun l = a[0]; 174062587Sitojun if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 174162587Sitojun return 0; 174262587Sitojun if (bcmp(a + 1, b + 1, l) != 0) 174362587Sitojun return 0; 174462587Sitojun 174562587Sitojun a += 1 + l; 174662587Sitojun b += 1 + l; 174762587Sitojun } 174862587Sitojun 174962587Sitojun if (a - a0 == alen && b - b0 == blen) 175062587Sitojun return 1; 175162587Sitojun else 175262587Sitojun return 0; 175362587Sitojun} 175462587Sitojun 175562587Sitojun/* 175653541Sshin * calculate the number of addresses to be returned in the node info reply. 175753541Sshin */ 175853541Sshinstatic int 1759171259Sdelphijni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp, 1760171259Sdelphij struct in6_addr *subj) 176153541Sshin{ 176278064Sume struct ifnet *ifp; 176378064Sume struct in6_ifaddr *ifa6; 176478064Sume struct ifaddr *ifa; 176553541Sshin int addrs = 0, addrsofif, iffound = 0; 176678064Sume int niflags = ni6->ni_flags; 176753541Sshin 176878064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 176978064Sume switch (ni6->ni_code) { 177078064Sume case ICMP6_NI_SUBJ_IPV6: 177178064Sume if (subj == NULL) /* must be impossible... */ 1772120856Sume return (0); 177378064Sume break; 177478064Sume default: 177578064Sume /* 177678064Sume * XXX: we only support IPv6 subject address for 177778064Sume * this Qtype. 177878064Sume */ 1779120856Sume return (0); 178078064Sume } 178178064Sume } 178278064Sume 1783196481Srwatson IFNET_RLOCK_NOSLEEP(); 1784233046Sjhb TAILQ_FOREACH(ifp, &V_ifnet, if_list) { 178553541Sshin addrsofif = 0; 1786233200Sjhb IF_ADDR_RLOCK(ifp); 1787191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 178853541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 178953541Sshin continue; 179053541Sshin ifa6 = (struct in6_ifaddr *)ifa; 179153541Sshin 179278064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 1793148892Sume IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr)) 179453541Sshin iffound = 1; 179553541Sshin 179662587Sitojun /* 179762587Sitojun * IPv4-mapped addresses can only be returned by a 179862587Sitojun * Node Information proxy, since they represent 179962587Sitojun * addresses of IPv4-only nodes, which perforce do 180062587Sitojun * not implement this protocol. 180178064Sume * [icmp-name-lookups-07, Section 5.4] 180262587Sitojun * So we don't support NI_NODEADDR_FLAG_COMPAT in 180362587Sitojun * this function at this moment. 180462587Sitojun */ 180562587Sitojun 180653541Sshin /* What do we have to do about ::1? */ 180778064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 180878064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 180978064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 181078064Sume continue; 181153541Sshin break; 181278064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 181378064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 181478064Sume continue; 181553541Sshin break; 181678064Sume case IPV6_ADDR_SCOPE_GLOBAL: 181778064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 181878064Sume continue; 181978064Sume break; 182078064Sume default: 182178064Sume continue; 182253541Sshin } 182378064Sume 182478064Sume /* 182578064Sume * check if anycast is okay. 182695023Ssuz * XXX: just experimental. not in the spec. 182778064Sume */ 182878064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 182978064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 183078064Sume continue; /* we need only unicast addresses */ 183178064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1832181803Sbz (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { 183378064Sume continue; 183478064Sume } 183578064Sume addrsofif++; /* count the address */ 183653541Sshin } 1837233200Sjhb IF_ADDR_RUNLOCK(ifp); 183853541Sshin if (iffound) { 183953541Sshin *ifpp = ifp; 1840196481Srwatson IFNET_RUNLOCK_NOSLEEP(); 1841120856Sume return (addrsofif); 184253541Sshin } 184353541Sshin 184453541Sshin addrs += addrsofif; 184553541Sshin } 1846196481Srwatson IFNET_RUNLOCK_NOSLEEP(); 184753541Sshin 1848120856Sume return (addrs); 184953541Sshin} 185053541Sshin 185153541Sshinstatic int 1852171259Sdelphijni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6, 1853171259Sdelphij struct ifnet *ifp0, int resid) 185453541Sshin{ 1855229637Sjhb struct ifnet *ifp; 185678064Sume struct in6_ifaddr *ifa6; 185778064Sume struct ifaddr *ifa; 185878064Sume struct ifnet *ifp_dep = NULL; 185978064Sume int copied = 0, allow_deprecated = 0; 186053541Sshin u_char *cp = (u_char *)(nni6 + 1); 186178064Sume int niflags = ni6->ni_flags; 186278064Sume u_int32_t ltime; 186353541Sshin 186478064Sume if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 1865120856Sume return (0); /* needless to copy */ 1866120891Sume 1867196481Srwatson IFNET_RLOCK_NOSLEEP(); 1868229637Sjhb ifp = ifp0 ? ifp0 : TAILQ_FIRST(&V_ifnet); 186978064Sume again: 1870120892Sume 1871120891Sume for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 1872233200Sjhb IF_ADDR_RLOCK(ifp); 1873191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 187453541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 187553541Sshin continue; 187653541Sshin ifa6 = (struct in6_ifaddr *)ifa; 187753541Sshin 187878064Sume if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && 187978064Sume allow_deprecated == 0) { 188078064Sume /* 188178064Sume * prefererred address should be put before 188278064Sume * deprecated addresses. 188378064Sume */ 188478064Sume 188578064Sume /* record the interface for later search */ 188678064Sume if (ifp_dep == NULL) 188778064Sume ifp_dep = ifp; 188878064Sume 188978064Sume continue; 1890120891Sume } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && 1891120891Sume allow_deprecated != 0) 189278064Sume continue; /* we now collect deprecated addrs */ 189353541Sshin 189453541Sshin /* What do we have to do about ::1? */ 189578064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 189678064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 189778064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 189878064Sume continue; 189953541Sshin break; 190078064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 190178064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 190278064Sume continue; 190353541Sshin break; 190478064Sume case IPV6_ADDR_SCOPE_GLOBAL: 190578064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 190678064Sume continue; 190778064Sume break; 190878064Sume default: 190978064Sume continue; 191053541Sshin } 191153541Sshin 191278064Sume /* 191378064Sume * check if anycast is okay. 1914120891Sume * XXX: just experimental. not in the spec. 191578064Sume */ 191678064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 191778064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 191878064Sume continue; 191978064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1920181803Sbz (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { 192178064Sume continue; 192253541Sshin } 192378064Sume 192478064Sume /* now we can copy the address */ 192578064Sume if (resid < sizeof(struct in6_addr) + 192678064Sume sizeof(u_int32_t)) { 1927233200Sjhb IF_ADDR_RUNLOCK(ifp); 192878064Sume /* 192978064Sume * We give up much more copy. 193078064Sume * Set the truncate flag and return. 193178064Sume */ 1932120891Sume nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE; 1933196481Srwatson IFNET_RUNLOCK_NOSLEEP(); 1934120856Sume return (copied); 193578064Sume } 193678064Sume 193778064Sume /* 193878064Sume * Set the TTL of the address. 193978064Sume * The TTL value should be one of the following 194078064Sume * according to the specification: 194178064Sume * 194278064Sume * 1. The remaining lifetime of a DHCP lease on the 194378064Sume * address, or 194478064Sume * 2. The remaining Valid Lifetime of a prefix from 194578064Sume * which the address was derived through Stateless 194678064Sume * Autoconfiguration. 194778064Sume * 194878064Sume * Note that we currently do not support stateful 194978064Sume * address configuration by DHCPv6, so the former 195078064Sume * case can't happen. 195178064Sume */ 195278064Sume if (ifa6->ia6_lifetime.ia6t_expire == 0) 195378064Sume ltime = ND6_INFINITE_LIFETIME; 195478064Sume else { 195578064Sume if (ifa6->ia6_lifetime.ia6t_expire > 195678064Sume time_second) 195778064Sume ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second); 195878064Sume else 195978064Sume ltime = 0; 196078064Sume } 1961120891Sume 196278064Sume bcopy(<ime, cp, sizeof(u_int32_t)); 196378064Sume cp += sizeof(u_int32_t); 196478064Sume 196578064Sume /* copy the address itself */ 196678064Sume bcopy(&ifa6->ia_addr.sin6_addr, cp, 1967120891Sume sizeof(struct in6_addr)); 1968121315Sume in6_clearscope((struct in6_addr *)cp); /* XXX */ 196978064Sume cp += sizeof(struct in6_addr); 1970120891Sume 197178064Sume resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1972120891Sume copied += (sizeof(struct in6_addr) + sizeof(u_int32_t)); 197353541Sshin } 1974233200Sjhb IF_ADDR_RUNLOCK(ifp); 197553541Sshin if (ifp0) /* we need search only on the specified IF */ 197653541Sshin break; 197753541Sshin } 197853541Sshin 197978064Sume if (allow_deprecated == 0 && ifp_dep != NULL) { 198078064Sume ifp = ifp_dep; 198178064Sume allow_deprecated = 1; 198278064Sume 198378064Sume goto again; 198478064Sume } 198578064Sume 1986196481Srwatson IFNET_RUNLOCK_NOSLEEP(); 1987108172Shsu 1988120856Sume return (copied); 198953541Sshin} 199053541Sshin 199153541Sshin/* 199253541Sshin * XXX almost dup'ed code with rip6_input. 199353541Sshin */ 199453541Sshinstatic int 1995171259Sdelphijicmp6_rip6_input(struct mbuf **mp, int off) 199653541Sshin{ 199753541Sshin struct mbuf *m = *mp; 199878064Sume struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1999186141Sbz struct inpcb *in6p; 2000186141Sbz struct inpcb *last = NULL; 2001121315Sume struct sockaddr_in6 fromsa; 200253541Sshin struct icmp6_hdr *icmp6; 200353541Sshin struct mbuf *opts = NULL; 200453541Sshin 200562587Sitojun#ifndef PULLDOWN_TEST 200653541Sshin /* this is assumed to be safe. */ 200753541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 200862587Sitojun#else 200962587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 201062587Sitojun if (icmp6 == NULL) { 201162587Sitojun /* m is already reclaimed */ 2012120891Sume return (IPPROTO_DONE); 201362587Sitojun } 201462587Sitojun#endif 201553541Sshin 2016148385Sume /* 2017148385Sume * XXX: the address may have embedded scope zone ID, which should be 2018148385Sume * hidden from applications. 2019148385Sume */ 2020121315Sume bzero(&fromsa, sizeof(fromsa)); 2021148385Sume fromsa.sin6_family = AF_INET6; 2022121315Sume fromsa.sin6_len = sizeof(struct sockaddr_in6); 2023148385Sume fromsa.sin6_addr = ip6->ip6_src; 2024148385Sume if (sa6_recoverscope(&fromsa)) { 2025148385Sume m_freem(m); 2026148385Sume return (IPPROTO_DONE); 2027148385Sume } 202853541Sshin 2029181803Sbz INP_INFO_RLOCK(&V_ripcbinfo); 2030181803Sbz LIST_FOREACH(in6p, &V_ripcb, inp_list) { 2031180850Smav if ((in6p->inp_vflag & INP_IPV6) == 0) 203253541Sshin continue; 2033186141Sbz if (in6p->inp_ip_p != IPPROTO_ICMPV6) 2034180850Smav continue; 203553541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 203653541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 2037180850Smav continue; 203853541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 203953541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 2040180850Smav continue; 2041180932Smav INP_RLOCK(in6p); 2042180965Srwatson if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, 2043180965Srwatson in6p->in6p_icmp6filt)) { 2044180932Smav INP_RUNLOCK(in6p); 2045180850Smav continue; 2046180932Smav } 2047186163Skmacy if (last != NULL) { 2048121809Sume struct mbuf *n = NULL; 2049121809Sume 2050121809Sume /* 2051121809Sume * Recent network drivers tend to allocate a single 2052121809Sume * mbuf cluster, rather than to make a couple of 2053121809Sume * mbufs without clusters. Also, since the IPv6 code 2054121809Sume * path tries to avoid m_pullup(), it is highly 2055121809Sume * probable that we still have an mbuf cluster here 2056121809Sume * even though the necessary length can be stored in an 2057121809Sume * mbuf's internal buffer. 2058121809Sume * Meanwhile, the default size of the receive socket 2059121809Sume * buffer for raw sockets is not so large. This means 2060121809Sume * the possibility of packet loss is relatively higher 2061121809Sume * than before. To avoid this scenario, we copy the 2062121809Sume * received data to a separate mbuf that does not use 2063121809Sume * a cluster, if possible. 2064121809Sume * XXX: it is better to copy the data after stripping 2065121809Sume * intermediate headers. 2066121809Sume */ 2067121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 2068121809Sume m->m_len <= MHLEN) { 2069121809Sume MGET(n, M_DONTWAIT, m->m_type); 2070121809Sume if (n != NULL) { 2071145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 2072171260Sdelphij bcopy(m->m_data, n->m_data, 2073145065Sgnn m->m_len); 2074145065Sgnn n->m_len = m->m_len; 2075145065Sgnn } else { 2076145065Sgnn m_free(n); 2077145065Sgnn n = NULL; 2078145065Sgnn } 2079121809Sume } 2080121809Sume } 2081121809Sume if (n != NULL || 2082121809Sume (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 2083186223Sbz if (last->inp_flags & INP_CONTROLOPTS) 2084121674Sume ip6_savecontrol(last, n, &opts); 208553541Sshin /* strip intermediate headers */ 208653541Sshin m_adj(n, off); 2087186141Sbz SOCKBUF_LOCK(&last->inp_socket->so_rcv); 2088160591Srwatson if (sbappendaddr_locked( 2089186141Sbz &last->inp_socket->so_rcv, 2090121315Sume (struct sockaddr *)&fromsa, n, opts) 2091120891Sume == 0) { 209253541Sshin /* should notify about lost packet */ 209353541Sshin m_freem(n); 209478064Sume if (opts) { 209553541Sshin m_freem(opts); 209678064Sume } 2097160591Srwatson SOCKBUF_UNLOCK( 2098186141Sbz &last->inp_socket->so_rcv); 209997658Stanimura } else 2100186141Sbz sorwakeup_locked(last->inp_socket); 210153541Sshin opts = NULL; 210253541Sshin } 2103178378Srwatson INP_RUNLOCK(last); 210453541Sshin } 210553541Sshin last = in6p; 210653541Sshin } 2107181803Sbz INP_INFO_RUNLOCK(&V_ripcbinfo); 2108186163Skmacy if (last != NULL) { 2109186223Sbz if (last->inp_flags & INP_CONTROLOPTS) 2110121674Sume ip6_savecontrol(last, m, &opts); 211153541Sshin /* strip intermediate headers */ 211253541Sshin m_adj(m, off); 2113121809Sume 2114121809Sume /* avoid using mbuf clusters if possible (see above) */ 2115121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 2116121809Sume m->m_len <= MHLEN) { 2117121809Sume struct mbuf *n; 2118121809Sume 2119121809Sume MGET(n, M_DONTWAIT, m->m_type); 2120121809Sume if (n != NULL) { 2121145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 2122145065Sgnn bcopy(m->m_data, n->m_data, m->m_len); 2123145065Sgnn n->m_len = m->m_len; 2124171260Sdelphij 2125145065Sgnn m_freem(m); 2126145065Sgnn m = n; 2127145065Sgnn } else { 2128145065Sgnn m_freem(n); 2129145065Sgnn n = NULL; 2130145065Sgnn } 2131121809Sume } 2132121809Sume } 2133186141Sbz SOCKBUF_LOCK(&last->inp_socket->so_rcv); 2134186141Sbz if (sbappendaddr_locked(&last->inp_socket->so_rcv, 2135121315Sume (struct sockaddr *)&fromsa, m, opts) == 0) { 213653541Sshin m_freem(m); 213753541Sshin if (opts) 213853541Sshin m_freem(opts); 2139186141Sbz SOCKBUF_UNLOCK(&last->inp_socket->so_rcv); 214097658Stanimura } else 2141186141Sbz sorwakeup_locked(last->inp_socket); 2142178378Srwatson INP_RUNLOCK(last); 214353541Sshin } else { 214453541Sshin m_freem(m); 2145191672Sbms IP6STAT_DEC(ip6s_delivered); 214653541Sshin } 214753541Sshin return IPPROTO_DONE; 214853541Sshin} 214953541Sshin 215053541Sshin/* 215153541Sshin * Reflect the ip6 packet back to the source. 215262587Sitojun * OFF points to the icmp6 header, counted from the top of the mbuf. 215353541Sshin */ 215453541Sshinvoid 2155171259Sdelphijicmp6_reflect(struct mbuf *m, size_t off) 215653541Sshin{ 215762587Sitojun struct ip6_hdr *ip6; 215853541Sshin struct icmp6_hdr *icmp6; 2159194760Srwatson struct in6_ifaddr *ia = NULL; 216062587Sitojun int plen; 216153541Sshin int type, code; 216253541Sshin struct ifnet *outif = NULL; 2163194777Sbz struct in6_addr origdst, src, *srcp = NULL; 216453541Sshin 216562587Sitojun /* too short to reflect */ 216662587Sitojun if (off < sizeof(struct ip6_hdr)) { 216778064Sume nd6log((LOG_DEBUG, 216878064Sume "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 216978064Sume (u_long)off, (u_long)sizeof(struct ip6_hdr), 217078064Sume __FILE__, __LINE__)); 217162587Sitojun goto bad; 217262587Sitojun } 217362587Sitojun 217453541Sshin /* 217553541Sshin * If there are extra headers between IPv6 and ICMPv6, strip 217653541Sshin * off that header first. 217753541Sshin */ 217862587Sitojun#ifdef DIAGNOSTIC 217962587Sitojun if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) 218062587Sitojun panic("assumption failed in icmp6_reflect"); 218162587Sitojun#endif 218262587Sitojun if (off > sizeof(struct ip6_hdr)) { 218362587Sitojun size_t l; 218462587Sitojun struct ip6_hdr nip6; 218553541Sshin 218662587Sitojun l = off - sizeof(struct ip6_hdr); 218762587Sitojun m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); 218862587Sitojun m_adj(m, l); 218962587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 219062587Sitojun if (m->m_len < l) { 219162587Sitojun if ((m = m_pullup(m, l)) == NULL) 219262587Sitojun return; 219353541Sshin } 219462587Sitojun bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); 219562587Sitojun } else /* off == sizeof(struct ip6_hdr) */ { 219662587Sitojun size_t l; 219762587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 219862587Sitojun if (m->m_len < l) { 219962587Sitojun if ((m = m_pullup(m, l)) == NULL) 220062587Sitojun return; 220153541Sshin } 220253541Sshin } 220362587Sitojun plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 220462587Sitojun ip6 = mtod(m, struct ip6_hdr *); 220562587Sitojun ip6->ip6_nxt = IPPROTO_ICMPV6; 220653541Sshin icmp6 = (struct icmp6_hdr *)(ip6 + 1); 220753541Sshin type = icmp6->icmp6_type; /* keep type for statistics */ 220853541Sshin code = icmp6->icmp6_code; /* ditto. */ 220953541Sshin 2210148385Sume origdst = ip6->ip6_dst; 221153541Sshin /* 221253541Sshin * ip6_input() drops a packet if its src is multicast. 221353541Sshin * So, the src is never multicast. 221453541Sshin */ 221553541Sshin ip6->ip6_dst = ip6->ip6_src; 221653541Sshin 221753541Sshin /* 2218120891Sume * If the incoming packet was addressed directly to us (i.e. unicast), 221953541Sshin * use dst as the src for the reply. 2220120891Sume * The IN6_IFF_NOTREADY case should be VERY rare, but is possible 222162587Sitojun * (for example) when we encounter an error while forwarding procedure 222262587Sitojun * destined to a duplicated address of ours. 2223148385Sume * Note that ip6_getdstifaddr() may fail if we are in an error handling 2224148385Sume * procedure of an outgoing packet of our own, in which case we need 2225148385Sume * to search in the ifaddr list. 222653541Sshin */ 2227148385Sume if (!IN6_IS_ADDR_MULTICAST(&origdst)) { 2228148385Sume if ((ia = ip6_getdstifaddr(m))) { 2229148385Sume if (!(ia->ia6_flags & 2230148385Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) 2231194777Sbz srcp = &ia->ia_addr.sin6_addr; 2232148385Sume } else { 2233148385Sume struct sockaddr_in6 d; 2234148385Sume 2235148385Sume bzero(&d, sizeof(d)); 2236148385Sume d.sin6_family = AF_INET6; 2237148385Sume d.sin6_len = sizeof(d); 2238148385Sume d.sin6_addr = origdst; 2239148385Sume ia = (struct in6_ifaddr *) 2240148385Sume ifa_ifwithaddr((struct sockaddr *)&d); 2241148385Sume if (ia && 2242148385Sume !(ia->ia6_flags & 2243148385Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) { 2244194777Sbz srcp = &ia->ia_addr.sin6_addr; 2245148385Sume } 224653541Sshin } 224753541Sshin } 224853541Sshin 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 2302223637Sbz m_addr_changed(m); 2303223637Sbz 2304105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 230553541Sshin if (outif) 230653541Sshin icmp6_ifoutstat_inc(outif, type, code); 230753541Sshin 2308194760Srwatson if (ia != NULL) 2309194760Srwatson ifa_free(&ia->ia_ifa); 231053541Sshin return; 231153541Sshin 231253541Sshin bad: 2313194760Srwatson if (ia != NULL) 2314194760Srwatson ifa_free(&ia->ia_ifa); 231553541Sshin m_freem(m); 231653541Sshin return; 231753541Sshin} 231853541Sshin 231953541Sshinvoid 2320171259Sdelphijicmp6_fasttimo(void) 232153541Sshin{ 232262587Sitojun 2323191672Sbms mld_fasttimo(); 232453541Sshin} 232553541Sshin 2326191672Sbmsvoid 2327191672Sbmsicmp6_slowtimo(void) 2328191672Sbms{ 2329191672Sbms 2330191672Sbms mld_slowtimo(); 2331191672Sbms} 2332191672Sbms 233353541Sshinstatic const char * 2334171259Sdelphijicmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6, 2335171259Sdelphij struct in6_addr *tgt6) 233653541Sshin{ 233753541Sshin static char buf[1024]; 2338165118Sbz char ip6bufs[INET6_ADDRSTRLEN]; 2339165118Sbz char ip6bufd[INET6_ADDRSTRLEN]; 2340165118Sbz char ip6buft[INET6_ADDRSTRLEN]; 234153541Sshin snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", 2342165118Sbz ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6), 2343165118Sbz ip6_sprintf(ip6buft, tgt6)); 234453541Sshin return buf; 234553541Sshin} 234653541Sshin 234753541Sshinvoid 2348171259Sdelphijicmp6_redirect_input(struct mbuf *m, int off) 234953541Sshin{ 2350171133Sgnn struct ifnet *ifp; 235153541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 235262587Sitojun struct nd_redirect *nd_rd; 235353541Sshin int icmp6len = ntohs(ip6->ip6_plen); 235453541Sshin char *lladdr = NULL; 235553541Sshin int lladdrlen = 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 2365233261Sglebius M_ASSERTPKTHDR(m); 2366233261Sglebius KASSERT(m->m_pkthdr.rcvif != NULL, ("%s: no rcvif", __func__)); 236753541Sshin 2368171133Sgnn ifp = m->m_pkthdr.rcvif; 2369171133Sgnn 237053541Sshin /* XXX if we are router, we don't update route by icmp6 redirect */ 2371181803Sbz if (V_ip6_forwarding) 237262587Sitojun goto freeit; 2373181803Sbz if (!V_icmp6_rediraccept) 237462587Sitojun goto freeit; 237562587Sitojun 237662587Sitojun#ifndef PULLDOWN_TEST 237762587Sitojun IP6_EXTHDR_CHECK(m, off, icmp6len,); 237862587Sitojun nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); 237962587Sitojun#else 238062587Sitojun IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); 238162587Sitojun if (nd_rd == NULL) { 2382190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 238353541Sshin return; 238462587Sitojun } 238562587Sitojun#endif 238662587Sitojun redtgt6 = nd_rd->nd_rd_target; 238762587Sitojun reddst6 = nd_rd->nd_rd_dst; 238853541Sshin 2389148385Sume if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) || 2390148385Sume in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) { 2391148385Sume goto freeit; 2392148385Sume } 239353541Sshin 239453541Sshin /* validation */ 239553541Sshin if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 239678064Sume nd6log((LOG_ERR, 2397120891Sume "ICMP6 redirect sent from %s rejected; " 2398120891Sume "must be from linklocal\n", 2399165118Sbz ip6_sprintf(ip6buf, &src6))); 240078064Sume goto bad; 240153541Sshin } 240253541Sshin if (ip6->ip6_hlim != 255) { 240378064Sume nd6log((LOG_ERR, 2404120891Sume "ICMP6 redirect sent from %s rejected; " 2405120891Sume "hlim=%d (must be 255)\n", 2406165118Sbz ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim)); 240778064Sume goto bad; 240853541Sshin } 240953541Sshin { 241053541Sshin /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 241153541Sshin struct sockaddr_in6 sin6; 241253541Sshin struct in6_addr *gw6; 241353541Sshin 241453541Sshin bzero(&sin6, sizeof(sin6)); 241553541Sshin sin6.sin6_family = AF_INET6; 241653541Sshin sin6.sin6_len = sizeof(struct sockaddr_in6); 241753541Sshin bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); 2418232292Sbz rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, 0UL, RT_DEFAULT_FIB); 241953541Sshin if (rt) { 242065895Sume if (rt->rt_gateway == NULL || 242165895Sume rt->rt_gateway->sa_family != AF_INET6) { 2422238242Sbz RTFREE_LOCKED(rt); 242378064Sume nd6log((LOG_ERR, 242465895Sume "ICMP6 redirect rejected; no route " 242565895Sume "with inet6 gateway found for redirect dst: %s\n", 242678064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 242778064Sume goto bad; 242865895Sume } 242965895Sume 243053541Sshin gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); 243153541Sshin if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { 2432238242Sbz RTFREE_LOCKED(rt); 243378064Sume nd6log((LOG_ERR, 2434120891Sume "ICMP6 redirect rejected; " 2435120891Sume "not equal to gw-for-src=%s (must be same): " 2436120891Sume "%s\n", 2437165118Sbz ip6_sprintf(ip6buf, gw6), 2438120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 243978064Sume goto bad; 244053541Sshin } 244153541Sshin } else { 244278064Sume nd6log((LOG_ERR, 2443120891Sume "ICMP6 redirect rejected; " 2444120891Sume "no route found for redirect dst: %s\n", 2445120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 244678064Sume goto bad; 244753541Sshin } 2448120727Ssam RTFREE_LOCKED(rt); 244953541Sshin rt = NULL; 245053541Sshin } 245153541Sshin if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 245278064Sume nd6log((LOG_ERR, 2453120891Sume "ICMP6 redirect rejected; " 2454120891Sume "redirect dst must be unicast: %s\n", 2455120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 245678064Sume goto bad; 245753541Sshin } 245853541Sshin 245953541Sshin is_router = is_onlink = 0; 246053541Sshin if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 246153541Sshin is_router = 1; /* router case */ 246253541Sshin if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 246353541Sshin is_onlink = 1; /* on-link destination case */ 246453541Sshin if (!is_router && !is_onlink) { 246578064Sume nd6log((LOG_ERR, 2466120891Sume "ICMP6 redirect rejected; " 2467120891Sume "neither router case nor onlink case: %s\n", 2468120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 246978064Sume goto bad; 247053541Sshin } 247153541Sshin /* validation passed */ 247253541Sshin 247353541Sshin icmp6len -= sizeof(*nd_rd); 247453541Sshin nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 247553541Sshin if (nd6_options(&ndopts) < 0) { 2476233261Sglebius nd6log((LOG_INFO, "%s: invalid ND option, rejected: %s\n", 2477233261Sglebius __func__, icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 247878064Sume /* nd6_options have incremented stats */ 247962587Sitojun goto freeit; 248053541Sshin } 248153541Sshin 248253541Sshin if (ndopts.nd_opts_tgt_lladdr) { 248353541Sshin lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 248453541Sshin lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 248553541Sshin } 248653541Sshin 248753541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 2488233261Sglebius nd6log((LOG_INFO, "%s: lladdrlen mismatch for %s " 2489120891Sume "(if %d, icmp6 packet %d): %s\n", 2490233261Sglebius __func__, ip6_sprintf(ip6buf, &redtgt6), 2491165118Sbz ifp->if_addrlen, lladdrlen - 2, 2492120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 249378064Sume goto bad; 249453541Sshin } 249553541Sshin 249653541Sshin /* RFC 2461 8.3 */ 249753541Sshin nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 2498120891Sume is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 249953541Sshin 250095023Ssuz if (!is_onlink) { /* better router case. perform rtredirect. */ 250153541Sshin /* perform rtredirect */ 250253541Sshin struct sockaddr_in6 sdst; 250353541Sshin struct sockaddr_in6 sgw; 250453541Sshin struct sockaddr_in6 ssrc; 2505232292Sbz u_int fibnum; 250653541Sshin 250753541Sshin bzero(&sdst, sizeof(sdst)); 250853541Sshin bzero(&sgw, sizeof(sgw)); 250953541Sshin bzero(&ssrc, sizeof(ssrc)); 251053541Sshin sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; 251153541Sshin sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = 251253541Sshin sizeof(struct sockaddr_in6); 251353541Sshin bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); 251453541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 251553541Sshin bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 2516232292Sbz for (fibnum = 0; fibnum < rt_numfibs; fibnum++) 2517232292Sbz in6_rtredirect((struct sockaddr *)&sdst, 2518232292Sbz (struct sockaddr *)&sgw, (struct sockaddr *)NULL, 2519232292Sbz RTF_GATEWAY | RTF_HOST, (struct sockaddr *)&ssrc, 2520232292Sbz fibnum); 252153541Sshin } 252253541Sshin /* finally update cached route in each socket via pfctlinput */ 252353541Sshin { 252453541Sshin struct sockaddr_in6 sdst; 252553541Sshin 252653541Sshin bzero(&sdst, sizeof(sdst)); 252753541Sshin sdst.sin6_family = AF_INET6; 252853541Sshin sdst.sin6_len = sizeof(struct sockaddr_in6); 252953541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 253053541Sshin pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); 2531171167Sgnn#ifdef IPSEC 253253541Sshin key_sa_routechange((struct sockaddr *)&sdst); 2533171167Sgnn#endif /* IPSEC */ 253453541Sshin } 253562587Sitojun 253662587Sitojun freeit: 253762587Sitojun m_freem(m); 253878064Sume return; 253978064Sume 254078064Sume bad: 2541190964Srwatson ICMP6STAT_INC(icp6s_badredirect); 254278064Sume m_freem(m); 254353541Sshin} 254453541Sshin 254553541Sshinvoid 2546171259Sdelphijicmp6_redirect_output(struct mbuf *m0, struct rtentry *rt) 254753541Sshin{ 254853541Sshin struct ifnet *ifp; /* my outgoing interface */ 254953541Sshin struct in6_addr *ifp_ll6; 255053541Sshin struct in6_addr *router_ll6; 255153541Sshin struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 255253541Sshin struct mbuf *m = NULL; /* newly allocated one */ 2553211501Sanchie struct m_tag *mtag; 255453541Sshin struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 255553541Sshin struct nd_redirect *nd_rd; 2556186157Skmacy struct llentry *ln = NULL; 255753541Sshin size_t maxlen; 255853541Sshin u_char *p; 255953541Sshin struct ifnet *outif = NULL; 256062587Sitojun struct sockaddr_in6 src_sa; 256153541Sshin 2562252563Sae icmp6_errcount(ND_REDIRECT, 0); 256362587Sitojun 256453541Sshin /* if we are not router, we don't send icmp6 redirect */ 2565181803Sbz if (!V_ip6_forwarding) 256653541Sshin goto fail; 256753541Sshin 256853541Sshin /* sanity check */ 256953541Sshin if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) 257053541Sshin goto fail; 257153541Sshin 257253541Sshin /* 257353541Sshin * Address check: 257453541Sshin * the source address must identify a neighbor, and 257553541Sshin * the destination address must not be a multicast address 257653541Sshin * [RFC 2461, sec 8.2] 257753541Sshin */ 257853541Sshin sip6 = mtod(m0, struct ip6_hdr *); 257962587Sitojun bzero(&src_sa, sizeof(src_sa)); 258062587Sitojun src_sa.sin6_family = AF_INET6; 258162587Sitojun src_sa.sin6_len = sizeof(src_sa); 258262587Sitojun src_sa.sin6_addr = sip6->ip6_src; 258362587Sitojun if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) 258453541Sshin goto fail; 258553541Sshin if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) 258653541Sshin goto fail; /* what should we do here? */ 258753541Sshin 258853541Sshin /* rate limit */ 258953541Sshin if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) 259053541Sshin goto fail; 259153541Sshin 259253541Sshin /* 259353541Sshin * Since we are going to append up to 1280 bytes (= IPV6_MMTU), 259453541Sshin * we almost always ask for an mbuf cluster for simplicity. 259553541Sshin * (MHLEN < IPV6_MMTU is almost always true) 259653541Sshin */ 259762587Sitojun#if IPV6_MMTU >= MCLBYTES 259862587Sitojun# error assumption failed about IPV6_MMTU and MCLBYTES 259962587Sitojun#endif 2600111119Simp MGETHDR(m, M_DONTWAIT, MT_HEADER); 260162587Sitojun if (m && IPV6_MMTU >= MHLEN) 2602111119Simp MCLGET(m, M_DONTWAIT); 260353541Sshin if (!m) 260453541Sshin goto fail; 2605232292Sbz M_SETFIB(m, rt->rt_fibnum); 260678064Sume m->m_pkthdr.rcvif = NULL; 260778064Sume m->m_len = 0; 260878064Sume maxlen = M_TRAILINGSPACE(m); 260953541Sshin maxlen = min(IPV6_MMTU, maxlen); 261053541Sshin /* just for safety */ 261162587Sitojun if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 261262587Sitojun ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { 261353541Sshin goto fail; 261462587Sitojun } 261553541Sshin 261653541Sshin { 261753541Sshin /* get ip6 linklocal address for ifp(my outgoing interface). */ 261862587Sitojun struct in6_ifaddr *ia; 261962587Sitojun if ((ia = in6ifa_ifpforlinklocal(ifp, 262062587Sitojun IN6_IFF_NOTREADY| 262162587Sitojun IN6_IFF_ANYCAST)) == NULL) 262253541Sshin goto fail; 262353541Sshin ifp_ll6 = &ia->ia_addr.sin6_addr; 2624194760Srwatson /* XXXRW: reference released prematurely. */ 2625194760Srwatson ifa_free(&ia->ia_ifa); 262653541Sshin } 262753541Sshin 262853541Sshin /* get ip6 linklocal address for the router. */ 262953541Sshin if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { 263053541Sshin struct sockaddr_in6 *sin6; 263153541Sshin sin6 = (struct sockaddr_in6 *)rt->rt_gateway; 263253541Sshin router_ll6 = &sin6->sin6_addr; 263353541Sshin if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) 263453541Sshin router_ll6 = (struct in6_addr *)NULL; 263553541Sshin } else 263653541Sshin router_ll6 = (struct in6_addr *)NULL; 263753541Sshin 263853541Sshin /* ip6 */ 263953541Sshin ip6 = mtod(m, struct ip6_hdr *); 264053541Sshin ip6->ip6_flow = 0; 264162587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 264262587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 264353541Sshin /* ip6->ip6_plen will be set later */ 264453541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 264553541Sshin ip6->ip6_hlim = 255; 264653541Sshin /* ip6->ip6_src must be linklocal addr for my outgoing if. */ 264753541Sshin bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); 264853541Sshin bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); 264953541Sshin 265053541Sshin /* ND Redirect */ 265153541Sshin nd_rd = (struct nd_redirect *)(ip6 + 1); 265253541Sshin nd_rd->nd_rd_type = ND_REDIRECT; 265353541Sshin nd_rd->nd_rd_code = 0; 265453541Sshin nd_rd->nd_rd_reserved = 0; 265553541Sshin if (rt->rt_flags & RTF_GATEWAY) { 265653541Sshin /* 265753541Sshin * nd_rd->nd_rd_target must be a link-local address in 265853541Sshin * better router cases. 265953541Sshin */ 266053541Sshin if (!router_ll6) 266153541Sshin goto fail; 266253541Sshin bcopy(router_ll6, &nd_rd->nd_rd_target, 2663120892Sume sizeof(nd_rd->nd_rd_target)); 266453541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2665120892Sume sizeof(nd_rd->nd_rd_dst)); 266653541Sshin } else { 266753541Sshin /* make sure redtgt == reddst */ 266853541Sshin bcopy(&sip6->ip6_dst, &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 } 267353541Sshin 267453541Sshin p = (u_char *)(nd_rd + 1); 267553541Sshin 267653541Sshin if (!router_ll6) 267753541Sshin goto nolladdropt; 267853541Sshin 2679120892Sume { 2680120892Sume /* target lladdr option */ 2681120892Sume int len; 2682120892Sume struct nd_opt_hdr *nd_opt; 2683120892Sume char *lladdr; 268453541Sshin 2685243382Sae IF_AFDATA_RLOCK(ifp); 2686186119Sqingli ln = nd6_lookup(router_ll6, 0, ifp); 2687243382Sae IF_AFDATA_RUNLOCK(ifp); 2688186157Skmacy if (ln == NULL) 2689120892Sume goto nolladdropt; 2690186119Sqingli 2691120892Sume len = sizeof(*nd_opt) + ifp->if_addrlen; 2692120892Sume len = (len + 7) & ~7; /* round by 8 */ 2693120892Sume /* safety check */ 2694186157Skmacy if (len + (p - (u_char *)ip6) > maxlen) 2695120892Sume goto nolladdropt; 2696186119Sqingli 2697186119Sqingli if (ln->la_flags & LLE_VALID) { 2698120892Sume nd_opt = (struct nd_opt_hdr *)p; 2699120892Sume nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 2700120892Sume nd_opt->nd_opt_len = len >> 3; 2701120892Sume lladdr = (char *)(nd_opt + 1); 2702186119Sqingli bcopy(&ln->ll_addr, lladdr, ifp->if_addrlen); 2703120892Sume p += len; 2704120892Sume } 2705186157Skmacy } 2706186157Skmacynolladdropt: 2707186157Skmacy if (ln != NULL) 2708186119Sqingli LLE_RUNLOCK(ln); 2709186157Skmacy 271053541Sshin m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 271153541Sshin 271253541Sshin /* just to be safe */ 271362587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 271453541Sshin if (m0->m_flags & M_DECRYPTED) 271553541Sshin goto noredhdropt; 271662587Sitojun#endif 271762587Sitojun if (p - (u_char *)ip6 > maxlen) 271862587Sitojun goto noredhdropt; 271953541Sshin 2720120891Sume { 2721120891Sume /* redirected header option */ 2722120891Sume int len; 2723120891Sume struct nd_opt_rd_hdr *nd_opt_rh; 272453541Sshin 2725120891Sume /* 2726120891Sume * compute the maximum size for icmp6 redirect header option. 2727120891Sume * XXX room for auth header? 2728120891Sume */ 2729120891Sume len = maxlen - (p - (u_char *)ip6); 2730120891Sume len &= ~7; 273153541Sshin 2732120891Sume /* This is just for simplicity. */ 2733120891Sume if (m0->m_pkthdr.len != m0->m_len) { 2734120891Sume if (m0->m_next) { 2735120891Sume m_freem(m0->m_next); 2736120891Sume m0->m_next = NULL; 2737120891Sume } 2738120891Sume m0->m_pkthdr.len = m0->m_len; 273953541Sshin } 274053541Sshin 2741120891Sume /* 2742120891Sume * Redirected header option spec (RFC2461 4.6.3) talks nothing 2743120891Sume * about padding/truncate rule for the original IP packet. 2744120891Sume * From the discussion on IPv6imp in Feb 1999, 2745120891Sume * the consensus was: 2746120891Sume * - "attach as much as possible" is the goal 2747120891Sume * - pad if not aligned (original size can be guessed by 2748120891Sume * original ip6 header) 2749120891Sume * Following code adds the padding if it is simple enough, 2750120891Sume * and truncates if not. 2751120891Sume */ 2752120891Sume if (m0->m_next || m0->m_pkthdr.len != m0->m_len) 2753120891Sume panic("assumption failed in %s:%d", __FILE__, 2754120891Sume __LINE__); 275553541Sshin 2756120891Sume if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { 2757120891Sume /* not enough room, truncate */ 2758120891Sume m0->m_pkthdr.len = m0->m_len = len - 2759120891Sume sizeof(*nd_opt_rh); 2760120891Sume } else { 2761120891Sume /* enough room, pad or truncate */ 2762120891Sume size_t extra; 276353541Sshin 2764120891Sume extra = m0->m_pkthdr.len % 8; 2765120891Sume if (extra) { 2766120891Sume /* pad if easy enough, truncate if not */ 2767120891Sume if (8 - extra <= M_TRAILINGSPACE(m0)) { 2768120891Sume /* pad */ 2769120891Sume m0->m_len += (8 - extra); 2770120891Sume m0->m_pkthdr.len += (8 - extra); 2771120891Sume } else { 2772120891Sume /* truncate */ 2773120891Sume m0->m_pkthdr.len -= extra; 2774120891Sume m0->m_len -= extra; 2775120891Sume } 277653541Sshin } 2777120891Sume len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); 2778120891Sume m0->m_pkthdr.len = m0->m_len = len - 2779120891Sume sizeof(*nd_opt_rh); 278053541Sshin } 278153541Sshin 2782120891Sume nd_opt_rh = (struct nd_opt_rd_hdr *)p; 2783120891Sume bzero(nd_opt_rh, sizeof(*nd_opt_rh)); 2784120891Sume nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; 2785120891Sume nd_opt_rh->nd_opt_rh_len = len >> 3; 2786120891Sume p += sizeof(*nd_opt_rh); 2787120891Sume m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 278853541Sshin 2789120891Sume /* connect m0 to m */ 2790120891Sume m_tag_delete_chain(m0, NULL); 2791120891Sume m0->m_flags &= ~M_PKTHDR; 2792120891Sume m->m_next = m0; 2793120891Sume m->m_pkthdr.len = m->m_len + m0->m_len; 2794120891Sume m0 = NULL; 2795120891Sume } 279653541Sshinnoredhdropt:; 2797112781Ssuz if (m0) { 2798112781Ssuz m_freem(m0); 2799112781Ssuz m0 = NULL; 2800112781Ssuz } 280153541Sshin 2802121315Sume /* XXX: clear embedded link IDs in the inner header */ 2803121315Sume in6_clearscope(&sip6->ip6_src); 2804121315Sume in6_clearscope(&sip6->ip6_dst); 2805121315Sume in6_clearscope(&nd_rd->nd_rd_target); 2806121315Sume in6_clearscope(&nd_rd->nd_rd_dst); 280753541Sshin 280853541Sshin ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 280953541Sshin 281053541Sshin nd_rd->nd_rd_cksum = 0; 2811120891Sume nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2812120891Sume sizeof(*ip6), ntohs(ip6->ip6_plen)); 281353541Sshin 2814211501Sanchie if (send_sendso_input_hook != NULL) { 2815211501Sanchie mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, sizeof(unsigned short), 2816211501Sanchie M_NOWAIT); 2817211501Sanchie if (mtag == NULL) 2818211501Sanchie goto fail; 2819211501Sanchie *(unsigned short *)(mtag + 1) = nd_rd->nd_rd_type; 2820211501Sanchie m_tag_prepend(m, mtag); 2821211501Sanchie } 2822211501Sanchie 282353541Sshin /* send the packet to outside... */ 2824105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 282553541Sshin if (outif) { 282653541Sshin icmp6_ifstat_inc(outif, ifs6_out_msg); 282753541Sshin icmp6_ifstat_inc(outif, ifs6_out_redirect); 282853541Sshin } 2829190964Srwatson ICMP6STAT_INC(icp6s_outhist[ND_REDIRECT]); 283053541Sshin 283153541Sshin return; 283253541Sshin 283353541Sshinfail: 283453541Sshin if (m) 283553541Sshin m_freem(m); 283653541Sshin if (m0) 283753541Sshin m_freem(m0); 283853541Sshin} 283953541Sshin 284053541Sshin/* 284153541Sshin * ICMPv6 socket option processing. 284253541Sshin */ 284353541Sshinint 2844171259Sdelphijicmp6_ctloutput(struct socket *so, struct sockopt *sopt) 284553541Sshin{ 284653541Sshin int error = 0; 284753541Sshin int optlen; 284878064Sume struct inpcb *inp = sotoinpcb(so); 284953541Sshin int level, op, optname; 285053541Sshin 285153541Sshin if (sopt) { 285253541Sshin level = sopt->sopt_level; 285353541Sshin op = sopt->sopt_dir; 285453541Sshin optname = sopt->sopt_name; 285553541Sshin optlen = sopt->sopt_valsize; 285653541Sshin } else 285753541Sshin level = op = optname = optlen = 0; 285878064Sume 285953541Sshin if (level != IPPROTO_ICMPV6) { 286053541Sshin return EINVAL; 286153541Sshin } 286253541Sshin 286378064Sume switch (op) { 286453541Sshin case PRCO_SETOPT: 286553541Sshin switch (optname) { 286653541Sshin case ICMP6_FILTER: 286753541Sshin { 2868180968Srwatson struct icmp6_filter ic6f; 286953541Sshin 2870180968Srwatson if (optlen != sizeof(ic6f)) { 287153541Sshin error = EMSGSIZE; 287253541Sshin break; 287353541Sshin } 2874180968Srwatson error = sooptcopyin(sopt, &ic6f, optlen, optlen); 2875180968Srwatson if (error == 0) { 2876180968Srwatson INP_WLOCK(inp); 2877180968Srwatson *inp->in6p_icmp6filt = ic6f; 2878180968Srwatson INP_WUNLOCK(inp); 2879180968Srwatson } 288053541Sshin break; 288153541Sshin } 288253541Sshin 288353541Sshin default: 288453541Sshin error = ENOPROTOOPT; 288553541Sshin break; 288653541Sshin } 288753541Sshin break; 288853541Sshin 288953541Sshin case PRCO_GETOPT: 289053541Sshin switch (optname) { 289153541Sshin case ICMP6_FILTER: 289253541Sshin { 2893180968Srwatson struct icmp6_filter ic6f; 2894180968Srwatson 2895180968Srwatson INP_RLOCK(inp); 2896180968Srwatson ic6f = *inp->in6p_icmp6filt; 2897180968Srwatson INP_RUNLOCK(inp); 2898180968Srwatson error = sooptcopyout(sopt, &ic6f, sizeof(ic6f)); 289953541Sshin break; 290053541Sshin } 290153541Sshin 290253541Sshin default: 290353541Sshin error = ENOPROTOOPT; 290453541Sshin break; 290553541Sshin } 290653541Sshin break; 290753541Sshin } 290853541Sshin 2909120856Sume return (error); 291053541Sshin} 291153541Sshin 291253541Sshin/* 291353541Sshin * Perform rate limit check. 291453541Sshin * Returns 0 if it is okay to send the icmp6 packet. 291553541Sshin * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 291653541Sshin * limitation. 291753541Sshin * 291853541Sshin * XXX per-destination/type check necessary? 2919171259Sdelphij * 2920171259Sdelphij * dst - not used at this moment 2921171259Sdelphij * type - not used at this moment 2922171259Sdelphij * code - not used at this moment 292353541Sshin */ 292453541Sshinstatic int 2925171259Sdelphijicmp6_ratelimit(const struct in6_addr *dst, const int type, 2926171259Sdelphij const int code) 292753541Sshin{ 292862587Sitojun int ret; 292953541Sshin 293095023Ssuz ret = 0; /* okay to send */ 293153541Sshin 293262587Sitojun /* PPS limit */ 2933181803Sbz if (!ppsratecheck(&V_icmp6errppslim_last, &V_icmp6errpps_count, 2934181803Sbz V_icmp6errppslim)) { 293553541Sshin /* The packet is subject to rate limit */ 293662587Sitojun ret++; 293753541Sshin } 293853541Sshin 293962587Sitojun return ret; 294053541Sshin} 2941