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