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>
28 #include <gpxe/dhcp.h>
36 /** List of registered DHCP option blocks */
37 static LIST_HEAD ( option_blocks );
40 * Obtain printable version of a DHCP option tag
42 * @v tag DHCP option tag
43 * @ret name String representation of the tag
46 static inline char * dhcp_tag_name ( unsigned int tag ) {
49 if ( DHCP_IS_ENCAP_OPT ( tag ) ) {
50 snprintf ( name, sizeof ( name ), "%d.%d",
51 DHCP_ENCAPSULATOR ( tag ),
52 DHCP_ENCAPSULATED ( tag ) );
54 snprintf ( name, sizeof ( name ), "%d", tag );
60 * Obtain value of a numerical DHCP option
62 * @v option DHCP option, or NULL
63 * @ret value Numerical value of the option, or 0
65 * Parses the numerical value from a DHCP option, if present. It is
66 * permitted to call dhcp_num_option() with @c option set to NULL; in
67 * this case 0 will be returned.
69 * The caller does not specify the size of the DHCP option data; this
70 * is implied by the length field stored within the DHCP option
73 unsigned long dhcp_num_option ( struct dhcp_option *option ) {
74 unsigned long value = 0;
78 /* This is actually smaller code than using htons()
79 * etc., and will also cope well with malformed
80 * options (such as zero-length options).
82 for ( data = option->data.bytes ;
83 data < ( option->data.bytes + option->len ) ; data++ )
84 value = ( ( value << 8 ) | *data );
90 * Obtain value of an IPv4-address DHCP option
92 * @v option DHCP option, or NULL
93 * @v inp IPv4 address to fill in
95 * Parses the IPv4 address value from a DHCP option, if present. It
96 * is permitted to call dhcp_ipv4_option() with @c option set to NULL;
97 * in this case the address will be set to 0.0.0.0.
99 void dhcp_ipv4_option ( struct dhcp_option *option, struct in_addr *inp ) {
101 *inp = option->data.in;
105 * Calculate length of a normal DHCP option
107 * @v option DHCP option
108 * @ret len Length (including tag and length field)
110 * @c option may not be a @c DHCP_PAD or @c DHCP_END option.
112 static inline unsigned int dhcp_option_len ( struct dhcp_option *option ) {
113 assert ( option->tag != DHCP_PAD );
114 assert ( option->tag != DHCP_END );
115 return ( option->len + DHCP_OPTION_HEADER_LEN );
119 * Calculate length of any DHCP option
121 * @v option DHCP option
122 * @ret len Length (including tag and length field)
124 static inline unsigned int dhcp_any_option_len ( struct dhcp_option *option ) {
125 if ( ( option->tag == DHCP_END ) || ( option->tag == DHCP_PAD ) ) {
128 return dhcp_option_len ( option );
133 * Find DHCP option within DHCP options block, and its encapsulator (if any)
135 * @v options DHCP options block
136 * @v tag DHCP option tag to search for
137 * @ret encapsulator Encapsulating DHCP option
138 * @ret option DHCP option, or NULL if not found
140 * Searches for the DHCP option matching the specified tag within the
141 * DHCP option block. Encapsulated options may be searched for by
142 * using DHCP_ENCAP_OPT() to construct the tag value.
144 * If the option is encapsulated, and @c encapsulator is non-NULL, it
145 * will be filled in with a pointer to the encapsulating option.
147 * This routine is designed to be paranoid. It does not assume that
148 * the option data is well-formatted, and so must guard against flaws
149 * such as options missing a @c DHCP_END terminator, or options whose
150 * length would take them beyond the end of the data block.
152 static struct dhcp_option *
153 find_dhcp_option_with_encap ( struct dhcp_option_block *options,
155 struct dhcp_option **encapsulator ) {
156 unsigned int original_tag __attribute__ (( unused )) = tag;
157 struct dhcp_option *option = options->data;
158 ssize_t remaining = options->len;
159 unsigned int option_len;
161 while ( remaining ) {
162 /* Calculate length of this option. Abort processing
163 * if the length is malformed (i.e. takes us beyond
164 * the end of the data block).
166 option_len = dhcp_any_option_len ( option );
167 remaining -= option_len;
170 /* Check for matching tag */
171 if ( option->tag == tag ) {
172 DBG ( "Found DHCP option %s (length %d) in block %p\n",
173 dhcp_tag_name ( original_tag ), option->len,
177 /* Check for explicit end marker */
178 if ( option->tag == DHCP_END )
180 /* Check for start of matching encapsulation block */
181 if ( DHCP_IS_ENCAP_OPT ( tag ) &&
182 ( option->tag == DHCP_ENCAPSULATOR ( tag ) ) ) {
184 *encapsulator = option;
185 /* Continue search within encapsulated option block */
186 tag = DHCP_ENCAPSULATED ( tag );
187 remaining = option->len;
188 option = ( void * ) &option->data;
191 option = ( ( ( void * ) option ) + option_len );
197 * Find DHCP option within DHCP options block
199 * @v options DHCP options block, or NULL
200 * @v tag DHCP option tag to search for
201 * @ret option DHCP option, or NULL if not found
203 * Searches for the DHCP option matching the specified tag within the
204 * DHCP option block. Encapsulated options may be searched for by
205 * using DHCP_ENCAP_OPT() to construct the tag value.
207 * If @c options is NULL, all registered option blocks will be
208 * searched. Where multiple option blocks contain the same DHCP
209 * option, the option from the highest-priority block will be
210 * returned. (Priority of an options block is determined by the value
211 * of the @c DHCP_EB_PRIORITY option within the block, if present; the
212 * default priority is zero).
214 struct dhcp_option * find_dhcp_option ( struct dhcp_option_block *options,
216 struct dhcp_option *option;
219 return find_dhcp_option_with_encap ( options, tag, NULL );
221 list_for_each_entry ( options, &option_blocks, list ) {
222 if ( ( option = find_dhcp_option ( options, tag ) ) )
230 * Register DHCP option block
232 * @v options DHCP option block
234 * Register a block of DHCP options.
236 void register_dhcp_options ( struct dhcp_option_block *options ) {
237 struct dhcp_option_block *existing;
239 /* Determine priority of new block */
240 options->priority = find_dhcp_num_option ( options, DHCP_EB_PRIORITY );
241 DBG ( "Registering DHCP options block %p with priority %d\n",
242 options, options->priority );
244 /* Insert after any existing blocks which have a higher priority */
245 list_for_each_entry ( existing, &option_blocks, list ) {
246 if ( options->priority > existing->priority )
249 list_add_tail ( &options->list, &existing->list );
253 * Unregister DHCP option block
255 * @v options DHCP option block
257 void unregister_dhcp_options ( struct dhcp_option_block *options ) {
258 list_del ( &options->list );
262 * Initialise empty block of DHCP options
264 * @v options Uninitialised DHCP option block
265 * @v data Memory for DHCP option data
266 * @v max_len Length of memory for DHCP option data
268 * Populates the DHCP option data with a single @c DHCP_END option and
269 * fills in the fields of the @c dhcp_option_block structure.
271 void init_dhcp_options ( struct dhcp_option_block *options,
272 void *data, size_t max_len ) {
273 struct dhcp_option *option;
275 options->data = data;
276 options->max_len = max_len;
277 option = options->data;
278 option->tag = DHCP_END;
281 DBG ( "DHCP options block %p initialised (data %p max_len %#zx)\n",
282 options, options->data, options->max_len );
286 * Allocate space for a block of DHCP options
288 * @v max_len Maximum length of option block
289 * @ret options DHCP option block, or NULL
291 * Creates a new DHCP option block and populates it with an empty
292 * options list. This call does not register the options block.
294 struct dhcp_option_block * alloc_dhcp_options ( size_t max_len ) {
295 struct dhcp_option_block *options;
297 options = malloc ( sizeof ( *options ) + max_len );
299 init_dhcp_options ( options,
300 ( (void *) options + sizeof ( *options ) ),
307 * Free DHCP options block
309 * @v options DHCP option block
311 void free_dhcp_options ( struct dhcp_option_block *options ) {
316 * Resize a DHCP option
318 * @v options DHCP option block
319 * @v option DHCP option to resize
320 * @v encapsulator Encapsulating option (or NULL)
321 * @v old_len Old length (including header)
322 * @v new_len New length (including header)
323 * @ret rc Return status code
325 static int resize_dhcp_option ( struct dhcp_option_block *options,
326 struct dhcp_option *option,
327 struct dhcp_option *encapsulator,
328 size_t old_len, size_t new_len ) {
329 void *source = ( ( ( void * ) option ) + old_len );
330 void *dest = ( ( ( void * ) option ) + new_len );
331 void *end = ( options->data + options->max_len );
332 ssize_t delta = ( new_len - old_len );
333 size_t new_options_len;
334 size_t new_encapsulator_len;
336 /* Check for sufficient space, and update length fields */
337 if ( new_len > DHCP_MAX_LEN )
339 new_options_len = ( options->len + delta );
340 if ( new_options_len > options->max_len )
342 if ( encapsulator ) {
343 new_encapsulator_len = ( encapsulator->len + delta );
344 if ( new_encapsulator_len > DHCP_MAX_LEN )
346 encapsulator->len = new_encapsulator_len;
348 options->len = new_options_len;
350 /* Move remainder of option data */
351 memmove ( dest, source, ( end - dest ) );
357 * Set value of DHCP option
359 * @v options DHCP option block
360 * @v tag DHCP option tag
361 * @v data New value for DHCP option
362 * @v len Length of value, in bytes
363 * @ret option DHCP option, or NULL
365 * Sets the value of a DHCP option within the options block. The
366 * option may or may not already exist. Encapsulators will be created
367 * (and deleted) as necessary.
369 * This call may fail due to insufficient space in the options block.
370 * If it does fail, and the option existed previously, the option will
371 * be left with its original value.
373 struct dhcp_option * set_dhcp_option ( struct dhcp_option_block *options,
375 const void *data, size_t len ) {
376 static const uint8_t empty_encapsulator[] = { DHCP_END };
377 struct dhcp_option *option;
378 void *insertion_point = options->data;
379 struct dhcp_option *encapsulator = NULL;
380 unsigned int encap_tag = DHCP_ENCAPSULATOR ( tag );
382 size_t new_len = ( len ? ( len + DHCP_OPTION_HEADER_LEN ) : 0 );
384 /* Find old instance of this option, if any */
385 option = find_dhcp_option_with_encap ( options, tag, &encapsulator );
387 old_len = dhcp_option_len ( option );
388 DBG ( "Resizing DHCP option %s from length %d to %d in block "
389 "%p\n", dhcp_tag_name (tag), option->len, len, options );
392 DBG ( "Creating DHCP option %s (length %d) in block %p\n",
393 dhcp_tag_name ( tag ), len, options );
396 /* Ensure that encapsulator exists, if required */
397 if ( DHCP_IS_ENCAP_OPT ( tag ) ) {
398 if ( ! encapsulator )
399 encapsulator = set_dhcp_option ( options, encap_tag,
401 sizeof ( empty_encapsulator) );
402 if ( ! encapsulator )
404 insertion_point = &encapsulator->data;
407 /* Create new option if necessary */
409 option = insertion_point;
411 /* Resize option to fit new data */
412 if ( resize_dhcp_option ( options, option, encapsulator,
413 old_len, new_len ) != 0 )
416 /* Copy new data into option, if applicable */
420 memcpy ( &option->data, data, len );
423 /* Delete encapsulator if there's nothing else left in it */
424 if ( encapsulator && ( encapsulator->len <= 1 ) )
425 set_dhcp_option ( options, encap_tag, NULL, 0 );
431 * Find DHCP option within all registered DHCP options blocks
433 * @v tag DHCP option tag to search for
434 * @ret option DHCP option, or NULL if not found
436 * This function exists merely as a notational shorthand for
437 * find_dhcp_option() with @c options set to NULL.
439 struct dhcp_option * find_global_dhcp_option ( unsigned int tag ) {
440 return find_dhcp_option ( NULL, tag );
444 * Find DHCP numerical option, and return its value
446 * @v options DHCP options block
447 * @v tag DHCP option tag to search for
448 * @ret value Numerical value of the option, or 0 if not found
450 * This function exists merely as a notational shorthand for a call to
451 * find_dhcp_option() followed by a call to dhcp_num_option(). It is
452 * not possible to distinguish between the cases "option not found"
453 * and "option has a value of zero" using this function; if this
454 * matters to you then issue the two constituent calls directly and
455 * check that find_dhcp_option() returns a non-NULL value.
457 unsigned long find_dhcp_num_option ( struct dhcp_option_block *options,
459 return dhcp_num_option ( find_dhcp_option ( options, tag ) );
463 * Find DHCP numerical option, and return its value
465 * @v tag DHCP option tag to search for
466 * @ret value Numerical value of the option, or 0 if not found
468 * This function exists merely as a notational shorthand for a call to
469 * find_global_dhcp_option() followed by a call to dhcp_num_option().
470 * It is not possible to distinguish between the cases "option not
471 * found" and "option has a value of zero" using this function; if
472 * this matters to you then issue the two constituent calls directly
473 * and check that find_global_dhcp_option() returns a non-NULL value.
475 unsigned long find_global_dhcp_num_option ( unsigned int tag ) {
476 return dhcp_num_option ( find_global_dhcp_option ( tag ) );
480 * Find DHCP IPv4-address option, and return its value
482 * @v options DHCP options block
483 * @v tag DHCP option tag to search for
484 * @v inp IPv4 address to fill in
485 * @ret value Numerical value of the option, or 0 if not found
487 * This function exists merely as a notational shorthand for a call to
488 * find_dhcp_option() followed by a call to dhcp_ipv4_option(). It is
489 * not possible to distinguish between the cases "option not found"
490 * and "option has a value of 0.0.0.0" using this function; if this
491 * matters to you then issue the two constituent calls directly and
492 * check that find_dhcp_option() returns a non-NULL value.
494 void find_dhcp_ipv4_option ( struct dhcp_option_block *options,
495 unsigned int tag, struct in_addr *inp ) {
496 dhcp_ipv4_option ( find_dhcp_option ( options, tag ), inp );
500 * Find DHCP IPv4-address option, and return its value
502 * @v options DHCP options block
503 * @v tag DHCP option tag to search for
504 * @v inp IPv4 address to fill in
505 * @ret value Numerical value of the option, or 0 if not found
507 * This function exists merely as a notational shorthand for a call to
508 * find_dhcp_option() followed by a call to dhcp_ipv4_option(). It is
509 * not possible to distinguish between the cases "option not found"
510 * and "option has a value of 0.0.0.0" using this function; if this
511 * matters to you then issue the two constituent calls directly and
512 * check that find_dhcp_option() returns a non-NULL value.
514 void find_global_dhcp_ipv4_option ( unsigned int tag, struct in_addr *inp ) {
515 dhcp_ipv4_option ( find_global_dhcp_option ( tag ), inp );
521 * @v options DHCP options block
522 * @v tag DHCP option tag
524 * This function exists merely as a notational shorthand for a call to
525 * set_dhcp_option() with @c len set to zero.
527 void delete_dhcp_option ( struct dhcp_option_block *options,
529 set_dhcp_option ( options, tag, NULL, 0 );