]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/ip_icmp.c
xnu-6153.141.1.tar.gz
[apple/xnu.git] / bsd / netinet / ip_icmp.c
CommitLineData
1c79356b 1/*
a39ff7e2 2 * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1982, 1986, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 *
60 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
61 */
2d21ac55
A
62/*
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,
66 * Version 2.0.
67 */
1c79356b
A
68
69#include <sys/param.h>
70#include <sys/systm.h>
71#include <sys/mbuf.h>
316670eb 72#include <sys/mcache.h>
1c79356b
A
73#include <sys/protosw.h>
74#include <sys/socket.h>
75#include <sys/time.h>
76#include <sys/kernel.h>
77#include <sys/sysctl.h>
78
b0d623f7
A
79#include <machine/endian.h>
80
1c79356b
A
81#include <net/if.h>
82#include <net/route.h>
bca245ac 83#include <net/content_filter.h>
1c79356b
A
84
85#define _IP_VHL
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>
e5568f75
A
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>
1c79356b
A
99
100#if IPSEC
101#include <netinet6/ipsec.h>
102#include <netkey/key.h>
13fec989
A
103#endif
104
fe8ab488
A
105#if NECP
106#include <net/necp.h>
107#endif /* NECP */
108
0a7de745 109/* XXX This one should go in sys/mbuf.h. It is used to avoid that
13fec989
A
110 * a firewall-generated packet loops forever through the firewall.
111 */
112#ifndef M_SKIP_FIREWALL
113#define M_SKIP_FIREWALL 0x4000
0a7de745 114#endif
2d21ac55
A
115
116#if CONFIG_MACF_NET
117#include <security/mac_framework.h>
118#endif /* MAC_NET */
1c79356b 119
b0d623f7 120
1c79356b
A
121/*
122 * ICMP routines: error generation, receive packet processing, and
123 * routines to turnaround packets back to the originator, and
124 * host table maintenance routines.
125 */
126
0a7de745 127struct icmpstat icmpstat;
04b8595b
A
128SYSCTL_STRUCT(_net_inet_icmp, ICMPCTL_STATS, stats,
129 CTLFLAG_RD | CTLFLAG_LOCKED,
130 &icmpstat, icmpstat, "");
1c79356b 131
0a7de745 132static int icmpmaskrepl = 0;
04b8595b
A
133SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl,
134 CTLFLAG_RW | CTLFLAG_LOCKED,
135 &icmpmaskrepl, 0, "");
1c79356b 136
0a7de745 137static int icmptimestamp = 0;
04b8595b
A
138SYSCTL_INT(_net_inet_icmp, ICMPCTL_TIMESTAMP, timestamp,
139 CTLFLAG_RW | CTLFLAG_LOCKED,
140 &icmptimestamp, 0, "");
55e303ae 141
0a7de745 142static int drop_redirect = 1;
04b8595b
A
143SYSCTL_INT(_net_inet_icmp, OID_AUTO, drop_redirect,
144 CTLFLAG_RW | CTLFLAG_LOCKED,
145 &drop_redirect, 0, "");
9bccf70c 146
0a7de745 147static int log_redirect = 0;
04b8595b
A
148SYSCTL_INT(_net_inet_icmp, OID_AUTO, log_redirect,
149 CTLFLAG_RW | CTLFLAG_LOCKED,
150 &log_redirect, 0, "");
151
3e170ce0 152const static int icmp_datalen = 8;
9bccf70c 153
0a7de745 154#if ICMP_BANDLIM
316670eb
A
155
156/* Default values in case CONFIG_ICMP_BANDLIM is not defined in the MASTER file */
157#ifndef CONFIG_ICMP_BANDLIM
5ba3f43e 158#if !CONFIG_EMBEDDED
316670eb 159#define CONFIG_ICMP_BANDLIM 250
5ba3f43e
A
160#else /* CONFIG_EMBEDDED */
161#define CONFIG_ICMP_BANDLIM 50
162#endif /* CONFIG_EMBEDDED */
316670eb
A
163#endif /* CONFIG_ICMP_BANDLIM */
164
0a7de745 165/*
1c79356b
A
166 * ICMP error-response bandwidth limiting sysctl. If not enabled, sysctl
167 * variable content is -1 and read-only.
0a7de745
A
168 */
169
316670eb 170static int icmplim = CONFIG_ICMP_BANDLIM;
6d2010ae 171SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RW | CTLFLAG_LOCKED,
0a7de745 172 &icmplim, 0, "");
316670eb
A
173
174#else /* ICMP_BANDLIM */
1c79356b
A
175
176static int icmplim = -1;
6d2010ae 177SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RD | CTLFLAG_LOCKED,
0a7de745
A
178 &icmplim, 0, "");
179
316670eb 180#endif /* ICMP_BANDLIM */
1c79356b
A
181
182/*
183 * ICMP broadcast echo sysctl
184 */
185
0a7de745 186static int icmpbmcastecho = 1;
6d2010ae 187SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_RW | CTLFLAG_LOCKED,
0a7de745 188 &icmpbmcastecho, 0, "");
1c79356b 189
39037602 190#if (DEBUG | DEVELOPMENT)
0a7de745 191static int icmpprintfs = 0;
39037602 192SYSCTL_INT(_net_inet_icmp, OID_AUTO, verbose, CTLFLAG_RW | CTLFLAG_LOCKED,
0a7de745 193 &icmpprintfs, 0, "");
1c79356b
A
194#endif
195
0a7de745
A
196static void icmp_reflect(struct mbuf *);
197static void icmp_send(struct mbuf *, struct mbuf *);
1c79356b 198
1c79356b
A
199/*
200 * Generate an error packet of type error
201 * in response to bad packet ip.
202 */
203void
91447636
A
204icmp_error(
205 struct mbuf *n,
206 int type,
207 int code,
04b8595b 208 u_int32_t dest,
b0d623f7 209 u_int32_t nextmtu)
1c79356b 210{
d9a64523
A
211 struct ip *oip = NULL;
212 struct ip *nip = NULL;
213 struct icmp *icp = NULL;
214 struct mbuf *m = NULL;
215 u_int32_t oiphlen = 0;
216 u_int32_t icmplen = 0;
217 u_int32_t icmpelen = 0;
218 u_int32_t nlen = 0;
219
220 VERIFY((u_int)type <= ICMP_MAXTYPE);
316670eb
A
221 /* Expect 32-bit aligned data pointer on strict-align platforms */
222 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(n);
223
0a7de745 224 if (type != ICMP_REDIRECT) {
d9a64523 225 icmpstat.icps_error++;
0a7de745 226 }
d9a64523
A
227 /*
228 * Don't send error:
229 * if not the first fragment of message
230 * if original packet was a multicast or broadcast packet
231 * if the old packet protocol was ICMP
232 * error message, only known informational types.
233 */
0a7de745 234 if (n->m_flags & (M_BCAST | M_MCAST)) {
d9a64523 235 goto freeit;
0a7de745 236 }
d9a64523
A
237
238 /*
239 * Drop if IP header plus ICMP_MINLEN bytes are not contiguous
240 * in first mbuf.
241 */
0a7de745 242 if (n->m_len < sizeof(struct ip) + ICMP_MINLEN) {
d9a64523 243 goto freeit;
0a7de745 244 }
d9a64523 245
04b8595b
A
246 oip = mtod(n, struct ip *);
247 oiphlen = IP_VHL_HL(oip->ip_vhl) << 2;
0a7de745 248 if (n->m_len < oiphlen + ICMP_MINLEN) {
d9a64523 249 goto freeit;
0a7de745 250 }
316670eb 251
39037602 252#if (DEBUG | DEVELOPMENT)
0a7de745 253 if (icmpprintfs > 1) {
39236c6e
A
254 printf("icmp_error(0x%llx, %x, %d)\n",
255 (uint64_t)VM_KERNEL_ADDRPERM(oip), type, code);
0a7de745 256 }
1c79356b 257#endif
d9a64523 258
0a7de745 259 if (oip->ip_off & ~(IP_MF | IP_DF)) {
1c79356b 260 goto freeit;
0a7de745 261 }
04b8595b 262
1c79356b 263 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
d9a64523
A
264 n->m_len >= oiphlen + ICMP_MINLEN &&
265 !ICMP_INFOTYPE(((struct icmp *)(void *)((caddr_t)oip + oiphlen))->
0a7de745 266 icmp_type)) {
1c79356b
A
267 icmpstat.icps_oldicmp++;
268 goto freeit;
269 }
04b8595b
A
270
271 /*
272 * Calculate the length to quote from original packet and prevent
273 * the ICMP mbuf from overflowing.
d9a64523
A
274 * Unfortunatly this is non-trivial since ip_forward()
275 * sends us truncated packets.
04b8595b
A
276 */
277 nlen = m_length(n);
278 if (oip->ip_p == IPPROTO_TCP) {
d9a64523
A
279 struct tcphdr *th = NULL;
280 u_int16_t tcphlen = 0;
04b8595b 281
d9a64523
A
282 /*
283 * If the packet got truncated and TCP header
284 * is not contained in the packet, send out
285 * standard reply with only IP header as payload
286 */
04b8595b 287 if (oiphlen + sizeof(struct tcphdr) > n->m_len &&
0a7de745 288 n->m_next == NULL) {
04b8595b 289 goto stdreply;
0a7de745 290 }
d9a64523
A
291
292 /*
293 * Otherwise, pull up to get IP and TCP headers
294 * together
295 */
04b8595b 296 if (n->m_len < (oiphlen + sizeof(struct tcphdr)) &&
0a7de745 297 (n = m_pullup(n, (oiphlen + sizeof(struct tcphdr)))) == NULL) {
04b8595b 298 goto freeit;
0a7de745 299 }
04b8595b 300
a39ff7e2
A
301 /*
302 * Reinit pointers derived from mbuf data pointer
303 * as things might have moved around with m_pullup
304 */
305 oip = mtod(n, struct ip *);
04b8595b 306 th = (struct tcphdr *)(void *)((caddr_t)oip + oiphlen);
a39ff7e2 307
04b8595b 308 if (th != ((struct tcphdr *)P2ROUNDDOWN(th,
0a7de745 309 sizeof(u_int32_t)))) {
04b8595b 310 goto freeit;
0a7de745 311 }
04b8595b 312 tcphlen = th->th_off << 2;
d9a64523
A
313
314 /* Sanity checks */
0a7de745 315 if (tcphlen < sizeof(struct tcphdr)) {
04b8595b 316 goto freeit;
0a7de745
A
317 }
318 if (oip->ip_len < (oiphlen + tcphlen)) {
04b8595b 319 goto freeit;
0a7de745
A
320 }
321 if ((oiphlen + tcphlen) > n->m_len && n->m_next == NULL) {
04b8595b 322 goto stdreply;
0a7de745 323 }
04b8595b 324 if (n->m_len < (oiphlen + tcphlen) &&
0a7de745 325 (n = m_pullup(n, (oiphlen + tcphlen))) == NULL) {
04b8595b 326 goto freeit;
0a7de745 327 }
04b8595b 328
a39ff7e2
A
329 /*
330 * Reinit pointers derived from mbuf data pointer
331 * as things might have moved around with m_pullup
332 */
333 oip = mtod(n, struct ip *);
334 th = (struct tcphdr *)(void *)((caddr_t)oip + oiphlen);
335
04b8595b
A
336 icmpelen = max(tcphlen, min(icmp_datalen,
337 (oip->ip_len - oiphlen)));
0a7de745
A
338 } else {
339stdreply: icmpelen = max(ICMP_MINLEN, min(icmp_datalen,
39037602 340 (oip->ip_len - oiphlen)));
0a7de745 341 }
04b8595b 342
d9a64523 343 icmplen = min(oiphlen + icmpelen, nlen);
0a7de745 344 if (icmplen < sizeof(struct ip)) {
04b8595b 345 goto freeit;
0a7de745 346 }
d9a64523 347
1c79356b
A
348 /*
349 * First, formulate icmp message
d9a64523
A
350 * Allocate enough space for the IP header, ICMP header
351 * and the payload (part of the original message to be sent back).
1c79356b 352 */
0a7de745
A
353 if (MHLEN > (sizeof(struct ip) + ICMP_MINLEN + icmplen)) {
354 m = m_gethdr(M_DONTWAIT, MT_HEADER); /* MAC-OK */
355 } else {
04b8595b 356 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
0a7de745 357 }
04b8595b 358
0a7de745 359 if (m == NULL) {
1c79356b 360 goto freeit;
0a7de745 361 }
13fec989 362
2d21ac55
A
363#if CONFIG_MACF_NET
364 mac_mbuf_label_associate_netlayer(n, m);
365#endif
d9a64523
A
366 /*
367 * Further refine the payload length to the space
368 * remaining in mbuf after including the IP header and ICMP
369 * header.
370 */
371 icmplen = min(icmplen, M_TRAILINGSPACE(m) -
372 sizeof(struct ip) - ICMP_MINLEN);
373 m_align(m, ICMP_MINLEN + icmplen);
374 m->m_len = ICMP_MINLEN + icmplen; /* for ICMP header and data */
375
1c79356b 376 icp = mtod(m, struct icmp *);
1c79356b
A
377 icmpstat.icps_outhist[type]++;
378 icp->icmp_type = type;
0a7de745 379 if (type == ICMP_REDIRECT) {
1c79356b 380 icp->icmp_gwaddr.s_addr = dest;
0a7de745 381 } else {
1c79356b
A
382 icp->icmp_void = 0;
383 /*
384 * The following assignments assume an overlay with the
385 * zeroed icmp_void field.
386 */
387 if (type == ICMP_PARAMPROB) {
388 icp->icmp_pptr = code;
389 code = 0;
390 } else if (type == ICMP_UNREACH &&
b0d623f7
A
391 code == ICMP_UNREACH_NEEDFRAG && nextmtu != 0) {
392 icp->icmp_nextmtu = htons(nextmtu);
1c79356b
A
393 }
394 }
395
396 icp->icmp_code = code;
d9a64523
A
397
398 /*
399 * Copy icmplen worth of content from original
400 * mbuf (n) to the new packet after ICMP header.
401 */
9bccf70c 402 m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip);
1c79356b 403 nip = &icp->icmp_ip;
9bccf70c
A
404
405 /*
406 * Convert fields to network representation.
407 */
b0d623f7 408#if BYTE_ORDER != BIG_ENDIAN
9bccf70c
A
409 HTONS(nip->ip_len);
410 HTONS(nip->ip_off);
b0d623f7 411#endif
1c79356b 412 /*
d9a64523
A
413 * Set up ICMP message mbuf and copy old IP header (without options
414 * in front of ICMP message.
415 * If the original mbuf was meant to bypass the firewall, the error
416 * reply should bypass as well.
0a7de745 417 */
d9a64523 418 m->m_flags |= n->m_flags & M_SKIP_FIREWALL;
1c79356b
A
419 m->m_data -= sizeof(struct ip);
420 m->m_len += sizeof(struct ip);
421 m->m_pkthdr.len = m->m_len;
422 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
1c79356b
A
423 nip = mtod(m, struct ip *);
424 bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
425 nip->ip_len = m->m_len;
426 nip->ip_vhl = IP_VHL_BORING;
427 nip->ip_p = IPPROTO_ICMP;
428 nip->ip_tos = 0;
04b8595b 429 nip->ip_off = 0;
1c79356b 430 icmp_reflect(m);
1c79356b
A
431freeit:
432 m_freem(n);
433}
434
1c79356b
A
435/*
436 * Process a received ICMP message.
437 */
438void
2d21ac55 439icmp_input(struct mbuf *m, int hlen)
1c79356b 440{
39236c6e 441 struct sockaddr_in icmpsrc, icmpdst, icmpgw;
2d21ac55
A
442 struct icmp *icp;
443 struct ip *ip = mtod(m, struct ip *);
316670eb 444 int icmplen;
2d21ac55 445 int i;
1c79356b 446 struct in_ifaddr *ia;
5ba3f43e 447 void (*ctlfunc)(int, struct sockaddr *, void *, struct ifnet *);
1c79356b 448 int code;
cb323159 449 boolean_t should_log_redirect = false;
1c79356b 450
316670eb
A
451 /* Expect 32-bit aligned data pointer on strict-align platforms */
452 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
453
454 icmplen = ip->ip_len;
455
1c79356b
A
456 /*
457 * Locate icmp structure in mbuf, and check
458 * that not corrupted and of at least minimum length.
459 */
39037602 460#if (DEBUG | DEVELOPMENT)
0a7de745 461 if (icmpprintfs > 2) {
39037602
A
462 char src_str[MAX_IPv4_STR_LEN];
463 char dst_str[MAX_IPv4_STR_LEN];
464
465 inet_ntop(AF_INET, &ip->ip_src, src_str, sizeof(src_str));
466 inet_ntop(AF_INET, &ip->ip_dst, dst_str, sizeof(dst_str));
467 printf("%s: from %s to %s, len %d\n",
468 __func__, src_str, dst_str, icmplen);
1c79356b
A
469 }
470#endif
471 if (icmplen < ICMP_MINLEN) {
472 icmpstat.icps_tooshort++;
473 goto freeit;
474 }
475 i = hlen + min(icmplen, ICMP_ADVLENMIN);
0a7de745 476 if (m->m_len < i && (m = m_pullup(m, i)) == 0) {
1c79356b
A
477 icmpstat.icps_tooshort++;
478 return;
479 }
480 ip = mtod(m, struct ip *);
481 m->m_len -= hlen;
482 m->m_data += hlen;
483 icp = mtod(m, struct icmp *);
484 if (in_cksum(m, icmplen)) {
485 icmpstat.icps_checksum++;
486 goto freeit;
487 }
488 m->m_len += hlen;
489 m->m_data -= hlen;
490
39037602 491#if (DEBUG | DEVELOPMENT)
0a7de745 492 if (icmpprintfs > 2) {
1c79356b
A
493 printf("icmp_input, type %d code %d\n", icp->icmp_type,
494 icp->icmp_code);
0a7de745 495 }
1c79356b
A
496#endif
497
1c79356b
A
498 /*
499 * Message type specific processing.
500 */
0a7de745 501 if (icp->icmp_type > ICMP_MAXTYPE) {
1c79356b 502 goto raw;
0a7de745 503 }
39236c6e
A
504
505 /* Initialize */
0a7de745
A
506 bzero(&icmpsrc, sizeof(icmpsrc));
507 icmpsrc.sin_len = sizeof(struct sockaddr_in);
39236c6e 508 icmpsrc.sin_family = AF_INET;
0a7de745
A
509 bzero(&icmpdst, sizeof(icmpdst));
510 icmpdst.sin_len = sizeof(struct sockaddr_in);
39236c6e 511 icmpdst.sin_family = AF_INET;
0a7de745
A
512 bzero(&icmpgw, sizeof(icmpgw));
513 icmpgw.sin_len = sizeof(struct sockaddr_in);
39236c6e
A
514 icmpgw.sin_family = AF_INET;
515
1c79356b
A
516 icmpstat.icps_inhist[icp->icmp_type]++;
517 code = icp->icmp_code;
518 switch (icp->icmp_type) {
1c79356b
A
519 case ICMP_UNREACH:
520 switch (code) {
0a7de745
A
521 case ICMP_UNREACH_NET:
522 case ICMP_UNREACH_HOST:
523 case ICMP_UNREACH_SRCFAIL:
524 case ICMP_UNREACH_NET_UNKNOWN:
525 case ICMP_UNREACH_HOST_UNKNOWN:
526 case ICMP_UNREACH_ISOLATED:
527 case ICMP_UNREACH_TOSNET:
528 case ICMP_UNREACH_TOSHOST:
529 case ICMP_UNREACH_HOST_PRECEDENCE:
530 case ICMP_UNREACH_PRECEDENCE_CUTOFF:
531 code = PRC_UNREACH_NET;
532 break;
1c79356b 533
0a7de745
A
534 case ICMP_UNREACH_NEEDFRAG:
535 code = PRC_MSGSIZE;
536 break;
1c79356b 537
0a7de745
A
538 /*
539 * RFC 1122, Sections 3.2.2.1 and 4.2.3.9.
540 * Treat subcodes 2,3 as immediate RST
541 */
542 case ICMP_UNREACH_PROTOCOL:
543 case ICMP_UNREACH_PORT:
544 code = PRC_UNREACH_PORT;
545 break;
1c79356b 546
0a7de745
A
547 case ICMP_UNREACH_NET_PROHIB:
548 case ICMP_UNREACH_HOST_PROHIB:
549 case ICMP_UNREACH_FILTER_PROHIB:
550 code = PRC_UNREACH_ADMIN_PROHIB;
551 break;
1c79356b 552
0a7de745
A
553 default:
554 goto badcode;
1c79356b
A
555 }
556 goto deliver;
557
558 case ICMP_TIMXCEED:
0a7de745 559 if (code > 1) {
1c79356b 560 goto badcode;
0a7de745 561 }
1c79356b
A
562 code += PRC_TIMXCEED_INTRANS;
563 goto deliver;
564
565 case ICMP_PARAMPROB:
0a7de745 566 if (code > 1) {
1c79356b 567 goto badcode;
0a7de745 568 }
1c79356b
A
569 code = PRC_PARAMPROB;
570 goto deliver;
571
572 case ICMP_SOURCEQUENCH:
0a7de745 573 if (code) {
1c79356b 574 goto badcode;
0a7de745 575 }
1c79356b 576 code = PRC_QUENCH;
0a7de745 577deliver:
1c79356b
A
578 /*
579 * Problem with datagram; advise higher level routines.
580 */
39037602
A
581 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp)
582 || IP_VHL_HL(icp->icmp_ip.ip_vhl) <
cb323159
A
583 (sizeof(struct ip) >> 2) ||
584 (m = m_pullup(m, hlen + ICMP_ADVLEN(icp))) == NULL) {
1c79356b
A
585 icmpstat.icps_badlen++;
586 goto freeit;
587 }
b0d623f7 588
cb323159
A
589 ip = mtod(m, struct ip *);
590 icp = (struct icmp *)(void *)(mtod(m, uint8_t *) + hlen);
591
b0d623f7 592#if BYTE_ORDER != BIG_ENDIAN
1c79356b 593 NTOHS(icp->icmp_ip.ip_len);
b0d623f7
A
594#endif
595
1c79356b 596 /* Discard ICMP's in response to multicast packets */
0a7de745 597 if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr))) {
1c79356b 598 goto badcode;
0a7de745 599 }
39037602 600#if (DEBUG | DEVELOPMENT)
0a7de745 601 if (icmpprintfs > 2) {
39037602
A
602 printf("deliver to protocol %d\n",
603 icp->icmp_ip.ip_p);
0a7de745 604 }
1c79356b
A
605#endif
606 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
1c79356b 607
1c79356b 608 /*
39037602 609 * if the packet contains [IPv4 AH TCP], we can't make a
1c79356b
A
610 * notification to TCP layer.
611 */
612 ctlfunc = ip_protox[icp->icmp_ip.ip_p]->pr_ctlinput;
5c9f4661
A
613
614 if (ctlfunc) {
615 LCK_MTX_ASSERT(inet_domain_mutex, LCK_MTX_ASSERT_OWNED);
616
617 lck_mtx_unlock(inet_domain_mutex);
618
1c79356b 619 (*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
0a7de745 620 (void *)&icp->icmp_ip, m->m_pkthdr.rcvif);
5c9f4661
A
621
622 lck_mtx_lock(inet_domain_mutex);
623 }
1c79356b
A
624 break;
625
0a7de745 626badcode:
1c79356b
A
627 icmpstat.icps_badcode++;
628 break;
629
630 case ICMP_ECHO:
0a7de745 631 if ((m->m_flags & (M_MCAST | M_BCAST))) {
39037602
A
632 if (icmpbmcastecho == 0) {
633 icmpstat.icps_bmcastecho++;
634 break;
635 }
636 }
0a7de745 637
39037602
A
638 /*
639 * rdar://18644769
640 * Do not reply when the destination is link local multicast or broadcast
641 * and the source is not from a directly connected subnet
642 */
643 if ((IN_LOCAL_GROUP(ntohl(ip->ip_dst.s_addr)) ||
644 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) &&
645 in_localaddr(ip->ip_src) == 0) {
1c79356b 646 icmpstat.icps_bmcastecho++;
39037602
A
647#if (DEBUG | DEVELOPMENT)
648 if (icmpprintfs > 0) {
649 char src_str[MAX_IPv4_STR_LEN];
650 char dst_str[MAX_IPv4_STR_LEN];
651
652 inet_ntop(AF_INET, &ip->ip_src, src_str, sizeof(src_str));
653 inet_ntop(AF_INET, &ip->ip_dst, dst_str, sizeof(dst_str));
654 printf("%s: non local (B|M)CAST %s to %s, len %d\n",
655 __func__, src_str, dst_str, icmplen);
656 }
657#endif
1c79356b
A
658 break;
659 }
39037602 660
1c79356b 661 icp->icmp_type = ICMP_ECHOREPLY;
9bccf70c 662#if ICMP_BANDLIM
0a7de745 663 if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0) {
9bccf70c 664 goto freeit;
0a7de745 665 } else
9bccf70c 666#endif
0a7de745 667 goto reflect;
1c79356b
A
668
669 case ICMP_TSTAMP:
0a7de745 670 if (icmptimestamp == 0) {
55e303ae 671 break;
0a7de745 672 }
55e303ae 673
1c79356b
A
674 if (!icmpbmcastecho
675 && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
676 icmpstat.icps_bmcasttstamp++;
677 break;
678 }
679 if (icmplen < ICMP_TSLEN) {
680 icmpstat.icps_badlen++;
681 break;
682 }
683 icp->icmp_type = ICMP_TSTAMPREPLY;
684 icp->icmp_rtime = iptime();
0a7de745 685 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */
9bccf70c 686#if ICMP_BANDLIM
0a7de745 687 if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0) {
9bccf70c 688 goto freeit;
0a7de745 689 } else
9bccf70c 690#endif
0a7de745 691 goto reflect;
1c79356b
A
692
693 case ICMP_MASKREQ:
0a7de745 694 if (icmpmaskrepl == 0) {
1c79356b 695 break;
0a7de745 696 }
1c79356b
A
697 /*
698 * We are not able to respond with all ones broadcast
699 * unless we receive it over a point-to-point interface.
700 */
0a7de745 701 if (icmplen < ICMP_MASKLEN) {
1c79356b 702 break;
0a7de745 703 }
1c79356b 704 switch (ip->ip_dst.s_addr) {
1c79356b
A
705 case INADDR_BROADCAST:
706 case INADDR_ANY:
707 icmpdst.sin_addr = ip->ip_src;
708 break;
709
710 default:
711 icmpdst.sin_addr = ip->ip_dst;
712 }
713 ia = (struct in_ifaddr *)ifaof_ifpforaddr(
0a7de745
A
714 (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
715 if (ia == 0) {
1c79356b 716 break;
0a7de745 717 }
6d2010ae 718 IFA_LOCK(&ia->ia_ifa);
91447636 719 if (ia->ia_ifp == 0) {
6d2010ae
A
720 IFA_UNLOCK(&ia->ia_ifa);
721 IFA_REMREF(&ia->ia_ifa);
2d21ac55 722 ia = NULL;
1c79356b 723 break;
91447636 724 }
1c79356b
A
725 icp->icmp_type = ICMP_MASKREPLY;
726 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
727 if (ip->ip_src.s_addr == 0) {
0a7de745
A
728 if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
729 ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
730 } else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) {
731 ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
732 }
1c79356b 733 }
6d2010ae
A
734 IFA_UNLOCK(&ia->ia_ifa);
735 IFA_REMREF(&ia->ia_ifa);
1c79356b 736reflect:
0a7de745 737 ip->ip_len += hlen; /* since ip_input deducts this */
1c79356b
A
738 icmpstat.icps_reflect++;
739 icmpstat.icps_outhist[icp->icmp_type]++;
740 icmp_reflect(m);
741 return;
742
743 case ICMP_REDIRECT:
0a7de745 744 if (drop_redirect) {
9bccf70c 745 break;
0a7de745
A
746 }
747 if (code > 3) {
1c79356b 748 goto badcode;
0a7de745 749 }
1c79356b
A
750 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
751 IP_VHL_HL(icp->icmp_ip.ip_vhl) < (sizeof(struct ip) >> 2)) {
752 icmpstat.icps_badlen++;
753 break;
754 }
cb323159
A
755
756#if (DEBUG | DEVELOPMENT)
757 should_log_redirect = log_redirect || (icmpprintfs > 0);
758#else
759 should_log_redirect = log_redirect;
760#endif
1c79356b
A
761 /*
762 * Short circuit routing redirects to force
763 * immediate change in the kernel's routing
764 * tables. The message is also handed to anyone
765 * listening on a raw socket (e.g. the routing
766 * daemon for use in updating its tables).
767 */
768 icmpgw.sin_addr = ip->ip_src;
769 icmpdst.sin_addr = icp->icmp_gwaddr;
cb323159
A
770
771 if (should_log_redirect) {
772 char src_str[MAX_IPv4_STR_LEN];
39037602
A
773 char dst_str[MAX_IPv4_STR_LEN];
774 char gw_str[MAX_IPv4_STR_LEN];
775
cb323159 776 inet_ntop(AF_INET, &ip->ip_src, src_str, sizeof(src_str));
39037602
A
777 inet_ntop(AF_INET, &icp->icmp_ip.ip_dst, dst_str, sizeof(dst_str));
778 inet_ntop(AF_INET, &icp->icmp_gwaddr, gw_str, sizeof(gw_str));
cb323159
A
779 printf("%s: redirect dst %s to %s from %s\n", __func__,
780 dst_str, gw_str, src_str);
1c79356b 781 }
1c79356b 782 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
c910b4d9 783 rtredirect(m->m_pkthdr.rcvif, (struct sockaddr *)&icmpsrc,
0a7de745
A
784 (struct sockaddr *)&icmpdst, NULL, RTF_GATEWAY | RTF_HOST,
785 (struct sockaddr *)&icmpgw, NULL);
1c79356b
A
786 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
787#if IPSEC
788 key_sa_routechange((struct sockaddr *)&icmpsrc);
789#endif
790 break;
791
792 /*
793 * No kernel processing for the following;
794 * just fall through to send to raw listener.
795 */
796 case ICMP_ECHOREPLY:
797 case ICMP_ROUTERADVERT:
798 case ICMP_ROUTERSOLICIT:
799 case ICMP_TSTAMPREPLY:
800 case ICMP_IREQREPLY:
801 case ICMP_MASKREPLY:
802 default:
803 break;
804 }
805
806raw:
807 rip_input(m, hlen);
808 return;
809
810freeit:
811 m_freem(m);
812}
813
814/*
815 * Reflect the ip packet back to the source
816 */
817static void
2d21ac55 818icmp_reflect(struct mbuf *m)
1c79356b 819{
2d21ac55 820 struct ip *ip = mtod(m, struct ip *);
39236c6e 821 struct sockaddr_in icmpdst;
2d21ac55 822 struct in_ifaddr *ia;
1c79356b 823 struct in_addr t;
2d21ac55 824 struct mbuf *opts = NULL;
1c79356b
A
825 int optlen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip);
826
827 if (!in_canforward(ip->ip_src) &&
828 ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) !=
0a7de745
A
829 (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
830 m_freem(m); /* Bad return address */
831 goto done; /* Ip_output() will check for broadcast */
1c79356b
A
832 }
833 t = ip->ip_dst;
834 ip->ip_dst = ip->ip_src;
835 /*
836 * If the incoming packet was addressed directly to us,
837 * use dst as the src for the reply. Otherwise (broadcast
838 * or anonymous), use the address which corresponds
839 * to the incoming interface.
840 */
b0d623f7
A
841 lck_rw_lock_shared(in_ifaddr_rwlock);
842 TAILQ_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) {
6d2010ae
A
843 IFA_LOCK(&ia->ia_ifa);
844 if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) {
845 IFA_ADDREF_LOCKED(&ia->ia_ifa);
846 IFA_UNLOCK(&ia->ia_ifa);
b0d623f7 847 goto match;
6d2010ae
A
848 }
849 IFA_UNLOCK(&ia->ia_ifa);
b0d623f7
A
850 }
851 /*
852 * Slow path; check for broadcast addresses. Find a source
853 * IP address to use when replying to the broadcast request;
854 * let IP handle the source interface selection work.
855 */
856 for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next) {
6d2010ae 857 IFA_LOCK(&ia->ia_ifa);
1c79356b 858 if (ia->ia_ifp && (ia->ia_ifp->if_flags & IFF_BROADCAST) &&
6d2010ae
A
859 t.s_addr == satosin(&ia->ia_broadaddr)->sin_addr.s_addr) {
860 IFA_ADDREF_LOCKED(&ia->ia_ifa);
861 IFA_UNLOCK(&ia->ia_ifa);
1c79356b 862 break;
6d2010ae
A
863 }
864 IFA_UNLOCK(&ia->ia_ifa);
1c79356b 865 }
b0d623f7 866match:
b0d623f7 867 lck_rw_done(in_ifaddr_rwlock);
39236c6e
A
868
869 /* Initialize */
0a7de745
A
870 bzero(&icmpdst, sizeof(icmpdst));
871 icmpdst.sin_len = sizeof(struct sockaddr_in);
39236c6e 872 icmpdst.sin_family = AF_INET;
1c79356b 873 icmpdst.sin_addr = t;
0a7de745 874 if ((ia == (struct in_ifaddr *)0) && m->m_pkthdr.rcvif) {
1c79356b
A
875 ia = (struct in_ifaddr *)ifaof_ifpforaddr(
876 (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
0a7de745 877 }
1c79356b
A
878 /*
879 * The following happens if the packet was not addressed to us,
880 * and was received on an interface with no IP address.
881 */
91447636 882 if (ia == (struct in_ifaddr *)0) {
b0d623f7 883 lck_rw_lock_shared(in_ifaddr_rwlock);
1c79356b 884 ia = in_ifaddrhead.tqh_first;
cc9f6e38 885 if (ia == (struct in_ifaddr *)0) {/* no address yet, bail out */
b0d623f7 886 lck_rw_done(in_ifaddr_rwlock);
cc9f6e38 887 m_freem(m);
cc9f6e38
A
888 goto done;
889 }
6d2010ae 890 IFA_ADDREF(&ia->ia_ifa);
b0d623f7 891 lck_rw_done(in_ifaddr_rwlock);
91447636 892 }
2d21ac55
A
893#if CONFIG_MACF_NET
894 mac_netinet_icmp_reply(m);
895#endif
6d2010ae 896 IFA_LOCK_SPIN(&ia->ia_ifa);
1c79356b 897 t = IA_SIN(ia)->sin_addr;
6d2010ae 898 IFA_UNLOCK(&ia->ia_ifa);
1c79356b 899 ip->ip_src = t;
9bccf70c 900 ip->ip_ttl = ip_defttl;
6d2010ae 901 IFA_REMREF(&ia->ia_ifa);
91447636 902 ia = NULL;
1c79356b
A
903
904 if (optlen > 0) {
2d21ac55 905 u_char *cp;
1c79356b
A
906 int opt, cnt;
907 u_int len;
908
909 /*
910 * Retrieve any source routing from the incoming packet;
911 * add on any record-route or timestamp options.
912 */
913 cp = (u_char *) (ip + 1);
914 if ((opts = ip_srcroute()) == 0 &&
0a7de745 915 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) { /* MAC-OK */
1c79356b
A
916 opts->m_len = sizeof(struct in_addr);
917 mtod(opts, struct in_addr *)->s_addr = 0;
918 }
919 if (opts) {
39037602 920#if (DEBUG | DEVELOPMENT)
0a7de745
A
921 if (icmpprintfs > 1) {
922 printf("icmp_reflect optlen %d rt %d => ",
923 optlen, opts->m_len);
924 }
1c79356b 925#endif
0a7de745
A
926 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
927 opt = cp[IPOPT_OPTVAL];
928 if (opt == IPOPT_EOL) {
929 break;
930 }
931 if (opt == IPOPT_NOP) {
932 len = 1;
933 } else {
934 if (cnt < IPOPT_OLEN + sizeof(*cp)) {
935 break;
936 }
937 len = cp[IPOPT_OLEN];
938 if (len < IPOPT_OLEN + sizeof(*cp) ||
939 len > cnt) {
940 break;
941 }
942 }
943 /*
944 * Should check for overflow, but it "can't happen"
945 */
946 if (opt == IPOPT_RR || opt == IPOPT_TS ||
947 opt == IPOPT_SECURITY) {
948 bcopy((caddr_t)cp,
949 mtod(opts, caddr_t) + opts->m_len, len);
950 opts->m_len += len;
951 }
952 }
953 /* Terminate & pad, if necessary */
954 cnt = opts->m_len % 4;
955 if (cnt) {
956 for (; cnt < 4; cnt++) {
957 *(mtod(opts, caddr_t) + opts->m_len) =
958 IPOPT_EOL;
959 opts->m_len++;
960 }
961 }
39037602 962#if (DEBUG | DEVELOPMENT)
0a7de745
A
963 if (icmpprintfs > 1) {
964 printf("%d\n", opts->m_len);
965 }
1c79356b
A
966#endif
967 }
968 /*
969 * Now strip out original options by copying rest of first
970 * mbuf's data back, and adjust the IP length.
971 */
972 ip->ip_len -= optlen;
973 ip->ip_vhl = IP_VHL_BORING;
974 m->m_len -= optlen;
0a7de745 975 if (m->m_flags & M_PKTHDR) {
1c79356b 976 m->m_pkthdr.len -= optlen;
0a7de745 977 }
1c79356b
A
978 optlen += sizeof(struct ip);
979 bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
0a7de745 980 (unsigned)(m->m_len - sizeof(struct ip)));
1c79356b 981 }
0a7de745 982 m->m_flags &= ~(M_BCAST | M_MCAST);
1c79356b
A
983 icmp_send(m, opts);
984done:
0a7de745 985 if (opts) {
1c79356b 986 (void)m_free(opts);
0a7de745 987 }
1c79356b
A
988}
989
990/*
991 * Send an icmp packet back to the ip level,
992 * after supplying a checksum.
993 */
994static void
2d21ac55 995icmp_send(struct mbuf *m, struct mbuf *opts)
1c79356b 996{
2d21ac55
A
997 struct ip *ip = mtod(m, struct ip *);
998 int hlen;
999 struct icmp *icp;
1c79356b 1000 struct route ro;
a39ff7e2
A
1001 struct ip_out_args ipoa;
1002
1003 bzero(&ipoa, sizeof(ipoa));
1004 ipoa.ipoa_boundif = IFSCOPE_NONE;
1005 ipoa.ipoa_flags = IPOAF_SELECT_SRCIF | IPOAF_BOUND_SRCADDR;
1006 ipoa.ipoa_sotc = SO_TC_UNSPEC;
1007 ipoa.ipoa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
c910b4d9 1008
39236c6e 1009 if (!(m->m_pkthdr.pkt_flags & PKTF_LOOP) && m->m_pkthdr.rcvif != NULL) {
6d2010ae 1010 ipoa.ipoa_boundif = m->m_pkthdr.rcvif->if_index;
316670eb
A
1011 ipoa.ipoa_flags |= IPOAF_BOUND_IF;
1012 }
1c79356b
A
1013
1014 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
1015 m->m_data += hlen;
1016 m->m_len -= hlen;
1017 icp = mtod(m, struct icmp *);
1018 icp->icmp_cksum = 0;
1019 icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
1020 m->m_data -= hlen;
1021 m->m_len += hlen;
2d21ac55 1022 m->m_pkthdr.rcvif = NULL;
0b4e3aa0
A
1023 m->m_pkthdr.csum_data = 0;
1024 m->m_pkthdr.csum_flags = 0;
39037602
A
1025#if (DEBUG | DEVELOPMENT)
1026 if (icmpprintfs > 2) {
1027 char src_str[MAX_IPv4_STR_LEN];
1028 char dst_str[MAX_IPv4_STR_LEN];
1029
1030 inet_ntop(AF_INET, &ip->ip_src, src_str, sizeof(src_str));
1031 inet_ntop(AF_INET, &ip->ip_dst, dst_str, sizeof(dst_str));
1032 printf("%s: dst %s src %s\n", __func__, dst_str, src_str);
1c79356b
A
1033 }
1034#endif
1035 bzero(&ro, sizeof ro);
c910b4d9 1036 (void) ip_output(m, opts, &ro, IP_OUTARGS, NULL, &ipoa);
39236c6e 1037 ROUTE_RELEASE(&ro);
1c79356b
A
1038}
1039
04b8595b 1040u_int32_t
2d21ac55 1041iptime(void)
1c79356b
A
1042{
1043 struct timeval atv;
b0d623f7 1044 u_int32_t t;
1c79356b 1045
39236c6e 1046 getmicrotime(&atv);
0a7de745
A
1047 t = (atv.tv_sec % (24 * 60 * 60)) * 1000 + atv.tv_usec / 1000;
1048 return htonl(t);
1c79356b
A
1049}
1050
1051#if 1
1052/*
1053 * Return the next larger or smaller MTU plateau (table from RFC 1191)
1054 * given current value MTU. If DIR is less than zero, a larger plateau
1055 * is returned; otherwise, a smaller value is returned.
1056 */
b0d623f7 1057int
2d21ac55 1058ip_next_mtu(int mtu, int dir)
1c79356b
A
1059{
1060 static int mtutab[] = {
1061 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1006, 508, 296,
1062 68, 0
1063 };
1064 int i;
1065
1066 for (i = 0; i < (sizeof mtutab) / (sizeof mtutab[0]); i++) {
0a7de745 1067 if (mtu >= mtutab[i]) {
1c79356b 1068 break;
0a7de745 1069 }
1c79356b
A
1070 }
1071
1072 if (dir < 0) {
1073 if (i == 0) {
1074 return 0;
1075 } else {
1076 return mtutab[i - 1];
1077 }
1078 } else {
1079 if (mtutab[i] == 0) {
1080 return 0;
0a7de745 1081 } else if (mtu > mtutab[i]) {
1c79356b
A
1082 return mtutab[i];
1083 } else {
1084 return mtutab[i + 1];
1085 }
1086 }
1087}
1088#endif
1089
1090#if ICMP_BANDLIM
1091
1092/*
1093 * badport_bandlim() - check for ICMP bandwidth limit
1094 *
1095 * Return 0 if it is ok to send an ICMP error response, -1 if we have
0a7de745 1096 * hit our bandwidth limit and it is not ok.
1c79356b
A
1097 *
1098 * If icmplim is <= 0, the feature is disabled and 0 is returned.
1099 *
1100 * For now we separate the TCP and UDP subsystems w/ different 'which'
1101 * values. We may eventually remove this separation (and simplify the
1102 * code further).
1103 *
1104 * Note that the printing of the error message is delayed so we can
1105 * properly print the icmp error rate that the system was trying to do
1106 * (i.e. 22000/100 pps, etc...). This can cause long delays in printing
0a7de745 1107 * the 'final' error, but it doesn't make sense to solve the printing
1c79356b
A
1108 * delay with more complex code.
1109 */
1110
1111int
1112badport_bandlim(int which)
1113{
39236c6e 1114 static uint64_t lticks[BANDLIM_MAX + 1];
9bccf70c 1115 static int lpackets[BANDLIM_MAX + 1];
39236c6e 1116 uint64_t time = net_uptime();
9bccf70c
A
1117 int secs;
1118
1119 const char *bandlimittype[] = {
1120 "Limiting icmp unreach response",
1121 "Limiting icmp ping response",
1122 "Limiting icmp tstamp response",
1123 "Limiting closed port RST response",
1124 "Limiting open port RST response"
0a7de745 1125 };
1c79356b
A
1126
1127 /*
1128 * Return ok status if feature disabled or argument out of
1129 * ranage.
1130 */
1131
0a7de745
A
1132 if (icmplim <= 0 || which > BANDLIM_MAX || which < 0) {
1133 return 0;
1134 }
1c79356b 1135
39236c6e 1136 secs = time - lticks[which];
9bccf70c 1137
1c79356b 1138 /*
9bccf70c 1139 * reset stats when cumulative delta exceeds one second.
1c79356b
A
1140 */
1141
39236c6e 1142 if (secs > 1) {
1c79356b 1143 if (lpackets[which] > icmplim) {
9bccf70c 1144 printf("%s from %d to %d packets per second\n",
0a7de745
A
1145 bandlimittype[which],
1146 lpackets[which],
1147 icmplim
1148 );
1c79356b 1149 }
39236c6e 1150 lticks[which] = time;
1c79356b
A
1151 lpackets[which] = 0;
1152 }
1153
1154 /*
1155 * bump packet count
1156 */
1157
1158 if (++lpackets[which] > icmplim) {
0a7de745 1159 return -1;
1c79356b 1160 }
0a7de745 1161 return 0;
1c79356b
A
1162}
1163
1164#endif
1165
9bccf70c
A
1166#if __APPLE__
1167
1168/*
1169 * Non-privileged ICMP socket operations
1170 * - send ICMP echo request
1171 * - all ICMP
1172 * - limited socket options
1173 */
1174
1175#include <netinet/ip_icmp.h>
1176#include <netinet/in_pcb.h>
1177
b0d623f7
A
1178extern u_int32_t rip_sendspace;
1179extern u_int32_t rip_recvspace;
9bccf70c
A
1180extern struct inpcbinfo ripcbinfo;
1181
1182int rip_abort(struct socket *);
1183int rip_bind(struct socket *, struct sockaddr *, struct proc *);
1184int rip_connect(struct socket *, struct sockaddr *, struct proc *);
1185int rip_detach(struct socket *);
1186int rip_disconnect(struct socket *);
1187int rip_shutdown(struct socket *);
1188
1189__private_extern__ int icmp_dgram_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, struct mbuf *control, struct proc *p);
1190__private_extern__ int icmp_dgram_attach(struct socket *so, int proto, struct proc *p);
1191__private_extern__ int icmp_dgram_ctloutput(struct socket *so, struct sockopt *sopt);
1192
1193__private_extern__ struct pr_usrreqs icmp_dgram_usrreqs = {
0a7de745
A
1194 .pru_abort = rip_abort,
1195 .pru_attach = icmp_dgram_attach,
1196 .pru_bind = rip_bind,
1197 .pru_connect = rip_connect,
1198 .pru_control = in_control,
1199 .pru_detach = rip_detach,
1200 .pru_disconnect = rip_disconnect,
1201 .pru_peeraddr = in_getpeeraddr,
1202 .pru_send = icmp_dgram_send,
1203 .pru_shutdown = rip_shutdown,
1204 .pru_sockaddr = in_getsockaddr,
1205 .pru_sosend = sosend,
1206 .pru_soreceive = soreceive,
9bccf70c
A
1207};
1208
1209/* Like rip_attach but without root privilege enforcement */
1210__private_extern__ int
2d21ac55 1211icmp_dgram_attach(struct socket *so, __unused int proto, struct proc *p)
9bccf70c 1212{
0a7de745
A
1213 struct inpcb *inp;
1214 int error;
1215
1216 inp = sotoinpcb(so);
1217 if (inp) {
1218 panic("icmp_dgram_attach");
1219 }
1220
1221 error = soreserve(so, rip_sendspace, rip_recvspace);
1222 if (error) {
1223 return error;
1224 }
1225 error = in_pcballoc(so, &ripcbinfo, p);
1226 if (error) {
1227 return error;
1228 }
1229 inp = (struct inpcb *)so->so_pcb;
1230 inp->inp_vflag |= INP_IPV4;
1231 inp->inp_ip_p = IPPROTO_ICMP;
1232 inp->inp_ip_ttl = ip_defttl;
1233 return 0;
9bccf70c
A
1234}
1235
1236/*
1237 * Raw IP socket option processing.
1238 */
1239__private_extern__ int
1240icmp_dgram_ctloutput(struct socket *so, struct sockopt *sopt)
1241{
0a7de745 1242 int error;
9bccf70c 1243
0a7de745
A
1244 if (sopt->sopt_level != IPPROTO_IP) {
1245 return EINVAL;
1246 }
9bccf70c
A
1247
1248 switch (sopt->sopt_name) {
0a7de745
A
1249 case IP_OPTIONS:
1250 case IP_HDRINCL:
1251 case IP_TOS:
1252 case IP_TTL:
1253 case IP_RECVOPTS:
1254 case IP_RECVRETOPTS:
1255 case IP_RECVDSTADDR:
1256 case IP_RETOPTS:
1257 case IP_MULTICAST_IF:
1258 case IP_MULTICAST_IFINDEX:
1259 case IP_MULTICAST_TTL:
1260 case IP_MULTICAST_LOOP:
1261 case IP_ADD_MEMBERSHIP:
1262 case IP_DROP_MEMBERSHIP:
1263 case IP_MULTICAST_VIF:
1264 case IP_PORTRANGE:
1265 case IP_RECVIF:
1266 case IP_IPSEC_POLICY:
1267 case IP_STRIPHDR:
1268 case IP_RECVTTL:
1269 case IP_BOUND_IF:
1270 case IP_NO_IFT_CELLULAR:
1271 error = rip_ctloutput(so, sopt);
1272 break;
b0d623f7 1273
0a7de745
A
1274 default:
1275 error = EINVAL;
1276 break;
9bccf70c
A
1277 }
1278
0a7de745 1279 return error;
9bccf70c
A
1280}
1281
1282__private_extern__ int
39236c6e
A
1283icmp_dgram_send(struct socket *so, int flags, struct mbuf *m,
1284 struct sockaddr *nam, struct mbuf *control, struct proc *p)
9bccf70c
A
1285{
1286 struct ip *ip;
1287 struct inpcb *inp = sotoinpcb(so);
1288 int hlen;
1289 struct icmp *icp;
0a7de745 1290 struct in_ifaddr *ia = NULL;
9bccf70c 1291 int icmplen;
39236c6e 1292 int error = EINVAL;
bca245ac 1293 int inp_flags = inp ? inp->inp_flags : 0;
39236c6e 1294
fe8ab488
A
1295 if (inp == NULL
1296#if NECP
0a7de745 1297 || (necp_socket_should_use_flow_divert(inp))
fe8ab488 1298#endif /* NECP */
0a7de745
A
1299 ) {
1300 if (inp != NULL) {
39236c6e 1301 error = EPROTOTYPE;
0a7de745 1302 }
39236c6e
A
1303 goto bad;
1304 }
9bccf70c 1305
bca245ac
A
1306#if CONTENT_FILTER
1307 /*
1308 * If socket is subject to Content Filter, get inp_flags from saved state
1309 */
1310 if (so->so_cfil_db && nam == NULL) {
1311 cfil_dgram_peek_socket_state(m, &inp_flags);
1312 }
1313#endif
1314
1315 if ((inp_flags & INP_HDRINCL) != 0) {
39236c6e 1316 /* Expect 32-bit aligned data ptr on strict-align platforms */
316670eb 1317 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
9bccf70c 1318 /*
39236c6e
A
1319 * This is not raw IP, we liberal only for fields TOS,
1320 * id and TTL.
9bccf70c
A
1321 */
1322 ip = mtod(m, struct ip *);
1323
1324 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
1325 /* Some sanity checks */
1326 if (m->m_pkthdr.len < hlen + ICMP_MINLEN) {
1327 goto bad;
1328 }
1329 /* Only IPv4 */
0a7de745 1330 if (IP_VHL_V(ip->ip_vhl) != 4) {
9bccf70c 1331 goto bad;
0a7de745
A
1332 }
1333 if (hlen < 20 || hlen > 40 || ip->ip_len != m->m_pkthdr.len) {
9bccf70c 1334 goto bad;
0a7de745 1335 }
39236c6e 1336 /* Bogus fragments can tie up peer resources */
0a7de745 1337 if ((ip->ip_off & ~IP_DF) != 0) {
9bccf70c 1338 goto bad;
0a7de745 1339 }
9bccf70c 1340 /* Allow only ICMP even for user provided IP header */
0a7de745 1341 if (ip->ip_p != IPPROTO_ICMP) {
9bccf70c 1342 goto bad;
0a7de745 1343 }
39236c6e
A
1344 /*
1345 * To prevent spoofing, specified source address must
1346 * be one of ours.
1347 */
9bccf70c 1348 if (ip->ip_src.s_addr != INADDR_ANY) {
91447636 1349 socket_unlock(so, 0);
b0d623f7 1350 lck_rw_lock_shared(in_ifaddr_rwlock);
91447636 1351 if (TAILQ_EMPTY(&in_ifaddrhead)) {
b0d623f7 1352 lck_rw_done(in_ifaddr_rwlock);
91447636 1353 socket_lock(so, 0);
9bccf70c 1354 goto bad;
91447636 1355 }
b0d623f7
A
1356 TAILQ_FOREACH(ia, INADDR_HASH(ip->ip_src.s_addr),
1357 ia_hash) {
6d2010ae 1358 IFA_LOCK(&ia->ia_ifa);
b0d623f7
A
1359 if (IA_SIN(ia)->sin_addr.s_addr ==
1360 ip->ip_src.s_addr) {
6d2010ae 1361 IFA_UNLOCK(&ia->ia_ifa);
b0d623f7 1362 lck_rw_done(in_ifaddr_rwlock);
91447636 1363 socket_lock(so, 0);
9bccf70c 1364 goto ours;
91447636 1365 }
6d2010ae 1366 IFA_UNLOCK(&ia->ia_ifa);
9bccf70c 1367 }
b0d623f7 1368 lck_rw_done(in_ifaddr_rwlock);
91447636 1369 socket_lock(so, 0);
9bccf70c
A
1370 goto bad;
1371 }
1372ours:
1373 /* Do not trust we got a valid checksum */
1374 ip->ip_sum = 0;
316670eb
A
1375
1376 icp = (struct icmp *)(void *)(((char *)m->m_data) + hlen);
9bccf70c
A
1377 icmplen = m->m_pkthdr.len - hlen;
1378 } else {
1379 if ((icmplen = m->m_pkthdr.len) < ICMP_MINLEN) {
1380 goto bad;
1381 }
1382 icp = mtod(m, struct icmp *);
1383 }
1384 /*
1385 * Allow only to send request types with code 0
1386 */
0a7de745 1387 if (icp->icmp_code != 0) {
9bccf70c 1388 goto bad;
0a7de745 1389 }
9bccf70c 1390 switch (icp->icmp_type) {
0a7de745
A
1391 case ICMP_ECHO:
1392 break;
1393 case ICMP_TSTAMP:
1394 if (icmplen != 20) {
1395 goto bad;
1396 }
1397 break;
1398 case ICMP_MASKREQ:
1399 if (icmplen != 12) {
9bccf70c 1400 goto bad;
0a7de745
A
1401 }
1402 break;
1403 default:
1404 goto bad;
9bccf70c 1405 }
0a7de745 1406 return rip_send(so, flags, m, nam, control, p);
9bccf70c 1407bad:
39236c6e
A
1408 VERIFY(error != 0);
1409
0a7de745 1410 if (m != NULL) {
39236c6e 1411 m_freem(m);
0a7de745
A
1412 }
1413 if (control != NULL) {
39236c6e 1414 m_freem(control);
0a7de745 1415 }
39236c6e 1416
0a7de745 1417 return error;
9bccf70c
A
1418}
1419
1420#endif /* __APPLE__ */