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" 6853541Sshin 6953541Sshin#include <sys/param.h> 7095759Stanimura#include <sys/domain.h> 71193066Sjamie#include <sys/jail.h> 7295759Stanimura#include <sys/kernel.h> 7395759Stanimura#include <sys/lock.h> 7478064Sume#include <sys/malloc.h> 7553541Sshin#include <sys/mbuf.h> 76196019Srwatson#include <sys/proc.h> 7753541Sshin#include <sys/protosw.h> 7895759Stanimura#include <sys/signalvar.h> 7953541Sshin#include <sys/socket.h> 8053541Sshin#include <sys/socketvar.h> 8195759Stanimura#include <sys/sx.h> 8295759Stanimura#include <sys/syslog.h> 8395759Stanimura#include <sys/systm.h> 8453541Sshin#include <sys/time.h> 8553541Sshin 8653541Sshin#include <net/if.h> 8753541Sshin#include <net/if_dl.h> 88186119Sqingli#include <net/if_llatbl.h> 8953541Sshin#include <net/if_types.h> 9095759Stanimura#include <net/route.h> 91185571Sbz#include <net/vnet.h> 9253541Sshin 9353541Sshin#include <netinet/in.h> 9495759Stanimura#include <netinet/in_pcb.h> 9553541Sshin#include <netinet/in_var.h> 9662587Sitojun#include <netinet/ip6.h> 9795759Stanimura#include <netinet/icmp6.h> 98122922Sandre#include <netinet/tcp_var.h> 99185571Sbz 10095759Stanimura#include <netinet6/in6_ifattach.h> 10195759Stanimura#include <netinet6/in6_pcb.h> 10295759Stanimura#include <netinet6/ip6protosw.h> 10353541Sshin#include <netinet6/ip6_var.h> 104148385Sume#include <netinet6/scope6_var.h> 10553541Sshin#include <netinet6/mld6_var.h> 10653541Sshin#include <netinet6/nd6.h> 107211501Sanchie#include <netinet6/send.h> 10853541Sshin 10962587Sitojunextern struct domain inet6domain; 110185348Szec 111253085SaeVNET_PCPUSTAT_DEFINE(struct icmp6stat, icmp6stat); 112253085SaeVNET_PCPUSTAT_SYSINIT(icmp6stat); 113207369Sbz 114253085Sae#ifdef VIMAGE 115253085SaeVNET_PCPUSTAT_SYSUNINIT(icmp6stat); 116253085Sae#endif /* VIMAGE */ 117253085Sae 118195699SrwatsonVNET_DECLARE(struct inpcbinfo, ripcbinfo); 119195699SrwatsonVNET_DECLARE(struct inpcbhead, ripcb); 120195699SrwatsonVNET_DECLARE(int, icmp6errppslim); 121215701Sdimstatic VNET_DEFINE(int, icmp6errpps_count) = 0; 122215701Sdimstatic VNET_DEFINE(struct timeval, icmp6errppslim_last); 123195699SrwatsonVNET_DECLARE(int, icmp6_nodeinfo); 12453541Sshin 125195727Srwatson#define V_ripcbinfo VNET(ripcbinfo) 126195727Srwatson#define V_ripcb VNET(ripcb) 127195727Srwatson#define V_icmp6errppslim VNET(icmp6errppslim) 128207369Sbz#define V_icmp6errpps_count VNET(icmp6errpps_count) 129207369Sbz#define V_icmp6errppslim_last VNET(icmp6errppslim_last) 130195727Srwatson#define V_icmp6_nodeinfo VNET(icmp6_nodeinfo) 131185088Szec 132251995Saestatic void icmp6_errcount(int, int); 133175162Sobrienstatic int icmp6_rip6_input(struct mbuf **, int); 134175162Sobrienstatic int icmp6_ratelimit(const struct in6_addr *, const int, const int); 135241916Sdelphijstatic const char *icmp6_redirect_diag(struct in6_addr *, 136241916Sdelphij struct in6_addr *, struct in6_addr *); 137175162Sobrienstatic struct mbuf *ni6_input(struct mbuf *, int); 138175162Sobrienstatic struct mbuf *ni6_nametodns(const char *, int, int); 139175162Sobrienstatic int ni6_dnsmatch(const char *, int, const char *, int); 140241916Sdelphijstatic int ni6_addrs(struct icmp6_nodeinfo *, struct mbuf *, 141241916Sdelphij struct ifnet **, struct in6_addr *); 142241916Sdelphijstatic int ni6_store_addrs(struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, 143241916Sdelphij struct ifnet *, int); 144175162Sobrienstatic int icmp6_notify_error(struct mbuf **, int, int, int); 14553541Sshin 146196039Srwatson/* 147196039Srwatson * Kernel module interface for updating icmp6stat. The argument is an index 148196039Srwatson * into icmp6stat treated as an array of u_quad_t. While this encodes the 149196039Srwatson * general layout of icmp6stat into the caller, it doesn't encode its 150196039Srwatson * location, so that future changes to add, for example, per-CPU stats 151196039Srwatson * support won't cause binary compatibility problems for kernel modules. 152196039Srwatson */ 153196039Srwatsonvoid 154196039Srwatsonkmod_icmp6stat_inc(int statnum) 155196039Srwatson{ 156196039Srwatson 157253085Sae counter_u64_add(VNET(icmp6stat)[statnum], 1); 158196039Srwatson} 159196039Srwatson 16062587Sitojunstatic void 161251995Saeicmp6_errcount(int type, int code) 16262587Sitojun{ 16378064Sume switch (type) { 16462587Sitojun case ICMP6_DST_UNREACH: 16562587Sitojun switch (code) { 16662587Sitojun case ICMP6_DST_UNREACH_NOROUTE: 167251995Sae ICMP6STAT_INC(icp6s_odst_unreach_noroute); 16862587Sitojun return; 16962587Sitojun case ICMP6_DST_UNREACH_ADMIN: 170251995Sae ICMP6STAT_INC(icp6s_odst_unreach_admin); 17162587Sitojun return; 17262587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 173251995Sae ICMP6STAT_INC(icp6s_odst_unreach_beyondscope); 17462587Sitojun return; 17562587Sitojun case ICMP6_DST_UNREACH_ADDR: 176251995Sae ICMP6STAT_INC(icp6s_odst_unreach_addr); 17762587Sitojun return; 17862587Sitojun case ICMP6_DST_UNREACH_NOPORT: 179251995Sae ICMP6STAT_INC(icp6s_odst_unreach_noport); 18062587Sitojun return; 18162587Sitojun } 18262587Sitojun break; 18362587Sitojun case ICMP6_PACKET_TOO_BIG: 184251995Sae ICMP6STAT_INC(icp6s_opacket_too_big); 18562587Sitojun return; 18662587Sitojun case ICMP6_TIME_EXCEEDED: 18778064Sume switch (code) { 18862587Sitojun case ICMP6_TIME_EXCEED_TRANSIT: 189251995Sae ICMP6STAT_INC(icp6s_otime_exceed_transit); 19062587Sitojun return; 19162587Sitojun case ICMP6_TIME_EXCEED_REASSEMBLY: 192251995Sae ICMP6STAT_INC(icp6s_otime_exceed_reassembly); 19362587Sitojun return; 19462587Sitojun } 19562587Sitojun break; 19662587Sitojun case ICMP6_PARAM_PROB: 19778064Sume switch (code) { 19862587Sitojun case ICMP6_PARAMPROB_HEADER: 199251995Sae ICMP6STAT_INC(icp6s_oparamprob_header); 20062587Sitojun return; 20162587Sitojun case ICMP6_PARAMPROB_NEXTHEADER: 202251995Sae ICMP6STAT_INC(icp6s_oparamprob_nextheader); 20362587Sitojun return; 20462587Sitojun case ICMP6_PARAMPROB_OPTION: 205251995Sae ICMP6STAT_INC(icp6s_oparamprob_option); 20662587Sitojun return; 20762587Sitojun } 20862587Sitojun break; 20962587Sitojun case ND_REDIRECT: 210251995Sae ICMP6STAT_INC(icp6s_oredirect); 21162587Sitojun return; 21262587Sitojun } 213251995Sae ICMP6STAT_INC(icp6s_ounknown); 21462587Sitojun} 21562587Sitojun 21653541Sshin/* 217148385Sume * A wrapper function for icmp6_error() necessary when the erroneous packet 218148385Sume * may not contain enough scope zone information. 219148385Sume */ 220148385Sumevoid 221171259Sdelphijicmp6_error2(struct mbuf *m, int type, int code, int param, 222171259Sdelphij struct ifnet *ifp) 223148385Sume{ 224148385Sume struct ip6_hdr *ip6; 225148385Sume 226148385Sume if (ifp == NULL) 227148385Sume return; 228148385Sume 229148385Sume#ifndef PULLDOWN_TEST 230148385Sume IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 231148385Sume#else 232148385Sume if (m->m_len < sizeof(struct ip6_hdr)) { 233148385Sume m = m_pullup(m, sizeof(struct ip6_hdr)); 234148385Sume if (m == NULL) 235148385Sume return; 236148385Sume } 237148385Sume#endif 238148385Sume 239148385Sume ip6 = mtod(m, struct ip6_hdr *); 240148385Sume 241148385Sume if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0) 242148385Sume return; 243148385Sume if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) 244148385Sume return; 245148385Sume 246148385Sume icmp6_error(m, type, code, param); 247148385Sume} 248148385Sume 249148385Sume/* 25053541Sshin * Generate an error packet of type error in response to bad IP6 packet. 25153541Sshin */ 25253541Sshinvoid 253171259Sdelphijicmp6_error(struct mbuf *m, int type, int code, int param) 25453541Sshin{ 25553541Sshin struct ip6_hdr *oip6, *nip6; 25653541Sshin struct icmp6_hdr *icmp6; 25762587Sitojun u_int preplen; 25853541Sshin int off; 25962587Sitojun int nxt; 26053541Sshin 261190964Srwatson ICMP6STAT_INC(icp6s_error); 26253541Sshin 26362587Sitojun /* count per-type-code statistics */ 264251995Sae icmp6_errcount(type, code); 26562587Sitojun 26662587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 26762587Sitojun if (m->m_flags & M_DECRYPTED) { 268190964Srwatson ICMP6STAT_INC(icp6s_canterror); 26953541Sshin goto freeit; 27062587Sitojun } 27162587Sitojun#endif 27253541Sshin 27362587Sitojun#ifndef PULLDOWN_TEST 27462587Sitojun IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 27562587Sitojun#else 27662587Sitojun if (m->m_len < sizeof(struct ip6_hdr)) { 27762587Sitojun m = m_pullup(m, sizeof(struct ip6_hdr)); 27862587Sitojun if (m == NULL) 27962587Sitojun return; 28062587Sitojun } 28162587Sitojun#endif 28253541Sshin oip6 = mtod(m, struct ip6_hdr *); 28353541Sshin 28453541Sshin /* 285121472Sume * If the destination address of the erroneous packet is a multicast 286121472Sume * address, or the packet was sent using link-layer multicast, 287121472Sume * we should basically suppress sending an error (RFC 2463, Section 288121472Sume * 2.4). 289121472Sume * We have two exceptions (the item e.2 in that section): 290201688Sbz * - the Packet Too Big message can be sent for path MTU discovery. 291121472Sume * - the Parameter Problem Message that can be allowed an icmp6 error 292121472Sume * in the option type field. This check has been done in 293121472Sume * ip6_unknown_opt(), so we can just check the type and code. 29453541Sshin */ 29553541Sshin if ((m->m_flags & (M_BCAST|M_MCAST) || 29653541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && 29753541Sshin (type != ICMP6_PACKET_TOO_BIG && 29853541Sshin (type != ICMP6_PARAM_PROB || 29953541Sshin code != ICMP6_PARAMPROB_OPTION))) 30053541Sshin goto freeit; 30153541Sshin 302121472Sume /* 303121472Sume * RFC 2463, 2.4 (e.5): source address check. 304121472Sume * XXX: the case of anycast source? 305121472Sume */ 30653541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || 30753541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) 30853541Sshin goto freeit; 30953541Sshin 31053541Sshin /* 31162587Sitojun * If we are about to send ICMPv6 against ICMPv6 error/redirect, 31262587Sitojun * don't do it. 31353541Sshin */ 31462587Sitojun nxt = -1; 31562587Sitojun off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 31662587Sitojun if (off >= 0 && nxt == IPPROTO_ICMPV6) { 31753541Sshin struct icmp6_hdr *icp; 31853541Sshin 31962587Sitojun#ifndef PULLDOWN_TEST 32062587Sitojun IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), ); 32162587Sitojun icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 32262587Sitojun#else 32362587Sitojun IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, 32462587Sitojun sizeof(*icp)); 32562587Sitojun if (icp == NULL) { 326190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 32762587Sitojun return; 32862587Sitojun } 32962587Sitojun#endif 33062587Sitojun if (icp->icmp6_type < ICMP6_ECHO_REQUEST || 33162587Sitojun icp->icmp6_type == ND_REDIRECT) { 33253541Sshin /* 33362587Sitojun * ICMPv6 error 33462587Sitojun * Special case: for redirect (which is 33562587Sitojun * informational) we must not send icmp6 error. 33653541Sshin */ 337190964Srwatson ICMP6STAT_INC(icp6s_canterror); 33862587Sitojun goto freeit; 33962587Sitojun } else { 34062587Sitojun /* ICMPv6 informational - send the error */ 34153541Sshin } 34262587Sitojun } else { 34362587Sitojun /* non-ICMPv6 - send the error */ 34453541Sshin } 34553541Sshin 34653541Sshin oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */ 34753541Sshin 34853541Sshin /* Finally, do rate limitation check. */ 34953541Sshin if (icmp6_ratelimit(&oip6->ip6_src, type, code)) { 350190964Srwatson ICMP6STAT_INC(icp6s_toofreq); 35153541Sshin goto freeit; 35253541Sshin } 35353541Sshin 35453541Sshin /* 35553541Sshin * OK, ICMP6 can be generated. 35653541Sshin */ 35753541Sshin 35853541Sshin if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) 35953541Sshin m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); 36053541Sshin 36162587Sitojun preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 362243882Sglebius M_PREPEND(m, preplen, M_NOWAIT); /* FIB is also copied over. */ 36353541Sshin if (m == NULL) { 36478064Sume nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__)); 36553541Sshin return; 36653541Sshin } 36753541Sshin 36853541Sshin nip6 = mtod(m, struct ip6_hdr *); 36953541Sshin nip6->ip6_src = oip6->ip6_src; 37053541Sshin nip6->ip6_dst = oip6->ip6_dst; 37153541Sshin 372121315Sume in6_clearscope(&oip6->ip6_src); 373121315Sume in6_clearscope(&oip6->ip6_dst); 37453541Sshin 37553541Sshin icmp6 = (struct icmp6_hdr *)(nip6 + 1); 37653541Sshin icmp6->icmp6_type = type; 37753541Sshin icmp6->icmp6_code = code; 37853541Sshin icmp6->icmp6_pptr = htonl((u_int32_t)param); 37953541Sshin 38078064Sume /* 38178064Sume * icmp6_reflect() is designed to be in the input path. 382148987Sume * icmp6_error() can be called from both input and output path, 38378064Sume * and if we are in output path rcvif could contain bogus value. 38478064Sume * clear m->m_pkthdr.rcvif for safety, we should have enough scope 38578064Sume * information in ip header (nip6). 38678064Sume */ 38778064Sume m->m_pkthdr.rcvif = NULL; 38878064Sume 389190964Srwatson ICMP6STAT_INC(icp6s_outhist[type]); 39095023Ssuz icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ 39162587Sitojun 39262587Sitojun return; 39362587Sitojun 39462587Sitojun freeit: 39562587Sitojun /* 396148987Sume * If we can't tell whether or not we can generate ICMP6, free it. 39762587Sitojun */ 39862587Sitojun m_freem(m); 39953541Sshin} 40053541Sshin 40153541Sshin/* 40253541Sshin * Process a received ICMP6 message. 40353541Sshin */ 40453541Sshinint 405171259Sdelphijicmp6_input(struct mbuf **mp, int *offp, int proto) 40653541Sshin{ 40753541Sshin struct mbuf *m = *mp, *n; 408192923Sbms struct ifnet *ifp; 40953541Sshin struct ip6_hdr *ip6, *nip6; 41053541Sshin struct icmp6_hdr *icmp6, *nicmp6; 41153541Sshin int off = *offp; 41253541Sshin int icmp6len = m->m_pkthdr.len - *offp; 41353541Sshin int code, sum, noff; 414165118Sbz char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 415211501Sanchie int ip6len, error; 41653541Sshin 417192923Sbms ifp = m->m_pkthdr.rcvif; 418192923Sbms 41962587Sitojun#ifndef PULLDOWN_TEST 42053541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE); 42195023Ssuz /* m might change if M_LOOP. So, call mtod after this */ 42262587Sitojun#endif 42353541Sshin 42453541Sshin /* 42553541Sshin * Locate icmp6 structure in mbuf, and check 42653541Sshin * that not corrupted and of at least minimum length 42753541Sshin */ 42853541Sshin 42953541Sshin ip6 = mtod(m, struct ip6_hdr *); 430211501Sanchie ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen); 43153541Sshin if (icmp6len < sizeof(struct icmp6_hdr)) { 432190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 43353541Sshin goto freeit; 43453541Sshin } 43553541Sshin 43653541Sshin /* 437191672Sbms * Check multicast group membership. 438191672Sbms * Note: SSM filters are not applied for ICMPv6 traffic. 439191672Sbms */ 440191672Sbms if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 441192923Sbms struct in6_multi *inm; 442191672Sbms 443191672Sbms inm = in6m_lookup(ifp, &ip6->ip6_dst); 444191672Sbms if (inm == NULL) { 445191672Sbms IP6STAT_INC(ip6s_notmember); 446191672Sbms in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 447191672Sbms goto freeit; 448191672Sbms } 449191672Sbms } 450191672Sbms 451191672Sbms /* 45253541Sshin * calculate the checksum 45353541Sshin */ 45462587Sitojun#ifndef PULLDOWN_TEST 45553541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 45662587Sitojun#else 45762587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 45862587Sitojun if (icmp6 == NULL) { 459190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 46062587Sitojun return IPPROTO_DONE; 46162587Sitojun } 46262587Sitojun#endif 46353541Sshin code = icmp6->icmp6_code; 46453541Sshin 46553541Sshin if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { 46678064Sume nd6log((LOG_ERR, 46753541Sshin "ICMP6 checksum error(%d|%x) %s\n", 468165118Sbz icmp6->icmp6_type, sum, 469165118Sbz ip6_sprintf(ip6bufs, &ip6->ip6_src))); 470190964Srwatson ICMP6STAT_INC(icp6s_checksum); 47153541Sshin goto freeit; 47253541Sshin } 47353541Sshin 47483934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 47553541Sshin /* 47653541Sshin * Deliver very specific ICMP6 type only. 477148987Sume * This is important to deliver TOOBIG. Otherwise PMTUD 47853541Sshin * will not work. 47953541Sshin */ 48053541Sshin switch (icmp6->icmp6_type) { 48153541Sshin case ICMP6_DST_UNREACH: 48253541Sshin case ICMP6_PACKET_TOO_BIG: 48353541Sshin case ICMP6_TIME_EXCEEDED: 48453541Sshin break; 48553541Sshin default: 48653541Sshin goto freeit; 48753541Sshin } 48853541Sshin } 48953541Sshin 490190964Srwatson ICMP6STAT_INC(icp6s_inhist[icmp6->icmp6_type]); 491192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_msg); 49253541Sshin if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) 493192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_error); 49453541Sshin 49553541Sshin switch (icmp6->icmp6_type) { 49653541Sshin case ICMP6_DST_UNREACH: 497192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_dstunreach); 49853541Sshin switch (code) { 49953541Sshin case ICMP6_DST_UNREACH_NOROUTE: 50053541Sshin code = PRC_UNREACH_NET; 50153541Sshin break; 50253541Sshin case ICMP6_DST_UNREACH_ADMIN: 503192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_adminprohib); 50462587Sitojun code = PRC_UNREACH_PROTOCOL; /* is this a good code? */ 50562587Sitojun break; 50653541Sshin case ICMP6_DST_UNREACH_ADDR: 50762587Sitojun code = PRC_HOSTDEAD; 50853541Sshin break; 50962587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 51062587Sitojun /* I mean "source address was incorrect." */ 51162587Sitojun code = PRC_PARAMPROB; 51262587Sitojun break; 51353541Sshin case ICMP6_DST_UNREACH_NOPORT: 51453541Sshin code = PRC_UNREACH_PORT; 51553541Sshin break; 51653541Sshin default: 51753541Sshin goto badcode; 51853541Sshin } 51953541Sshin goto deliver; 52053541Sshin break; 52153541Sshin 52253541Sshin case ICMP6_PACKET_TOO_BIG: 523192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_pkttoobig); 52453541Sshin 525126194Sume /* validation is made in icmp6_mtudisc_update */ 526126194Sume 52753541Sshin code = PRC_MSGSIZE; 52853541Sshin 52962587Sitojun /* 53062587Sitojun * Updating the path MTU will be done after examining 53162587Sitojun * intermediate extension headers. 53262587Sitojun */ 53353541Sshin goto deliver; 53453541Sshin break; 53553541Sshin 53653541Sshin case ICMP6_TIME_EXCEEDED: 537192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_timeexceed); 53853541Sshin switch (code) { 53953541Sshin case ICMP6_TIME_EXCEED_TRANSIT: 540151465Ssuz code = PRC_TIMXCEED_INTRANS; 541151465Ssuz break; 54253541Sshin case ICMP6_TIME_EXCEED_REASSEMBLY: 543151465Ssuz code = PRC_TIMXCEED_REASS; 54453541Sshin break; 54553541Sshin default: 54653541Sshin goto badcode; 54753541Sshin } 54853541Sshin goto deliver; 54953541Sshin break; 55053541Sshin 55153541Sshin case ICMP6_PARAM_PROB: 552192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_paramprob); 55353541Sshin switch (code) { 55453541Sshin case ICMP6_PARAMPROB_NEXTHEADER: 55553541Sshin code = PRC_UNREACH_PROTOCOL; 55653541Sshin break; 55753541Sshin case ICMP6_PARAMPROB_HEADER: 55853541Sshin case ICMP6_PARAMPROB_OPTION: 55953541Sshin code = PRC_PARAMPROB; 56053541Sshin break; 56153541Sshin default: 56253541Sshin goto badcode; 56353541Sshin } 56453541Sshin goto deliver; 56553541Sshin break; 56653541Sshin 56753541Sshin case ICMP6_ECHO_REQUEST: 568192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_echo); 56953541Sshin if (code != 0) 57053541Sshin goto badcode; 57153541Sshin if ((n = m_copy(m, 0, M_COPYALL)) == NULL) { 57253541Sshin /* Give up remote */ 57353541Sshin break; 57453541Sshin } 57562587Sitojun if ((n->m_flags & M_EXT) != 0 57662587Sitojun || n->m_len < off + sizeof(struct icmp6_hdr)) { 57753541Sshin struct mbuf *n0 = n; 578108741Ssam int n0len; 57953541Sshin 580248328Sglebius CTASSERT(sizeof(*nip6) + sizeof(*nicmp6) <= MHLEN); 581248321Sglebius n = m_gethdr(M_NOWAIT, n0->m_type); 58253541Sshin if (n == NULL) { 58353541Sshin /* Give up remote */ 58453541Sshin m_freem(n0); 58553541Sshin break; 58653541Sshin } 587248328Sglebius 588248328Sglebius m_move_pkthdr(n, n0); /* FIB copied. */ 589248328Sglebius n0len = n0->m_pkthdr.len; /* save for use below */ 59053541Sshin /* 59153541Sshin * Copy IPv6 and ICMPv6 only. 59253541Sshin */ 59353541Sshin nip6 = mtod(n, struct ip6_hdr *); 59453541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 59553541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 59653541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 59762587Sitojun noff = sizeof(struct ip6_hdr); 598108741Ssam /* new mbuf contains only ipv6+icmpv6 headers */ 599108741Ssam n->m_len = noff + sizeof(struct icmp6_hdr); 60053541Sshin /* 601120891Sume * Adjust mbuf. ip6_plen will be adjusted in 60262587Sitojun * ip6_output(). 60353541Sshin */ 60462587Sitojun m_adj(n0, off + sizeof(struct icmp6_hdr)); 605108741Ssam /* recalculate complete packet size */ 606108741Ssam n->m_pkthdr.len = n0len + (noff - off); 60762587Sitojun n->m_next = n0; 60853541Sshin } else { 60953541Sshin nip6 = mtod(n, struct ip6_hdr *); 610166046Sume IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off, 611166046Sume sizeof(*nicmp6)); 61253541Sshin noff = off; 61353541Sshin } 61453541Sshin nicmp6->icmp6_type = ICMP6_ECHO_REPLY; 61553541Sshin nicmp6->icmp6_code = 0; 61653541Sshin if (n) { 617190964Srwatson ICMP6STAT_INC(icp6s_reflect); 618190964Srwatson ICMP6STAT_INC(icp6s_outhist[ICMP6_ECHO_REPLY]); 61953541Sshin icmp6_reflect(n, noff); 62053541Sshin } 62153541Sshin break; 62253541Sshin 62353541Sshin case ICMP6_ECHO_REPLY: 624192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_echoreply); 62553541Sshin if (code != 0) 62653541Sshin goto badcode; 62753541Sshin break; 62853541Sshin 62996116Sume case MLD_LISTENER_QUERY: 63096116Sume case MLD_LISTENER_REPORT: 631191672Sbms case MLD_LISTENER_DONE: 632191672Sbms case MLDV2_LISTENER_REPORT: 633191672Sbms /* 634192923Sbms * Drop MLD traffic which is not link-local, has a hop limit 635192923Sbms * of greater than 1 hop, or which does not have the 636192923Sbms * IPv6 HBH Router Alert option. 637192923Sbms * As IPv6 HBH options are stripped in ip6_input() we must 638192923Sbms * check an mbuf header flag. 639191672Sbms * XXX Should we also sanity check that these messages 640191672Sbms * were directed to a link-local multicast prefix? 641191672Sbms */ 642192923Sbms if ((ip6->ip6_hlim != 1) || (m->m_flags & M_RTALERT_MLD) == 0) 64362587Sitojun goto freeit; 644191672Sbms if (mld_input(m, off, icmp6len) != 0) 645191672Sbms return (IPPROTO_DONE); 64653541Sshin /* m stays. */ 64753541Sshin break; 64853541Sshin 64953541Sshin case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ 65053541Sshin { 65153541Sshin enum { WRU, FQDN } mode; 65253541Sshin 653181803Sbz if (!V_icmp6_nodeinfo) 65462587Sitojun break; 65562587Sitojun 65653541Sshin if (icmp6len == sizeof(struct icmp6_hdr) + 4) 65753541Sshin mode = WRU; 65862587Sitojun else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) 65953541Sshin mode = FQDN; 66053541Sshin else 66153541Sshin goto badlen; 66253541Sshin 66353541Sshin if (mode == FQDN) { 66462587Sitojun#ifndef PULLDOWN_TEST 66553541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), 666120891Sume IPPROTO_DONE); 66762587Sitojun#endif 66862587Sitojun n = m_copy(m, 0, M_COPYALL); 66962587Sitojun if (n) 67062587Sitojun n = ni6_input(n, off); 67162587Sitojun /* XXX meaningless if n == NULL */ 67253541Sshin noff = sizeof(struct ip6_hdr); 67353541Sshin } else { 674193066Sjamie struct prison *pr; 67553541Sshin u_char *p; 676248328Sglebius int maxhlen, hlen; 67753541Sshin 678169664Sjinmei /* 679169664Sjinmei * XXX: this combination of flags is pointless, 680169664Sjinmei * but should we keep this for compatibility? 681169664Sjinmei */ 682303459Ssbruno if ((V_icmp6_nodeinfo & (ICMP6_NODEINFO_FQDNOK | 683303459Ssbruno ICMP6_NODEINFO_TMPADDROK)) != 684303459Ssbruno (ICMP6_NODEINFO_FQDNOK | ICMP6_NODEINFO_TMPADDROK)) 68578064Sume break; 68678064Sume 68762587Sitojun if (code != 0) 68862587Sitojun goto badcode; 689248328Sglebius 690248328Sglebius CTASSERT(sizeof(*nip6) + sizeof(*nicmp6) + 4 <= MHLEN); 691248328Sglebius n = m_gethdr(M_NOWAIT, m->m_type); 692248328Sglebius if (n == NULL) { 69362587Sitojun /* Give up remote */ 69462587Sitojun break; 69562587Sitojun } 696248328Sglebius if (!m_dup_pkthdr(n, m, M_NOWAIT)) { 697108466Ssam /* 698108466Ssam * Previous code did a blind M_COPY_PKTHDR 699108466Ssam * and said "just for rcvif". If true, then 700108466Ssam * we could tolerate the dup failing (due to 701108466Ssam * the deep copy of the tag chain). For now 702108466Ssam * be conservative and just fail. 703108466Ssam */ 704108466Ssam m_free(n); 705108466Ssam n = NULL; 706108466Ssam } 707248328Sglebius maxhlen = M_TRAILINGSPACE(n) - 708248328Sglebius (sizeof(*nip6) + sizeof(*nicmp6) + 4); 709193066Sjamie pr = curthread->td_ucred->cr_prison; 710193066Sjamie mtx_lock(&pr->pr_mtx); 711194118Sjamie hlen = strlen(pr->pr_hostname); 712193066Sjamie if (maxhlen > hlen) 713193066Sjamie maxhlen = hlen; 71453541Sshin /* 71553541Sshin * Copy IPv6 and ICMPv6 only. 71653541Sshin */ 71753541Sshin nip6 = mtod(n, struct ip6_hdr *); 71853541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 71953541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 72053541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 72153541Sshin p = (u_char *)(nicmp6 + 1); 72253541Sshin bzero(p, 4); 723194118Sjamie /* meaningless TTL */ 724194118Sjamie bcopy(pr->pr_hostname, p + 4, maxhlen); 725193066Sjamie mtx_unlock(&pr->pr_mtx); 72653541Sshin noff = sizeof(struct ip6_hdr); 72753541Sshin n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 72862587Sitojun sizeof(struct icmp6_hdr) + 4 + maxhlen; 72953541Sshin nicmp6->icmp6_type = ICMP6_WRUREPLY; 73053541Sshin nicmp6->icmp6_code = 0; 73153541Sshin } 73253541Sshin if (n) { 733190964Srwatson ICMP6STAT_INC(icp6s_reflect); 734190964Srwatson ICMP6STAT_INC(icp6s_outhist[ICMP6_WRUREPLY]); 73553541Sshin icmp6_reflect(n, noff); 73653541Sshin } 73753541Sshin break; 73853541Sshin } 73953541Sshin 74053541Sshin case ICMP6_WRUREPLY: 74153541Sshin if (code != 0) 74253541Sshin goto badcode; 74353541Sshin break; 74453541Sshin 74553541Sshin case ND_ROUTER_SOLICIT: 746192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_routersolicit); 74753541Sshin if (code != 0) 74853541Sshin goto badcode; 74953541Sshin if (icmp6len < sizeof(struct nd_router_solicit)) 75053541Sshin goto badlen; 751243882Sglebius if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) { 75262587Sitojun /* give up local */ 753211501Sanchie 754211501Sanchie /* Send incoming SeND packet to user space. */ 755211501Sanchie if (send_sendso_input_hook != NULL) { 756211501Sanchie IP6_EXTHDR_CHECK(m, off, 757211501Sanchie icmp6len, IPPROTO_DONE); 758211501Sanchie error = send_sendso_input_hook(m, ifp, 759211501Sanchie SND_IN, ip6len); 760211501Sanchie /* -1 == no app on SEND socket */ 761211501Sanchie if (error == 0) 762211501Sanchie return (IPPROTO_DONE); 763211501Sanchie nd6_rs_input(m, off, icmp6len); 764211501Sanchie } else 765211501Sanchie nd6_rs_input(m, off, icmp6len); 76662587Sitojun m = NULL; 76762587Sitojun goto freeit; 76862587Sitojun } 769211501Sanchie if (send_sendso_input_hook != NULL) { 770211520Sanchie IP6_EXTHDR_CHECK(n, off, 771211501Sanchie icmp6len, IPPROTO_DONE); 772211501Sanchie error = send_sendso_input_hook(n, ifp, 773211501Sanchie SND_IN, ip6len); 774211520Sanchie if (error == 0) 775211520Sanchie goto freeit; 776211501Sanchie /* -1 == no app on SEND socket */ 777211501Sanchie nd6_rs_input(n, off, icmp6len); 778211501Sanchie } else 779211501Sanchie nd6_rs_input(n, off, icmp6len); 78053541Sshin /* m stays. */ 78153541Sshin break; 78253541Sshin 78353541Sshin case ND_ROUTER_ADVERT: 784192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_routeradvert); 78553541Sshin if (code != 0) 78653541Sshin goto badcode; 78753541Sshin if (icmp6len < sizeof(struct nd_router_advert)) 78853541Sshin goto badlen; 789243882Sglebius if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) { 790211501Sanchie 791211501Sanchie /* Send incoming SeND-protected/ND packet to user space. */ 792211501Sanchie if (send_sendso_input_hook != NULL) { 793211501Sanchie error = send_sendso_input_hook(m, ifp, 794211501Sanchie SND_IN, ip6len); 795211501Sanchie if (error == 0) 796211501Sanchie return (IPPROTO_DONE); 797211501Sanchie nd6_ra_input(m, off, icmp6len); 798211501Sanchie } else 799211501Sanchie nd6_ra_input(m, off, icmp6len); 80062587Sitojun m = NULL; 80162587Sitojun goto freeit; 80262587Sitojun } 803211501Sanchie if (send_sendso_input_hook != NULL) { 804211501Sanchie error = send_sendso_input_hook(n, ifp, 805211501Sanchie SND_IN, ip6len); 806211501Sanchie if (error == 0) 807211520Sanchie goto freeit; 808211501Sanchie nd6_ra_input(n, off, icmp6len); 809211501Sanchie } else 810211501Sanchie nd6_ra_input(n, off, icmp6len); 81153541Sshin /* m stays. */ 81253541Sshin break; 81353541Sshin 81453541Sshin case ND_NEIGHBOR_SOLICIT: 815192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_neighborsolicit); 81653541Sshin if (code != 0) 81753541Sshin goto badcode; 81853541Sshin if (icmp6len < sizeof(struct nd_neighbor_solicit)) 81953541Sshin goto badlen; 820243882Sglebius if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) { 821211501Sanchie if (send_sendso_input_hook != NULL) { 822211501Sanchie error = send_sendso_input_hook(m, ifp, 823211501Sanchie SND_IN, ip6len); 824211501Sanchie if (error == 0) 825211501Sanchie return (IPPROTO_DONE); 826211501Sanchie nd6_ns_input(m, off, icmp6len); 827211501Sanchie } else 828211501Sanchie nd6_ns_input(m, off, icmp6len); 82962587Sitojun m = NULL; 83062587Sitojun goto freeit; 83162587Sitojun } 832211501Sanchie if (send_sendso_input_hook != NULL) { 833211501Sanchie error = send_sendso_input_hook(n, ifp, 834211501Sanchie SND_IN, ip6len); 835211501Sanchie if (error == 0) 836211520Sanchie goto freeit; 837211501Sanchie nd6_ns_input(n, off, icmp6len); 838211501Sanchie } else 839211501Sanchie nd6_ns_input(n, off, icmp6len); 84053541Sshin /* m stays. */ 84153541Sshin break; 84253541Sshin 84353541Sshin case ND_NEIGHBOR_ADVERT: 844192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_neighboradvert); 84553541Sshin if (code != 0) 84653541Sshin goto badcode; 84753541Sshin if (icmp6len < sizeof(struct nd_neighbor_advert)) 84853541Sshin goto badlen; 849243882Sglebius if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) { 850211501Sanchie 851211501Sanchie /* Send incoming SeND-protected/ND packet to user space. */ 852211501Sanchie if (send_sendso_input_hook != NULL) { 853211501Sanchie error = send_sendso_input_hook(m, ifp, 854211501Sanchie SND_IN, ip6len); 855211501Sanchie if (error == 0) 856211501Sanchie return (IPPROTO_DONE); 857211501Sanchie nd6_na_input(m, off, icmp6len); 858211501Sanchie } else 859211501Sanchie nd6_na_input(m, off, icmp6len); 86062587Sitojun m = NULL; 86162587Sitojun goto freeit; 86262587Sitojun } 863211501Sanchie if (send_sendso_input_hook != NULL) { 864211501Sanchie error = send_sendso_input_hook(n, ifp, 865211501Sanchie SND_IN, ip6len); 866211501Sanchie if (error == 0) 867211520Sanchie goto freeit; 868211501Sanchie nd6_na_input(n, off, icmp6len); 869211501Sanchie } else 870211501Sanchie nd6_na_input(n, off, icmp6len); 87153541Sshin /* m stays. */ 87253541Sshin break; 87353541Sshin 87453541Sshin case ND_REDIRECT: 875192923Sbms icmp6_ifstat_inc(ifp, ifs6_in_redirect); 87653541Sshin if (code != 0) 87753541Sshin goto badcode; 87853541Sshin if (icmp6len < sizeof(struct nd_redirect)) 87953541Sshin goto badlen; 880243882Sglebius if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) { 881211501Sanchie if (send_sendso_input_hook != NULL) { 882211501Sanchie error = send_sendso_input_hook(m, ifp, 883211501Sanchie SND_IN, ip6len); 884211501Sanchie if (error == 0) 885211501Sanchie return (IPPROTO_DONE); 886211501Sanchie icmp6_redirect_input(m, off); 887211501Sanchie } else 888211501Sanchie icmp6_redirect_input(m, off); 88962587Sitojun m = NULL; 89062587Sitojun goto freeit; 89162587Sitojun } 892211501Sanchie if (send_sendso_input_hook != NULL) { 893211501Sanchie error = send_sendso_input_hook(n, ifp, 894211501Sanchie SND_IN, ip6len); 895211501Sanchie if (error == 0) 896211520Sanchie goto freeit; 897211501Sanchie icmp6_redirect_input(n, off); 898211501Sanchie } else 899211501Sanchie icmp6_redirect_input(n, off); 90053541Sshin /* m stays. */ 90153541Sshin break; 90253541Sshin 90353541Sshin case ICMP6_ROUTER_RENUMBERING: 90453541Sshin if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && 90553541Sshin code != ICMP6_ROUTER_RENUMBERING_RESULT) 90653541Sshin goto badcode; 90753541Sshin if (icmp6len < sizeof(struct icmp6_router_renum)) 90853541Sshin goto badlen; 90953541Sshin break; 91053541Sshin 91153541Sshin default: 91278064Sume nd6log((LOG_DEBUG, 91378064Sume "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", 914165118Sbz icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src), 915165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst), 916192923Sbms ifp ? ifp->if_index : 0)); 91753541Sshin if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { 91853541Sshin /* ICMPv6 error: MUST deliver it by spec... */ 91953541Sshin code = PRC_NCMDS; 92053541Sshin /* deliver */ 92153541Sshin } else { 92253541Sshin /* ICMPv6 informational: MUST not deliver */ 92353541Sshin break; 92453541Sshin } 92553541Sshin deliver: 926198076Sbz if (icmp6_notify_error(&m, off, icmp6len, code) != 0) { 92778064Sume /* In this case, m should've been freed. */ 928120856Sume return (IPPROTO_DONE); 92953541Sshin } 93078064Sume break; 93178064Sume 93278064Sume badcode: 933190964Srwatson ICMP6STAT_INC(icp6s_badcode); 93478064Sume break; 93578064Sume 93678064Sume badlen: 937190964Srwatson ICMP6STAT_INC(icp6s_badlen); 93878064Sume break; 93978064Sume } 94078064Sume 94178064Sume /* deliver the packet to appropriate sockets */ 94278064Sume icmp6_rip6_input(&m, *offp); 94378064Sume 94478064Sume return IPPROTO_DONE; 94578064Sume 94678064Sume freeit: 94778064Sume m_freem(m); 94878064Sume return IPPROTO_DONE; 94978064Sume} 95078064Sume 95178064Sumestatic int 952171259Sdelphijicmp6_notify_error(struct mbuf **mp, int off, int icmp6len, int code) 95378064Sume{ 954142681Sume struct mbuf *m = *mp; 95578064Sume struct icmp6_hdr *icmp6; 95678064Sume struct ip6_hdr *eip6; 95778064Sume u_int32_t notifymtu; 95878064Sume struct sockaddr_in6 icmp6src, icmp6dst; 95978064Sume 96078064Sume if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { 961190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 96278064Sume goto freeit; 96378064Sume } 96462587Sitojun#ifndef PULLDOWN_TEST 96578064Sume IP6_EXTHDR_CHECK(m, off, 966120891Sume sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1); 96778064Sume icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 96862587Sitojun#else 96978064Sume IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 970120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 97178064Sume if (icmp6 == NULL) { 972190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 973120856Sume return (-1); 97478064Sume } 97562587Sitojun#endif 97678064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 97753541Sshin 97878064Sume /* Detect the upper level protocol */ 97978064Sume { 980175162Sobrien void (*ctlfunc)(int, struct sockaddr *, void *); 98153541Sshin u_int8_t nxt = eip6->ip6_nxt; 98253541Sshin int eoff = off + sizeof(struct icmp6_hdr) + 983120891Sume sizeof(struct ip6_hdr); 98453541Sshin struct ip6ctlparam ip6cp; 98562587Sitojun struct in6_addr *finaldst = NULL; 98662587Sitojun int icmp6type = icmp6->icmp6_type; 98762587Sitojun struct ip6_frag *fh; 98862587Sitojun struct ip6_rthdr *rth; 98962587Sitojun struct ip6_rthdr0 *rth0; 99062587Sitojun int rthlen; 99153541Sshin 99295023Ssuz while (1) { /* XXX: should avoid infinite loop explicitly? */ 99353541Sshin struct ip6_ext *eh; 99453541Sshin 99578064Sume switch (nxt) { 99653541Sshin case IPPROTO_HOPOPTS: 99753541Sshin case IPPROTO_DSTOPTS: 99853541Sshin case IPPROTO_AH: 99962587Sitojun#ifndef PULLDOWN_TEST 1000120891Sume IP6_EXTHDR_CHECK(m, 0, 1001120891Sume eoff + sizeof(struct ip6_ext), -1); 1002120892Sume eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff); 100362587Sitojun#else 100462587Sitojun IP6_EXTHDR_GET(eh, struct ip6_ext *, m, 1005120891Sume eoff, sizeof(*eh)); 100662587Sitojun if (eh == NULL) { 1007190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 1008120856Sume return (-1); 100962587Sitojun } 101062587Sitojun#endif 1011120891Sume 101253541Sshin if (nxt == IPPROTO_AH) 101353541Sshin eoff += (eh->ip6e_len + 2) << 2; 101453541Sshin else 101553541Sshin eoff += (eh->ip6e_len + 1) << 3; 101653541Sshin nxt = eh->ip6e_nxt; 101753541Sshin break; 101862587Sitojun case IPPROTO_ROUTING: 101962587Sitojun /* 102062587Sitojun * When the erroneous packet contains a 102162587Sitojun * routing header, we should examine the 102262587Sitojun * header to determine the final destination. 102362587Sitojun * Otherwise, we can't properly update 102462587Sitojun * information that depends on the final 102562587Sitojun * destination (e.g. path MTU). 102662587Sitojun */ 102762587Sitojun#ifndef PULLDOWN_TEST 1028120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1); 1029120891Sume rth = (struct ip6_rthdr *) 1030120891Sume (mtod(m, caddr_t) + eoff); 103162587Sitojun#else 103262587Sitojun IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m, 1033120891Sume eoff, sizeof(*rth)); 103462587Sitojun if (rth == NULL) { 1035190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 1036120856Sume return (-1); 103762587Sitojun } 103862587Sitojun#endif 103962587Sitojun rthlen = (rth->ip6r_len + 1) << 3; 104062587Sitojun /* 104162587Sitojun * XXX: currently there is no 104262587Sitojun * officially defined type other 104362587Sitojun * than type-0. 104462587Sitojun * Note that if the segment left field 104562587Sitojun * is 0, all intermediate hops must 104662587Sitojun * have been passed. 104762587Sitojun */ 104862587Sitojun if (rth->ip6r_segleft && 104962587Sitojun rth->ip6r_type == IPV6_RTHDR_TYPE_0) { 105062587Sitojun int hops; 105162587Sitojun 105262587Sitojun#ifndef PULLDOWN_TEST 1053120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1); 1054120891Sume rth0 = (struct ip6_rthdr0 *) 1055120891Sume (mtod(m, caddr_t) + eoff); 105662587Sitojun#else 105762587Sitojun IP6_EXTHDR_GET(rth0, 1058120891Sume struct ip6_rthdr0 *, m, 1059120891Sume eoff, rthlen); 106062587Sitojun if (rth0 == NULL) { 1061190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 1062120856Sume return (-1); 106362587Sitojun } 106462587Sitojun#endif 106562587Sitojun /* just ignore a bogus header */ 106662587Sitojun if ((rth0->ip6r0_len % 2) == 0 && 106762587Sitojun (hops = rth0->ip6r0_len/2)) 106862587Sitojun finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1); 106962587Sitojun } 107062587Sitojun eoff += rthlen; 107162587Sitojun nxt = rth->ip6r_nxt; 107262587Sitojun break; 107362587Sitojun case IPPROTO_FRAGMENT: 107462587Sitojun#ifndef PULLDOWN_TEST 107562587Sitojun IP6_EXTHDR_CHECK(m, 0, eoff + 1076120891Sume sizeof(struct ip6_frag), -1); 1077120891Sume fh = (struct ip6_frag *)(mtod(m, caddr_t) + 1078120891Sume eoff); 107962587Sitojun#else 108062587Sitojun IP6_EXTHDR_GET(fh, struct ip6_frag *, m, 1081120891Sume eoff, sizeof(*fh)); 108262587Sitojun if (fh == NULL) { 1083190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 1084120856Sume return (-1); 108562587Sitojun } 108662587Sitojun#endif 108762587Sitojun /* 108862587Sitojun * Data after a fragment header is meaningless 108962587Sitojun * unless it is the first fragment, but 109062587Sitojun * we'll go to the notify label for path MTU 109162587Sitojun * discovery. 109262587Sitojun */ 109362587Sitojun if (fh->ip6f_offlg & IP6F_OFF_MASK) 109462587Sitojun goto notify; 109562587Sitojun 109662587Sitojun eoff += sizeof(struct ip6_frag); 109762587Sitojun nxt = fh->ip6f_nxt; 109862587Sitojun break; 109953541Sshin default: 110062587Sitojun /* 110162587Sitojun * This case includes ESP and the No Next 110295023Ssuz * Header. In such cases going to the notify 110362587Sitojun * label does not have any meaning 110462587Sitojun * (i.e. ctlfunc will be NULL), but we go 110562587Sitojun * anyway since we might have to update 110662587Sitojun * path MTU information. 110762587Sitojun */ 110853541Sshin goto notify; 110953541Sshin } 111053541Sshin } 111178064Sume notify: 111262587Sitojun#ifndef PULLDOWN_TEST 111353541Sshin icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 111462587Sitojun#else 111562587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 1116120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 111762587Sitojun if (icmp6 == NULL) { 1118190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 1119120856Sume return (-1); 112062587Sitojun } 112162587Sitojun#endif 112278064Sume 1123120891Sume /* 1124120891Sume * retrieve parameters from the inner IPv6 header, and convert 1125120891Sume * them into sockaddr structures. 1126120891Sume * XXX: there is no guarantee that the source or destination 1127120891Sume * addresses of the inner packet are in the same scope as 1128120891Sume * the addresses of the icmp packet. But there is no other 1129120891Sume * way to determine the zone. 1130120891Sume */ 113178064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 1132120891Sume 113378064Sume bzero(&icmp6dst, sizeof(icmp6dst)); 113478064Sume icmp6dst.sin6_len = sizeof(struct sockaddr_in6); 113578064Sume icmp6dst.sin6_family = AF_INET6; 113678064Sume if (finaldst == NULL) 113778064Sume icmp6dst.sin6_addr = eip6->ip6_dst; 113878064Sume else 113978064Sume icmp6dst.sin6_addr = *finaldst; 1140148385Sume if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL)) 1141121315Sume goto freeit; 114278064Sume bzero(&icmp6src, sizeof(icmp6src)); 114378064Sume icmp6src.sin6_len = sizeof(struct sockaddr_in6); 114478064Sume icmp6src.sin6_family = AF_INET6; 114578064Sume icmp6src.sin6_addr = eip6->ip6_src; 1146148385Sume if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL)) 1147121315Sume goto freeit; 1148148385Sume icmp6src.sin6_flowinfo = 1149148385Sume (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); 115078064Sume 115178064Sume if (finaldst == NULL) 115278064Sume finaldst = &eip6->ip6_dst; 115378064Sume ip6cp.ip6c_m = m; 115478064Sume ip6cp.ip6c_icmp6 = icmp6; 115578064Sume ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1); 115678064Sume ip6cp.ip6c_off = eoff; 115778064Sume ip6cp.ip6c_finaldst = finaldst; 115878064Sume ip6cp.ip6c_src = &icmp6src; 115978064Sume ip6cp.ip6c_nxt = nxt; 116078064Sume 116162587Sitojun if (icmp6type == ICMP6_PACKET_TOO_BIG) { 116278064Sume notifymtu = ntohl(icmp6->icmp6_mtu); 116378064Sume ip6cp.ip6c_cmdarg = (void *)¬ifymtu; 116478064Sume icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ 116562587Sitojun } 116662587Sitojun 1167175162Sobrien ctlfunc = (void (*)(int, struct sockaddr *, void *)) 1168120891Sume (inet6sw[ip6_protox[nxt]].pr_ctlinput); 116953541Sshin if (ctlfunc) { 117078064Sume (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, 1171120891Sume &ip6cp); 117253541Sshin } 117353541Sshin } 1174142681Sume *mp = m; 1175120856Sume return (0); 117653541Sshin 117778064Sume freeit: 117853541Sshin m_freem(m); 1179120856Sume return (-1); 118053541Sshin} 118153541Sshin 118278064Sumevoid 1183171259Sdelphijicmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated) 118462587Sitojun{ 118578064Sume struct in6_addr *dst = ip6cp->ip6c_finaldst; 118678064Sume struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; 118778064Sume struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ 118862587Sitojun u_int mtu = ntohl(icmp6->icmp6_mtu); 1189122922Sandre struct in_conninfo inc; 119062587Sitojun 1191121472Sume#if 0 1192121472Sume /* 1193121472Sume * RFC2460 section 5, last paragraph. 1194121472Sume * even though minimum link MTU for IPv6 is IPV6_MMTU, 1195121472Sume * we may see ICMPv6 too big with mtu < IPV6_MMTU 1196121472Sume * due to packet translator in the middle. 1197121472Sume * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for 1198121472Sume * special handling. 1199121472Sume */ 1200121472Sume if (mtu < IPV6_MMTU) 1201121472Sume return; 1202121472Sume#endif 1203121472Sume 1204121472Sume /* 1205121472Sume * we reject ICMPv6 too big with abnormally small value. 1206121472Sume * XXX what is the good definition of "abnormally small"? 1207121472Sume */ 1208121472Sume if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) 1209121472Sume return; 1210121472Sume 121178064Sume if (!validated) 121278064Sume return; 121378064Sume 1214182740Ssimon /* 1215182740Ssimon * In case the suggested mtu is less than IPV6_MMTU, we 1216182740Ssimon * only need to remember that it was for above mentioned 1217182740Ssimon * "alwaysfrag" case. 1218182740Ssimon * Try to be as close to the spec as possible. 1219182740Ssimon */ 1220182740Ssimon if (mtu < IPV6_MMTU) 1221182740Ssimon mtu = IPV6_MMTU - 8; 1222182740Ssimon 1223122922Sandre bzero(&inc, sizeof(inc)); 1224257961Sae inc.inc_fibnum = M_GETFIB(m); 1225186222Sbz inc.inc_flags |= INC_ISIPV6; 1226122922Sandre inc.inc6_faddr = *dst; 1227148385Sume if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL)) 1228148385Sume return; 122962587Sitojun 1230162084Sandre if (mtu < tcp_maxmtu6(&inc, NULL)) { 1231122922Sandre tcp_hc_updatemtu(&inc, mtu); 1232190964Srwatson ICMP6STAT_INC(icp6s_pmtuchg); 123362587Sitojun } 123462587Sitojun} 123562587Sitojun 123653541Sshin/* 123778064Sume * Process a Node Information Query packet, based on 123878064Sume * draft-ietf-ipngwg-icmp-name-lookups-07. 1239120891Sume * 124062587Sitojun * Spec incompatibilities: 124162587Sitojun * - IPv6 Subject address handling 124262587Sitojun * - IPv4 Subject address handling support missing 124362587Sitojun * - Proxy reply (answer even if it's not for me) 124462587Sitojun * - joins NI group address at in6_ifattach() time only, does not cope 124562587Sitojun * with hostname changes by sethostname(3) 124653541Sshin */ 124753541Sshinstatic struct mbuf * 1248171259Sdelphijni6_input(struct mbuf *m, int off) 124953541Sshin{ 125062587Sitojun struct icmp6_nodeinfo *ni6, *nni6; 125153541Sshin struct mbuf *n = NULL; 1252193066Sjamie struct prison *pr; 125362587Sitojun u_int16_t qtype; 125462587Sitojun int subjlen; 125553541Sshin int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 125653541Sshin struct ni_reply_fqdn *fqdn; 125753541Sshin int addrs; /* for NI_QTYPE_NODEADDR */ 125853541Sshin struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ 1259148385Sume struct in6_addr in6_subj; /* subject address */ 126062587Sitojun struct ip6_hdr *ip6; 126162587Sitojun int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ 126278064Sume char *subj = NULL; 126378064Sume struct in6_ifaddr *ia6 = NULL; 126453541Sshin 126562587Sitojun ip6 = mtod(m, struct ip6_hdr *); 126662587Sitojun#ifndef PULLDOWN_TEST 126762587Sitojun ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); 126862587Sitojun#else 126962587Sitojun IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); 127062587Sitojun if (ni6 == NULL) { 127162587Sitojun /* m is already reclaimed */ 1272120891Sume return (NULL); 127362587Sitojun } 127462587Sitojun#endif 127553541Sshin 127662587Sitojun /* 1277169664Sjinmei * Validate IPv6 source address. 1278169664Sjinmei * The default configuration MUST be to refuse answering queries from 1279169664Sjinmei * global-scope addresses according to RFC4602. 1280169664Sjinmei * Notes: 1281169664Sjinmei * - it's not very clear what "refuse" means; this implementation 1282169664Sjinmei * simply drops it. 1283169664Sjinmei * - it's not very easy to identify global-scope (unicast) addresses 1284169664Sjinmei * since there are many prefixes for them. It should be safer 1285169664Sjinmei * and in practice sufficient to check "all" but loopback and 1286169664Sjinmei * link-local (note that site-local unicast was deprecated and 1287169664Sjinmei * ULA is defined as global scope-wise) 1288169664Sjinmei */ 1289181803Sbz if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 && 1290169664Sjinmei !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) && 1291169664Sjinmei !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) 1292169664Sjinmei goto bad; 1293169664Sjinmei 1294169664Sjinmei /* 129562587Sitojun * Validate IPv6 destination address. 129662587Sitojun * 129778064Sume * The Responder must discard the Query without further processing 129878064Sume * unless it is one of the Responder's unicast or anycast addresses, or 129978064Sume * a link-local scope multicast address which the Responder has joined. 1300169664Sjinmei * [RFC4602, Section 5.] 130162587Sitojun */ 1302121630Sume if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1303121630Sume if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) 1304121630Sume goto bad; 1305121630Sume /* else it's a link-local multicast, fine */ 1306121630Sume } else { /* unicast or anycast */ 1307121630Sume if ((ia6 = ip6_getdstifaddr(m)) == NULL) 1308121630Sume goto bad; /* XXX impossible */ 1309121630Sume 1310121630Sume if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) && 1311181803Sbz !(V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) { 1312194760Srwatson ifa_free(&ia6->ia_ifa); 131378064Sume nd6log((LOG_DEBUG, "ni6_input: ignore node info to " 131478064Sume "a temporary address in %s:%d", 131578064Sume __FILE__, __LINE__)); 131678064Sume goto bad; 131778064Sume } 1318194760Srwatson ifa_free(&ia6->ia_ifa); 1319121630Sume } 132062587Sitojun 132178064Sume /* validate query Subject field. */ 132262587Sitojun qtype = ntohs(ni6->ni_qtype); 132362587Sitojun subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); 132462587Sitojun switch (qtype) { 132562587Sitojun case NI_QTYPE_NOOP: 132662587Sitojun case NI_QTYPE_SUPTYPES: 132778064Sume /* 07 draft */ 132878064Sume if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) 132978064Sume break; 133078064Sume /* FALLTHROUGH */ 133162587Sitojun case NI_QTYPE_FQDN: 133262587Sitojun case NI_QTYPE_NODEADDR: 1333151475Ssuz case NI_QTYPE_IPV4ADDR: 133462587Sitojun switch (ni6->ni_code) { 133562587Sitojun case ICMP6_NI_SUBJ_IPV6: 133662587Sitojun#if ICMP6_NI_SUBJ_IPV6 != 0 133762587Sitojun case 0: 133862587Sitojun#endif 133962587Sitojun /* 134062587Sitojun * backward compatibility - try to accept 03 draft 134162587Sitojun * format, where no Subject is present. 134262587Sitojun */ 134378064Sume if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && 134478064Sume subjlen == 0) { 134562587Sitojun oldfqdn++; 134662587Sitojun break; 134762587Sitojun } 134878064Sume#if ICMP6_NI_SUBJ_IPV6 != 0 134978064Sume if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) 135078064Sume goto bad; 135178064Sume#endif 135262587Sitojun 1353121630Sume if (subjlen != sizeof(struct in6_addr)) 135462587Sitojun goto bad; 135562587Sitojun 135662587Sitojun /* 135762587Sitojun * Validate Subject address. 135862587Sitojun * 135978064Sume * Not sure what exactly "address belongs to the node" 136078064Sume * means in the spec, is it just unicast, or what? 136162587Sitojun * 136262587Sitojun * At this moment we consider Subject address as 136362587Sitojun * "belong to the node" if the Subject address equals 136462587Sitojun * to the IPv6 destination address; validation for 136562587Sitojun * IPv6 destination address should have done enough 136662587Sitojun * check for us. 136762587Sitojun * 136862587Sitojun * We do not do proxy at this moment. 136962587Sitojun */ 137062587Sitojun /* m_pulldown instead of copy? */ 137162587Sitojun m_copydata(m, off + sizeof(struct icmp6_nodeinfo), 1372148385Sume subjlen, (caddr_t)&in6_subj); 1373148385Sume if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL)) 1374121315Sume goto bad; 1375121630Sume 1376148385Sume subj = (char *)&in6_subj; 1377148385Sume if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj)) 137862587Sitojun break; 137978064Sume 138062587Sitojun /* 138162587Sitojun * XXX if we are to allow other cases, we should really 138262587Sitojun * be careful about scope here. 138362587Sitojun * basically, we should disallow queries toward IPv6 1384120891Sume * destination X with subject Y, 1385120891Sume * if scope(X) > scope(Y). 138662587Sitojun * if we allow scope(X) > scope(Y), it will result in 138762587Sitojun * information leakage across scope boundary. 138862587Sitojun */ 138962587Sitojun goto bad; 139062587Sitojun 139162587Sitojun case ICMP6_NI_SUBJ_FQDN: 139262587Sitojun /* 139362587Sitojun * Validate Subject name with gethostname(3). 139462587Sitojun * 139562587Sitojun * The behavior may need some debate, since: 139662587Sitojun * - we are not sure if the node has FQDN as 139762587Sitojun * hostname (returned by gethostname(3)). 139862587Sitojun * - the code does wildcard match for truncated names. 139962587Sitojun * however, we are not sure if we want to perform 140062587Sitojun * wildcard match, if gethostname(3) side has 140162587Sitojun * truncated hostname. 140262587Sitojun */ 1403193066Sjamie pr = curthread->td_ucred->cr_prison; 1404193066Sjamie mtx_lock(&pr->pr_mtx); 1405194118Sjamie n = ni6_nametodns(pr->pr_hostname, 1406194118Sjamie strlen(pr->pr_hostname), 0); 1407193066Sjamie mtx_unlock(&pr->pr_mtx); 140862587Sitojun if (!n || n->m_next || n->m_len == 0) 140962587Sitojun goto bad; 141062587Sitojun IP6_EXTHDR_GET(subj, char *, m, 141162587Sitojun off + sizeof(struct icmp6_nodeinfo), subjlen); 141262587Sitojun if (subj == NULL) 141362587Sitojun goto bad; 141462587Sitojun if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 1415120891Sume n->m_len)) { 141662587Sitojun goto bad; 141762587Sitojun } 141862587Sitojun m_freem(n); 141962587Sitojun n = NULL; 142062587Sitojun break; 142162587Sitojun 142278064Sume case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 142362587Sitojun default: 142462587Sitojun goto bad; 142562587Sitojun } 142662587Sitojun break; 142778064Sume } 142862587Sitojun 142978064Sume /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 143078064Sume switch (qtype) { 143178064Sume case NI_QTYPE_FQDN: 1432181803Sbz if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0) 143378064Sume goto bad; 143478064Sume break; 143578064Sume case NI_QTYPE_NODEADDR: 1436151475Ssuz case NI_QTYPE_IPV4ADDR: 1437181803Sbz if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0) 143878064Sume goto bad; 143978064Sume break; 144078064Sume } 144178064Sume 144278064Sume /* guess reply length */ 144378064Sume switch (qtype) { 144478064Sume case NI_QTYPE_NOOP: 144578064Sume break; /* no reply data */ 144678064Sume case NI_QTYPE_SUPTYPES: 144778064Sume replylen += sizeof(u_int32_t); 144878064Sume break; 144978064Sume case NI_QTYPE_FQDN: 145078064Sume /* XXX will append an mbuf */ 145178064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 145278064Sume break; 145378064Sume case NI_QTYPE_NODEADDR: 1454148892Sume addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj); 145578064Sume if ((replylen += addrs * (sizeof(struct in6_addr) + 1456120891Sume sizeof(u_int32_t))) > MCLBYTES) 145778064Sume replylen = MCLBYTES; /* XXX: will truncate pkt later */ 145878064Sume break; 1459151475Ssuz case NI_QTYPE_IPV4ADDR: 1460151475Ssuz /* unsupported - should respond with unknown Qtype? */ 1461151475Ssuz break; 146262587Sitojun default: 146378064Sume /* 146478064Sume * XXX: We must return a reply with the ICMP6 code 1465120891Sume * `unknown Qtype' in this case. However we regard the case 146678064Sume * as an FQDN query for backward compatibility. 146778064Sume * Older versions set a random value to this field, 146878064Sume * so it rarely varies in the defined qtypes. 146978064Sume * But the mechanism is not reliable... 147078064Sume * maybe we should obsolete older versions. 147178064Sume */ 147278064Sume qtype = NI_QTYPE_FQDN; 147378064Sume /* XXX will append an mbuf */ 147478064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 147578064Sume oldfqdn++; 147678064Sume break; 147762587Sitojun } 147862587Sitojun 1479248328Sglebius /* Allocate an mbuf to reply. */ 1480248328Sglebius if (replylen > MCLBYTES) { 1481248328Sglebius /* 1482248328Sglebius * XXX: should we try to allocate more? But MCLBYTES 1483248328Sglebius * is probably much larger than IPV6_MMTU... 1484248328Sglebius */ 1485248328Sglebius goto bad; 1486248328Sglebius } 1487248328Sglebius if (replylen > MHLEN) 1488248328Sglebius n = m_getcl(M_NOWAIT, m->m_type, M_PKTHDR); 1489248328Sglebius else 1490248328Sglebius n = m_gethdr(M_NOWAIT, m->m_type); 149162587Sitojun if (n == NULL) { 149262587Sitojun m_freem(m); 1493120856Sume return (NULL); 149462587Sitojun } 1495248328Sglebius m_move_pkthdr(n, m); /* just for recvif and FIB */ 149653541Sshin n->m_pkthdr.len = n->m_len = replylen; 149753541Sshin 149853541Sshin /* copy mbuf header and IPv6 + Node Information base headers */ 149953541Sshin bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); 150053541Sshin nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 150162587Sitojun bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); 150253541Sshin 150353541Sshin /* qtype dependent procedure */ 150453541Sshin switch (qtype) { 150562587Sitojun case NI_QTYPE_NOOP: 150678064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 150762587Sitojun nni6->ni_flags = 0; 150862587Sitojun break; 150962587Sitojun case NI_QTYPE_SUPTYPES: 151078064Sume { 151178064Sume u_int32_t v; 151278064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 151378064Sume nni6->ni_flags = htons(0x0000); /* raw bitmap */ 151478064Sume /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 151578064Sume v = (u_int32_t)htonl(0x0000000f); 151678064Sume bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 151762587Sitojun break; 151878064Sume } 151962587Sitojun case NI_QTYPE_FQDN: 152078064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 152162587Sitojun fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + 1522120891Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo)); 152362587Sitojun nni6->ni_flags = 0; /* XXX: meaningless TTL */ 152462587Sitojun fqdn->ni_fqdn_ttl = 0; /* ditto. */ 152562587Sitojun /* 1526193066Sjamie * XXX do we really have FQDN in hostname? 152762587Sitojun */ 1528193066Sjamie pr = curthread->td_ucred->cr_prison; 1529193066Sjamie mtx_lock(&pr->pr_mtx); 1530194118Sjamie n->m_next = ni6_nametodns(pr->pr_hostname, 1531194118Sjamie strlen(pr->pr_hostname), oldfqdn); 1532193066Sjamie mtx_unlock(&pr->pr_mtx); 153362587Sitojun if (n->m_next == NULL) 153462587Sitojun goto bad; 153562587Sitojun /* XXX we assume that n->m_next is not a chain */ 153662587Sitojun if (n->m_next->m_next != NULL) 153762587Sitojun goto bad; 153862587Sitojun n->m_pkthdr.len += n->m_next->m_len; 153962587Sitojun break; 154062587Sitojun case NI_QTYPE_NODEADDR: 154162587Sitojun { 154262587Sitojun int lenlim, copied; 154353541Sshin 154478064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 154578064Sume n->m_pkthdr.len = n->m_len = 154678064Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 154778064Sume lenlim = M_TRAILINGSPACE(n); 154862587Sitojun copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 154962587Sitojun /* XXX: reset mbuf length */ 155062587Sitojun n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 1551120891Sume sizeof(struct icmp6_nodeinfo) + copied; 155262587Sitojun break; 155353541Sshin } 155462587Sitojun default: 155562587Sitojun break; /* XXX impossible! */ 155662587Sitojun } 155753541Sshin 155853541Sshin nni6->ni_type = ICMP6_NI_REPLY; 155962587Sitojun m_freem(m); 1560120856Sume return (n); 156153541Sshin 156253541Sshin bad: 156362587Sitojun m_freem(m); 156453541Sshin if (n) 156553541Sshin m_freem(n); 1566120856Sume return (NULL); 156753541Sshin} 156853541Sshin 156953541Sshin/* 157062587Sitojun * make a mbuf with DNS-encoded string. no compression support. 157162587Sitojun * 157262587Sitojun * XXX names with less than 2 dots (like "foo" or "foo.section") will be 157362587Sitojun * treated as truncated name (two \0 at the end). this is a wild guess. 1574171259Sdelphij * 1575171259Sdelphij * old - return pascal string if non-zero 157662587Sitojun */ 157762587Sitojunstatic struct mbuf * 1578171259Sdelphijni6_nametodns(const char *name, int namelen, int old) 157962587Sitojun{ 158062587Sitojun struct mbuf *m; 158162587Sitojun char *cp, *ep; 158262587Sitojun const char *p, *q; 158362587Sitojun int i, len, nterm; 158462587Sitojun 158562587Sitojun if (old) 158662587Sitojun len = namelen + 1; 158762587Sitojun else 158862587Sitojun len = MCLBYTES; 158962587Sitojun 1590248321Sglebius /* Because MAXHOSTNAMELEN is usually 256, we use cluster mbuf. */ 1591248321Sglebius if (len > MLEN) 1592248321Sglebius m = m_getcl(M_NOWAIT, MT_DATA, 0); 1593248321Sglebius else 1594248321Sglebius m = m_get(M_NOWAIT, MT_DATA); 1595248321Sglebius if (m == NULL) 159662587Sitojun goto fail; 159762587Sitojun 159862587Sitojun if (old) { 159962587Sitojun m->m_len = len; 160062587Sitojun *mtod(m, char *) = namelen; 160162587Sitojun bcopy(name, mtod(m, char *) + 1, namelen); 160262587Sitojun return m; 160362587Sitojun } else { 160462587Sitojun m->m_len = 0; 160562587Sitojun cp = mtod(m, char *); 160662587Sitojun ep = mtod(m, char *) + M_TRAILINGSPACE(m); 160762587Sitojun 160862587Sitojun /* if not certain about my name, return empty buffer */ 160962587Sitojun if (namelen == 0) 161062587Sitojun return m; 161162587Sitojun 161262587Sitojun /* 161362587Sitojun * guess if it looks like shortened hostname, or FQDN. 161462587Sitojun * shortened hostname needs two trailing "\0". 161562587Sitojun */ 161662587Sitojun i = 0; 161762587Sitojun for (p = name; p < name + namelen; p++) { 161862587Sitojun if (*p && *p == '.') 161962587Sitojun i++; 162062587Sitojun } 162162587Sitojun if (i < 2) 162262587Sitojun nterm = 2; 162362587Sitojun else 162462587Sitojun nterm = 1; 162562587Sitojun 162662587Sitojun p = name; 162762587Sitojun while (cp < ep && p < name + namelen) { 162862587Sitojun i = 0; 162962587Sitojun for (q = p; q < name + namelen && *q && *q != '.'; q++) 163062587Sitojun i++; 163162587Sitojun /* result does not fit into mbuf */ 163262587Sitojun if (cp + i + 1 >= ep) 163362587Sitojun goto fail; 163478704Sume /* 163578704Sume * DNS label length restriction, RFC1035 page 8. 163678704Sume * "i == 0" case is included here to avoid returning 163778704Sume * 0-length label on "foo..bar". 163878704Sume */ 163978704Sume if (i <= 0 || i >= 64) 164062587Sitojun goto fail; 164162587Sitojun *cp++ = i; 164262587Sitojun bcopy(p, cp, i); 164362587Sitojun cp += i; 164462587Sitojun p = q; 164562587Sitojun if (p < name + namelen && *p == '.') 164662587Sitojun p++; 164762587Sitojun } 164862587Sitojun /* termination */ 164962587Sitojun if (cp + nterm >= ep) 165062587Sitojun goto fail; 165162587Sitojun while (nterm-- > 0) 165262587Sitojun *cp++ = '\0'; 165362587Sitojun m->m_len = cp - mtod(m, char *); 165462587Sitojun return m; 165562587Sitojun } 165662587Sitojun 165762587Sitojun panic("should not reach here"); 165895023Ssuz /* NOTREACHED */ 165962587Sitojun 166062587Sitojun fail: 166162587Sitojun if (m) 166262587Sitojun m_freem(m); 166362587Sitojun return NULL; 166462587Sitojun} 166562587Sitojun 166662587Sitojun/* 166762587Sitojun * check if two DNS-encoded string matches. takes care of truncated 166862587Sitojun * form (with \0\0 at the end). no compression support. 166978064Sume * XXX upper/lowercase match (see RFC2065) 167062587Sitojun */ 167162587Sitojunstatic int 1672171259Sdelphijni6_dnsmatch(const char *a, int alen, const char *b, int blen) 167362587Sitojun{ 167462587Sitojun const char *a0, *b0; 167562587Sitojun int l; 167662587Sitojun 167762587Sitojun /* simplest case - need validation? */ 167862587Sitojun if (alen == blen && bcmp(a, b, alen) == 0) 167962587Sitojun return 1; 168062587Sitojun 168162587Sitojun a0 = a; 168262587Sitojun b0 = b; 168362587Sitojun 168462587Sitojun /* termination is mandatory */ 168562587Sitojun if (alen < 2 || blen < 2) 168662587Sitojun return 0; 168762587Sitojun if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 168862587Sitojun return 0; 168962587Sitojun alen--; 169062587Sitojun blen--; 169162587Sitojun 169262587Sitojun while (a - a0 < alen && b - b0 < blen) { 169362587Sitojun if (a - a0 + 1 > alen || b - b0 + 1 > blen) 169462587Sitojun return 0; 169562587Sitojun 169662587Sitojun if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 169762587Sitojun return 0; 169862587Sitojun /* we don't support compression yet */ 169962587Sitojun if (a[0] >= 64 || b[0] >= 64) 170062587Sitojun return 0; 170162587Sitojun 170262587Sitojun /* truncated case */ 170362587Sitojun if (a[0] == 0 && a - a0 == alen - 1) 170462587Sitojun return 1; 170562587Sitojun if (b[0] == 0 && b - b0 == blen - 1) 170662587Sitojun return 1; 170762587Sitojun if (a[0] == 0 || b[0] == 0) 170862587Sitojun return 0; 170962587Sitojun 171062587Sitojun if (a[0] != b[0]) 171162587Sitojun return 0; 171262587Sitojun l = a[0]; 171362587Sitojun if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 171462587Sitojun return 0; 171562587Sitojun if (bcmp(a + 1, b + 1, l) != 0) 171662587Sitojun return 0; 171762587Sitojun 171862587Sitojun a += 1 + l; 171962587Sitojun b += 1 + l; 172062587Sitojun } 172162587Sitojun 172262587Sitojun if (a - a0 == alen && b - b0 == blen) 172362587Sitojun return 1; 172462587Sitojun else 172562587Sitojun return 0; 172662587Sitojun} 172762587Sitojun 172862587Sitojun/* 172953541Sshin * calculate the number of addresses to be returned in the node info reply. 173053541Sshin */ 173153541Sshinstatic int 1732171259Sdelphijni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp, 1733171259Sdelphij struct in6_addr *subj) 173453541Sshin{ 173578064Sume struct ifnet *ifp; 173678064Sume struct in6_ifaddr *ifa6; 173778064Sume struct ifaddr *ifa; 173853541Sshin int addrs = 0, addrsofif, iffound = 0; 173978064Sume int niflags = ni6->ni_flags; 174053541Sshin 174178064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 174278064Sume switch (ni6->ni_code) { 174378064Sume case ICMP6_NI_SUBJ_IPV6: 174478064Sume if (subj == NULL) /* must be impossible... */ 1745120856Sume return (0); 174678064Sume break; 174778064Sume default: 174878064Sume /* 174978064Sume * XXX: we only support IPv6 subject address for 175078064Sume * this Qtype. 175178064Sume */ 1752120856Sume return (0); 175378064Sume } 175478064Sume } 175578064Sume 1756196481Srwatson IFNET_RLOCK_NOSLEEP(); 1757228966Sjhb TAILQ_FOREACH(ifp, &V_ifnet, if_list) { 175853541Sshin addrsofif = 0; 1759229621Sjhb IF_ADDR_RLOCK(ifp); 1760191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 176153541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 176253541Sshin continue; 176353541Sshin ifa6 = (struct in6_ifaddr *)ifa; 176453541Sshin 176578064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 1766148892Sume IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr)) 176753541Sshin iffound = 1; 176853541Sshin 176962587Sitojun /* 177062587Sitojun * IPv4-mapped addresses can only be returned by a 177162587Sitojun * Node Information proxy, since they represent 177262587Sitojun * addresses of IPv4-only nodes, which perforce do 177362587Sitojun * not implement this protocol. 177478064Sume * [icmp-name-lookups-07, Section 5.4] 177562587Sitojun * So we don't support NI_NODEADDR_FLAG_COMPAT in 177662587Sitojun * this function at this moment. 177762587Sitojun */ 177862587Sitojun 177953541Sshin /* What do we have to do about ::1? */ 178078064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 178178064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 178278064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 178378064Sume continue; 178453541Sshin break; 178578064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 178678064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 178778064Sume continue; 178853541Sshin break; 178978064Sume case IPV6_ADDR_SCOPE_GLOBAL: 179078064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 179178064Sume continue; 179278064Sume break; 179378064Sume default: 179478064Sume continue; 179553541Sshin } 179678064Sume 179778064Sume /* 179878064Sume * check if anycast is okay. 179995023Ssuz * XXX: just experimental. not in the spec. 180078064Sume */ 180178064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 180278064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 180378064Sume continue; /* we need only unicast addresses */ 180478064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1805181803Sbz (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { 180678064Sume continue; 180778064Sume } 180878064Sume addrsofif++; /* count the address */ 180953541Sshin } 1810229621Sjhb IF_ADDR_RUNLOCK(ifp); 181153541Sshin if (iffound) { 181253541Sshin *ifpp = ifp; 1813196481Srwatson IFNET_RUNLOCK_NOSLEEP(); 1814120856Sume return (addrsofif); 181553541Sshin } 181653541Sshin 181753541Sshin addrs += addrsofif; 181853541Sshin } 1819196481Srwatson IFNET_RUNLOCK_NOSLEEP(); 182053541Sshin 1821120856Sume return (addrs); 182253541Sshin} 182353541Sshin 182453541Sshinstatic int 1825171259Sdelphijni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6, 1826171259Sdelphij struct ifnet *ifp0, int resid) 182753541Sshin{ 1828228866Sjhb struct ifnet *ifp; 182978064Sume struct in6_ifaddr *ifa6; 183078064Sume struct ifaddr *ifa; 183178064Sume struct ifnet *ifp_dep = NULL; 183278064Sume int copied = 0, allow_deprecated = 0; 183353541Sshin u_char *cp = (u_char *)(nni6 + 1); 183478064Sume int niflags = ni6->ni_flags; 183578064Sume u_int32_t ltime; 183653541Sshin 183778064Sume if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 1838120856Sume return (0); /* needless to copy */ 1839120891Sume 1840196481Srwatson IFNET_RLOCK_NOSLEEP(); 1841228866Sjhb ifp = ifp0 ? ifp0 : TAILQ_FIRST(&V_ifnet); 184278064Sume again: 1843120892Sume 1844120891Sume for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 1845229621Sjhb IF_ADDR_RLOCK(ifp); 1846191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 184753541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 184853541Sshin continue; 184953541Sshin ifa6 = (struct in6_ifaddr *)ifa; 185053541Sshin 185178064Sume if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && 185278064Sume allow_deprecated == 0) { 185378064Sume /* 185478064Sume * prefererred address should be put before 185578064Sume * deprecated addresses. 185678064Sume */ 185778064Sume 185878064Sume /* record the interface for later search */ 185978064Sume if (ifp_dep == NULL) 186078064Sume ifp_dep = ifp; 186178064Sume 186278064Sume continue; 1863120891Sume } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && 1864120891Sume allow_deprecated != 0) 186578064Sume continue; /* we now collect deprecated addrs */ 186653541Sshin 186753541Sshin /* What do we have to do about ::1? */ 186878064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 186978064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 187078064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 187178064Sume continue; 187253541Sshin break; 187378064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 187478064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 187578064Sume continue; 187653541Sshin break; 187778064Sume case IPV6_ADDR_SCOPE_GLOBAL: 187878064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 187978064Sume continue; 188078064Sume break; 188178064Sume default: 188278064Sume continue; 188353541Sshin } 188453541Sshin 188578064Sume /* 188678064Sume * check if anycast is okay. 1887120891Sume * XXX: just experimental. not in the spec. 188878064Sume */ 188978064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 189078064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 189178064Sume continue; 189278064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1893181803Sbz (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { 189478064Sume continue; 189553541Sshin } 189678064Sume 189778064Sume /* now we can copy the address */ 189878064Sume if (resid < sizeof(struct in6_addr) + 189978064Sume sizeof(u_int32_t)) { 1900229621Sjhb IF_ADDR_RUNLOCK(ifp); 190178064Sume /* 190278064Sume * We give up much more copy. 190378064Sume * Set the truncate flag and return. 190478064Sume */ 1905120891Sume nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE; 1906196481Srwatson IFNET_RUNLOCK_NOSLEEP(); 1907120856Sume return (copied); 190878064Sume } 190978064Sume 191078064Sume /* 191178064Sume * Set the TTL of the address. 191278064Sume * The TTL value should be one of the following 191378064Sume * according to the specification: 191478064Sume * 191578064Sume * 1. The remaining lifetime of a DHCP lease on the 191678064Sume * address, or 191778064Sume * 2. The remaining Valid Lifetime of a prefix from 191878064Sume * which the address was derived through Stateless 191978064Sume * Autoconfiguration. 192078064Sume * 192178064Sume * Note that we currently do not support stateful 192278064Sume * address configuration by DHCPv6, so the former 192378064Sume * case can't happen. 192478064Sume */ 192578064Sume if (ifa6->ia6_lifetime.ia6t_expire == 0) 192678064Sume ltime = ND6_INFINITE_LIFETIME; 192778064Sume else { 192878064Sume if (ifa6->ia6_lifetime.ia6t_expire > 1929253970Shrs time_uptime) 1930253970Shrs ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_uptime); 193178064Sume else 193278064Sume ltime = 0; 193378064Sume } 1934120891Sume 193578064Sume bcopy(<ime, cp, sizeof(u_int32_t)); 193678064Sume cp += sizeof(u_int32_t); 193778064Sume 193878064Sume /* copy the address itself */ 193978064Sume bcopy(&ifa6->ia_addr.sin6_addr, cp, 1940120891Sume sizeof(struct in6_addr)); 1941121315Sume in6_clearscope((struct in6_addr *)cp); /* XXX */ 194278064Sume cp += sizeof(struct in6_addr); 1943120891Sume 194478064Sume resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1945120891Sume copied += (sizeof(struct in6_addr) + sizeof(u_int32_t)); 194653541Sshin } 1947229621Sjhb IF_ADDR_RUNLOCK(ifp); 194853541Sshin if (ifp0) /* we need search only on the specified IF */ 194953541Sshin break; 195053541Sshin } 195153541Sshin 195278064Sume if (allow_deprecated == 0 && ifp_dep != NULL) { 195378064Sume ifp = ifp_dep; 195478064Sume allow_deprecated = 1; 195578064Sume 195678064Sume goto again; 195778064Sume } 195878064Sume 1959196481Srwatson IFNET_RUNLOCK_NOSLEEP(); 1960108172Shsu 1961120856Sume return (copied); 196253541Sshin} 196353541Sshin 196453541Sshin/* 196553541Sshin * XXX almost dup'ed code with rip6_input. 196653541Sshin */ 196753541Sshinstatic int 1968171259Sdelphijicmp6_rip6_input(struct mbuf **mp, int off) 196953541Sshin{ 197053541Sshin struct mbuf *m = *mp; 197178064Sume struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1972186141Sbz struct inpcb *in6p; 1973186141Sbz struct inpcb *last = NULL; 1974121315Sume struct sockaddr_in6 fromsa; 197553541Sshin struct icmp6_hdr *icmp6; 197653541Sshin struct mbuf *opts = NULL; 197753541Sshin 197862587Sitojun#ifndef PULLDOWN_TEST 197953541Sshin /* this is assumed to be safe. */ 198053541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 198162587Sitojun#else 198262587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 198362587Sitojun if (icmp6 == NULL) { 198462587Sitojun /* m is already reclaimed */ 1985120891Sume return (IPPROTO_DONE); 198662587Sitojun } 198762587Sitojun#endif 198853541Sshin 1989148385Sume /* 1990148385Sume * XXX: the address may have embedded scope zone ID, which should be 1991148385Sume * hidden from applications. 1992148385Sume */ 1993121315Sume bzero(&fromsa, sizeof(fromsa)); 1994148385Sume fromsa.sin6_family = AF_INET6; 1995121315Sume fromsa.sin6_len = sizeof(struct sockaddr_in6); 1996148385Sume fromsa.sin6_addr = ip6->ip6_src; 1997148385Sume if (sa6_recoverscope(&fromsa)) { 1998148385Sume m_freem(m); 1999148385Sume return (IPPROTO_DONE); 2000148385Sume } 200153541Sshin 2002181803Sbz INP_INFO_RLOCK(&V_ripcbinfo); 2003181803Sbz LIST_FOREACH(in6p, &V_ripcb, inp_list) { 2004180850Smav if ((in6p->inp_vflag & INP_IPV6) == 0) 200553541Sshin continue; 2006186141Sbz if (in6p->inp_ip_p != IPPROTO_ICMPV6) 2007180850Smav continue; 200853541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 200953541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 2010180850Smav continue; 201153541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 201253541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 2013180850Smav continue; 2014180932Smav INP_RLOCK(in6p); 2015180965Srwatson if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, 2016180965Srwatson in6p->in6p_icmp6filt)) { 2017180932Smav INP_RUNLOCK(in6p); 2018180850Smav continue; 2019180932Smav } 2020186163Skmacy if (last != NULL) { 2021121809Sume struct mbuf *n = NULL; 2022121809Sume 2023121809Sume /* 2024121809Sume * Recent network drivers tend to allocate a single 2025121809Sume * mbuf cluster, rather than to make a couple of 2026121809Sume * mbufs without clusters. Also, since the IPv6 code 2027121809Sume * path tries to avoid m_pullup(), it is highly 2028121809Sume * probable that we still have an mbuf cluster here 2029121809Sume * even though the necessary length can be stored in an 2030121809Sume * mbuf's internal buffer. 2031121809Sume * Meanwhile, the default size of the receive socket 2032121809Sume * buffer for raw sockets is not so large. This means 2033121809Sume * the possibility of packet loss is relatively higher 2034121809Sume * than before. To avoid this scenario, we copy the 2035121809Sume * received data to a separate mbuf that does not use 2036121809Sume * a cluster, if possible. 2037121809Sume * XXX: it is better to copy the data after stripping 2038121809Sume * intermediate headers. 2039121809Sume */ 2040121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 2041121809Sume m->m_len <= MHLEN) { 2042248321Sglebius n = m_get(M_NOWAIT, m->m_type); 2043121809Sume if (n != NULL) { 2044145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 2045171260Sdelphij bcopy(m->m_data, n->m_data, 2046145065Sgnn m->m_len); 2047145065Sgnn n->m_len = m->m_len; 2048145065Sgnn } else { 2049145065Sgnn m_free(n); 2050145065Sgnn n = NULL; 2051145065Sgnn } 2052121809Sume } 2053121809Sume } 2054121809Sume if (n != NULL || 2055121809Sume (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 2056186223Sbz if (last->inp_flags & INP_CONTROLOPTS) 2057121674Sume ip6_savecontrol(last, n, &opts); 205853541Sshin /* strip intermediate headers */ 205953541Sshin m_adj(n, off); 2060186141Sbz SOCKBUF_LOCK(&last->inp_socket->so_rcv); 2061160591Srwatson if (sbappendaddr_locked( 2062186141Sbz &last->inp_socket->so_rcv, 2063121315Sume (struct sockaddr *)&fromsa, n, opts) 2064120891Sume == 0) { 206553541Sshin /* should notify about lost packet */ 206653541Sshin m_freem(n); 206778064Sume if (opts) { 206853541Sshin m_freem(opts); 206978064Sume } 2070160591Srwatson SOCKBUF_UNLOCK( 2071186141Sbz &last->inp_socket->so_rcv); 207297658Stanimura } else 2073186141Sbz sorwakeup_locked(last->inp_socket); 207453541Sshin opts = NULL; 207553541Sshin } 2076178378Srwatson INP_RUNLOCK(last); 207753541Sshin } 207853541Sshin last = in6p; 207953541Sshin } 2080181803Sbz INP_INFO_RUNLOCK(&V_ripcbinfo); 2081186163Skmacy if (last != NULL) { 2082186223Sbz if (last->inp_flags & INP_CONTROLOPTS) 2083121674Sume ip6_savecontrol(last, m, &opts); 208453541Sshin /* strip intermediate headers */ 208553541Sshin m_adj(m, off); 2086121809Sume 2087121809Sume /* avoid using mbuf clusters if possible (see above) */ 2088121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 2089121809Sume m->m_len <= MHLEN) { 2090121809Sume struct mbuf *n; 2091121809Sume 2092248321Sglebius n = m_get(M_NOWAIT, m->m_type); 2093121809Sume if (n != NULL) { 2094145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 2095145065Sgnn bcopy(m->m_data, n->m_data, m->m_len); 2096145065Sgnn n->m_len = m->m_len; 2097171260Sdelphij 2098145065Sgnn m_freem(m); 2099145065Sgnn m = n; 2100145065Sgnn } else { 2101145065Sgnn m_freem(n); 2102145065Sgnn n = NULL; 2103145065Sgnn } 2104121809Sume } 2105121809Sume } 2106186141Sbz SOCKBUF_LOCK(&last->inp_socket->so_rcv); 2107186141Sbz if (sbappendaddr_locked(&last->inp_socket->so_rcv, 2108121315Sume (struct sockaddr *)&fromsa, m, opts) == 0) { 210953541Sshin m_freem(m); 211053541Sshin if (opts) 211153541Sshin m_freem(opts); 2112186141Sbz SOCKBUF_UNLOCK(&last->inp_socket->so_rcv); 211397658Stanimura } else 2114186141Sbz sorwakeup_locked(last->inp_socket); 2115178378Srwatson INP_RUNLOCK(last); 211653541Sshin } else { 211753541Sshin m_freem(m); 2118191672Sbms IP6STAT_DEC(ip6s_delivered); 211953541Sshin } 212053541Sshin return IPPROTO_DONE; 212153541Sshin} 212253541Sshin 212353541Sshin/* 212453541Sshin * Reflect the ip6 packet back to the source. 212562587Sitojun * OFF points to the icmp6 header, counted from the top of the mbuf. 212653541Sshin */ 212753541Sshinvoid 2128171259Sdelphijicmp6_reflect(struct mbuf *m, size_t off) 212953541Sshin{ 213062587Sitojun struct ip6_hdr *ip6; 213153541Sshin struct icmp6_hdr *icmp6; 2132194760Srwatson struct in6_ifaddr *ia = NULL; 213362587Sitojun int plen; 213453541Sshin int type, code; 213553541Sshin struct ifnet *outif = NULL; 2136194777Sbz struct in6_addr origdst, src, *srcp = NULL; 213753541Sshin 213862587Sitojun /* too short to reflect */ 213962587Sitojun if (off < sizeof(struct ip6_hdr)) { 214078064Sume nd6log((LOG_DEBUG, 214178064Sume "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 214278064Sume (u_long)off, (u_long)sizeof(struct ip6_hdr), 214378064Sume __FILE__, __LINE__)); 214462587Sitojun goto bad; 214562587Sitojun } 214662587Sitojun 214753541Sshin /* 214853541Sshin * If there are extra headers between IPv6 and ICMPv6, strip 214953541Sshin * off that header first. 215053541Sshin */ 215162587Sitojun#ifdef DIAGNOSTIC 215262587Sitojun if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) 215362587Sitojun panic("assumption failed in icmp6_reflect"); 215462587Sitojun#endif 215562587Sitojun if (off > sizeof(struct ip6_hdr)) { 215662587Sitojun size_t l; 215762587Sitojun struct ip6_hdr nip6; 215853541Sshin 215962587Sitojun l = off - sizeof(struct ip6_hdr); 216062587Sitojun m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); 216162587Sitojun m_adj(m, l); 216262587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 216362587Sitojun if (m->m_len < l) { 216462587Sitojun if ((m = m_pullup(m, l)) == NULL) 216562587Sitojun return; 216653541Sshin } 216762587Sitojun bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); 216862587Sitojun } else /* off == sizeof(struct ip6_hdr) */ { 216962587Sitojun size_t l; 217062587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 217162587Sitojun if (m->m_len < l) { 217262587Sitojun if ((m = m_pullup(m, l)) == NULL) 217362587Sitojun return; 217453541Sshin } 217553541Sshin } 217662587Sitojun plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 217762587Sitojun ip6 = mtod(m, struct ip6_hdr *); 217862587Sitojun ip6->ip6_nxt = IPPROTO_ICMPV6; 217953541Sshin icmp6 = (struct icmp6_hdr *)(ip6 + 1); 218053541Sshin type = icmp6->icmp6_type; /* keep type for statistics */ 218153541Sshin code = icmp6->icmp6_code; /* ditto. */ 218253541Sshin 2183148385Sume origdst = ip6->ip6_dst; 218453541Sshin /* 218553541Sshin * ip6_input() drops a packet if its src is multicast. 218653541Sshin * So, the src is never multicast. 218753541Sshin */ 218853541Sshin ip6->ip6_dst = ip6->ip6_src; 218953541Sshin 219053541Sshin /* 2191120891Sume * If the incoming packet was addressed directly to us (i.e. unicast), 219253541Sshin * use dst as the src for the reply. 2193120891Sume * The IN6_IFF_NOTREADY case should be VERY rare, but is possible 219462587Sitojun * (for example) when we encounter an error while forwarding procedure 219562587Sitojun * destined to a duplicated address of ours. 2196148385Sume * Note that ip6_getdstifaddr() may fail if we are in an error handling 2197148385Sume * procedure of an outgoing packet of our own, in which case we need 2198148385Sume * to search in the ifaddr list. 219953541Sshin */ 2200148385Sume if (!IN6_IS_ADDR_MULTICAST(&origdst)) { 2201148385Sume if ((ia = ip6_getdstifaddr(m))) { 2202148385Sume if (!(ia->ia6_flags & 2203148385Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) 2204194777Sbz srcp = &ia->ia_addr.sin6_addr; 2205148385Sume } else { 2206148385Sume struct sockaddr_in6 d; 2207148385Sume 2208148385Sume bzero(&d, sizeof(d)); 2209148385Sume d.sin6_family = AF_INET6; 2210148385Sume d.sin6_len = sizeof(d); 2211148385Sume d.sin6_addr = origdst; 2212148385Sume ia = (struct in6_ifaddr *) 2213148385Sume ifa_ifwithaddr((struct sockaddr *)&d); 2214148385Sume if (ia && 2215148385Sume !(ia->ia6_flags & 2216148385Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) { 2217194777Sbz srcp = &ia->ia_addr.sin6_addr; 2218148385Sume } 221953541Sshin } 222053541Sshin } 222153541Sshin 2222194777Sbz if (srcp == NULL) { 222378064Sume int e; 2224148385Sume struct sockaddr_in6 sin6; 222578064Sume struct route_in6 ro; 222678064Sume 222753541Sshin /* 222862587Sitojun * This case matches to multicasts, our anycast, or unicasts 222995023Ssuz * that we do not own. Select a source address based on the 223078064Sume * source address of the erroneous packet. 223153541Sshin */ 2232148385Sume bzero(&sin6, sizeof(sin6)); 2233148385Sume sin6.sin6_family = AF_INET6; 2234148385Sume sin6.sin6_len = sizeof(sin6); 2235148385Sume sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */ 2236148385Sume 223778064Sume bzero(&ro, sizeof(ro)); 2238194777Sbz e = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &src); 223978064Sume if (ro.ro_rt) 224078064Sume RTFREE(ro.ro_rt); /* XXX: we could use this */ 2241194777Sbz if (e) { 2242165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 224378064Sume nd6log((LOG_DEBUG, 224478064Sume "icmp6_reflect: source can't be determined: " 224578064Sume "dst=%s, error=%d\n", 2246165118Sbz ip6_sprintf(ip6buf, &sin6.sin6_addr), e)); 224778064Sume goto bad; 224878064Sume } 2249194777Sbz srcp = &src; 225078064Sume } 225153541Sshin 2252194777Sbz ip6->ip6_src = *srcp; 225353541Sshin ip6->ip6_flow = 0; 225462587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 225562587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 225653541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 2257121472Sume if (outif) 2258121472Sume ip6->ip6_hlim = ND_IFINFO(outif)->chlim; 2259121472Sume else if (m->m_pkthdr.rcvif) { 226053541Sshin /* XXX: This may not be the outgoing interface */ 2261121161Sume ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim; 226278064Sume } else 2263181803Sbz ip6->ip6_hlim = V_ip6_defhlim; 226453541Sshin 226553541Sshin icmp6->icmp6_cksum = 0; 226653541Sshin icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2267120891Sume sizeof(struct ip6_hdr), plen); 226853541Sshin 226953541Sshin /* 227078064Sume * XXX option handling 227153541Sshin */ 227253541Sshin 227353541Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 227453541Sshin 2275105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 227653541Sshin if (outif) 227753541Sshin icmp6_ifoutstat_inc(outif, type, code); 227853541Sshin 2279194760Srwatson if (ia != NULL) 2280194760Srwatson ifa_free(&ia->ia_ifa); 228153541Sshin return; 228253541Sshin 228353541Sshin bad: 2284194760Srwatson if (ia != NULL) 2285194760Srwatson ifa_free(&ia->ia_ifa); 228653541Sshin m_freem(m); 228753541Sshin return; 228853541Sshin} 228953541Sshin 229053541Sshinvoid 2291171259Sdelphijicmp6_fasttimo(void) 229253541Sshin{ 229362587Sitojun 2294191672Sbms mld_fasttimo(); 229553541Sshin} 229653541Sshin 2297191672Sbmsvoid 2298191672Sbmsicmp6_slowtimo(void) 2299191672Sbms{ 2300191672Sbms 2301191672Sbms mld_slowtimo(); 2302191672Sbms} 2303191672Sbms 230453541Sshinstatic const char * 2305171259Sdelphijicmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6, 2306171259Sdelphij struct in6_addr *tgt6) 230753541Sshin{ 230853541Sshin static char buf[1024]; 2309165118Sbz char ip6bufs[INET6_ADDRSTRLEN]; 2310165118Sbz char ip6bufd[INET6_ADDRSTRLEN]; 2311165118Sbz char ip6buft[INET6_ADDRSTRLEN]; 231253541Sshin snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", 2313165118Sbz ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6), 2314165118Sbz ip6_sprintf(ip6buft, tgt6)); 231553541Sshin return buf; 231653541Sshin} 231753541Sshin 231853541Sshinvoid 2319171259Sdelphijicmp6_redirect_input(struct mbuf *m, int off) 232053541Sshin{ 2321171133Sgnn struct ifnet *ifp; 232253541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 232362587Sitojun struct nd_redirect *nd_rd; 232453541Sshin int icmp6len = ntohs(ip6->ip6_plen); 232553541Sshin char *lladdr = NULL; 232653541Sshin int lladdrlen = 0; 232753541Sshin struct rtentry *rt = NULL; 232853541Sshin int is_router; 232953541Sshin int is_onlink; 233053541Sshin struct in6_addr src6 = ip6->ip6_src; 233162587Sitojun struct in6_addr redtgt6; 233262587Sitojun struct in6_addr reddst6; 233353541Sshin union nd_opts ndopts; 2334165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 233553541Sshin 2336227308Sglebius M_ASSERTPKTHDR(m); 2337227308Sglebius KASSERT(m->m_pkthdr.rcvif != NULL, ("%s: no rcvif", __func__)); 233853541Sshin 2339171133Sgnn ifp = m->m_pkthdr.rcvif; 2340171133Sgnn 234153541Sshin /* XXX if we are router, we don't update route by icmp6 redirect */ 2342181803Sbz if (V_ip6_forwarding) 234362587Sitojun goto freeit; 2344181803Sbz if (!V_icmp6_rediraccept) 234562587Sitojun goto freeit; 234662587Sitojun 234762587Sitojun#ifndef PULLDOWN_TEST 234862587Sitojun IP6_EXTHDR_CHECK(m, off, icmp6len,); 234962587Sitojun nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); 235062587Sitojun#else 235162587Sitojun IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); 235262587Sitojun if (nd_rd == NULL) { 2353190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 235453541Sshin return; 235562587Sitojun } 235662587Sitojun#endif 235762587Sitojun redtgt6 = nd_rd->nd_rd_target; 235862587Sitojun reddst6 = nd_rd->nd_rd_dst; 235953541Sshin 2360148385Sume if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) || 2361148385Sume in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) { 2362148385Sume goto freeit; 2363148385Sume } 236453541Sshin 236553541Sshin /* validation */ 236653541Sshin if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 236778064Sume nd6log((LOG_ERR, 2368120891Sume "ICMP6 redirect sent from %s rejected; " 2369120891Sume "must be from linklocal\n", 2370165118Sbz ip6_sprintf(ip6buf, &src6))); 237178064Sume goto bad; 237253541Sshin } 237353541Sshin if (ip6->ip6_hlim != 255) { 237478064Sume nd6log((LOG_ERR, 2375120891Sume "ICMP6 redirect sent from %s rejected; " 2376120891Sume "hlim=%d (must be 255)\n", 2377165118Sbz ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim)); 237878064Sume goto bad; 237953541Sshin } 238053541Sshin { 238153541Sshin /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 238253541Sshin struct sockaddr_in6 sin6; 238353541Sshin struct in6_addr *gw6; 238453541Sshin 238553541Sshin bzero(&sin6, sizeof(sin6)); 238653541Sshin sin6.sin6_family = AF_INET6; 238753541Sshin sin6.sin6_len = sizeof(struct sockaddr_in6); 238853541Sshin bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); 2389231852Sbz rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, 0UL, RT_DEFAULT_FIB); 239053541Sshin if (rt) { 239165895Sume if (rt->rt_gateway == NULL || 239265895Sume rt->rt_gateway->sa_family != AF_INET6) { 2393235953Sbz RTFREE_LOCKED(rt); 239478064Sume nd6log((LOG_ERR, 239565895Sume "ICMP6 redirect rejected; no route " 239665895Sume "with inet6 gateway found for redirect dst: %s\n", 239778064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 239878064Sume goto bad; 239965895Sume } 240065895Sume 240153541Sshin gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); 240253541Sshin if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { 2403235953Sbz RTFREE_LOCKED(rt); 240478064Sume nd6log((LOG_ERR, 2405120891Sume "ICMP6 redirect rejected; " 2406120891Sume "not equal to gw-for-src=%s (must be same): " 2407120891Sume "%s\n", 2408165118Sbz ip6_sprintf(ip6buf, gw6), 2409120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 241078064Sume goto bad; 241153541Sshin } 241253541Sshin } else { 241378064Sume nd6log((LOG_ERR, 2414120891Sume "ICMP6 redirect rejected; " 2415120891Sume "no route found for redirect dst: %s\n", 2416120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 241778064Sume goto bad; 241853541Sshin } 2419120727Ssam RTFREE_LOCKED(rt); 242053541Sshin rt = NULL; 242153541Sshin } 242253541Sshin if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 242378064Sume nd6log((LOG_ERR, 2424120891Sume "ICMP6 redirect rejected; " 2425120891Sume "redirect dst must be unicast: %s\n", 2426120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 242778064Sume goto bad; 242853541Sshin } 242953541Sshin 243053541Sshin is_router = is_onlink = 0; 243153541Sshin if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 243253541Sshin is_router = 1; /* router case */ 243353541Sshin if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 243453541Sshin is_onlink = 1; /* on-link destination case */ 243553541Sshin if (!is_router && !is_onlink) { 243678064Sume nd6log((LOG_ERR, 2437120891Sume "ICMP6 redirect rejected; " 2438120891Sume "neither router case nor onlink case: %s\n", 2439120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 244078064Sume goto bad; 244153541Sshin } 244253541Sshin /* validation passed */ 244353541Sshin 244453541Sshin icmp6len -= sizeof(*nd_rd); 244553541Sshin nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 244653541Sshin if (nd6_options(&ndopts) < 0) { 2447227308Sglebius nd6log((LOG_INFO, "%s: invalid ND option, rejected: %s\n", 2448227308Sglebius __func__, icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 244978064Sume /* nd6_options have incremented stats */ 245062587Sitojun goto freeit; 245153541Sshin } 245253541Sshin 245353541Sshin if (ndopts.nd_opts_tgt_lladdr) { 245453541Sshin lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 245553541Sshin lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 245653541Sshin } 245753541Sshin 245853541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 2459227308Sglebius nd6log((LOG_INFO, "%s: lladdrlen mismatch for %s " 2460120891Sume "(if %d, icmp6 packet %d): %s\n", 2461227308Sglebius __func__, ip6_sprintf(ip6buf, &redtgt6), 2462165118Sbz ifp->if_addrlen, lladdrlen - 2, 2463120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 246478064Sume goto bad; 246553541Sshin } 246653541Sshin 246753541Sshin /* RFC 2461 8.3 */ 246853541Sshin nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 2469120891Sume is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 247053541Sshin 247195023Ssuz if (!is_onlink) { /* better router case. perform rtredirect. */ 247253541Sshin /* perform rtredirect */ 247353541Sshin struct sockaddr_in6 sdst; 247453541Sshin struct sockaddr_in6 sgw; 247553541Sshin struct sockaddr_in6 ssrc; 2476231852Sbz u_int fibnum; 247753541Sshin 247853541Sshin bzero(&sdst, sizeof(sdst)); 247953541Sshin bzero(&sgw, sizeof(sgw)); 248053541Sshin bzero(&ssrc, sizeof(ssrc)); 248153541Sshin sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; 248253541Sshin sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = 248353541Sshin sizeof(struct sockaddr_in6); 248453541Sshin bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); 248553541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 248653541Sshin bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 2487231852Sbz for (fibnum = 0; fibnum < rt_numfibs; fibnum++) 2488231852Sbz in6_rtredirect((struct sockaddr *)&sdst, 2489231852Sbz (struct sockaddr *)&sgw, (struct sockaddr *)NULL, 2490231852Sbz RTF_GATEWAY | RTF_HOST, (struct sockaddr *)&ssrc, 2491231852Sbz fibnum); 249253541Sshin } 249353541Sshin /* finally update cached route in each socket via pfctlinput */ 249453541Sshin { 249553541Sshin struct sockaddr_in6 sdst; 249653541Sshin 249753541Sshin bzero(&sdst, sizeof(sdst)); 249853541Sshin sdst.sin6_family = AF_INET6; 249953541Sshin sdst.sin6_len = sizeof(struct sockaddr_in6); 250053541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 250153541Sshin pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); 250253541Sshin } 250362587Sitojun 250462587Sitojun freeit: 250562587Sitojun m_freem(m); 250678064Sume return; 250778064Sume 250878064Sume bad: 2509190964Srwatson ICMP6STAT_INC(icp6s_badredirect); 251078064Sume m_freem(m); 251153541Sshin} 251253541Sshin 251353541Sshinvoid 2514171259Sdelphijicmp6_redirect_output(struct mbuf *m0, struct rtentry *rt) 251553541Sshin{ 251653541Sshin struct ifnet *ifp; /* my outgoing interface */ 251753541Sshin struct in6_addr *ifp_ll6; 251853541Sshin struct in6_addr *router_ll6; 251953541Sshin struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 252053541Sshin struct mbuf *m = NULL; /* newly allocated one */ 2521211501Sanchie struct m_tag *mtag; 252253541Sshin struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 252353541Sshin struct nd_redirect *nd_rd; 2524186157Skmacy struct llentry *ln = NULL; 252553541Sshin size_t maxlen; 252653541Sshin u_char *p; 252753541Sshin struct ifnet *outif = NULL; 252862587Sitojun struct sockaddr_in6 src_sa; 252953541Sshin 2530251995Sae icmp6_errcount(ND_REDIRECT, 0); 253162587Sitojun 253253541Sshin /* if we are not router, we don't send icmp6 redirect */ 2533181803Sbz if (!V_ip6_forwarding) 253453541Sshin goto fail; 253553541Sshin 253653541Sshin /* sanity check */ 253753541Sshin if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) 253853541Sshin goto fail; 253953541Sshin 254053541Sshin /* 254153541Sshin * Address check: 254253541Sshin * the source address must identify a neighbor, and 254353541Sshin * the destination address must not be a multicast address 254453541Sshin * [RFC 2461, sec 8.2] 254553541Sshin */ 254653541Sshin sip6 = mtod(m0, struct ip6_hdr *); 254762587Sitojun bzero(&src_sa, sizeof(src_sa)); 254862587Sitojun src_sa.sin6_family = AF_INET6; 254962587Sitojun src_sa.sin6_len = sizeof(src_sa); 255062587Sitojun src_sa.sin6_addr = sip6->ip6_src; 255162587Sitojun if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) 255253541Sshin goto fail; 255353541Sshin if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) 255453541Sshin goto fail; /* what should we do here? */ 255553541Sshin 255653541Sshin /* rate limit */ 255753541Sshin if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) 255853541Sshin goto fail; 255953541Sshin 256053541Sshin /* 256153541Sshin * Since we are going to append up to 1280 bytes (= IPV6_MMTU), 256253541Sshin * we almost always ask for an mbuf cluster for simplicity. 256353541Sshin * (MHLEN < IPV6_MMTU is almost always true) 256453541Sshin */ 256562587Sitojun#if IPV6_MMTU >= MCLBYTES 256662587Sitojun# error assumption failed about IPV6_MMTU and MCLBYTES 256762587Sitojun#endif 2568248321Sglebius m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 2569248321Sglebius if (m == NULL) 257053541Sshin goto fail; 2571231852Sbz M_SETFIB(m, rt->rt_fibnum); 257278064Sume maxlen = M_TRAILINGSPACE(m); 257353541Sshin maxlen = min(IPV6_MMTU, maxlen); 257453541Sshin /* just for safety */ 257562587Sitojun if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 257662587Sitojun ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { 257753541Sshin goto fail; 257862587Sitojun } 257953541Sshin 258053541Sshin { 258153541Sshin /* get ip6 linklocal address for ifp(my outgoing interface). */ 258262587Sitojun struct in6_ifaddr *ia; 258362587Sitojun if ((ia = in6ifa_ifpforlinklocal(ifp, 258462587Sitojun IN6_IFF_NOTREADY| 258562587Sitojun IN6_IFF_ANYCAST)) == NULL) 258653541Sshin goto fail; 258753541Sshin ifp_ll6 = &ia->ia_addr.sin6_addr; 2588194760Srwatson /* XXXRW: reference released prematurely. */ 2589194760Srwatson ifa_free(&ia->ia_ifa); 259053541Sshin } 259153541Sshin 259253541Sshin /* get ip6 linklocal address for the router. */ 259353541Sshin if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { 259453541Sshin struct sockaddr_in6 *sin6; 259553541Sshin sin6 = (struct sockaddr_in6 *)rt->rt_gateway; 259653541Sshin router_ll6 = &sin6->sin6_addr; 259753541Sshin if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) 259853541Sshin router_ll6 = (struct in6_addr *)NULL; 259953541Sshin } else 260053541Sshin router_ll6 = (struct in6_addr *)NULL; 260153541Sshin 260253541Sshin /* ip6 */ 260353541Sshin ip6 = mtod(m, struct ip6_hdr *); 260453541Sshin ip6->ip6_flow = 0; 260562587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 260662587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 260753541Sshin /* ip6->ip6_plen will be set later */ 260853541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 260953541Sshin ip6->ip6_hlim = 255; 261053541Sshin /* ip6->ip6_src must be linklocal addr for my outgoing if. */ 261153541Sshin bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); 261253541Sshin bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); 261353541Sshin 261453541Sshin /* ND Redirect */ 261553541Sshin nd_rd = (struct nd_redirect *)(ip6 + 1); 261653541Sshin nd_rd->nd_rd_type = ND_REDIRECT; 261753541Sshin nd_rd->nd_rd_code = 0; 261853541Sshin nd_rd->nd_rd_reserved = 0; 261953541Sshin if (rt->rt_flags & RTF_GATEWAY) { 262053541Sshin /* 262153541Sshin * nd_rd->nd_rd_target must be a link-local address in 262253541Sshin * better router cases. 262353541Sshin */ 262453541Sshin if (!router_ll6) 262553541Sshin goto fail; 262653541Sshin bcopy(router_ll6, &nd_rd->nd_rd_target, 2627120892Sume sizeof(nd_rd->nd_rd_target)); 262853541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2629120892Sume sizeof(nd_rd->nd_rd_dst)); 263053541Sshin } else { 263153541Sshin /* make sure redtgt == reddst */ 263253541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, 2633120892Sume sizeof(nd_rd->nd_rd_target)); 263453541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2635120892Sume sizeof(nd_rd->nd_rd_dst)); 263653541Sshin } 263753541Sshin 263853541Sshin p = (u_char *)(nd_rd + 1); 263953541Sshin 264053541Sshin if (!router_ll6) 264153541Sshin goto nolladdropt; 264253541Sshin 2643120892Sume { 2644120892Sume /* target lladdr option */ 2645120892Sume int len; 2646120892Sume struct nd_opt_hdr *nd_opt; 2647120892Sume char *lladdr; 264853541Sshin 2649243148Sae IF_AFDATA_RLOCK(ifp); 2650186119Sqingli ln = nd6_lookup(router_ll6, 0, ifp); 2651243148Sae IF_AFDATA_RUNLOCK(ifp); 2652186157Skmacy if (ln == NULL) 2653120892Sume goto nolladdropt; 2654186119Sqingli 2655120892Sume len = sizeof(*nd_opt) + ifp->if_addrlen; 2656120892Sume len = (len + 7) & ~7; /* round by 8 */ 2657120892Sume /* safety check */ 2658186157Skmacy if (len + (p - (u_char *)ip6) > maxlen) 2659120892Sume goto nolladdropt; 2660186119Sqingli 2661186119Sqingli if (ln->la_flags & LLE_VALID) { 2662120892Sume nd_opt = (struct nd_opt_hdr *)p; 2663120892Sume nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 2664120892Sume nd_opt->nd_opt_len = len >> 3; 2665120892Sume lladdr = (char *)(nd_opt + 1); 2666186119Sqingli bcopy(&ln->ll_addr, lladdr, ifp->if_addrlen); 2667120892Sume p += len; 2668120892Sume } 2669186157Skmacy } 2670186157Skmacynolladdropt: 2671186157Skmacy if (ln != NULL) 2672186119Sqingli LLE_RUNLOCK(ln); 2673186157Skmacy 267453541Sshin m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 267553541Sshin 267653541Sshin /* just to be safe */ 267762587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 267853541Sshin if (m0->m_flags & M_DECRYPTED) 267953541Sshin goto noredhdropt; 268062587Sitojun#endif 268162587Sitojun if (p - (u_char *)ip6 > maxlen) 268262587Sitojun goto noredhdropt; 268353541Sshin 2684120891Sume { 2685120891Sume /* redirected header option */ 2686120891Sume int len; 2687120891Sume struct nd_opt_rd_hdr *nd_opt_rh; 268853541Sshin 2689120891Sume /* 2690120891Sume * compute the maximum size for icmp6 redirect header option. 2691120891Sume * XXX room for auth header? 2692120891Sume */ 2693120891Sume len = maxlen - (p - (u_char *)ip6); 2694120891Sume len &= ~7; 269553541Sshin 2696120891Sume /* This is just for simplicity. */ 2697120891Sume if (m0->m_pkthdr.len != m0->m_len) { 2698120891Sume if (m0->m_next) { 2699120891Sume m_freem(m0->m_next); 2700120891Sume m0->m_next = NULL; 2701120891Sume } 2702120891Sume m0->m_pkthdr.len = m0->m_len; 270353541Sshin } 270453541Sshin 2705120891Sume /* 2706120891Sume * Redirected header option spec (RFC2461 4.6.3) talks nothing 2707120891Sume * about padding/truncate rule for the original IP packet. 2708120891Sume * From the discussion on IPv6imp in Feb 1999, 2709120891Sume * the consensus was: 2710120891Sume * - "attach as much as possible" is the goal 2711120891Sume * - pad if not aligned (original size can be guessed by 2712120891Sume * original ip6 header) 2713120891Sume * Following code adds the padding if it is simple enough, 2714120891Sume * and truncates if not. 2715120891Sume */ 2716120891Sume if (m0->m_next || m0->m_pkthdr.len != m0->m_len) 2717120891Sume panic("assumption failed in %s:%d", __FILE__, 2718120891Sume __LINE__); 271953541Sshin 2720120891Sume if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { 2721120891Sume /* not enough room, truncate */ 2722120891Sume m0->m_pkthdr.len = m0->m_len = len - 2723120891Sume sizeof(*nd_opt_rh); 2724120891Sume } else { 2725120891Sume /* enough room, pad or truncate */ 2726120891Sume size_t extra; 272753541Sshin 2728120891Sume extra = m0->m_pkthdr.len % 8; 2729120891Sume if (extra) { 2730120891Sume /* pad if easy enough, truncate if not */ 2731120891Sume if (8 - extra <= M_TRAILINGSPACE(m0)) { 2732120891Sume /* pad */ 2733120891Sume m0->m_len += (8 - extra); 2734120891Sume m0->m_pkthdr.len += (8 - extra); 2735120891Sume } else { 2736120891Sume /* truncate */ 2737120891Sume m0->m_pkthdr.len -= extra; 2738120891Sume m0->m_len -= extra; 2739120891Sume } 274053541Sshin } 2741120891Sume len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); 2742120891Sume m0->m_pkthdr.len = m0->m_len = len - 2743120891Sume sizeof(*nd_opt_rh); 274453541Sshin } 274553541Sshin 2746120891Sume nd_opt_rh = (struct nd_opt_rd_hdr *)p; 2747120891Sume bzero(nd_opt_rh, sizeof(*nd_opt_rh)); 2748120891Sume nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; 2749120891Sume nd_opt_rh->nd_opt_rh_len = len >> 3; 2750120891Sume p += sizeof(*nd_opt_rh); 2751120891Sume m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 275253541Sshin 2753120891Sume /* connect m0 to m */ 2754120891Sume m_tag_delete_chain(m0, NULL); 2755120891Sume m0->m_flags &= ~M_PKTHDR; 2756120891Sume m->m_next = m0; 2757120891Sume m->m_pkthdr.len = m->m_len + m0->m_len; 2758120891Sume m0 = NULL; 2759120891Sume } 276053541Sshinnoredhdropt:; 2761112781Ssuz if (m0) { 2762112781Ssuz m_freem(m0); 2763112781Ssuz m0 = NULL; 2764112781Ssuz } 276553541Sshin 2766121315Sume /* XXX: clear embedded link IDs in the inner header */ 2767121315Sume in6_clearscope(&sip6->ip6_src); 2768121315Sume in6_clearscope(&sip6->ip6_dst); 2769121315Sume in6_clearscope(&nd_rd->nd_rd_target); 2770121315Sume in6_clearscope(&nd_rd->nd_rd_dst); 277153541Sshin 277253541Sshin ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 277353541Sshin 277453541Sshin nd_rd->nd_rd_cksum = 0; 2775120891Sume nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2776120891Sume sizeof(*ip6), ntohs(ip6->ip6_plen)); 277753541Sshin 2778211501Sanchie if (send_sendso_input_hook != NULL) { 2779211501Sanchie mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, sizeof(unsigned short), 2780211501Sanchie M_NOWAIT); 2781211501Sanchie if (mtag == NULL) 2782211501Sanchie goto fail; 2783211501Sanchie *(unsigned short *)(mtag + 1) = nd_rd->nd_rd_type; 2784211501Sanchie m_tag_prepend(m, mtag); 2785211501Sanchie } 2786211501Sanchie 278753541Sshin /* send the packet to outside... */ 2788105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 278953541Sshin if (outif) { 279053541Sshin icmp6_ifstat_inc(outif, ifs6_out_msg); 279153541Sshin icmp6_ifstat_inc(outif, ifs6_out_redirect); 279253541Sshin } 2793190964Srwatson ICMP6STAT_INC(icp6s_outhist[ND_REDIRECT]); 279453541Sshin 279553541Sshin return; 279653541Sshin 279753541Sshinfail: 279853541Sshin if (m) 279953541Sshin m_freem(m); 280053541Sshin if (m0) 280153541Sshin m_freem(m0); 280253541Sshin} 280353541Sshin 280453541Sshin/* 280553541Sshin * ICMPv6 socket option processing. 280653541Sshin */ 280753541Sshinint 2808171259Sdelphijicmp6_ctloutput(struct socket *so, struct sockopt *sopt) 280953541Sshin{ 281053541Sshin int error = 0; 281153541Sshin int optlen; 281278064Sume struct inpcb *inp = sotoinpcb(so); 281353541Sshin int level, op, optname; 281453541Sshin 281553541Sshin if (sopt) { 281653541Sshin level = sopt->sopt_level; 281753541Sshin op = sopt->sopt_dir; 281853541Sshin optname = sopt->sopt_name; 281953541Sshin optlen = sopt->sopt_valsize; 282053541Sshin } else 282153541Sshin level = op = optname = optlen = 0; 282278064Sume 282353541Sshin if (level != IPPROTO_ICMPV6) { 282453541Sshin return EINVAL; 282553541Sshin } 282653541Sshin 282778064Sume switch (op) { 282853541Sshin case PRCO_SETOPT: 282953541Sshin switch (optname) { 283053541Sshin case ICMP6_FILTER: 283153541Sshin { 2832180968Srwatson struct icmp6_filter ic6f; 283353541Sshin 2834180968Srwatson if (optlen != sizeof(ic6f)) { 283553541Sshin error = EMSGSIZE; 283653541Sshin break; 283753541Sshin } 2838180968Srwatson error = sooptcopyin(sopt, &ic6f, optlen, optlen); 2839180968Srwatson if (error == 0) { 2840180968Srwatson INP_WLOCK(inp); 2841180968Srwatson *inp->in6p_icmp6filt = ic6f; 2842180968Srwatson INP_WUNLOCK(inp); 2843180968Srwatson } 284453541Sshin break; 284553541Sshin } 284653541Sshin 284753541Sshin default: 284853541Sshin error = ENOPROTOOPT; 284953541Sshin break; 285053541Sshin } 285153541Sshin break; 285253541Sshin 285353541Sshin case PRCO_GETOPT: 285453541Sshin switch (optname) { 285553541Sshin case ICMP6_FILTER: 285653541Sshin { 2857180968Srwatson struct icmp6_filter ic6f; 2858180968Srwatson 2859180968Srwatson INP_RLOCK(inp); 2860180968Srwatson ic6f = *inp->in6p_icmp6filt; 2861180968Srwatson INP_RUNLOCK(inp); 2862180968Srwatson error = sooptcopyout(sopt, &ic6f, sizeof(ic6f)); 286353541Sshin break; 286453541Sshin } 286553541Sshin 286653541Sshin default: 286753541Sshin error = ENOPROTOOPT; 286853541Sshin break; 286953541Sshin } 287053541Sshin break; 287153541Sshin } 287253541Sshin 2873120856Sume return (error); 287453541Sshin} 287553541Sshin 287653541Sshin/* 287753541Sshin * Perform rate limit check. 287853541Sshin * Returns 0 if it is okay to send the icmp6 packet. 287953541Sshin * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 288053541Sshin * limitation. 288153541Sshin * 288253541Sshin * XXX per-destination/type check necessary? 2883171259Sdelphij * 2884171259Sdelphij * dst - not used at this moment 2885171259Sdelphij * type - not used at this moment 2886171259Sdelphij * code - not used at this moment 288753541Sshin */ 288853541Sshinstatic int 2889171259Sdelphijicmp6_ratelimit(const struct in6_addr *dst, const int type, 2890171259Sdelphij const int code) 289153541Sshin{ 289262587Sitojun int ret; 289353541Sshin 289495023Ssuz ret = 0; /* okay to send */ 289553541Sshin 289662587Sitojun /* PPS limit */ 2897181803Sbz if (!ppsratecheck(&V_icmp6errppslim_last, &V_icmp6errpps_count, 2898181803Sbz V_icmp6errppslim)) { 289953541Sshin /* The packet is subject to rate limit */ 290062587Sitojun ret++; 290153541Sshin } 290253541Sshin 290362587Sitojun return ret; 290453541Sshin} 2905