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