1 | /*
|
---|
2 | * Copyright (c) 1982, 1986, 1988, 1993
|
---|
3 | * The Regents of the University of California. All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | * 1. Redistributions of source code must retain the above copyright
|
---|
9 | * notice, this list of conditions and the following disclaimer.
|
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer in the
|
---|
12 | * documentation and/or other materials provided with the distribution.
|
---|
13 | * 3. All advertising materials mentioning features or use of this software
|
---|
14 | * must display the following acknowledgement:
|
---|
15 | * This product includes software developed by the University of
|
---|
16 | * California, Berkeley and its contributors.
|
---|
17 | * 4. Neither the name of the University nor the names of its contributors
|
---|
18 | * may be used to endorse or promote products derived from this software
|
---|
19 | * without specific prior written permission.
|
---|
20 | *
|
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
31 | * SUCH DAMAGE.
|
---|
32 | *
|
---|
33 | * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
|
---|
34 | * ip_icmp.c,v 1.7 1995/05/30 08:09:42 rgrimes Exp
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include "slirp.h"
|
---|
38 | #include "ip_icmp.h"
|
---|
39 | #ifdef RT_OS_WINDOWS
|
---|
40 | #include <Icmpapi.h>
|
---|
41 | #include <Iphlpapi.h>
|
---|
42 | #endif
|
---|
43 |
|
---|
44 |
|
---|
45 | /* The message sent when emulating PING */
|
---|
46 | /* Be nice and tell them it's just a psuedo-ping packet */
|
---|
47 | static const char icmp_ping_msg[] = "This is a psuedo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST packets.\n";
|
---|
48 |
|
---|
49 | /* list of actions for icmp_error() on RX of an icmp message */
|
---|
50 | static const int icmp_flush[19] =
|
---|
51 | {
|
---|
52 | /* ECHO REPLY (0) */ 0,
|
---|
53 | 1,
|
---|
54 | 1,
|
---|
55 | /* DEST UNREACH (3) */ 1,
|
---|
56 | /* SOURCE QUENCH (4)*/ 1,
|
---|
57 | /* REDIRECT (5) */ 1,
|
---|
58 | 1,
|
---|
59 | 1,
|
---|
60 | /* ECHO (8) */ 0,
|
---|
61 | /* ROUTERADVERT (9) */ 1,
|
---|
62 | /* ROUTERSOLICIT (10) */ 1,
|
---|
63 | /* TIME EXCEEDED (11) */ 1,
|
---|
64 | /* PARAMETER PROBLEM (12) */ 1,
|
---|
65 | /* TIMESTAMP (13) */ 0,
|
---|
66 | /* TIMESTAMP REPLY (14) */ 0,
|
---|
67 | /* INFO (15) */ 0,
|
---|
68 | /* INFO REPLY (16) */ 0,
|
---|
69 | /* ADDR MASK (17) */ 0,
|
---|
70 | /* ADDR MASK REPLY (18) */ 0
|
---|
71 | };
|
---|
72 |
|
---|
73 | #ifdef VBOX_WITH_SLIRP_ICMP
|
---|
74 | int
|
---|
75 | icmp_init(PNATState pData)
|
---|
76 | {
|
---|
77 | pData->icmp_socket.so_type = IPPROTO_ICMP;
|
---|
78 | pData->icmp_socket.so_state = SS_ISFCONNECTED;
|
---|
79 | #ifndef RT_OS_WINDOWS
|
---|
80 | # ifndef RT_OS_DARWIN
|
---|
81 | pData->icmp_socket.s = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
|
---|
82 | # else /* !RT_OS_DARWIN */
|
---|
83 | pData->icmp_socket.s = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
|
---|
84 | # endif /* RT_OS_DARWIN */
|
---|
85 | if (pData->icmp_socket.s == -1)
|
---|
86 | {
|
---|
87 | int rc = RTErrConvertFromErrno(errno);
|
---|
88 | LogRel(("NAT: ICMP/ping not available (could open ICMP socket, error %Rrc)\n", rc));
|
---|
89 | return 1;
|
---|
90 | }
|
---|
91 | #else /* RT_OS_WINDOWS */
|
---|
92 | pData->hmIcmpLibrary = LoadLibrary("Iphlpapi.dll");
|
---|
93 | if (pData->hmIcmpLibrary != NULL)
|
---|
94 | {
|
---|
95 | pData->pfIcmpParseReplies = (long (WINAPI *)(void *, long))
|
---|
96 | GetProcAddress(pData->hmIcmpLibrary, "IcmpParseReplies");
|
---|
97 | pData->pfIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
|
---|
98 | GetProcAddress(pData->hmIcmpLibrary, "IcmpCloseHandle");
|
---|
99 | }
|
---|
100 | if (pData->pfIcmpParseReplies == NULL)
|
---|
101 | {
|
---|
102 | FreeLibrary(pData->hmIcmpLibrary);
|
---|
103 | pData->hmIcmpLibrary = LoadLibrary("Icmp.dll");
|
---|
104 | if (pData->hmIcmpLibrary == NULL)
|
---|
105 | {
|
---|
106 | LogRel(("NAT: Icmp.dll could not be loaded\n"));
|
---|
107 | return 1;
|
---|
108 | }
|
---|
109 | pData->pfIcmpParseReplies = (long (WINAPI *)(void *, long))
|
---|
110 | GetProcAddress(pData->hmIcmpLibrary, "IcmpParseReplies");
|
---|
111 | pData->pfIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
|
---|
112 | GetProcAddress(pData->hmIcmpLibrary, "IcmpCloseHandle");
|
---|
113 | }
|
---|
114 | if (pData->pfIcmpParseReplies == NULL)
|
---|
115 | {
|
---|
116 | LogRel(("NAT: Can't find IcmpParseReplies symbol\n"));
|
---|
117 | FreeLibrary(pData->hmIcmpLibrary);
|
---|
118 | return 1;
|
---|
119 | }
|
---|
120 | if (pData->pfIcmpCloseHandle == NULL)
|
---|
121 | {
|
---|
122 | LogRel(("NAT: Can't find IcmpCloseHandle symbol\n"));
|
---|
123 | FreeLibrary(pData->hmIcmpLibrary);
|
---|
124 | return 1;
|
---|
125 | }
|
---|
126 | pData->icmp_socket.sh = IcmpCreateFile();
|
---|
127 | pData->phEvents[VBOX_ICMP_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
|
---|
128 | pData->szIcmpBuffer = sizeof(ICMP_ECHO_REPLY) * 10;
|
---|
129 | pData->pvIcmpBuffer = malloc(pData->szIcmpBuffer);
|
---|
130 | #endif /* RT_OS_WINDOWS */
|
---|
131 | LIST_INIT(&pData->icmp_msg_head);
|
---|
132 | return 0;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /*
|
---|
136 | * ip here is ip header + 64bytes readed from ICMP packet
|
---|
137 | */
|
---|
138 | struct icmp_msg *
|
---|
139 | icmp_find_original_mbuf(PNATState pData, struct ip *ip)
|
---|
140 | {
|
---|
141 | struct mbuf *m0;
|
---|
142 | struct ip *ip0;
|
---|
143 | struct icmp *icp, *icp0;
|
---|
144 | struct icmp_msg *icm = NULL;
|
---|
145 | int found = 0;
|
---|
146 | struct udphdr *udp;
|
---|
147 | struct tcphdr *tcp;
|
---|
148 | struct socket *head_socket = NULL;
|
---|
149 | struct socket *last_socket = NULL;
|
---|
150 | struct socket *so = NULL;
|
---|
151 | struct in_addr laddr, faddr;
|
---|
152 | u_short lport, fport;
|
---|
153 |
|
---|
154 | laddr.s_addr = ~0;
|
---|
155 | faddr.s_addr = ~0;
|
---|
156 |
|
---|
157 | lport = ~0;
|
---|
158 | fport = ~0;
|
---|
159 |
|
---|
160 |
|
---|
161 | LogRel(("%s: processing (proto:%d)\n", __FUNCTION__, ip->ip_p));
|
---|
162 | switch (ip->ip_p)
|
---|
163 | {
|
---|
164 | case IPPROTO_ICMP:
|
---|
165 | icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
|
---|
166 | LIST_FOREACH(icm, &pData->icmp_msg_head, im_list)
|
---|
167 | {
|
---|
168 | m0 = icm->im_m;
|
---|
169 | ip0 = mtod(m0, struct ip *);
|
---|
170 | AssertRelease(ip0->ip_p == IPPROTO_ICMP);
|
---|
171 | icp0 = (struct icmp *)((char *)ip0 + (ip0->ip_hl << 2));
|
---|
172 | if ( ( (icp->icmp_type != ICMP_ECHO && ip->ip_src.s_addr == ip0->ip_dst.s_addr)
|
---|
173 | || (icp->icmp_type == ICMP_ECHO && ip->ip_dst.s_addr == ip0->ip_dst.s_addr))
|
---|
174 | && icp->icmp_id == icp0->icmp_id
|
---|
175 | && icp->icmp_seq == icp0->icmp_seq)
|
---|
176 | {
|
---|
177 | found = 1;
|
---|
178 | break;
|
---|
179 | }
|
---|
180 | LogRel(("Have found nothing\n"));
|
---|
181 | }
|
---|
182 | break;
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * for TCP and UDP logic little bit reverted, we try to find the HOST socket
|
---|
186 | * from which the IP package has been sent.
|
---|
187 | */
|
---|
188 | case IPPROTO_UDP:
|
---|
189 | head_socket = &udb;
|
---|
190 | udp = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
|
---|
191 | faddr.s_addr = ip->ip_dst.s_addr;
|
---|
192 | fport = udp->uh_dport;
|
---|
193 | laddr.s_addr = ip->ip_src.s_addr;
|
---|
194 | lport = udp->uh_sport;
|
---|
195 | last_socket = udp_last_so;
|
---|
196 | /* fall through */
|
---|
197 |
|
---|
198 | case IPPROTO_TCP:
|
---|
199 | if (head_socket == NULL)
|
---|
200 | {
|
---|
201 | tcp = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
|
---|
202 | head_socket = &tcb; /* head_socket could be initialized with udb*/
|
---|
203 | faddr.s_addr = ip->ip_dst.s_addr;
|
---|
204 | fport = tcp->th_dport;
|
---|
205 | laddr.s_addr = ip->ip_src.s_addr;
|
---|
206 | lport = tcp->th_sport;
|
---|
207 | last_socket = tcp_last_so;
|
---|
208 | }
|
---|
209 | /* check last socket first */
|
---|
210 | if ( last_socket->so_faddr.s_addr == faddr.s_addr
|
---|
211 | && last_socket->so_fport == fport
|
---|
212 | && last_socket->so_hlport == lport)
|
---|
213 | {
|
---|
214 | found = 1;
|
---|
215 | so = last_socket;
|
---|
216 | goto sofound;
|
---|
217 | }
|
---|
218 | for (so = head_socket->so_prev; so != head_socket; so = so->so_prev)
|
---|
219 | {
|
---|
220 | /* Should be reaplaced by hash here */
|
---|
221 | LogRel(("trying:%R[natsock] against %R[IP4]:%d lport=%d hlport=%d\n", so, &faddr, fport, lport, so->so_hlport));
|
---|
222 | if ( so->so_faddr.s_addr == faddr.s_addr
|
---|
223 | && so->so_fport == fport
|
---|
224 | && so->so_hlport == lport)
|
---|
225 | {
|
---|
226 | found = 1;
|
---|
227 | break;
|
---|
228 | }
|
---|
229 | }
|
---|
230 | break;
|
---|
231 |
|
---|
232 | default:
|
---|
233 | LogRel(("%s:ICMP: unsupported protocol(%d)\n", __FUNCTION__, ip->ip_p));
|
---|
234 | }
|
---|
235 | sofound:
|
---|
236 | if (found == 1 && icm == NULL)
|
---|
237 | {
|
---|
238 | icm = malloc(sizeof(struct icmp_msg));
|
---|
239 | icm->im_m = so->so_m;
|
---|
240 | icm->im_so = so;
|
---|
241 | found = 1;
|
---|
242 | LogRel(("hit:%R[natsock]\n", so));
|
---|
243 | /*XXX: this storage not very long,
|
---|
244 | * better add flag if it should removed from lis
|
---|
245 | */
|
---|
246 | LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
|
---|
247 | return (icm);
|
---|
248 | }
|
---|
249 | if (found == 1)
|
---|
250 | return icm;
|
---|
251 |
|
---|
252 | return NULL;
|
---|
253 | }
|
---|
254 |
|
---|
255 | static int
|
---|
256 | icmp_attach(PNATState pData, struct mbuf *m)
|
---|
257 | {
|
---|
258 | struct icmp_msg *icm;
|
---|
259 | struct ip *ip;
|
---|
260 | ip = mtod(m, struct ip *);
|
---|
261 | Assert(ip->ip_p == IPPROTO_ICMP);
|
---|
262 | icm = malloc(sizeof(struct icmp_msg));
|
---|
263 | icm->im_m = m;
|
---|
264 | LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
|
---|
265 | return 0;
|
---|
266 | }
|
---|
267 | #endif /* VBOX_WITH_SLIRP_ICMP */
|
---|
268 |
|
---|
269 | /*
|
---|
270 | * Process a received ICMP message.
|
---|
271 | */
|
---|
272 | void
|
---|
273 | icmp_input(PNATState pData, struct mbuf *m, int hlen)
|
---|
274 | {
|
---|
275 | register struct icmp *icp;
|
---|
276 | register struct ip *ip = mtod(m, struct ip *);
|
---|
277 | int icmplen = ip->ip_len;
|
---|
278 | int status;
|
---|
279 | uint32_t dst;
|
---|
280 | int ttl;
|
---|
281 |
|
---|
282 | /* int code; */
|
---|
283 |
|
---|
284 | DEBUG_CALL("icmp_input");
|
---|
285 | DEBUG_ARG("m = %lx", (long )m);
|
---|
286 | DEBUG_ARG("m_len = %d", m->m_len);
|
---|
287 |
|
---|
288 | icmpstat.icps_received++;
|
---|
289 |
|
---|
290 | /*
|
---|
291 | * Locate icmp structure in mbuf, and check
|
---|
292 | * that its not corrupted and of at least minimum length.
|
---|
293 | */
|
---|
294 | if (icmplen < ICMP_MINLEN)
|
---|
295 | {
|
---|
296 | /* min 8 bytes payload */
|
---|
297 | icmpstat.icps_tooshort++;
|
---|
298 | freeit:
|
---|
299 | m_freem(pData, m);
|
---|
300 | goto end_error;
|
---|
301 | }
|
---|
302 |
|
---|
303 | m->m_len -= hlen;
|
---|
304 | m->m_data += hlen;
|
---|
305 | icp = mtod(m, struct icmp *);
|
---|
306 | if (cksum(m, icmplen))
|
---|
307 | {
|
---|
308 | icmpstat.icps_checksum++;
|
---|
309 | goto freeit;
|
---|
310 | }
|
---|
311 | m->m_len += hlen;
|
---|
312 | m->m_data -= hlen;
|
---|
313 |
|
---|
314 | /* icmpstat.icps_inhist[icp->icmp_type]++; */
|
---|
315 | /* code = icp->icmp_code; */
|
---|
316 |
|
---|
317 | DEBUG_ARG("icmp_type = %d", icp->icmp_type);
|
---|
318 | switch (icp->icmp_type)
|
---|
319 | {
|
---|
320 | case ICMP_ECHO:
|
---|
321 | #ifndef VBOX_WITH_SLIRP_ICMP
|
---|
322 | icp->icmp_type = ICMP_ECHOREPLY;
|
---|
323 | #endif /* !VBOX_WITH_SLIRP_ICMP */
|
---|
324 |
|
---|
325 | ip->ip_len += hlen; /* since ip_input subtracts this */
|
---|
326 | dst = ip->ip_dst.s_addr;
|
---|
327 | if (dst == alias_addr.s_addr)
|
---|
328 | {
|
---|
329 | #ifdef VBOX_WITH_SLIRP_ICMP
|
---|
330 | icp->icmp_type = ICMP_ECHOREPLY;
|
---|
331 | ip->ip_dst.s_addr = ip->ip_src.s_addr;
|
---|
332 | ip->ip_src.s_addr = dst;
|
---|
333 | #endif /* VBOX_WITH_SLIRP_ICMP */
|
---|
334 | icmp_reflect(pData, m);
|
---|
335 | }
|
---|
336 | else
|
---|
337 | {
|
---|
338 | struct socket *so;
|
---|
339 | struct sockaddr_in addr;
|
---|
340 | #ifndef VBOX_WITH_SLIRP_ICMP
|
---|
341 | if ((so = socreate()) == NULL)
|
---|
342 | goto freeit;
|
---|
343 | if (udp_attach(pData, so) == -1)
|
---|
344 | {
|
---|
345 | DEBUG_MISC((dfd,"icmp_input udp_attach errno = %d-%s\n",
|
---|
346 | errno,strerror(errno)));
|
---|
347 | sofree(pData, so);
|
---|
348 | m_free(pData, m);
|
---|
349 | goto end_error;
|
---|
350 | }
|
---|
351 | so->so_m = m;
|
---|
352 | so->so_faddr = ip->ip_dst;
|
---|
353 | so->so_fport = htons(7);
|
---|
354 | so->so_laddr = ip->ip_src;
|
---|
355 | so->so_lport = htons(9);
|
---|
356 | so->so_iptos = ip->ip_tos;
|
---|
357 | so->so_type = IPPROTO_ICMP;
|
---|
358 | so->so_state = SS_ISFCONNECTED;
|
---|
359 |
|
---|
360 | addr.sin_family = AF_INET;
|
---|
361 | if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
|
---|
362 | {
|
---|
363 | /* It's an alias */
|
---|
364 | switch (ntohl(so->so_faddr.s_addr) & ~pData->netmask)
|
---|
365 | {
|
---|
366 | case CTL_DNS:
|
---|
367 | addr.sin_addr = dns_addr;
|
---|
368 | break;
|
---|
369 | case CTL_ALIAS:
|
---|
370 | default:
|
---|
371 | addr.sin_addr = loopback_addr;
|
---|
372 | break;
|
---|
373 | }
|
---|
374 | }
|
---|
375 | else
|
---|
376 | addr.sin_addr = so->so_faddr;
|
---|
377 | addr.sin_port = so->so_fport;
|
---|
378 | if (sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
|
---|
379 | (struct sockaddr *)&addr, sizeof(addr)) == -1)
|
---|
380 | {
|
---|
381 | DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
|
---|
382 | errno,strerror(errno)));
|
---|
383 | icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
|
---|
384 | udp_detach(pData, so);
|
---|
385 | }
|
---|
386 | #else /* VBOX_WITH_SLIRP_ICMP */
|
---|
387 | # ifdef RT_OS_WINDOWS
|
---|
388 | IP_OPTION_INFORMATION ipopt;
|
---|
389 | int error;
|
---|
390 | # endif
|
---|
391 | addr.sin_family = AF_INET;
|
---|
392 | if ((ip->ip_dst.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
|
---|
393 | {
|
---|
394 | /* It's an alias */
|
---|
395 | switch (ntohl(ip->ip_dst.s_addr) & ~pData->netmask)
|
---|
396 | {
|
---|
397 | case CTL_DNS:
|
---|
398 | addr.sin_addr = dns_addr;
|
---|
399 | break;
|
---|
400 | case CTL_ALIAS:
|
---|
401 | default:
|
---|
402 | addr.sin_addr = loopback_addr;
|
---|
403 | break;
|
---|
404 | }
|
---|
405 | }
|
---|
406 | else
|
---|
407 | addr.sin_addr.s_addr = ip->ip_dst.s_addr;
|
---|
408 | # ifndef RT_OS_WINDOWS
|
---|
409 | if (pData->icmp_socket.s != -1)
|
---|
410 | {
|
---|
411 | icmp_attach(pData, m);
|
---|
412 | ttl = ip->ip_ttl;
|
---|
413 | LogRel(("NAT/ICMP: try to set TTL(%d)\n", ttl));
|
---|
414 | status = setsockopt(pData->icmp_socket.s, IPPROTO_IP, IP_TTL,
|
---|
415 | (void *)&ttl, sizeof(ttl));
|
---|
416 | if (status < 0)
|
---|
417 | LogRel(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n",
|
---|
418 | strerror(errno)));
|
---|
419 | if (sendto(pData->icmp_socket.s, icp, icmplen, 0,
|
---|
420 | (struct sockaddr *)&addr, sizeof(addr)) == -1)
|
---|
421 | {
|
---|
422 | DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
|
---|
423 | errno, strerror(errno)));
|
---|
424 | icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0, strerror(errno));
|
---|
425 | m_free(pData, m);
|
---|
426 | }
|
---|
427 | }
|
---|
428 | # else /* RT_OS_WINDOWS */
|
---|
429 | icmp_attach(pData, m);
|
---|
430 | pData->icmp_socket.so_laddr.s_addr = ip->ip_src.s_addr; /* XXX: hack*/
|
---|
431 | pData->icmp_socket.so_icmp_id = icp->icmp_id;
|
---|
432 | pData->icmp_socket.so_icmp_seq = icp->icmp_seq;
|
---|
433 | memset(&ipopt, 0, sizeof(IP_OPTION_INFORMATION));
|
---|
434 | ipopt.Ttl = ip->ip_ttl;
|
---|
435 | status = IcmpSendEcho2(pData->icmp_socket.sh, pData->phEvents[VBOX_ICMP_EVENT_INDEX],
|
---|
436 | NULL, NULL, addr.sin_addr.s_addr, icp->icmp_data,
|
---|
437 | icmplen - offsetof(struct icmp, icmp_data) , &ipopt,
|
---|
438 | pData->pvIcmpBuffer, pData->szIcmpBuffer, 1);
|
---|
439 | if (status == 0 && (error = GetLastError()) != ERROR_IO_PENDING)
|
---|
440 | {
|
---|
441 | error = GetLastError();
|
---|
442 | LogRel(("NAT: Error (%d) occurred while sending ICMP (", error));
|
---|
443 | switch (error)
|
---|
444 | {
|
---|
445 | case ERROR_INVALID_PARAMETER:
|
---|
446 | LogRel(("icmp_socket:%lx is invalid)\n", pData->icmp_socket.s));
|
---|
447 | break;
|
---|
448 | case ERROR_NOT_SUPPORTED:
|
---|
449 | LogRel(("operation is unsupported)\n"));
|
---|
450 | break;
|
---|
451 | case ERROR_NOT_ENOUGH_MEMORY:
|
---|
452 | LogRel(("OOM!!!)\n"));
|
---|
453 | break;
|
---|
454 | case IP_BUF_TOO_SMALL:
|
---|
455 | LogRel(("Buffer too small)\n"));
|
---|
456 | break;
|
---|
457 | default:
|
---|
458 | LogRel(("Other error!!!)\n"));
|
---|
459 | break;
|
---|
460 | }
|
---|
461 | }
|
---|
462 | # endif /* RT_OS_WINDOWS */
|
---|
463 | #endif /* VBOX_WITH_SLIRP_ICMP */
|
---|
464 | } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
|
---|
465 | break;
|
---|
466 | case ICMP_UNREACH:
|
---|
467 | /* XXX? report error? close socket? */
|
---|
468 | case ICMP_TIMXCEED:
|
---|
469 | case ICMP_PARAMPROB:
|
---|
470 | case ICMP_SOURCEQUENCH:
|
---|
471 | case ICMP_TSTAMP:
|
---|
472 | case ICMP_MASKREQ:
|
---|
473 | case ICMP_REDIRECT:
|
---|
474 | icmpstat.icps_notsupp++;
|
---|
475 | m_freem(pData, m);
|
---|
476 | break;
|
---|
477 |
|
---|
478 | default:
|
---|
479 | icmpstat.icps_badtype++;
|
---|
480 | m_freem(pData, m);
|
---|
481 | } /* switch */
|
---|
482 |
|
---|
483 | end_error:
|
---|
484 | /* m is m_free()'d xor put in a socket xor or given to ip_send */
|
---|
485 | ;
|
---|
486 | }
|
---|
487 |
|
---|
488 |
|
---|
489 | /*
|
---|
490 | * Send an ICMP message in response to a situation
|
---|
491 | *
|
---|
492 | * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
|
---|
493 | * MUST NOT change this header information.
|
---|
494 | * MUST NOT reply to a multicast/broadcast IP address.
|
---|
495 | * MUST NOT reply to a multicast/broadcast MAC address.
|
---|
496 | * MUST reply to only the first fragment.
|
---|
497 | */
|
---|
498 | /*
|
---|
499 | * Send ICMP_UNREACH back to the source regarding msrc.
|
---|
500 | * mbuf *msrc is used as a template, but is NOT m_free()'d.
|
---|
501 | * It is reported as the bad ip packet. The header should
|
---|
502 | * be fully correct and in host byte order.
|
---|
503 | * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
|
---|
504 | * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
|
---|
505 | */
|
---|
506 |
|
---|
507 | #define ICMP_MAXDATALEN (IP_MSS-28)
|
---|
508 | void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, const char *message)
|
---|
509 | {
|
---|
510 | unsigned hlen, shlen, s_ip_len;
|
---|
511 | register struct ip *ip;
|
---|
512 | register struct icmp *icp;
|
---|
513 | register struct mbuf *m;
|
---|
514 |
|
---|
515 | DEBUG_CALL("icmp_error");
|
---|
516 | DEBUG_ARG("msrc = %lx", (long )msrc);
|
---|
517 | DEBUG_ARG("msrc_len = %d", msrc->m_len);
|
---|
518 |
|
---|
519 | if (type!=ICMP_UNREACH && type!=ICMP_TIMXCEED)
|
---|
520 | goto end_error;
|
---|
521 |
|
---|
522 | /* check msrc */
|
---|
523 | if (!msrc)
|
---|
524 | goto end_error;
|
---|
525 |
|
---|
526 | ip = mtod(msrc, struct ip *);
|
---|
527 | #if DEBUG
|
---|
528 | {
|
---|
529 | char bufa[20], bufb[20];
|
---|
530 | strcpy(bufa, inet_ntoa(ip->ip_src));
|
---|
531 | strcpy(bufb, inet_ntoa(ip->ip_dst));
|
---|
532 | DEBUG_MISC((dfd, " %.16s to %.16s\n", bufa, bufb));
|
---|
533 | }
|
---|
534 | #endif
|
---|
535 | if (ip->ip_off & IP_OFFMASK)
|
---|
536 | goto end_error; /* Only reply to fragment 0 */
|
---|
537 |
|
---|
538 | shlen = ip->ip_hl << 2;
|
---|
539 | s_ip_len = ip->ip_len;
|
---|
540 | if (ip->ip_p == IPPROTO_ICMP)
|
---|
541 | {
|
---|
542 | icp = (struct icmp *)((char *)ip + shlen);
|
---|
543 | /*
|
---|
544 | * Assume any unknown ICMP type is an error. This isn't
|
---|
545 | * specified by the RFC, but think about it..
|
---|
546 | */
|
---|
547 | if (icp->icmp_type>18 || icmp_flush[icp->icmp_type])
|
---|
548 | goto end_error;
|
---|
549 | }
|
---|
550 |
|
---|
551 | /* make a copy */
|
---|
552 | if (!(m = m_get(pData)))
|
---|
553 | goto end_error; /* get mbuf */
|
---|
554 | {
|
---|
555 | int new_m_size;
|
---|
556 | new_m_size = sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
|
---|
557 | if (new_m_size>m->m_size)
|
---|
558 | m_inc(m, new_m_size);
|
---|
559 | }
|
---|
560 | memcpy(m->m_data, msrc->m_data, msrc->m_len);
|
---|
561 | m->m_len = msrc->m_len; /* copy msrc to m */
|
---|
562 |
|
---|
563 | /* make the header of the reply packet */
|
---|
564 | ip = mtod(m, struct ip *);
|
---|
565 | hlen = sizeof(struct ip ); /* no options in reply */
|
---|
566 |
|
---|
567 | /* fill in icmp */
|
---|
568 | m->m_data += hlen;
|
---|
569 | m->m_len -= hlen;
|
---|
570 |
|
---|
571 | icp = mtod(m, struct icmp *);
|
---|
572 |
|
---|
573 | if (minsize)
|
---|
574 | s_ip_len = shlen+ICMP_MINLEN; /* return header+8b only */
|
---|
575 | else if (s_ip_len > ICMP_MAXDATALEN) /* maximum size */
|
---|
576 | s_ip_len = ICMP_MAXDATALEN;
|
---|
577 |
|
---|
578 | m->m_len = ICMP_MINLEN + s_ip_len; /* 8 bytes ICMP header */
|
---|
579 |
|
---|
580 | /* min. size = 8+sizeof(struct ip)+8 */
|
---|
581 |
|
---|
582 | icp->icmp_type = type;
|
---|
583 | icp->icmp_code = code;
|
---|
584 | icp->icmp_id = 0;
|
---|
585 | icp->icmp_seq = 0;
|
---|
586 |
|
---|
587 | memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
|
---|
588 | HTONS(icp->icmp_ip.ip_len);
|
---|
589 | HTONS(icp->icmp_ip.ip_id);
|
---|
590 | HTONS(icp->icmp_ip.ip_off);
|
---|
591 |
|
---|
592 | #if DEBUG
|
---|
593 | if (message)
|
---|
594 | {
|
---|
595 | /* DEBUG : append message to ICMP packet */
|
---|
596 | int message_len;
|
---|
597 | char *cpnt;
|
---|
598 | message_len = strlen(message);
|
---|
599 | if (message_len > ICMP_MAXDATALEN)
|
---|
600 | message_len = ICMP_MAXDATALEN;
|
---|
601 | cpnt = (char *)m->m_data+m->m_len;
|
---|
602 | memcpy(cpnt, message, message_len);
|
---|
603 | m->m_len += message_len;
|
---|
604 | }
|
---|
605 | #endif
|
---|
606 |
|
---|
607 | icp->icmp_cksum = 0;
|
---|
608 | icp->icmp_cksum = cksum(m, m->m_len);
|
---|
609 |
|
---|
610 | m->m_data -= hlen;
|
---|
611 | m->m_len += hlen;
|
---|
612 |
|
---|
613 | /* fill in ip */
|
---|
614 | ip->ip_hl = hlen >> 2;
|
---|
615 | ip->ip_len = m->m_len;
|
---|
616 |
|
---|
617 | ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
|
---|
618 |
|
---|
619 | ip->ip_ttl = MAXTTL;
|
---|
620 | ip->ip_p = IPPROTO_ICMP;
|
---|
621 | ip->ip_dst = ip->ip_src; /* ip adresses */
|
---|
622 | ip->ip_src = alias_addr;
|
---|
623 |
|
---|
624 | (void ) ip_output(pData, (struct socket *)NULL, m);
|
---|
625 |
|
---|
626 | icmpstat.icps_reflect++;
|
---|
627 |
|
---|
628 | end_error:
|
---|
629 | ;
|
---|
630 | }
|
---|
631 | #undef ICMP_MAXDATALEN
|
---|
632 |
|
---|
633 | /*
|
---|
634 | * Reflect the ip packet back to the source
|
---|
635 | */
|
---|
636 | void
|
---|
637 | icmp_reflect(PNATState pData, struct mbuf *m)
|
---|
638 | {
|
---|
639 | register struct ip *ip = mtod(m, struct ip *);
|
---|
640 | int hlen = ip->ip_hl << 2;
|
---|
641 | int optlen = hlen - sizeof(struct ip );
|
---|
642 | register struct icmp *icp;
|
---|
643 |
|
---|
644 | /*
|
---|
645 | * Send an icmp packet back to the ip level,
|
---|
646 | * after supplying a checksum.
|
---|
647 | */
|
---|
648 | m->m_data += hlen;
|
---|
649 | m->m_len -= hlen;
|
---|
650 | icp = mtod(m, struct icmp *);
|
---|
651 |
|
---|
652 | icp->icmp_cksum = 0;
|
---|
653 | icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
|
---|
654 |
|
---|
655 | m->m_data -= hlen;
|
---|
656 | m->m_len += hlen;
|
---|
657 |
|
---|
658 | #ifndef VBOX_WITH_SLIRP_ICMP
|
---|
659 | /* fill in ip */
|
---|
660 | if (optlen > 0)
|
---|
661 | {
|
---|
662 | /*
|
---|
663 | * Strip out original options by copying rest of first
|
---|
664 | * mbuf's data back, and adjust the IP length.
|
---|
665 | */
|
---|
666 | memmove((caddr_t)(ip + 1), (caddr_t)ip + hlen,
|
---|
667 | (unsigned )(m->m_len - hlen));
|
---|
668 | hlen -= optlen;
|
---|
669 | ip->ip_hl = hlen >> 2;
|
---|
670 | ip->ip_len -= optlen;
|
---|
671 | m->m_len -= optlen;
|
---|
672 | }
|
---|
673 | ip->ip_ttl = MAXTTL;
|
---|
674 | {
|
---|
675 | /* swap */
|
---|
676 | struct in_addr icmp_dst;
|
---|
677 | icmp_dst = ip->ip_dst;
|
---|
678 | ip->ip_dst = ip->ip_src;
|
---|
679 | ip->ip_src = icmp_dst;
|
---|
680 | }
|
---|
681 | #endif /* !VBOX_WITH_SLIRP_ICMP */
|
---|
682 |
|
---|
683 | (void ) ip_output(pData, (struct socket *)NULL, m);
|
---|
684 |
|
---|
685 | icmpstat.icps_reflect++;
|
---|
686 | }
|
---|