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