iPXE
Functions | Variables
arp.c File Reference

Address Resolution Protocol. More...

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <byteswap.h>
#include <errno.h>
#include <ipxe/if_ether.h>
#include <ipxe/if_arp.h>
#include <ipxe/iobuf.h>
#include <ipxe/netdevice.h>
#include <ipxe/neighbour.h>
#include <ipxe/arp.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
int arp_tx_request (struct net_device *netdev, struct net_protocol *net_protocol, const void *net_dest, const void *net_source)
 Transmit ARP request.
static struct arp_net_protocolarp_find_protocol (uint16_t net_proto)
 Identify ARP protocol.
static int arp_rx (struct io_buffer *iobuf, struct net_device *netdev, const void *ll_dest __unused, const void *ll_source __unused, unsigned int flags __unused)
 Process incoming ARP packets.
static const char * arp_ntoa (const void *net_addr __unused)
 Transcribe ARP address.

Variables

struct net_protocol arp_protocol __net_protocol
 ARP network protocol.
struct neighbour_discovery arp_discovery
 ARP neighbour discovery protocol.

Detailed Description

Address Resolution Protocol.

This file implements the address resolution protocol as defined in RFC826. The implementation is media-independent and protocol-independent; it is not limited to Ethernet or to IPv4.

Definition in file arp.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
int arp_tx_request ( struct net_device netdev,
struct net_protocol net_protocol,
const void *  net_dest,
const void *  net_source 
)

Transmit ARP request.

Parameters:
netdevNetwork device
net_protocolNetwork-layer protocol
net_destDestination network-layer address
net_sourceSource network-layer address
Return values:
rcReturn status code

Definition at line 59 of file arp.c.

References alloc_iob(), arphdr::ar_hln, arphdr::ar_hrd, arphdr::ar_op, arphdr::ar_pln, arphdr::ar_pro, ARPOP_REQUEST, DBGC, ENOMEM, htons, iob_put, iob_reserve, net_device::ll_addr, ll_protocol::ll_addr_len, net_device::ll_broadcast, ll_protocol::ll_proto, net_device::ll_protocol, MAX_LL_ADDR_LEN, MAX_LL_HEADER_LEN, MAX_NET_ADDR_LEN, memcpy(), memset(), net_protocol::name, net_device::name, net_protocol::net_addr_len, net_protocol::net_proto, net_tx(), net_protocol::ntoa, rc, and strerror().

Referenced by ipoib_transmit(), and ipv4_gratuitous_arp().

                                                                    {
        struct ll_protocol *ll_protocol = netdev->ll_protocol;
        struct io_buffer *iobuf;
        struct arphdr *arphdr;
        int rc;

        /* Allocate ARP packet */
        iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *arphdr ) +
                            ( 2 * ( MAX_LL_ADDR_LEN + MAX_NET_ADDR_LEN ) ) );
        if ( ! iobuf )
                return -ENOMEM;
        iob_reserve ( iobuf, MAX_LL_HEADER_LEN );

        /* Build up ARP request */
        arphdr = iob_put ( iobuf, sizeof ( *arphdr ) );
        arphdr->ar_hrd = ll_protocol->ll_proto;
        arphdr->ar_hln = ll_protocol->ll_addr_len;
        arphdr->ar_pro = net_protocol->net_proto;
        arphdr->ar_pln = net_protocol->net_addr_len;
        arphdr->ar_op = htons ( ARPOP_REQUEST );
        memcpy ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
                 netdev->ll_addr, ll_protocol->ll_addr_len );
        memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
                 net_source, net_protocol->net_addr_len );
        memset ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
                 0, ll_protocol->ll_addr_len );
        memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
                 net_dest, net_protocol->net_addr_len );

        /* Transmit ARP request */
        if ( ( rc = net_tx ( iobuf, netdev, &arp_protocol,
                             netdev->ll_broadcast, netdev->ll_addr ) ) != 0 ) {
                DBGC ( netdev, "ARP %s %s %s could not transmit request: %s\n",
                       netdev->name, net_protocol->name,
                       net_protocol->ntoa ( net_dest ), strerror ( rc ) );
                return rc;
        }

        return 0;
}
static struct arp_net_protocol* arp_find_protocol ( uint16_t  net_proto) [static, read]

Identify ARP protocol.

Parameters:
net_protoNetwork-layer protocol, in network-endian order
Return values:
arp_net_protocolARP protocol, or NULL

Definition at line 115 of file arp.c.

References ARP_NET_PROTOCOLS, for_each_table_entry, net_protocol::net_proto, arp_net_protocol::net_protocol, and NULL.

Referenced by arp_rx().

                                                                          {
        struct arp_net_protocol *arp_net_protocol;

        for_each_table_entry ( arp_net_protocol, ARP_NET_PROTOCOLS ) {
                if ( arp_net_protocol->net_protocol->net_proto == net_proto )
                        return arp_net_protocol;
        }
        return NULL;
}
static int arp_rx ( struct io_buffer iobuf,
struct net_device netdev,
const void *ll_dest  __unused,
const void *ll_source  __unused,
unsigned int flags  __unused 
) [static]

