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