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