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,
298 unsigned int msgtype,
299 void *data, size_t max_len,
300 struct dhcp_packet *dhcppkt ) {
301 struct dhcphdr *dhcphdr = data;
305 if ( max_len < sizeof ( *dhcphdr ) )
308 /* Initialise DHCP packet content */
309 memset ( dhcphdr, 0, max_len );
310 dhcphdr->xid = dhcp_xid ( netdev );
311 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
312 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
313 dhcphdr->hlen = netdev->ll_protocol->ll_addr_len;
314 memcpy ( dhcphdr->chaddr, netdev->ll_addr, dhcphdr->hlen );
315 dhcphdr->op = dhcp_op[msgtype];
317 /* Initialise DHCP packet structure */
318 dhcppkt->dhcphdr = dhcphdr;
319 dhcppkt->max_len = max_len;
320 init_dhcp_options ( &dhcppkt->options, dhcphdr->options,
322 offsetof ( typeof ( *dhcphdr ), options ) ) );
324 /* Set DHCP_MESSAGE_TYPE option */
325 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
327 sizeof ( msgtype ) ) ) != 0 )
334 * Calculate used length of a field containing DHCP options
336 * @v data Field containing DHCP options
337 * @v max_len Field length
338 * @ret len Used length (excluding the @c DHCP_END tag)
340 static size_t dhcp_field_len ( const void *data, size_t max_len ) {
341 struct dhcp_option_block options;
342 struct dhcp_option *end;
344 options.data = ( ( void * ) data );
345 options.len = max_len;
346 end = find_dhcp_option ( &options, DHCP_END );
347 return ( end ? ( ( ( void * ) end ) - data ) : 0 );
351 * Merge field containing DHCP options or string into DHCP options block
353 * @v options DHCP option block
354 * @v data Field containing DHCP options
355 * @v max_len Field length
356 * @v tag DHCP option tag, or 0
358 * If @c tag is non-zero, the field will be treated as a
359 * NUL-terminated string representing the value of the specified DHCP
360 * option. If @c tag is zero, the field will be treated as a block of
361 * DHCP options, and simply appended to the existing options in the
364 * The caller must ensure that there is enough space in the options
365 * block to perform the merge.
367 static void merge_dhcp_field ( struct dhcp_option_block *options,
368 const void *data, size_t max_len,
372 struct dhcp_option *end;
375 set_dhcp_option ( options, tag, data, strlen ( data ) );
377 len = dhcp_field_len ( data, max_len );
378 dest = ( options->data + options->len - 1 );
379 memcpy ( dest, data, len );
381 end = ( dest + len );
387 * Parse DHCP packet and construct DHCP options block
389 * @v dhcphdr DHCP packet
390 * @v len Length of DHCP packet
391 * @ret options DHCP options block, or NULL
393 * Parses a received DHCP packet and canonicalises its contents into a
394 * single DHCP options block. The "file" and "sname" fields are
395 * converted into the corresponding DHCP options (@c
396 * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
397 * these fields are used for option overloading, their options are
398 * merged in to the options block.
400 * The values of the "yiaddr" and "siaddr" fields will be stored
401 * within the options block as the magic options @c DHCP_EB_YIADDR and
404 * Note that this call allocates new memory for the constructed DHCP
405 * options block; it is the responsibility of the caller to eventually
408 static struct dhcp_option_block * dhcp_parse ( const struct dhcphdr *dhcphdr,
410 struct dhcp_option_block *options;
412 unsigned int overloading;
415 if ( len < sizeof ( *dhcphdr ) )
418 /* Calculate size of resulting concatenated option block:
420 * The "options" field : length of the field minus the DHCP_END tag.
422 * The "file" field : maximum length of the field minus the
423 * NUL terminator, plus a 2-byte DHCP header or, if used for
424 * option overloading, the length of the field minus the
427 * The "sname" field : as for the "file" field.
429 * 15 bytes for an encapsulated options field to contain the
430 * value of the "yiaddr" and "siaddr" fields
432 * 1 byte for a final terminating DHCP_END tag.
434 options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
435 + ( sizeof ( dhcphdr->file ) + 1 )
436 + ( sizeof ( dhcphdr->sname ) + 1 )
437 + 15 /* yiaddr and siaddr */
438 + 1 /* DHCP_END tag */ );
440 /* Allocate empty options block of required size */
441 options = alloc_dhcp_options ( options_len );
443 DBG ( "DHCP could not allocate %d-byte option block\n",
448 /* Merge in "options" field, if this is a DHCP packet */
449 if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
450 merge_dhcp_field ( options, dhcphdr->options,
452 offsetof ( typeof (*dhcphdr), options ) ),
453 0 /* Always contains options */ );
456 /* Identify overloaded fields */
457 overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
459 /* Merge in "file" and "sname" fields */
460 merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
461 ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
462 0 : DHCP_BOOTFILE_NAME ) );
463 merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
464 ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
465 0 : DHCP_TFTP_SERVER_NAME ) );
467 /* Set magic options for "yiaddr" and "siaddr", if present */
468 if ( dhcphdr->yiaddr.s_addr ) {
469 set_dhcp_option ( options, DHCP_EB_YIADDR,
470 &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
472 if ( dhcphdr->siaddr.s_addr ) {
473 set_dhcp_option ( options, DHCP_EB_SIADDR,
474 &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
477 assert ( options->len <= options->max_len );
482 /****************************************************************************
484 * Whole-packet construction
488 /** DHCP network device descriptor */
489 struct dhcp_netdev_desc {
496 } __attribute__ (( packed ));
499 * Create DHCP request
501 * @v netdev Network device
502 * @v msgtype DHCP message type
503 * @v options DHCP server response options, or NULL
504 * @v data Buffer for DHCP packet
505 * @v max_len Size of DHCP packet buffer
506 * @v dhcppkt DHCP packet structure to fill in
507 * @ret rc Return status code
509 int create_dhcp_request ( struct net_device *netdev, int msgtype,
510 struct dhcp_option_block *options,
511 void *data, size_t max_len,
512 struct dhcp_packet *dhcppkt ) {
513 struct device_description *desc = &netdev->dev->desc;
514 struct dhcp_netdev_desc dhcp_desc;
515 size_t dhcp_features_len;
518 /* Create DHCP packet */
519 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
521 DBG ( "DHCP could not create DHCP packet: %s\n",
526 /* Copy in options common to all requests */
527 if ( ( rc = copy_dhcp_packet_options ( dhcppkt,
528 &dhcp_request_options )) !=0 ){
529 DBG ( "DHCP could not set common DHCP options: %s\n",
534 /* Copy any required options from previous server repsonse */
536 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
537 DHCP_SERVER_IDENTIFIER,
538 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
539 DBG ( "DHCP could not set server identifier "
540 "option: %s\n", strerror ( rc ) );
543 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
545 DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
546 DBG ( "DHCP could not set requested address "
547 "option: %s\n", strerror ( rc ) );
552 /* Add options to identify the feature list */
553 dhcp_features_len = ( dhcp_features_end - dhcp_features );
554 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_ENCAP,
556 dhcp_features_len ) ) != 0 ) {
557 DBG ( "DHCP could not set features list option: %s\n",
562 /* Add options to identify the network device */
563 dhcp_desc.type = desc->bus_type;
564 dhcp_desc.vendor = htons ( desc->vendor );
565 dhcp_desc.device = htons ( desc->device );
566 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_BUS_ID,
568 sizeof ( dhcp_desc ) ) ) != 0 ) {
569 DBG ( "DHCP could not set bus ID option: %s\n",
578 * Create DHCP response
580 * @v netdev Network device
581 * @v msgtype DHCP message type
582 * @v options DHCP options, or NULL
583 * @v data Buffer for DHCP packet
584 * @v max_len Size of DHCP packet buffer
585 * @v dhcppkt DHCP packet structure to fill in
586 * @ret rc Return status code
588 int create_dhcp_response ( struct net_device *netdev, int msgtype,
589 struct dhcp_option_block *options,
590 void *data, size_t max_len,
591 struct dhcp_packet *dhcppkt ) {
594 /* Create packet and copy in options */
595 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
597 DBG ( " failed to build packet" );
600 if ( ( rc = copy_dhcp_packet_options ( dhcppkt, options ) ) != 0 ) {
601 DBG ( " failed to copy options" );
608 /****************************************************************************
610 * DHCP to UDP interface
614 /** A DHCP session */
615 struct dhcp_session {
616 /** Reference counter */
617 struct refcnt refcnt;
618 /** Job control interface */
619 struct job_interface job;
620 /** Data transfer interface */
621 struct xfer_interface xfer;
623 /** Network device being configured */
624 struct net_device *netdev;
625 /** Option block registration routine */
626 int ( * register_options ) ( struct net_device *netdev,
627 struct dhcp_option_block *options );
629 /** State of the session
631 * This is a value for the @c DHCP_MESSAGE_TYPE option
632 * (e.g. @c DHCPDISCOVER).
635 /** Options obtained from server */
636 struct dhcp_option_block *options;
637 /** Retransmission timer */
638 struct retry_timer timer;
644 * @v refcnt Reference counter
646 static void dhcp_free ( struct refcnt *refcnt ) {
647 struct dhcp_session *dhcp =
648 container_of ( refcnt, struct dhcp_session, refcnt );
650 netdev_put ( dhcp->netdev );
651 dhcpopt_put ( dhcp->options );
656 * Mark DHCP session as complete
658 * @v dhcp DHCP session
659 * @v rc Return status code
661 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
663 /* Block futher incoming messages */
664 job_nullify ( &dhcp->job );
665 xfer_nullify ( &dhcp->xfer );
667 /* Stop retry timer */
668 stop_timer ( &dhcp->timer );
670 /* Free resources and close interfaces */
671 xfer_close ( &dhcp->xfer, rc );
672 job_done ( &dhcp->job, rc );
675 /****************************************************************************
677 * Data transfer interface
682 * Transmit DHCP request
684 * @v dhcp DHCP session
685 * @ret rc Return status code
687 static int dhcp_send_request ( struct dhcp_session *dhcp ) {
688 struct xfer_metadata meta = {
689 .netdev = dhcp->netdev,
691 struct io_buffer *iobuf;
692 struct dhcp_packet dhcppkt;
695 DBGC ( dhcp, "DHCP %p transmitting %s\n",
696 dhcp, dhcp_msgtype_name ( dhcp->state ) );
698 assert ( ( dhcp->state == DHCPDISCOVER ) ||
699 ( dhcp->state == DHCPREQUEST ) );
701 /* Start retry timer. Do this first so that failures to
702 * transmit will be retried.
704 start_timer ( &dhcp->timer );
706 /* Allocate buffer for packet */
707 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
711 /* Create DHCP packet in temporary buffer */
712 if ( ( rc = create_dhcp_request ( dhcp->netdev, dhcp->state,
713 dhcp->options, iobuf->data,
714 iob_tailroom ( iobuf ),
715 &dhcppkt ) ) != 0 ) {
716 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
717 dhcp, strerror ( rc ) );
721 /* Transmit the packet */
722 iob_put ( iobuf, dhcppkt.len );
723 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
726 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
727 dhcp, strerror ( rc ) );
737 * Handle DHCP retry timer expiry
739 * @v timer DHCP retry timer
740 * @v fail Failure indicator
742 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
743 struct dhcp_session *dhcp =
744 container_of ( timer, struct dhcp_session, timer );
747 dhcp_finished ( dhcp, -ETIMEDOUT );
749 dhcp_send_request ( dhcp );
756 * @v xfer Data transfer interface
757 * @v iobuf I/O buffer
758 * @v data Received data
759 * @v len Length of received data
760 * @ret rc Return status code
762 static int dhcp_deliver_raw ( struct xfer_interface *xfer,
763 const void *data, size_t len ) {
764 struct dhcp_session *dhcp =
765 container_of ( xfer, struct dhcp_session, xfer );
766 const struct dhcphdr *dhcphdr = data;
767 struct dhcp_option_block *options;
768 unsigned int msgtype;
770 /* Check for matching transaction ID */
771 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
772 DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
773 "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
774 ntohl ( dhcp_xid ( dhcp->netdev ) ) );
778 /* Parse packet and create options structure */
779 options = dhcp_parse ( dhcphdr, len );
781 DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
785 /* Determine message type */
786 msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
787 DBGC ( dhcp, "DHCP %p received %s\n",
788 dhcp, dhcp_msgtype_name ( msgtype ) );
790 /* Handle DHCP reply */
791 switch ( dhcp->state ) {
793 if ( msgtype != DHCPOFFER )
795 dhcp->state = DHCPREQUEST;
798 if ( msgtype != DHCPACK )
800 dhcp->state = DHCPACK;
807 /* Stop timer and update stored options */
808 stop_timer ( &dhcp->timer );
810 dhcpopt_put ( dhcp->options );
811 dhcp->options = options;
813 /* Transmit next packet, or terminate session */
814 if ( dhcp->state < DHCPACK ) {
815 dhcp_send_request ( dhcp );
817 dhcp->register_options ( dhcp->netdev, dhcp->options );
818 dhcp_finished ( dhcp, 0 );
823 dhcpopt_put ( options );
827 /** DHCP data transfer interface operations */
828 static struct xfer_interface_operations dhcp_xfer_operations = {
829 .close = ignore_xfer_close,
830 .vredirect = xfer_vopen,
831 .seek = ignore_xfer_seek,
832 .window = unlimited_xfer_window,
833 .deliver_iob = xfer_deliver_as_raw,
834 .deliver_raw = dhcp_deliver_raw,
837 /****************************************************************************
839 * Job control interface
844 * Handle kill() event received via job control interface
846 * @v job DHCP job control interface
848 static void dhcp_job_kill ( struct job_interface *job ) {
849 struct dhcp_session *dhcp =
850 container_of ( job, struct dhcp_session, job );
852 /* Terminate DHCP session */
853 dhcp_finished ( dhcp, -ECANCELED );
856 /** DHCP job control interface operations */
857 static struct job_interface_operations dhcp_job_operations = {
858 .done = ignore_job_done,
859 .kill = dhcp_job_kill,
860 .progress = ignore_job_progress,
863 /****************************************************************************
870 * Start DHCP on a network device
872 * @v job Job control interface
873 * @v netdev Network device
874 * @v register_options DHCP option block registration routine
875 * @ret rc Return status code
877 * Starts DHCP on the specified network device. If successful, the @c
878 * register_options() routine will be called with the acquired
881 int start_dhcp ( struct job_interface *job, struct net_device *netdev,
882 int ( * register_options ) ( struct net_device *netdev,
883 struct dhcp_option_block * ) ) {
884 static struct sockaddr_in server = {
885 .sin_family = AF_INET,
886 .sin_addr.s_addr = INADDR_BROADCAST,
887 .sin_port = htons ( BOOTPS_PORT ),
889 static struct sockaddr_in client = {
890 .sin_family = AF_INET,
891 .sin_port = htons ( BOOTPC_PORT ),
893 struct dhcp_session *dhcp;
896 /* Allocate and initialise structure */
897 dhcp = zalloc ( sizeof ( *dhcp ) );
900 dhcp->refcnt.free = dhcp_free;
901 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
902 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
903 dhcp->netdev = netdev_get ( netdev );
904 dhcp->register_options = register_options;
905 dhcp->timer.expired = dhcp_timer_expired;
906 dhcp->state = DHCPDISCOVER;
908 /* Instantiate child objects and attach to our interfaces */
909 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
910 ( struct sockaddr * ) &server,
911 ( struct sockaddr * ) &client ) ) != 0 )
914 /* Start timer to initiate initial DHCPREQUEST */
915 start_timer ( &dhcp->timer );
917 /* Attach parent interface, mortalise self, and return */
918 job_plug_plug ( &dhcp->job, job );
919 ref_put ( &dhcp->refcnt );
923 dhcp_finished ( dhcp, rc );
924 ref_put ( &dhcp->refcnt );
928 /****************************************************************************
930 * Network device configurator
935 * Configure network device from DHCP options
937 * @v netdev Network device
938 * @v options DHCP options block
939 * @ret rc Return status code
941 int dhcp_configure_netdev ( struct net_device *netdev,
942 struct dhcp_option_block *options ) {
943 struct in_addr address = { 0 };
944 struct in_addr netmask = { 0 };
945 struct in_addr gateway = { INADDR_NONE };
948 /* Clear any existing routing table entry */
949 del_ipv4_address ( netdev );
951 /* Retrieve IP address configuration */
952 find_dhcp_ipv4_option ( options, DHCP_EB_YIADDR, &address );
953 find_dhcp_ipv4_option ( options, DHCP_SUBNET_MASK, &netmask );
954 find_dhcp_ipv4_option ( options, DHCP_ROUTERS, &gateway );
956 /* Set up new IP address configuration */
957 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
959 DBG ( "Could not configure %s with DHCP results: %s\n",
960 netdev->name, strerror ( rc ) );