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.
25 #include <gpxe/if_ether.h>
26 #include <gpxe/netdevice.h>
27 #include <gpxe/device.h>
28 #include <gpxe/xfer.h>
29 #include <gpxe/open.h>
31 #include <gpxe/retry.h>
32 #include <gpxe/tcpip.h>
34 #include <gpxe/dhcp.h>
38 * Dynamic Host Configuration Protocol
42 /** DHCP operation types
44 * This table maps from DHCP message types (i.e. values of the @c
45 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
48 static const uint8_t dhcp_op[] = {
49 [DHCPDISCOVER] = BOOTP_REQUEST,
50 [DHCPOFFER] = BOOTP_REPLY,
51 [DHCPREQUEST] = BOOTP_REQUEST,
52 [DHCPDECLINE] = BOOTP_REQUEST,
53 [DHCPACK] = BOOTP_REPLY,
54 [DHCPNAK] = BOOTP_REPLY,
55 [DHCPRELEASE] = BOOTP_REQUEST,
56 [DHCPINFORM] = BOOTP_REQUEST,
59 /** Raw option data for options common to all DHCP requests */
60 static uint8_t dhcp_request_options_data[] = {
61 DHCP_MAX_MESSAGE_SIZE, DHCP_WORD ( ETH_MAX_MTU ),
63 DHCP_STRING ( 'E', 't', 'h', 'e', 'r', 'b', 'o', 'o', 't' ),
64 DHCP_PARAMETER_REQUEST_LIST,
65 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS, DHCP_LOG_SERVERS,
66 DHCP_HOST_NAME, DHCP_DOMAIN_NAME, DHCP_ROOT_PATH,
67 DHCP_VENDOR_ENCAP, DHCP_TFTP_SERVER_NAME,
68 DHCP_BOOTFILE_NAME, DHCP_EB_ENCAP,
69 DHCP_ISCSI_INITIATOR_IQN ),
73 /** DHCP feature codes */
74 static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
75 static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
78 * Name a DHCP packet type
80 * @v msgtype DHCP message type
81 * @ret string DHCP mesasge type name
83 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
85 case 0: return "BOOTP"; /* Non-DHCP packet */
86 case DHCPDISCOVER: return "DHCPDISCOVER";
87 case DHCPOFFER: return "DHCPOFFER";
88 case DHCPREQUEST: return "DHCPREQUEST";
89 case DHCPDECLINE: return "DHCPDECLINE";
90 case DHCPACK: return "DHCPACK";
91 case DHCPNAK: return "DHCPNAK";
92 case DHCPRELEASE: return "DHCPRELEASE";
93 case DHCPINFORM: return "DHCPINFORM";
94 default: return "DHCP<invalid>";
99 * Calculate DHCP transaction ID for a network device
101 * @v netdev Network device
104 * Extract the least significant bits of the hardware address for use
105 * as the transaction ID.
107 static uint32_t dhcp_xid ( struct net_device *netdev ) {
110 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
111 - sizeof ( xid ) ), sizeof ( xid ) );
115 /** Options common to all DHCP requests */
116 static struct dhcp_option_block dhcp_request_options = {
117 .data = dhcp_request_options_data,
118 .max_len = sizeof ( dhcp_request_options_data ),
119 .len = sizeof ( dhcp_request_options_data ),
123 * Set option within DHCP packet
125 * @v dhcppkt DHCP packet
126 * @v tag DHCP option tag
127 * @v data New value for DHCP option
128 * @v len Length of value, in bytes
129 * @ret rc Return status code
131 * Sets the option within the first available options block within the
132 * DHCP packet. Option blocks are tried in the order specified by @c
133 * dhcp_option_block_fill_order.
135 * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
136 * intercepted and inserted into the appropriate fixed fields within
137 * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
138 * ignored, since our DHCP packet assembly method relies on always
139 * having option overloading in use.
141 static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
142 unsigned int tag, const void *data,
144 struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
145 struct dhcp_option_block *options = &dhcppkt->options;
146 struct dhcp_option *option = NULL;
148 /* Special-case the magic options */
150 case DHCP_OPTION_OVERLOAD:
151 /* Hard-coded in packets we create; always ignore */
154 memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
157 memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
159 case DHCP_TFTP_SERVER_NAME:
160 memset ( dhcphdr->sname, 0, sizeof ( dhcphdr->sname ) );
161 if ( len > sizeof ( dhcphdr->sname ) )
162 len = sizeof ( dhcphdr->sname );
163 memcpy ( dhcphdr->sname, data, len );
165 case DHCP_BOOTFILE_NAME:
166 memset ( dhcphdr->file, 0, sizeof ( dhcphdr->file ) );
167 if ( len > sizeof ( dhcphdr->file ) )
168 len = sizeof ( dhcphdr->file );
169 memcpy ( dhcphdr->file, data, len );
172 /* Continue processing as normal */
177 option = set_dhcp_option ( options, tag, data, len );
179 /* Update DHCP packet length */
180 dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
181 + dhcppkt->options.len );
183 return ( option ? 0 : -ENOSPC );
187 * Copy option into DHCP packet
189 * @v dhcppkt DHCP packet
190 * @v options DHCP option block, or NULL
191 * @v tag DHCP option tag to search for
192 * @v new_tag DHCP option tag to use for copied option
193 * @ret rc Return status code
195 * Copies a single option, if present, from the DHCP options block
196 * into a DHCP packet. The tag for the option may be changed if
197 * desired; this is required by other parts of the DHCP code.
199 * @c options may specify a single options block, or be left as NULL
200 * in order to search for the option within all registered options
203 static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
204 struct dhcp_option_block *options,
205 unsigned int tag, unsigned int new_tag ) {
206 struct dhcp_option *option;
209 option = find_dhcp_option ( options, tag );
211 if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
213 option->len ) ) != 0 )
220 * Copy options into DHCP packet
222 * @v dhcppkt DHCP packet
223 * @v options DHCP option block, or NULL
224 * @v encapsulator Encapsulating option, or zero
225 * @ret rc Return status code
227 * Copies options with the specified encapsulator from DHCP options
228 * blocks into a DHCP packet. Most options are copied verbatim.
229 * Recognised encapsulated options fields are handled as such.
231 * @c options may specify a single options block, or be left as NULL
232 * in order to copy options from all registered options blocks.
234 static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
235 struct dhcp_option_block *options,
236 unsigned int encapsulator ) {
241 for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
242 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
245 case DHCP_VENDOR_ENCAP:
246 /* Process encapsulated options field */
247 if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
253 /* Copy option to reassembled packet */
254 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
265 * Copy options into DHCP packet
267 * @v dhcppkt DHCP packet
268 * @v options DHCP option block, or NULL
269 * @ret rc Return status code
271 * Copies options from DHCP options blocks into a DHCP packet. Most
272 * options are copied verbatim. Recognised encapsulated options
273 * fields are handled as such.
275 * @c options may specify a single options block, or be left as NULL
276 * in order to copy options from all registered options blocks.
278 static int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
279 struct dhcp_option_block *options ) {
280 return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
284 * Create a DHCP packet
286 * @v netdev Network device
287 * @v msgtype DHCP message type
288 * @v data Buffer for DHCP packet
289 * @v max_len Size of DHCP packet buffer
290 * @v dhcppkt DHCP packet structure to fill in
291 * @ret rc Return status code
293 * Creates a DHCP packet in the specified buffer, and fills out a @c
294 * dhcp_packet structure that can be passed to
295 * set_dhcp_packet_option() or copy_dhcp_packet_options().
297 static int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
298 void *data, size_t max_len,
299 struct dhcp_packet *dhcppkt ) {
300 struct dhcphdr *dhcphdr = data;
304 if ( max_len < sizeof ( *dhcphdr ) )
307 /* Initialise DHCP packet content */
308 memset ( dhcphdr, 0, max_len );
309 dhcphdr->xid = dhcp_xid ( netdev );
310 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
311 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
312 dhcphdr->hlen = netdev->ll_protocol->ll_addr_len;
313 memcpy ( dhcphdr->chaddr, netdev->ll_addr, dhcphdr->hlen );
314 dhcphdr->op = dhcp_op[msgtype];
316 /* Initialise DHCP packet structure */
317 dhcppkt->dhcphdr = dhcphdr;
318 dhcppkt->max_len = max_len;
319 init_dhcp_options ( &dhcppkt->options, dhcphdr->options,
321 offsetof ( typeof ( *dhcphdr ), options ) ) );
323 /* Set DHCP_MESSAGE_TYPE option */
324 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
326 sizeof ( msgtype ) ) ) != 0 )
333 * Calculate used length of a field containing DHCP options
335 * @v data Field containing DHCP options
336 * @v max_len Field length
337 * @ret len Used length (excluding the @c DHCP_END tag)
339 static size_t dhcp_field_len ( const void *data, size_t max_len ) {
340 struct dhcp_option_block options;
341 struct dhcp_option *end;
343 options.data = ( ( void * ) data );
344 options.len = max_len;
345 end = find_dhcp_option ( &options, DHCP_END );
346 return ( end ? ( ( ( void * ) end ) - data ) : 0 );
350 * Merge field containing DHCP options or string into DHCP options block
352 * @v options DHCP option block
353 * @v data Field containing DHCP options
354 * @v max_len Field length
355 * @v tag DHCP option tag, or 0
357 * If @c tag is non-zero, the field will be treated as a
358 * NUL-terminated string representing the value of the specified DHCP
359 * option. If @c tag is zero, the field will be treated as a block of
360 * DHCP options, and simply appended to the existing options in the
363 * The caller must ensure that there is enough space in the options
364 * block to perform the merge.
366 static void merge_dhcp_field ( struct dhcp_option_block *options,
367 const void *data, size_t max_len,
371 struct dhcp_option *end;
374 set_dhcp_option ( options, tag, data, strlen ( data ) );
376 len = dhcp_field_len ( data, max_len );
377 dest = ( options->data + options->len - 1 );
378 memcpy ( dest, data, len );
380 end = ( dest + len );
386 * Parse DHCP packet and construct DHCP options block
388 * @v dhcphdr DHCP packet
389 * @v len Length of DHCP packet
390 * @ret options DHCP options block, or NULL
392 * Parses a received DHCP packet and canonicalises its contents into a
393 * single DHCP options block. The "file" and "sname" fields are
394 * converted into the corresponding DHCP options (@c
395 * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
396 * these fields are used for option overloading, their options are
397 * merged in to the options block.
399 * The values of the "yiaddr" and "siaddr" fields will be stored
400 * within the options block as the magic options @c DHCP_EB_YIADDR and
403 * Note that this call allocates new memory for the constructed DHCP
404 * options block; it is the responsibility of the caller to eventually
407 static struct dhcp_option_block * dhcp_parse ( const struct dhcphdr *dhcphdr,
409 struct dhcp_option_block *options;
411 unsigned int overloading;
414 if ( len < sizeof ( *dhcphdr ) )
417 /* Calculate size of resulting concatenated option block:
419 * The "options" field : length of the field minus the DHCP_END tag.
421 * The "file" field : maximum length of the field minus the
422 * NUL terminator, plus a 2-byte DHCP header or, if used for
423 * option overloading, the length of the field minus the
426 * The "sname" field : as for the "file" field.
428 * 15 bytes for an encapsulated options field to contain the
429 * value of the "yiaddr" and "siaddr" fields
431 * 1 byte for a final terminating DHCP_END tag.
433 options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
434 + ( sizeof ( dhcphdr->file ) + 1 )
435 + ( sizeof ( dhcphdr->sname ) + 1 )
436 + 15 /* yiaddr and siaddr */
437 + 1 /* DHCP_END tag */ );
439 /* Allocate empty options block of required size */
440 options = alloc_dhcp_options ( options_len );
442 DBG ( "DHCP could not allocate %d-byte option block\n",
447 /* Merge in "options" field, if this is a DHCP packet */
448 if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
449 merge_dhcp_field ( options, dhcphdr->options,
451 offsetof ( typeof (*dhcphdr), options ) ),
452 0 /* Always contains options */ );
455 /* Identify overloaded fields */
456 overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
458 /* Merge in "file" and "sname" fields */
459 merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
460 ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
461 0 : DHCP_BOOTFILE_NAME ) );
462 merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
463 ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
464 0 : DHCP_TFTP_SERVER_NAME ) );
466 /* Set magic options for "yiaddr" and "siaddr", if present */
467 if ( dhcphdr->yiaddr.s_addr ) {
468 set_dhcp_option ( options, DHCP_EB_YIADDR,
469 &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
471 if ( dhcphdr->siaddr.s_addr ) {
472 set_dhcp_option ( options, DHCP_EB_SIADDR,
473 &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
476 assert ( options->len <= options->max_len );
481 /****************************************************************************
483 * Whole-packet construction
487 /** DHCP network device descriptor */
488 struct dhcp_netdev_desc {
495 } __attribute__ (( packed ));
498 * Create DHCP request
500 * @v netdev Network device
501 * @v msgtype DHCP message type
502 * @v options DHCP server response options, or NULL
503 * @v data Buffer for DHCP packet
504 * @v max_len Size of DHCP packet buffer
505 * @v dhcppkt DHCP packet structure to fill in
506 * @ret rc Return status code
508 int create_dhcp_request ( struct net_device *netdev, int msgtype,
509 struct dhcp_option_block *options,
510 void *data, size_t max_len,
511 struct dhcp_packet *dhcppkt ) {
512 struct device_description *desc = &netdev->dev->desc;
513 struct dhcp_netdev_desc dhcp_desc;
514 size_t dhcp_features_len;
517 /* Create DHCP packet */
518 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
520 DBG ( "DHCP could not create DHCP packet: %s\n",
525 /* Copy in options common to all requests */
526 if ( ( rc = copy_dhcp_packet_options ( dhcppkt,
527 &dhcp_request_options )) !=0 ){
528 DBG ( "DHCP could not set common DHCP options: %s\n",
533 /* Copy any required options from previous server repsonse */
535 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
536 DHCP_SERVER_IDENTIFIER,
537 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
538 DBG ( "DHCP could not set server identifier "
539 "option: %s\n", strerror ( rc ) );
542 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
544 DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
545 DBG ( "DHCP could not set requested address "
546 "option: %s\n", strerror ( rc ) );
551 /* Add options to identify the feature list */
552 dhcp_features_len = ( dhcp_features_end - dhcp_features );
553 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_ENCAP,
555 dhcp_features_len ) ) != 0 ) {
556 DBG ( "DHCP could not set features list option: %s\n",
561 /* Add options to identify the network device */
562 dhcp_desc.type = desc->bus_type;
563 dhcp_desc.vendor = htons ( desc->vendor );
564 dhcp_desc.device = htons ( desc->device );
565 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_BUS_ID,
567 sizeof ( dhcp_desc ) ) ) != 0 ) {
568 DBG ( "DHCP could not set bus ID option: %s\n",
577 * Create DHCP response
579 * @v netdev Network device
580 * @v msgtype DHCP message type
581 * @v options DHCP options, or NULL
582 * @v data Buffer for DHCP packet
583 * @v max_len Size of DHCP packet buffer
584 * @v dhcppkt DHCP packet structure to fill in
585 * @ret rc Return status code
587 int create_dhcp_response ( struct net_device *netdev, int msgtype,
588 struct dhcp_option_block *options,
589 void *data, size_t max_len,
590 struct dhcp_packet *dhcppkt ) {
593 /* Create packet and copy in options */
594 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
596 DBG ( " failed to build packet" );
599 if ( ( rc = copy_dhcp_packet_options ( dhcppkt, options ) ) != 0 ) {
600 DBG ( " failed to copy options" );
607 /****************************************************************************
609 * DHCP to UDP interface
613 /** A DHCP session */
614 struct dhcp_session {
615 /** Reference counter */
616 struct refcnt refcnt;
617 /** Job control interface */
618 struct job_interface job;
619 /** Data transfer interface */
620 struct xfer_interface xfer;
622 /** Network device being configured */
623 struct net_device *netdev;
624 /** Option block registration routine */
625 int ( * register_options ) ( struct net_device *netdev,
626 struct dhcp_option_block *options );
628 /** State of the session
630 * This is a value for the @c DHCP_MESSAGE_TYPE option
631 * (e.g. @c DHCPDISCOVER).
634 /** Options obtained from server */
635 struct dhcp_option_block *options;
636 /** Retransmission timer */
637 struct retry_timer timer;
643 * @v refcnt Reference counter
645 static void dhcp_free ( struct refcnt *refcnt ) {
646 struct dhcp_session *dhcp =
647 container_of ( refcnt, struct dhcp_session, refcnt );
649 netdev_put ( dhcp->netdev );
650 dhcpopt_put ( dhcp->options );
655 * Mark DHCP session as complete
657 * @v dhcp DHCP session
658 * @v rc Return status code
660 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
662 /* Block futher incoming messages */
663 job_nullify ( &dhcp->job );
664 xfer_nullify ( &dhcp->xfer );
666 /* Stop retry timer */
667 stop_timer ( &dhcp->timer );
669 /* Free resources and close interfaces */
670 xfer_close ( &dhcp->xfer, rc );
671 job_done ( &dhcp->job, rc );
674 /****************************************************************************
676 * Data transfer interface
681 * Transmit DHCP request
683 * @v dhcp DHCP session
684 * @ret rc Return status code
686 static int dhcp_send_request ( struct dhcp_session *dhcp ) {
687 struct xfer_metadata meta = {
688 .netdev = dhcp->netdev,
690 struct io_buffer *iobuf;
691 struct dhcp_packet dhcppkt;
694 DBGC ( dhcp, "DHCP %p transmitting %s\n",
695 dhcp, dhcp_msgtype_name ( dhcp->state ) );
697 assert ( ( dhcp->state == DHCPDISCOVER ) ||
698 ( dhcp->state == DHCPREQUEST ) );
700 /* Start retry timer. Do this first so that failures to
701 * transmit will be retried.
703 start_timer ( &dhcp->timer );
705 /* Allocate buffer for packet */
706 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
710 /* Create DHCP packet in temporary buffer */
711 if ( ( rc = create_dhcp_request ( dhcp->netdev, dhcp->state,
712 dhcp->options, iobuf->data,
713 iob_tailroom ( iobuf ),
714 &dhcppkt ) ) != 0 ) {
715 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
716 dhcp, strerror ( rc ) );
720 /* Transmit the packet */
721 iob_put ( iobuf, dhcppkt.len );
722 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
725 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
726 dhcp, strerror ( rc ) );
736 * Handle DHCP retry timer expiry
738 * @v timer DHCP retry timer
739 * @v fail Failure indicator
741 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
742 struct dhcp_session *dhcp =
743 container_of ( timer, struct dhcp_session, timer );
746 dhcp_finished ( dhcp, -ETIMEDOUT );
748 dhcp_send_request ( dhcp );
755 * @v xfer Data transfer interface
756 * @v iobuf I/O buffer
757 * @v data Received data
758 * @v len Length of received data
759 * @ret rc Return status code
761 static int dhcp_deliver_raw ( struct xfer_interface *xfer,
762 const void *data, size_t len ) {
763 struct dhcp_session *dhcp =
764 container_of ( xfer, struct dhcp_session, xfer );
765 const struct dhcphdr *dhcphdr = data;
766 struct dhcp_option_block *options;
767 unsigned int msgtype;
769 /* Check for matching transaction ID */
770 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
771 DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
772 "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
773 ntohl ( dhcp_xid ( dhcp->netdev ) ) );
777 /* Parse packet and create options structure */
778 options = dhcp_parse ( dhcphdr, len );
780 DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
784 /* Determine message type */
785 msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
786 DBGC ( dhcp, "DHCP %p received %s\n",
787 dhcp, dhcp_msgtype_name ( msgtype ) );
789 /* Handle DHCP reply */
790 switch ( dhcp->state ) {
792 if ( msgtype != DHCPOFFER )
794 dhcp->state = DHCPREQUEST;
797 if ( msgtype != DHCPACK )
799 dhcp->state = DHCPACK;
806 /* Stop timer and update stored options */
807 stop_timer ( &dhcp->timer );
809 dhcpopt_put ( dhcp->options );
810 dhcp->options = options;
812 /* Transmit next packet, or terminate session */
813 if ( dhcp->state < DHCPACK ) {
814 dhcp_send_request ( dhcp );
816 dhcp->register_options ( dhcp->netdev, dhcp->options );
817 dhcp_finished ( dhcp, 0 );
822 dhcpopt_put ( options );
826 /** DHCP data transfer interface operations */
827 static struct xfer_interface_operations dhcp_xfer_operations = {
828 .close = ignore_xfer_close,
829 .vredirect = xfer_vopen,
830 .seek = ignore_xfer_seek,
831 .window = unlimited_xfer_window,
832 .deliver_iob = xfer_deliver_as_raw,
833 .deliver_raw = dhcp_deliver_raw,
836 /****************************************************************************
838 * Job control interface
843 * Handle kill() event received via job control interface
845 * @v job DHCP job control interface
847 static void dhcp_job_kill ( struct job_interface *job ) {
848 struct dhcp_session *dhcp =
849 container_of ( job, struct dhcp_session, job );
851 /* Terminate DHCP session */
852 dhcp_finished ( dhcp, -ECANCELED );
855 /** DHCP job control interface operations */
856 static struct job_interface_operations dhcp_job_operations = {
857 .done = ignore_job_done,
858 .kill = dhcp_job_kill,
859 .progress = ignore_job_progress,
862 /****************************************************************************
869 * Start DHCP on a network device
871 * @v job Job control interface
872 * @v netdev Network device
873 * @v register_options DHCP option block registration routine
874 * @ret rc Return status code
876 * Starts DHCP on the specified network device. If successful, the @c
877 * register_options() routine will be called with the acquired
880 int start_dhcp ( struct job_interface *job, struct net_device *netdev,
881 int ( * register_options ) ( struct net_device *netdev,
882 struct dhcp_option_block * ) ) {
883 static struct sockaddr_in server = {
884 .sin_family = AF_INET,
885 .sin_addr.s_addr = INADDR_BROADCAST,
886 .sin_port = htons ( BOOTPS_PORT ),
888 static struct sockaddr_in client = {
889 .sin_family = AF_INET,
890 .sin_port = htons ( BOOTPC_PORT ),
892 struct dhcp_session *dhcp;
895 /* Allocate and initialise structure */
896 dhcp = zalloc ( sizeof ( *dhcp ) );
899 dhcp->refcnt.free = dhcp_free;
900 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
901 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
902 dhcp->netdev = netdev_get ( netdev );
903 dhcp->register_options = register_options;
904 dhcp->timer.expired = dhcp_timer_expired;
905 dhcp->state = DHCPDISCOVER;
907 /* Instantiate child objects and attach to our interfaces */
908 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
909 ( struct sockaddr * ) &server,
910 ( struct sockaddr * ) &client ) ) != 0 )
913 /* Start timer to initiate initial DHCPREQUEST */
914 start_timer_nodelay ( &dhcp->timer );
916 /* Attach parent interface, mortalise self, and return */
917 job_plug_plug ( &dhcp->job, job );
918 ref_put ( &dhcp->refcnt );
922 dhcp_finished ( dhcp, rc );
923 ref_put ( &dhcp->refcnt );
927 /****************************************************************************
929 * Network device configurator
934 * Configure network device from DHCP options
936 * @v netdev Network device
937 * @v options DHCP options block
938 * @ret rc Return status code
940 int dhcp_configure_netdev ( struct net_device *netdev,
941 struct dhcp_option_block *options ) {
942 struct in_addr address = { 0 };
943 struct in_addr netmask = { 0 };
944 struct in_addr gateway = { INADDR_NONE };
947 /* Clear any existing routing table entry */
948 del_ipv4_address ( netdev );
950 /* Retrieve IP address configuration */
951 find_dhcp_ipv4_option ( options, DHCP_EB_YIADDR, &address );
952 find_dhcp_ipv4_option ( options, DHCP_SUBNET_MASK, &netmask );
953 find_dhcp_ipv4_option ( options, DHCP_ROUTERS, &gateway );
955 /* Set up new IP address configuration */
956 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
958 DBG ( "Could not configure %s with DHCP results: %s\n",
959 netdev->name, strerror ( rc ) );