#include <stdint.h>
#include <string.h>
-#include <stdio.h>
#include <errno.h>
+#include <assert.h>
#include <gpxe/uaccess.h>
-#include <gpxe/uuid.h>
#include <realmode.h>
#include <pnpbios.h>
#include <smbios.h>
/** Checksum */
uint8_t checksum;
/** Length */
- uint8_t length;
+ uint8_t len;
/** Major version */
uint8_t major;
/** Minor version */
/** DMI checksum */
uint8_t dmi_checksum;
/** Structure table length */
- uint16_t smbios_length;
+ uint16_t smbios_len;
/** Structure table address */
physaddr_t smbios_address;
/** Number of SMBIOS structures */
/** Start of SMBIOS structures */
userptr_t address;
/** Length of SMBIOS structures */
- size_t length;
+ size_t len;
/** Number of SMBIOS structures */
unsigned int count;
};
-/**
- * SMBIOS strings descriptor
- *
- * This is returned as part of the search for an SMBIOS structure, and
- * contains the information needed for extracting the strings within
- * the "unformatted" portion of the structure.
- */
-struct smbios_strings {
- /** Start of strings data */
- userptr_t data;
- /** Length of strings data */
- size_t length;
+/** SMBIOS entry point descriptor */
+static struct smbios smbios = {
+ .address = UNULL,
};
/**
* Find SMBIOS
*
- * @ret smbios SMBIOS entry point descriptor, or NULL if not found
+ * @ret rc Return status code
*/
-static struct smbios * find_smbios ( void ) {
- static struct smbios smbios = {
- .address = UNULL,
- };
+static int find_smbios ( void ) {
union {
struct smbios_entry entry;
uint8_t bytes[256]; /* 256 is maximum length possible */
} u;
- unsigned int offset;
+ static unsigned int offset = 0;
size_t len;
unsigned int i;
uint8_t sum;
- /* Return cached result if available */
+ /* Return cached result if avaiable */
if ( smbios.address != UNULL )
- return &smbios;
+ return 0;
/* Try to find SMBIOS */
- for ( offset = 0 ; offset < 0x10000 ; offset += 0x10 ) {
+ for ( ; offset < 0x10000 ; offset += 0x10 ) {
/* Read start of header and verify signature */
copy_from_real ( &u.entry, BIOS_SEG, offset,
continue;
/* Read whole header and verify checksum */
- len = u.entry.length;
+ len = u.entry.len;
copy_from_real ( &u.bytes, BIOS_SEG, offset, len );
for ( i = 0 , sum = 0 ; i < len ; i++ ) {
sum += u.bytes[i];
DBG ( "Found SMBIOS entry point at %04x:%04x\n",
BIOS_SEG, offset );
smbios.address = phys_to_user ( u.entry.smbios_address );
- smbios.length = u.entry.smbios_length;
+ smbios.len = u.entry.smbios_len;
smbios.count = u.entry.smbios_count;
- return &smbios;
+ return 0;
}
DBG ( "No SMBIOS found\n" );
- return NULL;
+ return -ENODEV;
}
/**
* Find SMBIOS strings terminator
*
- * @v smbios SMBIOS entry point descriptor
* @v offset Offset to start of strings
* @ret offset Offset to strings terminator, or 0 if not found
*/
-static size_t find_strings_terminator ( struct smbios *smbios,
- size_t offset ) {
- size_t max_offset = ( smbios->length - 2 );
+static size_t find_strings_terminator ( size_t offset ) {
+ size_t max_offset = ( smbios.len - 2 );
uint16_t nulnul;
for ( ; offset <= max_offset ; offset++ ) {
- copy_from_user ( &nulnul, smbios->address, offset, 2 );
+ copy_from_user ( &nulnul, smbios.address, offset, 2 );
if ( nulnul == 0 )
return ( offset + 1 );
}
* Find specific structure type within SMBIOS
*
* @v type Structure type to search for
- * @v structure Buffer to fill in with structure
- * @v length Length of buffer
- * @v strings Strings descriptor to fill in, or NULL
+ * @v structure SMBIOS structure descriptor to fill in
* @ret rc Return status code
*/
-int find_smbios_structure ( unsigned int type, void *structure,
- size_t length, struct smbios_strings *strings ) {
- struct smbios *smbios;
- struct smbios_header header;
- struct smbios_strings temp_strings;
+int find_smbios_structure ( unsigned int type,
+ struct smbios_structure *structure ) {
unsigned int count = 0;
size_t offset = 0;
size_t strings_offset;
size_t terminator_offset;
+ int rc;
- /* Locate SMBIOS entry point */
- if ( ! ( smbios = find_smbios() ) )
- return -ENOENT;
-
- /* Ensure that we have a usable strings descriptor buffer */
- if ( ! strings )
- strings = &temp_strings;
+ /* Find SMBIOS */
+ if ( ( rc = find_smbios() ) != 0 )
+ return rc;
/* Scan through list of structures */
- while ( ( ( offset + sizeof ( header ) ) < smbios->length ) &&
- ( count < smbios->count ) ) {
+ while ( ( ( offset + sizeof ( structure->header ) ) < smbios.len )
+ && ( count < smbios.count ) ) {
/* Read next SMBIOS structure header */
- copy_from_user ( &header, smbios->address, offset,
- sizeof ( header ) );
+ copy_from_user ( &structure->header, smbios.address, offset,
+ sizeof ( structure->header ) );
/* Determine start and extent of strings block */
- strings_offset = ( offset + header.length );
- if ( strings_offset > smbios->length ) {
+ strings_offset = ( offset + structure->header.len );
+ if ( strings_offset > smbios.len ) {
DBG ( "SMBIOS structure at offset %zx with length "
"%x extends beyond SMBIOS\n", offset,
- header.length );
+ structure->header.len );
return -ENOENT;
}
- terminator_offset =
- find_strings_terminator ( smbios, strings_offset );
+ terminator_offset = find_strings_terminator ( strings_offset );
if ( ! terminator_offset ) {
DBG ( "SMBIOS structure at offset %zx has "
"unterminated strings section\n", offset );
return -ENOENT;
}
- strings->data = userptr_add ( smbios->address,
- strings_offset );
- strings->length = ( terminator_offset - strings_offset );
+ structure->strings_len = ( terminator_offset - strings_offset);
- DBG ( "SMBIOS structure at offset %zx has type %d, "
- "length %x, strings length %zx\n",
- offset, header.type, header.length, strings->length );
+ DBG ( "SMBIOS structure at offset %zx has type %d, length %x, "
+ "strings length %zx\n", offset, structure->header.type,
+ structure->header.len, structure->strings_len );
/* If this is the structure we want, return */
- if ( header.type == type ) {
- if ( length > header.length )
- length = header.length;
- copy_from_user ( structure, smbios->address,
- offset, length );
+ if ( structure->header.type == type ) {
+ structure->offset = offset;
return 0;
}
}
/**
+ * Copy SMBIOS structure
+ *
+ * @v structure SMBIOS structure descriptor
+ * @v data Buffer to hold SMBIOS structure
+ * @v len Length of buffer
+ * @ret rc Return status code
+ */
+int read_smbios_structure ( struct smbios_structure *structure,
+ void *data, size_t len ) {
+
+ assert ( smbios.address != UNULL );
+
+ if ( len > structure->header.len )
+ len = structure->header.len;
+ copy_from_user ( data, smbios.address, structure->offset, len );
+ return 0;
+}
+
+/**
* Find indexed string within SMBIOS structure
*
- * @v strings SMBIOS strings descriptor
+ * @v structure SMBIOS structure descriptor
* @v index String index
- * @v buffer Buffer for string
- * @v length Length of string buffer
- * @ret rc Return status code
+ * @v data Buffer for string
+ * @v len Length of string buffer
+ * @ret rc Length of string, or negative error
*/
-int find_smbios_string ( struct smbios_strings *strings, unsigned int index,
- char *buffer, size_t length ) {
- size_t offset = 0;
+int read_smbios_string ( struct smbios_structure *structure,
+ unsigned int index, void *data, size_t len ) {
+ size_t strings_start = ( structure->offset + structure->header.len );
+ size_t strings_end = ( strings_start + structure->strings_len );
+ size_t offset;
size_t string_len;
- /* Zero buffer. This ensures that a valid NUL terminator is
- * always present (unless length==0).
- */
- memset ( buffer, 0, length );
-
+ assert ( smbios.address != UNULL );
+
/* String numbers start at 1 (0 is used to indicate "no string") */
if ( ! index )
- return 0;
+ return -ENOENT;
- while ( offset < strings->length ) {
+ for ( offset = strings_start ; offset < strings_end ;
+ offset += ( string_len + 1 ) ) {
/* Get string length. This is known safe, since the
* smbios_strings struct is constructed so as to
* always end on a string boundary.
*/
- string_len = strlen_user ( strings->data, offset );
+ string_len = strlen_user ( smbios.address,
+ ( structure->offset + offset ) );
if ( --index == 0 ) {
/* Copy string, truncating as necessary. */
- if ( string_len >= length )
- string_len = ( length - 1 );
- copy_from_user ( buffer, strings->data,
- offset, string_len );
- return 0;
+ if ( len > string_len )
+ len = string_len;
+ copy_from_user ( data, smbios.address,
+ ( structure->offset + offset ), len );
+ return string_len;
}
- offset += ( string_len + 1 );
}
DBG ( "SMBIOS string index %d not found\n", index );
return -ENOENT;
}
-
-/**
- * Get UUID from SMBIOS
- *
- * @v uuid UUID to fill in
- * @ret rc Return status code
- */
-int smbios_get_uuid ( union uuid *uuid ) {
- struct smbios_system_information sysinfo;
- int rc;
-
- if ( ( rc = find_smbios_structure ( SMBIOS_TYPE_SYSTEM_INFORMATION,
- &sysinfo, sizeof ( sysinfo ),
- NULL ) ) != 0 )
- return rc;
-
- memcpy ( uuid, sysinfo.uuid, sizeof ( *uuid ) );
- DBG ( "SMBIOS found UUID %s\n", uuid_ntoa ( uuid ) );
-
- return 0;
-}
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 );
+/** DHCP network device descriptor */
+struct dhcp_netdev_desc {
+ /** Bus type ID */
+ uint8_t type;
+ /** Vendor ID */
+ uint16_t vendor;
+ /** Device ID */
+ uint16_t device;
+} __attribute__ (( packed ));
+
+/** DHCP client identifier */
+struct dhcp_client_id {
+ /** Link-layer protocol */
+ uint8_t ll_proto;
+ /** Link-layer address */
+ uint8_t ll_addr[MAX_LL_ADDR_LEN];
+} __attribute__ (( packed ));
+
+/** DHCP client UUID */
+struct dhcp_client_uuid {
+ /** Identifier type */
+ uint8_t type;
+ /** UUID */
+ union uuid uuid;
+} __attribute__ (( packed ));
+
+#define DHCP_CLIENT_UUID_TYPE 0
+
/**
* Name a DHCP packet type
*
return xid;
}
-/**
- * Create a DHCP packet
- *
- * @v dhcppkt DHCP packet structure to fill in
- * @v netdev Network device
- * @v msgtype DHCP message type
- * @v options Initial options to include (or NULL)
- * @v data Buffer for DHCP packet
- * @v max_len Size of DHCP packet buffer
- * @ret rc Return status code
- *
- * Creates a DHCP packet in the specified buffer, and fills out a @c
- * dhcp_packet structure that can be passed to
- * set_dhcp_packet_option() or copy_dhcp_packet_options().
- */
-int create_dhcp_packet ( struct dhcp_packet *dhcppkt,
- struct net_device *netdev, uint8_t msgtype,
- struct dhcp_options *options,
- void *data, size_t max_len ) {
- struct dhcphdr *dhcphdr = data;
- size_t options_len;
- unsigned int hlen;
- int rc;
-
- /* Sanity check */
- options_len = ( options ? options->len : 0 );
- if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
- return -ENOSPC;
-
- /* Initialise DHCP packet content */
- memset ( dhcphdr, 0, max_len );
- dhcphdr->xid = dhcp_xid ( netdev );
- dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
- dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
- dhcphdr->op = dhcp_op[msgtype];
- /* If hardware length exceeds the chaddr field length, don't
- * use the chaddr field. This is as per RFC4390.
- */
- hlen = netdev->ll_protocol->ll_addr_len;
- if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
- hlen = 0;
- dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
- }
- dhcphdr->hlen = hlen;
- memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
- memcpy ( dhcphdr->options, options->data, options_len );
-
- /* Initialise DHCP packet structure */
- memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
- dhcppkt_init ( dhcppkt, data, max_len );
-
- /* Set DHCP_MESSAGE_TYPE option */
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
- &msgtype, sizeof ( msgtype ) ) ) != 0 )
- return rc;
-
- return 0;
-}
-
-/** DHCP network device descriptor */
-struct dhcp_netdev_desc {
- /** Bus type ID */
- uint8_t type;
- /** Vendor ID */
- uint16_t vendor;
- /** Device ID */
- uint16_t device;
-} __attribute__ (( packed ));
-
-/** DHCP client identifier */
-struct dhcp_client_id {
- /** Link-layer protocol */
- uint8_t ll_proto;
- /** Link-layer address */
- uint8_t ll_addr[MAX_LL_ADDR_LEN];
-} __attribute__ (( packed ));
-
-/** DHCP client UUID */
-struct dhcp_client_uuid {
- /** Identifier type */
- uint8_t type;
- /** UUID */
- union uuid uuid;
-} __attribute__ (( packed ));
-
-#define DHCP_CLIENT_UUID_TYPE 0
-
-/**
- * Create DHCP request packet
- *
- * @v dhcppkt DHCP packet structure to fill in
- * @v netdev Network device
- * @v dhcpoffer DHCPOFFER packet received from server
- * @v data Buffer for DHCP packet
- * @v max_len Size of DHCP packet buffer
- * @ret rc Return status code
- */
-int create_dhcp_request ( struct dhcp_packet *dhcppkt,
- struct net_device *netdev,
- struct dhcp_packet *dhcpoffer,
- void *data, size_t max_len ) {
- struct device_description *desc = &netdev->dev->desc;
- struct dhcp_netdev_desc dhcp_desc;
- struct dhcp_client_id client_id;
- struct dhcp_client_uuid client_uuid;
- unsigned int msgtype;
- size_t dhcp_features_len;
- size_t ll_addr_len;
- int rc;
-
- /* Create DHCP packet */
- msgtype = ( dhcpoffer ? DHCPREQUEST : DHCPDISCOVER );
- if ( ( rc = create_dhcp_packet ( dhcppkt, netdev, msgtype,
- &dhcp_request_options, data,
- max_len ) ) != 0 ) {
- DBG ( "DHCP could not create DHCP packet: %s\n",
- strerror ( rc ) );
- return rc;
- }
-
- /* Copy any required options from previous server repsonse */
- if ( dhcpoffer ) {
- struct in_addr server_id;
- struct in_addr requested_ip;
-
- if ( dhcppkt_fetch ( dhcpoffer, DHCP_SERVER_IDENTIFIER,
- &server_id, sizeof ( server_id ) )
- != sizeof ( server_id ) ) {
- DBG ( "DHCP offer missing server identifier\n" );
- return -EINVAL;
- }
- if ( dhcppkt_fetch ( dhcpoffer, DHCP_EB_YIADDR,
- &requested_ip, sizeof ( requested_ip ) )
- != sizeof ( requested_ip ) ) {
- DBG ( "DHCP offer missing IP address\n" );
- return -EINVAL;
- }
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
- &server_id,
- sizeof ( server_id ) ) ) != 0 ) {
- DBG ( "DHCP could not set server identifier: %s\n ",
- strerror ( rc ) );
- return rc;
- }
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
- &requested_ip,
- sizeof ( requested_ip ) ) ) != 0 ){
- DBG ( "DHCP could not set requested address: %s\n",
- strerror ( rc ) );
- return rc;
- }
- }
-
- /* Add options to identify the feature list */
- dhcp_features_len = ( dhcp_features_end - dhcp_features );
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
- dhcp_features_len ) ) != 0 ) {
- DBG ( "DHCP could not set features list option: %s\n",
- strerror ( rc ) );
- return rc;
- }
-
- /* Add options to identify the network device */
- dhcp_desc.type = desc->bus_type;
- dhcp_desc.vendor = htons ( desc->vendor );
- dhcp_desc.device = htons ( desc->device );
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
- sizeof ( dhcp_desc ) ) ) != 0 ) {
- DBG ( "DHCP could not set bus ID option: %s\n",
- strerror ( rc ) );
- return rc;
- }
-
- /* Add DHCP client identifier. Required for Infiniband, and
- * doesn't hurt other link layers.
- */
- client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
- ll_addr_len = netdev->ll_protocol->ll_addr_len;
- assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
- memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
- ( ll_addr_len + 1 ) ) ) != 0 ) {
- DBG ( "DHCP could not set client ID: %s\n",
- strerror ( rc ) );
- return rc;
- }
-
- /* Add client UUID, if we have one. Required for PXE. */
- client_uuid.type = DHCP_CLIENT_UUID_TYPE;
- if ( ( rc = get_uuid ( &client_uuid.uuid ) ) == 0 ) {
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
- &client_uuid,
- sizeof ( client_uuid ) ) ) != 0 ) {
- DBG ( "DHCP could not set client UUID: %s\n",
- strerror ( rc ) );
- return rc;
- }
- }
-
- return 0;
-}
-
/****************************************************************************
*
* DHCP settings
};
/**
+ * Increment reference count on DHCP settings block
+ *
+ * @v dhcpset DHCP settings block
+ * @ret dhcpset DHCP settings block
+ */
+static inline __attribute__ (( always_inline )) struct dhcp_settings *
+dhcpset_get ( struct dhcp_settings *dhcpset ) {
+ ref_get ( &dhcpset->refcnt );
+ return dhcpset;
+}
+
+/**
* Decrement reference count on DHCP settings block
*
* @v dhcpset DHCP settings block
*/
-static inline void dhcpset_put ( struct dhcp_settings *dhcpset ) {
+static inline __attribute__ (( always_inline )) void
+dhcpset_put ( struct dhcp_settings *dhcpset ) {
ref_put ( &dhcpset->refcnt );
}
}
/**
- * Fetch value of setting
+ * Fetch value of DHCP setting
*
* @v settings Settings block, or NULL to search all blocks
* @v setting Setting to fetch
/****************************************************************************
*
- * DHCP to UDP interface
+ * DHCP session
+ *
+ */
+
+/** DHCP session states */
+enum dhcp_session_state {
+ /** Sending DHCPDISCOVERs, collecting DHCPOFFERs and ProxyDHCPOFFERs */
+ DHCP_STATE_DISCOVER = 0,
+ /** Sending DHCPREQUESTs, waiting for DHCPACK */
+ DHCP_STATE_REQUEST,
+ /** Sending ProxyDHCPREQUESTs, waiting for ProxyDHCPACK */
+ DHCP_STATE_PROXYREQUEST,
+};
+
+/**
+ * Name a DHCP session state
*
+ * @v state DHCP session state
+ * @ret string DHCP session state name
*/
+static inline const char * dhcp_state_name ( enum dhcp_session_state state ) {
+ switch ( state ) {
+ case DHCP_STATE_DISCOVER: return "DHCPDISCOVER";
+ case DHCP_STATE_REQUEST: return "DHCPREQUEST";
+ case DHCP_STATE_PROXYREQUEST: return "ProxyDHCPREQUEST";
+ default: return "<invalid>";
+ }
+}
/** A DHCP session */
struct dhcp_session {
* This is a value for the @c DHCP_MESSAGE_TYPE option
* (e.g. @c DHCPDISCOVER).
*/
- int state;
- /** Response obtained from DHCP server */
- struct dhcp_settings *response;
- /** Response obtained from ProxyDHCP server */
- struct dhcp_settings *proxy_response;
+ enum dhcp_session_state state;
+ /** DHCPOFFER obtained during DHCPDISCOVER */
+ struct dhcp_settings *dhcpoffer;
+ /** ProxyDHCPOFFER obtained during DHCPDISCOVER */
+ struct dhcp_settings *proxydhcpoffer;
/** Retransmission timer */
struct retry_timer timer;
- /** Session start time (in ticks) */
+ /** Start time of the current state (in ticks) */
unsigned long start;
};
container_of ( refcnt, struct dhcp_session, refcnt );
netdev_put ( dhcp->netdev );
- dhcpset_put ( dhcp->response );
- dhcpset_put ( dhcp->proxy_response );
+ dhcpset_put ( dhcp->dhcpoffer );
+ dhcpset_put ( dhcp->proxydhcpoffer );
free ( dhcp );
}
job_done ( &dhcp->job, rc );
}
+/****************************************************************************
+ *
+ * Data transfer interface
+ *
+ */
+
/**
- * Register options received via DHCP
+ * Create a DHCP packet
*
- * @v dhcp DHCP session
+ * @v dhcppkt DHCP packet structure to fill in
+ * @v netdev Network device
+ * @v msgtype DHCP message type
+ * @v options Initial options to include (or NULL)
+ * @v data Buffer for DHCP packet
+ * @v max_len Size of DHCP packet buffer
* @ret rc Return status code
+ *
+ * Creates a DHCP packet in the specified buffer, and fills out a @c
+ * dhcp_packet structure.
*/
-static int dhcp_register_settings ( struct dhcp_session *dhcp ) {
- struct settings *old_settings;
- struct settings *settings;
- struct settings *parent;
+int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
+ struct net_device *netdev, uint8_t msgtype,
+ struct dhcp_options *options,
+ void *data, size_t max_len ) {
+ struct dhcphdr *dhcphdr = data;
+ size_t options_len;
+ unsigned int hlen;
int rc;
- /* Register ProxyDHCP settings, if present */
- if ( dhcp->proxy_response ) {
- settings = &dhcp->proxy_response->settings;
- settings->name = PROXYDHCP_SETTINGS_NAME;
- old_settings = find_settings ( settings->name );
- if ( old_settings )
- unregister_settings ( old_settings );
- if ( ( rc = register_settings ( settings, NULL ) ) != 0 )
- return rc;
+ /* Sanity check */
+ options_len = ( options ? options->len : 0 );
+ if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
+ return -ENOSPC;
+
+ /* Initialise DHCP packet content */
+ memset ( dhcphdr, 0, max_len );
+ dhcphdr->xid = dhcp_xid ( netdev );
+ dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
+ dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
+ dhcphdr->op = dhcp_op[msgtype];
+ /* If hardware length exceeds the chaddr field length, don't
+ * use the chaddr field. This is as per RFC4390.
+ */
+ hlen = netdev->ll_protocol->ll_addr_len;
+ if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
+ hlen = 0;
+ dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
}
+ dhcphdr->hlen = hlen;
+ memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
+ memcpy ( dhcphdr->options, options->data, options_len );
- /* Register DHCP settings */
- parent = netdev_settings ( dhcp->netdev );
- settings = &dhcp->response->settings;
- old_settings = find_child_settings ( parent, settings->name );
- if ( old_settings )
- unregister_settings ( old_settings );
- if ( ( rc = register_settings ( settings, parent ) ) != 0 )
+ /* Initialise DHCP packet structure */
+ memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
+ dhcppkt_init ( dhcppkt, data, max_len );
+
+ /* Set DHCP_MESSAGE_TYPE option */
+ if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
+ &msgtype, sizeof ( msgtype ) ) ) != 0 )
return rc;
return 0;
}
-/****************************************************************************
- *
- * Data transfer interface
+/**
+ * Create DHCP request packet
*
+ * @v dhcppkt DHCP packet structure to fill in
+ * @v netdev Network device
+ * @v ciaddr Client IP address
+ * @v offer DHCP offer, if applicable
+ * @v data Buffer for DHCP packet
+ * @v max_len Size of DHCP packet buffer
+ * @ret rc Return status code
*/
+int dhcp_create_request ( struct dhcp_packet *dhcppkt,
+ struct net_device *netdev, struct in_addr ciaddr,
+ struct dhcp_packet *offer,
+ void *data, size_t max_len ) {
+ struct device_description *desc = &netdev->dev->desc;
+ struct dhcp_netdev_desc dhcp_desc;
+ struct dhcp_client_id client_id;
+ struct dhcp_client_uuid client_uuid;
+ unsigned int msgtype;
+ size_t dhcp_features_len;
+ size_t ll_addr_len;
+ int rc;
+
+ /* Create DHCP packet */
+ msgtype = ( offer ? DHCPREQUEST : DHCPDISCOVER );
+ if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
+ &dhcp_request_options, data,
+ max_len ) ) != 0 ) {
+ DBG ( "DHCP could not create DHCP packet: %s\n",
+ strerror ( rc ) );
+ return rc;
+ }
+
+ /* Set client IP address */
+ dhcppkt->dhcphdr->ciaddr = ciaddr;
+
+ /* Copy any required options from previous server repsonse */
+ if ( offer ) {
+ struct in_addr server = { 0 };
+ struct in_addr *ip = &offer->dhcphdr->yiaddr;
+
+ /* Copy server identifier, if present */
+ if ( ( dhcppkt_fetch ( offer, DHCP_SERVER_IDENTIFIER, &server,
+ sizeof ( server ) ) >= 0 ) &&
+ ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
+ &server,
+ sizeof ( server ) ) ) != 0 ) ) {
+ DBG ( "DHCP could not set server ID: %s\n",
+ strerror ( rc ) );
+ return rc;
+ }
+
+ /* Copy requested IP address, if present */
+ if ( ( ip->s_addr != 0 ) &&
+ ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
+ ip, sizeof ( *ip ) ) ) != 0 ) ) {
+ DBG ( "DHCP could not set requested address: %s\n",
+ strerror ( rc ) );
+ return rc;
+ }
+ }
+
+ /* Add options to identify the feature list */
+ dhcp_features_len = ( dhcp_features_end - dhcp_features );
+ if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
+ dhcp_features_len ) ) != 0 ) {
+ DBG ( "DHCP could not set features list option: %s\n",
+ strerror ( rc ) );
+ return rc;
+ }
+
+ /* Add options to identify the network device */
+ dhcp_desc.type = desc->bus_type;
+ dhcp_desc.vendor = htons ( desc->vendor );
+ dhcp_desc.device = htons ( desc->device );
+ if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
+ sizeof ( dhcp_desc ) ) ) != 0 ) {
+ DBG ( "DHCP could not set bus ID option: %s\n",
+ strerror ( rc ) );
+ return rc;
+ }
+
+ /* Add DHCP client identifier. Required for Infiniband, and
+ * doesn't hurt other link layers.
+ */
+ client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
+ ll_addr_len = netdev->ll_protocol->ll_addr_len;
+ assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
+ memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
+ if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
+ ( ll_addr_len + 1 ) ) ) != 0 ) {
+ DBG ( "DHCP could not set client ID: %s\n",
+ strerror ( rc ) );
+ return rc;
+ }
+
+ /* Add client UUID, if we have one. Required for PXE. */
+ client_uuid.type = DHCP_CLIENT_UUID_TYPE;
+ if ( ( rc = fetch_uuid_setting ( NULL, &uuid_setting,
+ &client_uuid.uuid ) ) >= 0 ) {
+ if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
+ &client_uuid,
+ sizeof ( client_uuid ) ) ) != 0 ) {
+ DBG ( "DHCP could not set client UUID: %s\n",
+ strerror ( rc ) );
+ return rc;
+ }
+ }
+
+ return 0;
+}
/**
* Transmit DHCP request
* @v dhcp DHCP session
* @ret rc Return status code
*/
-static int dhcp_send_request ( struct dhcp_session *dhcp ) {
+static int dhcp_tx ( struct dhcp_session *dhcp ) {
+ static struct sockaddr_in proxydhcp_server = {
+ .sin_family = AF_INET,
+ .sin_port = htons ( PROXYDHCP_PORT ),
+ };
struct xfer_metadata meta = {
.netdev = dhcp->netdev,
};
struct io_buffer *iobuf;
- struct dhcp_packet *dhcpoffer = NULL;
struct dhcp_packet dhcppkt;
+ struct dhcp_packet *offer = NULL;
+ struct in_addr ciaddr = { 0 };
+ int check_len;
int rc;
-
- DBGC ( dhcp, "DHCP %p transmitting %s\n",
- dhcp, dhcp_msgtype_name ( dhcp->state ) );
-
- assert ( ( dhcp->state == DHCPDISCOVER ) ||
- ( dhcp->state == DHCPREQUEST ) );
/* Start retry timer. Do this first so that failures to
* transmit will be retried.
*/
start_timer ( &dhcp->timer );
+ /* Determine packet contents based on current state */
+ switch ( dhcp->state ) {
+ case DHCP_STATE_DISCOVER:
+ DBGC ( dhcp, "DHCP %p transmitting DHCPDISCOVER\n", dhcp );
+ break;
+ case DHCP_STATE_REQUEST:
+ DBGC ( dhcp, "DHCP %p transmitting DHCPREQUEST\n", dhcp );
+ assert ( dhcp->dhcpoffer );
+ offer = &dhcp->dhcpoffer->dhcppkt;
+ break;
+ case DHCP_STATE_PROXYREQUEST:
+ DBGC ( dhcp, "DHCP %p transmitting ProxyDHCPREQUEST\n", dhcp );
+ assert ( dhcp->dhcpoffer );
+ assert ( dhcp->proxydhcpoffer );
+ offer = &dhcp->proxydhcpoffer->dhcppkt;
+ ciaddr = dhcp->dhcpoffer->dhcppkt.dhcphdr->yiaddr;
+ check_len = dhcppkt_fetch ( offer, DHCP_SERVER_IDENTIFIER,
+ &proxydhcp_server.sin_addr,
+ sizeof(proxydhcp_server.sin_addr));
+ meta.dest = ( struct sockaddr * ) &proxydhcp_server;
+ assert ( ciaddr.s_addr != 0 );
+ assert ( proxydhcp_server.sin_addr.s_addr != 0 );
+ assert ( check_len == sizeof ( proxydhcp_server.sin_addr ) );
+ break;
+ default:
+ assert ( 0 );
+ break;
+ }
+
/* Allocate buffer for packet */
iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
if ( ! iobuf )
return -ENOMEM;
/* Create DHCP packet in temporary buffer */
- if ( dhcp->state == DHCPREQUEST ) {
- assert ( dhcp->response );
- dhcpoffer = &dhcp->response->dhcppkt;
- }
- if ( ( rc = create_dhcp_request ( &dhcppkt, dhcp->netdev,
- dhcpoffer, iobuf->data,
+ if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev,
+ ciaddr, offer, iobuf->data,
iob_tailroom ( iobuf ) ) ) != 0 ) {
DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
dhcp, strerror ( rc ) );
}
/**
- * Handle DHCP retry timer expiry
+ * Transition to new DHCP session state
*
- * @v timer DHCP retry timer
- * @v fail Failure indicator
+ * @v dhcp DHCP session
+ * @v state New session state
*/
-static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
- struct dhcp_session *dhcp =
- container_of ( timer, struct dhcp_session, timer );
+static void dhcp_set_state ( struct dhcp_session *dhcp,
+ enum dhcp_session_state state ) {
+ DBGC ( dhcp, "DHCP %p entering %s state\n",
+ dhcp, dhcp_state_name ( state ) );
+ dhcp->state = state;
+ dhcp->start = currticks();
+ start_timer_nodelay ( &dhcp->timer );
+}
- if ( fail ) {
- dhcp_finished ( dhcp, -ETIMEDOUT );
- } else {
- dhcp_send_request ( dhcp );
+/**
+ * Store received DHCPOFFER
+ *
+ * @v dhcp DHCP session
+ * @v dhcpoffer Received DHCPOFFER
+ * @v stored_dhcpoffer Location to store DHCPOFFER
+ *
+ * The DHCPOFFER will be stored in place of the existing stored
+ * DHCPOFFER if its priority is equal to or greater than the stored
+ * DHCPOFFER.
+ */
+static void dhcp_store_dhcpoffer ( struct dhcp_session *dhcp,
+ struct dhcp_settings *dhcpoffer,
+ struct dhcp_settings **stored_dhcpoffer ) {
+ uint8_t stored_priority = 0;
+ uint8_t priority = 0;
+
+ /* Get priorities of the two DHCPOFFERs */
+ if ( *stored_dhcpoffer ) {
+ dhcppkt_fetch ( &(*stored_dhcpoffer)->dhcppkt,
+ DHCP_EB_PRIORITY, &stored_priority,
+ sizeof ( stored_priority ) );
+ }
+ dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_EB_PRIORITY, &priority,
+ sizeof ( priority ) );
+
+ /* Replace stored offer only if priority is equal or greater */
+ if ( priority >= stored_priority ) {
+ if ( *stored_dhcpoffer ) {
+ DBGC ( dhcp, "DHCP %p stored DHCPOFFER %p discarded\n",
+ dhcp, *stored_dhcpoffer );
+ }
+ DBGC ( dhcp, "DHCP %p received DHCPOFFER %p stored\n",
+ dhcp, dhcpoffer );
+ dhcpset_put ( *stored_dhcpoffer );
+ *stored_dhcpoffer = dhcpset_get ( dhcpoffer );
+ }
+}
+
+/**
+ * Handle received DHCPOFFER
+ *
+ * @v dhcp DHCP session
+ * @v dhcpoffer Received DHCPOFFER
+ */
+static void dhcp_rx_dhcpoffer ( struct dhcp_session *dhcp,
+ struct dhcp_settings *dhcpoffer ) {
+ char vci[9]; /* "PXEClient" */
+ int len;
+ uint8_t ignore_proxy = 0;
+ unsigned long elapsed;
+
+ /* Check for presence of DHCP server ID */
+ if ( dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_SERVER_IDENTIFIER,
+ NULL, 0 ) != sizeof ( struct in_addr ) ) {
+ DBGC ( dhcp, "DHCP %p received DHCPOFFER %p missing server "
+ "identifier\n", dhcp, dhcpoffer );
+ return;
+ }
+
+ /* If there is an IP address, it's a normal DHCPOFFER */
+ if ( dhcpoffer->dhcppkt.dhcphdr->yiaddr.s_addr != 0 ) {
+ DBGC ( dhcp, "DHCP %p received DHCPOFFER %p has IP address\n",
+ dhcp, dhcpoffer );
+ dhcp_store_dhcpoffer ( dhcp, dhcpoffer, &dhcp->dhcpoffer );
+ }
+
+ /* If there is a "PXEClient" vendor class ID, it's a
+ * ProxyDHCPOFFER. Note that it could be both a normal
+ * DHCPOFFER and a ProxyDHCPOFFER.
+ */
+ len = dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_VENDOR_CLASS_ID,
+ vci, sizeof ( vci ) );
+ if ( ( len >= ( int ) sizeof ( vci ) ) &&
+ ( strncmp ( "PXEClient", vci, sizeof ( vci ) ) == 0 ) ) {
+ DBGC ( dhcp, "DHCP %p received DHCPOFFER %p is a "
+ "ProxyDHCPOFFER\n", dhcp, dhcpoffer );
+ dhcp_store_dhcpoffer ( dhcp, dhcpoffer,
+ &dhcp->proxydhcpoffer );
+ }
+
+ /* We can transition to making the DHCPREQUEST when we have a
+ * valid DHCPOFFER, and either:
+ *
+ * o The DHCPOFFER instructs us to not wait for ProxyDHCP, or
+ * o We have a valid ProxyDHCPOFFER, or
+ * o We have allowed sufficient time for ProxyDHCPOFFERs.
+ */
+
+ /* If we don't yet have a DHCPOFFER, do nothing */
+ if ( ! dhcp->dhcpoffer )
+ return;
+
+ /* If the DHCPOFFER instructs us to ignore ProxyDHCP, discard
+ * any ProxyDHCPOFFER
+ */
+ dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt, DHCP_EB_NO_PROXYDHCP,
+ &ignore_proxy, sizeof ( ignore_proxy ) );
+ if ( ignore_proxy && dhcp->proxydhcpoffer ) {
+ DBGC ( dhcp, "DHCP %p discarding ProxyDHCPOFFER\n", dhcp );
+ dhcpset_put ( dhcp->proxydhcpoffer );
+ dhcp->proxydhcpoffer = NULL;
+ }
+
+ /* If we can't yet transition to DHCPREQUEST, do nothing */
+ elapsed = ( currticks() - dhcp->start );
+ if ( ! ( ignore_proxy || dhcp->proxydhcpoffer ||
+ ( elapsed > PROXYDHCP_WAIT_TIME ) ) )
+ return;
+
+ /* Transition to DHCPREQUEST */
+ dhcp_set_state ( dhcp, DHCP_STATE_REQUEST );
+}
+
+/**
+ * Store received DHCPACK
+ *
+ * @v dhcp DHCP session
+ * @v dhcpack Received DHCPACK
+ *
+ * The DHCPACK will be registered as a settings block.
+ */
+static int dhcp_store_dhcpack ( struct dhcp_session *dhcp,
+ struct dhcp_settings *dhcpack,
+ struct settings *parent ) {
+ struct settings *settings = &dhcpack->settings;
+ struct settings *old_settings;
+ int rc;
+
+ /* Unregister any old settings obtained via DHCP */
+ if ( ( old_settings = find_child_settings ( parent, settings->name ) ))
+ unregister_settings ( old_settings );
+
+ /* Register new settings */
+ if ( ( rc = register_settings ( settings, parent ) ) != 0 ) {
+ DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
+ dhcp, strerror ( rc ) );
+ dhcp_finished ( dhcp, rc ); /* This is a fatal error */
+ return rc;
+ }
+
+ return 0;
+}
+
+/**
+ * Handle received DHCPACK
+ *
+ * @v dhcp DHCP session
+ * @v dhcpack Received DHCPACK
+ */
+static void dhcp_rx_dhcpack ( struct dhcp_session *dhcp,
+ struct dhcp_settings *dhcpack ) {
+ struct settings *parent;
+ struct in_addr offer_server_id = { 0 };
+ struct in_addr ack_server_id = { 0 };
+ int rc;
+
+ /* Verify server ID matches */
+ assert ( dhcp->dhcpoffer != NULL );
+ dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt, DHCP_SERVER_IDENTIFIER,
+ &offer_server_id, sizeof ( offer_server_id ) );
+ dhcppkt_fetch ( &dhcpack->dhcppkt, DHCP_SERVER_IDENTIFIER,
+ &ack_server_id, sizeof ( ack_server_id ) );
+ if ( offer_server_id.s_addr != ack_server_id.s_addr ) {
+ DBGC ( dhcp, "DHCP %p ignoring DHCPACK with wrong server ID\n",
+ dhcp );
+ return;
+ }
+
+ /* Register settings */
+ parent = netdev_settings ( dhcp->netdev );
+ if ( ( rc = dhcp_store_dhcpack ( dhcp, dhcpack, parent ) ) !=0 )
+ return;
+
+ /* If we have a ProxyDHCPOFFER, transition to PROXYDHCPREQUEST */
+ if ( dhcp->proxydhcpoffer ) {
+ dhcp_set_state ( dhcp, DHCP_STATE_PROXYREQUEST );
+ return;
}
+
+ /* Terminate DHCP */
+ dhcp_finished ( dhcp, 0 );
+}
+
+/**
+ * Handle received ProxyDHCPACK
+ *
+ * @v dhcp DHCP session
+ * @v proxydhcpack Received ProxyDHCPACK
+ */
+static void dhcp_rx_proxydhcpack ( struct dhcp_session *dhcp,
+ struct dhcp_settings *proxydhcpack ) {
+ int rc;
+
+ /* Rename settings */
+ proxydhcpack->settings.name = PROXYDHCP_SETTINGS_NAME;
+
+ /* Register settings */
+ if ( ( rc = dhcp_store_dhcpack ( dhcp, proxydhcpack, NULL ) ) != 0 )
+ return;
+
+ /* Terminate DHCP */
+ dhcp_finished ( dhcp, 0 );
}
/**
* Receive new data
*
* @v xfer Data transfer interface
- * @v iobuf I/O buffer
* @v data Received data
* @v len Length of received data
* @ret rc Return status code
const void *data, size_t len ) {
struct dhcp_session *dhcp =
container_of ( xfer, struct dhcp_session, xfer );
- struct dhcp_settings *response;
- struct dhcp_settings **store_response;
+ struct dhcp_settings *dhcpset;
struct dhcphdr *dhcphdr;
uint8_t msgtype = 0;
- uint8_t priority = 0;
- uint8_t existing_priority = 0;
- unsigned long elapsed;
- int is_proxy;
- uint8_t ignore_proxy = 0;
- int rc;
/* Convert packet into a DHCP settings block */
- response = dhcpset_create ( data, len );
- if ( ! response ) {
+ dhcpset = dhcpset_create ( data, len );
+ if ( ! dhcpset ) {
DBGC ( dhcp, "DHCP %p could not store DHCP packet\n", dhcp );
return -ENOMEM;
}
- dhcphdr = response->dhcppkt.dhcphdr;
+ dhcphdr = dhcpset->dhcppkt.dhcphdr;
- /* Check for matching transaction ID */
- if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
- DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
- "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
- ntohl ( dhcp_xid ( dhcp->netdev ) ) );
- goto out_discard;
- };
-
- /* Determine and verify message type */
- is_proxy = ( dhcphdr->yiaddr.s_addr == 0 );
- dhcppkt_fetch ( &response->dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
+ /* Identify message type */
+ dhcppkt_fetch ( &dhcpset->dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
sizeof ( msgtype ) );
- DBGC ( dhcp, "DHCP %p received %s%s\n", dhcp,
- ( is_proxy ? "Proxy" : "" ), dhcp_msgtype_name ( msgtype ) );
- if ( ( ( dhcp->state != DHCPDISCOVER ) || ( msgtype != DHCPOFFER ) ) &&
- ( ( dhcp->state != DHCPREQUEST ) || ( msgtype != DHCPACK ) ) ) {
- DBGC ( dhcp, "DHCP %p discarding %s while in %s state\n",
- dhcp, dhcp_msgtype_name ( msgtype ),
- dhcp_msgtype_name ( dhcp->state ) );
- goto out_discard;
- }
-
- /* Update stored standard/ProxyDHCP options, if the new
- * options have equal or higher priority than the
- * currently-stored options.
- */
- store_response = ( is_proxy ? &dhcp->proxy_response : &dhcp->response);
- if ( *store_response ) {
- dhcppkt_fetch ( &(*store_response)->dhcppkt, DHCP_EB_PRIORITY,
- &existing_priority,
- sizeof ( existing_priority ) );
- }
- dhcppkt_fetch ( &response->dhcppkt, DHCP_EB_PRIORITY, &priority,
- sizeof ( priority ) );
- if ( priority >= existing_priority ) {
- dhcpset_put ( *store_response );
- *store_response = response;
- } else {
- dhcpset_put ( response );
- }
+ DBGC ( dhcp, "DHCP %p received %s %p\n",
+ dhcp, dhcp_msgtype_name ( msgtype ), dhcpset );
- /* If we don't yet have a standard DHCP response (i.e. one
- * with an IP address), then just leave the timer running.
- */
- if ( ! dhcp->response )
+ /* Check for matching transaction ID */
+ if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
+ DBGC ( dhcp, "DHCP %p received %s %p has bad transaction ID\n",
+ dhcp, dhcp_msgtype_name ( msgtype ), dhcpset );
goto out;
+ };
- /* Handle DHCP response */
- dhcppkt_fetch ( &dhcp->response->dhcppkt, DHCP_EB_NO_PROXYDHCP,
- &ignore_proxy, sizeof ( ignore_proxy ) );
+ /* Handle packet based on current state */
switch ( dhcp->state ) {
- case DHCPDISCOVER:
- /* If we have allowed sufficient time for ProxyDHCP
- * reponses, then transition to making the DHCPREQUEST.
- */
- elapsed = ( currticks() - dhcp->start );
- if ( ignore_proxy || ( elapsed > PROXYDHCP_WAIT_TIME ) ) {
- stop_timer ( &dhcp->timer );
- dhcp->state = DHCPREQUEST;
- dhcp_send_request ( dhcp );
- }
+ case DHCP_STATE_DISCOVER:
+ if ( msgtype == DHCPOFFER )
+ dhcp_rx_dhcpoffer ( dhcp, dhcpset );
break;
- case DHCPREQUEST:
- /* DHCP finished; register options and exit */
- if ( ignore_proxy && dhcp->proxy_response ) {
- dhcpset_put ( dhcp->proxy_response );
- dhcp->proxy_response = NULL;
- }
- if ( ( rc = dhcp_register_settings ( dhcp ) ) != 0 ) {
- dhcp_finished ( dhcp, rc );
- break;
- }
- dhcp_finished ( dhcp, 0 );
+ case DHCP_STATE_REQUEST:
+ if ( msgtype == DHCPACK )
+ dhcp_rx_dhcpack ( dhcp, dhcpset );
+ break;
+ case DHCP_STATE_PROXYREQUEST:
+ if ( msgtype == DHCPACK )
+ dhcp_rx_proxydhcpack ( dhcp, dhcpset );
break;
default:
assert ( 0 );
+ break;
}
out:
- return 0;
-
- out_discard:
- dhcpset_put ( response );
+ dhcpset_put ( dhcpset );
return 0;
}
.deliver_raw = dhcp_deliver_raw,
};
+/**
+ * Handle DHCP retry timer expiry
+ *
+ * @v timer DHCP retry timer
+ * @v fail Failure indicator
+ */
+static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
+ struct dhcp_session *dhcp =
+ container_of ( timer, struct dhcp_session, timer );
+ unsigned long elapsed = ( currticks() - dhcp->start );
+
+ /* If we have failed, terminate DHCP */
+ if ( fail ) {
+ dhcp_finished ( dhcp, -ETIMEDOUT );
+ return;
+ }
+
+ /* Give up waiting for ProxyDHCP before we reach the failure point */
+ if ( elapsed > PROXYDHCP_WAIT_TIME ) {
+ if ( dhcp->state == DHCP_STATE_DISCOVER ) {
+ dhcp_set_state ( dhcp, DHCP_STATE_REQUEST );
+ return;
+ } else if ( dhcp->state == DHCP_STATE_PROXYREQUEST ) {
+ dhcp_finished ( dhcp, 0 );
+ return;
+ }
+ }
+
+ /* Otherwise, retransmit current packet */
+ dhcp_tx ( dhcp );
+}
+
/****************************************************************************
*
* Job control interface
xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
dhcp->netdev = netdev_get ( netdev );
dhcp->timer.expired = dhcp_timer_expired;
- dhcp->state = DHCPDISCOVER;
dhcp->start = currticks();
/* Instantiate child objects and attach to our interfaces */