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