Search code examples
headertcpnetwork-programming

access and change the reserved bits on tcp header


in rfc793, section-3.1, TCP header header have 6 reserved bits that decreased to 4 reserved bits on rfc3168. How can i access and change these bits on Linux or with a program or even how can i write a program for this goal?


Solution

  • The standard sockets API does not provide a way for applications to directly change the individual flags in the TCP Header.

    I can think of a couple of possible ways to approach this:

    • Write a TUN/TAP network device that performs the required header modifications on the packets that pass through it, before forwarding them on to the real network device. The kernel is configured to send all outgoing packets though your TUN/TAP device.

    • Use a PF_PACKET socket to construct and send completely custom TCP packets from userspace. You'll be responsible for implementing all of IP and TCP yourself, though, including things like IP fragment reassembly. I don't recommend this, unless you just want to fire off a few packets rather than establish a full TCP connection.

    • Create a netfilter (iptables) module that implements a TCP-flag-modification target. You could do this quite easily based on the existing ipt_ECN.c module, which is only 146 lines.