Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

raw socket listener

This is a quick question for linux c programming raw sockets. If I wanted to just listen to any interface with a raw socket, must I actually bind to an ip address or interface to listen to traffic? From what I understand, I feel like I should be able to just call sock(); and then start recvfrom() traffic. Maybe I'm wrong, but I've seen some programs that don't use it.

like image 984
Dr.Knowitall Avatar asked Oct 26 '12 06:10

Dr.Knowitall


People also ask

What is a raw socket connection?

A raw socket is a type of socket that allows access to the underlying transport provider. This topic focuses only on raw sockets and the IPv4 and IPv6 protocols. This is because most other protocols with the exception of ATM do not support raw sockets.

Why raw socket is important?

RAW-sockets are an additional type of Internet socket available in addition to the well known DATAGRAM- and STREAM-sockets. They do allow the user to see and manipulate the information used for transmitting the data instead of hiding these details, like it is the case with the usually used STREAM- or DATAGRAM sockets.

What is a raw socket in Linux?

Raw sockets allow new IPv4 protocols to be implemented in user space. A raw socket receives or sends the raw datagram not including link level headers. The IPv4 layer generates an IP header when sending a packet unless the IP_HDRINCL socket option is enabled on the socket.

Why is raw socket important and where is it used?

A raw socket is used to receive raw packets. This means packets received at the Ethernet layer will directly pass to the raw socket. Stating it precisely, a raw socket bypasses the normal TCP/IP processing and sends the packets to the specific user application (see Figure 1).


2 Answers

You are right, the only thing you will need to do is call socket() and then recvfrom(). Nevertheless be aware of the fact that there are some limitations with listening using SOCK_RAW.

If you're not using raw sockets on a "send-and-forget" basis, you will be interested in reading the reply packet(s) for your raw packet(s). The decision logic for whether a packet will be delivered to a raw socket can be enumarated as such:

  1. TCP and UDP packets are never delivered to raw sockets, they are always handled by the kernel protocol stack.

  2. Copies of ICMP packets are delivered to a matching raw socket. For some of the ICMP types (ICMP echo request, ICMP timestamp request, mask request) the kernel, at the same time, may wish to do some processing and generate replies.

  3. All IGMP packets are delivered to raw sockets: e.g. OSPF packets.

  4. All other packets destined for protocols that are not processed by a kernel subsystem are delivered to raw sockets.

The fact that you're dealing with a protocol for which reply packets are delivered to your raw socket does not necessarily mean that you'll get the reply packet. For this you may also need to consider:

  1. setting the protocol accordingly while creating your socket via socket(2)system call. For instance, if you're sending an ICMP echo-request packet, and want to receive ICMP echo-reply, you can set the protocol argument (3rd argument) to IPPROTO_ICMP).

  2. setting the protocol argument in socket(2) to 0, so any protocol number in the received packet header will match.

  3. defining a local address for your socket (via e.g. bind(2)), so if the destination address matches the socket's local address, it'll be delivered to your application also.

For more details you can read e.g. this.

like image 168
codewarrior Avatar answered Oct 06 '22 02:10

codewarrior


If you meant to capture the traffic on a interface, you can use libpcap.

like image 33
lqs Avatar answered Oct 06 '22 02:10

lqs