Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux raw ethernet socket bind to specific protocol

I'm writing code to send raw Ethernet frames between two Linux boxes. To test this I just want to get a simple client-send and server-receive.

I have the client correctly making packets (I can see them using a packet sniffer).

On the server side I initialize the socket like so:

fd = socket(PF_PACKET, SOCK_RAW, htons(MY_ETH_PROTOCOL));

where MY_ETH_PROTOCOL is a 2 byte constant I use as an ethertype so I don't hear extraneous network traffic.

when I bind this socket to my interface I must pass it a protocol again in the socket_addr struct: socket_address.sll_protocol = htons(MY_ETH_PROTOCOL);
If I compile and run the code like this then it fails. My server does not see the packet. However if I change the code like so:
socket_address.sll_protocol = htons(ETH_P_ALL);
The server then can see the packet sent from the client (as well as many other packets) so I have to do some checking of the packet to see that it matches MY_ETH_PROTOCOL.

But I don't want my server to hear traffic that isn't being sent on the specified protocol so this isn't a solution. How do I do this?

like image 361
dschatz Avatar asked Jul 29 '10 20:07

dschatz


People also ask

How use 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.

How does a raw socket work?

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).

What is Sock_raw?

Send and Receive Operations. Once an application creates a socket of type SOCK_RAW, this socket may be used to send and receive data. All packets sent or received on a socket of type SOCK_RAW are treated as datagrams on an unconnected socket.

What is Eth_p_all?

When protocol is set to htons(ETH_P_ALL), then all protocols are received. All incoming packets of that protocol type will be passed to the packet socket before they are passed to the protocols implemented in the kernel.


1 Answers

I have resolved the issue.

According to http://linuxreviews.org/dictionary/Ethernet/ referring to the 2 byte field following the MAC addresses:

"values of that field between 64 and 1522 indicated the use of the new 802.3 Ethernet format with a length field, while values of 1536 decimal (0600 hexadecimal) and greater indicated the use of the original DIX or Ethernet II frame format with an EtherType sub-protocol identifier."

so I have to make sure my ethertype is >= 0x0600.

According to http://standards.ieee.org/regauth/ethertype/eth.txt use of 0x88b5 and 0x88b6 is "available for public use for prototype and vendor-specific protocol development." So this is what I am going to use as an ethertype. I shouldn't need any further filtering as the kernel should make sure to only pick up ethernet frames with the right destination MAC address and using that protocol.

like image 167
dschatz Avatar answered Sep 27 '22 18:09

dschatz