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