2
0
mirror of https://github.com/xcat2/xNBA.git synced 2024-12-15 07:41:45 +00:00

Add (untested) code for parsing a received DHCP packet and constructing a

DHCP options block from the contents.
This commit is contained in:
Michael Brown 2006-07-15 19:24:55 +00:00
parent 19e8b41562
commit 12da7ea475
2 changed files with 322 additions and 3 deletions

View File

@ -9,6 +9,87 @@
#include <stdint.h>
#include <gpxe/list.h>
#include <gpxe/in.h>
/**
* A DHCP packet
*
*/
struct dhcp_packet {
/** Operation
*
* This must be either @c BOOTP_REQUEST or @c BOOTP_REPLY.
*/
uint8_t op;
/** Hardware address type
*
* This is an ARPHRD_XXX constant.
*/
uint8_t htype;
/** Hardware address length */
uint8_t hlen;
/** Number of hops from server */
uint8_t hops;
/** Transaction ID */
uint32_t xid;
/** Seconds since start of acquisition */
uint16_t secs;
/** Flags */
uint16_t flags;
/** "Client" IP address
*
* This is filled in if the client already has an IP address
* assigned and can respond to ARP requests.
*/
struct in_addr ciaddr;
/** "Your" IP address
*
* This is the IP address assigned by the server to the client.
*/
struct in_addr yiaddr;
/** "Server" IP address
*
* This is the IP address of the next server to be used in the
* boot process.
*/
struct in_addr siaddr;
/** "Gateway" IP address
*
* This is the IP address of the DHCP relay agent, if any.
*/
struct in_addr giaddr;
/** Client hardware address */
uint8_t chaddr[16];
/** Server host name (null terminated)
*
* This field may be overridden and contain DHCP options
*/
uint8_t sname[64];
/** Boot file name (null terminated)
*
* This field may be overridden and contain DHCP options
*/
uint8_t file[128];
/** DHCP magic cookie
*
* Must have the value @c DHCP_MAGIC_COOKIE.
*/
uint32_t magic;
/** DHCP options
*
* Variable length; extends to the end of the packet.
*/
uint8_t options[0];
};
/** Opcode for a request from client to server */
#define BOOTP_REQUEST 1
/** Opcode for a reply from server to client */
#define BOOTP_REPLY 2
/** DHCP magic cookie */
#define DHCP_MAGIC_COOKIE 0x63825363UL
/** Construct a tag value for an encapsulated option
*
@ -22,7 +103,7 @@
#define DHCP_ENCAPSULATOR( encap_opt ) ( (encap_opt) >> 8 )
/** Extract encapsulated option tag from encapsulated tag value */
#define DHCP_ENCAPSULATED( encap_opt ) ( (encap_opt) & 0xff )
/** Option is encapsulated */
#define DHCP_IS_ENCAP_OPT( opt ) DHCP_ENCAPSULATOR( opt )
/**
@ -30,13 +111,80 @@
* @{
*/
/** Padding
*
* This tag does not have a length field; it is always only a single
* byte in length.
*/
#define DHCP_PAD 0
#define DHCP_END 255
/** Option overloading
*
* The value of this option is the bitwise-OR of zero or more
* DHCP_OPTION_OVERLOAD_XXX constants.
*/
#define DHCP_OPTION_OVERLOAD 52
/** The "file" field is overloaded to contain extra DHCP options */
#define DHCP_OPTION_OVERLOAD_FILE 1
/** The "sname" field is overloaded to contain extra DHCP options */
#define DHCP_OPTION_OVERLOAD_SNAME 2
/** TFTP server name
*
* This option replaces the fixed "sname" field, when that field is
* used to contain overloaded options.
*/
#define DHCP_TFTP_SERVER_NAME 66
/** Bootfile name
*
* This option replaces the fixed "file" field, when that field is
* used to contain overloaded options.
*/
#define DHCP_BOOTFILE_NAME 67
/** Etherboot-specific encapsulated options
*
* This encapsulated options field is used to contain all options
* specific to Etherboot (i.e. not assigned by IANA or other standards
* bodies).
*/
#define DHCP_EB_ENCAP 175
/** Priority of this options block
*
* This is a signed 8-bit integer field indicating the priority of
* this block of options. It can be used to specify the relative
* priority of multiple option blocks (e.g. options from non-volatile
* storage versus options from a DHCP server).
*/
#define DHCP_EB_PRIORITY DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 1 )
/** "Your" IP address
*
* This option is used internally to contain the value of the "yiaddr"
* field, in order to provide a consistent approach to storing and
* processing options. It should never be present in a DHCP packet.
*/
#define DHCP_EB_YIADDR DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 2 )
/** "Server" IP address
*
* This option is used internally to contain the value of the "siaddr"
* field, in order to provide a consistent approach to storing and
* processing options. It should never be present in a DHCP packet.
*/
#define DHCP_EB_SIADDR DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 3 )
/** End of options
*
* This tag does not have a length field; it is always only a single
* byte in length.
*/
#define DHCP_END 255
/** @} */
/**
@ -118,7 +266,6 @@ extern struct dhcp_option *
set_dhcp_option ( struct dhcp_option_block *options, unsigned int tag,
const void *data, size_t len );
/**
* Find DHCP numerical option, and return its value
*

172
src/net/udp/dhcp.c Normal file
View File

@ -0,0 +1,172 @@
/*
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <string.h>
#include <assert.h>
#include <byteswap.h>
#include <gpxe/netdevice.h>
#include <gpxe/dhcp.h>
/** @file
*
* Dynamic Host Configuration Protocol
*
*/
/**
* 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, 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 ) {
set_dhcp_option ( options, tag, data, strlen ( data ) );
} 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 data 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
* 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.
*/
struct dhcp_option_block * dhcp_parse ( const void *data, size_t len ) {
const struct dhcp_packet *dhcppkt = data;
struct dhcp_option_block *options;
size_t options_len;
unsigned int overloading;
/* 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 ( *dhcppkt ), options ) ) - 1
+ ( sizeof ( dhcppkt->file ) + 1 )
+ ( sizeof ( dhcppkt->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 %d-byte option block\n",
options_len );
return NULL;
}
/* Merge in "options" field, if this is a DHCP packet */
if ( dhcppkt->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
merge_dhcp_field ( options, dhcppkt->options,
( len -
offsetof ( typeof (*dhcppkt), 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, dhcppkt->file, sizeof ( dhcppkt->file ),
( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
DHCP_BOOTFILE_NAME : 0 ) );
merge_dhcp_field ( options, dhcppkt->sname, sizeof ( dhcppkt->sname ),
( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
DHCP_TFTP_SERVER_NAME : 0 ) );
/* Set options for "yiaddr" and "siaddr", if present */
if ( dhcppkt->yiaddr.s_addr ) {
set_dhcp_option ( options, DHCP_EB_YIADDR,
&dhcppkt->yiaddr, sizeof (dhcppkt->yiaddr) );
}
if ( dhcppkt->siaddr.s_addr ) {
set_dhcp_option ( options, DHCP_EB_SIADDR,
&dhcppkt->siaddr, sizeof (dhcppkt->siaddr) );
}
assert ( options->len <= options->max_len );
return options;
}