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