1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19FILE_LICENCE ( GPL2_OR_LATER );
20
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>
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42static inline char * dhcp_tag_name ( unsigned int tag ) {
43 static char name[8];
44
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 );
51 }
52 return name;
53}
54
55
56
57
58
59
60
61
62static inline __attribute__ (( always_inline )) struct dhcp_option *
63dhcp_option ( struct dhcp_options *options, unsigned int offset ) {
64 return ( ( struct dhcp_option * ) ( options->data + offset ) );
65}
66
67
68
69
70
71
72
73
74static inline __attribute__ (( always_inline )) int
75dhcp_option_offset ( struct dhcp_options *options,
76 struct dhcp_option *option ) {
77 return ( ( ( void * ) option ) - options->data );
78}
79
80
81
82
83
84
85
86static 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 );
91 }
92}
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114static 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;
122
123
124 if ( tag == DHCP_PAD )
125 return -ENOENT;
126
127
128 while ( remaining ) {
129
130
131
132
133 option = dhcp_option ( options, offset );
134 option_len = dhcp_option_len ( option );
135 remaining -= option_len;
136 if ( remaining < 0 )
137 break;
138
139 if ( option->tag == DHCP_END ) {
140 if ( tag == DHCP_END )
141
142
143
144 return offset;
145 else
146 break;
147 }
148
149 if ( option->tag == tag ) {
150 DBGC ( options, "DHCPOPT %p found %s (length %d)\n",
151 options, dhcp_tag_name ( original_tag ),
152 option_len );
153 return offset;
154 }
155
156 if ( DHCP_IS_ENCAP_OPT ( tag ) &&
157 ( option->tag == DHCP_ENCAPSULATOR ( tag ) ) ) {
158 if ( encap_offset )
159 *encap_offset = offset;
160
161 tag = DHCP_ENCAPSULATED ( tag );
162 remaining = option_len;
163 offset += DHCP_OPTION_HEADER_LEN;
164 continue;
165 }
166 offset += option_len;
167 }
168
169 return -ENOENT;
170}
171
172
173
174
175
176
177
178
179
180
181
182
183static int resize_dhcp_option ( struct dhcp_options *options,
184 int offset, int encap_offset,
185 size_t old_len, size_t new_len,
186 int can_realloc ) {
187 struct dhcp_option *encapsulator;
188 struct dhcp_option *option;
189 ssize_t delta = ( new_len - old_len );
190 size_t new_options_len;
191 size_t new_encapsulator_len;
192 void *new_data;
193 void *source;
194 void *dest;
195 void *end;
196
197
198 if ( new_len > DHCP_MAX_LEN ) {
199 DBGC ( options, "DHCPOPT %p overlength option\n", options );
200 return -ENOSPC;
201 }
202 new_options_len = ( options->len + delta );
203 if ( new_options_len > options->max_len ) {
204
205 if ( can_realloc ) {
206 new_data = realloc ( options->data, new_options_len );
207 if ( ! new_data ) {
208 DBGC ( options, "DHCPOPT %p could not "
209 "reallocate to %zd bytes\n", options,
210 new_options_len );
211 return -ENOMEM;
212 }
213 options->data = new_data;
214 options->max_len = new_options_len;
215 } else {
216 DBGC ( options, "DHCPOPT %p out of space\n", options );
217 return -ENOMEM;
218 }
219 }
220 if ( encap_offset >= 0 ) {
221 encapsulator = dhcp_option ( options, encap_offset );
222 new_encapsulator_len = ( encapsulator->len + delta );
223 if ( new_encapsulator_len > DHCP_MAX_LEN ) {
224 DBGC ( options, "DHCPOPT %p overlength encapsulator\n",
225 options );
226 return -ENOSPC;
227 }
228 encapsulator->len = new_encapsulator_len;
229 }
230 options->len = new_options_len;
231
232
233 option = dhcp_option ( options, offset );
234 source = ( ( ( void * ) option ) + old_len );
235 dest = ( ( ( void * ) option ) + new_len );
236 end = ( options->data + options->max_len );
237 memmove ( dest, source, ( end - dest ) );
238
239 return 0;
240}
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260static int set_dhcp_option ( struct dhcp_options *options, unsigned int tag,
261 const void *data, size_t len,
262 int can_realloc ) {
263 static const uint8_t empty_encapsulator[] = { DHCP_END };
264 int offset;
265 int encap_offset = -1;
266 int creation_offset;
267 struct dhcp_option *option;
268 unsigned int encap_tag = DHCP_ENCAPSULATOR ( tag );
269 size_t old_len = 0;
270 size_t new_len = ( len ? ( len + DHCP_OPTION_HEADER_LEN ) : 0 );
271 int rc;
272
273
274 if ( tag == DHCP_PAD )
275 return -ENOTTY;
276
277 creation_offset = find_dhcp_option_with_encap ( options, DHCP_END,
278 NULL );
279 if ( creation_offset < 0 )
280 creation_offset = options->len;
281
282 offset = find_dhcp_option_with_encap ( options, tag, &encap_offset );
283 if ( offset >= 0 ) {
284 old_len = dhcp_option_len ( dhcp_option ( options, offset ) );
285 DBGC ( options, "DHCPOPT %p resizing %s from %zd to %zd\n",
286 options, dhcp_tag_name ( tag ), old_len, new_len );
287 } else {
288 DBGC ( options, "DHCPOPT %p creating %s (length %zd)\n",
289 options, dhcp_tag_name ( tag ), new_len );
290 }
291
292
293 if ( encap_tag ) {
294 if ( encap_offset < 0 )
295 encap_offset = set_dhcp_option ( options, encap_tag,
296 empty_encapsulator, 1,
297 can_realloc );
298 if ( encap_offset < 0 )
299 return encap_offset;
300 creation_offset = ( encap_offset + DHCP_OPTION_HEADER_LEN );
301 }
302
303
304 if ( offset < 0 )
305 offset = creation_offset;
306
307
308 if ( ( rc = resize_dhcp_option ( options, offset, encap_offset,
309 old_len, new_len,
310 can_realloc ) ) != 0 )
311 return rc;
312
313
314 if ( len ) {
315 option = dhcp_option ( options, offset );
316 option->tag = tag;
317 option->len = len;
318 memcpy ( &option->data, data, len );
319 }
320
321
322 if ( encap_offset >= 0 ) {
323 option = dhcp_option ( options, encap_offset );
324 if ( option->len <= 1 )
325 set_dhcp_option ( options, encap_tag, NULL, 0, 0 );
326 }
327
328 return offset;
329}
330
331
332
333
334
335
336
337
338
339
340int dhcpopt_store ( struct dhcp_options *options, unsigned int tag,
341 const void *data, size_t len ) {
342 int offset;
343
344 offset = set_dhcp_option ( options, tag, data, len, 0 );
345 if ( offset < 0 )
346 return offset;
347 return 0;
348}
349
350
351
352
353
354
355
356
357
358
359int dhcpopt_extensible_store ( struct dhcp_options *options, unsigned int tag,
360 const void *data, size_t len ) {
361 int offset;
362
363 offset = set_dhcp_option ( options, tag, data, len, 1 );
364 if ( offset < 0 )
365 return offset;
366 return 0;
367}
368
369
370
371
372
373
374
375
376
377
378int dhcpopt_fetch ( struct dhcp_options *options, unsigned int tag,
379 void *data, size_t len ) {
380 int offset;
381 struct dhcp_option *option;
382 size_t option_len;
383
384 offset = find_dhcp_option_with_encap ( options, tag, NULL );
385 if ( offset < 0 )
386 return offset;
387
388 option = dhcp_option ( options, offset );
389 option_len = option->len;
390 if ( len > option_len )
391 len = option_len;
392 memcpy ( data, option->data, len );
393
394 return option_len;
395}
396
397
398
399
400
401
402
403
404
405static void dhcpopt_update_len ( struct dhcp_options *options ) {
406 struct dhcp_option *option;
407 int offset = 0;
408 ssize_t remaining = options->max_len;
409 unsigned int option_len;
410
411
412 options->len = 0;
413 while ( remaining ) {
414 option = dhcp_option ( options, offset );
415 option_len = dhcp_option_len ( option );
416 remaining -= option_len;
417 if ( remaining < 0 )
418 break;
419 offset += option_len;
420 if ( option->tag != DHCP_PAD )
421 options->len = offset;
422 }
423}
424
425
426
427
428
429
430
431
432
433
434
435void dhcpopt_init ( struct dhcp_options *options, void *data,
436 size_t max_len ) {
437
438
439 options->data = data;
440 options->max_len = max_len;
441
442
443 dhcpopt_update_len ( options );
444
445 DBGC ( options, "DHCPOPT %p created (data %p len %#zx max_len %#zx)\n",
446 options, options->data, options->len, options->max_len );
447}
448