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