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