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