2006-04-19 03:43:56 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <byteswap.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <gpxe/if_ether.h>
|
|
|
|
#include <gpxe/if_arp.h>
|
2007-05-19 20:39:40 +02:00
|
|
|
#include <gpxe/iobuf.h>
|
2006-04-19 03:43:56 +02:00
|
|
|
#include <gpxe/netdevice.h>
|
|
|
|
#include <gpxe/arp.h>
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-06-18 00:36:27 +02:00
|
|
|
/** Registered ARP protocols */
|
|
|
|
static struct arp_net_protocol arp_net_protocols[0]
|
2007-01-10 05:22:09 +01:00
|
|
|
__table_start ( struct arp_net_protocol, arp_net_protocols );
|
2006-06-18 00:36:27 +02:00
|
|
|
static struct arp_net_protocol arp_net_protocols_end[0]
|
2007-01-10 05:22:09 +01:00
|
|
|
__table_end ( struct arp_net_protocol, arp_net_protocols );
|
2006-06-18 00:36:27 +02:00
|
|
|
|
2006-04-19 03:43:56 +02:00
|
|
|
/** An ARP cache entry */
|
|
|
|
struct arp_entry {
|
|
|
|
/** Network-layer protocol */
|
2006-04-24 17:33:06 +02:00
|
|
|
struct net_protocol *net_protocol;
|
2006-04-19 03:43:56 +02:00
|
|
|
/** Link-layer protocol */
|
2006-04-24 17:33:06 +02:00
|
|
|
struct ll_protocol *ll_protocol;
|
2006-04-19 03:43:56 +02:00
|
|
|
/** Network-layer address */
|
|
|
|
uint8_t net_addr[MAX_NET_ADDR_LEN];
|
|
|
|
/** Link-layer address */
|
2006-04-24 17:33:06 +02:00
|
|
|
uint8_t ll_addr[MAX_LL_ADDR_LEN];
|
2006-04-19 03:43:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Number of entries in the ARP cache
|
|
|
|
*
|
|
|
|
* This is a global cache, covering all network interfaces,
|
|
|
|
* network-layer protocols and link-layer protocols.
|
|
|
|
*/
|
|
|
|
#define NUM_ARP_ENTRIES 4
|
|
|
|
|
|
|
|
/** The ARP cache */
|
|
|
|
static struct arp_entry arp_table[NUM_ARP_ENTRIES];
|
|
|
|
#define arp_table_end &arp_table[NUM_ARP_ENTRIES]
|
|
|
|
|
|
|
|
static unsigned int next_new_arp_entry = 0;
|
|
|
|
|
2006-04-24 17:33:06 +02:00
|
|
|
struct net_protocol arp_protocol;
|
|
|
|
|
2006-04-19 03:43:56 +02:00
|
|
|
/**
|
|
|
|
* Find entry in the ARP cache
|
|
|
|
*
|
2006-04-24 17:33:06 +02:00
|
|
|
* @v ll_protocol Link-layer protocol
|
|
|
|
* @v net_protocol Network-layer protocol
|
2006-04-19 03:43:56 +02:00
|
|
|
* @v net_addr Network-layer address
|
|
|
|
* @ret arp ARP cache entry, or NULL if not found
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static struct arp_entry *
|
2006-04-24 17:33:06 +02:00
|
|
|
arp_find_entry ( struct ll_protocol *ll_protocol,
|
|
|
|
struct net_protocol *net_protocol,
|
|
|
|
const void *net_addr ) {
|
2006-04-19 03:43:56 +02:00
|
|
|
struct arp_entry *arp;
|
|
|
|
|
|
|
|
for ( arp = arp_table ; arp < arp_table_end ; arp++ ) {
|
2006-04-24 17:33:06 +02:00
|
|
|
if ( ( arp->ll_protocol == ll_protocol ) &&
|
|
|
|
( arp->net_protocol == net_protocol ) &&
|
|
|
|
( memcmp ( arp->net_addr, net_addr,
|
|
|
|
net_protocol->net_addr_len ) == 0 ) )
|
2006-04-19 03:43:56 +02:00
|
|
|
return arp;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look up media-specific link-layer address in the ARP cache
|
|
|
|
*
|
2006-04-28 16:13:50 +02:00
|
|
|
* @v netdev Network device
|
2006-06-16 02:19:46 +02:00
|
|
|
* @v net_protocol Network-layer protocol
|
|
|
|
* @v dest_net_addr Destination network-layer address
|
|
|
|
* @v source_net_addr Source network-layer address
|
|
|
|
* @ret dest_ll_addr Destination link layer address
|
2006-04-19 03:43:56 +02:00
|
|
|
* @ret rc Return status code
|
|
|
|
*
|
2006-04-19 13:32:24 +02:00
|
|
|
* This function will use the ARP cache to look up the link-layer
|
2006-06-16 02:19:46 +02:00
|
|
|
* address for the link-layer protocol associated with the network
|
|
|
|
* device and the given network-layer protocol and addresses. If
|
2006-04-24 17:33:06 +02:00
|
|
|
* found, the destination link-layer address will be filled in in @c
|
2006-06-16 02:19:46 +02:00
|
|
|
* dest_ll_addr.
|
2006-04-19 03:43:56 +02:00
|
|
|
*
|
|
|
|
* If no address is found in the ARP cache, an ARP request will be
|
2006-04-28 16:13:50 +02:00
|
|
|
* transmitted on the specified network device and -ENOENT will be
|
|
|
|
* returned.
|
2006-04-19 03:43:56 +02:00
|
|
|
*/
|
2006-06-16 02:19:46 +02:00
|
|
|
int arp_resolve ( struct net_device *netdev, struct net_protocol *net_protocol,
|
|
|
|
const void *dest_net_addr, const void *source_net_addr,
|
|
|
|
void *dest_ll_addr ) {
|
|
|
|
struct ll_protocol *ll_protocol = netdev->ll_protocol;
|
2006-04-19 03:43:56 +02:00
|
|
|
const struct arp_entry *arp;
|
2007-05-19 20:39:40 +02:00
|
|
|
struct io_buffer *iobuf;
|
2006-04-19 03:43:56 +02:00
|
|
|
struct arphdr *arphdr;
|
2006-04-24 17:33:06 +02:00
|
|
|
int rc;
|
2006-04-19 03:43:56 +02:00
|
|
|
|
|
|
|
/* Look for existing entry in ARP table */
|
2006-06-16 02:19:46 +02:00
|
|
|
arp = arp_find_entry ( ll_protocol, net_protocol, dest_net_addr );
|
2006-04-19 03:43:56 +02:00
|
|
|
if ( arp ) {
|
2006-04-28 16:13:50 +02:00
|
|
|
DBG ( "ARP cache hit: %s %s => %s %s\n",
|
|
|
|
net_protocol->name, net_protocol->ntoa ( arp->net_addr ),
|
|
|
|
ll_protocol->name, ll_protocol->ntoa ( arp->ll_addr ) );
|
2006-06-16 02:19:46 +02:00
|
|
|
memcpy ( dest_ll_addr, arp->ll_addr, ll_protocol->ll_addr_len);
|
2006-04-19 03:43:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2006-04-28 16:13:50 +02:00
|
|
|
DBG ( "ARP cache miss: %s %s\n", net_protocol->name,
|
2006-06-16 02:19:46 +02:00
|
|
|
net_protocol->ntoa ( dest_net_addr ) );
|
2006-04-19 03:43:56 +02:00
|
|
|
|
2006-04-24 17:33:06 +02:00
|
|
|
/* Allocate ARP packet */
|
2007-05-19 20:39:40 +02:00
|
|
|
iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *arphdr ) +
|
2006-04-24 17:33:06 +02:00
|
|
|
2 * ( MAX_LL_ADDR_LEN + MAX_NET_ADDR_LEN ) );
|
2007-05-19 20:39:40 +02:00
|
|
|
if ( ! iobuf )
|
2006-04-24 17:33:06 +02:00
|
|
|
return -ENOMEM;
|
2007-05-19 20:39:40 +02:00
|
|
|
iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
|
2006-04-19 03:43:56 +02:00
|
|
|
|
|
|
|
/* Build up ARP request */
|
2007-05-19 20:39:40 +02:00
|
|
|
arphdr = iob_put ( iobuf, sizeof ( *arphdr ) );
|
2006-04-24 17:33:06 +02:00
|
|
|
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;
|
2006-04-19 03:43:56 +02:00
|
|
|
arphdr->ar_op = htons ( ARPOP_REQUEST );
|
2007-05-19 20:39:40 +02:00
|
|
|
memcpy ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
|
2006-06-16 02:19:46 +02:00
|
|
|
netdev->ll_addr, ll_protocol->ll_addr_len );
|
2007-05-19 20:39:40 +02:00
|
|
|
memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
|
2006-06-16 02:19:46 +02:00
|
|
|
source_net_addr, net_protocol->net_addr_len );
|
2007-05-19 20:39:40 +02:00
|
|
|
memset ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
|
2006-04-24 17:33:06 +02:00
|
|
|
0, ll_protocol->ll_addr_len );
|
2007-05-19 20:39:40 +02:00
|
|
|
memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
|
2006-06-16 02:19:46 +02:00
|
|
|
dest_net_addr, net_protocol->net_addr_len );
|
2006-04-24 17:33:06 +02:00
|
|
|
|
|
|
|
/* Transmit ARP request */
|
2007-05-19 20:39:40 +02:00
|
|
|
if ( ( rc = net_tx ( iobuf, netdev, &arp_protocol,
|
2006-06-18 00:36:27 +02:00
|
|
|
ll_protocol->ll_broadcast ) ) != 0 )
|
2006-04-24 17:33:06 +02:00
|
|
|
return rc;
|
2006-04-19 03:43:56 +02:00
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2006-06-18 00:36:27 +02:00
|
|
|
/**
|
|
|
|
* Identify ARP protocol
|
|
|
|
*
|
|
|
|
* @v net_proto Network-layer protocol, in network-endian order
|
|
|
|
* @ret arp_net_protocol ARP protocol, or NULL
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static struct arp_net_protocol * arp_find_protocol ( uint16_t net_proto ) {
|
|
|
|
struct arp_net_protocol *arp_net_protocol;
|
|
|
|
|
|
|
|
for ( arp_net_protocol = arp_net_protocols ;
|
|
|
|
arp_net_protocol < arp_net_protocols_end ; arp_net_protocol++ ) {
|
|
|
|
if ( arp_net_protocol->net_protocol->net_proto == net_proto ) {
|
|
|
|
return arp_net_protocol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-04-19 03:43:56 +02:00
|
|
|
/**
|
|
|
|
* Process incoming ARP packets
|
|
|
|
*
|
2007-05-19 20:39:40 +02:00
|
|
|
* @v iobuf I/O buffer
|
2006-06-18 00:36:27 +02:00
|
|
|
* @v netdev Network device
|
|
|
|
* @v ll_source Link-layer source address
|
2006-04-19 03:43:56 +02:00
|
|
|
* @ret rc Return status code
|
|
|
|
*
|
|
|
|
* This handles ARP requests and responses as detailed in RFC826. The
|
|
|
|
* method detailed within the RFC is pretty optimised, handling
|
|
|
|
* requests and responses with basically a single code path and
|
|
|
|
* avoiding the need for extraneous ARP requests; read the RFC for
|
|
|
|
* details.
|
|
|
|
*/
|
2007-05-19 20:39:40 +02:00
|
|
|
static int arp_rx ( struct io_buffer *iobuf, struct net_device *netdev,
|
2006-06-18 00:36:27 +02:00
|
|
|
const void *ll_source __unused ) {
|
2007-05-19 20:39:40 +02:00
|
|
|
struct arphdr *arphdr = iobuf->data;
|
2006-06-18 00:36:27 +02:00
|
|
|
struct arp_net_protocol *arp_net_protocol;
|
2006-04-24 17:33:06 +02:00
|
|
|
struct net_protocol *net_protocol;
|
2006-06-18 00:36:27 +02:00
|
|
|
struct ll_protocol *ll_protocol;
|
2006-04-19 03:43:56 +02:00
|
|
|
struct arp_entry *arp;
|
|
|
|
int merge = 0;
|
|
|
|
|
2006-06-18 00:36:27 +02:00
|
|
|
/* Identify network-layer and link-layer protocols */
|
|
|
|
arp_net_protocol = arp_find_protocol ( arphdr->ar_pro );
|
|
|
|
if ( ! arp_net_protocol )
|
2006-04-24 17:33:06 +02:00
|
|
|
goto done;
|
2006-06-18 00:36:27 +02:00
|
|
|
net_protocol = arp_net_protocol->net_protocol;
|
|
|
|
ll_protocol = netdev->ll_protocol;
|
2006-04-19 03:43:56 +02:00
|
|
|
|
2006-04-24 17:33:06 +02:00
|
|
|
/* 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 ) )
|
|
|
|
goto done;
|
2006-04-19 03:43:56 +02:00
|
|
|
|
|
|
|
/* See if we have an entry for this sender, and update it if so */
|
2006-04-24 17:33:06 +02:00
|
|
|
arp = arp_find_entry ( ll_protocol, net_protocol,
|
|
|
|
arp_sender_pa ( arphdr ) );
|
2006-04-19 03:43:56 +02:00
|
|
|
if ( arp ) {
|
|
|
|
memcpy ( arp->ll_addr, arp_sender_ha ( arphdr ),
|
|
|
|
arphdr->ar_hln );
|
|
|
|
merge = 1;
|
2006-04-28 16:13:50 +02:00
|
|
|
DBG ( "ARP cache update: %s %s => %s %s\n",
|
|
|
|
net_protocol->name, net_protocol->ntoa ( arp->net_addr ),
|
|
|
|
ll_protocol->name, ll_protocol->ntoa ( arp->ll_addr ) );
|
2006-04-19 03:43:56 +02:00
|
|
|
}
|
|
|
|
|
2006-04-24 17:33:06 +02:00
|
|
|
/* See if we own the target protocol address */
|
2006-06-18 00:36:27 +02:00
|
|
|
if ( arp_net_protocol->check ( netdev, arp_target_pa ( arphdr ) ) != 0)
|
2006-04-24 17:33:06 +02:00
|
|
|
goto done;
|
|
|
|
|
2006-04-19 03:43:56 +02:00
|
|
|
/* Create new ARP table entry if necessary */
|
|
|
|
if ( ! merge ) {
|
|
|
|
arp = &arp_table[next_new_arp_entry++ % NUM_ARP_ENTRIES];
|
2006-04-24 17:33:06 +02:00
|
|
|
arp->ll_protocol = ll_protocol;
|
|
|
|
arp->net_protocol = net_protocol;
|
2006-04-19 03:43:56 +02:00
|
|
|
memcpy ( arp->ll_addr, arp_sender_ha ( arphdr ),
|
|
|
|
arphdr->ar_hln );
|
|
|
|
memcpy ( arp->net_addr, arp_sender_pa ( arphdr ),
|
2006-04-24 17:33:06 +02:00
|
|
|
arphdr->ar_pln);
|
2006-04-28 16:13:50 +02:00
|
|
|
DBG ( "ARP cache add: %s %s => %s %s\n",
|
|
|
|
net_protocol->name, net_protocol->ntoa ( arp->net_addr ),
|
|
|
|
ll_protocol->name, ll_protocol->ntoa ( arp->ll_addr ) );
|
2006-04-19 03:43:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If it's not a request, there's nothing more to do */
|
|
|
|
if ( arphdr->ar_op != htons ( ARPOP_REQUEST ) )
|
2006-04-24 17:33:06 +02:00
|
|
|
goto done;
|
2006-04-19 03:43:56 +02:00
|
|
|
|
2006-04-29 19:17:43 +02:00
|
|
|
/* Change request to a reply */
|
2006-04-28 16:13:50 +02:00
|
|
|
DBG ( "ARP reply: %s %s => %s %s\n", net_protocol->name,
|
|
|
|
net_protocol->ntoa ( arp_target_pa ( arphdr ) ),
|
|
|
|
ll_protocol->name, ll_protocol->ntoa ( netdev->ll_addr ) );
|
2006-04-19 03:43:56 +02:00
|
|
|
arphdr->ar_op = htons ( ARPOP_REPLY );
|
2006-04-24 17:33:06 +02:00
|
|
|
memswap ( arp_sender_ha ( arphdr ), arp_target_ha ( arphdr ),
|
2006-04-19 03:43:56 +02:00
|
|
|
arphdr->ar_hln + arphdr->ar_pln );
|
2006-06-16 02:19:46 +02:00
|
|
|
memcpy ( arp_sender_ha ( arphdr ), netdev->ll_addr, arphdr->ar_hln );
|
2006-04-29 19:17:43 +02:00
|
|
|
|
|
|
|
/* Send reply */
|
2009-02-01 19:02:28 +01:00
|
|
|
net_tx ( iob_disown ( iobuf ), netdev, &arp_protocol,
|
|
|
|
arp_target_ha ( arphdr ) );
|
2006-04-19 03:43:56 +02:00
|
|
|
|
2006-04-24 17:33:06 +02:00
|
|
|
done:
|
2007-05-19 20:39:40 +02:00
|
|
|
free_iob ( iobuf );
|
2006-04-19 03:43:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-04-28 16:13:50 +02:00
|
|
|
/**
|
|
|
|
* Transcribe ARP address
|
|
|
|
*
|
|
|
|
* @v net_addr ARP address
|
|
|
|
* @ret string "<ARP>"
|
|
|
|
*
|
|
|
|
* This operation is meaningless for the ARP protocol.
|
|
|
|
*/
|
2007-07-02 21:05:58 +02:00
|
|
|
static const char * arp_ntoa ( const void *net_addr __unused ) {
|
2006-04-28 16:13:50 +02:00
|
|
|
return "<ARP>";
|
|
|
|
}
|
|
|
|
|
2006-04-24 17:33:06 +02:00
|
|
|
/** ARP protocol */
|
2006-08-09 06:42:14 +02:00
|
|
|
struct net_protocol arp_protocol __net_protocol = {
|
2006-04-28 16:13:50 +02:00
|
|
|
.name = "ARP",
|
2006-04-25 14:56:18 +02:00
|
|
|
.net_proto = htons ( ETH_P_ARP ),
|
2006-06-18 00:36:27 +02:00
|
|
|
.rx = arp_rx,
|
2006-04-28 16:13:50 +02:00
|
|
|
.ntoa = arp_ntoa,
|
2006-04-24 17:33:06 +02:00
|
|
|
};
|