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