VirtualBox

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

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

NAT: fixed memmory corruption in new Unix multiplexing mechanism

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