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