1 | /*
|
---|
2 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
3 | *
|
---|
4 | * Please read the file COPYRIGHT for the
|
---|
5 | * terms and conditions of the copyright.
|
---|
6 | */
|
---|
7 |
|
---|
8 | #define WANT_SYS_IOCTL_H
|
---|
9 | #include <slirp.h>
|
---|
10 | #include "ip_icmp.h"
|
---|
11 | #include "main.h"
|
---|
12 | #ifdef __sun__
|
---|
13 | #include <sys/filio.h>
|
---|
14 | #endif
|
---|
15 | #if defined (RT_OS_WINDOWS)
|
---|
16 | #include <iphlpapi.h>
|
---|
17 | #include <icmpapi.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 |
|
---|
21 | static void send_icmp_to_guest(PNATState, char *, size_t, struct socket *, const struct sockaddr_in *);
|
---|
22 | #ifdef RT_OS_WINDOWS
|
---|
23 | static void sorecvfrom_icmp_win(PNATState, struct socket *);
|
---|
24 | #else /* RT_OS_WINDOWS */
|
---|
25 | static void sorecvfrom_icmp_unix(PNATState, struct socket *);
|
---|
26 | #endif /* !RT_OS_WINDOWS */
|
---|
27 |
|
---|
28 | void
|
---|
29 | so_init()
|
---|
30 | {
|
---|
31 | }
|
---|
32 |
|
---|
33 |
|
---|
34 | struct socket *
|
---|
35 | solookup(struct socket *head, struct in_addr laddr,
|
---|
36 | u_int lport, struct in_addr faddr, u_int fport)
|
---|
37 | {
|
---|
38 | struct socket *so;
|
---|
39 |
|
---|
40 | for (so = head->so_next; so != head; so = so->so_next)
|
---|
41 | {
|
---|
42 | if ( so->so_lport == lport
|
---|
43 | && so->so_laddr.s_addr == laddr.s_addr
|
---|
44 | && so->so_faddr.s_addr == faddr.s_addr
|
---|
45 | && so->so_fport == fport)
|
---|
46 | return so;
|
---|
47 | }
|
---|
48 |
|
---|
49 | return (struct socket *)NULL;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * Create a new socket, initialise the fields
|
---|
54 | * It is the responsibility of the caller to
|
---|
55 | * insque() it into the correct linked-list
|
---|
56 | */
|
---|
57 | struct socket *
|
---|
58 | socreate()
|
---|
59 | {
|
---|
60 | struct socket *so;
|
---|
61 |
|
---|
62 | so = (struct socket *)RTMemAllocZ(sizeof(struct socket));
|
---|
63 | if(so)
|
---|
64 | {
|
---|
65 | memset(so, 0, sizeof(struct socket));
|
---|
66 | so->so_state = SS_NOFDREF;
|
---|
67 | so->s = -1;
|
---|
68 | }
|
---|
69 | return so;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * remque and free a socket, clobber cache
|
---|
74 | * VBOX_WITH_SLIRP_MT: before sofree queue should be locked, because
|
---|
75 | * in sofree we don't know from which queue item beeing removed.
|
---|
76 | */
|
---|
77 | void
|
---|
78 | sofree(PNATState pData, struct socket *so)
|
---|
79 | {
|
---|
80 | struct socket *so_prev = NULL;
|
---|
81 | if (so == tcp_last_so)
|
---|
82 | tcp_last_so = &tcb;
|
---|
83 | else if (so == udp_last_so)
|
---|
84 | udp_last_so = &udb;
|
---|
85 |
|
---|
86 | /* check if mbuf haven't been already freed */
|
---|
87 | if (so->so_m != NULL)
|
---|
88 | m_free(pData, so->so_m);
|
---|
89 | if(so->so_next && so->so_prev)
|
---|
90 | remque(pData, so); /* crashes if so is not in a queue */
|
---|
91 | so->so_state = SS_NOFDREF; /* for debugging purposes */
|
---|
92 | SOCKET_UNLOCK(so);
|
---|
93 | SOCKET_LOCK_DESTROY(so);
|
---|
94 |
|
---|
95 | RTMemFree(so);
|
---|
96 | }
|
---|
97 |
|
---|
98 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
99 | void
|
---|
100 | soread_queue(PNATState pData, struct socket *so, int fCloseIfNothingRead, int *ret)
|
---|
101 | {
|
---|
102 | *ret = soread(pData, so, fCloseIfNothingRead);
|
---|
103 | }
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Read from so's socket into sb_snd, updating all relevant sbuf fields
|
---|
108 | * NOTE: This will only be called if it is select()ed for reading, so
|
---|
109 | * a read() of 0 (or less) means it's disconnected
|
---|
110 | */
|
---|
111 | int
|
---|
112 | soread(PNATState pData, struct socket *so, int fCloseIfNothingRead)
|
---|
113 | {
|
---|
114 | int n, nn, lss, total;
|
---|
115 | struct sbuf *sb = &so->so_snd;
|
---|
116 | size_t len = sb->sb_datalen - sb->sb_cc;
|
---|
117 | struct iovec iov[2];
|
---|
118 | int mss = so->so_tcpcb->t_maxseg;
|
---|
119 | Log2(("%s:%d soread before lock\n", __FUNCTION__, __LINE__));
|
---|
120 | QSOCKET_LOCK(tcb);
|
---|
121 | SOCKET_LOCK(so);
|
---|
122 | QSOCKET_UNLOCK(tcb);
|
---|
123 | Log2(("%s:%d soread before lock\n",__FUNCTION__, __LINE__));
|
---|
124 |
|
---|
125 | DEBUG_CALL("soread");
|
---|
126 | DEBUG_ARG("so = %lx", (long )so);
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * No need to check if there's enough room to read.
|
---|
130 | * soread wouldn't have been called if there weren't
|
---|
131 | */
|
---|
132 |
|
---|
133 | len = sb->sb_datalen - sb->sb_cc;
|
---|
134 |
|
---|
135 | iov[0].iov_base = sb->sb_wptr;
|
---|
136 | iov[1].iov_base = 0;
|
---|
137 | iov[1].iov_len = 0;
|
---|
138 | if (sb->sb_wptr < sb->sb_rptr)
|
---|
139 | {
|
---|
140 | iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
|
---|
141 | /* Should never succeed, but... */
|
---|
142 | if (iov[0].iov_len > len)
|
---|
143 | iov[0].iov_len = len;
|
---|
144 | if (iov[0].iov_len > mss)
|
---|
145 | iov[0].iov_len -= iov[0].iov_len%mss;
|
---|
146 | n = 1;
|
---|
147 | }
|
---|
148 | else
|
---|
149 | {
|
---|
150 | iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
|
---|
151 | /* Should never succeed, but... */
|
---|
152 | if (iov[0].iov_len > len)
|
---|
153 | iov[0].iov_len = len;
|
---|
154 | len -= iov[0].iov_len;
|
---|
155 | if (len)
|
---|
156 | {
|
---|
157 | iov[1].iov_base = sb->sb_data;
|
---|
158 | iov[1].iov_len = sb->sb_rptr - sb->sb_data;
|
---|
159 | if(iov[1].iov_len > len)
|
---|
160 | iov[1].iov_len = len;
|
---|
161 | total = iov[0].iov_len + iov[1].iov_len;
|
---|
162 | if (total > mss)
|
---|
163 | {
|
---|
164 | lss = total % mss;
|
---|
165 | if (iov[1].iov_len > lss)
|
---|
166 | {
|
---|
167 | iov[1].iov_len -= lss;
|
---|
168 | n = 2;
|
---|
169 | }
|
---|
170 | else
|
---|
171 | {
|
---|
172 | lss -= iov[1].iov_len;
|
---|
173 | iov[0].iov_len -= lss;
|
---|
174 | n = 1;
|
---|
175 | }
|
---|
176 | }
|
---|
177 | else
|
---|
178 | n = 2;
|
---|
179 | }
|
---|
180 | else
|
---|
181 | {
|
---|
182 | if (iov[0].iov_len > mss)
|
---|
183 | iov[0].iov_len -= iov[0].iov_len%mss;
|
---|
184 | n = 1;
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | #ifdef HAVE_READV
|
---|
189 | nn = readv(so->s, (struct iovec *)iov, n);
|
---|
190 | DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
|
---|
191 | #else
|
---|
192 | nn = recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
|
---|
193 | #endif
|
---|
194 | if (nn <= 0)
|
---|
195 | {
|
---|
196 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
197 | /*
|
---|
198 | * Special case for WSAEnumNetworkEvents: If we receive 0 bytes that
|
---|
199 | * _could_ mean that the connection is closed. But we will receive an
|
---|
200 | * FD_CLOSE event later if the connection was _really_ closed. With
|
---|
201 | * www.youtube.com I see this very often. Closing the socket too early
|
---|
202 | * would be dangerous.
|
---|
203 | */
|
---|
204 | if (nn == 0 && !fCloseIfNothingRead)
|
---|
205 | {
|
---|
206 | SOCKET_UNLOCK(so);
|
---|
207 | return 0;
|
---|
208 | }
|
---|
209 | #endif
|
---|
210 | if (nn < 0 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
|
---|
211 | {
|
---|
212 | SOCKET_UNLOCK(so);
|
---|
213 | return 0;
|
---|
214 | }
|
---|
215 | else
|
---|
216 | {
|
---|
217 | /* nn == 0 means peer has performed an orderly shutdown */
|
---|
218 | DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n",
|
---|
219 | nn, errno,strerror(errno)));
|
---|
220 | sofcantrcvmore(so);
|
---|
221 | tcp_sockclosed(pData, sototcpcb(so));
|
---|
222 | SOCKET_UNLOCK(so);
|
---|
223 | return -1;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | #ifndef HAVE_READV
|
---|
228 | /*
|
---|
229 | * If there was no error, try and read the second time round
|
---|
230 | * We read again if n = 2 (ie, there's another part of the buffer)
|
---|
231 | * and we read as much as we could in the first read
|
---|
232 | * We don't test for <= 0 this time, because there legitimately
|
---|
233 | * might not be any more data (since the socket is non-blocking),
|
---|
234 | * a close will be detected on next iteration.
|
---|
235 | * A return of -1 wont (shouldn't) happen, since it didn't happen above
|
---|
236 | */
|
---|
237 | if (n == 2 && nn == iov[0].iov_len)
|
---|
238 | {
|
---|
239 | int ret;
|
---|
240 | ret = recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
|
---|
241 | if (ret > 0)
|
---|
242 | nn += ret;
|
---|
243 | }
|
---|
244 |
|
---|
245 | DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
|
---|
246 | #endif
|
---|
247 |
|
---|
248 | /* Update fields */
|
---|
249 | sb->sb_cc += nn;
|
---|
250 | sb->sb_wptr += nn;
|
---|
251 | if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
|
---|
252 | sb->sb_wptr -= sb->sb_datalen;
|
---|
253 | SOCKET_UNLOCK(so);
|
---|
254 | return nn;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /*
|
---|
258 | * Get urgent data
|
---|
259 | *
|
---|
260 | * When the socket is created, we set it SO_OOBINLINE,
|
---|
261 | * so when OOB data arrives, we soread() it and everything
|
---|
262 | * in the send buffer is sent as urgent data
|
---|
263 | */
|
---|
264 | void
|
---|
265 | sorecvoob(PNATState pData, struct socket *so)
|
---|
266 | {
|
---|
267 | struct tcpcb *tp = sototcpcb(so);
|
---|
268 |
|
---|
269 | DEBUG_CALL("sorecvoob");
|
---|
270 | DEBUG_ARG("so = %lx", (long)so);
|
---|
271 |
|
---|
272 | /*
|
---|
273 | * We take a guess at how much urgent data has arrived.
|
---|
274 | * In most situations, when urgent data arrives, the next
|
---|
275 | * read() should get all the urgent data. This guess will
|
---|
276 | * be wrong however if more data arrives just after the
|
---|
277 | * urgent data, or the read() doesn't return all the
|
---|
278 | * urgent data.
|
---|
279 | */
|
---|
280 | soread(pData, so, /*fCloseIfNothingRead=*/false);
|
---|
281 | tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
|
---|
282 | tp->t_force = 1;
|
---|
283 | tcp_output(pData, tp);
|
---|
284 | tp->t_force = 0;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /*
|
---|
288 | * Send urgent data
|
---|
289 | * There's a lot duplicated code here, but...
|
---|
290 | */
|
---|
291 | int
|
---|
292 | sosendoob(struct socket *so)
|
---|
293 | {
|
---|
294 | struct sbuf *sb = &so->so_rcv;
|
---|
295 | char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
|
---|
296 |
|
---|
297 | int n, len;
|
---|
298 |
|
---|
299 | DEBUG_CALL("sosendoob");
|
---|
300 | DEBUG_ARG("so = %lx", (long)so);
|
---|
301 | DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
|
---|
302 |
|
---|
303 | if (so->so_urgc > sizeof(buff))
|
---|
304 | so->so_urgc = sizeof(buff); /* XXX */
|
---|
305 |
|
---|
306 | if (sb->sb_rptr < sb->sb_wptr)
|
---|
307 | {
|
---|
308 | /* We can send it directly */
|
---|
309 | n = send(so->s, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
|
---|
310 | so->so_urgc -= n;
|
---|
311 |
|
---|
312 | DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n",
|
---|
313 | n, so->so_urgc));
|
---|
314 | }
|
---|
315 | else
|
---|
316 | {
|
---|
317 | /*
|
---|
318 | * Since there's no sendv or sendtov like writev,
|
---|
319 | * we must copy all data to a linear buffer then
|
---|
320 | * send it all
|
---|
321 | */
|
---|
322 | len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
|
---|
323 | if (len > so->so_urgc)
|
---|
324 | len = so->so_urgc;
|
---|
325 | memcpy(buff, sb->sb_rptr, len);
|
---|
326 | so->so_urgc -= len;
|
---|
327 | if (so->so_urgc)
|
---|
328 | {
|
---|
329 | n = sb->sb_wptr - sb->sb_data;
|
---|
330 | if (n > so->so_urgc)
|
---|
331 | n = so->so_urgc;
|
---|
332 | memcpy(buff + len, sb->sb_data, n);
|
---|
333 | so->so_urgc -= n;
|
---|
334 | len += n;
|
---|
335 | }
|
---|
336 | n = send(so->s, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
|
---|
337 | #ifdef DEBUG
|
---|
338 | if (n != len)
|
---|
339 | DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
|
---|
340 | #endif
|
---|
341 | DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n",
|
---|
342 | n, so->so_urgc));
|
---|
343 | }
|
---|
344 |
|
---|
345 | sb->sb_cc -= n;
|
---|
346 | sb->sb_rptr += n;
|
---|
347 | if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
|
---|
348 | sb->sb_rptr -= sb->sb_datalen;
|
---|
349 |
|
---|
350 | return n;
|
---|
351 | }
|
---|
352 |
|
---|
353 | /*
|
---|
354 | * Write data from so_rcv to so's socket,
|
---|
355 | * updating all sbuf field as necessary
|
---|
356 | */
|
---|
357 | int
|
---|
358 | sowrite(PNATState pData, struct socket *so)
|
---|
359 | {
|
---|
360 | int n,nn;
|
---|
361 | struct sbuf *sb = &so->so_rcv;
|
---|
362 | size_t len = sb->sb_cc;
|
---|
363 | struct iovec iov[2];
|
---|
364 |
|
---|
365 | DEBUG_CALL("sowrite");
|
---|
366 | DEBUG_ARG("so = %lx", (long)so);
|
---|
367 | QSOCKET_LOCK(tcb);
|
---|
368 | SOCKET_LOCK(so);
|
---|
369 | QSOCKET_UNLOCK(tcb);
|
---|
370 | if (so->so_urgc)
|
---|
371 | {
|
---|
372 | sosendoob(so);
|
---|
373 | if (sb->sb_cc == 0)
|
---|
374 | {
|
---|
375 | SOCKET_UNLOCK(so);
|
---|
376 | return 0;
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | /*
|
---|
381 | * No need to check if there's something to write,
|
---|
382 | * sowrite wouldn't have been called otherwise
|
---|
383 | */
|
---|
384 |
|
---|
385 | len = sb->sb_cc;
|
---|
386 |
|
---|
387 | iov[0].iov_base = sb->sb_rptr;
|
---|
388 | iov[1].iov_base = 0;
|
---|
389 | iov[1].iov_len = 0;
|
---|
390 | if (sb->sb_rptr < sb->sb_wptr)
|
---|
391 | {
|
---|
392 | iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
|
---|
393 | /* Should never succeed, but... */
|
---|
394 | if (iov[0].iov_len > len)
|
---|
395 | iov[0].iov_len = len;
|
---|
396 | n = 1;
|
---|
397 | }
|
---|
398 | else
|
---|
399 | {
|
---|
400 | iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
|
---|
401 | if (iov[0].iov_len > len)
|
---|
402 | iov[0].iov_len = len;
|
---|
403 | len -= iov[0].iov_len;
|
---|
404 | if (len)
|
---|
405 | {
|
---|
406 | iov[1].iov_base = sb->sb_data;
|
---|
407 | iov[1].iov_len = sb->sb_wptr - sb->sb_data;
|
---|
408 | if (iov[1].iov_len > len)
|
---|
409 | iov[1].iov_len = len;
|
---|
410 | n = 2;
|
---|
411 | }
|
---|
412 | else
|
---|
413 | n = 1;
|
---|
414 | }
|
---|
415 | /* Check if there's urgent data to send, and if so, send it */
|
---|
416 | #ifdef HAVE_READV
|
---|
417 | nn = writev(so->s, (const struct iovec *)iov, n);
|
---|
418 | DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
|
---|
419 | #else
|
---|
420 | nn = send(so->s, iov[0].iov_base, iov[0].iov_len, 0);
|
---|
421 | #endif
|
---|
422 | /* This should never happen, but people tell me it does *shrug* */
|
---|
423 | if (nn < 0 && (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
|
---|
424 | {
|
---|
425 | SOCKET_UNLOCK(so);
|
---|
426 | return 0;
|
---|
427 | }
|
---|
428 |
|
---|
429 | if (nn < 0 || (nn == 0 && iov[0].iov_len > 0))
|
---|
430 | {
|
---|
431 | DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
|
---|
432 | so->so_state, errno));
|
---|
433 | sofcantsendmore(so);
|
---|
434 | tcp_sockclosed(pData, sototcpcb(so));
|
---|
435 | SOCKET_UNLOCK(so);
|
---|
436 | return -1;
|
---|
437 | }
|
---|
438 |
|
---|
439 | #ifndef HAVE_READV
|
---|
440 | if (n == 2 && nn == iov[0].iov_len)
|
---|
441 | {
|
---|
442 | int ret;
|
---|
443 | ret = send(so->s, iov[1].iov_base, iov[1].iov_len,0);
|
---|
444 | if (ret > 0)
|
---|
445 | nn += ret;
|
---|
446 | }
|
---|
447 | DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
|
---|
448 | #endif
|
---|
449 |
|
---|
450 | /* Update sbuf */
|
---|
451 | sb->sb_cc -= nn;
|
---|
452 | sb->sb_rptr += nn;
|
---|
453 | if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
|
---|
454 | sb->sb_rptr -= sb->sb_datalen;
|
---|
455 |
|
---|
456 | /*
|
---|
457 | * If in DRAIN mode, and there's no more data, set
|
---|
458 | * it CANTSENDMORE
|
---|
459 | */
|
---|
460 | if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
|
---|
461 | sofcantsendmore(so);
|
---|
462 |
|
---|
463 | SOCKET_UNLOCK(so);
|
---|
464 | return nn;
|
---|
465 | }
|
---|
466 |
|
---|
467 | /*
|
---|
468 | * recvfrom() a UDP socket
|
---|
469 | */
|
---|
470 | void
|
---|
471 | sorecvfrom(PNATState pData, struct socket *so)
|
---|
472 | {
|
---|
473 | struct sockaddr_in addr;
|
---|
474 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
475 |
|
---|
476 | DEBUG_CALL("sorecvfrom");
|
---|
477 | DEBUG_ARG("so = %lx", (long)so);
|
---|
478 |
|
---|
479 | if (so->so_type == IPPROTO_ICMP)
|
---|
480 | {
|
---|
481 | /* This is a "ping" reply */
|
---|
482 | #ifdef RT_OS_WINDOWS
|
---|
483 | sorecvfrom_icmp_win(pData, so);
|
---|
484 | #else /* RT_OS_WINDOWS */
|
---|
485 | sorecvfrom_icmp_unix(pData, so);
|
---|
486 | #endif /* !RT_OS_WINDOWS */
|
---|
487 | udp_detach(pData, so);
|
---|
488 | }
|
---|
489 | else
|
---|
490 | {
|
---|
491 | /* A "normal" UDP packet */
|
---|
492 | struct mbuf *m;
|
---|
493 | size_t len;
|
---|
494 | u_long n;
|
---|
495 |
|
---|
496 | QSOCKET_LOCK(udb);
|
---|
497 | SOCKET_LOCK(so);
|
---|
498 | QSOCKET_UNLOCK(udb);
|
---|
499 |
|
---|
500 | if (!(m = m_get(pData)))
|
---|
501 | {
|
---|
502 | SOCKET_UNLOCK(so);
|
---|
503 | return;
|
---|
504 | }
|
---|
505 | m->m_data += if_maxlinkhdr;
|
---|
506 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
507 | m->m_data += sizeof(struct udphdr)
|
---|
508 | + sizeof(struct ip); /*XXX: no options atm*/
|
---|
509 | #endif
|
---|
510 |
|
---|
511 | /*
|
---|
512 | * XXX Shouldn't FIONREAD packets destined for port 53,
|
---|
513 | * but I don't know the max packet size for DNS lookups
|
---|
514 | */
|
---|
515 | len = M_FREEROOM(m);
|
---|
516 | /* if (so->so_fport != htons(53)) */
|
---|
517 | {
|
---|
518 | ioctlsocket(so->s, FIONREAD, &n);
|
---|
519 |
|
---|
520 | if (n > len)
|
---|
521 | {
|
---|
522 | n = (m->m_data - m->m_dat) + m->m_len + n + 1;
|
---|
523 | m_inc(m, n);
|
---|
524 | len = M_FREEROOM(m);
|
---|
525 | }
|
---|
526 | }
|
---|
527 |
|
---|
528 | m->m_len = recvfrom(so->s, m->m_data, len, 0,
|
---|
529 | (struct sockaddr *)&addr, &addrlen);
|
---|
530 | Log2((" did recvfrom %d, errno = %d-%s\n",
|
---|
531 | m->m_len, errno,strerror(errno)));
|
---|
532 | if(m->m_len < 0)
|
---|
533 | {
|
---|
534 | u_char code = ICMP_UNREACH_PORT;
|
---|
535 |
|
---|
536 | if (errno == EHOSTUNREACH)
|
---|
537 | code = ICMP_UNREACH_HOST;
|
---|
538 | else if(errno == ENETUNREACH)
|
---|
539 | code = ICMP_UNREACH_NET;
|
---|
540 |
|
---|
541 | Log2((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
|
---|
542 | icmp_error(pData, so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
|
---|
543 | m_free(pData, m);
|
---|
544 | }
|
---|
545 | else
|
---|
546 | {
|
---|
547 | /*
|
---|
548 | * Hack: domain name lookup will be used the most for UDP,
|
---|
549 | * and since they'll only be used once there's no need
|
---|
550 | * for the 4 minute (or whatever) timeout... So we time them
|
---|
551 | * out much quicker (10 seconds for now...)
|
---|
552 | */
|
---|
553 | if (so->so_expire)
|
---|
554 | {
|
---|
555 | if (so->so_fport == htons(53))
|
---|
556 | so->so_expire = curtime + SO_EXPIREFAST;
|
---|
557 | else
|
---|
558 | so->so_expire = curtime + SO_EXPIRE;
|
---|
559 | }
|
---|
560 |
|
---|
561 | #if 0
|
---|
562 | if (m->m_len == len)
|
---|
563 | {
|
---|
564 | m_inc(m, MINCSIZE);
|
---|
565 | m->m_len = 0;
|
---|
566 | }
|
---|
567 | #endif
|
---|
568 |
|
---|
569 | /*
|
---|
570 | * If this packet was destined for CTL_ADDR,
|
---|
571 | * make it look like that's where it came from, done by udp_output
|
---|
572 | */
|
---|
573 | udp_output(pData, so, m, &addr);
|
---|
574 | SOCKET_UNLOCK(so);
|
---|
575 | } /* rx error */
|
---|
576 | } /* if ping packet */
|
---|
577 | }
|
---|
578 |
|
---|
579 | /*
|
---|
580 | * sendto() a socket
|
---|
581 | */
|
---|
582 | int
|
---|
583 | sosendto(PNATState pData, struct socket *so, struct mbuf *m)
|
---|
584 | {
|
---|
585 | int ret;
|
---|
586 | struct sockaddr_in addr;
|
---|
587 | #if 0
|
---|
588 | struct sockaddr_in host_addr;
|
---|
589 | #endif
|
---|
590 |
|
---|
591 | DEBUG_CALL("sosendto");
|
---|
592 | DEBUG_ARG("so = %lx", (long)so);
|
---|
593 | DEBUG_ARG("m = %lx", (long)m);
|
---|
594 |
|
---|
595 | addr.sin_family = AF_INET;
|
---|
596 | if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
|
---|
597 | {
|
---|
598 | /* It's an alias */
|
---|
599 | uint32_t last_byte = ntohl(so->so_faddr.s_addr) & ~pData->netmask;
|
---|
600 | switch(last_byte)
|
---|
601 | {
|
---|
602 | #if 0
|
---|
603 | /* handle this case at 'default:' */
|
---|
604 | case CTL_BROADCAST:
|
---|
605 | addr.sin_addr.s_addr = INADDR_BROADCAST;
|
---|
606 | /* Send the packet to host to fully emulate broadcast */
|
---|
607 | /** @todo r=klaus: on Linux host this causes the host to receive
|
---|
608 | * the packet twice for some reason. And I cannot find any place
|
---|
609 | * in the man pages which states that sending a broadcast does not
|
---|
610 | * reach the host itself. */
|
---|
611 | host_addr.sin_family = AF_INET;
|
---|
612 | host_addr.sin_port = so->so_fport;
|
---|
613 | host_addr.sin_addr = our_addr;
|
---|
614 | sendto(so->s, m->m_data, m->m_len, 0,
|
---|
615 | (struct sockaddr *)&host_addr, sizeof (struct sockaddr));
|
---|
616 | break;
|
---|
617 | #endif
|
---|
618 | case CTL_DNS:
|
---|
619 | #ifndef VBOX_WITH_MULTI_DNS
|
---|
620 | if (!get_dns_addr(pData, &dns_addr))
|
---|
621 | addr.sin_addr = dns_addr;
|
---|
622 | else
|
---|
623 | addr.sin_addr = loopback_addr;
|
---|
624 | break;
|
---|
625 | #endif
|
---|
626 | case CTL_ALIAS:
|
---|
627 | default:
|
---|
628 | if (last_byte == ~pData->netmask)
|
---|
629 | addr.sin_addr.s_addr = INADDR_BROADCAST;
|
---|
630 | else
|
---|
631 | addr.sin_addr = loopback_addr;
|
---|
632 | break;
|
---|
633 | }
|
---|
634 | }
|
---|
635 | else
|
---|
636 | addr.sin_addr = so->so_faddr;
|
---|
637 | addr.sin_port = so->so_fport;
|
---|
638 |
|
---|
639 | DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n",
|
---|
640 | ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
|
---|
641 |
|
---|
642 | /* Don't care what port we get */
|
---|
643 | ret = sendto(so->s, m->m_data, m->m_len, 0,
|
---|
644 | (struct sockaddr *)&addr, sizeof (struct sockaddr));
|
---|
645 | if (ret < 0)
|
---|
646 | {
|
---|
647 | LogRel(("UDP: sendto fails (%s)\n", strerror(errno)));
|
---|
648 | return -1;
|
---|
649 | }
|
---|
650 |
|
---|
651 | /*
|
---|
652 | * Kill the socket if there's no reply in 4 minutes,
|
---|
653 | * but only if it's an expirable socket
|
---|
654 | */
|
---|
655 | if (so->so_expire)
|
---|
656 | so->so_expire = curtime + SO_EXPIRE;
|
---|
657 | so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
|
---|
658 | return 0;
|
---|
659 | }
|
---|
660 |
|
---|
661 | /*
|
---|
662 | * XXX This should really be tcp_listen
|
---|
663 | */
|
---|
664 | struct socket *
|
---|
665 | solisten(PNATState pData, u_int port, u_int32_t laddr, u_int lport, int flags)
|
---|
666 | {
|
---|
667 | struct sockaddr_in addr;
|
---|
668 | struct socket *so;
|
---|
669 | socklen_t addrlen = sizeof(addr);
|
---|
670 | int s, opt = 1;
|
---|
671 |
|
---|
672 | DEBUG_CALL("solisten");
|
---|
673 | DEBUG_ARG("port = %d", port);
|
---|
674 | DEBUG_ARG("laddr = %x", laddr);
|
---|
675 | DEBUG_ARG("lport = %d", lport);
|
---|
676 | DEBUG_ARG("flags = %x", flags);
|
---|
677 |
|
---|
678 | if ((so = socreate()) == NULL)
|
---|
679 | {
|
---|
680 | /* RTMemFree(so); Not sofree() ??? free(NULL) == NOP */
|
---|
681 | return NULL;
|
---|
682 | }
|
---|
683 |
|
---|
684 | /* Don't tcp_attach... we don't need so_snd nor so_rcv */
|
---|
685 | if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
|
---|
686 | {
|
---|
687 | RTMemFree(so);
|
---|
688 | return NULL;
|
---|
689 | }
|
---|
690 |
|
---|
691 | SOCKET_LOCK_CREATE(so);
|
---|
692 | SOCKET_LOCK(so);
|
---|
693 | QSOCKET_LOCK(tcb);
|
---|
694 | insque(pData, so,&tcb);
|
---|
695 | QSOCKET_UNLOCK(tcb);
|
---|
696 |
|
---|
697 | /*
|
---|
698 | * SS_FACCEPTONCE sockets must time out.
|
---|
699 | */
|
---|
700 | if (flags & SS_FACCEPTONCE)
|
---|
701 | so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
|
---|
702 |
|
---|
703 | so->so_state = (SS_FACCEPTCONN|flags);
|
---|
704 | so->so_lport = lport; /* Kept in network format */
|
---|
705 | so->so_laddr.s_addr = laddr; /* Ditto */
|
---|
706 |
|
---|
707 | addr.sin_family = AF_INET;
|
---|
708 | addr.sin_addr.s_addr = INADDR_ANY;
|
---|
709 | addr.sin_port = port;
|
---|
710 |
|
---|
711 | if ( ((s = socket(AF_INET,SOCK_STREAM,0)) < 0)
|
---|
712 | || (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int)) < 0)
|
---|
713 | || (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0)
|
---|
714 | || (listen(s,1) < 0))
|
---|
715 | {
|
---|
716 | #ifdef RT_OS_WINDOWS
|
---|
717 | int tmperrno = WSAGetLastError(); /* Don't clobber the real reason we failed */
|
---|
718 | closesocket(s);
|
---|
719 | QSOCKET_LOCK(tcb);
|
---|
720 | sofree(pData, so);
|
---|
721 | QSOCKET_UNLOCK(tcb);
|
---|
722 | /* Restore the real errno */
|
---|
723 | WSASetLastError(tmperrno);
|
---|
724 | #else
|
---|
725 | int tmperrno = errno; /* Don't clobber the real reason we failed */
|
---|
726 | close(s);
|
---|
727 | QSOCKET_LOCK(tcb);
|
---|
728 | sofree(pData, so);
|
---|
729 | QSOCKET_UNLOCK(tcb);
|
---|
730 | /* Restore the real errno */
|
---|
731 | errno = tmperrno;
|
---|
732 | #endif
|
---|
733 | return NULL;
|
---|
734 | }
|
---|
735 | setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
|
---|
736 |
|
---|
737 | getsockname(s,(struct sockaddr *)&addr,&addrlen);
|
---|
738 | so->so_fport = addr.sin_port;
|
---|
739 | if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
|
---|
740 | so->so_faddr = alias_addr;
|
---|
741 | else
|
---|
742 | so->so_faddr = addr.sin_addr;
|
---|
743 |
|
---|
744 | so->s = s;
|
---|
745 | SOCKET_UNLOCK(so);
|
---|
746 | return so;
|
---|
747 | }
|
---|
748 |
|
---|
749 | /*
|
---|
750 | * Data is available in so_rcv
|
---|
751 | * Just write() the data to the socket
|
---|
752 | * XXX not yet...
|
---|
753 | */
|
---|
754 | void
|
---|
755 | sorwakeup(struct socket *so)
|
---|
756 | {
|
---|
757 | #if 0
|
---|
758 | sowrite(so);
|
---|
759 | FD_CLR(so->s,&writefds);
|
---|
760 | #endif
|
---|
761 | }
|
---|
762 |
|
---|
763 | /*
|
---|
764 | * Data has been freed in so_snd
|
---|
765 | * We have room for a read() if we want to
|
---|
766 | * For now, don't read, it'll be done in the main loop
|
---|
767 | */
|
---|
768 | void
|
---|
769 | sowwakeup(struct socket *so)
|
---|
770 | {
|
---|
771 | }
|
---|
772 |
|
---|
773 | /*
|
---|
774 | * Various session state calls
|
---|
775 | * XXX Should be #define's
|
---|
776 | * The socket state stuff needs work, these often get call 2 or 3
|
---|
777 | * times each when only 1 was needed
|
---|
778 | */
|
---|
779 | void
|
---|
780 | soisfconnecting(struct socket *so)
|
---|
781 | {
|
---|
782 | so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
|
---|
783 | SS_FCANTSENDMORE|SS_FWDRAIN);
|
---|
784 | so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
|
---|
785 | }
|
---|
786 |
|
---|
787 | void
|
---|
788 | soisfconnected(struct socket *so)
|
---|
789 | {
|
---|
790 | so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
|
---|
791 | so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
|
---|
792 | }
|
---|
793 |
|
---|
794 | void
|
---|
795 | sofcantrcvmore(struct socket *so)
|
---|
796 | {
|
---|
797 | if ((so->so_state & SS_NOFDREF) == 0)
|
---|
798 | {
|
---|
799 | shutdown(so->s,0);
|
---|
800 | }
|
---|
801 | so->so_state &= ~(SS_ISFCONNECTING);
|
---|
802 | if (so->so_state & SS_FCANTSENDMORE)
|
---|
803 | so->so_state = SS_NOFDREF; /* Don't select it */
|
---|
804 | /* XXX close() here as well? */
|
---|
805 | else
|
---|
806 | so->so_state |= SS_FCANTRCVMORE;
|
---|
807 | }
|
---|
808 |
|
---|
809 | void
|
---|
810 | sofcantsendmore(struct socket *so)
|
---|
811 | {
|
---|
812 | if ((so->so_state & SS_NOFDREF) == 0)
|
---|
813 | shutdown(so->s, 1); /* send FIN to fhost */
|
---|
814 |
|
---|
815 | so->so_state &= ~(SS_ISFCONNECTING);
|
---|
816 | if (so->so_state & SS_FCANTRCVMORE)
|
---|
817 | so->so_state = SS_NOFDREF; /* as above */
|
---|
818 | else
|
---|
819 | so->so_state |= SS_FCANTSENDMORE;
|
---|
820 | }
|
---|
821 |
|
---|
822 | void
|
---|
823 | soisfdisconnected(struct socket *so)
|
---|
824 | {
|
---|
825 | #if 0
|
---|
826 | so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED);
|
---|
827 | close(so->s);
|
---|
828 | so->so_state = SS_ISFDISCONNECTED;
|
---|
829 | /*
|
---|
830 | * XXX Do nothing ... ?
|
---|
831 | */
|
---|
832 | #endif
|
---|
833 | }
|
---|
834 |
|
---|
835 | /*
|
---|
836 | * Set write drain mode
|
---|
837 | * Set CANTSENDMORE once all data has been write()n
|
---|
838 | */
|
---|
839 | void
|
---|
840 | sofwdrain(struct socket *so)
|
---|
841 | {
|
---|
842 | if (so->so_rcv.sb_cc)
|
---|
843 | so->so_state |= SS_FWDRAIN;
|
---|
844 | else
|
---|
845 | sofcantsendmore(so);
|
---|
846 | }
|
---|
847 |
|
---|
848 | static void
|
---|
849 | send_icmp_to_guest(PNATState pData, char *buff, size_t len, struct socket *so, const struct sockaddr_in *addr)
|
---|
850 | {
|
---|
851 | struct ip *ip;
|
---|
852 | uint32_t dst,src;
|
---|
853 | char ip_copy[256];
|
---|
854 | struct icmp *icp;
|
---|
855 | int old_ip_len;
|
---|
856 | int hlen, original_hlen = 0;
|
---|
857 | struct mbuf *m;
|
---|
858 | struct icmp_msg *icm;
|
---|
859 | uint8_t proto;
|
---|
860 | int type = 0;
|
---|
861 |
|
---|
862 | ip = (struct ip *)buff;
|
---|
863 | hlen = (ip->ip_hl << 2);
|
---|
864 | icp = (struct icmp *)((char *)ip + hlen);
|
---|
865 |
|
---|
866 | Log(("ICMP:received msg(t:%d, c:%d)\n", icp->icmp_type, icp->icmp_code));
|
---|
867 | if ( icp->icmp_type != ICMP_ECHOREPLY
|
---|
868 | && icp->icmp_type != ICMP_TIMXCEED
|
---|
869 | && icp->icmp_type != ICMP_UNREACH)
|
---|
870 | {
|
---|
871 | return;
|
---|
872 | }
|
---|
873 |
|
---|
874 | type = icp->icmp_type;
|
---|
875 | if ( type == ICMP_TIMXCEED
|
---|
876 | || type == ICMP_UNREACH)
|
---|
877 | {
|
---|
878 | ip = &icp->icmp_ip;
|
---|
879 | DO_ALIAS(&ip->ip_dst);
|
---|
880 | }
|
---|
881 | else
|
---|
882 | {
|
---|
883 | DO_ALIAS(&ip->ip_src);
|
---|
884 | }
|
---|
885 |
|
---|
886 | icm = icmp_find_original_mbuf(pData, ip);
|
---|
887 |
|
---|
888 | if (icm == NULL)
|
---|
889 | {
|
---|
890 | Log(("NAT: Can't find the corresponding packet for the received ICMP\n"));
|
---|
891 | return;
|
---|
892 | }
|
---|
893 |
|
---|
894 | m = icm->im_m;
|
---|
895 | Assert(m != NULL);
|
---|
896 |
|
---|
897 | src = addr->sin_addr.s_addr;
|
---|
898 |
|
---|
899 | ip = mtod(m, struct ip *);
|
---|
900 | proto = ip->ip_p;
|
---|
901 | /* Now ip is pointing on header we've sent from guest */
|
---|
902 | if ( icp->icmp_type == ICMP_TIMXCEED
|
---|
903 | || icp->icmp_type == ICMP_UNREACH)
|
---|
904 | {
|
---|
905 | old_ip_len = (ip->ip_hl << 2) + 64;
|
---|
906 | if (old_ip_len > sizeof(ip_copy))
|
---|
907 | old_ip_len = sizeof(ip_copy);
|
---|
908 | memcpy(ip_copy, ip, old_ip_len);
|
---|
909 | }
|
---|
910 |
|
---|
911 | /* source address from original IP packet*/
|
---|
912 | dst = ip->ip_src.s_addr;
|
---|
913 |
|
---|
914 | /* overide ther tail of old packet */
|
---|
915 | ip = mtod(m, struct ip *); /* ip is from mbuf we've overrided */
|
---|
916 | original_hlen = ip->ip_hl << 2;
|
---|
917 | /* saves original ip header and options */
|
---|
918 | memcpy(m->m_data + original_hlen, buff + hlen, len - hlen);
|
---|
919 | m->m_len = len - hlen + original_hlen;
|
---|
920 | ip->ip_len = m->m_len;
|
---|
921 | ip->ip_p = IPPROTO_ICMP; /* the original package could be whatever, but we're response via ICMP*/
|
---|
922 |
|
---|
923 | icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
|
---|
924 | type = icp->icmp_type;
|
---|
925 | if ( type == ICMP_TIMXCEED
|
---|
926 | || type == ICMP_UNREACH)
|
---|
927 | {
|
---|
928 | /* according RFC 793 error messages required copy of initial IP header + 64 bit */
|
---|
929 | memcpy(&icp->icmp_ip, ip_copy, old_ip_len);
|
---|
930 | ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
|
---|
931 | }
|
---|
932 |
|
---|
933 | ip->ip_src.s_addr = src;
|
---|
934 | ip->ip_dst.s_addr = dst;
|
---|
935 | icmp_reflect(pData, m);
|
---|
936 | LIST_REMOVE(icm, im_list);
|
---|
937 | /* Don't call m_free here*/
|
---|
938 |
|
---|
939 | if ( type == ICMP_TIMXCEED
|
---|
940 | || type == ICMP_UNREACH)
|
---|
941 | {
|
---|
942 | icm->im_so->so_m = NULL;
|
---|
943 | switch (proto)
|
---|
944 | {
|
---|
945 | case IPPROTO_UDP:
|
---|
946 | /*XXX: so->so_m already freed so we shouldn't call sofree */
|
---|
947 | udp_detach(pData, icm->im_so);
|
---|
948 | break;
|
---|
949 | case IPPROTO_TCP:
|
---|
950 | /*close tcp should be here */
|
---|
951 | break;
|
---|
952 | default:
|
---|
953 | /* do nothing */
|
---|
954 | break;
|
---|
955 | }
|
---|
956 | }
|
---|
957 | RTMemFree(icm);
|
---|
958 | }
|
---|
959 |
|
---|
960 | #ifdef RT_OS_WINDOWS
|
---|
961 | static void
|
---|
962 | sorecvfrom_icmp_win(PNATState pData, struct socket *so)
|
---|
963 | {
|
---|
964 | int len;
|
---|
965 | int i;
|
---|
966 | struct ip *ip;
|
---|
967 | struct mbuf *m;
|
---|
968 | struct icmp *icp;
|
---|
969 | struct icmp_msg *icm;
|
---|
970 | struct ip *ip_broken; /* ICMP returns header + 64 bit of packet */
|
---|
971 | uint32_t src;
|
---|
972 | ICMP_ECHO_REPLY *icr;
|
---|
973 | int hlen = 0;
|
---|
974 | int data_len = 0;
|
---|
975 | int nbytes = 0;
|
---|
976 | u_char code = ~0;
|
---|
977 |
|
---|
978 | len = pData->pfIcmpParseReplies(pData->pvIcmpBuffer, pData->szIcmpBuffer);
|
---|
979 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
980 | fIcmp = 0; /* reply processed */
|
---|
981 | #endif
|
---|
982 | if (len < 0)
|
---|
983 | {
|
---|
984 | LogRel(("NAT: Error (%d) occurred on ICMP receiving\n", GetLastError()));
|
---|
985 | return;
|
---|
986 | }
|
---|
987 | if (len == 0)
|
---|
988 | return; /* no error */
|
---|
989 |
|
---|
990 | icr = (ICMP_ECHO_REPLY *)pData->pvIcmpBuffer;
|
---|
991 | for (i = 0; i < len; ++i)
|
---|
992 | {
|
---|
993 | switch(icr[i].Status)
|
---|
994 | {
|
---|
995 | case IP_DEST_HOST_UNREACHABLE:
|
---|
996 | code = (code != ~0 ? code : ICMP_UNREACH_HOST);
|
---|
997 | case IP_DEST_NET_UNREACHABLE:
|
---|
998 | code = (code != ~0 ? code : ICMP_UNREACH_NET);
|
---|
999 | case IP_DEST_PROT_UNREACHABLE:
|
---|
1000 | code = (code != ~0 ? code : ICMP_UNREACH_PROTOCOL);
|
---|
1001 | /* UNREACH error inject here */
|
---|
1002 | case IP_DEST_PORT_UNREACHABLE:
|
---|
1003 | code = (code != ~0 ? code : ICMP_UNREACH_PORT);
|
---|
1004 | icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, "Error occurred!!!");
|
---|
1005 | break;
|
---|
1006 | case IP_SUCCESS: /* echo replied */
|
---|
1007 | m = m_get(pData);
|
---|
1008 | m->m_data += if_maxlinkhdr;
|
---|
1009 | ip = mtod(m, struct ip *);
|
---|
1010 | ip->ip_src.s_addr = icr[i].Address;
|
---|
1011 | DO_ALIAS(&ip->ip_src);
|
---|
1012 | ip->ip_p = IPPROTO_ICMP;
|
---|
1013 | ip->ip_dst.s_addr = so->so_laddr.s_addr; /*XXX: still the hack*/
|
---|
1014 | data_len = sizeof(struct ip);
|
---|
1015 | ip->ip_hl = data_len >> 2; /* requiered for icmp_reflect, no IP options */
|
---|
1016 | ip->ip_ttl = icr[i].Options.Ttl;
|
---|
1017 |
|
---|
1018 | icp = (struct icmp *)&ip[1]; /* no options */
|
---|
1019 | icp->icmp_type = ICMP_ECHOREPLY;
|
---|
1020 | icp->icmp_code = 0;
|
---|
1021 | icp->icmp_id = so->so_icmp_id;
|
---|
1022 | icp->icmp_seq = so->so_icmp_seq;
|
---|
1023 |
|
---|
1024 | data_len += ICMP_MINLEN;
|
---|
1025 |
|
---|
1026 | nbytes = (data_len + icr[i].DataSize > m->m_size? m->m_size - data_len: icr[i].DataSize);
|
---|
1027 | memcpy(icp->icmp_data, icr[i].Data, nbytes);
|
---|
1028 |
|
---|
1029 | data_len += icr[i].DataSize;
|
---|
1030 |
|
---|
1031 | ip->ip_len = data_len;
|
---|
1032 | m->m_len = ip->ip_len;
|
---|
1033 |
|
---|
1034 | icmp_reflect(pData, m);
|
---|
1035 | break;
|
---|
1036 | case IP_TTL_EXPIRED_TRANSIT: /* TTL expired */
|
---|
1037 |
|
---|
1038 | ip_broken = icr[i].Data;
|
---|
1039 | icm = icmp_find_original_mbuf(pData, ip_broken);
|
---|
1040 | if (icm == NULL) {
|
---|
1041 | Log(("ICMP: can't find original package (first double word %x)\n", *(uint32_t *)ip_broken));
|
---|
1042 | return;
|
---|
1043 | }
|
---|
1044 | m = icm->im_m;
|
---|
1045 | ip = mtod(m, struct ip *);
|
---|
1046 | ip->ip_ttl = icr[i].Options.Ttl;
|
---|
1047 | src = ip->ip_src.s_addr;
|
---|
1048 | ip->ip_dst.s_addr = src;
|
---|
1049 | ip->ip_dst.s_addr = icr[i].Address;
|
---|
1050 |
|
---|
1051 | hlen = (ip->ip_hl << 2);
|
---|
1052 | icp = (struct icmp *)((char *)ip + hlen);
|
---|
1053 | ip_broken->ip_src.s_addr = src; /*it packet sent from host not from guest*/
|
---|
1054 | data_len = (ip_broken->ip_hl << 2) + 64;
|
---|
1055 |
|
---|
1056 | nbytes =(hlen + ICMP_MINLEN + data_len > m->m_size? m->m_size - (hlen + ICMP_MINLEN): data_len);
|
---|
1057 | memcpy(icp->icmp_data, ip_broken, nbytes);
|
---|
1058 | icmp_reflect(pData, m);
|
---|
1059 | break;
|
---|
1060 | default:
|
---|
1061 | Log(("ICMP(default): message with Status: %x was received from %x\n", icr[i].Status, icr[i].Address));
|
---|
1062 | break;
|
---|
1063 | }
|
---|
1064 | }
|
---|
1065 | }
|
---|
1066 | #else /* RT_OS_WINDOWS */
|
---|
1067 | static void sorecvfrom_icmp_unix(PNATState pData, struct socket *so)
|
---|
1068 | {
|
---|
1069 | struct sockaddr_in addr;
|
---|
1070 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
1071 | char buff[1500];
|
---|
1072 | int len;
|
---|
1073 | len = recvfrom(so->s, buff, 1500, 0,
|
---|
1074 | (struct sockaddr *)&addr, &addrlen);
|
---|
1075 | /* XXX Check if reply is "correct"? */
|
---|
1076 |
|
---|
1077 | if (len == -1 || len == 0)
|
---|
1078 | {
|
---|
1079 | u_char code = ICMP_UNREACH_PORT;
|
---|
1080 |
|
---|
1081 | if (errno == EHOSTUNREACH)
|
---|
1082 | code = ICMP_UNREACH_HOST;
|
---|
1083 | else if(errno == ENETUNREACH)
|
---|
1084 | code = ICMP_UNREACH_NET;
|
---|
1085 |
|
---|
1086 | DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
|
---|
1087 | errno,strerror(errno)));
|
---|
1088 | icmp_error(pData, so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
|
---|
1089 | }
|
---|
1090 | else
|
---|
1091 | {
|
---|
1092 | send_icmp_to_guest(pData, buff, len, so, &addr);
|
---|
1093 | }
|
---|
1094 | }
|
---|
1095 | #endif /* !RT_OS_WINDOWS */
|
---|