Analyzing TCP/UDP/IP headers for Raw Sockets

In this article, we look at certain aspects of TCP/UDP/IP headers which we would most likely be used in the coming example implementations for Raw socket for TCP/UDP protocols in this website. 

Kindly note that this article does not try to explain the TCP/UDP/IP headers in detail, but only tries to provide context to the reader in understanding the code examples that will follow in the coming articles.

For a more detailed understanding of TCP/IP protocol, refer RFCs below

TCP RFC 793

UDP RFC 768

IP RFC 791

TCP Protocol Header

The TCP header is provided below for reference. The default TCP header size is 20 bytes in length. If additional Options fields are added, then the TCP header becomes larger than the default 20 bytes. In the coming code examples, The TCP header is maintained to be 20 bytes.

FIG Courtesy: https://en.wikipedia.org/wiki/Transmission_Control_Protocol

If the protocol field is chosen as IPPROTO_TCP for the raw socket, then the TCP header needs to be added to the packet by the application. Hence, the source port, destination port should be added by the application.

A random number generator can be used to generate the first sequence number of the TCP packet and to increment the sequence number later on. For most RAW socket examples in the following articles, the sequence number is not changed and the code is to just show a TCP transmission on a RAW socket and does delve into the protocol in itself.

The window size and flags and check sum fields can also be set just for testing whether the fields set by the application are the fields that are received at the other end. The checksum field takes into account certain fixed IP fields (pseudo header) and the code examples refer the “Unix network programming” book for the check sum calculation. An interesting piece of information on the Checksum pseudo-header is provided in the link below:

Check-sum pseudo header- purpose of pseudo header in TCP checksum

UDP Protocol Header

The UDP header is provided below. The UDP header size is 8 bytes. 

FIG Courtesy: https://en.wikipedia.org/wiki/User_Datagram_Protocol

If the Raw socket is created with IPPROTO_UDP as the protocol, the above header fields need to be filled by the application. The checksum calculation also is based on a pseudo-header and the calculation is similar to checksum calculation for the TCP packet.

The interested reader can refer the below wiki pages on UDP and TCP to understand the checksum calculation better.

https://en.wikipedia.org/wiki/Transmission_Control_Protocol

https://en.wikipedia.org/wiki/User_Datagram_Protocol

IPv4 Layer Header

The IPv4 header is provided below. The IPv4 header size is 20 bytes. Addition of optional elements can increase the size of the header. 

The Protocol field is filled with the upper layer protocol that is accessing the IP layer and it is important for the IP layer to check this field on the receive side to know which upper layer protocol that the packet needs to be routed towards. IPPROTO_TCP/IPPROTO_UDP are placed in the protocol field.

The source address and the destination addresses are the IP addresses of the source and the destination.

Raw Socket Code with IPPROTO_TCP protocol

Comments

  1. Pingback: Raw Sockets – an Introduction (continued) | Hitch Hiker's Guide to Learning

  2. Pingback: Raw Socket Code with IPPROTO_UDP protocol | Hitch Hiker's Guide to Learning

Leave a Reply

Your email address will not be published. Required fields are marked *