2 * Copyright (C) 2006 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.
23 #include <gpxe/if_ether.h>
24 #include <gpxe/netdevice.h>
26 #include <gpxe/dhcp.h>
30 * Dynamic Host Configuration Protocol
34 /** DHCP operation types
36 * This table maps from DHCP message types (i.e. values of the @c
37 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
40 static const uint8_t dhcp_op[] = {
41 [DHCPDISCOVER] = BOOTP_REQUEST,
42 [DHCPOFFER] = BOOTP_REPLY,
43 [DHCPREQUEST] = BOOTP_REQUEST,
44 [DHCPDECLINE] = BOOTP_REQUEST,
45 [DHCPACK] = BOOTP_REPLY,
46 [DHCPNAK] = BOOTP_REPLY,
47 [DHCPRELEASE] = BOOTP_REQUEST,
48 [DHCPINFORM] = BOOTP_REQUEST,
51 /** Raw option data for options common to all DHCP requests */
52 static uint8_t dhcp_request_options_data[] = {
53 DHCP_MAX_MESSAGE_SIZE, DHCP_WORD ( ETH_MAX_MTU ),
55 DHCP_STRING ( 'E', 't', 'h', 'e', 'r', 'b', 'o', 'o', 't' ),
56 DHCP_PARAMETER_REQUEST_LIST,
57 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_HOST_NAME,
63 * Name a DHCP packet type
65 * @v msgtype DHCP message type
66 * @ret string DHCP mesasge type name
68 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
70 case 0: return "BOOTP"; /* Non-DHCP packet */
71 case DHCPDISCOVER: return "DHCPDISCOVER";
72 case DHCPOFFER: return "DHCPOFFER";
73 case DHCPREQUEST: return "DHCPREQUEST";
74 case DHCPDECLINE: return "DHCPDECLINE";
75 case DHCPACK: return "DHCPACK";
76 case DHCPNAK: return "DHCPNAK";
77 case DHCPRELEASE: return "DHCPRELEASE";
78 case DHCPINFORM: return "DHCPINFORM";
79 default: return "DHCP<invalid>";
83 /** Options common to all DHCP requests */
84 static struct dhcp_option_block dhcp_request_options = {
85 .data = dhcp_request_options_data,
86 .max_len = sizeof ( dhcp_request_options_data ),
87 .len = sizeof ( dhcp_request_options_data ),
91 * Set option within DHCP packet
93 * @v dhcppkt DHCP packet
94 * @v tag DHCP option tag
95 * @v data New value for DHCP option
96 * @v len Length of value, in bytes
97 * @ret rc Return status code
99 * Sets the option within the first available options block within the
100 * DHCP packet. Option blocks are tried in the order specified by @c
101 * dhcp_option_block_fill_order.
103 * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
104 * intercepted and inserted into the appropriate fixed fields within
105 * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
106 * ignored, since our DHCP packet assembly method relies on always
107 * having option overloading in use.
109 static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
110 unsigned int tag, const void *data,
112 struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
113 struct dhcp_option_block *options = dhcppkt->options;
114 struct dhcp_option *option = NULL;
116 /* Special-case the magic options */
118 case DHCP_OPTION_OVERLOAD:
119 /* Hard-coded in packets we create; always ignore */
122 memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
125 memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
127 case DHCP_MESSAGE_TYPE:
128 case DHCP_REQUESTED_ADDRESS:
129 case DHCP_PARAMETER_REQUEST_LIST:
130 /* These options have to be within the main options
131 * block. This doesn't seem to be required by the
132 * RFCs, but at least ISC dhcpd refuses to recognise
135 options = &dhcppkt->options[OPTS_MAIN];
138 /* Continue processing as normal */
142 /* Set option in first available options block */
143 for ( ; options < &dhcppkt->options[NUM_OPT_BLOCKS] ; options++ ) {
144 option = set_dhcp_option ( options, tag, data, len );
149 /* Update DHCP packet length */
150 dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
151 + dhcppkt->options[OPTS_MAIN].len );
153 return ( option ? 0 : -ENOSPC );
157 * Copy option into DHCP packet
159 * @v dhcppkt DHCP packet
160 * @v options DHCP option block, or NULL
161 * @v tag DHCP option tag to search for
162 * @v new_tag DHCP option tag to use for copied option
163 * @ret rc Return status code
165 * Copies a single option, if present, from the DHCP options block
166 * into a DHCP packet. The tag for the option may be changed if
167 * desired; this is required by other parts of the DHCP code.
169 * @c options may specify a single options block, or be left as NULL
170 * in order to search for the option within all registered options
173 static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
174 struct dhcp_option_block *options,
175 unsigned int tag, unsigned int new_tag ) {
176 struct dhcp_option *option;
179 option = find_dhcp_option ( options, tag );
181 if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
183 option->len ) ) != 0 )
190 * Copy options into DHCP packet
192 * @v dhcppkt DHCP packet
193 * @v options DHCP option block, or NULL
194 * @v encapsulator Encapsulating option, or zero
195 * @ret rc Return status code
197 * Copies options with the specified encapsulator from DHCP options
198 * blocks into a DHCP packet. Most options are copied verbatim.
199 * Recognised encapsulated options fields are handled as such.
201 * @c options may specify a single options block, or be left as NULL
202 * in order to copy options from all registered options blocks.
204 static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
205 struct dhcp_option_block *options,
206 unsigned int encapsulator ) {
211 for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
212 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
215 case DHCP_VENDOR_ENCAP:
216 /* Process encapsulated options field */
217 if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
223 /* Copy option to reassembled packet */
224 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
235 * Copy options into DHCP packet
237 * @v dhcppkt DHCP packet
238 * @v options DHCP option block, or NULL
239 * @ret rc Return status code
241 * Copies options from DHCP options blocks into a DHCP packet. Most
242 * options are copied verbatim. Recognised encapsulated options
243 * fields are handled as such.
245 * @c options may specify a single options block, or be left as NULL
246 * in order to copy options from all registered options blocks.
248 static int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
249 struct dhcp_option_block *options ) {
250 return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
254 * Create a DHCP packet
256 * @v dhcp DHCP session
257 * @v msgtype DHCP message type
258 * @v data Buffer for DHCP packet
259 * @v max_len Size of DHCP packet buffer
260 * @v dhcppkt DHCP packet structure to fill in
261 * @ret rc Return status code
263 * Creates a DHCP packet in the specified buffer, and fills out a @c
264 * dhcp_packet structure that can be passed to
265 * set_dhcp_packet_option() or copy_dhcp_packet_options().
267 static int create_dhcp_packet ( struct dhcp_session *dhcp, uint8_t msgtype,
268 void *data, size_t max_len,
269 struct dhcp_packet *dhcppkt ) {
270 struct dhcphdr *dhcphdr = data;
271 static const uint8_t overloading = ( DHCP_OPTION_OVERLOAD_FILE |
272 DHCP_OPTION_OVERLOAD_SNAME );
276 if ( max_len < sizeof ( *dhcphdr ) )
279 /* Initialise DHCP packet content */
280 memset ( dhcphdr, 0, max_len );
281 dhcphdr->xid = dhcp->xid;
282 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
283 dhcphdr->htype = ntohs ( dhcp->netdev->ll_protocol->ll_proto );
284 dhcphdr->hlen = dhcp->netdev->ll_protocol->ll_addr_len;
285 memcpy ( dhcphdr->chaddr, dhcp->netdev->ll_addr, dhcphdr->hlen );
286 dhcphdr->op = dhcp_op[msgtype];
288 /* Initialise DHCP packet structure */
289 dhcppkt->dhcphdr = dhcphdr;
290 dhcppkt->max_len = max_len;
291 init_dhcp_options ( &dhcppkt->options[OPTS_MAIN], dhcphdr->options,
293 offsetof ( typeof ( *dhcphdr ), options ) ) );
294 init_dhcp_options ( &dhcppkt->options[OPTS_FILE], dhcphdr->file,
295 sizeof ( dhcphdr->file ) );
296 init_dhcp_options ( &dhcppkt->options[OPTS_SNAME], dhcphdr->sname,
297 sizeof ( dhcphdr->sname ) );
299 /* Set DHCP_OPTION_OVERLOAD option within the main options block */
300 if ( set_dhcp_option ( &dhcppkt->options[OPTS_MAIN],
301 DHCP_OPTION_OVERLOAD, &overloading,
302 sizeof ( overloading ) ) == NULL )
305 /* Set DHCP_MESSAGE_TYPE option */
306 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
308 sizeof ( msgtype ) ) ) != 0 )
315 * Calculate used length of a field containing DHCP options
317 * @v data Field containing DHCP options
318 * @v max_len Field length
319 * @ret len Used length (excluding the @c DHCP_END tag)
321 static size_t dhcp_field_len ( const void *data, size_t max_len ) {
322 struct dhcp_option_block options;
323 struct dhcp_option *end;
325 options.data = ( ( void * ) data );
326 options.len = max_len;
327 end = find_dhcp_option ( &options, DHCP_END );
328 return ( end ? ( ( ( void * ) end ) - data ) : 0 );
332 * Merge field containing DHCP options or string into DHCP options block
334 * @v options DHCP option block
335 * @v data Field containing DHCP options
336 * @v max_len Field length
337 * @v tag DHCP option tag, or 0
339 * If @c tag is non-zero, the field will be treated as a
340 * NUL-terminated string representing the value of the specified DHCP
341 * option. If @c tag is zero, the field will be treated as a block of
342 * DHCP options, and simply appended to the existing options in the
345 * The caller must ensure that there is enough space in the options
346 * block to perform the merge.
348 static void merge_dhcp_field ( struct dhcp_option_block *options,
349 const void *data, size_t max_len,
353 struct dhcp_option *end;
356 set_dhcp_option ( options, tag, data, strlen ( data ) );
358 len = dhcp_field_len ( data, max_len );
359 dest = ( options->data + options->len - 1 );
360 memcpy ( dest, data, len );
362 end = ( dest + len );
368 * Parse DHCP packet and construct DHCP options block
370 * @v dhcphdr DHCP packet
371 * @v len Length of DHCP packet
372 * @ret options DHCP options block, or NULL
374 * Parses a received DHCP packet and canonicalises its contents into a
375 * single DHCP options block. The "file" and "sname" fields are
376 * converted into the corresponding DHCP options (@c
377 * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
378 * these fields are used for option overloading, their options are
379 * merged in to the options block.
381 * The values of the "yiaddr" and "siaddr" fields will be stored
382 * within the options block as the magic options @c DHCP_EB_YIADDR and
385 * Note that this call allocates new memory for the constructed DHCP
386 * options block; it is the responsibility of the caller to eventually
389 static struct dhcp_option_block * dhcp_parse ( struct dhcphdr *dhcphdr,
391 struct dhcp_option_block *options;
393 unsigned int overloading;
396 if ( len < sizeof ( *dhcphdr ) )
399 /* Calculate size of resulting concatenated option block:
401 * The "options" field : length of the field minus the DHCP_END tag.
403 * The "file" field : maximum length of the field minus the
404 * NUL terminator, plus a 2-byte DHCP header or, if used for
405 * option overloading, the length of the field minus the
408 * The "sname" field : as for the "file" field.
410 * 15 bytes for an encapsulated options field to contain the
411 * value of the "yiaddr" and "siaddr" fields
413 * 1 byte for a final terminating DHCP_END tag.
415 options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
416 + ( sizeof ( dhcphdr->file ) + 1 )
417 + ( sizeof ( dhcphdr->sname ) + 1 )
418 + 15 /* yiaddr and siaddr */
419 + 1 /* DHCP_END tag */ );
421 /* Allocate empty options block of required size */
422 options = alloc_dhcp_options ( options_len );
424 DBG ( "DHCP could not allocate %d-byte option block\n",
429 /* Merge in "options" field, if this is a DHCP packet */
430 if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
431 merge_dhcp_field ( options, dhcphdr->options,
433 offsetof ( typeof (*dhcphdr), options ) ),
434 0 /* Always contains options */ );
437 /* Identify overloaded fields */
438 overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
440 /* Merge in "file" and "sname" fields */
441 merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
442 ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
443 0 : DHCP_BOOTFILE_NAME ) );
444 merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
445 ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
446 0 : DHCP_TFTP_SERVER_NAME ) );
448 /* Set magic options for "yiaddr" and "siaddr", if present */
449 if ( dhcphdr->yiaddr.s_addr ) {
450 set_dhcp_option ( options, DHCP_EB_YIADDR,
451 &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
453 if ( dhcphdr->siaddr.s_addr ) {
454 set_dhcp_option ( options, DHCP_EB_SIADDR,
455 &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
458 assert ( options->len <= options->max_len );
463 /****************************************************************************
465 * DHCP to UDP interface
469 static inline struct dhcp_session *
470 udp_to_dhcp ( struct udp_connection *conn ) {
471 return container_of ( conn, struct dhcp_session, udp );
475 * Mark DHCP session as complete
477 * @v dhcp DHCP session
478 * @v rc Return status code
480 static void dhcp_done ( struct dhcp_session *dhcp, int rc ) {
481 /* Free up options if we failed */
483 if ( dhcp->options ) {
484 free_dhcp_options ( dhcp->options );
485 dhcp->options = NULL;
489 /* Mark async operation as complete */
490 async_done ( &dhcp->aop, rc );
493 /** Address for transmitting DHCP requests */
494 static struct sockaddr sa_dhcp_server = {
495 .sa_family = AF_INET,
497 .sin_addr.s_addr = INADDR_BROADCAST,
498 .sin_port = htons ( BOOTPS_PORT ),
503 * Transmit DHCP request
505 * @v conn UDP connection
506 * @v buf Temporary data buffer
507 * @v len Length of temporary data buffer
509 static void dhcp_senddata ( struct udp_connection *conn,
510 void *buf, size_t len ) {
511 struct dhcp_session *dhcp = udp_to_dhcp ( conn );
512 struct dhcp_packet dhcppkt;
515 DBG ( "Transmitting %s\n", dhcp_msgtype_name ( dhcp->state ) );
517 assert ( ( dhcp->state == DHCPDISCOVER ) ||
518 ( dhcp->state == DHCPREQUEST ) );
520 /* Create DHCP packet in temporary buffer */
521 if ( ( rc = create_dhcp_packet ( dhcp, dhcp->state, buf, len,
522 &dhcppkt ) ) != 0 ) {
523 DBG ( "Could not create DHCP packet\n" );
527 /* Copy in options common to all requests */
528 if ( ( rc = copy_dhcp_packet_options ( &dhcppkt,
529 &dhcp_request_options ) ) != 0){
530 DBG ( "Could not set common DHCP options\n" );
534 /* Copy any required options from previous server repsonse */
535 if ( dhcp->options ) {
536 if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
537 DHCP_SERVER_IDENTIFIER,
538 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
539 DBG ( "Could not set server identifier option\n" );
542 if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
544 DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
545 DBG ( "Could not set requested address option\n" );
550 /* Transmit the packet */
551 if ( ( rc = udp_sendto ( conn, &sa_dhcp_server,
552 dhcppkt.dhcphdr, dhcppkt.len ) ) != 0 ) {
553 DBG ( "Could not transmit UDP packet\n" );
559 * Transmit DHCP request
561 * @v dhcp DHCP session
563 static void dhcp_send_request ( struct dhcp_session *dhcp ) {
564 start_timer ( &dhcp->timer );
565 udp_senddata ( &dhcp->udp );
569 * Handle DHCP retry timer expiry
571 * @v timer DHCP retry timer
572 * @v fail Failure indicator
574 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
575 struct dhcp_session *dhcp =
576 container_of ( timer, struct dhcp_session, timer );
579 dhcp_done ( dhcp, -ETIMEDOUT );
581 dhcp_send_request ( dhcp );
588 * @v udp UDP connection
589 * @v data Received data
590 * @v len Length of received data
592 static void dhcp_newdata ( struct udp_connection *conn,
593 void *data, size_t len ) {
594 struct dhcp_session *dhcp = udp_to_dhcp ( conn );
595 struct dhcphdr *dhcphdr = data;
596 struct dhcp_option_block *options;
597 unsigned int msgtype;
599 /* Check for matching transaction ID */
600 if ( dhcphdr->xid != dhcp->xid ) {
601 DBG ( "DHCP wrong transaction ID (wanted %08lx, got %08lx)\n",
602 ntohl ( dhcphdr->xid ), ntohl ( dhcp->xid ) );
606 /* Parse packet and create options structure */
607 options = dhcp_parse ( dhcphdr, len );
609 DBG ( "Could not parse DHCP packet\n" );
613 /* Determine message type */
614 msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
615 DBG ( "Received %s\n", dhcp_msgtype_name ( msgtype ) );
617 /* Handle DHCP reply */
618 switch ( dhcp->state ) {
620 if ( msgtype != DHCPOFFER )
622 dhcp->state = DHCPREQUEST;
625 if ( msgtype != DHCPACK )
627 dhcp->state = DHCPACK;
634 /* Stop timer and update stored options */
635 stop_timer ( &dhcp->timer );
637 free_dhcp_options ( dhcp->options );
638 dhcp->options = options;
640 /* Transmit next packet, or terminate session */
641 if ( dhcp->state < DHCPACK ) {
642 dhcp_send_request ( dhcp );
644 dhcp_done ( dhcp, 0 );
649 free_dhcp_options ( options );
652 /** DHCP UDP operations */
653 static struct udp_operations dhcp_udp_operations = {
654 .senddata = dhcp_senddata,
655 .newdata = dhcp_newdata,
659 * Initiate DHCP on a network interface
661 * @v dhcp DHCP session
662 * @ret aop Asynchronous operation
664 * If the DHCP operation completes successfully, the
665 * dhcp_session::options field will be filled in with the resulting
666 * options block. The caller takes responsibility for eventually
667 * calling free_dhcp_options().
669 struct async_operation * start_dhcp ( struct dhcp_session *dhcp ) {
672 /* Initialise DHCP session */
673 dhcp->udp.udp_op = &dhcp_udp_operations;
674 dhcp->timer.expired = dhcp_timer_expired;
675 dhcp->state = DHCPDISCOVER;
676 /* Use least significant 32 bits of link-layer address as XID */
677 memcpy ( &dhcp->xid, ( dhcp->netdev->ll_addr
678 + dhcp->netdev->ll_protocol->ll_addr_len
679 - sizeof ( dhcp->xid ) ), sizeof ( dhcp->xid ));
681 /* Bind to local port */
682 if ( ( rc = udp_open ( &dhcp->udp, htons ( BOOTPC_PORT ) ) ) != 0 ) {
683 async_done ( &dhcp->aop, rc );
687 /* Proof of concept: just send a single DHCPDISCOVER */
688 dhcp_send_request ( dhcp );