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