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