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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
26 #include <gpxe/dhcp.h>
27 #include <gpxe/dhcpopts.h>
36 * Obtain printable version of a DHCP option tag
38 * @v tag DHCP option tag
39 * @ret name String representation of the tag
42 static inline char * dhcp_tag_name ( unsigned int tag
) {
45 if ( DHCP_IS_ENCAP_OPT ( tag
) ) {
46 snprintf ( name
, sizeof ( name
), "%d.%d",
47 DHCP_ENCAPSULATOR ( tag
),
48 DHCP_ENCAPSULATED ( tag
) );
50 snprintf ( name
, sizeof ( name
), "%d", tag
);
56 * Get pointer to DHCP option
58 * @v options DHCP options block
59 * @v offset Offset within options block
60 * @ret option DHCP option
62 static inline __attribute__ (( always_inline
)) struct dhcp_option
*
63 dhcp_option ( struct dhcp_options
*options
, unsigned int offset
) {
64 return ( ( struct dhcp_option
* ) ( options
->data
+ offset
) );
68 * Get offset of a DHCP option
70 * @v options DHCP options block
71 * @v option DHCP option
72 * @ret offset Offset within options block
74 static inline __attribute__ (( always_inline
)) int
75 dhcp_option_offset ( struct dhcp_options
*options
,
76 struct dhcp_option
*option
) {
77 return ( ( ( void * ) option
) - options
->data
);
81 * Calculate length of any DHCP option
83 * @v option DHCP option
84 * @ret len Length (including tag and length field)
86 static unsigned int dhcp_option_len ( struct dhcp_option
*option
) {
87 if ( ( option
->tag
== DHCP_END
) || ( option
->tag
== DHCP_PAD
) ) {
90 return ( option
->len
+ DHCP_OPTION_HEADER_LEN
);
95 * Find DHCP option within DHCP options block, and its encapsulator (if any)
97 * @v options DHCP options block
98 * @v tag DHCP option tag to search for
99 * @ret encap_offset Offset of encapsulating DHCP option
100 * @ret offset Offset of DHCP option, or negative error
102 * Searches for the DHCP option matching the specified tag within the
103 * DHCP option block. Encapsulated options may be searched for by
104 * using DHCP_ENCAP_OPT() to construct the tag value.
106 * If the option is encapsulated, and @c encapsulator is non-NULL, it
107 * will be filled in with the offset of the encapsulating option.
109 * This routine is designed to be paranoid. It does not assume that
110 * the option data is well-formatted, and so must guard against flaws
111 * such as options missing a @c DHCP_END terminator, or options whose
112 * length would take them beyond the end of the data block.
114 static int find_dhcp_option_with_encap ( struct dhcp_options
*options
,
116 int *encap_offset
) {
117 unsigned int original_tag
__attribute__ (( unused
)) = tag
;
118 struct dhcp_option
*option
;
120 ssize_t remaining
= options
->len
;
121 unsigned int option_len
;
124 if ( tag
== DHCP_PAD
)
127 /* Search for option */
128 while ( remaining
) {
129 /* Calculate length of this option. Abort processing
130 * if the length is malformed (i.e. takes us beyond
131 * the end of the data block).
133 option
= dhcp_option ( options
, offset
);
134 option_len
= dhcp_option_len ( option
);
135 remaining
-= option_len
;
138 /* Check for explicit end marker */
139 if ( option
->tag
== DHCP_END
)
141 /* Check for matching tag */
142 if ( option
->tag
== tag
) {
143 DBGC ( options
, "DHCPOPT %p found %s (length %d)\n",
144 options
, dhcp_tag_name ( original_tag
),
148 /* Check for start of matching encapsulation block */
149 if ( DHCP_IS_ENCAP_OPT ( tag
) &&
150 ( option
->tag
== DHCP_ENCAPSULATOR ( tag
) ) ) {
152 *encap_offset
= offset
;
153 /* Continue search within encapsulated option block */
154 tag
= DHCP_ENCAPSULATED ( tag
);
155 remaining
= option_len
;
156 offset
+= DHCP_OPTION_HEADER_LEN
;
159 offset
+= option_len
;
166 * Resize a DHCP option
168 * @v options DHCP option block
169 * @v offset Offset of option to resize
170 * @v encap_offset Offset of encapsulating offset (or -ve for none)
171 * @v old_len Old length (including header)
172 * @v new_len New length (including header)
173 * @v can_realloc Can reallocate options data if necessary
174 * @ret rc Return status code
176 static int resize_dhcp_option ( struct dhcp_options
*options
,
177 int offset
, int encap_offset
,
178 size_t old_len
, size_t new_len
,
180 struct dhcp_option
*encapsulator
;
181 struct dhcp_option
*option
;
182 ssize_t delta
= ( new_len
- old_len
);
183 size_t new_options_len
;
184 size_t new_encapsulator_len
;
190 /* Check for sufficient space, and update length fields */
191 if ( new_len
> DHCP_MAX_LEN
) {
192 DBGC ( options
, "DHCPOPT %p overlength option\n", options
);
195 new_options_len
= ( options
->len
+ delta
);
196 if ( new_options_len
> options
->max_len
) {
197 /* Reallocate options block if allowed to do so. */
199 new_data
= realloc ( options
->data
, new_options_len
);
201 DBGC ( options
, "DHCPOPT %p could not "
202 "reallocate to %zd bytes\n", options
,
206 options
->data
= new_data
;
207 options
->max_len
= new_options_len
;
209 DBGC ( options
, "DHCPOPT %p out of space\n", options
);
213 if ( encap_offset
>= 0 ) {
214 encapsulator
= dhcp_option ( options
, encap_offset
);
215 new_encapsulator_len
= ( encapsulator
->len
+ delta
);
216 if ( new_encapsulator_len
> DHCP_MAX_LEN
) {
217 DBGC ( options
, "DHCPOPT %p overlength encapsulator\n",
221 encapsulator
->len
= new_encapsulator_len
;
223 options
->len
= new_options_len
;
225 /* Move remainder of option data */
226 option
= dhcp_option ( options
, offset
);
227 source
= ( ( ( void * ) option
) + old_len
);
228 dest
= ( ( ( void * ) option
) + new_len
);
229 end
= ( options
->data
+ options
->max_len
);
230 memmove ( dest
, source
, ( end
- dest
) );
236 * Set value of DHCP option
238 * @v options DHCP option block
239 * @v tag DHCP option tag
240 * @v data New value for DHCP option
241 * @v len Length of value, in bytes
242 * @v can_realloc Can reallocate options data if necessary
243 * @ret offset Offset of DHCP option, or negative error
245 * Sets the value of a DHCP option within the options block. The
246 * option may or may not already exist. Encapsulators will be created
247 * (and deleted) as necessary.
249 * This call may fail due to insufficient space in the options block.
250 * If it does fail, and the option existed previously, the option will
251 * be left with its original value.
253 static int set_dhcp_option ( struct dhcp_options
*options
, unsigned int tag
,
254 const void *data
, size_t len
,
256 static const uint8_t empty_encapsulator
[] = { DHCP_END
};
258 int encap_offset
= -1;
259 int creation_offset
= 0;
260 struct dhcp_option
*option
;
261 unsigned int encap_tag
= DHCP_ENCAPSULATOR ( tag
);
263 size_t new_len
= ( len
? ( len
+ DHCP_OPTION_HEADER_LEN
) : 0 );
267 if ( tag
== DHCP_PAD
)
270 /* Find old instance of this option, if any */
271 offset
= find_dhcp_option_with_encap ( options
, tag
, &encap_offset
);
273 old_len
= dhcp_option_len ( dhcp_option ( options
, offset
) );
274 DBGC ( options
, "DHCPOPT %p resizing %s from %zd to %zd\n",
275 options
, dhcp_tag_name ( tag
), old_len
, new_len
);
277 DBGC ( options
, "DHCPOPT %p creating %s (length %zd)\n",
278 options
, dhcp_tag_name ( tag
), new_len
);
281 /* Ensure that encapsulator exists, if required */
283 if ( encap_offset
< 0 )
284 encap_offset
= set_dhcp_option ( options
, encap_tag
,
285 empty_encapsulator
, 1,
287 if ( encap_offset
< 0 )
289 creation_offset
= ( encap_offset
+ DHCP_OPTION_HEADER_LEN
);
292 /* Create new option if necessary */
294 offset
= creation_offset
;
296 /* Resize option to fit new data */
297 if ( ( rc
= resize_dhcp_option ( options
, offset
, encap_offset
,
299 can_realloc
) ) != 0 )
302 /* Copy new data into option, if applicable */
304 option
= dhcp_option ( options
, offset
);
307 memcpy ( &option
->data
, data
, len
);
310 /* Delete encapsulator if there's nothing else left in it */
311 if ( encap_offset
>= 0 ) {
312 option
= dhcp_option ( options
, encap_offset
);
313 if ( option
->len
<= 1 )
314 set_dhcp_option ( options
, encap_tag
, NULL
, 0, 0 );
321 * Store value of DHCP option setting
323 * @v options DHCP option block
324 * @v tag Setting tag number
325 * @v data Setting data, or NULL to clear setting
326 * @v len Length of setting data
327 * @ret rc Return status code
329 int dhcpopt_store ( struct dhcp_options
*options
, unsigned int tag
,
330 const void *data
, size_t len
) {
333 offset
= set_dhcp_option ( options
, tag
, data
, len
, 0 );
340 * Store value of DHCP option setting, extending options block if necessary
342 * @v options DHCP option block
343 * @v tag Setting tag number
344 * @v data Setting data, or NULL to clear setting
345 * @v len Length of setting data
346 * @ret rc Return status code
348 int dhcpopt_extensible_store ( struct dhcp_options
*options
, unsigned int tag
,
349 const void *data
, size_t len
) {
352 offset
= set_dhcp_option ( options
, tag
, data
, len
, 1 );
359 * Fetch value of DHCP option setting
361 * @v options DHCP option block
362 * @v tag Setting tag number
363 * @v data Buffer to fill with setting data
364 * @v len Length of buffer
365 * @ret len Length of setting data, or negative error
367 int dhcpopt_fetch ( struct dhcp_options
*options
, unsigned int tag
,
368 void *data
, size_t len
) {
370 struct dhcp_option
*option
;
373 offset
= find_dhcp_option_with_encap ( options
, tag
, NULL
);
377 option
= dhcp_option ( options
, offset
);
378 option_len
= option
->len
;
379 if ( len
> option_len
)
381 memcpy ( data
, option
->data
, len
);
387 * Recalculate length of DHCP options block
389 * @v options Uninitialised DHCP option block
391 * The "used length" field will be updated based on scanning through
392 * the block to find the end of the options.
394 static void dhcpopt_update_len ( struct dhcp_options
*options
) {
395 struct dhcp_option
*option
;
397 ssize_t remaining
= options
->max_len
;
398 unsigned int option_len
;
400 /* Find last non-pad option */
402 while ( remaining
) {
403 option
= dhcp_option ( options
, offset
);
404 option_len
= dhcp_option_len ( option
);
405 remaining
-= option_len
;
408 offset
+= option_len
;
409 if ( option
->tag
!= DHCP_PAD
)
410 options
->len
= offset
;
415 * Initialise prepopulated block of DHCP options
417 * @v options Uninitialised DHCP option block
418 * @v data Memory for DHCP option data
419 * @v max_len Length of memory for DHCP option data
421 * The memory content must already be filled with valid DHCP options.
422 * A zeroed block counts as a block of valid DHCP options.
424 void dhcpopt_init ( struct dhcp_options
*options
, void *data
,
428 options
->data
= data
;
429 options
->max_len
= max_len
;
432 dhcpopt_update_len ( options
);
434 DBGC ( options
, "DHCPOPT %p created (data %p len %#zx max_len %#zx)\n",
435 options
, options
->data
, options
->len
, options
->max_len
);