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