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