Home | History | Annotate | Line # | Download | only in netinet6
icmp6.c revision 1.191
      1  1.191     ozaki /*	$NetBSD: icmp6.c,v 1.191 2016/07/05 03:40:52 ozaki-r Exp $	*/
      2   1.66    itojun /*	$KAME: icmp6.c,v 1.217 2001/06/20 15:03:29 jinmei Exp $	*/
      3    1.3   thorpej 
      4    1.2    itojun /*
      5    1.2    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6    1.2    itojun  * All rights reserved.
      7   1.29    itojun  *
      8    1.2    itojun  * Redistribution and use in source and binary forms, with or without
      9    1.2    itojun  * modification, are permitted provided that the following conditions
     10    1.2    itojun  * are met:
     11    1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     12    1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     13    1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14    1.2    itojun  *    notice, this list of conditions and the following disclaimer in the
     15    1.2    itojun  *    documentation and/or other materials provided with the distribution.
     16    1.2    itojun  * 3. Neither the name of the project nor the names of its contributors
     17    1.2    itojun  *    may be used to endorse or promote products derived from this software
     18    1.2    itojun  *    without specific prior written permission.
     19   1.29    itojun  *
     20    1.2    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21    1.2    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22    1.2    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23    1.2    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24    1.2    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25    1.2    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26    1.2    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27    1.2    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28    1.2    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29    1.2    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30    1.2    itojun  * SUCH DAMAGE.
     31    1.2    itojun  */
     32    1.2    itojun 
     33    1.2    itojun /*
     34    1.2    itojun  * Copyright (c) 1982, 1986, 1988, 1993
     35    1.2    itojun  *	The Regents of the University of California.  All rights reserved.
     36    1.2    itojun  *
     37    1.2    itojun  * Redistribution and use in source and binary forms, with or without
     38    1.2    itojun  * modification, are permitted provided that the following conditions
     39    1.2    itojun  * are met:
     40    1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     41    1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     42    1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     43    1.2    itojun  *    notice, this list of conditions and the following disclaimer in the
     44    1.2    itojun  *    documentation and/or other materials provided with the distribution.
     45   1.96       agc  * 3. Neither the name of the University nor the names of its contributors
     46    1.2    itojun  *    may be used to endorse or promote products derived from this software
     47    1.2    itojun  *    without specific prior written permission.
     48    1.2    itojun  *
     49    1.2    itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50    1.2    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51    1.2    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52    1.2    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53    1.2    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54    1.2    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55    1.2    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56    1.2    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57    1.2    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58    1.2    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59    1.2    itojun  * SUCH DAMAGE.
     60    1.2    itojun  *
     61    1.2    itojun  *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
     62    1.2    itojun  */
     63   1.71     lukem 
     64   1.71     lukem #include <sys/cdefs.h>
     65  1.191     ozaki __KERNEL_RCSID(0, "$NetBSD: icmp6.c,v 1.191 2016/07/05 03:40:52 ozaki-r Exp $");
     66    1.2    itojun 
     67  1.175     pooka #ifdef _KERNEL_OPT
     68    1.2    itojun #include "opt_inet.h"
     69    1.6   thorpej #include "opt_ipsec.h"
     70  1.175     pooka #endif
     71    1.2    itojun 
     72    1.2    itojun #include <sys/param.h>
     73    1.2    itojun #include <sys/systm.h>
     74  1.167     rmind #include <sys/kmem.h>
     75    1.2    itojun #include <sys/mbuf.h>
     76    1.2    itojun #include <sys/protosw.h>
     77    1.2    itojun #include <sys/socket.h>
     78    1.2    itojun #include <sys/socketvar.h>
     79    1.2    itojun #include <sys/time.h>
     80    1.2    itojun #include <sys/kernel.h>
     81    1.2    itojun #include <sys/syslog.h>
     82   1.12    itojun #include <sys/domain.h>
     83   1.70    simonb #include <sys/sysctl.h>
     84    1.2    itojun 
     85    1.2    itojun #include <net/if.h>
     86    1.2    itojun #include <net/route.h>
     87    1.2    itojun #include <net/if_dl.h>
     88    1.2    itojun #include <net/if_types.h>
     89    1.2    itojun 
     90    1.2    itojun #include <netinet/in.h>
     91    1.2    itojun #include <netinet/in_var.h>
     92   1.19    itojun #include <netinet/ip6.h>
     93    1.2    itojun #include <netinet6/ip6_var.h>
     94  1.145   thorpej #include <netinet6/ip6_private.h>
     95   1.19    itojun #include <netinet/icmp6.h>
     96  1.145   thorpej #include <netinet6/icmp6_private.h>
     97    1.2    itojun #include <netinet6/mld6_var.h>
     98    1.2    itojun #include <netinet6/in6_pcb.h>
     99    1.2    itojun #include <netinet6/nd6.h>
    100    1.2    itojun #include <netinet6/in6_ifattach.h>
    101   1.10    itojun #include <netinet6/ip6protosw.h>
    102  1.113    rpaulo #include <netinet6/scope6_var.h>
    103   1.12    itojun 
    104  1.162  christos #ifdef IPSEC
    105  1.129  degroote #include <netipsec/ipsec.h>
    106  1.129  degroote #include <netipsec/key.h>
    107  1.129  degroote #endif
    108  1.129  degroote 
    109  1.129  degroote 
    110    1.2    itojun #include "faith.h"
    111   1.64    itojun #if defined(NFAITH) && 0 < NFAITH
    112   1.64    itojun #include <net/if_faith.h>
    113   1.64    itojun #endif
    114    1.2    itojun 
    115   1.12    itojun #include <net/net_osdep.h>
    116   1.12    itojun 
    117   1.12    itojun extern struct domain inet6domain;
    118    1.2    itojun 
    119  1.145   thorpej percpu_t *icmp6stat_percpu;
    120    1.2    itojun 
    121  1.101    itojun extern struct inpcbtable raw6cbtable;
    122   1.35    itojun extern int icmp6errppslim;
    123   1.35    itojun static int icmp6errpps_count = 0;
    124   1.37    itojun static struct timeval icmp6errppslim_last;
    125   1.14    itojun extern int icmp6_nodeinfo;
    126   1.46    itojun 
    127   1.46    itojun /*
    128   1.46    itojun  * List of callbacks to notify when Path MTU changes are made.
    129   1.46    itojun  */
    130   1.46    itojun struct icmp6_mtudisc_callback {
    131   1.46    itojun 	LIST_ENTRY(icmp6_mtudisc_callback) mc_list;
    132  1.140    dyoung 	void (*mc_func)(struct in6_addr *);
    133   1.46    itojun };
    134   1.46    itojun 
    135   1.46    itojun LIST_HEAD(, icmp6_mtudisc_callback) icmp6_mtudisc_callbacks =
    136   1.46    itojun     LIST_HEAD_INITIALIZER(&icmp6_mtudisc_callbacks);
    137   1.46    itojun 
    138    1.8    itojun static struct rttimer_queue *icmp6_mtudisc_timeout_q = NULL;
    139    1.8    itojun extern int pmtu_expire;
    140    1.8    itojun 
    141   1.48    itojun /* XXX do these values make any sense? */
    142   1.55    itojun static int icmp6_mtudisc_hiwat = 1280;
    143   1.55    itojun static int icmp6_mtudisc_lowat = 256;
    144   1.55    itojun 
    145   1.55    itojun /*
    146   1.55    itojun  * keep track of # of redirect routes.
    147   1.55    itojun  */
    148   1.55    itojun static struct rttimer_queue *icmp6_redirect_timeout_q = NULL;
    149   1.55    itojun 
    150   1.55    itojun /* XXX experimental, turned off */
    151   1.55    itojun static int icmp6_redirect_hiwat = -1;
    152   1.55    itojun static int icmp6_redirect_lowat = -1;
    153   1.48    itojun 
    154  1.145   thorpej static void icmp6_errcount(u_int, int, int);
    155  1.140    dyoung static int icmp6_rip6_input(struct mbuf **, int);
    156  1.140    dyoung static int icmp6_ratelimit(const struct in6_addr *, const int, const int);
    157  1.140    dyoung static const char *icmp6_redirect_diag(struct in6_addr *,
    158  1.140    dyoung 	struct in6_addr *, struct in6_addr *);
    159  1.140    dyoung static struct mbuf *ni6_input(struct mbuf *, int);
    160  1.140    dyoung static struct mbuf *ni6_nametodns(const char *, int, int);
    161  1.140    dyoung static int ni6_dnsmatch(const char *, int, const char *, int);
    162  1.140    dyoung static int ni6_addrs(struct icmp6_nodeinfo *, struct mbuf *,
    163  1.140    dyoung 			  struct ifnet **, char *);
    164  1.140    dyoung static int ni6_store_addrs(struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
    165  1.140    dyoung 				struct ifnet *, int);
    166  1.140    dyoung static int icmp6_notify_error(struct mbuf *, int, int, int);
    167  1.140    dyoung static struct rtentry *icmp6_mtudisc_clone(struct sockaddr *);
    168  1.140    dyoung static void icmp6_mtudisc_timeout(struct rtentry *, struct rttimer *);
    169  1.140    dyoung static void icmp6_redirect_timeout(struct rtentry *, struct rttimer *);
    170  1.153     pooka static void sysctl_net_inet6_icmp6_setup(struct sysctllog **);
    171    1.2    itojun 
    172  1.116  christos 
    173    1.2    itojun void
    174  1.142      matt icmp6_init(void)
    175    1.2    itojun {
    176  1.153     pooka 
    177  1.153     pooka 	sysctl_net_inet6_icmp6_setup(NULL);
    178  1.115    rpaulo 	mld_init();
    179    1.8    itojun 	icmp6_mtudisc_timeout_q = rt_timer_queue_create(pmtu_expire);
    180   1.55    itojun 	icmp6_redirect_timeout_q = rt_timer_queue_create(icmp6_redirtimeout);
    181  1.145   thorpej 
    182  1.145   thorpej 	icmp6stat_percpu = percpu_alloc(sizeof(uint64_t) * ICMP6_NSTATS);
    183    1.2    itojun }
    184    1.2    itojun 
    185   1.35    itojun static void
    186  1.145   thorpej icmp6_errcount(u_int base, int type, int code)
    187   1.35    itojun {
    188   1.56    itojun 	switch (type) {
    189   1.35    itojun 	case ICMP6_DST_UNREACH:
    190   1.35    itojun 		switch (code) {
    191   1.35    itojun 		case ICMP6_DST_UNREACH_NOROUTE:
    192  1.145   thorpej 			ICMP6_STATINC(base + ICMP6_ERRSTAT_DST_UNREACH_NOROUTE);
    193   1.35    itojun 			return;
    194   1.35    itojun 		case ICMP6_DST_UNREACH_ADMIN:
    195  1.145   thorpej 			ICMP6_STATINC(base + ICMP6_ERRSTAT_DST_UNREACH_ADMIN);
    196   1.35    itojun 			return;
    197   1.35    itojun 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
    198  1.145   thorpej 			ICMP6_STATINC(base +
    199  1.145   thorpej 				      ICMP6_ERRSTAT_DST_UNREACH_BEYONDSCOPE);
    200   1.35    itojun 			return;
    201   1.35    itojun 		case ICMP6_DST_UNREACH_ADDR:
    202  1.145   thorpej 			ICMP6_STATINC(base + ICMP6_ERRSTAT_DST_UNREACH_ADDR);
    203   1.35    itojun 			return;
    204   1.35    itojun 		case ICMP6_DST_UNREACH_NOPORT:
    205  1.145   thorpej 			ICMP6_STATINC(base + ICMP6_ERRSTAT_DST_UNREACH_NOPORT);
    206   1.35    itojun 			return;
    207   1.35    itojun 		}
    208   1.35    itojun 		break;
    209   1.35    itojun 	case ICMP6_PACKET_TOO_BIG:
    210  1.145   thorpej 		ICMP6_STATINC(base + ICMP6_ERRSTAT_PACKET_TOO_BIG);
    211   1.35    itojun 		return;
    212   1.35    itojun 	case ICMP6_TIME_EXCEEDED:
    213   1.56    itojun 		switch (code) {
    214   1.35    itojun 		case ICMP6_TIME_EXCEED_TRANSIT:
    215  1.145   thorpej 			ICMP6_STATINC(base + ICMP6_ERRSTAT_TIME_EXCEED_TRANSIT);
    216   1.35    itojun 			return;
    217   1.35    itojun 		case ICMP6_TIME_EXCEED_REASSEMBLY:
    218  1.145   thorpej 			ICMP6_STATINC(base +
    219  1.145   thorpej 				      ICMP6_ERRSTAT_TIME_EXCEED_REASSEMBLY);
    220   1.35    itojun 			return;
    221   1.35    itojun 		}
    222   1.35    itojun 		break;
    223   1.35    itojun 	case ICMP6_PARAM_PROB:
    224   1.56    itojun 		switch (code) {
    225   1.35    itojun 		case ICMP6_PARAMPROB_HEADER:
    226  1.145   thorpej 			ICMP6_STATINC(base + ICMP6_ERRSTAT_PARAMPROB_HEADER);
    227   1.35    itojun 			return;
    228   1.35    itojun 		case ICMP6_PARAMPROB_NEXTHEADER:
    229  1.145   thorpej 			ICMP6_STATINC(base +
    230  1.145   thorpej 				      ICMP6_ERRSTAT_PARAMPROB_NEXTHEADER);
    231   1.35    itojun 			return;
    232   1.35    itojun 		case ICMP6_PARAMPROB_OPTION:
    233  1.145   thorpej 			ICMP6_STATINC(base + ICMP6_ERRSTAT_PARAMPROB_OPTION);
    234   1.35    itojun 			return;
    235   1.35    itojun 		}
    236   1.35    itojun 		break;
    237   1.35    itojun 	case ND_REDIRECT:
    238  1.145   thorpej 		ICMP6_STATINC(base + ICMP6_ERRSTAT_REDIRECT);
    239   1.35    itojun 		return;
    240   1.35    itojun 	}
    241  1.145   thorpej 	ICMP6_STATINC(base + ICMP6_ERRSTAT_UNKNOWN);
    242   1.35    itojun }
    243   1.35    itojun 
    244    1.2    itojun /*
    245   1.46    itojun  * Register a Path MTU Discovery callback.
    246   1.46    itojun  */
    247   1.46    itojun void
    248  1.140    dyoung icmp6_mtudisc_callback_register(void (*func)(struct in6_addr *))
    249   1.46    itojun {
    250   1.46    itojun 	struct icmp6_mtudisc_callback *mc;
    251   1.46    itojun 
    252   1.46    itojun 	for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL;
    253   1.46    itojun 	     mc = LIST_NEXT(mc, mc_list)) {
    254   1.46    itojun 		if (mc->mc_func == func)
    255   1.46    itojun 			return;
    256   1.46    itojun 	}
    257   1.46    itojun 
    258  1.167     rmind 	mc = kmem_alloc(sizeof(*mc), KM_SLEEP);
    259   1.46    itojun 	mc->mc_func = func;
    260   1.46    itojun 	LIST_INSERT_HEAD(&icmp6_mtudisc_callbacks, mc, mc_list);
    261   1.46    itojun }
    262   1.46    itojun 
    263   1.46    itojun /*
    264  1.113    rpaulo  * A wrapper function for icmp6_error() necessary when the erroneous packet
    265  1.113    rpaulo  * may not contain enough scope zone information.
    266  1.113    rpaulo  */
    267  1.113    rpaulo void
    268  1.133  christos icmp6_error2(struct mbuf *m, int type, int code, int param,
    269  1.133  christos 	struct ifnet *ifp)
    270  1.113    rpaulo {
    271  1.113    rpaulo 	struct ip6_hdr *ip6;
    272  1.113    rpaulo 
    273  1.113    rpaulo 	if (ifp == NULL)
    274  1.113    rpaulo 		return;
    275  1.113    rpaulo 
    276  1.113    rpaulo 	if (m->m_len < sizeof(struct ip6_hdr)) {
    277  1.113    rpaulo 		m = m_pullup(m, sizeof(struct ip6_hdr));
    278  1.113    rpaulo 		if (m == NULL)
    279  1.113    rpaulo 			return;
    280  1.113    rpaulo 	}
    281  1.113    rpaulo 
    282  1.113    rpaulo 	ip6 = mtod(m, struct ip6_hdr *);
    283  1.113    rpaulo 
    284  1.113    rpaulo 	if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
    285  1.113    rpaulo 		return;
    286  1.113    rpaulo 	if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
    287  1.113    rpaulo 		return;
    288  1.113    rpaulo 
    289  1.113    rpaulo 	icmp6_error(m, type, code, param);
    290  1.113    rpaulo }
    291  1.113    rpaulo 
    292  1.113    rpaulo /*
    293    1.2    itojun  * Generate an error packet of type error in response to bad IP6 packet.
    294    1.2    itojun  */
    295    1.2    itojun void
    296  1.133  christos icmp6_error(struct mbuf *m, int type, int code, int param)
    297    1.2    itojun {
    298    1.2    itojun 	struct ip6_hdr *oip6, *nip6;
    299    1.2    itojun 	struct icmp6_hdr *icmp6;
    300   1.17     itohy 	u_int preplen;
    301    1.2    itojun 	int off;
    302   1.27    itojun 	int nxt;
    303    1.2    itojun 
    304  1.145   thorpej 	ICMP6_STATINC(ICMP6_STAT_ERROR);
    305    1.2    itojun 
    306   1.35    itojun 	/* count per-type-code statistics */
    307  1.145   thorpej 	icmp6_errcount(ICMP6_STAT_OUTERRHIST, type, code);
    308   1.35    itojun 
    309   1.23    itojun 	if (m->m_flags & M_DECRYPTED) {
    310  1.145   thorpej 		ICMP6_STATINC(ICMP6_STAT_CANTERROR);
    311    1.2    itojun 		goto freeit;
    312   1.23    itojun 	}
    313    1.2    itojun 
    314  1.134    dyoung 	if (M_UNWRITABLE(m, sizeof(struct ip6_hdr)) &&
    315  1.134    dyoung 	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL)
    316  1.134    dyoung 		return;
    317    1.2    itojun 	oip6 = mtod(m, struct ip6_hdr *);
    318    1.2    itojun 
    319    1.2    itojun 	/*
    320   1.67    itojun 	 * If the destination address of the erroneous packet is a multicast
    321   1.67    itojun 	 * address, or the packet was sent using link-layer multicast,
    322   1.67    itojun 	 * we should basically suppress sending an error (RFC 2463, Section
    323   1.67    itojun 	 * 2.4).
    324   1.67    itojun 	 * We have two exceptions (the item e.2 in that section):
    325   1.67    itojun 	 * - the Pakcet Too Big message can be sent for path MTU discovery.
    326   1.67    itojun 	 * - the Parameter Problem Message that can be allowed an icmp6 error
    327   1.67    itojun 	 *   in the option type field.  This check has been done in
    328   1.67    itojun 	 *   ip6_unknown_opt(), so we can just check the type and code.
    329    1.2    itojun 	 */
    330    1.2    itojun 	if ((m->m_flags & (M_BCAST|M_MCAST) ||
    331    1.2    itojun 	     IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
    332    1.2    itojun 	    (type != ICMP6_PACKET_TOO_BIG &&
    333    1.2    itojun 	     (type != ICMP6_PARAM_PROB ||
    334    1.2    itojun 	      code != ICMP6_PARAMPROB_OPTION)))
    335    1.2    itojun 		goto freeit;
    336    1.2    itojun 
    337   1.67    itojun 	/*
    338   1.67    itojun 	 * RFC 2463, 2.4 (e.5): source address check.
    339   1.67    itojun 	 * XXX: the case of anycast source?
    340   1.67    itojun 	 */
    341    1.2    itojun 	if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
    342    1.2    itojun 	    IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
    343    1.2    itojun 		goto freeit;
    344    1.2    itojun 
    345    1.2    itojun 	/*
    346   1.27    itojun 	 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
    347   1.27    itojun 	 * don't do it.
    348    1.2    itojun 	 */
    349   1.27    itojun 	nxt = -1;
    350   1.28    itojun 	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
    351   1.27    itojun 	if (off >= 0 && nxt == IPPROTO_ICMPV6) {
    352    1.2    itojun 		struct icmp6_hdr *icp;
    353    1.2    itojun 
    354   1.27    itojun 		IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
    355   1.27    itojun 			sizeof(*icp));
    356   1.27    itojun 		if (icp == NULL) {
    357  1.145   thorpej 			ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    358   1.27    itojun 			return;
    359   1.27    itojun 		}
    360   1.27    itojun 		if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
    361   1.27    itojun 		    icp->icmp6_type == ND_REDIRECT) {
    362   1.27    itojun 			/*
    363   1.27    itojun 			 * ICMPv6 error
    364   1.27    itojun 			 * Special case: for redirect (which is
    365   1.27    itojun 			 * informational) we must not send icmp6 error.
    366   1.27    itojun 			 */
    367  1.145   thorpej 			ICMP6_STATINC(ICMP6_STAT_CANTERROR);
    368   1.27    itojun 			goto freeit;
    369   1.27    itojun 		} else {
    370   1.27    itojun 			/* ICMPv6 informational - send the error */
    371    1.2    itojun 		}
    372   1.67    itojun 	}
    373   1.67    itojun #if 0 /* controversial */
    374   1.67    itojun 	else if (off >= 0 && nxt == IPPROTO_ESP) {
    375   1.67    itojun 		/*
    376   1.67    itojun 		 * It could be ICMPv6 error inside ESP.  Take a safer side,
    377   1.67    itojun 		 * don't respond.
    378   1.67    itojun 		 */
    379  1.145   thorpej 		ICMP6_STATINC(ICMP6_STAT_CANTERROR);
    380   1.67    itojun 		goto freeit;
    381   1.67    itojun 	}
    382   1.67    itojun #endif
    383   1.67    itojun 	else {
    384   1.27    itojun 		/* non-ICMPv6 - send the error */
    385    1.2    itojun 	}
    386    1.2    itojun 
    387    1.2    itojun 	oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
    388    1.2    itojun 
    389    1.2    itojun 	/* Finally, do rate limitation check. */
    390    1.2    itojun 	if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
    391  1.145   thorpej 		ICMP6_STATINC(ICMP6_STAT_TOOFREQ);
    392    1.2    itojun 		goto freeit;
    393    1.2    itojun 	}
    394    1.2    itojun 
    395    1.2    itojun 	/*
    396    1.2    itojun 	 * OK, ICMP6 can be generated.
    397    1.2    itojun 	 */
    398    1.2    itojun 
    399    1.2    itojun 	if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
    400    1.2    itojun 		m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
    401    1.2    itojun 
    402   1.17     itohy 	preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
    403   1.17     itohy 	M_PREPEND(m, preplen, M_DONTWAIT);
    404  1.134    dyoung 	if (m && M_UNWRITABLE(m, preplen))
    405   1.17     itohy 		m = m_pullup(m, preplen);
    406    1.2    itojun 	if (m == NULL) {
    407  1.180     ozaki 		nd6log(LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__);
    408    1.2    itojun 		return;
    409    1.2    itojun 	}
    410    1.2    itojun 
    411    1.2    itojun 	nip6 = mtod(m, struct ip6_hdr *);
    412    1.2    itojun 	nip6->ip6_src  = oip6->ip6_src;
    413    1.2    itojun 	nip6->ip6_dst  = oip6->ip6_dst;
    414    1.2    itojun 
    415  1.113    rpaulo 	in6_clearscope(&oip6->ip6_src);
    416  1.113    rpaulo 	in6_clearscope(&oip6->ip6_dst);
    417    1.2    itojun 
    418    1.2    itojun 	icmp6 = (struct icmp6_hdr *)(nip6 + 1);
    419    1.2    itojun 	icmp6->icmp6_type = type;
    420    1.2    itojun 	icmp6->icmp6_code = code;
    421    1.7    itojun 	icmp6->icmp6_pptr = htonl((u_int32_t)param);
    422   1.63    itojun 
    423   1.63    itojun 	/*
    424   1.63    itojun 	 * icmp6_reflect() is designed to be in the input path.
    425  1.114    rpaulo 	 * icmp6_error() can be called from both input and output path,
    426   1.63    itojun 	 * and if we are in output path rcvif could contain bogus value.
    427   1.63    itojun 	 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
    428   1.63    itojun 	 * information in ip header (nip6).
    429   1.63    itojun 	 */
    430  1.187     ozaki 	m_reset_rcvif(m);
    431    1.2    itojun 
    432  1.145   thorpej 	ICMP6_STATINC(ICMP6_STAT_OUTHIST + type);
    433   1.67    itojun 	icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
    434   1.27    itojun 
    435   1.27    itojun 	return;
    436   1.27    itojun 
    437   1.27    itojun   freeit:
    438   1.27    itojun 	/*
    439  1.114    rpaulo 	 * If we can't tell whether or not we can generate ICMP6, free it.
    440   1.27    itojun 	 */
    441   1.27    itojun 	m_freem(m);
    442    1.2    itojun }
    443    1.2    itojun 
    444    1.2    itojun /*
    445    1.2    itojun  * Process a received ICMP6 message.
    446    1.2    itojun  */
    447    1.2    itojun int
    448  1.123  christos icmp6_input(struct mbuf **mp, int *offp, int proto)
    449    1.2    itojun {
    450    1.2    itojun 	struct mbuf *m = *mp, *n;
    451    1.2    itojun 	struct ip6_hdr *ip6, *nip6;
    452    1.2    itojun 	struct icmp6_hdr *icmp6, *nicmp6;
    453    1.2    itojun 	int off = *offp;
    454    1.2    itojun 	int icmp6len = m->m_pkthdr.len - *offp;
    455  1.148    bouyer 	int code, sum, noff, i;
    456  1.188     ozaki 	struct ifnet *rcvif;
    457  1.188     ozaki 	struct psref psref;
    458    1.2    itojun 
    459  1.188     ozaki 	rcvif = m_get_rcvif_psref(m, &psref);
    460  1.190     ozaki 	if (__predict_false(rcvif == NULL))
    461  1.190     ozaki 		goto freeit;
    462  1.190     ozaki 
    463  1.116  christos #define ICMP6_MAXLEN (sizeof(*nip6) + sizeof(*nicmp6) + 4)
    464  1.116  christos 	KASSERT(ICMP6_MAXLEN < MCLBYTES);
    465  1.185     ozaki 	icmp6_ifstat_inc(rcvif, ifs6_in_msg);
    466   1.72    itojun 
    467    1.2    itojun 	/*
    468    1.2    itojun 	 * Locate icmp6 structure in mbuf, and check
    469    1.2    itojun 	 * that not corrupted and of at least minimum length
    470    1.2    itojun 	 */
    471    1.2    itojun 
    472    1.2    itojun 	if (icmp6len < sizeof(struct icmp6_hdr)) {
    473  1.145   thorpej 		ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    474  1.185     ozaki 		icmp6_ifstat_inc(rcvif, ifs6_in_error);
    475    1.2    itojun 		goto freeit;
    476    1.2    itojun 	}
    477    1.2    itojun 
    478  1.148    bouyer 	i = off + sizeof(*icmp6);
    479  1.148    bouyer 	if ((m->m_len < i || M_READONLY(m)) && (m = m_pullup(m, i)) == 0) {
    480  1.148    bouyer 		ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    481  1.156  drochner #if 0 /* m is 0 here */
    482  1.185     ozaki 		icmp6_ifstat_inc(rcvif, ifs6_in_error);
    483  1.156  drochner #endif
    484  1.148    bouyer 		goto freeit;
    485  1.148    bouyer 	}
    486  1.148    bouyer 	ip6 = mtod(m, struct ip6_hdr *);
    487    1.2    itojun 	/*
    488    1.2    itojun 	 * calculate the checksum
    489    1.2    itojun 	 */
    490   1.12    itojun 	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
    491   1.12    itojun 	if (icmp6 == NULL) {
    492  1.188     ozaki 		m_put_rcvif_psref(rcvif, &psref);
    493  1.145   thorpej 		ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    494  1.106    itojun 		/* m is invalid */
    495  1.185     ozaki 		/*icmp6_ifstat_inc(rcvif, ifs6_in_error);*/
    496   1.12    itojun 		return IPPROTO_DONE;
    497   1.12    itojun 	}
    498   1.83   thorpej 	KASSERT(IP6_HDR_ALIGNED_P(icmp6));
    499    1.2    itojun 	code = icmp6->icmp6_code;
    500    1.2    itojun 
    501    1.2    itojun 	if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
    502  1.180     ozaki 		nd6log(LOG_ERR, "ICMP6 checksum error(%d|%x) %s\n",
    503  1.180     ozaki 		    icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src));
    504  1.145   thorpej 		ICMP6_STATINC(ICMP6_STAT_CHECKSUM);
    505  1.185     ozaki 		icmp6_ifstat_inc(rcvif, ifs6_in_error);
    506    1.2    itojun 		goto freeit;
    507    1.2    itojun 	}
    508    1.2    itojun 
    509    1.2    itojun #if defined(NFAITH) && 0 < NFAITH
    510   1.64    itojun 	if (faithprefix(&ip6->ip6_dst)) {
    511    1.2    itojun 		/*
    512    1.2    itojun 		 * Deliver very specific ICMP6 type only.
    513  1.114    rpaulo 		 * This is important to deliver TOOBIG.  Otherwise PMTUD
    514    1.2    itojun 		 * will not work.
    515    1.2    itojun 		 */
    516    1.2    itojun 		switch (icmp6->icmp6_type) {
    517    1.2    itojun 		case ICMP6_DST_UNREACH:
    518    1.2    itojun 		case ICMP6_PACKET_TOO_BIG:
    519    1.2    itojun 		case ICMP6_TIME_EXCEEDED:
    520    1.2    itojun 			break;
    521    1.2    itojun 		default:
    522    1.2    itojun 			goto freeit;
    523    1.2    itojun 		}
    524    1.2    itojun 	}
    525    1.2    itojun #endif
    526    1.2    itojun 
    527  1.145   thorpej 	ICMP6_STATINC(ICMP6_STAT_INHIST + icmp6->icmp6_type);
    528    1.2    itojun 
    529    1.2    itojun 	switch (icmp6->icmp6_type) {
    530    1.2    itojun 	case ICMP6_DST_UNREACH:
    531  1.185     ozaki 		icmp6_ifstat_inc(rcvif, ifs6_in_dstunreach);
    532    1.2    itojun 		switch (code) {
    533    1.2    itojun 		case ICMP6_DST_UNREACH_NOROUTE:
    534    1.2    itojun 			code = PRC_UNREACH_NET;
    535    1.2    itojun 			break;
    536    1.2    itojun 		case ICMP6_DST_UNREACH_ADMIN:
    537  1.185     ozaki 			icmp6_ifstat_inc(rcvif, ifs6_in_adminprohib);
    538   1.23    itojun 			code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
    539   1.23    itojun 			break;
    540    1.2    itojun 		case ICMP6_DST_UNREACH_ADDR:
    541   1.23    itojun 			code = PRC_HOSTDEAD;
    542    1.2    itojun 			break;
    543   1.23    itojun #ifdef COMPAT_RFC1885
    544    1.2    itojun 		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
    545    1.2    itojun 			code = PRC_UNREACH_SRCFAIL;
    546    1.2    itojun 			break;
    547   1.23    itojun #else
    548   1.23    itojun 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
    549   1.23    itojun 			/* I mean "source address was incorrect." */
    550   1.67    itojun 			code = PRC_UNREACH_NET;
    551   1.23    itojun 			break;
    552   1.29    itojun #endif
    553    1.2    itojun 		case ICMP6_DST_UNREACH_NOPORT:
    554    1.2    itojun 			code = PRC_UNREACH_PORT;
    555    1.2    itojun 			break;
    556    1.2    itojun 		default:
    557    1.2    itojun 			goto badcode;
    558    1.2    itojun 		}
    559    1.2    itojun 		goto deliver;
    560    1.2    itojun 
    561    1.2    itojun 	case ICMP6_PACKET_TOO_BIG:
    562  1.185     ozaki 		icmp6_ifstat_inc(rcvif, ifs6_in_pkttoobig);
    563    1.2    itojun 
    564  1.150   adrianp 		/*
    565  1.150   adrianp 		 * MTU is checked in icmp6_mtudisc.
    566  1.150   adrianp 		 */
    567    1.2    itojun 		code = PRC_MSGSIZE;
    568    1.2    itojun 
    569   1.23    itojun 		/*
    570   1.23    itojun 		 * Updating the path MTU will be done after examining
    571   1.23    itojun 		 * intermediate extension headers.
    572   1.23    itojun 		 */
    573    1.2    itojun 		goto deliver;
    574    1.2    itojun 
    575    1.2    itojun 	case ICMP6_TIME_EXCEEDED:
    576  1.185     ozaki 		icmp6_ifstat_inc(rcvif, ifs6_in_timeexceed);
    577    1.2    itojun 		switch (code) {
    578    1.2    itojun 		case ICMP6_TIME_EXCEED_TRANSIT:
    579   1.67    itojun 			code = PRC_TIMXCEED_INTRANS;
    580   1.67    itojun 			break;
    581    1.2    itojun 		case ICMP6_TIME_EXCEED_REASSEMBLY:
    582   1.67    itojun 			code = PRC_TIMXCEED_REASS;
    583    1.2    itojun 			break;
    584    1.2    itojun 		default:
    585    1.2    itojun 			goto badcode;
    586    1.2    itojun 		}
    587    1.2    itojun 		goto deliver;
    588    1.2    itojun 
    589    1.2    itojun 	case ICMP6_PARAM_PROB:
    590  1.185     ozaki 		icmp6_ifstat_inc(rcvif, ifs6_in_paramprob);
    591    1.2    itojun 		switch (code) {
    592    1.2    itojun 		case ICMP6_PARAMPROB_NEXTHEADER:
    593    1.2    itojun 			code = PRC_UNREACH_PROTOCOL;
    594    1.2    itojun 			break;
    595    1.2    itojun 		case ICMP6_PARAMPROB_HEADER:
    596    1.2    itojun 		case ICMP6_PARAMPROB_OPTION:
    597    1.2    itojun 			code = PRC_PARAMPROB;
    598    1.2    itojun 			break;
    599    1.2    itojun 		default:
    600    1.2    itojun 			goto badcode;
    601    1.2    itojun 		}
    602    1.2    itojun 		goto deliver;
    603    1.2    itojun 
    604    1.2    itojun 	case ICMP6_ECHO_REQUEST:
    605  1.185     ozaki 		icmp6_ifstat_inc(rcvif, ifs6_in_echo);
    606    1.2    itojun 		if (code != 0)
    607    1.2    itojun 			goto badcode;
    608   1.67    itojun 		/*
    609   1.67    itojun 		 * Copy mbuf to send to two data paths: userland socket(s),
    610   1.67    itojun 		 * and to the querier (echo reply).
    611   1.67    itojun 		 * m: a copy for socket, n: a copy for querier
    612  1.120    dyoung 		 *
    613  1.120    dyoung 		 * If the first mbuf is shared, or the first mbuf is too short,
    614  1.120    dyoung 		 * copy the first part of the data into a fresh mbuf.
    615  1.120    dyoung 		 * Otherwise, we will wrongly overwrite both copies.
    616   1.67    itojun 		 */
    617   1.67    itojun 		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    618   1.67    itojun 			/* Give up local */
    619   1.67    itojun 			n = m;
    620   1.67    itojun 			m = NULL;
    621  1.120    dyoung 		} else if (M_READONLY(n) ||
    622   1.67    itojun 		    n->m_len < off + sizeof(struct icmp6_hdr)) {
    623    1.2    itojun 			struct mbuf *n0 = n;
    624    1.2    itojun 
    625    1.2    itojun 			/*
    626   1.67    itojun 			 * Prepare an internal mbuf.  m_pullup() doesn't
    627    1.2    itojun 			 * always copy the length we specified.
    628    1.2    itojun 			 */
    629  1.121    dyoung 			if ((n = m_dup(n0, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    630   1.67    itojun 				/* Give up local */
    631   1.67    itojun 				n = m;
    632   1.67    itojun 				m = NULL;
    633    1.2    itojun 			}
    634  1.120    dyoung 			m_freem(n0);
    635    1.2    itojun 		}
    636  1.127    dyoung 		IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off,
    637  1.127    dyoung 		    sizeof(*nicmp6));
    638  1.164     joerg 		if (nicmp6 == NULL)
    639  1.164     joerg 			goto freeit;
    640    1.2    itojun 		nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
    641    1.2    itojun 		nicmp6->icmp6_code = 0;
    642   1.10    itojun 		if (n) {
    643  1.145   thorpej 			uint64_t *icmp6s = ICMP6_STAT_GETREF();
    644  1.145   thorpej 			icmp6s[ICMP6_STAT_REFLECT]++;
    645  1.145   thorpej 			icmp6s[ICMP6_STAT_OUTHIST + ICMP6_ECHO_REPLY]++;
    646  1.145   thorpej 			ICMP6_STAT_PUTREF();
    647  1.120    dyoung 			icmp6_reflect(n, off);
    648   1.10    itojun 		}
    649   1.67    itojun 		if (!m)
    650   1.67    itojun 			goto freeit;
    651    1.2    itojun 		break;
    652    1.2    itojun 
    653    1.2    itojun 	case ICMP6_ECHO_REPLY:
    654  1.185     ozaki 		icmp6_ifstat_inc(rcvif, ifs6_in_echoreply);
    655    1.2    itojun 		if (code != 0)
    656    1.2    itojun 			goto badcode;
    657    1.2    itojun 		break;
    658    1.2    itojun 
    659   1.92    itojun 	case MLD_LISTENER_QUERY:
    660   1.92    itojun 	case MLD_LISTENER_REPORT:
    661   1.92    itojun 		if (icmp6len < sizeof(struct mld_hdr))
    662    1.2    itojun 			goto badlen;
    663   1.92    itojun 		if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
    664  1.185     ozaki 			icmp6_ifstat_inc(rcvif, ifs6_in_mldquery);
    665   1.12    itojun 		else
    666  1.185     ozaki 			icmp6_ifstat_inc(rcvif, ifs6_in_mldreport);
    667   1.23    itojun 		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    668   1.23    itojun 			/* give up local */
    669  1.115    rpaulo 			mld_input(m, off);
    670   1.23    itojun 			m = NULL;
    671   1.23    itojun 			goto freeit;
    672   1.23    itojun 		}
    673  1.115    rpaulo 		mld_input(n, off);
    674    1.2    itojun 		/* m stays. */
    675    1.2    itojun 		break;
    676    1.2    itojun 
    677   1.92    itojun 	case MLD_LISTENER_DONE:
    678  1.185     ozaki 		icmp6_ifstat_inc(rcvif, ifs6_in_mlddone);
    679   1.92    itojun 		if (icmp6len < sizeof(struct mld_hdr))	/* necessary? */
    680    1.2    itojun 			goto badlen;
    681    1.2    itojun 		break;		/* nothing to be done in kernel */
    682    1.2    itojun 
    683   1.92    itojun 	case MLD_MTRACE_RESP:
    684   1.92    itojun 	case MLD_MTRACE:
    685   1.74    itojun 		/* XXX: these two are experimental.  not officially defined. */
    686   1.12    itojun 		/* XXX: per-interface statistics? */
    687   1.23    itojun 		break;		/* just pass it to applications */
    688   1.12    itojun 
    689    1.2    itojun 	case ICMP6_WRUREQUEST:	/* ICMP6_FQDN_QUERY */
    690    1.2    itojun 	    {
    691    1.2    itojun 		enum { WRU, FQDN } mode;
    692    1.2    itojun 
    693   1.14    itojun 		if (!icmp6_nodeinfo)
    694   1.14    itojun 			break;
    695   1.14    itojun 
    696    1.2    itojun 		if (icmp6len == sizeof(struct icmp6_hdr) + 4)
    697    1.2    itojun 			mode = WRU;
    698   1.31    itojun 		else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
    699    1.2    itojun 			mode = FQDN;
    700    1.2    itojun 		else
    701    1.2    itojun 			goto badlen;
    702    1.2    itojun 
    703    1.2    itojun 		if (mode == FQDN) {
    704   1.67    itojun 			n = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
    705   1.14    itojun 			if (n)
    706   1.14    itojun 				n = ni6_input(n, off);
    707   1.31    itojun 			/* XXX meaningless if n == NULL */
    708   1.31    itojun 			noff = sizeof(struct ip6_hdr);
    709   1.14    itojun 		} else {
    710    1.2    itojun 			u_char *p;
    711  1.116  christos 			int maxhlen;
    712    1.2    itojun 
    713   1.81    itojun 			if ((icmp6_nodeinfo & 5) != 5)
    714   1.67    itojun 				break;
    715   1.67    itojun 
    716   1.31    itojun 			if (code != 0)
    717   1.31    itojun 				goto badcode;
    718    1.2    itojun 			MGETHDR(n, M_DONTWAIT, m->m_type);
    719  1.116  christos 			if (n && ICMP6_MAXLEN > MHLEN) {
    720   1.23    itojun 				MCLGET(n, M_DONTWAIT);
    721   1.23    itojun 				if ((n->m_flags & M_EXT) == 0) {
    722   1.23    itojun 					m_free(n);
    723   1.23    itojun 					n = NULL;
    724   1.23    itojun 				}
    725   1.23    itojun 			}
    726    1.2    itojun 			if (n == NULL) {
    727    1.2    itojun 				/* Give up remote */
    728    1.2    itojun 				break;
    729    1.2    itojun 			}
    730  1.187     ozaki 			m_reset_rcvif(n);
    731   1.23    itojun 			n->m_len = 0;
    732  1.116  christos 			maxhlen = M_TRAILINGSPACE(n) - ICMP6_MAXLEN;
    733  1.170  christos 			if (maxhlen < 0)
    734  1.170  christos 				break;
    735   1.23    itojun 			if (maxhlen > hostnamelen)
    736   1.23    itojun 				maxhlen = hostnamelen;
    737    1.2    itojun 			/*
    738    1.2    itojun 			 * Copy IPv6 and ICMPv6 only.
    739    1.2    itojun 			 */
    740    1.2    itojun 			nip6 = mtod(n, struct ip6_hdr *);
    741    1.2    itojun 			bcopy(ip6, nip6, sizeof(struct ip6_hdr));
    742    1.2    itojun 			nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
    743    1.2    itojun 			bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
    744    1.2    itojun 			p = (u_char *)(nicmp6 + 1);
    745  1.152    cegger 			memset(p, 0, 4);
    746   1.67    itojun 			bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
    747    1.2    itojun 			noff = sizeof(struct ip6_hdr);
    748   1.74    itojun 			M_COPY_PKTHDR(n, m); /* just for rcvif */
    749    1.2    itojun 			n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
    750   1.23    itojun 				sizeof(struct icmp6_hdr) + 4 + maxhlen;
    751    1.2    itojun 			nicmp6->icmp6_type = ICMP6_WRUREPLY;
    752    1.2    itojun 			nicmp6->icmp6_code = 0;
    753    1.2    itojun 		}
    754    1.2    itojun #undef hostnamelen
    755    1.2    itojun 		if (n) {
    756  1.145   thorpej 			uint64_t *icmp6s = ICMP6_STAT_GETREF();
    757  1.145   thorpej 			icmp6s[ICMP6_STAT_REFLECT]++;
    758  1.145   thorpej 			icmp6s[ICMP6_STAT_OUTHIST + ICMP6_WRUREPLY]++;
    759  1.145   thorpej 			ICMP6_STAT_PUTREF();
    760    1.2    itojun 			icmp6_reflect(n, noff);
    761    1.2    itojun 		}
    762    1.2    itojun 		break;
    763    1.2    itojun 	    }
    764    1.2    itojun 
    765    1.2    itojun 	case ICMP6_WRUREPLY:
    766    1.2    itojun 		if (code != 0)
    767    1.2    itojun 			goto badcode;
    768    1.2    itojun 		break;
    769    1.2    itojun 
    770    1.2    itojun 	case ND_ROUTER_SOLICIT:
    771  1.185     ozaki 		icmp6_ifstat_inc(rcvif, ifs6_in_routersolicit);
    772    1.2    itojun 		if (code != 0)
    773    1.2    itojun 			goto badcode;
    774    1.2    itojun 		if (icmp6len < sizeof(struct nd_router_solicit))
    775    1.2    itojun 			goto badlen;
    776   1.23    itojun 		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    777   1.23    itojun 			/* give up local */
    778   1.23    itojun 			nd6_rs_input(m, off, icmp6len);
    779   1.23    itojun 			m = NULL;
    780   1.23    itojun 			goto freeit;
    781   1.23    itojun 		}
    782   1.23    itojun 		nd6_rs_input(n, off, icmp6len);
    783    1.2    itojun 		/* m stays. */
    784    1.2    itojun 		break;
    785    1.2    itojun 
    786    1.2    itojun 	case ND_ROUTER_ADVERT:
    787  1.185     ozaki 		icmp6_ifstat_inc(rcvif, ifs6_in_routeradvert);
    788    1.2    itojun 		if (code != 0)
    789    1.2    itojun 			goto badcode;
    790    1.2    itojun 		if (icmp6len < sizeof(struct nd_router_advert))
    791    1.2    itojun 			goto badlen;
    792   1.23    itojun 		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    793   1.23    itojun 			/* give up local */
    794   1.23    itojun 			nd6_ra_input(m, off, icmp6len);
    795   1.23    itojun 			m = NULL;
    796   1.23    itojun 			goto freeit;
    797   1.23    itojun 		}
    798   1.23    itojun 		nd6_ra_input(n, off, icmp6len);
    799    1.2    itojun 		/* m stays. */
    800    1.2    itojun 		break;
    801    1.2    itojun 
    802    1.2    itojun 	case ND_NEIGHBOR_SOLICIT:
    803  1.185     ozaki 		icmp6_ifstat_inc(rcvif, ifs6_in_neighborsolicit);
    804    1.2    itojun 		if (code != 0)
    805    1.2    itojun 			goto badcode;
    806    1.2    itojun 		if (icmp6len < sizeof(struct nd_neighbor_solicit))
    807    1.2    itojun 			goto badlen;
    808   1.23    itojun 		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    809   1.23    itojun 			/* give up local */
    810   1.23    itojun 			nd6_ns_input(m, off, icmp6len);
    811   1.23    itojun 			m = NULL;
    812   1.23    itojun 			goto freeit;
    813   1.23    itojun 		}
    814   1.23    itojun 		nd6_ns_input(n, off, icmp6len);
    815    1.2    itojun 		/* m stays. */
    816    1.2    itojun 		break;
    817    1.2    itojun 
    818    1.2    itojun 	case ND_NEIGHBOR_ADVERT:
    819  1.185     ozaki 		icmp6_ifstat_inc(rcvif, ifs6_in_neighboradvert);
    820    1.2    itojun 		if (code != 0)
    821    1.2    itojun 			goto badcode;
    822    1.2    itojun 		if (icmp6len < sizeof(struct nd_neighbor_advert))
    823    1.2    itojun 			goto badlen;
    824   1.23    itojun 		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    825   1.23    itojun 			/* give up local */
    826   1.23    itojun 			nd6_na_input(m, off, icmp6len);
    827   1.23    itojun 			m = NULL;
    828   1.23    itojun 			goto freeit;
    829   1.23    itojun 		}
    830   1.23    itojun 		nd6_na_input(n, off, icmp6len);
    831    1.2    itojun 		/* m stays. */
    832    1.2    itojun 		break;
    833    1.2    itojun 
    834    1.2    itojun 	case ND_REDIRECT:
    835  1.185     ozaki 		icmp6_ifstat_inc(rcvif, ifs6_in_redirect);
    836    1.2    itojun 		if (code != 0)
    837    1.2    itojun 			goto badcode;
    838    1.2    itojun 		if (icmp6len < sizeof(struct nd_redirect))
    839    1.2    itojun 			goto badlen;
    840   1.23    itojun 		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    841   1.23    itojun 			/* give up local */
    842   1.23    itojun 			icmp6_redirect_input(m, off);
    843   1.23    itojun 			m = NULL;
    844   1.23    itojun 			goto freeit;
    845   1.23    itojun 		}
    846   1.23    itojun 		icmp6_redirect_input(n, off);
    847    1.2    itojun 		/* m stays. */
    848    1.2    itojun 		break;
    849    1.2    itojun 
    850    1.2    itojun 	case ICMP6_ROUTER_RENUMBERING:
    851    1.2    itojun 		if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
    852    1.2    itojun 		    code != ICMP6_ROUTER_RENUMBERING_RESULT)
    853    1.2    itojun 			goto badcode;
    854    1.2    itojun 		if (icmp6len < sizeof(struct icmp6_router_renum))
    855    1.2    itojun 			goto badlen;
    856    1.2    itojun 		break;
    857    1.2    itojun 
    858    1.2    itojun 	default:
    859  1.180     ozaki 		nd6log(LOG_DEBUG, "unknown type %d(src=%s, dst=%s, ifid=%d)\n",
    860   1.53    itojun 		    icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
    861   1.53    itojun 		    ip6_sprintf(&ip6->ip6_dst),
    862  1.185     ozaki 		    rcvif ? rcvif->if_index : 0);
    863    1.2    itojun 		if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
    864    1.2    itojun 			/* ICMPv6 error: MUST deliver it by spec... */
    865    1.2    itojun 			code = PRC_NCMDS;
    866    1.2    itojun 			/* deliver */
    867    1.2    itojun 		} else {
    868    1.2    itojun 			/* ICMPv6 informational: MUST not deliver */
    869    1.2    itojun 			break;
    870    1.2    itojun 		}
    871    1.2    itojun 	deliver:
    872   1.58    itojun 		if (icmp6_notify_error(m, off, icmp6len, code)) {
    873   1.58    itojun 			/* In this case, m should've been freed. */
    874  1.188     ozaki 			m_put_rcvif_psref(rcvif, &psref);
    875   1.86    itojun 			return (IPPROTO_DONE);
    876    1.2    itojun 		}
    877   1.58    itojun 		break;
    878   1.58    itojun 
    879   1.58    itojun 	badcode:
    880  1.145   thorpej 		ICMP6_STATINC(ICMP6_STAT_BADCODE);
    881   1.58    itojun 		break;
    882   1.58    itojun 
    883   1.58    itojun 	badlen:
    884  1.145   thorpej 		ICMP6_STATINC(ICMP6_STAT_BADLEN);
    885   1.58    itojun 		break;
    886   1.58    itojun 	}
    887  1.188     ozaki 	m_put_rcvif_psref(rcvif, &psref);
    888   1.58    itojun 
    889   1.58    itojun 	/* deliver the packet to appropriate sockets */
    890   1.58    itojun 	icmp6_rip6_input(&m, *offp);
    891   1.58    itojun 
    892   1.58    itojun 	return IPPROTO_DONE;
    893   1.58    itojun 
    894   1.58    itojun  freeit:
    895  1.188     ozaki 	m_put_rcvif_psref(rcvif, &psref);
    896   1.58    itojun 	m_freem(m);
    897   1.58    itojun 	return IPPROTO_DONE;
    898   1.58    itojun }
    899   1.58    itojun 
    900   1.58    itojun static int
    901  1.133  christos icmp6_notify_error(struct mbuf *m, int off, int icmp6len, int code)
    902   1.58    itojun {
    903   1.58    itojun 	struct icmp6_hdr *icmp6;
    904   1.58    itojun 	struct ip6_hdr *eip6;
    905   1.58    itojun 	u_int32_t notifymtu;
    906   1.58    itojun 	struct sockaddr_in6 icmp6src, icmp6dst;
    907   1.58    itojun 
    908   1.58    itojun 	if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
    909  1.145   thorpej 		ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    910   1.58    itojun 		goto freeit;
    911   1.58    itojun 	}
    912   1.58    itojun 	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
    913   1.58    itojun 		       sizeof(*icmp6) + sizeof(struct ip6_hdr));
    914   1.58    itojun 	if (icmp6 == NULL) {
    915  1.145   thorpej 		ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    916   1.86    itojun 		return (-1);
    917   1.58    itojun 	}
    918   1.58    itojun 	eip6 = (struct ip6_hdr *)(icmp6 + 1);
    919    1.2    itojun 
    920   1.58    itojun 	/* Detect the upper level protocol */
    921   1.58    itojun 	{
    922  1.140    dyoung 		void (*ctlfunc)(int, struct sockaddr *, void *);
    923    1.2    itojun 		u_int8_t nxt = eip6->ip6_nxt;
    924    1.2    itojun 		int eoff = off + sizeof(struct icmp6_hdr) +
    925    1.2    itojun 			sizeof(struct ip6_hdr);
    926   1.12    itojun 		struct ip6ctlparam ip6cp;
    927   1.23    itojun 		struct in6_addr *finaldst = NULL;
    928   1.58    itojun 		int icmp6type = icmp6->icmp6_type;
    929   1.23    itojun 		struct ip6_frag *fh;
    930   1.23    itojun 		struct ip6_rthdr *rth;
    931   1.23    itojun 		struct ip6_rthdr0 *rth0;
    932   1.23    itojun 		int rthlen;
    933  1.188     ozaki 		struct ifnet *rcvif;
    934  1.188     ozaki 		int s;
    935    1.2    itojun 
    936   1.67    itojun 		while (1) { /* XXX: should avoid infinite loop explicitly? */
    937    1.2    itojun 			struct ip6_ext *eh;
    938    1.2    itojun 
    939   1.56    itojun 			switch (nxt) {
    940    1.2    itojun 			case IPPROTO_HOPOPTS:
    941    1.2    itojun 			case IPPROTO_DSTOPTS:
    942    1.2    itojun 			case IPPROTO_AH:
    943   1.12    itojun 				IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
    944   1.58    itojun 					       eoff, sizeof(*eh));
    945   1.12    itojun 				if (eh == NULL) {
    946  1.145   thorpej 					ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    947   1.86    itojun 					return (-1);
    948   1.12    itojun 				}
    949   1.82    itojun 
    950    1.2    itojun 				if (nxt == IPPROTO_AH)
    951    1.2    itojun 					eoff += (eh->ip6e_len + 2) << 2;
    952    1.2    itojun 				else
    953    1.2    itojun 					eoff += (eh->ip6e_len + 1) << 3;
    954    1.2    itojun 				nxt = eh->ip6e_nxt;
    955    1.2    itojun 				break;
    956   1.23    itojun 			case IPPROTO_ROUTING:
    957   1.23    itojun 				/*
    958   1.23    itojun 				 * When the erroneous packet contains a
    959   1.23    itojun 				 * routing header, we should examine the
    960   1.23    itojun 				 * header to determine the final destination.
    961   1.23    itojun 				 * Otherwise, we can't properly update
    962   1.23    itojun 				 * information that depends on the final
    963   1.23    itojun 				 * destination (e.g. path MTU).
    964   1.23    itojun 				 */
    965   1.23    itojun 				IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
    966   1.58    itojun 					       eoff, sizeof(*rth));
    967   1.23    itojun 				if (rth == NULL) {
    968  1.145   thorpej 					ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    969   1.86    itojun 					return (-1);
    970   1.23    itojun 				}
    971   1.23    itojun 				rthlen = (rth->ip6r_len + 1) << 3;
    972   1.23    itojun 				/*
    973   1.23    itojun 				 * XXX: currently there is no
    974   1.23    itojun 				 * officially defined type other
    975   1.23    itojun 				 * than type-0.
    976   1.23    itojun 				 * Note that if the segment left field
    977   1.23    itojun 				 * is 0, all intermediate hops must
    978   1.23    itojun 				 * have been passed.
    979   1.23    itojun 				 */
    980   1.23    itojun 				if (rth->ip6r_segleft &&
    981   1.23    itojun 				    rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
    982   1.23    itojun 					int hops;
    983   1.23    itojun 
    984   1.23    itojun 					IP6_EXTHDR_GET(rth0,
    985   1.23    itojun 						       struct ip6_rthdr0 *, m,
    986   1.23    itojun 						       eoff, rthlen);
    987   1.23    itojun 					if (rth0 == NULL) {
    988  1.145   thorpej 						ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    989   1.86    itojun 						return (-1);
    990   1.23    itojun 					}
    991   1.23    itojun 					/* just ignore a bogus header */
    992   1.23    itojun 					if ((rth0->ip6r0_len % 2) == 0 &&
    993   1.23    itojun 					    (hops = rth0->ip6r0_len/2))
    994   1.23    itojun 						finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
    995   1.23    itojun 				}
    996   1.23    itojun 				eoff += rthlen;
    997   1.23    itojun 				nxt = rth->ip6r_nxt;
    998   1.23    itojun 				break;
    999   1.23    itojun 			case IPPROTO_FRAGMENT:
   1000   1.23    itojun 				IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
   1001   1.58    itojun 					       eoff, sizeof(*fh));
   1002   1.23    itojun 				if (fh == NULL) {
   1003  1.145   thorpej 					ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
   1004   1.86    itojun 					return (-1);
   1005   1.23    itojun 				}
   1006   1.23    itojun 				/*
   1007   1.23    itojun 				 * Data after a fragment header is meaningless
   1008   1.23    itojun 				 * unless it is the first fragment, but
   1009   1.23    itojun 				 * we'll go to the notify label for path MTU
   1010   1.23    itojun 				 * discovery.
   1011   1.23    itojun 				 */
   1012   1.23    itojun 				if (fh->ip6f_offlg & IP6F_OFF_MASK)
   1013   1.23    itojun 					goto notify;
   1014   1.23    itojun 
   1015   1.23    itojun 				eoff += sizeof(struct ip6_frag);
   1016   1.23    itojun 				nxt = fh->ip6f_nxt;
   1017   1.23    itojun 				break;
   1018    1.2    itojun 			default:
   1019   1.23    itojun 				/*
   1020   1.23    itojun 				 * This case includes ESP and the No Next
   1021   1.67    itojun 				 * Header.  In such cases going to the notify
   1022   1.23    itojun 				 * label does not have any meaning
   1023   1.23    itojun 				 * (i.e. ctlfunc will be NULL), but we go
   1024   1.23    itojun 				 * anyway since we might have to update
   1025   1.23    itojun 				 * path MTU information.
   1026   1.23    itojun 				 */
   1027    1.2    itojun 				goto notify;
   1028    1.2    itojun 			}
   1029    1.2    itojun 		}
   1030   1.58    itojun 	  notify:
   1031   1.12    itojun 		IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
   1032   1.58    itojun 			       sizeof(*icmp6) + sizeof(struct ip6_hdr));
   1033   1.12    itojun 		if (icmp6 == NULL) {
   1034  1.145   thorpej 			ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
   1035   1.86    itojun 			return (-1);
   1036   1.58    itojun 		}
   1037   1.58    itojun 
   1038  1.113    rpaulo 		/*
   1039  1.113    rpaulo 		 * retrieve parameters from the inner IPv6 header, and convert
   1040  1.113    rpaulo 		 * them into sockaddr structures.
   1041  1.113    rpaulo 		 * XXX: there is no guarantee that the source or destination
   1042  1.113    rpaulo 		 * addresses of the inner packet are in the same scope zone as
   1043  1.113    rpaulo 		 * the addresses of the icmp packet.  But there is no other
   1044  1.113    rpaulo 		 * way to determine the zone.
   1045  1.113    rpaulo 		 */
   1046   1.58    itojun 		eip6 = (struct ip6_hdr *)(icmp6 + 1);
   1047  1.113    rpaulo 
   1048  1.188     ozaki 		rcvif = m_get_rcvif(m, &s);
   1049  1.138    dyoung 		sockaddr_in6_init(&icmp6dst,
   1050  1.138    dyoung 		    (finaldst == NULL) ? &eip6->ip6_dst : finaldst, 0, 0, 0);
   1051  1.188     ozaki 		if (in6_setscope(&icmp6dst.sin6_addr, rcvif, NULL)) {
   1052  1.188     ozaki 			m_put_rcvif(rcvif, &s);
   1053   1.58    itojun 			goto freeit;
   1054  1.188     ozaki 		}
   1055  1.138    dyoung 		sockaddr_in6_init(&icmp6src, &eip6->ip6_src, 0, 0, 0);
   1056  1.188     ozaki 		if (in6_setscope(&icmp6src.sin6_addr, rcvif, NULL)) {
   1057  1.188     ozaki 			m_put_rcvif(rcvif, &s);
   1058   1.58    itojun 			goto freeit;
   1059  1.188     ozaki 		}
   1060  1.188     ozaki 		m_put_rcvif(rcvif, &s);
   1061  1.188     ozaki 
   1062   1.58    itojun 		icmp6src.sin6_flowinfo =
   1063   1.58    itojun 			(eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
   1064   1.58    itojun 
   1065   1.46    itojun 		if (finaldst == NULL)
   1066   1.58    itojun 			finaldst = &eip6->ip6_dst;
   1067   1.46    itojun 		ip6cp.ip6c_m = m;
   1068   1.46    itojun 		ip6cp.ip6c_icmp6 = icmp6;
   1069   1.46    itojun 		ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
   1070   1.46    itojun 		ip6cp.ip6c_off = eoff;
   1071   1.46    itojun 		ip6cp.ip6c_finaldst = finaldst;
   1072   1.58    itojun 		ip6cp.ip6c_src = &icmp6src;
   1073   1.58    itojun 		ip6cp.ip6c_nxt = nxt;
   1074   1.58    itojun 
   1075   1.58    itojun 		if (icmp6type == ICMP6_PACKET_TOO_BIG) {
   1076   1.58    itojun 			notifymtu = ntohl(icmp6->icmp6_mtu);
   1077   1.58    itojun 			ip6cp.ip6c_cmdarg = (void *)&notifymtu;
   1078   1.58    itojun 		}
   1079   1.23    itojun 
   1080  1.179  riastrad 		ctlfunc = (void (*)(int, struct sockaddr *, void *))
   1081  1.179  riastrad 			(inet6sw[ip6_protox[nxt]].pr_ctlinput);
   1082   1.12    itojun 		if (ctlfunc) {
   1083   1.58    itojun 			(void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
   1084   1.58    itojun 					  &ip6cp);
   1085   1.12    itojun 		}
   1086    1.2    itojun 	}
   1087   1.86    itojun 	return (0);
   1088    1.2    itojun 
   1089   1.58    itojun   freeit:
   1090    1.2    itojun 	m_freem(m);
   1091   1.86    itojun 	return (-1);
   1092    1.2    itojun }
   1093    1.2    itojun 
   1094   1.46    itojun void
   1095  1.133  christos icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
   1096   1.23    itojun {
   1097   1.48    itojun 	unsigned long rtcount;
   1098   1.46    itojun 	struct icmp6_mtudisc_callback *mc;
   1099   1.46    itojun 	struct in6_addr *dst = ip6cp->ip6c_finaldst;
   1100   1.46    itojun 	struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
   1101   1.46    itojun 	struct mbuf *m = ip6cp->ip6c_m;	/* will be necessary for scope issue */
   1102   1.23    itojun 	u_int mtu = ntohl(icmp6->icmp6_mtu);
   1103   1.23    itojun 	struct rtentry *rt = NULL;
   1104   1.23    itojun 	struct sockaddr_in6 sin6;
   1105  1.188     ozaki 	struct ifnet *rcvif;
   1106  1.188     ozaki 	int s;
   1107   1.23    itojun 
   1108   1.48    itojun 	/*
   1109  1.150   adrianp 	 * The MTU should not be less than the minimal IPv6 MTU except for the
   1110  1.150   adrianp 	 * hack in ip6_output/ip6_setpmtu where we always include a frag header.
   1111  1.150   adrianp 	 * In that one case, the MTU might be less than 1280.
   1112  1.150   adrianp 	 */
   1113  1.150   adrianp 	if (__predict_false(mtu < IPV6_MMTU - sizeof(struct ip6_frag))) {
   1114  1.150   adrianp 		/* is the mtu even sane? */
   1115  1.150   adrianp 		if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
   1116  1.150   adrianp 			return;
   1117  1.150   adrianp 		if (!validated)
   1118  1.150   adrianp 			return;
   1119  1.150   adrianp 		mtu = IPV6_MMTU - sizeof(struct ip6_frag);
   1120  1.150   adrianp 	}
   1121  1.150   adrianp 
   1122  1.150   adrianp 	/*
   1123   1.48    itojun 	 * allow non-validated cases if memory is plenty, to make traffic
   1124   1.48    itojun 	 * from non-connected pcb happy.
   1125   1.48    itojun 	 */
   1126   1.48    itojun 	rtcount = rt_timer_count(icmp6_mtudisc_timeout_q);
   1127   1.48    itojun 	if (validated) {
   1128   1.55    itojun 		if (0 <= icmp6_mtudisc_hiwat && rtcount > icmp6_mtudisc_hiwat)
   1129   1.48    itojun 			return;
   1130   1.55    itojun 		else if (0 <= icmp6_mtudisc_lowat &&
   1131   1.55    itojun 		    rtcount > icmp6_mtudisc_lowat) {
   1132   1.48    itojun 			/*
   1133   1.48    itojun 			 * XXX nuke a victim, install the new one.
   1134   1.48    itojun 			 */
   1135   1.48    itojun 		}
   1136   1.48    itojun 	} else {
   1137   1.55    itojun 		if (0 <= icmp6_mtudisc_lowat && rtcount > icmp6_mtudisc_lowat)
   1138   1.48    itojun 			return;
   1139   1.48    itojun 	}
   1140   1.48    itojun 
   1141  1.152    cegger 	memset(&sin6, 0, sizeof(sin6));
   1142   1.23    itojun 	sin6.sin6_family = PF_INET6;
   1143   1.23    itojun 	sin6.sin6_len = sizeof(struct sockaddr_in6);
   1144   1.23    itojun 	sin6.sin6_addr = *dst;
   1145  1.188     ozaki 	s = pserialize_read_enter();
   1146  1.188     ozaki 	rcvif = m_get_rcvif(m, &s);
   1147  1.188     ozaki 	if (in6_setscope(&sin6.sin6_addr, rcvif, NULL)) {
   1148  1.188     ozaki 		m_put_rcvif(rcvif, &s);
   1149  1.113    rpaulo 		return;
   1150  1.188     ozaki 	}
   1151  1.188     ozaki 	m_put_rcvif(rcvif, &s);
   1152  1.113    rpaulo 
   1153   1.49    itojun 	rt = icmp6_mtudisc_clone((struct sockaddr *)&sin6);
   1154   1.23    itojun 
   1155   1.80    itojun 	if (rt && (rt->rt_flags & RTF_HOST) &&
   1156   1.80    itojun 	    !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
   1157   1.80    itojun 	    (rt->rt_rmx.rmx_mtu > mtu || rt->rt_rmx.rmx_mtu == 0)) {
   1158   1.80    itojun 		if (mtu < IN6_LINKMTU(rt->rt_ifp)) {
   1159  1.145   thorpej 			ICMP6_STATINC(ICMP6_STAT_PMTUCHG);
   1160   1.23    itojun 			rt->rt_rmx.rmx_mtu = mtu;
   1161   1.23    itojun 		}
   1162   1.23    itojun 	}
   1163  1.169     rmind 	if (rt) {
   1164  1.169     rmind 		rtfree(rt);
   1165   1.67    itojun 	}
   1166   1.46    itojun 
   1167   1.46    itojun 	/*
   1168   1.46    itojun 	 * Notify protocols that the MTU for this destination
   1169   1.46    itojun 	 * has changed.
   1170   1.46    itojun 	 */
   1171   1.46    itojun 	for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL;
   1172   1.46    itojun 	     mc = LIST_NEXT(mc, mc_list))
   1173   1.46    itojun 		(*mc->mc_func)(&sin6.sin6_addr);
   1174   1.23    itojun }
   1175   1.23    itojun 
   1176    1.2    itojun /*
   1177   1.40    itojun  * Process a Node Information Query packet, based on
   1178   1.47    itojun  * draft-ietf-ipngwg-icmp-name-lookups-07.
   1179   1.81    itojun  *
   1180   1.31    itojun  * Spec incompatibilities:
   1181   1.31    itojun  * - IPv6 Subject address handling
   1182   1.31    itojun  * - IPv4 Subject address handling support missing
   1183   1.31    itojun  * - Proxy reply (answer even if it's not for me)
   1184   1.31    itojun  * - joins NI group address at in6_ifattach() time only, does not cope
   1185   1.31    itojun  *   with hostname changes by sethostname(3)
   1186    1.2    itojun  */
   1187    1.2    itojun static struct mbuf *
   1188  1.133  christos ni6_input(struct mbuf *m, int off)
   1189    1.2    itojun {
   1190   1.12    itojun 	struct icmp6_nodeinfo *ni6, *nni6;
   1191    1.2    itojun 	struct mbuf *n = NULL;
   1192   1.12    itojun 	u_int16_t qtype;
   1193   1.31    itojun 	int subjlen;
   1194    1.2    itojun 	int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
   1195    1.2    itojun 	struct ni_reply_fqdn *fqdn;
   1196    1.2    itojun 	int addrs;		/* for NI_QTYPE_NODEADDR */
   1197    1.2    itojun 	struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
   1198  1.113    rpaulo 	struct sockaddr_in6 sin6; /* ip6_dst */
   1199  1.113    rpaulo 	struct in6_addr in6_subj; /* subject address */
   1200   1.31    itojun 	struct ip6_hdr *ip6;
   1201   1.31    itojun 	int oldfqdn = 0;	/* if 1, return pascal string (03 draft) */
   1202   1.47    itojun 	char *subj = NULL;
   1203  1.188     ozaki 	int s;
   1204  1.188     ozaki 	struct ifnet *rcvif;
   1205    1.2    itojun 
   1206   1.31    itojun 	ip6 = mtod(m, struct ip6_hdr *);
   1207   1.14    itojun 	IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
   1208   1.14    itojun 	if (ni6 == NULL) {
   1209   1.14    itojun 		/* m is already reclaimed */
   1210   1.12    itojun 		return NULL;
   1211   1.14    itojun 	}
   1212   1.31    itojun 
   1213   1.31    itojun 	/*
   1214   1.31    itojun 	 * Validate IPv6 destination address.
   1215   1.31    itojun 	 *
   1216   1.47    itojun 	 * The Responder must discard the Query without further processing
   1217   1.47    itojun 	 * unless it is one of the Responder's unicast or anycast addresses, or
   1218   1.47    itojun 	 * a link-local scope multicast address which the Responder has joined.
   1219   1.47    itojun 	 * [icmp-name-lookups-07, Section 4.]
   1220   1.31    itojun 	 */
   1221  1.138    dyoung 	sockaddr_in6_init(&sin6, &ip6->ip6_dst, 0, 0, 0);
   1222   1.31    itojun 	/* XXX scopeid */
   1223   1.31    itojun 	if (ifa_ifwithaddr((struct sockaddr *)&sin6))
   1224   1.47    itojun 		; /* unicast/anycast, fine */
   1225   1.31    itojun 	else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
   1226   1.47    itojun 		; /* link-local multicast, fine */
   1227   1.31    itojun 	else
   1228   1.31    itojun 		goto bad;
   1229   1.31    itojun 
   1230   1.47    itojun 	/* validate query Subject field. */
   1231   1.12    itojun 	qtype = ntohs(ni6->ni_qtype);
   1232   1.31    itojun 	subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
   1233   1.31    itojun 	switch (qtype) {
   1234   1.31    itojun 	case NI_QTYPE_NOOP:
   1235   1.31    itojun 	case NI_QTYPE_SUPTYPES:
   1236   1.47    itojun 		/* 07 draft */
   1237   1.40    itojun 		if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
   1238   1.40    itojun 			break;
   1239   1.67    itojun 		/* FALLTHROUGH */
   1240   1.31    itojun 	case NI_QTYPE_FQDN:
   1241   1.31    itojun 	case NI_QTYPE_NODEADDR:
   1242  1.115    rpaulo 	case NI_QTYPE_IPV4ADDR:
   1243   1.31    itojun 		switch (ni6->ni_code) {
   1244   1.31    itojun 		case ICMP6_NI_SUBJ_IPV6:
   1245   1.31    itojun #if ICMP6_NI_SUBJ_IPV6 != 0
   1246   1.31    itojun 		case 0:
   1247   1.31    itojun #endif
   1248   1.31    itojun 			/*
   1249   1.31    itojun 			 * backward compatibility - try to accept 03 draft
   1250   1.31    itojun 			 * format, where no Subject is present.
   1251   1.31    itojun 			 */
   1252   1.40    itojun 			if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
   1253   1.40    itojun 			    subjlen == 0) {
   1254   1.31    itojun 				oldfqdn++;
   1255   1.31    itojun 				break;
   1256   1.31    itojun 			}
   1257   1.40    itojun #if ICMP6_NI_SUBJ_IPV6 != 0
   1258   1.40    itojun 			if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
   1259   1.40    itojun 				goto bad;
   1260   1.40    itojun #endif
   1261   1.31    itojun 
   1262   1.31    itojun 			if (subjlen != sizeof(sin6.sin6_addr))
   1263   1.31    itojun 				goto bad;
   1264   1.31    itojun 
   1265   1.31    itojun 			/*
   1266   1.31    itojun 			 * Validate Subject address.
   1267   1.31    itojun 			 *
   1268   1.53    itojun 			 * Not sure what exactly "address belongs to the node"
   1269   1.53    itojun 			 * means in the spec, is it just unicast, or what?
   1270   1.31    itojun 			 *
   1271   1.31    itojun 			 * At this moment we consider Subject address as
   1272   1.31    itojun 			 * "belong to the node" if the Subject address equals
   1273   1.31    itojun 			 * to the IPv6 destination address; validation for
   1274   1.31    itojun 			 * IPv6 destination address should have done enough
   1275   1.31    itojun 			 * check for us.
   1276   1.31    itojun 			 *
   1277   1.31    itojun 			 * We do not do proxy at this moment.
   1278   1.31    itojun 			 */
   1279   1.31    itojun 			/* m_pulldown instead of copy? */
   1280   1.31    itojun 			m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
   1281  1.131  christos 			    subjlen, (void *)&in6_subj);
   1282  1.188     ozaki 			rcvif = m_get_rcvif(m, &s);
   1283  1.188     ozaki 			if (in6_setscope(&in6_subj, rcvif, NULL)) {
   1284  1.188     ozaki 				m_put_rcvif(rcvif, &s);
   1285  1.113    rpaulo 				goto bad;
   1286  1.188     ozaki 			}
   1287  1.188     ozaki 			m_put_rcvif(rcvif, &s);
   1288  1.113    rpaulo 
   1289  1.113    rpaulo 			subj = (char *)&in6_subj;
   1290  1.113    rpaulo 			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
   1291   1.31    itojun 				break;
   1292   1.47    itojun 
   1293   1.31    itojun 			/*
   1294   1.31    itojun 			 * XXX if we are to allow other cases, we should really
   1295   1.31    itojun 			 * be careful about scope here.
   1296   1.31    itojun 			 * basically, we should disallow queries toward IPv6
   1297   1.31    itojun 			 * destination X with subject Y, if scope(X) > scope(Y).
   1298   1.31    itojun 			 * if we allow scope(X) > scope(Y), it will result in
   1299   1.31    itojun 			 * information leakage across scope boundary.
   1300   1.31    itojun 			 */
   1301   1.31    itojun 			goto bad;
   1302   1.31    itojun 
   1303   1.31    itojun 		case ICMP6_NI_SUBJ_FQDN:
   1304   1.31    itojun 			/*
   1305   1.31    itojun 			 * Validate Subject name with gethostname(3).
   1306   1.31    itojun 			 *
   1307   1.31    itojun 			 * The behavior may need some debate, since:
   1308   1.31    itojun 			 * - we are not sure if the node has FQDN as
   1309   1.31    itojun 			 *   hostname (returned by gethostname(3)).
   1310   1.31    itojun 			 * - the code does wildcard match for truncated names.
   1311   1.31    itojun 			 *   however, we are not sure if we want to perform
   1312   1.31    itojun 			 *   wildcard match, if gethostname(3) side has
   1313   1.31    itojun 			 *   truncated hostname.
   1314   1.31    itojun 			 */
   1315   1.31    itojun 			n = ni6_nametodns(hostname, hostnamelen, 0);
   1316   1.31    itojun 			if (!n || n->m_next || n->m_len == 0)
   1317   1.31    itojun 				goto bad;
   1318   1.31    itojun 			IP6_EXTHDR_GET(subj, char *, m,
   1319   1.31    itojun 			    off + sizeof(struct icmp6_nodeinfo), subjlen);
   1320   1.31    itojun 			if (subj == NULL)
   1321   1.31    itojun 				goto bad;
   1322   1.31    itojun 			if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
   1323   1.31    itojun 					n->m_len)) {
   1324   1.31    itojun 				goto bad;
   1325   1.31    itojun 			}
   1326   1.31    itojun 			m_freem(n);
   1327   1.31    itojun 			n = NULL;
   1328   1.31    itojun 			break;
   1329   1.31    itojun 
   1330   1.47    itojun 		case ICMP6_NI_SUBJ_IPV4:	/* XXX: to be implemented? */
   1331   1.31    itojun 		default:
   1332   1.31    itojun 			goto bad;
   1333   1.31    itojun 		}
   1334   1.31    itojun 		break;
   1335    1.2    itojun 	}
   1336    1.2    itojun 
   1337   1.67    itojun 	/* refuse based on configuration.  XXX ICMP6_NI_REFUSED? */
   1338   1.67    itojun 	switch (qtype) {
   1339   1.67    itojun 	case NI_QTYPE_FQDN:
   1340   1.67    itojun 		if ((icmp6_nodeinfo & 1) == 0)
   1341   1.67    itojun 			goto bad;
   1342   1.67    itojun 		break;
   1343   1.67    itojun 	case NI_QTYPE_NODEADDR:
   1344  1.115    rpaulo 	case NI_QTYPE_IPV4ADDR:
   1345   1.67    itojun 		if ((icmp6_nodeinfo & 2) == 0)
   1346   1.67    itojun 			goto bad;
   1347   1.67    itojun 		break;
   1348   1.67    itojun 	}
   1349   1.67    itojun 
   1350   1.47    itojun 	/* guess reply length */
   1351   1.47    itojun 	switch (qtype) {
   1352   1.47    itojun 	case NI_QTYPE_NOOP:
   1353   1.47    itojun 		break;		/* no reply data */
   1354   1.47    itojun 	case NI_QTYPE_SUPTYPES:
   1355   1.47    itojun 		replylen += sizeof(u_int32_t);
   1356   1.47    itojun 		break;
   1357   1.47    itojun 	case NI_QTYPE_FQDN:
   1358   1.47    itojun 		/* XXX will append an mbuf */
   1359   1.47    itojun 		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
   1360   1.47    itojun 		break;
   1361   1.47    itojun 	case NI_QTYPE_NODEADDR:
   1362   1.47    itojun 		addrs = ni6_addrs(ni6, m, &ifp, subj);
   1363   1.47    itojun 		if ((replylen += addrs * (sizeof(struct in6_addr) +
   1364   1.47    itojun 					  sizeof(u_int32_t))) > MCLBYTES)
   1365   1.47    itojun 			replylen = MCLBYTES; /* XXX: will truncate pkt later */
   1366   1.47    itojun 		break;
   1367  1.115    rpaulo 	case NI_QTYPE_IPV4ADDR:
   1368  1.115    rpaulo 		/* unsupported - should respond with unknown Qtype? */
   1369  1.115    rpaulo 		goto bad;
   1370   1.47    itojun 	default:
   1371   1.47    itojun 		/*
   1372   1.47    itojun 		 * XXX: We must return a reply with the ICMP6 code
   1373   1.67    itojun 		 * `unknown Qtype' in this case.  However we regard the case
   1374   1.47    itojun 		 * as an FQDN query for backward compatibility.
   1375   1.47    itojun 		 * Older versions set a random value to this field,
   1376   1.47    itojun 		 * so it rarely varies in the defined qtypes.
   1377   1.47    itojun 		 * But the mechanism is not reliable...
   1378   1.47    itojun 		 * maybe we should obsolete older versions.
   1379   1.47    itojun 		 */
   1380   1.47    itojun 		qtype = NI_QTYPE_FQDN;
   1381   1.47    itojun 		/* XXX will append an mbuf */
   1382   1.47    itojun 		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
   1383   1.47    itojun 		oldfqdn++;
   1384   1.47    itojun 		break;
   1385   1.47    itojun 	}
   1386   1.47    itojun 
   1387   1.47    itojun 	/* allocate an mbuf to reply. */
   1388    1.2    itojun 	MGETHDR(n, M_DONTWAIT, m->m_type);
   1389   1.14    itojun 	if (n == NULL) {
   1390   1.14    itojun 		m_freem(m);
   1391   1.86    itojun 		return (NULL);
   1392   1.14    itojun 	}
   1393  1.110      yamt 	M_MOVE_PKTHDR(n, m); /* just for rcvif */
   1394    1.2    itojun 	if (replylen > MHLEN) {
   1395   1.31    itojun 		if (replylen > MCLBYTES) {
   1396   1.53    itojun 			/*
   1397   1.53    itojun 			 * XXX: should we try to allocate more? But MCLBYTES
   1398   1.53    itojun 			 * is probably much larger than IPV6_MMTU...
   1399   1.53    itojun 			 */
   1400    1.2    itojun 			goto bad;
   1401   1.31    itojun 		}
   1402    1.2    itojun 		MCLGET(n, M_DONTWAIT);
   1403    1.2    itojun 		if ((n->m_flags & M_EXT) == 0) {
   1404    1.2    itojun 			goto bad;
   1405    1.2    itojun 		}
   1406    1.2    itojun 	}
   1407    1.2    itojun 	n->m_pkthdr.len = n->m_len = replylen;
   1408    1.2    itojun 
   1409    1.2    itojun 	/* copy mbuf header and IPv6 + Node Information base headers */
   1410  1.131  christos 	bcopy(mtod(m, void *), mtod(n, void *), sizeof(struct ip6_hdr));
   1411   1.29    itojun 	nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
   1412  1.131  christos 	bcopy((void *)ni6, (void *)nni6, sizeof(struct icmp6_nodeinfo));
   1413    1.2    itojun 
   1414    1.2    itojun 	/* qtype dependent procedure */
   1415    1.2    itojun 	switch (qtype) {
   1416   1.31    itojun 	case NI_QTYPE_NOOP:
   1417   1.40    itojun 		nni6->ni_code = ICMP6_NI_SUCCESS;
   1418   1.31    itojun 		nni6->ni_flags = 0;
   1419   1.31    itojun 		break;
   1420   1.31    itojun 	case NI_QTYPE_SUPTYPES:
   1421   1.42    itojun 	{
   1422   1.42    itojun 		u_int32_t v;
   1423   1.40    itojun 		nni6->ni_code = ICMP6_NI_SUCCESS;
   1424   1.40    itojun 		nni6->ni_flags = htons(0x0000);	/* raw bitmap */
   1425   1.40    itojun 		/* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
   1426   1.42    itojun 		v = (u_int32_t)htonl(0x0000000f);
   1427   1.42    itojun 		bcopy(&v, nni6 + 1, sizeof(u_int32_t));
   1428   1.31    itojun 		break;
   1429   1.42    itojun 	}
   1430   1.31    itojun 	case NI_QTYPE_FQDN:
   1431   1.40    itojun 		nni6->ni_code = ICMP6_NI_SUCCESS;
   1432  1.131  christos 		fqdn = (struct ni_reply_fqdn *)(mtod(n, char *) +
   1433   1.31    itojun 						sizeof(struct ip6_hdr) +
   1434   1.31    itojun 						sizeof(struct icmp6_nodeinfo));
   1435   1.31    itojun 		nni6->ni_flags = 0; /* XXX: meaningless TTL */
   1436   1.31    itojun 		fqdn->ni_fqdn_ttl = 0;	/* ditto. */
   1437   1.31    itojun 		/*
   1438   1.31    itojun 		 * XXX do we really have FQDN in variable "hostname"?
   1439   1.31    itojun 		 */
   1440   1.31    itojun 		n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
   1441   1.31    itojun 		if (n->m_next == NULL)
   1442   1.31    itojun 			goto bad;
   1443   1.31    itojun 		/* XXX we assume that n->m_next is not a chain */
   1444   1.31    itojun 		if (n->m_next->m_next != NULL)
   1445   1.31    itojun 			goto bad;
   1446   1.31    itojun 		n->m_pkthdr.len += n->m_next->m_len;
   1447   1.31    itojun 		break;
   1448   1.31    itojun 	case NI_QTYPE_NODEADDR:
   1449   1.31    itojun 	{
   1450   1.31    itojun 		int lenlim, copied;
   1451   1.31    itojun 
   1452   1.40    itojun 		nni6->ni_code = ICMP6_NI_SUCCESS;
   1453   1.42    itojun 		n->m_pkthdr.len = n->m_len =
   1454   1.42    itojun 		    sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
   1455   1.42    itojun 		lenlim = M_TRAILINGSPACE(n);
   1456   1.31    itojun 		copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
   1457   1.31    itojun 		/* XXX: reset mbuf length */
   1458   1.31    itojun 		n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
   1459   1.31    itojun 			sizeof(struct icmp6_nodeinfo) + copied;
   1460   1.31    itojun 		break;
   1461   1.31    itojun 	}
   1462   1.31    itojun 	default:
   1463   1.31    itojun 		break;		/* XXX impossible! */
   1464    1.2    itojun 	}
   1465    1.2    itojun 
   1466    1.2    itojun 	nni6->ni_type = ICMP6_NI_REPLY;
   1467   1.14    itojun 	m_freem(m);
   1468   1.86    itojun 	return (n);
   1469    1.2    itojun 
   1470    1.2    itojun   bad:
   1471   1.14    itojun 	m_freem(m);
   1472    1.2    itojun 	if (n)
   1473    1.2    itojun 		m_freem(n);
   1474   1.86    itojun 	return (NULL);
   1475    1.2    itojun }
   1476    1.2    itojun #undef hostnamelen
   1477    1.2    itojun 
   1478   1.76    itojun #define isupper(x) ('A' <= (x) && (x) <= 'Z')
   1479   1.76    itojun #define isalpha(x) (('A' <= (x) && (x) <= 'Z') || ('a' <= (x) && (x) <= 'z'))
   1480   1.76    itojun #define isalnum(x) (isalpha(x) || ('0' <= (x) && (x) <= '9'))
   1481   1.76    itojun #define tolower(x) (isupper(x) ? (x) + 'a' - 'A' : (x))
   1482   1.76    itojun 
   1483    1.2    itojun /*
   1484   1.31    itojun  * make a mbuf with DNS-encoded string.  no compression support.
   1485   1.31    itojun  *
   1486   1.31    itojun  * XXX names with less than 2 dots (like "foo" or "foo.section") will be
   1487   1.31    itojun  * treated as truncated name (two \0 at the end).  this is a wild guess.
   1488  1.133  christos  *
   1489  1.133  christos  * old - return pascal string if non-zero
   1490   1.31    itojun  */
   1491   1.31    itojun static struct mbuf *
   1492  1.133  christos ni6_nametodns(const char *name, int namelen, int old)
   1493   1.31    itojun {
   1494   1.31    itojun 	struct mbuf *m;
   1495   1.31    itojun 	char *cp, *ep;
   1496   1.31    itojun 	const char *p, *q;
   1497   1.31    itojun 	int i, len, nterm;
   1498   1.31    itojun 
   1499   1.31    itojun 	if (old)
   1500   1.31    itojun 		len = namelen + 1;
   1501   1.31    itojun 	else
   1502   1.31    itojun 		len = MCLBYTES;
   1503   1.31    itojun 
   1504   1.31    itojun 	/* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
   1505   1.31    itojun 	MGET(m, M_DONTWAIT, MT_DATA);
   1506   1.31    itojun 	if (m && len > MLEN) {
   1507   1.31    itojun 		MCLGET(m, M_DONTWAIT);
   1508   1.31    itojun 		if ((m->m_flags & M_EXT) == 0)
   1509   1.31    itojun 			goto fail;
   1510   1.31    itojun 	}
   1511   1.31    itojun 	if (!m)
   1512   1.31    itojun 		goto fail;
   1513   1.31    itojun 	m->m_next = NULL;
   1514   1.31    itojun 
   1515   1.31    itojun 	if (old) {
   1516   1.31    itojun 		m->m_len = len;
   1517   1.31    itojun 		*mtod(m, char *) = namelen;
   1518   1.31    itojun 		bcopy(name, mtod(m, char *) + 1, namelen);
   1519   1.31    itojun 		return m;
   1520   1.31    itojun 	} else {
   1521   1.31    itojun 		m->m_len = 0;
   1522   1.31    itojun 		cp = mtod(m, char *);
   1523   1.31    itojun 		ep = mtod(m, char *) + M_TRAILINGSPACE(m);
   1524   1.31    itojun 
   1525   1.31    itojun 		/* if not certain about my name, return empty buffer */
   1526   1.31    itojun 		if (namelen == 0)
   1527   1.31    itojun 			return m;
   1528   1.31    itojun 
   1529   1.31    itojun 		/*
   1530   1.31    itojun 		 * guess if it looks like shortened hostname, or FQDN.
   1531   1.31    itojun 		 * shortened hostname needs two trailing "\0".
   1532   1.31    itojun 		 */
   1533   1.31    itojun 		i = 0;
   1534   1.31    itojun 		for (p = name; p < name + namelen; p++) {
   1535   1.31    itojun 			if (*p && *p == '.')
   1536   1.31    itojun 				i++;
   1537   1.31    itojun 		}
   1538   1.31    itojun 		if (i < 2)
   1539   1.31    itojun 			nterm = 2;
   1540   1.31    itojun 		else
   1541   1.31    itojun 			nterm = 1;
   1542   1.31    itojun 
   1543   1.31    itojun 		p = name;
   1544   1.31    itojun 		while (cp < ep && p < name + namelen) {
   1545   1.31    itojun 			i = 0;
   1546   1.31    itojun 			for (q = p; q < name + namelen && *q && *q != '.'; q++)
   1547   1.31    itojun 				i++;
   1548   1.31    itojun 			/* result does not fit into mbuf */
   1549   1.31    itojun 			if (cp + i + 1 >= ep)
   1550   1.31    itojun 				goto fail;
   1551   1.67    itojun 			/*
   1552   1.67    itojun 			 * DNS label length restriction, RFC1035 page 8.
   1553   1.67    itojun 			 * "i == 0" case is included here to avoid returning
   1554   1.67    itojun 			 * 0-length label on "foo..bar".
   1555   1.67    itojun 			 */
   1556   1.67    itojun 			if (i <= 0 || i >= 64)
   1557   1.31    itojun 				goto fail;
   1558   1.31    itojun 			*cp++ = i;
   1559   1.76    itojun 			if (!isalpha(p[0]) || !isalnum(p[i - 1]))
   1560   1.76    itojun 				goto fail;
   1561   1.76    itojun 			while (i > 0) {
   1562   1.76    itojun 				if (!isalnum(*p) && *p != '-')
   1563   1.76    itojun 					goto fail;
   1564   1.84    itojun 				if (isupper(*p)) {
   1565   1.84    itojun 					*cp++ = tolower(*p);
   1566   1.84    itojun 					p++;
   1567   1.84    itojun 				} else
   1568   1.76    itojun 					*cp++ = *p++;
   1569   1.76    itojun 				i--;
   1570   1.76    itojun 			}
   1571   1.31    itojun 			p = q;
   1572   1.31    itojun 			if (p < name + namelen && *p == '.')
   1573   1.31    itojun 				p++;
   1574   1.31    itojun 		}
   1575   1.31    itojun 		/* termination */
   1576   1.31    itojun 		if (cp + nterm >= ep)
   1577   1.31    itojun 			goto fail;
   1578   1.31    itojun 		while (nterm-- > 0)
   1579   1.31    itojun 			*cp++ = '\0';
   1580   1.31    itojun 		m->m_len = cp - mtod(m, char *);
   1581   1.31    itojun 		return m;
   1582   1.31    itojun 	}
   1583   1.31    itojun 
   1584   1.31    itojun 	panic("should not reach here");
   1585   1.67    itojun 	/* NOTREACHED */
   1586   1.31    itojun 
   1587   1.31    itojun  fail:
   1588   1.31    itojun 	if (m)
   1589   1.31    itojun 		m_freem(m);
   1590   1.31    itojun 	return NULL;
   1591   1.31    itojun }
   1592   1.31    itojun 
   1593   1.31    itojun /*
   1594   1.31    itojun  * check if two DNS-encoded string matches.  takes care of truncated
   1595   1.31    itojun  * form (with \0\0 at the end).  no compression support.
   1596   1.40    itojun  * XXX upper/lowercase match (see RFC2065)
   1597   1.31    itojun  */
   1598   1.31    itojun static int
   1599  1.133  christos ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
   1600   1.31    itojun {
   1601   1.31    itojun 	const char *a0, *b0;
   1602   1.31    itojun 	int l;
   1603   1.31    itojun 
   1604   1.31    itojun 	/* simplest case - need validation? */
   1605  1.151    cegger 	if (alen == blen && memcmp(a, b, alen) == 0)
   1606   1.31    itojun 		return 1;
   1607   1.31    itojun 
   1608   1.31    itojun 	a0 = a;
   1609   1.31    itojun 	b0 = b;
   1610   1.31    itojun 
   1611   1.31    itojun 	/* termination is mandatory */
   1612   1.31    itojun 	if (alen < 2 || blen < 2)
   1613   1.31    itojun 		return 0;
   1614   1.31    itojun 	if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
   1615   1.31    itojun 		return 0;
   1616   1.31    itojun 	alen--;
   1617   1.31    itojun 	blen--;
   1618   1.31    itojun 
   1619   1.31    itojun 	while (a - a0 < alen && b - b0 < blen) {
   1620   1.31    itojun 		if (a - a0 + 1 > alen || b - b0 + 1 > blen)
   1621   1.31    itojun 			return 0;
   1622   1.31    itojun 
   1623   1.33    itojun 		if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
   1624   1.31    itojun 			return 0;
   1625   1.31    itojun 		/* we don't support compression yet */
   1626   1.31    itojun 		if (a[0] >= 64 || b[0] >= 64)
   1627   1.31    itojun 			return 0;
   1628   1.31    itojun 
   1629   1.31    itojun 		/* truncated case */
   1630   1.31    itojun 		if (a[0] == 0 && a - a0 == alen - 1)
   1631   1.31    itojun 			return 1;
   1632   1.31    itojun 		if (b[0] == 0 && b - b0 == blen - 1)
   1633   1.31    itojun 			return 1;
   1634   1.31    itojun 		if (a[0] == 0 || b[0] == 0)
   1635   1.31    itojun 			return 0;
   1636   1.31    itojun 
   1637   1.31    itojun 		if (a[0] != b[0])
   1638   1.31    itojun 			return 0;
   1639   1.31    itojun 		l = a[0];
   1640   1.31    itojun 		if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
   1641   1.31    itojun 			return 0;
   1642  1.151    cegger 		if (memcmp(a + 1, b + 1, l) != 0)
   1643   1.31    itojun 			return 0;
   1644   1.31    itojun 
   1645   1.31    itojun 		a += 1 + l;
   1646   1.31    itojun 		b += 1 + l;
   1647   1.31    itojun 	}
   1648   1.31    itojun 
   1649   1.31    itojun 	if (a - a0 == alen && b - b0 == blen)
   1650   1.31    itojun 		return 1;
   1651   1.31    itojun 	else
   1652   1.31    itojun 		return 0;
   1653   1.31    itojun }
   1654   1.31    itojun 
   1655   1.31    itojun /*
   1656    1.2    itojun  * calculate the number of addresses to be returned in the node info reply.
   1657    1.2    itojun  */
   1658    1.2    itojun static int
   1659  1.123  christos ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m,
   1660  1.122  christos     struct ifnet **ifpp, char *subj)
   1661    1.2    itojun {
   1662   1.53    itojun 	struct ifnet *ifp;
   1663  1.191     ozaki 	struct in6_ifaddr *ia6;
   1664   1.53    itojun 	struct ifaddr *ifa;
   1665   1.47    itojun 	struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
   1666    1.2    itojun 	int addrs = 0, addrsofif, iffound = 0;
   1667   1.47    itojun 	int niflags = ni6->ni_flags;
   1668   1.47    itojun 
   1669   1.47    itojun 	if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
   1670   1.56    itojun 		switch (ni6->ni_code) {
   1671   1.47    itojun 		case ICMP6_NI_SUBJ_IPV6:
   1672   1.47    itojun 			if (subj == NULL) /* must be impossible... */
   1673   1.86    itojun 				return (0);
   1674   1.47    itojun 			subj_ip6 = (struct sockaddr_in6 *)subj;
   1675   1.47    itojun 			break;
   1676   1.47    itojun 		default:
   1677   1.47    itojun 			/*
   1678   1.47    itojun 			 * XXX: we only support IPv6 subject address for
   1679   1.47    itojun 			 * this Qtype.
   1680   1.47    itojun 			 */
   1681   1.86    itojun 			return (0);
   1682   1.47    itojun 		}
   1683   1.47    itojun 	}
   1684    1.2    itojun 
   1685  1.183     ozaki 	IFNET_READER_FOREACH(ifp) {
   1686    1.2    itojun 		addrsofif = 0;
   1687  1.141    dyoung 		IFADDR_FOREACH(ifa, ifp) {
   1688    1.2    itojun 			if (ifa->ifa_addr->sa_family != AF_INET6)
   1689    1.2    itojun 				continue;
   1690  1.191     ozaki 			ia6 = (struct in6_ifaddr *)ifa;
   1691    1.2    itojun 
   1692   1.47    itojun 			if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
   1693   1.47    itojun 			    IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
   1694  1.191     ozaki 					       &ia6->ia_addr.sin6_addr))
   1695    1.2    itojun 				iffound = 1;
   1696   1.24    itojun 
   1697   1.24    itojun 			/*
   1698   1.24    itojun 			 * IPv4-mapped addresses can only be returned by a
   1699   1.24    itojun 			 * Node Information proxy, since they represent
   1700   1.24    itojun 			 * addresses of IPv4-only nodes, which perforce do
   1701   1.24    itojun 			 * not implement this protocol.
   1702   1.47    itojun 			 * [icmp-name-lookups-07, Section 5.4]
   1703   1.24    itojun 			 * So we don't support NI_NODEADDR_FLAG_COMPAT in
   1704   1.24    itojun 			 * this function at this moment.
   1705   1.24    itojun 			 */
   1706    1.2    itojun 
   1707    1.2    itojun 			/* What do we have to do about ::1? */
   1708  1.191     ozaki 			switch (in6_addrscope(&ia6->ia_addr.sin6_addr)) {
   1709   1.47    itojun 			case IPV6_ADDR_SCOPE_LINKLOCAL:
   1710   1.53    itojun 				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
   1711   1.47    itojun 					continue;
   1712    1.2    itojun 				break;
   1713   1.47    itojun 			case IPV6_ADDR_SCOPE_SITELOCAL:
   1714   1.53    itojun 				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
   1715   1.47    itojun 					continue;
   1716   1.47    itojun 				break;
   1717   1.47    itojun 			case IPV6_ADDR_SCOPE_GLOBAL:
   1718   1.53    itojun 				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
   1719   1.47    itojun 					continue;
   1720    1.2    itojun 				break;
   1721   1.47    itojun 			default:
   1722   1.47    itojun 				continue;
   1723    1.2    itojun 			}
   1724   1.47    itojun 
   1725   1.47    itojun 			/*
   1726   1.47    itojun 			 * check if anycast is okay.
   1727   1.67    itojun 			 * XXX: just experimental.  not in the spec.
   1728   1.47    itojun 			 */
   1729  1.191     ozaki 			if ((ia6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
   1730   1.47    itojun 			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
   1731   1.47    itojun 				continue; /* we need only unicast addresses */
   1732   1.47    itojun 
   1733   1.47    itojun 			addrsofif++; /* count the address */
   1734    1.2    itojun 		}
   1735    1.2    itojun 		if (iffound) {
   1736    1.2    itojun 			*ifpp = ifp;
   1737   1.86    itojun 			return (addrsofif);
   1738    1.2    itojun 		}
   1739    1.2    itojun 
   1740    1.2    itojun 		addrs += addrsofif;
   1741    1.2    itojun 	}
   1742    1.2    itojun 
   1743   1.86    itojun 	return (addrs);
   1744    1.2    itojun }
   1745    1.2    itojun 
   1746    1.2    itojun static int
   1747  1.133  christos ni6_store_addrs(struct icmp6_nodeinfo *ni6,
   1748  1.133  christos 	struct icmp6_nodeinfo *nni6, struct ifnet *ifp0,
   1749  1.133  christos 	int resid)
   1750    1.2    itojun {
   1751  1.183     ozaki 	struct ifnet *ifp = ifp0 ? ifp0 : IFNET_READER_FIRST();
   1752  1.191     ozaki 	struct in6_ifaddr *ia6;
   1753   1.53    itojun 	struct ifaddr *ifa;
   1754   1.47    itojun 	struct ifnet *ifp_dep = NULL;
   1755   1.47    itojun 	int copied = 0, allow_deprecated = 0;
   1756    1.2    itojun 	u_char *cp = (u_char *)(nni6 + 1);
   1757   1.47    itojun 	int niflags = ni6->ni_flags;
   1758   1.47    itojun 	u_int32_t ltime;
   1759    1.2    itojun 
   1760   1.47    itojun 	if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
   1761   1.86    itojun 		return (0);	/* needless to copy */
   1762    1.2    itojun 
   1763   1.47    itojun   again:
   1764   1.47    itojun 
   1765  1.183     ozaki 	for (; ifp; ifp = IFNET_READER_NEXT(ifp))
   1766    1.2    itojun 	{
   1767  1.141    dyoung 		IFADDR_FOREACH(ifa, ifp) {
   1768    1.2    itojun 			if (ifa->ifa_addr->sa_family != AF_INET6)
   1769    1.2    itojun 				continue;
   1770  1.191     ozaki 			ia6 = (struct in6_ifaddr *)ifa;
   1771    1.2    itojun 
   1772  1.191     ozaki 			if ((ia6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
   1773   1.47    itojun 			    allow_deprecated == 0) {
   1774   1.47    itojun 				/*
   1775   1.47    itojun 				 * prefererred address should be put before
   1776   1.47    itojun 				 * deprecated addresses.
   1777   1.47    itojun 				 */
   1778   1.47    itojun 
   1779   1.47    itojun 				/* record the interface for later search */
   1780   1.47    itojun 				if (ifp_dep == NULL)
   1781   1.47    itojun 					ifp_dep = ifp;
   1782   1.47    itojun 
   1783   1.47    itojun 				continue;
   1784   1.12    itojun 			}
   1785  1.191     ozaki 			else if ((ia6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
   1786   1.47    itojun 				 allow_deprecated != 0)
   1787   1.47    itojun 				continue; /* we now collect deprecated addrs */
   1788    1.2    itojun 
   1789    1.2    itojun 			/* What do we have to do about ::1? */
   1790  1.191     ozaki 			switch (in6_addrscope(&ia6->ia_addr.sin6_addr)) {
   1791   1.47    itojun 			case IPV6_ADDR_SCOPE_LINKLOCAL:
   1792   1.53    itojun 				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
   1793   1.47    itojun 					continue;
   1794    1.2    itojun 				break;
   1795   1.47    itojun 			case IPV6_ADDR_SCOPE_SITELOCAL:
   1796   1.53    itojun 				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
   1797   1.47    itojun 					continue;
   1798   1.47    itojun 				break;
   1799   1.47    itojun 			case IPV6_ADDR_SCOPE_GLOBAL:
   1800   1.53    itojun 				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
   1801   1.47    itojun 					continue;
   1802    1.2    itojun 				break;
   1803   1.47    itojun 			default:
   1804   1.47    itojun 				continue;
   1805   1.47    itojun 			}
   1806   1.47    itojun 
   1807   1.47    itojun 			/*
   1808   1.47    itojun 			 * check if anycast is okay.
   1809   1.67    itojun 			 * XXX: just experimental.  not in the spec.
   1810   1.47    itojun 			 */
   1811  1.191     ozaki 			if ((ia6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
   1812   1.47    itojun 			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
   1813   1.47    itojun 				continue;
   1814   1.47    itojun 
   1815   1.47    itojun 			/* now we can copy the address */
   1816   1.47    itojun 			if (resid < sizeof(struct in6_addr) +
   1817   1.47    itojun 			    sizeof(u_int32_t)) {
   1818   1.47    itojun 				/*
   1819   1.47    itojun 				 * We give up much more copy.
   1820   1.47    itojun 				 * Set the truncate flag and return.
   1821   1.47    itojun 				 */
   1822  1.115    rpaulo 				nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
   1823   1.86    itojun 				return (copied);
   1824    1.2    itojun 			}
   1825    1.2    itojun 
   1826   1.47    itojun 			/*
   1827   1.47    itojun 			 * Set the TTL of the address.
   1828   1.47    itojun 			 * The TTL value should be one of the following
   1829   1.47    itojun 			 * according to the specification:
   1830   1.47    itojun 			 *
   1831   1.47    itojun 			 * 1. The remaining lifetime of a DHCP lease on the
   1832   1.47    itojun 			 *    address, or
   1833   1.47    itojun 			 * 2. The remaining Valid Lifetime of a prefix from
   1834   1.47    itojun 			 *    which the address was derived through Stateless
   1835   1.47    itojun 			 *    Autoconfiguration.
   1836   1.47    itojun 			 *
   1837   1.47    itojun 			 * Note that we currently do not support stateful
   1838   1.47    itojun 			 * address configuration by DHCPv6, so the former
   1839   1.47    itojun 			 * case can't happen.
   1840   1.67    itojun 			 *
   1841   1.67    itojun 			 * TTL must be 2^31 > TTL >= 0.
   1842   1.47    itojun 			 */
   1843  1.191     ozaki 			if (ia6->ia6_lifetime.ia6t_expire == 0)
   1844   1.47    itojun 				ltime = ND6_INFINITE_LIFETIME;
   1845   1.47    itojun 			else {
   1846  1.191     ozaki 				if (ia6->ia6_lifetime.ia6t_expire >
   1847  1.173     ozaki 				    time_uptime)
   1848  1.191     ozaki 					ltime = ia6->ia6_lifetime.ia6t_expire -
   1849  1.173     ozaki 					    time_uptime;
   1850   1.47    itojun 				else
   1851   1.47    itojun 					ltime = 0;
   1852    1.2    itojun 			}
   1853   1.67    itojun 			if (ltime > 0x7fffffff)
   1854   1.67    itojun 				ltime = 0x7fffffff;
   1855   1.67    itojun 			ltime = htonl(ltime);
   1856   1.82    itojun 
   1857   1.47    itojun 			bcopy(&ltime, cp, sizeof(u_int32_t));
   1858   1.47    itojun 			cp += sizeof(u_int32_t);
   1859   1.47    itojun 
   1860   1.47    itojun 			/* copy the address itself */
   1861  1.191     ozaki 			bcopy(&ia6->ia_addr.sin6_addr, cp,
   1862   1.47    itojun 			      sizeof(struct in6_addr));
   1863  1.113    rpaulo 			in6_clearscope((struct in6_addr *)cp); /* XXX */
   1864   1.47    itojun 			cp += sizeof(struct in6_addr);
   1865   1.82    itojun 
   1866   1.47    itojun 			resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
   1867  1.115    rpaulo 			copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
   1868    1.2    itojun 		}
   1869    1.2    itojun 		if (ifp0)	/* we need search only on the specified IF */
   1870    1.2    itojun 			break;
   1871   1.47    itojun 	}
   1872   1.47    itojun 
   1873   1.47    itojun 	if (allow_deprecated == 0 && ifp_dep != NULL) {
   1874   1.47    itojun 		ifp = ifp_dep;
   1875   1.47    itojun 		allow_deprecated = 1;
   1876   1.47    itojun 
   1877   1.47    itojun 		goto again;
   1878    1.2    itojun 	}
   1879    1.2    itojun 
   1880   1.86    itojun 	return (copied);
   1881    1.2    itojun }
   1882    1.2    itojun 
   1883    1.2    itojun /*
   1884    1.2    itojun  * XXX almost dup'ed code with rip6_input.
   1885    1.2    itojun  */
   1886    1.2    itojun static int
   1887  1.133  christos icmp6_rip6_input(struct mbuf **mp, int off)
   1888    1.2    itojun {
   1889    1.2    itojun 	struct mbuf *m = *mp;
   1890   1.53    itojun 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   1891  1.101    itojun 	struct inpcb_hdr *inph;
   1892   1.53    itojun 	struct in6pcb *in6p;
   1893    1.2    itojun 	struct in6pcb *last = NULL;
   1894    1.2    itojun 	struct sockaddr_in6 rip6src;
   1895    1.2    itojun 	struct icmp6_hdr *icmp6;
   1896    1.2    itojun 	struct mbuf *opts = NULL;
   1897    1.2    itojun 
   1898   1.23    itojun 	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
   1899   1.23    itojun 	if (icmp6 == NULL) {
   1900   1.23    itojun 		/* m is already reclaimed */
   1901   1.23    itojun 		return IPPROTO_DONE;
   1902   1.23    itojun 	}
   1903    1.2    itojun 
   1904  1.113    rpaulo 	/*
   1905  1.113    rpaulo 	 * XXX: the address may have embedded scope zone ID, which should be
   1906  1.113    rpaulo 	 * hidden from applications.
   1907  1.113    rpaulo 	 */
   1908  1.138    dyoung 	sockaddr_in6_init(&rip6src, &ip6->ip6_src, 0, 0, 0);
   1909  1.113    rpaulo 	if (sa6_recoverscope(&rip6src)) {
   1910  1.113    rpaulo 		m_freem(m);
   1911  1.113    rpaulo 		return (IPPROTO_DONE);
   1912  1.113    rpaulo 	}
   1913    1.2    itojun 
   1914  1.163  christos 	TAILQ_FOREACH(inph, &raw6cbtable.inpt_queue, inph_queue) {
   1915  1.101    itojun 		in6p = (struct in6pcb *)inph;
   1916  1.101    itojun 		if (in6p->in6p_af != AF_INET6)
   1917  1.101    itojun 			continue;
   1918  1.100    itojun 		if (in6p->in6p_ip6.ip6_nxt != IPPROTO_ICMPV6)
   1919    1.2    itojun 			continue;
   1920   1.12    itojun 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
   1921    1.2    itojun 		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
   1922    1.2    itojun 			continue;
   1923   1.12    itojun 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
   1924    1.2    itojun 		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
   1925    1.2    itojun 			continue;
   1926    1.2    itojun 		if (in6p->in6p_icmp6filt
   1927    1.2    itojun 		    && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
   1928    1.2    itojun 				 in6p->in6p_icmp6filt))
   1929    1.2    itojun 			continue;
   1930    1.2    itojun 		if (last) {
   1931    1.2    itojun 			struct	mbuf *n;
   1932    1.2    itojun 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
   1933    1.2    itojun 				if (last->in6p_flags & IN6P_CONTROLOPTS)
   1934    1.2    itojun 					ip6_savecontrol(last, &opts, ip6, n);
   1935    1.2    itojun 				/* strip intermediate headers */
   1936    1.2    itojun 				m_adj(n, off);
   1937    1.2    itojun 				if (sbappendaddr(&last->in6p_socket->so_rcv,
   1938    1.2    itojun 						 (struct sockaddr *)&rip6src,
   1939    1.2    itojun 						 n, opts) == 0) {
   1940    1.2    itojun 					/* should notify about lost packet */
   1941    1.2    itojun 					m_freem(n);
   1942    1.2    itojun 					if (opts)
   1943    1.2    itojun 						m_freem(opts);
   1944    1.2    itojun 				} else
   1945    1.2    itojun 					sorwakeup(last->in6p_socket);
   1946    1.2    itojun 				opts = NULL;
   1947    1.2    itojun 			}
   1948    1.2    itojun 		}
   1949    1.2    itojun 		last = in6p;
   1950    1.2    itojun 	}
   1951    1.2    itojun 	if (last) {
   1952    1.2    itojun 		if (last->in6p_flags & IN6P_CONTROLOPTS)
   1953    1.2    itojun 			ip6_savecontrol(last, &opts, ip6, m);
   1954    1.2    itojun 		/* strip intermediate headers */
   1955    1.2    itojun 		m_adj(m, off);
   1956    1.2    itojun 		if (sbappendaddr(&last->in6p_socket->so_rcv,
   1957    1.2    itojun 				(struct sockaddr *)&rip6src, m, opts) == 0) {
   1958    1.2    itojun 			m_freem(m);
   1959    1.2    itojun 			if (opts)
   1960    1.2    itojun 				m_freem(opts);
   1961    1.2    itojun 		} else
   1962    1.2    itojun 			sorwakeup(last->in6p_socket);
   1963    1.2    itojun 	} else {
   1964    1.2    itojun 		m_freem(m);
   1965  1.145   thorpej 		IP6_STATDEC(IP6_STAT_DELIVERED);
   1966    1.2    itojun 	}
   1967    1.2    itojun 	return IPPROTO_DONE;
   1968    1.2    itojun }
   1969    1.2    itojun 
   1970    1.2    itojun /*
   1971    1.2    itojun  * Reflect the ip6 packet back to the source.
   1972   1.14    itojun  * OFF points to the icmp6 header, counted from the top of the mbuf.
   1973   1.66    itojun  *
   1974   1.66    itojun  * Note: RFC 1885 required that an echo reply should be truncated if it
   1975   1.66    itojun  * did not fit in with (return) path MTU, and KAME code supported the
   1976   1.66    itojun  * behavior.  However, as a clarification after the RFC, this limitation
   1977   1.66    itojun  * was removed in a revised version of the spec, RFC 2463.  We had kept the
   1978   1.66    itojun  * old behavior, with a (non-default) ifdef block, while the new version of
   1979   1.66    itojun  * the spec was an internet-draft status, and even after the new RFC was
   1980   1.66    itojun  * published.  But it would rather make sense to clean the obsoleted part
   1981   1.66    itojun  * up, and to make the code simpler at this stage.
   1982    1.2    itojun  */
   1983    1.2    itojun void
   1984  1.130    dyoung icmp6_reflect(struct mbuf *m, size_t off)
   1985    1.2    itojun {
   1986   1.12    itojun 	struct ip6_hdr *ip6;
   1987    1.2    itojun 	struct icmp6_hdr *icmp6;
   1988  1.139    dyoung 	const struct in6_ifaddr *ia;
   1989  1.139    dyoung 	const struct ip6aux *ip6a;
   1990   1.12    itojun 	int plen;
   1991   1.12    itojun 	int type, code;
   1992   1.12    itojun 	struct ifnet *outif = NULL;
   1993  1.139    dyoung 	struct in6_addr origdst;
   1994  1.139    dyoung 	const struct in6_addr *src = NULL;
   1995  1.188     ozaki 	struct ifnet *rcvif;
   1996  1.188     ozaki 	int s;
   1997    1.2    itojun 
   1998   1.12    itojun 	/* too short to reflect */
   1999   1.12    itojun 	if (off < sizeof(struct ip6_hdr)) {
   2000  1.180     ozaki 		nd6log(LOG_DEBUG,
   2001   1.53    itojun 		    "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
   2002   1.53    itojun 		    (u_long)off, (u_long)sizeof(struct ip6_hdr),
   2003  1.180     ozaki 		    __FILE__, __LINE__);
   2004   1.12    itojun 		goto bad;
   2005   1.12    itojun 	}
   2006   1.12    itojun 
   2007    1.2    itojun 	/*
   2008    1.2    itojun 	 * If there are extra headers between IPv6 and ICMPv6, strip
   2009    1.2    itojun 	 * off that header first.
   2010    1.2    itojun 	 */
   2011   1.35    itojun #ifdef DIAGNOSTIC
   2012   1.35    itojun 	if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
   2013   1.35    itojun 		panic("assumption failed in icmp6_reflect");
   2014   1.35    itojun #endif
   2015   1.12    itojun 	if (off > sizeof(struct ip6_hdr)) {
   2016   1.12    itojun 		size_t l;
   2017   1.12    itojun 		struct ip6_hdr nip6;
   2018   1.12    itojun 
   2019   1.12    itojun 		l = off - sizeof(struct ip6_hdr);
   2020  1.131  christos 		m_copydata(m, 0, sizeof(nip6), (void *)&nip6);
   2021   1.12    itojun 		m_adj(m, l);
   2022   1.12    itojun 		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
   2023   1.12    itojun 		if (m->m_len < l) {
   2024   1.12    itojun 			if ((m = m_pullup(m, l)) == NULL)
   2025   1.12    itojun 				return;
   2026   1.12    itojun 		}
   2027  1.131  christos 		bcopy((void *)&nip6, mtod(m, void *), sizeof(nip6));
   2028   1.12    itojun 	} else /* off == sizeof(struct ip6_hdr) */ {
   2029   1.12    itojun 		size_t l;
   2030   1.12    itojun 		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
   2031   1.12    itojun 		if (m->m_len < l) {
   2032   1.12    itojun 			if ((m = m_pullup(m, l)) == NULL)
   2033   1.12    itojun 				return;
   2034    1.2    itojun 		}
   2035    1.2    itojun 	}
   2036   1.12    itojun 	plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
   2037   1.12    itojun 	ip6 = mtod(m, struct ip6_hdr *);
   2038   1.12    itojun 	ip6->ip6_nxt = IPPROTO_ICMPV6;
   2039    1.2    itojun 	icmp6 = (struct icmp6_hdr *)(ip6 + 1);
   2040   1.12    itojun 	type = icmp6->icmp6_type; /* keep type for statistics */
   2041   1.12    itojun 	code = icmp6->icmp6_code; /* ditto. */
   2042    1.2    itojun 
   2043  1.113    rpaulo 	origdst = ip6->ip6_dst;
   2044    1.2    itojun 	/*
   2045    1.2    itojun 	 * ip6_input() drops a packet if its src is multicast.
   2046    1.2    itojun 	 * So, the src is never multicast.
   2047    1.2    itojun 	 */
   2048    1.2    itojun 	ip6->ip6_dst = ip6->ip6_src;
   2049    1.2    itojun 
   2050   1.55    itojun 	/*
   2051   1.67    itojun 	 * If the incoming packet was addressed directly to us (i.e. unicast),
   2052    1.2    itojun 	 * use dst as the src for the reply.
   2053  1.114    rpaulo 	 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
   2054   1.23    itojun 	 * (for example) when we encounter an error while forwarding procedure
   2055   1.23    itojun 	 * destined to a duplicated address of ours.
   2056  1.113    rpaulo 	 * Note that ip6_getdstifaddr() may fail if we are in an error handling
   2057  1.113    rpaulo 	 * procedure of an outgoing packet of our own, in which case we need
   2058  1.113    rpaulo 	 * to search in the ifaddr list.
   2059    1.2    itojun 	 */
   2060  1.139    dyoung 	if (IN6_IS_ADDR_MULTICAST(&origdst))
   2061  1.139    dyoung 		;
   2062  1.139    dyoung 	else if ((ip6a = ip6_getdstifaddr(m)) != NULL) {
   2063  1.139    dyoung 		if ((ip6a->ip6a_flags &
   2064  1.139    dyoung 		     (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0)
   2065  1.139    dyoung 			src = &ip6a->ip6a_src;
   2066  1.139    dyoung 	} else {
   2067  1.139    dyoung 		union {
   2068  1.139    dyoung 			struct sockaddr_in6 sin6;
   2069  1.139    dyoung 			struct sockaddr sa;
   2070  1.139    dyoung 		} u;
   2071  1.139    dyoung 
   2072  1.139    dyoung 		sockaddr_in6_init(&u.sin6, &origdst, 0, 0, 0);
   2073  1.139    dyoung 
   2074  1.139    dyoung 		ia = (struct in6_ifaddr *)ifa_ifwithaddr(&u.sa);
   2075  1.139    dyoung 
   2076  1.139    dyoung 		if (ia == NULL)
   2077  1.139    dyoung 			;
   2078  1.139    dyoung 		else if ((ia->ia6_flags &
   2079  1.139    dyoung 			 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0)
   2080  1.139    dyoung 			src = &ia->ia_addr.sin6_addr;
   2081    1.2    itojun 	}
   2082    1.2    itojun 
   2083  1.113    rpaulo 	if (src == NULL) {
   2084   1.55    itojun 		int e;
   2085  1.113    rpaulo 		struct sockaddr_in6 sin6;
   2086  1.132    dyoung 		struct route ro;
   2087   1.55    itojun 
   2088    1.2    itojun 		/*
   2089   1.23    itojun 		 * This case matches to multicasts, our anycast, or unicasts
   2090   1.67    itojun 		 * that we do not own.  Select a source address based on the
   2091   1.55    itojun 		 * source address of the erroneous packet.
   2092    1.2    itojun 		 */
   2093  1.138    dyoung 		/* zone ID should be embedded */
   2094  1.138    dyoung 		sockaddr_in6_init(&sin6, &ip6->ip6_dst, 0, 0, 0);
   2095  1.113    rpaulo 
   2096  1.128    dyoung 		memset(&ro, 0, sizeof(ro));
   2097  1.189     ozaki 		src = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, NULL, NULL, &e);
   2098  1.132    dyoung 		rtcache_free(&ro);
   2099   1.55    itojun 		if (src == NULL) {
   2100  1.180     ozaki 			nd6log(LOG_DEBUG,
   2101  1.180     ozaki 			    "source can't be determined: "
   2102   1.55    itojun 			    "dst=%s, error=%d\n",
   2103  1.180     ozaki 			    ip6_sprintf(&sin6.sin6_addr), e);
   2104   1.55    itojun 			goto bad;
   2105   1.55    itojun 		}
   2106   1.55    itojun 	}
   2107    1.2    itojun 
   2108    1.2    itojun 	ip6->ip6_src = *src;
   2109    1.2    itojun 	ip6->ip6_flow = 0;
   2110   1.13    itojun 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
   2111   1.13    itojun 	ip6->ip6_vfc |= IPV6_VERSION;
   2112    1.2    itojun 	ip6->ip6_nxt = IPPROTO_ICMPV6;
   2113  1.188     ozaki 	rcvif = m_get_rcvif(m, &s);
   2114  1.188     ozaki 	if (rcvif) {
   2115    1.2    itojun 		/* XXX: This may not be the outgoing interface */
   2116  1.188     ozaki 		ip6->ip6_hlim = ND_IFINFO(rcvif)->chlim;
   2117   1.65    itojun 	} else
   2118   1.65    itojun 		ip6->ip6_hlim = ip6_defhlim;
   2119  1.188     ozaki 	m_put_rcvif(rcvif, &s);
   2120    1.2    itojun 
   2121  1.118      tron 	m->m_pkthdr.csum_flags = 0;
   2122    1.2    itojun 	icmp6->icmp6_cksum = 0;
   2123    1.2    itojun 	icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
   2124    1.2    itojun 					sizeof(struct ip6_hdr), plen);
   2125    1.2    itojun 
   2126    1.2    itojun 	/*
   2127   1.67    itojun 	 * XXX option handling
   2128    1.2    itojun 	 */
   2129    1.2    itojun 
   2130    1.2    itojun 	m->m_flags &= ~(M_BCAST|M_MCAST);
   2131    1.2    itojun 
   2132   1.73    itojun 	/*
   2133   1.73    itojun 	 * To avoid a "too big" situation at an intermediate router
   2134   1.73    itojun 	 * and the path MTU discovery process, specify the IPV6_MINMTU flag.
   2135   1.73    itojun 	 * Note that only echo and node information replies are affected,
   2136   1.73    itojun 	 * since the length of ICMP6 errors is limited to the minimum MTU.
   2137   1.73    itojun 	 */
   2138  1.188     ozaki 	if (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, &outif)
   2139  1.188     ozaki 	    != 0 && outif)
   2140   1.72    itojun 		icmp6_ifstat_inc(outif, ifs6_out_error);
   2141   1.12    itojun 	if (outif)
   2142   1.12    itojun 		icmp6_ifoutstat_inc(outif, type, code);
   2143    1.2    itojun 
   2144    1.2    itojun 	return;
   2145    1.2    itojun 
   2146    1.2    itojun  bad:
   2147    1.2    itojun 	m_freem(m);
   2148    1.2    itojun 	return;
   2149    1.2    itojun }
   2150    1.2    itojun 
   2151   1.12    itojun static const char *
   2152  1.133  christos icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
   2153  1.133  christos 	struct in6_addr *tgt6)
   2154   1.11    itojun {
   2155   1.12    itojun 	static char buf[1024];
   2156   1.12    itojun 	snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
   2157   1.12    itojun 		ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
   2158   1.12    itojun 	return buf;
   2159   1.11    itojun }
   2160   1.11    itojun 
   2161    1.2    itojun void
   2162  1.133  christos icmp6_redirect_input(struct mbuf *m, int off)
   2163    1.2    itojun {
   2164  1.188     ozaki 	struct ifnet *ifp;
   2165    1.2    itojun 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   2166   1.23    itojun 	struct nd_redirect *nd_rd;
   2167    1.2    itojun 	int icmp6len = ntohs(ip6->ip6_plen);
   2168    1.2    itojun 	char *lladdr = NULL;
   2169    1.2    itojun 	int lladdrlen = 0;
   2170    1.2    itojun 	struct rtentry *rt = NULL;
   2171    1.2    itojun 	int is_router;
   2172    1.2    itojun 	int is_onlink;
   2173    1.2    itojun 	struct in6_addr src6 = ip6->ip6_src;
   2174   1.23    itojun 	struct in6_addr redtgt6;
   2175   1.23    itojun 	struct in6_addr reddst6;
   2176    1.2    itojun 	union nd_opts ndopts;
   2177  1.188     ozaki 	struct psref psref;
   2178    1.2    itojun 
   2179  1.188     ozaki 	ifp = m_get_rcvif_psref(m, &psref);
   2180  1.184     ozaki 	if (ifp == NULL)
   2181  1.184     ozaki 		goto freeit;
   2182    1.2    itojun 
   2183    1.2    itojun 	/* XXX if we are router, we don't update route by icmp6 redirect */
   2184    1.2    itojun 	if (ip6_forwarding)
   2185   1.23    itojun 		goto freeit;
   2186    1.2    itojun 	if (!icmp6_rediraccept)
   2187   1.23    itojun 		goto freeit;
   2188    1.2    itojun 
   2189   1.23    itojun 	IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
   2190   1.23    itojun 	if (nd_rd == NULL) {
   2191  1.145   thorpej 		ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
   2192  1.188     ozaki 		m_put_rcvif_psref(ifp, &psref);
   2193   1.23    itojun 		return;
   2194   1.23    itojun 	}
   2195   1.23    itojun 	redtgt6 = nd_rd->nd_rd_target;
   2196   1.23    itojun 	reddst6 = nd_rd->nd_rd_dst;
   2197   1.25    itojun 
   2198  1.185     ozaki 	if (in6_setscope(&redtgt6, ifp, NULL) ||
   2199  1.185     ozaki 	    in6_setscope(&reddst6, ifp, NULL)) {
   2200  1.113    rpaulo 		goto freeit;
   2201  1.113    rpaulo 	}
   2202   1.23    itojun 
   2203    1.2    itojun 	/* validation */
   2204    1.2    itojun 	if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
   2205  1.180     ozaki 		nd6log(LOG_ERR,
   2206  1.180     ozaki 		    "ICMP6 redirect sent from %s rejected; "
   2207  1.180     ozaki 		    "must be from linklocal\n", ip6_sprintf(&src6));
   2208   1.53    itojun 		goto bad;
   2209    1.2    itojun 	}
   2210    1.2    itojun 	if (ip6->ip6_hlim != 255) {
   2211  1.180     ozaki 		nd6log(LOG_ERR,
   2212  1.180     ozaki 		    "ICMP6 redirect sent from %s rejected; "
   2213  1.180     ozaki 		    "hlim=%d (must be 255)\n",
   2214  1.180     ozaki 		    ip6_sprintf(&src6), ip6->ip6_hlim);
   2215   1.53    itojun 		goto bad;
   2216    1.2    itojun 	}
   2217    1.2    itojun     {
   2218    1.2    itojun 	/* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
   2219    1.2    itojun 	struct sockaddr_in6 sin6;
   2220    1.2    itojun 	struct in6_addr *gw6;
   2221    1.2    itojun 
   2222  1.138    dyoung 	sockaddr_in6_init(&sin6, &reddst6, 0, 0, 0);
   2223   1.23    itojun 	rt = rtalloc1((struct sockaddr *)&sin6, 0);
   2224    1.2    itojun 	if (rt) {
   2225   1.43    itojun 		if (rt->rt_gateway == NULL ||
   2226   1.43    itojun 		    rt->rt_gateway->sa_family != AF_INET6) {
   2227  1.180     ozaki 			nd6log(LOG_ERR,
   2228   1.43    itojun 			    "ICMP6 redirect rejected; no route "
   2229   1.43    itojun 			    "with inet6 gateway found for redirect dst: %s\n",
   2230  1.180     ozaki 			    icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
   2231  1.169     rmind 			rtfree(rt);
   2232   1.53    itojun 			goto bad;
   2233   1.43    itojun 		}
   2234   1.43    itojun 
   2235    1.2    itojun 		gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
   2236  1.151    cegger 		if (memcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
   2237  1.180     ozaki 			nd6log(LOG_ERR,
   2238  1.180     ozaki 			    "ICMP6 redirect rejected; "
   2239  1.180     ozaki 			    "not equal to gw-for-src=%s (must be same): %s\n",
   2240  1.180     ozaki 			    ip6_sprintf(gw6),
   2241  1.180     ozaki 			    icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
   2242  1.169     rmind 			rtfree(rt);
   2243   1.53    itojun 			goto bad;
   2244    1.2    itojun 		}
   2245    1.2    itojun 	} else {
   2246  1.180     ozaki 		nd6log(LOG_ERR, "ICMP6 redirect rejected; "
   2247  1.180     ozaki 		    "no route found for redirect dst: %s\n",
   2248  1.180     ozaki 		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
   2249   1.53    itojun 		goto bad;
   2250    1.2    itojun 	}
   2251  1.169     rmind 	rtfree(rt);
   2252    1.2    itojun 	rt = NULL;
   2253    1.2    itojun     }
   2254    1.2    itojun 	if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
   2255  1.180     ozaki 		nd6log(LOG_ERR, "ICMP6 redirect rejected; "
   2256  1.180     ozaki 		    "redirect dst must be unicast: %s\n",
   2257  1.180     ozaki 		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
   2258   1.53    itojun 		goto bad;
   2259    1.2    itojun 	}
   2260    1.2    itojun 
   2261    1.2    itojun 	is_router = is_onlink = 0;
   2262    1.2    itojun 	if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
   2263    1.2    itojun 		is_router = 1;	/* router case */
   2264  1.151    cegger 	if (memcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
   2265    1.2    itojun 		is_onlink = 1;	/* on-link destination case */
   2266    1.2    itojun 	if (!is_router && !is_onlink) {
   2267  1.180     ozaki 		nd6log(LOG_ERR, "ICMP6 redirect rejected; "
   2268  1.180     ozaki 		    "neither router case nor onlink case: %s\n",
   2269  1.180     ozaki 		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
   2270   1.53    itojun 		goto bad;
   2271    1.2    itojun 	}
   2272    1.2    itojun 	/* validation passed */
   2273    1.2    itojun 
   2274    1.2    itojun 	icmp6len -= sizeof(*nd_rd);
   2275    1.2    itojun 	nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
   2276    1.2    itojun 	if (nd6_options(&ndopts) < 0) {
   2277  1.180     ozaki 		nd6log(LOG_INFO, "invalid ND option, rejected: %s\n",
   2278  1.180     ozaki 		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
   2279   1.53    itojun 		/* nd6_options have incremented stats */
   2280   1.23    itojun 		goto freeit;
   2281    1.2    itojun 	}
   2282    1.2    itojun 
   2283    1.2    itojun 	if (ndopts.nd_opts_tgt_lladdr) {
   2284    1.2    itojun 		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
   2285    1.2    itojun 		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
   2286    1.2    itojun 	}
   2287    1.2    itojun 
   2288    1.2    itojun 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
   2289  1.180     ozaki 		nd6log(LOG_INFO, "lladdrlen mismatch for %s "
   2290  1.180     ozaki 		    "(if %d, icmp6 packet %d): %s\n",
   2291  1.180     ozaki 		    ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
   2292  1.180     ozaki 		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
   2293   1.53    itojun 		goto bad;
   2294    1.2    itojun 	}
   2295    1.2    itojun 
   2296    1.2    itojun 	/* RFC 2461 8.3 */
   2297   1.10    itojun 	nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
   2298   1.10    itojun 			 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
   2299    1.2    itojun 
   2300  1.188     ozaki 	m_put_rcvif_psref(ifp, &psref);
   2301  1.188     ozaki 	ifp = NULL;
   2302  1.188     ozaki 
   2303   1.67    itojun 	if (!is_onlink) {	/* better router case.  perform rtredirect. */
   2304    1.2    itojun 		/* perform rtredirect */
   2305    1.2    itojun 		struct sockaddr_in6 sdst;
   2306    1.2    itojun 		struct sockaddr_in6 sgw;
   2307    1.2    itojun 		struct sockaddr_in6 ssrc;
   2308   1.55    itojun 		unsigned long rtcount;
   2309   1.55    itojun 		struct rtentry *newrt = NULL;
   2310   1.55    itojun 
   2311   1.55    itojun 		/*
   2312   1.55    itojun 		 * do not install redirect route, if the number of entries
   2313   1.55    itojun 		 * is too much (> hiwat).  note that, the node (= host) will
   2314   1.55    itojun 		 * work just fine even if we do not install redirect route
   2315   1.55    itojun 		 * (there will be additional hops, though).
   2316   1.55    itojun 		 */
   2317   1.55    itojun 		rtcount = rt_timer_count(icmp6_redirect_timeout_q);
   2318  1.161  christos 		if (0 <= ip6_maxdynroutes && rtcount >= ip6_maxdynroutes)
   2319  1.161  christos 			goto freeit;
   2320   1.55    itojun 		if (0 <= icmp6_redirect_hiwat && rtcount > icmp6_redirect_hiwat)
   2321  1.184     ozaki 			goto freeit;
   2322   1.55    itojun 		else if (0 <= icmp6_redirect_lowat &&
   2323   1.55    itojun 		    rtcount > icmp6_redirect_lowat) {
   2324   1.55    itojun 			/*
   2325   1.55    itojun 			 * XXX nuke a victim, install the new one.
   2326   1.55    itojun 			 */
   2327   1.55    itojun 		}
   2328    1.2    itojun 
   2329  1.152    cegger 		memset(&sdst, 0, sizeof(sdst));
   2330  1.152    cegger 		memset(&sgw, 0, sizeof(sgw));
   2331  1.152    cegger 		memset(&ssrc, 0, sizeof(ssrc));
   2332    1.2    itojun 		sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
   2333    1.2    itojun 		sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
   2334    1.2    itojun 			sizeof(struct sockaddr_in6);
   2335    1.2    itojun 		bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
   2336    1.2    itojun 		bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
   2337    1.2    itojun 		bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
   2338    1.2    itojun 		rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
   2339  1.157    plunky 			   NULL, RTF_GATEWAY | RTF_HOST,
   2340    1.2    itojun 			   (struct sockaddr *)&ssrc,
   2341   1.55    itojun 			   &newrt);
   2342   1.55    itojun 
   2343   1.55    itojun 		if (newrt) {
   2344   1.55    itojun 			(void)rt_timer_add(newrt, icmp6_redirect_timeout,
   2345   1.55    itojun 			    icmp6_redirect_timeout_q);
   2346   1.55    itojun 			rtfree(newrt);
   2347   1.55    itojun 		}
   2348    1.2    itojun 	}
   2349    1.2    itojun 	/* finally update cached route in each socket via pfctlinput */
   2350   1.74    itojun 	{
   2351   1.74    itojun 		struct sockaddr_in6 sdst;
   2352    1.2    itojun 
   2353  1.138    dyoung 		sockaddr_in6_init(&sdst, &reddst6, 0, 0, 0);
   2354   1.74    itojun 		pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
   2355  1.162  christos #if defined(IPSEC)
   2356  1.168  christos 		if (ipsec_used)
   2357  1.168  christos 			key_sa_routechange((struct sockaddr *)&sdst);
   2358    1.2    itojun #endif
   2359   1.74    itojun 	}
   2360   1.23    itojun 
   2361   1.23    itojun  freeit:
   2362  1.188     ozaki 	if (ifp != NULL)
   2363  1.188     ozaki 		m_put_rcvif_psref(ifp, &psref);
   2364   1.23    itojun 	m_freem(m);
   2365   1.53    itojun 	return;
   2366   1.53    itojun 
   2367   1.53    itojun  bad:
   2368  1.188     ozaki 	m_put_rcvif_psref(ifp, &psref);
   2369  1.145   thorpej 	ICMP6_STATINC(ICMP6_STAT_BADREDIRECT);
   2370   1.53    itojun 	m_freem(m);
   2371    1.2    itojun }
   2372    1.2    itojun 
   2373    1.2    itojun void
   2374  1.133  christos icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
   2375    1.2    itojun {
   2376    1.2    itojun 	struct ifnet *ifp;	/* my outgoing interface */
   2377    1.2    itojun 	struct in6_addr *ifp_ll6;
   2378   1.75    itojun 	struct in6_addr *nexthop;
   2379    1.2    itojun 	struct ip6_hdr *sip6;	/* m0 as struct ip6_hdr */
   2380    1.2    itojun 	struct mbuf *m = NULL;	/* newly allocated one */
   2381    1.2    itojun 	struct ip6_hdr *ip6;	/* m as struct ip6_hdr */
   2382    1.2    itojun 	struct nd_redirect *nd_rd;
   2383    1.2    itojun 	size_t maxlen;
   2384    1.2    itojun 	u_char *p;
   2385   1.29    itojun 	struct sockaddr_in6 src_sa;
   2386    1.2    itojun 
   2387  1.145   thorpej 	icmp6_errcount(ICMP6_STAT_OUTERRHIST, ND_REDIRECT, 0);
   2388   1.35    itojun 
   2389    1.2    itojun 	/* if we are not router, we don't send icmp6 redirect */
   2390   1.94    itojun 	if (!ip6_forwarding)
   2391    1.2    itojun 		goto fail;
   2392    1.2    itojun 
   2393    1.2    itojun 	/* sanity check */
   2394    1.2    itojun 	if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
   2395    1.2    itojun 		goto fail;
   2396    1.5    itojun 
   2397    1.5    itojun 	/*
   2398    1.5    itojun 	 * Address check:
   2399    1.5    itojun 	 *  the source address must identify a neighbor, and
   2400    1.5    itojun 	 *  the destination address must not be a multicast address
   2401    1.5    itojun 	 *  [RFC 2461, sec 8.2]
   2402    1.5    itojun 	 */
   2403    1.2    itojun 	sip6 = mtod(m0, struct ip6_hdr *);
   2404  1.138    dyoung 	sockaddr_in6_init(&src_sa, &sip6->ip6_src, 0, 0, 0);
   2405   1.29    itojun 	if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
   2406    1.5    itojun 		goto fail;
   2407    1.2    itojun 	if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
   2408    1.2    itojun 		goto fail;	/* what should we do here? */
   2409    1.2    itojun 
   2410    1.2    itojun 	/* rate limit */
   2411    1.2    itojun 	if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
   2412    1.2    itojun 		goto fail;
   2413    1.2    itojun 
   2414    1.2    itojun 	/*
   2415    1.2    itojun 	 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
   2416    1.2    itojun 	 * we almost always ask for an mbuf cluster for simplicity.
   2417    1.2    itojun 	 * (MHLEN < IPV6_MMTU is almost always true)
   2418    1.2    itojun 	 */
   2419  1.154  christos 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
   2420  1.154  christos 	if (m && IPV6_MMTU >= MHLEN) {
   2421   1.23    itojun #if IPV6_MMTU >= MCLBYTES
   2422  1.155  christos 		_MCLGET(m, mcl_cache, IPV6_MMTU, M_DONTWAIT);
   2423  1.154  christos #else
   2424  1.154  christos 		MCLGET(m, M_DONTWAIT);
   2425   1.23    itojun #endif
   2426  1.154  christos 	}
   2427  1.154  christos 
   2428    1.2    itojun 	if (!m)
   2429    1.2    itojun 		goto fail;
   2430  1.187     ozaki 	m_reset_rcvif(m);
   2431   1.42    itojun 	m->m_len = 0;
   2432   1.42    itojun 	maxlen = M_TRAILINGSPACE(m);
   2433    1.2    itojun 	maxlen = min(IPV6_MMTU, maxlen);
   2434    1.2    itojun 	/* just for safety */
   2435   1.23    itojun 	if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
   2436   1.23    itojun 	    ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
   2437    1.2    itojun 		goto fail;
   2438   1.23    itojun 	}
   2439    1.2    itojun 
   2440    1.2    itojun 	{
   2441    1.2    itojun 		/* get ip6 linklocal address for ifp(my outgoing interface). */
   2442   1.23    itojun 		struct in6_ifaddr *ia;
   2443   1.23    itojun 		if ((ia = in6ifa_ifpforlinklocal(ifp,
   2444   1.23    itojun 						 IN6_IFF_NOTREADY|
   2445   1.23    itojun 						 IN6_IFF_ANYCAST)) == NULL)
   2446    1.2    itojun 			goto fail;
   2447    1.2    itojun 		ifp_ll6 = &ia->ia_addr.sin6_addr;
   2448    1.2    itojun 	}
   2449    1.2    itojun 
   2450    1.2    itojun 	/* get ip6 linklocal address for the router. */
   2451    1.2    itojun 	if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
   2452    1.2    itojun 		struct sockaddr_in6 *sin6;
   2453    1.2    itojun 		sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
   2454   1.75    itojun 		nexthop = &sin6->sin6_addr;
   2455   1.75    itojun 		if (!IN6_IS_ADDR_LINKLOCAL(nexthop))
   2456   1.75    itojun 			nexthop = NULL;
   2457    1.2    itojun 	} else
   2458   1.75    itojun 		nexthop = NULL;
   2459    1.2    itojun 
   2460    1.2    itojun 	/* ip6 */
   2461    1.2    itojun 	ip6 = mtod(m, struct ip6_hdr *);
   2462    1.2    itojun 	ip6->ip6_flow = 0;
   2463   1.13    itojun 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
   2464   1.13    itojun 	ip6->ip6_vfc |= IPV6_VERSION;
   2465    1.2    itojun 	/* ip6->ip6_plen will be set later */
   2466    1.2    itojun 	ip6->ip6_nxt = IPPROTO_ICMPV6;
   2467    1.2    itojun 	ip6->ip6_hlim = 255;
   2468    1.2    itojun 	/* ip6->ip6_src must be linklocal addr for my outgoing if. */
   2469    1.2    itojun 	bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
   2470    1.2    itojun 	bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
   2471    1.2    itojun 
   2472    1.2    itojun 	/* ND Redirect */
   2473    1.2    itojun 	nd_rd = (struct nd_redirect *)(ip6 + 1);
   2474    1.2    itojun 	nd_rd->nd_rd_type = ND_REDIRECT;
   2475    1.2    itojun 	nd_rd->nd_rd_code = 0;
   2476    1.2    itojun 	nd_rd->nd_rd_reserved = 0;
   2477    1.2    itojun 	if (rt->rt_flags & RTF_GATEWAY) {
   2478    1.2    itojun 		/*
   2479    1.2    itojun 		 * nd_rd->nd_rd_target must be a link-local address in
   2480    1.2    itojun 		 * better router cases.
   2481    1.2    itojun 		 */
   2482   1.75    itojun 		if (!nexthop)
   2483    1.2    itojun 			goto fail;
   2484   1.75    itojun 		bcopy(nexthop, &nd_rd->nd_rd_target,
   2485    1.2    itojun 		      sizeof(nd_rd->nd_rd_target));
   2486    1.2    itojun 		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
   2487    1.2    itojun 		      sizeof(nd_rd->nd_rd_dst));
   2488    1.2    itojun 	} else {
   2489    1.2    itojun 		/* make sure redtgt == reddst */
   2490   1.75    itojun 		nexthop = &sip6->ip6_dst;
   2491    1.2    itojun 		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
   2492    1.2    itojun 		      sizeof(nd_rd->nd_rd_target));
   2493    1.2    itojun 		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
   2494    1.2    itojun 		      sizeof(nd_rd->nd_rd_dst));
   2495    1.2    itojun 	}
   2496    1.2    itojun 
   2497    1.2    itojun 	p = (u_char *)(nd_rd + 1);
   2498    1.2    itojun 
   2499   1.75    itojun 	{
   2500   1.75    itojun 		/* target lladdr option */
   2501  1.182     ozaki 		struct llentry *ln = NULL;
   2502   1.75    itojun 		int len;
   2503   1.75    itojun 		struct nd_opt_hdr *nd_opt;
   2504   1.75    itojun 		char *lladdr;
   2505   1.75    itojun 
   2506  1.182     ozaki 		ln = nd6_lookup(nexthop, ifp, false);
   2507  1.182     ozaki 		if (ln == NULL)
   2508   1.75    itojun 			goto nolladdropt;
   2509   1.75    itojun 		len = sizeof(*nd_opt) + ifp->if_addrlen;
   2510   1.75    itojun 		len = (len + 7) & ~7;	/* round by 8 */
   2511   1.75    itojun 		/* safety check */
   2512  1.171     ozaki 		if (len + (p - (u_char *)ip6) > maxlen) {
   2513  1.182     ozaki 			LLE_RUNLOCK(ln);
   2514   1.75    itojun 			goto nolladdropt;
   2515  1.171     ozaki 		}
   2516  1.182     ozaki 		if (ln->la_flags & LLE_VALID) {
   2517   1.75    itojun 			nd_opt = (struct nd_opt_hdr *)p;
   2518   1.75    itojun 			nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
   2519   1.75    itojun 			nd_opt->nd_opt_len = len >> 3;
   2520   1.75    itojun 			lladdr = (char *)(nd_opt + 1);
   2521  1.182     ozaki 			memcpy(lladdr, &ln->ll_addr, ifp->if_addrlen);
   2522   1.75    itojun 			p += len;
   2523   1.75    itojun 		}
   2524  1.182     ozaki 		LLE_RUNLOCK(ln);
   2525    1.2    itojun 	}
   2526   1.75    itojun   nolladdropt:;
   2527    1.2    itojun 
   2528    1.2    itojun 	m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
   2529    1.2    itojun 
   2530    1.2    itojun 	/* just to be safe */
   2531    1.2    itojun 	if (m0->m_flags & M_DECRYPTED)
   2532    1.2    itojun 		goto noredhdropt;
   2533   1.23    itojun 	if (p - (u_char *)ip6 > maxlen)
   2534   1.23    itojun 		goto noredhdropt;
   2535    1.2    itojun 
   2536   1.91    itojun 	{
   2537   1.91    itojun 		/* redirected header option */
   2538   1.91    itojun 		int len;
   2539   1.91    itojun 		struct nd_opt_rd_hdr *nd_opt_rh;
   2540    1.2    itojun 
   2541   1.91    itojun 		/*
   2542   1.91    itojun 		 * compute the maximum size for icmp6 redirect header option.
   2543   1.91    itojun 		 * XXX room for auth header?
   2544   1.91    itojun 		 */
   2545   1.91    itojun 		len = maxlen - (p - (u_char *)ip6);
   2546   1.91    itojun 		len &= ~7;
   2547    1.2    itojun 
   2548   1.91    itojun 		/*
   2549   1.91    itojun 		 * Redirected header option spec (RFC2461 4.6.3) talks nothing
   2550   1.91    itojun 		 * about padding/truncate rule for the original IP packet.
   2551   1.91    itojun 		 * From the discussion on IPv6imp in Feb 1999,
   2552   1.91    itojun 		 * the consensus was:
   2553   1.91    itojun 		 * - "attach as much as possible" is the goal
   2554   1.91    itojun 		 * - pad if not aligned (original size can be guessed by
   2555   1.91    itojun 		 *   original ip6 header)
   2556   1.91    itojun 		 * Following code adds the padding if it is simple enough,
   2557   1.91    itojun 		 * and truncates if not.
   2558   1.91    itojun 		 */
   2559   1.91    itojun 		if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
   2560   1.91    itojun 			/* not enough room, truncate */
   2561   1.91    itojun 			m_adj(m0, (len - sizeof(*nd_opt_rh)) -
   2562   1.91    itojun 			    m0->m_pkthdr.len);
   2563   1.91    itojun 		} else {
   2564   1.91    itojun 			/*
   2565  1.115    rpaulo 			 * enough room, truncate if not aligned.
   2566   1.91    itojun 			 * we don't pad here for simplicity.
   2567   1.91    itojun 			 */
   2568   1.91    itojun 			size_t extra;
   2569    1.2    itojun 
   2570   1.91    itojun 			extra = m0->m_pkthdr.len % 8;
   2571   1.91    itojun 			if (extra) {
   2572    1.2    itojun 				/* truncate */
   2573   1.91    itojun 				m_adj(m0, -extra);
   2574    1.2    itojun 			}
   2575   1.91    itojun 			len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
   2576    1.2    itojun 		}
   2577   1.91    itojun 
   2578   1.91    itojun 		nd_opt_rh = (struct nd_opt_rd_hdr *)p;
   2579  1.152    cegger 		memset(nd_opt_rh, 0, sizeof(*nd_opt_rh));
   2580   1.91    itojun 		nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
   2581   1.91    itojun 		nd_opt_rh->nd_opt_rh_len = len >> 3;
   2582   1.91    itojun 		p += sizeof(*nd_opt_rh);
   2583   1.91    itojun 		m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
   2584   1.91    itojun 
   2585   1.91    itojun 		/* connect m0 to m */
   2586   1.95    itojun 		m->m_pkthdr.len += m0->m_pkthdr.len;
   2587   1.91    itojun 		m_cat(m, m0);
   2588   1.91    itojun 		m0 = NULL;
   2589    1.2    itojun 	}
   2590   1.91    itojun noredhdropt:
   2591   1.89    itojun 	if (m0) {
   2592   1.89    itojun 		m_freem(m0);
   2593   1.89    itojun 		m0 = NULL;
   2594   1.89    itojun 	}
   2595    1.2    itojun 
   2596  1.113    rpaulo 	/* XXX: clear embedded link IDs in the inner header */
   2597  1.113    rpaulo 	in6_clearscope(&sip6->ip6_src);
   2598  1.113    rpaulo 	in6_clearscope(&sip6->ip6_dst);
   2599  1.113    rpaulo 	in6_clearscope(&nd_rd->nd_rd_target);
   2600  1.113    rpaulo 	in6_clearscope(&nd_rd->nd_rd_dst);
   2601    1.2    itojun 
   2602    1.2    itojun 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
   2603    1.2    itojun 
   2604    1.2    itojun 	nd_rd->nd_rd_cksum = 0;
   2605    1.2    itojun 	nd_rd->nd_rd_cksum
   2606    1.2    itojun 		= in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
   2607    1.2    itojun 
   2608    1.2    itojun 	/* send the packet to outside... */
   2609  1.157    plunky 	if (ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL) != 0)
   2610   1.72    itojun 		icmp6_ifstat_inc(ifp, ifs6_out_error);
   2611   1.72    itojun 
   2612   1.68    itojun 	icmp6_ifstat_inc(ifp, ifs6_out_msg);
   2613   1.68    itojun 	icmp6_ifstat_inc(ifp, ifs6_out_redirect);
   2614  1.145   thorpej 	ICMP6_STATINC(ICMP6_STAT_OUTHIST + ND_REDIRECT);
   2615    1.2    itojun 
   2616    1.2    itojun 	return;
   2617    1.2    itojun 
   2618    1.2    itojun fail:
   2619    1.2    itojun 	if (m)
   2620    1.2    itojun 		m_freem(m);
   2621    1.2    itojun 	if (m0)
   2622    1.2    itojun 		m_freem(m0);
   2623    1.2    itojun }
   2624    1.2    itojun 
   2625    1.2    itojun /*
   2626    1.2    itojun  * ICMPv6 socket option processing.
   2627    1.2    itojun  */
   2628    1.2    itojun int
   2629  1.149    plunky icmp6_ctloutput(int op, struct socket *so, struct sockopt *sopt)
   2630    1.2    itojun {
   2631    1.2    itojun 	int error = 0;
   2632   1.56    itojun 	struct in6pcb *in6p = sotoin6pcb(so);
   2633    1.2    itojun 
   2634  1.149    plunky 	if (sopt->sopt_level != IPPROTO_ICMPV6)
   2635  1.149    plunky 		return rip6_ctloutput(op, so, sopt);
   2636   1.56    itojun 
   2637   1.56    itojun 	switch (op) {
   2638   1.56    itojun 	case PRCO_SETOPT:
   2639  1.149    plunky 		switch (sopt->sopt_name) {
   2640   1.56    itojun 		case ICMP6_FILTER:
   2641   1.56    itojun 		    {
   2642  1.149    plunky 			struct icmp6_filter fil;
   2643   1.56    itojun 
   2644  1.149    plunky 			error = sockopt_get(sopt, &fil, sizeof(fil));
   2645  1.149    plunky 			if (error)
   2646   1.56    itojun 				break;
   2647  1.149    plunky 			memcpy(in6p->in6p_icmp6filt, &fil,
   2648  1.149    plunky 			    sizeof(struct icmp6_filter));
   2649   1.56    itojun 			error = 0;
   2650   1.56    itojun 			break;
   2651   1.56    itojun 		    }
   2652   1.56    itojun 
   2653   1.56    itojun 		default:
   2654   1.56    itojun 			error = ENOPROTOOPT;
   2655   1.56    itojun 			break;
   2656   1.56    itojun 		}
   2657   1.56    itojun 		break;
   2658   1.56    itojun 
   2659   1.56    itojun 	case PRCO_GETOPT:
   2660  1.149    plunky 		switch (sopt->sopt_name) {
   2661   1.56    itojun 		case ICMP6_FILTER:
   2662   1.56    itojun 		    {
   2663  1.149    plunky 			if (in6p->in6p_icmp6filt == NULL) {
   2664   1.56    itojun 				error = EINVAL;
   2665   1.56    itojun 				break;
   2666   1.56    itojun 			}
   2667  1.149    plunky 			error = sockopt_set(sopt, in6p->in6p_icmp6filt,
   2668  1.149    plunky 			    sizeof(struct icmp6_filter));
   2669   1.56    itojun 			break;
   2670   1.56    itojun 		    }
   2671   1.56    itojun 
   2672   1.56    itojun 		default:
   2673   1.56    itojun 			error = ENOPROTOOPT;
   2674   1.56    itojun 			break;
   2675   1.56    itojun 		}
   2676   1.56    itojun 		break;
   2677    1.2    itojun 	}
   2678    1.2    itojun 
   2679   1.86    itojun 	return (error);
   2680    1.2    itojun }
   2681    1.2    itojun 
   2682    1.2    itojun /*
   2683    1.2    itojun  * Perform rate limit check.
   2684    1.2    itojun  * Returns 0 if it is okay to send the icmp6 packet.
   2685    1.2    itojun  * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
   2686    1.2    itojun  * limitation.
   2687   1.35    itojun  *
   2688    1.2    itojun  * XXX per-destination/type check necessary?
   2689    1.2    itojun  */
   2690    1.2    itojun static int
   2691  1.122  christos icmp6_ratelimit(
   2692  1.123  christos 	const struct in6_addr *dst,	/* not used at this moment */
   2693  1.123  christos 	const int type,		/* not used at this moment */
   2694  1.123  christos 	const int code)		/* not used at this moment */
   2695    1.2    itojun {
   2696   1.35    itojun 	int ret;
   2697   1.35    itojun 
   2698   1.67    itojun 	ret = 0;	/* okay to send */
   2699    1.2    itojun 
   2700   1.35    itojun 	/* PPS limit */
   2701   1.37    itojun 	if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
   2702   1.37    itojun 	    icmp6errppslim)) {
   2703   1.37    itojun 		/* The packet is subject to rate limit */
   2704   1.35    itojun 		ret++;
   2705   1.35    itojun 	}
   2706   1.35    itojun 
   2707   1.35    itojun 	return ret;
   2708    1.2    itojun }
   2709    1.8    itojun 
   2710    1.8    itojun static struct rtentry *
   2711  1.133  christos icmp6_mtudisc_clone(struct sockaddr *dst)
   2712    1.8    itojun {
   2713    1.8    itojun 	struct rtentry *rt;
   2714    1.8    itojun 	int    error;
   2715    1.8    itojun 
   2716    1.8    itojun 	rt = rtalloc1(dst, 1);
   2717    1.8    itojun 	if (rt == 0)
   2718    1.8    itojun 		return NULL;
   2719   1.29    itojun 
   2720    1.8    itojun 	/* If we didn't get a host route, allocate one */
   2721    1.8    itojun 	if ((rt->rt_flags & RTF_HOST) == 0) {
   2722    1.8    itojun 		struct rtentry *nrt;
   2723    1.8    itojun 
   2724  1.181     ozaki 		error = rtrequest(RTM_ADD, dst, rt->rt_gateway, NULL,
   2725    1.8    itojun 		    RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
   2726    1.8    itojun 		if (error) {
   2727    1.8    itojun 			rtfree(rt);
   2728    1.8    itojun 			return NULL;
   2729    1.8    itojun 		}
   2730    1.8    itojun 		nrt->rt_rmx = rt->rt_rmx;
   2731    1.8    itojun 		rtfree(rt);
   2732    1.8    itojun 		rt = nrt;
   2733    1.8    itojun 	}
   2734    1.8    itojun 	error = rt_timer_add(rt, icmp6_mtudisc_timeout,
   2735    1.8    itojun 			icmp6_mtudisc_timeout_q);
   2736    1.8    itojun 	if (error) {
   2737    1.8    itojun 		rtfree(rt);
   2738    1.8    itojun 		return NULL;
   2739    1.8    itojun 	}
   2740    1.8    itojun 
   2741    1.8    itojun 	return rt;	/* caller need to call rtfree() */
   2742    1.8    itojun }
   2743    1.8    itojun 
   2744    1.8    itojun static void
   2745  1.123  christos icmp6_mtudisc_timeout(struct rtentry *rt, struct rttimer *r)
   2746    1.8    itojun {
   2747  1.176     ozaki 
   2748  1.176     ozaki 	KASSERT(rt != NULL);
   2749  1.176     ozaki 	rt_assert_referenced(rt);
   2750  1.176     ozaki 
   2751   1.29    itojun 	if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
   2752    1.8    itojun 	    (RTF_DYNAMIC | RTF_HOST)) {
   2753  1.181     ozaki 		rtrequest(RTM_DELETE, rt_getkey(rt),
   2754  1.174     ozaki 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
   2755    1.8    itojun 	} else {
   2756   1.78    itojun 		if (!(rt->rt_rmx.rmx_locks & RTV_MTU))
   2757   1.78    itojun 			rt->rt_rmx.rmx_mtu = 0;
   2758   1.55    itojun 	}
   2759   1.55    itojun }
   2760   1.55    itojun 
   2761   1.55    itojun static void
   2762  1.123  christos icmp6_redirect_timeout(struct rtentry *rt, struct rttimer *r)
   2763   1.55    itojun {
   2764  1.176     ozaki 
   2765  1.176     ozaki 	KASSERT(rt != NULL);
   2766  1.176     ozaki 	rt_assert_referenced(rt);
   2767  1.176     ozaki 
   2768   1.55    itojun 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) ==
   2769   1.55    itojun 	    (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) {
   2770  1.181     ozaki 		rtrequest(RTM_DELETE, rt_getkey(rt),
   2771  1.174     ozaki 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
   2772    1.8    itojun 	}
   2773    1.8    itojun }
   2774   1.34       mrg 
   2775  1.103    atatat /*
   2776  1.103    atatat  * sysctl helper routine for the net.inet6.icmp6.nd6 nodes.  silly?
   2777  1.103    atatat  */
   2778  1.103    atatat static int
   2779  1.103    atatat sysctl_net_inet6_icmp6_nd6(SYSCTLFN_ARGS)
   2780    1.2    itojun {
   2781  1.122  christos 	(void)&name;
   2782  1.122  christos 	(void)&l;
   2783  1.122  christos 	(void)&oname;
   2784    1.2    itojun 
   2785  1.104       lha 	if (namelen != 0)
   2786  1.103    atatat 		return (EINVAL);
   2787  1.103    atatat 
   2788  1.104       lha 	return (nd6_sysctl(rnode->sysctl_num, oldp, oldlenp,
   2789  1.109  christos 	    /*XXXUNCONST*/
   2790  1.109  christos 	    __UNCONST(newp), newlen));
   2791  1.103    atatat }
   2792    1.2    itojun 
   2793  1.145   thorpej static int
   2794  1.145   thorpej sysctl_net_inet6_icmp6_stats(SYSCTLFN_ARGS)
   2795  1.145   thorpej {
   2796  1.145   thorpej 
   2797  1.147   thorpej 	return (NETSTAT_SYSCTL(icmp6stat_percpu, ICMP6_NSTATS));
   2798  1.145   thorpej }
   2799  1.145   thorpej 
   2800  1.177     ozaki static int
   2801  1.177     ozaki sysctl_net_inet6_icmp6_redirtimeout(SYSCTLFN_ARGS)
   2802  1.177     ozaki {
   2803  1.177     ozaki 	int error, tmp;
   2804  1.177     ozaki 	struct sysctlnode node;
   2805  1.177     ozaki 
   2806  1.177     ozaki 	node = *rnode;
   2807  1.177     ozaki 	node.sysctl_data = &tmp;
   2808  1.177     ozaki 	tmp = icmp6_redirtimeout;
   2809  1.177     ozaki 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2810  1.177     ozaki 	if (error || newp == NULL)
   2811  1.177     ozaki 		return error;
   2812  1.177     ozaki 	if (tmp < 0)
   2813  1.177     ozaki 		return EINVAL;
   2814  1.177     ozaki 	icmp6_redirtimeout = tmp;
   2815  1.177     ozaki 
   2816  1.177     ozaki 	if (icmp6_redirect_timeout_q != NULL) {
   2817  1.177     ozaki 		if (icmp6_redirtimeout == 0) {
   2818  1.177     ozaki 			rt_timer_queue_destroy(icmp6_redirect_timeout_q,
   2819  1.177     ozaki 			    true);
   2820  1.177     ozaki 		} else {
   2821  1.177     ozaki 			rt_timer_queue_change(icmp6_redirect_timeout_q,
   2822  1.177     ozaki 			    icmp6_redirtimeout);
   2823  1.177     ozaki 		}
   2824  1.177     ozaki 	} else if (icmp6_redirtimeout > 0) {
   2825  1.177     ozaki 		icmp6_redirect_timeout_q =
   2826  1.177     ozaki 		    rt_timer_queue_create(icmp6_redirtimeout);
   2827  1.177     ozaki 	}
   2828  1.177     ozaki 
   2829  1.177     ozaki 	return 0;
   2830  1.177     ozaki }
   2831  1.177     ozaki 
   2832  1.153     pooka static void
   2833  1.153     pooka sysctl_net_inet6_icmp6_setup(struct sysctllog **clog)
   2834  1.103    atatat {
   2835  1.115    rpaulo 	extern int nd6_maxqueuelen; /* defined in nd6.c */
   2836    1.2    itojun 
   2837  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2838  1.105    atatat 		       CTLFLAG_PERMANENT,
   2839  1.103    atatat 		       CTLTYPE_NODE, "inet6", NULL,
   2840  1.103    atatat 		       NULL, 0, NULL, 0,
   2841  1.103    atatat 		       CTL_NET, PF_INET6, CTL_EOL);
   2842  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2843  1.105    atatat 		       CTLFLAG_PERMANENT,
   2844  1.107    atatat 		       CTLTYPE_NODE, "icmp6",
   2845  1.107    atatat 		       SYSCTL_DESCR("ICMPv6 related settings"),
   2846  1.103    atatat 		       NULL, 0, NULL, 0,
   2847  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6, CTL_EOL);
   2848  1.103    atatat 
   2849  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2850  1.105    atatat 		       CTLFLAG_PERMANENT,
   2851  1.107    atatat 		       CTLTYPE_STRUCT, "stats",
   2852  1.107    atatat 		       SYSCTL_DESCR("ICMPv6 transmission statistics"),
   2853  1.145   thorpej 		       sysctl_net_inet6_icmp6_stats, 0, NULL, 0,
   2854  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2855  1.103    atatat 		       ICMPV6CTL_STATS, CTL_EOL);
   2856  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2857  1.105    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2858  1.107    atatat 		       CTLTYPE_INT, "rediraccept",
   2859  1.107    atatat 		       SYSCTL_DESCR("Accept and process redirect messages"),
   2860  1.103    atatat 		       NULL, 0, &icmp6_rediraccept, 0,
   2861  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2862  1.103    atatat 		       ICMPV6CTL_REDIRACCEPT, CTL_EOL);
   2863  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2864  1.105    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2865  1.107    atatat 		       CTLTYPE_INT, "redirtimeout",
   2866  1.107    atatat 		       SYSCTL_DESCR("Redirect generated route lifetime"),
   2867  1.177     ozaki 		       sysctl_net_inet6_icmp6_redirtimeout, 0,
   2868  1.177     ozaki 		       &icmp6_redirtimeout, 0,
   2869  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2870  1.103    atatat 		       ICMPV6CTL_REDIRTIMEOUT, CTL_EOL);
   2871  1.103    atatat #if 0 /* obsoleted */
   2872  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2873  1.105    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2874  1.103    atatat 		       CTLTYPE_INT, "errratelimit", NULL,
   2875  1.103    atatat 		       NULL, 0, &icmp6_errratelimit, 0,
   2876  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2877  1.103    atatat 		       ICMPV6CTL_ERRRATELIMIT, CTL_EOL);
   2878  1.103    atatat #endif
   2879  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2880  1.105    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2881  1.107    atatat 		       CTLTYPE_INT, "nd6_prune",
   2882  1.107    atatat 		       SYSCTL_DESCR("Neighbor discovery prune interval"),
   2883  1.103    atatat 		       NULL, 0, &nd6_prune, 0,
   2884  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2885  1.103    atatat 		       ICMPV6CTL_ND6_PRUNE, CTL_EOL);
   2886  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2887  1.105    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2888  1.107    atatat 		       CTLTYPE_INT, "nd6_delay",
   2889  1.107    atatat 		       SYSCTL_DESCR("First probe delay time"),
   2890  1.103    atatat 		       NULL, 0, &nd6_delay, 0,
   2891  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2892  1.103    atatat 		       ICMPV6CTL_ND6_DELAY, CTL_EOL);
   2893  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2894  1.105    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2895  1.107    atatat 		       CTLTYPE_INT, "nd6_umaxtries",
   2896  1.107    atatat 		       SYSCTL_DESCR("Number of unicast discovery attempts"),
   2897  1.103    atatat 		       NULL, 0, &nd6_umaxtries, 0,
   2898  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2899  1.103    atatat 		       ICMPV6CTL_ND6_UMAXTRIES, CTL_EOL);
   2900  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2901  1.105    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2902  1.107    atatat 		       CTLTYPE_INT, "nd6_mmaxtries",
   2903  1.107    atatat 		       SYSCTL_DESCR("Number of multicast discovery attempts"),
   2904  1.103    atatat 		       NULL, 0, &nd6_mmaxtries, 0,
   2905  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2906  1.103    atatat 		       ICMPV6CTL_ND6_MMAXTRIES, CTL_EOL);
   2907  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2908  1.105    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2909  1.107    atatat 		       CTLTYPE_INT, "nd6_useloopback",
   2910  1.107    atatat 		       SYSCTL_DESCR("Use loopback interface for local traffic"),
   2911  1.103    atatat 		       NULL, 0, &nd6_useloopback, 0,
   2912  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2913  1.103    atatat 		       ICMPV6CTL_ND6_USELOOPBACK, CTL_EOL);
   2914  1.103    atatat #if 0 /* obsoleted */
   2915  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2916  1.105    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2917  1.103    atatat 		       CTLTYPE_INT, "nd6_proxyall", NULL,
   2918  1.103    atatat 		       NULL, 0, &nd6_proxyall, 0,
   2919  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2920  1.103    atatat 		       ICMPV6CTL_ND6_PROXYALL, CTL_EOL);
   2921  1.103    atatat #endif
   2922  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2923  1.105    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2924  1.107    atatat 		       CTLTYPE_INT, "nodeinfo",
   2925  1.107    atatat 		       SYSCTL_DESCR("Respond to node information requests"),
   2926  1.103    atatat 		       NULL, 0, &icmp6_nodeinfo, 0,
   2927  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2928  1.103    atatat 		       ICMPV6CTL_NODEINFO, CTL_EOL);
   2929  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2930  1.105    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2931  1.107    atatat 		       CTLTYPE_INT, "errppslimit",
   2932  1.107    atatat 		       SYSCTL_DESCR("Maximum ICMP errors sent per second"),
   2933  1.103    atatat 		       NULL, 0, &icmp6errppslim, 0,
   2934  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2935  1.103    atatat 		       ICMPV6CTL_ERRPPSLIMIT, CTL_EOL);
   2936  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2937  1.105    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2938  1.107    atatat 		       CTLTYPE_INT, "nd6_maxnudhint",
   2939  1.107    atatat 		       SYSCTL_DESCR("Maximum neighbor unreachable hint count"),
   2940  1.103    atatat 		       NULL, 0, &nd6_maxnudhint, 0,
   2941  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2942  1.103    atatat 		       ICMPV6CTL_ND6_MAXNUDHINT, CTL_EOL);
   2943  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2944  1.105    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2945  1.107    atatat 		       CTLTYPE_INT, "mtudisc_hiwat",
   2946  1.107    atatat 		       SYSCTL_DESCR("Low mark on MTU Discovery route timers"),
   2947  1.103    atatat 		       NULL, 0, &icmp6_mtudisc_hiwat, 0,
   2948  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2949  1.103    atatat 		       ICMPV6CTL_MTUDISC_HIWAT, CTL_EOL);
   2950  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2951  1.105    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2952  1.107    atatat 		       CTLTYPE_INT, "mtudisc_lowat",
   2953  1.107    atatat 		       SYSCTL_DESCR("Low mark on MTU Discovery route timers"),
   2954  1.103    atatat 		       NULL, 0, &icmp6_mtudisc_lowat, 0,
   2955  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2956  1.103    atatat 		       ICMPV6CTL_MTUDISC_LOWAT, CTL_EOL);
   2957  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2958  1.105    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2959  1.107    atatat 		       CTLTYPE_INT, "nd6_debug",
   2960  1.107    atatat 		       SYSCTL_DESCR("Enable neighbor discovery debug output"),
   2961  1.103    atatat 		       NULL, 0, &nd6_debug, 0,
   2962  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2963  1.103    atatat 		       ICMPV6CTL_ND6_DEBUG, CTL_EOL);
   2964  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2965  1.105    atatat 		       CTLFLAG_PERMANENT,
   2966  1.107    atatat 		       CTLTYPE_STRUCT, "nd6_drlist",
   2967  1.107    atatat 		       SYSCTL_DESCR("Default router list"),
   2968  1.103    atatat 		       sysctl_net_inet6_icmp6_nd6, 0, NULL, 0,
   2969  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2970  1.103    atatat 		       ICMPV6CTL_ND6_DRLIST, CTL_EOL);
   2971  1.105    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2972  1.105    atatat 		       CTLFLAG_PERMANENT,
   2973  1.107    atatat 		       CTLTYPE_STRUCT, "nd6_prlist",
   2974  1.107    atatat 		       SYSCTL_DESCR("Prefix list"),
   2975  1.103    atatat 		       sysctl_net_inet6_icmp6_nd6, 0, NULL, 0,
   2976  1.103    atatat 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2977  1.103    atatat 		       ICMPV6CTL_ND6_PRLIST, CTL_EOL);
   2978  1.115    rpaulo 	sysctl_createv(clog, 0, NULL, NULL,
   2979  1.115    rpaulo 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2980  1.115    rpaulo 		       CTLTYPE_INT, "maxqueuelen",
   2981  1.115    rpaulo 		       SYSCTL_DESCR("max packet queue len for a unresolved ND"),
   2982  1.115    rpaulo 		       NULL, 1, &nd6_maxqueuelen, 0,
   2983  1.115    rpaulo 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2984  1.115    rpaulo 		       ICMPV6CTL_ND6_MAXQLEN, CTL_EOL);
   2985    1.2    itojun }
   2986  1.145   thorpej 
   2987  1.145   thorpej void
   2988  1.145   thorpej icmp6_statinc(u_int stat)
   2989  1.145   thorpej {
   2990  1.145   thorpej 
   2991  1.145   thorpej 	KASSERT(stat < ICMP6_NSTATS);
   2992  1.145   thorpej 	ICMP6_STATINC(stat);
   2993  1.145   thorpej }
   2994