IpV4 fragmentation lead to TCP bad checksum

222 views
Skip to first unread message

gsh...@gmail.com

unread,
Jun 19, 2018, 11:49:57 AM6/19/18
to Pcap.Net Q&A
I want to split packet in ip layer so i am using this function:

public static IEnumerable<Packet> FragmentIPv4Layer(Packet packet, int NumberOfFragments)
{
EthernetLayer ethernetLayer = ExtractPacketEthernetLayer(packet);
IpV4Datagram ipV4Datagram = ExtractPacketIpV4DatagramLayer(packet);
TransportLayer transportlayer = ExtractPacketTransportLayer(packet);
if (transportlayer != null)
{
IpV4Layer ipV4Layer = ExtractPacketIpV4Layer(packet);
ipV4Layer.HeaderChecksum = null;
DateTime packetTimestamp = GetPacketTimeStamp(packet);

// Extract the data.
ILayer layer = ExtractPacketDatagramLayer(packet);
int totalLength = layer.Length;

// Split data into smaller segments.
int partialLength = totalLength / NumberOfFragments;
//partialLength = 1478;

// Make sure it's divisible with 8.
partialLength = (partialLength / 8) * 8;
if (partialLength == 0)
partialLength = 8;

// Send one by one.
ushort offset = 0;

while (offset < totalLength)
{
// Get length for this fragment.
int fragmentLength = partialLength;
IpV4FragmentationOptions options = IpV4FragmentationOptions.MoreFragments;

// Is this the last fragment ? trim length if needed.
if (offset + fragmentLength >= totalLength)
{
options = IpV4FragmentationOptions.None;
fragmentLength = totalLength - offset;
}

// Copy the actual data into a new buffer.
byte[] newBuffer = ipV4Datagram.Payload.ToArray();
PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(newBuffer, offset, fragmentLength) };

// Change IP layer fragmentation options.
ipV4Layer.Fragmentation = new IpV4Fragmentation(options, offset);
transportlayer.Checksum = null;
yield return PacketBuilder.Build(packetTimestamp, ethernetLayer, ipV4Layer, transportlayer, payloadLayer);

// Next offset.
offset += (ushort)fragmentLength;
}
}
}

The result is that all my TCP packets (not the ipv4) is with bad checksum in the TCP layer.

Am i doing something wrong ?

gsh...@gmail.com

unread,
Jun 19, 2018, 12:01:13 PM6/19/18
to Pcap.Net Q&A
I also provide here the original file and the file with the fragmentation packets.

Original file:
https://drive.google.com/open?id=1mTKQiIkp2f5d-vrJsNRW3VKZuYy7rzkm

Fragmentation file:
https://drive.google.com/open?id=1iNg4cfJSDS3GdjpfueX-ooGeOw0lCeCP

Boaz Brickner

unread,
Sep 10, 2018, 3:40:24 AM9/10/18
to Pcap.Net Q&A
In Pcap.Net, the TCP checksum is calculated per packet.
In fragmented packets, you need to calculate the TCP checksum over the defragmented packet, which combines multiple packets.
So the automatic calculation of TCP checksum per packet wouldn't work as expected.

You might want to take the checksum of the defragmented packet instead.

I hope this helps,
Boaz.
Reply all
Reply to author
Forward
0 new messages