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/xfer.h>
28 #include <gpxe/open.h>
30 #include <gpxe/retry.h>
31 #include <gpxe/tcpip.h>
33 #include <gpxe/dhcp.h>
37 * Dynamic Host Configuration Protocol
41 /** DHCP operation types
43 * This table maps from DHCP message types (i.e. values of the @c
44 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
47 static const uint8_t dhcp_op[] = {
48 [DHCPDISCOVER] = BOOTP_REQUEST,
49 [DHCPOFFER] = BOOTP_REPLY,
50 [DHCPREQUEST] = BOOTP_REQUEST,
51 [DHCPDECLINE] = BOOTP_REQUEST,
52 [DHCPACK] = BOOTP_REPLY,
53 [DHCPNAK] = BOOTP_REPLY,
54 [DHCPRELEASE] = BOOTP_REQUEST,
55 [DHCPINFORM] = BOOTP_REQUEST,
58 /** Raw option data for options common to all DHCP requests */
59 static uint8_t dhcp_request_options_data[] = {
60 DHCP_MAX_MESSAGE_SIZE, DHCP_WORD ( ETH_MAX_MTU ),
62 DHCP_STRING ( 'E', 't', 'h', 'e', 'r', 'b', 'o', 'o', 't' ),
63 DHCP_PARAMETER_REQUEST_LIST,
64 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS, DHCP_LOG_SERVERS,
65 DHCP_HOST_NAME, DHCP_DOMAIN_NAME, DHCP_ROOT_PATH,
66 DHCP_VENDOR_ENCAP, DHCP_TFTP_SERVER_NAME,
67 DHCP_BOOTFILE_NAME, DHCP_EB_ENCAP,
68 DHCP_ISCSI_INITIATOR_IQN ),
73 * Name a DHCP packet type
75 * @v msgtype DHCP message type
76 * @ret string DHCP mesasge type name
78 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
80 case 0: return "BOOTP"; /* Non-DHCP packet */
81 case DHCPDISCOVER: return "DHCPDISCOVER";
82 case DHCPOFFER: return "DHCPOFFER";
83 case DHCPREQUEST: return "DHCPREQUEST";
84 case DHCPDECLINE: return "DHCPDECLINE";
85 case DHCPACK: return "DHCPACK";
86 case DHCPNAK: return "DHCPNAK";
87 case DHCPRELEASE: return "DHCPRELEASE";
88 case DHCPINFORM: return "DHCPINFORM";
89 default: return "DHCP<invalid>";
94 * Calculate DHCP transaction ID for a network device
96 * @v netdev Network device
99 * Extract the least significant bits of the hardware address for use
100 * as the transaction ID.
102 static uint32_t dhcp_xid ( struct net_device *netdev ) {
105 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
106 - sizeof ( xid ) ), sizeof ( xid ) );
110 /** Options common to all DHCP requests */
111 struct dhcp_option_block dhcp_request_options = {
112 .data = dhcp_request_options_data,
113 .max_len = sizeof ( dhcp_request_options_data ),
114 .len = sizeof ( dhcp_request_options_data ),
118 * Set option within DHCP packet
120 * @v dhcppkt DHCP packet
121 * @v tag DHCP option tag
122 * @v data New value for DHCP option
123 * @v len Length of value, in bytes
124 * @ret rc Return status code
126 * Sets the option within the first available options block within the
127 * DHCP packet. Option blocks are tried in the order specified by @c
128 * dhcp_option_block_fill_order.
130 * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
131 * intercepted and inserted into the appropriate fixed fields within
132 * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
133 * ignored, since our DHCP packet assembly method relies on always
134 * having option overloading in use.
136 static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
137 unsigned int tag, const void *data,
139 struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
140 struct dhcp_option_block *options = &dhcppkt->options;
141 struct dhcp_option *option = NULL;
143 /* Special-case the magic options */
145 case DHCP_OPTION_OVERLOAD:
146 /* Hard-coded in packets we create; always ignore */
149 memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
152 memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
154 case DHCP_TFTP_SERVER_NAME:
155 memset ( dhcphdr->sname, 0, sizeof ( dhcphdr->sname ) );
156 if ( len > sizeof ( dhcphdr->sname ) )
157 len = sizeof ( dhcphdr->sname );
158 memcpy ( dhcphdr->sname, data, len );
160 case DHCP_BOOTFILE_NAME:
161 memset ( dhcphdr->file, 0, sizeof ( dhcphdr->file ) );
162 if ( len > sizeof ( dhcphdr->file ) )
163 len = sizeof ( dhcphdr->file );
164 memcpy ( dhcphdr->file, data, len );
167 /* Continue processing as normal */
172 option = set_dhcp_option ( options, tag, data, len );
174 /* Update DHCP packet length */
175 dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
176 + dhcppkt->options.len );
178 return ( option ? 0 : -ENOSPC );
182 * Copy option into DHCP packet
184 * @v dhcppkt DHCP packet
185 * @v options DHCP option block, or NULL
186 * @v tag DHCP option tag to search for
187 * @v new_tag DHCP option tag to use for copied option
188 * @ret rc Return status code
190 * Copies a single option, if present, from the DHCP options block
191 * into a DHCP packet. The tag for the option may be changed if
192 * desired; this is required by other parts of the DHCP code.
194 * @c options may specify a single options block, or be left as NULL
195 * in order to search for the option within all registered options
198 static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
199 struct dhcp_option_block *options,
200 unsigned int tag, unsigned int new_tag ) {
201 struct dhcp_option *option;
204 option = find_dhcp_option ( options, tag );
206 if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
208 option->len ) ) != 0 )
215 * Copy options into DHCP packet
217 * @v dhcppkt DHCP packet
218 * @v options DHCP option block, or NULL
219 * @v encapsulator Encapsulating option, or zero
220 * @ret rc Return status code
222 * Copies options with the specified encapsulator from DHCP options
223 * blocks into a DHCP packet. Most options are copied verbatim.
224 * Recognised encapsulated options fields are handled as such.
226 * @c options may specify a single options block, or be left as NULL
227 * in order to copy options from all registered options blocks.
229 static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
230 struct dhcp_option_block *options,
231 unsigned int encapsulator ) {
236 for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
237 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
240 case DHCP_VENDOR_ENCAP:
241 /* Process encapsulated options field */
242 if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
248 /* Copy option to reassembled packet */
249 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
260 * Copy options into DHCP packet
262 * @v dhcppkt DHCP packet
263 * @v options DHCP option block, or NULL
264 * @ret rc Return status code
266 * Copies options from DHCP options blocks into a DHCP packet. Most
267 * options are copied verbatim. Recognised encapsulated options
268 * fields are handled as such.
270 * @c options may specify a single options block, or be left as NULL
271 * in order to copy options from all registered options blocks.
273 int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
274 struct dhcp_option_block *options ) {
275 return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
279 * Create a DHCP packet
281 * @v netdev Network device
282 * @v msgtype DHCP message type
283 * @v data Buffer for DHCP packet
284 * @v max_len Size of DHCP packet buffer
285 * @v dhcppkt DHCP packet structure to fill in
286 * @ret rc Return status code
288 * Creates a DHCP packet in the specified buffer, and fills out a @c
289 * dhcp_packet structure that can be passed to
290 * set_dhcp_packet_option() or copy_dhcp_packet_options().
292 int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
293 void *data, size_t max_len,
294 struct dhcp_packet *dhcppkt ) {
295 struct dhcphdr *dhcphdr = data;
299 if ( max_len < sizeof ( *dhcphdr ) )
302 /* Initialise DHCP packet content */
303 memset ( dhcphdr, 0, max_len );
304 dhcphdr->xid = dhcp_xid ( netdev );
305 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
306 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
307 dhcphdr->hlen = netdev->ll_protocol->ll_addr_len;
308 memcpy ( dhcphdr->chaddr, netdev->ll_addr, dhcphdr->hlen );
309 dhcphdr->op = dhcp_op[msgtype];
311 /* Initialise DHCP packet structure */
312 dhcppkt->dhcphdr = dhcphdr;
313 dhcppkt->max_len = max_len;
314 init_dhcp_options ( &dhcppkt->options, dhcphdr->options,
316 offsetof ( typeof ( *dhcphdr ), options ) ) );
318 /* Set DHCP_MESSAGE_TYPE option */
319 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
321 sizeof ( msgtype ) ) ) != 0 )
328 * Calculate used length of a field containing DHCP options
330 * @v data Field containing DHCP options
331 * @v max_len Field length
332 * @ret len Used length (excluding the @c DHCP_END tag)
334 static size_t dhcp_field_len ( const void *data, size_t max_len ) {
335 struct dhcp_option_block options;
336 struct dhcp_option *end;
338 options.data = ( ( void * ) data );
339 options.len = max_len;
340 end = find_dhcp_option ( &options, DHCP_END );
341 return ( end ? ( ( ( void * ) end ) - data ) : 0 );
345 * Merge field containing DHCP options or string into DHCP options block
347 * @v options DHCP option block
348 * @v data Field containing DHCP options
349 * @v max_len Field length
350 * @v tag DHCP option tag, or 0
352 * If @c tag is non-zero, the field will be treated as a
353 * NUL-terminated string representing the value of the specified DHCP
354 * option. If @c tag is zero, the field will be treated as a block of
355 * DHCP options, and simply appended to the existing options in the
358 * The caller must ensure that there is enough space in the options
359 * block to perform the merge.
361 static void merge_dhcp_field ( struct dhcp_option_block *options,
362 const void *data, size_t max_len,
366 struct dhcp_option *end;
369 set_dhcp_option ( options, tag, data, strlen ( data ) );
371 len = dhcp_field_len ( data, max_len );
372 dest = ( options->data + options->len - 1 );
373 memcpy ( dest, data, len );
375 end = ( dest + len );
381 * Parse DHCP packet and construct DHCP options block
383 * @v dhcphdr DHCP packet
384 * @v len Length of DHCP packet
385 * @ret options DHCP options block, or NULL
387 * Parses a received DHCP packet and canonicalises its contents into a
388 * single DHCP options block. The "file" and "sname" fields are
389 * converted into the corresponding DHCP options (@c
390 * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
391 * these fields are used for option overloading, their options are
392 * merged in to the options block.
394 * The values of the "yiaddr" and "siaddr" fields will be stored
395 * within the options block as the magic options @c DHCP_EB_YIADDR and
398 * Note that this call allocates new memory for the constructed DHCP
399 * options block; it is the responsibility of the caller to eventually
402 static struct dhcp_option_block * dhcp_parse ( const struct dhcphdr *dhcphdr,
404 struct dhcp_option_block *options;
406 unsigned int overloading;
409 if ( len < sizeof ( *dhcphdr ) )
412 /* Calculate size of resulting concatenated option block:
414 * The "options" field : length of the field minus the DHCP_END tag.
416 * The "file" field : maximum length of the field minus the
417 * NUL terminator, plus a 2-byte DHCP header or, if used for
418 * option overloading, the length of the field minus the
421 * The "sname" field : as for the "file" field.
423 * 15 bytes for an encapsulated options field to contain the
424 * value of the "yiaddr" and "siaddr" fields
426 * 1 byte for a final terminating DHCP_END tag.
428 options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
429 + ( sizeof ( dhcphdr->file ) + 1 )
430 + ( sizeof ( dhcphdr->sname ) + 1 )
431 + 15 /* yiaddr and siaddr */
432 + 1 /* DHCP_END tag */ );
434 /* Allocate empty options block of required size */
435 options = alloc_dhcp_options ( options_len );
437 DBG ( "DHCP could not allocate %d-byte option block\n",
442 /* Merge in "options" field, if this is a DHCP packet */
443 if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
444 merge_dhcp_field ( options, dhcphdr->options,
446 offsetof ( typeof (*dhcphdr), options ) ),
447 0 /* Always contains options */ );
450 /* Identify overloaded fields */
451 overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
453 /* Merge in "file" and "sname" fields */
454 merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
455 ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
456 0 : DHCP_BOOTFILE_NAME ) );
457 merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
458 ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
459 0 : DHCP_TFTP_SERVER_NAME ) );
461 /* Set magic options for "yiaddr" and "siaddr", if present */
462 if ( dhcphdr->yiaddr.s_addr ) {
463 set_dhcp_option ( options, DHCP_EB_YIADDR,
464 &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
466 if ( dhcphdr->siaddr.s_addr ) {
467 set_dhcp_option ( options, DHCP_EB_SIADDR,
468 &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
471 assert ( options->len <= options->max_len );
476 /****************************************************************************
478 * DHCP to UDP interface
482 /** A DHCP session */
483 struct dhcp_session {
484 /** Reference counter */
485 struct refcnt refcnt;
486 /** Job control interface */
487 struct job_interface job;
488 /** Data transfer interface */
489 struct xfer_interface xfer;
491 /** Network device being configured */
492 struct net_device *netdev;
493 /** Option block registration routine */
494 int ( * register_options ) ( struct net_device *netdev,
495 struct dhcp_option_block *options );
497 /** State of the session
499 * This is a value for the @c DHCP_MESSAGE_TYPE option
500 * (e.g. @c DHCPDISCOVER).
503 /** Options obtained from server */
504 struct dhcp_option_block *options;
505 /** Retransmission timer */
506 struct retry_timer timer;
512 * @v refcnt Reference counter
514 static void dhcp_free ( struct refcnt *refcnt ) {
515 struct dhcp_session *dhcp =
516 container_of ( refcnt, struct dhcp_session, refcnt );
518 netdev_put ( dhcp->netdev );
519 dhcpopt_put ( dhcp->options );
524 * Mark DHCP session as complete
526 * @v dhcp DHCP session
527 * @v rc Return status code
529 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
531 /* Block futher incoming messages */
532 job_nullify ( &dhcp->job );
533 xfer_nullify ( &dhcp->xfer );
535 /* Stop retry timer */
536 stop_timer ( &dhcp->timer );
538 /* Free resources and close interfaces */
539 xfer_close ( &dhcp->xfer, rc );
540 job_done ( &dhcp->job, rc );
543 /****************************************************************************
545 * Data transfer interface
550 * Transmit DHCP request
552 * @v dhcp DHCP session
553 * @ret rc Return status code
555 static int dhcp_send_request ( struct dhcp_session *dhcp ) {
556 struct xfer_metadata meta = {
557 .netdev = dhcp->netdev,
559 struct dhcp_packet dhcppkt;
560 struct io_buffer *iobuf;
563 DBGC ( dhcp, "DHCP %p transmitting %s\n",
564 dhcp, dhcp_msgtype_name ( dhcp->state ) );
566 assert ( ( dhcp->state == DHCPDISCOVER ) ||
567 ( dhcp->state == DHCPREQUEST ) );
569 /* Start retry timer. Do this first so that failures to
570 * transmit will be retried.
572 start_timer ( &dhcp->timer );
574 /* Allocate buffer for packet */
575 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
579 /* Create DHCP packet in temporary buffer */
580 if ( ( rc = create_dhcp_packet ( dhcp->netdev, dhcp->state,
581 iobuf->data, iob_tailroom ( iobuf ),
582 &dhcppkt ) ) != 0 ) {
583 DBGC ( dhcp, "DHCP %p could not create DHCP packet: %s\n",
584 dhcp, strerror ( rc ) );
588 /* Copy in options common to all requests */
589 if ( ( rc = copy_dhcp_packet_options ( &dhcppkt,
590 &dhcp_request_options ) ) != 0){
591 DBGC ( dhcp, "DHCP %p could not set common DHCP options: %s\n",
592 dhcp, strerror ( rc ) );
596 /* Copy any required options from previous server repsonse */
597 if ( dhcp->options ) {
598 if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
599 DHCP_SERVER_IDENTIFIER,
600 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
601 DBGC ( dhcp, "DHCP %p could not set server identifier "
602 "option: %s\n", dhcp, strerror ( rc ) );
605 if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
607 DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
608 DBGC ( dhcp, "DHCP %p could not set requested address "
609 "option: %s\n", dhcp, strerror ( rc ) );
614 /* Transmit the packet */
615 iob_put ( iobuf, dhcppkt.len );
616 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
619 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
620 dhcp, strerror ( rc ) );
630 * Handle DHCP retry timer expiry
632 * @v timer DHCP retry timer
633 * @v fail Failure indicator
635 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
636 struct dhcp_session *dhcp =
637 container_of ( timer, struct dhcp_session, timer );
640 dhcp_finished ( dhcp, -ETIMEDOUT );
642 dhcp_send_request ( dhcp );
649 * @v xfer Data transfer interface
650 * @v iobuf I/O buffer
651 * @v data Received data
652 * @v len Length of received data
653 * @ret rc Return status code
655 static int dhcp_deliver_raw ( struct xfer_interface *xfer,
656 const void *data, size_t len ) {
657 struct dhcp_session *dhcp =
658 container_of ( xfer, struct dhcp_session, xfer );
659 const struct dhcphdr *dhcphdr = data;
660 struct dhcp_option_block *options;
661 unsigned int msgtype;
663 /* Check for matching transaction ID */
664 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
665 DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
666 "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
667 ntohl ( dhcp_xid ( dhcp->netdev ) ) );
671 /* Parse packet and create options structure */
672 options = dhcp_parse ( dhcphdr, len );
674 DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
678 /* Determine message type */
679 msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
680 DBGC ( dhcp, "DHCP %p received %s\n",
681 dhcp, dhcp_msgtype_name ( msgtype ) );
683 /* Handle DHCP reply */
684 switch ( dhcp->state ) {
686 if ( msgtype != DHCPOFFER )
688 dhcp->state = DHCPREQUEST;
691 if ( msgtype != DHCPACK )
693 dhcp->state = DHCPACK;
700 /* Stop timer and update stored options */
701 stop_timer ( &dhcp->timer );
703 dhcpopt_put ( dhcp->options );
704 dhcp->options = options;
706 /* Transmit next packet, or terminate session */
707 if ( dhcp->state < DHCPACK ) {
708 dhcp_send_request ( dhcp );
710 dhcp->register_options ( dhcp->netdev, dhcp->options );
711 dhcp_finished ( dhcp, 0 );
716 dhcpopt_put ( options );
720 /** DHCP data transfer interface operations */
721 static struct xfer_interface_operations dhcp_xfer_operations = {
722 .close = ignore_xfer_close,
723 .vredirect = xfer_vopen,
724 .request = ignore_xfer_request,
725 .seek = ignore_xfer_seek,
726 .deliver_iob = xfer_deliver_as_raw,
727 .deliver_raw = dhcp_deliver_raw,
730 /****************************************************************************
732 * Job control interface
737 * Handle kill() event received via job control interface
739 * @v job DHCP job control interface
741 static void dhcp_job_kill ( struct job_interface *job ) {
742 struct dhcp_session *dhcp =
743 container_of ( job, struct dhcp_session, job );
745 /* Terminate DHCP session */
746 dhcp_finished ( dhcp, -ECANCELED );
749 /** DHCP job control interface operations */
750 static struct job_interface_operations dhcp_job_operations = {
751 .done = ignore_job_done,
752 .kill = dhcp_job_kill,
753 .progress = ignore_job_progress,
756 /****************************************************************************
763 * Start DHCP on a network device
765 * @v job Job control interface
766 * @v netdev Network device
767 * @v register_options DHCP option block registration routine
768 * @ret rc Return status code
770 * Starts DHCP on the specified network device. If successful, the @c
771 * register_options() routine will be called with the acquired
774 int start_dhcp ( struct job_interface *job, struct net_device *netdev,
775 int ( * register_options ) ( struct net_device *netdev,
776 struct dhcp_option_block * ) ) {
777 static struct sockaddr_in server = {
778 .sin_family = AF_INET,
779 .sin_addr.s_addr = INADDR_BROADCAST,
780 .sin_port = htons ( BOOTPS_PORT ),
782 static struct sockaddr_in client = {
783 .sin_family = AF_INET,
784 .sin_port = htons ( BOOTPC_PORT ),
786 struct dhcp_session *dhcp;
789 /* Allocate and initialise structure */
790 dhcp = malloc ( sizeof ( *dhcp ) );
793 memset ( dhcp, 0, sizeof ( *dhcp ) );
794 dhcp->refcnt.free = dhcp_free;
795 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
796 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
797 dhcp->netdev = netdev_get ( netdev );
798 dhcp->register_options = register_options;
799 dhcp->timer.expired = dhcp_timer_expired;
800 dhcp->state = DHCPDISCOVER;
802 /* Instantiate child objects and attach to our interfaces */
803 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
804 ( struct sockaddr * ) &server,
805 ( struct sockaddr * ) &client ) ) != 0 )
808 /* Start timer to initiate initial DHCPREQUEST */
809 start_timer ( &dhcp->timer );
811 /* Attach parent interface, mortalise self, and return */
812 job_plug_plug ( &dhcp->job, job );
813 ref_put ( &dhcp->refcnt );
817 dhcp_finished ( dhcp, rc );
818 ref_put ( &dhcp->refcnt );
822 /****************************************************************************
824 * Network device configurator
829 * Configure network device from DHCP options
831 * @v netdev Network device
832 * @v options DHCP options block
833 * @ret rc Return status code
835 int dhcp_configure_netdev ( struct net_device *netdev,
836 struct dhcp_option_block *options ) {
837 struct in_addr address = { 0 };
838 struct in_addr netmask = { 0 };
839 struct in_addr gateway = { INADDR_NONE };
842 /* Clear any existing routing table entry */
843 del_ipv4_address ( netdev );
845 /* Retrieve IP address configuration */
846 find_dhcp_ipv4_option ( options, DHCP_EB_YIADDR, &address );
847 find_dhcp_ipv4_option ( options, DHCP_SUBNET_MASK, &netmask );
848 find_dhcp_ipv4_option ( options, DHCP_ROUTERS, &gateway );
850 /* Set up new IP address configuration */
851 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
853 DBG ( "Could not configure %s with DHCP results: %s\n",
854 netdev->name, strerror ( rc ) );