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