2
votes

When receiving on an ICMP socket, (SOCK_RAW with IPPROTO_ICMP), since there is no concept of "port" in the ICMP protocol, how can an application determine that a received packet is not part of some other TCP/UDP/whatever socket transmission that is also happening at the same time?

For example, suppose you have an application with 2 threads. Thread 1 sets up a TCP server socket, and continuously receives data from a connected client. Thread 2 continuously sends echo request packets (ping) to the same client using an ICMP socket, and then receives echo replys. What is to prevent Thread 2 from receiving one of the TCP packets instead?

3

3 Answers

7
votes

ICMP is a different protocol from TCP and UDP, as determined by the protocol field in the IP header. When you open a socket with IPPROTO_ICMP, you're telling the socket to transmit and receive only packets with IP headers whose protocol field is set to ICMP.

Similarly, sockets opened with IPPROTO_TCP or IPPROTO_UDP respond only to packets whose IP headers contain a protocol field that is set to TCP or UDP, respectively.

1
votes

You can check the ICMP header for the type and see if its ICMP Echo Response (Type 0). Also in ICMP, the response will contain the request you had sent in the first place.

1
votes

Received UDP & TCP packets never passed to raw sockets . IF a process wants to read IP datagram containing UDP or TCP packets the packets must be read at data link layer . check this link

http://aschauf.landshut.org/fh/linux/udp_vs_raw/ch01s03.html

if the packet is not cached at layer 2 then it is processed by kernel . And if the packet is of icmp protocol and it is of type echo request or timestamp request or address mask request then it is entirely processed by kernel otherwise it will passed to RAW SOCKETS.

Another one all datagrams with a protocol field that the kernel does not understand are passed to raw sockets only basic processing of ip is done on them

At last if datagram arrives in fragments then nothing is passed to raw sockets until all fragments are arived and reassembled .

If you want to learn more, then read this book.