udp.c revision ee8111c48b716c926f1ca5bcd64415a553c09d54
/* $Id$ */
/** @file
* NAT - UDP protocol.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*
* This code is based on:
*
* Copyright (c) 1982, 1986, 1988, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)udp_usrreq.c 8.4 (Berkeley) 1/21/94
* udp_usrreq.c,v 1.4 1994/10/02 17:48:45 phk Exp
*/
/*
* Changes and additions relating to SLiRP
* Copyright (c) 1995 Danny Gasparovski.
*
* Please read the file COPYRIGHT for the
* terms and conditions of the copyright.
*/
#include <slirp.h>
#include "ip_icmp.h"
#include "ctl.h"
/*
* UDP protocol implementation.
* Per RFC 768, August, 1980.
*/
#define udpcksum 1
void
{
udp_last_so = &udb;
}
/* m->m_data points at ip packet header
* m->m_len length ip packet
* ip->ip_len length data (IPDU)
*/
void
{
int len;
int ret;
int ttl;
DEBUG_CALL("udp_input");
DEBUG_ARG("m = %lx", (long)m);
/*
* Strip IP options, if any; should skip this,
* make available to user, and use on returned packets,
* but we don't yet have a way to check the checksum
* with options still present.
*/
{
ip_stripoptions(m, (struct mbuf *)0);
}
/*
* Get IP and UDP header together in first mbuf.
*/
/*
* Make mbuf data length reflect UDP length.
* If not enough data to reflect UDP length, drop.
*/
{
{
}
}
/*
* Save a copy of the IP header in case we want restore it
* for sending an ICMP error message in response.
*/
/*
* Checksum extended UDP header and data.
*/
{
#if 0
/* keep uh_sum for ICMP reply */
{
#endif
{
goto bad;
}
}
#if 0
}
#endif
/*
*/
{
bootp_input(pData, m);
goto done;
}
if ( pData->fUseHostResolver
{
/* udp_output2 expects pointer on the body of UDP packet. */
/* we shouldn't free this mbuf*/
return;
}
/*
* handle TFTP
*/
{
tftp_input(pData, m);
goto done;
}
/*
* Locate pcb for datagram.
*/
so = udp_last_so;
{
{
{
break;
}
}
else
{
udp_last_so = so;
}
}
{
/*
* If there's no socket for this packet,
* create one
*/
{
goto bad;
}
{
Log2(("NAT: IP(id: %hd) udp_attach errno = %d-%s\n",
goto bad;
}
/*
* Setup fields
*/
/* udp_last_so = so; */
/*
* XXXXX Here, check if it's in udpexec_list,
* and if it is, do the fork_exec() etc.
*/
}
/*
* DNS proxy
*/
if ( pData->fUseDnsProxy
{
goto done;
}
if (ret < 0)
LogRel(("NAT: Error (%s) occurred while setting TTL(%d) attribute "
{
/* in case we receive ICMP on this socket we'll aware that ICMP has been already sent to host*/
}
/* restore the orig mbuf packet */
return;
bad:
Log2(("NAT: UDP(id: %hd) datagram to %R[IP4] with size(%d) claimed as bad\n",
done:
/* some services like bootp(built-in), dns(buildt-in) and dhcp don't need sockets
* and create new m'buffers to send them to guest, so we'll free their incomming
* buffers here.
*/
return;
}
int iptos)
{
int error = 0;
DEBUG_CALL("udp_output");
DEBUG_ARG("m = %lx", (long)m);
/*
* Adjust for header
*/
/*
* Fill in mbuf with extended UDP header
* and addresses and length put into network format.
*/
/* XXXXX Check for from-one-location sockets, or from-any-location sockets */
/*
* Stuff checksum and output datagram.
*/
if (udpcksum)
{
}
return error;
}
struct sockaddr_in *addr)
{
{
}
/* Any UDP packet to the loopback address must be translated to be from
* the forwarding address, i.e. 10.0.2.2. */
}
int
{
struct sockaddr_in *addr;
int status;
int opt = 1;
goto error;
/*
* Here, we bind() the socket. Although not really needed
* (sendto() on an unbound socket will bind it), it's done
* here so that emulation of ytalk etc. don't have to do it
*/
#ifdef RT_OS_DARWIN
#endif
fd_nonblock(so->s);
{
closesocket(so->s);
so->s = -1;
#ifdef RT_OS_WINDOWS
#else
#endif
goto error;
}
/* success, insert in queue */
/* enable broadcast for later use */
NSOCK_INC();
return so->s;
Log2(("NAT: can't create datagramm socket\n"));
return -1;
}
void
{
{
closesocket(so->s);
}
}
struct socket *
udp_listen(PNATState pData, u_int32_t bind_addr, u_int port, u_int32_t laddr, u_int lport, int flags)
{
struct sockaddr_in addr;
int opt = 1;
return NULL;
if (so->s == -1)
{
LogRel(("NAT: can't create datagram socket\n"));
return NULL;
}
fd_nonblock(so->s);
NSOCK_INC();
#ifdef RT_OS_DARWIN
#endif
{
return NULL;
}
/* setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int)); */
/* The original check was completely broken, as the commented out
* if statement was always true (INADDR_ANY=0). */
/* if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr) */
if (1 == 0) /* always use the else part */
else
if (flags != SS_FACCEPTONCE)
return so;
}