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