iPXE
dhcpopts.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3 *
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.
8 *
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.
13 *
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., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stdint.h>
28#include <stdlib.h>
29#include <stdio.h>
30#include <errno.h>
31#include <string.h>
32#include <ipxe/dhcp.h>
33#include <ipxe/dhcpopts.h>
34
35/** @file
36 *
37 * DHCP options
38 *
39 */
40
41/**
42 * Obtain printable version of a DHCP option tag
43 *
44 * @v tag DHCP option tag
45 * @ret name String representation of the tag
46 *
47 */
48static inline char * dhcp_tag_name ( unsigned int tag ) {
49 static char name[8];
50
51 if ( DHCP_IS_ENCAP_OPT ( tag ) ) {
52 snprintf ( name, sizeof ( name ), "%d.%d",
55 } else {
56 snprintf ( name, sizeof ( name ), "%d", tag );
57 }
58 return name;
59}
60
61/**
62 * Get pointer to DHCP option
63 *
64 * @v options DHCP options block
65 * @v offset Offset within options block
66 * @ret option DHCP option
67 */
68static inline __attribute__ (( always_inline )) struct dhcp_option *
69dhcp_option ( struct dhcp_options *options, unsigned int offset ) {
70 return ( ( struct dhcp_option * ) ( options->data + offset ) );
71}
72
73/**
74 * Get offset of a DHCP option
75 *
76 * @v options DHCP options block
77 * @v option DHCP option
78 * @ret offset Offset within options block
79 */
80static inline __attribute__ (( always_inline )) int
82 struct dhcp_option *option ) {
83 return ( ( ( void * ) option ) - options->data );
84}
85
86/**
87 * Calculate length of any DHCP option
88 *
89 * @v option DHCP option
90 * @v remaining Remaining space
91 * @ret len Length (including tag and length field)
92 */
93static unsigned int dhcp_option_len ( struct dhcp_option *option,
94 size_t remaining ) {
95 if ( ( option->tag == DHCP_END ) || ( option->tag == DHCP_PAD ) ||
96 ( remaining < DHCP_OPTION_HEADER_LEN ) ) {
97 return 1;
98 } else {
99 return ( option->len + DHCP_OPTION_HEADER_LEN );
100 }
101}
102
103/**
104 * Find DHCP option within DHCP options block, and its encapsulator (if any)
105 *
106 * @v options DHCP options block
107 * @v tag DHCP option tag to search for
108 * @ret encap_offset Offset of encapsulating DHCP option
109 * @ret offset Offset of DHCP option, or negative error
110 *
111 * Searches for the DHCP option matching the specified tag within the
112 * DHCP option block. Encapsulated options may be searched for by
113 * using DHCP_ENCAP_OPT() to construct the tag value.
114 *
115 * If the option is encapsulated, and @c encap_offset is non-NULL, it
116 * will be filled in with the offset of the encapsulating option.
117 *
118 * This routine is designed to be paranoid. It does not assume that
119 * the option data is well-formatted, and so must guard against flaws
120 * such as options missing a @c DHCP_END terminator, or options whose
121 * length would take them beyond the end of the data block.
122 */
124 unsigned int tag,
125 int *encap_offset ) {
126 unsigned int original_tag __attribute__ (( unused )) = tag;
127 struct dhcp_option *option;
128 int offset = 0;
129 ssize_t remaining = options->used_len;
130 unsigned int option_len;
131
132 /* Sanity check */
133 if ( tag == DHCP_PAD )
134 return -ENOENT;
135
136 /* Search for option */
137 while ( remaining ) {
138 /* Calculate length of this option. Abort processing
139 * if the length is malformed (i.e. takes us beyond
140 * the end of the data block).
141 */
143 option_len = dhcp_option_len ( option, remaining );
144 remaining -= option_len;
145 if ( remaining < 0 )
146 break;
147 /* Check for explicit end marker */
148 if ( option->tag == DHCP_END ) {
149 if ( tag == DHCP_END )
150 /* Special case where the caller is interested
151 * in whether we have this marker or not.
152 */
153 return offset;
154 else
155 break;
156 }
157 /* Check for matching tag */
158 if ( option->tag == tag ) {
159 DBGC ( options, "DHCPOPT %p found %s (length %d)\n",
160 options, dhcp_tag_name ( original_tag ),
161 option_len );
162 return offset;
163 }
164 /* Check for start of matching encapsulation block */
165 if ( DHCP_IS_ENCAP_OPT ( tag ) &&
166 ( option->tag == DHCP_ENCAPSULATOR ( tag ) ) &&
167 ( option_len >= DHCP_OPTION_HEADER_LEN ) ) {
168 if ( encap_offset )
169 *encap_offset = offset;
170 /* Continue search within encapsulated option block */
172 remaining = ( option_len - DHCP_OPTION_HEADER_LEN );
174 continue;
175 }
176 offset += option_len;
177 }
178
179 return -ENOENT;
180}
181
182/**
183 * Refuse to reallocate DHCP option block
184 *
185 * @v options DHCP option block
186 * @v len New length
187 * @ret rc Return status code
188 */
190 return ( ( len <= options->alloc_len ) ? 0 : -ENOSPC );
191}
192
193/**
194 * Resize a DHCP option
195 *
196 * @v options DHCP option block
197 * @v offset Offset of option to resize
198 * @v encap_offset Offset of encapsulating offset (or -ve for none)
199 * @v old_len Old length (including header)
200 * @v new_len New length (including header)
201 * @ret rc Return status code
202 */
204 int offset, int encap_offset,
205 size_t old_len, size_t new_len ) {
206 struct dhcp_option *encapsulator;
207 struct dhcp_option *option;
208 ssize_t delta = ( new_len - old_len );
209 size_t old_alloc_len;
210 size_t new_used_len;
211 size_t new_encapsulator_len;
212 void *source;
213 void *dest;
214 int rc;
215
216 /* Check for sufficient space */
217 if ( new_len > DHCP_MAX_LEN ) {
218 DBGC ( options, "DHCPOPT %p overlength option\n", options );
219 return -ENOSPC;
220 }
221 new_used_len = ( options->used_len + delta );
222
223 /* Expand options block, if necessary */
224 if ( new_used_len > options->alloc_len ) {
225 /* Reallocate options block */
226 old_alloc_len = options->alloc_len;
227 if ( ( rc = options->realloc ( options, new_used_len ) ) != 0 ){
228 DBGC ( options, "DHCPOPT %p could not reallocate to "
229 "%zd bytes\n", options, new_used_len );
230 return rc;
231 }
232 /* Clear newly allocated space */
233 memset ( ( options->data + old_alloc_len ), 0,
234 ( options->alloc_len - old_alloc_len ) );
235 }
236
237 /* Update encapsulator, if applicable */
238 if ( encap_offset >= 0 ) {
239 encapsulator = dhcp_option ( options, encap_offset );
240 new_encapsulator_len = ( encapsulator->len + delta );
241 if ( new_encapsulator_len > DHCP_MAX_LEN ) {
242 DBGC ( options, "DHCPOPT %p overlength encapsulator\n",
243 options );
244 return -ENOSPC;
245 }
246 encapsulator->len = new_encapsulator_len;
247 }
248
249 /* Update used length */
250 options->used_len = new_used_len;
251
252 /* Move remainder of option data */
254 source = ( ( ( void * ) option ) + old_len );
255 dest = ( ( ( void * ) option ) + new_len );
256 memmove ( dest, source, ( new_used_len - offset - new_len ) );
257
258 /* Shrink options block, if applicable */
259 if ( new_used_len < options->alloc_len ) {
260 if ( ( rc = options->realloc ( options, new_used_len ) ) != 0 ){
261 DBGC ( options, "DHCPOPT %p could not reallocate to "
262 "%zd bytes\n", options, new_used_len );
263 return rc;
264 }
265 }
266
267 return 0;
268}
269
270/**
271 * Set value of DHCP option
272 *
273 * @v options DHCP option block
274 * @v tag DHCP option tag
275 * @v data New value for DHCP option
276 * @v len Length of value, in bytes
277 * @ret offset Offset of DHCP option, or negative error
278 *
279 * Sets the value of a DHCP option within the options block. The
280 * option may or may not already exist. Encapsulators will be created
281 * (and deleted) as necessary.
282 *
283 * This call may fail due to insufficient space in the options block.
284 * If it does fail, and the option existed previously, the option will
285 * be left with its original value.
286 */
287static int set_dhcp_option ( struct dhcp_options *options, unsigned int tag,
288 const void *data, size_t len ) {
289 static const uint8_t empty_encap[] = { DHCP_END };
290 int offset;
291 int encap_offset = -1;
292 int creation_offset;
293 struct dhcp_option *option;
294 unsigned int encap_tag = DHCP_ENCAPSULATOR ( tag );
295 size_t old_len = 0;
296 size_t new_len = ( len ? ( len + DHCP_OPTION_HEADER_LEN ) : 0 );
297 int rc;
298
299 /* Sanity check */
300 if ( tag == DHCP_PAD )
301 return -ENOTTY;
302
303 creation_offset = find_dhcp_option_with_encap ( options, DHCP_END,
304 NULL );
305 if ( creation_offset < 0 )
306 creation_offset = options->used_len;
307 /* Find old instance of this option, if any */
308 offset = find_dhcp_option_with_encap ( options, tag, &encap_offset );
309 if ( offset >= 0 ) {
310 old_len = dhcp_option_len ( dhcp_option ( options, offset ),
311 ( options->used_len - offset ) );
312 DBGC ( options, "DHCPOPT %p resizing %s from %zd to %zd\n",
313 options, dhcp_tag_name ( tag ), old_len, new_len );
314 } else {
315 DBGC ( options, "DHCPOPT %p creating %s (length %zd)\n",
316 options, dhcp_tag_name ( tag ), new_len );
317 }
318
319 /* Ensure that encapsulator exists, if required */
320 if ( encap_tag ) {
321 if ( encap_offset < 0 ) {
322 encap_offset =
323 set_dhcp_option ( options, encap_tag,
324 empty_encap,
325 sizeof ( empty_encap ) );
326 }
327 if ( encap_offset < 0 )
328 return encap_offset;
329 creation_offset = ( encap_offset + DHCP_OPTION_HEADER_LEN );
330 }
331
332 /* Create new option if necessary */
333 if ( offset < 0 )
334 offset = creation_offset;
335
336 /* Resize option to fit new data */
337 if ( ( rc = resize_dhcp_option ( options, offset, encap_offset,
338 old_len, new_len ) ) != 0 )
339 return rc;
340
341 /* Copy new data into option, if applicable */
342 if ( len ) {
344 option->tag = tag;
345 option->len = len;
346 memcpy ( &option->data, data, len );
347 }
348
349 /* Delete encapsulator if there's nothing else left in it */
350 if ( encap_offset >= 0 ) {
351 option = dhcp_option ( options, encap_offset );
352 if ( option->len <= 1 )
353 set_dhcp_option ( options, encap_tag, NULL, 0 );
354 }
355
356 return offset;
357}
358
359/**
360 * Check applicability of DHCP option setting
361 *
362 * @v tag Setting tag number
363 * @ret applies Setting applies to this option block
364 */
365int dhcpopt_applies ( unsigned int tag ) {
366
367 return ( tag && ( tag <= DHCP_ENCAP_OPT ( DHCP_MAX_OPTION,
368 DHCP_MAX_OPTION ) ) );
369}
370
371/**
372 * Store value of DHCP option setting
373 *
374 * @v options DHCP option block
375 * @v tag Setting tag number
376 * @v data Setting data, or NULL to clear setting
377 * @v len Length of setting data
378 * @ret rc Return status code
379 */
380int dhcpopt_store ( struct dhcp_options *options, unsigned int tag,
381 const void *data, size_t len ) {
382 int offset;
383
385 if ( offset < 0 )
386 return offset;
387 return 0;
388}
389
390/**
391 * Fetch value of DHCP option setting
392 *
393 * @v options DHCP option block
394 * @v tag Setting tag number
395 * @v data Buffer to fill with setting data
396 * @v len Length of buffer
397 * @ret len Length of setting data, or negative error
398 */
399int dhcpopt_fetch ( struct dhcp_options *options, unsigned int tag,
400 void *data, size_t len ) {
401 int offset;
402 struct dhcp_option *option;
403 size_t option_len;
404
406 if ( offset < 0 )
407 return offset;
408
410 option_len = option->len;
411 if ( len > option_len )
412 len = option_len;
413 memcpy ( data, option->data, len );
414
415 return option_len;
416}
417
418/**
419 * Recalculate length of DHCP options block
420 *
421 * @v options Uninitialised DHCP option block
422 *
423 * The "used length" field will be updated based on scanning through
424 * the block to find the end of the options.
425 */
427 struct dhcp_option *option;
428 int offset = 0;
429 ssize_t remaining = options->alloc_len;
430 unsigned int option_len;
431
432 /* Find last non-pad option */
433 options->used_len = 0;
434 while ( remaining ) {
436 option_len = dhcp_option_len ( option, remaining );
437 remaining -= option_len;
438 if ( remaining < 0 )
439 break;
440 offset += option_len;
441 if ( option->tag != DHCP_PAD )
442 options->used_len = offset;
443 }
444}
445
446/**
447 * Initialise prepopulated block of DHCP options
448 *
449 * @v options Uninitialised DHCP option block
450 * @v data Memory for DHCP option data
451 * @v alloc_len Length of memory for DHCP option data
452 * @v realloc DHCP option block reallocator
453 *
454 * The memory content must already be filled with valid DHCP options.
455 * A zeroed block counts as a block of valid DHCP options.
456 */
457void dhcpopt_init ( struct dhcp_options *options, void *data, size_t alloc_len,
458 int ( * realloc ) ( struct dhcp_options *options,
459 size_t len ) ) {
460
461 /* Fill in fields */
462 options->data = data;
463 options->alloc_len = alloc_len;
464 options->realloc = realloc;
465
466 /* Update length */
468
469 DBGC ( options, "DHCPOPT %p created (data %p lengths %#zx,%#zx)\n",
470 options, options->data, options->used_len, options->alloc_len );
471}
static int options
Definition 3c515.c:286
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
unsigned char uint8_t
Definition stdint.h:10
signed long ssize_t
Definition stdint.h:7
if(len >=6 *4) __asm__ __volatile__("movsl" if(len >=5 *4) __asm__ __volatile__("movsl" if(len >=4 *4) __asm__ __volatile__("movsl" if(len >=3 *4) __asm__ __volatile__("movsl" if(len >=2 *4) __asm__ __volatile__("movsl" if(len >=1 *4) __asm__ __volatile__("movsl" if((len % 4) >=2) __asm__ __volatile__("movsw" if((len % 2) >=1) __asm__ __volatile__("movsb" retur dest)
Definition string.h:151
const char * name
Definition ath9k_hw.c:1986
uint16_t offset
Offset to command line.
Definition bzimage.h:3
int dhcpopt_no_realloc(struct dhcp_options *options, size_t len)
Refuse to reallocate DHCP option block.
Definition dhcpopts.c:189
int dhcpopt_applies(unsigned int tag)
Check applicability of DHCP option setting.
Definition dhcpopts.c:365
static int set_dhcp_option(struct dhcp_options *options, unsigned int tag, const void *data, size_t len)
Set value of DHCP option.
Definition dhcpopts.c:287
void dhcpopt_init(struct dhcp_options *options, void *data, size_t alloc_len, int(*realloc)(struct dhcp_options *options, size_t len))
Initialise prepopulated block of DHCP options.
Definition dhcpopts.c:457
int dhcpopt_fetch(struct dhcp_options *options, unsigned int tag, void *data, size_t len)
Fetch value of DHCP option setting.
Definition dhcpopts.c:399
void dhcpopt_update_used_len(struct dhcp_options *options)
Recalculate length of DHCP options block.
Definition dhcpopts.c:426
int dhcpopt_store(struct dhcp_options *options, unsigned int tag, const void *data, size_t len)
Store value of DHCP option setting.
Definition dhcpopts.c:380
static int find_dhcp_option_with_encap(struct dhcp_options *options, unsigned int tag, int *encap_offset)
Find DHCP option within DHCP options block, and its encapsulator (if any).
Definition dhcpopts.c:123
static unsigned int dhcp_option_len(struct dhcp_option *option, size_t remaining)
Calculate length of any DHCP option.
Definition dhcpopts.c:93
static char * dhcp_tag_name(unsigned int tag)
Obtain printable version of a DHCP option tag.
Definition dhcpopts.c:48
static struct dhcp_option * dhcp_option(struct dhcp_options *options, unsigned int offset)
Get pointer to DHCP option.
Definition dhcpopts.c:69
static int dhcp_option_offset(struct dhcp_options *options, struct dhcp_option *option)
Get offset of a DHCP option.
Definition dhcpopts.c:81
static int resize_dhcp_option(struct dhcp_options *options, int offset, int encap_offset, size_t old_len, size_t new_len)
Resize a DHCP option.
Definition dhcpopts.c:203
DHCP options.
ring len
Length.
Definition dwmac.h:226
uint64_t tag
Identity tag.
Definition edd.h:1
uint8_t data[48]
Additional event data.
Definition ena.h:11
Error codes.
#define DBGC(...)
Definition compiler.h:530
#define DHCP_PAD
Padding.
Definition dhcp.h:60
#define DHCP_MAX_OPTION
Maximum normal DHCP option.
Definition dhcp.h:542
#define DHCP_END
End of options.
Definition dhcp.h:549
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define ENOENT
No such file or directory.
Definition errno.h:515
#define ENOSPC
No space left on device.
Definition errno.h:550
#define ENOTTY
Inappropriate I/O control operation.
Definition errno.h:595
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
#define __attribute__(x)
Definition compiler.h:10
Dynamic Host Configuration Protocol.
#define DHCP_ENCAP_OPT(encapsulator, encapsulated)
Construct a tag value for an encapsulated option.
Definition dhcp.h:41
#define DHCP_ENCAPSULATOR(encap_opt)
Extract encapsulating option block tag from encapsulated tag value.
Definition dhcp.h:44
#define DHCP_OPTION_HEADER_LEN
Length of a DHCP option header.
Definition dhcp.h:607
#define DHCP_IS_ENCAP_OPT(opt)
Option is encapsulated.
Definition dhcp.h:48
#define DHCP_ENCAPSULATED(encap_opt)
Extract encapsulated option tag from encapsulated tag value.
Definition dhcp.h:46
#define DHCP_MAX_LEN
Maximum length for a single DHCP option.
Definition dhcp.h:610
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * memset(void *dest, int character, size_t len) __nonnull
void * memmove(void *dest, const void *src, size_t len) __nonnull
uint8_t unused
Unused.
Definition librm.h:5
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition malloc.c:607
A DHCP option.
Definition dhcp.h:582
uint8_t len
Length.
Definition dhcp.h:596
A DHCP options block.
Definition dhcpopts.h:16
A long option, as used for getopt_long().
Definition getopt.h:25
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition vsprintf.c:383