[e1000] Add 82576 support
[gpxe.git] / src / net / dhcpopts.c
blobc1940f1f5420bce20da756b12bd7d888c7687ad8
1 /*
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 );
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <gpxe/dhcp.h>
27 #include <gpxe/dhcpopts.h>
29 /** @file
31 * DHCP options
35 /**
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 ) {
43 static char name[8];
45 if ( DHCP_IS_ENCAP_OPT ( tag ) ) {
46 snprintf ( name, sizeof ( name ), "%d.%d",
47 DHCP_ENCAPSULATOR ( tag ),
48 DHCP_ENCAPSULATED ( tag ) );
49 } else {
50 snprintf ( name, sizeof ( name ), "%d", tag );
52 return name;
55 /**
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 ) );
67 /**
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 );
80 /**
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 ) ) {
88 return 1;
89 } else {
90 return ( option->len + DHCP_OPTION_HEADER_LEN );
94 /**
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,
115 unsigned int tag,
116 int *encap_offset ) {
117 unsigned int original_tag __attribute__ (( unused )) = tag;
118 struct dhcp_option *option;
119 int offset = 0;
120 ssize_t remaining = options->len;
121 unsigned int option_len;
123 /* Sanity check */
124 if ( tag == DHCP_PAD )
125 return -ENOENT;
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;
136 if ( remaining < 0 )
137 break;
138 /* Check for explicit end marker */
139 if ( option->tag == DHCP_END )
140 break;
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 ),
145 option_len );
146 return offset;
148 /* Check for start of matching encapsulation block */
149 if ( DHCP_IS_ENCAP_OPT ( tag ) &&
150 ( option->tag == DHCP_ENCAPSULATOR ( tag ) ) ) {
151 if ( encap_offset )
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;
157 continue;
159 offset += option_len;
162 return -ENOENT;
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,
179 int can_realloc ) {
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;
185 void *new_data;
186 void *source;
187 void *dest;
188 void *end;
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 );
193 return -ENOSPC;
195 new_options_len = ( options->len + delta );
196 if ( new_options_len > options->max_len ) {
197 /* Reallocate options block if allowed to do so. */
198 if ( can_realloc ) {
199 new_data = realloc ( options->data, new_options_len );
200 if ( ! new_data ) {
201 DBGC ( options, "DHCPOPT %p could not "
202 "reallocate to %zd bytes\n", options,
203 new_options_len );
204 return -ENOMEM;
206 options->data = new_data;
207 options->max_len = new_options_len;
208 } else {
209 DBGC ( options, "DHCPOPT %p out of space\n", options );
210 return -ENOMEM;
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",
218 options );
219 return -ENOSPC;
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 ) );
232 return 0;
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,
255 int can_realloc ) {
256 static const uint8_t empty_encapsulator[] = { DHCP_END };
257 int offset;
258 int encap_offset = -1;
259 int creation_offset = 0;
260 struct dhcp_option *option;
261 unsigned int encap_tag = DHCP_ENCAPSULATOR ( tag );
262 size_t old_len = 0;
263 size_t new_len = ( len ? ( len + DHCP_OPTION_HEADER_LEN ) : 0 );
264 int rc;
266 /* Sanity check */
267 if ( tag == DHCP_PAD )
268 return -ENOTTY;
270 /* Find old instance of this option, if any */
271 offset = find_dhcp_option_with_encap ( options, tag, &encap_offset );
272 if ( offset >= 0 ) {
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 );
276 } else {
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 */
282 if ( encap_tag ) {
283 if ( encap_offset < 0 )
284 encap_offset = set_dhcp_option ( options, encap_tag,
285 empty_encapsulator, 1,
286 can_realloc );
287 if ( encap_offset < 0 )
288 return encap_offset;
289 creation_offset = ( encap_offset + DHCP_OPTION_HEADER_LEN );
292 /* Create new option if necessary */
293 if ( offset < 0 )
294 offset = creation_offset;
296 /* Resize option to fit new data */
297 if ( ( rc = resize_dhcp_option ( options, offset, encap_offset,
298 old_len, new_len,
299 can_realloc ) ) != 0 )
300 return rc;
302 /* Copy new data into option, if applicable */
303 if ( len ) {
304 option = dhcp_option ( options, offset );
305 option->tag = tag;
306 option->len = len;
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 );
317 return offset;
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 ) {
331 int offset;
333 offset = set_dhcp_option ( options, tag, data, len, 0 );
334 if ( offset < 0 )
335 return offset;
336 return 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 ) {
350 int offset;
352 offset = set_dhcp_option ( options, tag, data, len, 1 );
353 if ( offset < 0 )
354 return offset;
355 return 0;
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 ) {
369 int offset;
370 struct dhcp_option *option;
371 size_t option_len;
373 offset = find_dhcp_option_with_encap ( options, tag, NULL );
374 if ( offset < 0 )
375 return offset;
377 option = dhcp_option ( options, offset );
378 option_len = option->len;
379 if ( len > option_len )
380 len = option_len;
381 memcpy ( data, option->data, len );
383 return option_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;
396 int offset = 0;
397 ssize_t remaining = options->max_len;
398 unsigned int option_len;
400 /* Find last non-pad option */
401 options->len = 0;
402 while ( remaining ) {
403 option = dhcp_option ( options, offset );
404 option_len = dhcp_option_len ( option );
405 remaining -= option_len;
406 if ( remaining < 0 )
407 break;
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,
425 size_t max_len ) {
427 /* Fill in fields */
428 options->data = data;
429 options->max_len = max_len;
431 /* Update length */
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 );