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