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 != RT_H2N_U16_C(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 | if (n > (if_mtu - len))
|
---|
648 | {
|
---|
649 | n = if_mtu - len; /* can't read than we can put in the mbuf*/
|
---|
650 | }
|
---|
651 | len += n;
|
---|
652 |
|
---|
653 | size = MCLBYTES;
|
---|
654 | if (len < MSIZE)
|
---|
655 | size = MCLBYTES;
|
---|
656 | else if (len < MCLBYTES)
|
---|
657 | size = MCLBYTES;
|
---|
658 | else if (len < MJUM9BYTES)
|
---|
659 | size = MJUM9BYTES;
|
---|
660 | else if (len < MJUM16BYTES)
|
---|
661 | size = MJUM16BYTES;
|
---|
662 | else
|
---|
663 | AssertMsgFailed(("Unsupported size"));
|
---|
664 |
|
---|
665 | m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, size);
|
---|
666 | m->m_data += ETH_HLEN;
|
---|
667 | m->m_pkthdr.header = mtod(m, void *);
|
---|
668 | m->m_data += sizeof(struct udpiphdr);
|
---|
669 | ret = recvfrom(so->s, mtod(m, char *), n, 0,
|
---|
670 | (struct sockaddr *)&addr, &addrlen);
|
---|
671 | /* @todo (vvl) check which flags and type should be passed */
|
---|
672 | #endif
|
---|
673 | m->m_len = ret;
|
---|
674 | if (ret < 0)
|
---|
675 | {
|
---|
676 | u_char code = ICMP_UNREACH_PORT;
|
---|
677 |
|
---|
678 | if (errno == EHOSTUNREACH)
|
---|
679 | code = ICMP_UNREACH_HOST;
|
---|
680 | else if (errno == ENETUNREACH)
|
---|
681 | code = ICMP_UNREACH_NET;
|
---|
682 |
|
---|
683 | m_free(pData, m);
|
---|
684 | if ( errno == EAGAIN
|
---|
685 | || errno == EWOULDBLOCK
|
---|
686 | || errno == EINPROGRESS
|
---|
687 | || errno == ENOTCONN)
|
---|
688 | {
|
---|
689 | return;
|
---|
690 | }
|
---|
691 |
|
---|
692 | Log2((" rx error, tx icmp ICMP_UNREACH:%i\n", code));
|
---|
693 | icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
|
---|
694 | so->so_m = NULL;
|
---|
695 | }
|
---|
696 | else
|
---|
697 | {
|
---|
698 | /*
|
---|
699 | * Hack: domain name lookup will be used the most for UDP,
|
---|
700 | * and since they'll only be used once there's no need
|
---|
701 | * for the 4 minute (or whatever) timeout... So we time them
|
---|
702 | * out much quicker (10 seconds for now...)
|
---|
703 | */
|
---|
704 | if (so->so_expire)
|
---|
705 | {
|
---|
706 | if (so->so_fport != RT_H2N_U16_C(53))
|
---|
707 | so->so_expire = curtime + SO_EXPIRE;
|
---|
708 | }
|
---|
709 | /*
|
---|
710 | * last argument should be changed if Slirp will inject IP attributes
|
---|
711 | * Note: Here we can't check if dnsproxy's sent initial request
|
---|
712 | */
|
---|
713 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
714 | if (so->so_fport == RT_H2N_U16_C(53))
|
---|
715 | dnsproxy_answer(pData, so, m);
|
---|
716 | #endif
|
---|
717 |
|
---|
718 | #if 0
|
---|
719 | if (m->m_len == len)
|
---|
720 | {
|
---|
721 | m_inc(m, MINCSIZE);
|
---|
722 | m->m_len = 0;
|
---|
723 | }
|
---|
724 | #endif
|
---|
725 |
|
---|
726 | /*
|
---|
727 | * If this packet was destined for CTL_ADDR,
|
---|
728 | * make it look like that's where it came from, done by udp_output
|
---|
729 | */
|
---|
730 | udp_output(pData, so, m, &addr);
|
---|
731 | SOCKET_UNLOCK(so);
|
---|
732 | } /* rx error */
|
---|
733 | } /* if ping packet */
|
---|
734 | }
|
---|
735 |
|
---|
736 | /*
|
---|
737 | * sendto() a socket
|
---|
738 | */
|
---|
739 | int
|
---|
740 | sosendto(PNATState pData, struct socket *so, struct mbuf *m)
|
---|
741 | {
|
---|
742 | int ret;
|
---|
743 | struct sockaddr_in *paddr;
|
---|
744 | struct sockaddr addr;
|
---|
745 | #if 0
|
---|
746 | struct sockaddr_in host_addr;
|
---|
747 | #endif
|
---|
748 | #ifdef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
749 | caddr_t buf;
|
---|
750 | int mlen;
|
---|
751 | #endif
|
---|
752 |
|
---|
753 | DEBUG_CALL("sosendto");
|
---|
754 | DEBUG_ARG("so = %lx", (long)so);
|
---|
755 | DEBUG_ARG("m = %lx", (long)m);
|
---|
756 |
|
---|
757 | memset(&addr, 0, sizeof(struct sockaddr));
|
---|
758 | #ifdef RT_OS_DARWIN
|
---|
759 | addr.sa_len = sizeof(struct sockaddr_in);
|
---|
760 | #endif
|
---|
761 | paddr = (struct sockaddr_in *)&addr;
|
---|
762 | paddr->sin_family = AF_INET;
|
---|
763 | if ((so->so_faddr.s_addr & RT_H2N_U32(pData->netmask)) == pData->special_addr.s_addr)
|
---|
764 | {
|
---|
765 | /* It's an alias */
|
---|
766 | uint32_t last_byte = RT_N2H_U32(so->so_faddr.s_addr) & ~pData->netmask;
|
---|
767 | switch(last_byte)
|
---|
768 | {
|
---|
769 | #if 0
|
---|
770 | /* handle this case at 'default:' */
|
---|
771 | case CTL_BROADCAST:
|
---|
772 | addr.sin_addr.s_addr = INADDR_BROADCAST;
|
---|
773 | /* Send the packet to host to fully emulate broadcast */
|
---|
774 | /** @todo r=klaus: on Linux host this causes the host to receive
|
---|
775 | * the packet twice for some reason. And I cannot find any place
|
---|
776 | * in the man pages which states that sending a broadcast does not
|
---|
777 | * reach the host itself. */
|
---|
778 | host_addr.sin_family = AF_INET;
|
---|
779 | host_addr.sin_port = so->so_fport;
|
---|
780 | host_addr.sin_addr = our_addr;
|
---|
781 | sendto(so->s, m->m_data, m->m_len, 0,
|
---|
782 | (struct sockaddr *)&host_addr, sizeof (struct sockaddr));
|
---|
783 | break;
|
---|
784 | #endif
|
---|
785 | case CTL_DNS:
|
---|
786 | case CTL_ALIAS:
|
---|
787 | default:
|
---|
788 | if (last_byte == ~pData->netmask)
|
---|
789 | paddr->sin_addr.s_addr = INADDR_BROADCAST;
|
---|
790 | else
|
---|
791 | paddr->sin_addr = loopback_addr;
|
---|
792 | break;
|
---|
793 | }
|
---|
794 | }
|
---|
795 | else
|
---|
796 | paddr->sin_addr = so->so_faddr;
|
---|
797 | paddr->sin_port = so->so_fport;
|
---|
798 |
|
---|
799 | DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n",
|
---|
800 | RT_N2H_U16(paddr->sin_port), inet_ntoa(paddr->sin_addr)));
|
---|
801 |
|
---|
802 | /* Don't care what port we get */
|
---|
803 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
804 | ret = sendto(so->s, m->m_data, m->m_len, 0, &addr, sizeof (struct sockaddr_in));
|
---|
805 | #else
|
---|
806 | mlen = m_length(m, NULL);
|
---|
807 | buf = RTMemAlloc(mlen);
|
---|
808 | if (buf == NULL)
|
---|
809 | {
|
---|
810 | return -1;
|
---|
811 | }
|
---|
812 | m_copydata(m, 0, mlen, buf);
|
---|
813 | ret = sendto(so->s, buf, mlen, 0,
|
---|
814 | (struct sockaddr *)&addr, sizeof (struct sockaddr));
|
---|
815 | #endif
|
---|
816 | if (ret < 0)
|
---|
817 | {
|
---|
818 | Log2(("UDP: sendto fails (%s)\n", strerror(errno)));
|
---|
819 | return -1;
|
---|
820 | }
|
---|
821 |
|
---|
822 | /*
|
---|
823 | * Kill the socket if there's no reply in 4 minutes,
|
---|
824 | * but only if it's an expirable socket
|
---|
825 | */
|
---|
826 | if (so->so_expire)
|
---|
827 | so->so_expire = curtime + SO_EXPIRE;
|
---|
828 | so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
|
---|
829 | return 0;
|
---|
830 | }
|
---|
831 |
|
---|
832 | /*
|
---|
833 | * XXX This should really be tcp_listen
|
---|
834 | */
|
---|
835 | struct socket *
|
---|
836 | solisten(PNATState pData, u_int32_t bind_addr, u_int port, u_int32_t laddr, u_int lport, int flags)
|
---|
837 | {
|
---|
838 | struct sockaddr_in addr;
|
---|
839 | struct socket *so;
|
---|
840 | socklen_t addrlen = sizeof(addr);
|
---|
841 | int s, opt = 1;
|
---|
842 | int status;
|
---|
843 |
|
---|
844 | DEBUG_CALL("solisten");
|
---|
845 | DEBUG_ARG("port = %d", port);
|
---|
846 | DEBUG_ARG("laddr = %x", laddr);
|
---|
847 | DEBUG_ARG("lport = %d", lport);
|
---|
848 | DEBUG_ARG("flags = %x", flags);
|
---|
849 |
|
---|
850 | if ((so = socreate()) == NULL)
|
---|
851 | {
|
---|
852 | /* RTMemFree(so); Not sofree() ??? free(NULL) == NOP */
|
---|
853 | return NULL;
|
---|
854 | }
|
---|
855 |
|
---|
856 | /* Don't tcp_attach... we don't need so_snd nor so_rcv */
|
---|
857 | if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
|
---|
858 | {
|
---|
859 | RTMemFree(so);
|
---|
860 | return NULL;
|
---|
861 | }
|
---|
862 |
|
---|
863 | SOCKET_LOCK_CREATE(so);
|
---|
864 | SOCKET_LOCK(so);
|
---|
865 | QSOCKET_LOCK(tcb);
|
---|
866 | insque(pData, so,&tcb);
|
---|
867 | NSOCK_INC();
|
---|
868 | QSOCKET_UNLOCK(tcb);
|
---|
869 |
|
---|
870 | /*
|
---|
871 | * SS_FACCEPTONCE sockets must time out.
|
---|
872 | */
|
---|
873 | if (flags & SS_FACCEPTONCE)
|
---|
874 | so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
|
---|
875 |
|
---|
876 | so->so_state = (SS_FACCEPTCONN|flags);
|
---|
877 | so->so_lport = lport; /* Kept in network format */
|
---|
878 | so->so_laddr.s_addr = laddr; /* Ditto */
|
---|
879 |
|
---|
880 | memset(&addr, 0, sizeof(addr));
|
---|
881 | #ifdef RT_OS_DARWIN
|
---|
882 | addr.sin_len = sizeof(addr);
|
---|
883 | #endif
|
---|
884 | addr.sin_family = AF_INET;
|
---|
885 | addr.sin_addr.s_addr = bind_addr;
|
---|
886 | addr.sin_port = port;
|
---|
887 |
|
---|
888 | if ( ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
---|
889 | || (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,(char *)&opt, sizeof(int)) < 0)
|
---|
890 | || (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0)
|
---|
891 | || (listen(s, 1) < 0))
|
---|
892 | {
|
---|
893 | #ifdef RT_OS_WINDOWS
|
---|
894 | int tmperrno = WSAGetLastError(); /* Don't clobber the real reason we failed */
|
---|
895 | closesocket(s);
|
---|
896 | QSOCKET_LOCK(tcb);
|
---|
897 | sofree(pData, so);
|
---|
898 | QSOCKET_UNLOCK(tcb);
|
---|
899 | /* Restore the real errno */
|
---|
900 | WSASetLastError(tmperrno);
|
---|
901 | #else
|
---|
902 | int tmperrno = errno; /* Don't clobber the real reason we failed */
|
---|
903 | close(s);
|
---|
904 | QSOCKET_LOCK(tcb);
|
---|
905 | sofree(pData, so);
|
---|
906 | QSOCKET_UNLOCK(tcb);
|
---|
907 | /* Restore the real errno */
|
---|
908 | errno = tmperrno;
|
---|
909 | #endif
|
---|
910 | return NULL;
|
---|
911 | }
|
---|
912 | fd_nonblock(s);
|
---|
913 | setsockopt(s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int));
|
---|
914 |
|
---|
915 | getsockname(s,(struct sockaddr *)&addr,&addrlen);
|
---|
916 | so->so_fport = addr.sin_port;
|
---|
917 | /* set socket buffers */
|
---|
918 | opt = pData->socket_rcv;
|
---|
919 | status = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&opt, sizeof(int));
|
---|
920 | if (status < 0)
|
---|
921 | {
|
---|
922 | LogRel(("NAT: Error(%d) while setting RCV capacity to (%d)\n", errno, opt));
|
---|
923 | goto no_sockopt;
|
---|
924 | }
|
---|
925 | opt = pData->socket_snd;
|
---|
926 | status = setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&opt, sizeof(int));
|
---|
927 | if (status < 0)
|
---|
928 | {
|
---|
929 | LogRel(("NAT: Error(%d) while setting SND capacity to (%d)\n", errno, opt));
|
---|
930 | goto no_sockopt;
|
---|
931 | }
|
---|
932 | no_sockopt:
|
---|
933 | if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
|
---|
934 | so->so_faddr = alias_addr;
|
---|
935 | else
|
---|
936 | so->so_faddr = addr.sin_addr;
|
---|
937 |
|
---|
938 | so->s = s;
|
---|
939 | SOCKET_UNLOCK(so);
|
---|
940 | return so;
|
---|
941 | }
|
---|
942 |
|
---|
943 | /*
|
---|
944 | * Data is available in so_rcv
|
---|
945 | * Just write() the data to the socket
|
---|
946 | * XXX not yet...
|
---|
947 | */
|
---|
948 | void
|
---|
949 | sorwakeup(struct socket *so)
|
---|
950 | {
|
---|
951 | #if 0
|
---|
952 | sowrite(so);
|
---|
953 | FD_CLR(so->s,&writefds);
|
---|
954 | #endif
|
---|
955 | }
|
---|
956 |
|
---|
957 | /*
|
---|
958 | * Data has been freed in so_snd
|
---|
959 | * We have room for a read() if we want to
|
---|
960 | * For now, don't read, it'll be done in the main loop
|
---|
961 | */
|
---|
962 | void
|
---|
963 | sowwakeup(struct socket *so)
|
---|
964 | {
|
---|
965 | }
|
---|
966 |
|
---|
967 | /*
|
---|
968 | * Various session state calls
|
---|
969 | * XXX Should be #define's
|
---|
970 | * The socket state stuff needs work, these often get call 2 or 3
|
---|
971 | * times each when only 1 was needed
|
---|
972 | */
|
---|
973 | void
|
---|
974 | soisfconnecting(struct socket *so)
|
---|
975 | {
|
---|
976 | so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
|
---|
977 | SS_FCANTSENDMORE|SS_FWDRAIN);
|
---|
978 | so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
|
---|
979 | }
|
---|
980 |
|
---|
981 | void
|
---|
982 | soisfconnected(struct socket *so)
|
---|
983 | {
|
---|
984 | so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
|
---|
985 | so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
|
---|
986 | }
|
---|
987 |
|
---|
988 | void
|
---|
989 | sofcantrcvmore(struct socket *so)
|
---|
990 | {
|
---|
991 | if ((so->so_state & SS_NOFDREF) == 0)
|
---|
992 | {
|
---|
993 | shutdown(so->s, 0);
|
---|
994 | }
|
---|
995 | so->so_state &= ~(SS_ISFCONNECTING);
|
---|
996 | if (so->so_state & SS_FCANTSENDMORE)
|
---|
997 | so->so_state = SS_NOFDREF; /* Don't select it */
|
---|
998 | /* XXX close() here as well? */
|
---|
999 | else
|
---|
1000 | so->so_state |= SS_FCANTRCVMORE;
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 | void
|
---|
1004 | sofcantsendmore(struct socket *so)
|
---|
1005 | {
|
---|
1006 | if ((so->so_state & SS_NOFDREF) == 0)
|
---|
1007 | shutdown(so->s, 1); /* send FIN to fhost */
|
---|
1008 |
|
---|
1009 | so->so_state &= ~(SS_ISFCONNECTING);
|
---|
1010 | if (so->so_state & SS_FCANTRCVMORE)
|
---|
1011 | so->so_state = SS_NOFDREF; /* as above */
|
---|
1012 | else
|
---|
1013 | so->so_state |= SS_FCANTSENDMORE;
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | void
|
---|
1017 | soisfdisconnected(struct socket *so)
|
---|
1018 | {
|
---|
1019 | #if 0
|
---|
1020 | so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED);
|
---|
1021 | close(so->s);
|
---|
1022 | so->so_state = SS_ISFDISCONNECTED;
|
---|
1023 | /*
|
---|
1024 | * XXX Do nothing ... ?
|
---|
1025 | */
|
---|
1026 | #endif
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | /*
|
---|
1030 | * Set write drain mode
|
---|
1031 | * Set CANTSENDMORE once all data has been write()n
|
---|
1032 | */
|
---|
1033 | void
|
---|
1034 | sofwdrain(struct socket *so)
|
---|
1035 | {
|
---|
1036 | if (so->so_rcv.sb_cc)
|
---|
1037 | so->so_state |= SS_FWDRAIN;
|
---|
1038 | else
|
---|
1039 | sofcantsendmore(so);
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | static void
|
---|
1043 | send_icmp_to_guest(PNATState pData, char *buff, size_t len, struct socket *so, const struct sockaddr_in *addr)
|
---|
1044 | {
|
---|
1045 | struct ip *ip;
|
---|
1046 | uint32_t dst, src;
|
---|
1047 | char ip_copy[256];
|
---|
1048 | struct icmp *icp;
|
---|
1049 | int old_ip_len = 0;
|
---|
1050 | int hlen, original_hlen = 0;
|
---|
1051 | struct mbuf *m;
|
---|
1052 | struct icmp_msg *icm;
|
---|
1053 | uint8_t proto;
|
---|
1054 | int type = 0;
|
---|
1055 | int m_room;
|
---|
1056 |
|
---|
1057 | ip = (struct ip *)buff;
|
---|
1058 | hlen = (ip->ip_hl << 2);
|
---|
1059 | if (RT_N2H_U16(ip->ip_len) < hlen + ICMP_MINLEN)
|
---|
1060 | {
|
---|
1061 | Log(("send_icmp_to_guest: ICMP header is too small to understand which type/subtype of the datagram\n"));
|
---|
1062 | return;
|
---|
1063 | }
|
---|
1064 | icp = (struct icmp *)((char *)ip + hlen);
|
---|
1065 |
|
---|
1066 | Log(("ICMP:received msg(t:%d, c:%d)\n", icp->icmp_type, icp->icmp_code));
|
---|
1067 | if ( icp->icmp_type != ICMP_ECHOREPLY
|
---|
1068 | && icp->icmp_type != ICMP_TIMXCEED
|
---|
1069 | && icp->icmp_type != ICMP_UNREACH)
|
---|
1070 | {
|
---|
1071 | return;
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | /*
|
---|
1075 | * ICMP_ECHOREPLY, ICMP_TIMXCEED, ICMP_UNREACH minimal header size is
|
---|
1076 | * ICMP_ECHOREPLY assuming data 0
|
---|
1077 | * icmp_{type(8), code(8), cksum(16),identifier(16),seqnum(16)}
|
---|
1078 | */
|
---|
1079 | if (RT_N2H_U16(ip->ip_len) < hlen + 8)
|
---|
1080 | {
|
---|
1081 | Log(("send_icmp_to_guest: NAT accept ICMP_{ECHOREPLY, TIMXCEED, UNREACH} the minimum size is 64 (see rfc792)\n"));
|
---|
1082 | return;
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | type = icp->icmp_type;
|
---|
1086 | if ( type == ICMP_TIMXCEED
|
---|
1087 | || type == ICMP_UNREACH)
|
---|
1088 | {
|
---|
1089 | /*
|
---|
1090 | * ICMP_TIMXCEED, ICMP_UNREACH minimal header size is
|
---|
1091 | * icmp_{type(8), code(8), cksum(16),unused(32)} + IP header + 64 bit of original datagram
|
---|
1092 | */
|
---|
1093 | if (RT_N2H_U16(ip->ip_len) < hlen + 2*8 + sizeof(struct ip))
|
---|
1094 | {
|
---|
1095 | Log(("send_icmp_to_guest: NAT accept ICMP_{TIMXCEED, UNREACH} the minimum size of ipheader + 64 bit of data (see rfc792)\n"));
|
---|
1096 | return;
|
---|
1097 | }
|
---|
1098 | ip = &icp->icmp_ip;
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | icm = icmp_find_original_mbuf(pData, ip);
|
---|
1102 |
|
---|
1103 | if (icm == NULL)
|
---|
1104 | {
|
---|
1105 | Log(("NAT: Can't find the corresponding packet for the received ICMP\n"));
|
---|
1106 | return;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | m = icm->im_m;
|
---|
1110 | Assert(m != NULL);
|
---|
1111 |
|
---|
1112 | src = addr->sin_addr.s_addr;
|
---|
1113 | if (type == ICMP_ECHOREPLY)
|
---|
1114 | {
|
---|
1115 | struct ip *ip0 = mtod(m, struct ip *);
|
---|
1116 | struct icmp *icp0 = (struct icmp *)((char *)ip0 + (ip0->ip_hl << 2));
|
---|
1117 | if (icp0->icmp_type != ICMP_ECHO)
|
---|
1118 | {
|
---|
1119 | Log(("NAT: we haven't found echo for this reply\n"));
|
---|
1120 | return;
|
---|
1121 | }
|
---|
1122 | if ( (RT_N2H_U16(ip->ip_len) - hlen )
|
---|
1123 | != (ip0->ip_len - (ip0->ip_hl << 2))) /* the IP header in the list is in host format */
|
---|
1124 | {
|
---|
1125 | Log(("NAT: ECHO lenght doesn't match ECHOREPLY\n"));
|
---|
1126 | return;
|
---|
1127 | }
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | ip = mtod(m, struct ip *);
|
---|
1131 | proto = ip->ip_p;
|
---|
1132 | /* Now ip is pointing on header we've sent from guest */
|
---|
1133 | if ( icp->icmp_type == ICMP_TIMXCEED
|
---|
1134 | || icp->icmp_type == ICMP_UNREACH)
|
---|
1135 | {
|
---|
1136 | old_ip_len = (ip->ip_hl << 2) + 64;
|
---|
1137 | if (old_ip_len > sizeof(ip_copy))
|
---|
1138 | old_ip_len = sizeof(ip_copy);
|
---|
1139 | memcpy(ip_copy, ip, old_ip_len);
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | /* source address from original IP packet*/
|
---|
1143 | dst = ip->ip_src.s_addr;
|
---|
1144 |
|
---|
1145 | /* overide ther tail of old packet */
|
---|
1146 | ip = mtod(m, struct ip *); /* ip is from mbuf we've overrided */
|
---|
1147 | original_hlen = ip->ip_hl << 2;
|
---|
1148 | /* saves original ip header and options */
|
---|
1149 | /* m_room space in the saved m buffer */
|
---|
1150 | m_room = m->m_len + M_TRAILINGSPACE(m) - original_hlen;
|
---|
1151 | if (m_room >= len - hlen)
|
---|
1152 | memcpy(m->m_data + original_hlen, buff + hlen, len - hlen);
|
---|
1153 | else
|
---|
1154 | {
|
---|
1155 | int size = m->m_size;
|
---|
1156 | /* we need involve ether header lenght into new buffer buffer calculation */
|
---|
1157 | m_inc(m, if_maxlinkhdr + len - hlen + original_hlen);
|
---|
1158 | if (size == m->m_size)
|
---|
1159 | {
|
---|
1160 | Log(("send_icmp_to_guest: extending buffer was failed (packet is dropped)\n"));
|
---|
1161 | return;
|
---|
1162 | }
|
---|
1163 | memcpy(m->m_data + original_hlen, buff + hlen, len - hlen);
|
---|
1164 | }
|
---|
1165 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
1166 | m->m_len = len - hlen + original_hlen;
|
---|
1167 | ip->ip_len = m->m_len;
|
---|
1168 | #else
|
---|
1169 | ip->ip_len = m_length(m, NULL);
|
---|
1170 | #endif
|
---|
1171 | ip->ip_p = IPPROTO_ICMP; /* the original package could be whatever, but we're response via ICMP*/
|
---|
1172 |
|
---|
1173 | icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
|
---|
1174 | type = icp->icmp_type;
|
---|
1175 | if ( type == ICMP_TIMXCEED
|
---|
1176 | || type == ICMP_UNREACH)
|
---|
1177 | {
|
---|
1178 | /* according RFC 793 error messages required copy of initial IP header + 64 bit */
|
---|
1179 | memcpy(&icp->icmp_ip, ip_copy, old_ip_len);
|
---|
1180 | ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | ip->ip_src.s_addr = src;
|
---|
1184 | ip->ip_dst.s_addr = dst;
|
---|
1185 | icmp_reflect(pData, m);
|
---|
1186 | LIST_REMOVE(icm, im_list);
|
---|
1187 | /* Don't call m_free here*/
|
---|
1188 |
|
---|
1189 | if ( type == ICMP_TIMXCEED
|
---|
1190 | || type == ICMP_UNREACH)
|
---|
1191 | {
|
---|
1192 | icm->im_so->so_m = NULL;
|
---|
1193 | switch (proto)
|
---|
1194 | {
|
---|
1195 | case IPPROTO_UDP:
|
---|
1196 | /*XXX: so->so_m already freed so we shouldn't call sofree */
|
---|
1197 | udp_detach(pData, icm->im_so);
|
---|
1198 | break;
|
---|
1199 | case IPPROTO_TCP:
|
---|
1200 | /*close tcp should be here */
|
---|
1201 | break;
|
---|
1202 | default:
|
---|
1203 | /* do nothing */
|
---|
1204 | break;
|
---|
1205 | }
|
---|
1206 | }
|
---|
1207 | RTMemFree(icm);
|
---|
1208 | }
|
---|
1209 |
|
---|
1210 | #ifdef RT_OS_WINDOWS
|
---|
1211 | static void
|
---|
1212 | sorecvfrom_icmp_win(PNATState pData, struct socket *so)
|
---|
1213 | {
|
---|
1214 | int len;
|
---|
1215 | int i;
|
---|
1216 | struct ip *ip;
|
---|
1217 | struct mbuf *m;
|
---|
1218 | struct icmp *icp;
|
---|
1219 | struct icmp_msg *icm;
|
---|
1220 | struct ip *ip_broken; /* ICMP returns header + 64 bit of packet */
|
---|
1221 | uint32_t src;
|
---|
1222 | ICMP_ECHO_REPLY *icr;
|
---|
1223 | int hlen = 0;
|
---|
1224 | int data_len = 0;
|
---|
1225 | int nbytes = 0;
|
---|
1226 | u_char code = ~0;
|
---|
1227 |
|
---|
1228 | len = pData->pfIcmpParseReplies(pData->pvIcmpBuffer, pData->szIcmpBuffer);
|
---|
1229 | if (len < 0)
|
---|
1230 | {
|
---|
1231 | LogRel(("NAT: Error (%d) occurred on ICMP receiving\n", GetLastError()));
|
---|
1232 | return;
|
---|
1233 | }
|
---|
1234 | if (len == 0)
|
---|
1235 | return; /* no error */
|
---|
1236 |
|
---|
1237 | icr = (ICMP_ECHO_REPLY *)pData->pvIcmpBuffer;
|
---|
1238 | for (i = 0; i < len; ++i)
|
---|
1239 | {
|
---|
1240 | switch(icr[i].Status)
|
---|
1241 | {
|
---|
1242 | case IP_DEST_HOST_UNREACHABLE:
|
---|
1243 | code = (code != ~0 ? code : ICMP_UNREACH_HOST);
|
---|
1244 | case IP_DEST_NET_UNREACHABLE:
|
---|
1245 | code = (code != ~0 ? code : ICMP_UNREACH_NET);
|
---|
1246 | case IP_DEST_PROT_UNREACHABLE:
|
---|
1247 | code = (code != ~0 ? code : ICMP_UNREACH_PROTOCOL);
|
---|
1248 | /* UNREACH error inject here */
|
---|
1249 | case IP_DEST_PORT_UNREACHABLE:
|
---|
1250 | code = (code != ~0 ? code : ICMP_UNREACH_PORT);
|
---|
1251 | icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, "Error occurred!!!");
|
---|
1252 | so->so_m = NULL;
|
---|
1253 | break;
|
---|
1254 | case IP_SUCCESS: /* echo replied */
|
---|
1255 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
1256 | m = m_get(pData);
|
---|
1257 | #else
|
---|
1258 | m = m_gethdr(pData, M_NOWAIT, MT_HEADER);
|
---|
1259 | #endif
|
---|
1260 | m->m_data += if_maxlinkhdr;
|
---|
1261 | ip = mtod(m, struct ip *);
|
---|
1262 | ip->ip_src.s_addr = icr[i].Address;
|
---|
1263 | ip->ip_p = IPPROTO_ICMP;
|
---|
1264 | ip->ip_dst.s_addr = so->so_laddr.s_addr; /*XXX: still the hack*/
|
---|
1265 | data_len = sizeof(struct ip);
|
---|
1266 | ip->ip_hl = data_len >> 2; /* requiered for icmp_reflect, no IP options */
|
---|
1267 | ip->ip_ttl = icr[i].Options.Ttl;
|
---|
1268 |
|
---|
1269 | icp = (struct icmp *)&ip[1]; /* no options */
|
---|
1270 | icp->icmp_type = ICMP_ECHOREPLY;
|
---|
1271 | icp->icmp_code = 0;
|
---|
1272 | icp->icmp_id = so->so_icmp_id;
|
---|
1273 | icp->icmp_seq = so->so_icmp_seq;
|
---|
1274 |
|
---|
1275 | data_len += ICMP_MINLEN;
|
---|
1276 |
|
---|
1277 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
1278 | nbytes = (data_len + icr[i].DataSize > m->m_size? m->m_size - data_len: icr[i].DataSize);
|
---|
1279 | memcpy(icp->icmp_data, icr[i].Data, nbytes);
|
---|
1280 | #else
|
---|
1281 | AssertMsgFailed(("ICMP"));
|
---|
1282 | #endif
|
---|
1283 |
|
---|
1284 | data_len += icr[i].DataSize;
|
---|
1285 |
|
---|
1286 | ip->ip_len = data_len;
|
---|
1287 | m->m_len = ip->ip_len;
|
---|
1288 |
|
---|
1289 | icmp_reflect(pData, m);
|
---|
1290 | break;
|
---|
1291 | case IP_TTL_EXPIRED_TRANSIT: /* TTL expired */
|
---|
1292 |
|
---|
1293 | ip_broken = icr[i].Data;
|
---|
1294 | icm = icmp_find_original_mbuf(pData, ip_broken);
|
---|
1295 | if (icm == NULL) {
|
---|
1296 | Log(("ICMP: can't find original package (first double word %x)\n", *(uint32_t *)ip_broken));
|
---|
1297 | return;
|
---|
1298 | }
|
---|
1299 | m = icm->im_m;
|
---|
1300 | ip = mtod(m, struct ip *);
|
---|
1301 | ip->ip_ttl = icr[i].Options.Ttl;
|
---|
1302 | src = ip->ip_src.s_addr;
|
---|
1303 | ip->ip_dst.s_addr = src;
|
---|
1304 | ip->ip_dst.s_addr = icr[i].Address;
|
---|
1305 |
|
---|
1306 | hlen = (ip->ip_hl << 2);
|
---|
1307 | icp = (struct icmp *)((char *)ip + hlen);
|
---|
1308 | ip_broken->ip_src.s_addr = src; /*it packet sent from host not from guest*/
|
---|
1309 | data_len = (ip_broken->ip_hl << 2) + 64;
|
---|
1310 |
|
---|
1311 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
1312 | nbytes =(hlen + ICMP_MINLEN + data_len > m->m_size? m->m_size - (hlen + ICMP_MINLEN): data_len);
|
---|
1313 | memcpy(icp->icmp_data, ip_broken, nbytes);
|
---|
1314 | #else
|
---|
1315 | AssertMsgFailed(("ICMP"));
|
---|
1316 | #endif
|
---|
1317 | icmp_reflect(pData, m);
|
---|
1318 | break;
|
---|
1319 | default:
|
---|
1320 | Log(("ICMP(default): message with Status: %x was received from %x\n", icr[i].Status, icr[i].Address));
|
---|
1321 | break;
|
---|
1322 | }
|
---|
1323 | }
|
---|
1324 | }
|
---|
1325 | #else /* RT_OS_WINDOWS */
|
---|
1326 | static void sorecvfrom_icmp_unix(PNATState pData, struct socket *so)
|
---|
1327 | {
|
---|
1328 | struct sockaddr_in addr;
|
---|
1329 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
1330 | struct ip ip;
|
---|
1331 | char *buff;
|
---|
1332 | int len = 0;
|
---|
1333 |
|
---|
1334 | /* 1- step: read the ip header */
|
---|
1335 | len = recvfrom(so->s, &ip, sizeof(struct ip), MSG_PEEK,
|
---|
1336 | (struct sockaddr *)&addr, &addrlen);
|
---|
1337 | if ( len < 0
|
---|
1338 | && ( errno == EAGAIN
|
---|
1339 | || errno == EWOULDBLOCK
|
---|
1340 | || errno == EINPROGRESS
|
---|
1341 | || errno == ENOTCONN))
|
---|
1342 | {
|
---|
1343 | Log(("sorecvfrom_icmp_unix: 1 - step can't read IP datagramm (would block)\n"));
|
---|
1344 | return;
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | if ( len < sizeof(struct ip)
|
---|
1348 | || len < 0
|
---|
1349 | || len == 0)
|
---|
1350 | {
|
---|
1351 | u_char code;
|
---|
1352 | code = ICMP_UNREACH_PORT;
|
---|
1353 |
|
---|
1354 | if (errno == EHOSTUNREACH)
|
---|
1355 | code = ICMP_UNREACH_HOST;
|
---|
1356 | else if (errno == ENETUNREACH)
|
---|
1357 | code = ICMP_UNREACH_NET;
|
---|
1358 |
|
---|
1359 | LogRel((" udp icmp rx errno = %d-%s\n",
|
---|
1360 | errno, strerror(errno)));
|
---|
1361 | icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
|
---|
1362 | so->so_m = NULL;
|
---|
1363 | Log(("sorecvfrom_icmp_unix: 1 - step can't read IP datagramm \n"));
|
---|
1364 | return;
|
---|
1365 | }
|
---|
1366 | /* basic check of IP header */
|
---|
1367 | if ( ip.ip_v != IPVERSION
|
---|
1368 | || ip.ip_p != IPPROTO_ICMP)
|
---|
1369 | {
|
---|
1370 | Log(("sorecvfrom_icmp_unix: 1 - step IP isn't IPv4 \n"));
|
---|
1371 | return;
|
---|
1372 | }
|
---|
1373 |
|
---|
1374 | len = RT_N2H_U16(ip.ip_len);
|
---|
1375 | buff = RTMemAlloc(len);
|
---|
1376 | if (buff == NULL)
|
---|
1377 | {
|
---|
1378 | Log(("sorecvfrom_icmp_unix: 1 - step can't allocate enought room for datagram\n"));
|
---|
1379 | return;
|
---|
1380 | }
|
---|
1381 | /* 2 - step: we're reading rest of the datagramm to the buffer */
|
---|
1382 | addrlen = sizeof(struct sockaddr_in);
|
---|
1383 | memset(&addr, 0, addrlen);
|
---|
1384 | len = recvfrom(so->s, buff, len, 0,
|
---|
1385 | (struct sockaddr *)&addr, &addrlen);
|
---|
1386 | if ( len < 0
|
---|
1387 | && ( errno == EAGAIN
|
---|
1388 | || errno == EWOULDBLOCK
|
---|
1389 | || errno == EINPROGRESS
|
---|
1390 | || errno == ENOTCONN))
|
---|
1391 | {
|
---|
1392 | Log(("sorecvfrom_icmp_unix: 2 - step can't read IP body (would block expected:%d)\n",
|
---|
1393 | RT_N2H_U16(ip.ip_len)));
|
---|
1394 | RTMemFree(buff);
|
---|
1395 | return;
|
---|
1396 | }
|
---|
1397 | if ( len < 0
|
---|
1398 | || len < (RT_N2H_U16(ip.ip_len))
|
---|
1399 | || len == 0)
|
---|
1400 | {
|
---|
1401 | Log(("sorecvfrom_icmp_unix: 2 - step read of the rest of datagramm is fallen (errno:%d, len:%d expected: %d)\n",
|
---|
1402 | errno, len, (RT_N2H_U16(ip.ip_len) - sizeof(struct ip))));
|
---|
1403 | RTMemFree(buff);
|
---|
1404 | return;
|
---|
1405 | }
|
---|
1406 | /* len is modified in 2nd read, when the rest of the datagramm was read */
|
---|
1407 | send_icmp_to_guest(pData, buff, len, so, &addr);
|
---|
1408 | RTMemFree(buff);
|
---|
1409 | }
|
---|
1410 | #endif /* !RT_OS_WINDOWS */
|
---|