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.
26 #include <gpxe/list.h>
27 #include <gpxe/dhcp.h>
35 /** List of registered DHCP option blocks */
36 static LIST_HEAD ( option_blocks );
39 * Obtain printable version of a DHCP option tag
41 * @v tag DHCP option tag
42 * @ret name String representation of the tag
45 static inline char * dhcp_tag_name ( unsigned int tag ) {
48 if ( DHCP_IS_ENCAP_OPT ( tag ) ) {
49 snprintf ( name, sizeof ( name ), "%d.%d",
50 DHCP_ENCAPSULATOR ( tag ),
51 DHCP_ENCAPSULATED ( tag ) );
53 snprintf ( name, sizeof ( name ), "%d", tag );
59 * Obtain value of a numerical DHCP option
61 * @v option DHCP option, or NULL
62 * @ret value Numerical value of the option, or 0
64 * Parses the numerical value from a DHCP option, if present. It is
65 * permitted to call dhcp_num_option() with @c option set to NULL; in
66 * this case 0 will be returned.
68 * The caller does not specify the size of the DHCP option data; this
69 * is implied by the length field stored within the DHCP option
72 unsigned long dhcp_num_option ( struct dhcp_option *option ) {
73 unsigned long value = 0;
77 /* This is actually smaller code than using htons()
78 * etc., and will also cope well with malformed
79 * options (such as zero-length options).
81 for ( data = option->data.bytes ;
82 data < ( option->data.bytes + option->len ) ; data++ )
83 value = ( ( value << 8 ) | *data );
89 * Calculate length of a normal DHCP option
91 * @v option DHCP option
92 * @ret len Length (including tag and length field)
94 * @c option may not be a @c DHCP_PAD or @c DHCP_END option.
96 static inline unsigned int dhcp_option_len ( struct dhcp_option *option ) {
97 assert ( option->tag != DHCP_PAD );
98 assert ( option->tag != DHCP_END );
99 return ( option->len + DHCP_OPTION_HEADER_LEN );
103 * Calculate length of any DHCP option
105 * @v option DHCP option
106 * @ret len Length (including tag and length field)
108 static inline unsigned int dhcp_any_option_len ( struct dhcp_option *option ) {
109 if ( ( option->tag == DHCP_END ) || ( option->tag == DHCP_PAD ) ) {
112 return dhcp_option_len ( option );
117 * Find DHCP option within DHCP options block, and its encapsulator (if any)
119 * @v options DHCP options block
120 * @v tag DHCP option tag to search for
121 * @ret encapsulator Encapsulating DHCP option
122 * @ret option DHCP option, or NULL if not found
124 * Searches for the DHCP option matching the specified tag within the
125 * DHCP option block. Encapsulated options may be searched for by
126 * using DHCP_ENCAP_OPT() to construct the tag value.
128 * If the option is encapsulated, and @c encapsulator is non-NULL, it
129 * will be filled in with a pointer to the encapsulating option.
131 * This routine is designed to be paranoid. It does not assume that
132 * the option data is well-formatted, and so must guard against flaws
133 * such as options missing a @c DHCP_END terminator, or options whose
134 * length would take them beyond the end of the data block.
136 static struct dhcp_option *
137 find_dhcp_option_with_encap ( struct dhcp_option_block *options,
139 struct dhcp_option **encapsulator ) {
140 unsigned int original_tag __attribute__ (( unused )) = tag;
141 struct dhcp_option *option = options->data;
142 ssize_t remaining = options->len;
143 unsigned int option_len;
145 while ( remaining ) {
146 /* Calculate length of this option. Abort processing
147 * if the length is malformed (i.e. takes us beyond
148 * the end of the data block).
150 option_len = dhcp_any_option_len ( option );
151 remaining -= option_len;
154 /* Check for matching tag */
155 if ( option->tag == tag ) {
156 DBG ( "Found DHCP option %s (length %d)\n",
157 dhcp_tag_name ( original_tag ), option->len );
160 /* Check for explicit end marker */
161 if ( option->tag == DHCP_END )
163 /* Check for start of matching encapsulation block */
164 if ( DHCP_IS_ENCAP_OPT ( tag ) &&
165 ( option->tag == DHCP_ENCAPSULATOR ( tag ) ) ) {
167 *encapsulator = option;
168 /* Continue search within encapsulated option block */
169 tag = DHCP_ENCAPSULATED ( tag );
170 remaining = option->len;
171 option = ( void * ) &option->data;
174 option = ( ( ( void * ) option ) + option_len );
180 * Find DHCP option within DHCP options block
182 * @v options DHCP options block, or NULL
183 * @v tag DHCP option tag to search for
184 * @ret option DHCP option, or NULL if not found
186 * Searches for the DHCP option matching the specified tag within the
187 * DHCP option block. Encapsulated options may be searched for by
188 * using DHCP_ENCAP_OPT() to construct the tag value.
190 * If @c options is NULL, all registered option blocks will be
191 * searched. Where multiple option blocks contain the same DHCP
192 * option, the option from the highest-priority block will be
193 * returned. (Priority of an options block is determined by the value
194 * of the @c DHCP_EB_PRIORITY option within the block, if present; the
195 * default priority is zero).
197 struct dhcp_option * find_dhcp_option ( struct dhcp_option_block *options,
199 struct dhcp_option *option;
202 return find_dhcp_option_with_encap ( options, tag, NULL );
204 list_for_each_entry ( options, &option_blocks, list ) {
205 if ( ( option = find_dhcp_option ( options, tag ) ) )
213 * Register DHCP option block
215 * @v options DHCP option block
217 * Register a block of DHCP options.
219 void register_dhcp_options ( struct dhcp_option_block *options ) {
220 struct dhcp_option_block *existing;
222 /* Determine priority of new block */
223 options->priority = find_dhcp_num_option ( options, DHCP_EB_PRIORITY );
224 DBG ( "Registering DHCP options block with priority %d\n",
227 /* Insert after any existing blocks which have a higher priority */
228 list_for_each_entry ( existing, &option_blocks, list ) {
229 if ( options->priority > existing->priority )
232 list_add_tail ( &options->list, &existing->list );
236 * Unregister DHCP option block
238 * @v options DHCP option block
240 void unregister_dhcp_options ( struct dhcp_option_block *options ) {
241 list_del ( &options->list );
245 * Initialise empty block of DHCP options
247 * @v options Uninitialised DHCP option block
248 * @v data Memory for DHCP option data
249 * @v max_len Length of memory for DHCP option data
251 * Populates the DHCP option data with a single @c DHCP_END option and
252 * fills in the fields of the @c dhcp_option_block structure.
254 void init_dhcp_options ( struct dhcp_option_block *options,
255 void *data, size_t max_len ) {
256 struct dhcp_option *option;
258 options->data = data;
259 options->max_len = max_len;
260 option = options->data;
261 option->tag = DHCP_END;
266 * Allocate space for a block of DHCP options
268 * @v max_len Maximum length of option block
269 * @ret options DHCP option block, or NULL
271 * Creates a new DHCP option block and populates it with an empty
272 * options list. This call does not register the options block.
274 struct dhcp_option_block * alloc_dhcp_options ( size_t max_len ) {
275 struct dhcp_option_block *options;
277 options = malloc ( sizeof ( *options ) + max_len );
279 init_dhcp_options ( options,
280 ( (void *) options + sizeof ( *options ) ),
287 * Free DHCP options block
289 * @v options DHCP option block
291 void free_dhcp_options ( struct dhcp_option_block *options ) {
296 * Resize a DHCP option
298 * @v options DHCP option block
299 * @v option DHCP option to resize
300 * @v encapsulator Encapsulating option (or NULL)
301 * @v old_len Old length (including header)
302 * @v new_len New length (including header)
303 * @ret rc Return status code
305 static int resize_dhcp_option ( struct dhcp_option_block *options,
306 struct dhcp_option *option,
307 struct dhcp_option *encapsulator,
308 size_t old_len, size_t new_len ) {
309 void *source = ( ( ( void * ) option ) + old_len );
310 void *dest = ( ( ( void * ) option ) + new_len );
311 void *end = ( options->data + options->max_len );
312 ssize_t delta = ( new_len - old_len );
313 size_t new_options_len;
314 size_t new_encapsulator_len;
316 /* Check for sufficient space, and update length fields */
317 if ( new_len > DHCP_MAX_LEN )
319 new_options_len = ( options->len + delta );
320 if ( new_options_len > options->max_len )
322 if ( encapsulator ) {
323 new_encapsulator_len = ( encapsulator->len + delta );
324 if ( new_encapsulator_len > DHCP_MAX_LEN )
326 encapsulator->len = new_encapsulator_len;
328 options->len = new_options_len;
330 /* Move remainder of option data */
331 memmove ( dest, source, ( end - dest ) );
337 * Set value of DHCP option
339 * @v options DHCP option block
340 * @v tag DHCP option tag
341 * @v data New value for DHCP option
342 * @v len Length of value, in bytes
343 * @ret option DHCP option, or NULL
345 * Sets the value of a DHCP option within the options block. The
346 * option may or may not already exist. Encapsulators will be created
347 * (and deleted) as necessary.
349 * This call may fail due to insufficient space in the options block.
350 * If it does fail, and the option existed previously, the option will
351 * be left with its original value.
353 struct dhcp_option * set_dhcp_option ( struct dhcp_option_block *options,
355 const void *data, size_t len ) {
356 static const uint8_t empty_encapsulator[] = { DHCP_END };
357 struct dhcp_option *option;
358 void *insertion_point = options->data;
359 struct dhcp_option *encapsulator = NULL;
360 unsigned int encap_tag = DHCP_ENCAPSULATOR ( tag );
362 size_t new_len = ( len ? ( len + DHCP_OPTION_HEADER_LEN ) : 0 );
364 /* Find old instance of this option, if any */
365 option = find_dhcp_option_with_encap ( options, tag, &encapsulator );
367 old_len = dhcp_option_len ( option );
368 DBG ( "Resizing DHCP option %s from length %d to %d\n",
369 dhcp_tag_name ( tag ), option->len, len );
372 DBG ( "Creating DHCP option %s (length %d)\n",
373 dhcp_tag_name ( tag ), new_len );
376 /* Ensure that encapsulator exists, if required */
377 if ( DHCP_IS_ENCAP_OPT ( tag ) ) {
378 if ( ! encapsulator )
379 encapsulator = set_dhcp_option ( options, encap_tag,
381 sizeof ( empty_encapsulator) );
382 if ( ! encapsulator )
384 insertion_point = &encapsulator->data;
387 /* Create new option if necessary */
389 option = insertion_point;
391 /* Resize option to fit new data */
392 if ( resize_dhcp_option ( options, option, encapsulator,
393 old_len, new_len ) != 0 )
396 /* Copy new data into option, if applicable */
400 memcpy ( &option->data, data, len );
403 /* Delete encapsulator if there's nothing else left in it */
404 if ( encapsulator && ( encapsulator->len <= 1 ) )
405 set_dhcp_option ( options, encap_tag, NULL, 0 );
411 * Find DHCP option within all registered DHCP options blocks
413 * @v tag DHCP option tag to search for
414 * @ret option DHCP option, or NULL if not found
416 * This function exists merely as a notational shorthand for
417 * find_dhcp_option() with @c options set to NULL.
419 struct dhcp_option * find_global_dhcp_option ( unsigned int tag ) {
420 return find_dhcp_option ( NULL, tag );
424 * Find DHCP numerical option, and return its value
426 * @v options DHCP options block
427 * @v tag DHCP option tag to search for
428 * @ret value Numerical value of the option, or 0 if not found
430 * This function exists merely as a notational shorthand for a call to
431 * find_dhcp_option() followed by a call to dhcp_num_option(). It is
432 * not possible to distinguish between the cases "option not found"
433 * and "option has a value of zero" using this function; if this
434 * matters to you then issue the two constituent calls directly and
435 * check that find_dhcp_option() returns a non-NULL value.
437 unsigned long find_dhcp_num_option ( struct dhcp_option_block *options,
439 return dhcp_num_option ( find_dhcp_option ( options, tag ) );
443 * Find DHCP numerical option, and return its value
445 * @v tag DHCP option tag to search for
446 * @ret value Numerical value of the option, or 0 if not found
448 * This function exists merely as a notational shorthand for a call to
449 * find_global_dhcp_option() followed by a call to dhcp_num_option().
450 * It is not possible to distinguish between the cases "option not
451 * found" and "option has a value of zero" using this function; if
452 * this matters to you then issue the two constituent calls directly
453 * and check that find_global_dhcp_option() returns a non-NULL value.
455 unsigned long find_global_dhcp_num_option ( unsigned int tag ) {
456 return dhcp_num_option ( find_global_dhcp_option ( tag ) );
462 * @v options DHCP options block
463 * @v tag DHCP option tag
465 * This function exists merely as a notational shorthand for a call to
466 * set_dhcp_option() with @c len set to zero.
468 void delete_dhcp_option ( struct dhcp_option_block *options,
470 set_dhcp_option ( options, tag, NULL, 0 );