|
|
@@ -34,6 +34,10 @@
|
|
34
|
34
|
#include <gpxe/uuid.h>
|
|
35
|
35
|
#include <gpxe/dhcp.h>
|
|
36
|
36
|
#include <gpxe/timer.h>
|
|
|
37
|
+#include <gpxe/settings.h>
|
|
|
38
|
+#include <gpxe/dhcp.h>
|
|
|
39
|
+#include <gpxe/dhcpopts.h>
|
|
|
40
|
+#include <gpxe/dhcppkt.h>
|
|
37
|
41
|
|
|
38
|
42
|
/** @file
|
|
39
|
43
|
*
|
|
|
@@ -41,7 +45,8 @@
|
|
41
|
45
|
*
|
|
42
|
46
|
*/
|
|
43
|
47
|
|
|
44
|
|
-/** DHCP operation types
|
|
|
48
|
+/**
|
|
|
49
|
+ * DHCP operation types
|
|
45
|
50
|
*
|
|
46
|
51
|
* This table maps from DHCP message types (i.e. values of the @c
|
|
47
|
52
|
* DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
|
|
|
@@ -76,6 +81,13 @@ static uint8_t dhcp_request_options_data[] = {
|
|
76
|
81
|
DHCP_END
|
|
77
|
82
|
};
|
|
78
|
83
|
|
|
|
84
|
+/** Options common to all DHCP requests */
|
|
|
85
|
+static struct dhcp_options dhcp_request_options = {
|
|
|
86
|
+ .data = dhcp_request_options_data,
|
|
|
87
|
+ .max_len = sizeof ( dhcp_request_options_data ),
|
|
|
88
|
+ .len = sizeof ( dhcp_request_options_data ),
|
|
|
89
|
+};
|
|
|
90
|
+
|
|
79
|
91
|
/** DHCP feature codes */
|
|
80
|
92
|
static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
|
|
81
|
93
|
static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
|
|
|
@@ -118,197 +130,33 @@ static uint32_t dhcp_xid ( struct net_device *netdev ) {
|
|
118
|
130
|
return xid;
|
|
119
|
131
|
}
|
|
120
|
132
|
|
|
121
|
|
-/** Options common to all DHCP requests */
|
|
122
|
|
-static struct dhcp_option_block dhcp_request_options = {
|
|
123
|
|
- .data = dhcp_request_options_data,
|
|
124
|
|
- .max_len = sizeof ( dhcp_request_options_data ),
|
|
125
|
|
- .len = sizeof ( dhcp_request_options_data ),
|
|
126
|
|
-};
|
|
127
|
|
-
|
|
128
|
|
-/**
|
|
129
|
|
- * Set option within DHCP packet
|
|
130
|
|
- *
|
|
131
|
|
- * @v dhcppkt DHCP packet
|
|
132
|
|
- * @v tag DHCP option tag
|
|
133
|
|
- * @v data New value for DHCP option
|
|
134
|
|
- * @v len Length of value, in bytes
|
|
135
|
|
- * @ret rc Return status code
|
|
136
|
|
- *
|
|
137
|
|
- * Sets the option within the first available options block within the
|
|
138
|
|
- * DHCP packet. Option blocks are tried in the order specified by @c
|
|
139
|
|
- * dhcp_option_block_fill_order.
|
|
140
|
|
- *
|
|
141
|
|
- * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
|
|
142
|
|
- * intercepted and inserted into the appropriate fixed fields within
|
|
143
|
|
- * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
|
|
144
|
|
- * ignored, since our DHCP packet assembly method relies on always
|
|
145
|
|
- * having option overloading in use.
|
|
146
|
|
- */
|
|
147
|
|
-static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
|
|
148
|
|
- unsigned int tag, const void *data,
|
|
149
|
|
- size_t len ) {
|
|
150
|
|
- struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
|
|
151
|
|
- struct dhcp_option_block *options = &dhcppkt->options;
|
|
152
|
|
- struct dhcp_option *option = NULL;
|
|
153
|
|
-
|
|
154
|
|
- /* Special-case the magic options */
|
|
155
|
|
- switch ( tag ) {
|
|
156
|
|
- case DHCP_OPTION_OVERLOAD:
|
|
157
|
|
- /* Hard-coded in packets we create; always ignore */
|
|
158
|
|
- return 0;
|
|
159
|
|
- case DHCP_EB_YIADDR:
|
|
160
|
|
- memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
|
|
161
|
|
- return 0;
|
|
162
|
|
- case DHCP_EB_SIADDR:
|
|
163
|
|
- memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
|
|
164
|
|
- return 0;
|
|
165
|
|
- case DHCP_TFTP_SERVER_NAME:
|
|
166
|
|
- memset ( dhcphdr->sname, 0, sizeof ( dhcphdr->sname ) );
|
|
167
|
|
- if ( len > sizeof ( dhcphdr->sname ) )
|
|
168
|
|
- len = sizeof ( dhcphdr->sname );
|
|
169
|
|
- memcpy ( dhcphdr->sname, data, len );
|
|
170
|
|
- return 0;
|
|
171
|
|
- case DHCP_BOOTFILE_NAME:
|
|
172
|
|
- memset ( dhcphdr->file, 0, sizeof ( dhcphdr->file ) );
|
|
173
|
|
- if ( len > sizeof ( dhcphdr->file ) )
|
|
174
|
|
- len = sizeof ( dhcphdr->file );
|
|
175
|
|
- memcpy ( dhcphdr->file, data, len );
|
|
176
|
|
- return 0;
|
|
177
|
|
- default:
|
|
178
|
|
- /* Continue processing as normal */
|
|
179
|
|
- break;
|
|
180
|
|
- }
|
|
181
|
|
-
|
|
182
|
|
- /* Set option */
|
|
183
|
|
- option = set_dhcp_option ( options, tag, data, len );
|
|
184
|
|
-
|
|
185
|
|
- /* Update DHCP packet length */
|
|
186
|
|
- dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
|
|
187
|
|
- + dhcppkt->options.len );
|
|
188
|
|
-
|
|
189
|
|
- return ( option ? 0 : -ENOSPC );
|
|
190
|
|
-}
|
|
191
|
|
-
|
|
192
|
|
-/**
|
|
193
|
|
- * Copy option into DHCP packet
|
|
194
|
|
- *
|
|
195
|
|
- * @v dhcppkt DHCP packet
|
|
196
|
|
- * @v options DHCP option block, or NULL
|
|
197
|
|
- * @v tag DHCP option tag to search for
|
|
198
|
|
- * @v new_tag DHCP option tag to use for copied option
|
|
199
|
|
- * @ret rc Return status code
|
|
200
|
|
- *
|
|
201
|
|
- * Copies a single option, if present, from the DHCP options block
|
|
202
|
|
- * into a DHCP packet. The tag for the option may be changed if
|
|
203
|
|
- * desired; this is required by other parts of the DHCP code.
|
|
204
|
|
- *
|
|
205
|
|
- * @c options may specify a single options block, or be left as NULL
|
|
206
|
|
- * in order to search for the option within all registered options
|
|
207
|
|
- * blocks.
|
|
208
|
|
- */
|
|
209
|
|
-static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
|
|
210
|
|
- struct dhcp_option_block *options,
|
|
211
|
|
- unsigned int tag, unsigned int new_tag ) {
|
|
212
|
|
- struct dhcp_option *option;
|
|
213
|
|
- int rc;
|
|
214
|
|
-
|
|
215
|
|
- option = find_dhcp_option ( options, tag );
|
|
216
|
|
- if ( option ) {
|
|
217
|
|
- if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
|
|
218
|
|
- &option->data,
|
|
219
|
|
- option->len ) ) != 0 )
|
|
220
|
|
- return rc;
|
|
221
|
|
- }
|
|
222
|
|
- return 0;
|
|
223
|
|
-}
|
|
224
|
|
-
|
|
225
|
|
-/**
|
|
226
|
|
- * Copy options into DHCP packet
|
|
227
|
|
- *
|
|
228
|
|
- * @v dhcppkt DHCP packet
|
|
229
|
|
- * @v options DHCP option block, or NULL
|
|
230
|
|
- * @v encapsulator Encapsulating option, or zero
|
|
231
|
|
- * @ret rc Return status code
|
|
232
|
|
- *
|
|
233
|
|
- * Copies options with the specified encapsulator from DHCP options
|
|
234
|
|
- * blocks into a DHCP packet. Most options are copied verbatim.
|
|
235
|
|
- * Recognised encapsulated options fields are handled as such.
|
|
236
|
|
- *
|
|
237
|
|
- * @c options may specify a single options block, or be left as NULL
|
|
238
|
|
- * in order to copy options from all registered options blocks.
|
|
239
|
|
- */
|
|
240
|
|
-static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
|
|
241
|
|
- struct dhcp_option_block *options,
|
|
242
|
|
- unsigned int encapsulator ) {
|
|
243
|
|
- unsigned int subtag;
|
|
244
|
|
- unsigned int tag;
|
|
245
|
|
- int rc;
|
|
246
|
|
-
|
|
247
|
|
- for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
|
|
248
|
|
- tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
|
|
249
|
|
- switch ( tag ) {
|
|
250
|
|
- case DHCP_EB_ENCAP:
|
|
251
|
|
- case DHCP_VENDOR_ENCAP:
|
|
252
|
|
- /* Process encapsulated options field */
|
|
253
|
|
- if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
|
|
254
|
|
- options,
|
|
255
|
|
- tag)) !=0)
|
|
256
|
|
- return rc;
|
|
257
|
|
- break;
|
|
258
|
|
- default:
|
|
259
|
|
- /* Copy option to reassembled packet */
|
|
260
|
|
- if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
|
|
261
|
|
- tag, tag ) ) !=0)
|
|
262
|
|
- return rc;
|
|
263
|
|
- break;
|
|
264
|
|
- };
|
|
265
|
|
- }
|
|
266
|
|
-
|
|
267
|
|
- return 0;
|
|
268
|
|
-}
|
|
269
|
|
-
|
|
270
|
|
-/**
|
|
271
|
|
- * Copy options into DHCP packet
|
|
272
|
|
- *
|
|
273
|
|
- * @v dhcppkt DHCP packet
|
|
274
|
|
- * @v options DHCP option block, or NULL
|
|
275
|
|
- * @ret rc Return status code
|
|
276
|
|
- *
|
|
277
|
|
- * Copies options from DHCP options blocks into a DHCP packet. Most
|
|
278
|
|
- * options are copied verbatim. Recognised encapsulated options
|
|
279
|
|
- * fields are handled as such.
|
|
280
|
|
- *
|
|
281
|
|
- * @c options may specify a single options block, or be left as NULL
|
|
282
|
|
- * in order to copy options from all registered options blocks.
|
|
283
|
|
- */
|
|
284
|
|
-static int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
|
|
285
|
|
- struct dhcp_option_block *options ) {
|
|
286
|
|
- return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
|
|
287
|
|
-}
|
|
288
|
|
-
|
|
289
|
133
|
/**
|
|
290
|
134
|
* Create a DHCP packet
|
|
291
|
135
|
*
|
|
|
136
|
+ * @v dhcppkt DHCP packet structure to fill in
|
|
292
|
137
|
* @v netdev Network device
|
|
293
|
138
|
* @v msgtype DHCP message type
|
|
|
139
|
+ * @v options Initial options to include (or NULL)
|
|
294
|
140
|
* @v data Buffer for DHCP packet
|
|
295
|
141
|
* @v max_len Size of DHCP packet buffer
|
|
296
|
|
- * @v dhcppkt DHCP packet structure to fill in
|
|
297
|
142
|
* @ret rc Return status code
|
|
298
|
143
|
*
|
|
299
|
144
|
* Creates a DHCP packet in the specified buffer, and fills out a @c
|
|
300
|
145
|
* dhcp_packet structure that can be passed to
|
|
301
|
146
|
* set_dhcp_packet_option() or copy_dhcp_packet_options().
|
|
302
|
147
|
*/
|
|
303
|
|
-static int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
|
|
304
|
|
- void *data, size_t max_len,
|
|
305
|
|
- struct dhcp_packet *dhcppkt ) {
|
|
|
148
|
+static int create_dhcp_packet ( struct dhcp_packet *dhcppkt,
|
|
|
149
|
+ struct net_device *netdev, uint8_t msgtype,
|
|
|
150
|
+ struct dhcp_options *options,
|
|
|
151
|
+ void *data, size_t max_len ) {
|
|
306
|
152
|
struct dhcphdr *dhcphdr = data;
|
|
|
153
|
+ size_t options_len;
|
|
307
|
154
|
unsigned int hlen;
|
|
308
|
155
|
int rc;
|
|
309
|
156
|
|
|
310
|
157
|
/* Sanity check */
|
|
311
|
|
- if ( max_len < sizeof ( *dhcphdr ) )
|
|
|
158
|
+ options_len = ( options ? options->len : 0 );
|
|
|
159
|
+ if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
|
|
312
|
160
|
return -ENOSPC;
|
|
313
|
161
|
|
|
314
|
162
|
/* Initialise DHCP packet content */
|
|
|
@@ -327,180 +175,19 @@ static int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
|
|
327
|
175
|
}
|
|
328
|
176
|
dhcphdr->hlen = hlen;
|
|
329
|
177
|
memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
|
|
|
178
|
+ memcpy ( dhcphdr->options, options, options_len );
|
|
330
|
179
|
|
|
331
|
|
- /* Initialise DHCP packet structure */
|
|
332
|
|
- dhcppkt->dhcphdr = dhcphdr;
|
|
333
|
|
- dhcppkt->max_len = max_len;
|
|
334
|
|
- init_dhcp_options ( &dhcppkt->options, dhcphdr->options,
|
|
335
|
|
- ( max_len -
|
|
336
|
|
- offsetof ( typeof ( *dhcphdr ), options ) ) );
|
|
|
180
|
+ /* Initialise DHCP packet structure and settings interface */
|
|
|
181
|
+ dhcppkt_init ( dhcppkt, NULL, data, max_len );
|
|
337
|
182
|
|
|
338
|
183
|
/* Set DHCP_MESSAGE_TYPE option */
|
|
339
|
|
- if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
|
|
340
|
|
- &msgtype,
|
|
341
|
|
- sizeof ( msgtype ) ) ) != 0 )
|
|
|
184
|
+ if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_MESSAGE_TYPE,
|
|
|
185
|
+ &msgtype, sizeof ( msgtype ) ) ) != 0 )
|
|
342
|
186
|
return rc;
|
|
343
|
187
|
|
|
344
|
188
|
return 0;
|
|
345
|
189
|
}
|
|
346
|
190
|
|
|
347
|
|
-/**
|
|
348
|
|
- * Calculate used length of a field containing DHCP options
|
|
349
|
|
- *
|
|
350
|
|
- * @v data Field containing DHCP options
|
|
351
|
|
- * @v max_len Field length
|
|
352
|
|
- * @ret len Used length (excluding the @c DHCP_END tag)
|
|
353
|
|
- */
|
|
354
|
|
-static size_t dhcp_field_len ( const void *data, size_t max_len ) {
|
|
355
|
|
- struct dhcp_option_block options;
|
|
356
|
|
- struct dhcp_option *end;
|
|
357
|
|
-
|
|
358
|
|
- options.data = ( ( void * ) data );
|
|
359
|
|
- options.len = max_len;
|
|
360
|
|
- end = find_dhcp_option ( &options, DHCP_END );
|
|
361
|
|
- return ( end ? ( ( ( void * ) end ) - data ) : 0 );
|
|
362
|
|
-}
|
|
363
|
|
-
|
|
364
|
|
-/**
|
|
365
|
|
- * Merge field containing DHCP options or string into DHCP options block
|
|
366
|
|
- *
|
|
367
|
|
- * @v options DHCP option block
|
|
368
|
|
- * @v data Field containing DHCP options
|
|
369
|
|
- * @v max_len Field length
|
|
370
|
|
- * @v tag DHCP option tag, or 0
|
|
371
|
|
- *
|
|
372
|
|
- * If @c tag is non-zero (and the field is not empty), the field will
|
|
373
|
|
- * be treated as a NUL-terminated string representing the value of the
|
|
374
|
|
- * specified DHCP option. If @c tag is zero, the field will be
|
|
375
|
|
- * treated as a block of DHCP options, and simply appended to the
|
|
376
|
|
- * existing options in the option block.
|
|
377
|
|
- *
|
|
378
|
|
- * The caller must ensure that there is enough space in the options
|
|
379
|
|
- * block to perform the merge.
|
|
380
|
|
- */
|
|
381
|
|
-static void merge_dhcp_field ( struct dhcp_option_block *options,
|
|
382
|
|
- const void *data, size_t max_len,
|
|
383
|
|
- unsigned int tag ) {
|
|
384
|
|
- size_t len;
|
|
385
|
|
- void *dest;
|
|
386
|
|
- struct dhcp_option *end;
|
|
387
|
|
-
|
|
388
|
|
- if ( tag ) {
|
|
389
|
|
- len = strlen ( data );
|
|
390
|
|
- if ( len )
|
|
391
|
|
- set_dhcp_option ( options, tag, data, len );
|
|
392
|
|
- } else {
|
|
393
|
|
- len = dhcp_field_len ( data, max_len );
|
|
394
|
|
- dest = ( options->data + options->len - 1 );
|
|
395
|
|
- memcpy ( dest, data, len );
|
|
396
|
|
- options->len += len;
|
|
397
|
|
- end = ( dest + len );
|
|
398
|
|
- end->tag = DHCP_END;
|
|
399
|
|
- }
|
|
400
|
|
-}
|
|
401
|
|
-
|
|
402
|
|
-/**
|
|
403
|
|
- * Parse DHCP packet and construct DHCP options block
|
|
404
|
|
- *
|
|
405
|
|
- * @v dhcphdr DHCP packet
|
|
406
|
|
- * @v len Length of DHCP packet
|
|
407
|
|
- * @ret options DHCP options block, or NULL
|
|
408
|
|
- *
|
|
409
|
|
- * Parses a received DHCP packet and canonicalises its contents into a
|
|
410
|
|
- * single DHCP options block. The "file" and "sname" fields are
|
|
411
|
|
- * converted into the corresponding DHCP options (@c
|
|
412
|
|
- * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
|
|
413
|
|
- * these fields are used for option overloading, their options are
|
|
414
|
|
- * merged in to the options block.
|
|
415
|
|
- *
|
|
416
|
|
- * The values of the "yiaddr" and "siaddr" fields will be stored
|
|
417
|
|
- * within the options block as the magic options @c DHCP_EB_YIADDR and
|
|
418
|
|
- * @c DHCP_EB_SIADDR.
|
|
419
|
|
- *
|
|
420
|
|
- * Note that this call allocates new memory for the constructed DHCP
|
|
421
|
|
- * options block; it is the responsibility of the caller to eventually
|
|
422
|
|
- * free this memory.
|
|
423
|
|
- */
|
|
424
|
|
-static struct dhcp_option_block * dhcp_parse ( const struct dhcphdr *dhcphdr,
|
|
425
|
|
- size_t len ) {
|
|
426
|
|
- struct dhcp_option_block *options;
|
|
427
|
|
- size_t options_len;
|
|
428
|
|
- unsigned int overloading;
|
|
429
|
|
-
|
|
430
|
|
- /* Sanity check */
|
|
431
|
|
- if ( len < sizeof ( *dhcphdr ) )
|
|
432
|
|
- return NULL;
|
|
433
|
|
-
|
|
434
|
|
- /* Calculate size of resulting concatenated option block:
|
|
435
|
|
- *
|
|
436
|
|
- * The "options" field : length of the field minus the DHCP_END tag.
|
|
437
|
|
- *
|
|
438
|
|
- * The "file" field : maximum length of the field minus the
|
|
439
|
|
- * NUL terminator, plus a 2-byte DHCP header or, if used for
|
|
440
|
|
- * option overloading, the length of the field minus the
|
|
441
|
|
- * DHCP_END tag.
|
|
442
|
|
- *
|
|
443
|
|
- * The "sname" field : as for the "file" field.
|
|
444
|
|
- *
|
|
445
|
|
- * 15 bytes for an encapsulated options field to contain the
|
|
446
|
|
- * value of the "yiaddr" and "siaddr" fields
|
|
447
|
|
- *
|
|
448
|
|
- * 1 byte for a final terminating DHCP_END tag.
|
|
449
|
|
- */
|
|
450
|
|
- options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
|
|
451
|
|
- + ( sizeof ( dhcphdr->file ) + 1 )
|
|
452
|
|
- + ( sizeof ( dhcphdr->sname ) + 1 )
|
|
453
|
|
- + 15 /* yiaddr and siaddr */
|
|
454
|
|
- + 1 /* DHCP_END tag */ );
|
|
455
|
|
-
|
|
456
|
|
- /* Allocate empty options block of required size */
|
|
457
|
|
- options = alloc_dhcp_options ( options_len );
|
|
458
|
|
- if ( ! options ) {
|
|
459
|
|
- DBG ( "DHCP could not allocate %zd-byte option block\n",
|
|
460
|
|
- options_len );
|
|
461
|
|
- return NULL;
|
|
462
|
|
- }
|
|
463
|
|
-
|
|
464
|
|
- /* Merge in "options" field, if this is a DHCP packet */
|
|
465
|
|
- if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
|
|
466
|
|
- merge_dhcp_field ( options, dhcphdr->options,
|
|
467
|
|
- ( len -
|
|
468
|
|
- offsetof ( typeof (*dhcphdr), options ) ),
|
|
469
|
|
- 0 /* Always contains options */ );
|
|
470
|
|
- }
|
|
471
|
|
-
|
|
472
|
|
- /* Identify overloaded fields */
|
|
473
|
|
- overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
|
|
474
|
|
-
|
|
475
|
|
- /* Merge in "file" and "sname" fields */
|
|
476
|
|
- merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
|
|
477
|
|
- ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
|
|
478
|
|
- 0 : DHCP_BOOTFILE_NAME ) );
|
|
479
|
|
- merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
|
|
480
|
|
- ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
|
|
481
|
|
- 0 : DHCP_TFTP_SERVER_NAME ) );
|
|
482
|
|
-
|
|
483
|
|
- /* Set magic options for "yiaddr" and "siaddr", if present */
|
|
484
|
|
- if ( dhcphdr->yiaddr.s_addr ) {
|
|
485
|
|
- set_dhcp_option ( options, DHCP_EB_YIADDR,
|
|
486
|
|
- &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
|
|
487
|
|
- }
|
|
488
|
|
- if ( dhcphdr->siaddr.s_addr ) {
|
|
489
|
|
- set_dhcp_option ( options, DHCP_EB_SIADDR,
|
|
490
|
|
- &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
|
|
491
|
|
- }
|
|
492
|
|
-
|
|
493
|
|
- assert ( options->len <= options->max_len );
|
|
494
|
|
-
|
|
495
|
|
- return options;
|
|
496
|
|
-}
|
|
497
|
|
-
|
|
498
|
|
-/****************************************************************************
|
|
499
|
|
- *
|
|
500
|
|
- * Whole-packet construction
|
|
501
|
|
- *
|
|
502
|
|
- */
|
|
503
|
|
-
|
|
504
|
191
|
/** DHCP network device descriptor */
|
|
505
|
192
|
struct dhcp_netdev_desc {
|
|
506
|
193
|
/** Bus type ID */
|
|
|
@@ -532,18 +219,18 @@ struct dhcp_client_uuid {
|
|
532
|
219
|
/**
|
|
533
|
220
|
* Create DHCP request
|
|
534
|
221
|
*
|
|
|
222
|
+ * @v dhcppkt DHCP packet structure to fill in
|
|
535
|
223
|
* @v netdev Network device
|
|
536
|
224
|
* @v msgtype DHCP message type
|
|
537
|
|
- * @v options DHCP server response options, or NULL
|
|
|
225
|
+ * @v offer_settings Settings received in DHCPOFFER, or NULL
|
|
538
|
226
|
* @v data Buffer for DHCP packet
|
|
539
|
227
|
* @v max_len Size of DHCP packet buffer
|
|
540
|
|
- * @v dhcppkt DHCP packet structure to fill in
|
|
541
|
228
|
* @ret rc Return status code
|
|
542
|
229
|
*/
|
|
543
|
|
-int create_dhcp_request ( struct net_device *netdev, int msgtype,
|
|
544
|
|
- struct dhcp_option_block *options,
|
|
545
|
|
- void *data, size_t max_len,
|
|
546
|
|
- struct dhcp_packet *dhcppkt ) {
|
|
|
230
|
+int create_dhcp_request ( struct dhcp_packet *dhcppkt,
|
|
|
231
|
+ struct net_device *netdev, int msgtype,
|
|
|
232
|
+ struct settings *offer_settings,
|
|
|
233
|
+ void *data, size_t max_len ) {
|
|
547
|
234
|
struct device_description *desc = &netdev->dev->desc;
|
|
548
|
235
|
struct dhcp_netdev_desc dhcp_desc;
|
|
549
|
236
|
struct dhcp_client_id client_id;
|
|
|
@@ -553,33 +240,27 @@ int create_dhcp_request ( struct net_device *netdev, int msgtype,
|
|
553
|
240
|
int rc;
|
|
554
|
241
|
|
|
555
|
242
|
/* Create DHCP packet */
|
|
556
|
|
- if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
|
|
557
|
|
- dhcppkt ) ) != 0 ) {
|
|
|
243
|
+ if ( ( rc = create_dhcp_packet ( dhcppkt, netdev, msgtype,
|
|
|
244
|
+ &dhcp_request_options, data,
|
|
|
245
|
+ max_len ) ) != 0 ) {
|
|
558
|
246
|
DBG ( "DHCP could not create DHCP packet: %s\n",
|
|
559
|
247
|
strerror ( rc ) );
|
|
560
|
248
|
return rc;
|
|
561
|
249
|
}
|
|
562
|
250
|
|
|
563
|
|
- /* Copy in options common to all requests */
|
|
564
|
|
- if ( ( rc = copy_dhcp_packet_options ( dhcppkt,
|
|
565
|
|
- &dhcp_request_options )) !=0 ){
|
|
566
|
|
- DBG ( "DHCP could not set common DHCP options: %s\n",
|
|
567
|
|
- strerror ( rc ) );
|
|
568
|
|
- return rc;
|
|
569
|
|
- }
|
|
570
|
|
-
|
|
571
|
251
|
/* Copy any required options from previous server repsonse */
|
|
572
|
|
- if ( options ) {
|
|
573
|
|
- if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
|
|
574
|
|
- DHCP_SERVER_IDENTIFIER,
|
|
575
|
|
- DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
|
|
|
252
|
+ if ( offer_settings ) {
|
|
|
253
|
+ if ( ( rc = copy_setting ( &dhcppkt->settings,
|
|
|
254
|
+ DHCP_SERVER_IDENTIFIER,
|
|
|
255
|
+ offer_settings,
|
|
|
256
|
+ DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
|
|
576
|
257
|
DBG ( "DHCP could not set server identifier "
|
|
577
|
258
|
"option: %s\n", strerror ( rc ) );
|
|
578
|
259
|
return rc;
|
|
579
|
260
|
}
|
|
580
|
|
- if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
|
|
581
|
|
- DHCP_EB_YIADDR,
|
|
582
|
|
- DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
|
|
|
261
|
+ if ( ( rc = copy_setting ( &dhcppkt->settings, DHCP_EB_YIADDR,
|
|
|
262
|
+ offer_settings,
|
|
|
263
|
+ DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
|
|
583
|
264
|
DBG ( "DHCP could not set requested address "
|
|
584
|
265
|
"option: %s\n", strerror ( rc ) );
|
|
585
|
266
|
return rc;
|
|
|
@@ -588,9 +269,8 @@ int create_dhcp_request ( struct net_device *netdev, int msgtype,
|
|
588
|
269
|
|
|
589
|
270
|
/* Add options to identify the feature list */
|
|
590
|
271
|
dhcp_features_len = ( dhcp_features_end - dhcp_features );
|
|
591
|
|
- if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_ENCAP,
|
|
592
|
|
- dhcp_features,
|
|
593
|
|
- dhcp_features_len ) ) != 0 ) {
|
|
|
272
|
+ if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_EB_ENCAP,
|
|
|
273
|
+ dhcp_features, dhcp_features_len ) ) !=0 ){
|
|
594
|
274
|
DBG ( "DHCP could not set features list option: %s\n",
|
|
595
|
275
|
strerror ( rc ) );
|
|
596
|
276
|
return rc;
|
|
|
@@ -600,9 +280,8 @@ int create_dhcp_request ( struct net_device *netdev, int msgtype,
|
|
600
|
280
|
dhcp_desc.type = desc->bus_type;
|
|
601
|
281
|
dhcp_desc.vendor = htons ( desc->vendor );
|
|
602
|
282
|
dhcp_desc.device = htons ( desc->device );
|
|
603
|
|
- if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_BUS_ID,
|
|
604
|
|
- &dhcp_desc,
|
|
605
|
|
- sizeof ( dhcp_desc ) ) ) != 0 ) {
|
|
|
283
|
+ if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_EB_BUS_ID,
|
|
|
284
|
+ &dhcp_desc, sizeof ( dhcp_desc ) ) ) !=0 ){
|
|
606
|
285
|
DBG ( "DHCP could not set bus ID option: %s\n",
|
|
607
|
286
|
strerror ( rc ) );
|
|
608
|
287
|
return rc;
|
|
|
@@ -615,9 +294,8 @@ int create_dhcp_request ( struct net_device *netdev, int msgtype,
|
|
615
|
294
|
ll_addr_len = netdev->ll_protocol->ll_addr_len;
|
|
616
|
295
|
assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
|
|
617
|
296
|
memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
|
|
618
|
|
- if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_CLIENT_ID,
|
|
619
|
|
- &client_id,
|
|
620
|
|
- ( ll_addr_len + 1 ) ) ) != 0 ) {
|
|
|
297
|
+ if ( ( rc = store_setting ( &dhcppkt->settings, DHCP_CLIENT_ID,
|
|
|
298
|
+ &client_id, ( ll_addr_len + 1 ) ) ) != 0 ){
|
|
621
|
299
|
DBG ( "DHCP could not set client ID: %s\n",
|
|
622
|
300
|
strerror ( rc ) );
|
|
623
|
301
|
return rc;
|
|
|
@@ -626,9 +304,9 @@ int create_dhcp_request ( struct net_device *netdev, int msgtype,
|
|
626
|
304
|
/* Add client UUID, if we have one. Required for PXE. */
|
|
627
|
305
|
client_uuid.type = DHCP_CLIENT_UUID_TYPE;
|
|
628
|
306
|
if ( ( rc = get_uuid ( &client_uuid.uuid ) ) == 0 ) {
|
|
629
|
|
- if ( ( rc = set_dhcp_packet_option ( dhcppkt,
|
|
630
|
|
- DHCP_CLIENT_UUID, &client_uuid,
|
|
631
|
|
- sizeof ( client_uuid ) ) ) != 0 ) {
|
|
|
307
|
+ if ( ( rc = store_setting ( &dhcppkt->settings,
|
|
|
308
|
+ DHCP_CLIENT_UUID, &client_uuid,
|
|
|
309
|
+ sizeof ( client_uuid ) ) ) != 0 ) {
|
|
632
|
310
|
DBG ( "DHCP could not set client UUID: %s\n",
|
|
633
|
311
|
strerror ( rc ) );
|
|
634
|
312
|
return rc;
|
|
|
@@ -641,34 +319,86 @@ int create_dhcp_request ( struct net_device *netdev, int msgtype,
|
|
641
|
319
|
/**
|
|
642
|
320
|
* Create DHCP response
|
|
643
|
321
|
*
|
|
|
322
|
+ * @v dhcppkt DHCP packet structure to fill in
|
|
644
|
323
|
* @v netdev Network device
|
|
645
|
324
|
* @v msgtype DHCP message type
|
|
646
|
|
- * @v options DHCP options, or NULL
|
|
|
325
|
+ * @v settings Settings to include, or NULL
|
|
647
|
326
|
* @v data Buffer for DHCP packet
|
|
648
|
327
|
* @v max_len Size of DHCP packet buffer
|
|
649
|
|
- * @v dhcppkt DHCP packet structure to fill in
|
|
650
|
328
|
* @ret rc Return status code
|
|
651
|
329
|
*/
|
|
652
|
|
-int create_dhcp_response ( struct net_device *netdev, int msgtype,
|
|
653
|
|
- struct dhcp_option_block *options,
|
|
654
|
|
- void *data, size_t max_len,
|
|
655
|
|
- struct dhcp_packet *dhcppkt ) {
|
|
|
330
|
+int create_dhcp_response ( struct dhcp_packet *dhcppkt,
|
|
|
331
|
+ struct net_device *netdev, int msgtype,
|
|
|
332
|
+ struct settings *settings,
|
|
|
333
|
+ void *data, size_t max_len ) {
|
|
656
|
334
|
int rc;
|
|
657
|
335
|
|
|
658
|
336
|
/* Create packet and copy in options */
|
|
659
|
|
- if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
|
|
660
|
|
- dhcppkt ) ) != 0 ) {
|
|
661
|
|
- DBG ( " failed to build packet" );
|
|
|
337
|
+ if ( ( rc = create_dhcp_packet ( dhcppkt, netdev, msgtype, NULL,
|
|
|
338
|
+ data, max_len ) ) != 0 )
|
|
662
|
339
|
return rc;
|
|
663
|
|
- }
|
|
664
|
|
- if ( ( rc = copy_dhcp_packet_options ( dhcppkt, options ) ) != 0 ) {
|
|
665
|
|
- DBG ( " failed to copy options" );
|
|
|
340
|
+ if ( ( rc = copy_settings ( &dhcppkt->settings, settings ) ) != 0 )
|
|
666
|
341
|
return rc;
|
|
667
|
|
- }
|
|
668
|
342
|
|
|
669
|
343
|
return 0;
|
|
670
|
344
|
}
|
|
671
|
345
|
|
|
|
346
|
+/****************************************************************************
|
|
|
347
|
+ *
|
|
|
348
|
+ * DHCP packets contained in I/O buffers
|
|
|
349
|
+ *
|
|
|
350
|
+ */
|
|
|
351
|
+
|
|
|
352
|
+/** A DHCP packet contained in an I/O buffer */
|
|
|
353
|
+struct dhcp_iobuf_packet {
|
|
|
354
|
+ /** Reference counter */
|
|
|
355
|
+ struct refcnt refcnt;
|
|
|
356
|
+ /** DHCP packet */
|
|
|
357
|
+ struct dhcp_packet dhcppkt;
|
|
|
358
|
+ /** Containing I/O buffer */
|
|
|
359
|
+ struct io_buffer *iobuf;
|
|
|
360
|
+};
|
|
|
361
|
+
|
|
|
362
|
+/**
|
|
|
363
|
+ * Free DHCP packet contained in an I/O buffer
|
|
|
364
|
+ *
|
|
|
365
|
+ * @v refcnt Reference counter
|
|
|
366
|
+ */
|
|
|
367
|
+static void dhcpiob_free ( struct refcnt *refcnt ) {
|
|
|
368
|
+ struct dhcp_iobuf_packet *dhcpiob =
|
|
|
369
|
+ container_of ( refcnt, struct dhcp_iobuf_packet, refcnt );
|
|
|
370
|
+
|
|
|
371
|
+ free_iob ( dhcpiob->iobuf );
|
|
|
372
|
+ free ( dhcpiob );
|
|
|
373
|
+}
|
|
|
374
|
+
|
|
|
375
|
+/**
|
|
|
376
|
+ * Create DHCP packet from I/O buffer
|
|
|
377
|
+ *
|
|
|
378
|
+ * @v iobuf I/O buffer
|
|
|
379
|
+ * @ret dhcpiob DHCP packet contained in I/O buffer
|
|
|
380
|
+ *
|
|
|
381
|
+ * This function takes ownership of the I/O buffer. Future accesses
|
|
|
382
|
+ * must be via the @c dhcpiob data structure.
|
|
|
383
|
+ */
|
|
|
384
|
+static struct dhcp_iobuf_packet * dhcpiob_create ( struct io_buffer *iobuf ) {
|
|
|
385
|
+ struct dhcp_iobuf_packet *dhcpiob;
|
|
|
386
|
+
|
|
|
387
|
+ dhcpiob = zalloc ( sizeof ( *dhcpiob ) );
|
|
|
388
|
+ if ( dhcpiob ) {
|
|
|
389
|
+ dhcpiob->refcnt.free = dhcpiob_free;
|
|
|
390
|
+ dhcpiob->iobuf = iobuf;
|
|
|
391
|
+ dhcppkt_init ( &dhcpiob->dhcppkt, &dhcpiob->refcnt,
|
|
|
392
|
+ iobuf->data, iob_len ( iobuf ) );
|
|
|
393
|
+ }
|
|
|
394
|
+ return dhcpiob;
|
|
|
395
|
+}
|
|
|
396
|
+
|
|
|
397
|
+static void dhcpiob_put ( struct dhcp_iobuf_packet *dhcpiob ) {
|
|
|
398
|
+ if ( dhcpiob )
|
|
|
399
|
+ ref_put ( &dhcpiob->refcnt );
|
|
|
400
|
+}
|
|
|
401
|
+
|
|
672
|
402
|
/****************************************************************************
|
|
673
|
403
|
*
|
|
674
|
404
|
* DHCP to UDP interface
|
|
|
@@ -686,9 +416,6 @@ struct dhcp_session {
|
|
686
|
416
|
|
|
687
|
417
|
/** Network device being configured */
|
|
688
|
418
|
struct net_device *netdev;
|
|
689
|
|
- /** Option block registration routine */
|
|
690
|
|
- int ( * register_options ) ( struct net_device *netdev,
|
|
691
|
|
- struct dhcp_option_block *options );
|
|
692
|
419
|
|
|
693
|
420
|
/** State of the session
|
|
694
|
421
|
*
|
|
|
@@ -696,10 +423,10 @@ struct dhcp_session {
|
|
696
|
423
|
* (e.g. @c DHCPDISCOVER).
|
|
697
|
424
|
*/
|
|
698
|
425
|
int state;
|
|
699
|
|
- /** Options obtained from DHCP server */
|
|
700
|
|
- struct dhcp_option_block *options;
|
|
701
|
|
- /** Options obtained from ProxyDHCP server */
|
|
702
|
|
- struct dhcp_option_block *proxy_options;
|
|
|
426
|
+ /** Response obtained from DHCP server */
|
|
|
427
|
+ struct dhcp_iobuf_packet *response;
|
|
|
428
|
+ /** Response obtained from ProxyDHCP server */
|
|
|
429
|
+ struct dhcp_iobuf_packet *proxy_response;
|
|
703
|
430
|
/** Retransmission timer */
|
|
704
|
431
|
struct retry_timer timer;
|
|
705
|
432
|
/** Session start time (in ticks) */
|
|
|
@@ -716,8 +443,8 @@ static void dhcp_free ( struct refcnt *refcnt ) {
|
|
716
|
443
|
container_of ( refcnt, struct dhcp_session, refcnt );
|
|
717
|
444
|
|
|
718
|
445
|
netdev_put ( dhcp->netdev );
|
|
719
|
|
- dhcpopt_put ( dhcp->options );
|
|
720
|
|
- dhcpopt_put ( dhcp->proxy_options );
|
|
|
446
|
+ dhcpiob_put ( dhcp->response );
|
|
|
447
|
+ dhcpiob_put ( dhcp->proxy_response );
|
|
721
|
448
|
free ( dhcp );
|
|
722
|
449
|
}
|
|
723
|
450
|
|
|
|
@@ -741,6 +468,31 @@ static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
|
|
741
|
468
|
job_done ( &dhcp->job, rc );
|
|
742
|
469
|
}
|
|
743
|
470
|
|
|
|
471
|
+/**
|
|
|
472
|
+ * Register options received via DHCP
|
|
|
473
|
+ *
|
|
|
474
|
+ * @v dhcp DHCP session
|
|
|
475
|
+ * @ret rc Return status code
|
|
|
476
|
+ */
|
|
|
477
|
+static int dhcp_register_settings ( struct dhcp_session *dhcp ) {
|
|
|
478
|
+ struct settings *settings;
|
|
|
479
|
+ struct settings *parent;
|
|
|
480
|
+ int rc;
|
|
|
481
|
+
|
|
|
482
|
+ if ( dhcp->proxy_response ) {
|
|
|
483
|
+ settings = &dhcp->proxy_response->dhcppkt.settings;
|
|
|
484
|
+ if ( ( rc = register_settings ( settings, NULL ) ) != 0 )
|
|
|
485
|
+ return rc;
|
|
|
486
|
+ }
|
|
|
487
|
+
|
|
|
488
|
+ settings = &dhcp->response->dhcppkt.settings;
|
|
|
489
|
+ parent = netdev_settings ( dhcp->netdev );
|
|
|
490
|
+ if ( ( rc = register_settings ( settings, parent ) ) != 0 )
|
|
|
491
|
+ return rc;
|
|
|
492
|
+
|
|
|
493
|
+ return 0;
|
|
|
494
|
+}
|
|
|
495
|
+
|
|
744
|
496
|
/****************************************************************************
|
|
745
|
497
|
*
|
|
746
|
498
|
* Data transfer interface
|
|
|
@@ -757,6 +509,7 @@ static int dhcp_send_request ( struct dhcp_session *dhcp ) {
|
|
757
|
509
|
struct xfer_metadata meta = {
|
|
758
|
510
|
.netdev = dhcp->netdev,
|
|
759
|
511
|
};
|
|
|
512
|
+ struct settings *offer_settings = NULL;
|
|
760
|
513
|
struct io_buffer *iobuf;
|
|
761
|
514
|
struct dhcp_packet dhcppkt;
|
|
762
|
515
|
int rc;
|
|
|
@@ -778,10 +531,11 @@ static int dhcp_send_request ( struct dhcp_session *dhcp ) {
|
|
778
|
531
|
return -ENOMEM;
|
|
779
|
532
|
|
|
780
|
533
|
/* Create DHCP packet in temporary buffer */
|
|
781
|
|
- if ( ( rc = create_dhcp_request ( dhcp->netdev, dhcp->state,
|
|
782
|
|
- dhcp->options, iobuf->data,
|
|
783
|
|
- iob_tailroom ( iobuf ),
|
|
784
|
|
- &dhcppkt ) ) != 0 ) {
|
|
|
534
|
+ if ( dhcp->response )
|
|
|
535
|
+ offer_settings = &dhcp->response->dhcppkt.settings;
|
|
|
536
|
+ if ( ( rc = create_dhcp_request ( &dhcppkt, dhcp->netdev, dhcp->state,
|
|
|
537
|
+ offer_settings, iobuf->data,
|
|
|
538
|
+ iob_tailroom ( iobuf ) ) ) != 0 ) {
|
|
785
|
539
|
DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
|
|
786
|
540
|
dhcp, strerror ( rc ) );
|
|
787
|
541
|
goto done;
|
|
|
@@ -828,36 +582,41 @@ static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
|
|
828
|
582
|
* @v len Length of received data
|
|
829
|
583
|
* @ret rc Return status code
|
|
830
|
584
|
*/
|
|
831
|
|
-static int dhcp_deliver_raw ( struct xfer_interface *xfer,
|
|
832
|
|
- const void *data, size_t len ) {
|
|
|
585
|
+static int dhcp_deliver_iob ( struct xfer_interface *xfer,
|
|
|
586
|
+ struct io_buffer *iobuf,
|
|
|
587
|
+ struct xfer_metadata *meta __unused ) {
|
|
833
|
588
|
struct dhcp_session *dhcp =
|
|
834
|
589
|
container_of ( xfer, struct dhcp_session, xfer );
|
|
835
|
|
- const struct dhcphdr *dhcphdr = data;
|
|
836
|
|
- struct dhcp_option_block *options;
|
|
837
|
|
- struct dhcp_option_block **store_options;
|
|
|
590
|
+ struct dhcp_iobuf_packet *response;
|
|
|
591
|
+ struct dhcp_iobuf_packet **store_response;
|
|
|
592
|
+ struct dhcphdr *dhcphdr;
|
|
|
593
|
+ struct settings *settings;
|
|
838
|
594
|
unsigned int msgtype;
|
|
839
|
595
|
unsigned long elapsed;
|
|
840
|
596
|
int is_proxy;
|
|
841
|
597
|
int ignore_proxy;
|
|
|
598
|
+ int rc;
|
|
|
599
|
+
|
|
|
600
|
+ /* Convert packet into a DHCP-packet-in-iobuf */
|
|
|
601
|
+ response = dhcpiob_create ( iobuf );
|
|
|
602
|
+ if ( ! response ) {
|
|
|
603
|
+ DBGC ( dhcp, "DHCP %p could not store DHCP packet\n", dhcp );
|
|
|
604
|
+ return -ENOMEM;
|
|
|
605
|
+ }
|
|
|
606
|
+ dhcphdr = response->dhcppkt.dhcphdr;
|
|
|
607
|
+ settings = &response->dhcppkt.settings;
|
|
842
|
608
|
|
|
843
|
609
|
/* Check for matching transaction ID */
|
|
844
|
610
|
if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
|
|
845
|
611
|
DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
|
|
846
|
612
|
"got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
|
|
847
|
613
|
ntohl ( dhcp_xid ( dhcp->netdev ) ) );
|
|
848
|
|
- return 0;
|
|
849
|
|
- };
|
|
850
|
|
-
|
|
851
|
|
- /* Parse packet and create options structure */
|
|
852
|
|
- options = dhcp_parse ( dhcphdr, len );
|
|
853
|
|
- if ( ! options ) {
|
|
854
|
|
- DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
|
|
855
|
|
- return -EINVAL;
|
|
856
|
|
- }
|
|
|
614
|
+ goto out_discard;
|
|
|
615
|
+ };
|
|
857
|
616
|
|
|
858
|
617
|
/* Determine and verify message type */
|
|
859
|
618
|
is_proxy = ( dhcphdr->yiaddr.s_addr == 0 );
|
|
860
|
|
- msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
|
|
|
619
|
+ msgtype = fetch_uintz_setting ( settings, DHCP_MESSAGE_TYPE );
|
|
861
|
620
|
DBGC ( dhcp, "DHCP %p received %s%s\n", dhcp,
|
|
862
|
621
|
( is_proxy ? "Proxy" : "" ), dhcp_msgtype_name ( msgtype ) );
|
|
863
|
622
|
if ( ( ( dhcp->state != DHCPDISCOVER ) || ( msgtype != DHCPOFFER ) ) &&
|
|
|
@@ -872,25 +631,26 @@ static int dhcp_deliver_raw ( struct xfer_interface *xfer,
|
|
872
|
631
|
* options have equal or higher priority than the
|
|
873
|
632
|
* currently-stored options.
|
|
874
|
633
|
*/
|
|
875
|
|
- store_options = ( is_proxy ? &dhcp->proxy_options : &dhcp->options );
|
|
876
|
|
- if ( ( ! *store_options ) ||
|
|
877
|
|
- ( find_dhcp_num_option ( options, DHCP_EB_PRIORITY ) >=
|
|
878
|
|
- find_dhcp_num_option ( *store_options, DHCP_EB_PRIORITY ) ) ) {
|
|
879
|
|
- dhcpopt_put ( *store_options );
|
|
880
|
|
- *store_options = options;
|
|
|
634
|
+ store_response = ( is_proxy ? &dhcp->proxy_response : &dhcp->response);
|
|
|
635
|
+ if ( ( ! *store_response ) ||
|
|
|
636
|
+ ( fetch_uintz_setting ( settings, DHCP_EB_PRIORITY ) >=
|
|
|
637
|
+ fetch_uintz_setting ( &(*store_response)->dhcppkt.settings,
|
|
|
638
|
+ DHCP_EB_PRIORITY ) ) ) {
|
|
|
639
|
+ dhcpiob_put ( *store_response );
|
|
|
640
|
+ *store_response = response;
|
|
881
|
641
|
} else {
|
|
882
|
|
- dhcpopt_put ( options );
|
|
|
642
|
+ dhcpiob_put ( response );
|
|
883
|
643
|
}
|
|
884
|
644
|
|
|
885
|
645
|
/* If we don't yet have a standard DHCP response (i.e. one
|
|
886
|
646
|
* with an IP address), then just leave the timer running.
|
|
887
|
647
|
*/
|
|
888
|
|
- if ( ! dhcp->options )
|
|
|
648
|
+ if ( ! dhcp->response )
|
|
889
|
649
|
goto out;
|
|
890
|
650
|
|
|
891
|
651
|
/* Handle DHCP response */
|
|
892
|
|
- ignore_proxy = find_dhcp_num_option ( dhcp->options,
|
|
893
|
|
- DHCP_EB_NO_PROXYDHCP );
|
|
|
652
|
+ ignore_proxy = fetch_uintz_setting ( &dhcp->response->dhcppkt.settings,
|
|
|
653
|
+ DHCP_EB_NO_PROXYDHCP );
|
|
894
|
654
|
switch ( dhcp->state ) {
|
|
895
|
655
|
case DHCPDISCOVER:
|
|
896
|
656
|
/* If we have allowed sufficient time for ProxyDHCP
|
|
|
@@ -905,11 +665,14 @@ static int dhcp_deliver_raw ( struct xfer_interface *xfer,
|
|
905
|
665
|
break;
|
|
906
|
666
|
case DHCPREQUEST:
|
|
907
|
667
|
/* DHCP finished; register options and exit */
|
|
908
|
|
- if ( dhcp->proxy_options && ( ! ignore_proxy ) ) {
|
|
909
|
|
- dhcp->register_options ( dhcp->netdev,
|
|
910
|
|
- dhcp->proxy_options );
|
|
|
668
|
+ if ( ignore_proxy && dhcp->proxy_response ) {
|
|
|
669
|
+ dhcpiob_put ( dhcp->proxy_response );
|
|
|
670
|
+ dhcp->proxy_response = NULL;
|
|
|
671
|
+ }
|
|
|
672
|
+ if ( ( rc = dhcp_register_settings ( dhcp ) ) != 0 ) {
|
|
|
673
|
+ dhcp_finished ( dhcp, rc );
|
|
|
674
|
+ break;
|
|
911
|
675
|
}
|
|
912
|
|
- dhcp->register_options ( dhcp->netdev, dhcp->options );
|
|
913
|
676
|
dhcp_finished ( dhcp, 0 );
|
|
914
|
677
|
break;
|
|
915
|
678
|
default:
|
|
|
@@ -920,7 +683,7 @@ static int dhcp_deliver_raw ( struct xfer_interface *xfer,
|
|
920
|
683
|
return 0;
|
|
921
|
684
|
|
|
922
|
685
|
out_discard:
|
|
923
|
|
- dhcpopt_put ( options );
|
|
|
686
|
+ dhcpiob_put ( response );
|
|
924
|
687
|
return 0;
|
|
925
|
688
|
}
|
|
926
|
689
|
|
|
|
@@ -930,8 +693,8 @@ static struct xfer_interface_operations dhcp_xfer_operations = {
|
|
930
|
693
|
.vredirect = xfer_vopen,
|
|
931
|
694
|
.window = unlimited_xfer_window,
|
|
932
|
695
|
.alloc_iob = default_xfer_alloc_iob,
|
|
933
|
|
- .deliver_iob = xfer_deliver_as_raw,
|
|
934
|
|
- .deliver_raw = dhcp_deliver_raw,
|
|
|
696
|
+ .deliver_iob = dhcp_deliver_iob,
|
|
|
697
|
+ .deliver_raw = xfer_deliver_as_iob,
|
|
935
|
698
|
};
|
|
936
|
699
|
|
|
937
|
700
|
/****************************************************************************
|
|
|
@@ -978,9 +741,7 @@ static struct job_interface_operations dhcp_job_operations = {
|
|
978
|
741
|
* register_options() routine will be called with the acquired
|
|
979
|
742
|
* options.
|
|
980
|
743
|
*/
|
|
981
|
|
-int start_dhcp ( struct job_interface *job, struct net_device *netdev,
|
|
982
|
|
- int ( * register_options ) ( struct net_device *netdev,
|
|
983
|
|
- struct dhcp_option_block * ) ) {
|
|
|
744
|
+int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
|
|
984
|
745
|
static struct sockaddr_in server = {
|
|
985
|
746
|
.sin_family = AF_INET,
|
|
986
|
747
|
.sin_addr.s_addr = INADDR_BROADCAST,
|
|
|
@@ -1001,7 +762,6 @@ int start_dhcp ( struct job_interface *job, struct net_device *netdev,
|
|
1001
|
762
|
job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
|
|
1002
|
763
|
xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
|
|
1003
|
764
|
dhcp->netdev = netdev_get ( netdev );
|
|
1004
|
|
- dhcp->register_options = register_options;
|
|
1005
|
765
|
dhcp->timer.expired = dhcp_timer_expired;
|
|
1006
|
766
|
dhcp->state = DHCPDISCOVER;
|
|
1007
|
767
|
dhcp->start = currticks();
|