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