1 /* $NetBSD: ip_icmp.c,v 1.120 2008/06/18 09:06:28 yamt Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
34 * All rights reserved.
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.
40 * This code is derived from software contributed to The NetBSD Foundation
41 * by Jason R. Thorpe of Zembu Labs, Inc.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
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.
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.
66 * Copyright (c) 1982, 1986, 1988, 1993
67 * The Regents of the University of California. All rights reserved.
69 * Redistribution and use in source and binary forms, with or without
70 * modification, are permitted provided that the following conditions
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.
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
93 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
96 #include <sys/cdefs.h>
97 __KERNEL_RCSID(0, "$NetBSD: ip_icmp.c,v 1.120 2008/06/18 09:06:28 yamt Exp $");
99 #include "opt_ipsec.h"
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/malloc.h>
104 #include <sys/mbuf.h>
105 #include <sys/protosw.h>
106 #include <sys/socket.h>
107 #include <sys/time.h>
108 #include <sys/kernel.h>
109 #include <sys/syslog.h>
110 #include <sys/sysctl.h>
113 #include <net/route.h>
115 #include <netinet/in.h>
116 #include <netinet/in_systm.h>
117 #include <netinet/in_var.h>
118 #include <netinet/ip.h>
119 #include <netinet/ip_icmp.h>
120 #include <netinet/ip_var.h>
121 #include <netinet/in_pcb.h>
122 #include <netinet/in_proto.h>
123 #include <netinet/icmp_var.h>
124 #include <netinet/icmp_private.h>
127 #include <netinet6/ipsec.h>
128 #include <netkey/key.h>
132 #include <netipsec/ipsec.h>
133 #include <netipsec/key.h>
134 #endif /* FAST_IPSEC*/
136 #include <machine/stdarg.h>
139 * ICMP routines: error generation, receive packet processing, and
140 * routines to turnaround packets back to the originator, and
141 * host table maintenance routines.
144 int icmpmaskrepl
= 0;
148 int icmpreturndatabytes
= 8;
150 percpu_t
*icmpstat_percpu
;
153 * List of callbacks to notify when Path MTU changes are made.
155 struct icmp_mtudisc_callback
{
156 LIST_ENTRY(icmp_mtudisc_callback
) mc_list
;
157 void (*mc_func
)(struct in_addr
);
160 LIST_HEAD(, icmp_mtudisc_callback
) icmp_mtudisc_callbacks
=
161 LIST_HEAD_INITIALIZER(&icmp_mtudisc_callbacks
);
164 static u_int
ip_next_mtu(u_int
, int);
166 /*static*/ u_int
ip_next_mtu(u_int
, int);
169 extern int icmperrppslim
;
170 static int icmperrpps_count
= 0;
171 static struct timeval icmperrppslim_last
;
172 static int icmp_rediraccept
= 1;
173 static int icmp_redirtimeout
= 600;
174 static struct rttimer_queue
*icmp_redirect_timeout_q
= NULL
;
176 static void icmp_mtudisc_timeout(struct rtentry
*, struct rttimer
*);
177 static void icmp_redirect_timeout(struct rtentry
*, struct rttimer
*);
179 static int icmp_ratelimit(const struct in_addr
*, const int, const int);
181 static void sysctl_netinet_icmp_setup(struct sysctllog
**);
187 sysctl_netinet_icmp_setup(NULL
);
190 * This is only useful if the user initializes redirtimeout to
191 * something other than zero.
193 if (icmp_redirtimeout
!= 0) {
194 icmp_redirect_timeout_q
=
195 rt_timer_queue_create(icmp_redirtimeout
);
198 icmpstat_percpu
= percpu_alloc(sizeof(uint64_t) * ICMP_NSTATS
);
202 * Register a Path MTU Discovery callback.
205 icmp_mtudisc_callback_register(void (*func
)(struct in_addr
))
207 struct icmp_mtudisc_callback
*mc
;
209 for (mc
= LIST_FIRST(&icmp_mtudisc_callbacks
); mc
!= NULL
;
210 mc
= LIST_NEXT(mc
, mc_list
)) {
211 if (mc
->mc_func
== func
)
215 mc
= malloc(sizeof(*mc
), M_PCB
, M_NOWAIT
);
217 panic("icmp_mtudisc_callback_register");
220 LIST_INSERT_HEAD(&icmp_mtudisc_callbacks
, mc
, mc_list
);
224 * Generate an error packet of type error
225 * in response to bad packet ip.
228 icmp_error(struct mbuf
*n
, int type
, int code
, n_long dest
,
231 struct ip
*oip
= mtod(n
, struct ip
*), *nip
;
232 unsigned oiplen
= oip
->ip_hl
<< 2;
236 unsigned icmplen
, mblen
;
240 printf("icmp_error(%p, type:%d, code:%d)\n", oip
, type
, code
);
242 if (type
!= ICMP_REDIRECT
)
243 ICMP_STATINC(ICMP_STAT_ERROR
);
245 * Don't send error if the original packet was encrypted.
246 * Don't send error if not the first fragment of message.
247 * Don't error if the old packet protocol was ICMP
248 * error message, only known informational types.
250 if (n
->m_flags
& M_DECRYPTED
)
252 if (oip
->ip_off
&~ htons(IP_MF
|IP_DF
))
254 if (oip
->ip_p
== IPPROTO_ICMP
&& type
!= ICMP_REDIRECT
&&
255 n
->m_len
>= oiplen
+ ICMP_MINLEN
&&
256 !ICMP_INFOTYPE(((struct icmp
*)((char *)oip
+ oiplen
))->icmp_type
)) {
257 ICMP_STATINC(ICMP_STAT_OLDICMP
);
260 /* Don't send error in response to a multicast or broadcast packet */
261 if (n
->m_flags
& (M_BCAST
|M_MCAST
))
265 * First, do a rate limitation check.
267 if (icmp_ratelimit(&oip
->ip_src
, type
, code
)) {
273 * Now, formulate icmp message
275 icmplen
= oiplen
+ min(icmpreturndatabytes
,
276 ntohs(oip
->ip_len
) - oiplen
);
278 * Defend against mbuf chains shorter than oip->ip_len - oiplen:
281 for (m
= n
; m
&& (mblen
< icmplen
); m
= m
->m_next
)
283 icmplen
= min(mblen
, icmplen
);
286 * As we are not required to return everything we have,
287 * we return whatever we can return at ease.
289 * Note that ICMP datagrams longer than 576 octets are out of spec
290 * according to RFC1812; the limit on icmpreturndatabytes below in
291 * icmp_sysctl will keep things below that limit.
294 KASSERT(ICMP_MINLEN
<= MCLBYTES
);
296 if (icmplen
+ ICMP_MINLEN
> MCLBYTES
)
297 icmplen
= MCLBYTES
- ICMP_MINLEN
;
299 m
= m_gethdr(M_DONTWAIT
, MT_HEADER
);
300 if (m
&& (icmplen
+ ICMP_MINLEN
> MHLEN
)) {
301 MCLGET(m
, M_DONTWAIT
);
302 if ((m
->m_flags
& M_EXT
) == 0) {
309 MCLAIM(m
, n
->m_owner
);
310 m
->m_len
= icmplen
+ ICMP_MINLEN
;
311 if ((m
->m_flags
& M_EXT
) == 0)
312 MH_ALIGN(m
, m
->m_len
);
313 icp
= mtod(m
, struct icmp
*);
314 if ((u_int
)type
> ICMP_MAXTYPE
)
316 ICMP_STATINC(ICMP_STAT_OUTHIST
+ type
);
317 icp
->icmp_type
= type
;
318 if (type
== ICMP_REDIRECT
)
319 icp
->icmp_gwaddr
.s_addr
= dest
;
323 * The following assignments assume an overlay with the
324 * zeroed icmp_void field.
326 if (type
== ICMP_PARAMPROB
) {
327 icp
->icmp_pptr
= code
;
329 } else if (type
== ICMP_UNREACH
&&
330 code
== ICMP_UNREACH_NEEDFRAG
&& destmtu
)
331 icp
->icmp_nextmtu
= htons(destmtu
);
334 icp
->icmp_code
= code
;
335 m_copydata(n
, 0, icmplen
, (void *)&icp
->icmp_ip
);
338 * Now, copy old ip header (without options)
339 * in front of icmp message.
341 if (m
->m_data
- sizeof(struct ip
) < m
->m_pktdat
)
343 m
->m_data
-= sizeof(struct ip
);
344 m
->m_len
+= sizeof(struct ip
);
345 m
->m_pkthdr
.len
= m
->m_len
;
346 m
->m_pkthdr
.rcvif
= n
->m_pkthdr
.rcvif
;
347 nip
= mtod(m
, struct ip
*);
348 /* ip_v set in ip_output */
349 nip
->ip_hl
= sizeof(struct ip
) >> 2;
351 nip
->ip_len
= htons(m
->m_len
);
352 /* ip_id set in ip_output */
353 nip
->ip_off
= htons(0);
354 /* ip_ttl set in icmp_reflect */
355 nip
->ip_p
= IPPROTO_ICMP
;
356 nip
->ip_src
= oip
->ip_src
;
357 nip
->ip_dst
= oip
->ip_dst
;
358 /* move PF m_tag to new packet, if it exists */
359 mtag
= m_tag_find(n
, PACKET_TAG_PF
, NULL
);
361 m_tag_unlink(n
, mtag
);
362 m_tag_prepend(m
, mtag
);
370 struct sockaddr_in icmpsrc
= {
371 .sin_len
= sizeof (struct sockaddr_in
),
372 .sin_family
= AF_INET
,
374 static struct sockaddr_in icmpdst
= {
375 .sin_len
= sizeof (struct sockaddr_in
),
376 .sin_family
= AF_INET
,
378 static struct sockaddr_in icmpgw
= {
379 .sin_len
= sizeof (struct sockaddr_in
),
380 .sin_family
= AF_INET
,
382 struct sockaddr_in icmpmask
= {
388 * Process a received ICMP message.
391 icmp_input(struct mbuf
*m
, ...)
395 struct ip
*ip
= mtod(m
, struct ip
*);
398 struct in_ifaddr
*ia
;
399 void *(*ctlfunc
)(int, const struct sockaddr
*, void *);
406 hlen
= va_arg(ap
, int);
407 proto
= va_arg(ap
, int);
411 * Locate icmp structure in mbuf, and check
412 * that not corrupted and of at least minimum length.
414 icmplen
= ntohs(ip
->ip_len
) - hlen
;
417 printf("icmp_input from `%s' to ", inet_ntoa(ip
->ip_src
));
418 printf("`%s', len %d\n", inet_ntoa(ip
->ip_dst
), icmplen
);
421 if (icmplen
< ICMP_MINLEN
) {
422 ICMP_STATINC(ICMP_STAT_TOOSHORT
);
425 i
= hlen
+ min(icmplen
, ICMP_ADVLENMIN
);
426 if ((m
->m_len
< i
|| M_READONLY(m
)) && (m
= m_pullup(m
, i
)) == 0) {
427 ICMP_STATINC(ICMP_STAT_TOOSHORT
);
430 ip
= mtod(m
, struct ip
*);
433 icp
= mtod(m
, struct icmp
*);
434 /* Don't need to assert alignment, here. */
435 if (in_cksum(m
, icmplen
)) {
436 ICMP_STATINC(ICMP_STAT_CHECKSUM
);
444 * Message type specific processing.
447 printf("icmp_input(type:%d, code:%d)\n", icp
->icmp_type
,
450 if (icp
->icmp_type
> ICMP_MAXTYPE
)
452 ICMP_STATINC(ICMP_STAT_INHIST
+ icp
->icmp_type
);
453 code
= icp
->icmp_code
;
454 switch (icp
->icmp_type
) {
458 case ICMP_UNREACH_NET
:
459 code
= PRC_UNREACH_NET
;
462 case ICMP_UNREACH_HOST
:
463 code
= PRC_UNREACH_HOST
;
466 case ICMP_UNREACH_PROTOCOL
:
467 code
= PRC_UNREACH_PROTOCOL
;
470 case ICMP_UNREACH_PORT
:
471 code
= PRC_UNREACH_PORT
;
474 case ICMP_UNREACH_SRCFAIL
:
475 code
= PRC_UNREACH_SRCFAIL
;
478 case ICMP_UNREACH_NEEDFRAG
:
482 case ICMP_UNREACH_NET_UNKNOWN
:
483 case ICMP_UNREACH_NET_PROHIB
:
484 case ICMP_UNREACH_TOSNET
:
485 code
= PRC_UNREACH_NET
;
488 case ICMP_UNREACH_HOST_UNKNOWN
:
489 case ICMP_UNREACH_ISOLATED
:
490 case ICMP_UNREACH_HOST_PROHIB
:
491 case ICMP_UNREACH_TOSHOST
:
492 code
= PRC_UNREACH_HOST
;
503 code
+= PRC_TIMXCEED_INTRANS
;
509 code
= PRC_PARAMPROB
;
512 case ICMP_SOURCEQUENCH
:
520 * Problem with datagram; advise higher level routines.
522 if (icmplen
< ICMP_ADVLENMIN
|| icmplen
< ICMP_ADVLEN(icp
) ||
523 icp
->icmp_ip
.ip_hl
< (sizeof(struct ip
) >> 2)) {
524 ICMP_STATINC(ICMP_STAT_BADLEN
);
527 if (IN_MULTICAST(icp
->icmp_ip
.ip_dst
.s_addr
))
531 printf("deliver to protocol %d\n", icp
->icmp_ip
.ip_p
);
533 icmpsrc
.sin_addr
= icp
->icmp_ip
.ip_dst
;
534 ctlfunc
= inetsw
[ip_protox
[icp
->icmp_ip
.ip_p
]].pr_ctlinput
;
536 (void) (*ctlfunc
)(code
, sintosa(&icmpsrc
),
541 ICMP_STATINC(ICMP_STAT_BADCODE
);
545 icp
->icmp_type
= ICMP_ECHOREPLY
;
549 if (icmplen
< ICMP_TSLEN
) {
550 ICMP_STATINC(ICMP_STAT_BADLEN
);
553 icp
->icmp_type
= ICMP_TSTAMPREPLY
;
554 icp
->icmp_rtime
= iptime();
555 icp
->icmp_ttime
= icp
->icmp_rtime
; /* bogus, do later! */
559 if (icmpmaskrepl
== 0)
562 * We are not able to respond with all ones broadcast
563 * unless we receive it over a point-to-point interface.
565 if (icmplen
< ICMP_MASKLEN
) {
566 ICMP_STATINC(ICMP_STAT_BADLEN
);
569 if (ip
->ip_dst
.s_addr
== INADDR_BROADCAST
||
570 in_nullhost(ip
->ip_dst
))
571 icmpdst
.sin_addr
= ip
->ip_src
;
573 icmpdst
.sin_addr
= ip
->ip_dst
;
574 ia
= ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst
),
578 icp
->icmp_type
= ICMP_MASKREPLY
;
579 icp
->icmp_mask
= ia
->ia_sockmask
.sin_addr
.s_addr
;
580 if (in_nullhost(ip
->ip_src
)) {
581 if (ia
->ia_ifp
->if_flags
& IFF_BROADCAST
)
582 ip
->ip_src
= ia
->ia_broadaddr
.sin_addr
;
583 else if (ia
->ia_ifp
->if_flags
& IFF_POINTOPOINT
)
584 ip
->ip_src
= ia
->ia_dstaddr
.sin_addr
;
588 uint64_t *icps
= percpu_getref(icmpstat_percpu
);
589 icps
[ICMP_STAT_REFLECT
]++;
590 icps
[ICMP_STAT_OUTHIST
+ icp
->icmp_type
]++;
591 percpu_putref(icmpstat_percpu
);
599 if (icmp_rediraccept
== 0)
601 if (icmplen
< ICMP_ADVLENMIN
|| icmplen
< ICMP_ADVLEN(icp
) ||
602 icp
->icmp_ip
.ip_hl
< (sizeof(struct ip
) >> 2)) {
603 ICMP_STATINC(ICMP_STAT_BADLEN
);
607 * Short circuit routing redirects to force
608 * immediate change in the kernel's routing
609 * tables. The message is also handed to anyone
610 * listening on a raw socket (e.g. the routing
611 * daemon for use in updating its tables).
613 icmpgw
.sin_addr
= ip
->ip_src
;
614 icmpdst
.sin_addr
= icp
->icmp_gwaddr
;
617 printf("redirect dst `%s' to `%s'\n",
618 inet_ntoa(icp
->icmp_ip
.ip_dst
),
619 inet_ntoa(icp
->icmp_gwaddr
));
622 icmpsrc
.sin_addr
= icp
->icmp_ip
.ip_dst
;
624 rtredirect(sintosa(&icmpsrc
), sintosa(&icmpdst
),
625 NULL
, RTF_GATEWAY
| RTF_HOST
, sintosa(&icmpgw
), &rt
);
626 if (rt
!= NULL
&& icmp_redirtimeout
!= 0) {
627 i
= rt_timer_add(rt
, icmp_redirect_timeout
,
628 icmp_redirect_timeout_q
);
630 log(LOG_ERR
, "ICMP: redirect failed to "
631 "register timeout for route to %x, "
633 icp
->icmp_ip
.ip_dst
.s_addr
, i
);
638 pfctlinput(PRC_REDIRECT_HOST
, sintosa(&icmpsrc
));
639 #if defined(IPSEC) || defined(FAST_IPSEC)
640 key_sa_routechange((struct sockaddr
*)&icmpsrc
);
645 * No kernel processing for the following;
646 * just fall through to send to raw listener.
649 case ICMP_ROUTERADVERT
:
650 case ICMP_ROUTERSOLICIT
:
651 case ICMP_TSTAMPREPLY
:
659 rip_input(m
, hlen
, proto
);
668 * Reflect the ip packet back to the source
671 icmp_reflect(struct mbuf
*m
)
673 struct ip
*ip
= mtod(m
, struct ip
*);
674 struct in_ifaddr
*ia
;
676 struct sockaddr_in
*sin
= 0;
678 struct mbuf
*opts
= 0;
679 int optlen
= (ip
->ip_hl
<< 2) - sizeof(struct ip
);
681 if (!in_canforward(ip
->ip_src
) &&
682 ((ip
->ip_src
.s_addr
& IN_CLASSA_NET
) !=
683 htonl(IN_LOOPBACKNET
<< IN_CLASSA_NSHIFT
))) {
684 m_freem(m
); /* Bad return address */
685 goto done
; /* ip_output() will check for broadcast */
688 ip
->ip_dst
= ip
->ip_src
;
690 * If the incoming packet was addressed directly to us, use
691 * dst as the src for the reply. Otherwise (broadcast or
692 * anonymous), use an address which corresponds to the
693 * incoming interface, with a preference for the address which
694 * corresponds to the route to the destination of the ICMP.
697 /* Look for packet addressed to us */
700 /* look for packet sent to broadcast address */
701 if (ia
== NULL
&& m
->m_pkthdr
.rcvif
&&
702 (m
->m_pkthdr
.rcvif
->if_flags
& IFF_BROADCAST
)) {
703 IFADDR_FOREACH(ifa
, m
->m_pkthdr
.rcvif
) {
704 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
706 if (in_hosteq(t
,ifatoia(ifa
)->ia_broadaddr
.sin_addr
)) {
716 icmpdst
.sin_addr
= t
;
719 * if the packet is addressed somewhere else, compute the
720 * source address for packets routed back to the source, and
721 * use that, if it's an address on the interface which
722 * received the packet
724 if (sin
== NULL
&& m
->m_pkthdr
.rcvif
) {
725 struct sockaddr_in sin_dst
;
726 struct route icmproute
;
729 sockaddr_in_init(&sin_dst
, &ip
->ip_dst
, 0);
730 memset(&icmproute
, 0, sizeof(icmproute
));
732 sin
= in_selectsrc(&sin_dst
, &icmproute
, 0, NULL
, &errornum
);
733 /* errornum is never used */
734 rtcache_free(&icmproute
);
735 /* check to make sure sin is a source address on rcvif */
741 if (ia
->ia_ifp
== m
->m_pkthdr
.rcvif
) {
745 NEXT_IA_WITH_SAME_ADDR(ia
);
751 * if it was not addressed to us, but the route doesn't go out
752 * the source interface, pick an address on the source
753 * interface. This can happen when routing is asymmetric, or
754 * when the incoming packet was encapsulated
756 if (sin
== NULL
&& m
->m_pkthdr
.rcvif
) {
757 IFADDR_FOREACH(ifa
, m
->m_pkthdr
.rcvif
) {
758 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
760 sin
= &(ifatoia(ifa
)->ia_addr
);
766 * The following happens if the packet was not addressed to us,
767 * and was received on an interface with no IP address:
768 * We find the first AF_INET address on the first non-loopback
772 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_list
) {
773 if (ia
->ia_ifp
->if_flags
& IFF_LOOPBACK
)
780 * If we still didn't find an address, punt. We could have an
781 * interface up (and receiving packets) with no address.
788 ip
->ip_src
= sin
->sin_addr
;
797 * Retrieve any source routing from the incoming packet;
798 * add on any record-route or timestamp options.
800 cp
= (u_char
*) (ip
+ 1);
801 if ((opts
= ip_srcroute()) == 0 &&
802 (opts
= m_gethdr(M_DONTWAIT
, MT_HEADER
))) {
803 MCLAIM(opts
, m
->m_owner
);
804 opts
->m_len
= sizeof(struct in_addr
);
805 *mtod(opts
, struct in_addr
*) = zeroin_addr
;
810 printf("icmp_reflect optlen %d rt %d => ",
811 optlen
, opts
->m_len
);
813 for (cnt
= optlen
; cnt
> 0; cnt
-= len
, cp
+= len
) {
814 opt
= cp
[IPOPT_OPTVAL
];
815 if (opt
== IPOPT_EOL
)
817 if (opt
== IPOPT_NOP
)
820 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
822 len
= cp
[IPOPT_OLEN
];
823 if (len
< IPOPT_OLEN
+ sizeof(*cp
) ||
828 * Should check for overflow, but it "can't happen"
830 if (opt
== IPOPT_RR
|| opt
== IPOPT_TS
||
831 opt
== IPOPT_SECURITY
) {
832 memmove(mtod(opts
, char *) + opts
->m_len
,
837 /* Terminate & pad, if necessary */
838 if ((cnt
= opts
->m_len
% 4) != 0) {
839 for (; cnt
< 4; cnt
++) {
840 *(mtod(opts
, char *) + opts
->m_len
) =
847 printf("%d\n", opts
->m_len
);
851 * Now strip out original options by copying rest of first
852 * mbuf's data back, and adjust the IP length.
854 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - optlen
);
855 ip
->ip_hl
= sizeof(struct ip
) >> 2;
857 if (m
->m_flags
& M_PKTHDR
)
858 m
->m_pkthdr
.len
-= optlen
;
859 optlen
+= sizeof(struct ip
);
860 memmove(ip
+ 1, (char *)ip
+ optlen
,
861 (unsigned)(m
->m_len
- sizeof(struct ip
)));
863 m_tag_delete_nonpersistent(m
);
864 m
->m_flags
&= ~(M_BCAST
|M_MCAST
);
867 * Clear any in-bound checksum flags for this packet.
869 if (m
->m_flags
& M_PKTHDR
)
870 m
->m_pkthdr
.csum_flags
= 0;
879 * Send an icmp packet back to the ip level,
880 * after supplying a checksum.
883 icmp_send(struct mbuf
*m
, struct mbuf
*opts
)
885 struct ip
*ip
= mtod(m
, struct ip
*);
889 hlen
= ip
->ip_hl
<< 2;
892 icp
= mtod(m
, struct icmp
*);
894 icp
->icmp_cksum
= in_cksum(m
, ntohs(ip
->ip_len
) - hlen
);
899 printf("icmp_send to destination `%s' from `%s'\n",
900 inet_ntoa(ip
->ip_dst
), inet_ntoa(ip
->ip_src
));
903 (void)ip_output(m
, opts
, NULL
, 0, NULL
, NULL
);
913 t
= (atv
.tv_sec
% (24*60*60)) * 1000 + atv
.tv_usec
/ 1000;
918 * sysctl helper routine for net.inet.icmp.returndatabytes. ensures
919 * that the new value is in the correct range.
922 sysctl_net_inet_icmp_returndatabytes(SYSCTLFN_ARGS
)
925 struct sysctlnode node
;
928 node
.sysctl_data
= &t
;
929 t
= icmpreturndatabytes
;
930 error
= sysctl_lookup(SYSCTLFN_CALL(&node
));
931 if (error
|| newp
== NULL
)
934 if (t
< 8 || t
> 512)
936 icmpreturndatabytes
= t
;
942 * sysctl helper routine for net.inet.icmp.redirtimeout. ensures that
943 * the given value is not less than zero and then resets the timeout
947 sysctl_net_inet_icmp_redirtimeout(SYSCTLFN_ARGS
)
950 struct sysctlnode node
;
953 node
.sysctl_data
= &tmp
;
954 tmp
= icmp_redirtimeout
;
955 error
= sysctl_lookup(SYSCTLFN_CALL(&node
));
956 if (error
|| newp
== NULL
)
960 icmp_redirtimeout
= tmp
;
963 * was it a *defined* side-effect that anyone even *reading*
964 * this value causes these things to happen?
966 if (icmp_redirect_timeout_q
!= NULL
) {
967 if (icmp_redirtimeout
== 0) {
968 rt_timer_queue_destroy(icmp_redirect_timeout_q
,
970 icmp_redirect_timeout_q
= NULL
;
972 rt_timer_queue_change(icmp_redirect_timeout_q
,
975 } else if (icmp_redirtimeout
> 0) {
976 icmp_redirect_timeout_q
=
977 rt_timer_queue_create(icmp_redirtimeout
);
984 sysctl_net_inet_icmp_stats(SYSCTLFN_ARGS
)
987 return (NETSTAT_SYSCTL(icmpstat_percpu
, ICMP_NSTATS
));
991 sysctl_netinet_icmp_setup(struct sysctllog
**clog
)
994 sysctl_createv(clog
, 0, NULL
, NULL
,
996 CTLTYPE_NODE
, "net", NULL
,
999 sysctl_createv(clog
, 0, NULL
, NULL
,
1001 CTLTYPE_NODE
, "inet", NULL
,
1003 CTL_NET
, PF_INET
, CTL_EOL
);
1004 sysctl_createv(clog
, 0, NULL
, NULL
,
1006 CTLTYPE_NODE
, "icmp",
1007 SYSCTL_DESCR("ICMPv4 related settings"),
1009 CTL_NET
, PF_INET
, IPPROTO_ICMP
, CTL_EOL
);
1011 sysctl_createv(clog
, 0, NULL
, NULL
,
1012 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
1013 CTLTYPE_INT
, "maskrepl",
1014 SYSCTL_DESCR("Respond to ICMP_MASKREQ messages"),
1015 NULL
, 0, &icmpmaskrepl
, 0,
1016 CTL_NET
, PF_INET
, IPPROTO_ICMP
,
1017 ICMPCTL_MASKREPL
, CTL_EOL
);
1018 sysctl_createv(clog
, 0, NULL
, NULL
,
1019 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
1020 CTLTYPE_INT
, "returndatabytes",
1021 SYSCTL_DESCR("Number of bytes to return in an ICMP "
1023 sysctl_net_inet_icmp_returndatabytes
, 0,
1024 &icmpreturndatabytes
, 0,
1025 CTL_NET
, PF_INET
, IPPROTO_ICMP
,
1026 ICMPCTL_RETURNDATABYTES
, CTL_EOL
);
1027 sysctl_createv(clog
, 0, NULL
, NULL
,
1028 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
1029 CTLTYPE_INT
, "errppslimit",
1030 SYSCTL_DESCR("Maximum number of outgoing ICMP error "
1031 "messages per second"),
1032 NULL
, 0, &icmperrppslim
, 0,
1033 CTL_NET
, PF_INET
, IPPROTO_ICMP
,
1034 ICMPCTL_ERRPPSLIMIT
, CTL_EOL
);
1035 sysctl_createv(clog
, 0, NULL
, NULL
,
1036 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
1037 CTLTYPE_INT
, "rediraccept",
1038 SYSCTL_DESCR("Accept ICMP_REDIRECT messages"),
1039 NULL
, 0, &icmp_rediraccept
, 0,
1040 CTL_NET
, PF_INET
, IPPROTO_ICMP
,
1041 ICMPCTL_REDIRACCEPT
, CTL_EOL
);
1042 sysctl_createv(clog
, 0, NULL
, NULL
,
1043 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
1044 CTLTYPE_INT
, "redirtimeout",
1045 SYSCTL_DESCR("Lifetime of ICMP_REDIRECT generated "
1047 sysctl_net_inet_icmp_redirtimeout
, 0,
1048 &icmp_redirtimeout
, 0,
1049 CTL_NET
, PF_INET
, IPPROTO_ICMP
,
1050 ICMPCTL_REDIRTIMEOUT
, CTL_EOL
);
1051 sysctl_createv(clog
, 0, NULL
, NULL
,
1053 CTLTYPE_STRUCT
, "stats",
1054 SYSCTL_DESCR("ICMP statistics"),
1055 sysctl_net_inet_icmp_stats
, 0, NULL
, 0,
1056 CTL_NET
, PF_INET
, IPPROTO_ICMP
, ICMPCTL_STATS
,
1061 icmp_statinc(u_int stat
)
1064 KASSERT(stat
< ICMP_NSTATS
);
1068 /* Table of common MTUs: */
1070 static const u_int mtu_table
[] = {
1071 65535, 65280, 32000, 17914, 9180, 8166,
1072 4352, 2002, 1492, 1006, 508, 296, 68, 0
1076 icmp_mtudisc(struct icmp
*icp
, struct in_addr faddr
)
1078 struct icmp_mtudisc_callback
*mc
;
1079 struct sockaddr
*dst
= sintosa(&icmpsrc
);
1081 u_long mtu
= ntohs(icp
->icmp_nextmtu
); /* Why a long? IPv6 */
1084 rt
= rtalloc1(dst
, 1);
1088 /* If we didn't get a host route, allocate one */
1090 if ((rt
->rt_flags
& RTF_HOST
) == 0) {
1091 struct rtentry
*nrt
;
1093 error
= rtrequest((int) RTM_ADD
, dst
,
1094 (struct sockaddr
*) rt
->rt_gateway
,
1095 (struct sockaddr
*) 0,
1096 RTF_GATEWAY
| RTF_HOST
| RTF_DYNAMIC
, &nrt
);
1101 nrt
->rt_rmx
= rt
->rt_rmx
;
1105 error
= rt_timer_add(rt
, icmp_mtudisc_timeout
, ip_mtudisc_timeout_q
);
1114 mtu
= ntohs(icp
->icmp_ip
.ip_len
);
1115 /* Some 4.2BSD-based routers incorrectly adjust the ip_len */
1116 if (mtu
> rt
->rt_rmx
.rmx_mtu
&& rt
->rt_rmx
.rmx_mtu
!= 0)
1117 mtu
-= (icp
->icmp_ip
.ip_hl
<< 2);
1119 /* If we still can't guess a value, try the route */
1122 mtu
= rt
->rt_rmx
.rmx_mtu
;
1124 /* If no route mtu, default to the interface mtu */
1127 mtu
= rt
->rt_ifp
->if_mtu
;
1130 for (i
= 0; i
< sizeof(mtu_table
) / sizeof(mtu_table
[0]); i
++)
1131 if (mtu
> mtu_table
[i
]) {
1138 * XXX: RTV_MTU is overloaded, since the admin can set it
1139 * to turn off PMTU for a route, and the kernel can
1140 * set it to indicate a serious problem with PMTU
1141 * on a route. We should be using a separate flag
1142 * for the kernel to indicate this.
1145 if ((rt
->rt_rmx
.rmx_locks
& RTV_MTU
) == 0) {
1146 if (mtu
< 296 || mtu
> rt
->rt_ifp
->if_mtu
)
1147 rt
->rt_rmx
.rmx_locks
|= RTV_MTU
;
1148 else if (rt
->rt_rmx
.rmx_mtu
> mtu
||
1149 rt
->rt_rmx
.rmx_mtu
== 0) {
1150 ICMP_STATINC(ICMP_STAT_PMTUCHG
);
1151 rt
->rt_rmx
.rmx_mtu
= mtu
;
1159 * Notify protocols that the MTU for this destination
1162 for (mc
= LIST_FIRST(&icmp_mtudisc_callbacks
); mc
!= NULL
;
1163 mc
= LIST_NEXT(mc
, mc_list
))
1164 (*mc
->mc_func
)(faddr
);
1168 * Return the next larger or smaller MTU plateau (table from RFC 1191)
1169 * given current value MTU. If DIR is less than zero, a larger plateau
1170 * is returned; otherwise, a smaller value is returned.
1173 ip_next_mtu(u_int mtu
, int dir
) /* XXX */
1177 for (i
= 0; i
< (sizeof mtu_table
) / (sizeof mtu_table
[0]); i
++) {
1178 if (mtu
>= mtu_table
[i
])
1186 return mtu_table
[i
- 1];
1189 if (mtu_table
[i
] == 0) {
1191 } else if (mtu
> mtu_table
[i
]) {
1192 return mtu_table
[i
];
1194 return mtu_table
[i
+ 1];
1200 icmp_mtudisc_timeout(struct rtentry
*rt
, struct rttimer
*r
)
1203 panic("icmp_mtudisc_timeout: bad route to timeout");
1204 if ((rt
->rt_flags
& (RTF_DYNAMIC
| RTF_HOST
)) ==
1205 (RTF_DYNAMIC
| RTF_HOST
)) {
1206 rtrequest((int) RTM_DELETE
, rt_getkey(rt
),
1207 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, 0);
1209 if ((rt
->rt_rmx
.rmx_locks
& RTV_MTU
) == 0) {
1210 rt
->rt_rmx
.rmx_mtu
= 0;
1216 icmp_redirect_timeout(struct rtentry
*rt
, struct rttimer
*r
)
1219 panic("icmp_redirect_timeout: bad route to timeout");
1220 if ((rt
->rt_flags
& (RTF_DYNAMIC
| RTF_HOST
)) ==
1221 (RTF_DYNAMIC
| RTF_HOST
)) {
1222 rtrequest((int) RTM_DELETE
, rt_getkey(rt
),
1223 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, 0);
1228 * Perform rate limit check.
1229 * Returns 0 if it is okay to send the icmp packet.
1230 * Returns 1 if the router SHOULD NOT send this icmp packet due to rate
1233 * XXX per-destination/type check necessary?
1236 icmp_ratelimit(const struct in_addr
*dst
, const int type
,
1241 if (!ppsratecheck(&icmperrppslim_last
, &icmperrpps_count
,
1243 /* The packet is subject to rate limit */