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>
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 ),
74 * Name a DHCP packet type
76 * @v msgtype DHCP message type
77 * @ret string DHCP mesasge type name
79 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
81 case 0: return "BOOTP"; /* Non-DHCP packet */
82 case DHCPDISCOVER: return "DHCPDISCOVER";
83 case DHCPOFFER: return "DHCPOFFER";
84 case DHCPREQUEST: return "DHCPREQUEST";
85 case DHCPDECLINE: return "DHCPDECLINE";
86 case DHCPACK: return "DHCPACK";
87 case DHCPNAK: return "DHCPNAK";
88 case DHCPRELEASE: return "DHCPRELEASE";
89 case DHCPINFORM: return "DHCPINFORM";
90 default: return "DHCP<invalid>";
95 * Calculate DHCP transaction ID for a network device
97 * @v netdev Network device
100 * Extract the least significant bits of the hardware address for use
101 * as the transaction ID.
103 static uint32_t dhcp_xid ( struct net_device *netdev ) {
106 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
107 - sizeof ( xid ) ), sizeof ( xid ) );
111 /** Options common to all DHCP requests */
112 struct dhcp_option_block dhcp_request_options = {
113 .data = dhcp_request_options_data,
114 .max_len = sizeof ( dhcp_request_options_data ),
115 .len = sizeof ( dhcp_request_options_data ),
119 * Set option within DHCP packet
121 * @v dhcppkt DHCP packet
122 * @v tag DHCP option tag
123 * @v data New value for DHCP option
124 * @v len Length of value, in bytes
125 * @ret rc Return status code
127 * Sets the option within the first available options block within the
128 * DHCP packet. Option blocks are tried in the order specified by @c
129 * dhcp_option_block_fill_order.
131 * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
132 * intercepted and inserted into the appropriate fixed fields within
133 * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
134 * ignored, since our DHCP packet assembly method relies on always
135 * having option overloading in use.
137 static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
138 unsigned int tag, const void *data,
140 struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
141 struct dhcp_option_block *options = &dhcppkt->options;
142 struct dhcp_option *option = NULL;
144 /* Special-case the magic options */
146 case DHCP_OPTION_OVERLOAD:
147 /* Hard-coded in packets we create; always ignore */
150 memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
153 memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
155 case DHCP_TFTP_SERVER_NAME:
156 strncpy ( dhcphdr->sname, data, sizeof ( dhcphdr->sname ) );
158 case DHCP_BOOTFILE_NAME:
159 strncpy ( dhcphdr->file, data, sizeof ( dhcphdr->file ) );
162 /* Continue processing as normal */
167 option = set_dhcp_option ( options, tag, data, len );
169 /* Update DHCP packet length */
170 dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
171 + dhcppkt->options.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;
294 if ( max_len < sizeof ( *dhcphdr ) )
297 /* Initialise DHCP packet content */
298 memset ( dhcphdr, 0, max_len );
299 dhcphdr->xid = dhcp_xid ( netdev );
300 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
301 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
302 dhcphdr->hlen = netdev->ll_protocol->ll_addr_len;
303 memcpy ( dhcphdr->chaddr, netdev->ll_addr, dhcphdr->hlen );
304 dhcphdr->op = dhcp_op[msgtype];
306 /* Initialise DHCP packet structure */
307 dhcppkt->dhcphdr = dhcphdr;
308 dhcppkt->max_len = max_len;
309 init_dhcp_options ( &dhcppkt->options, dhcphdr->options,
311 offsetof ( typeof ( *dhcphdr ), options ) ) );
313 /* Set DHCP_MESSAGE_TYPE option */
314 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
316 sizeof ( msgtype ) ) ) != 0 )
323 * Calculate used length of a field containing DHCP options
325 * @v data Field containing DHCP options
326 * @v max_len Field length
327 * @ret len Used length (excluding the @c DHCP_END tag)
329 static size_t dhcp_field_len ( const void *data, size_t max_len ) {
330 struct dhcp_option_block options;
331 struct dhcp_option *end;
333 options.data = ( ( void * ) data );
334 options.len = max_len;
335 end = find_dhcp_option ( &options, DHCP_END );
336 return ( end ? ( ( ( void * ) end ) - data ) : 0 );
340 * Merge field containing DHCP options or string into DHCP options block
342 * @v options DHCP option block
343 * @v data Field containing DHCP options
344 * @v max_len Field length
345 * @v tag DHCP option tag, or 0
347 * If @c tag is non-zero, the field will be treated as a
348 * NUL-terminated string representing the value of the specified DHCP
349 * option. If @c tag is zero, the field will be treated as a block of
350 * DHCP options, and simply appended to the existing options in the
353 * The caller must ensure that there is enough space in the options
354 * block to perform the merge.
356 static void merge_dhcp_field ( struct dhcp_option_block *options,
357 const void *data, size_t max_len,
361 struct dhcp_option *end;
364 set_dhcp_option ( options, tag, data, strlen ( data ) );
366 len = dhcp_field_len ( data, max_len );
367 dest = ( options->data + options->len - 1 );
368 memcpy ( dest, data, len );
370 end = ( dest + len );
376 * Parse DHCP packet and construct DHCP options block
378 * @v dhcphdr DHCP packet
379 * @v len Length of DHCP packet
380 * @ret options DHCP options block, or NULL
382 * Parses a received DHCP packet and canonicalises its contents into a
383 * single DHCP options block. The "file" and "sname" fields are
384 * converted into the corresponding DHCP options (@c
385 * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
386 * these fields are used for option overloading, their options are
387 * merged in to the options block.
389 * The values of the "yiaddr" and "siaddr" fields will be stored
390 * within the options block as the magic options @c DHCP_EB_YIADDR and
393 * Note that this call allocates new memory for the constructed DHCP
394 * options block; it is the responsibility of the caller to eventually
397 static struct dhcp_option_block * dhcp_parse ( const struct dhcphdr *dhcphdr,
399 struct dhcp_option_block *options;
401 unsigned int overloading;
404 if ( len < sizeof ( *dhcphdr ) )
407 /* Calculate size of resulting concatenated option block:
409 * The "options" field : length of the field minus the DHCP_END tag.
411 * The "file" field : maximum length of the field minus the
412 * NUL terminator, plus a 2-byte DHCP header or, if used for
413 * option overloading, the length of the field minus the
416 * The "sname" field : as for the "file" field.
418 * 15 bytes for an encapsulated options field to contain the
419 * value of the "yiaddr" and "siaddr" fields
421 * 1 byte for a final terminating DHCP_END tag.
423 options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
424 + ( sizeof ( dhcphdr->file ) + 1 )
425 + ( sizeof ( dhcphdr->sname ) + 1 )
426 + 15 /* yiaddr and siaddr */
427 + 1 /* DHCP_END tag */ );
429 /* Allocate empty options block of required size */
430 options = alloc_dhcp_options ( options_len );
432 DBG ( "DHCP could not allocate %d-byte option block\n",
437 /* Merge in "options" field, if this is a DHCP packet */
438 if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
439 merge_dhcp_field ( options, dhcphdr->options,
441 offsetof ( typeof (*dhcphdr), options ) ),
442 0 /* Always contains options */ );
445 /* Identify overloaded fields */
446 overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
448 /* Merge in "file" and "sname" fields */
449 merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
450 ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
451 0 : DHCP_BOOTFILE_NAME ) );
452 merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
453 ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
454 0 : DHCP_TFTP_SERVER_NAME ) );
456 /* Set magic options for "yiaddr" and "siaddr", if present */
457 if ( dhcphdr->yiaddr.s_addr ) {
458 set_dhcp_option ( options, DHCP_EB_YIADDR,
459 &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
461 if ( dhcphdr->siaddr.s_addr ) {
462 set_dhcp_option ( options, DHCP_EB_SIADDR,
463 &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
466 assert ( options->len <= options->max_len );
471 /****************************************************************************
473 * DHCP to UDP interface
477 /** A DHCP session */
478 struct dhcp_session {
479 /** Reference counter */
480 struct refcnt refcnt;
481 /** Job control interface */
482 struct job_interface job;
483 /** Data transfer interface */
484 struct xfer_interface xfer;
486 /** Network device being configured */
487 struct net_device *netdev;
488 /** Option block registration routine */
489 int ( * register_options ) ( struct net_device *netdev,
490 struct dhcp_option_block *options );
492 /** State of the session
494 * This is a value for the @c DHCP_MESSAGE_TYPE option
495 * (e.g. @c DHCPDISCOVER).
498 /** Options obtained from server */
499 struct dhcp_option_block *options;
500 /** Retransmission timer */
501 struct retry_timer timer;
507 * @v refcnt Reference counter
509 static void dhcp_free ( struct refcnt *refcnt ) {
510 struct dhcp_session *dhcp =
511 container_of ( refcnt, struct dhcp_session, refcnt );
513 netdev_put ( dhcp->netdev );
514 dhcpopt_put ( dhcp->options );
519 * Mark DHCP session as complete
521 * @v dhcp DHCP session
522 * @v rc Return status code
524 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
526 /* Block futher incoming messages */
527 job_nullify ( &dhcp->job );
528 xfer_nullify ( &dhcp->xfer );
530 /* Stop retry timer */
531 stop_timer ( &dhcp->timer );
533 /* Free resources and close interfaces */
534 xfer_close ( &dhcp->xfer, rc );
535 job_done ( &dhcp->job, rc );
538 /****************************************************************************
540 * Data transfer interface
545 * Transmit DHCP request
547 * @v dhcp DHCP session
548 * @ret rc Return status code
550 static int dhcp_send_request ( struct dhcp_session *dhcp ) {
551 struct xfer_metadata meta = {
552 .netdev = dhcp->netdev,
554 struct dhcp_packet dhcppkt;
555 struct io_buffer *iobuf;
558 DBGC ( dhcp, "DHCP %p transmitting %s\n",
559 dhcp, dhcp_msgtype_name ( dhcp->state ) );
561 assert ( ( dhcp->state == DHCPDISCOVER ) ||
562 ( dhcp->state == DHCPREQUEST ) );
564 /* Start retry timer. Do this first so that failures to
565 * transmit will be retried.
567 start_timer ( &dhcp->timer );
569 /* Allocate buffer for packet */
570 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
574 /* Create DHCP packet in temporary buffer */
575 if ( ( rc = create_dhcp_packet ( dhcp->netdev, dhcp->state,
576 iobuf->data, iob_tailroom ( iobuf ),
577 &dhcppkt ) ) != 0 ) {
578 DBGC ( dhcp, "DHCP %p could not create DHCP packet: %s\n",
579 dhcp, strerror ( rc ) );
583 /* Copy in options common to all requests */
584 if ( ( rc = copy_dhcp_packet_options ( &dhcppkt,
585 &dhcp_request_options ) ) != 0){
586 DBGC ( dhcp, "DHCP %p could not set common DHCP options: %s\n",
587 dhcp, strerror ( rc ) );
591 /* Copy any required options from previous server repsonse */
592 if ( dhcp->options ) {
593 if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
594 DHCP_SERVER_IDENTIFIER,
595 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
596 DBGC ( dhcp, "DHCP %p could not set server identifier "
597 "option: %s\n", dhcp, strerror ( rc ) );
600 if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
602 DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
603 DBGC ( dhcp, "DHCP %p could not set requested address "
604 "option: %s\n", dhcp, strerror ( rc ) );
609 /* Transmit the packet */
610 iob_put ( iobuf, dhcppkt.len );
611 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
614 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
615 dhcp, strerror ( rc ) );
625 * Handle DHCP retry timer expiry
627 * @v timer DHCP retry timer
628 * @v fail Failure indicator
630 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
631 struct dhcp_session *dhcp =
632 container_of ( timer, struct dhcp_session, timer );
635 dhcp_finished ( dhcp, -ETIMEDOUT );
637 dhcp_send_request ( dhcp );
644 * @v xfer Data transfer interface
645 * @v iobuf I/O buffer
646 * @v data Received data
647 * @v len Length of received data
648 * @ret rc Return status code
650 static int dhcp_deliver_raw ( struct xfer_interface *xfer,
651 const void *data, size_t len ) {
652 struct dhcp_session *dhcp =
653 container_of ( xfer, struct dhcp_session, xfer );
654 const struct dhcphdr *dhcphdr = data;
655 struct dhcp_option_block *options;
656 unsigned int msgtype;
658 /* Check for matching transaction ID */
659 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
660 DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
661 "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
662 ntohl ( dhcp_xid ( dhcp->netdev ) ) );
666 /* Parse packet and create options structure */
667 options = dhcp_parse ( dhcphdr, len );
669 DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
673 /* Determine message type */
674 msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
675 DBGC ( dhcp, "DHCP %p received %s\n",
676 dhcp, dhcp_msgtype_name ( msgtype ) );
678 /* Handle DHCP reply */
679 switch ( dhcp->state ) {
681 if ( msgtype != DHCPOFFER )
683 dhcp->state = DHCPREQUEST;
686 if ( msgtype != DHCPACK )
688 dhcp->state = DHCPACK;
695 /* Stop timer and update stored options */
696 stop_timer ( &dhcp->timer );
698 dhcpopt_put ( dhcp->options );
699 dhcp->options = options;
701 /* Transmit next packet, or terminate session */
702 if ( dhcp->state < DHCPACK ) {
703 dhcp_send_request ( dhcp );
705 dhcp->register_options ( dhcp->netdev, dhcp->options );
706 dhcp_finished ( dhcp, 0 );
711 dhcpopt_put ( options );
715 /** DHCP data transfer interface operations */
716 static struct xfer_interface_operations dhcp_xfer_operations = {
717 .close = ignore_xfer_close,
718 .vredirect = xfer_vopen,
719 .request = ignore_xfer_request,
720 .seek = ignore_xfer_seek,
721 .deliver_iob = xfer_deliver_as_raw,
722 .deliver_raw = dhcp_deliver_raw,
725 /****************************************************************************
727 * Job control interface
732 * Handle kill() event received via job control interface
734 * @v job DHCP job control interface
736 static void dhcp_job_kill ( struct job_interface *job ) {
737 struct dhcp_session *dhcp =
738 container_of ( job, struct dhcp_session, job );
740 /* Terminate DHCP session */
741 dhcp_finished ( dhcp, -ECANCELED );
744 /** DHCP job control interface operations */
745 static struct job_interface_operations dhcp_job_operations = {
746 .done = ignore_job_done,
747 .kill = dhcp_job_kill,
748 .progress = ignore_job_progress,
751 /****************************************************************************
758 * Start DHCP on a network device
760 * @v job Job control interface
761 * @v netdev Network device
762 * @v register_options DHCP option block registration routine
763 * @ret rc Return status code
765 * Starts DHCP on the specified network device. If successful, the @c
766 * register_options() routine will be called with the acquired
769 int start_dhcp ( struct job_interface *job, struct net_device *netdev,
770 int ( * register_options ) ( struct net_device *netdev,
771 struct dhcp_option_block * ) ) {
772 static struct sockaddr_in server = {
773 .sin_family = AF_INET,
774 .sin_addr.s_addr = INADDR_BROADCAST,
775 .sin_port = htons ( BOOTPS_PORT ),
777 static struct sockaddr_in client = {
778 .sin_family = AF_INET,
779 .sin_port = htons ( BOOTPC_PORT ),
781 struct dhcp_session *dhcp;
784 /* Allocate and initialise structure */
785 dhcp = malloc ( sizeof ( *dhcp ) );
788 memset ( dhcp, 0, sizeof ( *dhcp ) );
789 dhcp->refcnt.free = dhcp_free;
790 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
791 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
792 dhcp->netdev = netdev_get ( netdev );
793 dhcp->register_options = register_options;
794 dhcp->timer.expired = dhcp_timer_expired;
795 dhcp->state = DHCPDISCOVER;
797 /* Instantiate child objects and attach to our interfaces */
798 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
799 ( struct sockaddr * ) &server,
800 ( struct sockaddr * ) &client ) ) != 0 )
803 /* Start timer to initiate initial DHCPREQUEST */
804 start_timer ( &dhcp->timer );
806 /* Attach parent interface, mortalise self, and return */
807 job_plug_plug ( &dhcp->job, job );
808 ref_put ( &dhcp->refcnt );
812 dhcp_finished ( dhcp, rc );
813 ref_put ( &dhcp->refcnt );
817 /****************************************************************************
819 * Network device configurator
823 /* Avoid dragging in dns.o */
824 struct sockaddr_tcpip nameserver;
826 /* Avoid dragging in syslog.o */
827 struct in_addr syslogserver;
830 * Configure network device from DHCP options
832 * @v netdev Network device
833 * @v options DHCP options block
834 * @ret rc Return status code
836 int dhcp_configure_netdev ( struct net_device *netdev,
837 struct dhcp_option_block *options ) {
838 struct in_addr address = { 0 };
839 struct in_addr netmask = { 0 };
840 struct in_addr gateway = { INADDR_NONE };
841 struct sockaddr_in *sin_nameserver;
842 struct in_addr tftp_server;
847 /* Clear any existing routing table entry */
848 del_ipv4_address ( netdev );
850 /* Retrieve IP address configuration */
851 find_dhcp_ipv4_option ( options, DHCP_EB_YIADDR, &address );
852 find_dhcp_ipv4_option ( options, DHCP_SUBNET_MASK, &netmask );
853 find_dhcp_ipv4_option ( options, DHCP_ROUTERS, &gateway );
855 /* Set up new IP address configuration */
856 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
858 DBG ( "Could not configure %s with DHCP results: %s\n",
859 netdev->name, strerror ( rc ) );
863 /* Retrieve other DHCP options that we care about */
864 sin_nameserver = ( struct sockaddr_in * ) &nameserver;
865 sin_nameserver->sin_family = AF_INET;
866 find_dhcp_ipv4_option ( options, DHCP_DNS_SERVERS,
867 &sin_nameserver->sin_addr );
868 find_dhcp_ipv4_option ( options, DHCP_LOG_SERVERS,
871 /* Set current working URI based on TFTP server */
872 find_dhcp_ipv4_option ( options, DHCP_EB_SIADDR, &tftp_server );
873 snprintf ( uri_string, sizeof ( uri_string ),
874 "tftp://%s/", inet_ntoa ( tftp_server ) );
875 uri = parse_uri ( uri_string );