Process incoming ARP packets.

Parameters:
iobufI/O buffer
netdevNetwork device
ll_sourceLink-layer source address
flagsPacket flags
Return values:
rcReturn status code

Definition at line 134 of file arp.c.

References arphdr::ar_hln, arphdr::ar_hrd, arphdr::ar_op, arphdr::ar_pln, arphdr::ar_pro, arp_find_protocol(), arp_len(), arp_sender_ha(), arp_sender_pa(), arp_target_ha(), arp_target_pa(), ARPOP_REPLY, ARPOP_REQUEST, arp_net_protocol::check, io_buffer::data, DBGC, DBGC2, done, EINVAL, EPROTONOSUPPORT, free_iob(), htons, iob_disown, iob_len(), len, net_device::ll_addr, ll_protocol::ll_addr_len, ll_protocol::ll_proto, net_device::ll_protocol, memcpy(), memswap(), net_protocol::name, ll_protocol::name, net_device::name, neighbour_update(), net_protocol::net_addr_len, arp_net_protocol::net_protocol, net_tx(), net_protocol::ntoa, ll_protocol::ntoa, rc, and strerror().

                                                  {
        struct arphdr *arphdr = iobuf->data;
        struct arp_net_protocol *arp_net_protocol;
        struct net_protocol *net_protocol;
        struct ll_protocol *ll_protocol;
        size_t len = iob_len ( iobuf );
        int rc;

        /* Sanity check */
        if ( ( len < sizeof ( *arphdr ) ) || ( len < arp_len ( arphdr ) ) ) {
                rc = -EINVAL;
                goto done;
        }

        /* Identify network-layer and link-layer protocols */
        arp_net_protocol = arp_find_protocol ( arphdr->ar_pro );
        if ( ! arp_net_protocol ) {
                rc = -EPROTONOSUPPORT;
                goto done;
        }
        net_protocol = arp_net_protocol->net_protocol;
        ll_protocol = netdev->ll_protocol;

        /* Sanity checks */
        if ( ( arphdr->ar_hrd != ll_protocol->ll_proto ) ||
             ( arphdr->ar_hln != ll_protocol->ll_addr_len ) ||
             ( arphdr->ar_pln != net_protocol->net_addr_len ) ) {
                rc = -EINVAL;
                goto done;
        }

        /* Update neighbour cache entry for this sender, if any */
        neighbour_update ( netdev, net_protocol, arp_sender_pa ( arphdr ),
                           arp_sender_ha ( arphdr ) );

        /* If it's not a request, there's nothing more to do */
        if ( arphdr->ar_op != htons ( ARPOP_REQUEST ) ) {
                rc = 0;
                goto done;
        }

        /* See if we own the target protocol address */
        if ( arp_net_protocol->check ( netdev, arp_target_pa ( arphdr ) ) != 0){
                rc = 0;
                goto done;
        }

        /* Change request to a reply */
        DBGC2 ( netdev, "ARP %s %s %s reply => %s %s\n",
                netdev->name, net_protocol->name,
                net_protocol->ntoa ( arp_target_pa ( arphdr ) ),
                ll_protocol->name, ll_protocol->ntoa ( netdev->ll_addr ) );
        arphdr->ar_op = htons ( ARPOP_REPLY );
        memswap ( arp_sender_ha ( arphdr ), arp_target_ha ( arphdr ),
                 arphdr->ar_hln + arphdr->ar_pln );
        memcpy ( arp_sender_ha ( arphdr ), netdev->ll_addr, arphdr->ar_hln );

        /* Send reply */
        if ( ( rc = net_tx ( iob_disown ( iobuf ), netdev, &arp_protocol,
                             arp_target_ha ( arphdr ),
                             netdev->ll_addr ) ) != 0 ) {
                DBGC ( netdev, "ARP %s %s %s could not transmit reply: %s\n",
                       netdev->name, net_protocol->name,
                       net_protocol->ntoa ( arp_target_pa ( arphdr ) ),
                       strerror ( rc ) );
                goto done;
        }

        /* Success */
        rc = 0;

 done:
        free_iob ( iobuf );
        return rc;
}
static const char* arp_ntoa ( const void *net_addr  __unused) [static]

Transcribe ARP address.

Parameters:
net_addrARP address
Return values:
string"<ARP>"

This operation is meaningless for the ARP protocol.

Definition at line 221 of file arp.c.

                                                               {
        return "<ARP>";
}

Variable Documentation

struct net_protocol arp_protocol __net_protocol
Initial value:
 {
        .name = "ARP",
        .net_proto = htons ( ETH_P_ARP ),
        .rx = arp_rx,
        .ntoa = arp_ntoa,
}

ARP network protocol.

AoE protocol.

Definition at line 48 of file arp.c.

Initial value:
 {
        .name = "ARP",
        .tx_request = arp_tx_request,
}

ARP neighbour discovery protocol.

Definition at line 103 of file arp.c.

Referenced by arp_tx().