2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
5 * PURPOSE: Internet Control Message Protocol routines
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
8 * CSH 01/08-2000 Created
13 NTSTATUS
ICMPStartup()
15 IPRegisterProtocol(IPPROTO_ICMP
, ICMPReceive
);
17 return STATUS_SUCCESS
;
20 NTSTATUS
ICMPShutdown()
22 IPRegisterProtocol(IPPROTO_ICMP
, NULL
);
24 return STATUS_SUCCESS
;
27 VOID
SendICMPComplete(
30 NDIS_STATUS NdisStatus
)
32 * FUNCTION: ICMP datagram transmit completion handler
34 * Context = Pointer to context infomation (IP_PACKET)
35 * Packet = Pointer to NDIS packet
36 * NdisStatus = Status of transmit operation
38 * This routine is called by IP when a ICMP send completes
41 TI_DbgPrint(DEBUG_ICMP
, ("Freeing NDIS packet (%X).\n", Packet
));
44 FreeNdisPacket(Packet
);
46 TI_DbgPrint(DEBUG_ICMP
, ("Done\n"));
50 BOOLEAN
PrepareICMPPacket(
51 PADDRESS_FILE AddrFile
,
52 PIP_INTERFACE Interface
,
54 PIP_ADDRESS Destination
,
58 * FUNCTION: Prepares an ICMP packet
60 * NTE = Pointer to net table entry to use
61 * Destination = Pointer to destination address
62 * DataSize = Size of dataarea
64 * Pointer to IP packet, NULL if there is not enough free resources
67 PNDIS_PACKET NdisPacket
;
68 NDIS_STATUS NdisStatus
;
69 PIPv4_HEADER IPHeader
;
72 TI_DbgPrint(DEBUG_ICMP
, ("Called. DataSize (%d).\n", DataSize
));
74 IPInitializePacket(IPPacket
, IP_ADDRESS_V4
);
76 /* No special flags */
79 Size
= sizeof(IPv4_HEADER
) + DataSize
;
81 /* Allocate NDIS packet */
82 NdisStatus
= AllocatePacketWithBuffer( &NdisPacket
, NULL
, Size
);
84 if( !NT_SUCCESS(NdisStatus
) ) return FALSE
;
86 IPPacket
->NdisPacket
= NdisPacket
;
87 IPPacket
->MappedHeader
= TRUE
;
89 GetDataPtr( IPPacket
->NdisPacket
, 0,
90 (PCHAR
*)&IPPacket
->Header
, &IPPacket
->TotalSize
);
91 ASSERT(IPPacket
->TotalSize
== Size
);
93 TI_DbgPrint(DEBUG_ICMP
, ("Size (%d). Data at (0x%X).\n", Size
, Data
));
94 TI_DbgPrint(DEBUG_ICMP
, ("NdisPacket at (0x%X).\n", NdisPacket
));
96 IPPacket
->HeaderSize
= sizeof(IPv4_HEADER
);
97 IPPacket
->Data
= ((PCHAR
)IPPacket
->Header
) + IPPacket
->HeaderSize
;
99 TI_DbgPrint(DEBUG_ICMP
, ("Copying Address: %x -> %x\n",
100 &IPPacket
->DstAddr
, Destination
));
102 RtlCopyMemory(&IPPacket
->DstAddr
, Destination
, sizeof(IP_ADDRESS
));
103 RtlCopyMemory(IPPacket
->Data
, Data
, DataSize
);
105 /* Build IPv4 header. FIXME: IPv4 only */
107 IPHeader
= (PIPv4_HEADER
)IPPacket
->Header
;
109 /* Version = 4, Length = 5 DWORDs */
110 IPHeader
->VerIHL
= 0x45;
111 /* Normal Type-of-Service */
113 /* Length of data and header */
114 IPHeader
->TotalLength
= WH2N((USHORT
)DataSize
+ sizeof(IPv4_HEADER
));
116 IPHeader
->Id
= (USHORT
)Random();
117 /* One fragment at offset 0 */
118 IPHeader
->FlagsFragOfs
= 0;
121 IPHeader
->Ttl
= AddrFile
->TTL
;
124 /* Internet Control Message Protocol */
125 IPHeader
->Protocol
= IPPROTO_ICMP
;
126 /* Checksum is 0 (for later calculation of this) */
127 IPHeader
->Checksum
= 0;
129 IPHeader
->SrcAddr
= Interface
->Unicast
.Address
.IPv4Address
;
130 /* Destination address */
131 IPHeader
->DstAddr
= Destination
->Address
.IPv4Address
;
134 TI_DbgPrint(MID_TRACE
,("Leaving\n"));
139 NTSTATUS
ICMPSendDatagram(
140 PADDRESS_FILE AddrFile
,
141 PTDI_CONNECTION_INFORMATION ConnInfo
,
146 * FUNCTION: Sends an ICMP datagram to a remote address
148 * Request = Pointer to TDI request
149 * ConnInfo = Pointer to connection information
150 * Buffer = Pointer to NDIS buffer with data
151 * DataSize = Size in bytes of data to be sent
153 * Status of operation
157 PTA_IP_ADDRESS RemoteAddressTa
= (PTA_IP_ADDRESS
)ConnInfo
->RemoteAddress
;
158 IP_ADDRESS RemoteAddress
, LocalAddress
;
160 PNEIGHBOR_CACHE_ENTRY NCE
;
163 TI_DbgPrint(MID_TRACE
,("Sending Datagram(%x %x %x %d)\n",
164 AddrFile
, ConnInfo
, BufferData
, DataSize
));
165 TI_DbgPrint(MID_TRACE
,("RemoteAddressTa: %x\n", RemoteAddressTa
));
167 switch( RemoteAddressTa
->Address
[0].AddressType
) {
168 case TDI_ADDRESS_TYPE_IP
:
169 RemoteAddress
.Type
= IP_ADDRESS_V4
;
170 RemoteAddress
.Address
.IPv4Address
=
171 RemoteAddressTa
->Address
[0].Address
[0].in_addr
;
175 return STATUS_UNSUCCESSFUL
;
178 TI_DbgPrint(MID_TRACE
,("About to get route to destination\n"));
180 LockObject(AddrFile
, &OldIrql
);
182 LocalAddress
= AddrFile
->Address
;
183 if (AddrIsUnspecified(&LocalAddress
))
185 /* If the local address is unspecified (0),
186 * then use the unicast address of the
187 * interface we're sending over
189 if(!(NCE
= RouteGetRouteToDestination( &RemoteAddress
)))
191 UnlockObject(AddrFile
, OldIrql
);
192 return STATUS_NETWORK_UNREACHABLE
;
195 LocalAddress
= NCE
->Interface
->Unicast
;
199 if(!(NCE
= NBLocateNeighbor( &LocalAddress
)))
201 UnlockObject(AddrFile
, OldIrql
);
202 return STATUS_INVALID_PARAMETER
;
206 Status
= PrepareICMPPacket( AddrFile
,
213 UnlockObject(AddrFile
, OldIrql
);
215 if( !NT_SUCCESS(Status
) )
218 TI_DbgPrint(MID_TRACE
,("About to send datagram\n"));
220 Status
= IPSendDatagram(&Packet
, NCE
);
221 if (!NT_SUCCESS(Status
))
223 Packet
.Free(&Packet
);
227 *DataUsed
= DataSize
;
229 TI_DbgPrint(MID_TRACE
,("Leaving\n"));
231 return STATUS_SUCCESS
;
236 PIP_INTERFACE Interface
,
239 * FUNCTION: Receives an ICMP packet
241 * NTE = Pointer to net table entry which the packet was received on
242 * IPPacket = Pointer to an IP packet that was received
245 PICMP_HEADER ICMPHeader
;
247 TI_DbgPrint(DEBUG_ICMP
, ("Called.\n"));
249 ICMPHeader
= (PICMP_HEADER
)IPPacket
->Data
;
251 TI_DbgPrint(DEBUG_ICMP
, ("Size (%d).\n", IPPacket
->TotalSize
));
253 TI_DbgPrint(DEBUG_ICMP
, ("HeaderSize (%d).\n", IPPacket
->HeaderSize
));
255 TI_DbgPrint(DEBUG_ICMP
, ("Type (%d).\n", ICMPHeader
->Type
));
257 TI_DbgPrint(DEBUG_ICMP
, ("Code (%d).\n", ICMPHeader
->Code
));
259 TI_DbgPrint(DEBUG_ICMP
, ("Checksum (0x%X).\n", ICMPHeader
->Checksum
));
261 /* Checksum ICMP header and data */
262 if (!IPv4CorrectChecksum(IPPacket
->Data
, IPPacket
->TotalSize
- IPPacket
->HeaderSize
)) {
263 TI_DbgPrint(DEBUG_ICMP
, ("Bad ICMP checksum.\n"));
268 RawIpReceive(Interface
, IPPacket
);
270 switch (ICMPHeader
->Type
) {
271 case ICMP_TYPE_ECHO_REQUEST
:
272 ICMPReply( Interface
, IPPacket
, ICMP_TYPE_ECHO_REPLY
, 0 );
275 case ICMP_TYPE_ECHO_REPLY
:
279 TI_DbgPrint(DEBUG_ICMP
,
280 ("Discarded ICMP datagram of unknown type %d.\n",
290 PIP_TRANSMIT_COMPLETE Complete
,
293 * FUNCTION: Transmits an ICMP packet
295 * NTE = Pointer to net table entry to use (NULL if don't care)
296 * IPPacket = Pointer to IP packet to transmit
299 PNEIGHBOR_CACHE_ENTRY NCE
;
301 TI_DbgPrint(DEBUG_ICMP
, ("Called.\n"));
303 /* Calculate checksum of ICMP header and data */
304 ((PICMP_HEADER
)IPPacket
->Data
)->Checksum
= (USHORT
)
305 IPv4Checksum(IPPacket
->Data
, IPPacket
->TotalSize
- IPPacket
->HeaderSize
, 0);
307 /* Get a route to the destination address */
308 if ((NCE
= RouteGetRouteToDestination(&IPPacket
->DstAddr
))) {
309 /* Send the packet */
310 IPSendDatagram(IPPacket
, NCE
);
312 /* No route to destination (or no free resources) */
313 TI_DbgPrint(DEBUG_ICMP
, ("No route to destination address 0x%X.\n",
314 IPPacket
->DstAddr
.Address
.IPv4Address
));
315 IPPacket
->Free(IPPacket
);
321 PIP_INTERFACE Interface
,
326 * FUNCTION: Transmits an ICMP packet in response to an incoming packet
328 * NTE = Pointer to net table entry to use
329 * IPPacket = Pointer to IP packet that was received
330 * Type = ICMP message type
331 * Code = ICMP message code
333 * We have received a packet from someone and is unable to
334 * process it due to error(s) in the packet or we have run out
335 * of resources. We transmit an ICMP message to the host to
336 * notify him of the problem
342 TI_DbgPrint(DEBUG_ICMP
, ("Called. Type (%d) Code (%d).\n", Type
, Code
));
344 DataSize
= IPPacket
->TotalSize
- IPPacket
->HeaderSize
;
346 if( !PrepareICMPPacket(NULL
, Interface
, &NewPacket
, &IPPacket
->SrcAddr
,
347 IPPacket
->Data
, DataSize
) ) return;
349 ((PICMP_HEADER
)NewPacket
.Data
)->Type
= Type
;
350 ((PICMP_HEADER
)NewPacket
.Data
)->Code
= Code
;
351 ((PICMP_HEADER
)NewPacket
.Data
)->Checksum
= 0;
353 ICMPTransmit(&NewPacket
, NULL
, NULL
);