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