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