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