Home | History | Annotate | Line # | Download | only in netinet
ip_icmp.c revision 1.162
      1  1.162      maxv /*	$NetBSD: ip_icmp.c,v 1.162 2018/01/19 12:50:27 maxv Exp $	*/
      2   1.37    itojun 
      3   1.37    itojun /*
      4   1.37    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5   1.37    itojun  * All rights reserved.
      6   1.67    itojun  *
      7   1.37    itojun  * Redistribution and use in source and binary forms, with or without
      8   1.37    itojun  * modification, are permitted provided that the following conditions
      9   1.37    itojun  * are met:
     10   1.37    itojun  * 1. Redistributions of source code must retain the above copyright
     11   1.37    itojun  *    notice, this list of conditions and the following disclaimer.
     12   1.37    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.37    itojun  *    notice, this list of conditions and the following disclaimer in the
     14   1.37    itojun  *    documentation and/or other materials provided with the distribution.
     15   1.37    itojun  * 3. Neither the name of the project nor the names of its contributors
     16   1.37    itojun  *    may be used to endorse or promote products derived from this software
     17   1.37    itojun  *    without specific prior written permission.
     18   1.67    itojun  *
     19   1.37    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20   1.37    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.37    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.37    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23   1.37    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.37    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.37    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.37    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.37    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.37    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.37    itojun  * SUCH DAMAGE.
     30   1.37    itojun  */
     31   1.10       cgd 
     32   1.31   thorpej /*-
     33   1.55   thorpej  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
     34   1.31   thorpej  * All rights reserved.
     35   1.31   thorpej  *
     36   1.31   thorpej  * This code is derived from software contributed to The NetBSD Foundation
     37   1.31   thorpej  * by Public Access Networks Corporation ("Panix").  It was developed under
     38   1.31   thorpej  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
     39   1.31   thorpej  *
     40   1.55   thorpej  * This code is derived from software contributed to The NetBSD Foundation
     41   1.55   thorpej  * by Jason R. Thorpe of Zembu Labs, Inc.
     42   1.55   thorpej  *
     43   1.31   thorpej  * Redistribution and use in source and binary forms, with or without
     44   1.31   thorpej  * modification, are permitted provided that the following conditions
     45   1.31   thorpej  * are met:
     46   1.31   thorpej  * 1. Redistributions of source code must retain the above copyright
     47   1.31   thorpej  *    notice, this list of conditions and the following disclaimer.
     48   1.31   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.31   thorpej  *    notice, this list of conditions and the following disclaimer in the
     50   1.31   thorpej  *    documentation and/or other materials provided with the distribution.
     51   1.31   thorpej  *
     52   1.31   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     53   1.31   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     54   1.31   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     55   1.31   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     56   1.31   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     57   1.31   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     58   1.31   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     59   1.31   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     60   1.31   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     61   1.31   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     62   1.31   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     63   1.31   thorpej  */
     64   1.31   thorpej 
     65    1.1       cgd /*
     66    1.9   mycroft  * Copyright (c) 1982, 1986, 1988, 1993
     67    1.9   mycroft  *	The Regents of the University of California.  All rights reserved.
     68    1.1       cgd  *
     69    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     70    1.1       cgd  * modification, are permitted provided that the following conditions
     71    1.1       cgd  * are met:
     72    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     73    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     74    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     75    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     76    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     77   1.75       agc  * 3. Neither the name of the University nor the names of its contributors
     78    1.1       cgd  *    may be used to endorse or promote products derived from this software
     79    1.1       cgd  *    without specific prior written permission.
     80    1.1       cgd  *
     81    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     82    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     83    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     84    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     85    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     86    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     87    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     88    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     89    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     90    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     91    1.1       cgd  * SUCH DAMAGE.
     92    1.1       cgd  *
     93   1.10       cgd  *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
     94    1.1       cgd  */
     95   1.66     lukem 
     96   1.66     lukem #include <sys/cdefs.h>
     97  1.162      maxv __KERNEL_RCSID(0, "$NetBSD: ip_icmp.c,v 1.162 2018/01/19 12:50:27 maxv Exp $");
     98   1.38   thorpej 
     99  1.141     pooka #ifdef _KERNEL_OPT
    100   1.38   thorpej #include "opt_ipsec.h"
    101  1.141     pooka #endif
    102    1.1       cgd 
    103    1.4   mycroft #include <sys/param.h>
    104    1.4   mycroft #include <sys/systm.h>
    105    1.4   mycroft #include <sys/mbuf.h>
    106    1.4   mycroft #include <sys/protosw.h>
    107    1.4   mycroft #include <sys/socket.h>
    108  1.155     ozaki #include <sys/socketvar.h> /* For softnet_lock */
    109  1.133     rmind #include <sys/kmem.h>
    110    1.4   mycroft #include <sys/time.h>
    111    1.4   mycroft #include <sys/kernel.h>
    112   1.63       kml #include <sys/syslog.h>
    113   1.19  christos #include <sys/sysctl.h>
    114    1.1       cgd 
    115    1.9   mycroft #include <net/if.h>
    116    1.4   mycroft #include <net/route.h>
    117    1.1       cgd 
    118    1.4   mycroft #include <netinet/in.h>
    119    1.4   mycroft #include <netinet/in_systm.h>
    120    1.4   mycroft #include <netinet/in_var.h>
    121    1.4   mycroft #include <netinet/ip.h>
    122    1.4   mycroft #include <netinet/ip_icmp.h>
    123   1.19  christos #include <netinet/ip_var.h>
    124   1.39  sommerfe #include <netinet/in_pcb.h>
    125   1.92      yamt #include <netinet/in_proto.h>
    126    1.4   mycroft #include <netinet/icmp_var.h>
    127  1.116   thorpej #include <netinet/icmp_private.h>
    128  1.156     ozaki #include <netinet/wqinput.h>
    129    1.1       cgd 
    130  1.131  christos #ifdef IPSEC
    131   1.76  jonathan #include <netipsec/ipsec.h>
    132   1.76  jonathan #include <netipsec/key.h>
    133  1.131  christos #endif	/* IPSEC*/
    134   1.76  jonathan 
    135    1.1       cgd /*
    136    1.1       cgd  * ICMP routines: error generation, receive packet processing, and
    137    1.1       cgd  * routines to turnaround packets back to the originator, and
    138    1.1       cgd  * host table maintenance routines.
    139    1.1       cgd  */
    140    1.9   mycroft 
    141    1.9   mycroft int	icmpmaskrepl = 0;
    142  1.122  christos int	icmpbmcastecho = 0;
    143    1.1       cgd #ifdef ICMPPRINTFS
    144    1.1       cgd int	icmpprintfs = 0;
    145    1.1       cgd #endif
    146   1.47   darrenr int	icmpreturndatabytes = 8;
    147    1.1       cgd 
    148  1.116   thorpej percpu_t *icmpstat_percpu;
    149   1.87      cube 
    150   1.55   thorpej /*
    151   1.55   thorpej  * List of callbacks to notify when Path MTU changes are made.
    152   1.55   thorpej  */
    153   1.57    itojun struct icmp_mtudisc_callback {
    154   1.57    itojun 	LIST_ENTRY(icmp_mtudisc_callback) mc_list;
    155   1.89     perry 	void (*mc_func)(struct in_addr);
    156   1.55   thorpej };
    157   1.55   thorpej 
    158   1.57    itojun LIST_HEAD(, icmp_mtudisc_callback) icmp_mtudisc_callbacks =
    159   1.55   thorpej     LIST_HEAD_INITIALIZER(&icmp_mtudisc_callbacks);
    160   1.55   thorpej 
    161   1.37    itojun #if 0
    162   1.98      matt static u_int	ip_next_mtu(u_int, int);
    163   1.37    itojun #else
    164   1.98      matt /*static*/ u_int	ip_next_mtu(u_int, int);
    165   1.37    itojun #endif
    166   1.37    itojun 
    167   1.51    itojun extern int icmperrppslim;
    168   1.51    itojun static int icmperrpps_count = 0;
    169   1.51    itojun static struct timeval icmperrppslim_last;
    170   1.63       kml static int icmp_rediraccept = 1;
    171   1.68    itojun static int icmp_redirtimeout = 600;
    172   1.63       kml static struct rttimer_queue *icmp_redirect_timeout_q = NULL;
    173   1.40   thorpej 
    174  1.158     ozaki /* Protect mtudisc and redirect stuffs */
    175  1.158     ozaki static kmutex_t icmp_mtx __cacheline_aligned;
    176  1.158     ozaki 
    177   1.89     perry static void icmp_mtudisc_timeout(struct rtentry *, struct rttimer *);
    178   1.89     perry static void icmp_redirect_timeout(struct rtentry *, struct rttimer *);
    179   1.24       kml 
    180  1.121     pooka static void sysctl_netinet_icmp_setup(struct sysctllog **);
    181   1.63       kml 
    182  1.156     ozaki /* workqueue-based pr_input */
    183  1.156     ozaki static struct wqinput *icmp_wqinput;
    184  1.156     ozaki static void _icmp_input(struct mbuf *, int, int);
    185  1.156     ozaki 
    186   1.63       kml void
    187   1.90     perry icmp_init(void)
    188   1.63       kml {
    189  1.121     pooka 
    190  1.121     pooka 	sysctl_netinet_icmp_setup(NULL);
    191  1.121     pooka 
    192  1.158     ozaki 	mutex_init(&icmp_mtx, MUTEX_DEFAULT, IPL_NONE);
    193   1.67    itojun 	/*
    194   1.67    itojun 	 * This is only useful if the user initializes redirtimeout to
    195   1.63       kml 	 * something other than zero.
    196   1.63       kml 	 */
    197  1.158     ozaki 	mutex_enter(&icmp_mtx);
    198  1.160     ozaki 	icmp_redirect_timeout_q = rt_timer_queue_create(icmp_redirtimeout);
    199  1.158     ozaki 	mutex_exit(&icmp_mtx);
    200  1.116   thorpej 
    201  1.116   thorpej 	icmpstat_percpu = percpu_alloc(sizeof(uint64_t) * ICMP_NSTATS);
    202  1.156     ozaki 	icmp_wqinput = wqinput_create("icmp", _icmp_input);
    203   1.63       kml }
    204   1.63       kml 
    205  1.159     ozaki void
    206  1.159     ozaki icmp_mtudisc_lock(void)
    207  1.159     ozaki {
    208  1.159     ozaki 
    209  1.159     ozaki 	mutex_enter(&icmp_mtx);
    210  1.159     ozaki }
    211  1.159     ozaki 
    212  1.159     ozaki void
    213  1.159     ozaki icmp_mtudisc_unlock(void)
    214  1.159     ozaki {
    215  1.159     ozaki 
    216  1.159     ozaki 	mutex_exit(&icmp_mtx);
    217  1.159     ozaki }
    218  1.159     ozaki 
    219    1.1       cgd /*
    220   1.55   thorpej  * Register a Path MTU Discovery callback.
    221   1.55   thorpej  */
    222   1.55   thorpej void
    223   1.90     perry icmp_mtudisc_callback_register(void (*func)(struct in_addr))
    224   1.55   thorpej {
    225  1.158     ozaki 	struct icmp_mtudisc_callback *mc, *new;
    226  1.158     ozaki 
    227  1.158     ozaki 	new = kmem_alloc(sizeof(*mc), KM_SLEEP);
    228   1.55   thorpej 
    229  1.158     ozaki 	mutex_enter(&icmp_mtx);
    230   1.55   thorpej 	for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL;
    231   1.55   thorpej 	     mc = LIST_NEXT(mc, mc_list)) {
    232  1.158     ozaki 		if (mc->mc_func == func) {
    233  1.158     ozaki 			mutex_exit(&icmp_mtx);
    234  1.158     ozaki 			kmem_free(new, sizeof(*mc));
    235   1.55   thorpej 			return;
    236  1.158     ozaki 		}
    237   1.55   thorpej 	}
    238   1.55   thorpej 
    239  1.158     ozaki 	new->mc_func = func;
    240  1.158     ozaki 	LIST_INSERT_HEAD(&icmp_mtudisc_callbacks, new, mc_list);
    241  1.158     ozaki 	mutex_exit(&icmp_mtx);
    242   1.55   thorpej }
    243   1.55   thorpej 
    244   1.55   thorpej /*
    245  1.162      maxv  * Generate an error packet of type error in response to a bad IP packet. 'n'
    246  1.162      maxv  * contains this packet. We create 'm' and send it.
    247  1.162      maxv  *
    248  1.162      maxv  * As we are not required to return everything we have, we return whatever
    249  1.162      maxv  * we can return at ease.
    250  1.162      maxv  *
    251  1.162      maxv  * Note that ICMP datagrams longer than 576 octets are out of spec according
    252  1.162      maxv  * to RFC1812; the limit on icmpreturndatabytes will keep things below that
    253  1.162      maxv  * limit.
    254    1.1       cgd  */
    255    1.6   mycroft void
    256  1.162      maxv icmp_error(struct mbuf *n, int type, int code, n_long dest, int destmtu)
    257    1.1       cgd {
    258   1.44  augustss 	struct ip *oip = mtod(n, struct ip *), *nip;
    259  1.162      maxv 	const unsigned oiphlen = oip->ip_hl << 2;
    260   1.44  augustss 	struct icmp *icp;
    261   1.44  augustss 	struct mbuf *m;
    262  1.100     peter 	struct m_tag *mtag;
    263  1.162      maxv 	unsigned datalen, mblen, totlen;
    264    1.1       cgd 
    265    1.1       cgd #ifdef ICMPPRINTFS
    266    1.1       cgd 	if (icmpprintfs)
    267  1.101     peter 		printf("icmp_error(%p, type:%d, code:%d)\n", oip, type, code);
    268    1.1       cgd #endif
    269  1.162      maxv 
    270    1.1       cgd 	if (type != ICMP_REDIRECT)
    271  1.116   thorpej 		ICMP_STATINC(ICMP_STAT_ERROR);
    272  1.162      maxv 
    273    1.1       cgd 	/*
    274  1.162      maxv 	 * Don't send error if:
    275  1.162      maxv 	 *  - The original packet was encrypted.
    276  1.162      maxv 	 *  - The packet is multicast or broadcast.
    277  1.162      maxv 	 *  - The packet is not the first fragment of the message.
    278  1.162      maxv 	 *  - The packet is an ICMP message with an unknown type.
    279    1.1       cgd 	 */
    280   1.42    itojun 	if (n->m_flags & M_DECRYPTED)
    281   1.42    itojun 		goto freeit;
    282  1.162      maxv 	if (n->m_flags & (M_BCAST|M_MCAST))
    283  1.162      maxv 		goto freeit;
    284   1.70    itojun 	if (oip->ip_off &~ htons(IP_MF|IP_DF))
    285    1.1       cgd 		goto freeit;
    286    1.1       cgd 	if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
    287  1.162      maxv 	    n->m_len >= oiphlen + ICMP_MINLEN) {
    288  1.162      maxv 		struct icmp *oicp = (struct icmp *)((char *)oip + oiphlen);
    289  1.162      maxv 		if (!ICMP_INFOTYPE(oicp->icmp_type)) {
    290  1.162      maxv 			ICMP_STATINC(ICMP_STAT_OLDICMP);
    291  1.162      maxv 			goto freeit;
    292  1.162      maxv 		}
    293    1.1       cgd 	}
    294   1.40   thorpej 
    295    1.1       cgd 	/*
    296   1.40   thorpej 	 * First, do a rate limitation check.
    297   1.40   thorpej 	 */
    298   1.40   thorpej 	if (icmp_ratelimit(&oip->ip_src, type, code)) {
    299   1.40   thorpej 		/* XXX stat */
    300   1.40   thorpej 		goto freeit;
    301   1.40   thorpej 	}
    302   1.40   thorpej 
    303   1.40   thorpej 	/*
    304  1.162      maxv 	 * Compute the number of bytes we will put in 'icmp_ip'. Truncate
    305  1.162      maxv 	 * it to the size of the mbuf, if it's too big.
    306   1.52  sommerfe 	 */
    307  1.162      maxv 	datalen = oiphlen + min(icmpreturndatabytes,
    308  1.162      maxv 	    ntohs(oip->ip_len) - oiphlen);
    309   1.52  sommerfe 	mblen = 0;
    310  1.162      maxv 	for (m = n; m && (mblen < datalen); m = m->m_next)
    311   1.52  sommerfe 		mblen += m->m_len;
    312  1.162      maxv 	datalen = min(mblen, datalen);
    313   1.52  sommerfe 
    314   1.52  sommerfe 	/*
    315  1.162      maxv 	 * Compute the total length of the new packet. Truncate it if it's
    316  1.162      maxv 	 * bigger than the size of a cluster.
    317   1.52  sommerfe 	 */
    318  1.162      maxv 	CTASSERT(ICMP_MINLEN <= MCLBYTES);
    319  1.162      maxv 	if (datalen + ICMP_MINLEN > MCLBYTES)
    320  1.162      maxv 		datalen = MCLBYTES - ICMP_MINLEN;
    321  1.162      maxv 	totlen = datalen + ICMP_MINLEN;
    322   1.52  sommerfe 
    323  1.162      maxv 	/*
    324  1.162      maxv 	 * Allocate the mbuf for the new packet.
    325  1.162      maxv 	 */
    326    1.1       cgd 	m = m_gethdr(M_DONTWAIT, MT_HEADER);
    327  1.162      maxv 	if (m && (totlen > MHLEN)) {
    328   1.52  sommerfe 		MCLGET(m, M_DONTWAIT);
    329   1.52  sommerfe 		if ((m->m_flags & M_EXT) == 0) {
    330   1.52  sommerfe 			m_freem(m);
    331   1.52  sommerfe 			m = NULL;
    332   1.52  sommerfe 		}
    333   1.52  sommerfe 	}
    334    1.1       cgd 	if (m == NULL)
    335    1.1       cgd 		goto freeit;
    336   1.72      matt 	MCLAIM(m, n->m_owner);
    337  1.162      maxv 	m->m_len = totlen;
    338  1.162      maxv 
    339  1.162      maxv 	/*
    340  1.162      maxv 	 * Advance to the ICMP header.
    341  1.162      maxv 	 */
    342  1.162      maxv 	if ((m->m_flags & M_EXT) == 0) {
    343   1.52  sommerfe 		MH_ALIGN(m, m->m_len);
    344  1.162      maxv 	} else {
    345  1.124    kefren 		m->m_data += sizeof(struct ip);
    346  1.124    kefren 		m->m_len -= sizeof(struct ip);
    347  1.124    kefren 	}
    348  1.162      maxv 
    349    1.1       cgd 	if ((u_int)type > ICMP_MAXTYPE)
    350    1.1       cgd 		panic("icmp_error");
    351  1.116   thorpej 	ICMP_STATINC(ICMP_STAT_OUTHIST + type);
    352  1.162      maxv 
    353  1.162      maxv 	/*
    354  1.162      maxv 	 * Fill in the fields of the ICMP header: icmp_type, icmp_code
    355  1.162      maxv 	 * and icmp_ip. icmp_cksum gets filled later.
    356  1.162      maxv 	 */
    357  1.162      maxv 	icp = mtod(m, struct icmp *);
    358    1.1       cgd 	icp->icmp_type = type;
    359  1.162      maxv 	if (type == ICMP_REDIRECT) {
    360    1.9   mycroft 		icp->icmp_gwaddr.s_addr = dest;
    361  1.162      maxv 	} else {
    362    1.1       cgd 		icp->icmp_void = 0;
    363   1.67    itojun 		/*
    364    1.9   mycroft 		 * The following assignments assume an overlay with the
    365    1.9   mycroft 		 * zeroed icmp_void field.
    366    1.9   mycroft 		 */
    367    1.9   mycroft 		if (type == ICMP_PARAMPROB) {
    368    1.9   mycroft 			icp->icmp_pptr = code;
    369    1.9   mycroft 			code = 0;
    370    1.9   mycroft 		} else if (type == ICMP_UNREACH &&
    371   1.96  christos 		    code == ICMP_UNREACH_NEEDFRAG && destmtu)
    372   1.96  christos 			icp->icmp_nextmtu = htons(destmtu);
    373    1.1       cgd 	}
    374    1.1       cgd 	icp->icmp_code = code;
    375  1.162      maxv 	m_copydata(n, 0, datalen, (void *)&icp->icmp_ip);
    376    1.1       cgd 
    377    1.1       cgd 	/*
    378  1.162      maxv 	 * Come back to the IP header.
    379    1.1       cgd 	 */
    380  1.124    kefren 	if ((m->m_flags & M_EXT) == 0 &&
    381  1.124    kefren 	    m->m_data - sizeof(struct ip) < m->m_pktdat)
    382    1.1       cgd 		panic("icmp len");
    383    1.1       cgd 	m->m_data -= sizeof(struct ip);
    384    1.1       cgd 	m->m_len += sizeof(struct ip);
    385    1.1       cgd 	m->m_pkthdr.len = m->m_len;
    386  1.147     ozaki 	m_copy_rcvif(m, n);
    387  1.162      maxv 
    388  1.162      maxv 	/*
    389  1.162      maxv 	 * Now, copy the old IP header (without options) in front of the
    390  1.162      maxv 	 * ICMP message. The src/dst fields will be swapped in icmp_reflect.
    391  1.162      maxv 	 */
    392    1.1       cgd 	nip = mtod(m, struct ip *);
    393   1.47   darrenr 	/* ip_v set in ip_output */
    394   1.47   darrenr 	nip->ip_hl = sizeof(struct ip) >> 2;
    395   1.47   darrenr 	nip->ip_tos = 0;
    396   1.70    itojun 	nip->ip_len = htons(m->m_len);
    397   1.47   darrenr 	/* ip_id set in ip_output */
    398   1.70    itojun 	nip->ip_off = htons(0);
    399   1.47   darrenr 	/* ip_ttl set in icmp_reflect */
    400    1.1       cgd 	nip->ip_p = IPPROTO_ICMP;
    401   1.47   darrenr 	nip->ip_src = oip->ip_src;
    402   1.47   darrenr 	nip->ip_dst = oip->ip_dst;
    403  1.120      yamt 	/* move PF m_tag to new packet, if it exists */
    404  1.120      yamt 	mtag = m_tag_find(n, PACKET_TAG_PF, NULL);
    405  1.100     peter 	if (mtag != NULL) {
    406  1.100     peter 		m_tag_unlink(n, mtag);
    407  1.100     peter 		m_tag_prepend(m, mtag);
    408  1.100     peter 	}
    409  1.162      maxv 
    410    1.1       cgd 	icmp_reflect(m);
    411    1.1       cgd 
    412    1.1       cgd freeit:
    413    1.1       cgd 	m_freem(n);
    414    1.1       cgd }
    415    1.1       cgd 
    416  1.103  christos struct sockaddr_in icmpsrc = {
    417  1.103  christos 	.sin_len = sizeof (struct sockaddr_in),
    418  1.103  christos 	.sin_family = AF_INET,
    419  1.103  christos };
    420  1.103  christos static struct sockaddr_in icmpdst = {
    421  1.103  christos 	.sin_len = sizeof (struct sockaddr_in),
    422  1.103  christos 	.sin_family = AF_INET,
    423  1.103  christos };
    424  1.103  christos static struct sockaddr_in icmpgw = {
    425  1.103  christos 	.sin_len = sizeof (struct sockaddr_in),
    426  1.103  christos 	.sin_family = AF_INET,
    427  1.103  christos };
    428  1.103  christos struct sockaddr_in icmpmask = {
    429  1.103  christos 	.sin_len = 8,
    430  1.103  christos 	.sin_family = 0,
    431  1.103  christos };
    432    1.1       cgd 
    433    1.1       cgd /*
    434    1.1       cgd  * Process a received ICMP message.
    435    1.1       cgd  */
    436  1.156     ozaki static void
    437  1.156     ozaki _icmp_input(struct mbuf *m, int hlen, int proto)
    438    1.1       cgd {
    439   1.44  augustss 	struct icmp *icp;
    440   1.44  augustss 	struct ip *ip = mtod(m, struct ip *);
    441   1.34   mycroft 	int icmplen;
    442   1.44  augustss 	int i;
    443    1.1       cgd 	struct in_ifaddr *ia;
    444  1.109    dyoung 	void *(*ctlfunc)(int, const struct sockaddr *, void *);
    445    1.9   mycroft 	int code;
    446   1.63       kml 	struct rtentry *rt;
    447   1.19  christos 
    448    1.1       cgd 	/*
    449    1.1       cgd 	 * Locate icmp structure in mbuf, and check
    450    1.1       cgd 	 * that not corrupted and of at least minimum length.
    451    1.1       cgd 	 */
    452   1.70    itojun 	icmplen = ntohs(ip->ip_len) - hlen;
    453    1.1       cgd #ifdef ICMPPRINTFS
    454   1.95  christos 	if (icmpprintfs) {
    455  1.135  christos 		char sbuf[INET_ADDRSTRLEN], dbuf[INET_ADDRSTRLEN];
    456  1.135  christos 		printf("icmp_input from `%s' to `%s', len %d\n",
    457  1.135  christos 		    IN_PRINT(sbuf, &ip->ip_src), IN_PRINT(dbuf, &ip->ip_dst),
    458  1.135  christos 		    icmplen);
    459   1.95  christos 	}
    460    1.1       cgd #endif
    461    1.1       cgd 	if (icmplen < ICMP_MINLEN) {
    462  1.116   thorpej 		ICMP_STATINC(ICMP_STAT_TOOSHORT);
    463    1.1       cgd 		goto freeit;
    464    1.1       cgd 	}
    465    1.9   mycroft 	i = hlen + min(icmplen, ICMP_ADVLENMIN);
    466  1.128  liamjfoy 	if ((m->m_len < i || M_READONLY(m)) && (m = m_pullup(m, i)) == NULL) {
    467  1.116   thorpej 		ICMP_STATINC(ICMP_STAT_TOOSHORT);
    468    1.1       cgd 		return;
    469    1.1       cgd 	}
    470    1.5   mycroft 	ip = mtod(m, struct ip *);
    471    1.1       cgd 	m->m_len -= hlen;
    472    1.1       cgd 	m->m_data += hlen;
    473    1.1       cgd 	icp = mtod(m, struct icmp *);
    474   1.69   thorpej 	/* Don't need to assert alignment, here. */
    475    1.1       cgd 	if (in_cksum(m, icmplen)) {
    476  1.116   thorpej 		ICMP_STATINC(ICMP_STAT_CHECKSUM);
    477    1.1       cgd 		goto freeit;
    478    1.1       cgd 	}
    479    1.1       cgd 	m->m_len += hlen;
    480    1.1       cgd 	m->m_data -= hlen;
    481    1.1       cgd 
    482    1.1       cgd #ifdef ICMPPRINTFS
    483    1.1       cgd 	/*
    484    1.1       cgd 	 * Message type specific processing.
    485    1.1       cgd 	 */
    486    1.1       cgd 	if (icmpprintfs)
    487   1.95  christos 		printf("icmp_input(type:%d, code:%d)\n", icp->icmp_type,
    488    1.1       cgd 		    icp->icmp_code);
    489   1.37    itojun #endif
    490    1.1       cgd 	if (icp->icmp_type > ICMP_MAXTYPE)
    491    1.1       cgd 		goto raw;
    492  1.116   thorpej 	ICMP_STATINC(ICMP_STAT_INHIST + icp->icmp_type);
    493    1.1       cgd 	code = icp->icmp_code;
    494    1.1       cgd 	switch (icp->icmp_type) {
    495    1.1       cgd 
    496    1.1       cgd 	case ICMP_UNREACH:
    497    1.9   mycroft 		switch (code) {
    498  1.130  christos 		case ICMP_UNREACH_PROTOCOL:
    499  1.130  christos 			code = PRC_UNREACH_PROTOCOL;
    500  1.130  christos 			break;
    501  1.102      yamt 
    502  1.130  christos 		case ICMP_UNREACH_PORT:
    503  1.130  christos 			code = PRC_UNREACH_PORT;
    504  1.130  christos 			break;
    505  1.102      yamt 
    506  1.130  christos 		case ICMP_UNREACH_SRCFAIL:
    507  1.130  christos 			code = PRC_UNREACH_SRCFAIL;
    508  1.130  christos 			break;
    509  1.102      yamt 
    510  1.130  christos 		case ICMP_UNREACH_NEEDFRAG:
    511  1.130  christos 			code = PRC_MSGSIZE;
    512  1.130  christos 			break;
    513  1.102      yamt 
    514  1.130  christos 		case ICMP_UNREACH_NET:
    515  1.130  christos 		case ICMP_UNREACH_NET_UNKNOWN:
    516  1.130  christos 		case ICMP_UNREACH_NET_PROHIB:
    517  1.130  christos 		case ICMP_UNREACH_TOSNET:
    518  1.130  christos 			code = PRC_UNREACH_NET;
    519  1.130  christos 			break;
    520    1.9   mycroft 
    521  1.130  christos 		case ICMP_UNREACH_HOST:
    522  1.130  christos 		case ICMP_UNREACH_HOST_UNKNOWN:
    523  1.130  christos 		case ICMP_UNREACH_ISOLATED:
    524  1.130  christos 		case ICMP_UNREACH_HOST_PROHIB:
    525  1.130  christos 		case ICMP_UNREACH_TOSHOST:
    526  1.130  christos 		case ICMP_UNREACH_ADMIN_PROHIBIT:
    527  1.130  christos 		case ICMP_UNREACH_HOST_PREC:
    528  1.130  christos 		case ICMP_UNREACH_PREC_CUTOFF:
    529  1.130  christos 			code = PRC_UNREACH_HOST;
    530  1.130  christos 			break;
    531   1.67    itojun 
    532  1.130  christos 		default:
    533  1.130  christos 			goto badcode;
    534    1.9   mycroft 		}
    535    1.1       cgd 		goto deliver;
    536    1.1       cgd 
    537    1.1       cgd 	case ICMP_TIMXCEED:
    538    1.1       cgd 		if (code > 1)
    539    1.1       cgd 			goto badcode;
    540    1.1       cgd 		code += PRC_TIMXCEED_INTRANS;
    541    1.1       cgd 		goto deliver;
    542    1.1       cgd 
    543    1.1       cgd 	case ICMP_PARAMPROB:
    544    1.9   mycroft 		if (code > 1)
    545    1.1       cgd 			goto badcode;
    546    1.1       cgd 		code = PRC_PARAMPROB;
    547    1.1       cgd 		goto deliver;
    548    1.1       cgd 
    549    1.1       cgd 	case ICMP_SOURCEQUENCH:
    550    1.1       cgd 		if (code)
    551    1.1       cgd 			goto badcode;
    552    1.1       cgd 		code = PRC_QUENCH;
    553   1.20   mycroft 		goto deliver;
    554   1.20   mycroft 
    555    1.1       cgd 	deliver:
    556    1.1       cgd 		/*
    557    1.1       cgd 		 * Problem with datagram; advise higher level routines.
    558    1.1       cgd 		 */
    559    1.1       cgd 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
    560    1.1       cgd 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
    561  1.116   thorpej 			ICMP_STATINC(ICMP_STAT_BADLEN);
    562    1.1       cgd 			goto freeit;
    563    1.1       cgd 		}
    564   1.14   mycroft 		if (IN_MULTICAST(icp->icmp_ip.ip_dst.s_addr))
    565   1.13   mycroft 			goto badcode;
    566    1.1       cgd #ifdef ICMPPRINTFS
    567    1.1       cgd 		if (icmpprintfs)
    568   1.22  christos 			printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
    569    1.1       cgd #endif
    570    1.1       cgd 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
    571  1.144  riastrad 		ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
    572   1.19  christos 		if (ctlfunc)
    573   1.55   thorpej 			(void) (*ctlfunc)(code, sintosa(&icmpsrc),
    574   1.55   thorpej 			    &icp->icmp_ip);
    575    1.1       cgd 		break;
    576    1.1       cgd 
    577    1.1       cgd 	badcode:
    578  1.116   thorpej 		ICMP_STATINC(ICMP_STAT_BADCODE);
    579    1.1       cgd 		break;
    580    1.1       cgd 
    581    1.1       cgd 	case ICMP_ECHO:
    582  1.122  christos 		if (!icmpbmcastecho &&
    583  1.122  christos 		    (m->m_flags & (M_MCAST | M_BCAST)) != 0)  {
    584  1.122  christos 			ICMP_STATINC(ICMP_STAT_BMCASTECHO);
    585  1.122  christos 			break;
    586  1.122  christos 		}
    587    1.1       cgd 		icp->icmp_type = ICMP_ECHOREPLY;
    588    1.1       cgd 		goto reflect;
    589    1.1       cgd 
    590    1.1       cgd 	case ICMP_TSTAMP:
    591    1.1       cgd 		if (icmplen < ICMP_TSLEN) {
    592  1.116   thorpej 			ICMP_STATINC(ICMP_STAT_BADLEN);
    593    1.1       cgd 			break;
    594    1.1       cgd 		}
    595  1.122  christos 		if (!icmpbmcastecho &&
    596  1.122  christos 		    (m->m_flags & (M_MCAST | M_BCAST)) != 0)  {
    597  1.122  christos 			ICMP_STATINC(ICMP_STAT_BMCASTTSTAMP);
    598  1.122  christos 			break;
    599  1.122  christos 		}
    600    1.1       cgd 		icp->icmp_type = ICMP_TSTAMPREPLY;
    601    1.1       cgd 		icp->icmp_rtime = iptime();
    602    1.1       cgd 		icp->icmp_ttime = icp->icmp_rtime;	/* bogus, do later! */
    603    1.1       cgd 		goto reflect;
    604   1.67    itojun 
    605  1.147     ozaki 	case ICMP_MASKREQ: {
    606  1.147     ozaki 		struct ifnet *rcvif;
    607  1.151     ozaki 		int s, ss;
    608  1.157     ozaki 		struct ifaddr *ifa = NULL;
    609  1.147     ozaki 
    610    1.9   mycroft 		if (icmpmaskrepl == 0)
    611    1.9   mycroft 			break;
    612    1.9   mycroft 		/*
    613    1.9   mycroft 		 * We are not able to respond with all ones broadcast
    614    1.9   mycroft 		 * unless we receive it over a point-to-point interface.
    615    1.9   mycroft 		 */
    616   1.23   thorpej 		if (icmplen < ICMP_MASKLEN) {
    617  1.116   thorpej 			ICMP_STATINC(ICMP_STAT_BADLEN);
    618    1.9   mycroft 			break;
    619   1.23   thorpej 		}
    620   1.15   mycroft 		if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
    621   1.20   mycroft 		    in_nullhost(ip->ip_dst))
    622    1.9   mycroft 			icmpdst.sin_addr = ip->ip_src;
    623   1.15   mycroft 		else
    624    1.9   mycroft 			icmpdst.sin_addr = ip->ip_dst;
    625  1.151     ozaki 		ss = pserialize_read_enter();
    626  1.147     ozaki 		rcvif = m_get_rcvif(m, &s);
    627  1.157     ozaki 		if (__predict_true(rcvif != NULL))
    628  1.157     ozaki 			ifa = ifaof_ifpforaddr(sintosa(&icmpdst), rcvif);
    629  1.147     ozaki 		m_put_rcvif(rcvif, &s);
    630  1.151     ozaki 		if (ifa == NULL) {
    631  1.151     ozaki 			pserialize_read_exit(ss);
    632    1.1       cgd 			break;
    633  1.151     ozaki 		}
    634  1.151     ozaki 		ia = ifatoia(ifa);
    635    1.1       cgd 		icp->icmp_type = ICMP_MASKREPLY;
    636    1.1       cgd 		icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
    637   1.20   mycroft 		if (in_nullhost(ip->ip_src)) {
    638    1.1       cgd 			if (ia->ia_ifp->if_flags & IFF_BROADCAST)
    639   1.17   mycroft 				ip->ip_src = ia->ia_broadaddr.sin_addr;
    640    1.1       cgd 			else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
    641   1.17   mycroft 				ip->ip_src = ia->ia_dstaddr.sin_addr;
    642    1.1       cgd 		}
    643  1.151     ozaki 		pserialize_read_exit(ss);
    644    1.1       cgd reflect:
    645  1.116   thorpej 		{
    646  1.116   thorpej 			uint64_t *icps = percpu_getref(icmpstat_percpu);
    647  1.116   thorpej 			icps[ICMP_STAT_REFLECT]++;
    648  1.116   thorpej 			icps[ICMP_STAT_OUTHIST + icp->icmp_type]++;
    649  1.116   thorpej 			percpu_putref(icmpstat_percpu);
    650  1.116   thorpej 		}
    651    1.1       cgd 		icmp_reflect(m);
    652    1.1       cgd 		return;
    653  1.147     ozaki 	}
    654    1.1       cgd 
    655    1.1       cgd 	case ICMP_REDIRECT:
    656    1.9   mycroft 		if (code > 3)
    657    1.9   mycroft 			goto badcode;
    658   1.63       kml 		if (icmp_rediraccept == 0)
    659   1.63       kml 			goto freeit;
    660    1.9   mycroft 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
    661    1.9   mycroft 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
    662  1.116   thorpej 			ICMP_STATINC(ICMP_STAT_BADLEN);
    663    1.1       cgd 			break;
    664    1.1       cgd 		}
    665    1.1       cgd 		/*
    666    1.1       cgd 		 * Short circuit routing redirects to force
    667    1.1       cgd 		 * immediate change in the kernel's routing
    668    1.1       cgd 		 * tables.  The message is also handed to anyone
    669    1.1       cgd 		 * listening on a raw socket (e.g. the routing
    670    1.1       cgd 		 * daemon for use in updating its tables).
    671    1.1       cgd 		 */
    672    1.1       cgd 		icmpgw.sin_addr = ip->ip_src;
    673    1.1       cgd 		icmpdst.sin_addr = icp->icmp_gwaddr;
    674    1.1       cgd #ifdef	ICMPPRINTFS
    675   1.95  christos 		if (icmpprintfs) {
    676  1.135  christos 			char gbuf[INET_ADDRSTRLEN], dbuf[INET_ADDRSTRLEN];
    677  1.101     peter 			printf("redirect dst `%s' to `%s'\n",
    678  1.135  christos 			    IN_PRINT(dbuf, &icp->icmp_ip.ip_dst),
    679  1.135  christos 			    IN_PRINT(gbuf, &icp->icmp_gwaddr));
    680   1.95  christos 		}
    681    1.1       cgd #endif
    682    1.9   mycroft 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
    683   1.63       kml 		rt = NULL;
    684   1.16   mycroft 		rtredirect(sintosa(&icmpsrc), sintosa(&icmpdst),
    685  1.113    dyoung 		    NULL, RTF_GATEWAY | RTF_HOST, sintosa(&icmpgw), &rt);
    686  1.158     ozaki 		mutex_enter(&icmp_mtx);
    687   1.63       kml 		if (rt != NULL && icmp_redirtimeout != 0) {
    688   1.67    itojun 			i = rt_timer_add(rt, icmp_redirect_timeout,
    689   1.63       kml 					 icmp_redirect_timeout_q);
    690  1.135  christos 			if (i) {
    691  1.135  christos 				char buf[INET_ADDRSTRLEN];
    692   1.63       kml 				log(LOG_ERR, "ICMP:  redirect failed to "
    693  1.135  christos 				    "register timeout for route to %s, "
    694   1.67    itojun 				    "code %d\n",
    695  1.135  christos 				    IN_PRINT(buf, &icp->icmp_ip.ip_dst), i);
    696  1.135  christos 			}
    697   1.63       kml 		}
    698  1.158     ozaki 		mutex_exit(&icmp_mtx);
    699   1.63       kml 		if (rt != NULL)
    700  1.154     ozaki 			rt_unref(rt);
    701   1.63       kml 
    702   1.16   mycroft 		pfctlinput(PRC_REDIRECT_HOST, sintosa(&icmpsrc));
    703  1.131  christos #if defined(IPSEC)
    704  1.134  christos 		if (ipsec_used)
    705  1.134  christos 			key_sa_routechange((struct sockaddr *)&icmpsrc);
    706   1.37    itojun #endif
    707    1.1       cgd 		break;
    708    1.1       cgd 
    709    1.1       cgd 	/*
    710    1.1       cgd 	 * No kernel processing for the following;
    711    1.1       cgd 	 * just fall through to send to raw listener.
    712    1.1       cgd 	 */
    713    1.1       cgd 	case ICMP_ECHOREPLY:
    714    1.9   mycroft 	case ICMP_ROUTERADVERT:
    715    1.9   mycroft 	case ICMP_ROUTERSOLICIT:
    716    1.1       cgd 	case ICMP_TSTAMPREPLY:
    717    1.1       cgd 	case ICMP_IREQREPLY:
    718    1.1       cgd 	case ICMP_MASKREPLY:
    719    1.1       cgd 	default:
    720    1.1       cgd 		break;
    721    1.1       cgd 	}
    722    1.1       cgd 
    723    1.1       cgd raw:
    724   1.37    itojun 	rip_input(m, hlen, proto);
    725    1.1       cgd 	return;
    726    1.1       cgd 
    727    1.1       cgd freeit:
    728    1.1       cgd 	m_freem(m);
    729   1.37    itojun 	return;
    730    1.1       cgd }
    731    1.1       cgd 
    732  1.156     ozaki void
    733  1.156     ozaki icmp_input(struct mbuf *m, ...)
    734  1.156     ozaki {
    735  1.156     ozaki 	int hlen, proto;
    736  1.156     ozaki 	va_list ap;
    737  1.156     ozaki 
    738  1.156     ozaki 	va_start(ap, m);
    739  1.156     ozaki 	hlen = va_arg(ap, int);
    740  1.156     ozaki 	proto = va_arg(ap, int);
    741  1.156     ozaki 	va_end(ap);
    742  1.156     ozaki 
    743  1.156     ozaki 	wqinput_input(icmp_wqinput, m, hlen, proto);
    744  1.156     ozaki }
    745  1.156     ozaki 
    746    1.1       cgd /*
    747    1.1       cgd  * Reflect the ip packet back to the source
    748    1.1       cgd  */
    749    1.6   mycroft void
    750   1.90     perry icmp_reflect(struct mbuf *m)
    751    1.1       cgd {
    752   1.44  augustss 	struct ip *ip = mtod(m, struct ip *);
    753   1.44  augustss 	struct in_ifaddr *ia;
    754   1.44  augustss 	struct ifaddr *ifa;
    755  1.139  christos 	struct sockaddr_in *sin;
    756    1.1       cgd 	struct in_addr t;
    757  1.136     ozaki 	struct mbuf *opts = NULL;
    758    1.1       cgd 	int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
    759  1.147     ozaki 	struct ifnet *rcvif;
    760  1.151     ozaki 	struct psref psref, psref_ia;
    761  1.151     ozaki 	int s;
    762  1.151     ozaki 	int bound;
    763  1.151     ozaki 
    764  1.151     ozaki 	bound = curlwp_bind();
    765    1.1       cgd 
    766    1.9   mycroft 	if (!in_canforward(ip->ip_src) &&
    767   1.14   mycroft 	    ((ip->ip_src.s_addr & IN_CLASSA_NET) !=
    768   1.14   mycroft 	     htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
    769    1.9   mycroft 		m_freem(m);	/* Bad return address */
    770   1.12       cgd 		goto done;	/* ip_output() will check for broadcast */
    771    1.9   mycroft 	}
    772    1.1       cgd 	t = ip->ip_dst;
    773    1.1       cgd 	ip->ip_dst = ip->ip_src;
    774    1.1       cgd 	/*
    775   1.39  sommerfe 	 * If the incoming packet was addressed directly to us, use
    776   1.39  sommerfe 	 * dst as the src for the reply.  Otherwise (broadcast or
    777   1.39  sommerfe 	 * anonymous), use an address which corresponds to the
    778   1.39  sommerfe 	 * incoming interface, with a preference for the address which
    779   1.39  sommerfe 	 * corresponds to the route to the destination of the ICMP.
    780    1.1       cgd 	 */
    781   1.39  sommerfe 
    782   1.39  sommerfe 	/* Look for packet addressed to us */
    783  1.151     ozaki 	ia = in_get_ia_psref(t, &psref_ia);
    784  1.151     ozaki 	if (ia && (ia->ia4_flags & IN_IFF_NOTREADY)) {
    785  1.151     ozaki 		ia4_release(ia, &psref_ia);
    786  1.138       roy 		ia = NULL;
    787  1.151     ozaki 	}
    788   1.39  sommerfe 
    789  1.147     ozaki 	rcvif = m_get_rcvif_psref(m, &psref);
    790  1.147     ozaki 
    791   1.39  sommerfe 	/* look for packet sent to broadcast address */
    792  1.147     ozaki 	if (ia == NULL && rcvif &&
    793  1.147     ozaki 	    (rcvif->if_flags & IFF_BROADCAST)) {
    794  1.151     ozaki 		s = pserialize_read_enter();
    795  1.149     ozaki 		IFADDR_READER_FOREACH(ifa, rcvif) {
    796   1.27       tls 			if (ifa->ifa_addr->sa_family != AF_INET)
    797   1.27       tls 				continue;
    798   1.39  sommerfe 			if (in_hosteq(t,ifatoia(ifa)->ia_broadaddr.sin_addr)) {
    799   1.39  sommerfe 				ia = ifatoia(ifa);
    800  1.138       roy 				if ((ia->ia4_flags & IN_IFF_NOTREADY) == 0)
    801  1.138       roy 					break;
    802  1.138       roy 				ia = NULL;
    803   1.39  sommerfe 			}
    804   1.27       tls 		}
    805  1.151     ozaki 		if (ia != NULL)
    806  1.151     ozaki 			ia4_acquire(ia, &psref_ia);
    807  1.151     ozaki 		pserialize_read_exit(s);
    808    1.1       cgd 	}
    809   1.27       tls 
    810  1.139  christos 	sin = ia ? &ia->ia_addr : NULL;
    811   1.39  sommerfe 
    812    1.9   mycroft 	icmpdst.sin_addr = t;
    813   1.39  sommerfe 
    814   1.70    itojun 	/*
    815   1.70    itojun 	 * if the packet is addressed somewhere else, compute the
    816   1.70    itojun 	 * source address for packets routed back to the source, and
    817   1.70    itojun 	 * use that, if it's an address on the interface which
    818   1.70    itojun 	 * received the packet
    819   1.70    itojun 	 */
    820  1.147     ozaki 	if (sin == NULL && rcvif) {
    821   1.39  sommerfe 		struct sockaddr_in sin_dst;
    822   1.39  sommerfe 		struct route icmproute;
    823   1.39  sommerfe 		int errornum;
    824   1.39  sommerfe 
    825  1.114    dyoung 		sockaddr_in_init(&sin_dst, &ip->ip_dst, 0);
    826  1.108    dyoung 		memset(&icmproute, 0, sizeof(icmproute));
    827   1.39  sommerfe 		errornum = 0;
    828  1.151     ozaki 		ia = in_selectsrc(&sin_dst, &icmproute, 0, NULL, &errornum,
    829  1.151     ozaki 		    &psref_ia);
    830   1.39  sommerfe 		/* errornum is never used */
    831  1.107     joerg 		rtcache_free(&icmproute);
    832   1.39  sommerfe 		/* check to make sure sin is a source address on rcvif */
    833  1.151     ozaki 		if (ia != NULL) {
    834  1.151     ozaki 			sin = &ia->ia_addr;
    835   1.39  sommerfe 			t = sin->sin_addr;
    836  1.114    dyoung 			sin = NULL;
    837  1.151     ozaki 			ia4_release(ia, &psref_ia);
    838  1.151     ozaki 			ia = in_get_ia_on_iface_psref(t, rcvif, &psref_ia);
    839  1.150     ozaki 			if (ia != NULL)
    840  1.150     ozaki 				sin = &ia->ia_addr;
    841   1.39  sommerfe 		}
    842   1.39  sommerfe 	}
    843   1.39  sommerfe 
    844   1.70    itojun 	/*
    845   1.70    itojun 	 * if it was not addressed to us, but the route doesn't go out
    846   1.70    itojun 	 * the source interface, pick an address on the source
    847   1.70    itojun 	 * interface.  This can happen when routing is asymmetric, or
    848   1.70    itojun 	 * when the incoming packet was encapsulated
    849   1.70    itojun 	 */
    850  1.147     ozaki 	if (sin == NULL && rcvif) {
    851  1.151     ozaki 		KASSERT(ia == NULL);
    852  1.151     ozaki 		s = pserialize_read_enter();
    853  1.149     ozaki 		IFADDR_READER_FOREACH(ifa, rcvif) {
    854   1.39  sommerfe 			if (ifa->ifa_addr->sa_family != AF_INET)
    855   1.39  sommerfe 				continue;
    856   1.39  sommerfe 			sin = &(ifatoia(ifa)->ia_addr);
    857  1.152     ozaki 			ia = ifatoia(ifa);
    858  1.152     ozaki 			ia4_acquire(ia, &psref_ia);
    859   1.39  sommerfe 			break;
    860   1.39  sommerfe 		}
    861  1.151     ozaki 		pserialize_read_exit(s);
    862   1.39  sommerfe 	}
    863   1.39  sommerfe 
    864  1.147     ozaki 	m_put_rcvif_psref(rcvif, &psref);
    865  1.147     ozaki 
    866    1.9   mycroft 	/*
    867    1.9   mycroft 	 * The following happens if the packet was not addressed to us,
    868   1.27       tls 	 * and was received on an interface with no IP address:
    869   1.27       tls 	 * We find the first AF_INET address on the first non-loopback
    870   1.27       tls 	 * interface.
    871    1.9   mycroft 	 */
    872  1.151     ozaki 	if (sin == NULL) {
    873  1.151     ozaki 		KASSERT(ia == NULL);
    874  1.151     ozaki 		s = pserialize_read_enter();
    875  1.148     ozaki 		IN_ADDRLIST_READER_FOREACH(ia) {
    876   1.27       tls 			if (ia->ia_ifp->if_flags & IFF_LOOPBACK)
    877   1.27       tls 				continue;
    878   1.39  sommerfe 			sin = &ia->ia_addr;
    879  1.151     ozaki 			ia4_acquire(ia, &psref_ia);
    880   1.27       tls 			break;
    881   1.27       tls 		}
    882  1.151     ozaki 		pserialize_read_exit(s);
    883  1.151     ozaki 	}
    884   1.39  sommerfe 
    885   1.36   mycroft 	/*
    886   1.36   mycroft 	 * If we still didn't find an address, punt.  We could have an
    887   1.36   mycroft 	 * interface up (and receiving packets) with no address.
    888   1.36   mycroft 	 */
    889  1.114    dyoung 	if (sin == NULL) {
    890  1.151     ozaki 		KASSERT(ia == NULL);
    891   1.36   mycroft 		m_freem(m);
    892   1.36   mycroft 		goto done;
    893   1.36   mycroft 	}
    894   1.36   mycroft 
    895   1.39  sommerfe 	ip->ip_src = sin->sin_addr;
    896    1.1       cgd 	ip->ip_ttl = MAXTTL;
    897    1.1       cgd 
    898  1.151     ozaki 	if (ia != NULL)
    899  1.151     ozaki 		ia4_release(ia, &psref_ia);
    900  1.151     ozaki 
    901    1.1       cgd 	if (optlen > 0) {
    902   1.44  augustss 		u_char *cp;
    903    1.1       cgd 		int opt, cnt;
    904    1.1       cgd 		u_int len;
    905    1.1       cgd 
    906    1.1       cgd 		/*
    907    1.1       cgd 		 * Retrieve any source routing from the incoming packet;
    908    1.1       cgd 		 * add on any record-route or timestamp options.
    909    1.1       cgd 		 */
    910    1.1       cgd 		cp = (u_char *) (ip + 1);
    911  1.161     ozaki 		if ((opts = ip_srcroute(m)) == NULL &&
    912    1.1       cgd 		    (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
    913   1.72      matt 			MCLAIM(opts, m->m_owner);
    914    1.1       cgd 			opts->m_len = sizeof(struct in_addr);
    915   1.20   mycroft 			*mtod(opts, struct in_addr *) = zeroin_addr;
    916    1.1       cgd 		}
    917    1.1       cgd 		if (opts) {
    918    1.1       cgd #ifdef ICMPPRINTFS
    919    1.1       cgd 		    if (icmpprintfs)
    920   1.22  christos 			    printf("icmp_reflect optlen %d rt %d => ",
    921    1.1       cgd 				optlen, opts->m_len);
    922    1.1       cgd #endif
    923    1.1       cgd 		    for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
    924    1.1       cgd 			    opt = cp[IPOPT_OPTVAL];
    925    1.1       cgd 			    if (opt == IPOPT_EOL)
    926    1.1       cgd 				    break;
    927    1.1       cgd 			    if (opt == IPOPT_NOP)
    928    1.1       cgd 				    len = 1;
    929    1.1       cgd 			    else {
    930   1.45    itojun 				    if (cnt < IPOPT_OLEN + sizeof(*cp))
    931   1.45    itojun 					    break;
    932    1.1       cgd 				    len = cp[IPOPT_OLEN];
    933   1.45    itojun 				    if (len < IPOPT_OLEN + sizeof(*cp) ||
    934   1.45    itojun 				        len > cnt)
    935    1.1       cgd 					    break;
    936    1.1       cgd 			    }
    937    1.1       cgd 			    /*
    938    1.9   mycroft 			     * Should check for overflow, but it "can't happen"
    939    1.1       cgd 			     */
    940   1.67    itojun 			    if (opt == IPOPT_RR || opt == IPOPT_TS ||
    941    1.9   mycroft 				opt == IPOPT_SECURITY) {
    942  1.111  christos 				    memmove(mtod(opts, char *) + opts->m_len,
    943  1.111  christos 					cp, len);
    944    1.1       cgd 				    opts->m_len += len;
    945    1.1       cgd 			    }
    946    1.1       cgd 		    }
    947    1.9   mycroft 		    /* Terminate & pad, if necessary */
    948   1.19  christos 		    if ((cnt = opts->m_len % 4) != 0) {
    949    1.9   mycroft 			    for (; cnt < 4; cnt++) {
    950  1.111  christos 				    *(mtod(opts, char *) + opts->m_len) =
    951    1.9   mycroft 					IPOPT_EOL;
    952    1.9   mycroft 				    opts->m_len++;
    953    1.9   mycroft 			    }
    954    1.1       cgd 		    }
    955    1.1       cgd #ifdef ICMPPRINTFS
    956    1.1       cgd 		    if (icmpprintfs)
    957   1.22  christos 			    printf("%d\n", opts->m_len);
    958    1.1       cgd #endif
    959    1.1       cgd 		}
    960    1.1       cgd 		/*
    961    1.1       cgd 		 * Now strip out original options by copying rest of first
    962    1.1       cgd 		 * mbuf's data back, and adjust the IP length.
    963    1.1       cgd 		 */
    964   1.70    itojun 		ip->ip_len = htons(ntohs(ip->ip_len) - optlen);
    965    1.1       cgd 		ip->ip_hl = sizeof(struct ip) >> 2;
    966    1.1       cgd 		m->m_len -= optlen;
    967    1.1       cgd 		if (m->m_flags & M_PKTHDR)
    968    1.1       cgd 			m->m_pkthdr.len -= optlen;
    969    1.1       cgd 		optlen += sizeof(struct ip);
    970  1.111  christos 		memmove(ip + 1, (char *)ip + optlen,
    971  1.111  christos 		    (unsigned)(m->m_len - sizeof(struct ip)));
    972    1.1       cgd 	}
    973   1.80  jonathan 	m_tag_delete_nonpersistent(m);
    974    1.3   hpeyerl 	m->m_flags &= ~(M_BCAST|M_MCAST);
    975   1.73      tron 
    976   1.91     perry 	/*
    977   1.73      tron 	 * Clear any in-bound checksum flags for this packet.
    978   1.73      tron 	 */
    979   1.86    itojun 	if (m->m_flags & M_PKTHDR)
    980   1.86    itojun 		m->m_pkthdr.csum_flags = 0;
    981   1.73      tron 
    982    1.1       cgd 	icmp_send(m, opts);
    983    1.9   mycroft done:
    984  1.151     ozaki 	curlwp_bindx(bound);
    985    1.1       cgd 	if (opts)
    986    1.1       cgd 		(void)m_free(opts);
    987    1.1       cgd }
    988    1.1       cgd 
    989    1.1       cgd /*
    990    1.1       cgd  * Send an icmp packet back to the ip level,
    991    1.1       cgd  * after supplying a checksum.
    992    1.1       cgd  */
    993    1.6   mycroft void
    994   1.90     perry icmp_send(struct mbuf *m, struct mbuf *opts)
    995    1.1       cgd {
    996   1.44  augustss 	struct ip *ip = mtod(m, struct ip *);
    997   1.44  augustss 	int hlen;
    998   1.44  augustss 	struct icmp *icp;
    999    1.1       cgd 
   1000    1.1       cgd 	hlen = ip->ip_hl << 2;
   1001    1.1       cgd 	m->m_data += hlen;
   1002    1.1       cgd 	m->m_len -= hlen;
   1003    1.1       cgd 	icp = mtod(m, struct icmp *);
   1004    1.1       cgd 	icp->icmp_cksum = 0;
   1005   1.70    itojun 	icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen);
   1006    1.1       cgd 	m->m_data -= hlen;
   1007    1.1       cgd 	m->m_len += hlen;
   1008    1.1       cgd #ifdef ICMPPRINTFS
   1009   1.95  christos 	if (icmpprintfs) {
   1010  1.135  christos 		char sbuf[INET_ADDRSTRLEN], dbuf[INET_ADDRSTRLEN];
   1011  1.101     peter 		printf("icmp_send to destination `%s' from `%s'\n",
   1012  1.135  christos 		    IN_PRINT(dbuf, &ip->ip_dst), IN_PRINT(sbuf, &ip->ip_src));
   1013   1.95  christos 	}
   1014   1.43    itojun #endif
   1015  1.114    dyoung 	(void)ip_output(m, opts, NULL, 0, NULL, NULL);
   1016    1.1       cgd }
   1017    1.1       cgd 
   1018    1.1       cgd n_time
   1019   1.90     perry iptime(void)
   1020    1.1       cgd {
   1021    1.1       cgd 	struct timeval atv;
   1022    1.1       cgd 	u_long t;
   1023    1.1       cgd 
   1024    1.1       cgd 	microtime(&atv);
   1025    1.1       cgd 	t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
   1026    1.1       cgd 	return (htonl(t));
   1027    1.9   mycroft }
   1028    1.9   mycroft 
   1029   1.81    atatat /*
   1030   1.81    atatat  * sysctl helper routine for net.inet.icmp.returndatabytes.  ensures
   1031   1.81    atatat  * that the new value is in the correct range.
   1032   1.81    atatat  */
   1033   1.81    atatat static int
   1034   1.81    atatat sysctl_net_inet_icmp_returndatabytes(SYSCTLFN_ARGS)
   1035   1.81    atatat {
   1036   1.81    atatat 	int error, t;
   1037   1.81    atatat 	struct sysctlnode node;
   1038   1.81    atatat 
   1039   1.81    atatat 	node = *rnode;
   1040   1.81    atatat 	node.sysctl_data = &t;
   1041   1.81    atatat 	t = icmpreturndatabytes;
   1042   1.81    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1043   1.81    atatat 	if (error || newp == NULL)
   1044   1.81    atatat 		return (error);
   1045   1.81    atatat 
   1046   1.81    atatat 	if (t < 8 || t > 512)
   1047   1.81    atatat 		return (EINVAL);
   1048   1.81    atatat 	icmpreturndatabytes = t;
   1049   1.81    atatat 
   1050   1.81    atatat 	return (0);
   1051   1.81    atatat }
   1052   1.81    atatat 
   1053   1.81    atatat /*
   1054   1.81    atatat  * sysctl helper routine for net.inet.icmp.redirtimeout.  ensures that
   1055   1.81    atatat  * the given value is not less than zero and then resets the timeout
   1056   1.81    atatat  * queue.
   1057   1.81    atatat  */
   1058   1.81    atatat static int
   1059   1.81    atatat sysctl_net_inet_icmp_redirtimeout(SYSCTLFN_ARGS)
   1060    1.9   mycroft {
   1061   1.81    atatat 	int error, tmp;
   1062   1.81    atatat 	struct sysctlnode node;
   1063    1.9   mycroft 
   1064  1.158     ozaki 	mutex_enter(&icmp_mtx);
   1065  1.158     ozaki 
   1066   1.81    atatat 	node = *rnode;
   1067   1.81    atatat 	node.sysctl_data = &tmp;
   1068   1.81    atatat 	tmp = icmp_redirtimeout;
   1069   1.81    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1070   1.81    atatat 	if (error || newp == NULL)
   1071  1.158     ozaki 		goto out;
   1072  1.158     ozaki 	if (tmp < 0) {
   1073  1.158     ozaki 		error = EINVAL;
   1074  1.158     ozaki 		goto out;
   1075  1.158     ozaki 	}
   1076   1.81    atatat 	icmp_redirtimeout = tmp;
   1077   1.81    atatat 
   1078   1.81    atatat 	/*
   1079   1.81    atatat 	 * was it a *defined* side-effect that anyone even *reading*
   1080   1.81    atatat 	 * this value causes these things to happen?
   1081   1.81    atatat 	 */
   1082   1.81    atatat 	if (icmp_redirect_timeout_q != NULL) {
   1083   1.81    atatat 		if (icmp_redirtimeout == 0) {
   1084  1.153     ozaki 			rt_timer_queue_destroy(icmp_redirect_timeout_q);
   1085   1.81    atatat 			icmp_redirect_timeout_q = NULL;
   1086   1.81    atatat 		} else {
   1087   1.81    atatat 			rt_timer_queue_change(icmp_redirect_timeout_q,
   1088   1.81    atatat 			    icmp_redirtimeout);
   1089   1.63       kml 		}
   1090   1.81    atatat 	} else if (icmp_redirtimeout > 0) {
   1091   1.81    atatat 		icmp_redirect_timeout_q =
   1092   1.81    atatat 		    rt_timer_queue_create(icmp_redirtimeout);
   1093    1.9   mycroft 	}
   1094  1.158     ozaki 	error = 0;
   1095  1.158     ozaki out:
   1096  1.158     ozaki 	mutex_exit(&icmp_mtx);
   1097  1.158     ozaki 	return error;
   1098   1.81    atatat }
   1099   1.81    atatat 
   1100  1.116   thorpej static int
   1101  1.116   thorpej sysctl_net_inet_icmp_stats(SYSCTLFN_ARGS)
   1102  1.116   thorpej {
   1103  1.116   thorpej 
   1104  1.119   thorpej 	return (NETSTAT_SYSCTL(icmpstat_percpu, ICMP_NSTATS));
   1105  1.116   thorpej }
   1106  1.116   thorpej 
   1107  1.121     pooka static void
   1108  1.121     pooka sysctl_netinet_icmp_setup(struct sysctllog **clog)
   1109   1.81    atatat {
   1110   1.81    atatat 
   1111   1.82    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1112   1.82    atatat 		       CTLFLAG_PERMANENT,
   1113   1.81    atatat 		       CTLTYPE_NODE, "inet", NULL,
   1114   1.81    atatat 		       NULL, 0, NULL, 0,
   1115   1.81    atatat 		       CTL_NET, PF_INET, CTL_EOL);
   1116   1.82    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1117   1.82    atatat 		       CTLFLAG_PERMANENT,
   1118   1.84    atatat 		       CTLTYPE_NODE, "icmp",
   1119   1.84    atatat 		       SYSCTL_DESCR("ICMPv4 related settings"),
   1120   1.81    atatat 		       NULL, 0, NULL, 0,
   1121   1.81    atatat 		       CTL_NET, PF_INET, IPPROTO_ICMP, CTL_EOL);
   1122   1.81    atatat 
   1123   1.82    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1124   1.82    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1125   1.84    atatat 		       CTLTYPE_INT, "maskrepl",
   1126   1.84    atatat 		       SYSCTL_DESCR("Respond to ICMP_MASKREQ messages"),
   1127   1.81    atatat 		       NULL, 0, &icmpmaskrepl, 0,
   1128   1.81    atatat 		       CTL_NET, PF_INET, IPPROTO_ICMP,
   1129   1.81    atatat 		       ICMPCTL_MASKREPL, CTL_EOL);
   1130   1.82    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1131   1.82    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1132   1.84    atatat 		       CTLTYPE_INT, "returndatabytes",
   1133   1.84    atatat 		       SYSCTL_DESCR("Number of bytes to return in an ICMP "
   1134   1.84    atatat 				    "error message"),
   1135   1.81    atatat 		       sysctl_net_inet_icmp_returndatabytes, 0,
   1136   1.81    atatat 		       &icmpreturndatabytes, 0,
   1137   1.81    atatat 		       CTL_NET, PF_INET, IPPROTO_ICMP,
   1138   1.81    atatat 		       ICMPCTL_RETURNDATABYTES, CTL_EOL);
   1139   1.82    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1140   1.82    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1141   1.84    atatat 		       CTLTYPE_INT, "errppslimit",
   1142   1.84    atatat 		       SYSCTL_DESCR("Maximum number of outgoing ICMP error "
   1143   1.84    atatat 				    "messages per second"),
   1144   1.81    atatat 		       NULL, 0, &icmperrppslim, 0,
   1145   1.81    atatat 		       CTL_NET, PF_INET, IPPROTO_ICMP,
   1146   1.81    atatat 		       ICMPCTL_ERRPPSLIMIT, CTL_EOL);
   1147   1.82    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1148   1.82    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1149   1.84    atatat 		       CTLTYPE_INT, "rediraccept",
   1150   1.84    atatat 		       SYSCTL_DESCR("Accept ICMP_REDIRECT messages"),
   1151   1.81    atatat 		       NULL, 0, &icmp_rediraccept, 0,
   1152   1.81    atatat 		       CTL_NET, PF_INET, IPPROTO_ICMP,
   1153   1.81    atatat 		       ICMPCTL_REDIRACCEPT, CTL_EOL);
   1154   1.82    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1155   1.82    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1156   1.84    atatat 		       CTLTYPE_INT, "redirtimeout",
   1157   1.84    atatat 		       SYSCTL_DESCR("Lifetime of ICMP_REDIRECT generated "
   1158   1.84    atatat 				    "routes"),
   1159   1.81    atatat 		       sysctl_net_inet_icmp_redirtimeout, 0,
   1160   1.81    atatat 		       &icmp_redirtimeout, 0,
   1161   1.81    atatat 		       CTL_NET, PF_INET, IPPROTO_ICMP,
   1162   1.81    atatat 		       ICMPCTL_REDIRTIMEOUT, CTL_EOL);
   1163   1.94      elad 	sysctl_createv(clog, 0, NULL, NULL,
   1164   1.94      elad 		       CTLFLAG_PERMANENT,
   1165   1.94      elad 		       CTLTYPE_STRUCT, "stats",
   1166   1.94      elad 		       SYSCTL_DESCR("ICMP statistics"),
   1167  1.116   thorpej 		       sysctl_net_inet_icmp_stats, 0, NULL, 0,
   1168   1.94      elad 		       CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_STATS,
   1169   1.94      elad 		       CTL_EOL);
   1170  1.122  christos 	sysctl_createv(clog, 0, NULL, NULL,
   1171  1.122  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1172  1.122  christos 		       CTLTYPE_INT, "bmcastecho",
   1173  1.122  christos 		       SYSCTL_DESCR("Respond to ICMP_ECHO or ICMP_TIMESTAMP "
   1174  1.122  christos 				    "message to the broadcast or multicast"),
   1175  1.122  christos 		       NULL, 0, &icmpbmcastecho, 0,
   1176  1.122  christos 		       CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_BMCASTECHO,
   1177  1.122  christos 		       CTL_EOL);
   1178   1.24       kml }
   1179   1.24       kml 
   1180  1.116   thorpej void
   1181  1.116   thorpej icmp_statinc(u_int stat)
   1182  1.116   thorpej {
   1183  1.116   thorpej 
   1184  1.116   thorpej 	KASSERT(stat < ICMP_NSTATS);
   1185  1.116   thorpej 	ICMP_STATINC(stat);
   1186  1.116   thorpej }
   1187  1.116   thorpej 
   1188   1.64      matt /* Table of common MTUs: */
   1189   1.64      matt 
   1190   1.64      matt static const u_int mtu_table[] = {
   1191   1.64      matt 	65535, 65280, 32000, 17914, 9180, 8166,
   1192   1.64      matt 	4352, 2002, 1492, 1006, 508, 296, 68, 0
   1193   1.64      matt };
   1194   1.64      matt 
   1195   1.55   thorpej void
   1196   1.90     perry icmp_mtudisc(struct icmp *icp, struct in_addr faddr)
   1197   1.24       kml {
   1198   1.57    itojun 	struct icmp_mtudisc_callback *mc;
   1199   1.55   thorpej 	struct sockaddr *dst = sintosa(&icmpsrc);
   1200   1.24       kml 	struct rtentry *rt;
   1201   1.26       kml 	u_long mtu = ntohs(icp->icmp_nextmtu);  /* Why a long?  IPv6 */
   1202   1.29       kml 	int    error;
   1203   1.24       kml 
   1204   1.24       kml 	rt = rtalloc1(dst, 1);
   1205  1.136     ozaki 	if (rt == NULL)
   1206   1.24       kml 		return;
   1207   1.67    itojun 
   1208   1.24       kml 	/* If we didn't get a host route, allocate one */
   1209   1.67    itojun 
   1210   1.24       kml 	if ((rt->rt_flags & RTF_HOST) == 0) {
   1211   1.24       kml 		struct rtentry *nrt;
   1212   1.24       kml 
   1213  1.145     ozaki 		error = rtrequest(RTM_ADD, dst, rt->rt_gateway, NULL,
   1214   1.24       kml 		    RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
   1215   1.24       kml 		if (error) {
   1216  1.154     ozaki 			rt_unref(rt);
   1217   1.24       kml 			return;
   1218   1.24       kml 		}
   1219   1.24       kml 		nrt->rt_rmx = rt->rt_rmx;
   1220  1.154     ozaki 		rt_unref(rt);
   1221   1.24       kml 		rt = nrt;
   1222   1.24       kml 	}
   1223  1.154     ozaki 
   1224  1.158     ozaki 	mutex_enter(&icmp_mtx);
   1225   1.29       kml 	error = rt_timer_add(rt, icmp_mtudisc_timeout, ip_mtudisc_timeout_q);
   1226  1.158     ozaki 	mutex_exit(&icmp_mtx);
   1227   1.29       kml 	if (error) {
   1228  1.154     ozaki 		rt_unref(rt);
   1229   1.29       kml 		return;
   1230   1.29       kml 	}
   1231   1.24       kml 
   1232   1.24       kml 	if (mtu == 0) {
   1233   1.24       kml 		int i = 0;
   1234   1.24       kml 
   1235   1.70    itojun 		mtu = ntohs(icp->icmp_ip.ip_len);
   1236   1.24       kml 		/* Some 4.2BSD-based routers incorrectly adjust the ip_len */
   1237   1.24       kml 		if (mtu > rt->rt_rmx.rmx_mtu && rt->rt_rmx.rmx_mtu != 0)
   1238   1.24       kml 			mtu -= (icp->icmp_ip.ip_hl << 2);
   1239   1.24       kml 
   1240   1.26       kml 		/* If we still can't guess a value, try the route */
   1241   1.26       kml 
   1242   1.26       kml 		if (mtu == 0) {
   1243   1.24       kml 			mtu = rt->rt_rmx.rmx_mtu;
   1244   1.24       kml 
   1245   1.26       kml 			/* If no route mtu, default to the interface mtu */
   1246   1.26       kml 
   1247   1.26       kml 			if (mtu == 0)
   1248   1.26       kml 				mtu = rt->rt_ifp->if_mtu;
   1249   1.26       kml 		}
   1250   1.26       kml 
   1251   1.24       kml 		for (i = 0; i < sizeof(mtu_table) / sizeof(mtu_table[0]); i++)
   1252   1.26       kml 			if (mtu > mtu_table[i]) {
   1253   1.26       kml 				mtu = mtu_table[i];
   1254   1.24       kml 				break;
   1255   1.26       kml 			}
   1256   1.24       kml 	}
   1257   1.24       kml 
   1258   1.29       kml 	/*
   1259   1.29       kml 	 * XXX:   RTV_MTU is overloaded, since the admin can set it
   1260   1.29       kml 	 *	  to turn off PMTU for a route, and the kernel can
   1261   1.29       kml 	 *	  set it to indicate a serious problem with PMTU
   1262   1.29       kml 	 *	  on a route.  We should be using a separate flag
   1263   1.29       kml 	 *	  for the kernel to indicate this.
   1264   1.29       kml 	 */
   1265   1.29       kml 
   1266   1.24       kml 	if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
   1267   1.26       kml 		if (mtu < 296 || mtu > rt->rt_ifp->if_mtu)
   1268   1.24       kml 			rt->rt_rmx.rmx_locks |= RTV_MTU;
   1269   1.67    itojun 		else if (rt->rt_rmx.rmx_mtu > mtu ||
   1270   1.56    itojun 			 rt->rt_rmx.rmx_mtu == 0) {
   1271  1.116   thorpej 			ICMP_STATINC(ICMP_STAT_PMTUCHG);
   1272   1.24       kml 			rt->rt_rmx.rmx_mtu = mtu;
   1273   1.56    itojun 		}
   1274   1.24       kml 	}
   1275   1.26       kml 
   1276  1.154     ozaki 	if (rt != NULL)
   1277  1.154     ozaki 		rt_unref(rt);
   1278   1.55   thorpej 
   1279   1.55   thorpej 	/*
   1280   1.55   thorpej 	 * Notify protocols that the MTU for this destination
   1281   1.55   thorpej 	 * has changed.
   1282   1.55   thorpej 	 */
   1283  1.158     ozaki 	mutex_enter(&icmp_mtx);
   1284   1.55   thorpej 	for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL;
   1285   1.55   thorpej 	     mc = LIST_NEXT(mc, mc_list))
   1286   1.55   thorpej 		(*mc->mc_func)(faddr);
   1287  1.158     ozaki 	mutex_exit(&icmp_mtx);
   1288   1.37    itojun }
   1289   1.37    itojun 
   1290   1.37    itojun /*
   1291   1.37    itojun  * Return the next larger or smaller MTU plateau (table from RFC 1191)
   1292   1.37    itojun  * given current value MTU.  If DIR is less than zero, a larger plateau
   1293   1.37    itojun  * is returned; otherwise, a smaller value is returned.
   1294   1.37    itojun  */
   1295   1.98      matt u_int
   1296   1.98      matt ip_next_mtu(u_int mtu, int dir)	/* XXX */
   1297   1.37    itojun {
   1298   1.37    itojun 	int i;
   1299   1.37    itojun 
   1300   1.64      matt 	for (i = 0; i < (sizeof mtu_table) / (sizeof mtu_table[0]); i++) {
   1301   1.64      matt 		if (mtu >= mtu_table[i])
   1302   1.37    itojun 			break;
   1303   1.37    itojun 	}
   1304   1.37    itojun 
   1305   1.37    itojun 	if (dir < 0) {
   1306   1.37    itojun 		if (i == 0) {
   1307   1.37    itojun 			return 0;
   1308   1.37    itojun 		} else {
   1309   1.64      matt 			return mtu_table[i - 1];
   1310   1.37    itojun 		}
   1311   1.37    itojun 	} else {
   1312   1.64      matt 		if (mtu_table[i] == 0) {
   1313   1.37    itojun 			return 0;
   1314   1.64      matt 		} else if (mtu > mtu_table[i]) {
   1315   1.64      matt 			return mtu_table[i];
   1316   1.37    itojun 		} else {
   1317   1.64      matt 			return mtu_table[i + 1];
   1318   1.37    itojun 		}
   1319   1.37    itojun 	}
   1320   1.29       kml }
   1321   1.29       kml 
   1322   1.29       kml static void
   1323  1.105  christos icmp_mtudisc_timeout(struct rtentry *rt, struct rttimer *r)
   1324   1.29       kml {
   1325  1.142     ozaki 
   1326  1.137     ozaki 	KASSERT(rt != NULL);
   1327  1.142     ozaki 	rt_assert_referenced(rt);
   1328  1.137     ozaki 
   1329   1.67    itojun 	if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
   1330   1.29       kml 	    (RTF_DYNAMIC | RTF_HOST)) {
   1331  1.145     ozaki 		rtrequest(RTM_DELETE, rt_getkey(rt),
   1332  1.145     ozaki 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
   1333   1.29       kml 	} else {
   1334   1.29       kml 		if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
   1335   1.29       kml 			rt->rt_rmx.rmx_mtu = 0;
   1336   1.29       kml 		}
   1337   1.63       kml 	}
   1338   1.63       kml }
   1339   1.63       kml 
   1340   1.63       kml static void
   1341  1.105  christos icmp_redirect_timeout(struct rtentry *rt, struct rttimer *r)
   1342   1.63       kml {
   1343  1.142     ozaki 
   1344  1.137     ozaki 	KASSERT(rt != NULL);
   1345  1.142     ozaki 	rt_assert_referenced(rt);
   1346  1.137     ozaki 
   1347   1.67    itojun 	if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
   1348   1.63       kml 	    (RTF_DYNAMIC | RTF_HOST)) {
   1349  1.145     ozaki 		rtrequest(RTM_DELETE, rt_getkey(rt),
   1350  1.145     ozaki 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
   1351   1.29       kml 	}
   1352   1.40   thorpej }
   1353   1.40   thorpej 
   1354   1.40   thorpej /*
   1355   1.40   thorpej  * Perform rate limit check.
   1356   1.40   thorpej  * Returns 0 if it is okay to send the icmp packet.
   1357   1.40   thorpej  * Returns 1 if the router SHOULD NOT send this icmp packet due to rate
   1358   1.40   thorpej  * limitation.
   1359   1.40   thorpej  *
   1360   1.40   thorpej  * XXX per-destination/type check necessary?
   1361   1.40   thorpej  */
   1362  1.123    kefren int
   1363  1.105  christos icmp_ratelimit(const struct in_addr *dst, const int type,
   1364  1.105  christos     const int code)
   1365   1.40   thorpej {
   1366   1.40   thorpej 
   1367   1.51    itojun 	/* PPS limit */
   1368   1.51    itojun 	if (!ppsratecheck(&icmperrppslim_last, &icmperrpps_count,
   1369   1.51    itojun 	    icmperrppslim)) {
   1370   1.51    itojun 		/* The packet is subject to rate limit */
   1371   1.51    itojun 		return 1;
   1372   1.51    itojun 	}
   1373   1.51    itojun 
   1374   1.90     perry 	/* okay to send */
   1375   1.51    itojun 	return 0;
   1376    1.1       cgd }
   1377