2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <gpxe/netdevice.h>
23 #include <gpxe/dhcp.h>
27 * Dynamic Host Configuration Protocol
32 * Calculate used length of a field containing DHCP options
34 * @v data Field containing DHCP options
35 * @v max_len Field length
36 * @ret len Used length (excluding the @c DHCP_END tag)
38 static size_t dhcp_field_len ( const void *data, size_t max_len ) {
39 struct dhcp_option_block options;
40 struct dhcp_option *end;
42 options.data = ( ( void * ) data );
43 options.len = max_len;
44 end = find_dhcp_option ( &options, DHCP_END );
45 return ( end ? ( ( ( void * ) end ) - data ) : 0 );
49 * Merge field containing DHCP options or string into DHCP options block
51 * @v options DHCP option block
52 * @v data Field containing DHCP options
53 * @v max_len Field length
54 * @v tag DHCP option tag, or 0
56 * If @c tag is non-zero, the field will be treated as a
57 * NUL-terminated string representing the value of the specified DHCP
58 * option. If @c tag is zero, the field will be treated as a block of
59 * DHCP options, and simply appended to the existing options in the
62 * The caller must ensure that there is enough space in the options
63 * block to perform the merge.
65 static void merge_dhcp_field ( struct dhcp_option_block *options,
66 const void *data, size_t max_len,
70 struct dhcp_option *end;
73 set_dhcp_option ( options, tag, data, strlen ( data ) );
75 len = dhcp_field_len ( data, max_len );
76 dest = ( options->data + options->len - 1 );
77 memcpy ( dest, data, len );
85 * Parse DHCP packet and construct DHCP options block
88 * @v len Length of DHCP packet
89 * @ret options DHCP options block, or NULL
91 * Parses a received DHCP packet and canonicalises its contents into a
92 * single DHCP options block. The "file" and "sname" fields are
93 * converted into the corresponding DHCP options (@c
94 * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
95 * these fields are used for option overloading, their options are
96 * merged in to the options block. The values of the "yiaddr" and
97 * "siaddr" fields will be stored within the options block as the
98 * options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR.
100 * Note that this call allocates new memory for the constructed DHCP
101 * options block; it is the responsibility of the caller to eventually
104 struct dhcp_option_block * dhcp_parse ( const void *data, size_t len ) {
105 const struct dhcp_packet *dhcppkt = data;
106 struct dhcp_option_block *options;
108 unsigned int overloading;
110 /* Calculate size of resulting concatenated option block:
112 * The "options" field : length of the field minus the DHCP_END tag.
114 * The "file" field : maximum length of the field minus the
115 * NUL terminator, plus a 2-byte DHCP header or, if used for
116 * option overloading, the length of the field minus the
119 * The "sname" field : as for the "file" field.
121 * 15 bytes for an encapsulated options field to contain the
122 * value of the "yiaddr" and "siaddr" fields
124 * 1 byte for a final terminating DHCP_END tag.
126 options_len = ( ( len - offsetof ( typeof ( *dhcppkt ), options ) ) - 1
127 + ( sizeof ( dhcppkt->file ) + 1 )
128 + ( sizeof ( dhcppkt->sname ) + 1 )
129 + 15 /* yiaddr and siaddr */
130 + 1 /* DHCP_END tag */ );
132 /* Allocate empty options block of required size */
133 options = alloc_dhcp_options ( options_len );
135 DBG ( "DHCP could not allocate %d-byte option block\n",
140 /* Merge in "options" field, if this is a DHCP packet */
141 if ( dhcppkt->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
142 merge_dhcp_field ( options, dhcppkt->options,
144 offsetof ( typeof (*dhcppkt), options ) ),
145 0 /* Always contains options */ );
148 /* Identify overloaded fields */
149 overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
151 /* Merge in "file" and "sname" fields */
152 merge_dhcp_field ( options, dhcppkt->file, sizeof ( dhcppkt->file ),
153 ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
154 DHCP_BOOTFILE_NAME : 0 ) );
155 merge_dhcp_field ( options, dhcppkt->sname, sizeof ( dhcppkt->sname ),
156 ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
157 DHCP_TFTP_SERVER_NAME : 0 ) );
159 /* Set options for "yiaddr" and "siaddr", if present */
160 if ( dhcppkt->yiaddr.s_addr ) {
161 set_dhcp_option ( options, DHCP_EB_YIADDR,
162 &dhcppkt->yiaddr, sizeof (dhcppkt->yiaddr) );
164 if ( dhcppkt->siaddr.s_addr ) {
165 set_dhcp_option ( options, DHCP_EB_SIADDR,
166 &dhcppkt->siaddr, sizeof (dhcppkt->siaddr) );
169 assert ( options->len <= options->max_len );