david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

[Settings] Migrate DHCP and NVO code to the new settings API (untested)

This commit is contained in:
Michael Brown 2008-03-21 22:15:31 +00:00
parent bb32b8999c
commit 8afb36c3bc
16 changed files with 966 additions and 936 deletions

View File

@ -9,6 +9,7 @@
#include <gpxe/init.h> #include <gpxe/init.h>
#include <gpxe/netdevice.h> #include <gpxe/netdevice.h>
#include <gpxe/dhcp.h> #include <gpxe/dhcp.h>
#include <gpxe/dhcppkt.h>
#include <gpxe/image.h> #include <gpxe/image.h>
#include <gpxe/features.h> #include <gpxe/features.h>
@ -400,10 +401,9 @@ static int nbi_prepare_dhcp ( struct image *image ) {
return -ENODEV; return -ENODEV;
} }
if ( ( rc = create_dhcp_response ( boot_netdev, DHCPACK, NULL, if ( ( rc = create_dhcp_response ( &dhcppkt, boot_netdev, DHCPACK,
basemem_packet, NULL, basemem_packet,
sizeof ( basemem_packet ), sizeof ( basemem_packet ) ) ) != 0){
&dhcppkt ) ) != 0 ) {
DBGC ( image, "NBI %p failed to build DHCP packet\n", image ); DBGC ( image, "NBI %p failed to build DHCP packet\n", image );
return rc; return rc;
} }

View File

