socket.c revision 56ddefc0b36ad1ad21a595bf75c4ac460085c24b
0N/A/*
3261N/A * Copyright (c) 1995 Danny Gasparovski.
0N/A *
0N/A * Please read the file COPYRIGHT for the
0N/A * terms and conditions of the copyright.
0N/A */
2362N/A
0N/A#define WANT_SYS_IOCTL_H
2362N/A#include <slirp.h>
0N/A#include "ip_icmp.h"
0N/A#include "main.h"
0N/A#ifdef __sun__
0N/A#include <sys/filio.h>
0N/A#endif
0N/A#if defined(VBOX_WITH_SLIRP_ICMP) && defined (RT_OS_WINDOWS)
0N/A#include <iphlpapi.h>
0N/A#include <icmpapi.h>
0N/A#endif
0N/A
0N/A#ifdef VBOX_WITH_SLIRP_ICMP
2362N/Astatic void send_icmp_to_guest(PNATState, char *, size_t, struct socket *, const struct sockaddr_in *);
2362N/Astatic void sorecvfrom_icmp_win(PNATState, struct socket *);
2362N/A#endif
0N/Astatic void sorecvfrom_icmp_unix(PNATState, struct socket *);
0N/A
0N/Avoid
0N/Aso_init()
0N/A{
0N/A}
0N/A
0N/A
0N/Astruct socket *
0N/Asolookup(struct socket *head, struct in_addr laddr,
0N/A u_int lport, struct in_addr faddr, u_int fport)
0N/A{
0N/A struct socket *so;
0N/A
0N/A for (so = head->so_next; so != head; so = so->so_next)
0N/A {
0N/A if ( so->so_lport == lport
0N/A && so->so_laddr.s_addr == laddr.s_addr
0N/A && so->so_faddr.s_addr == faddr.s_addr
0N/A && so->so_fport == fport)
0N/A return so;
0N/A }
0N/A
0N/A return (struct socket *)NULL;
0N/A}
0N/A
0N/A/*
0N/A * Create a new socket, initialise the fields
0N/A * It is the responsibility of the caller to
0N/A * insque() it into the correct linked-list
0N/A */
0N/Astruct socket *
0N/Asocreate()
0N/A{
0N/A struct socket *so;
0N/A
0N/A so = (struct socket *)malloc(sizeof(struct socket));
0N/A if(so)
0N/A {
0N/A memset(so, 0, sizeof(struct socket));
0N/A so->so_state = SS_NOFDREF;
0N/A so->s = -1;
0N/A }
0N/A return so;
0N/A}
0N/A
0N/A/*
0N/A * remque and free a socket, clobber cache
0N/A */
0N/Avoid
0N/Asofree(PNATState pData, struct socket *so)
0N/A{
0N/A if (so == tcp_last_so)
0N/A tcp_last_so = &tcb;
0N/A else if (so == udp_last_so)
0N/A udp_last_so = &udb;
0N/A
0N/A m_free(pData, so->so_m);
0N/A
0N/A if(so->so_next && so->so_prev)
0N/A remque(pData, so); /* crashes if so is not in a queue */
0N/A
0N/A free(so);
0N/A}
0N/A
0N/A/*
0N/A * Read from so's socket into sb_snd, updating all relevant sbuf fields
0N/A * NOTE: This will only be called if it is select()ed for reading, so
0N/A * a read() of 0 (or less) means it's disconnected
0N/A */
0N/Aint
0N/Asoread(PNATState pData, struct socket *so, int fCloseIfNothingRead)
1999N/A{
1999N/A int n, nn, lss, total;
0N/A struct sbuf *sb = &so->so_snd;
1999N/A size_t len = sb->sb_datalen - sb->sb_cc;
1999N/A struct iovec iov[2];
0N/A int mss = so->so_tcpcb->t_maxseg;
1999N/A
0N/A DEBUG_CALL("soread");
0N/A DEBUG_ARG("so = %lx", (long )so);
0N/A
0N/A /*
0N/A * No need to check if there's enough room to read.
0N/A * soread wouldn't have been called if there weren't
0N/A */
0N/A
0N/A len = sb->sb_datalen - sb->sb_cc;
0N/A
0N/A iov[0].iov_base = sb->sb_wptr;
0N/A iov[1].iov_base = 0;
0N/A iov[1].iov_len = 0;
0N/A if (sb->sb_wptr < sb->sb_rptr)
0N/A {
1173N/A iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
1173N/A /* Should never succeed, but... */
1173N/A if (iov[0].iov_len > len)
1173N/A iov[0].iov_len = len;
1173N/A if (iov[0].iov_len > mss)
1999N/A iov[0].iov_len -= iov[0].iov_len%mss;
1173N/A n = 1;
1999N/A }
1173N/A else
1173N/A {
1173N/A iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
1173N/A /* Should never succeed, but... */
1173N/A if (iov[0].iov_len > len)
1173N/A iov[0].iov_len = len;
1999N/A len -= iov[0].iov_len;
1173N/A if (len)
1999N/A {
1173N/A iov[1].iov_base = sb->sb_data;
0N/A iov[1].iov_len = sb->sb_rptr - sb->sb_data;
0N/A if(iov[1].iov_len > len)
0N/A iov[1].iov_len = len;
0N/A total = iov[0].iov_len + iov[1].iov_len;
0N/A if (total > mss)
0N/A {
0N/A lss = total % mss;
0N/A if (iov[1].iov_len > lss)
0N/A {
0N/A iov[1].iov_len -= lss;
0N/A n = 2;
0N/A }
0N/A else
0N/A {
0N/A lss -= iov[1].iov_len;
0N/A iov[0].iov_len -= lss;
0N/A n = 1;
0N/A }
0N/A }
0N/A else
0N/A n = 2;
0N/A }
0N/A else
0N/A {
0N/A if (iov[0].iov_len > mss)
0N/A iov[0].iov_len -= iov[0].iov_len%mss;
0N/A n = 1;
0N/A }
0N/A }
0N/A
0N/A#ifdef HAVE_READV
0N/A nn = readv(so->s, (struct iovec *)iov, n);
0N/A DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
0N/A#else
0N/A nn = recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
0N/A#endif
0N/A if (nn <= 0)
0N/A {
0N/A#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
0N/A /*
0N/A * Special case for WSAEnumNetworkEvents: If we receive 0 bytes that
0N/A * _could_ mean that the connection is closed. But we will receive an
0N/A * FD_CLOSE event later if the connection was _really_ closed. With
0N/A * www.youtube.com I see this very often. Closing the socket too early
0N/A * would be dangerous.
0N/A */
0N/A if (nn == 0 && !fCloseIfNothingRead)
0N/A return 0;
0N/A#endif
0N/A if (nn < 0 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
0N/A return 0;
0N/A else
0N/A {
0N/A /* nn == 0 means peer has performed an orderly shutdown */
0N/A DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n",
0N/A nn, errno,strerror(errno)));
0N/A sofcantrcvmore(so);
0N/A tcp_sockclosed(pData, sototcpcb(so));
0N/A return -1;
0N/A }
0N/A }
0N/A
0N/A#ifndef HAVE_READV
0N/A /*
0N/A * If there was no error, try and read the second time round
0N/A * We read again if n = 2 (ie, there's another part of the buffer)
0N/A * and we read as much as we could in the first read
0N/A * We don't test for <= 0 this time, because there legitimately
0N/A * might not be any more data (since the socket is non-blocking),
0N/A * a close will be detected on next iteration.
0N/A * A return of -1 wont (shouldn't) happen, since it didn't happen above
0N/A */
0N/A if (n == 2 && nn == iov[0].iov_len)
0N/A {
1173N/A int ret;
1173N/A ret = recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
1173N/A if (ret > 0)
1173N/A nn += ret;
1173N/A }
1173N/A
1173N/A DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
1173N/A#endif
1173N/A
1173N/A /* Update fields */
1173N/A sb->sb_cc += nn;
1173N/A sb->sb_wptr += nn;
1173N/A if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
1173N/A sb->sb_wptr -= sb->sb_datalen;
1173N/A return nn;
1173N/A}
1173N/A
1173N/A/*
1173N/A * Get urgent data
1173N/A *
1173N/A * When the socket is created, we set it SO_OOBINLINE,
1173N/A * so when OOB data arrives, we soread() it and everything
1173N/A * in the send buffer is sent as urgent data
1173N/A */
1173N/Avoid
0N/Asorecvoob(PNATState pData, struct socket *so)
0N/A{
0N/A struct tcpcb *tp = sototcpcb(so);
0N/A
0N/A DEBUG_CALL("sorecvoob");
0N/A DEBUG_ARG("so = %lx", (long)so);
0N/A
0N/A /*
0N/A * We take a guess at how much urgent data has arrived.
0N/A * In most situations, when urgent data arrives, the next
0N/A * read() should get all the urgent data. This guess will
0N/A * be wrong however if more data arrives just after the
0N/A * urgent data, or the read() doesn't return all the
0N/A * urgent data.
0N/A */
0N/A soread(pData, so, /*fCloseIfNothingRead=*/false);
0N/A tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
0N/A tp->t_force = 1;
0N/A tcp_output(pData, tp);
0N/A tp->t_force = 0;
0N/A}
0N/A
0N/A/*
0N/A * Send urgent data
0N/A * There's a lot duplicated code here, but...
0N/A */
0N/Aint
0N/Asosendoob(struct socket *so)
0N/A{
0N/A struct sbuf *sb = &so->so_rcv;
0N/A char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
0N/A
0N/A int n, len;
0N/A
0N/A DEBUG_CALL("sosendoob");
0N/A DEBUG_ARG("so = %lx", (long)so);
0N/A DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
0N/A
0N/A if (so->so_urgc > sizeof(buff))
0N/A so->so_urgc = sizeof(buff); /* XXX */
0N/A
0N/A if (sb->sb_rptr < sb->sb_wptr)
0N/A {
0N/A /* We can send it directly */
0N/A n = send(so->s, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
0N/A so->so_urgc -= n;
0N/A
0N/A DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n",
0N/A n, so->so_urgc));
0N/A }
0N/A else
0N/A {
0N/A /*
0N/A * Since there's no sendv or sendtov like writev,
0N/A * we must copy all data to a linear buffer then
0N/A * send it all
0N/A */
0N/A len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
0N/A if (len > so->so_urgc)
0N/A len = so->so_urgc;
0N/A memcpy(buff, sb->sb_rptr, len);
0N/A so->so_urgc -= len;
0N/A if (so->so_urgc)
0N/A {
0N/A n = sb->sb_wptr - sb->sb_data;
0N/A if (n > so->so_urgc)
0N/A n = so->so_urgc;
0N/A memcpy(buff + len, sb->sb_data, n);
0N/A so->so_urgc -= n;
0N/A len += n;
0N/A }
0N/A n = send(so->s, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
0N/A#ifdef DEBUG
0N/A if (n != len)
0N/A DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
0N/A#endif
0N/A DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n",
0N/A n, so->so_urgc));
0N/A }
0N/A
0N/A sb->sb_cc -= n;
0N/A sb->sb_rptr += n;
0N/A if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
0N/A sb->sb_rptr -= sb->sb_datalen;
0N/A
0N/A return n;
0N/A}
0N/A
0N/A/*
0N/A * Write data from so_rcv to so's socket,
0N/A * updating all sbuf field as necessary
0N/A */
0N/Aint
0N/Asowrite(PNATState pData, struct socket *so)
0N/A{
0N/A int n,nn;
0N/A struct sbuf *sb = &so->so_rcv;
0N/A size_t len = sb->sb_cc;
0N/A struct iovec iov[2];
0N/A
0N/A DEBUG_CALL("sowrite");
0N/A DEBUG_ARG("so = %lx", (long)so);
0N/A
0N/A if (so->so_urgc)
0N/A {
0N/A sosendoob(so);
0N/A if (sb->sb_cc == 0)
0N/A return 0;
0N/A }
0N/A
0N/A /*
0N/A * No need to check if there's something to write,
0N/A * sowrite wouldn't have been called otherwise
0N/A */
0N/A
0N/A len = sb->sb_cc;
0N/A
0N/A iov[0].iov_base = sb->sb_rptr;
0N/A iov[1].iov_base = 0;
0N/A iov[1].iov_len = 0;
0N/A if (sb->sb_rptr < sb->sb_wptr)
0N/A {
0N/A iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
0N/A /* Should never succeed, but... */
0N/A if (iov[0].iov_len > len)
0N/A iov[0].iov_len = len;
0N/A n = 1;
0N/A }
0N/A else
0N/A {
0N/A iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
0N/A if (iov[0].iov_len > len)
0N/A iov[0].iov_len = len;
0N/A len -= iov[0].iov_len;
0N/A if (len)
0N/A {
0N/A iov[1].iov_base = sb->sb_data;
0N/A iov[1].iov_len = sb->sb_wptr - sb->sb_data;
0N/A if (iov[1].iov_len > len)
0N/A iov[1].iov_len = len;
0N/A n = 2;
0N/A }
0N/A else
0N/A n = 1;
0N/A }
0N/A /* Check if there's urgent data to send, and if so, send it */
0N/A#ifdef HAVE_READV
0N/A nn = writev(so->s, (const struct iovec *)iov, n);
0N/A DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
0N/A#else
0N/A nn = send(so->s, iov[0].iov_base, iov[0].iov_len, 0);
0N/A#endif
0N/A /* This should never happen, but people tell me it does *shrug* */
0N/A if (nn < 0 && (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
0N/A return 0;
0N/A
0N/A if (nn < 0 || (nn == 0 && iov[0].iov_len > 0))
0N/A {
0N/A DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
0N/A so->so_state, errno));
0N/A sofcantsendmore(so);
0N/A tcp_sockclosed(pData, sototcpcb(so));
0N/A return -1;
0N/A }
0N/A
0N/A#ifndef HAVE_READV
0N/A if (n == 2 && nn == iov[0].iov_len)
0N/A {
0N/A int ret;
0N/A ret = send(so->s, iov[1].iov_base, iov[1].iov_len,0);
0N/A if (ret > 0)
0N/A nn += ret;
0N/A }
0N/A DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
0N/A#endif
0N/A
0N/A /* Update sbuf */
0N/A sb->sb_cc -= nn;
0N/A sb->sb_rptr += nn;
0N/A if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
0N/A sb->sb_rptr -= sb->sb_datalen;
0N/A
0N/A /*
0N/A * If in DRAIN mode, and there's no more data, set
0N/A * it CANTSENDMORE
0N/A */
0N/A if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
0N/A sofcantsendmore(so);
0N/A
0N/A return nn;
0N/A}
0N/A
0N/A/*
0N/A * recvfrom() a UDP socket
0N/A */
0N/Avoid
0N/Asorecvfrom(PNATState pData, struct socket *so)
0N/A{
0N/A struct sockaddr_in addr;
0N/A socklen_t addrlen = sizeof(struct sockaddr_in);
0N/A
0N/A DEBUG_CALL("sorecvfrom");
0N/A DEBUG_ARG("so = %lx", (long)so);
0N/A
0N/A if (so->so_type == IPPROTO_ICMP)
0N/A {
0N/A /* This is a "ping" reply */
0N/A#if !defined(VBOX_WITH_SLIRP_ICMP) || (defined(VBOX_WITH_SLIRP_ICMP) && !defined(RT_OS_WINDOWS))
0N/A sorecvfrom_icmp_unix(pData, so);
0N/A#endif
0N/A#if defined(VBOX_WITH_SLIRP_ICMP) && defined(RT_OS_WINDOWS)
0N/A sorecvfrom_icmp_win(pData, so);
0N/A#endif
0N/A udp_detach(pData, so);
0N/A }
0N/A else
0N/A {
0N/A /* A "normal" UDP packet */
0N/A struct mbuf *m;
0N/A size_t len;
0N/A u_long n;
0N/A
0N/A if (!(m = m_get(pData)))
0N/A return;
0N/A m->m_data += if_maxlinkhdr;
0N/A
0N/A /*
0N/A * XXX Shouldn't FIONREAD packets destined for port 53,
0N/A * but I don't know the max packet size for DNS lookups
0N/A */
0N/A len = M_FREEROOM(m);
0N/A /* if (so->so_fport != htons(53)) */
0N/A {
0N/A ioctlsocket(so->s, FIONREAD, &n);
0N/A
0N/A if (n > len)
0N/A {
0N/A n = (m->m_data - m->m_dat) + m->m_len + n + 1;
0N/A m_inc(m, n);
0N/A len = M_FREEROOM(m);
0N/A }
0N/A }
0N/A
0N/A m->m_len = recvfrom(so->s, m->m_data, len, 0,
0N/A (struct sockaddr *)&addr, &addrlen);
0N/A DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
0N/A m->m_len, errno,strerror(errno)));
0N/A if(m->m_len < 0)
0N/A {
0N/A u_char code = ICMP_UNREACH_PORT;
0N/A
0N/A if (errno == EHOSTUNREACH)
0N/A code = ICMP_UNREACH_HOST;
0N/A else if(errno == ENETUNREACH)
0N/A code = ICMP_UNREACH_NET;
0N/A
1173N/A DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
0N/A icmp_error(pData, so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
1173N/A m_free(pData, m);
0N/A }
0N/A else
0N/A {
1173N/A /*
1173N/A * Hack: domain name lookup will be used the most for UDP,
1173N/A * and since they'll only be used once there's no need
1173N/A * for the 4 minute (or whatever) timeout... So we time them
0N/A * out much quicker (10 seconds for now...)
0N/A */
1173N/A if (so->so_expire)
0N/A {
0N/A if (so->so_fport == htons(53))
0N/A so->so_expire = curtime + SO_EXPIREFAST;
1173N/A else
0N/A so->so_expire = curtime + SO_EXPIRE;
0N/A }
0N/A
0N/A#if 0
0N/A if (m->m_len == len)
0N/A {
0N/A m_inc(m, MINCSIZE);
0N/A m->m_len = 0;
0N/A }
0N/A#endif
0N/A
0N/A /*
0N/A * If this packet was destined for CTL_ADDR,
0N/A * make it look like that's where it came from, done by udp_output
0N/A */
1173N/A udp_output(pData, so, m, &addr);
0N/A } /* rx error */
0N/A } /* if ping packet */
0N/A}
1173N/A
0N/A/*
0N/A * sendto() a socket
0N/A */
1173N/Aint
1173N/Asosendto(PNATState pData, struct socket *so, struct mbuf *m)
1173N/A{
0N/A int ret;
0N/A struct sockaddr_in addr;
0N/A#if 0
1173N/A struct sockaddr_in host_addr;
0N/A#endif
1173N/A
0N/A DEBUG_CALL("sosendto");
0N/A DEBUG_ARG("so = %lx", (long)so);
0N/A DEBUG_ARG("m = %lx", (long)m);
0N/A
0N/A addr.sin_family = AF_INET;
0N/A if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
0N/A {
0N/A /* It's an alias */
0N/A uint32_t last_byte = ntohl(so->so_faddr.s_addr) & ~pData->netmask;
0N/A switch(last_byte)
0N/A {
0N/A#if 0
0N/A /* handle this case at 'default:' */
0N/A case CTL_BROADCAST:
0N/A addr.sin_addr.s_addr = INADDR_BROADCAST;
0N/A /* Send the packet to host to fully emulate broadcast */
0N/A /** @todo r=klaus: on Linux host this causes the host to receive
0N/A * the packet twice for some reason. And I cannot find any place
0N/A * in the man pages which states that sending a broadcast does not
0N/A * reach the host itself. */
0N/A host_addr.sin_family = AF_INET;
0N/A host_addr.sin_port = so->so_fport;
0N/A host_addr.sin_addr = our_addr;
0N/A sendto(so->s, m->m_data, m->m_len, 0,
0N/A (struct sockaddr *)&host_addr, sizeof (struct sockaddr));
0N/A break;
0N/A#endif
0N/A case CTL_DNS:
0N/A if (!get_dns_addr(pData, &dns_addr))
0N/A addr.sin_addr = dns_addr;
0N/A else
0N/A addr.sin_addr = loopback_addr;
0N/A break;
0N/A case CTL_ALIAS:
0N/A default:
0N/A if (last_byte == ~pData->netmask)
0N/A addr.sin_addr.s_addr = INADDR_BROADCAST;
0N/A else
0N/A addr.sin_addr = loopback_addr;
0N/A break;
0N/A }
0N/A }
0N/A else
0N/A addr.sin_addr = so->so_faddr;
0N/A addr.sin_port = so->so_fport;
0N/A
0N/A DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n",
0N/A ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
0N/A
0N/A /* Don't care what port we get */
0N/A ret = sendto(so->s, m->m_data, m->m_len, 0,
0N/A (struct sockaddr *)&addr, sizeof (struct sockaddr));
1999N/A if (ret < 0)
0N/A return -1;
0N/A
0N/A /*
0N/A * Kill the socket if there's no reply in 4 minutes,
0N/A * but only if it's an expirable socket
0N/A */
0N/A if (so->so_expire)
0N/A so->so_expire = curtime + SO_EXPIRE;
0N/A so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
0N/A return 0;
0N/A}
0N/A
0N/A/*
0N/A * XXX This should really be tcp_listen
1999N/A */
0N/Astruct socket *
0N/Asolisten(PNATState pData, u_int port, u_int32_t laddr, u_int lport, int flags)
0N/A{
0N/A struct sockaddr_in addr;
0N/A struct socket *so;
0N/A socklen_t addrlen = sizeof(addr);
0N/A int s, opt = 1;
0N/A
0N/A DEBUG_CALL("solisten");
0N/A DEBUG_ARG("port = %d", port);
0N/A DEBUG_ARG("laddr = %x", laddr);
0N/A DEBUG_ARG("lport = %d", lport);
0N/A DEBUG_ARG("flags = %x", flags);
0N/A
0N/A if ((so = socreate()) == NULL)
0N/A {
0N/A /* free(so); Not sofree() ??? free(NULL) == NOP */
0N/A return NULL;
0N/A }
0N/A
0N/A /* Don't tcp_attach... we don't need so_snd nor so_rcv */
0N/A if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
0N/A {
0N/A free(so);
0N/A return NULL;
0N/A }
0N/A insque(pData, so,&tcb);
0N/A
0N/A /*
0N/A * SS_FACCEPTONCE sockets must time out.
0N/A */
0N/A if (flags & SS_FACCEPTONCE)
0N/A so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
0N/A
0N/A so->so_state = (SS_FACCEPTCONN|flags);
0N/A so->so_lport = lport; /* Kept in network format */
0N/A so->so_laddr.s_addr = laddr; /* Ditto */
0N/A
0N/A addr.sin_family = AF_INET;
0N/A addr.sin_addr.s_addr = INADDR_ANY;
0N/A addr.sin_port = port;
0N/A
0N/A if ( ((s = socket(AF_INET,SOCK_STREAM,0)) < 0)
0N/A || (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int)) < 0)
0N/A || (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0)
0N/A || (listen(s,1) < 0))
0N/A {
0N/A#ifdef RT_OS_WINDOWS
0N/A int tmperrno = WSAGetLastError(); /* Don't clobber the real reason we failed */
0N/A closesocket(s);
0N/A sofree(pData, so);
0N/A /* Restore the real errno */
0N/A WSASetLastError(tmperrno);
0N/A#else
0N/A int tmperrno = errno; /* Don't clobber the real reason we failed */
0N/A close(s);
0N/A sofree(pData, so);
0N/A /* Restore the real errno */
1173N/A errno = tmperrno;
1173N/A#endif
0N/A return NULL;
0N/A }
0N/A setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
1173N/A
1173N/A getsockname(s,(struct sockaddr *)&addr,&addrlen);
0N/A so->so_fport = addr.sin_port;
0N/A if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
0N/A so->so_faddr = alias_addr;
0N/A else
0N/A so->so_faddr = addr.sin_addr;
0N/A
0N/A so->s = s;
0N/A return so;
0N/A}
0N/A
0N/A/*
0N/A * Data is available in so_rcv
0N/A * Just write() the data to the socket
0N/A * XXX not yet...
0N/A */
0N/Avoid
0N/Asorwakeup(struct socket *so)
1173N/A{
0N/A#if 0
0N/A sowrite(so);
0N/A FD_CLR(so->s,&writefds);
1173N/A#endif
0N/A}
0N/A
0N/A/*
0N/A * Data has been freed in so_snd
0N/A * We have room for a read() if we want to
0N/A * For now, don't read, it'll be done in the main loop
0N/A */
0N/Avoid
0N/Asowwakeup(struct socket *so)
0N/A{
0N/A}
0N/A
0N/A/*
0N/A * Various session state calls
0N/A * XXX Should be #define's
1173N/A * The socket state stuff needs work, these often get call 2 or 3
1173N/A * times each when only 1 was needed
0N/A */
0N/Avoid
0N/Asoisfconnecting(struct socket *so)
0N/A{
0N/A so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
0N/A SS_FCANTSENDMORE|SS_FWDRAIN);
0N/A so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
0N/A}
0N/A
0N/Avoid
0N/Asoisfconnected(struct socket *so)
0N/A{
0N/A so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
0N/A so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
0N/A}
0N/A
0N/Avoid
1173N/Asofcantrcvmore(struct socket *so)
1173N/A{
0N/A if ((so->so_state & SS_NOFDREF) == 0)
1173N/A {
1173N/A shutdown(so->s,0);
0N/A }
0N/A so->so_state &= ~(SS_ISFCONNECTING);
0N/A if (so->so_state & SS_FCANTSENDMORE)
0N/A so->so_state = SS_NOFDREF; /* Don't select it */
0N/A /* XXX close() here as well? */
0N/A else
0N/A so->so_state |= SS_FCANTRCVMORE;
0N/A}
0N/A
0N/Avoid
0N/Asofcantsendmore(struct socket *so)
0N/A{
0N/A if ((so->so_state & SS_NOFDREF) == 0)
0N/A shutdown(so->s, 1); /* send FIN to fhost */
0N/A
0N/A so->so_state &= ~(SS_ISFCONNECTING);
0N/A if (so->so_state & SS_FCANTRCVMORE)
0N/A so->so_state = SS_NOFDREF; /* as above */
0N/A else
0N/A so->so_state |= SS_FCANTSENDMORE;
0N/A}
0N/A
0N/Avoid
0N/Asoisfdisconnected(struct socket *so)
0N/A{
0N/A#if 0
0N/A so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED);
0N/A close(so->s);
0N/A so->so_state = SS_ISFDISCONNECTED;
0N/A /*
0N/A * XXX Do nothing ... ?
0N/A */
0N/A#endif
0N/A}
1173N/A
1173N/A/*
0N/A * Set write drain mode
0N/A * Set CANTSENDMORE once all data has been write()n
1173N/A */
1173N/Avoid
0N/Asofwdrain(struct socket *so)
0N/A{
0N/A if (so->so_rcv.sb_cc)
1173N/A so->so_state |= SS_FWDRAIN;
0N/A else
0N/A sofcantsendmore(so);
0N/A}
0N/A
0N/A#ifdef VBOX_WITH_SLIRP_ICMP
0N/Astatic void
0N/Asend_icmp_to_guest(PNATState pData, char *buff, size_t len, struct socket *so, const struct sockaddr_in *addr)
0N/A{
0N/A struct ip *ip;
0N/A uint32_t dst,src;
0N/A char ip_copy[256];
0N/A struct icmp *icp;
0N/A int old_ip_len;
0N/A struct mbuf *m;
0N/A struct icmp_msg *icm;
0N/A
0N/A ip = (struct ip *)buff;
0N/A icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
1173N/A
0N/A if (icp->icmp_type != ICMP_ECHOREPLY && icp->icmp_type != ICMP_TIMXCEED)
0N/A {
0N/A LogRel(("received ICMP(t:%d, c:%d)\n", icp->icmp_type, icp->icmp_code));
0N/A return;
0N/A }
0N/A
0N/A if (icp->icmp_type == ICMP_TIMXCEED)
1173N/A ip = &icp->icmp_ip;
0N/A
0N/A icm = icmp_find_original_mbuf(pData, ip);
0N/A
0N/A if (icm == NULL)
0N/A {
0N/A LogRel(("NAT: Can't find the corresponding packet for the received ICMP\n"));
0N/A return;
0N/A }
1173N/A
0N/A m = icm->im_m;
0N/A Assert(m != NULL);
0N/A
0N/A src = addr->sin_addr.s_addr;
0N/A
0N/A ip = mtod(m, struct ip *);
0N/A /* Now ip is pointing on header we've sent from guest */
1173N/A if (icp->icmp_type == ICMP_TIMXCEED)
1173N/A {
0N/A old_ip_len = (ip->ip_hl << 2) + 64;
0N/A memcpy(ip_copy, ip, old_ip_len);
0N/A }
0N/A
0N/A /* source address from original IP packet*/
0N/A dst = ip->ip_src.s_addr;
0N/A
0N/A /* overide ther tail of old packet */
0N/A memcpy(m->m_data, buff, len);
0N/A m->m_len = len;
0N/A ip = mtod(m, struct ip *); /* ip is from mbuf we've overrided */
0N/A
0N/A icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
0N/A if (icp->icmp_type == ICMP_TIMXCEED)
0N/A {
0N/A /* according RFC 793 error messages required copy of initial IP header + 64 bit */
1173N/A memcpy(&icp->icmp_ip, ip_copy, old_ip_len);
1173N/A ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
0N/A }
1173N/A
1173N/A /* the low level expects fields to be in host format so let's convert them*/
0N/A NTOHS(ip->ip_len);
0N/A NTOHS(ip->ip_off);
0N/A NTOHS(ip->ip_id);
0N/A ip->ip_src.s_addr = src;
0N/A ip->ip_dst.s_addr = dst;
0N/A icmp_reflect(pData, m);
0N/A LIST_REMOVE(icm, im_list);
0N/A /* Don't call m_free here*/
0N/A free(icm);
0N/A}
0N/A
0N/A# ifdef RT_OS_WINDOWS
0N/Astatic void
0N/Asorecvfrom_icmp_win(PNATState pData, struct socket *so)
0N/A{
0N/A int len;
0N/A int i;
0N/A struct ip *ip;
0N/A struct mbuf *m;
0N/A struct icmp *icp;
0N/A struct icmp_msg *icm;
0N/A struct ip *ip_broken; /* ICMP returns header + 64 bit of packet */
0N/A uint32_t src;
0N/A ICMP_ECHO_REPLY *icr;
0N/A u_char code = ~0;
0N/A len = pData->pfIcmpParseReplies(pData->pvIcmpBuffer, pData->szIcmpBuffer);
0N/A if (len < 0)
0N/A {
0N/A LogRel(("NAT: Error (%d) occurred on ICMP receiving\n", GetLastError()));
0N/A return;
0N/A }
0N/A if (len == 0)
0N/A return; /* no error */
0N/A LogRel(("IcmpParseReplies returns %ld\n", len));
0N/A icr = (ICMP_ECHO_REPLY *)pData->pvIcmpBuffer;
0N/A for (i = 0; i < len; ++i)
0N/A {
0N/A switch(icr[i].Status)
0N/A {
0N/A case IP_DEST_HOST_UNREACHABLE:
0N/A code = (code != ~0 ? code : ICMP_UNREACH_HOST);
0N/A case IP_DEST_NET_UNREACHABLE:
0N/A code = (code != ~0 ? code : ICMP_UNREACH_NET);
0N/A case IP_DEST_PROT_UNREACHABLE:
0N/A code = (code != ~0 ? code : ICMP_UNREACH_PROTOCOL);
0N/A /* UNREACH error inject here */
0N/A case IP_DEST_PORT_UNREACHABLE:
0N/A code = (code != ~0 ? code : ICMP_UNREACH_PORT);
0N/A icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, "Error occurred!!!");
0N/A break;
0N/A case IP_SUCCESS: /* echo replied */
0N/A m = m_get(pData);
0N/A ip = mtod(m, struct ip *);
0N/A ip->ip_src.s_addr = icr[i].Address;
0N/A ip->ip_p = IPPROTO_ICMP;
0N/A ip->ip_dst.s_addr = so->so_laddr.s_addr; /*XXX: still the hack*/
0N/A ip->ip_hl = sizeof(struct ip) >> 2; /* requiered for icmp_reflect, no IP options */
0N/A ip->ip_ttl = icr[i].Options.Ttl;
0N/A
0N/A icp = (struct icmp *)&ip[1]; /* no options */
0N/A icp->icmp_type = ICMP_ECHOREPLY;
0N/A icp->icmp_code = 0;
0N/A icp->icmp_id = so->so_icmp_id;
0N/A icp->icmp_seq = so->so_icmp_seq;
0N/A memcpy(icp->icmp_data, icr[i].Data, icr[i].DataSize);
0N/A
0N/A ip->ip_len = sizeof(struct ip) + ICMP_MINLEN + icr[i].DataSize;
0N/A m->m_len = ip->ip_len;
0N/A
0N/A icmp_reflect(pData, m);
0N/A case IP_TTL_EXPIRED_TRANSIT: /* TTL expired */
0N/A
0N/A ip_broken = icr[i].Data;
0N/A icm = icmp_find_original_mbuf(pData, ip_broken);
0N/A if (icm == NULL) {
0N/A LogRel(("ICMP: can't find original package (first double word %x)\n", *(uint32_t *)ip_broken));
0N/A return;
0N/A }
0N/A m = icm->im_m;
0N/A ip = mtod(m, struct ip *);
0N/A ip->ip_ttl = icr[i].Options.Ttl;
0N/A src = ip->ip_src.s_addr;
0N/A ip->ip_dst.s_addr = src;
0N/A ip->ip_dst.s_addr = icr[i].Address;
0N/A icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
0N/A ip_broken->ip_src.s_addr = src; /*it packet sent from host not from guest*/
0N/A memcpy(icp->icmp_data, ip_broken, (ip_broken->ip_hl << 2) + 64);
0N/A icmp_reflect(pData, m);
0N/A break;
0N/A default:
0N/A LogRel(("ICMP(default): message with Status: %x was received from %x\n", icr[i].Status, icr[i].Address));
0N/A break;
0N/A }
0N/A }
0N/A}
0N/A# endif /* RT_OS_WINDOWS */
0N/A#endif /* VBOX_WITH_SLIRP_ICMP */
0N/A
0N/Astatic void sorecvfrom_icmp_unix(PNATState pData, struct socket *so)
0N/A{
0N/A struct sockaddr_in addr;
0N/A socklen_t addrlen = sizeof(struct sockaddr_in);
0N/A char buff[1500];
0N/A int len;
0N/A len = recvfrom(so->s, buff, 1500, 0,
0N/A (struct sockaddr *)&addr, &addrlen);
0N/A /* XXX Check if reply is "correct"? */
0N/A
0N/A if (len == -1 || len == 0)
0N/A {
0N/A u_char code = ICMP_UNREACH_PORT;
0N/A
0N/A if (errno == EHOSTUNREACH)
0N/A code = ICMP_UNREACH_HOST;
0N/A else if(errno == ENETUNREACH)
0N/A code = ICMP_UNREACH_NET;
0N/A
0N/A DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
0N/A errno,strerror(errno)));
0N/A icmp_error(pData, so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
0N/A }
0N/A else
0N/A {
0N/A#ifdef VBOX_WITH_SLIRP_ICMP
0N/A send_icmp_to_guest(pData, buff, len, so, &addr);
0N/A#else
0N/A icmp_reflect(pData, so->so_m);
0N/A so->so_m = 0; /* Don't m_free() it again! */
0N/A#endif
0N/A }
0N/A}
0N/A
0N/A