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