xref: /netbsd-src/sys/netinet/ip_icmp.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: ip_icmp.c,v 1.124 2010/07/02 07:02:00 kefren Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
34  * All rights reserved.
35  *
36  * This code is derived from software contributed to The NetBSD Foundation
37  * by Public Access Networks Corporation ("Panix").  It was developed under
38  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39  *
40  * This code is derived from software contributed to The NetBSD Foundation
41  * by Jason R. Thorpe of Zembu Labs, Inc.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
53  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
54  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
56  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62  * POSSIBILITY OF SUCH DAMAGE.
63  */
64 
65 /*
66  * Copyright (c) 1982, 1986, 1988, 1993
67  *	The Regents of the University of California.  All rights reserved.
68  *
69  * Redistribution and use in source and binary forms, with or without
70  * modification, are permitted provided that the following conditions
71  * are met:
72  * 1. Redistributions of source code must retain the above copyright
73  *    notice, this list of conditions and the following disclaimer.
74  * 2. Redistributions in binary form must reproduce the above copyright
75  *    notice, this list of conditions and the following disclaimer in the
76  *    documentation and/or other materials provided with the distribution.
77  * 3. Neither the name of the University nor the names of its contributors
78  *    may be used to endorse or promote products derived from this software
79  *    without specific prior written permission.
80  *
81  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
82  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
85  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
91  * SUCH DAMAGE.
92  *
93  *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
94  */
95 
96 #include <sys/cdefs.h>
97 __KERNEL_RCSID(0, "$NetBSD: ip_icmp.c,v 1.124 2010/07/02 07:02:00 kefren Exp $");
98 
99 #include "opt_ipsec.h"
100 
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/malloc.h>
104 #include <sys/mbuf.h>
105 #include <sys/protosw.h>
106 #include <sys/socket.h>
107 #include <sys/time.h>
108 #include <sys/kernel.h>
109 #include <sys/syslog.h>
110 #include <sys/sysctl.h>
111 
112 #include <net/if.h>
113 #include <net/route.h>
114 
115 #include <netinet/in.h>
116 #include <netinet/in_systm.h>
117 #include <netinet/in_var.h>
118 #include <netinet/ip.h>
119 #include <netinet/ip_icmp.h>
120 #include <netinet/ip_var.h>
121 #include <netinet/in_pcb.h>
122 #include <netinet/in_proto.h>
123 #include <netinet/icmp_var.h>
124 #include <netinet/icmp_private.h>
125 
126 #ifdef IPSEC
127 #include <netinet6/ipsec.h>
128 #include <netkey/key.h>
129 #endif
130 
131 #ifdef FAST_IPSEC
132 #include <netipsec/ipsec.h>
133 #include <netipsec/key.h>
134 #endif	/* FAST_IPSEC*/
135 
136 #include <machine/stdarg.h>
137 
138 /*
139  * ICMP routines: error generation, receive packet processing, and
140  * routines to turnaround packets back to the originator, and
141  * host table maintenance routines.
142  */
143 
144 int	icmpmaskrepl = 0;
145 int	icmpbmcastecho = 0;
146 #ifdef ICMPPRINTFS
147 int	icmpprintfs = 0;
148 #endif
149 int	icmpreturndatabytes = 8;
150 
151 percpu_t *icmpstat_percpu;
152 
153 /*
154  * List of callbacks to notify when Path MTU changes are made.
155  */
156 struct icmp_mtudisc_callback {
157 	LIST_ENTRY(icmp_mtudisc_callback) mc_list;
158 	void (*mc_func)(struct in_addr);
159 };
160 
161 LIST_HEAD(, icmp_mtudisc_callback) icmp_mtudisc_callbacks =
162     LIST_HEAD_INITIALIZER(&icmp_mtudisc_callbacks);
163 
164 #if 0
165 static u_int	ip_next_mtu(u_int, int);
166 #else
167 /*static*/ u_int	ip_next_mtu(u_int, int);
168 #endif
169 
170 extern int icmperrppslim;
171 static int icmperrpps_count = 0;
172 static struct timeval icmperrppslim_last;
173 static int icmp_rediraccept = 1;
174 static int icmp_redirtimeout = 600;
175 static struct rttimer_queue *icmp_redirect_timeout_q = NULL;
176 
177 static void icmp_mtudisc_timeout(struct rtentry *, struct rttimer *);
178 static void icmp_redirect_timeout(struct rtentry *, struct rttimer *);
179 
180 static void sysctl_netinet_icmp_setup(struct sysctllog **);
181 
182 void
183 icmp_init(void)
184 {
185 
186 	sysctl_netinet_icmp_setup(NULL);
187 
188 	/*
189 	 * This is only useful if the user initializes redirtimeout to
190 	 * something other than zero.
191 	 */
192 	if (icmp_redirtimeout != 0) {
193 		icmp_redirect_timeout_q =
194 			rt_timer_queue_create(icmp_redirtimeout);
195 	}
196 
197 	icmpstat_percpu = percpu_alloc(sizeof(uint64_t) * ICMP_NSTATS);
198 }
199 
200 /*
201  * Register a Path MTU Discovery callback.
202  */
203 void
204 icmp_mtudisc_callback_register(void (*func)(struct in_addr))
205 {
206 	struct icmp_mtudisc_callback *mc;
207 
208 	for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL;
209 	     mc = LIST_NEXT(mc, mc_list)) {
210 		if (mc->mc_func == func)
211 			return;
212 	}
213 
214 	mc = malloc(sizeof(*mc), M_PCB, M_NOWAIT);
215 	if (mc == NULL)
216 		panic("icmp_mtudisc_callback_register");
217 
218 	mc->mc_func = func;
219 	LIST_INSERT_HEAD(&icmp_mtudisc_callbacks, mc, mc_list);
220 }
221 
222 /*
223  * Generate an error packet of type error
224  * in response to bad packet ip.
225  */
226 void
227 icmp_error(struct mbuf *n, int type, int code, n_long dest,
228     int destmtu)
229 {
230 	struct ip *oip = mtod(n, struct ip *), *nip;
231 	unsigned oiplen = oip->ip_hl << 2;
232 	struct icmp *icp;
233 	struct mbuf *m;
234 	struct m_tag *mtag;
235 	unsigned icmplen, mblen;
236 
237 #ifdef ICMPPRINTFS
238 	if (icmpprintfs)
239 		printf("icmp_error(%p, type:%d, code:%d)\n", oip, type, code);
240 #endif
241 	if (type != ICMP_REDIRECT)
242 		ICMP_STATINC(ICMP_STAT_ERROR);
243 	/*
244 	 * Don't send error if the original packet was encrypted.
245 	 * Don't send error if not the first fragment of message.
246 	 * Don't error if the old packet protocol was ICMP
247 	 * error message, only known informational types.
248 	 */
249 	if (n->m_flags & M_DECRYPTED)
250 		goto freeit;
251 	if (oip->ip_off &~ htons(IP_MF|IP_DF))
252 		goto freeit;
253 	if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
254 	  n->m_len >= oiplen + ICMP_MINLEN &&
255 	  !ICMP_INFOTYPE(((struct icmp *)((char *)oip + oiplen))->icmp_type)) {
256 		ICMP_STATINC(ICMP_STAT_OLDICMP);
257 		goto freeit;
258 	}
259 	/* Don't send error in response to a multicast or broadcast packet */
260 	if (n->m_flags & (M_BCAST|M_MCAST))
261 		goto freeit;
262 
263 	/*
264 	 * First, do a rate limitation check.
265 	 */
266 	if (icmp_ratelimit(&oip->ip_src, type, code)) {
267 		/* XXX stat */
268 		goto freeit;
269 	}
270 
271 	/*
272 	 * Now, formulate icmp message
273 	 */
274 	icmplen = oiplen + min(icmpreturndatabytes,
275 	    ntohs(oip->ip_len) - oiplen);
276 	/*
277 	 * Defend against mbuf chains shorter than oip->ip_len - oiplen:
278 	 */
279 	mblen = 0;
280 	for (m = n; m && (mblen < icmplen); m = m->m_next)
281 		mblen += m->m_len;
282 	icmplen = min(mblen, icmplen);
283 
284 	/*
285 	 * As we are not required to return everything we have,
286 	 * we return whatever we can return at ease.
287 	 *
288 	 * Note that ICMP datagrams longer than 576 octets are out of spec
289 	 * according to RFC1812; the limit on icmpreturndatabytes below in
290 	 * icmp_sysctl will keep things below that limit.
291 	 */
292 
293 	KASSERT(ICMP_MINLEN <= MCLBYTES);
294 
295 	if (icmplen + ICMP_MINLEN > MCLBYTES)
296 		icmplen = MCLBYTES - ICMP_MINLEN;
297 
298 	m = m_gethdr(M_DONTWAIT, MT_HEADER);
299 	if (m && (icmplen + ICMP_MINLEN > MHLEN)) {
300 		MCLGET(m, M_DONTWAIT);
301 		if ((m->m_flags & M_EXT) == 0) {
302 			m_freem(m);
303 			m = NULL;
304 		}
305 	}
306 	if (m == NULL)
307 		goto freeit;
308 	MCLAIM(m, n->m_owner);
309 	m->m_len = icmplen + ICMP_MINLEN;
310 	if ((m->m_flags & M_EXT) == 0)
311 		MH_ALIGN(m, m->m_len);
312 	else {
313 		m->m_data += sizeof(struct ip);
314 		m->m_len -= sizeof(struct ip);
315 	}
316 	icp = mtod(m, struct icmp *);
317 	if ((u_int)type > ICMP_MAXTYPE)
318 		panic("icmp_error");
319 	ICMP_STATINC(ICMP_STAT_OUTHIST + type);
320 	icp->icmp_type = type;
321 	if (type == ICMP_REDIRECT)
322 		icp->icmp_gwaddr.s_addr = dest;
323 	else {
324 		icp->icmp_void = 0;
325 		/*
326 		 * The following assignments assume an overlay with the
327 		 * zeroed icmp_void field.
328 		 */
329 		if (type == ICMP_PARAMPROB) {
330 			icp->icmp_pptr = code;
331 			code = 0;
332 		} else if (type == ICMP_UNREACH &&
333 		    code == ICMP_UNREACH_NEEDFRAG && destmtu)
334 			icp->icmp_nextmtu = htons(destmtu);
335 	}
336 
337 	icp->icmp_code = code;
338 	m_copydata(n, 0, icmplen, (void *)&icp->icmp_ip);
339 
340 	/*
341 	 * Now, copy old ip header (without options)
342 	 * in front of icmp message.
343 	 */
344 	if ((m->m_flags & M_EXT) == 0 &&
345 	    m->m_data - sizeof(struct ip) < m->m_pktdat)
346 		panic("icmp len");
347 	m->m_data -= sizeof(struct ip);
348 	m->m_len += sizeof(struct ip);
349 	m->m_pkthdr.len = m->m_len;
350 	m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
351 	nip = mtod(m, struct ip *);
352 	/* ip_v set in ip_output */
353 	nip->ip_hl = sizeof(struct ip) >> 2;
354 	nip->ip_tos = 0;
355 	nip->ip_len = htons(m->m_len);
356 	/* ip_id set in ip_output */
357 	nip->ip_off = htons(0);
358 	/* ip_ttl set in icmp_reflect */
359 	nip->ip_p = IPPROTO_ICMP;
360 	nip->ip_src = oip->ip_src;
361 	nip->ip_dst = oip->ip_dst;
362 	/* move PF m_tag to new packet, if it exists */
363 	mtag = m_tag_find(n, PACKET_TAG_PF, NULL);
364 	if (mtag != NULL) {
365 		m_tag_unlink(n, mtag);
366 		m_tag_prepend(m, mtag);
367 	}
368 	icmp_reflect(m);
369 
370 freeit:
371 	m_freem(n);
372 }
373 
374 struct sockaddr_in icmpsrc = {
375 	.sin_len = sizeof (struct sockaddr_in),
376 	.sin_family = AF_INET,
377 };
378 static struct sockaddr_in icmpdst = {
379 	.sin_len = sizeof (struct sockaddr_in),
380 	.sin_family = AF_INET,
381 };
382 static struct sockaddr_in icmpgw = {
383 	.sin_len = sizeof (struct sockaddr_in),
384 	.sin_family = AF_INET,
385 };
386 struct sockaddr_in icmpmask = {
387 	.sin_len = 8,
388 	.sin_family = 0,
389 };
390 
391 /*
392  * Process a received ICMP message.
393  */
394 void
395 icmp_input(struct mbuf *m, ...)
396 {
397 	int proto;
398 	struct icmp *icp;
399 	struct ip *ip = mtod(m, struct ip *);
400 	int icmplen;
401 	int i;
402 	struct in_ifaddr *ia;
403 	void *(*ctlfunc)(int, const struct sockaddr *, void *);
404 	int code;
405 	int hlen;
406 	va_list ap;
407 	struct rtentry *rt;
408 
409 	va_start(ap, m);
410 	hlen = va_arg(ap, int);
411 	proto = va_arg(ap, int);
412 	va_end(ap);
413 
414 	/*
415 	 * Locate icmp structure in mbuf, and check
416 	 * that not corrupted and of at least minimum length.
417 	 */
418 	icmplen = ntohs(ip->ip_len) - hlen;
419 #ifdef ICMPPRINTFS
420 	if (icmpprintfs) {
421 		printf("icmp_input from `%s' to ", inet_ntoa(ip->ip_src));
422 		printf("`%s', len %d\n", inet_ntoa(ip->ip_dst), icmplen);
423 	}
424 #endif
425 	if (icmplen < ICMP_MINLEN) {
426 		ICMP_STATINC(ICMP_STAT_TOOSHORT);
427 		goto freeit;
428 	}
429 	i = hlen + min(icmplen, ICMP_ADVLENMIN);
430 	if ((m->m_len < i || M_READONLY(m)) && (m = m_pullup(m, i)) == 0) {
431 		ICMP_STATINC(ICMP_STAT_TOOSHORT);
432 		return;
433 	}
434 	ip = mtod(m, struct ip *);
435 	m->m_len -= hlen;
436 	m->m_data += hlen;
437 	icp = mtod(m, struct icmp *);
438 	/* Don't need to assert alignment, here. */
439 	if (in_cksum(m, icmplen)) {
440 		ICMP_STATINC(ICMP_STAT_CHECKSUM);
441 		goto freeit;
442 	}
443 	m->m_len += hlen;
444 	m->m_data -= hlen;
445 
446 #ifdef ICMPPRINTFS
447 	/*
448 	 * Message type specific processing.
449 	 */
450 	if (icmpprintfs)
451 		printf("icmp_input(type:%d, code:%d)\n", icp->icmp_type,
452 		    icp->icmp_code);
453 #endif
454 	if (icp->icmp_type > ICMP_MAXTYPE)
455 		goto raw;
456 	ICMP_STATINC(ICMP_STAT_INHIST + icp->icmp_type);
457 	code = icp->icmp_code;
458 	switch (icp->icmp_type) {
459 
460 	case ICMP_UNREACH:
461 		switch (code) {
462 			case ICMP_UNREACH_NET:
463 				code = PRC_UNREACH_NET;
464 				break;
465 
466 			case ICMP_UNREACH_HOST:
467 				code = PRC_UNREACH_HOST;
468 				break;
469 
470 			case ICMP_UNREACH_PROTOCOL:
471 				code = PRC_UNREACH_PROTOCOL;
472 				break;
473 
474 			case ICMP_UNREACH_PORT:
475 				code = PRC_UNREACH_PORT;
476 				break;
477 
478 			case ICMP_UNREACH_SRCFAIL:
479 				code = PRC_UNREACH_SRCFAIL;
480 				break;
481 
482 			case ICMP_UNREACH_NEEDFRAG:
483 				code = PRC_MSGSIZE;
484 				break;
485 
486 			case ICMP_UNREACH_NET_UNKNOWN:
487 			case ICMP_UNREACH_NET_PROHIB:
488 			case ICMP_UNREACH_TOSNET:
489 				code = PRC_UNREACH_NET;
490 				break;
491 
492 			case ICMP_UNREACH_HOST_UNKNOWN:
493 			case ICMP_UNREACH_ISOLATED:
494 			case ICMP_UNREACH_HOST_PROHIB:
495 			case ICMP_UNREACH_TOSHOST:
496 				code = PRC_UNREACH_HOST;
497 				break;
498 
499 			default:
500 				goto badcode;
501 		}
502 		goto deliver;
503 
504 	case ICMP_TIMXCEED:
505 		if (code > 1)
506 			goto badcode;
507 		code += PRC_TIMXCEED_INTRANS;
508 		goto deliver;
509 
510 	case ICMP_PARAMPROB:
511 		if (code > 1)
512 			goto badcode;
513 		code = PRC_PARAMPROB;
514 		goto deliver;
515 
516 	case ICMP_SOURCEQUENCH:
517 		if (code)
518 			goto badcode;
519 		code = PRC_QUENCH;
520 		goto deliver;
521 
522 	deliver:
523 		/*
524 		 * Problem with datagram; advise higher level routines.
525 		 */
526 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
527 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
528 			ICMP_STATINC(ICMP_STAT_BADLEN);
529 			goto freeit;
530 		}
531 		if (IN_MULTICAST(icp->icmp_ip.ip_dst.s_addr))
532 			goto badcode;
533 #ifdef ICMPPRINTFS
534 		if (icmpprintfs)
535 			printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
536 #endif
537 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
538 		ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
539 		if (ctlfunc)
540 			(void) (*ctlfunc)(code, sintosa(&icmpsrc),
541 			    &icp->icmp_ip);
542 		break;
543 
544 	badcode:
545 		ICMP_STATINC(ICMP_STAT_BADCODE);
546 		break;
547 
548 	case ICMP_ECHO:
549 		if (!icmpbmcastecho &&
550 		    (m->m_flags & (M_MCAST | M_BCAST)) != 0)  {
551 			ICMP_STATINC(ICMP_STAT_BMCASTECHO);
552 			break;
553 		}
554 		icp->icmp_type = ICMP_ECHOREPLY;
555 		goto reflect;
556 
557 	case ICMP_TSTAMP:
558 		if (icmplen < ICMP_TSLEN) {
559 			ICMP_STATINC(ICMP_STAT_BADLEN);
560 			break;
561 		}
562 		if (!icmpbmcastecho &&
563 		    (m->m_flags & (M_MCAST | M_BCAST)) != 0)  {
564 			ICMP_STATINC(ICMP_STAT_BMCASTTSTAMP);
565 			break;
566 		}
567 		icp->icmp_type = ICMP_TSTAMPREPLY;
568 		icp->icmp_rtime = iptime();
569 		icp->icmp_ttime = icp->icmp_rtime;	/* bogus, do later! */
570 		goto reflect;
571 
572 	case ICMP_MASKREQ:
573 		if (icmpmaskrepl == 0)
574 			break;
575 		/*
576 		 * We are not able to respond with all ones broadcast
577 		 * unless we receive it over a point-to-point interface.
578 		 */
579 		if (icmplen < ICMP_MASKLEN) {
580 			ICMP_STATINC(ICMP_STAT_BADLEN);
581 			break;
582 		}
583 		if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
584 		    in_nullhost(ip->ip_dst))
585 			icmpdst.sin_addr = ip->ip_src;
586 		else
587 			icmpdst.sin_addr = ip->ip_dst;
588 		ia = ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst),
589 		    m->m_pkthdr.rcvif));
590 		if (ia == 0)
591 			break;
592 		icp->icmp_type = ICMP_MASKREPLY;
593 		icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
594 		if (in_nullhost(ip->ip_src)) {
595 			if (ia->ia_ifp->if_flags & IFF_BROADCAST)
596 				ip->ip_src = ia->ia_broadaddr.sin_addr;
597 			else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
598 				ip->ip_src = ia->ia_dstaddr.sin_addr;
599 		}
600 reflect:
601 		{
602 			uint64_t *icps = percpu_getref(icmpstat_percpu);
603 			icps[ICMP_STAT_REFLECT]++;
604 			icps[ICMP_STAT_OUTHIST + icp->icmp_type]++;
605 			percpu_putref(icmpstat_percpu);
606 		}
607 		icmp_reflect(m);
608 		return;
609 
610 	case ICMP_REDIRECT:
611 		if (code > 3)
612 			goto badcode;
613 		if (icmp_rediraccept == 0)
614 			goto freeit;
615 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
616 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
617 			ICMP_STATINC(ICMP_STAT_BADLEN);
618 			break;
619 		}
620 		/*
621 		 * Short circuit routing redirects to force
622 		 * immediate change in the kernel's routing
623 		 * tables.  The message is also handed to anyone
624 		 * listening on a raw socket (e.g. the routing
625 		 * daemon for use in updating its tables).
626 		 */
627 		icmpgw.sin_addr = ip->ip_src;
628 		icmpdst.sin_addr = icp->icmp_gwaddr;
629 #ifdef	ICMPPRINTFS
630 		if (icmpprintfs) {
631 			printf("redirect dst `%s' to `%s'\n",
632 			    inet_ntoa(icp->icmp_ip.ip_dst),
633 			    inet_ntoa(icp->icmp_gwaddr));
634 		}
635 #endif
636 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
637 		rt = NULL;
638 		rtredirect(sintosa(&icmpsrc), sintosa(&icmpdst),
639 		    NULL, RTF_GATEWAY | RTF_HOST, sintosa(&icmpgw), &rt);
640 		if (rt != NULL && icmp_redirtimeout != 0) {
641 			i = rt_timer_add(rt, icmp_redirect_timeout,
642 					 icmp_redirect_timeout_q);
643 			if (i)
644 				log(LOG_ERR, "ICMP:  redirect failed to "
645 				    "register timeout for route to %x, "
646 				    "code %d\n",
647 				    icp->icmp_ip.ip_dst.s_addr, i);
648 		}
649 		if (rt != NULL)
650 			rtfree(rt);
651 
652 		pfctlinput(PRC_REDIRECT_HOST, sintosa(&icmpsrc));
653 #if defined(IPSEC) || defined(FAST_IPSEC)
654 		key_sa_routechange((struct sockaddr *)&icmpsrc);
655 #endif
656 		break;
657 
658 	/*
659 	 * No kernel processing for the following;
660 	 * just fall through to send to raw listener.
661 	 */
662 	case ICMP_ECHOREPLY:
663 	case ICMP_ROUTERADVERT:
664 	case ICMP_ROUTERSOLICIT:
665 	case ICMP_TSTAMPREPLY:
666 	case ICMP_IREQREPLY:
667 	case ICMP_MASKREPLY:
668 	default:
669 		break;
670 	}
671 
672 raw:
673 	rip_input(m, hlen, proto);
674 	return;
675 
676 freeit:
677 	m_freem(m);
678 	return;
679 }
680 
681 /*
682  * Reflect the ip packet back to the source
683  */
684 void
685 icmp_reflect(struct mbuf *m)
686 {
687 	struct ip *ip = mtod(m, struct ip *);
688 	struct in_ifaddr *ia;
689 	struct ifaddr *ifa;
690 	struct sockaddr_in *sin = 0;
691 	struct in_addr t;
692 	struct mbuf *opts = 0;
693 	int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
694 
695 	if (!in_canforward(ip->ip_src) &&
696 	    ((ip->ip_src.s_addr & IN_CLASSA_NET) !=
697 	     htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
698 		m_freem(m);	/* Bad return address */
699 		goto done;	/* ip_output() will check for broadcast */
700 	}
701 	t = ip->ip_dst;
702 	ip->ip_dst = ip->ip_src;
703 	/*
704 	 * If the incoming packet was addressed directly to us, use
705 	 * dst as the src for the reply.  Otherwise (broadcast or
706 	 * anonymous), use an address which corresponds to the
707 	 * incoming interface, with a preference for the address which
708 	 * corresponds to the route to the destination of the ICMP.
709 	 */
710 
711 	/* Look for packet addressed to us */
712 	INADDR_TO_IA(t, ia);
713 
714 	/* look for packet sent to broadcast address */
715 	if (ia == NULL && m->m_pkthdr.rcvif &&
716 	    (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST)) {
717 		IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
718 			if (ifa->ifa_addr->sa_family != AF_INET)
719 				continue;
720 			if (in_hosteq(t,ifatoia(ifa)->ia_broadaddr.sin_addr)) {
721 				ia = ifatoia(ifa);
722 				break;
723 			}
724 		}
725 	}
726 
727 	if (ia)
728 		sin = &ia->ia_addr;
729 
730 	icmpdst.sin_addr = t;
731 
732 	/*
733 	 * if the packet is addressed somewhere else, compute the
734 	 * source address for packets routed back to the source, and
735 	 * use that, if it's an address on the interface which
736 	 * received the packet
737 	 */
738 	if (sin == NULL && m->m_pkthdr.rcvif) {
739 		struct sockaddr_in sin_dst;
740 		struct route icmproute;
741 		int errornum;
742 
743 		sockaddr_in_init(&sin_dst, &ip->ip_dst, 0);
744 		memset(&icmproute, 0, sizeof(icmproute));
745 		errornum = 0;
746 		sin = in_selectsrc(&sin_dst, &icmproute, 0, NULL, &errornum);
747 		/* errornum is never used */
748 		rtcache_free(&icmproute);
749 		/* check to make sure sin is a source address on rcvif */
750 		if (sin) {
751 			t = sin->sin_addr;
752 			sin = NULL;
753 			INADDR_TO_IA(t, ia);
754 			while (ia) {
755 				if (ia->ia_ifp == m->m_pkthdr.rcvif) {
756 					sin = &ia->ia_addr;
757 					break;
758 				}
759 				NEXT_IA_WITH_SAME_ADDR(ia);
760 			}
761 		}
762 	}
763 
764 	/*
765 	 * if it was not addressed to us, but the route doesn't go out
766 	 * the source interface, pick an address on the source
767 	 * interface.  This can happen when routing is asymmetric, or
768 	 * when the incoming packet was encapsulated
769 	 */
770 	if (sin == NULL && m->m_pkthdr.rcvif) {
771 		IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
772 			if (ifa->ifa_addr->sa_family != AF_INET)
773 				continue;
774 			sin = &(ifatoia(ifa)->ia_addr);
775 			break;
776 		}
777 	}
778 
779 	/*
780 	 * The following happens if the packet was not addressed to us,
781 	 * and was received on an interface with no IP address:
782 	 * We find the first AF_INET address on the first non-loopback
783 	 * interface.
784 	 */
785 	if (sin == NULL)
786 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
787 			if (ia->ia_ifp->if_flags & IFF_LOOPBACK)
788 				continue;
789 			sin = &ia->ia_addr;
790 			break;
791 		}
792 
793 	/*
794 	 * If we still didn't find an address, punt.  We could have an
795 	 * interface up (and receiving packets) with no address.
796 	 */
797 	if (sin == NULL) {
798 		m_freem(m);
799 		goto done;
800 	}
801 
802 	ip->ip_src = sin->sin_addr;
803 	ip->ip_ttl = MAXTTL;
804 
805 	if (optlen > 0) {
806 		u_char *cp;
807 		int opt, cnt;
808 		u_int len;
809 
810 		/*
811 		 * Retrieve any source routing from the incoming packet;
812 		 * add on any record-route or timestamp options.
813 		 */
814 		cp = (u_char *) (ip + 1);
815 		if ((opts = ip_srcroute()) == 0 &&
816 		    (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
817 			MCLAIM(opts, m->m_owner);
818 			opts->m_len = sizeof(struct in_addr);
819 			*mtod(opts, struct in_addr *) = zeroin_addr;
820 		}
821 		if (opts) {
822 #ifdef ICMPPRINTFS
823 		    if (icmpprintfs)
824 			    printf("icmp_reflect optlen %d rt %d => ",
825 				optlen, opts->m_len);
826 #endif
827 		    for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
828 			    opt = cp[IPOPT_OPTVAL];
829 			    if (opt == IPOPT_EOL)
830 				    break;
831 			    if (opt == IPOPT_NOP)
832 				    len = 1;
833 			    else {
834 				    if (cnt < IPOPT_OLEN + sizeof(*cp))
835 					    break;
836 				    len = cp[IPOPT_OLEN];
837 				    if (len < IPOPT_OLEN + sizeof(*cp) ||
838 				        len > cnt)
839 					    break;
840 			    }
841 			    /*
842 			     * Should check for overflow, but it "can't happen"
843 			     */
844 			    if (opt == IPOPT_RR || opt == IPOPT_TS ||
845 				opt == IPOPT_SECURITY) {
846 				    memmove(mtod(opts, char *) + opts->m_len,
847 					cp, len);
848 				    opts->m_len += len;
849 			    }
850 		    }
851 		    /* Terminate & pad, if necessary */
852 		    if ((cnt = opts->m_len % 4) != 0) {
853 			    for (; cnt < 4; cnt++) {
854 				    *(mtod(opts, char *) + opts->m_len) =
855 					IPOPT_EOL;
856 				    opts->m_len++;
857 			    }
858 		    }
859 #ifdef ICMPPRINTFS
860 		    if (icmpprintfs)
861 			    printf("%d\n", opts->m_len);
862 #endif
863 		}
864 		/*
865 		 * Now strip out original options by copying rest of first
866 		 * mbuf's data back, and adjust the IP length.
867 		 */
868 		ip->ip_len = htons(ntohs(ip->ip_len) - optlen);
869 		ip->ip_hl = sizeof(struct ip) >> 2;
870 		m->m_len -= optlen;
871 		if (m->m_flags & M_PKTHDR)
872 			m->m_pkthdr.len -= optlen;
873 		optlen += sizeof(struct ip);
874 		memmove(ip + 1, (char *)ip + optlen,
875 		    (unsigned)(m->m_len - sizeof(struct ip)));
876 	}
877 	m_tag_delete_nonpersistent(m);
878 	m->m_flags &= ~(M_BCAST|M_MCAST);
879 
880 	/*
881 	 * Clear any in-bound checksum flags for this packet.
882 	 */
883 	if (m->m_flags & M_PKTHDR)
884 		m->m_pkthdr.csum_flags = 0;
885 
886 	icmp_send(m, opts);
887 done:
888 	if (opts)
889 		(void)m_free(opts);
890 }
891 
892 /*
893  * Send an icmp packet back to the ip level,
894  * after supplying a checksum.
895  */
896 void
897 icmp_send(struct mbuf *m, struct mbuf *opts)
898 {
899 	struct ip *ip = mtod(m, struct ip *);
900 	int hlen;
901 	struct icmp *icp;
902 
903 	hlen = ip->ip_hl << 2;
904 	m->m_data += hlen;
905 	m->m_len -= hlen;
906 	icp = mtod(m, struct icmp *);
907 	icp->icmp_cksum = 0;
908 	icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen);
909 	m->m_data -= hlen;
910 	m->m_len += hlen;
911 #ifdef ICMPPRINTFS
912 	if (icmpprintfs) {
913 		printf("icmp_send to destination `%s' from `%s'\n",
914 		    inet_ntoa(ip->ip_dst), inet_ntoa(ip->ip_src));
915 	}
916 #endif
917 	(void)ip_output(m, opts, NULL, 0, NULL, NULL);
918 }
919 
920 n_time
921 iptime(void)
922 {
923 	struct timeval atv;
924 	u_long t;
925 
926 	microtime(&atv);
927 	t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
928 	return (htonl(t));
929 }
930 
931 /*
932  * sysctl helper routine for net.inet.icmp.returndatabytes.  ensures
933  * that the new value is in the correct range.
934  */
935 static int
936 sysctl_net_inet_icmp_returndatabytes(SYSCTLFN_ARGS)
937 {
938 	int error, t;
939 	struct sysctlnode node;
940 
941 	node = *rnode;
942 	node.sysctl_data = &t;
943 	t = icmpreturndatabytes;
944 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
945 	if (error || newp == NULL)
946 		return (error);
947 
948 	if (t < 8 || t > 512)
949 		return (EINVAL);
950 	icmpreturndatabytes = t;
951 
952 	return (0);
953 }
954 
955 /*
956  * sysctl helper routine for net.inet.icmp.redirtimeout.  ensures that
957  * the given value is not less than zero and then resets the timeout
958  * queue.
959  */
960 static int
961 sysctl_net_inet_icmp_redirtimeout(SYSCTLFN_ARGS)
962 {
963 	int error, tmp;
964 	struct sysctlnode node;
965 
966 	node = *rnode;
967 	node.sysctl_data = &tmp;
968 	tmp = icmp_redirtimeout;
969 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
970 	if (error || newp == NULL)
971 		return (error);
972 	if (tmp < 0)
973 		return (EINVAL);
974 	icmp_redirtimeout = tmp;
975 
976 	/*
977 	 * was it a *defined* side-effect that anyone even *reading*
978 	 * this value causes these things to happen?
979 	 */
980 	if (icmp_redirect_timeout_q != NULL) {
981 		if (icmp_redirtimeout == 0) {
982 			rt_timer_queue_destroy(icmp_redirect_timeout_q,
983 			    true);
984 			icmp_redirect_timeout_q = NULL;
985 		} else {
986 			rt_timer_queue_change(icmp_redirect_timeout_q,
987 			    icmp_redirtimeout);
988 		}
989 	} else if (icmp_redirtimeout > 0) {
990 		icmp_redirect_timeout_q =
991 		    rt_timer_queue_create(icmp_redirtimeout);
992 	}
993 
994 	return (0);
995 }
996 
997 static int
998 sysctl_net_inet_icmp_stats(SYSCTLFN_ARGS)
999 {
1000 
1001 	return (NETSTAT_SYSCTL(icmpstat_percpu, ICMP_NSTATS));
1002 }
1003 
1004 static void
1005 sysctl_netinet_icmp_setup(struct sysctllog **clog)
1006 {
1007 
1008 	sysctl_createv(clog, 0, NULL, NULL,
1009 		       CTLFLAG_PERMANENT,
1010 		       CTLTYPE_NODE, "net", NULL,
1011 		       NULL, 0, NULL, 0,
1012 		       CTL_NET, CTL_EOL);
1013 	sysctl_createv(clog, 0, NULL, NULL,
1014 		       CTLFLAG_PERMANENT,
1015 		       CTLTYPE_NODE, "inet", NULL,
1016 		       NULL, 0, NULL, 0,
1017 		       CTL_NET, PF_INET, CTL_EOL);
1018 	sysctl_createv(clog, 0, NULL, NULL,
1019 		       CTLFLAG_PERMANENT,
1020 		       CTLTYPE_NODE, "icmp",
1021 		       SYSCTL_DESCR("ICMPv4 related settings"),
1022 		       NULL, 0, NULL, 0,
1023 		       CTL_NET, PF_INET, IPPROTO_ICMP, CTL_EOL);
1024 
1025 	sysctl_createv(clog, 0, NULL, NULL,
1026 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1027 		       CTLTYPE_INT, "maskrepl",
1028 		       SYSCTL_DESCR("Respond to ICMP_MASKREQ messages"),
1029 		       NULL, 0, &icmpmaskrepl, 0,
1030 		       CTL_NET, PF_INET, IPPROTO_ICMP,
1031 		       ICMPCTL_MASKREPL, CTL_EOL);
1032 	sysctl_createv(clog, 0, NULL, NULL,
1033 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1034 		       CTLTYPE_INT, "returndatabytes",
1035 		       SYSCTL_DESCR("Number of bytes to return in an ICMP "
1036 				    "error message"),
1037 		       sysctl_net_inet_icmp_returndatabytes, 0,
1038 		       &icmpreturndatabytes, 0,
1039 		       CTL_NET, PF_INET, IPPROTO_ICMP,
1040 		       ICMPCTL_RETURNDATABYTES, CTL_EOL);
1041 	sysctl_createv(clog, 0, NULL, NULL,
1042 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1043 		       CTLTYPE_INT, "errppslimit",
1044 		       SYSCTL_DESCR("Maximum number of outgoing ICMP error "
1045 				    "messages per second"),
1046 		       NULL, 0, &icmperrppslim, 0,
1047 		       CTL_NET, PF_INET, IPPROTO_ICMP,
1048 		       ICMPCTL_ERRPPSLIMIT, CTL_EOL);
1049 	sysctl_createv(clog, 0, NULL, NULL,
1050 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1051 		       CTLTYPE_INT, "rediraccept",
1052 		       SYSCTL_DESCR("Accept ICMP_REDIRECT messages"),
1053 		       NULL, 0, &icmp_rediraccept, 0,
1054 		       CTL_NET, PF_INET, IPPROTO_ICMP,
1055 		       ICMPCTL_REDIRACCEPT, CTL_EOL);
1056 	sysctl_createv(clog, 0, NULL, NULL,
1057 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1058 		       CTLTYPE_INT, "redirtimeout",
1059 		       SYSCTL_DESCR("Lifetime of ICMP_REDIRECT generated "
1060 				    "routes"),
1061 		       sysctl_net_inet_icmp_redirtimeout, 0,
1062 		       &icmp_redirtimeout, 0,
1063 		       CTL_NET, PF_INET, IPPROTO_ICMP,
1064 		       ICMPCTL_REDIRTIMEOUT, CTL_EOL);
1065 	sysctl_createv(clog, 0, NULL, NULL,
1066 		       CTLFLAG_PERMANENT,
1067 		       CTLTYPE_STRUCT, "stats",
1068 		       SYSCTL_DESCR("ICMP statistics"),
1069 		       sysctl_net_inet_icmp_stats, 0, NULL, 0,
1070 		       CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_STATS,
1071 		       CTL_EOL);
1072 	sysctl_createv(clog, 0, NULL, NULL,
1073 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1074 		       CTLTYPE_INT, "bmcastecho",
1075 		       SYSCTL_DESCR("Respond to ICMP_ECHO or ICMP_TIMESTAMP "
1076 				    "message to the broadcast or multicast"),
1077 		       NULL, 0, &icmpbmcastecho, 0,
1078 		       CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_BMCASTECHO,
1079 		       CTL_EOL);
1080 }
1081 
1082 void
1083 icmp_statinc(u_int stat)
1084 {
1085 
1086 	KASSERT(stat < ICMP_NSTATS);
1087 	ICMP_STATINC(stat);
1088 }
1089 
1090 /* Table of common MTUs: */
1091 
1092 static const u_int mtu_table[] = {
1093 	65535, 65280, 32000, 17914, 9180, 8166,
1094 	4352, 2002, 1492, 1006, 508, 296, 68, 0
1095 };
1096 
1097 void
1098 icmp_mtudisc(struct icmp *icp, struct in_addr faddr)
1099 {
1100 	struct icmp_mtudisc_callback *mc;
1101 	struct sockaddr *dst = sintosa(&icmpsrc);
1102 	struct rtentry *rt;
1103 	u_long mtu = ntohs(icp->icmp_nextmtu);  /* Why a long?  IPv6 */
1104 	int    error;
1105 
1106 	rt = rtalloc1(dst, 1);
1107 	if (rt == 0)
1108 		return;
1109 
1110 	/* If we didn't get a host route, allocate one */
1111 
1112 	if ((rt->rt_flags & RTF_HOST) == 0) {
1113 		struct rtentry *nrt;
1114 
1115 		error = rtrequest((int) RTM_ADD, dst,
1116 		    (struct sockaddr *) rt->rt_gateway,
1117 		    (struct sockaddr *) 0,
1118 		    RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
1119 		if (error) {
1120 			rtfree(rt);
1121 			return;
1122 		}
1123 		nrt->rt_rmx = rt->rt_rmx;
1124 		rtfree(rt);
1125 		rt = nrt;
1126 	}
1127 	error = rt_timer_add(rt, icmp_mtudisc_timeout, ip_mtudisc_timeout_q);
1128 	if (error) {
1129 		rtfree(rt);
1130 		return;
1131 	}
1132 
1133 	if (mtu == 0) {
1134 		int i = 0;
1135 
1136 		mtu = ntohs(icp->icmp_ip.ip_len);
1137 		/* Some 4.2BSD-based routers incorrectly adjust the ip_len */
1138 		if (mtu > rt->rt_rmx.rmx_mtu && rt->rt_rmx.rmx_mtu != 0)
1139 			mtu -= (icp->icmp_ip.ip_hl << 2);
1140 
1141 		/* If we still can't guess a value, try the route */
1142 
1143 		if (mtu == 0) {
1144 			mtu = rt->rt_rmx.rmx_mtu;
1145 
1146 			/* If no route mtu, default to the interface mtu */
1147 
1148 			if (mtu == 0)
1149 				mtu = rt->rt_ifp->if_mtu;
1150 		}
1151 
1152 		for (i = 0; i < sizeof(mtu_table) / sizeof(mtu_table[0]); i++)
1153 			if (mtu > mtu_table[i]) {
1154 				mtu = mtu_table[i];
1155 				break;
1156 			}
1157 	}
1158 
1159 	/*
1160 	 * XXX:   RTV_MTU is overloaded, since the admin can set it
1161 	 *	  to turn off PMTU for a route, and the kernel can
1162 	 *	  set it to indicate a serious problem with PMTU
1163 	 *	  on a route.  We should be using a separate flag
1164 	 *	  for the kernel to indicate this.
1165 	 */
1166 
1167 	if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
1168 		if (mtu < 296 || mtu > rt->rt_ifp->if_mtu)
1169 			rt->rt_rmx.rmx_locks |= RTV_MTU;
1170 		else if (rt->rt_rmx.rmx_mtu > mtu ||
1171 			 rt->rt_rmx.rmx_mtu == 0) {
1172 			ICMP_STATINC(ICMP_STAT_PMTUCHG);
1173 			rt->rt_rmx.rmx_mtu = mtu;
1174 		}
1175 	}
1176 
1177 	if (rt)
1178 		rtfree(rt);
1179 
1180 	/*
1181 	 * Notify protocols that the MTU for this destination
1182 	 * has changed.
1183 	 */
1184 	for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL;
1185 	     mc = LIST_NEXT(mc, mc_list))
1186 		(*mc->mc_func)(faddr);
1187 }
1188 
1189 /*
1190  * Return the next larger or smaller MTU plateau (table from RFC 1191)
1191  * given current value MTU.  If DIR is less than zero, a larger plateau
1192  * is returned; otherwise, a smaller value is returned.
1193  */
1194 u_int
1195 ip_next_mtu(u_int mtu, int dir)	/* XXX */
1196 {
1197 	int i;
1198 
1199 	for (i = 0; i < (sizeof mtu_table) / (sizeof mtu_table[0]); i++) {
1200 		if (mtu >= mtu_table[i])
1201 			break;
1202 	}
1203 
1204 	if (dir < 0) {
1205 		if (i == 0) {
1206 			return 0;
1207 		} else {
1208 			return mtu_table[i - 1];
1209 		}
1210 	} else {
1211 		if (mtu_table[i] == 0) {
1212 			return 0;
1213 		} else if (mtu > mtu_table[i]) {
1214 			return mtu_table[i];
1215 		} else {
1216 			return mtu_table[i + 1];
1217 		}
1218 	}
1219 }
1220 
1221 static void
1222 icmp_mtudisc_timeout(struct rtentry *rt, struct rttimer *r)
1223 {
1224 	if (rt == NULL)
1225 		panic("icmp_mtudisc_timeout:  bad route to timeout");
1226 	if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
1227 	    (RTF_DYNAMIC | RTF_HOST)) {
1228 		rtrequest((int) RTM_DELETE, rt_getkey(rt),
1229 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
1230 	} else {
1231 		if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
1232 			rt->rt_rmx.rmx_mtu = 0;
1233 		}
1234 	}
1235 }
1236 
1237 static void
1238 icmp_redirect_timeout(struct rtentry *rt, struct rttimer *r)
1239 {
1240 	if (rt == NULL)
1241 		panic("icmp_redirect_timeout:  bad route to timeout");
1242 	if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
1243 	    (RTF_DYNAMIC | RTF_HOST)) {
1244 		rtrequest((int) RTM_DELETE, rt_getkey(rt),
1245 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
1246 	}
1247 }
1248 
1249 /*
1250  * Perform rate limit check.
1251  * Returns 0 if it is okay to send the icmp packet.
1252  * Returns 1 if the router SHOULD NOT send this icmp packet due to rate
1253  * limitation.
1254  *
1255  * XXX per-destination/type check necessary?
1256  */
1257 int
1258 icmp_ratelimit(const struct in_addr *dst, const int type,
1259     const int code)
1260 {
1261 
1262 	/* PPS limit */
1263 	if (!ppsratecheck(&icmperrppslim_last, &icmperrpps_count,
1264 	    icmperrppslim)) {
1265 		/* The packet is subject to rate limit */
1266 		return 1;
1267 	}
1268 
1269 	/* okay to send */
1270 	return 0;
1271 }
1272