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