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