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