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