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