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