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