Checksum calculation when adding payload to syn packet

Hi Sara,

I found your error. I think it works for me let’s see if you have the same error.

The main mistake I found is that you miss a field. This one:

bit<1>  cwr;

So your function should be:

(...)
hdr.tcp.res,
hdr.tcp.cwr, //missing field
hdr.tcp.ecn,
(...)

Now, if you want to add my_tunnel to your packet, you have to run the following checksum function:

//In your action
    hdr.ipv4.totalLen=hdr.ipv4.totalLen + TUNNEL_HDR_SIZE;
    meta.tcpLength = hdr.ipv4.totalLen - (bit<16>)(hdr.ipv4.ihl)*4

//In your checksum computation
    update_checksum_with_payload(hdr.tcp.isValid(),
        {   hdr.ipv4.srcAddr,
            hdr.ipv4.dstAddr,
            8w0,
            hdr.ipv4.protocol,
            meta.tcpLength, //I changed this name, so change it back to yours
            hdr.tcp.srcPort,
            hdr.tcp.dstPort,
            hdr.tcp.seqNo,
            hdr.tcp.ackNo,
            hdr.tcp.dataOffset,
            hdr.tcp.res,
            hdr.tcp.cwr,
            hdr.tcp.ecn,
            hdr.tcp.urg,
            hdr.tcp.ack,
            hdr.tcp.psh,
            hdr.tcp.rst,
            hdr.tcp.syn,
            hdr.tcp.fin,
            hdr.tcp.window,
            16w0,
            hdr.tcp.urgentPtr,
            hdr.my_tunnel.proto_id,
            hdr.my_tunnel.dst_id},
        hdr.tcp.checksum, HashAlgorithm.csum16);
    }

The problem at this point is to add TCP options. Why? you might ask. Well, a few reasons:

  1. If you use options defined as a varbit field… I have no idea how they behave. So… you will have to test the update_checksum_with_payload function again with TCP options ar varbit :slight_smile:
  2. If you do not parse TCP options then my_tunnel will remain between the TCP header and the options. If that works for you then you are fine.
  3. Also, be aware that if the packet has IPv4 options, my code does not account for that right now. If your use case can does not include options… then much better.

I remember this file from Andy Fingerhut (here) in which he defines options as “typical” without varbit fields. He shared two parsers there so check if any works for you (in one of them he uses a stack). Still, it is going to be a little challenge to calculate the TCP checksum IF you need to parse TCP options mandatorily. I will let you investigate that.

I hope it helps.

Cheers,