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