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