Change ipv4 packet into ipv6 packet cause some data loss

Dec 29, 2014 at 7:32 AM
I have Winforms application and i am using PcapDotNet DLLs.
Because last PcapDotNet version support ipv6 i write simple function that change ipv4 packet into ipv6 packet:
private Packet ConvertToIPv6(Packet packet, string oldIpAddress, string newIpv6Address)
{
    try
    {
        if (packet.Ethernet.IpV4.Source.ToString() == oldIpAddress || packet.Ethernet.IpV4.Destination.ToString() == oldIpAddress)
        {
            EthernetLayer ethernet = (EthernetLayer)packet.Ethernet.ExtractLayer();
            DateTime packetTimestamp = packet.Timestamp;

            EthernetLayer ethernetLayer =
            new EthernetLayer
            {
                Source = ethernet.Source,
                Destination = ethernet.Destination,
                EtherType = EthernetType.None,
            };

            if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Tcp)
            {
                ILayer payload = packet.Ethernet.IpV4.Tcp.Payload.ExtractLayer();
                TcpLayer tcpLayer =
                    new TcpLayer
                    {
                        SourcePort = packet.Ethernet.IpV4.Tcp.SourcePort,
                        DestinationPort = packet.Ethernet.IpV4.Tcp.DestinationPort,
                        Checksum = null, // Will be filled automatically.
                        SequenceNumber = packet.Ethernet.IpV4.Tcp.SequenceNumber,
                        AcknowledgmentNumber = packet.Ethernet.IpV4.Tcp.AcknowledgmentNumber,
                        ControlBits = TcpControlBits.Acknowledgment,
                        Window = packet.Ethernet.IpV4.Tcp.Window,
                        UrgentPointer = packet.Ethernet.IpV4.Tcp.UrgentPointer,
                        Options = TcpOptions.None,
                    };

                IpV6Layer ipV6Layer = new IpV6Layer();
                if (packet.Ethernet.IpV4.Source.ToString() == oldIpAddress)
                {
                    ipV6Layer.Source = new IpV6Address(newIpv6Address);
                    ipV6Layer.CurrentDestination = new IpV6Address("FEDC:BA98:7654:3210:FEDC:BA98:0000:0000");
                }
                else
                {
                    ipV6Layer.Source = new IpV6Address("FEDC:BA98:7654:3210:FEDC:BA98:0000:0000");
                    ipV6Layer.CurrentDestination = new IpV6Address(newIpv6Address);
                }
                ipV6Layer.FlowLabel = 123;
                ipV6Layer.HopLimit = 100;
                ipV6Layer.NextHeader = IpV4Protocol.Tcp;

                return PacketBuilder.Build(packetTimestamp, ethernet, ipV6Layer, tcpLayer, payload);
            }
            else if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Udp)
            {
                ILayer payload = packet.Ethernet.IpV4.Udp.Payload.ExtractLayer();
                UdpLayer udpLayer =
                    new UdpLayer
                    {
                        SourcePort = packet.Ethernet.IpV4.Udp.SourcePort,
                        DestinationPort = packet.Ethernet.IpV4.Udp.DestinationPort,
                        Checksum = null, // Will be filled automatically.
                        CalculateChecksumValue = true,
                    };

                IpV6Layer ipV6Layer = new IpV6Layer();
                if (packet.Ethernet.IpV4.Source.ToString() == oldIpAddress)
                {
                    ipV6Layer.Source = new IpV6Address(newIpv6Address);
                    ipV6Layer.CurrentDestination = new IpV6Address("FEDC:BA98:7654:3210:FEDC:BA98:0000:0000");
                }
                else
                {
                    ipV6Layer.Source = new IpV6Address("FEDC:BA98:7654:3210:FEDC:BA98:0000:0000");
                    ipV6Layer.CurrentDestination = new IpV6Address(newIpv6Address);
                }
                ipV6Layer.FlowLabel = 123;
                ipV6Layer.HopLimit = 100;
                ipV6Layer.NextHeader = IpV4Protocol.Udp;

                return PacketBuilder.Build(packetTimestamp, ethernet, ipV6Layer, udpLayer, payload);

            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }
    }
    catch (Exception)
    {
        return null;
    }
}
And i think that is some packet the new ipv6 packet missing some of the date.
Please see this original packet and this new ipv6 packet.
The original packet TCP layer contain 32 bytes and the new ipv6 packet TCP layer contains only 20 bytes (and i think that some of the data also is different).
Am i right or this is OK ?
Coordinator
Jan 9, 2015 at 5:48 PM
Hi gshhar,

I've looked a bit at your code.
Why don't you use TcpDatagram.ExtractLayer() method?

Boaz.
Jan 9, 2015 at 8:17 PM
Edited Jan 9, 2015 at 8:18 PM
What the benefit of using TcpDatagram.ExtractLayer() ?
Can i have an code example ?

What about the missing data i found ?
Coordinator
Jan 10, 2015 at 8:22 PM
It seems that you don't copy the TCP options, which will make your TCP layer shorter.
If you use ExtractLayer() it will copy all the fields.

TcpLayer tcpLayer = packet.Ethernet.IpV4.Tcp.ExtractLayer();