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