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