ReactOS  0.4.15-dev-506-ga3ec01c
icmp.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS TCP/IP protocol driver
4  * FILE: network/icmp.c
5  * PURPOSE: Internet Control Message Protocol routines
6  * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7  * REVISIONS:
8  * CSH 01/08-2000 Created
9  */
10 
11 #include "precomp.h"
12 
13 #include <icmp.h>
14 
16 {
18 
19  return STATUS_SUCCESS;
20 }
21 
23 {
25 
26  return STATUS_SUCCESS;
27 }
28 
30  PADDRESS_FILE AddrFile,
32  PCHAR BufferData,
34  PULONG DataUsed )
35 /*
36  * FUNCTION: Sends an ICMP datagram to a remote address
37  * ARGUMENTS:
38  * Request = Pointer to TDI request
39  * ConnInfo = Pointer to connection information
40  * Buffer = Pointer to NDIS buffer with data
41  * DataSize = Size in bytes of data to be sent
42  * RETURNS:
43  * Status of operation
44  */
45 {
46  TI_DbgPrint(DEBUG_ICMP, ("Sending ICMP datagram (0x%x)\n", AddrFile));
47 
48  /* just forward the call to RawIP handler */
49  return RawIPSendDatagram(AddrFile, ConnInfo, BufferData, DataSize, DataUsed);
50 }
51 
52 
55  PIP_PACKET IPPacket)
56 /*
57  * FUNCTION: Receives an ICMP packet
58  * ARGUMENTS:
59  * NTE = Pointer to net table entry which the packet was received on
60  * IPPacket = Pointer to an IP packet that was received
61  */
62 {
63  PICMP_HEADER ICMPHeader = (PICMP_HEADER)IPPacket->Data;
64  UINT32 DataSize = IPPacket->TotalSize - IPPacket->HeaderSize;
65 
66  TI_DbgPrint(DEBUG_ICMP, ("ICMPReceive: Size (%d) HeaderSize (%d) Type (%d) Code (%d) Checksum (0x%x)\n",
67  IPPacket->TotalSize, IPPacket->HeaderSize, ICMPHeader->Type, ICMPHeader->Code, ICMPHeader->Checksum));
68 
69  /* Discard too short packets */
70  if (DataSize < sizeof(ICMP_HEADER))
71  {
72  TI_DbgPrint(DEBUG_ICMP, ("Packet doesn't fit ICMP header. Discarded\n"));
73  return;
74  }
75 
76  /* Discard packets with bad checksum */
77  if (!IPv4CorrectChecksum(IPPacket->Data, DataSize))
78  {
79  TI_DbgPrint(DEBUG_ICMP, ("Bad ICMP checksum. Packet discarded\n"));
80  return;
81  }
82 
83  RawIpReceive(Interface, IPPacket);
84 
85  if (ICMPHeader->Type == ICMP_TYPE_ECHO_REQUEST)
86  {
88  }
89 }
90 
93  PIP_PACKET IPPacket,
94  UCHAR Type,
95  UCHAR Code)
96 /*
97  * FUNCTION: Transmits an ICMP packet in response to an incoming packet
98  * ARGUMENTS:
99  * NTE = Pointer to net table entry to use
100  * IPPacket = Pointer to IP packet that was received
101  * Type = ICMP message type
102  * Code = ICMP message code
103  * NOTES:
104  * We have received a packet from someone and is unable to
105  * process it due to error(s) in the packet or we have run out
106  * of resources. We transmit an ICMP message to the host to
107  * notify him of the problem
108  */
109 {
110  UINT DataSize;
111  IP_PACKET NewPacket;
112  ADDRESS_FILE FakeAddrFile;
114 
115  TI_DbgPrint(DEBUG_ICMP, ("Called. Type (%d) Code (%d).\n", Type, Code));
116 
117  DataSize = IPPacket->TotalSize - IPPacket->HeaderSize;
118 
119  /* First check if we have a route to sender */
120  NCE = RouteGetRouteToDestination(&IPPacket->SrcAddr);
121  if (!NCE)
122  {
123  return;
124  }
125 
126  /* This is the only data needed to generate a packet */
127  FakeAddrFile.Protocol = IPPROTO_ICMP;
128  FakeAddrFile.TTL = 128;
129 
131  &FakeAddrFile, &NewPacket, &IPPacket->SrcAddr, 0, &Interface->Unicast, 0, IPPacket->Data, DataSize)))
132  {
133  return;
134  }
135 
136  ((PICMP_HEADER)NewPacket.Data)->Type = Type;
137  ((PICMP_HEADER)NewPacket.Data)->Code = Code;
138  ((PICMP_HEADER)NewPacket.Data)->Checksum = 0;
139  ((PICMP_HEADER)NewPacket.Data)->Checksum = (USHORT)IPv4Checksum(NewPacket.Data, DataSize, 0);
140 
141  IPSendDatagram(&NewPacket, NCE);
142 }
143 
144 /* EOF */
signed char * PCHAR
Definition: retypes.h:7
#define TI_DbgPrint(_t_, _x_)
Definition: debug.h:45
UINT8 Type
Definition: icmp.h:12
Type
Definition: Type.h:6
UCHAR TTL
Definition: titypes.h:141
Definition: neighbor.h:28
IP_ADDRESS SrcAddr
Definition: ip.h:89
LONG NTSTATUS
Definition: precomp.h:26
USHORT Protocol
Definition: titypes.h:138
VOID ICMPReply(PIP_INTERFACE Interface, PIP_PACKET IPPacket, UCHAR Type, UCHAR Code)
Definition: icmp.c:91
PNEIGHBOR_CACHE_ENTRY RouteGetRouteToDestination(PIP_ADDRESS Destination)
Definition: router.c:300
VOID IPRegisterProtocol(UINT ProtocolNumber, IP_PROTOCOL_HANDLER Handler)
Definition: ip.c:390
PVOID Data
Definition: ip.h:85
unsigned int UINT32
NTSTATUS IPSendDatagram(PIP_PACKET IPPacket, PNEIGHBOR_CACHE_ENTRY NCE)
Definition: transmit.c:223
#define IPv4Checksum(Data, Count, Seed)
Definition: checksum.h:30
smooth NULL
Definition: ftsmooth.c:416
#define DEBUG_ICMP
Definition: debug.h:29
#define ICMP_TYPE_ECHO_REQUEST
Definition: icmp.h:25
#define Code
Definition: deflate.h:80
IN PVOID IN PVOID IN USHORT IN USHORT IN PINTERFACE Interface
Definition: pci.h:361
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
UINT16 Checksum
Definition: icmp.h:14
NTSTATUS RawIPSendDatagram(PADDRESS_FILE AddrFile, PTDI_CONNECTION_INFORMATION ConnInfo, PCHAR Buffer, ULONG DataSize, PULONG DataUsed)
Definition: rawip.c:171
#define ICMP_TYPE_ECHO_REPLY
Definition: icmp.h:21
UINT TotalSize
Definition: ip.h:86
UINT8 Code
Definition: icmp.h:13
unsigned char UCHAR
Definition: xmlstorage.h:181
NTSTATUS BuildRawIpPacket(PADDRESS_FILE AddrFile, PIP_PACKET Packet, PIP_ADDRESS RemoteAddress, USHORT RemotePort, PIP_ADDRESS LocalAddress, USHORT LocalPort, PCHAR DataBuffer, UINT DataLen)
Definition: rawip.c:86
NTSTATUS ICMPStartup()
Definition: icmp.c:15
UINT HeaderSize
Definition: ip.h:84
VOID RawIpReceive(PIP_INTERFACE Interface, PIP_PACKET IPPacket)
Definition: rawip.c:267
unsigned short USHORT
Definition: pedump.c:61
NTSTATUS ICMPShutdown()
Definition: icmp.c:22
unsigned int * PULONG
Definition: retypes.h:1
unsigned int UINT
Definition: ndis.h:50
struct ICMP_HEADER * PICMP_HEADER
NTSTATUS ICMPSendDatagram(PADDRESS_FILE AddrFile, PTDI_CONNECTION_INFORMATION ConnInfo, PCHAR BufferData, ULONG DataSize, PULONG DataUsed)
Definition: icmp.c:29
unsigned int ULONG
Definition: retypes.h:1
Definition: ip.h:77
return STATUS_SUCCESS
Definition: btrfs.c:3014
_In_ NDIS_STATUS _In_ ULONG _In_ USHORT _In_opt_ PVOID _In_ ULONG DataSize
Definition: ndis.h:4751
VOID ICMPReceive(PIP_INTERFACE Interface, PIP_PACKET IPPacket)
Definition: icmp.c:53
#define IPv4CorrectChecksum(Data, Count)
Definition: checksum.h:38