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 ),
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 static 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 memset ( dhcphdr->sname, 0, sizeof ( dhcphdr->sname ) );
157 if ( len > sizeof ( dhcphdr->sname ) )
158 len = sizeof ( dhcphdr->sname );
159 memcpy ( dhcphdr->sname, data, len );
161 case DHCP_BOOTFILE_NAME:
162 memset ( dhcphdr->file, 0, sizeof ( dhcphdr->file ) );
163 if ( len > sizeof ( dhcphdr->file ) )
164 len = sizeof ( dhcphdr->file );
165 memcpy ( dhcphdr->file, data, len );
168 /* Continue processing as normal */
173 option = set_dhcp_option ( options, tag, data, len );
175 /* Update DHCP packet length */
176 dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
177 + dhcppkt->options.len );
179 return ( option ? 0 : -ENOSPC );
183 * Copy option into DHCP packet
185 * @v dhcppkt DHCP packet
186 * @v options DHCP option block, or NULL
187 * @v tag DHCP option tag to search for
188 * @v new_tag DHCP option tag to use for copied option
189 * @ret rc Return status code
191 * Copies a single option, if present, from the DHCP options block
192 * into a DHCP packet. The tag for the option may be changed if
193 * desired; this is required by other parts of the DHCP code.
195 * @c options may specify a single options block, or be left as NULL
196 * in order to search for the option within all registered options
199 static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
200 struct dhcp_option_block *options,
201 unsigned int tag, unsigned int new_tag ) {
202 struct dhcp_option *option;
205 option = find_dhcp_option ( options, tag );
207 if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
209 option->len ) ) != 0 )
216 * Copy options into DHCP packet
218 * @v dhcppkt DHCP packet
219 * @v options DHCP option block, or NULL
220 * @v encapsulator Encapsulating option, or zero
221 * @ret rc Return status code
223 * Copies options with the specified encapsulator from DHCP options
224 * blocks into a DHCP packet. Most options are copied verbatim.
225 * Recognised encapsulated options fields are handled as such.
227 * @c options may specify a single options block, or be left as NULL
228 * in order to copy options from all registered options blocks.
230 static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
231 struct dhcp_option_block *options,
232 unsigned int encapsulator ) {
237 for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
238 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
241 case DHCP_VENDOR_ENCAP:
242 /* Process encapsulated options field */
243 if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
249 /* Copy option to reassembled packet */
250 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
261 * Copy options into DHCP packet
263 * @v dhcppkt DHCP packet
264 * @v options DHCP option block, or NULL
265 * @ret rc Return status code
267 * Copies options from DHCP options blocks into a DHCP packet. Most
268 * options are copied verbatim. Recognised encapsulated options
269 * fields are handled as such.
271 * @c options may specify a single options block, or be left as NULL
272 * in order to copy options from all registered options blocks.
274 static int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
275 struct dhcp_option_block *options ) {
276 return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
280 * Create a DHCP packet
282 * @v netdev Network device
283 * @v msgtype DHCP message type
284 * @v data Buffer for DHCP packet
285 * @v max_len Size of DHCP packet buffer
286 * @v dhcppkt DHCP packet structure to fill in
287 * @ret rc Return status code
289 * Creates a DHCP packet in the specified buffer, and fills out a @c
290 * dhcp_packet structure that can be passed to
291 * set_dhcp_packet_option() or copy_dhcp_packet_options().
293 static int create_dhcp_packet ( struct net_device *netdev,
294 unsigned int msgtype,
295 void *data, size_t max_len,
296 struct dhcp_packet *dhcppkt ) {
297 struct dhcphdr *dhcphdr = data;
301 if ( max_len < sizeof ( *dhcphdr ) )
304 /* Initialise DHCP packet content */
305 memset ( dhcphdr, 0, max_len );
306 dhcphdr->xid = dhcp_xid ( netdev );
307 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
308 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
309 dhcphdr->hlen = netdev->ll_protocol->ll_addr_len;
310 memcpy ( dhcphdr->chaddr, netdev->ll_addr, dhcphdr->hlen );
311 dhcphdr->op = dhcp_op[msgtype];
313 /* Initialise DHCP packet structure */
314 dhcppkt->dhcphdr = dhcphdr;
315 dhcppkt->max_len = max_len;
316 init_dhcp_options ( &dhcppkt->options, dhcphdr->options,
318 offsetof ( typeof ( *dhcphdr ), options ) ) );
320 /* Set DHCP_MESSAGE_TYPE option */
321 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
323 sizeof ( msgtype ) ) ) != 0 )
330 * Calculate used length of a field containing DHCP options
332 * @v data Field containing DHCP options
333 * @v max_len Field length
334 * @ret len Used length (excluding the @c DHCP_END tag)
336 static size_t dhcp_field_len ( const void *data, size_t max_len ) {
337 struct dhcp_option_block options;
338 struct dhcp_option *end;
340 options.data = ( ( void * ) data );
341 options.len = max_len;
342 end = find_dhcp_option ( &options, DHCP_END );
343 return ( end ? ( ( ( void * ) end ) - data ) : 0 );
347 * Merge field containing DHCP options or string into DHCP options block
349 * @v options DHCP option block
350 * @v data Field containing DHCP options
351 * @v max_len Field length
352 * @v tag DHCP option tag, or 0
354 * If @c tag is non-zero, the field will be treated as a
355 * NUL-terminated string representing the value of the specified DHCP
356 * option. If @c tag is zero, the field will be treated as a block of
357 * DHCP options, and simply appended to the existing options in the
360 * The caller must ensure that there is enough space in the options
361 * block to perform the merge.
363 static void merge_dhcp_field ( struct dhcp_option_block *options,
364 const void *data, size_t max_len,
368 struct dhcp_option *end;
371 set_dhcp_option ( options, tag, data, strlen ( data ) );
373 len = dhcp_field_len ( data, max_len );
374 dest = ( options->data + options->len - 1 );
375 memcpy ( dest, data, len );
377 end = ( dest + len );
383 * Parse DHCP packet and construct DHCP options block
385 * @v dhcphdr DHCP packet
386 * @v len Length of DHCP packet
387 * @ret options DHCP options block, or NULL
389 * Parses a received DHCP packet and canonicalises its contents into a
390 * single DHCP options block. The "file" and "sname" fields are
391 * converted into the corresponding DHCP options (@c
392 * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
393 * these fields are used for option overloading, their options are
394 * merged in to the options block.
396 * The values of the "yiaddr" and "siaddr" fields will be stored
397 * within the options block as the magic options @c DHCP_EB_YIADDR and
400 * Note that this call allocates new memory for the constructed DHCP
401 * options block; it is the responsibility of the caller to eventually
404 static struct dhcp_option_block * dhcp_parse ( const struct dhcphdr *dhcphdr,
406 struct dhcp_option_block *options;
408 unsigned int overloading;
411 if ( len < sizeof ( *dhcphdr ) )
414 /* Calculate size of resulting concatenated option block:
416 * The "options" field : length of the field minus the DHCP_END tag.
418 * The "file" field : maximum length of the field minus the
419 * NUL terminator, plus a 2-byte DHCP header or, if used for
420 * option overloading, the length of the field minus the
423 * The "sname" field : as for the "file" field.
425 * 15 bytes for an encapsulated options field to contain the
426 * value of the "yiaddr" and "siaddr" fields
428 * 1 byte for a final terminating DHCP_END tag.
430 options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
431 + ( sizeof ( dhcphdr->file ) + 1 )
432 + ( sizeof ( dhcphdr->sname ) + 1 )
433 + 15 /* yiaddr and siaddr */
434 + 1 /* DHCP_END tag */ );
436 /* Allocate empty options block of required size */
437 options = alloc_dhcp_options ( options_len );
439 DBG ( "DHCP could not allocate %d-byte option block\n",
444 /* Merge in "options" field, if this is a DHCP packet */
445 if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
446 merge_dhcp_field ( options, dhcphdr->options,
448 offsetof ( typeof (*dhcphdr), options ) ),
449 0 /* Always contains options */ );
452 /* Identify overloaded fields */
453 overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
455 /* Merge in "file" and "sname" fields */
456 merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
457 ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
458 0 : DHCP_BOOTFILE_NAME ) );
459 merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
460 ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
461 0 : DHCP_TFTP_SERVER_NAME ) );
463 /* Set magic options for "yiaddr" and "siaddr", if present */
464 if ( dhcphdr->yiaddr.s_addr ) {
465 set_dhcp_option ( options, DHCP_EB_YIADDR,
466 &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
468 if ( dhcphdr->siaddr.s_addr ) {
469 set_dhcp_option ( options, DHCP_EB_SIADDR,
470 &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
473 assert ( options->len <= options->max_len );
478 /****************************************************************************
480 * Whole-packet construction
484 /** DHCP network device descriptor */
485 struct dhcp_netdev_desc {
492 } __attribute__ (( packed ));
495 * Create DHCP request
497 * @v netdev Network device
498 * @v msgtype DHCP message type
499 * @v options DHCP server response options, or NULL
500 * @v data Buffer for DHCP packet
501 * @v max_len Size of DHCP packet buffer
502 * @v dhcppkt DHCP packet structure to fill in
503 * @ret rc Return status code
505 int create_dhcp_request ( struct net_device *netdev, int msgtype,
506 struct dhcp_option_block *options,
507 void *data, size_t max_len,
508 struct dhcp_packet *dhcppkt ) {
509 struct device_description *desc = &netdev->dev->desc;
510 struct dhcp_netdev_desc dhcp_desc;
513 /* Create DHCP packet */
514 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
516 DBG ( "DHCP could not create DHCP packet: %s\n",
521 /* Copy in options common to all requests */
522 if ( ( rc = copy_dhcp_packet_options ( dhcppkt,
523 &dhcp_request_options )) !=0 ){
524 DBG ( "DHCP could not set common DHCP options: %s\n",
529 /* Copy any required options from previous server repsonse */
531 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
532 DHCP_SERVER_IDENTIFIER,
533 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
534 DBG ( "DHCP could not set server identifier "
535 "option: %s\n", strerror ( rc ) );
538 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
540 DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
541 DBG ( "DHCP could not set requested address "
542 "option: %s\n", strerror ( rc ) );
547 /* Add options to identify the network device */
548 dhcp_desc.type = desc->bus_type;
549 dhcp_desc.vendor = htons ( desc->vendor );
550 dhcp_desc.device = htons ( desc->device );
551 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_BUS_ID,
553 sizeof ( dhcp_desc ) ) ) != 0 ) {
554 DBG ( "DHCP could not set bus ID option: %s\n",
563 * Create DHCP response
565 * @v netdev Network device
566 * @v msgtype DHCP message type
567 * @v options DHCP options, or NULL
568 * @v data Buffer for DHCP packet
569 * @v max_len Size of DHCP packet buffer
570 * @v dhcppkt DHCP packet structure to fill in
571 * @ret rc Return status code
573 int create_dhcp_response ( struct net_device *netdev, int msgtype,
574 struct dhcp_option_block *options,
575 void *data, size_t max_len,
576 struct dhcp_packet *dhcppkt ) {
579 /* Create packet and copy in options */
580 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
582 DBG ( " failed to build packet" );
585 if ( ( rc = copy_dhcp_packet_options ( dhcppkt, options ) ) != 0 ) {
586 DBG ( " failed to copy options" );
593 /****************************************************************************
595 * DHCP to UDP interface
599 /** A DHCP session */
600 struct dhcp_session {
601 /** Reference counter */
602 struct refcnt refcnt;
603 /** Job control interface */
604 struct job_interface job;
605 /** Data transfer interface */
606 struct xfer_interface xfer;
608 /** Network device being configured */
609 struct net_device *netdev;
610 /** Option block registration routine */
611 int ( * register_options ) ( struct net_device *netdev,
612 struct dhcp_option_block *options );
614 /** State of the session
616 * This is a value for the @c DHCP_MESSAGE_TYPE option
617 * (e.g. @c DHCPDISCOVER).
620 /** Options obtained from server */
621 struct dhcp_option_block *options;
622 /** Retransmission timer */
623 struct retry_timer timer;
629 * @v refcnt Reference counter
631 static void dhcp_free ( struct refcnt *refcnt ) {
632 struct dhcp_session *dhcp =
633 container_of ( refcnt, struct dhcp_session, refcnt );
635 netdev_put ( dhcp->netdev );
636 dhcpopt_put ( dhcp->options );
641 * Mark DHCP session as complete
643 * @v dhcp DHCP session
644 * @v rc Return status code
646 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
648 /* Block futher incoming messages */
649 job_nullify ( &dhcp->job );
650 xfer_nullify ( &dhcp->xfer );
652 /* Stop retry timer */
653 stop_timer ( &dhcp->timer );
655 /* Free resources and close interfaces */
656 xfer_close ( &dhcp->xfer, rc );
657 job_done ( &dhcp->job, rc );
660 /****************************************************************************
662 * Data transfer interface
667 * Transmit DHCP request
669 * @v dhcp DHCP session
670 * @ret rc Return status code
672 static int dhcp_send_request ( struct dhcp_session *dhcp ) {
673 struct xfer_metadata meta = {
674 .netdev = dhcp->netdev,
676 struct io_buffer *iobuf;
677 struct dhcp_packet dhcppkt;
680 DBGC ( dhcp, "DHCP %p transmitting %s\n",
681 dhcp, dhcp_msgtype_name ( dhcp->state ) );
683 assert ( ( dhcp->state == DHCPDISCOVER ) ||
684 ( dhcp->state == DHCPREQUEST ) );
686 /* Start retry timer. Do this first so that failures to
687 * transmit will be retried.
689 start_timer ( &dhcp->timer );
691 /* Allocate buffer for packet */
692 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
696 /* Create DHCP packet in temporary buffer */
697 if ( ( rc = create_dhcp_request ( dhcp->netdev, dhcp->state,
698 dhcp->options, iobuf->data,
699 iob_tailroom ( iobuf ),
700 &dhcppkt ) ) != 0 ) {
701 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
702 dhcp, strerror ( rc ) );
706 /* Transmit the packet */
707 iob_put ( iobuf, dhcppkt.len );
708 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
711 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
712 dhcp, strerror ( rc ) );
722 * Handle DHCP retry timer expiry
724 * @v timer DHCP retry timer
725 * @v fail Failure indicator
727 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
728 struct dhcp_session *dhcp =
729 container_of ( timer, struct dhcp_session, timer );
732 dhcp_finished ( dhcp, -ETIMEDOUT );
734 dhcp_send_request ( dhcp );
741 * @v xfer Data transfer interface
742 * @v iobuf I/O buffer
743 * @v data Received data
744 * @v len Length of received data
745 * @ret rc Return status code
747 static int dhcp_deliver_raw ( struct xfer_interface *xfer,
748 const void *data, size_t len ) {
749 struct dhcp_session *dhcp =
750 container_of ( xfer, struct dhcp_session, xfer );
751 const struct dhcphdr *dhcphdr = data;
752 struct dhcp_option_block *options;
753 unsigned int msgtype;
755 /* Check for matching transaction ID */
756 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
757 DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
758 "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
759 ntohl ( dhcp_xid ( dhcp->netdev ) ) );
763 /* Parse packet and create options structure */
764 options = dhcp_parse ( dhcphdr, len );
766 DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
770 /* Determine message type */
771 msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
772 DBGC ( dhcp, "DHCP %p received %s\n",
773 dhcp, dhcp_msgtype_name ( msgtype ) );
775 /* Handle DHCP reply */
776 switch ( dhcp->state ) {
778 if ( msgtype != DHCPOFFER )
780 dhcp->state = DHCPREQUEST;
783 if ( msgtype != DHCPACK )
785 dhcp->state = DHCPACK;
792 /* Stop timer and update stored options */
793 stop_timer ( &dhcp->timer );
795 dhcpopt_put ( dhcp->options );
796 dhcp->options = options;
798 /* Transmit next packet, or terminate session */
799 if ( dhcp->state < DHCPACK ) {
800 dhcp_send_request ( dhcp );
802 dhcp->register_options ( dhcp->netdev, dhcp->options );
803 dhcp_finished ( dhcp, 0 );
808 dhcpopt_put ( options );
812 /** DHCP data transfer interface operations */
813 static struct xfer_interface_operations dhcp_xfer_operations = {
814 .close = ignore_xfer_close,
815 .vredirect = xfer_vopen,
816 .seek = ignore_xfer_seek,
817 .window = unlimited_xfer_window,
818 .deliver_iob = xfer_deliver_as_raw,
819 .deliver_raw = dhcp_deliver_raw,
822 /****************************************************************************
824 * Job control interface
829 * Handle kill() event received via job control interface
831 * @v job DHCP job control interface
833 static void dhcp_job_kill ( struct job_interface *job ) {
834 struct dhcp_session *dhcp =
835 container_of ( job, struct dhcp_session, job );
837 /* Terminate DHCP session */
838 dhcp_finished ( dhcp, -ECANCELED );
841 /** DHCP job control interface operations */
842 static struct job_interface_operations dhcp_job_operations = {
843 .done = ignore_job_done,
844 .kill = dhcp_job_kill,
845 .progress = ignore_job_progress,
848 /****************************************************************************
855 * Start DHCP on a network device
857 * @v job Job control interface
858 * @v netdev Network device
859 * @v register_options DHCP option block registration routine
860 * @ret rc Return status code
862 * Starts DHCP on the specified network device. If successful, the @c
863 * register_options() routine will be called with the acquired
866 int start_dhcp ( struct job_interface *job, struct net_device *netdev,
867 int ( * register_options ) ( struct net_device *netdev,
868 struct dhcp_option_block * ) ) {
869 static struct sockaddr_in server = {
870 .sin_family = AF_INET,
871 .sin_addr.s_addr = INADDR_BROADCAST,
872 .sin_port = htons ( BOOTPS_PORT ),
874 static struct sockaddr_in client = {
875 .sin_family = AF_INET,
876 .sin_port = htons ( BOOTPC_PORT ),
878 struct dhcp_session *dhcp;
881 /* Allocate and initialise structure */
882 dhcp = zalloc ( sizeof ( *dhcp ) );
885 dhcp->refcnt.free = dhcp_free;
886 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
887 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
888 dhcp->netdev = netdev_get ( netdev );
889 dhcp->register_options = register_options;
890 dhcp->timer.expired = dhcp_timer_expired;
891 dhcp->state = DHCPDISCOVER;
893 /* Instantiate child objects and attach to our interfaces */
894 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
895 ( struct sockaddr * ) &server,
896 ( struct sockaddr * ) &client ) ) != 0 )
899 /* Start timer to initiate initial DHCPREQUEST */
900 start_timer ( &dhcp->timer );
902 /* Attach parent interface, mortalise self, and return */
903 job_plug_plug ( &dhcp->job, job );
904 ref_put ( &dhcp->refcnt );
908 dhcp_finished ( dhcp, rc );
909 ref_put ( &dhcp->refcnt );
913 /****************************************************************************
915 * Network device configurator
920 * Configure network device from DHCP options
922 * @v netdev Network device
923 * @v options DHCP options block
924 * @ret rc Return status code
926 int dhcp_configure_netdev ( struct net_device *netdev,
927 struct dhcp_option_block *options ) {
928 struct in_addr address = { 0 };
929 struct in_addr netmask = { 0 };
930 struct in_addr gateway = { INADDR_NONE };
933 /* Clear any existing routing table entry */
934 del_ipv4_address ( netdev );
936 /* Retrieve IP address configuration */
937 find_dhcp_ipv4_option ( options, DHCP_EB_YIADDR, &address );
938 find_dhcp_ipv4_option ( options, DHCP_SUBNET_MASK, &netmask );
939 find_dhcp_ipv4_option ( options, DHCP_ROUTERS, &gateway );
941 /* Set up new IP address configuration */
942 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
944 DBG ( "Could not configure %s with DHCP results: %s\n",
945 netdev->name, strerror ( rc ) );