VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/socket.c@ 23253

最後變更 在這個檔案從23253是 23241,由 vboxsync 提交於 16 年 前

NAT: do UDP/ICMP errno analisys.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette