1 | /* $Id: socket.c 35826 2011-02-03 08:17:33Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NAT - socket handling.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*
|
---|
19 | * This code is based on:
|
---|
20 | *
|
---|
21 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
22 | *
|
---|
23 | * Please read the file COPYRIGHT for the
|
---|
24 | * terms and conditions of the copyright.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #define WANT_SYS_IOCTL_H
|
---|
28 | #include <slirp.h>
|
---|
29 | #include "ip_icmp.h"
|
---|
30 | #include "main.h"
|
---|
31 | #ifdef __sun__
|
---|
32 | #include <sys/filio.h>
|
---|
33 | #endif
|
---|
34 | #include <VBox/vmm/pdmdrv.h>
|
---|
35 | #if defined (RT_OS_WINDOWS)
|
---|
36 | #include <iphlpapi.h>
|
---|
37 | #include <icmpapi.h>
|
---|
38 | #endif
|
---|
39 |
|
---|
40 |
|
---|
41 | static void send_icmp_to_guest(PNATState, char *, size_t, struct socket *, const struct sockaddr_in *);
|
---|
42 | #ifdef RT_OS_WINDOWS
|
---|
43 | static void sorecvfrom_icmp_win(PNATState, struct socket *);
|
---|
44 | #else /* RT_OS_WINDOWS */
|
---|
45 | static void sorecvfrom_icmp_unix(PNATState, struct socket *);
|
---|
46 | #endif /* !RT_OS_WINDOWS */
|
---|
47 |
|
---|
48 | void
|
---|
49 | so_init()
|
---|
50 | {
|
---|
51 | }
|
---|
52 |
|
---|
53 | struct socket *
|
---|
54 | solookup(struct socket *head, struct in_addr laddr,
|
---|
55 | u_int lport, struct in_addr faddr, u_int fport)
|
---|
56 | {
|
---|
57 | struct socket *so;
|
---|
58 |
|
---|
59 | for (so = head->so_next; so != head; so = so->so_next)
|
---|
60 | {
|
---|
61 | if ( so->so_lport == lport
|
---|
62 | && so->so_laddr.s_addr == laddr.s_addr
|
---|
63 | && so->so_faddr.s_addr == faddr.s_addr
|
---|
64 | && so->so_fport == fport)
|
---|
65 | return so;
|
---|
66 | }
|
---|
67 |
|
---|
68 | return (struct socket *)NULL;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /*
|
---|
72 | * Create a new socket, initialise the fields
|
---|
73 | * It is the responsibility of the caller to
|
---|
74 | * insque() it into the correct linked-list
|
---|
75 | */
|
---|
76 | struct socket *
|
---|
77 | socreate()
|
---|
78 | {
|
---|
79 | struct socket *so;
|
---|
80 |
|
---|
81 | so = (struct socket *)RTMemAllocZ(sizeof(struct socket));
|
---|
82 | if (so)
|
---|
83 | {
|
---|
84 | so->so_state = SS_NOFDREF;
|
---|
85 | so->s = -1;
|
---|
86 | #if !defined(RT_OS_WINDOWS)
|
---|
87 | so->so_poll_index = -1;
|
---|
88 | #endif
|
---|
89 | }
|
---|
90 | return so;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /*
|
---|
94 | * remque and free a socket, clobber cache
|
---|
95 | * VBOX_WITH_SLIRP_MT: before sofree queue should be locked, because
|
---|
96 | * in sofree we don't know from which queue item beeing removed.
|
---|
97 | */
|
---|
98 | void
|
---|
99 | sofree(PNATState pData, struct socket *so)
|
---|
100 | {
|
---|
101 | struct socket *so_prev = NULL;
|
---|
102 | if (so == tcp_last_so)
|
---|
103 | tcp_last_so = &tcb;
|
---|
104 | else if (so == udp_last_so)
|
---|
105 | udp_last_so = &udb;
|
---|
106 |
|
---|
107 | /* check if mbuf haven't been already freed */
|
---|
108 | if (so->so_m != NULL)
|
---|
109 | m_freem(pData, so->so_m);
|
---|
110 | #ifndef VBOX_WITH_SLIRP_MT
|
---|
111 | if (so->so_next && so->so_prev)
|
---|
112 | {
|
---|
113 | remque(pData, so); /* crashes if so is not in a queue */
|
---|
114 | NSOCK_DEC();
|
---|
115 | }
|
---|
116 |
|
---|
117 | RTMemFree(so);
|
---|
118 | #else
|
---|
119 | so->so_deleted = 1;
|
---|
120 | #endif
|
---|
121 | }
|
---|
122 |
|
---|
123 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
124 | void
|
---|
125 | soread_queue(PNATState pData, struct socket *so, int *ret)
|
---|
126 | {
|
---|
127 | *ret = soread(pData, so);
|
---|
128 | }
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | /*
|
---|
132 | * Read from so's socket into sb_snd, updating all relevant sbuf fields
|
---|
133 | * NOTE: This will only be called if it is select()ed for reading, so
|
---|
134 | * a read() of 0 (or less) means it's disconnected
|
---|
135 | */
|
---|
136 | #ifndef VBOX_WITH_SLIRP_BSD_SBUF
|
---|
137 | int
|
---|
138 | soread(PNATState pData, struct socket *so)
|
---|
139 | {
|
---|
140 | int n, nn, lss, total;
|
---|
141 | struct sbuf *sb = &so->so_snd;
|
---|
142 | size_t len = sb->sb_datalen - sb->sb_cc;
|
---|
143 | struct iovec iov[2];
|
---|
144 | int mss = so->so_tcpcb->t_maxseg;
|
---|
145 |
|
---|
146 | STAM_PROFILE_START(&pData->StatIOread, a);
|
---|
147 | STAM_COUNTER_RESET(&pData->StatIORead_in_1);
|
---|
148 | STAM_COUNTER_RESET(&pData->StatIORead_in_2);
|
---|
149 |
|
---|
150 | QSOCKET_LOCK(tcb);
|
---|
151 | SOCKET_LOCK(so);
|
---|
152 | QSOCKET_UNLOCK(tcb);
|
---|
153 |
|
---|
154 | LogFlow(("soread: so = %lx\n", (long)so));
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * No need to check if there's enough room to read.
|
---|
158 | * soread wouldn't have been called if there weren't
|
---|
159 | */
|
---|
160 |
|
---|
161 | len = sb->sb_datalen - sb->sb_cc;
|
---|
162 |
|
---|
163 | iov[0].iov_base = sb->sb_wptr;
|
---|
164 | iov[1].iov_base = 0;
|
---|
165 | iov[1].iov_len = 0;
|
---|
166 | if (sb->sb_wptr < sb->sb_rptr)
|
---|
167 | {
|
---|
168 | iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
|
---|
169 | /* Should never succeed, but... */
|
---|
170 | if (iov[0].iov_len > len)
|
---|
171 | iov[0].iov_len = len;
|
---|
172 | if (iov[0].iov_len > mss)
|
---|
173 | iov[0].iov_len -= iov[0].iov_len%mss;
|
---|
174 | n = 1;
|
---|
175 | }
|
---|
176 | else
|
---|
177 | {
|
---|
178 | iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
|
---|
179 | /* Should never succeed, but... */
|
---|
180 | if (iov[0].iov_len > len)
|
---|
181 | iov[0].iov_len = len;
|
---|
182 | len -= iov[0].iov_len;
|
---|
183 | if (len)
|
---|
184 | {
|
---|
185 | iov[1].iov_base = sb->sb_data;
|
---|
186 | iov[1].iov_len = sb->sb_rptr - sb->sb_data;
|
---|
187 | if (iov[1].iov_len > len)
|
---|
188 | iov[1].iov_len = len;
|
---|
189 | total = iov[0].iov_len + iov[1].iov_len;
|
---|
190 | if (total > mss)
|
---|
191 | {
|
---|
192 | lss = total % mss;
|
---|
193 | if (iov[1].iov_len > lss)
|
---|
194 | {
|
---|
195 | iov[1].iov_len -= lss;
|
---|
196 | n = 2;
|
---|
197 | }
|
---|
198 | else
|
---|
199 | {
|
---|
200 | lss -= iov[1].iov_len;
|
---|
201 | iov[0].iov_len -= lss;
|
---|
202 | n = 1;
|
---|
203 | }
|
---|
204 | }
|
---|
205 | else
|
---|
206 | n = 2;
|
---|
207 | }
|
---|
208 | else
|
---|
209 | {
|
---|
210 | if (iov[0].iov_len > mss)
|
---|
211 | iov[0].iov_len -= iov[0].iov_len%mss;
|
---|
212 | n = 1;
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | #ifdef HAVE_READV
|
---|
217 | nn = readv(so->s, (struct iovec *)iov, n);
|
---|
218 | Log2((" ... read nn = %d bytes\n", nn));
|
---|
219 | #else
|
---|
220 | nn = recv(so->s, iov[0].iov_base, iov[0].iov_len, (so->so_tcpcb->t_force? MSG_OOB:0));
|
---|
221 | #endif
|
---|
222 | if (nn <= 0)
|
---|
223 | {
|
---|
224 | /*
|
---|
225 | * Special case for WSAEnumNetworkEvents: If we receive 0 bytes that
|
---|
226 | * _could_ mean that the connection is closed. But we will receive an
|
---|
227 | * FD_CLOSE event later if the connection was _really_ closed. With
|
---|
228 | * www.youtube.com I see this very often. Closing the socket too early
|
---|
229 | * would be dangerous.
|
---|
230 | */
|
---|
231 | int status;
|
---|
232 | unsigned long pending = 0;
|
---|
233 | status = ioctlsocket(so->s, FIONREAD, &pending);
|
---|
234 | if (status < 0)
|
---|
235 | Log(("NAT:error in WSAIoctl: %d\n", errno));
|
---|
236 | if (nn == 0 && (pending != 0))
|
---|
237 | {
|
---|
238 | SOCKET_UNLOCK(so);
|
---|
239 | STAM_PROFILE_STOP(&pData->StatIOread, a);
|
---|
240 | return 0;
|
---|
241 | }
|
---|
242 | if ( nn < 0
|
---|
243 | && ( errno == EINTR
|
---|
244 | || errno == EAGAIN
|
---|
245 | || errno == EWOULDBLOCK))
|
---|
246 | {
|
---|
247 | SOCKET_UNLOCK(so);
|
---|
248 | STAM_PROFILE_STOP(&pData->StatIOread, a);
|
---|
249 | return 0;
|
---|
250 | }
|
---|
251 | else
|
---|
252 | {
|
---|
253 | /* nn == 0 means peer has performed an orderly shutdown */
|
---|
254 | Log2((" --- soread() disconnected, nn = %d, errno = %d (%s)\n",
|
---|
255 | nn, errno, strerror(errno)));
|
---|
256 | sofcantrcvmore(so);
|
---|
257 | tcp_sockclosed(pData, sototcpcb(so));
|
---|
258 | SOCKET_UNLOCK(so);
|
---|
259 | STAM_PROFILE_STOP(&pData->StatIOread, a);
|
---|
260 | return -1;
|
---|
261 | }
|
---|
262 | }
|
---|
263 | STAM_STATS(
|
---|
264 | if (n == 1)
|
---|
265 | {
|
---|
266 | STAM_COUNTER_INC(&pData->StatIORead_in_1);
|
---|
267 | STAM_COUNTER_ADD(&pData->StatIORead_in_1_bytes, nn);
|
---|
268 | }
|
---|
269 | else
|
---|
270 | {
|
---|
271 | STAM_COUNTER_INC(&pData->StatIORead_in_2);
|
---|
272 | STAM_COUNTER_ADD(&pData->StatIORead_in_2_1st_bytes, nn);
|
---|
273 | }
|
---|
274 | );
|
---|
275 |
|
---|
276 | #ifndef HAVE_READV
|
---|
277 | /*
|
---|
278 | * If there was no error, try and read the second time round
|
---|
279 | * We read again if n = 2 (ie, there's another part of the buffer)
|
---|
280 | * and we read as much as we could in the first read
|
---|
281 | * We don't test for <= 0 this time, because there legitimately
|
---|
282 | * might not be any more data (since the socket is non-blocking),
|
---|
283 | * a close will be detected on next iteration.
|
---|
284 | * A return of -1 wont (shouldn't) happen, since it didn't happen above
|
---|
285 | */
|
---|
286 | if (n == 2 && nn == iov[0].iov_len)
|
---|
287 | {
|
---|
288 | int ret;
|
---|
289 | ret = recv(so->s, iov[1].iov_base, iov[1].iov_len, 0);
|
---|
290 | if (ret > 0)
|
---|
291 | nn += ret;
|
---|
292 | STAM_STATS(
|
---|
293 | if (ret > 0)
|
---|
294 | {
|
---|
295 | STAM_COUNTER_INC(&pData->StatIORead_in_2);
|
---|
296 | STAM_COUNTER_ADD(&pData->StatIORead_in_2_2nd_bytes, ret);
|
---|
297 | }
|
---|
298 | );
|
---|
299 | }
|
---|
300 |
|
---|
301 | Log2((" ... read nn = %d bytes\n", nn));
|
---|
302 | #endif
|
---|
303 |
|
---|
304 | /* Update fields */
|
---|
305 | sb->sb_cc += nn;
|
---|
306 | sb->sb_wptr += nn;
|
---|
307 | if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
|
---|
308 | sb->sb_wptr -= sb->sb_datalen;
|
---|
309 | STAM_PROFILE_STOP(&pData->StatIOread, a);
|
---|
310 | SOCKET_UNLOCK(so);
|
---|
311 | return nn;
|
---|
312 | }
|
---|
313 | #else /* VBOX_WITH_SLIRP_BSD_SBUF */
|
---|
314 | int
|
---|
315 | soread(PNATState pData, struct socket *so)
|
---|
316 | {
|
---|
317 | int n;
|
---|
318 | char *buf;
|
---|
319 | struct sbuf *sb = &so->so_snd;
|
---|
320 | size_t len = sbspace(sb);
|
---|
321 | int mss = so->so_tcpcb->t_maxseg;
|
---|
322 |
|
---|
323 | STAM_PROFILE_START(&pData->StatIOread, a);
|
---|
324 | STAM_COUNTER_RESET(&pData->StatIORead_in_1);
|
---|
325 | STAM_COUNTER_RESET(&pData->StatIORead_in_2);
|
---|
326 |
|
---|
327 | QSOCKET_LOCK(tcb);
|
---|
328 | SOCKET_LOCK(so);
|
---|
329 | QSOCKET_UNLOCK(tcb);
|
---|
330 |
|
---|
331 | LogFlow(("soread: so = %lx\n", (long)so));
|
---|
332 |
|
---|
333 | if (len > mss)
|
---|
334 | len -= len % mss;
|
---|
335 | buf = RTMemAlloc(len);
|
---|
336 | if (buf == NULL)
|
---|
337 | {
|
---|
338 | Log(("NAT: can't alloc enough memory\n"));
|
---|
339 | return -1;
|
---|
340 | }
|
---|
341 |
|
---|
342 | n = recv(so->s, buf, len, (so->so_tcpcb->t_force? MSG_OOB:0));
|
---|
343 | if (n <= 0)
|
---|
344 | {
|
---|
345 | /*
|
---|
346 | * Special case for WSAEnumNetworkEvents: If we receive 0 bytes that
|
---|
347 | * _could_ mean that the connection is closed. But we will receive an
|
---|
348 | * FD_CLOSE event later if the connection was _really_ closed. With
|
---|
349 | * www.youtube.com I see this very often. Closing the socket too early
|
---|
350 | * would be dangerous.
|
---|
351 | */
|
---|
352 | int status;
|
---|
353 | unsigned long pending = 0;
|
---|
354 | status = ioctlsocket(so->s, FIONREAD, &pending);
|
---|
355 | if (status < 0)
|
---|
356 | Log(("NAT:error in WSAIoctl: %d\n", errno));
|
---|
357 | if (n == 0 && (pending != 0))
|
---|
358 | {
|
---|
359 | SOCKET_UNLOCK(so);
|
---|
360 | STAM_PROFILE_STOP(&pData->StatIOread, a);
|
---|
361 | RTMemFree(buf);
|
---|
362 | return 0;
|
---|
363 | }
|
---|
364 | if ( n < 0
|
---|
365 | && ( errno == EINTR
|
---|
366 | || errno == EAGAIN
|
---|
367 | || errno == EWOULDBLOCK))
|
---|
368 | {
|
---|
369 | SOCKET_UNLOCK(so);
|
---|
370 | STAM_PROFILE_STOP(&pData->StatIOread, a);
|
---|
371 | RTMemFree(buf);
|
---|
372 | return 0;
|
---|
373 | }
|
---|
374 | else
|
---|
375 | {
|
---|
376 | Log2((" --- soread() disconnected, n = %d, errno = %d (%s)\n",
|
---|
377 | n, errno, strerror(errno)));
|
---|
378 | sofcantrcvmore(so);
|
---|
379 | tcp_sockclosed(pData, sototcpcb(so));
|
---|
380 | SOCKET_UNLOCK(so);
|
---|
381 | STAM_PROFILE_STOP(&pData->StatIOread, a);
|
---|
382 | RTMemFree(buf);
|
---|
383 | return -1;
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | sbuf_bcat(sb, buf, n);
|
---|
388 | RTMemFree(buf);
|
---|
389 | return n;
|
---|
390 | }
|
---|
391 | #endif
|
---|
392 |
|
---|
393 | /*
|
---|
394 | * Get urgent data
|
---|
395 | *
|
---|
396 | * When the socket is created, we set it SO_OOBINLINE,
|
---|
397 | * so when OOB data arrives, we soread() it and everything
|
---|
398 | * in the send buffer is sent as urgent data
|
---|
399 | */
|
---|
400 | void
|
---|
401 | sorecvoob(PNATState pData, struct socket *so)
|
---|
402 | {
|
---|
403 | struct tcpcb *tp = sototcpcb(so);
|
---|
404 | ssize_t ret;
|
---|
405 |
|
---|
406 | LogFlow(("sorecvoob: so = %lx\n", (long)so));
|
---|
407 |
|
---|
408 | /*
|
---|
409 | * We take a guess at how much urgent data has arrived.
|
---|
410 | * In most situations, when urgent data arrives, the next
|
---|
411 | * read() should get all the urgent data. This guess will
|
---|
412 | * be wrong however if more data arrives just after the
|
---|
413 | * urgent data, or the read() doesn't return all the
|
---|
414 | * urgent data.
|
---|
415 | */
|
---|
416 | ret = soread(pData, so);
|
---|
417 | tp->snd_up = tp->snd_una + SBUF_LEN(&so->so_snd);
|
---|
418 | tp->t_force = 1;
|
---|
419 | tcp_output(pData, tp);
|
---|
420 | tp->t_force = 0;
|
---|
421 | }
|
---|
422 | #ifndef VBOX_WITH_SLIRP_BSD_SBUF
|
---|
423 | /*
|
---|
424 | * Send urgent data
|
---|
425 | * There's a lot duplicated code here, but...
|
---|
426 | */
|
---|
427 | int
|
---|
428 | sosendoob(struct socket *so)
|
---|
429 | {
|
---|
430 | struct sbuf *sb = &so->so_rcv;
|
---|
431 | char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
|
---|
432 |
|
---|
433 | int n, len;
|
---|
434 |
|
---|
435 | LogFlow(("sosendoob so = %lx\n", (long)so));
|
---|
436 |
|
---|
437 | if (so->so_urgc > sizeof(buff))
|
---|
438 | so->so_urgc = sizeof(buff); /* XXX */
|
---|
439 |
|
---|
440 | if (sb->sb_rptr < sb->sb_wptr)
|
---|
441 | {
|
---|
442 | /* We can send it directly */
|
---|
443 | n = send(so->s, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
|
---|
444 | so->so_urgc -= n;
|
---|
445 |
|
---|
446 | Log2((" --- sent %d bytes urgent data, %d urgent bytes left\n",
|
---|
447 | n, so->so_urgc));
|
---|
448 | }
|
---|
449 | else
|
---|
450 | {
|
---|
451 | /*
|
---|
452 | * Since there's no sendv or sendtov like writev,
|
---|
453 | * we must copy all data to a linear buffer then
|
---|
454 | * send it all
|
---|
455 | */
|
---|
456 | len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
|
---|
457 | if (len > so->so_urgc)
|
---|
458 | len = so->so_urgc;
|
---|
459 | memcpy(buff, sb->sb_rptr, len);
|
---|
460 | so->so_urgc -= len;
|
---|
461 | if (so->so_urgc)
|
---|
462 | {
|
---|
463 | n = sb->sb_wptr - sb->sb_data;
|
---|
464 | if (n > so->so_urgc)
|
---|
465 | n = so->so_urgc;
|
---|
466 | memcpy(buff + len, sb->sb_data, n);
|
---|
467 | so->so_urgc -= n;
|
---|
468 | len += n;
|
---|
469 | }
|
---|
470 | n = send(so->s, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
|
---|
471 | #ifdef DEBUG
|
---|
472 | if (n != len)
|
---|
473 | Log(("Didn't send all data urgently XXXXX\n"));
|
---|
474 | #endif
|
---|
475 | Log2((" ---2 sent %d bytes urgent data, %d urgent bytes left\n",
|
---|
476 | n, so->so_urgc));
|
---|
477 | }
|
---|
478 |
|
---|
479 | sb->sb_cc -= n;
|
---|
480 | sb->sb_rptr += n;
|
---|
481 | if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
|
---|
482 | sb->sb_rptr -= sb->sb_datalen;
|
---|
483 |
|
---|
484 | return n;
|
---|
485 | }
|
---|
486 |
|
---|
487 | /*
|
---|
488 | * Write data from so_rcv to so's socket,
|
---|
489 | * updating all sbuf field as necessary
|
---|
490 | */
|
---|
491 | int
|
---|
492 | sowrite(PNATState pData, struct socket *so)
|
---|
493 | {
|
---|
494 | int n, nn;
|
---|
495 | struct sbuf *sb = &so->so_rcv;
|
---|
496 | size_t len = sb->sb_cc;
|
---|
497 | struct iovec iov[2];
|
---|
498 |
|
---|
499 | STAM_PROFILE_START(&pData->StatIOwrite, a);
|
---|
500 | STAM_COUNTER_RESET(&pData->StatIOWrite_in_1);
|
---|
501 | STAM_COUNTER_RESET(&pData->StatIOWrite_in_1_bytes);
|
---|
502 | STAM_COUNTER_RESET(&pData->StatIOWrite_in_2);
|
---|
503 | STAM_COUNTER_RESET(&pData->StatIOWrite_in_2_1st_bytes);
|
---|
504 | STAM_COUNTER_RESET(&pData->StatIOWrite_in_2_2nd_bytes);
|
---|
505 | STAM_COUNTER_RESET(&pData->StatIOWrite_no_w);
|
---|
506 | STAM_COUNTER_RESET(&pData->StatIOWrite_rest);
|
---|
507 | STAM_COUNTER_RESET(&pData->StatIOWrite_rest_bytes);
|
---|
508 | LogFlow(("sowrite: so = %lx\n", (long)so));
|
---|
509 | QSOCKET_LOCK(tcb);
|
---|
510 | SOCKET_LOCK(so);
|
---|
511 | QSOCKET_UNLOCK(tcb);
|
---|
512 | if (so->so_urgc)
|
---|
513 | {
|
---|
514 | sosendoob(so);
|
---|
515 | if (sb->sb_cc == 0)
|
---|
516 | {
|
---|
517 | SOCKET_UNLOCK(so);
|
---|
518 | STAM_PROFILE_STOP(&pData->StatIOwrite, a);
|
---|
519 | return 0;
|
---|
520 | }
|
---|
521 | }
|
---|
522 |
|
---|
523 | /*
|
---|
524 | * No need to check if there's something to write,
|
---|
525 | * sowrite wouldn't have been called otherwise
|
---|
526 | */
|
---|
527 |
|
---|
528 | len = sb->sb_cc;
|
---|
529 |
|
---|
530 | iov[0].iov_base = sb->sb_rptr;
|
---|
531 | iov[1].iov_base = 0;
|
---|
532 | iov[1].iov_len = 0;
|
---|
533 | if (sb->sb_rptr < sb->sb_wptr)
|
---|
534 | {
|
---|
535 | iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
|
---|
536 | /* Should never succeed, but... */
|
---|
537 | if (iov[0].iov_len > len)
|
---|
538 | iov[0].iov_len = len;
|
---|
539 | n = 1;
|
---|
540 | }
|
---|
541 | else
|
---|
542 | {
|
---|
543 | iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
|
---|
544 | if (iov[0].iov_len > len)
|
---|
545 | iov[0].iov_len = len;
|
---|
546 | len -= iov[0].iov_len;
|
---|
547 | if (len)
|
---|
548 | {
|
---|
549 | iov[1].iov_base = sb->sb_data;
|
---|
550 | iov[1].iov_len = sb->sb_wptr - sb->sb_data;
|
---|
551 | if (iov[1].iov_len > len)
|
---|
552 | iov[1].iov_len = len;
|
---|
553 | n = 2;
|
---|
554 | }
|
---|
555 | else
|
---|
556 | n = 1;
|
---|
557 | }
|
---|
558 | STAM_STATS({
|
---|
559 | if (n == 1)
|
---|
560 | {
|
---|
561 | STAM_COUNTER_INC(&pData->StatIOWrite_in_1);
|
---|
562 | STAM_COUNTER_ADD(&pData->StatIOWrite_in_1_bytes, iov[0].iov_len);
|
---|
563 | }
|
---|
564 | else
|
---|
565 | {
|
---|
566 | STAM_COUNTER_INC(&pData->StatIOWrite_in_2);
|
---|
567 | STAM_COUNTER_ADD(&pData->StatIOWrite_in_2_1st_bytes, iov[0].iov_len);
|
---|
568 | STAM_COUNTER_ADD(&pData->StatIOWrite_in_2_2nd_bytes, iov[1].iov_len);
|
---|
569 | }
|
---|
570 | });
|
---|
571 | /* Check if there's urgent data to send, and if so, send it */
|
---|
572 | #ifdef HAVE_READV
|
---|
573 | nn = writev(so->s, (const struct iovec *)iov, n);
|
---|
574 | Log2((" ... wrote nn = %d bytes\n", nn));
|
---|
575 | #else
|
---|
576 | nn = send(so->s, iov[0].iov_base, iov[0].iov_len, 0);
|
---|
577 | #endif
|
---|
578 | /* This should never happen, but people tell me it does *shrug* */
|
---|
579 | if ( nn < 0
|
---|
580 | && ( errno == EAGAIN
|
---|
581 | || errno == EINTR
|
---|
582 | || errno == EWOULDBLOCK))
|
---|
583 | {
|
---|
584 | SOCKET_UNLOCK(so);
|
---|
585 | STAM_PROFILE_STOP(&pData->StatIOwrite, a);
|
---|
586 | return 0;
|
---|
587 | }
|
---|
588 |
|
---|
589 | if (nn < 0 || (nn == 0 && iov[0].iov_len > 0))
|
---|
590 | {
|
---|
591 | Log2((" --- sowrite disconnected, so->so_state = %x, errno = %d\n",
|
---|
592 | so->so_state, errno));
|
---|
593 | sofcantsendmore(so);
|
---|
594 | tcp_sockclosed(pData, sototcpcb(so));
|
---|
595 | SOCKET_UNLOCK(so);
|
---|
596 | STAM_PROFILE_STOP(&pData->StatIOwrite, a);
|
---|
597 | return -1;
|
---|
598 | }
|
---|
599 |
|
---|
600 | #ifndef HAVE_READV
|
---|
601 | if (n == 2 && nn == iov[0].iov_len)
|
---|
602 | {
|
---|
603 | int ret;
|
---|
604 | ret = send(so->s, iov[1].iov_base, iov[1].iov_len, 0);
|
---|
605 | if (ret > 0)
|
---|
606 | nn += ret;
|
---|
607 | STAM_STATS({
|
---|
608 | if (ret > 0 && ret != iov[1].iov_len)
|
---|
609 | {
|
---|
610 | STAM_COUNTER_INC(&pData->StatIOWrite_rest);
|
---|
611 | STAM_COUNTER_ADD(&pData->StatIOWrite_rest_bytes, (iov[1].iov_len - ret));
|
---|
612 | }
|
---|
613 | });
|
---|
614 | }
|
---|
615 | Log2((" ... wrote nn = %d bytes\n", nn));
|
---|
616 | #endif
|
---|
617 |
|
---|
618 | /* Update sbuf */
|
---|
619 | sb->sb_cc -= nn;
|
---|
620 | sb->sb_rptr += nn;
|
---|
621 | if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
|
---|
622 | sb->sb_rptr -= sb->sb_datalen;
|
---|
623 |
|
---|
624 | /*
|
---|
625 | * If in DRAIN mode, and there's no more data, set
|
---|
626 | * it CANTSENDMORE
|
---|
627 | */
|
---|
628 | if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
|
---|
629 | sofcantsendmore(so);
|
---|
630 |
|
---|
631 | SOCKET_UNLOCK(so);
|
---|
632 | STAM_PROFILE_STOP(&pData->StatIOwrite, a);
|
---|
633 | return nn;
|
---|
634 | }
|
---|
635 | #else /* VBOX_WITH_SLIRP_BSD_SBUF */
|
---|
636 | static int
|
---|
637 | do_sosend(struct socket *so, int fUrg)
|
---|
638 | {
|
---|
639 | struct sbuf *sb = &so->so_rcv;
|
---|
640 |
|
---|
641 | int n, len;
|
---|
642 |
|
---|
643 | LogFlow(("sosendoob: so = %lx\n", (long)so));
|
---|
644 |
|
---|
645 | len = sbuf_len(sb);
|
---|
646 |
|
---|
647 | n = send(so->s, sbuf_data(sb), len, (fUrg ? MSG_OOB : 0));
|
---|
648 | if (n < 0)
|
---|
649 | Log(("NAT: Can't sent sbuf via socket.\n"));
|
---|
650 | if (fUrg)
|
---|
651 | so->so_urgc -= n;
|
---|
652 | if (n > 0 && n < len)
|
---|
653 | {
|
---|
654 | char *ptr;
|
---|
655 | char *buff;
|
---|
656 | buff = RTMemAlloc(len);
|
---|
657 | if (buff == NULL)
|
---|
658 | {
|
---|
659 | Log(("NAT: No space to allocate temporal buffer\n"));
|
---|
660 | return -1;
|
---|
661 | }
|
---|
662 | ptr = sbuf_data(sb);
|
---|
663 | memcpy(buff, &ptr[n], len - n);
|
---|
664 | sbuf_bcpy(sb, buff, len - n);
|
---|
665 | RTMemFree(buff);
|
---|
666 | return n;
|
---|
667 | }
|
---|
668 | sbuf_clear(sb);
|
---|
669 | return n;
|
---|
670 | }
|
---|
671 | int
|
---|
672 | sosendoob(struct socket *so)
|
---|
673 | {
|
---|
674 | return do_sosend(so, 1);
|
---|
675 | }
|
---|
676 |
|
---|
677 | /*
|
---|
678 | * Write data from so_rcv to so's socket,
|
---|
679 | * updating all sbuf field as necessary
|
---|
680 | */
|
---|
681 | int
|
---|
682 | sowrite(PNATState pData, struct socket *so)
|
---|
683 | {
|
---|
684 | return do_sosend(so, 0);
|
---|
685 | }
|
---|
686 | #endif
|
---|
687 |
|
---|
688 | /*
|
---|
689 | * recvfrom() a UDP socket
|
---|
690 | */
|
---|
691 | void
|
---|
692 | sorecvfrom(PNATState pData, struct socket *so)
|
---|
693 | {
|
---|
694 | ssize_t ret = 0;
|
---|
695 | struct sockaddr_in addr;
|
---|
696 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
697 |
|
---|
698 | LogFlow(("sorecvfrom: so = %lx\n", (long)so));
|
---|
699 |
|
---|
700 | if (so->so_type == IPPROTO_ICMP)
|
---|
701 | {
|
---|
702 | /* This is a "ping" reply */
|
---|
703 | #ifdef RT_OS_WINDOWS
|
---|
704 | sorecvfrom_icmp_win(pData, so);
|
---|
705 | #else /* RT_OS_WINDOWS */
|
---|
706 | sorecvfrom_icmp_unix(pData, so);
|
---|
707 | #endif /* !RT_OS_WINDOWS */
|
---|
708 | udp_detach(pData, so);
|
---|
709 | }
|
---|
710 | else
|
---|
711 | {
|
---|
712 | /* A "normal" UDP packet */
|
---|
713 | struct mbuf *m;
|
---|
714 | ssize_t len;
|
---|
715 | u_long n = 0;
|
---|
716 | int size;
|
---|
717 | int rc = 0;
|
---|
718 | static int signalled = 0;
|
---|
719 | char *pchBuffer = NULL;
|
---|
720 | bool fWithTemporalBuffer = false;
|
---|
721 |
|
---|
722 | QSOCKET_LOCK(udb);
|
---|
723 | SOCKET_LOCK(so);
|
---|
724 | QSOCKET_UNLOCK(udb);
|
---|
725 |
|
---|
726 | /*How many data has been received ?*/
|
---|
727 | /*
|
---|
728 | * 1. calculate how much we can read
|
---|
729 | * 2. read as much as possible
|
---|
730 | * 3. attach buffer to allocated header mbuf
|
---|
731 | */
|
---|
732 | rc = ioctlsocket(so->s, FIONREAD, &n);
|
---|
733 | if (rc == -1)
|
---|
734 | {
|
---|
735 | if ( errno == EAGAIN
|
---|
736 | || errno == EWOULDBLOCK
|
---|
737 | || errno == EINPROGRESS
|
---|
738 | || errno == ENOTCONN)
|
---|
739 | return;
|
---|
740 | else if (signalled == 0)
|
---|
741 | {
|
---|
742 | LogRel(("NAT: can't fetch amount of bytes on socket %R[natsock], so message will be truncated.\n", so));
|
---|
743 | signalled = 1;
|
---|
744 | }
|
---|
745 | return;
|
---|
746 | }
|
---|
747 |
|
---|
748 | len = sizeof(struct udpiphdr);
|
---|
749 | m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, slirp_size(pData));
|
---|
750 | if (m == NULL)
|
---|
751 | return;
|
---|
752 |
|
---|
753 | len += n;
|
---|
754 | m->m_data += ETH_HLEN;
|
---|
755 | m->m_pkthdr.header = mtod(m, void *);
|
---|
756 | m->m_data += sizeof(struct udpiphdr);
|
---|
757 |
|
---|
758 | pchBuffer = mtod(m, char *);
|
---|
759 | fWithTemporalBuffer = false;
|
---|
760 | /*
|
---|
761 | * Even if amounts of bytes on socket is greater than MTU value
|
---|
762 | * Slirp will able fragment it, but we won't create temporal location
|
---|
763 | * here.
|
---|
764 | */
|
---|
765 | if (n > (slirp_size(pData) - sizeof(struct udpiphdr)))
|
---|
766 | {
|
---|
767 | pchBuffer = RTMemAlloc((n) * sizeof(char));
|
---|
768 | if (!pchBuffer)
|
---|
769 | {
|
---|
770 | m_freem(pData, m);
|
---|
771 | return;
|
---|
772 | }
|
---|
773 | fWithTemporalBuffer = true;
|
---|
774 | }
|
---|
775 | ret = recvfrom(so->s, pchBuffer, n, 0,
|
---|
776 | (struct sockaddr *)&addr, &addrlen);
|
---|
777 | if (fWithTemporalBuffer)
|
---|
778 | {
|
---|
779 | if (ret > 0)
|
---|
780 | {
|
---|
781 | m_copyback(pData, m, 0, ret, pchBuffer);
|
---|
782 | /*
|
---|
783 | * If we've met comporison below our size prediction was failed
|
---|
784 | * it's not fatal just we've allocated for nothing. (@todo add counter here
|
---|
785 | * to calculate how rare we here)
|
---|
786 | */
|
---|
787 | if(ret < slirp_size(pData) && !m->m_next)
|
---|
788 | Log(("NAT:udp: Expected size(%d) lesser than real(%d) and less minimal mbuf size(%d)\n",
|
---|
789 | n, ret, slirp_size(pData)));
|
---|
790 | }
|
---|
791 | /* we're freeing buffer anyway */
|
---|
792 | RTMemFree(pchBuffer);
|
---|
793 | }
|
---|
794 | else
|
---|
795 | m->m_len = ret;
|
---|
796 |
|
---|
797 | if (ret < 0)
|
---|
798 | {
|
---|
799 | u_char code = ICMP_UNREACH_PORT;
|
---|
800 |
|
---|
801 | if (errno == EHOSTUNREACH)
|
---|
802 | code = ICMP_UNREACH_HOST;
|
---|
803 | else if (errno == ENETUNREACH)
|
---|
804 | code = ICMP_UNREACH_NET;
|
---|
805 |
|
---|
806 | m_freem(pData, m);
|
---|
807 | if ( errno == EAGAIN
|
---|
808 | || errno == EWOULDBLOCK
|
---|
809 | || errno == EINPROGRESS
|
---|
810 | || errno == ENOTCONN)
|
---|
811 | {
|
---|
812 | return;
|
---|
813 | }
|
---|
814 |
|
---|
815 | Log2((" rx error, tx icmp ICMP_UNREACH:%i\n", code));
|
---|
816 | icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
|
---|
817 | m_freem(pData, so->so_m);
|
---|
818 | so->so_m = NULL;
|
---|
819 | }
|
---|
820 | else
|
---|
821 | {
|
---|
822 | Assert((m_length(m,NULL) == ret));
|
---|
823 | /*
|
---|
824 | * Hack: domain name lookup will be used the most for UDP,
|
---|
825 | * and since they'll only be used once there's no need
|
---|
826 | * for the 4 minute (or whatever) timeout... So we time them
|
---|
827 | * out much quicker (10 seconds for now...)
|
---|
828 | */
|
---|
829 | if (so->so_expire)
|
---|
830 | {
|
---|
831 | if (so->so_fport != RT_H2N_U16_C(53))
|
---|
832 | so->so_expire = curtime + SO_EXPIRE;
|
---|
833 | }
|
---|
834 | /*
|
---|
835 | * last argument should be changed if Slirp will inject IP attributes
|
---|
836 | * Note: Here we can't check if dnsproxy's sent initial request
|
---|
837 | */
|
---|
838 | if ( pData->fUseDnsProxy
|
---|
839 | && so->so_fport == RT_H2N_U16_C(53))
|
---|
840 | dnsproxy_answer(pData, so, m);
|
---|
841 |
|
---|
842 | #if 0
|
---|
843 | if (m->m_len == len)
|
---|
844 | {
|
---|
845 | m_inc(m, MINCSIZE);
|
---|
846 | m->m_len = 0;
|
---|
847 | }
|
---|
848 | #endif
|
---|
849 |
|
---|
850 | /*
|
---|
851 | * If this packet was destined for CTL_ADDR,
|
---|
852 | * make it look like that's where it came from, done by udp_output
|
---|
853 | */
|
---|
854 | udp_output(pData, so, m, &addr);
|
---|
855 | SOCKET_UNLOCK(so);
|
---|
856 | } /* rx error */
|
---|
857 | } /* if ping packet */
|
---|
858 | }
|
---|
859 |
|
---|
860 | /*
|
---|
861 | * sendto() a socket
|
---|
862 | */
|
---|
863 | int
|
---|
864 | sosendto(PNATState pData, struct socket *so, struct mbuf *m)
|
---|
865 | {
|
---|
866 | int ret;
|
---|
867 | struct sockaddr_in *paddr;
|
---|
868 | struct sockaddr addr;
|
---|
869 | #if 0
|
---|
870 | struct sockaddr_in host_addr;
|
---|
871 | #endif
|
---|
872 | caddr_t buf = 0;
|
---|
873 | int mlen;
|
---|
874 |
|
---|
875 | LogFlow(("sosendto: so = %lx, m = %lx\n", (long)so, (long)m));
|
---|
876 |
|
---|
877 | memset(&addr, 0, sizeof(struct sockaddr));
|
---|
878 | #ifdef RT_OS_DARWIN
|
---|
879 | addr.sa_len = sizeof(struct sockaddr_in);
|
---|
880 | #endif
|
---|
881 | paddr = (struct sockaddr_in *)&addr;
|
---|
882 | paddr->sin_family = AF_INET;
|
---|
883 | if ((so->so_faddr.s_addr & RT_H2N_U32(pData->netmask)) == pData->special_addr.s_addr)
|
---|
884 | {
|
---|
885 | /* It's an alias */
|
---|
886 | uint32_t last_byte = RT_N2H_U32(so->so_faddr.s_addr) & ~pData->netmask;
|
---|
887 | switch(last_byte)
|
---|
888 | {
|
---|
889 | #if 0
|
---|
890 | /* handle this case at 'default:' */
|
---|
891 | case CTL_BROADCAST:
|
---|
892 | addr.sin_addr.s_addr = INADDR_BROADCAST;
|
---|
893 | /* Send the packet to host to fully emulate broadcast */
|
---|
894 | /** @todo r=klaus: on Linux host this causes the host to receive
|
---|
895 | * the packet twice for some reason. And I cannot find any place
|
---|
896 | * in the man pages which states that sending a broadcast does not
|
---|
897 | * reach the host itself. */
|
---|
898 | host_addr.sin_family = AF_INET;
|
---|
899 | host_addr.sin_port = so->so_fport;
|
---|
900 | host_addr.sin_addr = our_addr;
|
---|
901 | sendto(so->s, m->m_data, m->m_len, 0,
|
---|
902 | (struct sockaddr *)&host_addr, sizeof (struct sockaddr));
|
---|
903 | break;
|
---|
904 | #endif
|
---|
905 | case CTL_DNS:
|
---|
906 | case CTL_ALIAS:
|
---|
907 | default:
|
---|
908 | if (last_byte == ~pData->netmask)
|
---|
909 | paddr->sin_addr.s_addr = INADDR_BROADCAST;
|
---|
910 | else
|
---|
911 | paddr->sin_addr = loopback_addr;
|
---|
912 | break;
|
---|
913 | }
|
---|
914 | }
|
---|
915 | else
|
---|
916 | paddr->sin_addr = so->so_faddr;
|
---|
917 | paddr->sin_port = so->so_fport;
|
---|
918 |
|
---|
919 | Log2((" sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n",
|
---|
920 | RT_N2H_U16(paddr->sin_port), inet_ntoa(paddr->sin_addr)));
|
---|
921 |
|
---|
922 | /* Don't care what port we get */
|
---|
923 | /*
|
---|
924 | * > nmap -sV -T4 -O -A -v -PU3483 255.255.255.255
|
---|
925 | * generates bodyless messages, annoying memmory management system.
|
---|
926 | */
|
---|
927 | mlen = m_length(m, NULL);
|
---|
928 | if (mlen > 0)
|
---|
929 | {
|
---|
930 | buf = RTMemAlloc(mlen);
|
---|
931 | if (buf == NULL)
|
---|
932 | {
|
---|
933 | return -1;
|
---|
934 | }
|
---|
935 | m_copydata(m, 0, mlen, buf);
|
---|
936 | }
|
---|
937 | ret = sendto(so->s, buf, mlen, 0,
|
---|
938 | (struct sockaddr *)&addr, sizeof (struct sockaddr));
|
---|
939 | if (buf)
|
---|
940 | RTMemFree(buf);
|
---|
941 | if (ret < 0)
|
---|
942 | {
|
---|
943 | Log2(("UDP: sendto fails (%s)\n", strerror(errno)));
|
---|
944 | return -1;
|
---|
945 | }
|
---|
946 |
|
---|
947 | /*
|
---|
948 | * Kill the socket if there's no reply in 4 minutes,
|
---|
949 | * but only if it's an expirable socket
|
---|
950 | */
|
---|
951 | if (so->so_expire)
|
---|
952 | so->so_expire = curtime + SO_EXPIRE;
|
---|
953 | so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
|
---|
954 | return 0;
|
---|
955 | }
|
---|
956 |
|
---|
957 | /*
|
---|
958 | * XXX This should really be tcp_listen
|
---|
959 | */
|
---|
960 | struct socket *
|
---|
961 | solisten(PNATState pData, u_int32_t bind_addr, u_int port, u_int32_t laddr, u_int lport, int flags)
|
---|
962 | {
|
---|
963 | struct sockaddr_in addr;
|
---|
964 | struct socket *so;
|
---|
965 | socklen_t addrlen = sizeof(addr);
|
---|
966 | int s, opt = 1;
|
---|
967 | int status;
|
---|
968 |
|
---|
969 | LogFlow(("solisten: port = %d, laddr = %x, lport = %d, flags = %x\n", port, laddr, lport, flags));
|
---|
970 |
|
---|
971 | if ((so = socreate()) == NULL)
|
---|
972 | {
|
---|
973 | /* RTMemFree(so); Not sofree() ??? free(NULL) == NOP */
|
---|
974 | return NULL;
|
---|
975 | }
|
---|
976 |
|
---|
977 | /* Don't tcp_attach... we don't need so_snd nor so_rcv */
|
---|
978 | if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
|
---|
979 | {
|
---|
980 | RTMemFree(so);
|
---|
981 | return NULL;
|
---|
982 | }
|
---|
983 |
|
---|
984 | SOCKET_LOCK_CREATE(so);
|
---|
985 | SOCKET_LOCK(so);
|
---|
986 | QSOCKET_LOCK(tcb);
|
---|
987 | insque(pData, so,&tcb);
|
---|
988 | NSOCK_INC();
|
---|
989 | QSOCKET_UNLOCK(tcb);
|
---|
990 |
|
---|
991 | /*
|
---|
992 | * SS_FACCEPTONCE sockets must time out.
|
---|
993 | */
|
---|
994 | if (flags & SS_FACCEPTONCE)
|
---|
995 | so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
|
---|
996 |
|
---|
997 | so->so_state = (SS_FACCEPTCONN|flags);
|
---|
998 | so->so_lport = lport; /* Kept in network format */
|
---|
999 | so->so_laddr.s_addr = laddr; /* Ditto */
|
---|
1000 |
|
---|
1001 | memset(&addr, 0, sizeof(addr));
|
---|
1002 | #ifdef RT_OS_DARWIN
|
---|
1003 | addr.sin_len = sizeof(addr);
|
---|
1004 | #endif
|
---|
1005 | addr.sin_family = AF_INET;
|
---|
1006 | addr.sin_addr.s_addr = bind_addr;
|
---|
1007 | addr.sin_port = port;
|
---|
1008 |
|
---|
1009 | if ( ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
---|
1010 | || (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,(char *)&opt, sizeof(int)) < 0)
|
---|
1011 | || (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0)
|
---|
1012 | || (listen(s, 1) < 0))
|
---|
1013 | {
|
---|
1014 | #ifdef RT_OS_WINDOWS
|
---|
1015 | int tmperrno = WSAGetLastError(); /* Don't clobber the real reason we failed */
|
---|
1016 | closesocket(s);
|
---|
1017 | QSOCKET_LOCK(tcb);
|
---|
1018 | sofree(pData, so);
|
---|
1019 | QSOCKET_UNLOCK(tcb);
|
---|
1020 | /* Restore the real errno */
|
---|
1021 | WSASetLastError(tmperrno);
|
---|
1022 | #else
|
---|
1023 | int tmperrno = errno; /* Don't clobber the real reason we failed */
|
---|
1024 | close(s);
|
---|
1025 | QSOCKET_LOCK(tcb);
|
---|
1026 | sofree(pData, so);
|
---|
1027 | QSOCKET_UNLOCK(tcb);
|
---|
1028 | /* Restore the real errno */
|
---|
1029 | errno = tmperrno;
|
---|
1030 | #endif
|
---|
1031 | return NULL;
|
---|
1032 | }
|
---|
1033 | fd_nonblock(s);
|
---|
1034 | setsockopt(s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int));
|
---|
1035 |
|
---|
1036 | getsockname(s,(struct sockaddr *)&addr,&addrlen);
|
---|
1037 | so->so_fport = addr.sin_port;
|
---|
1038 | /* set socket buffers */
|
---|
1039 | opt = pData->socket_rcv;
|
---|
1040 | status = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&opt, sizeof(int));
|
---|
1041 | if (status < 0)
|
---|
1042 | {
|
---|
1043 | LogRel(("NAT: Error(%d) while setting RCV capacity to (%d)\n", errno, opt));
|
---|
1044 | goto no_sockopt;
|
---|
1045 | }
|
---|
1046 | opt = pData->socket_snd;
|
---|
1047 | status = setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&opt, sizeof(int));
|
---|
1048 | if (status < 0)
|
---|
1049 | {
|
---|
1050 | LogRel(("NAT: Error(%d) while setting SND capacity to (%d)\n", errno, opt));
|
---|
1051 | goto no_sockopt;
|
---|
1052 | }
|
---|
1053 | no_sockopt:
|
---|
1054 | if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
|
---|
1055 | so->so_faddr = alias_addr;
|
---|
1056 | else
|
---|
1057 | so->so_faddr = addr.sin_addr;
|
---|
1058 |
|
---|
1059 | so->s = s;
|
---|
1060 | SOCKET_UNLOCK(so);
|
---|
1061 | return so;
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | /*
|
---|
1065 | * Data is available in so_rcv
|
---|
1066 | * Just write() the data to the socket
|
---|
1067 | * XXX not yet...
|
---|
1068 | */
|
---|
1069 | void
|
---|
1070 | sorwakeup(struct socket *so)
|
---|
1071 | {
|
---|
1072 | #if 0
|
---|
1073 | sowrite(so);
|
---|
1074 | FD_CLR(so->s,&writefds);
|
---|
1075 | #endif
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | /*
|
---|
1079 | * Data has been freed in so_snd
|
---|
1080 | * We have room for a read() if we want to
|
---|
1081 | * For now, don't read, it'll be done in the main loop
|
---|
1082 | */
|
---|
1083 | void
|
---|
1084 | sowwakeup(struct socket *so)
|
---|
1085 | {
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | /*
|
---|
1089 | * Various session state calls
|
---|
1090 | * XXX Should be #define's
|
---|
1091 | * The socket state stuff needs work, these often get call 2 or 3
|
---|
1092 | * times each when only 1 was needed
|
---|
1093 | */
|
---|
1094 | void
|
---|
1095 | soisfconnecting(struct socket *so)
|
---|
1096 | {
|
---|
1097 | so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
|
---|
1098 | SS_FCANTSENDMORE|SS_FWDRAIN);
|
---|
1099 | so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | void
|
---|
1103 | soisfconnected(struct socket *so)
|
---|
1104 | {
|
---|
1105 | so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
|
---|
1106 | so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | void
|
---|
1110 | sofcantrcvmore(struct socket *so)
|
---|
1111 | {
|
---|
1112 | if ((so->so_state & SS_NOFDREF) == 0)
|
---|
1113 | {
|
---|
1114 | shutdown(so->s, 0);
|
---|
1115 | }
|
---|
1116 | so->so_state &= ~(SS_ISFCONNECTING);
|
---|
1117 | if (so->so_state & SS_FCANTSENDMORE)
|
---|
1118 | so->so_state = SS_NOFDREF; /* Don't select it */
|
---|
1119 | /* XXX close() here as well? */
|
---|
1120 | else
|
---|
1121 | so->so_state |= SS_FCANTRCVMORE;
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | void
|
---|
1125 | sofcantsendmore(struct socket *so)
|
---|
1126 | {
|
---|
1127 | if ((so->so_state & SS_NOFDREF) == 0)
|
---|
1128 | shutdown(so->s, 1); /* send FIN to fhost */
|
---|
1129 |
|
---|
1130 | so->so_state &= ~(SS_ISFCONNECTING);
|
---|
1131 | if (so->so_state & SS_FCANTRCVMORE)
|
---|
1132 | so->so_state = SS_NOFDREF; /* as above */
|
---|
1133 | else
|
---|
1134 | so->so_state |= SS_FCANTSENDMORE;
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | void
|
---|
1138 | soisfdisconnected(struct socket *so)
|
---|
1139 | {
|
---|
1140 | #if 0
|
---|
1141 | so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED);
|
---|
1142 | close(so->s);
|
---|
1143 | so->so_state = SS_ISFDISCONNECTED;
|
---|
1144 | /*
|
---|
1145 | * XXX Do nothing ... ?
|
---|
1146 | */
|
---|
1147 | #endif
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | /*
|
---|
1151 | * Set write drain mode
|
---|
1152 | * Set CANTSENDMORE once all data has been write()n
|
---|
1153 | */
|
---|
1154 | void
|
---|
1155 | sofwdrain(struct socket *so)
|
---|
1156 | {
|
---|
1157 | if (SBUF_LEN(&so->so_rcv))
|
---|
1158 | so->so_state |= SS_FWDRAIN;
|
---|
1159 | else
|
---|
1160 | sofcantsendmore(so);
|
---|
1161 | }
|
---|
1162 |
|
---|
1163 | static void
|
---|
1164 | send_icmp_to_guest(PNATState pData, char *buff, size_t len, struct socket *so, const struct sockaddr_in *addr)
|
---|
1165 | {
|
---|
1166 | struct ip *ip;
|
---|
1167 | uint32_t dst, src;
|
---|
1168 | char ip_copy[256];
|
---|
1169 | struct icmp *icp;
|
---|
1170 | int old_ip_len = 0;
|
---|
1171 | int hlen, original_hlen = 0;
|
---|
1172 | struct mbuf *m;
|
---|
1173 | struct icmp_msg *icm;
|
---|
1174 | uint8_t proto;
|
---|
1175 | int type = 0;
|
---|
1176 |
|
---|
1177 | ip = (struct ip *)buff;
|
---|
1178 | /* Fix ip->ip_len to contain the total packet length including the header
|
---|
1179 | * in _host_ byte order for all OSes. On Darwin, that value already is in
|
---|
1180 | * host byte order. Solaris and Darwin report only the payload. */
|
---|
1181 | #ifndef RT_OS_DARWIN
|
---|
1182 | ip->ip_len = RT_N2H_U16(ip->ip_len);
|
---|
1183 | #endif
|
---|
1184 | hlen = (ip->ip_hl << 2);
|
---|
1185 | #if defined(RT_OS_SOLARIS) || defined(RT_OS_DARWIN)
|
---|
1186 | ip->ip_len += hlen;
|
---|
1187 | #endif
|
---|
1188 | if (ip->ip_len < hlen + ICMP_MINLEN)
|
---|
1189 | {
|
---|
1190 | Log(("send_icmp_to_guest: ICMP header is too small to understand which type/subtype of the datagram\n"));
|
---|
1191 | return;
|
---|
1192 | }
|
---|
1193 | icp = (struct icmp *)((char *)ip + hlen);
|
---|
1194 |
|
---|
1195 | Log(("ICMP:received msg(t:%d, c:%d)\n", icp->icmp_type, icp->icmp_code));
|
---|
1196 | if ( icp->icmp_type != ICMP_ECHOREPLY
|
---|
1197 | && icp->icmp_type != ICMP_TIMXCEED
|
---|
1198 | && icp->icmp_type != ICMP_UNREACH)
|
---|
1199 | {
|
---|
1200 | return;
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | /*
|
---|
1204 | * ICMP_ECHOREPLY, ICMP_TIMXCEED, ICMP_UNREACH minimal header size is
|
---|
1205 | * ICMP_ECHOREPLY assuming data 0
|
---|
1206 | * icmp_{type(8), code(8), cksum(16),identifier(16),seqnum(16)}
|
---|
1207 | */
|
---|
1208 | if (ip->ip_len < hlen + 8)
|
---|
1209 | {
|
---|
1210 | Log(("send_icmp_to_guest: NAT accept ICMP_{ECHOREPLY, TIMXCEED, UNREACH} the minimum size is 64 (see rfc792)\n"));
|
---|
1211 | return;
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | type = icp->icmp_type;
|
---|
1215 | if ( type == ICMP_TIMXCEED
|
---|
1216 | || type == ICMP_UNREACH)
|
---|
1217 | {
|
---|
1218 | /*
|
---|
1219 | * ICMP_TIMXCEED, ICMP_UNREACH minimal header size is
|
---|
1220 | * icmp_{type(8), code(8), cksum(16),unused(32)} + IP header + 64 bit of original datagram
|
---|
1221 | */
|
---|
1222 | if (ip->ip_len < hlen + 2*8 + sizeof(struct ip))
|
---|
1223 | {
|
---|
1224 | Log(("send_icmp_to_guest: NAT accept ICMP_{TIMXCEED, UNREACH} the minimum size of ipheader + 64 bit of data (see rfc792)\n"));
|
---|
1225 | return;
|
---|
1226 | }
|
---|
1227 | ip = &icp->icmp_ip;
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 | icm = icmp_find_original_mbuf(pData, ip);
|
---|
1231 | if (icm == NULL)
|
---|
1232 | {
|
---|
1233 | Log(("NAT: Can't find the corresponding packet for the received ICMP\n"));
|
---|
1234 | return;
|
---|
1235 | }
|
---|
1236 |
|
---|
1237 | m = icm->im_m;
|
---|
1238 | Assert(m != NULL);
|
---|
1239 |
|
---|
1240 | src = addr->sin_addr.s_addr;
|
---|
1241 | if (type == ICMP_ECHOREPLY)
|
---|
1242 | {
|
---|
1243 | struct ip *ip0 = mtod(m, struct ip *);
|
---|
1244 | struct icmp *icp0 = (struct icmp *)((char *)ip0 + (ip0->ip_hl << 2));
|
---|
1245 | if (icp0->icmp_type != ICMP_ECHO)
|
---|
1246 | {
|
---|
1247 | Log(("NAT: we haven't found echo for this reply\n"));
|
---|
1248 | return;
|
---|
1249 | }
|
---|
1250 | /*
|
---|
1251 | * while combining buffer to send (see ip_icmp.c) we control ICMP header only,
|
---|
1252 | * IP header combined by OS network stack, our local copy of IP header contians values
|
---|
1253 | * in host byte order so no byte order conversion is required. IP headers fields are converting
|
---|
1254 | * in ip_output0 routine only.
|
---|
1255 | */
|
---|
1256 | if ( (ip->ip_len - hlen)
|
---|
1257 | != (ip0->ip_len - (ip0->ip_hl << 2)))
|
---|
1258 | {
|
---|
1259 | Log(("NAT: ECHO(%d) lenght doesn't match ECHOREPLY(%d)\n",
|
---|
1260 | (ip->ip_len - hlen), (ip0->ip_len - (ip0->ip_hl << 2))));
|
---|
1261 | return;
|
---|
1262 | }
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | /* ip points on origianal ip header */
|
---|
1266 | ip = mtod(m, struct ip *);
|
---|
1267 | proto = ip->ip_p;
|
---|
1268 | /* Now ip is pointing on header we've sent from guest */
|
---|
1269 | if ( icp->icmp_type == ICMP_TIMXCEED
|
---|
1270 | || icp->icmp_type == ICMP_UNREACH)
|
---|
1271 | {
|
---|
1272 | old_ip_len = (ip->ip_hl << 2) + 64;
|
---|
1273 | if (old_ip_len > sizeof(ip_copy))
|
---|
1274 | old_ip_len = sizeof(ip_copy);
|
---|
1275 | memcpy(ip_copy, ip, old_ip_len);
|
---|
1276 | }
|
---|
1277 |
|
---|
1278 | /* source address from original IP packet*/
|
---|
1279 | dst = ip->ip_src.s_addr;
|
---|
1280 |
|
---|
1281 | /* overide ther tail of old packet */
|
---|
1282 | ip = mtod(m, struct ip *); /* ip is from mbuf we've overrided */
|
---|
1283 | original_hlen = ip->ip_hl << 2;
|
---|
1284 | /* saves original ip header and options */
|
---|
1285 | m_copyback(pData, m, original_hlen, len - hlen, buff + hlen);
|
---|
1286 | ip->ip_len = m_length(m, NULL);
|
---|
1287 | ip->ip_p = IPPROTO_ICMP; /* the original package could be whatever, but we're response via ICMP*/
|
---|
1288 |
|
---|
1289 | icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
|
---|
1290 | type = icp->icmp_type;
|
---|
1291 | if ( type == ICMP_TIMXCEED
|
---|
1292 | || type == ICMP_UNREACH)
|
---|
1293 | {
|
---|
1294 | /* according RFC 793 error messages required copy of initial IP header + 64 bit */
|
---|
1295 | memcpy(&icp->icmp_ip, ip_copy, old_ip_len);
|
---|
1296 | ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | ip->ip_src.s_addr = src;
|
---|
1300 | ip->ip_dst.s_addr = dst;
|
---|
1301 | icmp_reflect(pData, m);
|
---|
1302 | LIST_REMOVE(icm, im_list);
|
---|
1303 | /* Don't call m_free here*/
|
---|
1304 |
|
---|
1305 | if ( type == ICMP_TIMXCEED
|
---|
1306 | || type == ICMP_UNREACH)
|
---|
1307 | {
|
---|
1308 | icm->im_so->so_m = NULL;
|
---|
1309 | switch (proto)
|
---|
1310 | {
|
---|
1311 | case IPPROTO_UDP:
|
---|
1312 | /*XXX: so->so_m already freed so we shouldn't call sofree */
|
---|
1313 | udp_detach(pData, icm->im_so);
|
---|
1314 | break;
|
---|
1315 | case IPPROTO_TCP:
|
---|
1316 | /*close tcp should be here */
|
---|
1317 | break;
|
---|
1318 | default:
|
---|
1319 | /* do nothing */
|
---|
1320 | break;
|
---|
1321 | }
|
---|
1322 | }
|
---|
1323 | RTMemFree(icm);
|
---|
1324 | }
|
---|
1325 |
|
---|
1326 | #ifdef RT_OS_WINDOWS
|
---|
1327 | static void
|
---|
1328 | sorecvfrom_icmp_win(PNATState pData, struct socket *so)
|
---|
1329 | {
|
---|
1330 | int len;
|
---|
1331 | int i;
|
---|
1332 | struct ip *ip;
|
---|
1333 | struct mbuf *m;
|
---|
1334 | struct icmp *icp;
|
---|
1335 | struct icmp_msg *icm;
|
---|
1336 | struct ip *ip_broken; /* ICMP returns header + 64 bit of packet */
|
---|
1337 | uint32_t src;
|
---|
1338 | ICMP_ECHO_REPLY *icr;
|
---|
1339 | int hlen = 0;
|
---|
1340 | int data_len = 0;
|
---|
1341 | int nbytes = 0;
|
---|
1342 | u_char code = ~0;
|
---|
1343 | int out_len;
|
---|
1344 | int size;
|
---|
1345 |
|
---|
1346 | len = pData->pfIcmpParseReplies(pData->pvIcmpBuffer, pData->szIcmpBuffer);
|
---|
1347 | if (len < 0)
|
---|
1348 | {
|
---|
1349 | LogRel(("NAT: Error (%d) occurred on ICMP receiving\n", GetLastError()));
|
---|
1350 | return;
|
---|
1351 | }
|
---|
1352 | if (len == 0)
|
---|
1353 | return; /* no error */
|
---|
1354 |
|
---|
1355 | icr = (ICMP_ECHO_REPLY *)pData->pvIcmpBuffer;
|
---|
1356 | for (i = 0; i < len; ++i)
|
---|
1357 | {
|
---|
1358 | switch(icr[i].Status)
|
---|
1359 | {
|
---|
1360 | case IP_DEST_HOST_UNREACHABLE:
|
---|
1361 | code = (code != ~0 ? code : ICMP_UNREACH_HOST);
|
---|
1362 | case IP_DEST_NET_UNREACHABLE:
|
---|
1363 | code = (code != ~0 ? code : ICMP_UNREACH_NET);
|
---|
1364 | case IP_DEST_PROT_UNREACHABLE:
|
---|
1365 | code = (code != ~0 ? code : ICMP_UNREACH_PROTOCOL);
|
---|
1366 | /* UNREACH error inject here */
|
---|
1367 | case IP_DEST_PORT_UNREACHABLE:
|
---|
1368 | code = (code != ~0 ? code : ICMP_UNREACH_PORT);
|
---|
1369 | icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, "Error occurred!!!");
|
---|
1370 | m_freem(pData, so->so_m);
|
---|
1371 | so->so_m = NULL;
|
---|
1372 | break;
|
---|
1373 | case IP_SUCCESS: /* echo replied */
|
---|
1374 | out_len = ETH_HLEN + sizeof(struct ip) + 8;
|
---|
1375 | size;
|
---|
1376 | size = MCLBYTES;
|
---|
1377 | if (out_len < MSIZE)
|
---|
1378 | size = MCLBYTES;
|
---|
1379 | else if (out_len < MCLBYTES)
|
---|
1380 | size = MCLBYTES;
|
---|
1381 | else if (out_len < MJUM9BYTES)
|
---|
1382 | size = MJUM9BYTES;
|
---|
1383 | else if (out_len < MJUM16BYTES)
|
---|
1384 | size = MJUM16BYTES;
|
---|
1385 | else
|
---|
1386 | AssertMsgFailed(("Unsupported size"));
|
---|
1387 |
|
---|
1388 | m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, size);
|
---|
1389 | if (m == NULL)
|
---|
1390 | return;
|
---|
1391 | m->m_len = 0;
|
---|
1392 | m->m_data += if_maxlinkhdr;
|
---|
1393 | ip = mtod(m, struct ip *);
|
---|
1394 | ip->ip_src.s_addr = icr[i].Address;
|
---|
1395 | ip->ip_p = IPPROTO_ICMP;
|
---|
1396 | ip->ip_dst.s_addr = so->so_laddr.s_addr; /*XXX: still the hack*/
|
---|
1397 | data_len = sizeof(struct ip);
|
---|
1398 | ip->ip_hl = data_len >> 2; /* requiered for icmp_reflect, no IP options */
|
---|
1399 | ip->ip_ttl = icr[i].Options.Ttl;
|
---|
1400 |
|
---|
1401 | icp = (struct icmp *)&ip[1]; /* no options */
|
---|
1402 | icp->icmp_type = ICMP_ECHOREPLY;
|
---|
1403 | icp->icmp_code = 0;
|
---|
1404 | icp->icmp_id = so->so_icmp_id;
|
---|
1405 | icp->icmp_seq = so->so_icmp_seq;
|
---|
1406 |
|
---|
1407 | data_len += ICMP_MINLEN;
|
---|
1408 |
|
---|
1409 | hlen = (ip->ip_hl << 2);
|
---|
1410 | m->m_pkthdr.header = mtod(m, void *);
|
---|
1411 | m->m_len = data_len;
|
---|
1412 |
|
---|
1413 | m_copyback(pData, m, hlen + 8, icr[i].DataSize, icr[i].Data);
|
---|
1414 |
|
---|
1415 | data_len += icr[i].DataSize;
|
---|
1416 |
|
---|
1417 | ip->ip_len = data_len;
|
---|
1418 | m->m_len = ip->ip_len;
|
---|
1419 |
|
---|
1420 | icmp_reflect(pData, m);
|
---|
1421 | break;
|
---|
1422 | case IP_TTL_EXPIRED_TRANSIT: /* TTL expired */
|
---|
1423 |
|
---|
1424 | ip_broken = icr[i].Data;
|
---|
1425 | icm = icmp_find_original_mbuf(pData, ip_broken);
|
---|
1426 | if (icm == NULL) {
|
---|
1427 | Log(("ICMP: can't find original package (first double word %x)\n", *(uint32_t *)ip_broken));
|
---|
1428 | return;
|
---|
1429 | }
|
---|
1430 | m = icm->im_m;
|
---|
1431 | ip = mtod(m, struct ip *);
|
---|
1432 | ip->ip_ttl = icr[i].Options.Ttl;
|
---|
1433 | src = ip->ip_src.s_addr;
|
---|
1434 | ip->ip_dst.s_addr = src;
|
---|
1435 | ip->ip_dst.s_addr = icr[i].Address;
|
---|
1436 |
|
---|
1437 | hlen = (ip->ip_hl << 2);
|
---|
1438 | icp = (struct icmp *)((char *)ip + hlen);
|
---|
1439 | ip_broken->ip_src.s_addr = src; /*it packet sent from host not from guest*/
|
---|
1440 | data_len = (ip_broken->ip_hl << 2) + 64;
|
---|
1441 |
|
---|
1442 | m->m_len = data_len;
|
---|
1443 | m->m_pkthdr.header = mtod(m, void *);
|
---|
1444 | m_copyback(pData, m, ip->ip_hl >> 2, icr[i].DataSize, icr[i].Data);
|
---|
1445 | icmp_reflect(pData, m);
|
---|
1446 | break;
|
---|
1447 | default:
|
---|
1448 | Log(("ICMP(default): message with Status: %x was received from %x\n", icr[i].Status, icr[i].Address));
|
---|
1449 | break;
|
---|
1450 | }
|
---|
1451 | }
|
---|
1452 | }
|
---|
1453 | #else /* !RT_OS_WINDOWS */
|
---|
1454 | static void sorecvfrom_icmp_unix(PNATState pData, struct socket *so)
|
---|
1455 | {
|
---|
1456 | struct sockaddr_in addr;
|
---|
1457 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
1458 | struct ip ip;
|
---|
1459 | char *buff;
|
---|
1460 | int len = 0;
|
---|
1461 |
|
---|
1462 | /* 1- step: read the ip header */
|
---|
1463 | len = recvfrom(so->s, &ip, sizeof(struct ip), MSG_PEEK,
|
---|
1464 | (struct sockaddr *)&addr, &addrlen);
|
---|
1465 | if ( len < 0
|
---|
1466 | && ( errno == EAGAIN
|
---|
1467 | || errno == EWOULDBLOCK
|
---|
1468 | || errno == EINPROGRESS
|
---|
1469 | || errno == ENOTCONN))
|
---|
1470 | {
|
---|
1471 | Log(("sorecvfrom_icmp_unix: 1 - step can't read IP datagramm (would block)\n"));
|
---|
1472 | return;
|
---|
1473 | }
|
---|
1474 |
|
---|
1475 | if ( len < sizeof(struct ip)
|
---|
1476 | || len < 0
|
---|
1477 | || len == 0)
|
---|
1478 | {
|
---|
1479 | u_char code;
|
---|
1480 | code = ICMP_UNREACH_PORT;
|
---|
1481 |
|
---|
1482 | if (errno == EHOSTUNREACH)
|
---|
1483 | code = ICMP_UNREACH_HOST;
|
---|
1484 | else if (errno == ENETUNREACH)
|
---|
1485 | code = ICMP_UNREACH_NET;
|
---|
1486 |
|
---|
1487 | LogRel((" udp icmp rx errno = %d (%s)\n", errno, strerror(errno)));
|
---|
1488 | icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
|
---|
1489 | m_freem(pData, so->so_m);
|
---|
1490 | so->so_m = NULL;
|
---|
1491 | Log(("sorecvfrom_icmp_unix: 1 - step can't read IP datagramm\n"));
|
---|
1492 | return;
|
---|
1493 | }
|
---|
1494 | /* basic check of IP header */
|
---|
1495 | if ( ip.ip_v != IPVERSION
|
---|
1496 | # ifndef RT_OS_DARWIN
|
---|
1497 | || ip.ip_p != IPPROTO_ICMP
|
---|
1498 | # endif
|
---|
1499 | )
|
---|
1500 | {
|
---|
1501 | Log(("sorecvfrom_icmp_unix: 1 - step IP isn't IPv4\n"));
|
---|
1502 | return;
|
---|
1503 | }
|
---|
1504 | # ifndef RT_OS_DARWIN
|
---|
1505 | /* Darwin reports the IP length already in host byte order. */
|
---|
1506 | ip.ip_len = RT_N2H_U16(ip.ip_len);
|
---|
1507 | # endif
|
---|
1508 | # if defined(RT_OS_SOLARIS) || defined(RT_OS_DARWIN)
|
---|
1509 | /* Solaris and Darwin report the payload only */
|
---|
1510 | ip.ip_len += (ip.ip_hl << 2);
|
---|
1511 | # endif
|
---|
1512 | /* Note: ip->ip_len in host byte order (all OS) */
|
---|
1513 | len = ip.ip_len;
|
---|
1514 | buff = RTMemAlloc(len);
|
---|
1515 | if (buff == NULL)
|
---|
1516 | {
|
---|
1517 | Log(("sorecvfrom_icmp_unix: 1 - step can't allocate enought room for datagram\n"));
|
---|
1518 | return;
|
---|
1519 | }
|
---|
1520 | /* 2 - step: we're reading rest of the datagramm to the buffer */
|
---|
1521 | addrlen = sizeof(struct sockaddr_in);
|
---|
1522 | memset(&addr, 0, addrlen);
|
---|
1523 | len = recvfrom(so->s, buff, len, 0,
|
---|
1524 | (struct sockaddr *)&addr, &addrlen);
|
---|
1525 | if ( len < 0
|
---|
1526 | && ( errno == EAGAIN
|
---|
1527 | || errno == EWOULDBLOCK
|
---|
1528 | || errno == EINPROGRESS
|
---|
1529 | || errno == ENOTCONN))
|
---|
1530 | {
|
---|
1531 | Log(("sorecvfrom_icmp_unix: 2 - step can't read IP body (would block expected:%d)\n",
|
---|
1532 | ip.ip_len));
|
---|
1533 | RTMemFree(buff);
|
---|
1534 | return;
|
---|
1535 | }
|
---|
1536 | if ( len < 0
|
---|
1537 | || len == 0)
|
---|
1538 | {
|
---|
1539 | Log(("sorecvfrom_icmp_unix: 2 - step read of the rest of datagramm is fallen (errno:%d, len:%d expected: %d)\n",
|
---|
1540 | errno, len, (ip.ip_len - sizeof(struct ip))));
|
---|
1541 | RTMemFree(buff);
|
---|
1542 | return;
|
---|
1543 | }
|
---|
1544 | /* len is modified in 2nd read, when the rest of the datagramm was read */
|
---|
1545 | send_icmp_to_guest(pData, buff, len, so, &addr);
|
---|
1546 | RTMemFree(buff);
|
---|
1547 | }
|
---|
1548 | #endif /* !RT_OS_WINDOWS */
|
---|