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_DNS_SERVERS, DHCP_LOG_SERVERS,
58 DHCP_HOST_NAME, DHCP_DOMAIN_NAME, DHCP_ROOT_PATH,
59 DHCP_VENDOR_ENCAP, DHCP_TFTP_SERVER_NAME,
60 DHCP_BOOTFILE_NAME, DHCP_EB_ENCAP,
61 DHCP_ISCSI_INITIATOR_IQN ),
66 * Name a DHCP packet type
68 * @v msgtype DHCP message type
69 * @ret string DHCP mesasge type name
71 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
73 case 0: return "BOOTP"; /* Non-DHCP packet */
74 case DHCPDISCOVER: return "DHCPDISCOVER";
75 case DHCPOFFER: return "DHCPOFFER";
76 case DHCPREQUEST: return "DHCPREQUEST";
77 case DHCPDECLINE: return "DHCPDECLINE";
78 case DHCPACK: return "DHCPACK";
79 case DHCPNAK: return "DHCPNAK";
80 case DHCPRELEASE: return "DHCPRELEASE";
81 case DHCPINFORM: return "DHCPINFORM";
82 default: return "DHCP<invalid>";
87 * Calculate DHCP transaction ID for a network device
89 * @v netdev Network device
92 * Extract the least significant bits of the hardware address for use
93 * as the transaction ID.
95 static uint32_t dhcp_xid ( struct net_device *netdev ) {
98 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
99 - sizeof ( xid ) ), sizeof ( xid ) );
103 /** Options common to all DHCP requests */
104 struct dhcp_option_block dhcp_request_options = {
105 .data = dhcp_request_options_data,
106 .max_len = sizeof ( dhcp_request_options_data ),
107 .len = sizeof ( dhcp_request_options_data ),
111 * Set option within DHCP packet
113 * @v dhcppkt DHCP packet
114 * @v tag DHCP option tag
115 * @v data New value for DHCP option
116 * @v len Length of value, in bytes
117 * @ret rc Return status code
119 * Sets the option within the first available options block within the
120 * DHCP packet. Option blocks are tried in the order specified by @c
121 * dhcp_option_block_fill_order.
123 * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
124 * intercepted and inserted into the appropriate fixed fields within
125 * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
126 * ignored, since our DHCP packet assembly method relies on always
127 * having option overloading in use.
129 static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
130 unsigned int tag, const void *data,
132 struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
133 struct dhcp_option_block *options = dhcppkt->options;
134 struct dhcp_option *option = NULL;
136 /* Special-case the magic options */
138 case DHCP_OPTION_OVERLOAD:
139 /* Hard-coded in packets we create; always ignore */
142 memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
145 memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
147 case DHCP_MESSAGE_TYPE:
148 case DHCP_REQUESTED_ADDRESS:
149 case DHCP_PARAMETER_REQUEST_LIST:
150 /* These options have to be within the main options
151 * block. This doesn't seem to be required by the
152 * RFCs, but at least ISC dhcpd refuses to recognise
155 options = &dhcppkt->options[OPTS_MAIN];
158 /* Continue processing as normal */
162 /* Set option in first available options block */
163 for ( ; options < &dhcppkt->options[NUM_OPT_BLOCKS] ; options++ ) {
164 option = set_dhcp_option ( options, tag, data, len );
169 /* Update DHCP packet length */
170 dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
171 + dhcppkt->options[OPTS_MAIN].len );
173 return ( option ? 0 : -ENOSPC );
177 * Copy option into DHCP packet
179 * @v dhcppkt DHCP packet
180 * @v options DHCP option block, or NULL
181 * @v tag DHCP option tag to search for
182 * @v new_tag DHCP option tag to use for copied option
183 * @ret rc Return status code
185 * Copies a single option, if present, from the DHCP options block
186 * into a DHCP packet. The tag for the option may be changed if
187 * desired; this is required by other parts of the DHCP code.
189 * @c options may specify a single options block, or be left as NULL
190 * in order to search for the option within all registered options
193 static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
194 struct dhcp_option_block *options,
195 unsigned int tag, unsigned int new_tag ) {
196 struct dhcp_option *option;
199 option = find_dhcp_option ( options, tag );
201 if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
203 option->len ) ) != 0 )
210 * Copy options into DHCP packet
212 * @v dhcppkt DHCP packet
213 * @v options DHCP option block, or NULL
214 * @v encapsulator Encapsulating option, or zero
215 * @ret rc Return status code
217 * Copies options with the specified encapsulator from DHCP options
218 * blocks into a DHCP packet. Most options are copied verbatim.
219 * Recognised encapsulated options fields are handled as such.
221 * @c options may specify a single options block, or be left as NULL
222 * in order to copy options from all registered options blocks.
224 static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
225 struct dhcp_option_block *options,
226 unsigned int encapsulator ) {
231 for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
232 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
235 case DHCP_VENDOR_ENCAP:
236 /* Process encapsulated options field */
237 if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
243 /* Copy option to reassembled packet */
244 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
255 * Copy options into DHCP packet
257 * @v dhcppkt DHCP packet
258 * @v options DHCP option block, or NULL
259 * @ret rc Return status code
261 * Copies options from DHCP options blocks into a DHCP packet. Most
262 * options are copied verbatim. Recognised encapsulated options
263 * fields are handled as such.
265 * @c options may specify a single options block, or be left as NULL
266 * in order to copy options from all registered options blocks.
268 int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
269 struct dhcp_option_block *options ) {
270 return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
274 * Create a DHCP packet
276 * @v netdev Network device
277 * @v msgtype DHCP message type
278 * @v data Buffer for DHCP packet
279 * @v max_len Size of DHCP packet buffer
280 * @v dhcppkt DHCP packet structure to fill in
281 * @ret rc Return status code
283 * Creates a DHCP packet in the specified buffer, and fills out a @c
284 * dhcp_packet structure that can be passed to
285 * set_dhcp_packet_option() or copy_dhcp_packet_options().
287 int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
288 void *data, size_t max_len,
289 struct dhcp_packet *dhcppkt ) {
290 struct dhcphdr *dhcphdr = data;
291 static const uint8_t overloading = ( DHCP_OPTION_OVERLOAD_FILE |
292 DHCP_OPTION_OVERLOAD_SNAME );
296 if ( max_len < sizeof ( *dhcphdr ) )
299 /* Initialise DHCP packet content */
300 memset ( dhcphdr, 0, max_len );
301 dhcphdr->xid = dhcp_xid ( netdev );
302 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
303 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
304 dhcphdr->hlen = netdev->ll_protocol->ll_addr_len;
305 memcpy ( dhcphdr->chaddr, netdev->ll_addr, dhcphdr->hlen );
306 dhcphdr->op = dhcp_op[msgtype];
308 /* Initialise DHCP packet structure */
309 dhcppkt->dhcphdr = dhcphdr;
310 dhcppkt->max_len = max_len;
311 init_dhcp_options ( &dhcppkt->options[OPTS_MAIN], dhcphdr->options,
313 offsetof ( typeof ( *dhcphdr ), options ) ) );
314 init_dhcp_options ( &dhcppkt->options[OPTS_FILE], dhcphdr->file,
315 sizeof ( dhcphdr->file ) );
316 init_dhcp_options ( &dhcppkt->options[OPTS_SNAME], dhcphdr->sname,
317 sizeof ( dhcphdr->sname ) );
319 /* Set DHCP_OPTION_OVERLOAD option within the main options block */
320 if ( set_dhcp_option ( &dhcppkt->options[OPTS_MAIN],
321 DHCP_OPTION_OVERLOAD, &overloading,
322 sizeof ( overloading ) ) == NULL )
325 /* Set DHCP_MESSAGE_TYPE option */
326 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
328 sizeof ( msgtype ) ) ) != 0 )
335 * Calculate used length of a field containing DHCP options
337 * @v data Field containing DHCP options
338 * @v max_len Field length
339 * @ret len Used length (excluding the @c DHCP_END tag)
341 static size_t dhcp_field_len ( const void *data, size_t max_len ) {
342 struct dhcp_option_block options;
343 struct dhcp_option *end;
345 options.data = ( ( void * ) data );
346 options.len = max_len;
347 end = find_dhcp_option ( &options, DHCP_END );
348 return ( end ? ( ( ( void * ) end ) - data ) : 0 );
352 * Merge field containing DHCP options or string into DHCP options block
354 * @v options DHCP option block
355 * @v data Field containing DHCP options
356 * @v max_len Field length
357 * @v tag DHCP option tag, or 0
359 * If @c tag is non-zero, the field will be treated as a
360 * NUL-terminated string representing the value of the specified DHCP
361 * option. If @c tag is zero, the field will be treated as a block of
362 * DHCP options, and simply appended to the existing options in the
365 * The caller must ensure that there is enough space in the options
366 * block to perform the merge.
368 static void merge_dhcp_field ( struct dhcp_option_block *options,
369 const void *data, size_t max_len,
373 struct dhcp_option *end;
376 set_dhcp_option ( options, tag, data, strlen ( data ) );
378 len = dhcp_field_len ( data, max_len );
379 dest = ( options->data + options->len - 1 );
380 memcpy ( dest, data, len );
382 end = ( dest + len );
388 * Parse DHCP packet and construct DHCP options block
390 * @v dhcp DHCP session
391 * @v dhcphdr DHCP packet
392 * @v len Length of DHCP packet
393 * @ret options DHCP options block, or NULL
395 * Parses a received DHCP packet and canonicalises its contents into a
396 * single DHCP options block. The "file" and "sname" fields are
397 * converted into the corresponding DHCP options (@c
398 * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
399 * these fields are used for option overloading, their options are
400 * merged in to the options block.
402 * The values of the "yiaddr" and "siaddr" fields will be stored
403 * within the options block as the magic options @c DHCP_EB_YIADDR and
406 * Note that this call allocates new memory for the constructed DHCP
407 * options block; it is the responsibility of the caller to eventually
410 static struct dhcp_option_block * dhcp_parse ( struct dhcp_session *dhcp,
411 struct dhcphdr *dhcphdr,
413 struct dhcp_option_block *options;
415 unsigned int overloading;
418 if ( len < sizeof ( *dhcphdr ) )
421 /* Calculate size of resulting concatenated option block:
423 * The "options" field : length of the field minus the DHCP_END tag.
425 * The "file" field : maximum length of the field minus the
426 * NUL terminator, plus a 2-byte DHCP header or, if used for
427 * option overloading, the length of the field minus the
430 * The "sname" field : as for the "file" field.
432 * 15 bytes for an encapsulated options field to contain the
433 * value of the "yiaddr" and "siaddr" fields
435 * 1 byte for a final terminating DHCP_END tag.
437 options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
438 + ( sizeof ( dhcphdr->file ) + 1 )
439 + ( sizeof ( dhcphdr->sname ) + 1 )
440 + 15 /* yiaddr and siaddr */
441 + 1 /* DHCP_END tag */ );
443 /* Allocate empty options block of required size */
444 options = alloc_dhcp_options ( options_len );
446 DBGC ( dhcp, "DHCP %p could not allocate %d-byte option "
447 "block\n", dhcp, options_len );
451 /* Merge in "options" field, if this is a DHCP packet */
452 if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
453 merge_dhcp_field ( options, dhcphdr->options,
455 offsetof ( typeof (*dhcphdr), options ) ),
456 0 /* Always contains options */ );
459 /* Identify overloaded fields */
460 overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
462 /* Merge in "file" and "sname" fields */
463 merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
464 ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
465 0 : DHCP_BOOTFILE_NAME ) );
466 merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
467 ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
468 0 : DHCP_TFTP_SERVER_NAME ) );
470 /* Set magic options for "yiaddr" and "siaddr", if present */
471 if ( dhcphdr->yiaddr.s_addr ) {
472 set_dhcp_option ( options, DHCP_EB_YIADDR,
473 &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
475 if ( dhcphdr->siaddr.s_addr ) {
476 set_dhcp_option ( options, DHCP_EB_SIADDR,
477 &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
480 assert ( options->len <= options->max_len );
485 /****************************************************************************
487 * DHCP to UDP interface
491 static inline struct dhcp_session *
492 udp_to_dhcp ( struct udp_connection *conn ) {
493 return container_of ( conn, struct dhcp_session, udp );
497 * Mark DHCP session as complete
499 * @v dhcp DHCP session
500 * @v rc Return status code
502 static void dhcp_done ( struct dhcp_session *dhcp, int rc ) {
504 /* Free up options if we failed */
506 if ( dhcp->options ) {
507 free_dhcp_options ( dhcp->options );
508 dhcp->options = NULL;
512 /* Stop retry timer */
513 stop_timer ( &dhcp->timer );
515 /* Close UDP connection */
516 udp_close ( &dhcp->udp );
518 /* Release reference on net device */
519 ref_del ( &dhcp->netdev_ref );
521 /* Mark async operation as complete */
522 async_done ( &dhcp->async, rc );
525 /** Address for transmitting DHCP requests */
527 struct sockaddr_tcpip st;
528 struct sockaddr_in sin;
531 .sin_family = AF_INET,
532 .sin_addr.s_addr = INADDR_BROADCAST,
533 .sin_port = htons ( BOOTPS_PORT ),
538 * Transmit DHCP request
540 * @v conn UDP connection
541 * @v buf Temporary data buffer
542 * @v len Length of temporary data buffer
543 * @ret rc Return status code
545 static int dhcp_senddata ( struct udp_connection *conn,
546 void *buf, size_t len ) {
547 struct dhcp_session *dhcp = udp_to_dhcp ( conn );
548 struct dhcp_packet dhcppkt;
551 DBGC ( dhcp, "DHCP %p transmitting %s\n",
552 dhcp, dhcp_msgtype_name ( dhcp->state ) );
554 assert ( ( dhcp->state == DHCPDISCOVER ) ||
555 ( dhcp->state == DHCPREQUEST ) );
557 /* Create DHCP packet in temporary buffer */
558 if ( ( rc = create_dhcp_packet ( dhcp->netdev, dhcp->state, buf, len,
559 &dhcppkt ) ) != 0 ) {
560 DBGC ( dhcp, "DHCP %p could not create DHCP packet: %s\n",
561 dhcp, strerror ( rc ) );
565 /* Copy in options common to all requests */
566 if ( ( rc = copy_dhcp_packet_options ( &dhcppkt,
567 &dhcp_request_options ) ) != 0){
568 DBGC ( dhcp, "DHCP %p could not set common DHCP options: %s\n",
569 dhcp, strerror ( rc ) );
573 /* Copy any required options from previous server repsonse */
574 if ( dhcp->options ) {
575 if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
576 DHCP_SERVER_IDENTIFIER,
577 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
578 DBGC ( dhcp, "DHCP %p could not set server identifier "
579 "option: %s\n", dhcp, strerror ( rc ) );
582 if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
584 DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
585 DBGC ( dhcp, "DHCP %p could not set requested address "
586 "option: %s\n", dhcp, strerror ( rc ) );
591 /* Transmit the packet */
592 if ( ( rc = udp_sendto_via ( conn, &sa_dhcp_server.st, dhcp->netdev,
593 dhcppkt.dhcphdr, dhcppkt.len ) ) != 0 ) {
594 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
595 dhcp, strerror ( rc ) );
603 * Transmit DHCP request
605 * @v dhcp DHCP session
607 static void dhcp_send_request ( struct dhcp_session *dhcp ) {
608 start_timer ( &dhcp->timer );
609 udp_senddata ( &dhcp->udp );
613 * Handle DHCP retry timer expiry
615 * @v timer DHCP retry timer
616 * @v fail Failure indicator
618 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
619 struct dhcp_session *dhcp =
620 container_of ( timer, struct dhcp_session, timer );
623 dhcp_done ( dhcp, -ETIMEDOUT );
625 dhcp_send_request ( dhcp );
632 * @v udp UDP connection
633 * @v data Received data
634 * @v len Length of received data
635 * @v st_src Partially-filled source address
636 * @v st_dest Partially-filled destination address
638 static int dhcp_newdata ( struct udp_connection *conn, void *data, size_t len,
639 struct sockaddr_tcpip *st_src __unused,
640 struct sockaddr_tcpip *st_dest __unused ) {
641 struct dhcp_session *dhcp = udp_to_dhcp ( conn );
642 struct dhcphdr *dhcphdr = data;
643 struct dhcp_option_block *options;
644 unsigned int msgtype;
646 /* Check for matching transaction ID */
647 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
648 DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
649 "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
650 ntohl ( dhcp_xid ( dhcp->netdev ) ) );
654 /* Parse packet and create options structure */
655 options = dhcp_parse ( dhcp, dhcphdr, len );
657 DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
661 /* Determine message type */
662 msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
663 DBGC ( dhcp, "DHCP %p received %s\n",
664 dhcp, dhcp_msgtype_name ( msgtype ) );
666 /* Handle DHCP reply */
667 switch ( dhcp->state ) {
669 if ( msgtype != DHCPOFFER )
671 dhcp->state = DHCPREQUEST;
674 if ( msgtype != DHCPACK )
676 dhcp->state = DHCPACK;
683 /* Stop timer and update stored options */
684 stop_timer ( &dhcp->timer );
686 free_dhcp_options ( dhcp->options );
687 dhcp->options = options;
689 /* Transmit next packet, or terminate session */
690 if ( dhcp->state < DHCPACK ) {
691 dhcp_send_request ( dhcp );
693 dhcp_done ( dhcp, 0 );
698 free_dhcp_options ( options );
702 /** DHCP UDP operations */
703 static struct udp_operations dhcp_udp_operations = {
704 .senddata = dhcp_senddata,
705 .newdata = dhcp_newdata,
709 * Forget reference to net_device
711 * @v ref Persistent reference
713 static void dhcp_forget_netdev ( struct reference *ref ) {
714 struct dhcp_session *dhcp
715 = container_of ( ref, struct dhcp_session, netdev_ref );
717 /* Kill DHCP session immediately */
718 dhcp_done ( dhcp, -ENETUNREACH );
722 * Initiate DHCP on a network interface
724 * @v dhcp DHCP session
725 * @v parent Parent asynchronous operation
726 * @ret rc Return status code
728 * If the DHCP operation completes successfully, the
729 * dhcp_session::options field will be filled in with the resulting
730 * options block. The caller takes responsibility for eventually
731 * calling free_dhcp_options().
733 int start_dhcp ( struct dhcp_session *dhcp, struct async *parent ) {
736 /* Initialise DHCP session */
737 dhcp->udp.udp_op = &dhcp_udp_operations;
738 dhcp->timer.expired = dhcp_timer_expired;
739 dhcp->state = DHCPDISCOVER;
741 /* Bind to local port */
742 if ( ( rc = udp_open ( &dhcp->udp, htons ( BOOTPC_PORT ) ) ) != 0 )
745 /* Add persistent reference to net device */
746 dhcp->netdev_ref.forget = dhcp_forget_netdev;
747 ref_add ( &dhcp->netdev_ref, &dhcp->netdev->references );
749 /* Proof of concept: just send a single DHCPDISCOVER */
750 dhcp_send_request ( dhcp );
752 async_init ( &dhcp->async, &default_async_operations, parent );