Re: Q: Extract hardware address from received datagram?

From: Henrik Nordstrom
Date: Mon Aug 15 2005 - 05:10:31 EST


On Sun, 14 Aug 2005, [iso-8859-1] Albrecht Dreß wrote:

is there any possibility to read the hardware address (MAC) of the remote machine from received PF_INET/SOCK_DGRAM packets?

Not that I know of. Hardware link addresses is generally unknown to the IP layer of the kernel, in large due to the simple fact that not all IP packets have link addresses and in addition there exists several forms of link addresses (Ethernet is not the only means of communication in this world).

I want to write an application which opens a datagram socket, binds it to a specific port and receives broadcast messages. The sending machine is not necessarily in the same net segment. It is of course easy to get the remote ip address, but I also need the in the information in the packet header, in particular the 6-byte hardware (MAC) address of the sending machine.

You could use a PF_PACKET socket to receive any Ethernet frames received on the selected interface. You will need to decode the frame yourself to see if it is a UDP packet and addressed to your port.

Another alternative is to use the QUEUE target of iptables and write the receiving part of your application as a QUEUE handler getting the packets this way.

But as already noted in this thread, you can only receive packets which are sent on the wire connected to you. Broadcasts from stations in other segments is unlikely to appear there as routers do not by definition forward broadcasts, and also by definition never forwards the link layer addressing.

Regards
Henrik
-
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html