xref: /freebsd/sys/netinet/ip_icmp.c (revision ef5d438ed4bc17ad7ece3e40fe4d1f9baf3aadf7)
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.16 1995/12/14 09:53:40 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 <sys/sysctl.h>
47 
48 #include <net/if.h>
49 #include <net/route.h>
50 
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/in_var.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_icmp.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/icmp_var.h>
58 
59 /*
60  * ICMP routines: error generation, receive packet processing, and
61  * routines to turnaround packets back to the originator, and
62  * host table maintenance routines.
63  */
64 
65 static struct	icmpstat icmpstat;
66 SYSCTL_STRUCT(_net_inet_icmp, ICMPCTL_STATS, stats, CTLFLAG_RD,
67 	&icmpstat, icmpstat, "");
68 
69 static int	icmpmaskrepl = 0;
70 SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_RW,
71 	&icmpmaskrepl, 0, "");
72 
73 #ifdef ICMPPRINTFS
74 int	icmpprintfs = 0;
75 #endif
76 
77 static void	icmp_reflect __P((struct mbuf *));
78 static void	icmp_send __P((struct mbuf *, struct mbuf *));
79 static int	ip_next_mtu __P((int, int));
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 *, void *));
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 #if 1
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 #ifdef DEBUG_MTUDISC
341 				printf("MTU for %s reduced to %d\n",
342 					inet_ntoa(icmpsrc.sin_addr), mtu);
343 #endif
344 				if (mtu < 296) {
345 					/* rt->rt_rmx.rmx_mtu =
346 						rt->rt_ifp->if_mtu; */
347 					rt->rt_rmx.rmx_locks |= RTV_MTU;
348 				} else if (rt->rt_rmx.rmx_mtu > mtu) {
349 					rt->rt_rmx.rmx_mtu = mtu;
350 				}
351 			}
352 			if (rt)
353 				RTFREE(rt);
354 		}
355 
356 #endif
357 		ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
358 		if (ctlfunc)
359 			(*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
360 				   (void *)&icp->icmp_ip);
361 		break;
362 
363 	badcode:
364 		icmpstat.icps_badcode++;
365 		break;
366 
367 	case ICMP_ECHO:
368 		icp->icmp_type = ICMP_ECHOREPLY;
369 		goto reflect;
370 
371 	case ICMP_TSTAMP:
372 		if (icmplen < ICMP_TSLEN) {
373 			icmpstat.icps_badlen++;
374 			break;
375 		}
376 		icp->icmp_type = ICMP_TSTAMPREPLY;
377 		icp->icmp_rtime = iptime();
378 		icp->icmp_ttime = icp->icmp_rtime;	/* bogus, do later! */
379 		goto reflect;
380 
381 	case ICMP_MASKREQ:
382 #define	satosin(sa)	((struct sockaddr_in *)(sa))
383 		if (icmpmaskrepl == 0)
384 			break;
385 		/*
386 		 * We are not able to respond with all ones broadcast
387 		 * unless we receive it over a point-to-point interface.
388 		 */
389 		if (icmplen < ICMP_MASKLEN)
390 			break;
391 		switch (ip->ip_dst.s_addr) {
392 
393 		case INADDR_BROADCAST:
394 		case INADDR_ANY:
395 			icmpdst.sin_addr = ip->ip_src;
396 			break;
397 
398 		default:
399 			icmpdst.sin_addr = ip->ip_dst;
400 		}
401 		ia = (struct in_ifaddr *)ifaof_ifpforaddr(
402 			    (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
403 		if (ia == 0)
404 			break;
405 		icp->icmp_type = ICMP_MASKREPLY;
406 		icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
407 		if (ip->ip_src.s_addr == 0) {
408 			if (ia->ia_ifp->if_flags & IFF_BROADCAST)
409 			    ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
410 			else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
411 			    ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
412 		}
413 reflect:
414 		ip->ip_len += hlen;	/* since ip_input deducts this */
415 		icmpstat.icps_reflect++;
416 		icmpstat.icps_outhist[icp->icmp_type]++;
417 		icmp_reflect(m);
418 		return;
419 
420 	case ICMP_REDIRECT:
421 		if (code > 3)
422 			goto badcode;
423 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
424 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
425 			icmpstat.icps_badlen++;
426 			break;
427 		}
428 		/*
429 		 * Short circuit routing redirects to force
430 		 * immediate change in the kernel's routing
431 		 * tables.  The message is also handed to anyone
432 		 * listening on a raw socket (e.g. the routing
433 		 * daemon for use in updating its tables).
434 		 */
435 		icmpgw.sin_addr = ip->ip_src;
436 		icmpdst.sin_addr = icp->icmp_gwaddr;
437 #ifdef	ICMPPRINTFS
438 		if (icmpprintfs) {
439 			char buf[4 * sizeof "123"];
440 			strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst));
441 
442 			printf("redirect dst %s to %s\n",
443 			       buf, inet_ntoa(icp->icmp_gwaddr));
444 		}
445 #endif
446 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
447 		rtredirect((struct sockaddr *)&icmpsrc,
448 		  (struct sockaddr *)&icmpdst,
449 		  (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
450 		  (struct sockaddr *)&icmpgw, (struct rtentry **)0);
451 		pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
452 		break;
453 
454 	/*
455 	 * No kernel processing for the following;
456 	 * just fall through to send to raw listener.
457 	 */
458 	case ICMP_ECHOREPLY:
459 	case ICMP_ROUTERADVERT:
460 	case ICMP_ROUTERSOLICIT:
461 	case ICMP_TSTAMPREPLY:
462 	case ICMP_IREQREPLY:
463 	case ICMP_MASKREPLY:
464 	default:
465 		break;
466 	}
467 
468 raw:
469 	rip_input(m);
470 	return;
471 
472 freeit:
473 	m_freem(m);
474 }
475 
476 /*
477  * Reflect the ip packet back to the source
478  */
479 static void
480 icmp_reflect(m)
481 	struct mbuf *m;
482 {
483 	register struct ip *ip = mtod(m, struct ip *);
484 	register struct in_ifaddr *ia;
485 	struct in_addr t;
486 	struct mbuf *opts = 0;
487 	int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
488 
489 	if (!in_canforward(ip->ip_src) &&
490 	    ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) !=
491 	     (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
492 		m_freem(m);	/* Bad return address */
493 		goto done;	/* Ip_output() will check for broadcast */
494 	}
495 	t = ip->ip_dst;
496 	ip->ip_dst = ip->ip_src;
497 	/*
498 	 * If the incoming packet was addressed directly to us,
499 	 * use dst as the src for the reply.  Otherwise (broadcast
500 	 * or anonymous), use the address which corresponds
501 	 * to the incoming interface.
502 	 */
503 	for (ia = in_ifaddr; ia; ia = ia->ia_next) {
504 		if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr)
505 			break;
506 		if ((ia->ia_ifp->if_flags & IFF_BROADCAST) &&
507 		    t.s_addr == satosin(&ia->ia_broadaddr)->sin_addr.s_addr)
508 			break;
509 	}
510 	icmpdst.sin_addr = t;
511 	if (ia == (struct in_ifaddr *)0)
512 		ia = (struct in_ifaddr *)ifaof_ifpforaddr(
513 			(struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
514 	/*
515 	 * The following happens if the packet was not addressed to us,
516 	 * and was received on an interface with no IP address.
517 	 */
518 	if (ia == (struct in_ifaddr *)0)
519 		ia = in_ifaddr;
520 	t = IA_SIN(ia)->sin_addr;
521 	ip->ip_src = t;
522 	ip->ip_ttl = MAXTTL;
523 
524 	if (optlen > 0) {
525 		register u_char *cp;
526 		int opt, cnt;
527 		u_int len;
528 
529 		/*
530 		 * Retrieve any source routing from the incoming packet;
531 		 * add on any record-route or timestamp options.
532 		 */
533 		cp = (u_char *) (ip + 1);
534 		if ((opts = ip_srcroute()) == 0 &&
535 		    (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
536 			opts->m_len = sizeof(struct in_addr);
537 			mtod(opts, struct in_addr *)->s_addr = 0;
538 		}
539 		if (opts) {
540 #ifdef ICMPPRINTFS
541 		    if (icmpprintfs)
542 			    printf("icmp_reflect optlen %d rt %d => ",
543 				optlen, opts->m_len);
544 #endif
545 		    for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
546 			    opt = cp[IPOPT_OPTVAL];
547 			    if (opt == IPOPT_EOL)
548 				    break;
549 			    if (opt == IPOPT_NOP)
550 				    len = 1;
551 			    else {
552 				    len = cp[IPOPT_OLEN];
553 				    if (len <= 0 || len > cnt)
554 					    break;
555 			    }
556 			    /*
557 			     * Should check for overflow, but it "can't happen"
558 			     */
559 			    if (opt == IPOPT_RR || opt == IPOPT_TS ||
560 				opt == IPOPT_SECURITY) {
561 				    bcopy((caddr_t)cp,
562 					mtod(opts, caddr_t) + opts->m_len, len);
563 				    opts->m_len += len;
564 			    }
565 		    }
566 		    /* Terminate & pad, if necessary */
567 		    cnt = opts->m_len % 4;
568 		    if (cnt) {
569 			    for (; cnt < 4; cnt++) {
570 				    *(mtod(opts, caddr_t) + opts->m_len) =
571 					IPOPT_EOL;
572 				    opts->m_len++;
573 			    }
574 		    }
575 #ifdef ICMPPRINTFS
576 		    if (icmpprintfs)
577 			    printf("%d\n", opts->m_len);
578 #endif
579 		}
580 		/*
581 		 * Now strip out original options by copying rest of first
582 		 * mbuf's data back, and adjust the IP length.
583 		 */
584 		ip->ip_len -= optlen;
585 		ip->ip_hl = sizeof(struct ip) >> 2;
586 		m->m_len -= optlen;
587 		if (m->m_flags & M_PKTHDR)
588 			m->m_pkthdr.len -= optlen;
589 		optlen += sizeof(struct ip);
590 		bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
591 			 (unsigned)(m->m_len - sizeof(struct ip)));
592 	}
593 	m->m_flags &= ~(M_BCAST|M_MCAST);
594 	icmp_send(m, opts);
595 done:
596 	if (opts)
597 		(void)m_free(opts);
598 }
599 
600 /*
601  * Send an icmp packet back to the ip level,
602  * after supplying a checksum.
603  */
604 static void
605 icmp_send(m, opts)
606 	register struct mbuf *m;
607 	struct mbuf *opts;
608 {
609 	register struct ip *ip = mtod(m, struct ip *);
610 	register int hlen;
611 	register struct icmp *icp;
612 
613 	hlen = ip->ip_hl << 2;
614 	m->m_data += hlen;
615 	m->m_len -= hlen;
616 	icp = mtod(m, struct icmp *);
617 	icp->icmp_cksum = 0;
618 	icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
619 	m->m_data -= hlen;
620 	m->m_len += hlen;
621 #ifdef ICMPPRINTFS
622 	if (icmpprintfs) {
623 		char buf[4 * sizeof "123"];
624 		strcpy(buf, inet_ntoa(ip->ip_dst));
625 		printf("icmp_send dst %s src %s\n",
626 		       buf, inet_ntoa(ip->ip_src));
627 	}
628 #endif
629 	(void) ip_output(m, opts, NULL, 0, NULL);
630 }
631 
632 n_time
633 iptime()
634 {
635 	struct timeval atv;
636 	u_long t;
637 
638 	microtime(&atv);
639 	t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
640 	return (htonl(t));
641 }
642 
643 #if 1
644 /*
645  * Return the next larger or smaller MTU plateau (table from RFC 1191)
646  * given current value MTU.  If DIR is less than zero, a larger plateau
647  * is returned; otherwise, a smaller value is returned.
648  */
649 static int
650 ip_next_mtu(mtu, dir)
651 	int mtu;
652 	int dir;
653 {
654 	static int mtutab[] = {
655 		65535, 32000, 17914, 8166, 4352, 2002, 1492, 1006, 508, 296,
656 		68, 0
657 	};
658 	int i;
659 
660 	for (i = 0; i < (sizeof mtutab) / (sizeof mtutab[0]); i++) {
661 		if (mtu >= mtutab[i])
662 			break;
663 	}
664 
665 	if (dir < 0) {
666 		if (i == 0) {
667 			return 0;
668 		} else {
669 			return mtutab[i - 1];
670 		}
671 	} else {
672 		if (mtutab[i] == 0) {
673 			return 0;
674 		} else if(mtu > mtutab[i]) {
675 			return mtutab[i];
676 		} else {
677 			return mtutab[i + 1];
678 		}
679 	}
680 }
681 #endif
682