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/uuid.h>
35 #include <gpxe/dhcp.h>
39 * Dynamic Host Configuration Protocol
43 /** DHCP operation types
45 * This table maps from DHCP message types (i.e. values of the @c
46 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
49 static const uint8_t dhcp_op[] = {
50 [DHCPDISCOVER] = BOOTP_REQUEST,
51 [DHCPOFFER] = BOOTP_REPLY,
52 [DHCPREQUEST] = BOOTP_REQUEST,
53 [DHCPDECLINE] = BOOTP_REQUEST,
54 [DHCPACK] = BOOTP_REPLY,
55 [DHCPNAK] = BOOTP_REPLY,
56 [DHCPRELEASE] = BOOTP_REQUEST,
57 [DHCPINFORM] = BOOTP_REQUEST,
60 /** Raw option data for options common to all DHCP requests */
61 static uint8_t dhcp_request_options_data[] = {
62 DHCP_MAX_MESSAGE_SIZE, DHCP_WORD ( ETH_MAX_MTU ),
64 DHCP_STRING ( 'P', 'X', 'E', 'C', 'l', 'i', 'e', 'n', 't', ':',
65 'A', 'r', 'c', 'h', ':', '0', '0', '0', '0', '0', ':',
66 'U', 'N', 'D', 'I', ':', '0', '0', '2', '0', '0', '1' ),
67 DHCP_CLIENT_ARCHITECTURE, DHCP_WORD ( 0 ),
68 DHCP_CLIENT_NDI, DHCP_OPTION ( 1 /* UNDI */ , 2, 1 /* v2.1 */ ),
69 DHCP_PARAMETER_REQUEST_LIST,
70 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
71 DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
72 DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
73 DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
74 DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
78 /** DHCP feature codes */
79 static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
80 static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
83 * Name a DHCP packet type
85 * @v msgtype DHCP message type
86 * @ret string DHCP mesasge type name
88 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
90 case 0: return "BOOTP"; /* Non-DHCP packet */
91 case DHCPDISCOVER: return "DHCPDISCOVER";
92 case DHCPOFFER: return "DHCPOFFER";
93 case DHCPREQUEST: return "DHCPREQUEST";
94 case DHCPDECLINE: return "DHCPDECLINE";
95 case DHCPACK: return "DHCPACK";
96 case DHCPNAK: return "DHCPNAK";
97 case DHCPRELEASE: return "DHCPRELEASE";
98 case DHCPINFORM: return "DHCPINFORM";
99 default: return "DHCP<invalid>";
104 * Calculate DHCP transaction ID for a network device
106 * @v netdev Network device
109 * Extract the least significant bits of the hardware address for use
110 * as the transaction ID.
112 static uint32_t dhcp_xid ( struct net_device *netdev ) {
115 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
116 - sizeof ( xid ) ), sizeof ( xid ) );
120 /** Options common to all DHCP requests */
121 static struct dhcp_option_block dhcp_request_options = {
122 .data = dhcp_request_options_data,
123 .max_len = sizeof ( dhcp_request_options_data ),
124 .len = sizeof ( dhcp_request_options_data ),
128 * Set option within DHCP packet
130 * @v dhcppkt DHCP packet
131 * @v tag DHCP option tag
132 * @v data New value for DHCP option
133 * @v len Length of value, in bytes
134 * @ret rc Return status code
136 * Sets the option within the first available options block within the
137 * DHCP packet. Option blocks are tried in the order specified by @c
138 * dhcp_option_block_fill_order.
140 * The magic options @c DHCP_EB_YIADDR and @c DHCP_EB_SIADDR are
141 * intercepted and inserted into the appropriate fixed fields within
142 * the DHCP packet. The option @c DHCP_OPTION_OVERLOAD is silently
143 * ignored, since our DHCP packet assembly method relies on always
144 * having option overloading in use.
146 static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
147 unsigned int tag, const void *data,
149 struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
150 struct dhcp_option_block *options = &dhcppkt->options;
151 struct dhcp_option *option = NULL;
153 /* Special-case the magic options */
155 case DHCP_OPTION_OVERLOAD:
156 /* Hard-coded in packets we create; always ignore */
159 memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
162 memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
164 case DHCP_TFTP_SERVER_NAME:
165 memset ( dhcphdr->sname, 0, sizeof ( dhcphdr->sname ) );
166 if ( len > sizeof ( dhcphdr->sname ) )
167 len = sizeof ( dhcphdr->sname );
168 memcpy ( dhcphdr->sname, data, len );
170 case DHCP_BOOTFILE_NAME:
171 memset ( dhcphdr->file, 0, sizeof ( dhcphdr->file ) );
172 if ( len > sizeof ( dhcphdr->file ) )
173 len = sizeof ( dhcphdr->file );
174 memcpy ( dhcphdr->file, data, len );
177 /* Continue processing as normal */
182 option = set_dhcp_option ( options, tag, data, len );
184 /* Update DHCP packet length */
185 dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
186 + dhcppkt->options.len );
188 return ( option ? 0 : -ENOSPC );
192 * Copy option into DHCP packet
194 * @v dhcppkt DHCP packet
195 * @v options DHCP option block, or NULL
196 * @v tag DHCP option tag to search for
197 * @v new_tag DHCP option tag to use for copied option
198 * @ret rc Return status code
200 * Copies a single option, if present, from the DHCP options block
201 * into a DHCP packet. The tag for the option may be changed if
202 * desired; this is required by other parts of the DHCP code.
204 * @c options may specify a single options block, or be left as NULL
205 * in order to search for the option within all registered options
208 static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
209 struct dhcp_option_block *options,
210 unsigned int tag, unsigned int new_tag ) {
211 struct dhcp_option *option;
214 option = find_dhcp_option ( options, tag );
216 if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
218 option->len ) ) != 0 )
225 * Copy options into DHCP packet
227 * @v dhcppkt DHCP packet
228 * @v options DHCP option block, or NULL
229 * @v encapsulator Encapsulating option, or zero
230 * @ret rc Return status code
232 * Copies options with the specified encapsulator from DHCP options
233 * blocks into a DHCP packet. Most options are copied verbatim.
234 * Recognised encapsulated options fields are handled as such.
236 * @c options may specify a single options block, or be left as NULL
237 * in order to copy options from all registered options blocks.
239 static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
240 struct dhcp_option_block *options,
241 unsigned int encapsulator ) {
246 for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
247 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
250 case DHCP_VENDOR_ENCAP:
251 /* Process encapsulated options field */
252 if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
258 /* Copy option to reassembled packet */
259 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
270 * Copy options into DHCP packet
272 * @v dhcppkt DHCP packet
273 * @v options DHCP option block, or NULL
274 * @ret rc Return status code
276 * Copies options from DHCP options blocks into a DHCP packet. Most
277 * options are copied verbatim. Recognised encapsulated options
278 * fields are handled as such.
280 * @c options may specify a single options block, or be left as NULL
281 * in order to copy options from all registered options blocks.
283 static int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
284 struct dhcp_option_block *options ) {
285 return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
289 * Create a DHCP packet
291 * @v netdev Network device
292 * @v msgtype DHCP message type
293 * @v data Buffer for DHCP packet
294 * @v max_len Size of DHCP packet buffer
295 * @v dhcppkt DHCP packet structure to fill in
296 * @ret rc Return status code
298 * Creates a DHCP packet in the specified buffer, and fills out a @c
299 * dhcp_packet structure that can be passed to
300 * set_dhcp_packet_option() or copy_dhcp_packet_options().
302 static int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
303 void *data, size_t max_len,
304 struct dhcp_packet *dhcppkt ) {
305 struct dhcphdr *dhcphdr = data;
310 if ( max_len < sizeof ( *dhcphdr ) )
313 /* Initialise DHCP packet content */
314 memset ( dhcphdr, 0, max_len );
315 dhcphdr->xid = dhcp_xid ( netdev );
316 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
317 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
318 dhcphdr->op = dhcp_op[msgtype];
319 /* If hardware length exceeds the chaddr field length, don't
320 * use the chaddr field. This is as per RFC4390.
322 hlen = netdev->ll_protocol->ll_addr_len;
323 if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
325 dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
327 dhcphdr->hlen = hlen;
328 memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
330 /* Initialise DHCP packet structure */
331 dhcppkt->dhcphdr = dhcphdr;
332 dhcppkt->max_len = max_len;
333 init_dhcp_options ( &dhcppkt->options, dhcphdr->options,
335 offsetof ( typeof ( *dhcphdr ), options ) ) );
337 /* Set DHCP_MESSAGE_TYPE option */
338 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
340 sizeof ( msgtype ) ) ) != 0 )
347 * Calculate used length of a field containing DHCP options
349 * @v data Field containing DHCP options
350 * @v max_len Field length
351 * @ret len Used length (excluding the @c DHCP_END tag)
353 static size_t dhcp_field_len ( const void *data, size_t max_len ) {
354 struct dhcp_option_block options;
355 struct dhcp_option *end;
357 options.data = ( ( void * ) data );
358 options.len = max_len;
359 end = find_dhcp_option ( &options, DHCP_END );
360 return ( end ? ( ( ( void * ) end ) - data ) : 0 );
364 * Merge field containing DHCP options or string into DHCP options block
366 * @v options DHCP option block
367 * @v data Field containing DHCP options
368 * @v max_len Field length
369 * @v tag DHCP option tag, or 0
371 * If @c tag is non-zero, the field will be treated as a
372 * NUL-terminated string representing the value of the specified DHCP
373 * option. If @c tag is zero, the field will be treated as a block of
374 * DHCP options, and simply appended to the existing options in the
377 * The caller must ensure that there is enough space in the options
378 * block to perform the merge.
380 static void merge_dhcp_field ( struct dhcp_option_block *options,
381 const void *data, size_t max_len,
385 struct dhcp_option *end;
388 set_dhcp_option ( options, tag, data, strlen ( data ) );
390 len = dhcp_field_len ( data, max_len );
391 dest = ( options->data + options->len - 1 );
392 memcpy ( dest, data, len );
394 end = ( dest + len );
400 * Parse DHCP packet and construct DHCP options block
402 * @v dhcphdr DHCP packet
403 * @v len Length of DHCP packet
404 * @ret options DHCP options block, or NULL
406 * Parses a received DHCP packet and canonicalises its contents into a
407 * single DHCP options block. The "file" and "sname" fields are
408 * converted into the corresponding DHCP options (@c
409 * DHCP_BOOTFILE_NAME and @c DHCP_TFTP_SERVER_NAME respectively). If
410 * these fields are used for option overloading, their options are
411 * merged in to the options block.
413 * The values of the "yiaddr" and "siaddr" fields will be stored
414 * within the options block as the magic options @c DHCP_EB_YIADDR and
417 * Note that this call allocates new memory for the constructed DHCP
418 * options block; it is the responsibility of the caller to eventually
421 static struct dhcp_option_block * dhcp_parse ( const struct dhcphdr *dhcphdr,
423 struct dhcp_option_block *options;
425 unsigned int overloading;
428 if ( len < sizeof ( *dhcphdr ) )
431 /* Calculate size of resulting concatenated option block:
433 * The "options" field : length of the field minus the DHCP_END tag.
435 * The "file" field : maximum length of the field minus the
436 * NUL terminator, plus a 2-byte DHCP header or, if used for
437 * option overloading, the length of the field minus the
440 * The "sname" field : as for the "file" field.
442 * 15 bytes for an encapsulated options field to contain the
443 * value of the "yiaddr" and "siaddr" fields
445 * 1 byte for a final terminating DHCP_END tag.
447 options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
448 + ( sizeof ( dhcphdr->file ) + 1 )
449 + ( sizeof ( dhcphdr->sname ) + 1 )
450 + 15 /* yiaddr and siaddr */
451 + 1 /* DHCP_END tag */ );
453 /* Allocate empty options block of required size */
454 options = alloc_dhcp_options ( options_len );
456 DBG ( "DHCP could not allocate %d-byte option block\n",
461 /* Merge in "options" field, if this is a DHCP packet */
462 if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
463 merge_dhcp_field ( options, dhcphdr->options,
465 offsetof ( typeof (*dhcphdr), options ) ),
466 0 /* Always contains options */ );
469 /* Identify overloaded fields */
470 overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
472 /* Merge in "file" and "sname" fields */
473 merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
474 ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
475 0 : DHCP_BOOTFILE_NAME ) );
476 merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
477 ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
478 0 : DHCP_TFTP_SERVER_NAME ) );
480 /* Set magic options for "yiaddr" and "siaddr", if present */
481 if ( dhcphdr->yiaddr.s_addr ) {
482 set_dhcp_option ( options, DHCP_EB_YIADDR,
483 &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
485 if ( dhcphdr->siaddr.s_addr ) {
486 set_dhcp_option ( options, DHCP_EB_SIADDR,
487 &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
490 assert ( options->len <= options->max_len );
495 /****************************************************************************
497 * Whole-packet construction
501 /** DHCP network device descriptor */
502 struct dhcp_netdev_desc {
509 } __attribute__ (( packed ));
511 /** DHCP client identifier */
512 struct dhcp_client_id {
513 /** Link-layer protocol */
515 /** Link-layer address */
516 uint8_t ll_addr[MAX_LL_ADDR_LEN];
517 } __attribute__ (( packed ));
519 /** DHCP client UUID */
520 struct dhcp_client_uuid {
521 /** Identifier type */
525 } __attribute__ (( packed ));
527 #define DHCP_CLIENT_UUID_TYPE 0
530 * Create DHCP request
532 * @v netdev Network device
533 * @v msgtype DHCP message type
534 * @v options DHCP server response options, or NULL
535 * @v data Buffer for DHCP packet
536 * @v max_len Size of DHCP packet buffer
537 * @v dhcppkt DHCP packet structure to fill in
538 * @ret rc Return status code
540 int create_dhcp_request ( struct net_device *netdev, int msgtype,
541 struct dhcp_option_block *options,
542 void *data, size_t max_len,
543 struct dhcp_packet *dhcppkt ) {
544 struct device_description *desc = &netdev->dev->desc;
545 struct dhcp_netdev_desc dhcp_desc;
546 struct dhcp_client_id client_id;
547 struct dhcp_client_uuid client_uuid;
548 size_t dhcp_features_len;
552 /* Create DHCP packet */
553 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
555 DBG ( "DHCP could not create DHCP packet: %s\n",
560 /* Copy in options common to all requests */
561 if ( ( rc = copy_dhcp_packet_options ( dhcppkt,
562 &dhcp_request_options )) !=0 ){
563 DBG ( "DHCP could not set common DHCP options: %s\n",
568 /* Copy any required options from previous server repsonse */
570 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
571 DHCP_SERVER_IDENTIFIER,
572 DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
573 DBG ( "DHCP could not set server identifier "
574 "option: %s\n", strerror ( rc ) );
577 if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
579 DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
580 DBG ( "DHCP could not set requested address "
581 "option: %s\n", strerror ( rc ) );
586 /* Add options to identify the feature list */
587 dhcp_features_len = ( dhcp_features_end - dhcp_features );
588 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_ENCAP,
590 dhcp_features_len ) ) != 0 ) {
591 DBG ( "DHCP could not set features list option: %s\n",
596 /* Add options to identify the network device */
597 dhcp_desc.type = desc->bus_type;
598 dhcp_desc.vendor = htons ( desc->vendor );
599 dhcp_desc.device = htons ( desc->device );
600 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_EB_BUS_ID,
602 sizeof ( dhcp_desc ) ) ) != 0 ) {
603 DBG ( "DHCP could not set bus ID option: %s\n",
608 /* Add DHCP client identifier. Required for Infiniband, and
609 * doesn't hurt other link layers.
611 client_id.ll_proto = netdev->ll_protocol->ll_proto;
612 ll_addr_len = netdev->ll_protocol->ll_addr_len;
613 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
614 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
615 if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_CLIENT_ID,
617 ( ll_addr_len + 1 ) ) ) != 0 ) {
618 DBG ( "DHCP could not set client ID: %s\n",
623 /* Add client UUID, if we have one. Required for PXE. */
624 client_uuid.type = DHCP_CLIENT_UUID_TYPE;
625 if ( ( rc = get_uuid ( &client_uuid.uuid ) ) == 0 ) {
626 if ( ( rc = set_dhcp_packet_option ( dhcppkt,
627 DHCP_CLIENT_UUID, &client_uuid,
628 sizeof ( client_uuid ) ) ) != 0 ) {
629 DBG ( "DHCP could not set client UUID: %s\n",
639 * Create DHCP response
641 * @v netdev Network device
642 * @v msgtype DHCP message type
643 * @v options DHCP options, or NULL
644 * @v data Buffer for DHCP packet
645 * @v max_len Size of DHCP packet buffer
646 * @v dhcppkt DHCP packet structure to fill in
647 * @ret rc Return status code
649 int create_dhcp_response ( struct net_device *netdev, int msgtype,
650 struct dhcp_option_block *options,
651 void *data, size_t max_len,
652 struct dhcp_packet *dhcppkt ) {
655 /* Create packet and copy in options */
656 if ( ( rc = create_dhcp_packet ( netdev, msgtype, data, max_len,
658 DBG ( " failed to build packet" );
661 if ( ( rc = copy_dhcp_packet_options ( dhcppkt, options ) ) != 0 ) {
662 DBG ( " failed to copy options" );
669 /****************************************************************************
671 * DHCP to UDP interface
675 /** A DHCP session */
676 struct dhcp_session {
677 /** Reference counter */
678 struct refcnt refcnt;
679 /** Job control interface */
680 struct job_interface job;
681 /** Data transfer interface */
682 struct xfer_interface xfer;
684 /** Network device being configured */
685 struct net_device *netdev;
686 /** Option block registration routine */
687 int ( * register_options ) ( struct net_device *netdev,
688 struct dhcp_option_block *options );
690 /** State of the session
692 * This is a value for the @c DHCP_MESSAGE_TYPE option
693 * (e.g. @c DHCPDISCOVER).
696 /** Options obtained from server */
697 struct dhcp_option_block *options;
698 /** Retransmission timer */
699 struct retry_timer timer;
705 * @v refcnt Reference counter
707 static void dhcp_free ( struct refcnt *refcnt ) {
708 struct dhcp_session *dhcp =
709 container_of ( refcnt, struct dhcp_session, refcnt );
711 netdev_put ( dhcp->netdev );
712 dhcpopt_put ( dhcp->options );
717 * Mark DHCP session as complete
719 * @v dhcp DHCP session
720 * @v rc Return status code
722 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
724 /* Block futher incoming messages */
725 job_nullify ( &dhcp->job );
726 xfer_nullify ( &dhcp->xfer );
728 /* Stop retry timer */
729 stop_timer ( &dhcp->timer );
731 /* Free resources and close interfaces */
732 xfer_close ( &dhcp->xfer, rc );
733 job_done ( &dhcp->job, rc );
736 /****************************************************************************
738 * Data transfer interface
743 * Transmit DHCP request
745 * @v dhcp DHCP session
746 * @ret rc Return status code
748 static int dhcp_send_request ( struct dhcp_session *dhcp ) {
749 struct xfer_metadata meta = {
750 .netdev = dhcp->netdev,
752 struct io_buffer *iobuf;
753 struct dhcp_packet dhcppkt;
756 DBGC ( dhcp, "DHCP %p transmitting %s\n",
757 dhcp, dhcp_msgtype_name ( dhcp->state ) );
759 assert ( ( dhcp->state == DHCPDISCOVER ) ||
760 ( dhcp->state == DHCPREQUEST ) );
762 /* Start retry timer. Do this first so that failures to
763 * transmit will be retried.
765 start_timer ( &dhcp->timer );
767 /* Allocate buffer for packet */
768 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
772 /* Create DHCP packet in temporary buffer */
773 if ( ( rc = create_dhcp_request ( dhcp->netdev, dhcp->state,
774 dhcp->options, iobuf->data,
775 iob_tailroom ( iobuf ),
776 &dhcppkt ) ) != 0 ) {
777 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
778 dhcp, strerror ( rc ) );
782 /* Transmit the packet */
783 iob_put ( iobuf, dhcppkt.len );
784 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
787 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
788 dhcp, strerror ( rc ) );
798 * Handle DHCP retry timer expiry
800 * @v timer DHCP retry timer
801 * @v fail Failure indicator
803 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
804 struct dhcp_session *dhcp =
805 container_of ( timer, struct dhcp_session, timer );
808 dhcp_finished ( dhcp, -ETIMEDOUT );
810 dhcp_send_request ( dhcp );
817 * @v xfer Data transfer interface
818 * @v iobuf I/O buffer
819 * @v data Received data
820 * @v len Length of received data
821 * @ret rc Return status code
823 static int dhcp_deliver_raw ( struct xfer_interface *xfer,
824 const void *data, size_t len ) {
825 struct dhcp_session *dhcp =
826 container_of ( xfer, struct dhcp_session, xfer );
827 const struct dhcphdr *dhcphdr = data;
828 struct dhcp_option_block *options;
829 unsigned int msgtype;
831 /* Check for matching transaction ID */
832 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
833 DBGC ( dhcp, "DHCP %p wrong transaction ID (wanted %08lx, "
834 "got %08lx)\n", dhcp, ntohl ( dhcphdr->xid ),
835 ntohl ( dhcp_xid ( dhcp->netdev ) ) );
839 /* Parse packet and create options structure */
840 options = dhcp_parse ( dhcphdr, len );
842 DBGC ( dhcp, "DHCP %p could not parse DHCP packet\n", dhcp );
846 /* Determine message type */
847 msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
848 DBGC ( dhcp, "DHCP %p received %s\n",
849 dhcp, dhcp_msgtype_name ( msgtype ) );
851 /* Handle DHCP reply */
852 switch ( dhcp->state ) {
854 if ( msgtype != DHCPOFFER )
856 dhcp->state = DHCPREQUEST;
859 if ( msgtype != DHCPACK )
861 dhcp->state = DHCPACK;
868 /* Stop timer and update stored options */
869 stop_timer ( &dhcp->timer );
871 dhcpopt_put ( dhcp->options );
872 dhcp->options = options;
874 /* Transmit next packet, or terminate session */
875 if ( dhcp->state < DHCPACK ) {
876 dhcp_send_request ( dhcp );
878 dhcp->register_options ( dhcp->netdev, dhcp->options );
879 dhcp_finished ( dhcp, 0 );
884 dhcpopt_put ( options );
888 /** DHCP data transfer interface operations */
889 static struct xfer_interface_operations dhcp_xfer_operations = {
890 .close = ignore_xfer_close,
891 .vredirect = xfer_vopen,
892 .seek = ignore_xfer_seek,
893 .window = unlimited_xfer_window,
894 .deliver_iob = xfer_deliver_as_raw,
895 .deliver_raw = dhcp_deliver_raw,
898 /****************************************************************************
900 * Job control interface
905 * Handle kill() event received via job control interface
907 * @v job DHCP job control interface
909 static void dhcp_job_kill ( struct job_interface *job ) {
910 struct dhcp_session *dhcp =
911 container_of ( job, struct dhcp_session, job );
913 /* Terminate DHCP session */
914 dhcp_finished ( dhcp, -ECANCELED );
917 /** DHCP job control interface operations */
918 static struct job_interface_operations dhcp_job_operations = {
919 .done = ignore_job_done,
920 .kill = dhcp_job_kill,
921 .progress = ignore_job_progress,
924 /****************************************************************************
931 * Start DHCP on a network device
933 * @v job Job control interface
934 * @v netdev Network device
935 * @v register_options DHCP option block registration routine
936 * @ret rc Return status code
938 * Starts DHCP on the specified network device. If successful, the @c
939 * register_options() routine will be called with the acquired
942 int start_dhcp ( struct job_interface *job, struct net_device *netdev,
943 int ( * register_options ) ( struct net_device *netdev,
944 struct dhcp_option_block * ) ) {
945 static struct sockaddr_in server = {
946 .sin_family = AF_INET,
947 .sin_addr.s_addr = INADDR_BROADCAST,
948 .sin_port = htons ( BOOTPS_PORT ),
950 static struct sockaddr_in client = {
951 .sin_family = AF_INET,
952 .sin_port = htons ( BOOTPC_PORT ),
954 struct dhcp_session *dhcp;
957 /* Allocate and initialise structure */
958 dhcp = zalloc ( sizeof ( *dhcp ) );
961 dhcp->refcnt.free = dhcp_free;
962 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
963 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
964 dhcp->netdev = netdev_get ( netdev );
965 dhcp->register_options = register_options;
966 dhcp->timer.expired = dhcp_timer_expired;
967 dhcp->state = DHCPDISCOVER;
969 /* Instantiate child objects and attach to our interfaces */
970 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
971 ( struct sockaddr * ) &server,
972 ( struct sockaddr * ) &client ) ) != 0 )
975 /* Start timer to initiate initial DHCPREQUEST */
976 start_timer_nodelay ( &dhcp->timer );
978 /* Attach parent interface, mortalise self, and return */
979 job_plug_plug ( &dhcp->job, job );
980 ref_put ( &dhcp->refcnt );
984 dhcp_finished ( dhcp, rc );
985 ref_put ( &dhcp->refcnt );
989 /****************************************************************************
991 * Network device configurator
996 * Configure network device from DHCP options
998 * @v netdev Network device
999 * @v options DHCP options block
1000 * @ret rc Return status code
1002 int dhcp_configure_netdev ( struct net_device *netdev,
1003 struct dhcp_option_block *options ) {
1004 struct in_addr address = { 0 };
1005 struct in_addr netmask = { 0 };
1006 struct in_addr gateway = { INADDR_NONE };
1009 /* Retrieve IP address configuration */
1010 find_dhcp_ipv4_option ( options, DHCP_EB_YIADDR, &address );
1011 find_dhcp_ipv4_option ( options, DHCP_SUBNET_MASK, &netmask );
1012 find_dhcp_ipv4_option ( options, DHCP_ROUTERS, &gateway );
1014 /* Do nothing unless we have at least an IP address to use */
1015 if ( ! address.s_addr )
1018 /* Clear any existing routing table entry */
1019 del_ipv4_address ( netdev );
1021 /* Set up new IP address configuration */
1022 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
1023 gateway ) ) != 0 ) {
1024 DBG ( "Could not configure %s with DHCP results: %s\n",
1025 netdev->name, strerror ( rc ) );