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