]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
a39ff7e2 | 2 | * Copyright (c) 2000-2018 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1982, 1986, 1988, 1993 | |
30 | * The Regents of the University of California. All rights reserved. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 | |
61 | */ | |
2d21ac55 A |
62 | /* |
63 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
64 | * support for mandatory and extensible security protections. This notice | |
65 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
66 | * Version 2.0. | |
67 | */ | |
1c79356b A |
68 | |
69 | #include <sys/param.h> | |
70 | #include <sys/systm.h> | |
71 | #include <sys/mbuf.h> | |
316670eb | 72 | #include <sys/mcache.h> |
1c79356b A |
73 | #include <sys/protosw.h> |
74 | #include <sys/socket.h> | |
75 | #include <sys/time.h> | |
76 | #include <sys/kernel.h> | |
77 | #include <sys/sysctl.h> | |
78 | ||
b0d623f7 A |
79 | #include <machine/endian.h> |
80 | ||
1c79356b A |
81 | #include <net/if.h> |
82 | #include <net/route.h> | |
83 | ||
84 | #define _IP_VHL | |
85 | #include <netinet/in.h> | |
86 | #include <netinet/in_systm.h> | |
87 | #include <netinet/in_var.h> | |
88 | #include <netinet/ip.h> | |
89 | #include <netinet/ip_icmp.h> | |
90 | #include <netinet/ip_var.h> | |
91 | #include <netinet/icmp_var.h> | |
e5568f75 A |
92 | #include <netinet/tcp.h> |
93 | #include <netinet/tcp_fsm.h> | |
94 | #include <netinet/tcp_seq.h> | |
95 | #include <netinet/tcp_timer.h> | |
96 | #include <netinet/tcp_var.h> | |
97 | #include <netinet/tcpip.h> | |
1c79356b A |
98 | |
99 | #if IPSEC | |
100 | #include <netinet6/ipsec.h> | |
101 | #include <netkey/key.h> | |
13fec989 A |
102 | #endif |
103 | ||
fe8ab488 A |
104 | #if NECP |
105 | #include <net/necp.h> | |
106 | #endif /* NECP */ | |
107 | ||
0a7de745 | 108 | /* XXX This one should go in sys/mbuf.h. It is used to avoid that |
13fec989 A |
109 | * a firewall-generated packet loops forever through the firewall. |
110 | */ | |
111 | #ifndef M_SKIP_FIREWALL | |
112 | #define M_SKIP_FIREWALL 0x4000 | |
0a7de745 | 113 | #endif |
2d21ac55 A |
114 | |
115 | #if CONFIG_MACF_NET | |
116 | #include <security/mac_framework.h> | |
117 | #endif /* MAC_NET */ | |
1c79356b | 118 | |
b0d623f7 | 119 | |
1c79356b A |
120 | /* |
121 | * ICMP routines: error generation, receive packet processing, and | |
122 | * routines to turnaround packets back to the originator, and | |
123 | * host table maintenance routines. | |
124 | */ | |
125 | ||
0a7de745 | 126 | struct icmpstat icmpstat; |
04b8595b A |
127 | SYSCTL_STRUCT(_net_inet_icmp, ICMPCTL_STATS, stats, |
128 | CTLFLAG_RD | CTLFLAG_LOCKED, | |
129 | &icmpstat, icmpstat, ""); | |
1c79356b | 130 | |
0a7de745 | 131 | static int icmpmaskrepl = 0; |
04b8595b A |
132 | SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, |
133 | CTLFLAG_RW | CTLFLAG_LOCKED, | |
134 | &icmpmaskrepl, 0, ""); | |
1c79356b | 135 | |
0a7de745 | 136 | static int icmptimestamp = 0; |
04b8595b A |
137 | SYSCTL_INT(_net_inet_icmp, ICMPCTL_TIMESTAMP, timestamp, |
138 | CTLFLAG_RW | CTLFLAG_LOCKED, | |
139 | &icmptimestamp, 0, ""); | |
55e303ae | 140 | |
0a7de745 | 141 | static int drop_redirect = 1; |
04b8595b A |
142 | SYSCTL_INT(_net_inet_icmp, OID_AUTO, drop_redirect, |
143 | CTLFLAG_RW | CTLFLAG_LOCKED, | |
144 | &drop_redirect, 0, ""); | |
9bccf70c | 145 | |
0a7de745 | 146 | static int log_redirect = 0; |
04b8595b A |
147 | SYSCTL_INT(_net_inet_icmp, OID_AUTO, log_redirect, |
148 | CTLFLAG_RW | CTLFLAG_LOCKED, | |
149 | &log_redirect, 0, ""); | |
150 | ||
3e170ce0 | 151 | const static int icmp_datalen = 8; |
9bccf70c | 152 | |
0a7de745 | 153 | #if ICMP_BANDLIM |
316670eb A |
154 | |
155 | /* Default values in case CONFIG_ICMP_BANDLIM is not defined in the MASTER file */ | |
156 | #ifndef CONFIG_ICMP_BANDLIM | |
5ba3f43e | 157 | #if !CONFIG_EMBEDDED |
316670eb | 158 | #define CONFIG_ICMP_BANDLIM 250 |
5ba3f43e A |
159 | #else /* CONFIG_EMBEDDED */ |
160 | #define CONFIG_ICMP_BANDLIM 50 | |
161 | #endif /* CONFIG_EMBEDDED */ | |
316670eb A |
162 | #endif /* CONFIG_ICMP_BANDLIM */ |
163 | ||
0a7de745 | 164 | /* |
1c79356b A |
165 | * ICMP error-response bandwidth limiting sysctl. If not enabled, sysctl |
166 | * variable content is -1 and read-only. | |
0a7de745 A |
167 | */ |
168 | ||
316670eb | 169 | static int icmplim = CONFIG_ICMP_BANDLIM; |
6d2010ae | 170 | SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RW | CTLFLAG_LOCKED, |
0a7de745 | 171 | &icmplim, 0, ""); |
316670eb A |
172 | |
173 | #else /* ICMP_BANDLIM */ | |
1c79356b A |
174 | |
175 | static int icmplim = -1; | |
6d2010ae | 176 | SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RD | CTLFLAG_LOCKED, |
0a7de745 A |
177 | &icmplim, 0, ""); |
178 | ||
316670eb | 179 | #endif /* ICMP_BANDLIM */ |
1c79356b A |
180 | |
181 | /* | |
182 | * ICMP broadcast echo sysctl | |
183 | */ | |
184 | ||
0a7de745 | 185 | static int icmpbmcastecho = 1; |
6d2010ae | 186 | SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_RW | CTLFLAG_LOCKED, |
0a7de745 | 187 | &icmpbmcastecho, 0, ""); |
1c79356b | 188 | |
39037602 | 189 | #if (DEBUG | DEVELOPMENT) |
0a7de745 | 190 | static int icmpprintfs = 0; |
39037602 | 191 | SYSCTL_INT(_net_inet_icmp, OID_AUTO, verbose, CTLFLAG_RW | CTLFLAG_LOCKED, |
0a7de745 | 192 | &icmpprintfs, 0, ""); |
1c79356b A |
193 | #endif |
194 | ||
0a7de745 A |
195 | static void icmp_reflect(struct mbuf *); |
196 | static void icmp_send(struct mbuf *, struct mbuf *); | |
1c79356b | 197 | |
1c79356b A |
198 | /* |
199 | * Generate an error packet of type error | |
200 | * in response to bad packet ip. | |
201 | */ | |
202 | void | |
91447636 A |
203 | icmp_error( |
204 | struct mbuf *n, | |
205 | int type, | |
206 | int code, | |
04b8595b | 207 | u_int32_t dest, |
b0d623f7 | 208 | u_int32_t nextmtu) |
1c79356b | 209 | { |
d9a64523 A |
210 | struct ip *oip = NULL; |
211 | struct ip *nip = NULL; | |
212 | struct icmp *icp = NULL; | |
213 | struct mbuf *m = NULL; | |
214 | u_int32_t oiphlen = 0; | |
215 | u_int32_t icmplen = 0; | |
216 | u_int32_t icmpelen = 0; | |
217 | u_int32_t nlen = 0; | |
218 | ||
219 | VERIFY((u_int)type <= ICMP_MAXTYPE); | |
316670eb A |
220 | /* Expect 32-bit aligned data pointer on strict-align platforms */ |
221 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(n); | |
222 | ||
0a7de745 | 223 | if (type != ICMP_REDIRECT) { |
d9a64523 | 224 | icmpstat.icps_error++; |
0a7de745 | 225 | } |
d9a64523 A |
226 | /* |
227 | * Don't send error: | |
228 | * if not the first fragment of message | |
229 | * if original packet was a multicast or broadcast packet | |
230 | * if the old packet protocol was ICMP | |
231 | * error message, only known informational types. | |
232 | */ | |
0a7de745 | 233 | if (n->m_flags & (M_BCAST | M_MCAST)) { |
d9a64523 | 234 | goto freeit; |
0a7de745 | 235 | } |
d9a64523 A |
236 | |
237 | /* | |
238 | * Drop if IP header plus ICMP_MINLEN bytes are not contiguous | |
239 | * in first mbuf. | |
240 | */ | |
0a7de745 | 241 | if (n->m_len < sizeof(struct ip) + ICMP_MINLEN) { |
d9a64523 | 242 | goto freeit; |
0a7de745 | 243 | } |
d9a64523 | 244 | |
04b8595b A |
245 | oip = mtod(n, struct ip *); |
246 | oiphlen = IP_VHL_HL(oip->ip_vhl) << 2; | |
0a7de745 | 247 | if (n->m_len < oiphlen + ICMP_MINLEN) { |
d9a64523 | 248 | goto freeit; |
0a7de745 | 249 | } |
316670eb | 250 | |
39037602 | 251 | #if (DEBUG | DEVELOPMENT) |
0a7de745 | 252 | if (icmpprintfs > 1) { |
39236c6e A |
253 | printf("icmp_error(0x%llx, %x, %d)\n", |
254 | (uint64_t)VM_KERNEL_ADDRPERM(oip), type, code); | |
0a7de745 | 255 | } |
1c79356b | 256 | #endif |
d9a64523 | 257 | |
0a7de745 | 258 | if (oip->ip_off & ~(IP_MF | IP_DF)) { |
1c79356b | 259 | goto freeit; |
0a7de745 | 260 | } |
04b8595b | 261 | |
1c79356b | 262 | if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT && |
d9a64523 A |
263 | n->m_len >= oiphlen + ICMP_MINLEN && |
264 | !ICMP_INFOTYPE(((struct icmp *)(void *)((caddr_t)oip + oiphlen))-> | |
0a7de745 | 265 | icmp_type)) { |
1c79356b A |
266 | icmpstat.icps_oldicmp++; |
267 | goto freeit; | |
268 | } | |
04b8595b A |
269 | |
270 | /* | |
271 | * Calculate the length to quote from original packet and prevent | |
272 | * the ICMP mbuf from overflowing. | |
d9a64523 A |
273 | * Unfortunatly this is non-trivial since ip_forward() |
274 | * sends us truncated packets. | |
04b8595b A |
275 | */ |
276 | nlen = m_length(n); | |
277 | if (oip->ip_p == IPPROTO_TCP) { | |
d9a64523 A |
278 | struct tcphdr *th = NULL; |
279 | u_int16_t tcphlen = 0; | |
04b8595b | 280 | |
d9a64523 A |
281 | /* |
282 | * If the packet got truncated and TCP header | |
283 | * is not contained in the packet, send out | |
284 | * standard reply with only IP header as payload | |
285 | */ | |
04b8595b | 286 | if (oiphlen + sizeof(struct tcphdr) > n->m_len && |
0a7de745 | 287 | n->m_next == NULL) { |
04b8595b | 288 | goto stdreply; |
0a7de745 | 289 | } |
d9a64523 A |
290 | |
291 | /* | |
292 | * Otherwise, pull up to get IP and TCP headers | |
293 | * together | |
294 | */ | |
04b8595b | 295 | if (n->m_len < (oiphlen + sizeof(struct tcphdr)) && |
0a7de745 | 296 | (n = m_pullup(n, (oiphlen + sizeof(struct tcphdr)))) == NULL) { |
04b8595b | 297 | goto freeit; |
0a7de745 | 298 | } |
04b8595b | 299 | |
a39ff7e2 A |
300 | /* |
301 | * Reinit pointers derived from mbuf data pointer | |
302 | * as things might have moved around with m_pullup | |
303 | */ | |
304 | oip = mtod(n, struct ip *); | |
04b8595b | 305 | th = (struct tcphdr *)(void *)((caddr_t)oip + oiphlen); |
a39ff7e2 | 306 | |
04b8595b | 307 | if (th != ((struct tcphdr *)P2ROUNDDOWN(th, |
0a7de745 | 308 | sizeof(u_int32_t)))) { |
04b8595b | 309 | goto freeit; |
0a7de745 | 310 | } |
04b8595b | 311 | tcphlen = th->th_off << 2; |
d9a64523 A |
312 | |
313 | /* Sanity checks */ | |
0a7de745 | 314 | if (tcphlen < sizeof(struct tcphdr)) { |
04b8595b | 315 | goto freeit; |
0a7de745 A |
316 | } |
317 | if (oip->ip_len < (oiphlen + tcphlen)) { | |
04b8595b | 318 | goto freeit; |
0a7de745 A |
319 | } |
320 | if ((oiphlen + tcphlen) > n->m_len && n->m_next == NULL) { | |
04b8595b | 321 | goto stdreply; |
0a7de745 | 322 | } |
04b8595b | 323 | if (n->m_len < (oiphlen + tcphlen) && |
0a7de745 | 324 | (n = m_pullup(n, (oiphlen + tcphlen))) == NULL) { |
04b8595b | 325 | goto freeit; |
0a7de745 | 326 | } |
04b8595b | 327 | |
a39ff7e2 A |
328 | /* |
329 | * Reinit pointers derived from mbuf data pointer | |
330 | * as things might have moved around with m_pullup | |
331 | */ | |
332 | oip = mtod(n, struct ip *); | |
333 | th = (struct tcphdr *)(void *)((caddr_t)oip + oiphlen); | |
334 | ||
04b8595b A |
335 | icmpelen = max(tcphlen, min(icmp_datalen, |
336 | (oip->ip_len - oiphlen))); | |
0a7de745 A |
337 | } else { |
338 | stdreply: icmpelen = max(ICMP_MINLEN, min(icmp_datalen, | |
39037602 | 339 | (oip->ip_len - oiphlen))); |
0a7de745 | 340 | } |
04b8595b | 341 | |
d9a64523 | 342 | icmplen = min(oiphlen + icmpelen, nlen); |
0a7de745 | 343 | if (icmplen < sizeof(struct ip)) { |
04b8595b | 344 | goto freeit; |
0a7de745 | 345 | } |
d9a64523 | 346 | |
1c79356b A |
347 | /* |
348 | * First, formulate icmp message | |
d9a64523 A |
349 | * Allocate enough space for the IP header, ICMP header |
350 | * and the payload (part of the original message to be sent back). | |
1c79356b | 351 | */ |
0a7de745 A |
352 | if (MHLEN > (sizeof(struct ip) + ICMP_MINLEN + icmplen)) { |
353 | m = m_gethdr(M_DONTWAIT, MT_HEADER); /* MAC-OK */ | |
354 | } else { | |
04b8595b | 355 | m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); |
0a7de745 | 356 | } |
04b8595b | 357 | |
0a7de745 | 358 | if (m == NULL) { |
1c79356b | 359 | goto freeit; |
0a7de745 | 360 | } |
13fec989 | 361 | |
2d21ac55 A |
362 | #if CONFIG_MACF_NET |
363 | mac_mbuf_label_associate_netlayer(n, m); | |
364 | #endif | |
d9a64523 A |
365 | /* |
366 | * Further refine the payload length to the space | |
367 | * remaining in mbuf after including the IP header and ICMP | |
368 | * header. | |
369 | */ | |
370 | icmplen = min(icmplen, M_TRAILINGSPACE(m) - | |
371 | sizeof(struct ip) - ICMP_MINLEN); | |
372 | m_align(m, ICMP_MINLEN + icmplen); | |
373 | m->m_len = ICMP_MINLEN + icmplen; /* for ICMP header and data */ | |
374 | ||
1c79356b | 375 | icp = mtod(m, struct icmp *); |
1c79356b A |
376 | icmpstat.icps_outhist[type]++; |
377 | icp->icmp_type = type; | |
0a7de745 | 378 | if (type == ICMP_REDIRECT) { |
1c79356b | 379 | icp->icmp_gwaddr.s_addr = dest; |
0a7de745 | 380 | } else { |
1c79356b A |
381 | icp->icmp_void = 0; |
382 | /* | |
383 | * The following assignments assume an overlay with the | |
384 | * zeroed icmp_void field. | |
385 | */ | |
386 | if (type == ICMP_PARAMPROB) { | |
387 | icp->icmp_pptr = code; | |
388 | code = 0; | |
389 | } else if (type == ICMP_UNREACH && | |
b0d623f7 A |
390 | code == ICMP_UNREACH_NEEDFRAG && nextmtu != 0) { |
391 | icp->icmp_nextmtu = htons(nextmtu); | |
1c79356b A |
392 | } |
393 | } | |
394 | ||
395 | icp->icmp_code = code; | |
d9a64523 A |
396 | |
397 | /* | |
398 | * Copy icmplen worth of content from original | |
399 | * mbuf (n) to the new packet after ICMP header. | |
400 | */ | |
9bccf70c | 401 | m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip); |
1c79356b | 402 | nip = &icp->icmp_ip; |
9bccf70c A |
403 | |
404 | /* | |
405 | * Convert fields to network representation. | |
406 | */ | |
b0d623f7 | 407 | #if BYTE_ORDER != BIG_ENDIAN |
9bccf70c A |
408 | HTONS(nip->ip_len); |
409 | HTONS(nip->ip_off); | |
b0d623f7 | 410 | #endif |
1c79356b | 411 | /* |
d9a64523 A |
412 | * Set up ICMP message mbuf and copy old IP header (without options |
413 | * in front of ICMP message. | |
414 | * If the original mbuf was meant to bypass the firewall, the error | |
415 | * reply should bypass as well. | |
0a7de745 | 416 | */ |
d9a64523 | 417 | m->m_flags |= n->m_flags & M_SKIP_FIREWALL; |
1c79356b A |
418 | m->m_data -= sizeof(struct ip); |
419 | m->m_len += sizeof(struct ip); | |
420 | m->m_pkthdr.len = m->m_len; | |
421 | m->m_pkthdr.rcvif = n->m_pkthdr.rcvif; | |
1c79356b A |
422 | nip = mtod(m, struct ip *); |
423 | bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip)); | |
424 | nip->ip_len = m->m_len; | |
425 | nip->ip_vhl = IP_VHL_BORING; | |
426 | nip->ip_p = IPPROTO_ICMP; | |
427 | nip->ip_tos = 0; | |
04b8595b | 428 | nip->ip_off = 0; |
1c79356b | 429 | icmp_reflect(m); |
1c79356b A |
430 | freeit: |
431 | m_freem(n); | |
432 | } | |
433 | ||
1c79356b A |
434 | /* |
435 | * Process a received ICMP message. | |
436 | */ | |
437 | void | |
2d21ac55 | 438 | icmp_input(struct mbuf *m, int hlen) |
1c79356b | 439 | { |
39236c6e | 440 | struct sockaddr_in icmpsrc, icmpdst, icmpgw; |
2d21ac55 A |
441 | struct icmp *icp; |
442 | struct ip *ip = mtod(m, struct ip *); | |
316670eb | 443 | int icmplen; |
2d21ac55 | 444 | int i; |
1c79356b | 445 | struct in_ifaddr *ia; |
5ba3f43e | 446 | void (*ctlfunc)(int, struct sockaddr *, void *, struct ifnet *); |
1c79356b | 447 | int code; |
cb323159 | 448 | boolean_t should_log_redirect = false; |
1c79356b | 449 | |
316670eb A |
450 | /* Expect 32-bit aligned data pointer on strict-align platforms */ |
451 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); | |
452 | ||
453 | icmplen = ip->ip_len; | |
454 | ||
1c79356b A |
455 | /* |
456 | * Locate icmp structure in mbuf, and check | |
457 | * that not corrupted and of at least minimum length. | |
458 | */ | |
39037602 | 459 | #if (DEBUG | DEVELOPMENT) |
0a7de745 | 460 | if (icmpprintfs > 2) { |
39037602 A |
461 | char src_str[MAX_IPv4_STR_LEN]; |
462 | char dst_str[MAX_IPv4_STR_LEN]; | |
463 | ||
464 | inet_ntop(AF_INET, &ip->ip_src, src_str, sizeof(src_str)); | |
465 | inet_ntop(AF_INET, &ip->ip_dst, dst_str, sizeof(dst_str)); | |
466 | printf("%s: from %s to %s, len %d\n", | |
467 | __func__, src_str, dst_str, icmplen); | |
1c79356b A |
468 | } |
469 | #endif | |
470 | if (icmplen < ICMP_MINLEN) { | |
471 | icmpstat.icps_tooshort++; | |
472 | goto freeit; | |
473 | } | |
474 | i = hlen + min(icmplen, ICMP_ADVLENMIN); | |
0a7de745 | 475 | if (m->m_len < i && (m = m_pullup(m, i)) == 0) { |
1c79356b A |
476 | icmpstat.icps_tooshort++; |
477 | return; | |
478 | } | |
479 | ip = mtod(m, struct ip *); | |
480 | m->m_len -= hlen; | |
481 | m->m_data += hlen; | |
482 | icp = mtod(m, struct icmp *); | |
483 | if (in_cksum(m, icmplen)) { | |
484 | icmpstat.icps_checksum++; | |
485 | goto freeit; | |
486 | } | |
487 | m->m_len += hlen; | |
488 | m->m_data -= hlen; | |
489 | ||
39037602 | 490 | #if (DEBUG | DEVELOPMENT) |
0a7de745 | 491 | if (icmpprintfs > 2) { |
1c79356b A |
492 | printf("icmp_input, type %d code %d\n", icp->icmp_type, |
493 | icp->icmp_code); | |
0a7de745 | 494 | } |
1c79356b A |
495 | #endif |
496 | ||
1c79356b A |
497 | /* |
498 | * Message type specific processing. | |
499 | */ | |
0a7de745 | 500 | if (icp->icmp_type > ICMP_MAXTYPE) { |
1c79356b | 501 | goto raw; |
0a7de745 | 502 | } |
39236c6e A |
503 | |
504 | /* Initialize */ | |
0a7de745 A |
505 | bzero(&icmpsrc, sizeof(icmpsrc)); |
506 | icmpsrc.sin_len = sizeof(struct sockaddr_in); | |
39236c6e | 507 | icmpsrc.sin_family = AF_INET; |
0a7de745 A |
508 | bzero(&icmpdst, sizeof(icmpdst)); |
509 | icmpdst.sin_len = sizeof(struct sockaddr_in); | |
39236c6e | 510 | icmpdst.sin_family = AF_INET; |
0a7de745 A |
511 | bzero(&icmpgw, sizeof(icmpgw)); |
512 | icmpgw.sin_len = sizeof(struct sockaddr_in); | |
39236c6e A |
513 | icmpgw.sin_family = AF_INET; |
514 | ||
1c79356b A |
515 | icmpstat.icps_inhist[icp->icmp_type]++; |
516 | code = icp->icmp_code; | |
517 | switch (icp->icmp_type) { | |
1c79356b A |
518 | case ICMP_UNREACH: |
519 | switch (code) { | |
0a7de745 A |
520 | case ICMP_UNREACH_NET: |
521 | case ICMP_UNREACH_HOST: | |
522 | case ICMP_UNREACH_SRCFAIL: | |
523 | case ICMP_UNREACH_NET_UNKNOWN: | |
524 | case ICMP_UNREACH_HOST_UNKNOWN: | |
525 | case ICMP_UNREACH_ISOLATED: | |
526 | case ICMP_UNREACH_TOSNET: | |
527 | case ICMP_UNREACH_TOSHOST: | |
528 | case ICMP_UNREACH_HOST_PRECEDENCE: | |
529 | case ICMP_UNREACH_PRECEDENCE_CUTOFF: | |
530 | code = PRC_UNREACH_NET; | |
531 | break; | |
1c79356b | 532 | |
0a7de745 A |
533 | case ICMP_UNREACH_NEEDFRAG: |
534 | code = PRC_MSGSIZE; | |
535 | break; | |
1c79356b | 536 | |
0a7de745 A |
537 | /* |
538 | * RFC 1122, Sections 3.2.2.1 and 4.2.3.9. | |
539 | * Treat subcodes 2,3 as immediate RST | |
540 | */ | |
541 | case ICMP_UNREACH_PROTOCOL: | |
542 | case ICMP_UNREACH_PORT: | |
543 | code = PRC_UNREACH_PORT; | |
544 | break; | |
1c79356b | 545 | |
0a7de745 A |
546 | case ICMP_UNREACH_NET_PROHIB: |
547 | case ICMP_UNREACH_HOST_PROHIB: | |
548 | case ICMP_UNREACH_FILTER_PROHIB: | |
549 | code = PRC_UNREACH_ADMIN_PROHIB; | |
550 | break; | |
1c79356b | 551 | |
0a7de745 A |
552 | default: |
553 | goto badcode; | |
1c79356b A |
554 | } |
555 | goto deliver; | |
556 | ||
557 | case ICMP_TIMXCEED: | |
0a7de745 | 558 | if (code > 1) { |
1c79356b | 559 | goto badcode; |
0a7de745 | 560 | } |
1c79356b A |
561 | code += PRC_TIMXCEED_INTRANS; |
562 | goto deliver; | |
563 | ||
564 | case ICMP_PARAMPROB: | |
0a7de745 | 565 | if (code > 1) { |
1c79356b | 566 | goto badcode; |
0a7de745 | 567 | } |
1c79356b A |
568 | code = PRC_PARAMPROB; |
569 | goto deliver; | |
570 | ||
571 | case ICMP_SOURCEQUENCH: | |
0a7de745 | 572 | if (code) { |
1c79356b | 573 | goto badcode; |
0a7de745 | 574 | } |
1c79356b | 575 | code = PRC_QUENCH; |
0a7de745 | 576 | deliver: |
1c79356b A |
577 | /* |
578 | * Problem with datagram; advise higher level routines. | |
579 | */ | |
39037602 A |
580 | if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) |
581 | || IP_VHL_HL(icp->icmp_ip.ip_vhl) < | |
cb323159 A |
582 | (sizeof(struct ip) >> 2) || |
583 | (m = m_pullup(m, hlen + ICMP_ADVLEN(icp))) == NULL) { | |
1c79356b A |
584 | icmpstat.icps_badlen++; |
585 | goto freeit; | |
586 | } | |
b0d623f7 | 587 | |
cb323159 A |
588 | ip = mtod(m, struct ip *); |
589 | icp = (struct icmp *)(void *)(mtod(m, uint8_t *) + hlen); | |
590 | ||
b0d623f7 | 591 | #if BYTE_ORDER != BIG_ENDIAN |
1c79356b | 592 | NTOHS(icp->icmp_ip.ip_len); |
b0d623f7 A |
593 | #endif |
594 | ||
1c79356b | 595 | /* Discard ICMP's in response to multicast packets */ |
0a7de745 | 596 | if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr))) { |
1c79356b | 597 | goto badcode; |
0a7de745 | 598 | } |
39037602 | 599 | #if (DEBUG | DEVELOPMENT) |
0a7de745 | 600 | if (icmpprintfs > 2) { |
39037602 A |
601 | printf("deliver to protocol %d\n", |
602 | icp->icmp_ip.ip_p); | |
0a7de745 | 603 | } |
1c79356b A |
604 | #endif |
605 | icmpsrc.sin_addr = icp->icmp_ip.ip_dst; | |
1c79356b | 606 | |
1c79356b | 607 | /* |
39037602 | 608 | * if the packet contains [IPv4 AH TCP], we can't make a |
1c79356b A |
609 | * notification to TCP layer. |
610 | */ | |
611 | ctlfunc = ip_protox[icp->icmp_ip.ip_p]->pr_ctlinput; | |
5c9f4661 A |
612 | |
613 | if (ctlfunc) { | |
614 | LCK_MTX_ASSERT(inet_domain_mutex, LCK_MTX_ASSERT_OWNED); | |
615 | ||
616 | lck_mtx_unlock(inet_domain_mutex); | |
617 | ||
1c79356b | 618 | (*ctlfunc)(code, (struct sockaddr *)&icmpsrc, |
0a7de745 | 619 | (void *)&icp->icmp_ip, m->m_pkthdr.rcvif); |
5c9f4661 A |
620 | |
621 | lck_mtx_lock(inet_domain_mutex); | |
622 | } | |
1c79356b A |
623 | break; |
624 | ||
0a7de745 | 625 | badcode: |
1c79356b A |
626 | icmpstat.icps_badcode++; |
627 | break; | |
628 | ||
629 | case ICMP_ECHO: | |
0a7de745 | 630 | if ((m->m_flags & (M_MCAST | M_BCAST))) { |
39037602 A |
631 | if (icmpbmcastecho == 0) { |
632 | icmpstat.icps_bmcastecho++; | |
633 | break; | |
634 | } | |
635 | } | |
0a7de745 | 636 | |
39037602 A |
637 | /* |
638 | * rdar://18644769 | |
639 | * Do not reply when the destination is link local multicast or broadcast | |
640 | * and the source is not from a directly connected subnet | |
641 | */ | |
642 | if ((IN_LOCAL_GROUP(ntohl(ip->ip_dst.s_addr)) || | |
643 | in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) && | |
644 | in_localaddr(ip->ip_src) == 0) { | |
1c79356b | 645 | icmpstat.icps_bmcastecho++; |
39037602 A |
646 | #if (DEBUG | DEVELOPMENT) |
647 | if (icmpprintfs > 0) { | |
648 | char src_str[MAX_IPv4_STR_LEN]; | |
649 | char dst_str[MAX_IPv4_STR_LEN]; | |
650 | ||
651 | inet_ntop(AF_INET, &ip->ip_src, src_str, sizeof(src_str)); | |
652 | inet_ntop(AF_INET, &ip->ip_dst, dst_str, sizeof(dst_str)); | |
653 | printf("%s: non local (B|M)CAST %s to %s, len %d\n", | |
654 | __func__, src_str, dst_str, icmplen); | |
655 | } | |
656 | #endif | |
1c79356b A |
657 | break; |
658 | } | |
39037602 | 659 | |
1c79356b | 660 | icp->icmp_type = ICMP_ECHOREPLY; |
9bccf70c | 661 | #if ICMP_BANDLIM |
0a7de745 | 662 | if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0) { |
9bccf70c | 663 | goto freeit; |
0a7de745 | 664 | } else |
9bccf70c | 665 | #endif |
0a7de745 | 666 | goto reflect; |
1c79356b A |
667 | |
668 | case ICMP_TSTAMP: | |
0a7de745 | 669 | if (icmptimestamp == 0) { |
55e303ae | 670 | break; |
0a7de745 | 671 | } |
55e303ae | 672 | |
1c79356b A |
673 | if (!icmpbmcastecho |
674 | && (m->m_flags & (M_MCAST | M_BCAST)) != 0) { | |
675 | icmpstat.icps_bmcasttstamp++; | |
676 | break; | |
677 | } | |
678 | if (icmplen < ICMP_TSLEN) { | |
679 | icmpstat.icps_badlen++; | |
680 | break; | |
681 | } | |
682 | icp->icmp_type = ICMP_TSTAMPREPLY; | |
683 | icp->icmp_rtime = iptime(); | |
0a7de745 | 684 | icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */ |
9bccf70c | 685 | #if ICMP_BANDLIM |
0a7de745 | 686 | if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0) { |
9bccf70c | 687 | goto freeit; |
0a7de745 | 688 | } else |
9bccf70c | 689 | #endif |
0a7de745 | 690 | goto reflect; |
1c79356b A |
691 | |
692 | case ICMP_MASKREQ: | |
0a7de745 | 693 | if (icmpmaskrepl == 0) { |
1c79356b | 694 | break; |
0a7de745 | 695 | } |
1c79356b A |
696 | /* |
697 | * We are not able to respond with all ones broadcast | |
698 | * unless we receive it over a point-to-point interface. | |
699 | */ | |
0a7de745 | 700 | if (icmplen < ICMP_MASKLEN) { |
1c79356b | 701 | break; |
0a7de745 | 702 | } |
1c79356b | 703 | switch (ip->ip_dst.s_addr) { |
1c79356b A |
704 | case INADDR_BROADCAST: |
705 | case INADDR_ANY: | |
706 | icmpdst.sin_addr = ip->ip_src; | |
707 | break; | |
708 | ||
709 | default: | |
710 | icmpdst.sin_addr = ip->ip_dst; | |
711 | } | |
712 | ia = (struct in_ifaddr *)ifaof_ifpforaddr( | |
0a7de745 A |
713 | (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif); |
714 | if (ia == 0) { | |
1c79356b | 715 | break; |
0a7de745 | 716 | } |
6d2010ae | 717 | IFA_LOCK(&ia->ia_ifa); |
91447636 | 718 | if (ia->ia_ifp == 0) { |
6d2010ae A |
719 | IFA_UNLOCK(&ia->ia_ifa); |
720 | IFA_REMREF(&ia->ia_ifa); | |
2d21ac55 | 721 | ia = NULL; |
1c79356b | 722 | break; |
91447636 | 723 | } |
1c79356b A |
724 | icp->icmp_type = ICMP_MASKREPLY; |
725 | icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr; | |
726 | if (ip->ip_src.s_addr == 0) { | |
0a7de745 A |
727 | if (ia->ia_ifp->if_flags & IFF_BROADCAST) { |
728 | ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr; | |
729 | } else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) { | |
730 | ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr; | |
731 | } | |
1c79356b | 732 | } |
6d2010ae A |
733 | IFA_UNLOCK(&ia->ia_ifa); |
734 | IFA_REMREF(&ia->ia_ifa); | |
1c79356b | 735 | reflect: |
0a7de745 | 736 | ip->ip_len += hlen; /* since ip_input deducts this */ |
1c79356b A |
737 | icmpstat.icps_reflect++; |
738 | icmpstat.icps_outhist[icp->icmp_type]++; | |
739 | icmp_reflect(m); | |
740 | return; | |
741 | ||
742 | case ICMP_REDIRECT: | |
0a7de745 | 743 | if (drop_redirect) { |
9bccf70c | 744 | break; |
0a7de745 A |
745 | } |
746 | if (code > 3) { | |
1c79356b | 747 | goto badcode; |
0a7de745 | 748 | } |
1c79356b A |
749 | if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || |
750 | IP_VHL_HL(icp->icmp_ip.ip_vhl) < (sizeof(struct ip) >> 2)) { | |
751 | icmpstat.icps_badlen++; | |
752 | break; | |
753 | } | |
cb323159 A |
754 | |
755 | #if (DEBUG | DEVELOPMENT) | |
756 | should_log_redirect = log_redirect || (icmpprintfs > 0); | |
757 | #else | |
758 | should_log_redirect = log_redirect; | |
759 | #endif | |
1c79356b A |
760 | /* |
761 | * Short circuit routing redirects to force | |
762 | * immediate change in the kernel's routing | |
763 | * tables. The message is also handed to anyone | |
764 | * listening on a raw socket (e.g. the routing | |
765 | * daemon for use in updating its tables). | |
766 | */ | |
767 | icmpgw.sin_addr = ip->ip_src; | |
768 | icmpdst.sin_addr = icp->icmp_gwaddr; | |
cb323159 A |
769 | |
770 | if (should_log_redirect) { | |
771 | char src_str[MAX_IPv4_STR_LEN]; | |
39037602 A |
772 | char dst_str[MAX_IPv4_STR_LEN]; |
773 | char gw_str[MAX_IPv4_STR_LEN]; | |
774 | ||
cb323159 | 775 | inet_ntop(AF_INET, &ip->ip_src, src_str, sizeof(src_str)); |
39037602 A |
776 | inet_ntop(AF_INET, &icp->icmp_ip.ip_dst, dst_str, sizeof(dst_str)); |
777 | inet_ntop(AF_INET, &icp->icmp_gwaddr, gw_str, sizeof(gw_str)); | |
cb323159 A |
778 | printf("%s: redirect dst %s to %s from %s\n", __func__, |
779 | dst_str, gw_str, src_str); | |
1c79356b | 780 | } |
1c79356b | 781 | icmpsrc.sin_addr = icp->icmp_ip.ip_dst; |
c910b4d9 | 782 | rtredirect(m->m_pkthdr.rcvif, (struct sockaddr *)&icmpsrc, |
0a7de745 A |
783 | (struct sockaddr *)&icmpdst, NULL, RTF_GATEWAY | RTF_HOST, |
784 | (struct sockaddr *)&icmpgw, NULL); | |
1c79356b A |
785 | pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc); |
786 | #if IPSEC | |
787 | key_sa_routechange((struct sockaddr *)&icmpsrc); | |
788 | #endif | |
789 | break; | |
790 | ||
791 | /* | |
792 | * No kernel processing for the following; | |
793 | * just fall through to send to raw listener. | |
794 | */ | |
795 | case ICMP_ECHOREPLY: | |
796 | case ICMP_ROUTERADVERT: | |
797 | case ICMP_ROUTERSOLICIT: | |
798 | case ICMP_TSTAMPREPLY: | |
799 | case ICMP_IREQREPLY: | |
800 | case ICMP_MASKREPLY: | |
801 | default: | |
802 | break; | |
803 | } | |
804 | ||
805 | raw: | |
806 | rip_input(m, hlen); | |
807 | return; | |
808 | ||
809 | freeit: | |
810 | m_freem(m); | |
811 | } | |
812 | ||
813 | /* | |
814 | * Reflect the ip packet back to the source | |
815 | */ | |
816 | static void | |
2d21ac55 | 817 | icmp_reflect(struct mbuf *m) |
1c79356b | 818 | { |
2d21ac55 | 819 | struct ip *ip = mtod(m, struct ip *); |
39236c6e | 820 | struct sockaddr_in icmpdst; |
2d21ac55 | 821 | struct in_ifaddr *ia; |
1c79356b | 822 | struct in_addr t; |
2d21ac55 | 823 | struct mbuf *opts = NULL; |
1c79356b A |
824 | int optlen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip); |
825 | ||
826 | if (!in_canforward(ip->ip_src) && | |
827 | ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) != | |
0a7de745 A |
828 | (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) { |
829 | m_freem(m); /* Bad return address */ | |
830 | goto done; /* Ip_output() will check for broadcast */ | |
1c79356b A |
831 | } |
832 | t = ip->ip_dst; | |
833 | ip->ip_dst = ip->ip_src; | |
834 | /* | |
835 | * If the incoming packet was addressed directly to us, | |
836 | * use dst as the src for the reply. Otherwise (broadcast | |
837 | * or anonymous), use the address which corresponds | |
838 | * to the incoming interface. | |
839 | */ | |
b0d623f7 A |
840 | lck_rw_lock_shared(in_ifaddr_rwlock); |
841 | TAILQ_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) { | |
6d2010ae A |
842 | IFA_LOCK(&ia->ia_ifa); |
843 | if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) { | |
844 | IFA_ADDREF_LOCKED(&ia->ia_ifa); | |
845 | IFA_UNLOCK(&ia->ia_ifa); | |
b0d623f7 | 846 | goto match; |
6d2010ae A |
847 | } |
848 | IFA_UNLOCK(&ia->ia_ifa); | |
b0d623f7 A |
849 | } |
850 | /* | |
851 | * Slow path; check for broadcast addresses. Find a source | |
852 | * IP address to use when replying to the broadcast request; | |
853 | * let IP handle the source interface selection work. | |
854 | */ | |
855 | for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next) { | |
6d2010ae | 856 | IFA_LOCK(&ia->ia_ifa); |
1c79356b | 857 | if (ia->ia_ifp && (ia->ia_ifp->if_flags & IFF_BROADCAST) && |
6d2010ae A |
858 | t.s_addr == satosin(&ia->ia_broadaddr)->sin_addr.s_addr) { |
859 | IFA_ADDREF_LOCKED(&ia->ia_ifa); | |
860 | IFA_UNLOCK(&ia->ia_ifa); | |
1c79356b | 861 | break; |
6d2010ae A |
862 | } |
863 | IFA_UNLOCK(&ia->ia_ifa); | |
1c79356b | 864 | } |
b0d623f7 | 865 | match: |
b0d623f7 | 866 | lck_rw_done(in_ifaddr_rwlock); |
39236c6e A |
867 | |
868 | /* Initialize */ | |
0a7de745 A |
869 | bzero(&icmpdst, sizeof(icmpdst)); |
870 | icmpdst.sin_len = sizeof(struct sockaddr_in); | |
39236c6e | 871 | icmpdst.sin_family = AF_INET; |
1c79356b | 872 | icmpdst.sin_addr = t; |
0a7de745 | 873 | if ((ia == (struct in_ifaddr *)0) && m->m_pkthdr.rcvif) { |
1c79356b A |
874 | ia = (struct in_ifaddr *)ifaof_ifpforaddr( |
875 | (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif); | |
0a7de745 | 876 | } |
1c79356b A |
877 | /* |
878 | * The following happens if the packet was not addressed to us, | |
879 | * and was received on an interface with no IP address. | |
880 | */ | |
91447636 | 881 | if (ia == (struct in_ifaddr *)0) { |
b0d623f7 | 882 | lck_rw_lock_shared(in_ifaddr_rwlock); |
1c79356b | 883 | ia = in_ifaddrhead.tqh_first; |
cc9f6e38 | 884 | if (ia == (struct in_ifaddr *)0) {/* no address yet, bail out */ |
b0d623f7 | 885 | lck_rw_done(in_ifaddr_rwlock); |
cc9f6e38 | 886 | m_freem(m); |
cc9f6e38 A |
887 | goto done; |
888 | } | |
6d2010ae | 889 | IFA_ADDREF(&ia->ia_ifa); |
b0d623f7 | 890 | lck_rw_done(in_ifaddr_rwlock); |
91447636 | 891 | } |
2d21ac55 A |
892 | #if CONFIG_MACF_NET |
893 | mac_netinet_icmp_reply(m); | |
894 | #endif | |
6d2010ae | 895 | IFA_LOCK_SPIN(&ia->ia_ifa); |
1c79356b | 896 | t = IA_SIN(ia)->sin_addr; |
6d2010ae | 897 | IFA_UNLOCK(&ia->ia_ifa); |
1c79356b | 898 | ip->ip_src = t; |
9bccf70c | 899 | ip->ip_ttl = ip_defttl; |
6d2010ae | 900 | IFA_REMREF(&ia->ia_ifa); |
91447636 | 901 | ia = NULL; |
1c79356b A |
902 | |
903 | if (optlen > 0) { | |
2d21ac55 | 904 | u_char *cp; |
1c79356b A |
905 | int opt, cnt; |
906 | u_int len; | |
907 | ||
908 | /* | |
909 | * Retrieve any source routing from the incoming packet; | |
910 | * add on any record-route or timestamp options. | |
911 | */ | |
912 | cp = (u_char *) (ip + 1); | |
913 | if ((opts = ip_srcroute()) == 0 && | |
0a7de745 | 914 | (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) { /* MAC-OK */ |
1c79356b A |
915 | opts->m_len = sizeof(struct in_addr); |
916 | mtod(opts, struct in_addr *)->s_addr = 0; | |
917 | } | |
918 | if (opts) { | |
39037602 | 919 | #if (DEBUG | DEVELOPMENT) |
0a7de745 A |
920 | if (icmpprintfs > 1) { |
921 | printf("icmp_reflect optlen %d rt %d => ", | |
922 | optlen, opts->m_len); | |
923 | } | |
1c79356b | 924 | #endif |
0a7de745 A |
925 | for (cnt = optlen; cnt > 0; cnt -= len, cp += len) { |
926 | opt = cp[IPOPT_OPTVAL]; | |
927 | if (opt == IPOPT_EOL) { | |
928 | break; | |
929 | } | |
930 | if (opt == IPOPT_NOP) { | |
931 | len = 1; | |
932 | } else { | |
933 | if (cnt < IPOPT_OLEN + sizeof(*cp)) { | |
934 | break; | |
935 | } | |
936 | len = cp[IPOPT_OLEN]; | |
937 | if (len < IPOPT_OLEN + sizeof(*cp) || | |
938 | len > cnt) { | |
939 | break; | |
940 | } | |
941 | } | |
942 | /* | |
943 | * Should check for overflow, but it "can't happen" | |
944 | */ | |
945 | if (opt == IPOPT_RR || opt == IPOPT_TS || | |
946 | opt == IPOPT_SECURITY) { | |
947 | bcopy((caddr_t)cp, | |
948 | mtod(opts, caddr_t) + opts->m_len, len); | |
949 | opts->m_len += len; | |
950 | } | |
951 | } | |
952 | /* Terminate & pad, if necessary */ | |
953 | cnt = opts->m_len % 4; | |
954 | if (cnt) { | |
955 | for (; cnt < 4; cnt++) { | |
956 | *(mtod(opts, caddr_t) + opts->m_len) = | |
957 | IPOPT_EOL; | |
958 | opts->m_len++; | |
959 | } | |
960 | } | |
39037602 | 961 | #if (DEBUG | DEVELOPMENT) |
0a7de745 A |
962 | if (icmpprintfs > 1) { |
963 | printf("%d\n", opts->m_len); | |
964 | } | |
1c79356b A |
965 | #endif |
966 | } | |
967 | /* | |
968 | * Now strip out original options by copying rest of first | |
969 | * mbuf's data back, and adjust the IP length. | |
970 | */ | |
971 | ip->ip_len -= optlen; | |
972 | ip->ip_vhl = IP_VHL_BORING; | |
973 | m->m_len -= optlen; | |
0a7de745 | 974 | if (m->m_flags & M_PKTHDR) { |
1c79356b | 975 | m->m_pkthdr.len -= optlen; |
0a7de745 | 976 | } |
1c79356b A |
977 | optlen += sizeof(struct ip); |
978 | bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1), | |
0a7de745 | 979 | (unsigned)(m->m_len - sizeof(struct ip))); |
1c79356b | 980 | } |
0a7de745 | 981 | m->m_flags &= ~(M_BCAST | M_MCAST); |
1c79356b A |
982 | icmp_send(m, opts); |
983 | done: | |
0a7de745 | 984 | if (opts) { |
1c79356b | 985 | (void)m_free(opts); |
0a7de745 | 986 | } |
1c79356b A |
987 | } |
988 | ||
989 | /* | |
990 | * Send an icmp packet back to the ip level, | |
991 | * after supplying a checksum. | |
992 | */ | |
993 | static void | |
2d21ac55 | 994 | icmp_send(struct mbuf *m, struct mbuf *opts) |
1c79356b | 995 | { |
2d21ac55 A |
996 | struct ip *ip = mtod(m, struct ip *); |
997 | int hlen; | |
998 | struct icmp *icp; | |
1c79356b | 999 | struct route ro; |
a39ff7e2 A |
1000 | struct ip_out_args ipoa; |
1001 | ||
1002 | bzero(&ipoa, sizeof(ipoa)); | |
1003 | ipoa.ipoa_boundif = IFSCOPE_NONE; | |
1004 | ipoa.ipoa_flags = IPOAF_SELECT_SRCIF | IPOAF_BOUND_SRCADDR; | |
1005 | ipoa.ipoa_sotc = SO_TC_UNSPEC; | |
1006 | ipoa.ipoa_netsvctype = _NET_SERVICE_TYPE_UNSPEC; | |
c910b4d9 | 1007 | |
39236c6e | 1008 | if (!(m->m_pkthdr.pkt_flags & PKTF_LOOP) && m->m_pkthdr.rcvif != NULL) { |
6d2010ae | 1009 | ipoa.ipoa_boundif = m->m_pkthdr.rcvif->if_index; |
316670eb A |
1010 | ipoa.ipoa_flags |= IPOAF_BOUND_IF; |
1011 | } | |
1c79356b A |
1012 | |
1013 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
1014 | m->m_data += hlen; | |
1015 | m->m_len -= hlen; | |
1016 | icp = mtod(m, struct icmp *); | |
1017 | icp->icmp_cksum = 0; | |
1018 | icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen); | |
1019 | m->m_data -= hlen; | |
1020 | m->m_len += hlen; | |
2d21ac55 | 1021 | m->m_pkthdr.rcvif = NULL; |
0b4e3aa0 A |
1022 | m->m_pkthdr.csum_data = 0; |
1023 | m->m_pkthdr.csum_flags = 0; | |
39037602 A |
1024 | #if (DEBUG | DEVELOPMENT) |
1025 | if (icmpprintfs > 2) { | |
1026 | char src_str[MAX_IPv4_STR_LEN]; | |
1027 | char dst_str[MAX_IPv4_STR_LEN]; | |
1028 | ||
1029 | inet_ntop(AF_INET, &ip->ip_src, src_str, sizeof(src_str)); | |
1030 | inet_ntop(AF_INET, &ip->ip_dst, dst_str, sizeof(dst_str)); | |
1031 | printf("%s: dst %s src %s\n", __func__, dst_str, src_str); | |
1c79356b A |
1032 | } |
1033 | #endif | |
1034 | bzero(&ro, sizeof ro); | |
c910b4d9 | 1035 | (void) ip_output(m, opts, &ro, IP_OUTARGS, NULL, &ipoa); |
39236c6e | 1036 | ROUTE_RELEASE(&ro); |
1c79356b A |
1037 | } |
1038 | ||
04b8595b | 1039 | u_int32_t |
2d21ac55 | 1040 | iptime(void) |
1c79356b A |
1041 | { |
1042 | struct timeval atv; | |
b0d623f7 | 1043 | u_int32_t t; |
1c79356b | 1044 | |
39236c6e | 1045 | getmicrotime(&atv); |
0a7de745 A |
1046 | t = (atv.tv_sec % (24 * 60 * 60)) * 1000 + atv.tv_usec / 1000; |
1047 | return htonl(t); | |
1c79356b A |
1048 | } |
1049 | ||
1050 | #if 1 | |
1051 | /* | |
1052 | * Return the next larger or smaller MTU plateau (table from RFC 1191) | |
1053 | * given current value MTU. If DIR is less than zero, a larger plateau | |
1054 | * is returned; otherwise, a smaller value is returned. | |
1055 | */ | |
b0d623f7 | 1056 | int |
2d21ac55 | 1057 | ip_next_mtu(int mtu, int dir) |
1c79356b A |
1058 | { |
1059 | static int mtutab[] = { | |
1060 | 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1006, 508, 296, | |
1061 | 68, 0 | |
1062 | }; | |
1063 | int i; | |
1064 | ||
1065 | for (i = 0; i < (sizeof mtutab) / (sizeof mtutab[0]); i++) { | |
0a7de745 | 1066 | if (mtu >= mtutab[i]) { |
1c79356b | 1067 | break; |
0a7de745 | 1068 | } |
1c79356b A |
1069 | } |
1070 | ||
1071 | if (dir < 0) { | |
1072 | if (i == 0) { | |
1073 | return 0; | |
1074 | } else { | |
1075 | return mtutab[i - 1]; | |
1076 | } | |
1077 | } else { | |
1078 | if (mtutab[i] == 0) { | |
1079 | return 0; | |
0a7de745 | 1080 | } else if (mtu > mtutab[i]) { |
1c79356b A |
1081 | return mtutab[i]; |
1082 | } else { | |
1083 | return mtutab[i + 1]; | |
1084 | } | |
1085 | } | |
1086 | } | |
1087 | #endif | |
1088 | ||
1089 | #if ICMP_BANDLIM | |
1090 | ||
1091 | /* | |
1092 | * badport_bandlim() - check for ICMP bandwidth limit | |
1093 | * | |
1094 | * Return 0 if it is ok to send an ICMP error response, -1 if we have | |
0a7de745 | 1095 | * hit our bandwidth limit and it is not ok. |
1c79356b A |
1096 | * |
1097 | * If icmplim is <= 0, the feature is disabled and 0 is returned. | |
1098 | * | |
1099 | * For now we separate the TCP and UDP subsystems w/ different 'which' | |
1100 | * values. We may eventually remove this separation (and simplify the | |
1101 | * code further). | |
1102 | * | |
1103 | * Note that the printing of the error message is delayed so we can | |
1104 | * properly print the icmp error rate that the system was trying to do | |
1105 | * (i.e. 22000/100 pps, etc...). This can cause long delays in printing | |
0a7de745 | 1106 | * the 'final' error, but it doesn't make sense to solve the printing |
1c79356b A |
1107 | * delay with more complex code. |
1108 | */ | |
1109 | ||
1110 | int | |
1111 | badport_bandlim(int which) | |
1112 | { | |
39236c6e | 1113 | static uint64_t lticks[BANDLIM_MAX + 1]; |
9bccf70c | 1114 | static int lpackets[BANDLIM_MAX + 1]; |
39236c6e | 1115 | uint64_t time = net_uptime(); |
9bccf70c A |
1116 | int secs; |
1117 | ||
1118 | const char *bandlimittype[] = { | |
1119 | "Limiting icmp unreach response", | |
1120 | "Limiting icmp ping response", | |
1121 | "Limiting icmp tstamp response", | |
1122 | "Limiting closed port RST response", | |
1123 | "Limiting open port RST response" | |
0a7de745 | 1124 | }; |
1c79356b A |
1125 | |
1126 | /* | |
1127 | * Return ok status if feature disabled or argument out of | |
1128 | * ranage. | |
1129 | */ | |
1130 | ||
0a7de745 A |
1131 | if (icmplim <= 0 || which > BANDLIM_MAX || which < 0) { |
1132 | return 0; | |
1133 | } | |
1c79356b | 1134 | |
39236c6e | 1135 | secs = time - lticks[which]; |
9bccf70c | 1136 | |
1c79356b | 1137 | /* |
9bccf70c | 1138 | * reset stats when cumulative delta exceeds one second. |
1c79356b A |
1139 | */ |
1140 | ||
39236c6e | 1141 | if (secs > 1) { |
1c79356b | 1142 | if (lpackets[which] > icmplim) { |
9bccf70c | 1143 | printf("%s from %d to %d packets per second\n", |
0a7de745 A |
1144 | bandlimittype[which], |
1145 | lpackets[which], | |
1146 | icmplim | |
1147 | ); | |
1c79356b | 1148 | } |
39236c6e | 1149 | lticks[which] = time; |
1c79356b A |
1150 | lpackets[which] = 0; |
1151 | } | |
1152 | ||
1153 | /* | |
1154 | * bump packet count | |
1155 | */ | |
1156 | ||
1157 | if (++lpackets[which] > icmplim) { | |
0a7de745 | 1158 | return -1; |
1c79356b | 1159 | } |
0a7de745 | 1160 | return 0; |
1c79356b A |
1161 | } |
1162 | ||
1163 | #endif | |
1164 | ||
9bccf70c A |
1165 | #if __APPLE__ |
1166 | ||
1167 | /* | |
1168 | * Non-privileged ICMP socket operations | |
1169 | * - send ICMP echo request | |
1170 | * - all ICMP | |
1171 | * - limited socket options | |
1172 | */ | |
1173 | ||
1174 | #include <netinet/ip_icmp.h> | |
1175 | #include <netinet/in_pcb.h> | |
1176 | ||
b0d623f7 A |
1177 | extern u_int32_t rip_sendspace; |
1178 | extern u_int32_t rip_recvspace; | |
9bccf70c A |
1179 | extern struct inpcbinfo ripcbinfo; |
1180 | ||
1181 | int rip_abort(struct socket *); | |
1182 | int rip_bind(struct socket *, struct sockaddr *, struct proc *); | |
1183 | int rip_connect(struct socket *, struct sockaddr *, struct proc *); | |
1184 | int rip_detach(struct socket *); | |
1185 | int rip_disconnect(struct socket *); | |
1186 | int rip_shutdown(struct socket *); | |
1187 | ||
1188 | __private_extern__ int icmp_dgram_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, struct mbuf *control, struct proc *p); | |
1189 | __private_extern__ int icmp_dgram_attach(struct socket *so, int proto, struct proc *p); | |
1190 | __private_extern__ int icmp_dgram_ctloutput(struct socket *so, struct sockopt *sopt); | |
1191 | ||
1192 | __private_extern__ struct pr_usrreqs icmp_dgram_usrreqs = { | |
0a7de745 A |
1193 | .pru_abort = rip_abort, |
1194 | .pru_attach = icmp_dgram_attach, | |
1195 | .pru_bind = rip_bind, | |
1196 | .pru_connect = rip_connect, | |
1197 | .pru_control = in_control, | |
1198 | .pru_detach = rip_detach, | |
1199 | .pru_disconnect = rip_disconnect, | |
1200 | .pru_peeraddr = in_getpeeraddr, | |
1201 | .pru_send = icmp_dgram_send, | |
1202 | .pru_shutdown = rip_shutdown, | |
1203 | .pru_sockaddr = in_getsockaddr, | |
1204 | .pru_sosend = sosend, | |
1205 | .pru_soreceive = soreceive, | |
9bccf70c A |
1206 | }; |
1207 | ||
1208 | /* Like rip_attach but without root privilege enforcement */ | |
1209 | __private_extern__ int | |
2d21ac55 | 1210 | icmp_dgram_attach(struct socket *so, __unused int proto, struct proc *p) |
9bccf70c | 1211 | { |
0a7de745 A |
1212 | struct inpcb *inp; |
1213 | int error; | |
1214 | ||
1215 | inp = sotoinpcb(so); | |
1216 | if (inp) { | |
1217 | panic("icmp_dgram_attach"); | |
1218 | } | |
1219 | ||
1220 | error = soreserve(so, rip_sendspace, rip_recvspace); | |
1221 | if (error) { | |
1222 | return error; | |
1223 | } | |
1224 | error = in_pcballoc(so, &ripcbinfo, p); | |
1225 | if (error) { | |
1226 | return error; | |
1227 | } | |
1228 | inp = (struct inpcb *)so->so_pcb; | |
1229 | inp->inp_vflag |= INP_IPV4; | |
1230 | inp->inp_ip_p = IPPROTO_ICMP; | |
1231 | inp->inp_ip_ttl = ip_defttl; | |
1232 | return 0; | |
9bccf70c A |
1233 | } |
1234 | ||
1235 | /* | |
1236 | * Raw IP socket option processing. | |
1237 | */ | |
1238 | __private_extern__ int | |
1239 | icmp_dgram_ctloutput(struct socket *so, struct sockopt *sopt) | |
1240 | { | |
0a7de745 | 1241 | int error; |
9bccf70c | 1242 | |
0a7de745 A |
1243 | if (sopt->sopt_level != IPPROTO_IP) { |
1244 | return EINVAL; | |
1245 | } | |
9bccf70c A |
1246 | |
1247 | switch (sopt->sopt_name) { | |
0a7de745 A |
1248 | case IP_OPTIONS: |
1249 | case IP_HDRINCL: | |
1250 | case IP_TOS: | |
1251 | case IP_TTL: | |
1252 | case IP_RECVOPTS: | |
1253 | case IP_RECVRETOPTS: | |
1254 | case IP_RECVDSTADDR: | |
1255 | case IP_RETOPTS: | |
1256 | case IP_MULTICAST_IF: | |
1257 | case IP_MULTICAST_IFINDEX: | |
1258 | case IP_MULTICAST_TTL: | |
1259 | case IP_MULTICAST_LOOP: | |
1260 | case IP_ADD_MEMBERSHIP: | |
1261 | case IP_DROP_MEMBERSHIP: | |
1262 | case IP_MULTICAST_VIF: | |
1263 | case IP_PORTRANGE: | |
1264 | case IP_RECVIF: | |
1265 | case IP_IPSEC_POLICY: | |
1266 | case IP_STRIPHDR: | |
1267 | case IP_RECVTTL: | |
1268 | case IP_BOUND_IF: | |
1269 | case IP_NO_IFT_CELLULAR: | |
1270 | error = rip_ctloutput(so, sopt); | |
1271 | break; | |
b0d623f7 | 1272 | |
0a7de745 A |
1273 | default: |
1274 | error = EINVAL; | |
1275 | break; | |
9bccf70c A |
1276 | } |
1277 | ||
0a7de745 | 1278 | return error; |
9bccf70c A |
1279 | } |
1280 | ||
1281 | __private_extern__ int | |
39236c6e A |
1282 | icmp_dgram_send(struct socket *so, int flags, struct mbuf *m, |
1283 | struct sockaddr *nam, struct mbuf *control, struct proc *p) | |
9bccf70c A |
1284 | { |
1285 | struct ip *ip; | |
1286 | struct inpcb *inp = sotoinpcb(so); | |
1287 | int hlen; | |
1288 | struct icmp *icp; | |
0a7de745 | 1289 | struct in_ifaddr *ia = NULL; |
9bccf70c | 1290 | int icmplen; |
39236c6e A |
1291 | int error = EINVAL; |
1292 | ||
fe8ab488 A |
1293 | if (inp == NULL |
1294 | #if NECP | |
0a7de745 | 1295 | || (necp_socket_should_use_flow_divert(inp)) |
fe8ab488 | 1296 | #endif /* NECP */ |
0a7de745 A |
1297 | ) { |
1298 | if (inp != NULL) { | |
39236c6e | 1299 | error = EPROTOTYPE; |
0a7de745 | 1300 | } |
39236c6e A |
1301 | goto bad; |
1302 | } | |
9bccf70c A |
1303 | |
1304 | if ((inp->inp_flags & INP_HDRINCL) != 0) { | |
39236c6e | 1305 | /* Expect 32-bit aligned data ptr on strict-align platforms */ |
316670eb | 1306 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); |
9bccf70c | 1307 | /* |
39236c6e A |
1308 | * This is not raw IP, we liberal only for fields TOS, |
1309 | * id and TTL. | |
9bccf70c A |
1310 | */ |
1311 | ip = mtod(m, struct ip *); | |
1312 | ||
1313 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
1314 | /* Some sanity checks */ | |
1315 | if (m->m_pkthdr.len < hlen + ICMP_MINLEN) { | |
1316 | goto bad; | |
1317 | } | |
1318 | /* Only IPv4 */ | |
0a7de745 | 1319 | if (IP_VHL_V(ip->ip_vhl) != 4) { |
9bccf70c | 1320 | goto bad; |
0a7de745 A |
1321 | } |
1322 | if (hlen < 20 || hlen > 40 || ip->ip_len != m->m_pkthdr.len) { | |
9bccf70c | 1323 | goto bad; |
0a7de745 | 1324 | } |
39236c6e | 1325 | /* Bogus fragments can tie up peer resources */ |
0a7de745 | 1326 | if ((ip->ip_off & ~IP_DF) != 0) { |
9bccf70c | 1327 | goto bad; |
0a7de745 | 1328 | } |
9bccf70c | 1329 | /* Allow only ICMP even for user provided IP header */ |
0a7de745 | 1330 | if (ip->ip_p != IPPROTO_ICMP) { |
9bccf70c | 1331 | goto bad; |
0a7de745 | 1332 | } |
39236c6e A |
1333 | /* |
1334 | * To prevent spoofing, specified source address must | |
1335 | * be one of ours. | |
1336 | */ | |
9bccf70c | 1337 | if (ip->ip_src.s_addr != INADDR_ANY) { |
91447636 | 1338 | socket_unlock(so, 0); |
b0d623f7 | 1339 | lck_rw_lock_shared(in_ifaddr_rwlock); |
91447636 | 1340 | if (TAILQ_EMPTY(&in_ifaddrhead)) { |
b0d623f7 | 1341 | lck_rw_done(in_ifaddr_rwlock); |
91447636 | 1342 | socket_lock(so, 0); |
9bccf70c | 1343 | goto bad; |
91447636 | 1344 | } |
b0d623f7 A |
1345 | TAILQ_FOREACH(ia, INADDR_HASH(ip->ip_src.s_addr), |
1346 | ia_hash) { | |
6d2010ae | 1347 | IFA_LOCK(&ia->ia_ifa); |
b0d623f7 A |
1348 | if (IA_SIN(ia)->sin_addr.s_addr == |
1349 | ip->ip_src.s_addr) { | |
6d2010ae | 1350 | IFA_UNLOCK(&ia->ia_ifa); |
b0d623f7 | 1351 | lck_rw_done(in_ifaddr_rwlock); |
91447636 | 1352 | socket_lock(so, 0); |
9bccf70c | 1353 | goto ours; |
91447636 | 1354 | } |
6d2010ae | 1355 | IFA_UNLOCK(&ia->ia_ifa); |
9bccf70c | 1356 | } |
b0d623f7 | 1357 | lck_rw_done(in_ifaddr_rwlock); |
91447636 | 1358 | socket_lock(so, 0); |
9bccf70c A |
1359 | goto bad; |
1360 | } | |
1361 | ours: | |
1362 | /* Do not trust we got a valid checksum */ | |
1363 | ip->ip_sum = 0; | |
316670eb A |
1364 | |
1365 | icp = (struct icmp *)(void *)(((char *)m->m_data) + hlen); | |
9bccf70c A |
1366 | icmplen = m->m_pkthdr.len - hlen; |
1367 | } else { | |
1368 | if ((icmplen = m->m_pkthdr.len) < ICMP_MINLEN) { | |
1369 | goto bad; | |
1370 | } | |
1371 | icp = mtod(m, struct icmp *); | |
1372 | } | |
1373 | /* | |
1374 | * Allow only to send request types with code 0 | |
1375 | */ | |
0a7de745 | 1376 | if (icp->icmp_code != 0) { |
9bccf70c | 1377 | goto bad; |
0a7de745 | 1378 | } |
9bccf70c | 1379 | switch (icp->icmp_type) { |
0a7de745 A |
1380 | case ICMP_ECHO: |
1381 | break; | |
1382 | case ICMP_TSTAMP: | |
1383 | if (icmplen != 20) { | |
1384 | goto bad; | |
1385 | } | |
1386 | break; | |
1387 | case ICMP_MASKREQ: | |
1388 | if (icmplen != 12) { | |
9bccf70c | 1389 | goto bad; |
0a7de745 A |
1390 | } |
1391 | break; | |
1392 | default: | |
1393 | goto bad; | |
9bccf70c | 1394 | } |
0a7de745 | 1395 | return rip_send(so, flags, m, nam, control, p); |
9bccf70c | 1396 | bad: |
39236c6e A |
1397 | VERIFY(error != 0); |
1398 | ||
0a7de745 | 1399 | if (m != NULL) { |
39236c6e | 1400 | m_freem(m); |
0a7de745 A |
1401 | } |
1402 | if (control != NULL) { | |
39236c6e | 1403 | m_freem(control); |
0a7de745 | 1404 | } |
39236c6e | 1405 | |
0a7de745 | 1406 | return error; |
9bccf70c A |
1407 | } |
1408 | ||
1409 | #endif /* __APPLE__ */ |