2 * Copyright (C) 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
27 #include <ipxe/dhcp.h>
28 #include <ipxe/dhcpopts.h>
37 * Obtain printable version of a DHCP option tag
39 * @v tag DHCP option tag
40 * @ret name String representation of the tag
43 static inline char * dhcp_tag_name ( unsigned int tag ) {
46 if ( DHCP_IS_ENCAP_OPT ( tag ) ) {
47 snprintf ( name, sizeof ( name ), "%d.%d",
48 DHCP_ENCAPSULATOR ( tag ),
49 DHCP_ENCAPSULATED ( tag ) );
51 snprintf ( name, sizeof ( name ), "%d", tag );
57 * Get pointer to DHCP option
59 * @v options DHCP options block
60 * @v offset Offset within options block
61 * @ret option DHCP option
63 static inline __attribute__ (( always_inline )) struct dhcp_option *
64 dhcp_option ( struct dhcp_options *options, unsigned int offset ) {
65 return ( ( struct dhcp_option * ) ( options->data + offset ) );
69 * Get offset of a DHCP option
71 * @v options DHCP options block
72 * @v option DHCP option
73 * @ret offset Offset within options block
75 static inline __attribute__ (( always_inline )) int
76 dhcp_option_offset ( struct dhcp_options *options,
77 struct dhcp_option *option ) {
78 return ( ( ( void * ) option ) - options->data );
82 * Calculate length of any DHCP option
84 * @v option DHCP option
85 * @ret len Length (including tag and length field)
87 static unsigned int dhcp_option_len ( struct dhcp_option *option ) {
88 if ( ( option->tag == DHCP_END ) || ( option->tag == DHCP_PAD ) ) {
91 return ( option->len + DHCP_OPTION_HEADER_LEN );
96 * Find DHCP option within DHCP options block, and its encapsulator (if any)
98 * @v options DHCP options block
99 * @v tag DHCP option tag to search for
100 * @ret encap_offset Offset of encapsulating DHCP option
101 * @ret offset Offset of DHCP option, or negative error
103 * Searches for the DHCP option matching the specified tag within the
104 * DHCP option block. Encapsulated options may be searched for by
105 * using DHCP_ENCAP_OPT() to construct the tag value.
107 * If the option is encapsulated, and @c encap_offset is non-NULL, it
108 * will be filled in with the offset of the encapsulating option.
110 * This routine is designed to be paranoid. It does not assume that
111 * the option data is well-formatted, and so must guard against flaws
112 * such as options missing a @c DHCP_END terminator, or options whose
113 * length would take them beyond the end of the data block.
115 static int find_dhcp_option_with_encap ( struct dhcp_options *options,
117 int *encap_offset ) {
118 unsigned int original_tag __attribute__ (( unused )) = tag;
119 struct dhcp_option *option;
121 ssize_t remaining = options->used_len;
122 unsigned int option_len;
125 if ( tag == DHCP_PAD )
128 /* Search for option */
129 while ( remaining ) {
130 /* Calculate length of this option. Abort processing
131 * if the length is malformed (i.e. takes us beyond
132 * the end of the data block).
134 option = dhcp_option ( options, offset );
135 option_len = dhcp_option_len ( option );
136 remaining -= option_len;
139 /* Check for explicit end marker */
140 if ( option->tag == DHCP_END ) {
141 if ( tag == DHCP_END )
142 /* Special case where the caller is interested
143 * in whether we have this marker or not.
149 /* Check for matching tag */
150 if ( option->tag == tag ) {
151 DBGC ( options, "DHCPOPT %p found %s (length %d)\n",
152 options, dhcp_tag_name ( original_tag ),
156 /* Check for start of matching encapsulation block */
157 if ( DHCP_IS_ENCAP_OPT ( tag ) &&
158 ( option->tag == DHCP_ENCAPSULATOR ( tag ) ) ) {
160 *encap_offset = offset;
161 /* Continue search within encapsulated option block */
162 tag = DHCP_ENCAPSULATED ( tag );
163 remaining = option_len;
164 offset += DHCP_OPTION_HEADER_LEN;
167 offset += option_len;
174 * Refuse to reallocate DHCP option block
176 * @v options DHCP option block
178 * @ret rc Return status code
180 int dhcpopt_no_realloc ( struct dhcp_options *options, size_t len ) {
181 return ( ( len <= options->alloc_len ) ? 0 : -ENOSPC );
185 * Resize a DHCP option
187 * @v options DHCP option block
188 * @v offset Offset of option to resize
189 * @v encap_offset Offset of encapsulating offset (or -ve for none)
190 * @v old_len Old length (including header)
191 * @v new_len New length (including header)
192 * @ret rc Return status code
194 static int resize_dhcp_option ( struct dhcp_options *options,
195 int offset, int encap_offset,
196 size_t old_len, size_t new_len ) {
197 struct dhcp_option *encapsulator;
198 struct dhcp_option *option;
199 ssize_t delta = ( new_len - old_len );
200 size_t old_alloc_len;
202 size_t new_encapsulator_len;
207 /* Check for sufficient space */
208 if ( new_len > DHCP_MAX_LEN ) {
209 DBGC ( options, "DHCPOPT %p overlength option\n", options );
212 new_used_len = ( options->used_len + delta );
214 /* Expand options block, if necessary */
215 if ( new_used_len > options->alloc_len ) {
216 /* Reallocate options block */
217 old_alloc_len = options->alloc_len;
218 if ( ( rc = options->realloc ( options, new_used_len ) ) != 0 ){
219 DBGC ( options, "DHCPOPT %p could not reallocate to "
220 "%zd bytes\n", options, new_used_len );
223 /* Clear newly allocated space */
224 memset ( ( options->data + old_alloc_len ), 0,
225 ( options->alloc_len - old_alloc_len ) );
228 /* Update encapsulator, if applicable */
229 if ( encap_offset >= 0 ) {
230 encapsulator = dhcp_option ( options, encap_offset );
231 new_encapsulator_len = ( encapsulator->len + delta );
232 if ( new_encapsulator_len > DHCP_MAX_LEN ) {
233 DBGC ( options, "DHCPOPT %p overlength encapsulator\n",
237 encapsulator->len = new_encapsulator_len;
240 /* Update used length */
241 options->used_len = new_used_len;
243 /* Move remainder of option data */
244 option = dhcp_option ( options, offset );
245 source = ( ( ( void * ) option ) + old_len );
246 dest = ( ( ( void * ) option ) + new_len );
247 memmove ( dest, source, ( new_used_len - offset - new_len ) );
249 /* Shrink options block, if applicable */
250 if ( new_used_len < options->alloc_len ) {
251 if ( ( rc = options->realloc ( options, new_used_len ) ) != 0 ){
252 DBGC ( options, "DHCPOPT %p could not reallocate to "
253 "%zd bytes\n", options, new_used_len );
262 * Set value of DHCP option
264 * @v options DHCP option block
265 * @v tag DHCP option tag
266 * @v data New value for DHCP option
267 * @v len Length of value, in bytes
268 * @ret offset Offset of DHCP option, or negative error
270 * Sets the value of a DHCP option within the options block. The
271 * option may or may not already exist. Encapsulators will be created
272 * (and deleted) as necessary.
274 * This call may fail due to insufficient space in the options block.
275 * If it does fail, and the option existed previously, the option will
276 * be left with its original value.
278 static int set_dhcp_option ( struct dhcp_options *options, unsigned int tag,
279 const void *data, size_t len ) {
280 static const uint8_t empty_encap[] = { DHCP_END };
282 int encap_offset = -1;
284 struct dhcp_option *option;
285 unsigned int encap_tag = DHCP_ENCAPSULATOR ( tag );
287 size_t new_len = ( len ? ( len + DHCP_OPTION_HEADER_LEN ) : 0 );
291 if ( tag == DHCP_PAD )
294 creation_offset = find_dhcp_option_with_encap ( options, DHCP_END,
296 if ( creation_offset < 0 )
297 creation_offset = options->used_len;
298 /* Find old instance of this option, if any */
299 offset = find_dhcp_option_with_encap ( options, tag, &encap_offset );
301 old_len = dhcp_option_len ( dhcp_option ( options, offset ) );
302 DBGC ( options, "DHCPOPT %p resizing %s from %zd to %zd\n",
303 options, dhcp_tag_name ( tag ), old_len, new_len );
305 DBGC ( options, "DHCPOPT %p creating %s (length %zd)\n",
306 options, dhcp_tag_name ( tag ), new_len );
309 /* Ensure that encapsulator exists, if required */
311 if ( encap_offset < 0 ) {
313 set_dhcp_option ( options, encap_tag,
315 sizeof ( empty_encap ) );
317 if ( encap_offset < 0 )
319 creation_offset = ( encap_offset + DHCP_OPTION_HEADER_LEN );
322 /* Create new option if necessary */
324 offset = creation_offset;
326 /* Resize option to fit new data */
327 if ( ( rc = resize_dhcp_option ( options, offset, encap_offset,
328 old_len, new_len ) ) != 0 )
331 /* Copy new data into option, if applicable */
333 option = dhcp_option ( options, offset );
336 memcpy ( &option->data, data, len );
339 /* Delete encapsulator if there's nothing else left in it */
340 if ( encap_offset >= 0 ) {
341 option = dhcp_option ( options, encap_offset );
342 if ( option->len <= 1 )
343 set_dhcp_option ( options, encap_tag, NULL, 0 );
350 * Check applicability of DHCP option setting
352 * @v tag Setting tag number
353 * @ret applies Setting applies to this option block
355 int dhcpopt_applies ( unsigned int tag ) {
357 return ( tag && ( tag <= DHCP_ENCAP_OPT ( DHCP_MAX_OPTION,
358 DHCP_MAX_OPTION ) ) );
362 * Store value of DHCP option setting
364 * @v options DHCP option block
365 * @v tag Setting tag number
366 * @v data Setting data, or NULL to clear setting
367 * @v len Length of setting data
368 * @ret rc Return status code
370 int dhcpopt_store ( struct dhcp_options *options, unsigned int tag,
371 const void *data, size_t len ) {
374 offset = set_dhcp_option ( options, tag, data, len );
381 * Fetch value of DHCP option setting
383 * @v options DHCP option block
384 * @v tag Setting tag number
385 * @v data Buffer to fill with setting data
386 * @v len Length of buffer
387 * @ret len Length of setting data, or negative error
389 int dhcpopt_fetch ( struct dhcp_options *options, unsigned int tag,
390 void *data, size_t len ) {
392 struct dhcp_option *option;
395 offset = find_dhcp_option_with_encap ( options, tag, NULL );
399 option = dhcp_option ( options, offset );
400 option_len = option->len;
401 if ( len > option_len )
403 memcpy ( data, option->data, len );
409 * Recalculate length of DHCP options block
411 * @v options Uninitialised DHCP option block
413 * The "used length" field will be updated based on scanning through
414 * the block to find the end of the options.
416 void dhcpopt_update_used_len ( struct dhcp_options *options ) {
417 struct dhcp_option *option;
419 ssize_t remaining = options->alloc_len;
420 unsigned int option_len;
422 /* Find last non-pad option */
423 options->used_len = 0;
424 while ( remaining ) {
425 option = dhcp_option ( options, offset );
426 option_len = dhcp_option_len ( option );
427 remaining -= option_len;
430 offset += option_len;
431 if ( option->tag != DHCP_PAD )
432 options->used_len = offset;
437 * Initialise prepopulated block of DHCP options
439 * @v options Uninitialised DHCP option block
440 * @v data Memory for DHCP option data
441 * @v alloc_len Length of memory for DHCP option data
442 * @v realloc DHCP option block reallocator
444 * The memory content must already be filled with valid DHCP options.
445 * A zeroed block counts as a block of valid DHCP options.
447 void dhcpopt_init ( struct dhcp_options *options, void *data, size_t alloc_len,
448 int ( * realloc ) ( struct dhcp_options *options,
452 options->data = data;
453 options->alloc_len = alloc_len;
454 options->realloc = realloc;
457 dhcpopt_update_used_len ( options );
459 DBGC ( options, "DHCPOPT %p created (data %p lengths %#zx,%#zx)\n",
460 options, options->data, options->used_len, options->alloc_len );