VirtualBox

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

Last change on this file since 15287 was 15261, checked in by vboxsync, 16 years ago

slirp: less noise

  • Property svn:eol-style set to native
File size: 27.1 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) len = so->so_urgc;
294 memcpy(buff, sb->sb_rptr, len);
295 so->so_urgc -= len;
296 if (so->so_urgc)
297 {
298 n = sb->sb_wptr - sb->sb_data;
299 if (n > so->so_urgc)
300 n = so->so_urgc;
301 memcpy(buff + len, sb->sb_data, n);
302 so->so_urgc -= n;
303 len += n;
304 }
305 n = send(so->s, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
306#ifdef DEBUG
307 if (n != len)
308 DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
309#endif
310 DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n",
311 n, so->so_urgc));
312 }
313
314 sb->sb_cc -= n;
315 sb->sb_rptr += n;
316 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
317 sb->sb_rptr -= sb->sb_datalen;
318
319 return n;
320}
321
322/*
323 * Write data from so_rcv to so's socket,
324 * updating all sbuf field as necessary
325 */
326int
327sowrite(PNATState pData, struct socket *so)
328{
329 int n,nn;
330 struct sbuf *sb = &so->so_rcv;
331 size_t len = sb->sb_cc;
332 struct iovec iov[2];
333
334 DEBUG_CALL("sowrite");
335 DEBUG_ARG("so = %lx", (long)so);
336
337 if (so->so_urgc)
338 {
339 sosendoob(so);
340 if (sb->sb_cc == 0)
341 return 0;
342 }
343
344 /*
345 * No need to check if there's something to write,
346 * sowrite wouldn't have been called otherwise
347 */
348
349 len = sb->sb_cc;
350
351 iov[0].iov_base = sb->sb_rptr;
352 iov[1].iov_base = 0;
353 iov[1].iov_len = 0;
354 if (sb->sb_rptr < sb->sb_wptr)
355 {
356 iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
357 /* Should never succeed, but... */
358 if (iov[0].iov_len > len)
359 iov[0].iov_len = len;
360 n = 1;
361 }
362 else
363 {
364 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
365 if (iov[0].iov_len > len)
366 iov[0].iov_len = len;
367 len -= iov[0].iov_len;
368 if (len)
369 {
370 iov[1].iov_base = sb->sb_data;
371 iov[1].iov_len = sb->sb_wptr - sb->sb_data;
372 if (iov[1].iov_len > len)
373 iov[1].iov_len = len;
374 n = 2;
375 }
376 else
377 n = 1;
378 }
379 /* Check if there's urgent data to send, and if so, send it */
380#ifdef HAVE_READV
381 nn = writev(so->s, (const struct iovec *)iov, n);
382 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
383#else
384 nn = send(so->s, iov[0].iov_base, iov[0].iov_len, 0);
385#endif
386 /* This should never happen, but people tell me it does *shrug* */
387 if (nn < 0 && (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
388 return 0;
389
390 if (nn < 0 || (nn == 0 && iov[0].iov_len > 0))
391 {
392 DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
393 so->so_state, errno));
394 sofcantsendmore(so);
395 tcp_sockclosed(pData, sototcpcb(so));
396 return -1;
397 }
398
399#ifndef HAVE_READV
400 if (n == 2 && nn == iov[0].iov_len)
401 {
402 int ret;
403 ret = send(so->s, iov[1].iov_base, iov[1].iov_len,0);
404 if (ret > 0)
405 nn += ret;
406 }
407 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
408#endif
409
410 /* Update sbuf */
411 sb->sb_cc -= nn;
412 sb->sb_rptr += nn;
413 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
414 sb->sb_rptr -= sb->sb_datalen;
415
416 /*
417 * If in DRAIN mode, and there's no more data, set
418 * it CANTSENDMORE
419 */
420 if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
421 sofcantsendmore(so);
422
423 return nn;
424}
425
426/*
427 * recvfrom() a UDP socket
428 */
429void
430sorecvfrom(PNATState pData, struct socket *so)
431{
432 struct sockaddr_in addr;
433 socklen_t addrlen = sizeof(struct sockaddr_in);
434
435 DEBUG_CALL("sorecvfrom");
436 DEBUG_ARG("so = %lx", (long)so);
437
438 if (so->so_type == IPPROTO_ICMP)
439 {
440 /* This is a "ping" reply */
441#if !defined(VBOX_WITH_SLIRP_ICMP) || (defined(VBOX_WITH_SLIRP_ICMP) && !defined(RT_OS_WINDOWS))
442 sorecvfrom_icmp_unix(pData, so);
443#endif
444#if defined(VBOX_WITH_SLIRP_ICMP) && defined(RT_OS_WINDOWS)
445 sorecvfrom_icmp_win(pData, so);
446#endif
447 udp_detach(pData, so);
448 }
449 else
450 {
451 /* A "normal" UDP packet */
452 struct mbuf *m;
453 size_t len;
454 u_long n;
455
456 if (!(m = m_get(pData)))
457 return;
458 m->m_data += if_maxlinkhdr;
459
460 /*
461 * XXX Shouldn't FIONREAD packets destined for port 53,
462 * but I don't know the max packet size for DNS lookups
463 */
464 len = M_FREEROOM(m);
465 /* if (so->so_fport != htons(53)) */
466 {
467 ioctlsocket(so->s, FIONREAD, &n);
468
469 if (n > len)
470 {
471 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
472 m_inc(m, n);
473 len = M_FREEROOM(m);
474 }
475 }
476
477 m->m_len = recvfrom(so->s, m->m_data, len, 0,
478 (struct sockaddr *)&addr, &addrlen);
479 DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
480 m->m_len, errno,strerror(errno)));
481 if(m->m_len < 0)
482 {
483 u_char code=ICMP_UNREACH_PORT;
484
485 if (errno == EHOSTUNREACH)
486 code=ICMP_UNREACH_HOST;
487 else if(errno == ENETUNREACH)
488 code=ICMP_UNREACH_NET;
489
490 DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
491 icmp_error(pData, so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
492 m_free(pData, m);
493 }
494 else
495 {
496 /*
497 * Hack: domain name lookup will be used the most for UDP,
498 * and since they'll only be used once there's no need
499 * for the 4 minute (or whatever) timeout... So we time them
500 * out much quicker (10 seconds for now...)
501 */
502 if (so->so_expire)
503 {
504 if (so->so_fport == htons(53))
505 so->so_expire = curtime + SO_EXPIREFAST;
506 else
507 so->so_expire = curtime + SO_EXPIRE;
508 }
509
510#if 0
511 if (m->m_len == len)
512 {
513 m_inc(m, MINCSIZE);
514 m->m_len = 0;
515 }
516#endif
517
518 /*
519 * If this packet was destined for CTL_ADDR,
520 * make it look like that's where it came from, done by udp_output
521 */
522 udp_output(pData, so, m, &addr);
523 } /* rx error */
524 } /* if ping packet */
525}
526
527/*
528 * sendto() a socket
529 */
530int
531sosendto(PNATState pData, struct socket *so, struct mbuf *m)
532{
533 int ret;
534 struct sockaddr_in addr;
535#if 0
536 struct sockaddr_in host_addr;
537#endif
538
539 DEBUG_CALL("sosendto");
540 DEBUG_ARG("so = %lx", (long)so);
541 DEBUG_ARG("m = %lx", (long)m);
542
543 addr.sin_family = AF_INET;
544 if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
545 {
546 /* It's an alias */
547 uint32_t last_byte = ntohl(so->so_faddr.s_addr) & ~pData->netmask;
548 switch(last_byte)
549 {
550#if 0
551 /* handle this case at 'default:' */
552 case CTL_BROADCAST:
553 addr.sin_addr.s_addr = INADDR_BROADCAST;
554 /* Send the packet to host to fully emulate broadcast */
555 /** @todo r=klaus: on Linux host this causes the host to receive
556 * the packet twice for some reason. And I cannot find any place
557 * in the man pages which states that sending a broadcast does not
558 * reach the host itself. */
559 host_addr.sin_family = AF_INET;
560 host_addr.sin_port = so->so_fport;
561 host_addr.sin_addr = our_addr;
562 sendto(so->s, m->m_data, m->m_len, 0,
563 (struct sockaddr *)&host_addr, sizeof (struct sockaddr));
564 break;
565#endif
566 case CTL_DNS:
567 if (!get_dns_addr(pData, &dns_addr))
568 addr.sin_addr = dns_addr;
569 else
570 addr.sin_addr = loopback_addr;
571 break;
572 case CTL_ALIAS:
573 default:
574 if (last_byte == ~pData->netmask)
575 addr.sin_addr.s_addr = INADDR_BROADCAST;
576 else
577 addr.sin_addr = loopback_addr;
578 break;
579 }
580 }
581 else
582 addr.sin_addr = so->so_faddr;
583 addr.sin_port = so->so_fport;
584
585 DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n",
586 ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
587
588 /* Don't care what port we get */
589 ret = sendto(so->s, m->m_data, m->m_len, 0,
590 (struct sockaddr *)&addr, sizeof (struct sockaddr));
591 if (ret < 0)
592 return -1;
593
594 /*
595 * Kill the socket if there's no reply in 4 minutes,
596 * but only if it's an expirable socket
597 */
598 if (so->so_expire)
599 so->so_expire = curtime + SO_EXPIRE;
600 so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
601 return 0;
602}
603
604/*
605 * XXX This should really be tcp_listen
606 */
607struct socket *
608solisten(PNATState pData, u_int port, u_int32_t laddr, u_int lport, int flags)
609{
610 struct sockaddr_in addr;
611 struct socket *so;
612 socklen_t addrlen = sizeof(addr);
613 int s, opt = 1;
614
615 DEBUG_CALL("solisten");
616 DEBUG_ARG("port = %d", port);
617 DEBUG_ARG("laddr = %x", laddr);
618 DEBUG_ARG("lport = %d", lport);
619 DEBUG_ARG("flags = %x", flags);
620
621 if ((so = socreate()) == NULL)
622 {
623 /* free(so); Not sofree() ??? free(NULL) == NOP */
624 return NULL;
625 }
626
627 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
628 if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
629 {
630 free(so);
631 return NULL;
632 }
633 insque(pData, so,&tcb);
634
635 /*
636 * SS_FACCEPTONCE sockets must time out.
637 */
638 if (flags & SS_FACCEPTONCE)
639 so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
640
641 so->so_state = (SS_FACCEPTCONN|flags);
642 so->so_lport = lport; /* Kept in network format */
643 so->so_laddr.s_addr = laddr; /* Ditto */
644
645 addr.sin_family = AF_INET;
646 addr.sin_addr.s_addr = INADDR_ANY;
647 addr.sin_port = port;
648
649 if ( ((s = socket(AF_INET,SOCK_STREAM,0)) < 0)
650 || (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int)) < 0)
651 || (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0)
652 || (listen(s,1) < 0))
653 {
654#ifdef RT_OS_WINDOWS
655 int tmperrno = WSAGetLastError(); /* Don't clobber the real reason we failed */
656 closesocket(s);
657 sofree(pData, so);
658 /* Restore the real errno */
659 WSASetLastError(tmperrno);
660#else
661 int tmperrno = errno; /* Don't clobber the real reason we failed */
662 close(s);
663 sofree(pData, so);
664 /* Restore the real errno */
665 errno = tmperrno;
666#endif
667 return NULL;
668 }
669 setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
670
671 getsockname(s,(struct sockaddr *)&addr,&addrlen);
672 so->so_fport = addr.sin_port;
673 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
674 so->so_faddr = alias_addr;
675 else
676 so->so_faddr = addr.sin_addr;
677
678 so->s = s;
679 return so;
680}
681
682/*
683 * Data is available in so_rcv
684 * Just write() the data to the socket
685 * XXX not yet...
686 */
687void
688sorwakeup(struct socket *so)
689{
690#if 0
691 sowrite(so);
692 FD_CLR(so->s,&writefds);
693#endif
694}
695
696/*
697 * Data has been freed in so_snd
698 * We have room for a read() if we want to
699 * For now, don't read, it'll be done in the main loop
700 */
701void
702sowwakeup(struct socket *so)
703{
704}
705
706/*
707 * Various session state calls
708 * XXX Should be #define's
709 * The socket state stuff needs work, these often get call 2 or 3
710 * times each when only 1 was needed
711 */
712void
713soisfconnecting(struct socket *so)
714{
715 so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
716 SS_FCANTSENDMORE|SS_FWDRAIN);
717 so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
718}
719
720void
721soisfconnected(struct socket *so)
722{
723 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
724 so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
725}
726
727void
728sofcantrcvmore(struct socket *so)
729{
730 if ((so->so_state & SS_NOFDREF) == 0)
731 {
732 shutdown(so->s,0);
733 }
734 so->so_state &= ~(SS_ISFCONNECTING);
735 if (so->so_state & SS_FCANTSENDMORE)
736 so->so_state = SS_NOFDREF; /* Don't select it */
737 /* XXX close() here as well? */
738 else
739 so->so_state |= SS_FCANTRCVMORE;
740}
741
742void
743sofcantsendmore(struct socket *so)
744{
745 if ((so->so_state & SS_NOFDREF) == 0)
746 shutdown(so->s, 1); /* send FIN to fhost */
747
748 so->so_state &= ~(SS_ISFCONNECTING);
749 if (so->so_state & SS_FCANTRCVMORE)
750 so->so_state = SS_NOFDREF; /* as above */
751 else
752 so->so_state |= SS_FCANTSENDMORE;
753}
754
755void
756soisfdisconnected(struct socket *so)
757{
758#if 0
759 so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED);
760 close(so->s);
761 so->so_state = SS_ISFDISCONNECTED;
762 /*
763 * XXX Do nothing ... ?
764 */
765#endif
766}
767
768/*
769 * Set write drain mode
770 * Set CANTSENDMORE once all data has been write()n
771 */
772void
773sofwdrain(struct socket *so)
774{
775 if (so->so_rcv.sb_cc)
776 so->so_state |= SS_FWDRAIN;
777 else
778 sofcantsendmore(so);
779}
780
781#ifdef VBOX_WITH_SLIRP_ICMP
782static void
783send_icmp_to_guest(PNATState pData, char *buff, size_t len, struct socket *so, const struct sockaddr_in *addr)
784{
785 struct ip *ip;
786 uint32_t dst,src;
787 char ip_copy[256];
788 struct icmp *icp;
789 int old_ip_len;
790 struct mbuf *m;
791 struct icmp_msg *icm;
792
793 ip = (struct ip *)buff;
794 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
795
796 if (icp->icmp_type != ICMP_ECHOREPLY && icp->icmp_type != ICMP_TIMXCEED)
797 {
798 LogRel(("received ICMP(t:%d, c:%d)\n", icp->icmp_type, icp->icmp_code));
799 return;
800 }
801
802 if (icp->icmp_type == ICMP_TIMXCEED)
803 ip = &icp->icmp_ip;
804
805 icm = icmp_find_original_mbuf(pData, ip);
806
807 if (icm == NULL)
808 {
809 LogRel(("NAT: Can't find the corresponding packet for the received ICMP\n"));
810 return;
811 }
812
813 m = icm->im_m;
814 Assert(m != NULL);
815
816 src = addr->sin_addr.s_addr;
817
818 ip = mtod(m, struct ip *);
819 /* Now ip is pointing on header we've sent from guest */
820 if (icp->icmp_type == ICMP_TIMXCEED)
821 {
822 old_ip_len = (ip->ip_hl << 2) + 64;
823 memcpy(ip_copy, ip, old_ip_len);
824 }
825
826 /* source address from original IP packet*/
827 dst = ip->ip_src.s_addr;
828
829 /* overide ther tail of old packet */
830 memcpy(m->m_data, buff, len);
831 m->m_len = len;
832 ip = mtod(m, struct ip *); /* ip is from mbuf we've overrided */
833
834 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
835 if (icp->icmp_type == ICMP_TIMXCEED)
836 {
837 /* according RFC 793 error messages required copy of initial IP header + 64 bit */
838 memcpy(&icp->icmp_ip, ip_copy, old_ip_len);
839 ip->ip_tos=((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
840 }
841
842 /* the low level expects fields to be in host format so let's convert them*/
843 NTOHS(ip->ip_len);
844 NTOHS(ip->ip_off);
845 NTOHS(ip->ip_id);
846 ip->ip_src.s_addr = src;
847 ip->ip_dst.s_addr = dst;
848 icmp_reflect(pData, m);
849 LIST_REMOVE(icm, im_list);
850 /* Don't call m_free here*/
851 free(icm);
852}
853
854# ifdef RT_OS_WINDOWS
855static void
856sorecvfrom_icmp_win(PNATState pData, struct socket *so)
857{
858 int len;
859 int i;
860 struct ip *ip;
861 struct mbuf *m;
862 struct icmp *icp;
863 struct icmp_msg *icm;
864 struct ip *ip_broken; /* ICMP returns header + 64 bit of packet */
865 uint32_t src;
866 ICMP_ECHO_REPLY *icr;
867 u_char code = ~0;
868 len = pData->pfIcmpParseReplies(pData->pvIcmpBuffer, pData->szIcmpBuffer);
869 if (len < 0)
870 {
871 LogRel(("NAT: Error (%d) occurred on ICMP receiving\n", GetLastError()));
872 return;
873 }
874 if (len == 0)
875 return; /* no error */
876 LogRel(("IcmpParseReplies returns %ld\n", len));
877 icr = (ICMP_ECHO_REPLY *)pData->pvIcmpBuffer;
878 for (i = 0; i < len; ++i)
879 {
880 switch(icr[i].Status)
881 {
882 case IP_DEST_HOST_UNREACHABLE:
883 code = (code != ~0 ? code : ICMP_UNREACH_HOST);
884 case IP_DEST_NET_UNREACHABLE:
885 code = (code != ~0 ? code : ICMP_UNREACH_NET);
886 case IP_DEST_PROT_UNREACHABLE:
887 code = (code != ~0 ? code : ICMP_UNREACH_PROTOCOL);
888 /* UNREACH error inject here */
889 case IP_DEST_PORT_UNREACHABLE:
890 code = (code != ~0 ? code : ICMP_UNREACH_PORT);
891 icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, "Error occurred!!!");
892 break;
893 case IP_SUCCESS: /* echo replied */
894 m = m_get(pData);
895 ip = mtod(m, struct ip *);
896 ip->ip_src.s_addr = icr[i].Address;
897 ip->ip_p = IPPROTO_ICMP;
898 ip->ip_dst.s_addr = so->so_laddr.s_addr; /*XXX: still the hack*/
899 ip->ip_hl = sizeof(struct ip) >> 2; /* requiered for icmp_reflect, no IP options */
900 ip->ip_ttl = icr[i].Options.Ttl;
901
902 icp = (struct icmp *)&ip[1]; /* no options */
903 icp->icmp_type = ICMP_ECHOREPLY;
904 icp->icmp_code = 0;
905 icp->icmp_id = so->so_icmp_id;
906 icp->icmp_seq = so->so_icmp_seq;
907 memcpy(icp->icmp_data, icr[i].Data, icr[i].DataSize);
908
909 ip->ip_len = sizeof(struct ip) + ICMP_MINLEN + icr[i].DataSize;
910 m->m_len = ip->ip_len;
911
912 icmp_reflect(pData, m);
913 case IP_TTL_EXPIRED_TRANSIT: /* TTL expired */
914
915 ip_broken = icr[i].Data;
916 icm = icmp_find_original_mbuf(pData, ip_broken);
917 if (icm == NULL) {
918 LogRel(("ICMP: can't find original package (first double word %x)\n", *(uint32_t *)ip_broken));
919 return;
920 }
921 m = icm->im_m;
922 ip = mtod(m, struct ip *);
923 ip->ip_ttl = icr[i].Options.Ttl;
924 src = ip->ip_src.s_addr;
925 ip->ip_dst.s_addr = src;
926 ip->ip_dst.s_addr = icr[i].Address;
927 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
928 ip_broken->ip_src.s_addr = src; /*it packet sent from host not from guest*/
929 memcpy(icp->icmp_data, ip_broken, (ip_broken->ip_hl << 2) + 64);
930 icmp_reflect(pData, m);
931 break;
932 default:
933 LogRel(("ICMP(default): message with Status: %x was received from %x\n", icr[i].Status, icr[i].Address));
934 break;
935 }
936 }
937}
938# endif /* RT_OS_WINDOWS */
939#endif /* VBOX_WITH_SLIRP_ICMP */
940
941static void sorecvfrom_icmp_unix(PNATState pData, struct socket *so)
942{
943 struct sockaddr_in addr;
944 socklen_t addrlen = sizeof(struct sockaddr_in);
945 char buff[1500];
946 int len;
947 len = recvfrom(so->s, buff, 1500, 0,
948 (struct sockaddr *)&addr, &addrlen);
949 /* XXX Check if reply is "correct"? */
950
951 if (len == -1 || len == 0)
952 {
953 u_char code = ICMP_UNREACH_PORT;
954
955 if (errno == EHOSTUNREACH)
956 code=ICMP_UNREACH_HOST;
957 else if(errno == ENETUNREACH)
958 code=ICMP_UNREACH_NET;
959
960 DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
961 errno,strerror(errno)));
962 icmp_error(pData, so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
963 }
964 else
965 {
966#ifdef VBOX_WITH_SLIRP_ICMP
967 send_icmp_to_guest(pData, buff, len, so, &addr);
968#else
969 icmp_reflect(pData, so->so_m);
970 so->so_m = 0; /* Don't m_free() it again! */
971#endif
972 }
973}
974
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