VirtualBox

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

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

NAT: cosmetics

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