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