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