ip_icmp.c revision f3a33ad348f1f84e6a558fc99f3519c48dd7009f
/* $Id$ */
/** @file
*/
/*
* 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, 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.
*
* @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
* ip_icmp.c,v 1.7 1995/05/30 08:09:42 rgrimes Exp
*/
#include "slirp.h"
#include "ip_icmp.h"
#ifdef RT_OS_WINDOWS
#include <Icmpapi.h>
#include <Iphlpapi.h>
#endif
/* The message sent when emulating PING */
/* Be nice and tell them it's just a psuedo-ping packet */
static const char icmp_ping_msg[] = "This is a psuedo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST packets.\n";
/* list of actions for icmp_error() on RX of an icmp message */
static const int icmp_flush[19] =
{
/* ECHO REPLY (0) */ 0,
1,
1,
/* DEST UNREACH (3) */ 1,
/* SOURCE QUENCH (4)*/ 1,
/* REDIRECT (5) */ 1,
1,
1,
/* ECHO (8) */ 0,
/* ROUTERADVERT (9) */ 1,
/* ROUTERSOLICIT (10) */ 1,
/* TIME EXCEEDED (11) */ 1,
/* PARAMETER PROBLEM (12) */ 1,
/* TIMESTAMP (13) */ 0,
/* TIMESTAMP REPLY (14) */ 0,
/* INFO (15) */ 0,
/* INFO REPLY (16) */ 0,
/* ADDR MASK (17) */ 0,
/* ADDR MASK REPLY (18) */ 0
};
int
{
#ifndef RT_OS_WINDOWS
# ifndef RT_OS_DARWIN
# else /* !RT_OS_DARWIN */
# endif /* RT_OS_DARWIN */
{
return 1;
}
NSOCK_INC();
#else /* RT_OS_WINDOWS */
{
pData->pfGetAdaptersAddresses = (ULONG (WINAPI *)(ULONG, ULONG, PVOID, PIP_ADAPTER_ADDRESSES, PULONG))
{
LogRel(("NAT: Can't find GetAdapterAddresses in Iphlpapi.dll\n"));
}
}
{
{
LogRel(("NAT: Icmp.dll could not be loaded\n"));
return 1;
}
}
{
LogRel(("NAT: Can't find IcmpParseReplies symbol\n"));
return 1;
}
{
LogRel(("NAT: Can't find IcmpCloseHandle symbol\n"));
return 1;
}
#endif /* RT_OS_WINDOWS */
return 0;
}
/*
* ip here is ip header + 64bytes readed from ICMP packet
*/
struct icmp_msg *
{
int found = 0;
lport = ~0;
fport = ~0;
{
case IPPROTO_ICMP:
{
{
/* try next item */
continue;
}
/*
* IP could pointer to ICMP_REPLY datagram (1)
* or pointer IP header in ICMP payload in case of
* ICMP_TIMXCEED or ICMP_UNREACH (2)
*
* if (1) and then ICMP (type should be ICMP_ECHOREPLY) and we need check that
* IP.IP_SRC == IP0.IP_DST received datagramm comes from destination.
*
* if (2) then check that payload ICMP has got type ICMP_ECHO and
* IP.IP_DST == IP0.IP_DST destination of returned datagram is the same as
* one was sent.
*/
{
found = 1;
break;
}
Log(("Have found nothing\n"));
}
break;
/*
* for TCP and UDP logic little bit reverted, we try to find the HOST socket
* from which the IP package has been sent.
*/
case IPPROTO_UDP:
head_socket = &udb;
/* fall through */
case IPPROTO_TCP:
if (head_socket == NULL)
{
}
/* check last socket first */
{
found = 1;
so = last_socket;
goto sofound;
}
{
/* Should be reaplaced by hash here */
Log(("trying:%R[natsock] against %RTnaipv4:%d lport=%d hlport=%d\n", so, &faddr, fport, lport, so->so_hlport));
{
found = 1;
break;
}
}
break;
default:
}
{
{
/* socket is shutdowning we've already sent ICMP on it.*/
Log(("NAT: Received icmp on shutdowning socket (probably corresponding ICMP socket has been already sent)\n"));
return NULL;
}
found = 1;
/*XXX: this storage not very long,
* better add flag if it should removed from lis
*/
return (icm);
}
if (found == 1)
return icm;
return NULL;
}
static int
{
return 0;
}
/*
* Process a received ICMP message.
*/
void
{
int status;
#if !defined(RT_OS_WINDOWS)
int ttl;
#endif
/* int code; */
/*
* Locate icmp structure in mbuf, and check
* that its not corrupted and of at least minimum length.
*/
if (icmplen < ICMP_MINLEN)
{
/* min 8 bytes payload */
goto end_error_free_m;
}
{
goto end_error_free_m;
}
if (m->m_next)
{
if (!icp_buf)
{
Log(("NAT: not enought memory to allocate the buffer\n"));
goto end_error_free_m;
}
}
else
/* icmpstat.icps_inhist[icp->icmp_type]++; */
/* code = icp->icmp_code; */
{
case ICMP_ECHO:
{
icmp_reflect(pData, m);
goto done;
}
else
{
struct sockaddr_in addr;
#ifdef RT_OS_WINDOWS
int error;
#endif
{
/* It's an alias */
{
case CTL_DNS:
case CTL_ALIAS:
default:
break;
}
}
else
#ifndef RT_OS_WINDOWS
{
static bool fIcmpSocketErrorReported;
if (status < 0)
Log(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n",
if (rc >= 0)
{
icmp_attach(pData, m);
/* don't let m_freem at the end free atached buffer */
goto done;
}
if (!fIcmpSocketErrorReported)
{
LogRel(("icmp_input udp sendto tx errno = %d (%s)\n",
fIcmpSocketErrorReported = true;
}
}
#else /* RT_OS_WINDOWS */
NULL /*=ApcRoutine*/,
NULL /*=ApcContext*/,
&ipopt /*=RequestOptions*/,
1 /*=Timeout in ms*/);
error = GetLastError();
if ( status != 0
|| error == ERROR_IO_PENDING)
{
/* no error! */
icmp_attach(pData, m);
/* don't let m_freem at the end free atached buffer */
goto done;
}
switch (error)
{
case ERROR_INVALID_PARAMETER:
break;
case ERROR_NOT_SUPPORTED:
Log(("operation is unsupported)\n"));
break;
case ERROR_NOT_ENOUGH_MEMORY:
Log(("OOM!!!)\n"));
break;
case IP_BUF_TOO_SMALL:
Log(("Buffer too small)\n"));
break;
default:
Log(("Other error!!!)\n"));
break;
}
#endif /* RT_OS_WINDOWS */
} /* if ip->ip_dst.s_addr == alias_addr.s_addr */
break;
case ICMP_UNREACH:
case ICMP_TIMXCEED:
/* @todo(vvl): both up cases comes from guest,
* indeed right solution would be find the socket
* corresponding to ICMP data and close it.
*/
case ICMP_PARAMPROB:
case ICMP_SOURCEQUENCH:
case ICMP_TSTAMP:
case ICMP_MASKREQ:
case ICMP_REDIRECT:
break;
default:
} /* switch */
done:
if (icp_buf)
}
/**
* Send an ICMP message in response to a situation
*
* RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
* MUST NOT change this header information.
* MUST reply to only the first fragment.
*
* Send ICMP_UNREACH back to the source regarding msrc.
* It is reported as the bad ip packet. The header should
* be fully correct and in host byte order.
* ICMP fragmentation is illegal. All machines must accept 576 bytes in one
* packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
*
* @note This function will free msrc!
*/
void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, const char *message)
{
register struct mbuf *m;
int new_m_size = 0;
int size = 0;
if ( type != ICMP_UNREACH
&& type != ICMP_TIMXCEED
&& type != ICMP_SOURCEQUENCH)
goto end_error;
/* check msrc */
if (!msrc)
goto end_error;
#if DEBUG
{
}
#endif
&& type != ICMP_SOURCEQUENCH)
goto end_error; /* Only reply to fragment 0 */
{
/*
* Assume any unknown ICMP type is an error. This isn't
* specified by the RFC, but think about it..
*/
goto end_error;
}
if (new_m_size < MSIZE)
else if (new_m_size < MCLBYTES)
else if(new_m_size < MJUM9BYTES)
size = MJUM9BYTES;
else if (new_m_size < MJUM16BYTES)
size = MJUM16BYTES;
else
AssertMsgFailed(("Unsupported size"));
if (!m)
goto end_error;
m->m_data += if_maxlinkhdr;
/* make the header of the reply packet */
/* fill in icmp */
if (minsize)
/* min. size = 8+sizeof(struct ip)+8 */
#if DEBUG
if (message)
{
/* DEBUG : append message to ICMP packet */
int message_len;
char *cpnt;
if (message_len > ICMP_MAXDATALEN)
}
#endif
icp->icmp_cksum = 0;
/* fill in ip */
/* clear source datagramm in positive branch */
return;
/*
* clear source datagramm in case if some of requirement haven't been met.
*/
if (!msrc)
{
static bool fIcmpErrorReported;
if (!fIcmpErrorReported)
{
LogRel(("NAT: error occurred while sending ICMP error message\n"));
fIcmpErrorReported = true;
}
}
}
/*
* Reflect the ip packet back to the source
* Note: m isn't duplicated by this method and more delivered to ip_output then.
*/
void
{
/*
* Send an icmp packet back to the ip level,
* after supplying a checksum.
*/
icp->icmp_cksum = 0;
}