2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
63 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
64 * support for mandatory and extensible security protections. This notice
65 * is included in support of clause 2.2 (b) of the Apple Public License,
69 #include <sys/param.h>
70 #include <sys/systm.h>
72 #include <sys/mcache.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
76 #include <sys/kernel.h>
77 #include <sys/sysctl.h>
79 #include <machine/endian.h>
82 #include <net/route.h>
83 #include <net/content_filter.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip.h>
90 #include <netinet/ip_icmp.h>
91 #include <netinet/ip_var.h>
92 #include <netinet/icmp_var.h>
93 #include <netinet/tcp.h>
94 #include <netinet/tcp_fsm.h>
95 #include <netinet/tcp_seq.h>
96 #include <netinet/tcp_timer.h>
97 #include <netinet/tcp_var.h>
98 #include <netinet/tcpip.h>
101 #include <netinet6/ipsec.h>
102 #include <netkey/key.h>
106 #include <net/necp.h>
111 * ICMP routines: error generation, receive packet processing, and
112 * routines to turnaround packets back to the originator, and
113 * host table maintenance routines.
116 struct icmpstat icmpstat
;
117 SYSCTL_STRUCT(_net_inet_icmp
, ICMPCTL_STATS
, stats
,
118 CTLFLAG_RD
| CTLFLAG_LOCKED
,
119 &icmpstat
, icmpstat
, "");
121 static int icmpmaskrepl
= 0;
122 SYSCTL_INT(_net_inet_icmp
, ICMPCTL_MASKREPL
, maskrepl
,
123 CTLFLAG_RW
| CTLFLAG_LOCKED
,
124 &icmpmaskrepl
, 0, "");
126 static int icmptimestamp
= 0;
127 SYSCTL_INT(_net_inet_icmp
, ICMPCTL_TIMESTAMP
, timestamp
,
128 CTLFLAG_RW
| CTLFLAG_LOCKED
,
129 &icmptimestamp
, 0, "");
131 static int drop_redirect
= 1;
132 SYSCTL_INT(_net_inet_icmp
, OID_AUTO
, drop_redirect
,
133 CTLFLAG_RW
| CTLFLAG_LOCKED
,
134 &drop_redirect
, 0, "");
136 static int log_redirect
= 0;
137 SYSCTL_INT(_net_inet_icmp
, OID_AUTO
, log_redirect
,
138 CTLFLAG_RW
| CTLFLAG_LOCKED
,
139 &log_redirect
, 0, "");
141 const static int icmp_datalen
= 8;
144 /* Default values in case CONFIG_ICMP_BANDLIM is not defined in the MASTER file */
145 #ifndef CONFIG_ICMP_BANDLIM
146 #if XNU_TARGET_OS_OSX
147 #define CONFIG_ICMP_BANDLIM 250
148 #else /* !XNU_TARGET_OS_OSX */
149 #define CONFIG_ICMP_BANDLIM 50
150 #endif /* !XNU_TARGET_OS_OSX */
151 #endif /* CONFIG_ICMP_BANDLIM */
154 * ICMP error-response bandwidth limiting sysctl. If not enabled, sysctl
155 * variable content is -1 and read-only.
158 static int icmplim
= CONFIG_ICMP_BANDLIM
;
159 SYSCTL_INT(_net_inet_icmp
, ICMPCTL_ICMPLIM
, icmplim
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
161 #else /* ICMP_BANDLIM */
162 static int icmplim
= -1;
163 SYSCTL_INT(_net_inet_icmp
, ICMPCTL_ICMPLIM
, icmplim
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
165 #endif /* ICMP_BANDLIM */
167 static int icmplim_random_incr
= CONFIG_ICMP_BANDLIM
;
168 SYSCTL_INT(_net_inet_icmp
, ICMPCTL_ICMPLIM_INCR
, icmplim_random_incr
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
169 &icmplim_random_incr
, 0, "");
172 * ICMP broadcast echo sysctl
175 static int icmpbmcastecho
= 1;
176 SYSCTL_INT(_net_inet_icmp
, OID_AUTO
, bmcastecho
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
177 &icmpbmcastecho
, 0, "");
179 #if (DEBUG | DEVELOPMENT)
180 static int icmpprintfs
= 0;
181 SYSCTL_INT(_net_inet_icmp
, OID_AUTO
, verbose
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
182 &icmpprintfs
, 0, "");
185 static void icmp_reflect(struct mbuf
*);
186 static void icmp_send(struct mbuf
*, struct mbuf
*);
189 * Generate an error packet of type error
190 * in response to bad packet ip.
200 struct ip
*oip
= NULL
;
201 struct ip
*nip
= NULL
;
202 struct icmp
*icp
= NULL
;
203 struct mbuf
*m
= NULL
;
204 u_int32_t oiphlen
= 0;
205 u_int32_t icmplen
= 0;
206 u_int32_t icmpelen
= 0;
209 VERIFY((u_int
)type
<= ICMP_MAXTYPE
);
210 VERIFY(code
<= UINT8_MAX
);
212 /* Expect 32-bit aligned data pointer on strict-align platforms */
213 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(n
);
215 if (type
!= ICMP_REDIRECT
) {
216 icmpstat
.icps_error
++;
220 * if not the first fragment of message
221 * if original packet was a multicast or broadcast packet
222 * if the old packet protocol was ICMP
223 * error message, only known informational types.
225 if (n
->m_flags
& (M_BCAST
| M_MCAST
)) {
230 * Drop if IP header plus ICMP_MINLEN bytes are not contiguous
233 if (n
->m_len
< sizeof(struct ip
) + ICMP_MINLEN
) {
237 oip
= mtod(n
, struct ip
*);
238 oiphlen
= IP_VHL_HL(oip
->ip_vhl
) << 2;
239 if (n
->m_len
< oiphlen
+ ICMP_MINLEN
) {
243 #if (DEBUG | DEVELOPMENT)
244 if (icmpprintfs
> 1) {
245 printf("icmp_error(0x%llx, %x, %d)\n",
246 (uint64_t)VM_KERNEL_ADDRPERM(oip
), type
, code
);
250 if (oip
->ip_off
& ~(IP_MF
| IP_DF
)) {
254 if (oip
->ip_p
== IPPROTO_ICMP
&& type
!= ICMP_REDIRECT
&&
255 n
->m_len
>= oiphlen
+ ICMP_MINLEN
&&
256 !ICMP_INFOTYPE(((struct icmp
*)(void *)((caddr_t
)oip
+ oiphlen
))->
258 icmpstat
.icps_oldicmp
++;
263 * Calculate the length to quote from original packet and prevent
264 * the ICMP mbuf from overflowing.
265 * Unfortunatly this is non-trivial since ip_forward()
266 * sends us truncated packets.
269 if (oip
->ip_p
== IPPROTO_TCP
) {
270 struct tcphdr
*th
= NULL
;
271 u_int16_t tcphlen
= 0;
274 * If the packet got truncated and TCP header
275 * is not contained in the packet, send out
276 * standard reply with only IP header as payload
278 if (oiphlen
+ sizeof(struct tcphdr
) > n
->m_len
&&
284 * Otherwise, pull up to get IP and TCP headers
287 if (n
->m_len
< (oiphlen
+ sizeof(struct tcphdr
)) &&
288 (n
= m_pullup(n
, (oiphlen
+ sizeof(struct tcphdr
)))) == NULL
) {
293 * Reinit pointers derived from mbuf data pointer
294 * as things might have moved around with m_pullup
296 oip
= mtod(n
, struct ip
*);
297 th
= (struct tcphdr
*)(void *)((caddr_t
)oip
+ oiphlen
);
299 if (th
!= ((struct tcphdr
*)P2ROUNDDOWN(th
,
300 sizeof(u_int32_t
))) ||
301 ((th
->th_off
<< 2) > UINT16_MAX
)) {
304 tcphlen
= (uint16_t)(th
->th_off
<< 2);
307 if (tcphlen
< sizeof(struct tcphdr
)) {
310 if (oip
->ip_len
< (oiphlen
+ tcphlen
)) {
313 if ((oiphlen
+ tcphlen
) > n
->m_len
&& n
->m_next
== NULL
) {
316 if (n
->m_len
< (oiphlen
+ tcphlen
) &&
317 (n
= m_pullup(n
, (oiphlen
+ tcphlen
))) == NULL
) {
322 * Reinit pointers derived from mbuf data pointer
323 * as things might have moved around with m_pullup
325 oip
= mtod(n
, struct ip
*);
326 th
= (struct tcphdr
*)(void *)((caddr_t
)oip
+ oiphlen
);
328 icmpelen
= max(tcphlen
, min(icmp_datalen
,
329 (oip
->ip_len
- oiphlen
)));
331 stdreply
: icmpelen
= max(ICMP_MINLEN
, min(icmp_datalen
,
332 (oip
->ip_len
- oiphlen
)));
335 icmplen
= min(oiphlen
+ icmpelen
, nlen
);
336 if (icmplen
< sizeof(struct ip
)) {
341 * First, formulate icmp message
342 * Allocate enough space for the IP header, ICMP header
343 * and the payload (part of the original message to be sent back).
345 if (MHLEN
> (sizeof(struct ip
) + ICMP_MINLEN
+ icmplen
)) {
346 m
= m_gethdr(M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
348 m
= m_getcl(M_DONTWAIT
, MT_DATA
, M_PKTHDR
);
356 * Further refine the payload length to the space
357 * remaining in mbuf after including the IP header and ICMP
360 icmplen
= min(icmplen
, (u_int
)M_TRAILINGSPACE(m
) -
361 (u_int
)(sizeof(struct ip
) - ICMP_MINLEN
));
362 m_align(m
, ICMP_MINLEN
+ icmplen
);
363 m
->m_len
= ICMP_MINLEN
+ icmplen
; /* for ICMP header and data */
365 icp
= mtod(m
, struct icmp
*);
366 icmpstat
.icps_outhist
[type
]++;
367 icp
->icmp_type
= (u_char
)type
;
368 if (type
== ICMP_REDIRECT
) {
369 icp
->icmp_gwaddr
.s_addr
= dest
;
373 * The following assignments assume an overlay with the
374 * zeroed icmp_void field.
376 if (type
== ICMP_PARAMPROB
) {
377 icp
->icmp_pptr
= (u_char
)code
;
379 } else if (type
== ICMP_UNREACH
&&
380 code
== ICMP_UNREACH_NEEDFRAG
&& nextmtu
!= 0) {
381 icp
->icmp_nextmtu
= htons((uint16_t)nextmtu
);
385 icp
->icmp_code
= (u_char
)code
;
388 * Copy icmplen worth of content from original
389 * mbuf (n) to the new packet after ICMP header.
391 m_copydata(n
, 0, icmplen
, (caddr_t
)&icp
->icmp_ip
);
395 * Convert fields to network representation.
397 #if BYTE_ORDER != BIG_ENDIAN
402 * Set up ICMP message mbuf and copy old IP header (without options
403 * in front of ICMP message.
405 m
->m_data
-= sizeof(struct ip
);
406 m
->m_len
+= sizeof(struct ip
);
407 m
->m_pkthdr
.len
= m
->m_len
;
408 m
->m_pkthdr
.rcvif
= n
->m_pkthdr
.rcvif
;
409 nip
= mtod(m
, struct ip
*);
410 bcopy((caddr_t
)oip
, (caddr_t
)nip
, sizeof(struct ip
));
411 nip
->ip_len
= (uint16_t)m
->m_len
;
412 nip
->ip_vhl
= IP_VHL_BORING
;
413 nip
->ip_p
= IPPROTO_ICMP
;
422 * Process a received ICMP message.
425 icmp_input(struct mbuf
*m
, int hlen
)
427 struct sockaddr_in icmpsrc
, icmpdst
, icmpgw
;
429 struct ip
*ip
= mtod(m
, struct ip
*);
432 struct in_ifaddr
*ia
;
433 void (*ctlfunc
)(int, struct sockaddr
*, void *, struct ifnet
*);
435 boolean_t should_log_redirect
= false;
437 /* Expect 32-bit aligned data pointer on strict-align platforms */
438 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
440 icmplen
= ip
->ip_len
;
443 * Locate icmp structure in mbuf, and check
444 * that not corrupted and of at least minimum length.
446 #if (DEBUG | DEVELOPMENT)
447 if (icmpprintfs
> 2) {
448 char src_str
[MAX_IPv4_STR_LEN
];
449 char dst_str
[MAX_IPv4_STR_LEN
];
451 inet_ntop(AF_INET
, &ip
->ip_src
, src_str
, sizeof(src_str
));
452 inet_ntop(AF_INET
, &ip
->ip_dst
, dst_str
, sizeof(dst_str
));
453 printf("%s: from %s to %s, len %d\n",
454 __func__
, src_str
, dst_str
, icmplen
);
457 if (icmplen
< ICMP_MINLEN
) {
458 icmpstat
.icps_tooshort
++;
461 i
= hlen
+ min(icmplen
, ICMP_ADVLENMIN
);
462 if (m
->m_len
< i
&& (m
= m_pullup(m
, i
)) == 0) {
463 icmpstat
.icps_tooshort
++;
466 ip
= mtod(m
, struct ip
*);
469 icp
= mtod(m
, struct icmp
*);
470 if (in_cksum(m
, icmplen
)) {
471 icmpstat
.icps_checksum
++;
477 #if (DEBUG | DEVELOPMENT)
478 if (icmpprintfs
> 2) {
479 printf("icmp_input, type %d code %d\n", icp
->icmp_type
,
485 * Message type specific processing.
487 if (icp
->icmp_type
> ICMP_MAXTYPE
) {
492 bzero(&icmpsrc
, sizeof(icmpsrc
));
493 icmpsrc
.sin_len
= sizeof(struct sockaddr_in
);
494 icmpsrc
.sin_family
= AF_INET
;
495 bzero(&icmpdst
, sizeof(icmpdst
));
496 icmpdst
.sin_len
= sizeof(struct sockaddr_in
);
497 icmpdst
.sin_family
= AF_INET
;
498 bzero(&icmpgw
, sizeof(icmpgw
));
499 icmpgw
.sin_len
= sizeof(struct sockaddr_in
);
500 icmpgw
.sin_family
= AF_INET
;
502 icmpstat
.icps_inhist
[icp
->icmp_type
]++;
503 code
= icp
->icmp_code
;
504 switch (icp
->icmp_type
) {
507 case ICMP_UNREACH_NET
:
508 case ICMP_UNREACH_HOST
:
509 case ICMP_UNREACH_SRCFAIL
:
510 case ICMP_UNREACH_NET_UNKNOWN
:
511 case ICMP_UNREACH_HOST_UNKNOWN
:
512 case ICMP_UNREACH_ISOLATED
:
513 case ICMP_UNREACH_TOSNET
:
514 case ICMP_UNREACH_TOSHOST
:
515 case ICMP_UNREACH_HOST_PRECEDENCE
:
516 case ICMP_UNREACH_PRECEDENCE_CUTOFF
:
517 code
= PRC_UNREACH_NET
;
520 case ICMP_UNREACH_NEEDFRAG
:
525 * RFC 1122, Sections 3.2.2.1 and 4.2.3.9.
526 * Treat subcodes 2,3 as immediate RST
528 case ICMP_UNREACH_PROTOCOL
:
529 case ICMP_UNREACH_PORT
:
530 code
= PRC_UNREACH_PORT
;
533 case ICMP_UNREACH_NET_PROHIB
:
534 case ICMP_UNREACH_HOST_PROHIB
:
535 case ICMP_UNREACH_FILTER_PROHIB
:
536 code
= PRC_UNREACH_ADMIN_PROHIB
;
548 code
+= PRC_TIMXCEED_INTRANS
;
555 code
= PRC_PARAMPROB
;
558 case ICMP_SOURCEQUENCH
:
565 * Problem with datagram; advise higher level routines.
567 if (icmplen
< ICMP_ADVLENMIN
|| icmplen
< ICMP_ADVLEN(icp
)
568 || IP_VHL_HL(icp
->icmp_ip
.ip_vhl
) <
569 (sizeof(struct ip
) >> 2) ||
570 (m
= m_pullup(m
, hlen
+ ICMP_ADVLEN(icp
))) == NULL
) {
571 icmpstat
.icps_badlen
++;
575 ip
= mtod(m
, struct ip
*);
576 icp
= (struct icmp
*)(void *)(mtod(m
, uint8_t *) + hlen
);
578 #if BYTE_ORDER != BIG_ENDIAN
579 NTOHS(icp
->icmp_ip
.ip_len
);
582 /* Discard ICMP's in response to multicast packets */
583 if (IN_MULTICAST(ntohl(icp
->icmp_ip
.ip_dst
.s_addr
))) {
586 #if (DEBUG | DEVELOPMENT)
587 if (icmpprintfs
> 2) {
588 printf("deliver to protocol %d\n",
592 icmpsrc
.sin_addr
= icp
->icmp_ip
.ip_dst
;
595 * if the packet contains [IPv4 AH TCP], we can't make a
596 * notification to TCP layer.
598 ctlfunc
= ip_protox
[icp
->icmp_ip
.ip_p
]->pr_ctlinput
;
601 LCK_MTX_ASSERT(inet_domain_mutex
, LCK_MTX_ASSERT_OWNED
);
603 lck_mtx_unlock(inet_domain_mutex
);
605 (*ctlfunc
)(code
, (struct sockaddr
*)&icmpsrc
,
606 (void *)&icp
->icmp_ip
, m
->m_pkthdr
.rcvif
);
608 lck_mtx_lock(inet_domain_mutex
);
613 icmpstat
.icps_badcode
++;
617 if ((m
->m_flags
& (M_MCAST
| M_BCAST
))) {
618 if (icmpbmcastecho
== 0) {
619 icmpstat
.icps_bmcastecho
++;
626 * Do not reply when the destination is link local multicast or broadcast
627 * and the source is not from a directly connected subnet
629 if ((IN_LOCAL_GROUP(ntohl(ip
->ip_dst
.s_addr
)) ||
630 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
)) &&
631 in_localaddr(ip
->ip_src
) == 0) {
632 icmpstat
.icps_bmcastecho
++;
633 #if (DEBUG | DEVELOPMENT)
634 if (icmpprintfs
> 0) {
635 char src_str
[MAX_IPv4_STR_LEN
];
636 char dst_str
[MAX_IPv4_STR_LEN
];
638 inet_ntop(AF_INET
, &ip
->ip_src
, src_str
, sizeof(src_str
));
639 inet_ntop(AF_INET
, &ip
->ip_dst
, dst_str
, sizeof(dst_str
));
640 printf("%s: non local (B|M)CAST %s to %s, len %d\n",
641 __func__
, src_str
, dst_str
, icmplen
);
647 icp
->icmp_type
= ICMP_ECHOREPLY
;
649 if (badport_bandlim(BANDLIM_ICMP_ECHO
)) {
656 if (icmptimestamp
== 0) {
661 && (m
->m_flags
& (M_MCAST
| M_BCAST
)) != 0) {
662 icmpstat
.icps_bmcasttstamp
++;
665 if (icmplen
< ICMP_TSLEN
) {
666 icmpstat
.icps_badlen
++;
669 icp
->icmp_type
= ICMP_TSTAMPREPLY
;
670 icp
->icmp_rtime
= iptime();
671 icp
->icmp_ttime
= icp
->icmp_rtime
; /* bogus, do later! */
673 if (badport_bandlim(BANDLIM_ICMP_TSTAMP
)) {
680 if (icmpmaskrepl
== 0) {
684 * We are not able to respond with all ones broadcast
685 * unless we receive it over a point-to-point interface.
687 if (icmplen
< ICMP_MASKLEN
) {
690 switch (ip
->ip_dst
.s_addr
) {
691 case INADDR_BROADCAST
:
693 icmpdst
.sin_addr
= ip
->ip_src
;
697 icmpdst
.sin_addr
= ip
->ip_dst
;
699 ia
= (struct in_ifaddr
*)ifaof_ifpforaddr(
700 (struct sockaddr
*)&icmpdst
, m
->m_pkthdr
.rcvif
);
704 IFA_LOCK(&ia
->ia_ifa
);
705 if (ia
->ia_ifp
== 0) {
706 IFA_UNLOCK(&ia
->ia_ifa
);
707 IFA_REMREF(&ia
->ia_ifa
);
711 icp
->icmp_type
= ICMP_MASKREPLY
;
712 icp
->icmp_mask
= ia
->ia_sockmask
.sin_addr
.s_addr
;
713 if (ip
->ip_src
.s_addr
== 0) {
714 if (ia
->ia_ifp
->if_flags
& IFF_BROADCAST
) {
715 ip
->ip_src
= satosin(&ia
->ia_broadaddr
)->sin_addr
;
716 } else if (ia
->ia_ifp
->if_flags
& IFF_POINTOPOINT
) {
717 ip
->ip_src
= satosin(&ia
->ia_dstaddr
)->sin_addr
;
720 IFA_UNLOCK(&ia
->ia_ifa
);
721 IFA_REMREF(&ia
->ia_ifa
);
723 ip
->ip_len
+= hlen
; /* since ip_input deducts this */
724 icmpstat
.icps_reflect
++;
725 icmpstat
.icps_outhist
[icp
->icmp_type
]++;
736 if (icmplen
< ICMP_ADVLENMIN
|| icmplen
< ICMP_ADVLEN(icp
) ||
737 IP_VHL_HL(icp
->icmp_ip
.ip_vhl
) < (sizeof(struct ip
) >> 2)) {
738 icmpstat
.icps_badlen
++;
742 #if (DEBUG | DEVELOPMENT)
743 should_log_redirect
= log_redirect
|| (icmpprintfs
> 0);
745 should_log_redirect
= log_redirect
;
748 * Short circuit routing redirects to force
749 * immediate change in the kernel's routing
750 * tables. The message is also handed to anyone
751 * listening on a raw socket (e.g. the routing
752 * daemon for use in updating its tables).
754 icmpgw
.sin_addr
= ip
->ip_src
;
755 icmpdst
.sin_addr
= icp
->icmp_gwaddr
;
757 if (should_log_redirect
) {
758 char src_str
[MAX_IPv4_STR_LEN
];
759 char dst_str
[MAX_IPv4_STR_LEN
];
760 char gw_str
[MAX_IPv4_STR_LEN
];
762 inet_ntop(AF_INET
, &ip
->ip_src
, src_str
, sizeof(src_str
));
763 inet_ntop(AF_INET
, &icp
->icmp_ip
.ip_dst
, dst_str
, sizeof(dst_str
));
764 inet_ntop(AF_INET
, &icp
->icmp_gwaddr
, gw_str
, sizeof(gw_str
));
765 printf("%s: redirect dst %s to %s from %s\n", __func__
,
766 dst_str
, gw_str
, src_str
);
768 icmpsrc
.sin_addr
= icp
->icmp_ip
.ip_dst
;
769 rtredirect(m
->m_pkthdr
.rcvif
, (struct sockaddr
*)&icmpsrc
,
770 (struct sockaddr
*)&icmpdst
, NULL
, RTF_GATEWAY
| RTF_HOST
,
771 (struct sockaddr
*)&icmpgw
, NULL
);
772 pfctlinput(PRC_REDIRECT_HOST
, (struct sockaddr
*)&icmpsrc
);
774 key_sa_routechange((struct sockaddr
*)&icmpsrc
);
779 * No kernel processing for the following;
780 * just fall through to send to raw listener.
783 case ICMP_ROUTERADVERT
:
784 case ICMP_ROUTERSOLICIT
:
785 case ICMP_TSTAMPREPLY
:
801 * Reflect the ip packet back to the source
804 icmp_reflect(struct mbuf
*m
)
806 struct ip
*ip
= mtod(m
, struct ip
*);
807 struct sockaddr_in icmpdst
;
808 struct in_ifaddr
*ia
;
810 struct mbuf
*opts
= NULL
;
811 int optlen
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof(struct ip
);
813 if (!in_canforward(ip
->ip_src
) &&
814 ((ntohl(ip
->ip_src
.s_addr
) & IN_CLASSA_NET
) !=
815 (IN_LOOPBACKNET
<< IN_CLASSA_NSHIFT
))) {
816 m_freem(m
); /* Bad return address */
817 goto done
; /* Ip_output() will check for broadcast */
820 ip
->ip_dst
= ip
->ip_src
;
822 * If the incoming packet was addressed directly to us,
823 * use dst as the src for the reply. Otherwise (broadcast
824 * or anonymous), use the address which corresponds
825 * to the incoming interface.
827 lck_rw_lock_shared(in_ifaddr_rwlock
);
828 TAILQ_FOREACH(ia
, INADDR_HASH(t
.s_addr
), ia_hash
) {
829 IFA_LOCK(&ia
->ia_ifa
);
830 if (t
.s_addr
== IA_SIN(ia
)->sin_addr
.s_addr
) {
831 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
832 IFA_UNLOCK(&ia
->ia_ifa
);
835 IFA_UNLOCK(&ia
->ia_ifa
);
838 * Slow path; check for broadcast addresses. Find a source
839 * IP address to use when replying to the broadcast request;
840 * let IP handle the source interface selection work.
842 for (ia
= in_ifaddrhead
.tqh_first
; ia
; ia
= ia
->ia_link
.tqe_next
) {
843 IFA_LOCK(&ia
->ia_ifa
);
844 if (ia
->ia_ifp
&& (ia
->ia_ifp
->if_flags
& IFF_BROADCAST
) &&
845 t
.s_addr
== satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
) {
846 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
847 IFA_UNLOCK(&ia
->ia_ifa
);
850 IFA_UNLOCK(&ia
->ia_ifa
);
853 lck_rw_done(in_ifaddr_rwlock
);
856 bzero(&icmpdst
, sizeof(icmpdst
));
857 icmpdst
.sin_len
= sizeof(struct sockaddr_in
);
858 icmpdst
.sin_family
= AF_INET
;
859 icmpdst
.sin_addr
= t
;
860 if ((ia
== (struct in_ifaddr
*)0) && m
->m_pkthdr
.rcvif
) {
861 ia
= (struct in_ifaddr
*)ifaof_ifpforaddr(
862 (struct sockaddr
*)&icmpdst
, m
->m_pkthdr
.rcvif
);
865 * The following happens if the packet was not addressed to us,
866 * and was received on an interface with no IP address.
868 if (ia
== (struct in_ifaddr
*)0) {
869 lck_rw_lock_shared(in_ifaddr_rwlock
);
870 ia
= in_ifaddrhead
.tqh_first
;
871 if (ia
== (struct in_ifaddr
*)0) {/* no address yet, bail out */
872 lck_rw_done(in_ifaddr_rwlock
);
876 IFA_ADDREF(&ia
->ia_ifa
);
877 lck_rw_done(in_ifaddr_rwlock
);
879 IFA_LOCK_SPIN(&ia
->ia_ifa
);
880 t
= IA_SIN(ia
)->sin_addr
;
881 IFA_UNLOCK(&ia
->ia_ifa
);
883 ip
->ip_ttl
= (u_char
)ip_defttl
;
884 IFA_REMREF(&ia
->ia_ifa
);
893 * Retrieve any source routing from the incoming packet;
894 * add on any record-route or timestamp options.
896 cp
= (u_char
*) (ip
+ 1);
897 if ((opts
= ip_srcroute()) == 0 &&
898 (opts
= m_gethdr(M_DONTWAIT
, MT_HEADER
))) { /* MAC-OK */
899 opts
->m_len
= sizeof(struct in_addr
);
900 mtod(opts
, struct in_addr
*)->s_addr
= 0;
903 #if (DEBUG | DEVELOPMENT)
904 if (icmpprintfs
> 1) {
905 printf("icmp_reflect optlen %d rt %d => ",
906 optlen
, opts
->m_len
);
909 for (cnt
= optlen
; cnt
> 0; cnt
-= len
, cp
+= len
) {
910 opt
= cp
[IPOPT_OPTVAL
];
911 if (opt
== IPOPT_EOL
) {
914 if (opt
== IPOPT_NOP
) {
917 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
)) {
920 len
= cp
[IPOPT_OLEN
];
921 if (len
< IPOPT_OLEN
+ sizeof(*cp
) ||
927 * Should check for overflow, but it "can't happen"
929 if (opt
== IPOPT_RR
|| opt
== IPOPT_TS
||
930 opt
== IPOPT_SECURITY
) {
932 mtod(opts
, caddr_t
) + opts
->m_len
, len
);
936 /* Terminate & pad, if necessary */
937 cnt
= opts
->m_len
% 4;
939 for (; cnt
< 4; cnt
++) {
940 *(mtod(opts
, caddr_t
) + opts
->m_len
) =
945 #if (DEBUG | DEVELOPMENT)
946 if (icmpprintfs
> 1) {
947 printf("%d\n", opts
->m_len
);
952 * Now strip out original options by copying rest of first
953 * mbuf's data back, and adjust the IP length.
955 ip
->ip_len
-= optlen
;
956 ip
->ip_vhl
= IP_VHL_BORING
;
958 if (m
->m_flags
& M_PKTHDR
) {
959 m
->m_pkthdr
.len
-= optlen
;
961 optlen
+= sizeof(struct ip
);
962 bcopy((caddr_t
)ip
+ optlen
, (caddr_t
)(ip
+ 1),
963 (unsigned)(m
->m_len
- sizeof(struct ip
)));
965 m
->m_flags
&= ~(M_BCAST
| M_MCAST
);
974 * Send an icmp packet back to the ip level,
975 * after supplying a checksum.
978 icmp_send(struct mbuf
*m
, struct mbuf
*opts
)
980 struct ip
*ip
= mtod(m
, struct ip
*);
984 struct ip_out_args ipoa
;
986 bzero(&ipoa
, sizeof(ipoa
));
987 ipoa
.ipoa_boundif
= IFSCOPE_NONE
;
988 ipoa
.ipoa_flags
= IPOAF_SELECT_SRCIF
| IPOAF_BOUND_SRCADDR
;
989 ipoa
.ipoa_sotc
= SO_TC_UNSPEC
;
990 ipoa
.ipoa_netsvctype
= _NET_SERVICE_TYPE_UNSPEC
;
992 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
) && m
->m_pkthdr
.rcvif
!= NULL
) {
993 ipoa
.ipoa_boundif
= m
->m_pkthdr
.rcvif
->if_index
;
994 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
997 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1000 icp
= mtod(m
, struct icmp
*);
1001 icp
->icmp_cksum
= 0;
1002 icp
->icmp_cksum
= in_cksum(m
, ip
->ip_len
- hlen
);
1005 m
->m_pkthdr
.rcvif
= NULL
;
1006 m
->m_pkthdr
.csum_data
= 0;
1007 m
->m_pkthdr
.csum_flags
= 0;
1008 #if (DEBUG | DEVELOPMENT)
1009 if (icmpprintfs
> 2) {
1010 char src_str
[MAX_IPv4_STR_LEN
];
1011 char dst_str
[MAX_IPv4_STR_LEN
];
1013 inet_ntop(AF_INET
, &ip
->ip_src
, src_str
, sizeof(src_str
));
1014 inet_ntop(AF_INET
, &ip
->ip_dst
, dst_str
, sizeof(dst_str
));
1015 printf("%s: dst %s src %s\n", __func__
, dst_str
, src_str
);
1018 bzero(&ro
, sizeof ro
);
1019 (void) ip_output(m
, opts
, &ro
, IP_OUTARGS
, NULL
, &ipoa
);
1030 t
= (atv
.tv_sec
% (24 * 60 * 60)) * 1000 + atv
.tv_usec
/ 1000;
1036 * Return the next larger or smaller MTU plateau (table from RFC 1191)
1037 * given current value MTU. If DIR is less than zero, a larger plateau
1038 * is returned; otherwise, a smaller value is returned.
1041 ip_next_mtu(int mtu
, int dir
)
1043 static int mtutab
[] = {
1044 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1006, 508, 296,
1049 for (i
= 0; i
< (sizeof mtutab
) / (sizeof mtutab
[0]); i
++) {
1050 if (mtu
>= mtutab
[i
]) {
1059 return mtutab
[i
- 1];
1062 if (mtutab
[i
] == 0) {
1064 } else if (mtu
> mtutab
[i
]) {
1067 return mtutab
[i
+ 1];
1076 * badport_bandlim() - check for ICMP bandwidth limit
1077 * Returns false when it is ok to send ICMP error and true to limit sending
1080 * For now we separate the TCP and UDP subsystems w/ different 'which'
1081 * values. We may eventually remove this separation (and simplify the
1084 * Note that the printing of the error message is delayed so we can
1085 * properly print the icmp error rate that the system was trying to do
1086 * (i.e. 22000/100 pps, etc...). This can cause long delays in printing
1087 * the 'final' error, but it doesn't make sense to solve the printing
1088 * delay with more complex code.
1092 badport_bandlim(int which
)
1094 static uint64_t lticks
[BANDLIM_MAX
+ 1];
1095 static int lpackets
[BANDLIM_MAX
+ 1];
1098 static boolean_t is_initialized
= FALSE
;
1099 static int icmplim_random
;
1100 const char *bandlimittype
[] = {
1101 "Limiting icmp unreach response",
1102 "Limiting icmp ping response",
1103 "Limiting icmp tstamp response",
1104 "Limiting closed port RST response",
1105 "Limiting open port RST response"
1108 /* Return ok status if feature disabled or argument out of range. */
1110 if (icmplim
<= 0 || which
> BANDLIM_MAX
|| which
< 0) {
1114 if (is_initialized
== FALSE
) {
1115 if (icmplim_random_incr
> 0 &&
1116 icmplim
<= INT32_MAX
- (icmplim_random_incr
+ 1)) {
1117 icmplim_random
= icmplim
+ (random() % icmplim_random_incr
) + 1;
1119 is_initialized
= TRUE
;
1122 time
= net_uptime();
1123 secs
= time
- lticks
[which
];
1126 * reset stats when cumulative delta exceeds one second.
1130 if (lpackets
[which
] > icmplim_random
) {
1131 printf("%s from %d to %d packets per second\n",
1132 bandlimittype
[which
],
1137 lticks
[which
] = time
;
1138 lpackets
[which
] = 0;
1144 if (++lpackets
[which
] > icmplim_random
) {
1146 * After hitting the randomized limit, we further randomize the
1147 * behavior of how we apply rate limitation.
1148 * We rate limit based on probability that increases with the
1149 * increase in lpackets[which] count.
1151 if ((random() % (lpackets
[which
] - icmplim_random
)) != 0) {
1163 * Non-privileged ICMP socket operations
1164 * - send ICMP echo request
1166 * - limited socket options
1169 #include <netinet/ip_icmp.h>
1170 #include <netinet/in_pcb.h>
1172 extern u_int32_t rip_sendspace
;
1173 extern u_int32_t rip_recvspace
;
1174 extern struct inpcbinfo ripcbinfo
;
1176 int rip_abort(struct socket
*);
1177 int rip_bind(struct socket
*, struct sockaddr
*, struct proc
*);
1178 int rip_connect(struct socket
*, struct sockaddr
*, struct proc
*);
1179 int rip_detach(struct socket
*);
1180 int rip_disconnect(struct socket
*);
1181 int rip_shutdown(struct socket
*);
1183 __private_extern__
int icmp_dgram_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
);
1184 __private_extern__
int icmp_dgram_attach(struct socket
*so
, int proto
, struct proc
*p
);
1185 __private_extern__
int icmp_dgram_ctloutput(struct socket
*so
, struct sockopt
*sopt
);
1187 __private_extern__
struct pr_usrreqs icmp_dgram_usrreqs
= {
1188 .pru_abort
= rip_abort
,
1189 .pru_attach
= icmp_dgram_attach
,
1190 .pru_bind
= rip_bind
,
1191 .pru_connect
= rip_connect
,
1192 .pru_control
= in_control
,
1193 .pru_detach
= rip_detach
,
1194 .pru_disconnect
= rip_disconnect
,
1195 .pru_peeraddr
= in_getpeeraddr
,
1196 .pru_send
= icmp_dgram_send
,
1197 .pru_shutdown
= rip_shutdown
,
1198 .pru_sockaddr
= in_getsockaddr
,
1199 .pru_sosend
= sosend
,
1200 .pru_soreceive
= soreceive
,
1203 /* Like rip_attach but without root privilege enforcement */
1204 __private_extern__
int
1205 icmp_dgram_attach(struct socket
*so
, __unused
int proto
, struct proc
*p
)
1210 inp
= sotoinpcb(so
);
1212 panic("icmp_dgram_attach");
1215 error
= soreserve(so
, rip_sendspace
, rip_recvspace
);
1219 error
= in_pcballoc(so
, &ripcbinfo
, p
);
1223 inp
= (struct inpcb
*)so
->so_pcb
;
1224 inp
->inp_vflag
|= INP_IPV4
;
1225 inp
->inp_ip_p
= IPPROTO_ICMP
;
1226 inp
->inp_ip_ttl
= (u_char
)ip_defttl
;
1231 * Raw IP socket option processing.
1233 __private_extern__
int
1234 icmp_dgram_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
1238 if (sopt
->sopt_level
!= IPPROTO_IP
) {
1242 switch (sopt
->sopt_name
) {
1248 case IP_RECVRETOPTS
:
1249 case IP_RECVDSTADDR
:
1251 case IP_MULTICAST_IF
:
1252 case IP_MULTICAST_IFINDEX
:
1253 case IP_MULTICAST_TTL
:
1254 case IP_MULTICAST_LOOP
:
1255 case IP_ADD_MEMBERSHIP
:
1256 case IP_DROP_MEMBERSHIP
:
1257 case IP_MULTICAST_VIF
:
1260 case IP_IPSEC_POLICY
:
1265 case IP_NO_IFT_CELLULAR
:
1266 error
= rip_ctloutput(so
, sopt
);
1277 __private_extern__
int
1278 icmp_dgram_send(struct socket
*so
, int flags
, struct mbuf
*m
,
1279 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
1282 struct inpcb
*inp
= sotoinpcb(so
);
1285 struct in_ifaddr
*ia
= NULL
;
1288 int inp_flags
= inp
? inp
->inp_flags
: 0;
1292 || (necp_socket_should_use_flow_divert(inp
))
1303 * If socket is subject to Content Filter, get inp_flags from saved state
1305 if (so
->so_cfil_db
&& nam
== NULL
) {
1306 cfil_dgram_peek_socket_state(m
, &inp_flags
);
1310 if ((inp_flags
& INP_HDRINCL
) != 0) {
1311 /* Expect 32-bit aligned data ptr on strict-align platforms */
1312 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
1314 * This is not raw IP, we liberal only for fields TOS,
1317 ip
= mtod(m
, struct ip
*);
1319 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1320 /* Some sanity checks */
1321 if (m
->m_pkthdr
.len
< hlen
+ ICMP_MINLEN
) {
1325 if (IP_VHL_V(ip
->ip_vhl
) != 4) {
1328 if (hlen
< 20 || hlen
> 40 || ip
->ip_len
!= m
->m_pkthdr
.len
) {
1331 /* Bogus fragments can tie up peer resources */
1332 if ((ip
->ip_off
& ~IP_DF
) != 0) {
1335 /* Allow only ICMP even for user provided IP header */
1336 if (ip
->ip_p
!= IPPROTO_ICMP
) {
1340 * To prevent spoofing, specified source address must
1343 if (ip
->ip_src
.s_addr
!= INADDR_ANY
) {
1344 socket_unlock(so
, 0);
1345 lck_rw_lock_shared(in_ifaddr_rwlock
);
1346 if (TAILQ_EMPTY(&in_ifaddrhead
)) {
1347 lck_rw_done(in_ifaddr_rwlock
);
1351 TAILQ_FOREACH(ia
, INADDR_HASH(ip
->ip_src
.s_addr
),
1353 IFA_LOCK(&ia
->ia_ifa
);
1354 if (IA_SIN(ia
)->sin_addr
.s_addr
==
1355 ip
->ip_src
.s_addr
) {
1356 IFA_UNLOCK(&ia
->ia_ifa
);
1357 lck_rw_done(in_ifaddr_rwlock
);
1361 IFA_UNLOCK(&ia
->ia_ifa
);
1363 lck_rw_done(in_ifaddr_rwlock
);
1368 /* Do not trust we got a valid checksum */
1371 icp
= (struct icmp
*)(void *)(((char *)m
->m_data
) + hlen
);
1372 icmplen
= m
->m_pkthdr
.len
- hlen
;
1374 if ((icmplen
= m
->m_pkthdr
.len
) < ICMP_MINLEN
) {
1377 icp
= mtod(m
, struct icmp
*);
1380 * Allow only to send request types with code 0
1382 if (icp
->icmp_code
!= 0) {
1385 switch (icp
->icmp_type
) {
1389 if (icmplen
!= 20) {
1394 if (icmplen
!= 12) {
1401 return rip_send(so
, flags
, m
, nam
, control
, p
);
1408 if (control
!= NULL
) {
1415 #endif /* __APPLE__ */