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