VirtualBox

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

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

NAT:typo

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