ip_icmp.c revision 571c90a734400801da973f986190fac9fc5efd0d
/*
* 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
#ifdef RT_OS_WINDOWS
#endif /* RT_OS_WINDOWS */
/* 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"));
}
}
{
{
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:
{
{
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 %R[IP4]:%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.*/
LogRel(("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; */
DEBUG_CALL("icmp_input");
DEBUG_ARG("m = %lx", (long )m);
/*
* 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;
}
{
goto freeit;
}
/* icmpstat.icps_inhist[icp->icmp_type]++; */
/* code = icp->icmp_code; */
{
case ICMP_ECHO:
{
icmp_reflect(pData, m);
}
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
{
icmp_attach(pData, m);
if (status < 0)
LogRel(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n",
{
}
}
else
{
/*
* We're freeing the ICMP message, which unable sent or process.
* That behavior described in rfc 793, we shouldn't notify sender about
* fail of processing it's ICMP packets
*/
return;
}
#else /* RT_OS_WINDOWS */
icmp_attach(pData, m);
{
error = GetLastError();
switch (error)
{
case ERROR_INVALID_PARAMETER:
break;
case ERROR_NOT_SUPPORTED:
LogRel(("operation is unsupported)\n"));
break;
case ERROR_NOT_ENOUGH_MEMORY:
LogRel(("OOM!!!)\n"));
break;
case IP_BUF_TOO_SMALL:
LogRel(("Buffer too small)\n"));
break;
default:
LogRel(("Other error!!!)\n"));
break;
}
}
#endif /* RT_OS_WINDOWS */
} /* if ip->ip_dst.s_addr == alias_addr.s_addr */
break;
case ICMP_UNREACH:
/* XXX? report error? close socket? */
case ICMP_TIMXCEED:
case ICMP_PARAMPROB:
case ICMP_SOURCEQUENCH:
case ICMP_TSTAMP:
case ICMP_MASKREQ:
case ICMP_REDIRECT:
break;
default:
} /* switch */
/* m is m_free()'d xor put in a socket xor or given to ip_send */
;
}
/*
* 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.
* mbuf *msrc is used as a template, but is NOT m_free()'d.
* 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
*/
void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, const char *message)
{
register struct mbuf *m;
DEBUG_CALL("icmp_error");
goto end_error;
/* check msrc */
if (!msrc)
goto end_error;
#if DEBUG
{
}
#endif
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;
}
/* make a copy */
goto end_error; /* get mbuf */
{
int new_m_size;
m_adj(m, if_maxlinkhdr);
if (new_m_size>m->m_size)
m_inc(m, new_m_size);
}
/* 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)
m->m_len += message_len;
}
#endif
icp->icmp_cksum = 0;
/* fill in ip */
return;
LogRel(("NAT: error occurred while sending ICMP error message \n"));
}
/*
* Reflect the ip packet back to the source
*/
void
{
/*
* Send an icmp packet back to the ip level,
* after supplying a checksum.
*/
icp->icmp_cksum = 0;
}