Home | History | Annotate | Line # | Download | only in netinet
ip_icmp.c revision 1.36.6.3
      1 /*	$NetBSD: ip_icmp.c,v 1.36.6.3 1999/11/30 13:35:30 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 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  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by the NetBSD
     51  *	Foundation, Inc. and its contributors.
     52  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  *    contributors may be used to endorse or promote products derived
     54  *    from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  * POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1982, 1986, 1988, 1993
     71  *	The Regents of the University of California.  All rights reserved.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. All advertising materials mentioning features or use of this software
     82  *    must display the following acknowledgement:
     83  *	This product includes software developed by the University of
     84  *	California, Berkeley and its contributors.
     85  * 4. Neither the name of the University nor the names of its contributors
     86  *    may be used to endorse or promote products derived from this software
     87  *    without specific prior written permission.
     88  *
     89  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     90  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     91  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     92  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     93  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     94  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     95  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     96  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     97  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     98  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     99  * SUCH DAMAGE.
    100  *
    101  *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
    102  */
    103 
    104 #include "opt_ipsec.h"
    105 
    106 #include <sys/param.h>
    107 #include <sys/systm.h>
    108 #include <sys/malloc.h>
    109 #include <sys/mbuf.h>
    110 #include <sys/protosw.h>
    111 #include <sys/socket.h>
    112 #include <sys/time.h>
    113 #include <sys/kernel.h>
    114 #include <sys/proc.h>
    115 
    116 #include <vm/vm.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/icmp_var.h>
    129 
    130 #ifdef IPSEC
    131 #include <netinet6/ipsec.h>
    132 #include <netkey/key.h>
    133 #include <netkey/key_debug.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 
    149 #if 0
    150 static int	ip_next_mtu __P((int, int));
    151 #else
    152 /*static*/ int	ip_next_mtu __P((int, int));
    153 #endif
    154 
    155 extern	struct protosw inetsw[];
    156 
    157 static void icmp_mtudisc __P((struct icmp *));
    158 static void icmp_mtudisc_timeout __P((struct rtentry *, struct rttimer *));
    159 
    160 /*
    161  * Generate an error packet of type error
    162  * in response to bad packet ip.
    163  */
    164 void
    165 icmp_error(n, type, code, dest, destifp)
    166 	struct mbuf *n;
    167 	int type, code;
    168 	n_long dest;
    169 	struct ifnet *destifp;
    170 {
    171 	register struct ip *oip = mtod(n, struct ip *), *nip;
    172 	register unsigned oiplen = oip->ip_hl << 2;
    173 	register struct icmp *icp;
    174 	register struct mbuf *m;
    175 	unsigned icmplen;
    176 
    177 #ifdef ICMPPRINTFS
    178 	if (icmpprintfs)
    179 		printf("icmp_error(%x, %d, %d)\n", oip, type, code);
    180 #endif
    181 	if (type != ICMP_REDIRECT)
    182 		icmpstat.icps_error++;
    183 	/*
    184 	 * Don't send error if not the first fragment of message.
    185 	 * Don't error if the old packet protocol was ICMP
    186 	 * error message, only known informational types.
    187 	 */
    188 	if (oip->ip_off &~ (IP_MF|IP_DF))
    189 		goto freeit;
    190 	if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
    191 	  n->m_len >= oiplen + ICMP_MINLEN &&
    192 	  !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) {
    193 		icmpstat.icps_oldicmp++;
    194 		goto freeit;
    195 	}
    196 	/* Don't send error in response to a multicast or broadcast packet */
    197 	if (n->m_flags & (M_BCAST|M_MCAST))
    198 		goto freeit;
    199 	/*
    200 	 * First, formulate icmp message
    201 	 */
    202 	m = m_gethdr(M_DONTWAIT, MT_HEADER);
    203 	if (m == NULL)
    204 		goto freeit;
    205 	icmplen = oiplen + min(8, oip->ip_len - oiplen);
    206 	m->m_len = icmplen + ICMP_MINLEN;
    207 	MH_ALIGN(m, m->m_len);
    208 	icp = mtod(m, struct icmp *);
    209 	if ((u_int)type > ICMP_MAXTYPE)
    210 		panic("icmp_error");
    211 	icmpstat.icps_outhist[type]++;
    212 	icp->icmp_type = type;
    213 	if (type == ICMP_REDIRECT)
    214 		icp->icmp_gwaddr.s_addr = dest;
    215 	else {
    216 		icp->icmp_void = 0;
    217 		/*
    218 		 * The following assignments assume an overlay with the
    219 		 * zeroed icmp_void field.
    220 		 */
    221 		if (type == ICMP_PARAMPROB) {
    222 			icp->icmp_pptr = code;
    223 			code = 0;
    224 		} else if (type == ICMP_UNREACH &&
    225 		    code == ICMP_UNREACH_NEEDFRAG && destifp)
    226 			icp->icmp_nextmtu = htons(destifp->if_mtu);
    227 	}
    228 
    229 	HTONS(oip->ip_off);
    230 	HTONS(oip->ip_len);
    231 	icp->icmp_code = code;
    232 	bcopy((caddr_t)oip, (caddr_t)&icp->icmp_ip, icmplen);
    233 	nip = &icp->icmp_ip;
    234 
    235 	/*
    236 	 * Now, copy old ip header (without options)
    237 	 * in front of icmp message.
    238 	 */
    239 	if (m->m_data - sizeof(struct ip) < m->m_pktdat)
    240 		panic("icmp len");
    241 	m->m_data -= sizeof(struct ip);
    242 	m->m_len += sizeof(struct ip);
    243 	m->m_pkthdr.len = m->m_len;
    244 	m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
    245 	nip = mtod(m, struct ip *);
    246 	bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
    247 	nip->ip_len = m->m_len;
    248 	nip->ip_hl = sizeof(struct ip) >> 2;
    249 	nip->ip_p = IPPROTO_ICMP;
    250 	nip->ip_tos = 0;
    251 	icmp_reflect(m);
    252 
    253 freeit:
    254 	m_freem(n);
    255 }
    256 
    257 static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET };
    258 static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET };
    259 static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET };
    260 struct sockaddr_in icmpmask = { 8, 0 };
    261 
    262 /*
    263  * Process a received ICMP message.
    264  */
    265 void
    266 #if __STDC__
    267 icmp_input(struct mbuf *m, ...)
    268 #else
    269 icmp_input(m, va_alist)
    270 	struct mbuf *m;
    271 	va_dcl
    272 #endif
    273 {
    274 	int proto;
    275 	register struct icmp *icp;
    276 	register struct ip *ip = mtod(m, struct ip *);
    277 	int icmplen;
    278 	register int i;
    279 	struct in_ifaddr *ia;
    280 	void *(*ctlfunc) __P((int, struct sockaddr *, void *));
    281 	int code;
    282 	extern u_char ip_protox[];
    283 	int hlen;
    284 	va_list ap;
    285 
    286 	va_start(ap, m);
    287 	hlen = va_arg(ap, int);
    288 	proto = va_arg(ap, int);
    289 	va_end(ap);
    290 
    291 	/*
    292 	 * Locate icmp structure in mbuf, and check
    293 	 * that not corrupted and of at least minimum length.
    294 	 */
    295 	icmplen = ip->ip_len - hlen;
    296 #ifdef ICMPPRINTFS
    297 	if (icmpprintfs)
    298 		printf("icmp_input from %x to %x, len %d\n",
    299 		    ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr),
    300 		    icmplen);
    301 #endif
    302 	if (icmplen < ICMP_MINLEN) {
    303 		icmpstat.icps_tooshort++;
    304 		goto freeit;
    305 	}
    306 	i = hlen + min(icmplen, ICMP_ADVLENMIN);
    307 	if (m->m_len < i && (m = m_pullup(m, i)) == 0) {
    308 		icmpstat.icps_tooshort++;
    309 		return;
    310 	}
    311 	ip = mtod(m, struct ip *);
    312 	m->m_len -= hlen;
    313 	m->m_data += hlen;
    314 	icp = mtod(m, struct icmp *);
    315 	if (in_cksum(m, icmplen)) {
    316 		icmpstat.icps_checksum++;
    317 		goto freeit;
    318 	}
    319 	m->m_len += hlen;
    320 	m->m_data -= hlen;
    321 
    322 #ifdef ICMPPRINTFS
    323 	/*
    324 	 * Message type specific processing.
    325 	 */
    326 	if (icmpprintfs)
    327 		printf("icmp_input, type %d code %d\n", icp->icmp_type,
    328 		    icp->icmp_code);
    329 #endif
    330 #ifdef IPSEC
    331 	/* drop it if it does not match the policy */
    332 	if (ipsec4_in_reject(m, NULL)) {
    333 		ipsecstat.in_polvio++;
    334 		goto freeit;
    335 	}
    336 #endif
    337 	if (icp->icmp_type > ICMP_MAXTYPE)
    338 		goto raw;
    339 	icmpstat.icps_inhist[icp->icmp_type]++;
    340 	code = icp->icmp_code;
    341 	switch (icp->icmp_type) {
    342 
    343 	case ICMP_UNREACH:
    344 		switch (code) {
    345 			case ICMP_UNREACH_NET:
    346 			case ICMP_UNREACH_HOST:
    347 			case ICMP_UNREACH_PROTOCOL:
    348 			case ICMP_UNREACH_PORT:
    349 			case ICMP_UNREACH_SRCFAIL:
    350 				code += PRC_UNREACH_NET;
    351 				break;
    352 
    353 			case ICMP_UNREACH_NEEDFRAG:
    354 				code = PRC_MSGSIZE;
    355 				break;
    356 
    357 			case ICMP_UNREACH_NET_UNKNOWN:
    358 			case ICMP_UNREACH_NET_PROHIB:
    359 			case ICMP_UNREACH_TOSNET:
    360 				code = PRC_UNREACH_NET;
    361 				break;
    362 
    363 			case ICMP_UNREACH_HOST_UNKNOWN:
    364 			case ICMP_UNREACH_ISOLATED:
    365 			case ICMP_UNREACH_HOST_PROHIB:
    366 			case ICMP_UNREACH_TOSHOST:
    367 				code = PRC_UNREACH_HOST;
    368 				break;
    369 
    370 			default:
    371 				goto badcode;
    372 		}
    373 		goto deliver;
    374 
    375 	case ICMP_TIMXCEED:
    376 		if (code > 1)
    377 			goto badcode;
    378 		code += PRC_TIMXCEED_INTRANS;
    379 		goto deliver;
    380 
    381 	case ICMP_PARAMPROB:
    382 		if (code > 1)
    383 			goto badcode;
    384 		code = PRC_PARAMPROB;
    385 		goto deliver;
    386 
    387 	case ICMP_SOURCEQUENCH:
    388 		if (code)
    389 			goto badcode;
    390 		code = PRC_QUENCH;
    391 		goto deliver;
    392 
    393 	deliver:
    394 		/*
    395 		 * Problem with datagram; advise higher level routines.
    396 		 */
    397 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
    398 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
    399 			icmpstat.icps_badlen++;
    400 			goto freeit;
    401 		}
    402 		if (IN_MULTICAST(icp->icmp_ip.ip_dst.s_addr))
    403 			goto badcode;
    404 		NTOHS(icp->icmp_ip.ip_len);
    405 #ifdef ICMPPRINTFS
    406 		if (icmpprintfs)
    407 			printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
    408 #endif
    409 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
    410 		if (code == PRC_MSGSIZE && ip_mtudisc)
    411 			icmp_mtudisc(icp);
    412 		/*
    413 		 * XXX if the packet contains [IPv4 AH TCP], we can't make a
    414 		 * notification to TCP layer.
    415 		 */
    416 		ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
    417 		if (ctlfunc)
    418 			(*ctlfunc)(code, sintosa(&icmpsrc), &icp->icmp_ip);
    419 		break;
    420 
    421 	badcode:
    422 		icmpstat.icps_badcode++;
    423 		break;
    424 
    425 	case ICMP_ECHO:
    426 		icp->icmp_type = ICMP_ECHOREPLY;
    427 		goto reflect;
    428 
    429 	case ICMP_TSTAMP:
    430 		if (icmplen < ICMP_TSLEN) {
    431 			icmpstat.icps_badlen++;
    432 			break;
    433 		}
    434 		icp->icmp_type = ICMP_TSTAMPREPLY;
    435 		icp->icmp_rtime = iptime();
    436 		icp->icmp_ttime = icp->icmp_rtime;	/* bogus, do later! */
    437 		goto reflect;
    438 
    439 	case ICMP_MASKREQ:
    440 		if (icmpmaskrepl == 0)
    441 			break;
    442 		/*
    443 		 * We are not able to respond with all ones broadcast
    444 		 * unless we receive it over a point-to-point interface.
    445 		 */
    446 		if (icmplen < ICMP_MASKLEN) {
    447 			icmpstat.icps_badlen++;
    448 			break;
    449 		}
    450 		if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
    451 		    in_nullhost(ip->ip_dst))
    452 			icmpdst.sin_addr = ip->ip_src;
    453 		else
    454 			icmpdst.sin_addr = ip->ip_dst;
    455 		ia = ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst),
    456 		    m->m_pkthdr.rcvif));
    457 		if (ia == 0)
    458 			break;
    459 		icp->icmp_type = ICMP_MASKREPLY;
    460 		icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
    461 		if (in_nullhost(ip->ip_src)) {
    462 			if (ia->ia_ifp->if_flags & IFF_BROADCAST)
    463 				ip->ip_src = ia->ia_broadaddr.sin_addr;
    464 			else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
    465 				ip->ip_src = ia->ia_dstaddr.sin_addr;
    466 		}
    467 reflect:
    468 		icmpstat.icps_reflect++;
    469 		icmpstat.icps_outhist[icp->icmp_type]++;
    470 		icmp_reflect(m);
    471 		return;
    472 
    473 	case ICMP_REDIRECT:
    474 		if (code > 3)
    475 			goto badcode;
    476 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
    477 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
    478 			icmpstat.icps_badlen++;
    479 			break;
    480 		}
    481 		/*
    482 		 * Short circuit routing redirects to force
    483 		 * immediate change in the kernel's routing
    484 		 * tables.  The message is also handed to anyone
    485 		 * listening on a raw socket (e.g. the routing
    486 		 * daemon for use in updating its tables).
    487 		 */
    488 		icmpgw.sin_addr = ip->ip_src;
    489 		icmpdst.sin_addr = icp->icmp_gwaddr;
    490 #ifdef	ICMPPRINTFS
    491 		if (icmpprintfs)
    492 			printf("redirect dst %x to %x\n", icp->icmp_ip.ip_dst,
    493 			    icp->icmp_gwaddr);
    494 #endif
    495 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
    496 		rtredirect(sintosa(&icmpsrc), sintosa(&icmpdst),
    497 		    (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
    498 		    sintosa(&icmpgw), (struct rtentry **)0);
    499 		pfctlinput(PRC_REDIRECT_HOST, sintosa(&icmpsrc));
    500 #ifdef IPSEC
    501 		key_sa_routechange((struct sockaddr *)&icmpsrc);
    502 #endif
    503 		break;
    504 
    505 	/*
    506 	 * No kernel processing for the following;
    507 	 * just fall through to send to raw listener.
    508 	 */
    509 	case ICMP_ECHOREPLY:
    510 	case ICMP_ROUTERADVERT:
    511 	case ICMP_ROUTERSOLICIT:
    512 	case ICMP_TSTAMPREPLY:
    513 	case ICMP_IREQREPLY:
    514 	case ICMP_MASKREPLY:
    515 	default:
    516 		break;
    517 	}
    518 
    519 raw:
    520 	rip_input(m, hlen, proto);
    521 	return;
    522 
    523 freeit:
    524 	m_freem(m);
    525 	return;
    526 }
    527 
    528 /*
    529  * Reflect the ip packet back to the source
    530  */
    531 void
    532 icmp_reflect(m)
    533 	struct mbuf *m;
    534 {
    535 	register struct ip *ip = mtod(m, struct ip *);
    536 	register struct in_ifaddr *ia;
    537 	register struct ifaddr *ifa;
    538 	struct in_addr t;
    539 	struct mbuf *opts = 0;
    540 	int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
    541 
    542 	if (!in_canforward(ip->ip_src) &&
    543 	    ((ip->ip_src.s_addr & IN_CLASSA_NET) !=
    544 	     htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
    545 		m_freem(m);	/* Bad return address */
    546 		goto done;	/* ip_output() will check for broadcast */
    547 	}
    548 	t = ip->ip_dst;
    549 	ip->ip_dst = ip->ip_src;
    550 	/*
    551 	 * If the incoming packet was addressed directly to us,
    552 	 * use dst as the src for the reply.  Otherwise (broadcast
    553 	 * or anonymous), use the address which corresponds
    554 	 * to the incoming interface.
    555 	 */
    556 	INADDR_TO_IA(t, ia);
    557 	if (ia == NULL && (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST)) {
    558 		for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
    559 		    ifa != NULL; ifa = ifa->ifa_list.tqe_next) {
    560 			if (ifa->ifa_addr->sa_family != AF_INET)
    561 				continue;
    562 			ia = ifatoia(ifa);
    563 			if (in_hosteq(t, ia->ia_broadaddr.sin_addr))
    564 				break;
    565 		}
    566 	}
    567 
    568 	icmpdst.sin_addr = t;
    569 	if (ia == (struct in_ifaddr *)0)
    570 		ia = ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst),
    571 		    m->m_pkthdr.rcvif));
    572 	/*
    573 	 * The following happens if the packet was not addressed to us,
    574 	 * and was received on an interface with no IP address:
    575 	 * We find the first AF_INET address on the first non-loopback
    576 	 * interface.
    577 	 */
    578 	if (ia == (struct in_ifaddr *)0)
    579 		for (ia = in_ifaddr.tqh_first; ia != NULL;
    580 		    ia = ia->ia_list.tqe_next) {
    581 			if (ia->ia_ifp->if_flags & IFF_LOOPBACK)
    582 				continue;
    583 			break;
    584 		}
    585 	/*
    586 	 * If we still didn't find an address, punt.  We could have an
    587 	 * interface up (and receiving packets) with no address.
    588 	 */
    589 	if (ia == (struct in_ifaddr *)0) {
    590 		m_freem(m);
    591 		goto done;
    592 	}
    593 
    594 	t = ia->ia_addr.sin_addr;
    595 	ip->ip_src = t;
    596 	ip->ip_ttl = MAXTTL;
    597 
    598 	if (optlen > 0) {
    599 		register u_char *cp;
    600 		int opt, cnt;
    601 		u_int len;
    602 
    603 		/*
    604 		 * Retrieve any source routing from the incoming packet;
    605 		 * add on any record-route or timestamp options.
    606 		 */
    607 		cp = (u_char *) (ip + 1);
    608 		if ((opts = ip_srcroute()) == 0 &&
    609 		    (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
    610 			opts->m_len = sizeof(struct in_addr);
    611 			*mtod(opts, struct in_addr *) = zeroin_addr;
    612 		}
    613 		if (opts) {
    614 #ifdef ICMPPRINTFS
    615 		    if (icmpprintfs)
    616 			    printf("icmp_reflect optlen %d rt %d => ",
    617 				optlen, opts->m_len);
    618 #endif
    619 		    for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
    620 			    opt = cp[IPOPT_OPTVAL];
    621 			    if (opt == IPOPT_EOL)
    622 				    break;
    623 			    if (opt == IPOPT_NOP)
    624 				    len = 1;
    625 			    else {
    626 				    len = cp[IPOPT_OLEN];
    627 				    if (len <= 0 || len > cnt)
    628 					    break;
    629 			    }
    630 			    /*
    631 			     * Should check for overflow, but it "can't happen"
    632 			     */
    633 			    if (opt == IPOPT_RR || opt == IPOPT_TS ||
    634 				opt == IPOPT_SECURITY) {
    635 				    bcopy((caddr_t)cp,
    636 					mtod(opts, caddr_t) + opts->m_len, len);
    637 				    opts->m_len += len;
    638 			    }
    639 		    }
    640 		    /* Terminate & pad, if necessary */
    641 		    if ((cnt = opts->m_len % 4) != 0) {
    642 			    for (; cnt < 4; cnt++) {
    643 				    *(mtod(opts, caddr_t) + opts->m_len) =
    644 					IPOPT_EOL;
    645 				    opts->m_len++;
    646 			    }
    647 		    }
    648 #ifdef ICMPPRINTFS
    649 		    if (icmpprintfs)
    650 			    printf("%d\n", opts->m_len);
    651 #endif
    652 		}
    653 		/*
    654 		 * Now strip out original options by copying rest of first
    655 		 * mbuf's data back, and adjust the IP length.
    656 		 */
    657 		ip->ip_len -= optlen;
    658 		ip->ip_hl = sizeof(struct ip) >> 2;
    659 		m->m_len -= optlen;
    660 		if (m->m_flags & M_PKTHDR)
    661 			m->m_pkthdr.len -= optlen;
    662 		optlen += sizeof(struct ip);
    663 		bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
    664 			 (unsigned)(m->m_len - sizeof(struct ip)));
    665 	}
    666 	m->m_flags &= ~(M_BCAST|M_MCAST);
    667 	icmp_send(m, opts);
    668 done:
    669 	if (opts)
    670 		(void)m_free(opts);
    671 }
    672 
    673 /*
    674  * Send an icmp packet back to the ip level,
    675  * after supplying a checksum.
    676  */
    677 void
    678 icmp_send(m, opts)
    679 	register struct mbuf *m;
    680 	struct mbuf *opts;
    681 {
    682 	register struct ip *ip = mtod(m, struct ip *);
    683 	register int hlen;
    684 	register struct icmp *icp;
    685 
    686 	hlen = ip->ip_hl << 2;
    687 	m->m_data += hlen;
    688 	m->m_len -= hlen;
    689 	icp = mtod(m, struct icmp *);
    690 	icp->icmp_cksum = 0;
    691 	icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
    692 	m->m_data -= hlen;
    693 	m->m_len += hlen;
    694 #ifdef ICMPPRINTFS
    695 	if (icmpprintfs)
    696 		printf("icmp_send dst %x src %x\n", ip->ip_dst, ip->ip_src);
    697 #endif
    698 #ifdef IPSEC
    699 	m->m_pkthdr.rcvif = NULL;
    700 #endif /*IPSEC*/
    701 	(void) ip_output(m, opts, NULL, 0, NULL);
    702 }
    703 
    704 n_time
    705 iptime()
    706 {
    707 	struct timeval atv;
    708 	u_long t;
    709 
    710 	microtime(&atv);
    711 	t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
    712 	return (htonl(t));
    713 }
    714 
    715 int
    716 icmp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    717 	int *name;
    718 	u_int namelen;
    719 	void *oldp;
    720 	size_t *oldlenp;
    721 	void *newp;
    722 	size_t newlen;
    723 {
    724 
    725 	/* All sysctl names at this level are terminal. */
    726 	if (namelen != 1)
    727 		return (ENOTDIR);
    728 
    729 	switch (name[0]) {
    730 	case ICMPCTL_MASKREPL:
    731 		return (sysctl_int(oldp, oldlenp, newp, newlen, &icmpmaskrepl));
    732 	default:
    733 		return (ENOPROTOOPT);
    734 	}
    735 	/* NOTREACHED */
    736 }
    737 
    738 static void
    739 icmp_mtudisc(icp)
    740 	struct icmp *icp;
    741 {
    742 	struct rtentry *rt;
    743 	struct sockaddr *dst = sintosa(&icmpsrc);
    744 	u_long mtu = ntohs(icp->icmp_nextmtu);  /* Why a long?  IPv6 */
    745 	int    error;
    746 
    747 	/* Table of common MTUs: */
    748 
    749 	static u_long mtu_table[] = {65535, 65280, 32000, 17914, 9180, 8166,
    750 				     4352, 2002, 1492, 1006, 508, 296, 68, 0};
    751 
    752 	rt = rtalloc1(dst, 1);
    753 	if (rt == 0)
    754 		return;
    755 
    756 	/* If we didn't get a host route, allocate one */
    757 
    758 	if ((rt->rt_flags & RTF_HOST) == 0) {
    759 		struct rtentry *nrt;
    760 
    761 		error = rtrequest((int) RTM_ADD, dst,
    762 		    (struct sockaddr *) rt->rt_gateway,
    763 		    (struct sockaddr *) 0,
    764 		    RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
    765 		if (error) {
    766 			rtfree(rt);
    767 			rtfree(nrt);
    768 			return;
    769 		}
    770 		nrt->rt_rmx = rt->rt_rmx;
    771 		rtfree(rt);
    772 		rt = nrt;
    773 	}
    774 	error = rt_timer_add(rt, icmp_mtudisc_timeout, ip_mtudisc_timeout_q);
    775 	if (error) {
    776 		rtfree(rt);
    777 		return;
    778 	}
    779 
    780 	if (mtu == 0) {
    781 		int i = 0;
    782 
    783 		mtu = icp->icmp_ip.ip_len; /* NTOHS happened in deliver: */
    784 		/* Some 4.2BSD-based routers incorrectly adjust the ip_len */
    785 		if (mtu > rt->rt_rmx.rmx_mtu && rt->rt_rmx.rmx_mtu != 0)
    786 			mtu -= (icp->icmp_ip.ip_hl << 2);
    787 
    788 		/* If we still can't guess a value, try the route */
    789 
    790 		if (mtu == 0) {
    791 			mtu = rt->rt_rmx.rmx_mtu;
    792 
    793 			/* If no route mtu, default to the interface mtu */
    794 
    795 			if (mtu == 0)
    796 				mtu = rt->rt_ifp->if_mtu;
    797 		}
    798 
    799 		for (i = 0; i < sizeof(mtu_table) / sizeof(mtu_table[0]); i++)
    800 			if (mtu > mtu_table[i]) {
    801 				mtu = mtu_table[i];
    802 				break;
    803 			}
    804 	}
    805 
    806 	/*
    807 	 * XXX:   RTV_MTU is overloaded, since the admin can set it
    808 	 *	  to turn off PMTU for a route, and the kernel can
    809 	 *	  set it to indicate a serious problem with PMTU
    810 	 *	  on a route.  We should be using a separate flag
    811 	 *	  for the kernel to indicate this.
    812 	 */
    813 
    814 	if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
    815 		if (mtu < 296 || mtu > rt->rt_ifp->if_mtu)
    816 			rt->rt_rmx.rmx_locks |= RTV_MTU;
    817 		else if (rt->rt_rmx.rmx_mtu > mtu ||
    818 			 rt->rt_rmx.rmx_mtu == 0)
    819 			rt->rt_rmx.rmx_mtu = mtu;
    820 	}
    821 
    822 	if (rt)
    823 		rtfree(rt);
    824 }
    825 
    826 /*
    827  * Return the next larger or smaller MTU plateau (table from RFC 1191)
    828  * given current value MTU.  If DIR is less than zero, a larger plateau
    829  * is returned; otherwise, a smaller value is returned.
    830  */
    831 int
    832 ip_next_mtu(mtu, dir)	/* XXX */
    833 	int mtu;
    834 	int dir;
    835 {
    836 	static int mtutab[] = {
    837 		65535, 32000, 17914, 8166, 4352, 2002, 1492, 1006, 508, 296,
    838 		68, 0
    839 	};
    840 	int i;
    841 
    842 	for (i = 0; i < (sizeof mtutab) / (sizeof mtutab[0]); i++) {
    843 		if (mtu >= mtutab[i])
    844 			break;
    845 	}
    846 
    847 	if (dir < 0) {
    848 		if (i == 0) {
    849 			return 0;
    850 		} else {
    851 			return mtutab[i - 1];
    852 		}
    853 	} else {
    854 		if (mtutab[i] == 0) {
    855 			return 0;
    856 		} else if(mtu > mtutab[i]) {
    857 			return mtutab[i];
    858 		} else {
    859 			return mtutab[i + 1];
    860 		}
    861 	}
    862 }
    863 
    864 
    865 static void
    866 icmp_mtudisc_timeout(rt, r)
    867 	struct rtentry *rt;
    868 	struct rttimer *r;
    869 {
    870 	if (rt == NULL)
    871 		panic("icmp_mtudisc_timeout:  bad route to timeout");
    872 	if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
    873 	    (RTF_DYNAMIC | RTF_HOST)) {
    874 		rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
    875 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
    876 	} else {
    877 		if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
    878 			rt->rt_rmx.rmx_mtu = 0;
    879 		}
    880 	}
    881 }
    882