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