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) in block %p\n",
157 dhcp_tag_name ( original_tag ), option->len,
161 /* Check for explicit end marker */
162 if ( option->tag == DHCP_END )
164 /* Check for start of matching encapsulation block */
165 if ( DHCP_IS_ENCAP_OPT ( tag ) &&
166 ( option->tag == DHCP_ENCAPSULATOR ( tag ) ) ) {
168 *encapsulator = option;
169 /* Continue search within encapsulated option block */
170 tag = DHCP_ENCAPSULATED ( tag );
171 remaining = option->len;
172 option = ( void * ) &option->data;
175 option = ( ( ( void * ) option ) + option_len );
181 * Find DHCP option within DHCP options block
183 * @v options DHCP options block, or NULL
184 * @v tag DHCP option tag to search for
185 * @ret option DHCP option, or NULL if not found
187 * Searches for the DHCP option matching the specified tag within the
188 * DHCP option block. Encapsulated options may be searched for by
189 * using DHCP_ENCAP_OPT() to construct the tag value.
191 * If @c options is NULL, all registered option blocks will be
192 * searched. Where multiple option blocks contain the same DHCP
193 * option, the option from the highest-priority block will be
194 * returned. (Priority of an options block is determined by the value
195 * of the @c DHCP_EB_PRIORITY option within the block, if present; the
196 * default priority is zero).
198 struct dhcp_option * find_dhcp_option ( struct dhcp_option_block *options,
200 struct dhcp_option *option;
203 return find_dhcp_option_with_encap ( options, tag, NULL );
205 list_for_each_entry ( options, &option_blocks, list ) {
206 if ( ( option = find_dhcp_option ( options, tag ) ) )
214 * Register DHCP option block
216 * @v options DHCP option block
218 * Register a block of DHCP options.
220 void register_dhcp_options ( struct dhcp_option_block *options ) {
221 struct dhcp_option_block *existing;
223 /* Determine priority of new block */
224 options->priority = find_dhcp_num_option ( options, DHCP_EB_PRIORITY );
225 DBG ( "Registering DHCP options block %p with priority %d\n",
226 options, options->priority );
228 /* Insert after any existing blocks which have a higher priority */
229 list_for_each_entry ( existing, &option_blocks, list ) {
230 if ( options->priority > existing->priority )
233 list_add_tail ( &options->list, &existing->list );
237 * Unregister DHCP option block
239 * @v options DHCP option block
241 void unregister_dhcp_options ( struct dhcp_option_block *options ) {
242 list_del ( &options->list );
246 * Initialise empty block of DHCP options
248 * @v options Uninitialised DHCP option block
249 * @v data Memory for DHCP option data
250 * @v max_len Length of memory for DHCP option data
252 * Populates the DHCP option data with a single @c DHCP_END option and
253 * fills in the fields of the @c dhcp_option_block structure.
255 void init_dhcp_options ( struct dhcp_option_block *options,
256 void *data, size_t max_len ) {
257 struct dhcp_option *option;
259 options->data = data;
260 options->max_len = max_len;
261 option = options->data;
262 option->tag = DHCP_END;
265 DBG ( "DHCP options block %p initialised (data %p max_len %#zx)\n",
266 options, options->data, options->max_len );
270 * Allocate space for a block of DHCP options
272 * @v max_len Maximum length of option block
273 * @ret options DHCP option block, or NULL
275 * Creates a new DHCP option block and populates it with an empty
276 * options list. This call does not register the options block.
278 struct dhcp_option_block * alloc_dhcp_options ( size_t max_len ) {
279 struct dhcp_option_block *options;
281 options = malloc ( sizeof ( *options ) + max_len );
283 init_dhcp_options ( options,
284 ( (void *) options + sizeof ( *options ) ),
291 * Free DHCP options block
293 * @v options DHCP option block
295 void free_dhcp_options ( struct dhcp_option_block *options ) {
300 * Resize a DHCP option
302 * @v options DHCP option block
303 * @v option DHCP option to resize
304 * @v encapsulator Encapsulating option (or NULL)
305 * @v old_len Old length (including header)
306 * @v new_len New length (including header)
307 * @ret rc Return status code
309 static int resize_dhcp_option ( struct dhcp_option_block *options,
310 struct dhcp_option *option,
311 struct dhcp_option *encapsulator,
312 size_t old_len, size_t new_len ) {
313 void *source = ( ( ( void * ) option ) + old_len );
314 void *dest = ( ( ( void * ) option ) + new_len );
315 void *end = ( options->data + options->max_len );
316 ssize_t delta = ( new_len - old_len );
317 size_t new_options_len;
318 size_t new_encapsulator_len;
320 /* Check for sufficient space, and update length fields */
321 if ( new_len > DHCP_MAX_LEN )
323 new_options_len = ( options->len + delta );
324 if ( new_options_len > options->max_len )
326 if ( encapsulator ) {
327 new_encapsulator_len = ( encapsulator->len + delta );
328 if ( new_encapsulator_len > DHCP_MAX_LEN )
330 encapsulator->len = new_encapsulator_len;
332 options->len = new_options_len;
334 /* Move remainder of option data */
335 memmove ( dest, source, ( end - dest ) );
341 * Set value of DHCP option
343 * @v options DHCP option block
344 * @v tag DHCP option tag
345 * @v data New value for DHCP option
346 * @v len Length of value, in bytes
347 * @ret option DHCP option, or NULL
349 * Sets the value of a DHCP option within the options block. The
350 * option may or may not already exist. Encapsulators will be created
351 * (and deleted) as necessary.
353 * This call may fail due to insufficient space in the options block.
354 * If it does fail, and the option existed previously, the option will
355 * be left with its original value.
357 struct dhcp_option * set_dhcp_option ( struct dhcp_option_block *options,
359 const void *data, size_t len ) {
360 static const uint8_t empty_encapsulator[] = { DHCP_END };
361 struct dhcp_option *option;
362 void *insertion_point = options->data;
363 struct dhcp_option *encapsulator = NULL;
364 unsigned int encap_tag = DHCP_ENCAPSULATOR ( tag );
366 size_t new_len = ( len ? ( len + DHCP_OPTION_HEADER_LEN ) : 0 );
368 /* Find old instance of this option, if any */
369 option = find_dhcp_option_with_encap ( options, tag, &encapsulator );
371 old_len = dhcp_option_len ( option );
372 DBG ( "Resizing DHCP option %s from length %d to %d in block "
373 "%p\n", dhcp_tag_name (tag), option->len, len, options );
376 DBG ( "Creating DHCP option %s (length %d) in block %p\n",
377 dhcp_tag_name ( tag ), len, options );
380 /* Ensure that encapsulator exists, if required */
381 if ( DHCP_IS_ENCAP_OPT ( tag ) ) {
382 if ( ! encapsulator )
383 encapsulator = set_dhcp_option ( options, encap_tag,
385 sizeof ( empty_encapsulator) );
386 if ( ! encapsulator )
388 insertion_point = &encapsulator->data;
391 /* Create new option if necessary */
393 option = insertion_point;
395 /* Resize option to fit new data */
396 if ( resize_dhcp_option ( options, option, encapsulator,
397 old_len, new_len ) != 0 )
400 /* Copy new data into option, if applicable */
404 memcpy ( &option->data, data, len );
407 /* Delete encapsulator if there's nothing else left in it */
408 if ( encapsulator && ( encapsulator->len <= 1 ) )
409 set_dhcp_option ( options, encap_tag, NULL, 0 );
415 * Find DHCP option within all registered DHCP options blocks
417 * @v tag DHCP option tag to search for
418 * @ret option DHCP option, or NULL if not found
420 * This function exists merely as a notational shorthand for
421 * find_dhcp_option() with @c options set to NULL.
423 struct dhcp_option * find_global_dhcp_option ( unsigned int tag ) {
424 return find_dhcp_option ( NULL, tag );
428 * Find DHCP numerical option, and return its value
430 * @v options DHCP options block
431 * @v tag DHCP option tag to search for
432 * @ret value Numerical value of the option, or 0 if not found
434 * This function exists merely as a notational shorthand for a call to
435 * find_dhcp_option() followed by a call to dhcp_num_option(). It is
436 * not possible to distinguish between the cases "option not found"
437 * and "option has a value of zero" using this function; if this
438 * matters to you then issue the two constituent calls directly and
439 * check that find_dhcp_option() returns a non-NULL value.
441 unsigned long find_dhcp_num_option ( struct dhcp_option_block *options,
443 return dhcp_num_option ( find_dhcp_option ( options, tag ) );
447 * Find DHCP numerical option, and return its value
449 * @v tag DHCP option tag to search for
450 * @ret value Numerical value of the option, or 0 if not found
452 * This function exists merely as a notational shorthand for a call to
453 * find_global_dhcp_option() followed by a call to dhcp_num_option().
454 * It is not possible to distinguish between the cases "option not
455 * found" and "option has a value of zero" using this function; if
456 * this matters to you then issue the two constituent calls directly
457 * and check that find_global_dhcp_option() returns a non-NULL value.
459 unsigned long find_global_dhcp_num_option ( unsigned int tag ) {
460 return dhcp_num_option ( find_global_dhcp_option ( tag ) );
466 * @v options DHCP options block
467 * @v tag DHCP option tag
469 * This function exists merely as a notational shorthand for a call to
470 * set_dhcp_option() with @c len set to zero.
472 void delete_dhcp_option ( struct dhcp_option_block *options,
474 set_dhcp_option ( options, tag, NULL, 0 );