Question

I am trying to checksum icmp packet using the same technique for tcp and udp but it get wrong checksum, can you tell me where is my problem ?

        ICMP_HEADER *icmpheader = new ICMP_HEADER;
        memcpy(icmpheader,ICMPHeader,sizeof(ICMP_HEADER));
        icmpheader->Checksum = 0;

        PSEUDO_HEADER *psheader = new PSEUDO_HEADER;
        memcpy(&psheader->daddr, &IPHeader->DestinationAddress, sizeof(UINT));
        memcpy(&psheader->saddr, &IPHeader->SourceAddress, sizeof(UINT));
        psheader->protocol = IPHeader->Protocol;
        psheader->length = htons((USHORT)(sizeof(ICMP_HEADER) + ICMPDataSize));
        psheader->zero = 0x0000;

        UINT packet_size = sizeof(ICMP_HEADER) + ICMPDataSize + sizeof(PSEUDO_HEADER);
        packet_size = packet_size + ((packet_size%2)*2);
        UCHAR *icmppacket = (UCHAR*)malloc(packet_size);

        memset(icmppacket,0, packet_size);
        memcpy(icmppacket, psheader, sizeof(PSEUDO_HEADER));
        memcpy(&icmppacket[sizeof(PSEUDO_HEADER)], icmpheader,sizeof(ICMP_HEADER));
        memcpy(&icmppacket[sizeof(PSEUDO_HEADER) + sizeof(ICMP_HEADER)],ICMPData,ICMPDataSize);


        if (GlobalChecksum((USHORT*)icmppacket,packet_size) != ICMPHeader->Checksum)
        {
            isMalformed = true;
            PacketError = PACKET_ICMP_CHECKSUM;
        }   

        USHORT cPacket::GlobalChecksum(USHORT *buffer, UINT length)
{
    register int sum = 0;
    USHORT answer = 0;
    register USHORT *w = buffer;
    register int nleft = length;

    while(nleft > 1){
    sum += *w++;
    nleft -= 2;
    }

    sum = (sum >> 16) + (sum & 0xFFFF);
    sum += (sum >> 16);
    answer = ~sum;
    return(answer);
}
Was it helpful?

Solution

solved it by removing pseudo header and calculating icmp header and icmp data only

ICMP_HEADER *icmpheader = new ICMP_HEADER;
memcpy(icmpheader,ICMPHeader,sizeof(ICMP_HEADER));
icmpheader->Checksum = 0x0000;

UINT packet_size = sizeof(ICMP_HEADER) + ICMPDataSize;
packet_size = packet_size + ((packet_size%2)*2);
UCHAR *icmppacket = (UCHAR*)malloc(packet_size);

memset(icmppacket,0, packet_size);
memcpy(icmppacket, icmpheader,sizeof(ICMP_HEADER));
memcpy(&icmppacket[sizeof(ICMP_HEADER)],ICMPData,ICMPDataSize);

if (GlobalChecksum((USHORT*)icmppacket,packet_size) != ICMPHeader->Checksum)
{
    isMalformed = true;
    PacketError = PACKET_ICMP_CHECKSUM;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top