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. More...
 
static struct arp_net_protocolarp_find_protocol (uint16_t net_proto)
 Identify ARP protocol. More...
 
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. More...
 
static const char * arp_ntoa (const void *net_addr __unused)
 Transcribe ARP address. More...
 

Variables

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

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

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ arp_tx_request()

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.

61  {
63  struct io_buffer *iobuf;
64  struct arphdr *arphdr;
65  int rc;
66 
67  /* Allocate ARP packet */
68  iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *arphdr ) +
69  ( 2 * ( MAX_LL_ADDR_LEN + MAX_NET_ADDR_LEN ) ) );
70  if ( ! iobuf )
71  return -ENOMEM;
72  iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
73 
74  /* Build up ARP request */
75  arphdr = iob_put ( iobuf, sizeof ( *arphdr ) );
81  memcpy ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
84  net_source, net_protocol->net_addr_len );
85  memset ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
88  net_dest, net_protocol->net_addr_len );
89 
90  /* Transmit ARP request */
91  if ( ( rc = net_tx ( iobuf, netdev, &arp_protocol,
92  netdev->ll_broadcast, netdev->ll_addr ) ) != 0 ) {
93  DBGC ( netdev, "ARP %s %s %s could not transmit request: %s\n",
95  net_protocol->ntoa ( net_dest ), strerror ( rc ) );
96  return rc;
97  }
98 
99  return 0;
100 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Protocol name.
Definition: netdevice.h:66
#define iob_put(iobuf, len)
Definition: iobuf.h:120
uint8_t ll_addr_len
Link-layer address length.
Definition: netdevice.h:198
#define DBGC(...)
Definition: compiler.h:505
const uint8_t * ll_broadcast
Link-layer broadcast address.
Definition: netdevice.h:389
uint8_t ar_hln
Link-layer address length.
Definition: if_arp.h:59
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:129
A link-layer protocol.
Definition: netdevice.h:114
uint16_t ar_hrd
Link-layer protocol.
Definition: if_arp.h:52
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define MAX_LL_ADDR_LEN
Maximum length of a link-layer address.
Definition: netdevice.h:36
#define ARPOP_REQUEST
ARP request.
Definition: if_arp.h:32
const char *(* ntoa)(const void *net_addr)
Transcribe network-layer address.
Definition: netdevice.h:94
static struct net_device * netdev
Definition: gdbudp.c:52
#define MAX_LL_HEADER_LEN
Maximum length of a link-layer header.
Definition: netdevice.h:45
An ARP header.
Definition: if_arp.h:47
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
uint16_t ar_op
ARP opcode.
Definition: if_arp.h:63
uint16_t net_proto
Network-layer protocol.
Definition: netdevice.h:99
uint16_t ll_proto
Link-layer protocol.
Definition: netdevice.h:194
A network-layer protocol.
Definition: netdevice.h:64
#define iob_reserve(iobuf, len)
Definition: iobuf.h:67
char name[NETDEV_NAME_LEN]
Name of this network device.
Definition: netdevice.h:362
int net_tx(struct io_buffer *iobuf, struct net_device *netdev, struct net_protocol *net_protocol, const void *ll_dest, const void *ll_source)
Transmit network-layer packet.
Definition: netdevice.c:1073
uint8_t net_addr_len
Network-layer address length.
Definition: netdevice.h:101
uint16_t ar_pro
Network-layer protocol.
Definition: if_arp.h:57
uint8_t ar_pln
Network-layer address length.
Definition: if_arp.h:61
uint8_t ll_addr[MAX_LL_ADDR_LEN]
Link-layer address.
Definition: netdevice.h:387
#define MAX_NET_ADDR_LEN
Maximum length of a network-layer address.
Definition: netdevice.h:48
#define htons(value)
Definition: byteswap.h:135
struct ll_protocol * ll_protocol
Link-layer protocol.
Definition: netdevice.h:372
void * memset(void *dest, int character, size_t len) __nonnull
A persistent I/O buffer.
Definition: iobuf.h:33

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(), netdev, net_protocol::ntoa, rc, and strerror().

