Raw Socket Communication with Data Link Layer

In the previous examples, we used the TCP and UDP protocols and added the TCP/UDP headers in the sample application. The IP header was added by the linux kernel since we had not set the IP_HDRINCL flag via setsockopt API or used IPPROTO_RAW for the protocol in the socket API.

However, Raw Sockets can be used to access the data link layer directly.  In the case of Datalink Layer Raw socket, we use ETH_P_ALL (or relevant protocol can be used – e.g. ETH_P_ARP) as the protocol. If the implementer wishes to send a TCP/UDP packet with the ETH_P_ALL protocol flag, then the L2 header, IP (L3) header and the TCP/UDP (L4) header needs to be incorporated by the application.

In the following articles, we try to send a packet wherein we select an L2 protocol (like ARP) and so do not have to concern ourselves with forming the upper layer protocols.

Raw Socket Communication with Data Link layer – code example

Leave a Reply

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