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, *so;
|
---|
149 | struct in_addr laddr, faddr;
|
---|
150 | u_int lport, fport;
|
---|
151 |
|
---|
152 | laddr.s_addr = ~0;
|
---|
153 | faddr.s_addr = ~0;
|
---|
154 |
|
---|
155 | lport = ~0;
|
---|
156 | fport = ~0;
|
---|
157 |
|
---|
158 |
|
---|
159 | LogRel(("%s: processing (proto:%d)\n", __FUNCTION__, ip->ip_p));
|
---|
160 | switch (ip->ip_p)
|
---|
161 | {
|
---|
162 | case IPPROTO_ICMP:
|
---|
163 | icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
|
---|
164 | LIST_FOREACH(icm, &pData->icmp_msg_head, im_list)
|
---|
165 | {
|
---|
166 | m0 = icm->im_m;
|
---|
167 | ip0 = mtod(m0, struct ip *);
|
---|
168 | AssertRelease(ip0->ip_p == IPPROTO_ICMP);
|
---|
169 | icp0 = (struct icmp *)((char *)ip0 + (ip0->ip_hl << 2));
|
---|
170 | if ( ( (icp->icmp_type != ICMP_ECHO && ip->ip_src.s_addr == ip0->ip_dst.s_addr)
|
---|
171 | || (icp->icmp_type == ICMP_ECHO && ip->ip_dst.s_addr == ip0->ip_dst.s_addr))
|
---|
172 | && icp->icmp_id == icp0->icmp_id
|
---|
173 | && icp->icmp_seq == icp->icmp_seq)
|
---|
174 | {
|
---|
175 | found = 1;
|
---|
176 | break;
|
---|
177 | }
|
---|
178 | LogRel(("Have found nothing\n"));
|
---|
179 | }
|
---|
180 | break;
|
---|
181 |
|
---|
182 | /*
|
---|
183 | * for TCP and UDP logic little bit reverted, we try to find the HOST socket
|
---|
184 | * from which the IP package has been sent.
|
---|
185 | */
|
---|
186 | case IPPROTO_UDP:
|
---|
187 | head_socket = &udb;
|
---|
188 | udp = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
|
---|
189 | faddr.s_addr = ip->ip_dst.s_addr;
|
---|
190 | fport = udp->uh_dport;
|
---|
191 | laddr.s_addr = ip->ip_src.s_addr;
|
---|
192 | lport = udp->uh_sport;
|
---|
193 | /* fall through */
|
---|
194 |
|
---|
195 | case IPPROTO_TCP:
|
---|
196 | if (head_socket == NULL)
|
---|
197 | {
|
---|
198 | tcp = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
|
---|
199 | head_socket = &tcb; /* head_socket could be initialized with udb*/
|
---|
200 | faddr.s_addr = ip->ip_dst.s_addr;
|
---|
201 | fport = tcp->th_dport;
|
---|
202 | laddr.s_addr = ip->ip_src.s_addr;
|
---|
203 | lport = tcp->th_sport;
|
---|
204 | }
|
---|
205 | for (so = head_socket->so_next; so != head_socket; so = so->so_next)
|
---|
206 | {
|
---|
207 | /* Should be reaplaced by hash here */
|
---|
208 | LogRel(("trying:%R[natsock] against %R[IP4]:%d lport=%d hlport=%d\n", so, &faddr, fport, lport, so->so_hlport));
|
---|
209 | if ( so->so_faddr.s_addr == faddr.s_addr
|
---|
210 | && so->so_fport == fport
|
---|
211 | #if 0
|
---|
212 | && so->so_hladdr.s_addr == laddr.s_addr
|
---|
213 | #endif
|
---|
214 | && so->so_hlport == lport)
|
---|
215 | {
|
---|
216 | icm = malloc(sizeof(struct icmp_msg));
|
---|
217 | icm->im_m = so->so_m;
|
---|
218 | found = 1;
|
---|
219 | LogRel(("hit:%R[natsock]\n", so));
|
---|
220 | /*XXX: this storage not very long,
|
---|
221 | * better add flag if it should removed from lis
|
---|
222 | */
|
---|
223 | LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
|
---|
224 | break;
|
---|
225 | }
|
---|
226 | }
|
---|
227 | break;
|
---|
228 |
|
---|
229 | default:
|
---|
230 | LogRel(("%s:ICMP: unsupported protocol(%d)\n", __FUNCTION__, ip->ip_p));
|
---|
231 | }
|
---|
232 | if (found == 1)
|
---|
233 | return icm;
|
---|
234 |
|
---|
235 | return NULL;
|
---|
236 | }
|
---|
237 |
|
---|
238 | static int
|
---|
239 | icmp_attach(PNATState pData, struct mbuf *m)
|
---|
240 | {
|
---|
241 | struct icmp_msg *icm;
|
---|
242 | struct ip *ip;
|
---|
243 | ip = mtod(m, struct ip *);
|
---|
244 | Assert(ip->ip_p == IPPROTO_ICMP);
|
---|
245 | icm = malloc(sizeof(struct icmp_msg));
|
---|
246 | icm->im_m = m;
|
---|
247 | LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
|
---|
248 | return 0;
|
---|
249 | }
|
---|
250 | #endif /* VBOX_WITH_SLIRP_ICMP */
|
---|
251 |
|
---|
252 | /*
|
---|
253 | * Process a received ICMP message.
|
---|
254 | */
|
---|
255 | void
|
---|
256 | icmp_input(PNATState pData, struct mbuf *m, int hlen)
|
---|
257 | {
|
---|
258 | register struct icmp *icp;
|
---|
259 | register struct ip *ip = mtod(m, struct ip *);
|
---|
260 | int icmplen = ip->ip_len;
|
---|
261 | int status;
|
---|
262 | uint32_t dst;
|
---|
263 |
|
---|
264 | /* int code; */
|
---|
265 |
|
---|
266 | DEBUG_CALL("icmp_input");
|
---|
267 | DEBUG_ARG("m = %lx", (long )m);
|
---|
268 | DEBUG_ARG("m_len = %d", m->m_len);
|
---|
269 |
|
---|
270 | icmpstat.icps_received++;
|
---|
271 |
|
---|
272 | /*
|
---|
273 | * Locate icmp structure in mbuf, and check
|
---|
274 | * that its not corrupted and of at least minimum length.
|
---|
275 | */
|
---|
276 | if (icmplen < ICMP_MINLEN)
|
---|
277 | {
|
---|
278 | /* min 8 bytes payload */
|
---|
279 | icmpstat.icps_tooshort++;
|
---|
280 | freeit:
|
---|
281 | m_freem(pData, m);
|
---|
282 | goto end_error;
|
---|
283 | }
|
---|
284 |
|
---|
285 | m->m_len -= hlen;
|
---|
286 | m->m_data += hlen;
|
---|
287 | icp = mtod(m, struct icmp *);
|
---|
288 | if (cksum(m, icmplen))
|
---|
289 | {
|
---|
290 | icmpstat.icps_checksum++;
|
---|
291 | goto freeit;
|
---|
292 | }
|
---|
293 | m->m_len += hlen;
|
---|
294 | m->m_data -= hlen;
|
---|
295 |
|
---|
296 | /* icmpstat.icps_inhist[icp->icmp_type]++; */
|
---|
297 | /* code = icp->icmp_code; */
|
---|
298 |
|
---|
299 | DEBUG_ARG("icmp_type = %d", icp->icmp_type);
|
---|
300 | switch (icp->icmp_type)
|
---|
301 | {
|
---|
302 | case ICMP_ECHO:
|
---|
303 | #ifndef VBOX_WITH_SLIRP_ICMP
|
---|
304 | icp->icmp_type = ICMP_ECHOREPLY;
|
---|
305 | #endif /* !VBOX_WITH_SLIRP_ICMP */
|
---|
306 |
|
---|
307 | ip->ip_len += hlen; /* since ip_input subtracts this */
|
---|
308 | dst = ip->ip_dst.s_addr;
|
---|
309 | if (dst == alias_addr.s_addr)
|
---|
310 | {
|
---|
311 | #ifdef VBOX_WITH_SLIRP_ICMP
|
---|
312 | icp->icmp_type = ICMP_ECHOREPLY;
|
---|
313 | ip->ip_dst.s_addr = ip->ip_src.s_addr;
|
---|
314 | ip->ip_src.s_addr = dst;
|
---|
315 | #endif /* VBOX_WITH_SLIRP_ICMP */
|
---|
316 | icmp_reflect(pData, m);
|
---|
317 | }
|
---|
318 | else
|
---|
319 | {
|
---|
320 | struct socket *so;
|
---|
321 | struct sockaddr_in addr;
|
---|
322 | #ifndef VBOX_WITH_SLIRP_ICMP
|
---|
323 | if ((so = socreate()) == NULL)
|
---|
324 | goto freeit;
|
---|
325 | if (udp_attach(pData, so) == -1)
|
---|
326 | {
|
---|
327 | DEBUG_MISC((dfd,"icmp_input udp_attach errno = %d-%s\n",
|
---|
328 | errno,strerror(errno)));
|
---|
329 | sofree(pData, so);
|
---|
330 | m_free(pData, m);
|
---|
331 | goto end_error;
|
---|
332 | }
|
---|
333 | so->so_m = m;
|
---|
334 | so->so_faddr = ip->ip_dst;
|
---|
335 | so->so_fport = htons(7);
|
---|
336 | so->so_laddr = ip->ip_src;
|
---|
337 | so->so_lport = htons(9);
|
---|
338 | so->so_iptos = ip->ip_tos;
|
---|
339 | so->so_type = IPPROTO_ICMP;
|
---|
340 | so->so_state = SS_ISFCONNECTED;
|
---|
341 |
|
---|
342 | addr.sin_family = AF_INET;
|
---|
343 | if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
|
---|
344 | {
|
---|
345 | /* It's an alias */
|
---|
346 | switch (ntohl(so->so_faddr.s_addr) & ~pData->netmask)
|
---|
347 | {
|
---|
348 | case CTL_DNS:
|
---|
349 | addr.sin_addr = dns_addr;
|
---|
350 | break;
|
---|
351 | case CTL_ALIAS:
|
---|
352 | default:
|
---|
353 | addr.sin_addr = loopback_addr;
|
---|
354 | break;
|
---|
355 | }
|
---|
356 | }
|
---|
357 | else
|
---|
358 | addr.sin_addr = so->so_faddr;
|
---|
359 | addr.sin_port = so->so_fport;
|
---|
360 | if (sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
|
---|
361 | (struct sockaddr *)&addr, sizeof(addr)) == -1)
|
---|
362 | {
|
---|
363 | DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
|
---|
364 | errno,strerror(errno)));
|
---|
365 | icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
|
---|
366 | udp_detach(pData, so);
|
---|
367 | }
|
---|
368 | #else /* VBOX_WITH_SLIRP_ICMP */
|
---|
369 | # ifdef RT_OS_WINDOWS
|
---|
370 | IP_OPTION_INFORMATION ipopt;
|
---|
371 | int error;
|
---|
372 | # endif
|
---|
373 | addr.sin_family = AF_INET;
|
---|
374 | if ((ip->ip_dst.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
|
---|
375 | {
|
---|
376 | /* It's an alias */
|
---|
377 | switch(ntohl(ip->ip_dst.s_addr) & ~pData->netmask)
|
---|
378 | {
|
---|
379 | case CTL_DNS:
|
---|
380 | addr.sin_addr = dns_addr;
|
---|
381 | break;
|
---|
382 | case CTL_ALIAS:
|
---|
383 | default:
|
---|
384 | addr.sin_addr = loopback_addr;
|
---|
385 | break;
|
---|
386 | }
|
---|
387 | }
|
---|
388 | else
|
---|
389 | addr.sin_addr.s_addr = ip->ip_dst.s_addr;
|
---|
390 | icmp_attach(pData, m);
|
---|
391 | /* Send the packet */
|
---|
392 | # ifndef RT_OS_WINDOWS
|
---|
393 | LogRel(("NAT/ICMP: try to set TTL(%d)\n", ip->ip_ttl));
|
---|
394 | status = setsockopt(pData->icmp_socket.s, IPPROTO_IP, IP_TTL, (void *)&ip->ip_ttl, sizeof(ip->ip_ttl));
|
---|
395 | if (status < 0)
|
---|
396 | {
|
---|
397 | LogRel(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n", strerror(errno)));
|
---|
398 | }
|
---|
399 | if (sendto(pData->icmp_socket.s, icp, icmplen, 0,
|
---|
400 | (struct sockaddr *)&addr, sizeof(addr)) == -1)
|
---|
401 | {
|
---|
402 | DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
|
---|
403 | errno,strerror(errno)));
|
---|
404 | icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
|
---|
405 | m_free(pData, m);
|
---|
406 | }
|
---|
407 | # else /* RT_OS_WINDOWS */
|
---|
408 | pData->icmp_socket.so_laddr.s_addr = ip->ip_src.s_addr; /* XXX: hack*/
|
---|
409 | pData->icmp_socket.so_icmp_id = icp->icmp_id;
|
---|
410 | pData->icmp_socket.so_icmp_seq = icp->icmp_seq;
|
---|
411 | memset(&ipopt, 0, sizeof(IP_OPTION_INFORMATION));
|
---|
412 | ipopt.Ttl = ip->ip_ttl;
|
---|
413 | status = IcmpSendEcho2(pData->icmp_socket.sh, pData->phEvents[VBOX_ICMP_EVENT_INDEX],
|
---|
414 | NULL, NULL, addr.sin_addr.s_addr, icp->icmp_data,
|
---|
415 | icmplen - offsetof(struct icmp, icmp_data) , &ipopt,
|
---|
416 | pData->pvIcmpBuffer, pData->szIcmpBuffer, 1);
|
---|
417 | if (status == 0 && (error = GetLastError()) != ERROR_IO_PENDING)
|
---|
418 | {
|
---|
419 | error = GetLastError();
|
---|
420 | LogRel(("NAT: Error (%d) occurred while sending ICMP (", error));
|
---|
421 | switch (error)
|
---|
422 | {
|
---|
423 | case ERROR_INVALID_PARAMETER:
|
---|
424 | LogRel(("icmp_socket:%lx is invalid)\n", pData->icmp_socket.s));
|
---|
425 | break;
|
---|
426 | case ERROR_NOT_SUPPORTED:
|
---|
427 | LogRel(("operation is unsupported)\n"));
|
---|
428 | break;
|
---|
429 | case ERROR_NOT_ENOUGH_MEMORY:
|
---|
430 | LogRel(("OOM!!!)\n"));
|
---|
431 | break;
|
---|
432 | case IP_BUF_TOO_SMALL:
|
---|
433 | LogRel(("Buffer too small)\n"));
|
---|
434 | break;
|
---|
435 | default:
|
---|
436 | LogRel(("Other error!!!)\n"));
|
---|
437 | break;
|
---|
438 | }
|
---|
439 | }
|
---|
440 | # endif /* RT_OS_WINDOWS */
|
---|
441 | #endif /* VBOX_WITH_SLIRP_ICMP */
|
---|
442 | } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
|
---|
443 | break;
|
---|
444 | case ICMP_UNREACH:
|
---|
445 | /* XXX? report error? close socket? */
|
---|
446 | case ICMP_TIMXCEED:
|
---|
447 | case ICMP_PARAMPROB:
|
---|
448 | case ICMP_SOURCEQUENCH:
|
---|
449 | case ICMP_TSTAMP:
|
---|
450 | case ICMP_MASKREQ:
|
---|
451 | case ICMP_REDIRECT:
|
---|
452 | icmpstat.icps_notsupp++;
|
---|
453 | m_freem(pData, m);
|
---|
454 | break;
|
---|
455 |
|
---|
456 | default:
|
---|
457 | icmpstat.icps_badtype++;
|
---|
458 | m_freem(pData, m);
|
---|
459 | } /* switch */
|
---|
460 |
|
---|
461 | end_error:
|
---|
462 | /* m is m_free()'d xor put in a socket xor or given to ip_send */
|
---|
463 | ;
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 | /*
|
---|
468 | * Send an ICMP message in response to a situation
|
---|
469 | *
|
---|
470 | * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
|
---|
471 | * MUST NOT change this header information.
|
---|
472 | * MUST NOT reply to a multicast/broadcast IP address.
|
---|
473 | * MUST NOT reply to a multicast/broadcast MAC address.
|
---|
474 | * MUST reply to only the first fragment.
|
---|
475 | */
|
---|
476 | /*
|
---|
477 | * Send ICMP_UNREACH back to the source regarding msrc.
|
---|
478 | * mbuf *msrc is used as a template, but is NOT m_free()'d.
|
---|
479 | * It is reported as the bad ip packet. The header should
|
---|
480 | * be fully correct and in host byte order.
|
---|
481 | * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
|
---|
482 | * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
|
---|
483 | */
|
---|
484 |
|
---|
485 | #define ICMP_MAXDATALEN (IP_MSS-28)
|
---|
486 | void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, const char *message)
|
---|
487 | {
|
---|
488 | unsigned hlen, shlen, s_ip_len;
|
---|
489 | register struct ip *ip;
|
---|
490 | register struct icmp *icp;
|
---|
491 | register struct mbuf *m;
|
---|
492 |
|
---|
493 | DEBUG_CALL("icmp_error");
|
---|
494 | DEBUG_ARG("msrc = %lx", (long )msrc);
|
---|
495 | DEBUG_ARG("msrc_len = %d", msrc->m_len);
|
---|
496 |
|
---|
497 | if (type!=ICMP_UNREACH && type!=ICMP_TIMXCEED)
|
---|
498 | goto end_error;
|
---|
499 |
|
---|
500 | /* check msrc */
|
---|
501 | if (!msrc)
|
---|
502 | goto end_error;
|
---|
503 |
|
---|
504 | ip = mtod(msrc, struct ip *);
|
---|
505 | #if DEBUG
|
---|
506 | {
|
---|
507 | char bufa[20], bufb[20];
|
---|
508 | strcpy(bufa, inet_ntoa(ip->ip_src));
|
---|
509 | strcpy(bufb, inet_ntoa(ip->ip_dst));
|
---|
510 | DEBUG_MISC((dfd, " %.16s to %.16s\n", bufa, bufb));
|
---|
511 | }
|
---|
512 | #endif
|
---|
513 | if (ip->ip_off & IP_OFFMASK)
|
---|
514 | goto end_error; /* Only reply to fragment 0 */
|
---|
515 |
|
---|
516 | shlen = ip->ip_hl << 2;
|
---|
517 | s_ip_len = ip->ip_len;
|
---|
518 | if (ip->ip_p == IPPROTO_ICMP)
|
---|
519 | {
|
---|
520 | icp = (struct icmp *)((char *)ip + shlen);
|
---|
521 | /*
|
---|
522 | * Assume any unknown ICMP type is an error. This isn't
|
---|
523 | * specified by the RFC, but think about it..
|
---|
524 | */
|
---|
525 | if (icp->icmp_type>18 || icmp_flush[icp->icmp_type])
|
---|
526 | goto end_error;
|
---|
527 | }
|
---|
528 |
|
---|
529 | /* make a copy */
|
---|
530 | if (!(m = m_get(pData)))
|
---|
531 | goto end_error; /* get mbuf */
|
---|
532 | {
|
---|
533 | int new_m_size;
|
---|
534 | new_m_size = sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
|
---|
535 | if (new_m_size>m->m_size)
|
---|
536 | m_inc(m, new_m_size);
|
---|
537 | }
|
---|
538 | memcpy(m->m_data, msrc->m_data, msrc->m_len);
|
---|
539 | m->m_len = msrc->m_len; /* copy msrc to m */
|
---|
540 |
|
---|
541 | /* make the header of the reply packet */
|
---|
542 | ip = mtod(m, struct ip *);
|
---|
543 | hlen = sizeof(struct ip ); /* no options in reply */
|
---|
544 |
|
---|
545 | /* fill in icmp */
|
---|
546 | m->m_data += hlen;
|
---|
547 | m->m_len -= hlen;
|
---|
548 |
|
---|
549 | icp = mtod(m, struct icmp *);
|
---|
550 |
|
---|
551 | if (minsize)
|
---|
552 | s_ip_len = shlen+ICMP_MINLEN; /* return header+8b only */
|
---|
553 | else if (s_ip_len > ICMP_MAXDATALEN) /* maximum size */
|
---|
554 | s_ip_len = ICMP_MAXDATALEN;
|
---|
555 |
|
---|
556 | m->m_len = ICMP_MINLEN + s_ip_len; /* 8 bytes ICMP header */
|
---|
557 |
|
---|
558 | /* min. size = 8+sizeof(struct ip)+8 */
|
---|
559 |
|
---|
560 | icp->icmp_type = type;
|
---|
561 | icp->icmp_code = code;
|
---|
562 | icp->icmp_id = 0;
|
---|
563 | icp->icmp_seq = 0;
|
---|
564 |
|
---|
565 | memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
|
---|
566 | HTONS(icp->icmp_ip.ip_len);
|
---|
567 | HTONS(icp->icmp_ip.ip_id);
|
---|
568 | HTONS(icp->icmp_ip.ip_off);
|
---|
569 |
|
---|
570 | #if DEBUG
|
---|
571 | if (message)
|
---|
572 | {
|
---|
573 | /* DEBUG : append message to ICMP packet */
|
---|
574 | int message_len;
|
---|
575 | char *cpnt;
|
---|
576 | message_len = strlen(message);
|
---|
577 | if (message_len > ICMP_MAXDATALEN)
|
---|
578 | message_len = ICMP_MAXDATALEN;
|
---|
579 | cpnt = (char *)m->m_data+m->m_len;
|
---|
580 | memcpy(cpnt, message, message_len);
|
---|
581 | m->m_len += message_len;
|
---|
582 | }
|
---|
583 | #endif
|
---|
584 |
|
---|
585 | icp->icmp_cksum = 0;
|
---|
586 | icp->icmp_cksum = cksum(m, m->m_len);
|
---|
587 |
|
---|
588 | m->m_data -= hlen;
|
---|
589 | m->m_len += hlen;
|
---|
590 |
|
---|
591 | /* fill in ip */
|
---|
592 | ip->ip_hl = hlen >> 2;
|
---|
593 | ip->ip_len = m->m_len;
|
---|
594 |
|
---|
595 | ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
|
---|
596 |
|
---|
597 | ip->ip_ttl = MAXTTL;
|
---|
598 | ip->ip_p = IPPROTO_ICMP;
|
---|
599 | ip->ip_dst = ip->ip_src; /* ip adresses */
|
---|
600 | ip->ip_src = alias_addr;
|
---|
601 |
|
---|
602 | (void ) ip_output(pData, (struct socket *)NULL, m);
|
---|
603 |
|
---|
604 | icmpstat.icps_reflect++;
|
---|
605 |
|
---|
606 | end_error:
|
---|
607 | ;
|
---|
608 | }
|
---|
609 | #undef ICMP_MAXDATALEN
|
---|
610 |
|
---|
611 | /*
|
---|
612 | * Reflect the ip packet back to the source
|
---|
613 | */
|
---|
614 | void
|
---|
615 | icmp_reflect(PNATState pData, struct mbuf *m)
|
---|
616 | {
|
---|
617 | register struct ip *ip = mtod(m, struct ip *);
|
---|
618 | int hlen = ip->ip_hl << 2;
|
---|
619 | int optlen = hlen - sizeof(struct ip );
|
---|
620 | register struct icmp *icp;
|
---|
621 |
|
---|
622 | /*
|
---|
623 | * Send an icmp packet back to the ip level,
|
---|
624 | * after supplying a checksum.
|
---|
625 | */
|
---|
626 | m->m_data += hlen;
|
---|
627 | m->m_len -= hlen;
|
---|
628 | icp = mtod(m, struct icmp *);
|
---|
629 |
|
---|
630 | icp->icmp_cksum = 0;
|
---|
631 | icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
|
---|
632 |
|
---|
633 | m->m_data -= hlen;
|
---|
634 | m->m_len += hlen;
|
---|
635 |
|
---|
636 | #ifndef VBOX_WITH_SLIRP_ICMP
|
---|
637 | /* fill in ip */
|
---|
638 | if (optlen > 0)
|
---|
639 | {
|
---|
640 | /*
|
---|
641 | * Strip out original options by copying rest of first
|
---|
642 | * mbuf's data back, and adjust the IP length.
|
---|
643 | */
|
---|
644 | memmove((caddr_t)(ip + 1), (caddr_t)ip + hlen,
|
---|
645 | (unsigned )(m->m_len - hlen));
|
---|
646 | hlen -= optlen;
|
---|
647 | ip->ip_hl = hlen >> 2;
|
---|
648 | ip->ip_len -= optlen;
|
---|
649 | m->m_len -= optlen;
|
---|
650 | }
|
---|
651 | ip->ip_ttl = MAXTTL;
|
---|
652 | {
|
---|
653 | /* swap */
|
---|
654 | struct in_addr icmp_dst;
|
---|
655 | icmp_dst = ip->ip_dst;
|
---|
656 | ip->ip_dst = ip->ip_src;
|
---|
657 | ip->ip_src = icmp_dst;
|
---|
658 | }
|
---|
659 | #endif /* !VBOX_WITH_SLIRP_ICMP */
|
---|
660 |
|
---|
661 | (void ) ip_output(pData, (struct socket *)NULL, m);
|
---|
662 |
|
---|
663 | icmpstat.icps_reflect++;
|
---|
664 | }
|
---|