@ -17,6 +17,7 @@
*/ */
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <gpxe/dhcp.h> #include <gpxe/dhcp.h>
@ -29,9 +30,6 @@
* *
*/ */
/* FIXME: "Temporary hack" */
struct nvo_block *ugly_nvo_hack = NULL;
/** /**
* Calculate checksum over non-volatile stored options * Calculate checksum over non-volatile stored options
* *
@ -39,7 +37,7 @@ struct nvo_block *ugly_nvo_hack = NULL;
* @ret sum Checksum * @ret sum Checksum
*/ */
static unsigned int nvo_checksum ( struct nvo_block *nvo ) { static unsigned int nvo_checksum ( struct nvo_block *nvo ) {
uint8_t *data = nvo->options->data; uint8_t *data = nvo->data;
uint8_t sum = 0; uint8_t sum = 0;
unsigned int i; unsigned int i;
@ -56,22 +54,22 @@ static unsigned int nvo_checksum ( struct nvo_block *nvo ) {
* @ret rc Return status code * @ret rc Return status code
*/ */
static int nvo_load ( struct nvo_block *nvo ) { static int nvo_load ( struct nvo_block *nvo ) {
void *data = nvo->options->data; void *data = nvo->data;
struct nvo_fragment *fragment; struct nvo_fragment *frag;
int rc; int rc;
/* Read data a fragment at a time */ /* Read data a fragment at a time */
for ( fragment = nvo->fragments ; fragment->len ; fragment++ ) { for ( frag = nvo->fragments ; frag->len ; frag++ ) {
if ( ( rc = nvs_read ( nvo->nvs, fragment->address, if ( ( rc = nvs_read ( nvo->nvs, frag->address, data,
data, fragment->len ) ) != 0 ) { frag->len ) ) != 0 ) {
DBG ( "NVO %p could not read %zd bytes at %#04x\n", DBGC ( nvo, "NVO %p could not read %zd bytes at "
nvo, fragment->len, fragment->address ); "%#04x\n", nvo, frag->len, frag->address );
return rc; return rc;
} }
data += fragment->len; data += frag->len;
} }
DBG ( "NVO %p loaded from non-volatile storage\n", nvo ); DBGC ( nvo, "NVO %p loaded from non-volatile storage\n", nvo );
return 0; return 0;
} }
@ -81,27 +79,27 @@ static int nvo_load ( struct nvo_block *nvo ) {
* @v nvo Non-volatile options block * @v nvo Non-volatile options block
* @ret rc Return status code * @ret rc Return status code
*/ */
int nvo_save ( struct nvo_block *nvo ) { static int nvo_save ( struct nvo_block *nvo ) {
void *data = nvo->options->data; void *data = nvo->data;
uint8_t *checksum = ( data + nvo->total_len - 1 ); uint8_t *checksum = data;
struct nvo_fragment *fragment; struct nvo_fragment *frag;
int rc; int rc;
/* Recalculate checksum */ /* Recalculate checksum */
*checksum -= nvo_checksum ( nvo ); *checksum -= nvo_checksum ( nvo );
/* Write data a fragment at a time */ /* Write data a fragment at a time */
for ( fragment = nvo->fragments ; fragment->len ; fragment++ ) { for ( frag = nvo->fragments ; frag->len ; frag++ ) {
if ( ( rc = nvs_write ( nvo->nvs, fragment->address, if ( ( rc = nvs_write ( nvo->nvs, frag->address, data,
data, fragment->len ) ) != 0 ) { frag->len ) ) != 0 ) {
DBG ( "NVO %p could not write %zd bytes at %#04x\n", DBGC ( nvo, "NVO %p could not write %zd bytes at "
nvo, fragment->len, fragment->address ); "%#04x\n", nvo, frag->len, frag->address );
return rc; return rc;
} }
data += fragment->len; data += frag->len;
} }
DBG ( "NVO %p saved to non-volatile storage\n", nvo ); DBGC ( nvo, "NVO %p saved to non-volatile storage\n", nvo );
return 0; return 0;
} }
@ -114,88 +112,138 @@ int nvo_save ( struct nvo_block *nvo ) {
* options block. If the data is not valid, it is replaced with an * options block. If the data is not valid, it is replaced with an
* empty options block. * empty options block.
*/ */
static void nvo_init_dhcp ( struct nvo_block *nvo ) { static void nvo_init_dhcpopts ( struct nvo_block *nvo ) {
struct dhcp_option_block *options = nvo->options; uint8_t *options_data;
struct dhcp_option *option; size_t options_len;
/* Steal one byte for the checksum */ /* Steal one byte for the checksum */
options->max_len = ( nvo->total_len - 1 ); options_data = ( nvo->data + 1 );
options_len = ( nvo->total_len - 1 );
/* Verify checksum over whole block */ /* If checksum fails, or options data starts with a zero,
if ( nvo_checksum ( nvo ) != 0 ) { * assume the whole block is invalid. This should capture the
DBG ( "NVO %p has bad checksum %02x; assuming empty\n", * case of random initial contents.
nvo, nvo_checksum ( nvo ) ); */
goto empty; if ( ( nvo_checksum ( nvo ) != 0 ) || ( options_data[0] == 0 ) ) {
DBGC ( nvo, "NVO %p has checksum %02x and initial byte %02x; "
"assuming empty\n", nvo, nvo_checksum ( nvo ),
options_data[0] );
memset ( nvo->data, 0, nvo->total_len );
} }
/* Check that we don't just have a block full of zeroes */ dhcpopt_init ( &nvo->dhcpopts, options_data, options_len );
option = options->data; }
if ( option->tag == DHCP_PAD ) {
DBG ( "NVO %p has bad start; assuming empty\n", nvo ); /**
goto empty; * Store value of NVO setting
} *
* @v settings Settings block
/* Search for the DHCP_END tag */ * @v tag Setting tag number
options->len = options->max_len; * @v data Setting data, or NULL to clear setting
option = find_dhcp_option ( options, DHCP_END ); * @v len Length of setting data
if ( ! option ) { * @ret rc Return status code
DBG ( "NVO %p has no end tag; assuming empty\n", nvo ); */
goto empty; static int nvo_store ( struct settings *settings, unsigned int tag,
const void *data, size_t len ) {
struct nvo_block *nvo =
container_of ( settings, struct nvo_block, settings );
int rc;
/* Update stored options */
if ( ( rc = dhcpopt_store ( &nvo->dhcpopts, tag, data, len ) ) != 0 ) {
DBGC ( nvo, "NVO %p could not store %zd bytes: %s\n",
nvo, len, strerror ( rc ) );
return rc;
} }
/* Set correct length of DHCP options */ /* Save updated options to NVS */
options->len = ( ( void * ) option - options->data + 1 ); if ( ( rc = nvo_save ( nvo ) ) != 0 )
DBG ( "NVO %p contains %zd bytes of options (maximum %zd)\n", return rc;
nvo, options->len, options->max_len );
return;
empty: return 0;
/* No options found; initialise an empty options block */ }
option = options->data;
option->tag = DHCP_END; /**
options->len = 1; * Fetch value of NVO setting
return; *
* @v settings Settings block
* @v tag Setting tag number
* @v data Buffer to fill with setting data
* @v len Length of buffer
* @ret len Length of setting data, or negative error
*
* The actual length of the setting will be returned even if
* the buffer was too small.
*/
static int nvo_fetch ( struct settings *settings, unsigned int tag,
void *data, size_t len ) {
struct nvo_block *nvo =
container_of ( settings, struct nvo_block, settings );
return dhcpopt_fetch ( &nvo->dhcpopts, tag, data, len );
}
/** NVO settings operations */
static struct settings_operations nvo_settings_operations = {
.store = nvo_store,
.fetch = nvo_fetch,
};
/**
* Initialise non-volatile stored options
*
* @v nvo Non-volatile options block
* @v nvs Underlying non-volatile storage device
* @v fragments List of option-containing fragments
* @v refcnt Containing object reference counter, or NULL
*/
void nvo_init ( struct nvo_block *nvo, struct nvs_device *nvs,
struct nvo_fragment *fragments, struct refcnt *refcnt ) {
nvo->nvs = nvs;
nvo->fragments = fragments;
settings_init ( &nvo->settings, &nvo_settings_operations, refcnt,
"nvo" );
} }
/** /**
* Register non-volatile stored options * Register non-volatile stored options
* *
* @v nvo Non-volatile options block * @v nvo Non-volatile options block
* @v parent Parent settings block, or NULL
* @ret rc Return status code * @ret rc Return status code
*/ */
int nvo_register ( struct nvo_block *nvo ) { int register_nvo ( struct nvo_block *nvo, struct settings *parent ) {
struct nvo_fragment *fragment = nvo->fragments; struct nvo_fragment *fragment = nvo->fragments;
int rc; int rc;
/* Calculate total length of all fragments */ /* Calculate total length of all fragments */
nvo->total_len = 0; for ( fragment = nvo->fragments ; fragment->len ; fragment++ )
for ( fragment = nvo->fragments ; fragment->len ; fragment++ ) {
nvo->total_len += fragment->len; nvo->total_len += fragment->len;
}
/* Allocate memory for options and read in from NVS */ /* Allocate memory for options and read in from NVS */
nvo->options = alloc_dhcp_options ( nvo->total_len ); nvo->data = malloc ( nvo->total_len );
if ( ! nvo->options ) { if ( ! nvo->data ) {
DBG ( "NVO %p could not allocate %zd bytes\n", DBGC ( nvo, "NVO %p could not allocate %zd bytes\n",
nvo, nvo->total_len ); nvo, nvo->total_len );
rc = -ENOMEM; rc = -ENOMEM;
goto err; goto err_malloc;
} }
if ( ( rc = nvo_load ( nvo ) ) != 0 ) if ( ( rc = nvo_load ( nvo ) ) != 0 )
goto err; goto err_load;
/* Verify and register options */ /* Verify and register options */
nvo_init_dhcp ( nvo ); nvo_init_dhcpopts ( nvo );
register_dhcp_options ( nvo->options ); if ( ( rc = register_settings ( &nvo->settings, parent ) ) != 0 )
goto err_register;
ugly_nvo_hack = nvo; DBGC ( nvo, "NVO %p registered\n", nvo );
DBG ( "NVO %p registered\n", nvo );
return 0; return 0;
err: err_register:
dhcpopt_put ( nvo->options ); err_load:
nvo->options = NULL; free ( nvo->data );
nvo->data = NULL;
err_malloc:
return rc; return rc;
} }
@ -204,15 +252,9 @@ int nvo_register ( struct nvo_block *nvo ) {
* *
* @v nvo Non-volatile options block * @v nvo Non-volatile options block
*/ */
void nvo_unregister ( struct nvo_block *nvo ) { void unregister_nvo ( struct nvo_block *nvo ) {
unregister_settings ( &nvo->settings );
if ( nvo->options ) { free ( nvo->data );
unregister_dhcp_options ( nvo->options ); nvo->data = NULL;
dhcpopt_put ( nvo->options ); DBGC ( nvo, "NVO %p unregistered\n", nvo );
nvo->options = NULL;
}
DBG ( "NVO %p unregistered\n", nvo );
ugly_nvo_hack = NULL;
} }

View File

@ -213,6 +213,7 @@ void unregister_settings ( struct settings *settings ) {
/* Remove from list of settings */ /* Remove from list of settings */
ref_put ( settings->refcnt ); ref_put ( settings->refcnt );
ref_put ( settings->parent->refcnt ); ref_put ( settings->parent->refcnt );
settings->parent = NULL;
list_del ( &settings->siblings ); list_del ( &settings->siblings );
DBGC ( settings, "Settings %p unregistered\n", settings ); DBGC ( settings, "Settings %p unregistered\n", settings );
@ -280,8 +281,13 @@ struct settings * find_settings ( const char *name ) {
*/ */
int store_setting ( struct settings *settings, unsigned int tag, int store_setting ( struct settings *settings, unsigned int tag,
const void *data, size_t len ) { const void *data, size_t len ) {
struct settings *parent;
int rc; int rc;
/* Sanity check */
if ( ! settings )
return -ENODEV;
/* Store setting */ /* Store setting */
if ( ( rc = settings->op->store ( settings, tag, data, len ) ) != 0 ) if ( ( rc = settings->op->store ( settings, tag, data, len ) ) != 0 )
return rc; return rc;
@ -290,9 +296,16 @@ int store_setting ( struct settings *settings, unsigned int tag,
if ( tag == DHCP_EB_PRIORITY ) if ( tag == DHCP_EB_PRIORITY )
reprioritise_settings ( settings ); reprioritise_settings ( settings );
/* Apply potentially-updated setting */ /* If these settings are registered, apply potentially-updated
if ( ( rc = apply_settings() ) != 0 ) * settings
return rc; */
for ( parent = settings->parent ; parent ; parent = parent->parent ) {
if ( parent == &settings_root ) {
if ( ( rc = apply_settings() ) != 0 )
return rc;
break;
}
}
return 0; return 0;
} }
@ -468,6 +481,88 @@ unsigned long fetch_uintz_setting ( struct settings *settings,
return value; return value;
} }
/**
* Copy setting
*
* @v dest Destination settings block
* @v dest_tag Destination setting tag number
* @v source Source settings block
* @v source_tag Source setting tag number
* @ret rc Return status code
*/
int copy_setting ( struct settings *dest, unsigned int dest_tag,
struct settings *source, unsigned int source_tag ) {
int len;
int check_len;
int rc;
len = fetch_setting_len ( source, source_tag );
if ( len < 0 )
return len;
{
char buf[len];
check_len = fetch_setting ( source, source_tag, buf,
sizeof ( buf ) );
assert ( check_len == len );
if ( ( rc = store_setting ( dest, dest_tag, buf,
sizeof ( buf ) ) ) != 0 )
return rc;
}
return 0;
}
/**
* Copy settings
*
* @v dest Destination settings block
* @v source Source settings block
* @v encapsulator Encapsulating setting tag number, or zero
* @ret rc Return status code
*/
static int copy_encap_settings ( struct settings *dest,
struct settings *source,
unsigned int encapsulator ) {
unsigned int subtag;
unsigned int tag;
int rc;
for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
switch ( tag ) {
case DHCP_EB_ENCAP:
case DHCP_VENDOR_ENCAP:
/* Process encapsulated options field */
if ( ( rc = copy_encap_settings ( dest, source,
tag ) ) != 0 )
return rc;
break;
default:
/* Copy option to reassembled packet */
if ( ( rc = copy_setting ( dest, tag, source,
tag ) ) != 0 )
return rc;
break;
}
}
return 0;
}
/**
* Copy settings
*
* @v dest Destination settings block
* @v source Source settings block
* @ret rc Return status code
*/
int copy_settings ( struct settings *dest, struct settings *source ) {
return copy_encap_settings ( dest, source, 0 );
}
/****************************************************************************** /******************************************************************************
* *
* Named and typed setting routines * Named and typed setting routines

View File

@ -3008,9 +3008,9 @@ static int falcon_init_nic ( struct efab_nic *efab ) {
/* Register non-volatile storage */ /* Register non-volatile storage */
if ( efab->has_eeprom ) { if ( efab->has_eeprom ) {
efab->nvo.nvs = &efab->falcon_eeprom.nvs; nvo_init ( &efab->nvo, &efab->falcon_eeprom.nvs,
efab->nvo.fragments = falcon_eeprom_fragments; falcon_eeprom_fragments, NULL /* hack */ );
if ( nvo_register ( &efab->nvo ) != 0 ) if ( register_nvo ( &efab->nvo, NULL /* hack */ ) != 0 )
return 0; return 0;
} }

View File

@ -261,9 +261,10 @@ static struct nvo_fragment rtl_nvo_fragments[] = {
/** /**
* Set up for EEPROM access * Set up for EEPROM access
* *
* @v rtl RTL8139 NIC * @v netdev Net device
*/ */
static void rtl_init_eeprom ( struct rtl8139_nic *rtl ) { static void rtl_init_eeprom ( struct net_device *netdev ) {
struct rtl8139_nic *rtl = netdev->priv;
int ee9356; int ee9356;
int vpd; int vpd;
@ -288,19 +289,20 @@ static void rtl_init_eeprom ( struct rtl8139_nic *rtl ) {
if ( vpd ) { if ( vpd ) {
DBG ( "EEPROM in use for VPD; cannot use for options\n" ); DBG ( "EEPROM in use for VPD; cannot use for options\n" );
} else { } else {
rtl->nvo.nvs = &rtl->eeprom.nvs; nvo_init ( &rtl->nvo, &rtl->eeprom.nvs, rtl_nvo_fragments,
rtl->nvo.fragments = rtl_nvo_fragments; &netdev->refcnt );
} }
} }
/** /**
* Reset NIC * Reset NIC
* *
* @v rtl RTL8139 NIC * @v netdev Net device
* *
* Issues a hardware reset and waits for the reset to complete. * Issues a hardware reset and waits for the reset to complete.
*/ */
static void rtl_reset ( struct rtl8139_nic *rtl ) { static void rtl_reset ( struct net_device *netdev ) {
struct rtl8139_nic *rtl = netdev->priv;
/* Reset chip */ /* Reset chip */
outb ( CmdReset, rtl->ioaddr + ChipCmd ); outb ( CmdReset, rtl->ioaddr + ChipCmd );
@ -352,7 +354,7 @@ static void rtl_close ( struct net_device *netdev ) {
struct rtl8139_nic *rtl = netdev->priv; struct rtl8139_nic *rtl = netdev->priv;
/* Reset the hardware to disable everything in one go */ /* Reset the hardware to disable everything in one go */
rtl_reset ( rtl ); rtl_reset ( netdev );
/* Free RX ring */ /* Free RX ring */
free ( rtl->rx.ring ); free ( rtl->rx.ring );
@ -366,7 +368,8 @@ static void rtl_close ( struct net_device *netdev ) {
* @v iobuf I/O buffer * @v iobuf I/O buffer
* @ret rc Return status code * @ret rc Return status code
*/ */
static int rtl_transmit ( struct net_device *netdev, struct io_buffer *iobuf ) { static int rtl_transmit ( struct net_device *netdev,
struct io_buffer *iobuf ) {
struct rtl8139_nic *rtl = netdev->priv; struct rtl8139_nic *rtl = netdev->priv;
/* Check for space in TX ring */ /* Check for space in TX ring */
@ -512,8 +515,8 @@ static int rtl_probe ( struct pci_device *pci,
adjust_pci_device ( pci ); adjust_pci_device ( pci );
/* Reset the NIC, set up EEPROM access and read MAC address */ /* Reset the NIC, set up EEPROM access and read MAC address */
rtl_reset ( rtl ); rtl_reset ( netdev );
rtl_init_eeprom ( rtl ); rtl_init_eeprom ( netdev );
nvs_read ( &rtl->eeprom.nvs, EE_MAC, netdev->ll_addr, ETH_ALEN ); nvs_read ( &rtl->eeprom.nvs, EE_MAC, netdev->ll_addr, ETH_ALEN );
/* Register network device */ /* Register network device */
@ -522,7 +525,8 @@ static int rtl_probe ( struct pci_device *pci,
/* Register non-volatile storage */ /* Register non-volatile storage */
if ( rtl->nvo.nvs ) { if ( rtl->nvo.nvs ) {
if ( ( rc = nvo_register ( &rtl->nvo ) ) != 0 ) if ( ( rc = register_nvo ( &rtl->nvo,
netdev_settings ( netdev ) ) ) != 0)
goto err_register_nvo; goto err_register_nvo;
} }
@ -531,7 +535,7 @@ static int rtl_probe ( struct pci_device *pci,
err_register_nvo: err_register_nvo:
unregister_netdev ( netdev ); unregister_netdev ( netdev );
err_register_netdev: err_register_netdev:
rtl_reset ( rtl ); rtl_reset ( netdev );
netdev_nullify ( netdev ); netdev_nullify ( netdev );
netdev_put ( netdev ); netdev_put ( netdev );
return rc; return rc;
@ -547,9 +551,9 @@ static void rtl_remove ( struct pci_device *pci ) {
struct rtl8139_nic *rtl = netdev->priv; struct rtl8139_nic *rtl = netdev->priv;
if ( rtl->nvo.nvs ) if ( rtl->nvo.nvs )
nvo_unregister ( &rtl->nvo ); unregister_nvo ( &rtl->nvo );
unregister_netdev ( netdev ); unregister_netdev ( netdev );
rtl_reset ( rtl ); rtl_reset ( netdev );
netdev_nullify ( netdev ); netdev_nullify ( netdev );
netdev_put ( netdev ); netdev_put ( netdev );
} }

View File

@ -8,13 +8,15 @@
*/ */
#include <stdint.h> #include <stdint.h>
#include <gpxe/list.h>
#include <gpxe/in.h> #include <gpxe/in.h>
#include <gpxe/list.h>
#include <gpxe/refcnt.h> #include <gpxe/refcnt.h>
#include <gpxe/tables.h> #include <gpxe/tables.h>
struct net_device; struct net_device;
struct job_interface; struct job_interface;
struct dhcp_packet;
struct settings;
/** BOOTP/DHCP server port */ /** BOOTP/DHCP server port */
#define BOOTPS_PORT 67 #define BOOTPS_PORT 67
@ -294,7 +296,7 @@ struct job_interface;
/** Construct a word-valued DHCP option */ /** Construct a word-valued DHCP option */
#define DHCP_WORD( value ) DHCP_OPTION ( ( ( (value) >> 8 ) & 0xff ), \ #define DHCP_WORD( value ) DHCP_OPTION ( ( ( (value) >> 8 ) & 0xff ), \
( ( (value) >> 0 ) & 0xff ) ) ( ( (value) >> 0 ) & 0xff ) )
/** Construct a dword-valued DHCP option */ /** Construct a dword-valued DHCP option */
#define DHCP_DWORD( value ) DHCP_OPTION ( ( ( (value) >> 24 ) & 0xff ), \ #define DHCP_DWORD( value ) DHCP_OPTION ( ( ( (value) >> 24 ) & 0xff ), \
@ -327,21 +329,8 @@ struct dhcp_option {
* byte in length. * byte in length.
*/ */
uint8_t len; uint8_t len;
/** Option data /** Option data */
* uint8_t data[0];
* Interpretation of the content is entirely dependent upon
* the tag. For fields containing a multi-byte integer, the
* field is defined to be in network-endian order (unless you
* are Intel and feel like violating the spec for fun).
*/
union {
uint8_t byte;
uint16_t word;
uint32_t dword;
struct in_addr in;
uint8_t bytes[0];
char string[0];
} data;
} __attribute__ (( packed )); } __attribute__ (( packed ));
/** /**
@ -355,27 +344,6 @@ struct dhcp_option {
/** Maximum length for a single DHCP option */ /** Maximum length for a single DHCP option */
#define DHCP_MAX_LEN 0xff #define DHCP_MAX_LEN 0xff
/** A DHCP options block */
struct dhcp_option_block {
/** Reference counter */
struct refcnt refcnt;
/** List of option blocks */
struct list_head list;
/** Option block raw data */
void *data;
/** Option block length */
size_t len;
/** Option block maximum length */
size_t max_len;
/** Block priority
*
* This is determined at the time of the call to
* register_options() by searching for the @c DHCP_EB_PRIORITY
* option.
*/
signed int priority;
};
/** /**
* A DHCP header * A DHCP header
* *
@ -448,7 +416,7 @@ struct dhcphdr {
* length (for the sake of sanity) is 1, to allow for a single * length (for the sake of sanity) is 1, to allow for a single
* @c DHCP_END tag. * @c DHCP_END tag.
*/ */
uint8_t options[1]; uint8_t options[0];
}; };
/** Opcode for a request from client to server */ /** Opcode for a request from client to server */
@ -474,74 +442,17 @@ struct dhcphdr {
*/ */
#define DHCP_MIN_LEN 552 #define DHCP_MIN_LEN 552
/**
* A DHCP packet
*
*/
struct dhcp_packet {
/** The DHCP packet contents */
struct dhcphdr *dhcphdr;
/** Maximum length of the DHCP packet buffer */
size_t max_len;
/** Used length of the DHCP packet buffer */
size_t len;
/** DHCP options */
struct dhcp_option_block options;
};
/**
* Get reference to DHCP options block
*
* @v options DHCP options block
* @ret options DHCP options block
*/
static inline __attribute__ (( always_inline )) struct dhcp_option_block *
dhcpopt_get ( struct dhcp_option_block *options ) {
ref_get ( &options->refcnt );
return options;
}
/**
* Drop reference to DHCP options block
*
* @v options DHCP options block, or NULL
*/
static inline __attribute__ (( always_inline )) void
dhcpopt_put ( struct dhcp_option_block *options ) {
ref_put ( &options->refcnt );
}
/** Maximum time that we will wait for ProxyDHCP offers */ /** Maximum time that we will wait for ProxyDHCP offers */
#define PROXYDHCP_WAIT_TIME ( TICKS_PER_SEC * 1 ) #define PROXYDHCP_WAIT_TIME ( TICKS_PER_SEC * 1 )
extern struct list_head dhcp_option_blocks; extern int create_dhcp_request ( struct dhcp_packet *dhcppkt,
struct net_device *netdev, int msgtype,
struct settings *offer_settings,
void *data, size_t max_len );
extern int create_dhcp_response ( struct dhcp_packet *dhcppkt,
struct net_device *netdev, int msgtype,
struct settings *settings,
void *data, size_t max_len );
extern int start_dhcp ( struct job_interface *job, struct net_device *netdev );
extern unsigned long dhcp_num_option ( struct dhcp_option *option );
extern struct dhcp_option *
find_dhcp_option ( struct dhcp_option_block *options, unsigned int tag );
extern void register_dhcp_options ( struct dhcp_option_block *options );
extern void unregister_dhcp_options ( struct dhcp_option_block *options );
extern void init_dhcp_options ( struct dhcp_option_block *options,
void *data, size_t max_len );
extern struct dhcp_option_block * __malloc alloc_dhcp_options ( size_t max_len );
extern struct dhcp_option *
set_dhcp_option ( struct dhcp_option_block *options, unsigned int tag,
const void *data, size_t len );
extern unsigned long find_dhcp_num_option ( struct dhcp_option_block *options,
unsigned int tag );
extern void delete_dhcp_option ( struct dhcp_option_block *options,
unsigned int tag );
extern int create_dhcp_request ( struct net_device *netdev, int msgtype,
struct dhcp_option_block *options,
void *data, size_t max_len,
struct dhcp_packet *dhcppkt );
extern int create_dhcp_response ( struct net_device *netdev, int msgtype,
struct dhcp_option_block *options,
void *data, size_t max_len,
struct dhcp_packet *dhcppkt );
extern int start_dhcp ( struct job_interface *job, struct net_device *netdev,
int (*register_options) ( struct net_device *,
struct dhcp_option_block * ));
#endif /* _GPXE_DHCP_H */ #endif /* _GPXE_DHCP_H */

View File

@ -0,0 +1,32 @@
#ifndef _GPXE_DHCPOPTS_H
#define _GPXE_DHCPOPTS_H
/** @file
*
* DHCP options
*
*/
#include <gpxe/dhcp.h>
/** A DHCP options block */
struct dhcp_options {
/** Option block raw data */
void *data;
/** Option block length */
size_t len;
/** Option block maximum length */
size_t max_len;
};
extern int dhcpopt_store ( struct dhcp_options *options, unsigned int tag,
const void *data, size_t len );
extern int dhcpopt_extensible_store ( struct dhcp_options *options,
unsigned int tag,
const void *data, size_t len );
extern int dhcpopt_fetch ( struct dhcp_options *options, unsigned int tag,
void *data, size_t len );
extern void dhcpopt_init ( struct dhcp_options *options,
void *data, size_t max_len );
#endif /* _GPXE_DHCPOPTS_H */

View File

@ -0,0 +1,34 @@
#ifndef _GPXE_DHCPPKT_H
#define _GPXE_DHCPPKT_H
/** @file
*
* DHCP packets
*
*/
#include <gpxe/dhcp.h>
#include <gpxe/dhcpopts.h>
#include <gpxe/settings.h>
/**
* A DHCP packet
*
*/
struct dhcp_packet {
/** Settings block */
struct settings settings;
/** The DHCP packet contents */
struct dhcphdr *dhcphdr;
/** Maximum length of the DHCP packet buffer */
size_t max_len;
/** Used length of the DHCP packet buffer */
size_t len;
/** DHCP option blocks */
struct dhcp_options options;
};
extern void dhcppkt_init ( struct dhcp_packet *dhcppkt, struct refcnt *refcnt,
void *data, size_t len );
#endif /* _GPXE_DHCPPKT_H */

View File

@ -130,6 +130,7 @@
#define ERRFILE_tftp ( ERRFILE_NET | 0x00120000 ) #define ERRFILE_tftp ( ERRFILE_NET | 0x00120000 )
#define ERRFILE_infiniband ( ERRFILE_NET | 0x00130000 ) #define ERRFILE_infiniband ( ERRFILE_NET | 0x00130000 )
#define ERRFILE_netdev_settings ( ERRFILE_NET | 0x00140000 ) #define ERRFILE_netdev_settings ( ERRFILE_NET | 0x00140000 )
#define ERRFILE_dhcppkt ( ERRFILE_NET | 0x00150000 )
#define ERRFILE_image ( ERRFILE_IMAGE | 0x00000000 ) #define ERRFILE_image ( ERRFILE_IMAGE | 0x00000000 )
#define ERRFILE_elf ( ERRFILE_IMAGE | 0x00010000 ) #define ERRFILE_elf ( ERRFILE_IMAGE | 0x00010000 )

View File

@ -8,9 +8,11 @@
*/ */
#include <stdint.h> #include <stdint.h>
#include <gpxe/dhcpopts.h>
#include <gpxe/settings.h>
struct nvs_device; struct nvs_device;
struct dhcp_option_block; struct refcnt;
/** /**
* A fragment of a non-volatile storage device used for stored options * A fragment of a non-volatile storage device used for stored options
@ -26,6 +28,8 @@ struct nvo_fragment {
* A block of non-volatile stored options * A block of non-volatile stored options
*/ */
struct nvo_block { struct nvo_block {
/** Settings block */
struct settings settings;
/** Underlying non-volatile storage device */ /** Underlying non-volatile storage device */
struct nvs_device *nvs; struct nvs_device *nvs;
/** List of option-containing fragments /** List of option-containing fragments
@ -33,17 +37,17 @@ struct nvo_block {
* The list is terminated by a fragment with a length of zero. * The list is terminated by a fragment with a length of zero.
*/ */
struct nvo_fragment *fragments; struct nvo_fragment *fragments;
/** Total length of all fragments /** Total length of option-containing fragments */
*
* This field is filled in by nvo_register().
*/
size_t total_len; size_t total_len;
/** Option-containing data */
void *data;
/** DHCP options block */ /** DHCP options block */
struct dhcp_option_block *options; struct dhcp_options dhcpopts;
}; };
extern int nvo_register ( struct nvo_block *nvo ); extern void nvo_init ( struct nvo_block *nvo, struct nvs_device *nvs,
extern int nvo_save ( struct nvo_block *nvo ); struct nvo_fragment *fragments, struct refcnt *refcnt );
extern void nvo_unregister ( struct nvo_block *nvo ); extern int register_nvo ( struct nvo_block *nvo, struct settings *parent );
extern void unregister_nvo ( struct nvo_block *nvo );
#endif /* _GPXE_NVO_H */ #endif /* _GPXE_NVO_H */

View File

@ -151,6 +151,9 @@ extern int store_setting ( struct settings *settings, unsigned int tag,
const void *data, size_t len ); const void *data, size_t len );
extern int fetch_setting ( struct settings *settings, unsigned int tag, extern int fetch_setting ( struct settings *settings, unsigned int tag,
void *data, size_t len ); void *data, size_t len );
extern int copy_setting ( struct settings *dest, unsigned int dest_tag,
struct settings *source, unsigned int source_tag );
extern int copy_settings ( struct settings *dest, struct settings *source );
extern int fetch_setting_len ( struct settings *settings, unsigned int tag ); extern int fetch_setting_len ( struct settings *settings, unsigned int tag );
extern int fetch_string_setting ( struct settings *settings, unsigned int tag, extern int fetch_string_setting ( struct settings *settings, unsigned int tag,
char *data, size_t len ); char *data, size_t len );
@ -163,6 +166,8 @@ extern int fetch_uint_setting ( struct settings *settings, unsigned int tag,
extern long fetch_intz_setting ( struct settings *settings, unsigned int tag ); extern long fetch_intz_setting ( struct settings *settings, unsigned int tag );
extern unsigned long fetch_uintz_setting ( struct settings *settings, extern unsigned long fetch_uintz_setting ( struct settings *settings,
unsigned int tag ); unsigned int tag );
extern struct settings * find_child_settings ( struct settings *parent,
const char *name );
extern struct settings * find_settings ( const char *name ); extern struct settings * find_settings ( const char *name );
extern int store_typed_setting ( struct settings *settings, extern int store_typed_setting ( struct settings *settings,
unsigned int tag, struct setting_type *type, unsigned int tag, struct setting_type *type,

View File

@ -28,6 +28,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <gpxe/uaccess.h> #include <gpxe/uaccess.h>
#include <gpxe/dhcp.h> #include <gpxe/dhcp.h>
#include <gpxe/dhcppkt.h>
#include <gpxe/device.h> #include <gpxe/device.h>
#include <gpxe/netdevice.h> #include <gpxe/netdevice.h>
#include <gpxe/isapnp.h> #include <gpxe/isapnp.h>
@ -117,9 +118,10 @@ PXENV_EXIT_t pxenv_unload_stack ( struct s_PXENV_UNLOAD_STACK *unload_stack ) {
PXENV_EXIT_t pxenv_get_cached_info ( struct s_PXENV_GET_CACHED_INFO PXENV_EXIT_t pxenv_get_cached_info ( struct s_PXENV_GET_CACHED_INFO
*get_cached_info ) { *get_cached_info ) {
struct dhcp_packet dhcppkt; struct dhcp_packet dhcppkt;
int ( * dhcp_packet_creator ) ( struct net_device *, int, int ( * dhcp_packet_creator ) ( struct dhcp_packet *dhcppkt,
struct dhcp_option_block *, void *, struct net_device *netdev, int msgtype,
size_t, struct dhcp_packet * ); struct settings *settings,
void *data, size_t max_len );
unsigned int idx; unsigned int idx;
unsigned int msgtype; unsigned int msgtype;
size_t len; size_t len;
@ -149,10 +151,9 @@ PXENV_EXIT_t pxenv_get_cached_info ( struct s_PXENV_GET_CACHED_INFO
dhcp_packet_creator = create_dhcp_response; dhcp_packet_creator = create_dhcp_response;
msgtype = DHCPACK; msgtype = DHCPACK;
} }
if ( ( rc = dhcp_packet_creator ( pxe_netdev, msgtype, if ( ( rc = dhcp_packet_creator ( &dhcppkt, pxe_netdev,
NULL, &cached_info[idx], msgtype, NULL, &cached_info[idx],
sizeof ( cached_info[idx] ), sizeof ( cached_info[idx] ) ) ) != 0 ) {
&dhcppkt ) ) != 0 ) {
DBG ( " failed to build packet" ); DBG ( " failed to build packet" );
goto err; goto err;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>. * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as * modify it under the terms of the GNU General Public License as
@ -19,14 +19,10 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <byteswap.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <assert.h>
#include <gpxe/list.h>
#include <gpxe/in.h>
#include <gpxe/uri.h>
#include <gpxe/dhcp.h> #include <gpxe/dhcp.h>
#include <gpxe/dhcpopts.h>
/** @file /** @file
* *
@ -34,9 +30,6 @@
* *
*/ */
/** List of registered DHCP option blocks */
LIST_HEAD ( dhcp_option_blocks );
/** /**
* Obtain printable version of a DHCP option tag * Obtain printable version of a DHCP option tag
* *
@ -58,47 +51,28 @@ static inline char * dhcp_tag_name ( unsigned int tag ) {
} }
/** /**
* Obtain value of a numerical DHCP option * Get pointer to DHCP option
* *
* @v option DHCP option, or NULL * @v options DHCP options block
* @ret value Numerical value of the option, or 0 * @v offset Offset within options block
* * @ret option DHCP option
* Parses the numerical value from a DHCP option, if present. It is
* permitted to call dhcp_num_option() with @c option set to NULL; in
* this case 0 will be returned.
*
* The caller does not specify the size of the DHCP option data; this
* is implied by the length field stored within the DHCP option
* itself.
*/ */
unsigned long dhcp_num_option ( struct dhcp_option *option ) { static inline __attribute__ (( always_inline )) struct dhcp_option *
unsigned long value = 0; dhcp_option ( struct dhcp_options *options, unsigned int offset ) {
uint8_t *data; return ( ( struct dhcp_option * ) ( options->data + offset ) );
if ( option ) {
/* This is actually smaller code than using htons()
* etc., and will also cope well with malformed
* options (such as zero-length options).
*/
for ( data = option->data.bytes ;
data < ( option->data.bytes + option->len ) ; data++ )
value = ( ( value << 8 ) | *data );
}
return value;
} }
/** /**
* Calculate length of a normal DHCP option * Get offset of a DHCP option
* *
* @v options DHCP options block
* @v option DHCP option * @v option DHCP option
* @ret len Length (including tag and length field) * @ret offset Offset within options block
*
* @c option may not be a @c DHCP_PAD or @c DHCP_END option.
*/ */
static inline unsigned int dhcp_option_len ( struct dhcp_option *option ) { static inline __attribute__ (( always_inline )) int
assert ( option->tag != DHCP_PAD ); dhcp_option_offset ( struct dhcp_options *options,
assert ( option->tag != DHCP_END ); struct dhcp_option *option ) {
return ( option->len + DHCP_OPTION_HEADER_LEN ); return ( ( ( void * ) option ) - options->data );
} }
/** /**
@ -107,11 +81,11 @@ static inline unsigned int dhcp_option_len ( struct dhcp_option *option ) {
* @v option DHCP option * @v option DHCP option
* @ret len Length (including tag and length field) * @ret len Length (including tag and length field)
*/ */
static inline unsigned int dhcp_any_option_len ( struct dhcp_option *option ) { static unsigned int dhcp_option_len ( struct dhcp_option *option ) {
if ( ( option->tag == DHCP_END ) || ( option->tag == DHCP_PAD ) ) { if ( ( option->tag == DHCP_END ) || ( option->tag == DHCP_PAD ) ) {
return 1; return 1;
} else { } else {
return dhcp_option_len ( option ); return ( option->len + DHCP_OPTION_HEADER_LEN );
} }
} }
@ -120,27 +94,27 @@ static inline unsigned int dhcp_any_option_len ( struct dhcp_option *option ) {
* *
* @v options DHCP options block * @v options DHCP options block
* @v tag DHCP option tag to search for * @v tag DHCP option tag to search for
* @ret encapsulator Encapsulating DHCP option * @ret encap_offset Offset of encapsulating DHCP option
* @ret option DHCP option, or NULL if not found * @ret offset Offset of DHCP option, or negative error
* *
* Searches for the DHCP option matching the specified tag within the * Searches for the DHCP option matching the specified tag within the
* DHCP option block. Encapsulated options may be searched for by * DHCP option block. Encapsulated options may be searched for by
* using DHCP_ENCAP_OPT() to construct the tag value. * using DHCP_ENCAP_OPT() to construct the tag value.
* *
* If the option is encapsulated, and @c encapsulator is non-NULL, it * If the option is encapsulated, and @c encapsulator is non-NULL, it
* will be filled in with a pointer to the encapsulating option. * will be filled in with the offset of the encapsulating option.
* *
* This routine is designed to be paranoid. It does not assume that * This routine is designed to be paranoid. It does not assume that
* the option data is well-formatted, and so must guard against flaws * the option data is well-formatted, and so must guard against flaws
* such as options missing a @c DHCP_END terminator, or options whose * such as options missing a @c DHCP_END terminator, or options whose
* length would take them beyond the end of the data block. * length would take them beyond the end of the data block.
*/ */
static struct dhcp_option * static int find_dhcp_option_with_encap ( struct dhcp_options *options,
find_dhcp_option_with_encap ( struct dhcp_option_block *options, unsigned int tag,
unsigned int tag, int *encap_offset ) {
struct dhcp_option **encapsulator ) {
unsigned int original_tag __attribute__ (( unused )) = tag; unsigned int original_tag __attribute__ (( unused )) = tag;
struct dhcp_option *option = options->data; struct dhcp_option *option;
int offset = 0;
ssize_t remaining = options->len; ssize_t remaining = options->len;
unsigned int option_len; unsigned int option_len;
@ -149,16 +123,17 @@ find_dhcp_option_with_encap ( struct dhcp_option_block *options,
* if the length is malformed (i.e. takes us beyond * if the length is malformed (i.e. takes us beyond
* the end of the data block). * the end of the data block).
*/ */
option_len = dhcp_any_option_len ( option ); option = dhcp_option ( options, offset );
option_len = dhcp_option_len ( option );
remaining -= option_len; remaining -= option_len;
if ( remaining < 0 ) if ( remaining < 0 )
break; break;
/* Check for matching tag */ /* Check for matching tag */
if ( option->tag == tag ) { if ( option->tag == tag ) {
DBG ( "Found DHCP option %s (length %d) in block %p\n", DBGC ( options, "DHCPOPT %p found %s (length %d)\n",
dhcp_tag_name ( original_tag ), option->len, options, dhcp_tag_name ( original_tag ),
options ); option_len );
return option; return offset;
} }
/* Check for explicit end marker */ /* Check for explicit end marker */
if ( option->tag == DHCP_END ) if ( option->tag == DHCP_END )
@ -166,168 +141,74 @@ find_dhcp_option_with_encap ( struct dhcp_option_block *options,
/* Check for start of matching encapsulation block */ /* Check for start of matching encapsulation block */
if ( DHCP_IS_ENCAP_OPT ( tag ) && if ( DHCP_IS_ENCAP_OPT ( tag ) &&
( option->tag == DHCP_ENCAPSULATOR ( tag ) ) ) { ( option->tag == DHCP_ENCAPSULATOR ( tag ) ) ) {
if ( encapsulator ) if ( encap_offset )
*encapsulator = option; *encap_offset = offset;
/* Continue search within encapsulated option block */ /* Continue search within encapsulated option block */
tag = DHCP_ENCAPSULATED ( tag ); tag = DHCP_ENCAPSULATED ( tag );
remaining = option->len; remaining = option_len;
option = ( void * ) &option->data; offset += DHCP_OPTION_HEADER_LEN;
continue; continue;
} }
option = ( ( ( void * ) option ) + option_len ); offset += option_len;
} }
return NULL; return -ENOENT;
}
/**
* Find DHCP option within DHCP options block
*
* @v options DHCP options block, or NULL
* @v tag DHCP option tag to search for
* @ret option DHCP option, or NULL if not found
*
* Searches for the DHCP option matching the specified tag within the
* DHCP option block. Encapsulated options may be searched for by
* using DHCP_ENCAP_OPT() to construct the tag value.
*
* If @c options is NULL, all registered option blocks will be
* searched. Where multiple option blocks contain the same DHCP
* option, the option from the highest-priority block will be
* returned. (Priority of an options block is determined by the value
* of the @c DHCP_EB_PRIORITY option within the block, if present; the
* default priority is zero).
*/
struct dhcp_option * find_dhcp_option ( struct dhcp_option_block *options,
unsigned int tag ) {
struct dhcp_option *option;
if ( options ) {
return find_dhcp_option_with_encap ( options, tag, NULL );
} else {
list_for_each_entry ( options, &dhcp_option_blocks, list ) {
if ( ( option = find_dhcp_option ( options, tag ) ) )
return option;
}
return NULL;
}
}
/**
* Register DHCP option block
*
* @v options DHCP option block
*
* Register a block of DHCP options.
*/
void register_dhcp_options ( struct dhcp_option_block *options ) {
struct dhcp_option_block *existing;
/* Determine priority of new block */
options->priority = find_dhcp_num_option ( options, DHCP_EB_PRIORITY );
DBG ( "Registering DHCP options block %p with priority %d\n",
options, options->priority );
/* Insert after any existing blocks which have a higher priority */
list_for_each_entry ( existing, &dhcp_option_blocks, list ) {
if ( options->priority > existing->priority )
break;
}
dhcpopt_get ( options );
list_add_tail ( &options->list, &existing->list );
}
/**
* Unregister DHCP option block
*
* @v options DHCP option block
*/
void unregister_dhcp_options ( struct dhcp_option_block *options ) {
list_del ( &options->list );
dhcpopt_put ( options );
}
/**
* Initialise empty block of DHCP options
*
* @v options Uninitialised DHCP option block
* @v data Memory for DHCP option data
* @v max_len Length of memory for DHCP option data
*
* Populates the DHCP option data with a single @c DHCP_END option and
* fills in the fields of the @c dhcp_option_block structure.
*/
void init_dhcp_options ( struct dhcp_option_block *options,
void *data, size_t max_len ) {
struct dhcp_option *option;
options->data = data;
options->max_len = max_len;
option = options->data;
option->tag = DHCP_END;
options->len = 1;
DBG ( "DHCP options block %p initialised (data %p max_len %#zx)\n",
options, options->data, options->max_len );
}
/**
* Allocate space for a block of DHCP options
*
* @v max_len Maximum length of option block
* @ret options DHCP option block, or NULL
*
* Creates a new DHCP option block and populates it with an empty
* options list. This call does not register the options block.
*/
struct dhcp_option_block * alloc_dhcp_options ( size_t max_len ) {
struct dhcp_option_block *options;
options = malloc ( sizeof ( *options ) + max_len );
if ( options ) {
init_dhcp_options ( options,
( (void *) options + sizeof ( *options ) ),
max_len );
}
return options;
} }
/** /**
* Resize a DHCP option * Resize a DHCP option
* *
* @v options DHCP option block * @v options DHCP option block
* @v option DHCP option to resize * @v offset Offset of option to resize
* @v encapsulator Encapsulating option (or NULL) * @v encap_offset Offset of encapsulating offset (or -ve for none)
* @v old_len Old length (including header) * @v old_len Old length (including header)
* @v new_len New length (including header) * @v new_len New length (including header)
* @v can_realloc Can reallocate options data if necessary
* @ret rc Return status code * @ret rc Return status code
*/ */
static int resize_dhcp_option ( struct dhcp_option_block *options, static int resize_dhcp_option ( struct dhcp_options *options,
struct dhcp_option *option, int offset, int encap_offset,
struct dhcp_option *encapsulator, size_t old_len, size_t new_len,
size_t old_len, size_t new_len ) { int can_realloc ) {
void *source = ( ( ( void * ) option ) + old_len ); struct dhcp_option *encapsulator;
void *dest = ( ( ( void * ) option ) + new_len ); struct dhcp_option *option;
void *end = ( options->data + options->max_len );
ssize_t delta = ( new_len - old_len ); ssize_t delta = ( new_len - old_len );
size_t new_options_len; size_t new_options_len;
size_t new_encapsulator_len; size_t new_encapsulator_len;
void *new_data;
void *source;
void *dest;
void *end;
/* Check for sufficient space, and update length fields */ /* Check for sufficient space, and update length fields */
if ( new_len > DHCP_MAX_LEN ) if ( new_len > DHCP_MAX_LEN )
return -ENOMEM; return -ENOSPC;
new_options_len = ( options->len + delta ); new_options_len = ( options->len + delta );
if ( new_options_len > options->max_len ) if ( new_options_len > options->max_len ) {
return -ENOMEM; /* Reallocate options block if allowed to do so. */
if ( encapsulator ) { if ( can_realloc ) {
new_data = realloc ( options->data, new_options_len );
if ( ! new_data )
return -ENOMEM;
options->data = new_data;
options->max_len = new_options_len;
} else {
return -ENOMEM;
}
}
if ( encap_offset >= 0 ) {
encapsulator = dhcp_option ( options, encap_offset );
new_encapsulator_len = ( encapsulator->len + delta ); new_encapsulator_len = ( encapsulator->len + delta );
if ( new_encapsulator_len > DHCP_MAX_LEN ) if ( new_encapsulator_len > DHCP_MAX_LEN )
return -ENOMEM; return -ENOSPC;
encapsulator->len = new_encapsulator_len; encapsulator->len = new_encapsulator_len;
} }
options->len = new_options_len; options->len = new_options_len;
/* Move remainder of option data */ /* Move remainder of option data */
option = dhcp_option ( options, offset );
source = ( ( ( void * ) option ) + old_len );
dest = ( ( ( void * ) option ) + new_len );
end = ( options->data + options->max_len );
memmove ( dest, source, ( end - dest ) ); memmove ( dest, source, ( end - dest ) );
return 0; return 0;
@ -340,7 +221,8 @@ static int resize_dhcp_option ( struct dhcp_option_block *options,
* @v tag DHCP option tag * @v tag DHCP option tag
* @v data New value for DHCP option * @v data New value for DHCP option
* @v len Length of value, in bytes * @v len Length of value, in bytes
* @ret option DHCP option, or NULL * @v can_realloc Can reallocate options data if necessary
* @ret offset Offset of DHCP option, or negative error
* *
* Sets the value of a DHCP option within the options block. The * Sets the value of a DHCP option within the options block. The
* option may or may not already exist. Encapsulators will be created * option may or may not already exist. Encapsulators will be created
@ -350,97 +232,183 @@ static int resize_dhcp_option ( struct dhcp_option_block *options,
* If it does fail, and the option existed previously, the option will * If it does fail, and the option existed previously, the option will
* be left with its original value. * be left with its original value.
*/ */
struct dhcp_option * set_dhcp_option ( struct dhcp_option_block *options, static int set_dhcp_option ( struct dhcp_options *options, unsigned int tag,
unsigned int tag, const void *data, size_t len,
const void *data, size_t len ) { int can_realloc ) {
static const uint8_t empty_encapsulator[] = { DHCP_END }; static const uint8_t empty_encapsulator[] = { DHCP_END };
int offset;
int encap_offset = -1;
int creation_offset = 0;
struct dhcp_option *option; struct dhcp_option *option;
void *insertion_point;
struct dhcp_option *encapsulator = NULL;
unsigned int encap_tag = DHCP_ENCAPSULATOR ( tag ); unsigned int encap_tag = DHCP_ENCAPSULATOR ( tag );
size_t old_len = 0; size_t old_len = 0;
size_t new_len = ( len ? ( len + DHCP_OPTION_HEADER_LEN ) : 0 ); size_t new_len = ( len ? ( len + DHCP_OPTION_HEADER_LEN ) : 0 );
int rc;
/* Return NULL if no options block specified */
if ( ! options )
return NULL;
/* Find old instance of this option, if any */ /* Find old instance of this option, if any */
option = find_dhcp_option_with_encap ( options, tag, &encapsulator ); offset = find_dhcp_option_with_encap ( options, tag, &encap_offset );
if ( option ) { if ( offset >= 0 ) {
old_len = dhcp_option_len ( option ); old_len = dhcp_option_len ( dhcp_option ( options, offset ) );
DBG ( "Resizing DHCP option %s from length %d to %zd in block " DBGC ( options, "DHCPOPT %p resizing %s from %zd to %zd\n",
"%p\n", dhcp_tag_name (tag), option->len, len, options ); options, dhcp_tag_name ( tag ), old_len, new_len );
} else { } else {
old_len = 0; DBGC ( options, "DHCPOPT %p creating %s (length %zd)\n",
DBG ( "Creating DHCP option %s (length %zd) in block %p\n", options, dhcp_tag_name ( tag ), len );
dhcp_tag_name ( tag ), len, options );
} }
/* Ensure that encapsulator exists, if required */ /* Ensure that encapsulator exists, if required */
insertion_point = options->data; if ( encap_tag ) {
if ( DHCP_IS_ENCAP_OPT ( tag ) ) { if ( encap_offset < 0 )
if ( ! encapsulator ) encap_offset = set_dhcp_option ( options, encap_tag,
encapsulator = set_dhcp_option ( options, encap_tag, empty_encapsulator, 1,
empty_encapsulator, can_realloc );
sizeof ( empty_encapsulator) ); if ( encap_offset < 0 )
if ( ! encapsulator ) return encap_offset;
return NULL; creation_offset = ( encap_offset + DHCP_OPTION_HEADER_LEN );
insertion_point = &encapsulator->data;
} }
/* Create new option if necessary */ /* Create new option if necessary */
if ( ! option ) if ( offset < 0 )
option = insertion_point; offset = creation_offset;
/* Resize option to fit new data */ /* Resize option to fit new data */
if ( resize_dhcp_option ( options, option, encapsulator, if ( ( rc = resize_dhcp_option ( options, offset, encap_offset,
old_len, new_len ) != 0 ) old_len, new_len,
return NULL; can_realloc ) ) != 0 )
return rc;
/* Copy new data into option, if applicable */ /* Copy new data into option, if applicable */
if ( len ) { if ( len ) {
option = dhcp_option ( options, offset );
option->tag = tag; option->tag = tag;
option->len = len; option->len = len;
memcpy ( &option->data, data, len ); memcpy ( &option->data, data, len );
} }
/* Delete encapsulator if there's nothing else left in it */ /* Delete encapsulator if there's nothing else left in it */
if ( encapsulator && ( encapsulator->len <= 1 ) ) if ( encap_offset >= 0 ) {
set_dhcp_option ( options, encap_tag, NULL, 0 ); option = dhcp_option ( options, encap_offset );
if ( option->len <= 1 )
set_dhcp_option ( options, encap_tag, NULL, 0, 0 );
}
return option; return offset;
} }
/** /**
* Find DHCP numerical option, and return its value * Store value of DHCP option setting
* *
* @v options DHCP options block * @v options DHCP option block
* @v tag DHCP option tag to search for * @v tag Setting tag number
* @ret value Numerical value of the option, or 0 if not found * @v data Setting data, or NULL to clear setting
* * @v len Length of setting data
* This function exists merely as a notational shorthand for a call to * @ret rc Return status code
* find_dhcp_option() followed by a call to dhcp_num_option(). It is
* not possible to distinguish between the cases "option not found"
* and "option has a value of zero" using this function; if this
* matters to you then issue the two constituent calls directly and
* check that find_dhcp_option() returns a non-NULL value.
*/ */
unsigned long find_dhcp_num_option ( struct dhcp_option_block *options, int dhcpopt_store ( struct dhcp_options *options, unsigned int tag,
unsigned int tag ) { const void *data, size_t len ) {
return dhcp_num_option ( find_dhcp_option ( options, tag ) ); int offset;
offset = set_dhcp_option ( options, tag, data, len, 0 );
if ( offset < 0 )
return offset;
return 0;
} }
/** /**
* Delete DHCP option * Store value of DHCP option setting, extending options block if necessary
* *
* @v options DHCP options block * @v options DHCP option block
* @v tag DHCP option tag * @v tag Setting tag number
* * @v data Setting data, or NULL to clear setting
* This function exists merely as a notational shorthand for a call to * @v len Length of setting data
* set_dhcp_option() with @c len set to zero. * @ret rc Return status code
*/ */
void delete_dhcp_option ( struct dhcp_option_block *options, int dhcpopt_extensible_store ( struct dhcp_options *options, unsigned int tag,
unsigned int tag ) { const void *data, size_t len ) {
set_dhcp_option ( options, tag, NULL, 0 ); int offset;
offset = set_dhcp_option ( options, tag, data, len, 1 );
if ( offset < 0 )
return offset;
return 0;
}
/**
* Fetch value of DHCP option setting
*
* @v options DHCP option block
* @v tag Setting tag number
* @v data Buffer to fill with setting data
* @v len Length of buffer
* @ret len Length of setting data, or negative error
*/
int dhcpopt_fetch ( struct dhcp_options *options, unsigned int tag,
void *data, size_t len ) {
int offset;
struct dhcp_option *option;
size_t option_len;
offset = find_dhcp_option_with_encap ( options, tag, NULL );
if ( offset < 0 )
return offset;
option = dhcp_option ( options, offset );
option_len = dhcp_option_len ( option );
if ( len > option_len )
len = option_len;
memcpy ( data, option->data, len );
return option_len;
}
/**
* Recalculate length of DHCP options block
*
* @v options Uninitialised DHCP option block
*
* The "used length" field will be updated based on scanning through
* the block to find the end of the options.
*/
static void dhcpopt_update_len ( struct dhcp_options *options ) {
struct dhcp_option *option;
int offset = 0;
ssize_t remaining = options->max_len;
unsigned int option_len;
/* Find last non-pad option */
options->len = 0;
while ( remaining ) {
option = dhcp_option ( options, offset );
option_len = dhcp_option_len ( option );
remaining -= option_len;
if ( remaining < 0 )
break;
offset += option_len;
if ( option->tag != DHCP_PAD )
options->len = offset;
}
}
/**
* Initialise prepopulated block of DHCP options
*
* @v options Uninitialised DHCP option block
* @v data Memory for DHCP option data
* @v max_len Length of memory for DHCP option data
*
* The memory content must already be filled with valid DHCP options.
* A zeroed block counts as a block of valid DHCP options.
*/
void dhcpopt_init ( struct dhcp_options *options, void *data,
size_t max_len ) {
/* Fill in fields */
options->data = data;
options->max_len = max_len;
/* Update length */
dhcpopt_update_len ( options );
DBGC ( options, "DHCPOPT %p created (data %p len %#zx max_len %#zx)\n",
options, options->data, options->len, options->max_len );
} }

186
src/net/dhcppkt.c Normal file
View File

@ -0,0 +1,186 @@
/*
* Copyright (C) 2008 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 <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <gpxe/netdevice.h>
#include <gpxe/dhcp.h>
#include <gpxe/dhcpopts.h>
#include <gpxe/dhcppkt.h>
/** @file
*
* DHCP packets
*
*/
/** A dedicated field within a DHCP packet */
struct dhcp_packet_field {
/** Settings tag number */
unsigned int tag;
/** Offset within DHCP packet */
uint16_t offset;
/** Length of field */
uint16_t len;
};
/** Declare a dedicated field within a DHCP packet
*
* @v _tag Settings tag number
* @v _field Field name
*/
#define DHCP_PACKET_FIELD( _tag, _field ) { \
.tag = (_tag), \
.offset = offsetof ( struct dhcphdr, _field ), \
.len = sizeof ( ( ( struct dhcphdr * ) 0 )->_field ), \
}
/** Dedicated fields within a DHCP packet */
static struct dhcp_packet_field dhcp_packet_fields[] = {
DHCP_PACKET_FIELD ( DHCP_EB_YIADDR, yiaddr ),
DHCP_PACKET_FIELD ( DHCP_EB_SIADDR, siaddr ),
DHCP_PACKET_FIELD ( DHCP_TFTP_SERVER_NAME, sname ),
DHCP_PACKET_FIELD ( DHCP_BOOTFILE_NAME, file ),
};
/**
* Get address of a DHCP packet field
*
* @v dhcphdr DHCP packet header
* @v field DHCP packet field
* @ret data Packet field data
*/
static inline void * dhcp_packet_field ( struct dhcphdr *dhcphdr,
struct dhcp_packet_field *field ) {
return ( ( ( void * ) dhcphdr ) + field->offset );
}
/**
* Find DHCP packet field corresponding to settings tag number
*
* @v tag Settings tag number
* @ret field DHCP packet field, or NULL
*/
static struct dhcp_packet_field *
find_dhcp_packet_field ( unsigned int tag ) {
struct dhcp_packet_field *field;
unsigned int i;
for ( i = 0 ; i < ( sizeof ( dhcp_packet_fields ) /
sizeof ( dhcp_packet_fields[0] ) ) ; i++ ) {
field = &dhcp_packet_fields[i];
if ( field->tag == tag )
return field;
}
return NULL;
}
/**
* Store value of DHCP packet setting
*
* @v settings Settings block
* @v tag Setting tag number
* @v data Setting data, or NULL to clear setting
* @v len Length of setting data
* @ret rc Return status code
*/
static int dhcppkt_store ( struct settings *settings, unsigned int tag,
const void *data, size_t len ) {
struct dhcp_packet *dhcppkt =
container_of ( settings, struct dhcp_packet, settings );
struct dhcp_packet_field *field;
int rc;
/* If this is a special field, fill it in */
if ( ( field = find_dhcp_packet_field ( tag ) ) != NULL ) {
if ( len > field->len )
return -ENOSPC;
memcpy ( dhcp_packet_field ( dhcppkt->dhcphdr, field ),
data, len );
return 0;
}
/* Otherwise, use the generic options block */
rc = dhcpopt_store ( &dhcppkt->options, tag, data, len );
/* Update our used-length field */
dhcppkt->len = ( offsetof ( struct dhcphdr, options ) +
dhcppkt->options.len );
return rc;
}
/**
* Fetch value of DHCP packet setting
*
* @v settings Settings block
* @v tag Setting tag number
* @v data Buffer to fill with setting data
* @v len Length of buffer
* @ret len Length of setting data, or negative error
*/
static int dhcppkt_fetch ( struct settings *settings, unsigned int tag,
void *data, size_t len ) {
struct dhcp_packet *dhcppkt =
container_of ( settings, struct dhcp_packet, settings );
struct dhcp_packet_field *field;
/* If this is a special field, return it */
if ( ( field = find_dhcp_packet_field ( tag ) ) != NULL ) {
if ( len > field->len )
len = field->len;
memcpy ( data,
dhcp_packet_field ( dhcppkt->dhcphdr, field ), len );
return field->len;
}
/* Otherwise, use the generic options block */
return dhcpopt_fetch ( &dhcppkt->options, tag, data, len );
}
/** DHCP settings operations */
static struct settings_operations dhcppkt_settings_operations = {
.store = dhcppkt_store,
.fetch = dhcppkt_fetch,
};
/**
* Initialise prepopulated DHCP packet
*
* @v dhcppkt Uninitialised DHCP packet
* @v refcnt Reference counter of containing object, or NULL
* @v data Memory for DHCP packet data
* @v max_len Length of memory for DHCP packet data
*
* The memory content must already be filled with valid DHCP options.
* A zeroed block counts as a block of valid DHCP options.
*/
void dhcppkt_init ( struct dhcp_packet *dhcppkt, struct refcnt *refcnt,
void *data, size_t len ) {
dhcppkt->dhcphdr = data;
dhcppkt->max_len = len;
dhcpopt_init ( &dhcppkt->options, &dhcppkt->dhcphdr->options,
( len - offsetof ( struct dhcphdr, options ) ) );
dhcppkt->len = ( offsetof ( struct dhcphdr, options ) +
dhcppkt->options.len );
settings_init ( &dhcppkt->settings, &dhcppkt_settings_operations,
refcnt, "dhcp" );
}

View File

@ -34,6 +34,10 @@
#include <gpxe/uuid.h> #include <gpxe/uuid.h>
#include <gpxe/dhcp.h> #include <gpxe/dhcp.h>
#include <gpxe/timer.h> #include <gpxe/timer.h>
#include <gpxe/settings.h>
#include <gpxe/dhcp.h>
#include <gpxe/dhcpopts.h>
#include <gpxe/dhcppkt.h>
/** @file /** @file
* *
@ -41,7 +45,8 @@
* *
*/ */
/** DHCP operation types /**
* DHCP operation types
* *
* This table maps from DHCP message types (i.e. values of the @c * This table maps from DHCP message types (i.e. values of the @c
* DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
@ -76,6 +81,13 @@ static uint8_t dhcp_request_options_data[] = {
DHCP_END DHCP_END
}; };
/** Options common to all DHCP requests */
static struct dhcp_options dhcp_request_options = {
.data = dhcp_request_options_data,
.max_len = sizeof ( dhcp_request_options_data ),
.len = sizeof ( dhcp_request_options_data ),
};
/** DHCP feature codes */ /** DHCP feature codes */
static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features ); static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features ); static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
@ -118,197 +130,33 @@ static uint32_t dhcp_xid ( struct net_device *netdev ) {
return xid; return xid;
} }
/** Options common to all DHCP requests */
static struct dhcp_option_block dhcp_request_options = {
.data = dhcp_request_options_data,
.max_len = sizeof ( dhcp_request_options_data ),
.len = sizeof ( dhcp_request_options_data ),
};
/**
* Set option within DHCP packet
*
* @v dhcppkt DHCP packet
* @v tag DHCP option tag
* @v data New value for DHCP option
* @v len Length of value, in bytes
* @ret rc Return status code
*
* Sets the option within the first available options block within the
* DHCP packet. Option blocks are tried in the order specified by @c
* dhcp_option_block_fill_order.
*
* The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
* intercepted and inserted into the appropriate fixed fields within
* the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
* ignored, since our DHCP packet assembly method relies on always
* having option overloading in use.
*/
static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
unsigned int tag, const void *data,
size_t len ) {
struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
struct dhcp_option_block *options = &dhcppkt->options;
struct dhcp_option *option = NULL;
/* Special-case the magic options */
switch ( tag ) {
case DHCP_OPTION_OVERLOAD:
/* Hard-coded in packets we create; always ignore */
return 0;
case DHCP_EB_YIADDR:
memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
return 0;
case DHCP_EB_SIADDR:
memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
return 0;
case DHCP_TFTP_SERVER_NAME:
memset ( dhcphdr->sname, 0, sizeof ( dhcphdr->sname ) );
if ( len > sizeof ( dhcphdr->sname ) )
len = sizeof ( dhcphdr->sname );
memcpy ( dhcphdr->sname, data, len );
return 0;
case DHCP_BOOTFILE_NAME:
memset ( dhcphdr->file, 0, sizeof ( dhcphdr->file ) );
if ( len > sizeof ( dhcphdr->file ) )
len = sizeof ( dhcphdr->file );
memcpy ( dhcphdr->file, data, len );
return 0;
default:
/* Continue processing as normal */
break;
}
/* Set option */
option = set_dhcp_option ( options, tag, data, len );
/* Update DHCP packet length */
dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
+ dhcppkt->options.len );
return ( option ? 0 : -ENOSPC );
}
/**
* Copy option into DHCP packet
*
* @v dhcppkt DHCP packet
* @v options DHCP option block, or NULL
* @v tag DHCP option tag to search for
* @v new_tag DHCP option tag to use for copied option
* @ret rc Return status code
*
* Copies a single option, if present, from the DHCP options block
* into a DHCP packet. The tag for the option may be changed if
* desired; this is required by other parts of the DHCP code.
*
* @c options may specify a single options block, or be left as NULL
* in order to search for the option within all registered options
* blocks.
*/
static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
struct dhcp_option_block *options,
unsigned int tag, unsigned int new_tag ) {
struct dhcp_option *option;
int rc;
option = find_dhcp_option ( options, tag );
if ( option ) {
if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
&option->data,
option->len ) ) != 0 )
return rc;
}
return 0;
}
/**
* Copy options into DHCP packet
*
* @v dhcppkt DHCP packet
* @v options DHCP option block, or NULL
* @v encapsulator Encapsulating option, or zero
* @ret rc Return status code
*
* Copies options with the specified encapsulator from DHCP options
* blocks into a DHCP packet. Most options are copied verbatim.
* Recognised encapsulated options fields are handled as such.
*
* @c options may specify a single options block, or be left as NULL
* in order to copy options from all registered options blocks.
*/
static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
struct dhcp_option_block *options,
unsigned int encapsulator ) {
unsigned int subtag;
unsigned int tag;
int rc;
for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
switch ( tag ) {
case DHCP_EB_ENCAP:
case DHCP_VENDOR_ENCAP:
/* Process encapsulated options field */
if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
options,
tag)) !=0)
return rc;
break;
default:
/* Copy option to reassembled packet */
if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
tag, tag ) ) !=0)
return rc;
break;
};
}
return 0;
}
/**
* Copy options into DHCP packet
*
* @v dhcppkt DHCP packet
* @v options DHCP option block, or NULL
* @ret rc Return status code
*
* Copies options from DHCP options blocks into a DHCP packet. Most
* options are copied verbatim. Recognised encapsulated options
* fields are handled as such.
*
* @c options may specify a single options block, or be left as NULL
* in order to copy options from all registered options blocks.
*/
static int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
struct dhcp_option_block *options ) {
return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
}
/** /**
* Create a DHCP packet * Create a DHCP packet
* *
* @v dhcppkt DHCP packet structure to fill in
* @v netdev Network device * @v netdev Network device
* @v msgtype DHCP message type * @v msgtype DHCP message type
* @v options Initial options to include (or NULL)
* @v data Buffer for DHCP packet * @v data Buffer for DHCP packet
* @v max_len Size of DHCP packet buffer * @v max_len Size of DHCP packet buffer
* @v dhcppkt DHCP packet structure to fill in
* @ret rc Return status code * @ret rc Return status code
* *
* Creates a DHCP packet in the specified buffer, and fills out a @c * Creates a DHCP packet in the specified buffer, and fills out a @c
* dhcp_packet structure that can be passed to * dhcp_packet structure that can be passed to
* set_dhcp_packet_option() or copy_dhcp_packet_options(). * set_dhcp_packet_option() or copy_dhcp_packet_options().
*/ */
static int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype, static int create_dhcp_packet ( struct dhcp_packet *dhcppkt,
void *data, size_t max_len, struct net_device *netdev, uint8_t msgtype,
struct dhcp_packet *dhcppkt ) { struct dhcp_options *options,
void *data, size_t max_len ) {
struct dhcphdr *dhcphdr = data; struct dhcphdr *dhcphdr = data;
size_t options_len;
unsigned int hlen; unsigned int hlen;
int rc; int rc;
/* Sanity check */ /* Sanity check */
if ( max_len < sizeof ( *dhcphdr ) ) options_len = ( options ? options->len : 0 );
if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
return -ENOSPC; return -ENOSPC;
/* Initialise DHCP packet content */ /* Initialise DHCP packet content */
@ -327,180 +175,19 @@ static int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
} }
dhcphdr->hlen = hlen; dhcphdr->hlen = hlen;
memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen ); memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
memcpy ( dhcphdr->options, options, options_len );
/* Initialise DHCP packet structure */ /* Initialise DHCP packet structure and settings interface */
dhcppkt->dhcphdr = dhcphdr; dhcppkt_init ( dhcppkt, NULL, data, max_len );
dhcppkt->max_len = max_len;
init_dhcp_options ( &dhcppkt->options, dhcphdr->options,
( max_len -
offsetof ( typeof ( *dhcphdr ), options ) ) );
/* Set DHCP_MESSAGE_TYPE option */ /* Set DHCP_MESSAGE_TYPE option */
if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE, if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_MESSAGE_TYPE,
&msgtype, &msgtype, sizeof ( msgtype ) ) ) != 0 )
sizeof ( msgtype ) ) ) != 0 )
return rc; return rc;
return 0; return 0;
} }
/**
* Calculate used length of a field containing DHCP options
*
* @v data Field containing DHCP options
* @v max_len Field length
* @ret len Used length (excluding the @c DHCP_END tag)
*/
static size_t dhcp_field_len ( const void *data, size_t max_len ) {
struct dhcp_option_block options;
struct dhcp_option *end;
options.data = ( ( void * ) data );
options.len = max_len;
end = find_dhcp_option ( &options, DHCP_END );
return ( end ? ( ( ( void * ) end ) - data ) : 0 );
}
/**
* Merge field containing DHCP options or string into DHCP options block
*
* @v options DHCP option block
* @v data Field containing DHCP options
* @v max_len Field length
* @v tag DHCP option tag, or 0
*
* If @c tag is non-zero (and the field is not empty), the field will
* be treated as a NUL-terminated string representing the value of the
* specified DHCP option. If @c tag is zero, the field will be
* treated as a block of DHCP options, and simply appended to the
* existing options in the option block.
*
* The caller must ensure that there is enough space in the options
* block to perform the merge.
*/
static void merge_dhcp_field ( struct dhcp_option_block *options,
const void *data, size_t max_len,
unsigned int tag ) {
size_t len;
void *dest;
struct dhcp_option *end;
if ( tag ) {
len = strlen ( data );
if ( len )
set_dhcp_option ( options, tag, data, len );
} else {
len = dhcp_field_len ( data, max_len );
dest = ( options->data + options->len - 1 );
memcpy ( dest, data, len );
options->len += len;
end = ( dest + len );
end->tag = DHCP_END;
}
}
/**
* Parse DHCP packet and construct DHCP options block
*
* @v dhcphdr DHCP packet
* @v len Length of DHCP packet
* @ret options DHCP options block, or NULL
*
* Parses a received DHCP packet and canonicalises its contents into a
* single DHCP options block. The "file" and "sname" fields are
* converted into the corresponding DHCP options (@c
* DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
* these fields are used for option overloading, their options are
* merged in to the options block.
*
* The values of the "yiaddr" and "siaddr" fields will be stored
* within the options block as the magic options @c DHCP_EB_YIADDR and
* @c DHCP_EB_SIADDR.
*
* Note that this call allocates new memory for the constructed DHCP
* options block; it is the responsibility of the caller to eventually
* free this memory.
*/
static struct dhcp_option_block * dhcp_parse ( const struct dhcphdr *dhcphdr,
size_t len ) {
struct dhcp_option_block *options;
size_t options_len;
unsigned int overloading;
/* Sanity check */
if ( len < sizeof ( *dhcphdr ) )
return NULL;
/* Calculate size of resulting concatenated option block:
*
* The "options" field : length of the field minus the DHCP_END tag.
*
* The "file" field : maximum length of the field minus the
* NUL terminator, plus a 2-byte DHCP header or, if used for
* option overloading, the length of the field minus the
* DHCP_END tag.
*
* The "sname" field : as for the "file" field.
*
* 15 bytes for an encapsulated options field to contain the
* value of the "yiaddr" and "siaddr" fields
*
* 1 byte for a final terminating DHCP_END tag.
*/
options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
+ ( sizeof ( dhcphdr->file ) + 1 )
+ ( sizeof ( dhcphdr->sname ) + 1 )
+ 15 /* yiaddr and siaddr */
+ 1 /* DHCP_END tag */ );
/* Allocate empty options block of required size */
options = alloc_dhcp_options ( options_len );
if ( ! options ) {
DBG ( "DHCP could not allocate %zd-byte option block\n",
options_len );
return NULL;
}
/* Merge in "options" field, if this is a DHCP packet */
if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
merge_dhcp_field ( options, dhcphdr->options,
( len -
offsetof ( typeof (*dhcphdr), options ) ),
0 /* Always contains options */ );
}
/* Identify overloaded fields */
overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
/* Merge in "file" and "sname" fields */
merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
0 : DHCP_BOOTFILE_NAME ) );
merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
0 : DHCP_TFTP_SERVER_NAME ) );
/* Set magic options for "yiaddr" and "siaddr", if present */
if ( dhcphdr->yiaddr.s_addr ) {
set_dhcp_option ( options, DHCP_EB_YIADDR,
&dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
}
if ( dhcphdr->siaddr.s_addr ) {
set_dhcp_option ( options, DHCP_EB_SIADDR,
&dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
}
assert ( options->len <= options->max_len );
return options;
}
/****************************************************************************
*
* Whole-packet construction
*
*/
/** DHCP network device descriptor */ /** DHCP network device descriptor */
struct dhcp_netdev_desc { struct dhcp_netdev_desc {
/** Bus type ID */ /** Bus type ID */
@ -532,18 +219,18 @@ struct dhcp_client_uuid {
/** /**
* Create DHCP request * Create DHCP request
* *
* @v dhcppkt DHCP packet structure to fill in
* @v netdev Network device * @v netdev Network device
* @v msgtype DHCP message type * @v msgtype DHCP message type
* @v options DHCP server response options, or NULL * @v offer_settings Settings received in DHCPOFFER, or NULL
* @v data Buffer for DHCP packet * @v data Buffer for DHCP packet
* @v max_len Size of DHCP packet buffer * @v max_len Size of DHCP packet buffer
* @v dhcppkt DHCP packet structure to fill in
* @ret rc Return status code * @ret rc Return status code
*/ */
int create_dhcp_request ( struct net_device *netdev, int msgtype, int create_dhcp_request ( struct dhcp_packet *dhcppkt,
struct dhcp_option_block *options, struct net_device *netdev, int msgtype,
void *data, size_t max_len, struct settings *offer_settings,
struct dhcp_packet *dhcppkt ) { void *data, size_t max_len ) {
struct device_description *desc = &netdev->dev->desc; struct device_description *desc = &netdev->dev->desc;
struct dhcp_netdev_desc dhcp_desc; struct dhcp_netdev_desc dhcp_desc;
struct dhcp_client_id client_id; struct dhcp_client_id client_id;
@ -553,33 +240,27 @@ int create_dhcp_request ( struct net_device *netdev, int msgtype,
int rc; int rc;
/* Create DHCP packet */ /* Create DHCP packet */
if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len, if ( ( rc = create_dhcp_packet ( dhcppkt, netdev, msgtype,
dhcppkt ) ) != 0 ) { &dhcp_request_options, data,
max_len ) ) != 0 ) {
DBG ( "DHCP could not create DHCP packet: %s\n", DBG ( "DHCP could not create DHCP packet: %s\n",
strerror ( rc ) ); strerror ( rc ) );
return rc; return rc;
} }
/* Copy in options common to all requests */
if ( ( rc = copy_dhcp_packet_options ( dhcppkt,
&dhcp_request_options )) !=0 ){
DBG ( "DHCP could not set common DHCP options: %s\n",
strerror ( rc ) );
return rc;
}
/* Copy any required options from previous server repsonse */ /* Copy any required options from previous server repsonse */
if ( options ) { if ( offer_settings ) {
if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options, if ( ( rc = copy_setting ( &dhcppkt->settings,
DHCP_SERVER_IDENTIFIER, DHCP_SERVER_IDENTIFIER,
DHCP_SERVER_IDENTIFIER ) ) != 0 ) { offer_settings,
DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
DBG ( "DHCP could not set server identifier " DBG ( "DHCP could not set server identifier "
"option: %s\n", strerror ( rc ) ); "option: %s\n", strerror ( rc ) );
return rc; return rc;
} }
if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options, if ( ( rc = copy_setting ( &dhcppkt->settings, DHCP_EB_YIADDR,
DHCP_EB_YIADDR, offer_settings,
DHCP_REQUESTED_ADDRESS ) ) != 0 ) { DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
DBG ( "DHCP could not set requested address " DBG ( "DHCP could not set requested address "
"option: %s\n", strerror ( rc ) ); "option: %s\n", strerror ( rc ) );
return rc; return rc;
@ -588,9 +269,8 @@ int create_dhcp_request ( struct net_device *netdev, int msgtype,
/* Add options to identify the feature list */ /* Add options to identify the feature list */
dhcp_features_len = ( dhcp_features_end - dhcp_features ); dhcp_features_len = ( dhcp_features_end - dhcp_features );
if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_ENCAP, if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_EB_ENCAP,
dhcp_features, dhcp_features, dhcp_features_len ) ) !=0 ){
dhcp_features_len ) ) != 0 ) {
DBG ( "DHCP could not set features list option: %s\n", DBG ( "DHCP could not set features list option: %s\n",
strerror ( rc ) ); strerror ( rc ) );
return rc; return rc;
@ -600,9 +280,8 @@ int create_dhcp_request ( struct net_device *netdev, int msgtype,
dhcp_desc.type = desc->bus_type; dhcp_desc.type = desc->bus_type;
dhcp_desc.vendor = htons ( desc->vendor ); dhcp_desc.vendor = htons ( desc->vendor );
dhcp_desc.device = htons ( desc->device ); dhcp_desc.device = htons ( desc->device );
if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_BUS_ID, if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_EB_BUS_ID,
&dhcp_desc, &dhcp_desc, sizeof ( dhcp_desc ) ) ) !=0 ){
sizeof ( dhcp_desc ) ) ) != 0 ) {
DBG ( "DHCP could not set bus ID option: %s\n", DBG ( "DHCP could not set bus ID option: %s\n",
strerror ( rc ) ); strerror ( rc ) );
return rc; return rc;
@ -615,9 +294,8 @@ int create_dhcp_request ( struct net_device *netdev, int msgtype,
ll_addr_len = netdev->ll_protocol->ll_addr_len; ll_addr_len = netdev->ll_protocol->ll_addr_len;
assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) ); assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len ); memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_CLIENT_ID, if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_CLIENT_ID,
&client_id, &client_id, ( ll_addr_len + 1 ) ) ) != 0 ){
( ll_addr_len + 1 ) ) ) != 0 ) {
DBG ( "DHCP could not set client ID: %s\n", DBG ( "DHCP could not set client ID: %s\n",
strerror ( rc ) ); strerror ( rc ) );
return rc; return rc;
@ -626,9 +304,9 @@ int create_dhcp_request ( struct net_device *netdev, int msgtype,
/* Add client UUID, if we have one. Required for PXE. */ /* Add client UUID, if we have one. Required for PXE. */
client_uuid.type = DHCP_CLIENT_UUID_TYPE; client_uuid.type = DHCP_CLIENT_UUID_TYPE;
if ( ( rc = get_uuid ( &client_uuid.uuid ) ) == 0 ) { if ( ( rc = get_uuid ( &client_uuid.uuid ) ) == 0 ) {
if ( ( rc = set_dhcp_packet_option ( dhcppkt, if ( ( rc = store_setting ( &dhcppkt->settings,
DHCP_CLIENT_UUID, &client_uuid, DHCP_CLIENT_UUID, &client_uuid,
sizeof ( client_uuid ) ) ) != 0 ) { sizeof ( client_uuid ) ) ) != 0 ) {
DBG ( "DHCP could not set client UUID: %s\n", DBG ( "DHCP could not set client UUID: %s\n",
strerror ( rc ) ); strerror ( rc ) );
return rc; return rc;
@ -641,34 +319,86 @@ int create_dhcp_request ( struct net_device *netdev, int msgtype,
/** /**
* Create DHCP response * Create DHCP response
* *
* @v dhcppkt DHCP packet structure to fill in
* @v netdev Network device * @v netdev Network device
* @v msgtype DHCP message type * @v msgtype DHCP message type
* @v options DHCP options, or NULL * @v settings Settings to include, or NULL
* @v data Buffer for DHCP packet * @v data Buffer for DHCP packet
* @v max_len Size of DHCP packet buffer * @v max_len Size of DHCP packet buffer
* @v dhcppkt DHCP packet structure to fill in
* @ret rc Return status code * @ret rc Return status code
*/ */
int create_dhcp_response ( struct net_device *netdev, int msgtype, int create_dhcp_response ( struct dhcp_packet *dhcppkt,
struct dhcp_option_block *options, struct net_device *netdev, int msgtype,
void *data, size_t max_len, struct settings *settings,
struct dhcp_packet *dhcppkt ) { void *data, size_t max_len ) {
int rc; int rc;
/* Create packet and copy in options */ /* Create packet and copy in options */
if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len, if ( ( rc = create_dhcp_packet ( dhcppkt, netdev, msgtype, NULL,
dhcppkt ) ) != 0 ) { data, max_len ) ) != 0 )
DBG ( " failed to build packet" );
return rc; return rc;
} if ( ( rc = copy_settings ( &dhcppkt->settings, settings ) ) != 0 )
if ( ( rc = copy_dhcp_packet_options ( dhcppkt, options ) ) != 0 ) {
DBG ( " failed to copy options" );
return rc; return rc;
}
return 0; return 0;
} }
/****************************************************************************
*
* DHCP packets contained in I/O buffers
*
*/
/** A DHCP packet contained in an I/O buffer */
struct dhcp_iobuf_packet {
/** Reference counter */
struct refcnt refcnt;
/** DHCP packet */
struct dhcp_packet dhcppkt;
/** Containing I/O buffer */
struct io_buffer *iobuf;
};
/**
* Free DHCP packet contained in an I/O buffer
*
* @v refcnt Reference counter
*/
static void dhcpiob_free ( struct refcnt *refcnt ) {
struct dhcp_iobuf_packet *dhcpiob =
container_of ( refcnt, struct dhcp_iobuf_packet, refcnt );
free_iob ( dhcpiob->iobuf );
free ( dhcpiob );
}
/**
* Create DHCP packet from I/O buffer
*
* @v iobuf I/O buffer
* @ret dhcpiob DHCP packet contained in I/O buffer
*
* This function takes ownership of the I/O buffer. Future accesses
* must be via the @c dhcpiob data structure.
*/
static struct dhcp_iobuf_packet * dhcpiob_create ( struct io_buffer *iobuf ) {
struct dhcp_iobuf_packet *dhcpiob;
dhcpiob = zalloc ( sizeof ( *dhcpiob ) );
if ( dhcpiob ) {
dhcpiob->refcnt.free = dhcpiob_free;
dhcpiob->iobuf = iobuf;
dhcppkt_init ( &dhcpiob->dhcppkt, &dhcpiob->refcnt,
iobuf->data, iob_len ( iobuf ) );
}
return dhcpiob;
}
static void dhcpiob_put ( struct dhcp_iobuf_packet *dhcpiob ) {
if ( dhcpiob )
ref_put ( &dhcpiob->refcnt );
}
/**************************************************************************** /****************************************************************************
* *
* DHCP to UDP interface * DHCP to UDP interface
@ -686,9 +416,6 @@ struct dhcp_session {
/** Network device being configured */ /** Network device being configured */
struct net_device *netdev; struct net_device *netdev;
/** Option block registration routine */
int ( * register_options ) ( struct net_device *netdev,
struct dhcp_option_block *options );
/** State of the session /** State of the session
* *
@ -696,10 +423,10 @@ struct dhcp_session {
* (e.g. @c DHCPDISCOVER). * (e.g. @c DHCPDISCOVER).
*/ */
int state; int state;
/** Options obtained from DHCP server */ /** Response obtained from DHCP server */
struct dhcp_option_block *options; struct dhcp_iobuf_packet *response;
/** Options obtained from ProxyDHCP server */ /** Response obtained from ProxyDHCP server */
struct dhcp_option_block *proxy_options; struct dhcp_iobuf_packet *proxy_response;
/** Retransmission timer */ /** Retransmission timer */
struct retry_timer timer; struct retry_timer timer;
/** Session start time (in ticks) */ /** Session start time (in ticks) */
@ -716,8 +443,8 @@ static void dhcp_free ( struct refcnt *refcnt ) {
container_of ( refcnt, struct dhcp_session, refcnt ); container_of ( refcnt, struct dhcp_session, refcnt );
netdev_put ( dhcp->netdev ); netdev_put ( dhcp->netdev );
dhcpopt_put ( dhcp->options ); dhcpiob_put ( dhcp->response );
dhcpopt_put ( dhcp->proxy_options ); dhcpiob_put ( dhcp->proxy_response );
free ( dhcp ); free ( dhcp );
} }
@ -741,6 +468,31 @@ static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
job_done ( &dhcp->job, rc ); job_done ( &dhcp->job, rc );
} }
/**
* Register options received via DHCP
*
* @v dhcp DHCP session
* @ret rc Return status code
*/
static int dhcp_register_settings ( struct dhcp_session *dhcp ) {
struct settings *settings;
struct settings *parent;
int rc;
if ( dhcp->proxy_response ) {
settings = &dhcp->proxy_response->dhcppkt.settings;
if ( ( rc = register_settings ( settings, NULL ) ) != 0 )
return rc;
}
settings = &dhcp->response->dhcppkt.settings;
parent = netdev_settings ( dhcp->netdev );
if ( ( rc = register_settings ( settings, parent ) ) != 0 )
return rc;
return 0;
}
/**************************************************************************** /****************************************************************************
* *
* Data transfer interface * Data transfer interface
@ -757,6 +509,7 @@ static int dhcp_send_request ( struct dhcp_session *dhcp ) {
struct xfer_metadata meta = { struct xfer_metadata meta = {
.netdev = dhcp->netdev, .netdev = dhcp->netdev,
}; };
struct settings *offer_settings = NULL;
struct io_buffer *iobuf; struct io_buffer *iobuf;
struct dhcp_packet dhcppkt; struct dhcp_packet dhcppkt;
int rc; int rc;
@ -778,10 +531,11 @@ static int dhcp_send_request ( struct dhcp_session *dhcp ) {
return -ENOMEM; return -ENOMEM;
/* Create DHCP packet in temporary buffer */ /* Create DHCP packet in temporary buffer */
if ( ( rc = create_dhcp_request ( dhcp->netdev, dhcp->state, if ( dhcp->response )
dhcp->options, iobuf->data, offer_settings = &dhcp->response->dhcppkt.settings;
iob_tailroom ( iobuf ), if ( ( rc = create_dhcp_request ( &dhcppkt, dhcp->netdev, dhcp->state,
&dhcppkt ) ) != 0 ) { offer_settings, iobuf->data,
iob_tailroom ( iobuf ) ) ) != 0 ) {
DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n", DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
dhcp, strerror ( rc ) ); dhcp, strerror ( rc ) );
goto done; goto done;
@ -828,36 +582,41 @@ static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
* @v len Length of received data * @v len Length of received data
* @ret rc Return status code * @ret rc Return status code
*/ */
static int dhcp_deliver_raw ( struct xfer_interface *xfer, static int dhcp_deliver_iob ( struct xfer_interface *xfer,
const void *data, size_t len ) { struct io_buffer *iobuf,
struct xfer_metadata *meta __unused ) {
struct dhcp_session *dhcp = struct dhcp_session *dhcp =
container_of ( xfer, struct dhcp_session, xfer ); container_of ( xfer, struct dhcp_session, xfer );
const struct dhcphdr *dhcphdr = data; struct dhcp_iobuf_packet *response;
struct dhcp_option_block *options; struct dhcp_iobuf_packet **store_response;
struct dhcp_option_block **store_options; struct dhcphdr *dhcphdr;
struct settings *settings;
unsigned int msgtype; unsigned int msgtype;
unsigned long elapsed; unsigned long elapsed;
int is_proxy; int is_proxy;
int ignore_proxy; int ignore_proxy;
int rc;
/* Convert packet into a DHCP-packet-in-iobuf */
response = dhcpiob_create ( iobuf );
if ( ! response ) {
DBGC ( dhcp, "DHCP %p could not store DHCP packet\n", dhcp );
return -ENOMEM;
}
dhcphdr = response->dhcppkt.dhcphdr;
settings = &response->dhcppkt.settings;
/* Check for matching transaction ID */ /* Check for matching transaction ID */
if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) { if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, " DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
"got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ), "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
ntohl ( dhcp_xid ( dhcp->netdev ) ) ); ntohl ( dhcp_xid ( dhcp->netdev ) ) );
return 0; goto out_discard;
}; };
/* Parse packet and create options structure */
options = dhcp_parse ( dhcphdr, len );
if ( ! options ) {
DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
return -EINVAL;
}
/* Determine and verify message type */ /* Determine and verify message type */
is_proxy = ( dhcphdr->yiaddr.s_addr == 0 ); is_proxy = ( dhcphdr->yiaddr.s_addr == 0 );
msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE ); msgtype = fetch_uintz_setting ( settings, DHCP_MESSAGE_TYPE );
DBGC ( dhcp, "DHCP %p received %s%s\n", dhcp, DBGC ( dhcp, "DHCP %p received %s%s\n", dhcp,
( is_proxy ? "Proxy" : "" ), dhcp_msgtype_name ( msgtype ) ); ( is_proxy ? "Proxy" : "" ), dhcp_msgtype_name ( msgtype ) );
if ( ( ( dhcp->state != DHCPDISCOVER ) || ( msgtype != DHCPOFFER ) ) && if ( ( ( dhcp->state != DHCPDISCOVER ) || ( msgtype != DHCPOFFER ) ) &&
@ -872,25 +631,26 @@ static int dhcp_deliver_raw ( struct xfer_interface *xfer,
* options have equal or higher priority than the * options have equal or higher priority than the
* currently-stored options. * currently-stored options.
*/ */
store_options = ( is_proxy ? &dhcp->proxy_options : &dhcp->options ); store_response = ( is_proxy ? &dhcp->proxy_response : &dhcp->response);
if ( ( ! *store_options ) || if ( ( ! *store_response ) ||
( find_dhcp_num_option ( options, DHCP_EB_PRIORITY ) >= ( fetch_uintz_setting ( settings, DHCP_EB_PRIORITY ) >=
find_dhcp_num_option ( *store_options, DHCP_EB_PRIORITY ) ) ) { fetch_uintz_setting ( &(*store_response)->dhcppkt.settings,
dhcpopt_put ( *store_options ); DHCP_EB_PRIORITY ) ) ) {
*store_options = options; dhcpiob_put ( *store_response );
*store_response = response;
} else { } else {
dhcpopt_put ( options ); dhcpiob_put ( response );
} }
/* If we don't yet have a standard DHCP response (i.e. one /* If we don't yet have a standard DHCP response (i.e. one
* with an IP address), then just leave the timer running. * with an IP address), then just leave the timer running.
*/ */
if ( ! dhcp->options ) if ( ! dhcp->response )
goto out; goto out;
/* Handle DHCP response */ /* Handle DHCP response */
ignore_proxy = find_dhcp_num_option ( dhcp->options, ignore_proxy = fetch_uintz_setting ( &dhcp->response->dhcppkt.settings,
DHCP_EB_NO_PROXYDHCP ); DHCP_EB_NO_PROXYDHCP );
switch ( dhcp->state ) { switch ( dhcp->state ) {
case DHCPDISCOVER: case DHCPDISCOVER:
/* If we have allowed sufficient time for ProxyDHCP /* If we have allowed sufficient time for ProxyDHCP
@ -905,11 +665,14 @@ static int dhcp_deliver_raw ( struct xfer_interface *xfer,
break; break;
case DHCPREQUEST: case DHCPREQUEST:
/* DHCP finished; register options and exit */ /* DHCP finished; register options and exit */
if ( dhcp->proxy_options && ( ! ignore_proxy ) ) { if ( ignore_proxy && dhcp->proxy_response ) {
dhcp->register_options ( dhcp->netdev, dhcpiob_put ( dhcp->proxy_response );
dhcp->proxy_options ); dhcp->proxy_response = NULL;
}
if ( ( rc = dhcp_register_settings ( dhcp ) ) != 0 ) {
dhcp_finished ( dhcp, rc );
break;
} }
dhcp->register_options ( dhcp->netdev, dhcp->options );
dhcp_finished ( dhcp, 0 ); dhcp_finished ( dhcp, 0 );
break; break;
default: default:
@ -920,7 +683,7 @@ static int dhcp_deliver_raw ( struct xfer_interface *xfer,
return 0; return 0;
out_discard: out_discard:
dhcpopt_put ( options ); dhcpiob_put ( response );
return 0; return 0;
} }
@ -930,8 +693,8 @@ static struct xfer_interface_operations dhcp_xfer_operations = {
.vredirect = xfer_vopen, .vredirect = xfer_vopen,
.window = unlimited_xfer_window, .window = unlimited_xfer_window,
.alloc_iob = default_xfer_alloc_iob, .alloc_iob = default_xfer_alloc_iob,
.deliver_iob = xfer_deliver_as_raw, .deliver_iob = dhcp_deliver_iob,
.deliver_raw = dhcp_deliver_raw, .deliver_raw = xfer_deliver_as_iob,
}; };
/**************************************************************************** /****************************************************************************
@ -978,9 +741,7 @@ static struct job_interface_operations dhcp_job_operations = {
* register_options() routine will be called with the acquired * register_options() routine will be called with the acquired
* options. * options.
*/ */
int start_dhcp ( struct job_interface *job, struct net_device *netdev, int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
int ( * register_options ) ( struct net_device *netdev,
struct dhcp_option_block * ) ) {
static struct sockaddr_in server = { static struct sockaddr_in server = {
.sin_family = AF_INET, .sin_family = AF_INET,
.sin_addr.s_addr = INADDR_BROADCAST, .sin_addr.s_addr = INADDR_BROADCAST,
@ -1001,7 +762,6 @@ int start_dhcp ( struct job_interface *job, struct net_device *netdev,
job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt ); job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt ); xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
dhcp->netdev = netdev_get ( netdev ); dhcp->netdev = netdev_get ( netdev );
dhcp->register_options = register_options;
dhcp->timer.expired = dhcp_timer_expired; dhcp->timer.expired = dhcp_timer_expired;
dhcp->state = DHCPDISCOVER; dhcp->state = DHCPDISCOVER;
dhcp->start = currticks(); dhcp->start = currticks();

View File

@ -32,16 +32,8 @@
* *
*/ */
static int dhcp_success ( struct net_device *netdev __unused,
struct dhcp_option_block *options ) {
DBGC ( options, "DHCP client registering options %p\n", options );
register_dhcp_options ( options );
return 0;
}
int dhcp ( struct net_device *netdev ) { int dhcp ( struct net_device *netdev ) {
struct dhcp_option_block *options; struct settings *settings;
struct dhcp_option_block *tmp;
int rc; int rc;
/* Check we can open the interface first */ /* Check we can open the interface first */
@ -49,18 +41,13 @@ int dhcp ( struct net_device *netdev ) {
return rc; return rc;
/* Unregister any option blocks acquired via DHCP */ /* Unregister any option blocks acquired via DHCP */
list_for_each_entry_safe ( options, tmp, &dhcp_option_blocks, list ) { settings = find_child_settings ( netdev_settings ( netdev ), "dhcp" );
/* Skip static option blocks (e.g. from NVS) */ if ( settings )
if ( find_dhcp_option ( options, DHCP_MESSAGE_TYPE ) ) { unregister_settings ( settings );
DBGC ( options, "DHCP client unregistering options "
"%p\n", options );
unregister_dhcp_options ( options );
}
}
/* Perform DHCP */ /* Perform DHCP */
printf ( "DHCP (%s %s)", netdev->name, netdev_hwaddr ( netdev ) ); printf ( "DHCP (%s %s)", netdev->name, netdev_hwaddr ( netdev ) );
if ( ( rc = start_dhcp ( &monojob, netdev, dhcp_success ) ) == 0 ) if ( ( rc = start_dhcp ( &monojob, netdev ) ) == 0 )
rc = monojob_wait ( "" ); rc = monojob_wait ( "" );
return rc; return rc;