Address Resolution Protocol

From OSDev Wiki
Jump to navigation Jump to search

Address Resolution Protocol, commonly referred to as "ARP", is a protocol that permits hosts to gain the ethernet address (MAC) of a remote host when they only have an IPv4 address. Technically, ARP supports other address formats due to a variable size data format, however it is typically used with Ethernet and IP addresses.

It should be noted that similar functionality is provided in IPv6 by a different protocol.

Background

ARP is defined in RFC826. It runs over the link layer.

Packet Format

** Note **

All integer literals should be assumed to be in little-endian notation. When sending packets you will need to convert 16-bit and 32-bit integers to big-endian.

C-style Packet Format

struct arp
{
    u16 htype; // Hardware type
    u16 ptype; // Protocol type
    u8  hlen; // Hardware address length (Ethernet = 6)
    u8  plen; // Protocol address length (IPv4 = 4)
    u16 opcode; // ARP Operation Code
    u8  srchw[hlen]; // Source hardware address - hlen bytes (see above)
    u8  srcpr[plen]; // Source protocol address - plen bytes (see above). If IPv4 can just be a "u32" type.
    u8  dsthw[hlen]; // Destination hardware address - hlen bytes (see above)
    u8  dstpr[plen]; // Destination protocol address - plen bytes (see above). If IPv4 can just be a "u32" type.
};

Description

Hardware Type

The type of the hardware layer over which the ARP packet is being sent. Ethernet is 0x1, and you shouldn't need any other value unless you're working with PPP or something.

Protocol Type

The type of the protocol address that the ARP request uses. IP is 0x0800. Again, you shouldn't need another value here unless you have unusual requirements (in which case you'll hopefully know what you're doing anyway).

Hardware Length

The length of the hardware address for the type of hardware layer. MAC addresses are 6 bytes in length.

Protocol Length

The length of the protocol address for the protocol layer. IPv4 addresses are 4 bytes in length.

Operation Code

The operation to perform.

Source/Dest Hardware Address

Hardware addresses of the source and destination hosts. For an ARP request most implementations zero the destination MAC address.

Source/Dest Protocol Address

Protocol addresses of the source and destination hosts.

Operations

ARP Request

An ARP request allows a host to find the MAC address for a host whose IP is known. It's operation code is 0x0001.

ARP Reply

An ARP reply is the response to an ARP request. It's operation code is 0x0002.