Home | History | Annotate | Line # | Download | only in netinet
ip_icmp.c revision 1.75
      1 /*	$NetBSD: ip_icmp.c,v 1.75 2003/08/07 16:33:12 agc 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.75 2003/08/07 16:33:12 agc 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 #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 /*
    151  * List of callbacks to notify when Path MTU changes are made.
    152  */
    153 struct icmp_mtudisc_callback {
    154 	LIST_ENTRY(icmp_mtudisc_callback) mc_list;
    155 	void (*mc_func) __P((struct in_addr));
    156 };
    157 
    158 LIST_HEAD(, icmp_mtudisc_callback) icmp_mtudisc_callbacks =
    159     LIST_HEAD_INITIALIZER(&icmp_mtudisc_callbacks);
    160 
    161 #if 0
    162 static int	ip_next_mtu __P((int, int));
    163 #else
    164 /*static*/ int	ip_next_mtu __P((int, int));
    165 #endif
    166 
    167 extern int icmperrppslim;
    168 static int icmperrpps_count = 0;
    169 static struct timeval icmperrppslim_last;
    170 static int icmp_rediraccept = 1;
    171 static int icmp_redirtimeout = 600;
    172 static struct rttimer_queue *icmp_redirect_timeout_q = NULL;
    173 
    174 static void icmp_mtudisc_timeout __P((struct rtentry *, struct rttimer *));
    175 static void icmp_redirect_timeout __P((struct rtentry *, struct rttimer *));
    176 
    177 static int icmp_ratelimit __P((const struct in_addr *, const int, const int));
    178 
    179 
    180 void
    181 icmp_init()
    182 {
    183 	/*
    184 	 * This is only useful if the user initializes redirtimeout to
    185 	 * something other than zero.
    186 	 */
    187 	if (icmp_redirtimeout != 0) {
    188 		icmp_redirect_timeout_q =
    189 			rt_timer_queue_create(icmp_redirtimeout);
    190 	}
    191 }
    192 
    193 /*
    194  * Register a Path MTU Discovery callback.
    195  */
    196 void
    197 icmp_mtudisc_callback_register(func)
    198 	void (*func) __P((struct in_addr));
    199 {
    200 	struct icmp_mtudisc_callback *mc;
    201 
    202 	for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL;
    203 	     mc = LIST_NEXT(mc, mc_list)) {
    204 		if (mc->mc_func == func)
    205 			return;
    206 	}
    207 
    208 	mc = malloc(sizeof(*mc), M_PCB, M_NOWAIT);
    209 	if (mc == NULL)
    210 		panic("icmp_mtudisc_callback_register");
    211 
    212 	mc->mc_func = func;
    213 	LIST_INSERT_HEAD(&icmp_mtudisc_callbacks, mc, mc_list);
    214 }
    215 
    216 /*
    217  * Generate an error packet of type error
    218  * in response to bad packet ip.
    219  */
    220 void
    221 icmp_error(n, type, code, dest, destifp)
    222 	struct mbuf *n;
    223 	int type, code;
    224 	n_long dest;
    225 	struct ifnet *destifp;
    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 	unsigned icmplen, mblen;
    232 
    233 #ifdef ICMPPRINTFS
    234 	if (icmpprintfs)
    235 		printf("icmp_error(%x, %d, %d)\n", oip, type, code);
    236 #endif
    237 	if (type != ICMP_REDIRECT)
    238 		icmpstat.icps_error++;
    239 	/*
    240 	 * Don't send error if the original packet was encrypted.
    241 	 * Don't send error if not the first fragment of message.
    242 	 * Don't error if the old packet protocol was ICMP
    243 	 * error message, only known informational types.
    244 	 */
    245 	if (n->m_flags & M_DECRYPTED)
    246 		goto freeit;
    247 	if (oip->ip_off &~ htons(IP_MF|IP_DF))
    248 		goto freeit;
    249 	if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
    250 	  n->m_len >= oiplen + ICMP_MINLEN &&
    251 	  !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) {
    252 		icmpstat.icps_oldicmp++;
    253 		goto freeit;
    254 	}
    255 	/* Don't send error in response to a multicast or broadcast packet */
    256 	if (n->m_flags & (M_BCAST|M_MCAST))
    257 		goto freeit;
    258 
    259 	/*
    260 	 * First, do a rate limitation check.
    261 	 */
    262 	if (icmp_ratelimit(&oip->ip_src, type, code)) {
    263 		/* XXX stat */
    264 		goto freeit;
    265 	}
    266 
    267 	/*
    268 	 * Now, formulate icmp message
    269 	 */
    270 	icmplen = oiplen + min(icmpreturndatabytes,
    271 	    ntohs(oip->ip_len) - oiplen);
    272 	/*
    273 	 * Defend against mbuf chains shorter than oip->ip_len - oiplen:
    274 	 */
    275 	mblen = 0;
    276 	for (m = n; m && (mblen < icmplen); m = m->m_next)
    277 		mblen += m->m_len;
    278 	icmplen = min(mblen, icmplen);
    279 
    280 	/*
    281 	 * As we are not required to return everything we have,
    282 	 * we return whatever we can return at ease.
    283 	 *
    284 	 * Note that ICMP datagrams longer than 576 octets are out of spec
    285 	 * according to RFC1812; the limit on icmpreturndatabytes below in
    286 	 * icmp_sysctl will keep things below that limit.
    287 	 */
    288 
    289 	KASSERT(ICMP_MINLEN <= MCLBYTES);
    290 
    291 	if (icmplen + ICMP_MINLEN > MCLBYTES)
    292 		icmplen = MCLBYTES - ICMP_MINLEN;
    293 
    294 	m = m_gethdr(M_DONTWAIT, MT_HEADER);
    295 	if (m && (icmplen + ICMP_MINLEN > MHLEN)) {
    296 		MCLGET(m, M_DONTWAIT);
    297 		if ((m->m_flags & M_EXT) == 0) {
    298 			m_freem(m);
    299 			m = NULL;
    300 		}
    301 	}
    302 	if (m == NULL)
    303 		goto freeit;
    304 	MCLAIM(m, n->m_owner);
    305 	m->m_len = icmplen + ICMP_MINLEN;
    306 	if ((m->m_flags & M_EXT) == 0)
    307 		MH_ALIGN(m, m->m_len);
    308 	icp = mtod(m, struct icmp *);
    309 	if ((u_int)type > ICMP_MAXTYPE)
    310 		panic("icmp_error");
    311 	icmpstat.icps_outhist[type]++;
    312 	icp->icmp_type = type;
    313 	if (type == ICMP_REDIRECT)
    314 		icp->icmp_gwaddr.s_addr = dest;
    315 	else {
    316 		icp->icmp_void = 0;
    317 		/*
    318 		 * The following assignments assume an overlay with the
    319 		 * zeroed icmp_void field.
    320 		 */
    321 		if (type == ICMP_PARAMPROB) {
    322 			icp->icmp_pptr = code;
    323 			code = 0;
    324 		} else if (type == ICMP_UNREACH &&
    325 		    code == ICMP_UNREACH_NEEDFRAG && destifp)
    326 			icp->icmp_nextmtu = htons(destifp->if_mtu);
    327 	}
    328 
    329 	icp->icmp_code = code;
    330 	m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip);
    331 	nip = &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 	icmp_reflect(m);
    355 
    356 freeit:
    357 	m_freem(n);
    358 }
    359 
    360 static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET };
    361 static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET };
    362 static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET };
    363 struct sockaddr_in icmpmask = { 8, 0 };
    364 
    365 /*
    366  * Process a received ICMP message.
    367  */
    368 void
    369 #if __STDC__
    370 icmp_input(struct mbuf *m, ...)
    371 #else
    372 icmp_input(m, va_alist)
    373 	struct mbuf *m;
    374 	va_dcl
    375 #endif
    376 {
    377 	int proto;
    378 	struct icmp *icp;
    379 	struct ip *ip = mtod(m, struct ip *);
    380 	int icmplen;
    381 	int i;
    382 	struct in_ifaddr *ia;
    383 	void *(*ctlfunc) __P((int, struct sockaddr *, void *));
    384 	int code;
    385 	int hlen;
    386 	va_list ap;
    387 	struct rtentry *rt;
    388 
    389 	va_start(ap, m);
    390 	hlen = va_arg(ap, int);
    391 	proto = va_arg(ap, int);
    392 	va_end(ap);
    393 
    394 	/*
    395 	 * Locate icmp structure in mbuf, and check
    396 	 * that not corrupted and of at least minimum length.
    397 	 */
    398 	icmplen = ntohs(ip->ip_len) - hlen;
    399 #ifdef ICMPPRINTFS
    400 	if (icmpprintfs)
    401 		printf("icmp_input from %x to %x, len %d\n",
    402 		    ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr),
    403 		    icmplen);
    404 #endif
    405 	if (icmplen < ICMP_MINLEN) {
    406 		icmpstat.icps_tooshort++;
    407 		goto freeit;
    408 	}
    409 	i = hlen + min(icmplen, ICMP_ADVLENMIN);
    410 	if (m->m_len < i && (m = m_pullup(m, i)) == 0) {
    411 		icmpstat.icps_tooshort++;
    412 		return;
    413 	}
    414 	ip = mtod(m, struct ip *);
    415 	m->m_len -= hlen;
    416 	m->m_data += hlen;
    417 	icp = mtod(m, struct icmp *);
    418 	/* Don't need to assert alignment, here. */
    419 	if (in_cksum(m, icmplen)) {
    420 		icmpstat.icps_checksum++;
    421 		goto freeit;
    422 	}
    423 	m->m_len += hlen;
    424 	m->m_data -= hlen;
    425 
    426 #ifdef ICMPPRINTFS
    427 	/*
    428 	 * Message type specific processing.
    429 	 */
    430 	if (icmpprintfs)
    431 		printf("icmp_input, type %d code %d\n", icp->icmp_type,
    432 		    icp->icmp_code);
    433 #endif
    434 	if (icp->icmp_type > ICMP_MAXTYPE)
    435 		goto raw;
    436 	icmpstat.icps_inhist[icp->icmp_type]++;
    437 	code = icp->icmp_code;
    438 	switch (icp->icmp_type) {
    439 
    440 	case ICMP_UNREACH:
    441 		switch (code) {
    442 			case ICMP_UNREACH_NET:
    443 			case ICMP_UNREACH_HOST:
    444 			case ICMP_UNREACH_PROTOCOL:
    445 			case ICMP_UNREACH_PORT:
    446 			case ICMP_UNREACH_SRCFAIL:
    447 				code += PRC_UNREACH_NET;
    448 				break;
    449 
    450 			case ICMP_UNREACH_NEEDFRAG:
    451 				code = PRC_MSGSIZE;
    452 				break;
    453 
    454 			case ICMP_UNREACH_NET_UNKNOWN:
    455 			case ICMP_UNREACH_NET_PROHIB:
    456 			case ICMP_UNREACH_TOSNET:
    457 				code = PRC_UNREACH_NET;
    458 				break;
    459 
    460 			case ICMP_UNREACH_HOST_UNKNOWN:
    461 			case ICMP_UNREACH_ISOLATED:
    462 			case ICMP_UNREACH_HOST_PROHIB:
    463 			case ICMP_UNREACH_TOSHOST:
    464 				code = PRC_UNREACH_HOST;
    465 				break;
    466 
    467 			default:
    468 				goto badcode;
    469 		}
    470 		goto deliver;
    471 
    472 	case ICMP_TIMXCEED:
    473 		if (code > 1)
    474 			goto badcode;
    475 		code += PRC_TIMXCEED_INTRANS;
    476 		goto deliver;
    477 
    478 	case ICMP_PARAMPROB:
    479 		if (code > 1)
    480 			goto badcode;
    481 		code = PRC_PARAMPROB;
    482 		goto deliver;
    483 
    484 	case ICMP_SOURCEQUENCH:
    485 		if (code)
    486 			goto badcode;
    487 		code = PRC_QUENCH;
    488 		goto deliver;
    489 
    490 	deliver:
    491 		/*
    492 		 * Problem with datagram; advise higher level routines.
    493 		 */
    494 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
    495 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
    496 			icmpstat.icps_badlen++;
    497 			goto freeit;
    498 		}
    499 		if (IN_MULTICAST(icp->icmp_ip.ip_dst.s_addr))
    500 			goto badcode;
    501 #ifdef ICMPPRINTFS
    502 		if (icmpprintfs)
    503 			printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
    504 #endif
    505 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
    506 		ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
    507 		if (ctlfunc)
    508 			(void) (*ctlfunc)(code, sintosa(&icmpsrc),
    509 			    &icp->icmp_ip);
    510 		break;
    511 
    512 	badcode:
    513 		icmpstat.icps_badcode++;
    514 		break;
    515 
    516 	case ICMP_ECHO:
    517 		icp->icmp_type = ICMP_ECHOREPLY;
    518 		goto reflect;
    519 
    520 	case ICMP_TSTAMP:
    521 		if (icmplen < ICMP_TSLEN) {
    522 			icmpstat.icps_badlen++;
    523 			break;
    524 		}
    525 		icp->icmp_type = ICMP_TSTAMPREPLY;
    526 		icp->icmp_rtime = iptime();
    527 		icp->icmp_ttime = icp->icmp_rtime;	/* bogus, do later! */
    528 		goto reflect;
    529 
    530 	case ICMP_MASKREQ:
    531 		if (icmpmaskrepl == 0)
    532 			break;
    533 		/*
    534 		 * We are not able to respond with all ones broadcast
    535 		 * unless we receive it over a point-to-point interface.
    536 		 */
    537 		if (icmplen < ICMP_MASKLEN) {
    538 			icmpstat.icps_badlen++;
    539 			break;
    540 		}
    541 		if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
    542 		    in_nullhost(ip->ip_dst))
    543 			icmpdst.sin_addr = ip->ip_src;
    544 		else
    545 			icmpdst.sin_addr = ip->ip_dst;
    546 		ia = ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst),
    547 		    m->m_pkthdr.rcvif));
    548 		if (ia == 0)
    549 			break;
    550 		icp->icmp_type = ICMP_MASKREPLY;
    551 		icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
    552 		if (in_nullhost(ip->ip_src)) {
    553 			if (ia->ia_ifp->if_flags & IFF_BROADCAST)
    554 				ip->ip_src = ia->ia_broadaddr.sin_addr;
    555 			else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
    556 				ip->ip_src = ia->ia_dstaddr.sin_addr;
    557 		}
    558 reflect:
    559 		icmpstat.icps_reflect++;
    560 		icmpstat.icps_outhist[icp->icmp_type]++;
    561 		icmp_reflect(m);
    562 		return;
    563 
    564 	case ICMP_REDIRECT:
    565 		if (code > 3)
    566 			goto badcode;
    567 		if (icmp_rediraccept == 0)
    568 			goto freeit;
    569 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
    570 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
    571 			icmpstat.icps_badlen++;
    572 			break;
    573 		}
    574 		/*
    575 		 * Short circuit routing redirects to force
    576 		 * immediate change in the kernel's routing
    577 		 * tables.  The message is also handed to anyone
    578 		 * listening on a raw socket (e.g. the routing
    579 		 * daemon for use in updating its tables).
    580 		 */
    581 		icmpgw.sin_addr = ip->ip_src;
    582 		icmpdst.sin_addr = icp->icmp_gwaddr;
    583 #ifdef	ICMPPRINTFS
    584 		if (icmpprintfs)
    585 			printf("redirect dst %x to %x\n", icp->icmp_ip.ip_dst,
    586 			    icp->icmp_gwaddr);
    587 #endif
    588 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
    589 		rt = NULL;
    590 		rtredirect(sintosa(&icmpsrc), sintosa(&icmpdst),
    591 		    (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
    592 		    sintosa(&icmpgw), (struct rtentry **)&rt);
    593 		if (rt != NULL && icmp_redirtimeout != 0) {
    594 			i = rt_timer_add(rt, icmp_redirect_timeout,
    595 					 icmp_redirect_timeout_q);
    596 			if (i)
    597 				log(LOG_ERR, "ICMP:  redirect failed to "
    598 				    "register timeout for route to %x, "
    599 				    "code %d\n",
    600 				    icp->icmp_ip.ip_dst.s_addr, i);
    601 		}
    602 		if (rt != NULL)
    603 			rtfree(rt);
    604 
    605 		pfctlinput(PRC_REDIRECT_HOST, sintosa(&icmpsrc));
    606 #ifdef IPSEC
    607 		key_sa_routechange((struct sockaddr *)&icmpsrc);
    608 #endif
    609 		break;
    610 
    611 	/*
    612 	 * No kernel processing for the following;
    613 	 * just fall through to send to raw listener.
    614 	 */
    615 	case ICMP_ECHOREPLY:
    616 	case ICMP_ROUTERADVERT:
    617 	case ICMP_ROUTERSOLICIT:
    618 	case ICMP_TSTAMPREPLY:
    619 	case ICMP_IREQREPLY:
    620 	case ICMP_MASKREPLY:
    621 	default:
    622 		break;
    623 	}
    624 
    625 raw:
    626 	rip_input(m, hlen, proto);
    627 	return;
    628 
    629 freeit:
    630 	m_freem(m);
    631 	return;
    632 }
    633 
    634 /*
    635  * Reflect the ip packet back to the source
    636  */
    637 void
    638 icmp_reflect(m)
    639 	struct mbuf *m;
    640 {
    641 	struct ip *ip = mtod(m, struct ip *);
    642 	struct in_ifaddr *ia;
    643 	struct ifaddr *ifa;
    644 	struct sockaddr_in *sin = 0;
    645 	struct in_addr t;
    646 	struct mbuf *opts = 0;
    647 	int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
    648 
    649 	if (!in_canforward(ip->ip_src) &&
    650 	    ((ip->ip_src.s_addr & IN_CLASSA_NET) !=
    651 	     htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
    652 		m_freem(m);	/* Bad return address */
    653 		goto done;	/* ip_output() will check for broadcast */
    654 	}
    655 	t = ip->ip_dst;
    656 	ip->ip_dst = ip->ip_src;
    657 	/*
    658 	 * If the incoming packet was addressed directly to us, use
    659 	 * dst as the src for the reply.  Otherwise (broadcast or
    660 	 * anonymous), use an address which corresponds to the
    661 	 * incoming interface, with a preference for the address which
    662 	 * corresponds to the route to the destination of the ICMP.
    663 	 */
    664 
    665 	/* Look for packet addressed to us */
    666 	INADDR_TO_IA(t, ia);
    667 
    668 	/* look for packet sent to broadcast address */
    669 	if (ia == NULL && (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) {
    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) {
    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_ifaddr, 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->m_flags &= ~(M_BCAST|M_MCAST);
    834 
    835 	/*
    836 	 * Clear any in-bound checksum flags for this packet.
    837 	 */
    838 	m->m_pkthdr.csum_flags = 0;
    839 
    840 	icmp_send(m, opts);
    841 done:
    842 	if (opts)
    843 		(void)m_free(opts);
    844 }
    845 
    846 /*
    847  * Send an icmp packet back to the ip level,
    848  * after supplying a checksum.
    849  */
    850 void
    851 icmp_send(m, opts)
    852 	struct mbuf *m;
    853 	struct mbuf *opts;
    854 {
    855 	struct ip *ip = mtod(m, struct ip *);
    856 	int hlen;
    857 	struct icmp *icp;
    858 
    859 	hlen = ip->ip_hl << 2;
    860 	m->m_data += hlen;
    861 	m->m_len -= hlen;
    862 	icp = mtod(m, struct icmp *);
    863 	icp->icmp_cksum = 0;
    864 	icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen);
    865 	m->m_data -= hlen;
    866 	m->m_len += hlen;
    867 #ifdef ICMPPRINTFS
    868 	if (icmpprintfs)
    869 		printf("icmp_send dst %x src %x\n", ip->ip_dst, ip->ip_src);
    870 #endif
    871 #ifdef IPSEC
    872 	/* Don't lookup socket */
    873 	(void)ipsec_setsocket(m, NULL);
    874 #endif
    875 	(void) ip_output(m, opts, NULL, 0, NULL);
    876 }
    877 
    878 n_time
    879 iptime()
    880 {
    881 	struct timeval atv;
    882 	u_long t;
    883 
    884 	microtime(&atv);
    885 	t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
    886 	return (htonl(t));
    887 }
    888 
    889 int
    890 icmp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    891 	int *name;
    892 	u_int namelen;
    893 	void *oldp;
    894 	size_t *oldlenp;
    895 	void *newp;
    896 	size_t newlen;
    897 {
    898 	int arg, error;
    899 
    900 	/* All sysctl names at this level are terminal. */
    901 	if (namelen != 1)
    902 		return (ENOTDIR);
    903 
    904 	switch (name[0])
    905 	{
    906 	case ICMPCTL_MASKREPL:
    907 		error = sysctl_int(oldp, oldlenp, newp, newlen, &icmpmaskrepl);
    908 		break;
    909 	case ICMPCTL_RETURNDATABYTES:
    910 		arg = icmpreturndatabytes;
    911 		error = sysctl_int(oldp, oldlenp, newp, newlen, &arg);
    912 		if (error)
    913 			break;
    914 		if ((arg >= 8) || (arg <= 512))
    915 			icmpreturndatabytes = arg;
    916 		else
    917 			error = EINVAL;
    918 		break;
    919 	case ICMPCTL_ERRPPSLIMIT:
    920 		error = sysctl_int(oldp, oldlenp, newp, newlen, &icmperrppslim);
    921 		break;
    922 	case ICMPCTL_REDIRACCEPT:
    923 		error = sysctl_int(oldp, oldlenp, newp, newlen,
    924 				   &icmp_rediraccept);
    925 		break;
    926 	case ICMPCTL_REDIRTIMEOUT:
    927 		error = sysctl_int(oldp, oldlenp, newp, newlen,
    928 				   &icmp_redirtimeout);
    929 		if (icmp_redirect_timeout_q != NULL) {
    930 			if (icmp_redirtimeout == 0) {
    931 				rt_timer_queue_destroy(icmp_redirect_timeout_q,
    932 						       TRUE);
    933 				icmp_redirect_timeout_q = NULL;
    934 			} else {
    935 				rt_timer_queue_change(icmp_redirect_timeout_q,
    936 						      icmp_redirtimeout);
    937 			}
    938 		} else if (icmp_redirtimeout > 0) {
    939 			icmp_redirect_timeout_q =
    940 				rt_timer_queue_create(icmp_redirtimeout);
    941 		}
    942 		return (error);
    943 	default:
    944 		error = ENOPROTOOPT;
    945 		break;
    946 	}
    947 	return error;
    948 }
    949 
    950 /* Table of common MTUs: */
    951 
    952 static const u_int mtu_table[] = {
    953 	65535, 65280, 32000, 17914, 9180, 8166,
    954 	4352, 2002, 1492, 1006, 508, 296, 68, 0
    955 };
    956 
    957 void
    958 icmp_mtudisc(icp, faddr)
    959 	struct icmp *icp;
    960 	struct in_addr faddr;
    961 {
    962 	struct icmp_mtudisc_callback *mc;
    963 	struct sockaddr *dst = sintosa(&icmpsrc);
    964 	struct rtentry *rt;
    965 	u_long mtu = ntohs(icp->icmp_nextmtu);  /* Why a long?  IPv6 */
    966 	int    error;
    967 
    968 	rt = rtalloc1(dst, 1);
    969 	if (rt == 0)
    970 		return;
    971 
    972 	/* If we didn't get a host route, allocate one */
    973 
    974 	if ((rt->rt_flags & RTF_HOST) == 0) {
    975 		struct rtentry *nrt;
    976 
    977 		error = rtrequest((int) RTM_ADD, dst,
    978 		    (struct sockaddr *) rt->rt_gateway,
    979 		    (struct sockaddr *) 0,
    980 		    RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
    981 		if (error) {
    982 			rtfree(rt);
    983 			return;
    984 		}
    985 		nrt->rt_rmx = rt->rt_rmx;
    986 		rtfree(rt);
    987 		rt = nrt;
    988 	}
    989 	error = rt_timer_add(rt, icmp_mtudisc_timeout, ip_mtudisc_timeout_q);
    990 	if (error) {
    991 		rtfree(rt);
    992 		return;
    993 	}
    994 
    995 	if (mtu == 0) {
    996 		int i = 0;
    997 
    998 		mtu = ntohs(icp->icmp_ip.ip_len);
    999 		/* Some 4.2BSD-based routers incorrectly adjust the ip_len */
   1000 		if (mtu > rt->rt_rmx.rmx_mtu && rt->rt_rmx.rmx_mtu != 0)
   1001 			mtu -= (icp->icmp_ip.ip_hl << 2);
   1002 
   1003 		/* If we still can't guess a value, try the route */
   1004 
   1005 		if (mtu == 0) {
   1006 			mtu = rt->rt_rmx.rmx_mtu;
   1007 
   1008 			/* If no route mtu, default to the interface mtu */
   1009 
   1010 			if (mtu == 0)
   1011 				mtu = rt->rt_ifp->if_mtu;
   1012 		}
   1013 
   1014 		for (i = 0; i < sizeof(mtu_table) / sizeof(mtu_table[0]); i++)
   1015 			if (mtu > mtu_table[i]) {
   1016 				mtu = mtu_table[i];
   1017 				break;
   1018 			}
   1019 	}
   1020 
   1021 	/*
   1022 	 * XXX:   RTV_MTU is overloaded, since the admin can set it
   1023 	 *	  to turn off PMTU for a route, and the kernel can
   1024 	 *	  set it to indicate a serious problem with PMTU
   1025 	 *	  on a route.  We should be using a separate flag
   1026 	 *	  for the kernel to indicate this.
   1027 	 */
   1028 
   1029 	if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
   1030 		if (mtu < 296 || mtu > rt->rt_ifp->if_mtu)
   1031 			rt->rt_rmx.rmx_locks |= RTV_MTU;
   1032 		else if (rt->rt_rmx.rmx_mtu > mtu ||
   1033 			 rt->rt_rmx.rmx_mtu == 0) {
   1034 			icmpstat.icps_pmtuchg++;
   1035 			rt->rt_rmx.rmx_mtu = mtu;
   1036 		}
   1037 	}
   1038 
   1039 	if (rt)
   1040 		rtfree(rt);
   1041 
   1042 	/*
   1043 	 * Notify protocols that the MTU for this destination
   1044 	 * has changed.
   1045 	 */
   1046 	for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL;
   1047 	     mc = LIST_NEXT(mc, mc_list))
   1048 		(*mc->mc_func)(faddr);
   1049 }
   1050 
   1051 /*
   1052  * Return the next larger or smaller MTU plateau (table from RFC 1191)
   1053  * given current value MTU.  If DIR is less than zero, a larger plateau
   1054  * is returned; otherwise, a smaller value is returned.
   1055  */
   1056 int
   1057 ip_next_mtu(mtu, dir)	/* XXX */
   1058 	int mtu;
   1059 	int dir;
   1060 {
   1061 	int i;
   1062 
   1063 	for (i = 0; i < (sizeof mtu_table) / (sizeof mtu_table[0]); i++) {
   1064 		if (mtu >= mtu_table[i])
   1065 			break;
   1066 	}
   1067 
   1068 	if (dir < 0) {
   1069 		if (i == 0) {
   1070 			return 0;
   1071 		} else {
   1072 			return mtu_table[i - 1];
   1073 		}
   1074 	} else {
   1075 		if (mtu_table[i] == 0) {
   1076 			return 0;
   1077 		} else if (mtu > mtu_table[i]) {
   1078 			return mtu_table[i];
   1079 		} else {
   1080 			return mtu_table[i + 1];
   1081 		}
   1082 	}
   1083 }
   1084 
   1085 static void
   1086 icmp_mtudisc_timeout(rt, r)
   1087 	struct rtentry *rt;
   1088 	struct rttimer *r;
   1089 {
   1090 	if (rt == NULL)
   1091 		panic("icmp_mtudisc_timeout:  bad route to timeout");
   1092 	if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
   1093 	    (RTF_DYNAMIC | RTF_HOST)) {
   1094 		rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
   1095 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
   1096 	} else {
   1097 		if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
   1098 			rt->rt_rmx.rmx_mtu = 0;
   1099 		}
   1100 	}
   1101 }
   1102 
   1103 static void
   1104 icmp_redirect_timeout(rt, r)
   1105 	struct rtentry *rt;
   1106 	struct rttimer *r;
   1107 {
   1108 	if (rt == NULL)
   1109 		panic("icmp_redirect_timeout:  bad route to timeout");
   1110 	if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
   1111 	    (RTF_DYNAMIC | RTF_HOST)) {
   1112 		rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
   1113 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
   1114 	}
   1115 }
   1116 
   1117 /*
   1118  * Perform rate limit check.
   1119  * Returns 0 if it is okay to send the icmp packet.
   1120  * Returns 1 if the router SHOULD NOT send this icmp packet due to rate
   1121  * limitation.
   1122  *
   1123  * XXX per-destination/type check necessary?
   1124  */
   1125 static int
   1126 icmp_ratelimit(dst, type, code)
   1127 	const struct in_addr *dst;
   1128 	const int type;			/* not used at this moment */
   1129 	const int code;			/* not used at this moment */
   1130 {
   1131 
   1132 	/* PPS limit */
   1133 	if (!ppsratecheck(&icmperrppslim_last, &icmperrpps_count,
   1134 	    icmperrppslim)) {
   1135 		/* The packet is subject to rate limit */
   1136 		return 1;
   1137 	}
   1138 
   1139 	/*okay to send*/
   1140 	return 0;
   1141 }
   1142