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