2020-03-27 18:06:16 +00:00
|
|
|
#define ICMP_TYPE_ECHO_REPLY 0
|
|
|
|
#define ICMP_TYPE_ECHO_REQUEST 8
|
2021-02-22 23:50:14 +00:00
|
|
|
#define ICMP_CODE_ECHO 0 // RFC 792: "Echo or Echo Reply Message". 0 is the only code explicitly defined for Echo.
|
2020-03-27 18:06:16 +00:00
|
|
|
|
2021-02-22 23:50:14 +00:00
|
|
|
class CICMPHeader
|
2020-03-27 18:06:16 +00:00
|
|
|
{
|
2020-07-31 18:49:37 +01:00
|
|
|
U8 type;
|
|
|
|
U8 code;
|
2020-03-27 18:06:16 +00:00
|
|
|
U16 checksum;
|
|
|
|
|
|
|
|
U16 identifier;
|
|
|
|
U16 sequence_number;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-02-22 23:50:14 +00:00
|
|
|
U0 ICMPReplySend(U32 destination_ip_address,
|
2020-07-28 08:36:21 +01:00
|
|
|
U16 identifier,
|
|
|
|
U16 sequence_number,
|
|
|
|
U16 request_checksum,
|
|
|
|
U8 *payload,
|
|
|
|
I64 length)
|
2020-03-27 18:06:16 +00:00
|
|
|
{
|
2021-01-19 04:31:05 +00:00
|
|
|
U8 *frame;
|
|
|
|
I64 de_index;
|
|
|
|
CICMPHeader *header;
|
2020-03-27 18:06:16 +00:00
|
|
|
|
|
|
|
de_index = IPV4PacketAllocate(&frame,
|
2020-07-28 08:36:21 +01:00
|
|
|
IP_PROTOCOL_ICMP,
|
2021-02-22 23:50:14 +00:00
|
|
|
IPV4AddressGet(),
|
2020-07-28 08:36:21 +01:00
|
|
|
destination_ip_address,
|
|
|
|
sizeof(CICMPHeader) + length);
|
2020-03-27 18:06:16 +00:00
|
|
|
if (de_index < 0)
|
|
|
|
{
|
2021-01-21 00:33:03 +00:00
|
|
|
NetErr("ICMP SEND REPLY: Failed to allocate IPV4 packet.");
|
2021-02-22 08:35:59 +00:00
|
|
|
return;
|
2020-03-27 18:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
header = frame;
|
|
|
|
|
2020-07-31 18:49:37 +01:00
|
|
|
header->type = ICMP_TYPE_ECHO_REPLY;
|
|
|
|
header->code = 0; // why is 0 okay?
|
|
|
|
header->checksum = EndianU16(EndianU16(request_checksum) + 0x0800);
|
|
|
|
header->identifier = identifier;
|
|
|
|
header->sequence_number = sequence_number;
|
|
|
|
// TODO: header checksum is awful. Shrine says hack alert.
|
2020-03-27 18:06:16 +00:00
|
|
|
|
|
|
|
MemCopy(frame + sizeof(CICMPHeader), payload, length);
|
|
|
|
|
|
|
|
IPV4PacketFinish(de_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
I64 ICMPHandler(CIPV4Packet *packet)
|
|
|
|
{
|
|
|
|
CICMPHeader *header;
|
|
|
|
|
|
|
|
if (packet->length < sizeof(CICMPHeader))
|
|
|
|
{
|
2021-01-21 00:33:03 +00:00
|
|
|
NetErr("ICMP HANDLER: Caught wrong IPV4 length.");
|
2020-03-27 18:06:16 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
header = packet->data;
|
|
|
|
|
2021-02-22 23:50:14 +00:00
|
|
|
if (header->type == ICMP_TYPE_ECHO_REQUEST && header->code == ICMP_CODE_ECHO)
|
2020-03-27 18:06:16 +00:00
|
|
|
{
|
|
|
|
ARPCachePut(packet->source_ip_address, packet->ethernet_frame->source_address);
|
|
|
|
|
2021-02-22 23:50:14 +00:00
|
|
|
ICMPReplySend(packet->source_ip_address,
|
2020-07-28 08:36:21 +01:00
|
|
|
header->identifier,
|
|
|
|
header->sequence_number,
|
|
|
|
header->checksum,
|
2021-02-22 23:50:14 +00:00
|
|
|
packet->data + sizeof(CICMPHeader), // Data payload at IPV4Packet data location after the ICMP header
|
|
|
|
packet->length - sizeof(CICMPHeader));// Payload length is size of packet after dropping header.
|
2020-03-27 18:06:16 +00:00
|
|
|
}
|
2021-02-06 11:05:14 +00:00
|
|
|
else
|
|
|
|
NetWarn("ICMP HANDLER: Unhandled ICMP packet. type, code: 0x%X, 0x%X", header->type, header->code);
|
2020-03-27 18:06:16 +00:00
|
|
|
|
2021-01-27 01:38:49 +00:00
|
|
|
NetLog("ICMP HANDLER: Exiting.");
|
|
|
|
|
2020-03-27 18:06:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|