Referenced by ipoib_transmit(), and ipv4_gratuitous_arp().

◆ arp_find_protocol()

static struct arp_net_protocol* arp_find_protocol ( uint16_t  net_proto)
static

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.

115  {
117 
119  if ( arp_net_protocol->net_protocol->net_proto == net_proto )
120  return arp_net_protocol;
121  }
122  return NULL;
123 }
A network-layer protocol that relies upon ARP.
Definition: arp.h:17
struct net_protocol * net_protocol
Network-layer protocol.
Definition: arp.h:19
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
uint16_t net_proto
Network-layer protocol.
Definition: netdevice.h:99
#define ARP_NET_PROTOCOLS
ARP protocol table.
Definition: arp.h:31
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

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

Referenced by arp_rx().

◆ arp_rx()

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.

137  {
138  struct arphdr *arphdr = iobuf->data;
140  struct net_protocol *net_protocol;
141  struct ll_protocol *ll_protocol;
142  size_t len = iob_len ( iobuf );
143  int rc;
144 
145  /* Sanity check */
146  if ( ( len < sizeof ( *arphdr ) ) || ( len < arp_len ( arphdr ) ) ) {
147  rc = -EINVAL;
148  goto done;
149  }
150 
151  /* Identify network-layer and link-layer protocols */
153  if ( ! arp_net_protocol ) {
154  rc = -EPROTONOSUPPORT;
155  goto done;
156  }
159 
160  /* Sanity checks */
161  if ( ( arphdr->ar_hrd != ll_protocol->ll_proto ) ||
164  rc = -EINVAL;
165  goto done;
166  }
167 
168  /* Update neighbour cache entry for this sender, if any */
170  arp_sender_ha ( arphdr ) );
171 
172  /* If it's not a request, there's nothing more to do */
173  if ( arphdr->ar_op != htons ( ARPOP_REQUEST ) ) {
174  rc = 0;
175  goto done;
176  }
177 
178  /* See if we own the target protocol address */
179  if ( arp_net_protocol->check ( netdev, arp_target_pa ( arphdr ) ) != 0){
180  rc = 0;
181  goto done;
182  }
183 
184  /* Change request to a reply */
185  DBGC2 ( netdev, "ARP %s %s %s reply => %s %s\n",
189  arphdr->ar_op = htons ( ARPOP_REPLY );
191  arphdr->ar_hln + arphdr->ar_pln );
193 
194  /* Send reply */
195  if ( ( rc = net_tx ( iob_disown ( iobuf ), netdev, &arp_protocol,
196  arp_target_ha ( arphdr ),
197  netdev->ll_addr ) ) != 0 ) {
198  DBGC ( netdev, "ARP %s %s %s could not transmit reply: %s\n",
201  strerror ( rc ) );
202  goto done;
203  }
204 
205  /* Success */
206  rc = 0;
207 
208  done:
209  free_iob ( iobuf );
210  return rc;
211 }
void * memswap(void *first, void *second, size_t len)
Swap memory regions.
Definition: string.c:153
static void * arp_sender_pa(struct arphdr *arphdr)
ARP packet sender protocol address.
Definition: if_arp.h:80
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Protocol name.
Definition: netdevice.h:66
uint8_t ll_addr_len
Link-layer address length.
Definition: netdevice.h:198
static void * arp_target_ha(struct arphdr *arphdr)
ARP packet target hardware address.
Definition: if_arp.h:89
static void * arp_target_pa(struct arphdr *arphdr)
ARP packet target protocol address.
Definition: if_arp.h:98
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:146
#define DBGC(...)
Definition: compiler.h:505
uint8_t ar_hln
Link-layer address length.
Definition: if_arp.h:59
int neighbour_update(struct net_device *netdev, struct net_protocol *net_protocol, const void *net_dest, const void *ll_dest)
Update existing neighbour cache entry.
Definition: neighbour.c:338
A link-layer protocol.
Definition: netdevice.h:114
uint16_t ar_hrd
Link-layer protocol.
Definition: if_arp.h:52
int(* check)(struct net_device *netdev, const void *net_addr)
Check existence of address.
Definition: arp.h:26
A network-layer protocol that relies upon ARP.
Definition: arp.h:17
const char * name
Protocol name.
Definition: netdevice.h:116
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition: iobuf.h:212
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define ARPOP_REQUEST
ARP request.
Definition: if_arp.h:32
const char *(* ntoa)(const void *net_addr)
Transcribe network-layer address.
Definition: netdevice.h:94
struct net_protocol * net_protocol
Network-layer protocol.
Definition: arp.h:19
static struct net_device * netdev
Definition: gdbudp.c:52
An ARP header.
Definition: if_arp.h:47
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
uint16_t ar_op
ARP opcode.
Definition: if_arp.h:63
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:155
#define ARPOP_REPLY
ARP reply.
Definition: if_arp.h:33
uint16_t ll_proto
Link-layer protocol.
Definition: netdevice.h:194
static void * arp_sender_ha(struct arphdr *arphdr)
ARP packet sender hardware address.
Definition: if_arp.h:71
A network-layer protocol.
Definition: netdevice.h:64
char name[NETDEV_NAME_LEN]
Name of this network device.
Definition: netdevice.h:362
int net_tx(struct io_buffer *iobuf, struct net_device *netdev, struct net_protocol *net_protocol, const void *ll_dest, const void *ll_source)
Transmit network-layer packet.
Definition: netdevice.c:1073
uint32_t len
Length.
Definition: ena.h:14
#define DBGC2(...)
Definition: compiler.h:522
#define EPROTONOSUPPORT
Protocol not supported.
Definition: errno.h:629
uint8_t net_addr_len
Network-layer address length.
Definition: netdevice.h:101
void * data
Start of data.
Definition: iobuf.h:48
uint16_t ar_pro
Network-layer protocol.
Definition: if_arp.h:57
uint8_t ar_pln
Network-layer address length.
Definition: if_arp.h:61
const char *(* ntoa)(const void *ll_addr)
Transcribe link-layer address.
Definition: netdevice.h:163
uint8_t ll_addr[MAX_LL_ADDR_LEN]
Link-layer address.
Definition: netdevice.h:387
static struct arp_net_protocol * arp_find_protocol(uint16_t net_proto)
Identify ARP protocol.
Definition: arp.c:115
static size_t arp_len(struct arphdr *arphdr)
ARP packet length.
Definition: if_arp.h:107
#define htons(value)
Definition: byteswap.h:135
struct bofm_section_header done
Definition: bofm_test.c:46
struct ll_protocol * ll_protocol
Link-layer protocol.
Definition: netdevice.h:372

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(), netdev, net_protocol::ntoa, ll_protocol::ntoa, rc, and strerror().

◆ arp_ntoa()

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.

221  {
222  return "<ARP>";
223 }

Variable Documentation

◆ __net_protocol

struct net_protocol arp_protocol __net_protocol
Initial value:
= {
.name = "ARP",
.net_proto = htons ( ETH_P_ARP ),
.rx = arp_rx,
.ntoa = arp_ntoa,
}
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.
Definition: arp.c:134
static const char * arp_ntoa(const void *net_addr __unused)
Transcribe ARP address.
Definition: arp.c:221
#define ETH_P_ARP
Definition: if_ether.h:19
#define htons(value)
Definition: byteswap.h:135

ARP network protocol.

AoE protocol.

Definition at line 48 of file arp.c.

◆ arp_discovery

struct neighbour_discovery arp_discovery
Initial value:
= {
.name = "ARP",
.tx_request = arp_tx_request,
}
int arp_tx_request(struct net_device *netdev, struct net_protocol *net_protocol, const void *net_dest, const void *net_source)
Transmit ARP request.
Definition: arp.c:59

ARP neighbour discovery protocol.

Definition at line 103 of file arp.c.

Referenced by arp_tx().