VirtualBox

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

Last change on this file since 15387 was 15369, checked in by vboxsync, 16 years ago

icmp:slirp: other Darwin fix

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

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