icmp6.c revision 175162
1139826Simp/*- 253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 353541Sshin * All rights reserved. 453541Sshin * 553541Sshin * Redistribution and use in source and binary forms, with or without 653541Sshin * modification, are permitted provided that the following conditions 753541Sshin * are met: 853541Sshin * 1. Redistributions of source code must retain the above copyright 953541Sshin * notice, this list of conditions and the following disclaimer. 1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1153541Sshin * notice, this list of conditions and the following disclaimer in the 1253541Sshin * documentation and/or other materials provided with the distribution. 1353541Sshin * 3. Neither the name of the project nor the names of its contributors 1453541Sshin * may be used to endorse or promote products derived from this software 1553541Sshin * without specific prior written permission. 1653541Sshin * 1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2053541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2753541Sshin * SUCH DAMAGE. 28174510Sobrien * 29174510Sobrien * $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ 3053541Sshin */ 3153541Sshin 32139826Simp/*- 3353541Sshin * Copyright (c) 1982, 1986, 1988, 1993 3453541Sshin * The Regents of the University of California. All rights reserved. 3553541Sshin * 3653541Sshin * Redistribution and use in source and binary forms, with or without 3753541Sshin * modification, are permitted provided that the following conditions 3853541Sshin * are met: 3953541Sshin * 1. Redistributions of source code must retain the above copyright 4053541Sshin * notice, this list of conditions and the following disclaimer. 4153541Sshin * 2. Redistributions in binary form must reproduce the above copyright 4253541Sshin * notice, this list of conditions and the following disclaimer in the 4353541Sshin * documentation and/or other materials provided with the distribution. 4453541Sshin * 4. Neither the name of the University nor the names of its contributors 4553541Sshin * may be used to endorse or promote products derived from this software 4653541Sshin * without specific prior written permission. 4753541Sshin * 4853541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 4953541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5053541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5153541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5253541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5353541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5453541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5553541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5653541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5753541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5853541Sshin * SUCH DAMAGE. 5953541Sshin * 6053541Sshin * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 6153541Sshin */ 6253541Sshin 63174510Sobrien#include <sys/cdefs.h> 64174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/icmp6.c 175162 2008-01-08 19:08:58Z obrien $"); 65174510Sobrien 6662587Sitojun#include "opt_inet.h" 6762587Sitojun#include "opt_inet6.h" 6855009Sshin#include "opt_ipsec.h" 6953541Sshin 7053541Sshin#include <sys/param.h> 7195759Stanimura#include <sys/domain.h> 7295759Stanimura#include <sys/kernel.h> 7395759Stanimura#include <sys/lock.h> 7478064Sume#include <sys/malloc.h> 7553541Sshin#include <sys/mbuf.h> 7653541Sshin#include <sys/protosw.h> 7795759Stanimura#include <sys/signalvar.h> 7853541Sshin#include <sys/socket.h> 7953541Sshin#include <sys/socketvar.h> 8095759Stanimura#include <sys/sx.h> 8195759Stanimura#include <sys/syslog.h> 8295759Stanimura#include <sys/systm.h> 8353541Sshin#include <sys/time.h> 8453541Sshin 8553541Sshin#include <net/if.h> 8653541Sshin#include <net/if_dl.h> 8753541Sshin#include <net/if_types.h> 8895759Stanimura#include <net/route.h> 8953541Sshin 9053541Sshin#include <netinet/in.h> 9195759Stanimura#include <netinet/in_pcb.h> 9253541Sshin#include <netinet/in_var.h> 9362587Sitojun#include <netinet/ip6.h> 9495759Stanimura#include <netinet/icmp6.h> 95122922Sandre#include <netinet/tcp_var.h> 9695759Stanimura#include <netinet6/in6_ifattach.h> 9795759Stanimura#include <netinet6/in6_pcb.h> 9895759Stanimura#include <netinet6/ip6protosw.h> 9953541Sshin#include <netinet6/ip6_var.h> 100148385Sume#include <netinet6/scope6_var.h> 10153541Sshin#include <netinet6/mld6_var.h> 10253541Sshin#include <netinet6/nd6.h> 10353541Sshin 104171167Sgnn#ifdef IPSEC 105105199Ssam#include <netipsec/ipsec.h> 106105199Ssam#include <netipsec/key.h> 107105199Ssam#endif 108105199Ssam 10962587Sitojunextern struct domain inet6domain; 11053541Sshin 11162587Sitojunstruct icmp6stat icmp6stat; 11253541Sshin 113141553Srwatsonextern struct inpcbinfo ripcbinfo; 11462587Sitojunextern struct inpcbhead ripcb; 11562587Sitojunextern int icmp6errppslim; 11662587Sitojunstatic int icmp6errpps_count = 0; 11778064Sumestatic struct timeval icmp6errppslim_last; 11862587Sitojunextern int icmp6_nodeinfo; 11953541Sshin 120175162Sobrienstatic void icmp6_errcount(struct icmp6errstat *, int, int); 121175162Sobrienstatic int icmp6_rip6_input(struct mbuf **, int); 122175162Sobrienstatic int icmp6_ratelimit(const struct in6_addr *, const int, const int); 12362587Sitojunstatic const char *icmp6_redirect_diag __P((struct in6_addr *, 12462587Sitojun struct in6_addr *, struct in6_addr *)); 125175162Sobrienstatic struct mbuf *ni6_input(struct mbuf *, int); 126175162Sobrienstatic struct mbuf *ni6_nametodns(const char *, int, int); 127175162Sobrienstatic int ni6_dnsmatch(const char *, int, const char *, int); 12862587Sitojunstatic int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *, 129148892Sume struct ifnet **, struct in6_addr *)); 13062587Sitojunstatic int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, 13162587Sitojun struct ifnet *, int)); 132175162Sobrienstatic int icmp6_notify_error(struct mbuf **, int, int, int); 13353541Sshin 13453541Sshin 13553541Sshinvoid 136171259Sdelphijicmp6_init(void) 13753541Sshin{ 138171259Sdelphij 13953541Sshin mld6_init(); 14053541Sshin} 14153541Sshin 14262587Sitojunstatic void 143171259Sdelphijicmp6_errcount(struct icmp6errstat *stat, int type, int code) 14462587Sitojun{ 14578064Sume switch (type) { 14662587Sitojun case ICMP6_DST_UNREACH: 14762587Sitojun switch (code) { 14862587Sitojun case ICMP6_DST_UNREACH_NOROUTE: 14962587Sitojun stat->icp6errs_dst_unreach_noroute++; 15062587Sitojun return; 15162587Sitojun case ICMP6_DST_UNREACH_ADMIN: 15262587Sitojun stat->icp6errs_dst_unreach_admin++; 15362587Sitojun return; 15462587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 15562587Sitojun stat->icp6errs_dst_unreach_beyondscope++; 15662587Sitojun return; 15762587Sitojun case ICMP6_DST_UNREACH_ADDR: 15862587Sitojun stat->icp6errs_dst_unreach_addr++; 15962587Sitojun return; 16062587Sitojun case ICMP6_DST_UNREACH_NOPORT: 16162587Sitojun stat->icp6errs_dst_unreach_noport++; 16262587Sitojun return; 16362587Sitojun } 16462587Sitojun break; 16562587Sitojun case ICMP6_PACKET_TOO_BIG: 16662587Sitojun stat->icp6errs_packet_too_big++; 16762587Sitojun return; 16862587Sitojun case ICMP6_TIME_EXCEEDED: 16978064Sume switch (code) { 17062587Sitojun case ICMP6_TIME_EXCEED_TRANSIT: 17162587Sitojun stat->icp6errs_time_exceed_transit++; 17262587Sitojun return; 17362587Sitojun case ICMP6_TIME_EXCEED_REASSEMBLY: 17462587Sitojun stat->icp6errs_time_exceed_reassembly++; 17562587Sitojun return; 17662587Sitojun } 17762587Sitojun break; 17862587Sitojun case ICMP6_PARAM_PROB: 17978064Sume switch (code) { 18062587Sitojun case ICMP6_PARAMPROB_HEADER: 18162587Sitojun stat->icp6errs_paramprob_header++; 18262587Sitojun return; 18362587Sitojun case ICMP6_PARAMPROB_NEXTHEADER: 18462587Sitojun stat->icp6errs_paramprob_nextheader++; 18562587Sitojun return; 18662587Sitojun case ICMP6_PARAMPROB_OPTION: 18762587Sitojun stat->icp6errs_paramprob_option++; 18862587Sitojun return; 18962587Sitojun } 19062587Sitojun break; 19162587Sitojun case ND_REDIRECT: 19262587Sitojun stat->icp6errs_redirect++; 19362587Sitojun return; 19462587Sitojun } 19562587Sitojun stat->icp6errs_unknown++; 19662587Sitojun} 19762587Sitojun 19853541Sshin/* 199148385Sume * A wrapper function for icmp6_error() necessary when the erroneous packet 200148385Sume * may not contain enough scope zone information. 201148385Sume */ 202148385Sumevoid 203171259Sdelphijicmp6_error2(struct mbuf *m, int type, int code, int param, 204171259Sdelphij struct ifnet *ifp) 205148385Sume{ 206148385Sume struct ip6_hdr *ip6; 207148385Sume 208148385Sume if (ifp == NULL) 209148385Sume return; 210148385Sume 211148385Sume#ifndef PULLDOWN_TEST 212148385Sume IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 213148385Sume#else 214148385Sume if (m->m_len < sizeof(struct ip6_hdr)) { 215148385Sume m = m_pullup(m, sizeof(struct ip6_hdr)); 216148385Sume if (m == NULL) 217148385Sume return; 218148385Sume } 219148385Sume#endif 220148385Sume 221148385Sume ip6 = mtod(m, struct ip6_hdr *); 222148385Sume 223148385Sume if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0) 224148385Sume return; 225148385Sume if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) 226148385Sume return; 227148385Sume 228148385Sume icmp6_error(m, type, code, param); 229148385Sume} 230148385Sume 231148385Sume/* 23253541Sshin * Generate an error packet of type error in response to bad IP6 packet. 23353541Sshin */ 23453541Sshinvoid 235171259Sdelphijicmp6_error(struct mbuf *m, int type, int code, int param) 23653541Sshin{ 23753541Sshin struct ip6_hdr *oip6, *nip6; 23853541Sshin struct icmp6_hdr *icmp6; 23962587Sitojun u_int preplen; 24053541Sshin int off; 24162587Sitojun int nxt; 24253541Sshin 24353541Sshin icmp6stat.icp6s_error++; 24453541Sshin 24562587Sitojun /* count per-type-code statistics */ 24662587Sitojun icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code); 24762587Sitojun 24862587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 24962587Sitojun if (m->m_flags & M_DECRYPTED) { 25062587Sitojun icmp6stat.icp6s_canterror++; 25153541Sshin goto freeit; 25262587Sitojun } 25362587Sitojun#endif 25453541Sshin 25562587Sitojun#ifndef PULLDOWN_TEST 25662587Sitojun IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ); 25762587Sitojun#else 25862587Sitojun if (m->m_len < sizeof(struct ip6_hdr)) { 25962587Sitojun m = m_pullup(m, sizeof(struct ip6_hdr)); 26062587Sitojun if (m == NULL) 26162587Sitojun return; 26262587Sitojun } 26362587Sitojun#endif 26453541Sshin oip6 = mtod(m, struct ip6_hdr *); 26553541Sshin 26653541Sshin /* 267121472Sume * If the destination address of the erroneous packet is a multicast 268121472Sume * address, or the packet was sent using link-layer multicast, 269121472Sume * we should basically suppress sending an error (RFC 2463, Section 270121472Sume * 2.4). 271121472Sume * We have two exceptions (the item e.2 in that section): 272121472Sume * - the Pakcet Too Big message can be sent for path MTU discovery. 273121472Sume * - the Parameter Problem Message that can be allowed an icmp6 error 274121472Sume * in the option type field. This check has been done in 275121472Sume * ip6_unknown_opt(), so we can just check the type and code. 27653541Sshin */ 27753541Sshin if ((m->m_flags & (M_BCAST|M_MCAST) || 27853541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && 27953541Sshin (type != ICMP6_PACKET_TOO_BIG && 28053541Sshin (type != ICMP6_PARAM_PROB || 28153541Sshin code != ICMP6_PARAMPROB_OPTION))) 28253541Sshin goto freeit; 28353541Sshin 284121472Sume /* 285121472Sume * RFC 2463, 2.4 (e.5): source address check. 286121472Sume * XXX: the case of anycast source? 287121472Sume */ 28853541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || 28953541Sshin IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) 29053541Sshin goto freeit; 29153541Sshin 29253541Sshin /* 29362587Sitojun * If we are about to send ICMPv6 against ICMPv6 error/redirect, 29462587Sitojun * don't do it. 29553541Sshin */ 29662587Sitojun nxt = -1; 29762587Sitojun off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 29862587Sitojun if (off >= 0 && nxt == IPPROTO_ICMPV6) { 29953541Sshin struct icmp6_hdr *icp; 30053541Sshin 30162587Sitojun#ifndef PULLDOWN_TEST 30262587Sitojun IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), ); 30362587Sitojun icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 30462587Sitojun#else 30562587Sitojun IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, 30662587Sitojun sizeof(*icp)); 30762587Sitojun if (icp == NULL) { 30862587Sitojun icmp6stat.icp6s_tooshort++; 30962587Sitojun return; 31062587Sitojun } 31162587Sitojun#endif 31262587Sitojun if (icp->icmp6_type < ICMP6_ECHO_REQUEST || 31362587Sitojun icp->icmp6_type == ND_REDIRECT) { 31453541Sshin /* 31562587Sitojun * ICMPv6 error 31662587Sitojun * Special case: for redirect (which is 31762587Sitojun * informational) we must not send icmp6 error. 31853541Sshin */ 31962587Sitojun icmp6stat.icp6s_canterror++; 32062587Sitojun goto freeit; 32162587Sitojun } else { 32262587Sitojun /* ICMPv6 informational - send the error */ 32353541Sshin } 32462587Sitojun } else { 32562587Sitojun /* non-ICMPv6 - send the error */ 32653541Sshin } 32753541Sshin 32853541Sshin oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */ 32953541Sshin 33053541Sshin /* Finally, do rate limitation check. */ 33153541Sshin if (icmp6_ratelimit(&oip6->ip6_src, type, code)) { 33253541Sshin icmp6stat.icp6s_toofreq++; 33353541Sshin goto freeit; 33453541Sshin } 33553541Sshin 33653541Sshin /* 33753541Sshin * OK, ICMP6 can be generated. 33853541Sshin */ 33953541Sshin 34053541Sshin if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) 34153541Sshin m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); 34253541Sshin 34362587Sitojun preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 344111119Simp M_PREPEND(m, preplen, M_DONTWAIT); 34562587Sitojun if (m && m->m_len < preplen) 34662587Sitojun m = m_pullup(m, preplen); 34753541Sshin if (m == NULL) { 34878064Sume nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__)); 34953541Sshin return; 35053541Sshin } 35153541Sshin 35253541Sshin nip6 = mtod(m, struct ip6_hdr *); 35353541Sshin nip6->ip6_src = oip6->ip6_src; 35453541Sshin nip6->ip6_dst = oip6->ip6_dst; 35553541Sshin 356121315Sume in6_clearscope(&oip6->ip6_src); 357121315Sume in6_clearscope(&oip6->ip6_dst); 35853541Sshin 35953541Sshin icmp6 = (struct icmp6_hdr *)(nip6 + 1); 36053541Sshin icmp6->icmp6_type = type; 36153541Sshin icmp6->icmp6_code = code; 36253541Sshin icmp6->icmp6_pptr = htonl((u_int32_t)param); 36353541Sshin 36478064Sume /* 36578064Sume * icmp6_reflect() is designed to be in the input path. 366148987Sume * icmp6_error() can be called from both input and output path, 36778064Sume * and if we are in output path rcvif could contain bogus value. 36878064Sume * clear m->m_pkthdr.rcvif for safety, we should have enough scope 36978064Sume * information in ip header (nip6). 37078064Sume */ 37178064Sume m->m_pkthdr.rcvif = NULL; 37278064Sume 37353541Sshin icmp6stat.icp6s_outhist[type]++; 37495023Ssuz icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ 37562587Sitojun 37662587Sitojun return; 37762587Sitojun 37862587Sitojun freeit: 37962587Sitojun /* 380148987Sume * If we can't tell whether or not we can generate ICMP6, free it. 38162587Sitojun */ 38262587Sitojun m_freem(m); 38353541Sshin} 38453541Sshin 38553541Sshin/* 38653541Sshin * Process a received ICMP6 message. 38753541Sshin */ 38853541Sshinint 389171259Sdelphijicmp6_input(struct mbuf **mp, int *offp, int proto) 39053541Sshin{ 39153541Sshin struct mbuf *m = *mp, *n; 39253541Sshin struct ip6_hdr *ip6, *nip6; 39353541Sshin struct icmp6_hdr *icmp6, *nicmp6; 39453541Sshin int off = *offp; 39553541Sshin int icmp6len = m->m_pkthdr.len - *offp; 39653541Sshin int code, sum, noff; 397165118Sbz char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 39853541Sshin 39962587Sitojun#ifndef PULLDOWN_TEST 40053541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE); 40195023Ssuz /* m might change if M_LOOP. So, call mtod after this */ 40262587Sitojun#endif 40353541Sshin 40453541Sshin /* 40553541Sshin * Locate icmp6 structure in mbuf, and check 40653541Sshin * that not corrupted and of at least minimum length 40753541Sshin */ 40853541Sshin 40953541Sshin ip6 = mtod(m, struct ip6_hdr *); 41053541Sshin if (icmp6len < sizeof(struct icmp6_hdr)) { 41153541Sshin icmp6stat.icp6s_tooshort++; 41253541Sshin goto freeit; 41353541Sshin } 41453541Sshin 41553541Sshin /* 41653541Sshin * calculate the checksum 41753541Sshin */ 41862587Sitojun#ifndef PULLDOWN_TEST 41953541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 42062587Sitojun#else 42162587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 42262587Sitojun if (icmp6 == NULL) { 42362587Sitojun icmp6stat.icp6s_tooshort++; 42462587Sitojun return IPPROTO_DONE; 42562587Sitojun } 42662587Sitojun#endif 42753541Sshin code = icmp6->icmp6_code; 42853541Sshin 42953541Sshin if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { 43078064Sume nd6log((LOG_ERR, 43153541Sshin "ICMP6 checksum error(%d|%x) %s\n", 432165118Sbz icmp6->icmp6_type, sum, 433165118Sbz ip6_sprintf(ip6bufs, &ip6->ip6_src))); 43453541Sshin icmp6stat.icp6s_checksum++; 43553541Sshin goto freeit; 43653541Sshin } 43753541Sshin 43883934Sbrooks if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 43953541Sshin /* 44053541Sshin * Deliver very specific ICMP6 type only. 441148987Sume * This is important to deliver TOOBIG. Otherwise PMTUD 44253541Sshin * will not work. 44353541Sshin */ 44453541Sshin switch (icmp6->icmp6_type) { 44553541Sshin case ICMP6_DST_UNREACH: 44653541Sshin case ICMP6_PACKET_TOO_BIG: 44753541Sshin case ICMP6_TIME_EXCEEDED: 44853541Sshin break; 44953541Sshin default: 45053541Sshin goto freeit; 45153541Sshin } 45253541Sshin } 45353541Sshin 45453541Sshin icmp6stat.icp6s_inhist[icmp6->icmp6_type]++; 45553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg); 45653541Sshin if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) 45753541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error); 45853541Sshin 45953541Sshin switch (icmp6->icmp6_type) { 46053541Sshin case ICMP6_DST_UNREACH: 46153541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach); 46253541Sshin switch (code) { 46353541Sshin case ICMP6_DST_UNREACH_NOROUTE: 46453541Sshin code = PRC_UNREACH_NET; 46553541Sshin break; 46653541Sshin case ICMP6_DST_UNREACH_ADMIN: 46753541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib); 46862587Sitojun code = PRC_UNREACH_PROTOCOL; /* is this a good code? */ 46962587Sitojun break; 47053541Sshin case ICMP6_DST_UNREACH_ADDR: 47162587Sitojun code = PRC_HOSTDEAD; 47253541Sshin break; 47362587Sitojun case ICMP6_DST_UNREACH_BEYONDSCOPE: 47462587Sitojun /* I mean "source address was incorrect." */ 47562587Sitojun code = PRC_PARAMPROB; 47662587Sitojun break; 47753541Sshin case ICMP6_DST_UNREACH_NOPORT: 47853541Sshin code = PRC_UNREACH_PORT; 47953541Sshin break; 48053541Sshin default: 48153541Sshin goto badcode; 48253541Sshin } 48353541Sshin goto deliver; 48453541Sshin break; 48553541Sshin 48653541Sshin case ICMP6_PACKET_TOO_BIG: 48753541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig); 48853541Sshin 489126194Sume /* validation is made in icmp6_mtudisc_update */ 490126194Sume 49153541Sshin code = PRC_MSGSIZE; 49253541Sshin 49362587Sitojun /* 49462587Sitojun * Updating the path MTU will be done after examining 49562587Sitojun * intermediate extension headers. 49662587Sitojun */ 49753541Sshin goto deliver; 49853541Sshin break; 49953541Sshin 50053541Sshin case ICMP6_TIME_EXCEEDED: 50153541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed); 50253541Sshin switch (code) { 50353541Sshin case ICMP6_TIME_EXCEED_TRANSIT: 504151465Ssuz code = PRC_TIMXCEED_INTRANS; 505151465Ssuz break; 50653541Sshin case ICMP6_TIME_EXCEED_REASSEMBLY: 507151465Ssuz code = PRC_TIMXCEED_REASS; 50853541Sshin break; 50953541Sshin default: 51053541Sshin goto badcode; 51153541Sshin } 51253541Sshin goto deliver; 51353541Sshin break; 51453541Sshin 51553541Sshin case ICMP6_PARAM_PROB: 51653541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob); 51753541Sshin switch (code) { 51853541Sshin case ICMP6_PARAMPROB_NEXTHEADER: 51953541Sshin code = PRC_UNREACH_PROTOCOL; 52053541Sshin break; 52153541Sshin case ICMP6_PARAMPROB_HEADER: 52253541Sshin case ICMP6_PARAMPROB_OPTION: 52353541Sshin code = PRC_PARAMPROB; 52453541Sshin break; 52553541Sshin default: 52653541Sshin goto badcode; 52753541Sshin } 52853541Sshin goto deliver; 52953541Sshin break; 53053541Sshin 53153541Sshin case ICMP6_ECHO_REQUEST: 53253541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo); 53353541Sshin if (code != 0) 53453541Sshin goto badcode; 53553541Sshin if ((n = m_copy(m, 0, M_COPYALL)) == NULL) { 53653541Sshin /* Give up remote */ 53753541Sshin break; 53853541Sshin } 53962587Sitojun if ((n->m_flags & M_EXT) != 0 54062587Sitojun || n->m_len < off + sizeof(struct icmp6_hdr)) { 54153541Sshin struct mbuf *n0 = n; 54262587Sitojun const int maxlen = sizeof(*nip6) + sizeof(*nicmp6); 543108741Ssam int n0len; 54453541Sshin 545111119Simp MGETHDR(n, M_DONTWAIT, n0->m_type); 546111070Ssam n0len = n0->m_pkthdr.len; /* save for use below */ 547111070Ssam if (n) 548111070Ssam M_MOVE_PKTHDR(n, n0); 54962587Sitojun if (n && maxlen >= MHLEN) { 550111119Simp MCLGET(n, M_DONTWAIT); 55162587Sitojun if ((n->m_flags & M_EXT) == 0) { 55262587Sitojun m_free(n); 55362587Sitojun n = NULL; 55462587Sitojun } 55562587Sitojun } 55653541Sshin if (n == NULL) { 55753541Sshin /* Give up remote */ 55853541Sshin m_freem(n0); 55953541Sshin break; 56053541Sshin } 56153541Sshin /* 56253541Sshin * Copy IPv6 and ICMPv6 only. 56353541Sshin */ 56453541Sshin nip6 = mtod(n, struct ip6_hdr *); 56553541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 56653541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 56753541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 56862587Sitojun noff = sizeof(struct ip6_hdr); 569108741Ssam /* new mbuf contains only ipv6+icmpv6 headers */ 570108741Ssam n->m_len = noff + sizeof(struct icmp6_hdr); 57153541Sshin /* 572120891Sume * Adjust mbuf. ip6_plen will be adjusted in 57362587Sitojun * ip6_output(). 57453541Sshin */ 57562587Sitojun m_adj(n0, off + sizeof(struct icmp6_hdr)); 576108741Ssam /* recalculate complete packet size */ 577108741Ssam n->m_pkthdr.len = n0len + (noff - off); 57862587Sitojun n->m_next = n0; 57953541Sshin } else { 58053541Sshin nip6 = mtod(n, struct ip6_hdr *); 581166046Sume IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off, 582166046Sume sizeof(*nicmp6)); 58353541Sshin noff = off; 58453541Sshin } 58553541Sshin nicmp6->icmp6_type = ICMP6_ECHO_REPLY; 58653541Sshin nicmp6->icmp6_code = 0; 58753541Sshin if (n) { 58853541Sshin icmp6stat.icp6s_reflect++; 58953541Sshin icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++; 59053541Sshin icmp6_reflect(n, noff); 59153541Sshin } 59253541Sshin break; 59353541Sshin 59453541Sshin case ICMP6_ECHO_REPLY: 59553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply); 59653541Sshin if (code != 0) 59753541Sshin goto badcode; 59853541Sshin break; 59953541Sshin 60096116Sume case MLD_LISTENER_QUERY: 60196116Sume case MLD_LISTENER_REPORT: 60296116Sume if (icmp6len < sizeof(struct mld_hdr)) 60353541Sshin goto badlen; 60496116Sume if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */ 60553541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery); 60653541Sshin else 60753541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport); 608111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 60962587Sitojun /* give up local */ 61062587Sitojun mld6_input(m, off); 61162587Sitojun m = NULL; 61262587Sitojun goto freeit; 61362587Sitojun } 61462587Sitojun mld6_input(n, off); 61553541Sshin /* m stays. */ 61653541Sshin break; 61753541Sshin 61896116Sume case MLD_LISTENER_DONE: 61953541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone); 62096116Sume if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */ 62153541Sshin goto badlen; 62253541Sshin break; /* nothing to be done in kernel */ 62353541Sshin 62496116Sume case MLD_MTRACE_RESP: 62596116Sume case MLD_MTRACE: 626120891Sume /* XXX: these two are experimental. not officially defined. */ 62753541Sshin /* XXX: per-interface statistics? */ 62862587Sitojun break; /* just pass it to applications */ 62953541Sshin 63053541Sshin case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ 63153541Sshin { 63253541Sshin enum { WRU, FQDN } mode; 63353541Sshin 63462587Sitojun if (!icmp6_nodeinfo) 63562587Sitojun break; 63662587Sitojun 63753541Sshin if (icmp6len == sizeof(struct icmp6_hdr) + 4) 63853541Sshin mode = WRU; 63962587Sitojun else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) 64053541Sshin mode = FQDN; 64153541Sshin else 64253541Sshin goto badlen; 64353541Sshin 64453541Sshin#define hostnamelen strlen(hostname) 64553541Sshin if (mode == FQDN) { 64662587Sitojun#ifndef PULLDOWN_TEST 64753541Sshin IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), 648120891Sume IPPROTO_DONE); 64962587Sitojun#endif 65062587Sitojun n = m_copy(m, 0, M_COPYALL); 65162587Sitojun if (n) 65262587Sitojun n = ni6_input(n, off); 65362587Sitojun /* XXX meaningless if n == NULL */ 65453541Sshin noff = sizeof(struct ip6_hdr); 65553541Sshin } else { 65653541Sshin u_char *p; 65762587Sitojun int maxlen, maxhlen; 65853541Sshin 659169664Sjinmei /* 660169664Sjinmei * XXX: this combination of flags is pointless, 661169664Sjinmei * but should we keep this for compatibility? 662169664Sjinmei */ 663120891Sume if ((icmp6_nodeinfo & 5) != 5) 66478064Sume break; 66578064Sume 66662587Sitojun if (code != 0) 66762587Sitojun goto badcode; 66862587Sitojun maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4; 66962587Sitojun if (maxlen >= MCLBYTES) { 67062587Sitojun /* Give up remote */ 67162587Sitojun break; 67262587Sitojun } 673111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 67462587Sitojun if (n && maxlen > MHLEN) { 675111119Simp MCLGET(n, M_DONTWAIT); 67662587Sitojun if ((n->m_flags & M_EXT) == 0) { 67762587Sitojun m_free(n); 67862587Sitojun n = NULL; 67962587Sitojun } 68062587Sitojun } 681166619Sbms if (n && !m_dup_pkthdr(n, m, M_DONTWAIT)) { 682108466Ssam /* 683108466Ssam * Previous code did a blind M_COPY_PKTHDR 684108466Ssam * and said "just for rcvif". If true, then 685108466Ssam * we could tolerate the dup failing (due to 686108466Ssam * the deep copy of the tag chain). For now 687108466Ssam * be conservative and just fail. 688108466Ssam */ 689108466Ssam m_free(n); 690108466Ssam n = NULL; 691108466Ssam } 69253541Sshin if (n == NULL) { 69353541Sshin /* Give up remote */ 69453541Sshin break; 69553541Sshin } 69678064Sume n->m_pkthdr.rcvif = NULL; 69762587Sitojun n->m_len = 0; 69862587Sitojun maxhlen = M_TRAILINGSPACE(n) - maxlen; 69962587Sitojun if (maxhlen > hostnamelen) 70062587Sitojun maxhlen = hostnamelen; 70153541Sshin /* 70253541Sshin * Copy IPv6 and ICMPv6 only. 70353541Sshin */ 70453541Sshin nip6 = mtod(n, struct ip6_hdr *); 70553541Sshin bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 70653541Sshin nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 70753541Sshin bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 70853541Sshin p = (u_char *)(nicmp6 + 1); 70953541Sshin bzero(p, 4); 71095023Ssuz bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */ 71153541Sshin noff = sizeof(struct ip6_hdr); 71253541Sshin n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 71362587Sitojun sizeof(struct icmp6_hdr) + 4 + maxhlen; 71453541Sshin nicmp6->icmp6_type = ICMP6_WRUREPLY; 71553541Sshin nicmp6->icmp6_code = 0; 71653541Sshin } 71753541Sshin#undef hostnamelen 71853541Sshin if (n) { 71953541Sshin icmp6stat.icp6s_reflect++; 72053541Sshin icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++; 72153541Sshin icmp6_reflect(n, noff); 72253541Sshin } 72353541Sshin break; 72453541Sshin } 72553541Sshin 72653541Sshin case ICMP6_WRUREPLY: 72753541Sshin if (code != 0) 72853541Sshin goto badcode; 72953541Sshin break; 73053541Sshin 73153541Sshin case ND_ROUTER_SOLICIT: 73253541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit); 73353541Sshin if (code != 0) 73453541Sshin goto badcode; 73553541Sshin if (icmp6len < sizeof(struct nd_router_solicit)) 73653541Sshin goto badlen; 737111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 73862587Sitojun /* give up local */ 73962587Sitojun nd6_rs_input(m, off, icmp6len); 74062587Sitojun m = NULL; 74162587Sitojun goto freeit; 74262587Sitojun } 74362587Sitojun nd6_rs_input(n, off, icmp6len); 74453541Sshin /* m stays. */ 74553541Sshin break; 74653541Sshin 74753541Sshin case ND_ROUTER_ADVERT: 74853541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert); 74953541Sshin if (code != 0) 75053541Sshin goto badcode; 75153541Sshin if (icmp6len < sizeof(struct nd_router_advert)) 75253541Sshin goto badlen; 753111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 75462587Sitojun /* give up local */ 75562587Sitojun nd6_ra_input(m, off, icmp6len); 75662587Sitojun m = NULL; 75762587Sitojun goto freeit; 75862587Sitojun } 75962587Sitojun nd6_ra_input(n, off, icmp6len); 76053541Sshin /* m stays. */ 76153541Sshin break; 76253541Sshin 76353541Sshin case ND_NEIGHBOR_SOLICIT: 76453541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit); 76553541Sshin if (code != 0) 76653541Sshin goto badcode; 76753541Sshin if (icmp6len < sizeof(struct nd_neighbor_solicit)) 76853541Sshin goto badlen; 769111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 77062587Sitojun /* give up local */ 77162587Sitojun nd6_ns_input(m, off, icmp6len); 77262587Sitojun m = NULL; 77362587Sitojun goto freeit; 77462587Sitojun } 77562587Sitojun nd6_ns_input(n, off, icmp6len); 77653541Sshin /* m stays. */ 77753541Sshin break; 77853541Sshin 77953541Sshin case ND_NEIGHBOR_ADVERT: 78053541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert); 78153541Sshin if (code != 0) 78253541Sshin goto badcode; 78353541Sshin if (icmp6len < sizeof(struct nd_neighbor_advert)) 78453541Sshin goto badlen; 785111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 78662587Sitojun /* give up local */ 78762587Sitojun nd6_na_input(m, off, icmp6len); 78862587Sitojun m = NULL; 78962587Sitojun goto freeit; 79062587Sitojun } 79162587Sitojun nd6_na_input(n, off, icmp6len); 79253541Sshin /* m stays. */ 79353541Sshin break; 79453541Sshin 79553541Sshin case ND_REDIRECT: 79653541Sshin icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect); 79753541Sshin if (code != 0) 79853541Sshin goto badcode; 79953541Sshin if (icmp6len < sizeof(struct nd_redirect)) 80053541Sshin goto badlen; 801111119Simp if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 80262587Sitojun /* give up local */ 80362587Sitojun icmp6_redirect_input(m, off); 80462587Sitojun m = NULL; 80562587Sitojun goto freeit; 80662587Sitojun } 80762587Sitojun icmp6_redirect_input(n, off); 80853541Sshin /* m stays. */ 80953541Sshin break; 81053541Sshin 81153541Sshin case ICMP6_ROUTER_RENUMBERING: 81253541Sshin if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && 81353541Sshin code != ICMP6_ROUTER_RENUMBERING_RESULT) 81453541Sshin goto badcode; 81553541Sshin if (icmp6len < sizeof(struct icmp6_router_renum)) 81653541Sshin goto badlen; 81753541Sshin break; 81853541Sshin 81953541Sshin default: 82078064Sume nd6log((LOG_DEBUG, 82178064Sume "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", 822165118Sbz icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src), 823165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst), 82478064Sume m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0)); 82553541Sshin if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { 82653541Sshin /* ICMPv6 error: MUST deliver it by spec... */ 82753541Sshin code = PRC_NCMDS; 82853541Sshin /* deliver */ 82953541Sshin } else { 83053541Sshin /* ICMPv6 informational: MUST not deliver */ 83153541Sshin break; 83253541Sshin } 83353541Sshin deliver: 834142681Sume if (icmp6_notify_error(&m, off, icmp6len, code)) { 83578064Sume /* In this case, m should've been freed. */ 836120856Sume return (IPPROTO_DONE); 83753541Sshin } 83878064Sume break; 83978064Sume 84078064Sume badcode: 84178064Sume icmp6stat.icp6s_badcode++; 84278064Sume break; 84378064Sume 84478064Sume badlen: 84578064Sume icmp6stat.icp6s_badlen++; 84678064Sume break; 84778064Sume } 84878064Sume 84978064Sume /* deliver the packet to appropriate sockets */ 85078064Sume icmp6_rip6_input(&m, *offp); 85178064Sume 85278064Sume return IPPROTO_DONE; 85378064Sume 85478064Sume freeit: 85578064Sume m_freem(m); 85678064Sume return IPPROTO_DONE; 85778064Sume} 85878064Sume 85978064Sumestatic int 860171259Sdelphijicmp6_notify_error(struct mbuf **mp, int off, int icmp6len, int code) 86178064Sume{ 862142681Sume struct mbuf *m = *mp; 86378064Sume struct icmp6_hdr *icmp6; 86478064Sume struct ip6_hdr *eip6; 86578064Sume u_int32_t notifymtu; 86678064Sume struct sockaddr_in6 icmp6src, icmp6dst; 86778064Sume 86878064Sume if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { 86978064Sume icmp6stat.icp6s_tooshort++; 87078064Sume goto freeit; 87178064Sume } 87262587Sitojun#ifndef PULLDOWN_TEST 87378064Sume IP6_EXTHDR_CHECK(m, off, 874120891Sume sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1); 87578064Sume icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 87662587Sitojun#else 87778064Sume IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 878120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 87978064Sume if (icmp6 == NULL) { 88078064Sume icmp6stat.icp6s_tooshort++; 881120856Sume return (-1); 88278064Sume } 88362587Sitojun#endif 88478064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 88553541Sshin 88678064Sume /* Detect the upper level protocol */ 88778064Sume { 888175162Sobrien void (*ctlfunc)(int, struct sockaddr *, void *); 88953541Sshin u_int8_t nxt = eip6->ip6_nxt; 89053541Sshin int eoff = off + sizeof(struct icmp6_hdr) + 891120891Sume sizeof(struct ip6_hdr); 89253541Sshin struct ip6ctlparam ip6cp; 89362587Sitojun struct in6_addr *finaldst = NULL; 89462587Sitojun int icmp6type = icmp6->icmp6_type; 89562587Sitojun struct ip6_frag *fh; 89662587Sitojun struct ip6_rthdr *rth; 89762587Sitojun struct ip6_rthdr0 *rth0; 89862587Sitojun int rthlen; 89953541Sshin 90095023Ssuz while (1) { /* XXX: should avoid infinite loop explicitly? */ 90153541Sshin struct ip6_ext *eh; 90253541Sshin 90378064Sume switch (nxt) { 90453541Sshin case IPPROTO_HOPOPTS: 90553541Sshin case IPPROTO_DSTOPTS: 90653541Sshin case IPPROTO_AH: 90762587Sitojun#ifndef PULLDOWN_TEST 908120891Sume IP6_EXTHDR_CHECK(m, 0, 909120891Sume eoff + sizeof(struct ip6_ext), -1); 910120892Sume eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff); 91162587Sitojun#else 91262587Sitojun IP6_EXTHDR_GET(eh, struct ip6_ext *, m, 913120891Sume eoff, sizeof(*eh)); 91462587Sitojun if (eh == NULL) { 91562587Sitojun icmp6stat.icp6s_tooshort++; 916120856Sume return (-1); 91762587Sitojun } 91862587Sitojun#endif 919120891Sume 92053541Sshin if (nxt == IPPROTO_AH) 92153541Sshin eoff += (eh->ip6e_len + 2) << 2; 92253541Sshin else 92353541Sshin eoff += (eh->ip6e_len + 1) << 3; 92453541Sshin nxt = eh->ip6e_nxt; 92553541Sshin break; 92662587Sitojun case IPPROTO_ROUTING: 92762587Sitojun /* 92862587Sitojun * When the erroneous packet contains a 92962587Sitojun * routing header, we should examine the 93062587Sitojun * header to determine the final destination. 93162587Sitojun * Otherwise, we can't properly update 93262587Sitojun * information that depends on the final 93362587Sitojun * destination (e.g. path MTU). 93462587Sitojun */ 93562587Sitojun#ifndef PULLDOWN_TEST 936120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1); 937120891Sume rth = (struct ip6_rthdr *) 938120891Sume (mtod(m, caddr_t) + eoff); 93962587Sitojun#else 94062587Sitojun IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m, 941120891Sume eoff, sizeof(*rth)); 94262587Sitojun if (rth == NULL) { 94362587Sitojun icmp6stat.icp6s_tooshort++; 944120856Sume return (-1); 94562587Sitojun } 94662587Sitojun#endif 94762587Sitojun rthlen = (rth->ip6r_len + 1) << 3; 94862587Sitojun /* 94962587Sitojun * XXX: currently there is no 95062587Sitojun * officially defined type other 95162587Sitojun * than type-0. 95262587Sitojun * Note that if the segment left field 95362587Sitojun * is 0, all intermediate hops must 95462587Sitojun * have been passed. 95562587Sitojun */ 95662587Sitojun if (rth->ip6r_segleft && 95762587Sitojun rth->ip6r_type == IPV6_RTHDR_TYPE_0) { 95862587Sitojun int hops; 95962587Sitojun 96062587Sitojun#ifndef PULLDOWN_TEST 961120891Sume IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1); 962120891Sume rth0 = (struct ip6_rthdr0 *) 963120891Sume (mtod(m, caddr_t) + eoff); 96462587Sitojun#else 96562587Sitojun IP6_EXTHDR_GET(rth0, 966120891Sume struct ip6_rthdr0 *, m, 967120891Sume eoff, rthlen); 96862587Sitojun if (rth0 == NULL) { 96962587Sitojun icmp6stat.icp6s_tooshort++; 970120856Sume return (-1); 97162587Sitojun } 97262587Sitojun#endif 97362587Sitojun /* just ignore a bogus header */ 97462587Sitojun if ((rth0->ip6r0_len % 2) == 0 && 97562587Sitojun (hops = rth0->ip6r0_len/2)) 97662587Sitojun finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1); 97762587Sitojun } 97862587Sitojun eoff += rthlen; 97962587Sitojun nxt = rth->ip6r_nxt; 98062587Sitojun break; 98162587Sitojun case IPPROTO_FRAGMENT: 98262587Sitojun#ifndef PULLDOWN_TEST 98362587Sitojun IP6_EXTHDR_CHECK(m, 0, eoff + 984120891Sume sizeof(struct ip6_frag), -1); 985120891Sume fh = (struct ip6_frag *)(mtod(m, caddr_t) + 986120891Sume eoff); 98762587Sitojun#else 98862587Sitojun IP6_EXTHDR_GET(fh, struct ip6_frag *, m, 989120891Sume eoff, sizeof(*fh)); 99062587Sitojun if (fh == NULL) { 99162587Sitojun icmp6stat.icp6s_tooshort++; 992120856Sume return (-1); 99362587Sitojun } 99462587Sitojun#endif 99562587Sitojun /* 99662587Sitojun * Data after a fragment header is meaningless 99762587Sitojun * unless it is the first fragment, but 99862587Sitojun * we'll go to the notify label for path MTU 99962587Sitojun * discovery. 100062587Sitojun */ 100162587Sitojun if (fh->ip6f_offlg & IP6F_OFF_MASK) 100262587Sitojun goto notify; 100362587Sitojun 100462587Sitojun eoff += sizeof(struct ip6_frag); 100562587Sitojun nxt = fh->ip6f_nxt; 100662587Sitojun break; 100753541Sshin default: 100862587Sitojun /* 100962587Sitojun * This case includes ESP and the No Next 101095023Ssuz * Header. In such cases going to the notify 101162587Sitojun * label does not have any meaning 101262587Sitojun * (i.e. ctlfunc will be NULL), but we go 101362587Sitojun * anyway since we might have to update 101462587Sitojun * path MTU information. 101562587Sitojun */ 101653541Sshin goto notify; 101753541Sshin } 101853541Sshin } 101978064Sume notify: 102062587Sitojun#ifndef PULLDOWN_TEST 102153541Sshin icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 102262587Sitojun#else 102362587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 1024120891Sume sizeof(*icmp6) + sizeof(struct ip6_hdr)); 102562587Sitojun if (icmp6 == NULL) { 102662587Sitojun icmp6stat.icp6s_tooshort++; 1027120856Sume return (-1); 102862587Sitojun } 102962587Sitojun#endif 103078064Sume 1031120891Sume /* 1032120891Sume * retrieve parameters from the inner IPv6 header, and convert 1033120891Sume * them into sockaddr structures. 1034120891Sume * XXX: there is no guarantee that the source or destination 1035120891Sume * addresses of the inner packet are in the same scope as 1036120891Sume * the addresses of the icmp packet. But there is no other 1037120891Sume * way to determine the zone. 1038120891Sume */ 103978064Sume eip6 = (struct ip6_hdr *)(icmp6 + 1); 1040120891Sume 104178064Sume bzero(&icmp6dst, sizeof(icmp6dst)); 104278064Sume icmp6dst.sin6_len = sizeof(struct sockaddr_in6); 104378064Sume icmp6dst.sin6_family = AF_INET6; 104478064Sume if (finaldst == NULL) 104578064Sume icmp6dst.sin6_addr = eip6->ip6_dst; 104678064Sume else 104778064Sume icmp6dst.sin6_addr = *finaldst; 1048148385Sume if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL)) 1049121315Sume goto freeit; 105078064Sume bzero(&icmp6src, sizeof(icmp6src)); 105178064Sume icmp6src.sin6_len = sizeof(struct sockaddr_in6); 105278064Sume icmp6src.sin6_family = AF_INET6; 105378064Sume icmp6src.sin6_addr = eip6->ip6_src; 1054148385Sume if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL)) 1055121315Sume goto freeit; 1056148385Sume icmp6src.sin6_flowinfo = 1057148385Sume (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); 105878064Sume 105978064Sume if (finaldst == NULL) 106078064Sume finaldst = &eip6->ip6_dst; 106178064Sume ip6cp.ip6c_m = m; 106278064Sume ip6cp.ip6c_icmp6 = icmp6; 106378064Sume ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1); 106478064Sume ip6cp.ip6c_off = eoff; 106578064Sume ip6cp.ip6c_finaldst = finaldst; 106678064Sume ip6cp.ip6c_src = &icmp6src; 106778064Sume ip6cp.ip6c_nxt = nxt; 106878064Sume 106962587Sitojun if (icmp6type == ICMP6_PACKET_TOO_BIG) { 107078064Sume notifymtu = ntohl(icmp6->icmp6_mtu); 107178064Sume ip6cp.ip6c_cmdarg = (void *)¬ifymtu; 107278064Sume icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ 107362587Sitojun } 107462587Sitojun 1075175162Sobrien ctlfunc = (void (*)(int, struct sockaddr *, void *)) 1076120891Sume (inet6sw[ip6_protox[nxt]].pr_ctlinput); 107753541Sshin if (ctlfunc) { 107878064Sume (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, 1079120891Sume &ip6cp); 108053541Sshin } 108153541Sshin } 1082142681Sume *mp = m; 1083120856Sume return (0); 108453541Sshin 108578064Sume freeit: 108653541Sshin m_freem(m); 1087120856Sume return (-1); 108853541Sshin} 108953541Sshin 109078064Sumevoid 1091171259Sdelphijicmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated) 109262587Sitojun{ 109378064Sume struct in6_addr *dst = ip6cp->ip6c_finaldst; 109478064Sume struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; 109578064Sume struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ 109662587Sitojun u_int mtu = ntohl(icmp6->icmp6_mtu); 1097122922Sandre struct in_conninfo inc; 109862587Sitojun 1099121472Sume#if 0 1100121472Sume /* 1101121472Sume * RFC2460 section 5, last paragraph. 1102121472Sume * even though minimum link MTU for IPv6 is IPV6_MMTU, 1103121472Sume * we may see ICMPv6 too big with mtu < IPV6_MMTU 1104121472Sume * due to packet translator in the middle. 1105121472Sume * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for 1106121472Sume * special handling. 1107121472Sume */ 1108121472Sume if (mtu < IPV6_MMTU) 1109121472Sume return; 1110121472Sume#endif 1111121472Sume 1112121472Sume /* 1113121472Sume * we reject ICMPv6 too big with abnormally small value. 1114121472Sume * XXX what is the good definition of "abnormally small"? 1115121472Sume */ 1116121472Sume if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) 1117121472Sume return; 1118121472Sume 111978064Sume if (!validated) 112078064Sume return; 112178064Sume 1122122922Sandre bzero(&inc, sizeof(inc)); 1123122922Sandre inc.inc_flags = 1; /* IPv6 */ 1124122922Sandre inc.inc6_faddr = *dst; 1125148385Sume if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL)) 1126148385Sume return; 112762587Sitojun 1128162084Sandre if (mtu < tcp_maxmtu6(&inc, NULL)) { 1129122922Sandre tcp_hc_updatemtu(&inc, mtu); 1130122922Sandre icmp6stat.icp6s_pmtuchg++; 113162587Sitojun } 113262587Sitojun} 113362587Sitojun 113453541Sshin/* 113578064Sume * Process a Node Information Query packet, based on 113678064Sume * draft-ietf-ipngwg-icmp-name-lookups-07. 1137120891Sume * 113862587Sitojun * Spec incompatibilities: 113962587Sitojun * - IPv6 Subject address handling 114062587Sitojun * - IPv4 Subject address handling support missing 114162587Sitojun * - Proxy reply (answer even if it's not for me) 114262587Sitojun * - joins NI group address at in6_ifattach() time only, does not cope 114362587Sitojun * with hostname changes by sethostname(3) 114453541Sshin */ 114562587Sitojun#define hostnamelen strlen(hostname) 114653541Sshinstatic struct mbuf * 1147171259Sdelphijni6_input(struct mbuf *m, int off) 114853541Sshin{ 114962587Sitojun struct icmp6_nodeinfo *ni6, *nni6; 115053541Sshin struct mbuf *n = NULL; 115162587Sitojun u_int16_t qtype; 115262587Sitojun int subjlen; 115353541Sshin int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 115453541Sshin struct ni_reply_fqdn *fqdn; 115553541Sshin int addrs; /* for NI_QTYPE_NODEADDR */ 115653541Sshin struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ 1157148385Sume struct in6_addr in6_subj; /* subject address */ 115862587Sitojun struct ip6_hdr *ip6; 115962587Sitojun int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ 116078064Sume char *subj = NULL; 116178064Sume struct in6_ifaddr *ia6 = NULL; 116253541Sshin 116362587Sitojun ip6 = mtod(m, struct ip6_hdr *); 116462587Sitojun#ifndef PULLDOWN_TEST 116562587Sitojun ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); 116662587Sitojun#else 116762587Sitojun IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); 116862587Sitojun if (ni6 == NULL) { 116962587Sitojun /* m is already reclaimed */ 1170120891Sume return (NULL); 117162587Sitojun } 117262587Sitojun#endif 117353541Sshin 117462587Sitojun /* 1175169664Sjinmei * Validate IPv6 source address. 1176169664Sjinmei * The default configuration MUST be to refuse answering queries from 1177169664Sjinmei * global-scope addresses according to RFC4602. 1178169664Sjinmei * Notes: 1179169664Sjinmei * - it's not very clear what "refuse" means; this implementation 1180169664Sjinmei * simply drops it. 1181169664Sjinmei * - it's not very easy to identify global-scope (unicast) addresses 1182169664Sjinmei * since there are many prefixes for them. It should be safer 1183169664Sjinmei * and in practice sufficient to check "all" but loopback and 1184169664Sjinmei * link-local (note that site-local unicast was deprecated and 1185169664Sjinmei * ULA is defined as global scope-wise) 1186169664Sjinmei */ 1187169664Sjinmei if ((icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 && 1188169664Sjinmei !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) && 1189169664Sjinmei !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) 1190169664Sjinmei goto bad; 1191169664Sjinmei 1192169664Sjinmei /* 119362587Sitojun * Validate IPv6 destination address. 119462587Sitojun * 119578064Sume * The Responder must discard the Query without further processing 119678064Sume * unless it is one of the Responder's unicast or anycast addresses, or 119778064Sume * a link-local scope multicast address which the Responder has joined. 1198169664Sjinmei * [RFC4602, Section 5.] 119962587Sitojun */ 1200121630Sume if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1201121630Sume if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) 1202121630Sume goto bad; 1203121630Sume /* else it's a link-local multicast, fine */ 1204121630Sume } else { /* unicast or anycast */ 1205121630Sume if ((ia6 = ip6_getdstifaddr(m)) == NULL) 1206121630Sume goto bad; /* XXX impossible */ 1207121630Sume 1208121630Sume if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) && 1209169664Sjinmei !(icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) { 121078064Sume nd6log((LOG_DEBUG, "ni6_input: ignore node info to " 121178064Sume "a temporary address in %s:%d", 121278064Sume __FILE__, __LINE__)); 121378064Sume goto bad; 121478064Sume } 1215121630Sume } 121662587Sitojun 121778064Sume /* validate query Subject field. */ 121862587Sitojun qtype = ntohs(ni6->ni_qtype); 121962587Sitojun subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); 122062587Sitojun switch (qtype) { 122162587Sitojun case NI_QTYPE_NOOP: 122262587Sitojun case NI_QTYPE_SUPTYPES: 122378064Sume /* 07 draft */ 122478064Sume if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) 122578064Sume break; 122678064Sume /* FALLTHROUGH */ 122762587Sitojun case NI_QTYPE_FQDN: 122862587Sitojun case NI_QTYPE_NODEADDR: 1229151475Ssuz case NI_QTYPE_IPV4ADDR: 123062587Sitojun switch (ni6->ni_code) { 123162587Sitojun case ICMP6_NI_SUBJ_IPV6: 123262587Sitojun#if ICMP6_NI_SUBJ_IPV6 != 0 123362587Sitojun case 0: 123462587Sitojun#endif 123562587Sitojun /* 123662587Sitojun * backward compatibility - try to accept 03 draft 123762587Sitojun * format, where no Subject is present. 123862587Sitojun */ 123978064Sume if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && 124078064Sume subjlen == 0) { 124162587Sitojun oldfqdn++; 124262587Sitojun break; 124362587Sitojun } 124478064Sume#if ICMP6_NI_SUBJ_IPV6 != 0 124578064Sume if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) 124678064Sume goto bad; 124778064Sume#endif 124862587Sitojun 1249121630Sume if (subjlen != sizeof(struct in6_addr)) 125062587Sitojun goto bad; 125162587Sitojun 125262587Sitojun /* 125362587Sitojun * Validate Subject address. 125462587Sitojun * 125578064Sume * Not sure what exactly "address belongs to the node" 125678064Sume * means in the spec, is it just unicast, or what? 125762587Sitojun * 125862587Sitojun * At this moment we consider Subject address as 125962587Sitojun * "belong to the node" if the Subject address equals 126062587Sitojun * to the IPv6 destination address; validation for 126162587Sitojun * IPv6 destination address should have done enough 126262587Sitojun * check for us. 126362587Sitojun * 126462587Sitojun * We do not do proxy at this moment. 126562587Sitojun */ 126662587Sitojun /* m_pulldown instead of copy? */ 126762587Sitojun m_copydata(m, off + sizeof(struct icmp6_nodeinfo), 1268148385Sume subjlen, (caddr_t)&in6_subj); 1269148385Sume if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL)) 1270121315Sume goto bad; 1271121630Sume 1272148385Sume subj = (char *)&in6_subj; 1273148385Sume if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj)) 127462587Sitojun break; 127578064Sume 127662587Sitojun /* 127762587Sitojun * XXX if we are to allow other cases, we should really 127862587Sitojun * be careful about scope here. 127962587Sitojun * basically, we should disallow queries toward IPv6 1280120891Sume * destination X with subject Y, 1281120891Sume * if scope(X) > scope(Y). 128262587Sitojun * if we allow scope(X) > scope(Y), it will result in 128362587Sitojun * information leakage across scope boundary. 128462587Sitojun */ 128562587Sitojun goto bad; 128662587Sitojun 128762587Sitojun case ICMP6_NI_SUBJ_FQDN: 128862587Sitojun /* 128962587Sitojun * Validate Subject name with gethostname(3). 129062587Sitojun * 129162587Sitojun * The behavior may need some debate, since: 129262587Sitojun * - we are not sure if the node has FQDN as 129362587Sitojun * hostname (returned by gethostname(3)). 129462587Sitojun * - the code does wildcard match for truncated names. 129562587Sitojun * however, we are not sure if we want to perform 129662587Sitojun * wildcard match, if gethostname(3) side has 129762587Sitojun * truncated hostname. 129862587Sitojun */ 129962587Sitojun n = ni6_nametodns(hostname, hostnamelen, 0); 130062587Sitojun if (!n || n->m_next || n->m_len == 0) 130162587Sitojun goto bad; 130262587Sitojun IP6_EXTHDR_GET(subj, char *, m, 130362587Sitojun off + sizeof(struct icmp6_nodeinfo), subjlen); 130462587Sitojun if (subj == NULL) 130562587Sitojun goto bad; 130662587Sitojun if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 1307120891Sume n->m_len)) { 130862587Sitojun goto bad; 130962587Sitojun } 131062587Sitojun m_freem(n); 131162587Sitojun n = NULL; 131262587Sitojun break; 131362587Sitojun 131478064Sume case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 131562587Sitojun default: 131662587Sitojun goto bad; 131762587Sitojun } 131862587Sitojun break; 131978064Sume } 132062587Sitojun 132178064Sume /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 132278064Sume switch (qtype) { 132378064Sume case NI_QTYPE_FQDN: 1324169664Sjinmei if ((icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0) 132578064Sume goto bad; 132678064Sume break; 132778064Sume case NI_QTYPE_NODEADDR: 1328151475Ssuz case NI_QTYPE_IPV4ADDR: 1329169664Sjinmei if ((icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0) 133078064Sume goto bad; 133178064Sume break; 133278064Sume } 133378064Sume 133478064Sume /* guess reply length */ 133578064Sume switch (qtype) { 133678064Sume case NI_QTYPE_NOOP: 133778064Sume break; /* no reply data */ 133878064Sume case NI_QTYPE_SUPTYPES: 133978064Sume replylen += sizeof(u_int32_t); 134078064Sume break; 134178064Sume case NI_QTYPE_FQDN: 134278064Sume /* XXX will append an mbuf */ 134378064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 134478064Sume break; 134578064Sume case NI_QTYPE_NODEADDR: 1346148892Sume addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj); 134778064Sume if ((replylen += addrs * (sizeof(struct in6_addr) + 1348120891Sume sizeof(u_int32_t))) > MCLBYTES) 134978064Sume replylen = MCLBYTES; /* XXX: will truncate pkt later */ 135078064Sume break; 1351151475Ssuz case NI_QTYPE_IPV4ADDR: 1352151475Ssuz /* unsupported - should respond with unknown Qtype? */ 1353151475Ssuz break; 135462587Sitojun default: 135578064Sume /* 135678064Sume * XXX: We must return a reply with the ICMP6 code 1357120891Sume * `unknown Qtype' in this case. However we regard the case 135878064Sume * as an FQDN query for backward compatibility. 135978064Sume * Older versions set a random value to this field, 136078064Sume * so it rarely varies in the defined qtypes. 136178064Sume * But the mechanism is not reliable... 136278064Sume * maybe we should obsolete older versions. 136378064Sume */ 136478064Sume qtype = NI_QTYPE_FQDN; 136578064Sume /* XXX will append an mbuf */ 136678064Sume replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 136778064Sume oldfqdn++; 136878064Sume break; 136962587Sitojun } 137062587Sitojun 137178064Sume /* allocate an mbuf to reply. */ 1372111119Simp MGETHDR(n, M_DONTWAIT, m->m_type); 137362587Sitojun if (n == NULL) { 137462587Sitojun m_freem(m); 1375120856Sume return (NULL); 137662587Sitojun } 1377108466Ssam M_MOVE_PKTHDR(n, m); /* just for recvif */ 137853541Sshin if (replylen > MHLEN) { 137962587Sitojun if (replylen > MCLBYTES) { 138078064Sume /* 138178064Sume * XXX: should we try to allocate more? But MCLBYTES 138278064Sume * is probably much larger than IPV6_MMTU... 138378064Sume */ 138453541Sshin goto bad; 138562587Sitojun } 1386111119Simp MCLGET(n, M_DONTWAIT); 138753541Sshin if ((n->m_flags & M_EXT) == 0) { 138853541Sshin goto bad; 138953541Sshin } 139053541Sshin } 139153541Sshin n->m_pkthdr.len = n->m_len = replylen; 139253541Sshin 139353541Sshin /* copy mbuf header and IPv6 + Node Information base headers */ 139453541Sshin bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); 139553541Sshin nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 139662587Sitojun bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); 139753541Sshin 139853541Sshin /* qtype dependent procedure */ 139953541Sshin switch (qtype) { 140062587Sitojun case NI_QTYPE_NOOP: 140178064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 140262587Sitojun nni6->ni_flags = 0; 140362587Sitojun break; 140462587Sitojun case NI_QTYPE_SUPTYPES: 140578064Sume { 140678064Sume u_int32_t v; 140778064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 140878064Sume nni6->ni_flags = htons(0x0000); /* raw bitmap */ 140978064Sume /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 141078064Sume v = (u_int32_t)htonl(0x0000000f); 141178064Sume bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 141262587Sitojun break; 141378064Sume } 141462587Sitojun case NI_QTYPE_FQDN: 141578064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 141662587Sitojun fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + 1417120891Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo)); 141862587Sitojun nni6->ni_flags = 0; /* XXX: meaningless TTL */ 141962587Sitojun fqdn->ni_fqdn_ttl = 0; /* ditto. */ 142062587Sitojun /* 142162587Sitojun * XXX do we really have FQDN in variable "hostname"? 142262587Sitojun */ 142362587Sitojun n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn); 142462587Sitojun if (n->m_next == NULL) 142562587Sitojun goto bad; 142662587Sitojun /* XXX we assume that n->m_next is not a chain */ 142762587Sitojun if (n->m_next->m_next != NULL) 142862587Sitojun goto bad; 142962587Sitojun n->m_pkthdr.len += n->m_next->m_len; 143062587Sitojun break; 143162587Sitojun case NI_QTYPE_NODEADDR: 143262587Sitojun { 143362587Sitojun int lenlim, copied; 143453541Sshin 143578064Sume nni6->ni_code = ICMP6_NI_SUCCESS; 143678064Sume n->m_pkthdr.len = n->m_len = 143778064Sume sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 143878064Sume lenlim = M_TRAILINGSPACE(n); 143962587Sitojun copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 144062587Sitojun /* XXX: reset mbuf length */ 144162587Sitojun n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 1442120891Sume sizeof(struct icmp6_nodeinfo) + copied; 144362587Sitojun break; 144453541Sshin } 144562587Sitojun default: 144662587Sitojun break; /* XXX impossible! */ 144762587Sitojun } 144853541Sshin 144953541Sshin nni6->ni_type = ICMP6_NI_REPLY; 145062587Sitojun m_freem(m); 1451120856Sume return (n); 145253541Sshin 145353541Sshin bad: 145462587Sitojun m_freem(m); 145553541Sshin if (n) 145653541Sshin m_freem(n); 1457120856Sume return (NULL); 145853541Sshin} 145953541Sshin#undef hostnamelen 146053541Sshin 146153541Sshin/* 146262587Sitojun * make a mbuf with DNS-encoded string. no compression support. 146362587Sitojun * 146462587Sitojun * XXX names with less than 2 dots (like "foo" or "foo.section") will be 146562587Sitojun * treated as truncated name (two \0 at the end). this is a wild guess. 1466171259Sdelphij * 1467171259Sdelphij * old - return pascal string if non-zero 146862587Sitojun */ 146962587Sitojunstatic struct mbuf * 1470171259Sdelphijni6_nametodns(const char *name, int namelen, int old) 147162587Sitojun{ 147262587Sitojun struct mbuf *m; 147362587Sitojun char *cp, *ep; 147462587Sitojun const char *p, *q; 147562587Sitojun int i, len, nterm; 147662587Sitojun 147762587Sitojun if (old) 147862587Sitojun len = namelen + 1; 147962587Sitojun else 148062587Sitojun len = MCLBYTES; 148162587Sitojun 148262587Sitojun /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ 1483111119Simp MGET(m, M_DONTWAIT, MT_DATA); 148462587Sitojun if (m && len > MLEN) { 1485111119Simp MCLGET(m, M_DONTWAIT); 148662587Sitojun if ((m->m_flags & M_EXT) == 0) 148762587Sitojun goto fail; 148862587Sitojun } 148962587Sitojun if (!m) 149062587Sitojun goto fail; 149162587Sitojun m->m_next = NULL; 149262587Sitojun 149362587Sitojun if (old) { 149462587Sitojun m->m_len = len; 149562587Sitojun *mtod(m, char *) = namelen; 149662587Sitojun bcopy(name, mtod(m, char *) + 1, namelen); 149762587Sitojun return m; 149862587Sitojun } else { 149962587Sitojun m->m_len = 0; 150062587Sitojun cp = mtod(m, char *); 150162587Sitojun ep = mtod(m, char *) + M_TRAILINGSPACE(m); 150262587Sitojun 150362587Sitojun /* if not certain about my name, return empty buffer */ 150462587Sitojun if (namelen == 0) 150562587Sitojun return m; 150662587Sitojun 150762587Sitojun /* 150862587Sitojun * guess if it looks like shortened hostname, or FQDN. 150962587Sitojun * shortened hostname needs two trailing "\0". 151062587Sitojun */ 151162587Sitojun i = 0; 151262587Sitojun for (p = name; p < name + namelen; p++) { 151362587Sitojun if (*p && *p == '.') 151462587Sitojun i++; 151562587Sitojun } 151662587Sitojun if (i < 2) 151762587Sitojun nterm = 2; 151862587Sitojun else 151962587Sitojun nterm = 1; 152062587Sitojun 152162587Sitojun p = name; 152262587Sitojun while (cp < ep && p < name + namelen) { 152362587Sitojun i = 0; 152462587Sitojun for (q = p; q < name + namelen && *q && *q != '.'; q++) 152562587Sitojun i++; 152662587Sitojun /* result does not fit into mbuf */ 152762587Sitojun if (cp + i + 1 >= ep) 152862587Sitojun goto fail; 152978704Sume /* 153078704Sume * DNS label length restriction, RFC1035 page 8. 153178704Sume * "i == 0" case is included here to avoid returning 153278704Sume * 0-length label on "foo..bar". 153378704Sume */ 153478704Sume if (i <= 0 || i >= 64) 153562587Sitojun goto fail; 153662587Sitojun *cp++ = i; 153762587Sitojun bcopy(p, cp, i); 153862587Sitojun cp += i; 153962587Sitojun p = q; 154062587Sitojun if (p < name + namelen && *p == '.') 154162587Sitojun p++; 154262587Sitojun } 154362587Sitojun /* termination */ 154462587Sitojun if (cp + nterm >= ep) 154562587Sitojun goto fail; 154662587Sitojun while (nterm-- > 0) 154762587Sitojun *cp++ = '\0'; 154862587Sitojun m->m_len = cp - mtod(m, char *); 154962587Sitojun return m; 155062587Sitojun } 155162587Sitojun 155262587Sitojun panic("should not reach here"); 155395023Ssuz /* NOTREACHED */ 155462587Sitojun 155562587Sitojun fail: 155662587Sitojun if (m) 155762587Sitojun m_freem(m); 155862587Sitojun return NULL; 155962587Sitojun} 156062587Sitojun 156162587Sitojun/* 156262587Sitojun * check if two DNS-encoded string matches. takes care of truncated 156362587Sitojun * form (with \0\0 at the end). no compression support. 156478064Sume * XXX upper/lowercase match (see RFC2065) 156562587Sitojun */ 156662587Sitojunstatic int 1567171259Sdelphijni6_dnsmatch(const char *a, int alen, const char *b, int blen) 156862587Sitojun{ 156962587Sitojun const char *a0, *b0; 157062587Sitojun int l; 157162587Sitojun 157262587Sitojun /* simplest case - need validation? */ 157362587Sitojun if (alen == blen && bcmp(a, b, alen) == 0) 157462587Sitojun return 1; 157562587Sitojun 157662587Sitojun a0 = a; 157762587Sitojun b0 = b; 157862587Sitojun 157962587Sitojun /* termination is mandatory */ 158062587Sitojun if (alen < 2 || blen < 2) 158162587Sitojun return 0; 158262587Sitojun if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 158362587Sitojun return 0; 158462587Sitojun alen--; 158562587Sitojun blen--; 158662587Sitojun 158762587Sitojun while (a - a0 < alen && b - b0 < blen) { 158862587Sitojun if (a - a0 + 1 > alen || b - b0 + 1 > blen) 158962587Sitojun return 0; 159062587Sitojun 159162587Sitojun if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 159262587Sitojun return 0; 159362587Sitojun /* we don't support compression yet */ 159462587Sitojun if (a[0] >= 64 || b[0] >= 64) 159562587Sitojun return 0; 159662587Sitojun 159762587Sitojun /* truncated case */ 159862587Sitojun if (a[0] == 0 && a - a0 == alen - 1) 159962587Sitojun return 1; 160062587Sitojun if (b[0] == 0 && b - b0 == blen - 1) 160162587Sitojun return 1; 160262587Sitojun if (a[0] == 0 || b[0] == 0) 160362587Sitojun return 0; 160462587Sitojun 160562587Sitojun if (a[0] != b[0]) 160662587Sitojun return 0; 160762587Sitojun l = a[0]; 160862587Sitojun if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 160962587Sitojun return 0; 161062587Sitojun if (bcmp(a + 1, b + 1, l) != 0) 161162587Sitojun return 0; 161262587Sitojun 161362587Sitojun a += 1 + l; 161462587Sitojun b += 1 + l; 161562587Sitojun } 161662587Sitojun 161762587Sitojun if (a - a0 == alen && b - b0 == blen) 161862587Sitojun return 1; 161962587Sitojun else 162062587Sitojun return 0; 162162587Sitojun} 162262587Sitojun 162362587Sitojun/* 162453541Sshin * calculate the number of addresses to be returned in the node info reply. 162553541Sshin */ 162653541Sshinstatic int 1627171259Sdelphijni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp, 1628171259Sdelphij struct in6_addr *subj) 162953541Sshin{ 163078064Sume struct ifnet *ifp; 163178064Sume struct in6_ifaddr *ifa6; 163278064Sume struct ifaddr *ifa; 163353541Sshin int addrs = 0, addrsofif, iffound = 0; 163478064Sume int niflags = ni6->ni_flags; 163553541Sshin 163678064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 163778064Sume switch (ni6->ni_code) { 163878064Sume case ICMP6_NI_SUBJ_IPV6: 163978064Sume if (subj == NULL) /* must be impossible... */ 1640120856Sume return (0); 164178064Sume break; 164278064Sume default: 164378064Sume /* 164478064Sume * XXX: we only support IPv6 subject address for 164578064Sume * this Qtype. 164678064Sume */ 1647120856Sume return (0); 164878064Sume } 164978064Sume } 165078064Sume 1651108172Shsu IFNET_RLOCK(); 1652120891Sume for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 165353541Sshin addrsofif = 0; 1654120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 165553541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 165653541Sshin continue; 165753541Sshin ifa6 = (struct in6_ifaddr *)ifa; 165853541Sshin 165978064Sume if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 1660148892Sume IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr)) 166153541Sshin iffound = 1; 166253541Sshin 166362587Sitojun /* 166462587Sitojun * IPv4-mapped addresses can only be returned by a 166562587Sitojun * Node Information proxy, since they represent 166662587Sitojun * addresses of IPv4-only nodes, which perforce do 166762587Sitojun * not implement this protocol. 166878064Sume * [icmp-name-lookups-07, Section 5.4] 166962587Sitojun * So we don't support NI_NODEADDR_FLAG_COMPAT in 167062587Sitojun * this function at this moment. 167162587Sitojun */ 167262587Sitojun 167353541Sshin /* What do we have to do about ::1? */ 167478064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 167578064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 167678064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 167778064Sume continue; 167853541Sshin break; 167978064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 168078064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 168178064Sume continue; 168253541Sshin break; 168378064Sume case IPV6_ADDR_SCOPE_GLOBAL: 168478064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 168578064Sume continue; 168678064Sume break; 168778064Sume default: 168878064Sume continue; 168953541Sshin } 169078064Sume 169178064Sume /* 169278064Sume * check if anycast is okay. 169395023Ssuz * XXX: just experimental. not in the spec. 169478064Sume */ 169578064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 169678064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 169778064Sume continue; /* we need only unicast addresses */ 169878064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1699169664Sjinmei (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { 170078064Sume continue; 170178064Sume } 170278064Sume addrsofif++; /* count the address */ 170353541Sshin } 170453541Sshin if (iffound) { 170553541Sshin *ifpp = ifp; 1706108172Shsu IFNET_RUNLOCK(); 1707120856Sume return (addrsofif); 170853541Sshin } 170953541Sshin 171053541Sshin addrs += addrsofif; 171153541Sshin } 1712108172Shsu IFNET_RUNLOCK(); 171353541Sshin 1714120856Sume return (addrs); 171553541Sshin} 171653541Sshin 171753541Sshinstatic int 1718171259Sdelphijni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6, 1719171259Sdelphij struct ifnet *ifp0, int resid) 172053541Sshin{ 172178064Sume struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet); 172278064Sume struct in6_ifaddr *ifa6; 172378064Sume struct ifaddr *ifa; 172478064Sume struct ifnet *ifp_dep = NULL; 172578064Sume int copied = 0, allow_deprecated = 0; 172653541Sshin u_char *cp = (u_char *)(nni6 + 1); 172778064Sume int niflags = ni6->ni_flags; 172878064Sume u_int32_t ltime; 172953541Sshin 173078064Sume if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 1731120856Sume return (0); /* needless to copy */ 1732120891Sume 1733108172Shsu IFNET_RLOCK(); 173478064Sume again: 1735120892Sume 1736120891Sume for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 173762587Sitojun for (ifa = ifp->if_addrlist.tqh_first; ifa; 1738120891Sume ifa = ifa->ifa_list.tqe_next) { 173953541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 174053541Sshin continue; 174153541Sshin ifa6 = (struct in6_ifaddr *)ifa; 174253541Sshin 174378064Sume if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && 174478064Sume allow_deprecated == 0) { 174578064Sume /* 174678064Sume * prefererred address should be put before 174778064Sume * deprecated addresses. 174878064Sume */ 174978064Sume 175078064Sume /* record the interface for later search */ 175178064Sume if (ifp_dep == NULL) 175278064Sume ifp_dep = ifp; 175378064Sume 175478064Sume continue; 1755120891Sume } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && 1756120891Sume allow_deprecated != 0) 175778064Sume continue; /* we now collect deprecated addrs */ 175853541Sshin 175953541Sshin /* What do we have to do about ::1? */ 176078064Sume switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 176178064Sume case IPV6_ADDR_SCOPE_LINKLOCAL: 176278064Sume if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 176378064Sume continue; 176453541Sshin break; 176578064Sume case IPV6_ADDR_SCOPE_SITELOCAL: 176678064Sume if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 176778064Sume continue; 176853541Sshin break; 176978064Sume case IPV6_ADDR_SCOPE_GLOBAL: 177078064Sume if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 177178064Sume continue; 177278064Sume break; 177378064Sume default: 177478064Sume continue; 177553541Sshin } 177653541Sshin 177778064Sume /* 177878064Sume * check if anycast is okay. 1779120891Sume * XXX: just experimental. not in the spec. 178078064Sume */ 178178064Sume if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 178278064Sume (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 178378064Sume continue; 178478064Sume if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1785169664Sjinmei (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { 178678064Sume continue; 178753541Sshin } 178878064Sume 178978064Sume /* now we can copy the address */ 179078064Sume if (resid < sizeof(struct in6_addr) + 179178064Sume sizeof(u_int32_t)) { 179278064Sume /* 179378064Sume * We give up much more copy. 179478064Sume * Set the truncate flag and return. 179578064Sume */ 1796120891Sume nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE; 1797108172Shsu IFNET_RUNLOCK(); 1798120856Sume return (copied); 179978064Sume } 180078064Sume 180178064Sume /* 180278064Sume * Set the TTL of the address. 180378064Sume * The TTL value should be one of the following 180478064Sume * according to the specification: 180578064Sume * 180678064Sume * 1. The remaining lifetime of a DHCP lease on the 180778064Sume * address, or 180878064Sume * 2. The remaining Valid Lifetime of a prefix from 180978064Sume * which the address was derived through Stateless 181078064Sume * Autoconfiguration. 181178064Sume * 181278064Sume * Note that we currently do not support stateful 181378064Sume * address configuration by DHCPv6, so the former 181478064Sume * case can't happen. 181578064Sume */ 181678064Sume if (ifa6->ia6_lifetime.ia6t_expire == 0) 181778064Sume ltime = ND6_INFINITE_LIFETIME; 181878064Sume else { 181978064Sume if (ifa6->ia6_lifetime.ia6t_expire > 182078064Sume time_second) 182178064Sume ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second); 182278064Sume else 182378064Sume ltime = 0; 182478064Sume } 1825120891Sume 182678064Sume bcopy(<ime, cp, sizeof(u_int32_t)); 182778064Sume cp += sizeof(u_int32_t); 182878064Sume 182978064Sume /* copy the address itself */ 183078064Sume bcopy(&ifa6->ia_addr.sin6_addr, cp, 1831120891Sume sizeof(struct in6_addr)); 1832121315Sume in6_clearscope((struct in6_addr *)cp); /* XXX */ 183378064Sume cp += sizeof(struct in6_addr); 1834120891Sume 183578064Sume resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1836120891Sume copied += (sizeof(struct in6_addr) + sizeof(u_int32_t)); 183753541Sshin } 183853541Sshin if (ifp0) /* we need search only on the specified IF */ 183953541Sshin break; 184053541Sshin } 184153541Sshin 184278064Sume if (allow_deprecated == 0 && ifp_dep != NULL) { 184378064Sume ifp = ifp_dep; 184478064Sume allow_deprecated = 1; 184578064Sume 184678064Sume goto again; 184778064Sume } 184878064Sume 1849108172Shsu IFNET_RUNLOCK(); 1850108172Shsu 1851120856Sume return (copied); 185253541Sshin} 185353541Sshin 185453541Sshin/* 185553541Sshin * XXX almost dup'ed code with rip6_input. 185653541Sshin */ 185753541Sshinstatic int 1858171259Sdelphijicmp6_rip6_input(struct mbuf **mp, int off) 185953541Sshin{ 186053541Sshin struct mbuf *m = *mp; 186178064Sume struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 186278064Sume struct in6pcb *in6p; 186353541Sshin struct in6pcb *last = NULL; 1864121315Sume struct sockaddr_in6 fromsa; 186553541Sshin struct icmp6_hdr *icmp6; 186653541Sshin struct mbuf *opts = NULL; 186753541Sshin 186862587Sitojun#ifndef PULLDOWN_TEST 186953541Sshin /* this is assumed to be safe. */ 187053541Sshin icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 187162587Sitojun#else 187262587Sitojun IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 187362587Sitojun if (icmp6 == NULL) { 187462587Sitojun /* m is already reclaimed */ 1875120891Sume return (IPPROTO_DONE); 187662587Sitojun } 187762587Sitojun#endif 187853541Sshin 1879148385Sume /* 1880148385Sume * XXX: the address may have embedded scope zone ID, which should be 1881148385Sume * hidden from applications. 1882148385Sume */ 1883121315Sume bzero(&fromsa, sizeof(fromsa)); 1884148385Sume fromsa.sin6_family = AF_INET6; 1885121315Sume fromsa.sin6_len = sizeof(struct sockaddr_in6); 1886148385Sume fromsa.sin6_addr = ip6->ip6_src; 1887148385Sume if (sa6_recoverscope(&fromsa)) { 1888148385Sume m_freem(m); 1889148385Sume return (IPPROTO_DONE); 1890148385Sume } 189153541Sshin 1892141545Srwatson INP_INFO_RLOCK(&ripcbinfo); 1893120891Sume LIST_FOREACH(in6p, &ripcb, inp_list) { 1894141545Srwatson INP_LOCK(in6p); 1895141545Srwatson if ((in6p->inp_vflag & INP_IPV6) == 0) { 1896141545Srwatson docontinue: 1897141545Srwatson INP_UNLOCK(in6p); 189853541Sshin continue; 1899141545Srwatson } 190053541Sshin if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6) 1901141545Srwatson goto docontinue; 190253541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 190353541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 1904141545Srwatson goto docontinue; 190553541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 190653541Sshin !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 1907141545Srwatson goto docontinue; 190853541Sshin if (in6p->in6p_icmp6filt 190953541Sshin && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, 191053541Sshin in6p->in6p_icmp6filt)) 1911141545Srwatson goto docontinue; 191253541Sshin if (last) { 1913121809Sume struct mbuf *n = NULL; 1914121809Sume 1915121809Sume /* 1916121809Sume * Recent network drivers tend to allocate a single 1917121809Sume * mbuf cluster, rather than to make a couple of 1918121809Sume * mbufs without clusters. Also, since the IPv6 code 1919121809Sume * path tries to avoid m_pullup(), it is highly 1920121809Sume * probable that we still have an mbuf cluster here 1921121809Sume * even though the necessary length can be stored in an 1922121809Sume * mbuf's internal buffer. 1923121809Sume * Meanwhile, the default size of the receive socket 1924121809Sume * buffer for raw sockets is not so large. This means 1925121809Sume * the possibility of packet loss is relatively higher 1926121809Sume * than before. To avoid this scenario, we copy the 1927121809Sume * received data to a separate mbuf that does not use 1928121809Sume * a cluster, if possible. 1929121809Sume * XXX: it is better to copy the data after stripping 1930121809Sume * intermediate headers. 1931121809Sume */ 1932121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 1933121809Sume m->m_len <= MHLEN) { 1934121809Sume MGET(n, M_DONTWAIT, m->m_type); 1935121809Sume if (n != NULL) { 1936145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 1937171260Sdelphij bcopy(m->m_data, n->m_data, 1938145065Sgnn m->m_len); 1939145065Sgnn n->m_len = m->m_len; 1940145065Sgnn } else { 1941145065Sgnn m_free(n); 1942145065Sgnn n = NULL; 1943145065Sgnn } 1944121809Sume } 1945121809Sume } 1946121809Sume if (n != NULL || 1947121809Sume (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 194853541Sshin if (last->in6p_flags & IN6P_CONTROLOPTS) 1949121674Sume ip6_savecontrol(last, n, &opts); 195053541Sshin /* strip intermediate headers */ 195153541Sshin m_adj(n, off); 1952160591Srwatson SOCKBUF_LOCK(&last->in6p_socket->so_rcv); 1953160591Srwatson if (sbappendaddr_locked( 1954160591Srwatson &last->in6p_socket->so_rcv, 1955121315Sume (struct sockaddr *)&fromsa, n, opts) 1956120891Sume == 0) { 195753541Sshin /* should notify about lost packet */ 195853541Sshin m_freem(n); 195978064Sume if (opts) { 196053541Sshin m_freem(opts); 196178064Sume } 1962160591Srwatson SOCKBUF_UNLOCK( 1963160591Srwatson &last->in6p_socket->so_rcv); 196497658Stanimura } else 1965160591Srwatson sorwakeup_locked(last->in6p_socket); 196653541Sshin opts = NULL; 196753541Sshin } 1968141545Srwatson INP_UNLOCK(last); 196953541Sshin } 197053541Sshin last = in6p; 197153541Sshin } 197253541Sshin if (last) { 197353541Sshin if (last->in6p_flags & IN6P_CONTROLOPTS) 1974121674Sume ip6_savecontrol(last, m, &opts); 197553541Sshin /* strip intermediate headers */ 197653541Sshin m_adj(m, off); 1977121809Sume 1978121809Sume /* avoid using mbuf clusters if possible (see above) */ 1979121809Sume if ((m->m_flags & M_EXT) && m->m_next == NULL && 1980121809Sume m->m_len <= MHLEN) { 1981121809Sume struct mbuf *n; 1982121809Sume 1983121809Sume MGET(n, M_DONTWAIT, m->m_type); 1984121809Sume if (n != NULL) { 1985145065Sgnn if (m_dup_pkthdr(n, m, M_NOWAIT)) { 1986145065Sgnn bcopy(m->m_data, n->m_data, m->m_len); 1987145065Sgnn n->m_len = m->m_len; 1988171260Sdelphij 1989145065Sgnn m_freem(m); 1990145065Sgnn m = n; 1991145065Sgnn } else { 1992145065Sgnn m_freem(n); 1993145065Sgnn n = NULL; 1994145065Sgnn } 1995121809Sume } 1996121809Sume } 1997160591Srwatson SOCKBUF_LOCK(&last->in6p_socket->so_rcv); 1998160591Srwatson if (sbappendaddr_locked(&last->in6p_socket->so_rcv, 1999121315Sume (struct sockaddr *)&fromsa, m, opts) == 0) { 200053541Sshin m_freem(m); 200153541Sshin if (opts) 200253541Sshin m_freem(opts); 2003160591Srwatson SOCKBUF_UNLOCK(&last->in6p_socket->so_rcv); 200497658Stanimura } else 2005160591Srwatson sorwakeup_locked(last->in6p_socket); 2006141545Srwatson INP_UNLOCK(last); 200753541Sshin } else { 200853541Sshin m_freem(m); 200953541Sshin ip6stat.ip6s_delivered--; 201053541Sshin } 2011141545Srwatson INP_INFO_RUNLOCK(&ripcbinfo); 201253541Sshin return IPPROTO_DONE; 201353541Sshin} 201453541Sshin 201553541Sshin/* 201653541Sshin * Reflect the ip6 packet back to the source. 201762587Sitojun * OFF points to the icmp6 header, counted from the top of the mbuf. 201853541Sshin */ 201953541Sshinvoid 2020171259Sdelphijicmp6_reflect(struct mbuf *m, size_t off) 202153541Sshin{ 202262587Sitojun struct ip6_hdr *ip6; 202353541Sshin struct icmp6_hdr *icmp6; 202453541Sshin struct in6_ifaddr *ia; 202562587Sitojun int plen; 202653541Sshin int type, code; 202753541Sshin struct ifnet *outif = NULL; 2028148385Sume struct in6_addr origdst, *src = NULL; 202953541Sshin 203062587Sitojun /* too short to reflect */ 203162587Sitojun if (off < sizeof(struct ip6_hdr)) { 203278064Sume nd6log((LOG_DEBUG, 203378064Sume "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 203478064Sume (u_long)off, (u_long)sizeof(struct ip6_hdr), 203578064Sume __FILE__, __LINE__)); 203662587Sitojun goto bad; 203762587Sitojun } 203862587Sitojun 203953541Sshin /* 204053541Sshin * If there are extra headers between IPv6 and ICMPv6, strip 204153541Sshin * off that header first. 204253541Sshin */ 204362587Sitojun#ifdef DIAGNOSTIC 204462587Sitojun if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) 204562587Sitojun panic("assumption failed in icmp6_reflect"); 204662587Sitojun#endif 204762587Sitojun if (off > sizeof(struct ip6_hdr)) { 204862587Sitojun size_t l; 204962587Sitojun struct ip6_hdr nip6; 205053541Sshin 205162587Sitojun l = off - sizeof(struct ip6_hdr); 205262587Sitojun m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); 205362587Sitojun m_adj(m, l); 205462587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 205562587Sitojun if (m->m_len < l) { 205662587Sitojun if ((m = m_pullup(m, l)) == NULL) 205762587Sitojun return; 205853541Sshin } 205962587Sitojun bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); 206062587Sitojun } else /* off == sizeof(struct ip6_hdr) */ { 206162587Sitojun size_t l; 206262587Sitojun l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 206362587Sitojun if (m->m_len < l) { 206462587Sitojun if ((m = m_pullup(m, l)) == NULL) 206562587Sitojun return; 206653541Sshin } 206753541Sshin } 206862587Sitojun plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 206962587Sitojun ip6 = mtod(m, struct ip6_hdr *); 207062587Sitojun ip6->ip6_nxt = IPPROTO_ICMPV6; 207153541Sshin icmp6 = (struct icmp6_hdr *)(ip6 + 1); 207253541Sshin type = icmp6->icmp6_type; /* keep type for statistics */ 207353541Sshin code = icmp6->icmp6_code; /* ditto. */ 207453541Sshin 2075148385Sume origdst = ip6->ip6_dst; 207653541Sshin /* 207753541Sshin * ip6_input() drops a packet if its src is multicast. 207853541Sshin * So, the src is never multicast. 207953541Sshin */ 208053541Sshin ip6->ip6_dst = ip6->ip6_src; 208153541Sshin 208253541Sshin /* 2083120891Sume * If the incoming packet was addressed directly to us (i.e. unicast), 208453541Sshin * use dst as the src for the reply. 2085120891Sume * The IN6_IFF_NOTREADY case should be VERY rare, but is possible 208662587Sitojun * (for example) when we encounter an error while forwarding procedure 208762587Sitojun * destined to a duplicated address of ours. 2088148385Sume * Note that ip6_getdstifaddr() may fail if we are in an error handling 2089148385Sume * procedure of an outgoing packet of our own, in which case we need 2090148385Sume * to search in the ifaddr list. 209153541Sshin */ 2092148385Sume if (!IN6_IS_ADDR_MULTICAST(&origdst)) { 2093148385Sume if ((ia = ip6_getdstifaddr(m))) { 2094148385Sume if (!(ia->ia6_flags & 2095148385Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) 2096148385Sume src = &ia->ia_addr.sin6_addr; 2097148385Sume } else { 2098148385Sume struct sockaddr_in6 d; 2099148385Sume 2100148385Sume bzero(&d, sizeof(d)); 2101148385Sume d.sin6_family = AF_INET6; 2102148385Sume d.sin6_len = sizeof(d); 2103148385Sume d.sin6_addr = origdst; 2104148385Sume ia = (struct in6_ifaddr *) 2105148385Sume ifa_ifwithaddr((struct sockaddr *)&d); 2106148385Sume if (ia && 2107148385Sume !(ia->ia6_flags & 2108148385Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) { 2109148385Sume src = &ia->ia_addr.sin6_addr; 2110148385Sume } 211153541Sshin } 211253541Sshin } 211353541Sshin 2114148385Sume if (src == NULL) { 211578064Sume int e; 2116148385Sume struct sockaddr_in6 sin6; 211778064Sume struct route_in6 ro; 211878064Sume 211953541Sshin /* 212062587Sitojun * This case matches to multicasts, our anycast, or unicasts 212195023Ssuz * that we do not own. Select a source address based on the 212278064Sume * source address of the erroneous packet. 212353541Sshin */ 2124148385Sume bzero(&sin6, sizeof(sin6)); 2125148385Sume sin6.sin6_family = AF_INET6; 2126148385Sume sin6.sin6_len = sizeof(sin6); 2127148385Sume sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */ 2128148385Sume 212978064Sume bzero(&ro, sizeof(ro)); 2130148385Sume src = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &e); 213178064Sume if (ro.ro_rt) 213278064Sume RTFREE(ro.ro_rt); /* XXX: we could use this */ 213378064Sume if (src == NULL) { 2134165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 213578064Sume nd6log((LOG_DEBUG, 213678064Sume "icmp6_reflect: source can't be determined: " 213778064Sume "dst=%s, error=%d\n", 2138165118Sbz ip6_sprintf(ip6buf, &sin6.sin6_addr), e)); 213978064Sume goto bad; 214078064Sume } 214178064Sume } 214253541Sshin 214353541Sshin ip6->ip6_src = *src; 214453541Sshin ip6->ip6_flow = 0; 214562587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 214662587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 214753541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 2148121472Sume if (outif) 2149121472Sume ip6->ip6_hlim = ND_IFINFO(outif)->chlim; 2150121472Sume else if (m->m_pkthdr.rcvif) { 215153541Sshin /* XXX: This may not be the outgoing interface */ 2152121161Sume ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim; 215378064Sume } else 215478064Sume ip6->ip6_hlim = ip6_defhlim; 215553541Sshin 215653541Sshin icmp6->icmp6_cksum = 0; 215753541Sshin icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2158120891Sume sizeof(struct ip6_hdr), plen); 215953541Sshin 216053541Sshin /* 216178064Sume * XXX option handling 216253541Sshin */ 216353541Sshin 216453541Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 216553541Sshin 2166105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 216753541Sshin if (outif) 216853541Sshin icmp6_ifoutstat_inc(outif, type, code); 216953541Sshin 217053541Sshin return; 217153541Sshin 217253541Sshin bad: 217353541Sshin m_freem(m); 217453541Sshin return; 217553541Sshin} 217653541Sshin 217753541Sshinvoid 2178171259Sdelphijicmp6_fasttimo(void) 217953541Sshin{ 218062587Sitojun 2181151539Ssuz return; 218253541Sshin} 218353541Sshin 218453541Sshinstatic const char * 2185171259Sdelphijicmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6, 2186171259Sdelphij struct in6_addr *tgt6) 218753541Sshin{ 218853541Sshin static char buf[1024]; 2189165118Sbz char ip6bufs[INET6_ADDRSTRLEN]; 2190165118Sbz char ip6bufd[INET6_ADDRSTRLEN]; 2191165118Sbz char ip6buft[INET6_ADDRSTRLEN]; 219253541Sshin snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", 2193165118Sbz ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6), 2194165118Sbz ip6_sprintf(ip6buft, tgt6)); 219553541Sshin return buf; 219653541Sshin} 219753541Sshin 219853541Sshinvoid 2199171259Sdelphijicmp6_redirect_input(struct mbuf *m, int off) 220053541Sshin{ 2201171133Sgnn struct ifnet *ifp; 220253541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 220362587Sitojun struct nd_redirect *nd_rd; 220453541Sshin int icmp6len = ntohs(ip6->ip6_plen); 220553541Sshin char *lladdr = NULL; 220653541Sshin int lladdrlen = 0; 220753541Sshin u_char *redirhdr = NULL; 220853541Sshin int redirhdrlen = 0; 220953541Sshin struct rtentry *rt = NULL; 221053541Sshin int is_router; 221153541Sshin int is_onlink; 221253541Sshin struct in6_addr src6 = ip6->ip6_src; 221362587Sitojun struct in6_addr redtgt6; 221462587Sitojun struct in6_addr reddst6; 221553541Sshin union nd_opts ndopts; 2216165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 221753541Sshin 2218171133Sgnn if (!m) 221953541Sshin return; 222053541Sshin 2221171133Sgnn ifp = m->m_pkthdr.rcvif; 2222171133Sgnn 2223171133Sgnn if (!ifp) 2224171133Sgnn return; 2225171133Sgnn 222653541Sshin /* XXX if we are router, we don't update route by icmp6 redirect */ 222753541Sshin if (ip6_forwarding) 222862587Sitojun goto freeit; 222953541Sshin if (!icmp6_rediraccept) 223062587Sitojun goto freeit; 223162587Sitojun 223262587Sitojun#ifndef PULLDOWN_TEST 223362587Sitojun IP6_EXTHDR_CHECK(m, off, icmp6len,); 223462587Sitojun nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); 223562587Sitojun#else 223662587Sitojun IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); 223762587Sitojun if (nd_rd == NULL) { 223862587Sitojun icmp6stat.icp6s_tooshort++; 223953541Sshin return; 224062587Sitojun } 224162587Sitojun#endif 224262587Sitojun redtgt6 = nd_rd->nd_rd_target; 224362587Sitojun reddst6 = nd_rd->nd_rd_dst; 224453541Sshin 2245148385Sume if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) || 2246148385Sume in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) { 2247148385Sume goto freeit; 2248148385Sume } 224953541Sshin 225053541Sshin /* validation */ 225153541Sshin if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 225278064Sume nd6log((LOG_ERR, 2253120891Sume "ICMP6 redirect sent from %s rejected; " 2254120891Sume "must be from linklocal\n", 2255165118Sbz ip6_sprintf(ip6buf, &src6))); 225678064Sume goto bad; 225753541Sshin } 225853541Sshin if (ip6->ip6_hlim != 255) { 225978064Sume nd6log((LOG_ERR, 2260120891Sume "ICMP6 redirect sent from %s rejected; " 2261120891Sume "hlim=%d (must be 255)\n", 2262165118Sbz ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim)); 226378064Sume goto bad; 226453541Sshin } 226553541Sshin { 226653541Sshin /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 226753541Sshin struct sockaddr_in6 sin6; 226853541Sshin struct in6_addr *gw6; 226953541Sshin 227053541Sshin bzero(&sin6, sizeof(sin6)); 227153541Sshin sin6.sin6_family = AF_INET6; 227253541Sshin sin6.sin6_len = sizeof(struct sockaddr_in6); 227353541Sshin bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); 227453541Sshin rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 227553541Sshin if (rt) { 227665895Sume if (rt->rt_gateway == NULL || 227765895Sume rt->rt_gateway->sa_family != AF_INET6) { 227878064Sume nd6log((LOG_ERR, 227965895Sume "ICMP6 redirect rejected; no route " 228065895Sume "with inet6 gateway found for redirect dst: %s\n", 228178064Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2282120727Ssam RTFREE_LOCKED(rt); 228378064Sume goto bad; 228465895Sume } 228565895Sume 228653541Sshin gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); 228753541Sshin if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { 228878064Sume nd6log((LOG_ERR, 2289120891Sume "ICMP6 redirect rejected; " 2290120891Sume "not equal to gw-for-src=%s (must be same): " 2291120891Sume "%s\n", 2292165118Sbz ip6_sprintf(ip6buf, gw6), 2293120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2294120727Ssam RTFREE_LOCKED(rt); 229578064Sume goto bad; 229653541Sshin } 229753541Sshin } else { 229878064Sume nd6log((LOG_ERR, 2299120891Sume "ICMP6 redirect rejected; " 2300120891Sume "no route found for redirect dst: %s\n", 2301120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 230278064Sume goto bad; 230353541Sshin } 2304120727Ssam RTFREE_LOCKED(rt); 230553541Sshin rt = NULL; 230653541Sshin } 230753541Sshin if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 230878064Sume nd6log((LOG_ERR, 2309120891Sume "ICMP6 redirect rejected; " 2310120891Sume "redirect dst must be unicast: %s\n", 2311120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 231278064Sume goto bad; 231353541Sshin } 231453541Sshin 231553541Sshin is_router = is_onlink = 0; 231653541Sshin if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 231753541Sshin is_router = 1; /* router case */ 231853541Sshin if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 231953541Sshin is_onlink = 1; /* on-link destination case */ 232053541Sshin if (!is_router && !is_onlink) { 232178064Sume nd6log((LOG_ERR, 2322120891Sume "ICMP6 redirect rejected; " 2323120891Sume "neither router case nor onlink case: %s\n", 2324120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 232578064Sume goto bad; 232653541Sshin } 232753541Sshin /* validation passed */ 232853541Sshin 232953541Sshin icmp6len -= sizeof(*nd_rd); 233053541Sshin nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 233153541Sshin if (nd6_options(&ndopts) < 0) { 233278064Sume nd6log((LOG_INFO, "icmp6_redirect_input: " 2333120891Sume "invalid ND option, rejected: %s\n", 2334120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 233578064Sume /* nd6_options have incremented stats */ 233662587Sitojun goto freeit; 233753541Sshin } 233853541Sshin 233953541Sshin if (ndopts.nd_opts_tgt_lladdr) { 234053541Sshin lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 234153541Sshin lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 234253541Sshin } 234353541Sshin 234453541Sshin if (ndopts.nd_opts_rh) { 234553541Sshin redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len; 234653541Sshin redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */ 234753541Sshin } 234853541Sshin 234953541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 235078064Sume nd6log((LOG_INFO, 2351120891Sume "icmp6_redirect_input: lladdrlen mismatch for %s " 2352120891Sume "(if %d, icmp6 packet %d): %s\n", 2353165118Sbz ip6_sprintf(ip6buf, &redtgt6), 2354165118Sbz ifp->if_addrlen, lladdrlen - 2, 2355120891Sume icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 235678064Sume goto bad; 235753541Sshin } 235853541Sshin 235953541Sshin /* RFC 2461 8.3 */ 236053541Sshin nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 2361120891Sume is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 236253541Sshin 236395023Ssuz if (!is_onlink) { /* better router case. perform rtredirect. */ 236453541Sshin /* perform rtredirect */ 236553541Sshin struct sockaddr_in6 sdst; 236653541Sshin struct sockaddr_in6 sgw; 236753541Sshin struct sockaddr_in6 ssrc; 236853541Sshin 236953541Sshin bzero(&sdst, sizeof(sdst)); 237053541Sshin bzero(&sgw, sizeof(sgw)); 237153541Sshin bzero(&ssrc, sizeof(ssrc)); 237253541Sshin sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; 237353541Sshin sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = 237453541Sshin sizeof(struct sockaddr_in6); 237553541Sshin bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); 237653541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 237753541Sshin bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 237853541Sshin rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw, 2379120891Sume (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST, 2380120891Sume (struct sockaddr *)&ssrc); 238153541Sshin } 238253541Sshin /* finally update cached route in each socket via pfctlinput */ 238353541Sshin { 238453541Sshin struct sockaddr_in6 sdst; 238553541Sshin 238653541Sshin bzero(&sdst, sizeof(sdst)); 238753541Sshin sdst.sin6_family = AF_INET6; 238853541Sshin sdst.sin6_len = sizeof(struct sockaddr_in6); 238953541Sshin bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 239053541Sshin pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); 2391171167Sgnn#ifdef IPSEC 239253541Sshin key_sa_routechange((struct sockaddr *)&sdst); 2393171167Sgnn#endif /* IPSEC */ 239453541Sshin } 239562587Sitojun 239662587Sitojun freeit: 239762587Sitojun m_freem(m); 239878064Sume return; 239978064Sume 240078064Sume bad: 240178064Sume icmp6stat.icp6s_badredirect++; 240278064Sume m_freem(m); 240353541Sshin} 240453541Sshin 240553541Sshinvoid 2406171259Sdelphijicmp6_redirect_output(struct mbuf *m0, struct rtentry *rt) 240753541Sshin{ 240853541Sshin struct ifnet *ifp; /* my outgoing interface */ 240953541Sshin struct in6_addr *ifp_ll6; 241053541Sshin struct in6_addr *router_ll6; 241153541Sshin struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 241253541Sshin struct mbuf *m = NULL; /* newly allocated one */ 241353541Sshin struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 241453541Sshin struct nd_redirect *nd_rd; 241553541Sshin size_t maxlen; 241653541Sshin u_char *p; 241753541Sshin struct ifnet *outif = NULL; 241862587Sitojun struct sockaddr_in6 src_sa; 241953541Sshin 242062587Sitojun icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0); 242162587Sitojun 242253541Sshin /* if we are not router, we don't send icmp6 redirect */ 2423151539Ssuz if (!ip6_forwarding) 242453541Sshin goto fail; 242553541Sshin 242653541Sshin /* sanity check */ 242753541Sshin if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) 242853541Sshin goto fail; 242953541Sshin 243053541Sshin /* 243153541Sshin * Address check: 243253541Sshin * the source address must identify a neighbor, and 243353541Sshin * the destination address must not be a multicast address 243453541Sshin * [RFC 2461, sec 8.2] 243553541Sshin */ 243653541Sshin sip6 = mtod(m0, struct ip6_hdr *); 243762587Sitojun bzero(&src_sa, sizeof(src_sa)); 243862587Sitojun src_sa.sin6_family = AF_INET6; 243962587Sitojun src_sa.sin6_len = sizeof(src_sa); 244062587Sitojun src_sa.sin6_addr = sip6->ip6_src; 244162587Sitojun if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) 244253541Sshin goto fail; 244353541Sshin if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) 244453541Sshin goto fail; /* what should we do here? */ 244553541Sshin 244653541Sshin /* rate limit */ 244753541Sshin if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) 244853541Sshin goto fail; 244953541Sshin 245053541Sshin /* 245153541Sshin * Since we are going to append up to 1280 bytes (= IPV6_MMTU), 245253541Sshin * we almost always ask for an mbuf cluster for simplicity. 245353541Sshin * (MHLEN < IPV6_MMTU is almost always true) 245453541Sshin */ 245562587Sitojun#if IPV6_MMTU >= MCLBYTES 245662587Sitojun# error assumption failed about IPV6_MMTU and MCLBYTES 245762587Sitojun#endif 2458111119Simp MGETHDR(m, M_DONTWAIT, MT_HEADER); 245962587Sitojun if (m && IPV6_MMTU >= MHLEN) 2460111119Simp MCLGET(m, M_DONTWAIT); 246153541Sshin if (!m) 246253541Sshin goto fail; 246378064Sume m->m_pkthdr.rcvif = NULL; 246478064Sume m->m_len = 0; 246578064Sume maxlen = M_TRAILINGSPACE(m); 246653541Sshin maxlen = min(IPV6_MMTU, maxlen); 246753541Sshin /* just for safety */ 246862587Sitojun if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 246962587Sitojun ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { 247053541Sshin goto fail; 247162587Sitojun } 247253541Sshin 247353541Sshin { 247453541Sshin /* get ip6 linklocal address for ifp(my outgoing interface). */ 247562587Sitojun struct in6_ifaddr *ia; 247662587Sitojun if ((ia = in6ifa_ifpforlinklocal(ifp, 247762587Sitojun IN6_IFF_NOTREADY| 247862587Sitojun IN6_IFF_ANYCAST)) == NULL) 247953541Sshin goto fail; 248053541Sshin ifp_ll6 = &ia->ia_addr.sin6_addr; 248153541Sshin } 248253541Sshin 248353541Sshin /* get ip6 linklocal address for the router. */ 248453541Sshin if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { 248553541Sshin struct sockaddr_in6 *sin6; 248653541Sshin sin6 = (struct sockaddr_in6 *)rt->rt_gateway; 248753541Sshin router_ll6 = &sin6->sin6_addr; 248853541Sshin if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) 248953541Sshin router_ll6 = (struct in6_addr *)NULL; 249053541Sshin } else 249153541Sshin router_ll6 = (struct in6_addr *)NULL; 249253541Sshin 249353541Sshin /* ip6 */ 249453541Sshin ip6 = mtod(m, struct ip6_hdr *); 249553541Sshin ip6->ip6_flow = 0; 249662587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 249762587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 249853541Sshin /* ip6->ip6_plen will be set later */ 249953541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 250053541Sshin ip6->ip6_hlim = 255; 250153541Sshin /* ip6->ip6_src must be linklocal addr for my outgoing if. */ 250253541Sshin bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); 250353541Sshin bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); 250453541Sshin 250553541Sshin /* ND Redirect */ 250653541Sshin nd_rd = (struct nd_redirect *)(ip6 + 1); 250753541Sshin nd_rd->nd_rd_type = ND_REDIRECT; 250853541Sshin nd_rd->nd_rd_code = 0; 250953541Sshin nd_rd->nd_rd_reserved = 0; 251053541Sshin if (rt->rt_flags & RTF_GATEWAY) { 251153541Sshin /* 251253541Sshin * nd_rd->nd_rd_target must be a link-local address in 251353541Sshin * better router cases. 251453541Sshin */ 251553541Sshin if (!router_ll6) 251653541Sshin goto fail; 251753541Sshin bcopy(router_ll6, &nd_rd->nd_rd_target, 2518120892Sume sizeof(nd_rd->nd_rd_target)); 251953541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2520120892Sume sizeof(nd_rd->nd_rd_dst)); 252153541Sshin } else { 252253541Sshin /* make sure redtgt == reddst */ 252353541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, 2524120892Sume sizeof(nd_rd->nd_rd_target)); 252553541Sshin bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2526120892Sume sizeof(nd_rd->nd_rd_dst)); 252753541Sshin } 252853541Sshin 252953541Sshin p = (u_char *)(nd_rd + 1); 253053541Sshin 253153541Sshin if (!router_ll6) 253253541Sshin goto nolladdropt; 253353541Sshin 2534120892Sume { 2535120892Sume /* target lladdr option */ 2536120892Sume struct rtentry *rt_router = NULL; 2537120892Sume int len; 2538120892Sume struct sockaddr_dl *sdl; 2539120892Sume struct nd_opt_hdr *nd_opt; 2540120892Sume char *lladdr; 254153541Sshin 2542120892Sume rt_router = nd6_lookup(router_ll6, 0, ifp); 2543120892Sume if (!rt_router) 2544120892Sume goto nolladdropt; 2545120892Sume len = sizeof(*nd_opt) + ifp->if_addrlen; 2546120892Sume len = (len + 7) & ~7; /* round by 8 */ 2547120892Sume /* safety check */ 2548120892Sume if (len + (p - (u_char *)ip6) > maxlen) 2549120892Sume goto nolladdropt; 2550120892Sume if (!(rt_router->rt_flags & RTF_GATEWAY) && 2551120892Sume (rt_router->rt_flags & RTF_LLINFO) && 2552120892Sume (rt_router->rt_gateway->sa_family == AF_LINK) && 2553120892Sume (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) && 2554120892Sume sdl->sdl_alen) { 2555120892Sume nd_opt = (struct nd_opt_hdr *)p; 2556120892Sume nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 2557120892Sume nd_opt->nd_opt_len = len >> 3; 2558120892Sume lladdr = (char *)(nd_opt + 1); 2559120892Sume bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen); 2560120892Sume p += len; 2561120892Sume } 2562120893Sume } 256353541Sshinnolladdropt:; 256453541Sshin 256553541Sshin m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 256653541Sshin 256753541Sshin /* just to be safe */ 256862587Sitojun#ifdef M_DECRYPTED /*not openbsd*/ 256953541Sshin if (m0->m_flags & M_DECRYPTED) 257053541Sshin goto noredhdropt; 257162587Sitojun#endif 257262587Sitojun if (p - (u_char *)ip6 > maxlen) 257362587Sitojun goto noredhdropt; 257453541Sshin 2575120891Sume { 2576120891Sume /* redirected header option */ 2577120891Sume int len; 2578120891Sume struct nd_opt_rd_hdr *nd_opt_rh; 257953541Sshin 2580120891Sume /* 2581120891Sume * compute the maximum size for icmp6 redirect header option. 2582120891Sume * XXX room for auth header? 2583120891Sume */ 2584120891Sume len = maxlen - (p - (u_char *)ip6); 2585120891Sume len &= ~7; 258653541Sshin 2587120891Sume /* This is just for simplicity. */ 2588120891Sume if (m0->m_pkthdr.len != m0->m_len) { 2589120891Sume if (m0->m_next) { 2590120891Sume m_freem(m0->m_next); 2591120891Sume m0->m_next = NULL; 2592120891Sume } 2593120891Sume m0->m_pkthdr.len = m0->m_len; 259453541Sshin } 259553541Sshin 2596120891Sume /* 2597120891Sume * Redirected header option spec (RFC2461 4.6.3) talks nothing 2598120891Sume * about padding/truncate rule for the original IP packet. 2599120891Sume * From the discussion on IPv6imp in Feb 1999, 2600120891Sume * the consensus was: 2601120891Sume * - "attach as much as possible" is the goal 2602120891Sume * - pad if not aligned (original size can be guessed by 2603120891Sume * original ip6 header) 2604120891Sume * Following code adds the padding if it is simple enough, 2605120891Sume * and truncates if not. 2606120891Sume */ 2607120891Sume if (m0->m_next || m0->m_pkthdr.len != m0->m_len) 2608120891Sume panic("assumption failed in %s:%d", __FILE__, 2609120891Sume __LINE__); 261053541Sshin 2611120891Sume if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { 2612120891Sume /* not enough room, truncate */ 2613120891Sume m0->m_pkthdr.len = m0->m_len = len - 2614120891Sume sizeof(*nd_opt_rh); 2615120891Sume } else { 2616120891Sume /* enough room, pad or truncate */ 2617120891Sume size_t extra; 261853541Sshin 2619120891Sume extra = m0->m_pkthdr.len % 8; 2620120891Sume if (extra) { 2621120891Sume /* pad if easy enough, truncate if not */ 2622120891Sume if (8 - extra <= M_TRAILINGSPACE(m0)) { 2623120891Sume /* pad */ 2624120891Sume m0->m_len += (8 - extra); 2625120891Sume m0->m_pkthdr.len += (8 - extra); 2626120891Sume } else { 2627120891Sume /* truncate */ 2628120891Sume m0->m_pkthdr.len -= extra; 2629120891Sume m0->m_len -= extra; 2630120891Sume } 263153541Sshin } 2632120891Sume len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); 2633120891Sume m0->m_pkthdr.len = m0->m_len = len - 2634120891Sume sizeof(*nd_opt_rh); 263553541Sshin } 263653541Sshin 2637120891Sume nd_opt_rh = (struct nd_opt_rd_hdr *)p; 2638120891Sume bzero(nd_opt_rh, sizeof(*nd_opt_rh)); 2639120891Sume nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; 2640120891Sume nd_opt_rh->nd_opt_rh_len = len >> 3; 2641120891Sume p += sizeof(*nd_opt_rh); 2642120891Sume m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 264353541Sshin 2644120891Sume /* connect m0 to m */ 2645120891Sume m_tag_delete_chain(m0, NULL); 2646120891Sume m0->m_flags &= ~M_PKTHDR; 2647120891Sume m->m_next = m0; 2648120891Sume m->m_pkthdr.len = m->m_len + m0->m_len; 2649120891Sume m0 = NULL; 2650120891Sume } 265153541Sshinnoredhdropt:; 2652112781Ssuz if (m0) { 2653112781Ssuz m_freem(m0); 2654112781Ssuz m0 = NULL; 2655112781Ssuz } 265653541Sshin 2657121315Sume /* XXX: clear embedded link IDs in the inner header */ 2658121315Sume in6_clearscope(&sip6->ip6_src); 2659121315Sume in6_clearscope(&sip6->ip6_dst); 2660121315Sume in6_clearscope(&nd_rd->nd_rd_target); 2661121315Sume in6_clearscope(&nd_rd->nd_rd_dst); 266253541Sshin 266353541Sshin ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 266453541Sshin 266553541Sshin nd_rd->nd_rd_cksum = 0; 2666120891Sume nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2667120891Sume sizeof(*ip6), ntohs(ip6->ip6_plen)); 266853541Sshin 266953541Sshin /* send the packet to outside... */ 2670105194Ssam ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 267153541Sshin if (outif) { 267253541Sshin icmp6_ifstat_inc(outif, ifs6_out_msg); 267353541Sshin icmp6_ifstat_inc(outif, ifs6_out_redirect); 267453541Sshin } 267553541Sshin icmp6stat.icp6s_outhist[ND_REDIRECT]++; 267653541Sshin 267753541Sshin return; 267853541Sshin 267953541Sshinfail: 268053541Sshin if (m) 268153541Sshin m_freem(m); 268253541Sshin if (m0) 268353541Sshin m_freem(m0); 268453541Sshin} 268553541Sshin 268653541Sshin/* 268753541Sshin * ICMPv6 socket option processing. 268853541Sshin */ 268953541Sshinint 2690171259Sdelphijicmp6_ctloutput(struct socket *so, struct sockopt *sopt) 269153541Sshin{ 269253541Sshin int error = 0; 269353541Sshin int optlen; 269478064Sume struct inpcb *inp = sotoinpcb(so); 269553541Sshin int level, op, optname; 269653541Sshin 269753541Sshin if (sopt) { 269853541Sshin level = sopt->sopt_level; 269953541Sshin op = sopt->sopt_dir; 270053541Sshin optname = sopt->sopt_name; 270153541Sshin optlen = sopt->sopt_valsize; 270253541Sshin } else 270353541Sshin level = op = optname = optlen = 0; 270478064Sume 270553541Sshin if (level != IPPROTO_ICMPV6) { 270653541Sshin return EINVAL; 270753541Sshin } 270853541Sshin 270978064Sume switch (op) { 271053541Sshin case PRCO_SETOPT: 271153541Sshin switch (optname) { 271253541Sshin case ICMP6_FILTER: 271353541Sshin { 271453541Sshin struct icmp6_filter *p; 271553541Sshin 271653541Sshin if (optlen != sizeof(*p)) { 271753541Sshin error = EMSGSIZE; 271853541Sshin break; 271953541Sshin } 272053541Sshin if (inp->in6p_icmp6filt == NULL) { 272153541Sshin error = EINVAL; 272253541Sshin break; 272353541Sshin } 272453541Sshin error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen, 272553541Sshin optlen); 272653541Sshin break; 272753541Sshin } 272853541Sshin 272953541Sshin default: 273053541Sshin error = ENOPROTOOPT; 273153541Sshin break; 273253541Sshin } 273353541Sshin break; 273453541Sshin 273553541Sshin case PRCO_GETOPT: 273653541Sshin switch (optname) { 273753541Sshin case ICMP6_FILTER: 273853541Sshin { 273953541Sshin if (inp->in6p_icmp6filt == NULL) { 274053541Sshin error = EINVAL; 274153541Sshin break; 274253541Sshin } 274353541Sshin error = sooptcopyout(sopt, inp->in6p_icmp6filt, 274453541Sshin sizeof(struct icmp6_filter)); 274553541Sshin break; 274653541Sshin } 274753541Sshin 274853541Sshin default: 274953541Sshin error = ENOPROTOOPT; 275053541Sshin break; 275153541Sshin } 275253541Sshin break; 275353541Sshin } 275453541Sshin 2755120856Sume return (error); 275653541Sshin} 275753541Sshin 275853541Sshin/* 275953541Sshin * Perform rate limit check. 276053541Sshin * Returns 0 if it is okay to send the icmp6 packet. 276153541Sshin * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 276253541Sshin * limitation. 276353541Sshin * 276453541Sshin * XXX per-destination/type check necessary? 2765171259Sdelphij * 2766171259Sdelphij * dst - not used at this moment 2767171259Sdelphij * type - not used at this moment 2768171259Sdelphij * code - not used at this moment 276953541Sshin */ 277053541Sshinstatic int 2771171259Sdelphijicmp6_ratelimit(const struct in6_addr *dst, const int type, 2772171259Sdelphij const int code) 277353541Sshin{ 277462587Sitojun int ret; 277553541Sshin 277695023Ssuz ret = 0; /* okay to send */ 277753541Sshin 277862587Sitojun /* PPS limit */ 277978064Sume if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count, 278078064Sume icmp6errppslim)) { 278153541Sshin /* The packet is subject to rate limit */ 278262587Sitojun ret++; 278353541Sshin } 278453541Sshin 278562587Sitojun return ret; 278653541Sshin} 2787