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