2 * Copyright (c) 2000-2016 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>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/in_var.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_icmp.h>
90 #include <netinet/ip_var.h>
91 #include <netinet/icmp_var.h>
92 #include <netinet/tcp.h>
93 #include <netinet/tcp_fsm.h>
94 #include <netinet/tcp_seq.h>
95 #include <netinet/tcp_timer.h>
96 #include <netinet/tcp_var.h>
97 #include <netinet/tcpip.h>
100 #include <netinet6/ipsec.h>
101 #include <netkey/key.h>
105 #include <net/necp.h>
108 /* XXX This one should go in sys/mbuf.h. It is used to avoid that
109 * a firewall-generated packet loops forever through the firewall.
111 #ifndef M_SKIP_FIREWALL
112 #define M_SKIP_FIREWALL 0x4000
116 #include <security/mac_framework.h>
121 * ICMP routines: error generation, receive packet processing, and
122 * routines to turnaround packets back to the originator, and
123 * host table maintenance routines.
126 struct icmpstat icmpstat
;
127 SYSCTL_STRUCT(_net_inet_icmp
, ICMPCTL_STATS
, stats
,
128 CTLFLAG_RD
| CTLFLAG_LOCKED
,
129 &icmpstat
, icmpstat
, "");
131 static int icmpmaskrepl
= 0;
132 SYSCTL_INT(_net_inet_icmp
, ICMPCTL_MASKREPL
, maskrepl
,
133 CTLFLAG_RW
| CTLFLAG_LOCKED
,
134 &icmpmaskrepl
, 0, "");
136 static int icmptimestamp
= 0;
137 SYSCTL_INT(_net_inet_icmp
, ICMPCTL_TIMESTAMP
, timestamp
,
138 CTLFLAG_RW
| CTLFLAG_LOCKED
,
139 &icmptimestamp
, 0, "");
141 static int drop_redirect
= 1;
142 SYSCTL_INT(_net_inet_icmp
, OID_AUTO
, drop_redirect
,
143 CTLFLAG_RW
| CTLFLAG_LOCKED
,
144 &drop_redirect
, 0, "");
146 static int log_redirect
= 0;
147 SYSCTL_INT(_net_inet_icmp
, OID_AUTO
, log_redirect
,
148 CTLFLAG_RW
| CTLFLAG_LOCKED
,
149 &log_redirect
, 0, "");
151 const static int icmp_datalen
= 8;
155 /* Default values in case CONFIG_ICMP_BANDLIM is not defined in the MASTER file */
156 #ifndef CONFIG_ICMP_BANDLIM
158 #define CONFIG_ICMP_BANDLIM 250
159 #else /* CONFIG_EMBEDDED */
160 #define CONFIG_ICMP_BANDLIM 50
161 #endif /* CONFIG_EMBEDDED */
162 #endif /* CONFIG_ICMP_BANDLIM */
165 * ICMP error-response bandwidth limiting sysctl. If not enabled, sysctl
166 * variable content is -1 and read-only.
169 static int icmplim
= CONFIG_ICMP_BANDLIM
;
170 SYSCTL_INT(_net_inet_icmp
, ICMPCTL_ICMPLIM
, icmplim
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
173 #else /* ICMP_BANDLIM */
175 static int icmplim
= -1;
176 SYSCTL_INT(_net_inet_icmp
, ICMPCTL_ICMPLIM
, icmplim
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
179 #endif /* ICMP_BANDLIM */
182 * ICMP broadcast echo sysctl
185 static int icmpbmcastecho
= 1;
186 SYSCTL_INT(_net_inet_icmp
, OID_AUTO
, bmcastecho
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
187 &icmpbmcastecho
, 0, "");
189 #if (DEBUG | DEVELOPMENT)
190 static int icmpprintfs
= 0;
191 SYSCTL_INT(_net_inet_icmp
, OID_AUTO
, verbose
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
192 &icmpprintfs
, 0, "");
195 static void icmp_reflect(struct mbuf
*);
196 static void icmp_send(struct mbuf
*, struct mbuf
*);
199 * Generate an error packet of type error
200 * in response to bad packet ip.
210 struct ip
*oip
, *nip
;
213 u_int32_t oiphlen
, icmplen
, icmpelen
, nlen
;
215 /* Expect 32-bit aligned data pointer on strict-align platforms */
216 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(n
);
218 oip
= mtod(n
, struct ip
*);
219 oiphlen
= IP_VHL_HL(oip
->ip_vhl
) << 2;
221 #if (DEBUG | DEVELOPMENT)
223 printf("icmp_error(0x%llx, %x, %d)\n",
224 (uint64_t)VM_KERNEL_ADDRPERM(oip
), type
, code
);
226 if (type
!= ICMP_REDIRECT
)
227 icmpstat
.icps_error
++;
229 * Don't send error if not the first fragment of message.
230 * Don't error if the old packet protocol was ICMP
231 * error message, only known informational types.
233 if (oip
->ip_off
& ~(IP_MF
|IP_DF
))
236 if (oip
->ip_p
== IPPROTO_ICMP
&& type
!= ICMP_REDIRECT
&&
237 n
->m_len
>= oiphlen
+ ICMP_MINLEN
&&
238 !ICMP_INFOTYPE(((struct icmp
*)(void *)((caddr_t
)oip
+ oiphlen
))->
240 icmpstat
.icps_oldicmp
++;
244 * Don't send error in response to a multicast or
247 if (n
->m_flags
& (M_BCAST
|M_MCAST
))
251 * Calculate the length to quote from original packet and prevent
252 * the ICMP mbuf from overflowing.
255 if (oip
->ip_p
== IPPROTO_TCP
) {
259 if (oiphlen
+ sizeof(struct tcphdr
) > n
->m_len
&&
262 if (n
->m_len
< (oiphlen
+ sizeof(struct tcphdr
)) &&
263 (n
= m_pullup(n
, (oiphlen
+ sizeof(struct tcphdr
)))) == NULL
)
266 th
= (struct tcphdr
*)(void *)((caddr_t
)oip
+ oiphlen
);
267 if (th
!= ((struct tcphdr
*)P2ROUNDDOWN(th
,
270 tcphlen
= th
->th_off
<< 2;
271 if (tcphlen
< sizeof(struct tcphdr
))
273 if (oip
->ip_len
< (oiphlen
+ tcphlen
))
275 if ((oiphlen
+ tcphlen
) > n
->m_len
&& n
->m_next
== NULL
)
277 if (n
->m_len
< (oiphlen
+ tcphlen
) &&
278 (n
= m_pullup(n
, (oiphlen
+ tcphlen
))) == NULL
)
281 icmpelen
= max(tcphlen
, min(icmp_datalen
,
282 (oip
->ip_len
- oiphlen
)));
284 stdreply
: icmpelen
= max(ICMP_MINLEN
, min(icmp_datalen
,
285 (oip
->ip_len
- oiphlen
)));
287 icmplen
= min(oiphlen
+ icmpelen
, min(nlen
, oip
->ip_len
));
288 if (icmplen
< sizeof(struct ip
))
291 * First, formulate icmp message
293 if (MHLEN
> (sizeof(struct ip
) + ICMP_MINLEN
+ icmplen
))
294 m
= m_gethdr(M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
296 m
= m_getcl(M_DONTWAIT
, MT_DATA
, M_PKTHDR
);
301 if (n
->m_flags
& M_SKIP_FIREWALL
) {
303 * set M_SKIP_FIREWALL to skip firewall check, since
304 * we're called from firewall
306 m
->m_flags
|= M_SKIP_FIREWALL
;
310 mac_mbuf_label_associate_netlayer(n
, m
);
312 m
->m_len
= icmplen
+ ICMP_MINLEN
; /* for ICMP header and data */
313 MH_ALIGN(m
, m
->m_len
);
314 icp
= mtod(m
, struct icmp
*);
315 if ((u_int
)type
> ICMP_MAXTYPE
) {
319 icmpstat
.icps_outhist
[type
]++;
320 icp
->icmp_type
= type
;
321 if (type
== ICMP_REDIRECT
)
322 icp
->icmp_gwaddr
.s_addr
= dest
;
326 * The following assignments assume an overlay with the
327 * zeroed icmp_void field.
329 if (type
== ICMP_PARAMPROB
) {
330 icp
->icmp_pptr
= code
;
332 } else if (type
== ICMP_UNREACH
&&
333 code
== ICMP_UNREACH_NEEDFRAG
&& nextmtu
!= 0) {
334 icp
->icmp_nextmtu
= htons(nextmtu
);
338 icp
->icmp_code
= code
;
339 m_copydata(n
, 0, icmplen
, (caddr_t
)&icp
->icmp_ip
);
343 * Convert fields to network representation.
345 #if BYTE_ORDER != BIG_ENDIAN
350 * Now, copy old ip header (without options)
351 * in front of icmp message.
353 if (m
->m_data
- sizeof(struct ip
) < m
->m_pktdat
) {
357 m
->m_data
-= sizeof(struct ip
);
358 m
->m_len
+= sizeof(struct ip
);
359 m
->m_pkthdr
.len
= m
->m_len
;
360 m
->m_pkthdr
.rcvif
= n
->m_pkthdr
.rcvif
;
361 nip
= mtod(m
, struct ip
*);
362 bcopy((caddr_t
)oip
, (caddr_t
)nip
, sizeof(struct ip
));
363 nip
->ip_len
= m
->m_len
;
364 nip
->ip_vhl
= IP_VHL_BORING
;
365 nip
->ip_p
= IPPROTO_ICMP
;
375 * Process a received ICMP message.
378 icmp_input(struct mbuf
*m
, int hlen
)
380 struct sockaddr_in icmpsrc
, icmpdst
, icmpgw
;
382 struct ip
*ip
= mtod(m
, struct ip
*);
385 struct in_ifaddr
*ia
;
386 void (*ctlfunc
)(int, struct sockaddr
*, void *, struct ifnet
*);
389 /* Expect 32-bit aligned data pointer on strict-align platforms */
390 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
392 icmplen
= ip
->ip_len
;
395 * Locate icmp structure in mbuf, and check
396 * that not corrupted and of at least minimum length.
398 #if (DEBUG | DEVELOPMENT)
399 if (icmpprintfs
> 2) {
400 char src_str
[MAX_IPv4_STR_LEN
];
401 char dst_str
[MAX_IPv4_STR_LEN
];
403 inet_ntop(AF_INET
, &ip
->ip_src
, src_str
, sizeof(src_str
));
404 inet_ntop(AF_INET
, &ip
->ip_dst
, dst_str
, sizeof(dst_str
));
405 printf("%s: from %s to %s, len %d\n",
406 __func__
, src_str
, dst_str
, icmplen
);
409 if (icmplen
< ICMP_MINLEN
) {
410 icmpstat
.icps_tooshort
++;
413 i
= hlen
+ min(icmplen
, ICMP_ADVLENMIN
);
414 if (m
->m_len
< i
&& (m
= m_pullup(m
, i
)) == 0) {
415 icmpstat
.icps_tooshort
++;
418 ip
= mtod(m
, struct ip
*);
421 icp
= mtod(m
, struct icmp
*);
422 if (in_cksum(m
, icmplen
)) {
423 icmpstat
.icps_checksum
++;
429 #if (DEBUG | DEVELOPMENT)
431 printf("icmp_input, type %d code %d\n", icp
->icmp_type
,
436 * Message type specific processing.
438 if (icp
->icmp_type
> ICMP_MAXTYPE
)
442 bzero(&icmpsrc
, sizeof (icmpsrc
));
443 icmpsrc
.sin_len
= sizeof (struct sockaddr_in
);
444 icmpsrc
.sin_family
= AF_INET
;
445 bzero(&icmpdst
, sizeof (icmpdst
));
446 icmpdst
.sin_len
= sizeof (struct sockaddr_in
);
447 icmpdst
.sin_family
= AF_INET
;
448 bzero(&icmpgw
, sizeof (icmpgw
));
449 icmpgw
.sin_len
= sizeof (struct sockaddr_in
);
450 icmpgw
.sin_family
= AF_INET
;
452 icmpstat
.icps_inhist
[icp
->icmp_type
]++;
453 code
= icp
->icmp_code
;
454 switch (icp
->icmp_type
) {
458 case ICMP_UNREACH_NET
:
459 case ICMP_UNREACH_HOST
:
460 case ICMP_UNREACH_SRCFAIL
:
461 case ICMP_UNREACH_NET_UNKNOWN
:
462 case ICMP_UNREACH_HOST_UNKNOWN
:
463 case ICMP_UNREACH_ISOLATED
:
464 case ICMP_UNREACH_TOSNET
:
465 case ICMP_UNREACH_TOSHOST
:
466 case ICMP_UNREACH_HOST_PRECEDENCE
:
467 case ICMP_UNREACH_PRECEDENCE_CUTOFF
:
468 code
= PRC_UNREACH_NET
;
471 case ICMP_UNREACH_NEEDFRAG
:
476 * RFC 1122, Sections 3.2.2.1 and 4.2.3.9.
477 * Treat subcodes 2,3 as immediate RST
479 case ICMP_UNREACH_PROTOCOL
:
480 case ICMP_UNREACH_PORT
:
481 code
= PRC_UNREACH_PORT
;
484 case ICMP_UNREACH_NET_PROHIB
:
485 case ICMP_UNREACH_HOST_PROHIB
:
486 case ICMP_UNREACH_FILTER_PROHIB
:
487 code
= PRC_UNREACH_ADMIN_PROHIB
;
498 code
+= PRC_TIMXCEED_INTRANS
;
504 code
= PRC_PARAMPROB
;
507 case ICMP_SOURCEQUENCH
:
513 * Problem with datagram; advise higher level routines.
515 if (icmplen
< ICMP_ADVLENMIN
|| icmplen
< ICMP_ADVLEN(icp
)
516 || IP_VHL_HL(icp
->icmp_ip
.ip_vhl
) <
517 (sizeof(struct ip
) >> 2)) {
518 icmpstat
.icps_badlen
++;
522 #if BYTE_ORDER != BIG_ENDIAN
523 NTOHS(icp
->icmp_ip
.ip_len
);
526 /* Discard ICMP's in response to multicast packets */
527 if (IN_MULTICAST(ntohl(icp
->icmp_ip
.ip_dst
.s_addr
)))
529 #if (DEBUG | DEVELOPMENT)
531 printf("deliver to protocol %d\n",
534 icmpsrc
.sin_addr
= icp
->icmp_ip
.ip_dst
;
537 * if the packet contains [IPv4 AH TCP], we can't make a
538 * notification to TCP layer.
540 ctlfunc
= ip_protox
[icp
->icmp_ip
.ip_p
]->pr_ctlinput
;
542 (*ctlfunc
)(code
, (struct sockaddr
*)&icmpsrc
,
543 (void *)&icp
->icmp_ip
, m
->m_pkthdr
.rcvif
);
547 icmpstat
.icps_badcode
++;
551 if ((m
->m_flags
& (M_MCAST
| M_BCAST
))) {
552 if (icmpbmcastecho
== 0) {
553 icmpstat
.icps_bmcastecho
++;
560 * Do not reply when the destination is link local multicast or broadcast
561 * and the source is not from a directly connected subnet
563 if ((IN_LOCAL_GROUP(ntohl(ip
->ip_dst
.s_addr
)) ||
564 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
)) &&
565 in_localaddr(ip
->ip_src
) == 0) {
566 icmpstat
.icps_bmcastecho
++;
567 #if (DEBUG | DEVELOPMENT)
568 if (icmpprintfs
> 0) {
569 char src_str
[MAX_IPv4_STR_LEN
];
570 char dst_str
[MAX_IPv4_STR_LEN
];
572 inet_ntop(AF_INET
, &ip
->ip_src
, src_str
, sizeof(src_str
));
573 inet_ntop(AF_INET
, &ip
->ip_dst
, dst_str
, sizeof(dst_str
));
574 printf("%s: non local (B|M)CAST %s to %s, len %d\n",
575 __func__
, src_str
, dst_str
, icmplen
);
581 icp
->icmp_type
= ICMP_ECHOREPLY
;
583 if (badport_bandlim(BANDLIM_ICMP_ECHO
) < 0)
590 if (icmptimestamp
== 0)
594 && (m
->m_flags
& (M_MCAST
| M_BCAST
)) != 0) {
595 icmpstat
.icps_bmcasttstamp
++;
598 if (icmplen
< ICMP_TSLEN
) {
599 icmpstat
.icps_badlen
++;
602 icp
->icmp_type
= ICMP_TSTAMPREPLY
;
603 icp
->icmp_rtime
= iptime();
604 icp
->icmp_ttime
= icp
->icmp_rtime
; /* bogus, do later! */
606 if (badport_bandlim(BANDLIM_ICMP_TSTAMP
) < 0)
613 if (icmpmaskrepl
== 0)
616 * We are not able to respond with all ones broadcast
617 * unless we receive it over a point-to-point interface.
619 if (icmplen
< ICMP_MASKLEN
)
621 switch (ip
->ip_dst
.s_addr
) {
623 case INADDR_BROADCAST
:
625 icmpdst
.sin_addr
= ip
->ip_src
;
629 icmpdst
.sin_addr
= ip
->ip_dst
;
631 ia
= (struct in_ifaddr
*)ifaof_ifpforaddr(
632 (struct sockaddr
*)&icmpdst
, m
->m_pkthdr
.rcvif
);
635 IFA_LOCK(&ia
->ia_ifa
);
636 if (ia
->ia_ifp
== 0) {
637 IFA_UNLOCK(&ia
->ia_ifa
);
638 IFA_REMREF(&ia
->ia_ifa
);
642 icp
->icmp_type
= ICMP_MASKREPLY
;
643 icp
->icmp_mask
= ia
->ia_sockmask
.sin_addr
.s_addr
;
644 if (ip
->ip_src
.s_addr
== 0) {
645 if (ia
->ia_ifp
->if_flags
& IFF_BROADCAST
)
646 ip
->ip_src
= satosin(&ia
->ia_broadaddr
)->sin_addr
;
647 else if (ia
->ia_ifp
->if_flags
& IFF_POINTOPOINT
)
648 ip
->ip_src
= satosin(&ia
->ia_dstaddr
)->sin_addr
;
650 IFA_UNLOCK(&ia
->ia_ifa
);
651 IFA_REMREF(&ia
->ia_ifa
);
653 ip
->ip_len
+= hlen
; /* since ip_input deducts this */
654 icmpstat
.icps_reflect
++;
655 icmpstat
.icps_outhist
[icp
->icmp_type
]++;
661 u_int32_t src
, dst
, gw
;
663 src
= ntohl(ip
->ip_src
.s_addr
);
664 dst
= ntohl(icp
->icmp_ip
.ip_dst
.s_addr
);
665 gw
= ntohl(icp
->icmp_gwaddr
.s_addr
);
666 printf("icmp redirect from %d.%d.%d.%d: "
667 "%d.%d.%d.%d => %d.%d.%d.%d\n",
668 (int)(src
>> 24), (int)((src
>> 16) & 0xff),
669 (int)((src
>> 8) & 0xff), (int)(src
& 0xff),
670 (int)(dst
>> 24), (int)((dst
>> 16) & 0xff),
671 (int)((dst
>> 8) & 0xff), (int)(dst
& 0xff),
672 (int)(gw
>> 24), (int)((gw
>> 16) & 0xff),
673 (int)((gw
>> 8) & 0xff), (int)(gw
& 0xff));
679 if (icmplen
< ICMP_ADVLENMIN
|| icmplen
< ICMP_ADVLEN(icp
) ||
680 IP_VHL_HL(icp
->icmp_ip
.ip_vhl
) < (sizeof(struct ip
) >> 2)) {
681 icmpstat
.icps_badlen
++;
685 * Short circuit routing redirects to force
686 * immediate change in the kernel's routing
687 * tables. The message is also handed to anyone
688 * listening on a raw socket (e.g. the routing
689 * daemon for use in updating its tables).
691 icmpgw
.sin_addr
= ip
->ip_src
;
692 icmpdst
.sin_addr
= icp
->icmp_gwaddr
;
693 #if (DEBUG | DEVELOPMENT)
694 if (icmpprintfs
> 0) {
695 char dst_str
[MAX_IPv4_STR_LEN
];
696 char gw_str
[MAX_IPv4_STR_LEN
];
698 inet_ntop(AF_INET
, &icp
->icmp_ip
.ip_dst
, dst_str
, sizeof(dst_str
));
699 inet_ntop(AF_INET
, &icp
->icmp_gwaddr
, gw_str
, sizeof(gw_str
));
700 printf("%s: redirect dst %s to %s\n", __func__
, dst_str
, gw_str
);
703 icmpsrc
.sin_addr
= icp
->icmp_ip
.ip_dst
;
704 rtredirect(m
->m_pkthdr
.rcvif
, (struct sockaddr
*)&icmpsrc
,
705 (struct sockaddr
*)&icmpdst
, NULL
, RTF_GATEWAY
| RTF_HOST
,
706 (struct sockaddr
*)&icmpgw
, NULL
);
707 pfctlinput(PRC_REDIRECT_HOST
, (struct sockaddr
*)&icmpsrc
);
709 key_sa_routechange((struct sockaddr
*)&icmpsrc
);
714 * No kernel processing for the following;
715 * just fall through to send to raw listener.
718 case ICMP_ROUTERADVERT
:
719 case ICMP_ROUTERSOLICIT
:
720 case ICMP_TSTAMPREPLY
:
736 * Reflect the ip packet back to the source
739 icmp_reflect(struct mbuf
*m
)
741 struct ip
*ip
= mtod(m
, struct ip
*);
742 struct sockaddr_in icmpdst
;
743 struct in_ifaddr
*ia
;
745 struct mbuf
*opts
= NULL
;
746 int optlen
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof(struct ip
);
748 if (!in_canforward(ip
->ip_src
) &&
749 ((ntohl(ip
->ip_src
.s_addr
) & IN_CLASSA_NET
) !=
750 (IN_LOOPBACKNET
<< IN_CLASSA_NSHIFT
))) {
751 m_freem(m
); /* Bad return address */
752 goto done
; /* Ip_output() will check for broadcast */
755 ip
->ip_dst
= ip
->ip_src
;
757 * If the incoming packet was addressed directly to us,
758 * use dst as the src for the reply. Otherwise (broadcast
759 * or anonymous), use the address which corresponds
760 * to the incoming interface.
762 lck_rw_lock_shared(in_ifaddr_rwlock
);
763 TAILQ_FOREACH(ia
, INADDR_HASH(t
.s_addr
), ia_hash
) {
764 IFA_LOCK(&ia
->ia_ifa
);
765 if (t
.s_addr
== IA_SIN(ia
)->sin_addr
.s_addr
) {
766 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
767 IFA_UNLOCK(&ia
->ia_ifa
);
770 IFA_UNLOCK(&ia
->ia_ifa
);
773 * Slow path; check for broadcast addresses. Find a source
774 * IP address to use when replying to the broadcast request;
775 * let IP handle the source interface selection work.
777 for (ia
= in_ifaddrhead
.tqh_first
; ia
; ia
= ia
->ia_link
.tqe_next
) {
778 IFA_LOCK(&ia
->ia_ifa
);
779 if (ia
->ia_ifp
&& (ia
->ia_ifp
->if_flags
& IFF_BROADCAST
) &&
780 t
.s_addr
== satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
) {
781 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
782 IFA_UNLOCK(&ia
->ia_ifa
);
785 IFA_UNLOCK(&ia
->ia_ifa
);
788 lck_rw_done(in_ifaddr_rwlock
);
791 bzero(&icmpdst
, sizeof (icmpdst
));
792 icmpdst
.sin_len
= sizeof (struct sockaddr_in
);
793 icmpdst
.sin_family
= AF_INET
;
794 icmpdst
.sin_addr
= t
;
795 if ((ia
== (struct in_ifaddr
*)0) && m
->m_pkthdr
.rcvif
)
796 ia
= (struct in_ifaddr
*)ifaof_ifpforaddr(
797 (struct sockaddr
*)&icmpdst
, m
->m_pkthdr
.rcvif
);
799 * The following happens if the packet was not addressed to us,
800 * and was received on an interface with no IP address.
802 if (ia
== (struct in_ifaddr
*)0) {
803 lck_rw_lock_shared(in_ifaddr_rwlock
);
804 ia
= in_ifaddrhead
.tqh_first
;
805 if (ia
== (struct in_ifaddr
*)0) {/* no address yet, bail out */
806 lck_rw_done(in_ifaddr_rwlock
);
810 IFA_ADDREF(&ia
->ia_ifa
);
811 lck_rw_done(in_ifaddr_rwlock
);
814 mac_netinet_icmp_reply(m
);
816 IFA_LOCK_SPIN(&ia
->ia_ifa
);
817 t
= IA_SIN(ia
)->sin_addr
;
818 IFA_UNLOCK(&ia
->ia_ifa
);
820 ip
->ip_ttl
= ip_defttl
;
821 IFA_REMREF(&ia
->ia_ifa
);
830 * Retrieve any source routing from the incoming packet;
831 * add on any record-route or timestamp options.
833 cp
= (u_char
*) (ip
+ 1);
834 if ((opts
= ip_srcroute()) == 0 &&
835 (opts
= m_gethdr(M_DONTWAIT
, MT_HEADER
))) { /* MAC-OK */
836 opts
->m_len
= sizeof(struct in_addr
);
837 mtod(opts
, struct in_addr
*)->s_addr
= 0;
840 #if (DEBUG | DEVELOPMENT)
842 printf("icmp_reflect optlen %d rt %d => ",
843 optlen
, opts
->m_len
);
845 for (cnt
= optlen
; cnt
> 0; cnt
-= len
, cp
+= len
) {
846 opt
= cp
[IPOPT_OPTVAL
];
847 if (opt
== IPOPT_EOL
)
849 if (opt
== IPOPT_NOP
)
852 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
854 len
= cp
[IPOPT_OLEN
];
855 if (len
< IPOPT_OLEN
+ sizeof(*cp
) ||
860 * Should check for overflow, but it "can't happen"
862 if (opt
== IPOPT_RR
|| opt
== IPOPT_TS
||
863 opt
== IPOPT_SECURITY
) {
865 mtod(opts
, caddr_t
) + opts
->m_len
, len
);
869 /* Terminate & pad, if necessary */
870 cnt
= opts
->m_len
% 4;
872 for (; cnt
< 4; cnt
++) {
873 *(mtod(opts
, caddr_t
) + opts
->m_len
) =
878 #if (DEBUG | DEVELOPMENT)
880 printf("%d\n", opts
->m_len
);
884 * Now strip out original options by copying rest of first
885 * mbuf's data back, and adjust the IP length.
887 ip
->ip_len
-= optlen
;
888 ip
->ip_vhl
= IP_VHL_BORING
;
890 if (m
->m_flags
& M_PKTHDR
)
891 m
->m_pkthdr
.len
-= optlen
;
892 optlen
+= sizeof(struct ip
);
893 bcopy((caddr_t
)ip
+ optlen
, (caddr_t
)(ip
+ 1),
894 (unsigned)(m
->m_len
- sizeof(struct ip
)));
896 m
->m_flags
&= ~(M_BCAST
|M_MCAST
);
904 * Send an icmp packet back to the ip level,
905 * after supplying a checksum.
908 icmp_send(struct mbuf
*m
, struct mbuf
*opts
)
910 struct ip
*ip
= mtod(m
, struct ip
*);
914 struct ip_out_args ipoa
= { IFSCOPE_NONE
, { 0 },
915 IPOAF_SELECT_SRCIF
| IPOAF_BOUND_SRCADDR
, 0,
916 SO_TC_UNSPEC
, _NET_SERVICE_TYPE_UNSPEC
};
918 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
) && m
->m_pkthdr
.rcvif
!= NULL
) {
919 ipoa
.ipoa_boundif
= m
->m_pkthdr
.rcvif
->if_index
;
920 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
923 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
926 icp
= mtod(m
, struct icmp
*);
928 icp
->icmp_cksum
= in_cksum(m
, ip
->ip_len
- hlen
);
931 m
->m_pkthdr
.rcvif
= NULL
;
932 m
->m_pkthdr
.csum_data
= 0;
933 m
->m_pkthdr
.csum_flags
= 0;
934 #if (DEBUG | DEVELOPMENT)
935 if (icmpprintfs
> 2) {
936 char src_str
[MAX_IPv4_STR_LEN
];
937 char dst_str
[MAX_IPv4_STR_LEN
];
939 inet_ntop(AF_INET
, &ip
->ip_src
, src_str
, sizeof(src_str
));
940 inet_ntop(AF_INET
, &ip
->ip_dst
, dst_str
, sizeof(dst_str
));
941 printf("%s: dst %s src %s\n", __func__
, dst_str
, src_str
);
944 bzero(&ro
, sizeof ro
);
945 (void) ip_output(m
, opts
, &ro
, IP_OUTARGS
, NULL
, &ipoa
);
956 t
= (atv
.tv_sec
% (24*60*60)) * 1000 + atv
.tv_usec
/ 1000;
962 * Return the next larger or smaller MTU plateau (table from RFC 1191)
963 * given current value MTU. If DIR is less than zero, a larger plateau
964 * is returned; otherwise, a smaller value is returned.
967 ip_next_mtu(int mtu
, int dir
)
969 static int mtutab
[] = {
970 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1006, 508, 296,
975 for (i
= 0; i
< (sizeof mtutab
) / (sizeof mtutab
[0]); i
++) {
976 if (mtu
>= mtutab
[i
])
984 return mtutab
[i
- 1];
987 if (mtutab
[i
] == 0) {
989 } else if(mtu
> mtutab
[i
]) {
992 return mtutab
[i
+ 1];
1001 * badport_bandlim() - check for ICMP bandwidth limit
1003 * Return 0 if it is ok to send an ICMP error response, -1 if we have
1004 * hit our bandwidth limit and it is not ok.
1006 * If icmplim is <= 0, the feature is disabled and 0 is returned.
1008 * For now we separate the TCP and UDP subsystems w/ different 'which'
1009 * values. We may eventually remove this separation (and simplify the
1012 * Note that the printing of the error message is delayed so we can
1013 * properly print the icmp error rate that the system was trying to do
1014 * (i.e. 22000/100 pps, etc...). This can cause long delays in printing
1015 * the 'final' error, but it doesn't make sense to solve the printing
1016 * delay with more complex code.
1020 badport_bandlim(int which
)
1022 static uint64_t lticks
[BANDLIM_MAX
+ 1];
1023 static int lpackets
[BANDLIM_MAX
+ 1];
1024 uint64_t time
= net_uptime();
1027 const char *bandlimittype
[] = {
1028 "Limiting icmp unreach response",
1029 "Limiting icmp ping response",
1030 "Limiting icmp tstamp response",
1031 "Limiting closed port RST response",
1032 "Limiting open port RST response"
1036 * Return ok status if feature disabled or argument out of
1040 if (icmplim
<= 0 || which
> BANDLIM_MAX
|| which
< 0)
1043 secs
= time
- lticks
[which
];
1046 * reset stats when cumulative delta exceeds one second.
1050 if (lpackets
[which
] > icmplim
) {
1051 printf("%s from %d to %d packets per second\n",
1052 bandlimittype
[which
],
1057 lticks
[which
] = time
;
1058 lpackets
[which
] = 0;
1065 if (++lpackets
[which
] > icmplim
) {
1076 * Non-privileged ICMP socket operations
1077 * - send ICMP echo request
1079 * - limited socket options
1082 #include <netinet/ip_icmp.h>
1083 #include <netinet/in_pcb.h>
1085 extern u_int32_t rip_sendspace
;
1086 extern u_int32_t rip_recvspace
;
1087 extern struct inpcbinfo ripcbinfo
;
1089 int rip_abort(struct socket
*);
1090 int rip_bind(struct socket
*, struct sockaddr
*, struct proc
*);
1091 int rip_connect(struct socket
*, struct sockaddr
*, struct proc
*);
1092 int rip_detach(struct socket
*);
1093 int rip_disconnect(struct socket
*);
1094 int rip_shutdown(struct socket
*);
1096 __private_extern__
int icmp_dgram_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
);
1097 __private_extern__
int icmp_dgram_attach(struct socket
*so
, int proto
, struct proc
*p
);
1098 __private_extern__
int icmp_dgram_ctloutput(struct socket
*so
, struct sockopt
*sopt
);
1100 __private_extern__
struct pr_usrreqs icmp_dgram_usrreqs
= {
1101 .pru_abort
= rip_abort
,
1102 .pru_attach
= icmp_dgram_attach
,
1103 .pru_bind
= rip_bind
,
1104 .pru_connect
= rip_connect
,
1105 .pru_control
= in_control
,
1106 .pru_detach
= rip_detach
,
1107 .pru_disconnect
= rip_disconnect
,
1108 .pru_peeraddr
= in_getpeeraddr
,
1109 .pru_send
= icmp_dgram_send
,
1110 .pru_shutdown
= rip_shutdown
,
1111 .pru_sockaddr
= in_getsockaddr
,
1112 .pru_sosend
= sosend
,
1113 .pru_soreceive
= soreceive
,
1116 /* Like rip_attach but without root privilege enforcement */
1117 __private_extern__
int
1118 icmp_dgram_attach(struct socket
*so
, __unused
int proto
, struct proc
*p
)
1123 inp
= sotoinpcb(so
);
1125 panic("icmp_dgram_attach");
1127 error
= soreserve(so
, rip_sendspace
, rip_recvspace
);
1130 error
= in_pcballoc(so
, &ripcbinfo
, p
);
1133 inp
= (struct inpcb
*)so
->so_pcb
;
1134 inp
->inp_vflag
|= INP_IPV4
;
1135 inp
->inp_ip_p
= IPPROTO_ICMP
;
1136 inp
->inp_ip_ttl
= ip_defttl
;
1141 * Raw IP socket option processing.
1143 __private_extern__
int
1144 icmp_dgram_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
1148 if (sopt
->sopt_level
!= IPPROTO_IP
)
1151 switch (sopt
->sopt_name
) {
1157 case IP_RECVRETOPTS
:
1158 case IP_RECVDSTADDR
:
1160 case IP_MULTICAST_IF
:
1161 case IP_MULTICAST_IFINDEX
:
1162 case IP_MULTICAST_TTL
:
1163 case IP_MULTICAST_LOOP
:
1164 case IP_ADD_MEMBERSHIP
:
1165 case IP_DROP_MEMBERSHIP
:
1166 case IP_MULTICAST_VIF
:
1169 case IP_IPSEC_POLICY
:
1173 case IP_NO_IFT_CELLULAR
:
1174 error
= rip_ctloutput(so
, sopt
);
1185 __private_extern__
int
1186 icmp_dgram_send(struct socket
*so
, int flags
, struct mbuf
*m
,
1187 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
1190 struct inpcb
*inp
= sotoinpcb(so
);
1193 struct in_ifaddr
*ia
= NULL
;
1199 || (necp_socket_should_use_flow_divert(inp
))
1207 if ((inp
->inp_flags
& INP_HDRINCL
) != 0) {
1208 /* Expect 32-bit aligned data ptr on strict-align platforms */
1209 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
1211 * This is not raw IP, we liberal only for fields TOS,
1214 ip
= mtod(m
, struct ip
*);
1216 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1217 /* Some sanity checks */
1218 if (m
->m_pkthdr
.len
< hlen
+ ICMP_MINLEN
) {
1222 if (IP_VHL_V(ip
->ip_vhl
) != 4)
1224 if (hlen
< 20 || hlen
> 40 || ip
->ip_len
!= m
->m_pkthdr
.len
)
1226 /* Bogus fragments can tie up peer resources */
1227 if ((ip
->ip_off
& ~IP_DF
) != 0)
1229 /* Allow only ICMP even for user provided IP header */
1230 if (ip
->ip_p
!= IPPROTO_ICMP
)
1233 * To prevent spoofing, specified source address must
1236 if (ip
->ip_src
.s_addr
!= INADDR_ANY
) {
1237 socket_unlock(so
, 0);
1238 lck_rw_lock_shared(in_ifaddr_rwlock
);
1239 if (TAILQ_EMPTY(&in_ifaddrhead
)) {
1240 lck_rw_done(in_ifaddr_rwlock
);
1244 TAILQ_FOREACH(ia
, INADDR_HASH(ip
->ip_src
.s_addr
),
1246 IFA_LOCK(&ia
->ia_ifa
);
1247 if (IA_SIN(ia
)->sin_addr
.s_addr
==
1248 ip
->ip_src
.s_addr
) {
1249 IFA_UNLOCK(&ia
->ia_ifa
);
1250 lck_rw_done(in_ifaddr_rwlock
);
1254 IFA_UNLOCK(&ia
->ia_ifa
);
1256 lck_rw_done(in_ifaddr_rwlock
);
1261 /* Do not trust we got a valid checksum */
1264 icp
= (struct icmp
*)(void *)(((char *)m
->m_data
) + hlen
);
1265 icmplen
= m
->m_pkthdr
.len
- hlen
;
1267 if ((icmplen
= m
->m_pkthdr
.len
) < ICMP_MINLEN
) {
1270 icp
= mtod(m
, struct icmp
*);
1273 * Allow only to send request types with code 0
1275 if (icp
->icmp_code
!= 0)
1277 switch (icp
->icmp_type
) {
1291 return (rip_send(so
, flags
, m
, nam
, control
, p
));
1297 if (control
!= NULL
)
1303 #endif /* __APPLE__ */