2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <gpxe/if_ether.h>
26 #include <gpxe/netdevice.h>
27 #include <gpxe/device.h>
28 #include <gpxe/xfer.h>
29 #include <gpxe/open.h>
31 #include <gpxe/retry.h>
32 #include <gpxe/tcpip.h>
34 #include <gpxe/dhcp.h>
38 * Dynamic Host Configuration Protocol
42 /** DHCP operation types
44 * This table maps from DHCP message types (i.e. values of the @c
45 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
48 static const uint8_t dhcp_op[] = {
49 [DHCPDISCOVER] = BOOTP_REQUEST,
50 [DHCPOFFER] = BOOTP_REPLY,
51 [DHCPREQUEST] = BOOTP_REQUEST,
52 [DHCPDECLINE] = BOOTP_REQUEST,
53 [DHCPACK] = BOOTP_REPLY,
54 [DHCPNAK] = BOOTP_REPLY,
55 [DHCPRELEASE] = BOOTP_REQUEST,
56 [DHCPINFORM] = BOOTP_REQUEST,
59 /** Raw option data for options common to all DHCP requests */
60 static uint8_t dhcp_request_options_data[] = {
61 DHCP_MAX_MESSAGE_SIZE, DHCP_WORD ( ETH_MAX_MTU ),
63 DHCP_STRING ( 'E', 't', 'h', 'e', 'r', 'b', 'o', 'o', 't' ),
64 DHCP_PARAMETER_REQUEST_LIST,
65 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS, DHCP_LOG_SERVERS,
66 DHCP_HOST_NAME, DHCP_DOMAIN_NAME, DHCP_ROOT_PATH,
67 DHCP_VENDOR_ENCAP, DHCP_TFTP_SERVER_NAME,
68 DHCP_BOOTFILE_NAME, DHCP_EB_ENCAP,
69 DHCP_ISCSI_INITIATOR_IQN ),
73 /** DHCP feature codes */
74 static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
75 static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
78 * Name a DHCP packet type
80 * @v msgtype DHCP message type
81 * @ret string DHCP mesasge type name
83 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
85 case 0: return "BOOTP"; /* Non-DHCP packet */
86 case DHCPDISCOVER: return "DHCPDISCOVER";
87 case DHCPOFFER: return "DHCPOFFER";
88 case DHCPREQUEST: return "DHCPREQUEST";
89 case DHCPDECLINE: return "DHCPDECLINE";
90 case DHCPACK: return "DHCPACK";
91 case DHCPNAK: return "DHCPNAK";
92 case DHCPRELEASE: return "DHCPRELEASE";
93 case DHCPINFORM: return "DHCPINFORM";
94 default: return "DHCP<invalid>";
99 * Calculate DHCP transaction ID for a network device
101 * @v netdev Network device
104 * Extract the least significant bits of the hardware address for use
105 * as the transaction ID.
107 static uint32_t dhcp_xid ( struct net_device *netdev ) {
110 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
111 - sizeof ( xid ) ), sizeof ( xid ) );
115 /** Options common to all DHCP requests */
116 static struct dhcp_option_block dhcp_request_options = {
117 .data = dhcp_request_options_data,
118 .max_len = sizeof ( dhcp_request_options_data ),
119 .len = sizeof ( dhcp_request_options_data ),
123 * Set option within DHCP packet
125 * @v dhcppkt DHCP packet
126 * @v tag DHCP option tag
127 * @v data New value for DHCP option
128 * @v len Length of value, in bytes
129 * @ret rc Return status code
131 * Sets the option within the first available options block within the
132 * DHCP packet. Option blocks are tried in the order specified by @c
133 * dhcp_option_block_fill_order.
135 * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
136 * intercepted and inserted into the appropriate fixed fields within
137 * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
138 * ignored, since our DHCP packet assembly method relies on always
139 * having option overloading in use.
141 static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
142 unsigned int tag, const void *data,
144 struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
145 struct dhcp_option_block *options = &dhcppkt->options;
146 struct dhcp_option *option = NULL;
148 /* Special-case the magic options */
150 case DHCP_OPTION_OVERLOAD:
151 /* Hard-coded in packets we create; always ignore */
154 memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
157 memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
159 case DHCP_TFTP_SERVER_NAME:
160 memset ( dhcphdr->sname, 0, sizeof ( dhcphdr->sname ) );
161 if ( len > sizeof ( dhcphdr->sname ) )
162 len = sizeof ( dhcphdr->sname );
163 memcpy ( dhcphdr->sname, data, len );
165 case DHCP_BOOTFILE_NAME:
166 memset ( dhcphdr->file, 0, sizeof ( dhcphdr->file ) );
167 if ( len > sizeof ( dhcphdr->file ) )
168 len = sizeof ( dhcphdr->file );
169 memcpy ( dhcphdr->file, data, len );
172 /* Continue processing as normal */
177 option = set_dhcp_option ( options, tag, data, len );
179 /* Update DHCP packet length */
180 dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
181 + dhcppkt->options.len );
183 return ( option ? 0 : -ENOSPC );
187 * Copy option into DHCP packet
189 * @v dhcppkt DHCP packet
190 * @v options DHCP option block, or NULL
191 * @v tag DHCP option tag to search for
192 * @v new_tag DHCP option tag to use for copied option
193 * @ret rc Return status code
195 * Copies a single option, if present, from the DHCP options block
196 * into a DHCP packet. The tag for the option may be changed if
197 * desired; this is required by other parts of the DHCP code.
199 * @c options may specify a single options block, or be left as NULL
200 * in order to search for the option within all registered options
203 static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
204 struct dhcp_option_block *options,
205 unsigned int tag, unsigned int new_tag ) {
206 struct dhcp_option *option;
209 option = find_dhcp_option ( options, tag );
211 if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
213 option->len ) ) != 0 )
220 * Copy options into DHCP packet
222 * @v dhcppkt DHCP packet
223 * @v options DHCP option block, or NULL
224 * @v encapsulator Encapsulating option, or zero
225 * @ret rc Return status code
227 * Copies options with the specified encapsulator from DHCP options
228 * blocks into a DHCP packet. Most options are copied verbatim.
229 * Recognised encapsulated options fields are handled as such.
231 * @c options may specify a single options block, or be left as NULL
232 * in order to copy options from all registered options blocks.
234 static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
235 struct dhcp_option_block *options,
236 unsigned int encapsulator ) {
241 for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
242 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
245 case DHCP_VENDOR_ENCAP:
246 /* Process encapsulated options field */
247 if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
253 /* Copy option to reassembled packet */
254 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
265 * Copy options into DHCP packet
267 * @v dhcppkt DHCP packet
268 * @v options DHCP option block, or NULL
269 * @ret rc Return status code
271 * Copies options from DHCP options blocks into a DHCP packet. Most
272 * options are copied verbatim. Recognised encapsulated options
273 * fields are handled as such.
275 * @c options may specify a single options block, or be left as NULL
276 * in order to copy options from all registered options blocks.
278 static int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
279 struct dhcp_option_block *options ) {
280 return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
284 * Create a DHCP packet
286 * @v netdev Network device
287 * @v msgtype DHCP message type
288 * @v data Buffer for DHCP packet
289 * @v max_len Size of DHCP packet buffer
290 * @v dhcppkt DHCP packet structure to fill in
291 * @ret rc Return status code
293 * Creates a DHCP packet in the specified buffer, and fills out a @c
294 * dhcp_packet structure that can be passed to
295 * set_dhcp_packet_option() or copy_dhcp_packet_options().
297 static int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
298 void *data, size_t max_len,
299 struct dhcp_packet *dhcppkt ) {
300 struct dhcphdr *dhcphdr = data;
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->op = dhcp_op[msgtype];
314 /* If hardware length exceeds the chaddr field length, don't
315 * use the chaddr field. This is as per RFC4390.
317 hlen = netdev->ll_protocol->ll_addr_len;
318 if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
320 dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
322 dhcphdr->hlen = hlen;
323 memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
325 /* Initialise DHCP packet structure */
326 dhcppkt->dhcphdr = dhcphdr;
327 dhcppkt->max_len = max_len;
328 init_dhcp_options ( &dhcppkt->options, dhcphdr->options,
330 offsetof ( typeof ( *dhcphdr ), options ) ) );
332 /* Set DHCP_MESSAGE_TYPE option */
333 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
335 sizeof ( msgtype ) ) ) != 0 )
342 * Calculate used length of a field containing DHCP options
344 * @v data Field containing DHCP options
345 * @v max_len Field length
346 * @ret len Used length (excluding the @c DHCP_END tag)
348 static size_t dhcp_field_len ( const void *data, size_t max_len ) {
349 struct dhcp_option_block options;
350 struct dhcp_option *end;
352 options.data = ( ( void * ) data );
353 options.len = max_len;
354 end = find_dhcp_option ( &options, DHCP_END );
355 return ( end ? ( ( ( void * ) end ) - data ) : 0 );
359 * Merge field containing DHCP options or string into DHCP options block
361 * @v options DHCP option block
362 * @v data Field containing DHCP options
363 * @v max_len Field length
364 * @v tag DHCP option tag, or 0
366 * If @c tag is non-zero, the field will be treated as a
367 * NUL-terminated string representing the value of the specified DHCP
368 * option. If @c tag is zero, the field will be treated as a block of
369 * DHCP options, and simply appended to the existing options in the
372 * The caller must ensure that there is enough space in the options
373 * block to perform the merge.
375 static void merge_dhcp_field ( struct dhcp_option_block *options,
376 const void *data, size_t max_len,
380 struct dhcp_option *end;
383 set_dhcp_option ( options, tag, data, strlen ( data ) );
385 len = dhcp_field_len ( data, max_len );
386 dest = ( options->data + options->len - 1 );
387 memcpy ( dest, data, len );
389 end = ( dest + len );
395 * Parse DHCP packet and construct DHCP options block
397 * @v dhcphdr DHCP packet
398 * @v len Length of DHCP packet
399 * @ret options DHCP options block, or NULL
401 * Parses a received DHCP packet and canonicalises its contents into a
402 * single DHCP options block. The "file" and "sname" fields are
403 * converted into the corresponding DHCP options (@c
404 * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
405 * these fields are used for option overloading, their options are
406 * merged in to the options block.
408 * The values of the "yiaddr" and "siaddr" fields will be stored
409 * within the options block as the magic options @c DHCP_EB_YIADDR and
412 * Note that this call allocates new memory for the constructed DHCP
413 * options block; it is the responsibility of the caller to eventually
416 static struct dhcp_option_block * dhcp_parse ( const struct dhcphdr *dhcphdr,
418 struct dhcp_option_block *options;
420 unsigned int overloading;
423 if ( len < sizeof ( *dhcphdr ) )
426 /* Calculate size of resulting concatenated option block:
428 * The "options" field : length of the field minus the DHCP_END tag.
430 * The "file" field : maximum length of the field minus the
431 * NUL terminator, plus a 2-byte DHCP header or, if used for
432 * option overloading, the length of the field minus the
435 * The "sname" field : as for the "file" field.
437 * 15 bytes for an encapsulated options field to contain the
438 * value of the "yiaddr" and "siaddr" fields
440 * 1 byte for a final terminating DHCP_END tag.
442 options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
443 + ( sizeof ( dhcphdr->file ) + 1 )
444 + ( sizeof ( dhcphdr->sname ) + 1 )
445 + 15 /* yiaddr and siaddr */
446 + 1 /* DHCP_END tag */ );
448 /* Allocate empty options block of required size */
449 options = alloc_dhcp_options ( options_len );
451 DBG ( "DHCP could not allocate %d-byte option block\n",
456 /* Merge in "options" field, if this is a DHCP packet */
457 if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
458 merge_dhcp_field ( options, dhcphdr->options,
460 offsetof ( typeof (*dhcphdr), options ) ),
461 0 /* Always contains options */ );
464 /* Identify overloaded fields */
465 overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
467 /* Merge in "file" and "sname" fields */
468 merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
469 ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
470 0 : DHCP_BOOTFILE_NAME ) );
471 merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
472 ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
473 0 : DHCP_TFTP_SERVER_NAME ) );
475 /* Set magic options for "yiaddr" and "siaddr", if present */
476 if ( dhcphdr->yiaddr.s_addr ) {
477 set_dhcp_option ( options, DHCP_EB_YIADDR,
478 &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
480 if ( dhcphdr->siaddr.s_addr ) {
481 set_dhcp_option ( options, DHCP_EB_SIADDR,
482 &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
485 assert ( options->len <= options->max_len );
490 /****************************************************************************
492 * Whole-packet construction
496 /** DHCP network device descriptor */
497 struct dhcp_netdev_desc {
504 } __attribute__ (( packed ));
506 /** DHCP client identifier */
507 struct dhcp_client_id {
508 /** Link-layer protocol */
510 /** Link-layer address */
511 uint8_t ll_addr[MAX_LL_ADDR_LEN];
512 } __attribute__ (( packed ));
515 * Create DHCP request
517 * @v netdev Network device
518 * @v msgtype DHCP message type
519 * @v options DHCP server response options, or NULL
520 * @v data Buffer for DHCP packet
521 * @v max_len Size of DHCP packet buffer
522 * @v dhcppkt DHCP packet structure to fill in
523 * @ret rc Return status code
525 int create_dhcp_request ( struct net_device *netdev, int msgtype,
526 struct dhcp_option_block *options,
527 void *data, size_t max_len,
528 struct dhcp_packet *dhcppkt ) {
529 struct device_description *desc = &netdev->dev->desc;
530 struct dhcp_netdev_desc dhcp_desc;
531 struct dhcp_client_id client_id;
532 size_t dhcp_features_len;
536 /* Create DHCP packet */
537 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
539 DBG ( "DHCP could not create DHCP packet: %s\n",
544 /* Copy in options common to all requests */
545 if ( ( rc = copy_dhcp_packet_options ( dhcppkt,
546 &dhcp_request_options )) !=0 ){
547 DBG ( "DHCP could not set common DHCP options: %s\n",
552 /* Copy any required options from previous server repsonse */
554 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
555 DHCP_SERVER_IDENTIFIER,
556 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
557 DBG ( "DHCP could not set server identifier "
558 "option: %s\n", strerror ( rc ) );
561 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
563 DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
564 DBG ( "DHCP could not set requested address "
565 "option: %s\n", strerror ( rc ) );
570 /* Add options to identify the feature list */
571 dhcp_features_len = ( dhcp_features_end - dhcp_features );
572 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_ENCAP,
574 dhcp_features_len ) ) != 0 ) {
575 DBG ( "DHCP could not set features list option: %s\n",
580 /* Add options to identify the network device */
581 dhcp_desc.type = desc->bus_type;
582 dhcp_desc.vendor = htons ( desc->vendor );
583 dhcp_desc.device = htons ( desc->device );
584 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_BUS_ID,
586 sizeof ( dhcp_desc ) ) ) != 0 ) {
587 DBG ( "DHCP could not set bus ID option: %s\n",
592 /* Add DHCP client identifier. Required for Infiniband, and
593 * doesn't hurt other link layers.
595 client_id.ll_proto = netdev->ll_protocol->ll_proto;
596 ll_addr_len = netdev->ll_protocol->ll_addr_len;
597 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
598 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
599 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_CLIENT_ID,
601 ( ll_addr_len + 1 ) ) ) != 0 ) {
602 DBG ( "DHCP could not set client ID: %s\n",
611 * Create DHCP response
613 * @v netdev Network device
614 * @v msgtype DHCP message type
615 * @v options DHCP options, or NULL
616 * @v data Buffer for DHCP packet
617 * @v max_len Size of DHCP packet buffer
618 * @v dhcppkt DHCP packet structure to fill in
619 * @ret rc Return status code
621 int create_dhcp_response ( struct net_device *netdev, int msgtype,
622 struct dhcp_option_block *options,
623 void *data, size_t max_len,
624 struct dhcp_packet *dhcppkt ) {
627 /* Create packet and copy in options */
628 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
630 DBG ( " failed to build packet" );
633 if ( ( rc = copy_dhcp_packet_options ( dhcppkt, options ) ) != 0 ) {
634 DBG ( " failed to copy options" );
641 /****************************************************************************
643 * DHCP to UDP interface
647 /** A DHCP session */
648 struct dhcp_session {
649 /** Reference counter */
650 struct refcnt refcnt;
651 /** Job control interface */
652 struct job_interface job;
653 /** Data transfer interface */
654 struct xfer_interface xfer;
656 /** Network device being configured */
657 struct net_device *netdev;
658 /** Option block registration routine */
659 int ( * register_options ) ( struct net_device *netdev,
660 struct dhcp_option_block *options );
662 /** State of the session
664 * This is a value for the @c DHCP_MESSAGE_TYPE option
665 * (e.g. @c DHCPDISCOVER).
668 /** Options obtained from server */
669 struct dhcp_option_block *options;
670 /** Retransmission timer */
671 struct retry_timer timer;
677 * @v refcnt Reference counter
679 static void dhcp_free ( struct refcnt *refcnt ) {
680 struct dhcp_session *dhcp =
681 container_of ( refcnt, struct dhcp_session, refcnt );
683 netdev_put ( dhcp->netdev );
684 dhcpopt_put ( dhcp->options );
689 * Mark DHCP session as complete
691 * @v dhcp DHCP session
692 * @v rc Return status code
694 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
696 /* Block futher incoming messages */
697 job_nullify ( &dhcp->job );
698 xfer_nullify ( &dhcp->xfer );
700 /* Stop retry timer */
701 stop_timer ( &dhcp->timer );
703 /* Free resources and close interfaces */
704 xfer_close ( &dhcp->xfer, rc );
705 job_done ( &dhcp->job, rc );
708 /****************************************************************************
710 * Data transfer interface
715 * Transmit DHCP request
717 * @v dhcp DHCP session
718 * @ret rc Return status code
720 static int dhcp_send_request ( struct dhcp_session *dhcp ) {
721 struct xfer_metadata meta = {
722 .netdev = dhcp->netdev,
724 struct io_buffer *iobuf;
725 struct dhcp_packet dhcppkt;
728 DBGC ( dhcp, "DHCP %p transmitting %s\n",
729 dhcp, dhcp_msgtype_name ( dhcp->state ) );
731 assert ( ( dhcp->state == DHCPDISCOVER ) ||
732 ( dhcp->state == DHCPREQUEST ) );
734 /* Start retry timer. Do this first so that failures to
735 * transmit will be retried.
737 start_timer ( &dhcp->timer );
739 /* Allocate buffer for packet */
740 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
744 /* Create DHCP packet in temporary buffer */
745 if ( ( rc = create_dhcp_request ( dhcp->netdev, dhcp->state,
746 dhcp->options, iobuf->data,
747 iob_tailroom ( iobuf ),
748 &dhcppkt ) ) != 0 ) {
749 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
750 dhcp, strerror ( rc ) );
754 /* Transmit the packet */
755 iob_put ( iobuf, dhcppkt.len );
756 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
759 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
760 dhcp, strerror ( rc ) );
770 * Handle DHCP retry timer expiry
772 * @v timer DHCP retry timer
773 * @v fail Failure indicator
775 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
776 struct dhcp_session *dhcp =
777 container_of ( timer, struct dhcp_session, timer );
780 dhcp_finished ( dhcp, -ETIMEDOUT );
782 dhcp_send_request ( dhcp );
789 * @v xfer Data transfer interface
790 * @v iobuf I/O buffer
791 * @v data Received data
792 * @v len Length of received data
793 * @ret rc Return status code
795 static int dhcp_deliver_raw ( struct xfer_interface *xfer,
796 const void *data, size_t len ) {
797 struct dhcp_session *dhcp =
798 container_of ( xfer, struct dhcp_session, xfer );
799 const struct dhcphdr *dhcphdr = data;
800 struct dhcp_option_block *options;
801 unsigned int msgtype;
803 /* Check for matching transaction ID */
804 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
805 DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
806 "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
807 ntohl ( dhcp_xid ( dhcp->netdev ) ) );
811 /* Parse packet and create options structure */
812 options = dhcp_parse ( dhcphdr, len );
814 DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
818 /* Determine message type */
819 msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
820 DBGC ( dhcp, "DHCP %p received %s\n",
821 dhcp, dhcp_msgtype_name ( msgtype ) );
823 /* Handle DHCP reply */
824 switch ( dhcp->state ) {
826 if ( msgtype != DHCPOFFER )
828 dhcp->state = DHCPREQUEST;
831 if ( msgtype != DHCPACK )
833 dhcp->state = DHCPACK;
840 /* Stop timer and update stored options */
841 stop_timer ( &dhcp->timer );
843 dhcpopt_put ( dhcp->options );
844 dhcp->options = options;
846 /* Transmit next packet, or terminate session */
847 if ( dhcp->state < DHCPACK ) {
848 dhcp_send_request ( dhcp );
850 dhcp->register_options ( dhcp->netdev, dhcp->options );
851 dhcp_finished ( dhcp, 0 );
856 dhcpopt_put ( options );
860 /** DHCP data transfer interface operations */
861 static struct xfer_interface_operations dhcp_xfer_operations = {
862 .close = ignore_xfer_close,
863 .vredirect = xfer_vopen,
864 .seek = ignore_xfer_seek,
865 .window = unlimited_xfer_window,
866 .deliver_iob = xfer_deliver_as_raw,
867 .deliver_raw = dhcp_deliver_raw,
870 /****************************************************************************
872 * Job control interface
877 * Handle kill() event received via job control interface
879 * @v job DHCP job control interface
881 static void dhcp_job_kill ( struct job_interface *job ) {
882 struct dhcp_session *dhcp =
883 container_of ( job, struct dhcp_session, job );
885 /* Terminate DHCP session */
886 dhcp_finished ( dhcp, -ECANCELED );
889 /** DHCP job control interface operations */
890 static struct job_interface_operations dhcp_job_operations = {
891 .done = ignore_job_done,
892 .kill = dhcp_job_kill,
893 .progress = ignore_job_progress,
896 /****************************************************************************
903 * Start DHCP on a network device
905 * @v job Job control interface
906 * @v netdev Network device
907 * @v register_options DHCP option block registration routine
908 * @ret rc Return status code
910 * Starts DHCP on the specified network device. If successful, the @c
911 * register_options() routine will be called with the acquired
914 int start_dhcp ( struct job_interface *job, struct net_device *netdev,
915 int ( * register_options ) ( struct net_device *netdev,
916 struct dhcp_option_block * ) ) {
917 static struct sockaddr_in server = {
918 .sin_family = AF_INET,
919 .sin_addr.s_addr = INADDR_BROADCAST,
920 .sin_port = htons ( BOOTPS_PORT ),
922 static struct sockaddr_in client = {
923 .sin_family = AF_INET,
924 .sin_port = htons ( BOOTPC_PORT ),
926 struct dhcp_session *dhcp;
929 /* Allocate and initialise structure */
930 dhcp = zalloc ( sizeof ( *dhcp ) );
933 dhcp->refcnt.free = dhcp_free;
934 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
935 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
936 dhcp->netdev = netdev_get ( netdev );
937 dhcp->register_options = register_options;
938 dhcp->timer.expired = dhcp_timer_expired;
939 dhcp->state = DHCPDISCOVER;
941 /* Instantiate child objects and attach to our interfaces */
942 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
943 ( struct sockaddr * ) &server,
944 ( struct sockaddr * ) &client ) ) != 0 )
947 /* Start timer to initiate initial DHCPREQUEST */
948 start_timer_nodelay ( &dhcp->timer );
950 /* Attach parent interface, mortalise self, and return */
951 job_plug_plug ( &dhcp->job, job );
952 ref_put ( &dhcp->refcnt );
956 dhcp_finished ( dhcp, rc );
957 ref_put ( &dhcp->refcnt );
961 /****************************************************************************
963 * Network device configurator
968 * Configure network device from DHCP options
970 * @v netdev Network device
971 * @v options DHCP options block
972 * @ret rc Return status code
974 int dhcp_configure_netdev ( struct net_device *netdev,
975 struct dhcp_option_block *options ) {
976 struct in_addr address = { 0 };
977 struct in_addr netmask = { 0 };
978 struct in_addr gateway = { INADDR_NONE };
981 /* Clear any existing routing table entry */
982 del_ipv4_address ( netdev );
984 /* Retrieve IP address configuration */
985 find_dhcp_ipv4_option ( options, DHCP_EB_YIADDR, &address );
986 find_dhcp_ipv4_option ( options, DHCP_SUBNET_MASK, &netmask );
987 find_dhcp_ipv4_option ( options, DHCP_ROUTERS, &gateway );
989 /* Set up new IP address configuration */
990 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
992 DBG ( "Could not configure %s with DHCP results: %s\n",
993 netdev->name, strerror ( rc ) );