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>
36 #include <gpxe/timer.h>
37 #include <gpxe/settings.h>
38 #include <gpxe/dhcp.h>
39 #include <gpxe/dhcpopts.h>
40 #include <gpxe/dhcppkt.h>
41 #include <gpxe/features.h>
45 * Dynamic Host Configuration Protocol
50 * DHCP operation types
52 * This table maps from DHCP message types (i.e. values of the @c
53 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
56 static const uint8_t dhcp_op[] = {
57 [DHCPDISCOVER] = BOOTP_REQUEST,
58 [DHCPOFFER] = BOOTP_REPLY,
59 [DHCPREQUEST] = BOOTP_REQUEST,
60 [DHCPDECLINE] = BOOTP_REQUEST,
61 [DHCPACK] = BOOTP_REPLY,
62 [DHCPNAK] = BOOTP_REPLY,
63 [DHCPRELEASE] = BOOTP_REQUEST,
64 [DHCPINFORM] = BOOTP_REQUEST,
67 /** Raw option data for options common to all DHCP requests */
68 static uint8_t dhcp_request_options_data[] = {
69 DHCP_MAX_MESSAGE_SIZE,
70 DHCP_WORD ( ETH_MAX_MTU - 20 /* IP header */ - 8 /* UDP header */ ),
71 DHCP_CLIENT_ARCHITECTURE, DHCP_WORD ( 0 ),
72 DHCP_CLIENT_NDI, DHCP_OPTION ( 1 /* UNDI */ , 2, 1 /* v2.1 */ ),
74 DHCP_STRING ( 'P', 'X', 'E', 'C', 'l', 'i', 'e', 'n', 't', ':',
75 'A', 'r', 'c', 'h', ':', '0', '0', '0', '0', '0', ':',
76 'U', 'N', 'D', 'I', ':', '0', '0', '2', '0', '0', '1' ),
77 DHCP_PARAMETER_REQUEST_LIST,
78 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
79 DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
80 DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
81 DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
82 DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
86 /** Options common to all DHCP requests */
87 static struct dhcp_options dhcp_request_options = {
88 .data = dhcp_request_options_data,
89 .max_len = sizeof ( dhcp_request_options_data ),
90 .len = sizeof ( dhcp_request_options_data ),
93 /** DHCP feature codes */
94 static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
95 static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
97 /** Version number feature */
98 FEATURE_VERSION ( VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH );
100 /** DHCP network device descriptor */
101 struct dhcp_netdev_desc {
108 } __attribute__ (( packed ));
110 /** DHCP client identifier */
111 struct dhcp_client_id {
112 /** Link-layer protocol */
114 /** Link-layer address */
115 uint8_t ll_addr[MAX_LL_ADDR_LEN];
116 } __attribute__ (( packed ));
118 /** DHCP client UUID */
119 struct dhcp_client_uuid {
120 /** Identifier type */
124 } __attribute__ (( packed ));
126 #define DHCP_CLIENT_UUID_TYPE 0
128 /** DHCP PXE boot menu item */
129 struct dhcp_pxe_boot_menu_item {
132 * This field actually identifies the specific boot server (or
133 * cluster of boot servers offering identical boot files).
141 } __attribute__ (( packed ));
144 * Name a DHCP packet type
146 * @v msgtype DHCP message type
147 * @ret string DHCP mesasge type name
149 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
151 case DHCPNONE: return "BOOTP"; /* Non-DHCP packet */
152 case DHCPDISCOVER: return "DHCPDISCOVER";
153 case DHCPOFFER: return "DHCPOFFER";
154 case DHCPREQUEST: return "DHCPREQUEST";
155 case DHCPDECLINE: return "DHCPDECLINE";
156 case DHCPACK: return "DHCPACK";
157 case DHCPNAK: return "DHCPNAK";
158 case DHCPRELEASE: return "DHCPRELEASE";
159 case DHCPINFORM: return "DHCPINFORM";
160 default: return "DHCP<invalid>";
165 * Calculate DHCP transaction ID for a network device
167 * @v netdev Network device
170 * Extract the least significant bits of the hardware address for use
171 * as the transaction ID.
173 static uint32_t dhcp_xid ( struct net_device *netdev ) {
176 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
177 - sizeof ( xid ) ), sizeof ( xid ) );
181 /****************************************************************************
187 /** A DHCP settings block */
188 struct dhcp_settings {
189 /** Reference counter */
190 struct refcnt refcnt;
192 struct dhcp_packet dhcppkt;
193 /** Setting interface */
194 struct settings settings;
198 * Increment reference count on DHCP settings block
200 * @v dhcpset DHCP settings block
201 * @ret dhcpset DHCP settings block
203 static inline __attribute__ (( always_inline )) struct dhcp_settings *
204 dhcpset_get ( struct dhcp_settings *dhcpset ) {
205 ref_get ( &dhcpset->refcnt );
210 * Decrement reference count on DHCP settings block
212 * @v dhcpset DHCP settings block
214 static inline __attribute__ (( always_inline )) void
215 dhcpset_put ( struct dhcp_settings *dhcpset ) {
216 ref_put ( &dhcpset->refcnt );
220 * Store value of DHCP setting
222 * @v settings Settings block
223 * @v setting Setting to store
224 * @v data Setting data, or NULL to clear setting
225 * @v len Length of setting data
226 * @ret rc Return status code
228 static int dhcpset_store ( struct settings *settings, struct setting *setting,
229 const void *data, size_t len ) {
230 struct dhcp_settings *dhcpset =
231 container_of ( settings, struct dhcp_settings, settings );
233 return dhcppkt_store ( &dhcpset->dhcppkt, setting->tag, data, len );
237 * Fetch value of DHCP setting
239 * @v settings Settings block, or NULL to search all blocks
240 * @v setting Setting to fetch
241 * @v data Buffer to fill with setting data
242 * @v len Length of buffer
243 * @ret len Length of setting data, or negative error
245 static int dhcpset_fetch ( struct settings *settings, struct setting *setting,
246 void *data, size_t len ) {
247 struct dhcp_settings *dhcpset =
248 container_of ( settings, struct dhcp_settings, settings );
250 return dhcppkt_fetch ( &dhcpset->dhcppkt, setting->tag, data, len );
253 /** DHCP settings operations */
254 static struct settings_operations dhcpset_settings_operations = {
255 .store = dhcpset_store,
256 .fetch = dhcpset_fetch,
260 * Create DHCP setting block
262 * @v dhcphdr DHCP packet
263 * @v len Length of DHCP packet
264 * @ret dhcpset DHCP settings block
266 static struct dhcp_settings * dhcpset_create ( const struct dhcphdr *dhcphdr,
268 struct dhcp_settings *dhcpset;
271 dhcpset = zalloc ( sizeof ( *dhcpset ) + len );
273 data = ( ( ( void * ) dhcpset ) + sizeof ( *dhcpset ) );
274 memcpy ( data, dhcphdr, len );
275 dhcppkt_init ( &dhcpset->dhcppkt, data, len );
276 settings_init ( &dhcpset->settings,
277 &dhcpset_settings_operations, &dhcpset->refcnt,
278 DHCP_SETTINGS_NAME, 0 );
283 /** DHCP server address setting */
284 struct setting dhcp_server_setting __setting = {
285 .name = "dhcp-server",
286 .description = "DHCP server address",
287 .tag = DHCP_SERVER_IDENTIFIER,
288 .type = &setting_type_ipv4,
291 /****************************************************************************
297 /** DHCP session states */
298 enum dhcp_session_state {
299 /** Sending DHCPDISCOVERs, collecting DHCPOFFERs and ProxyDHCPOFFERs */
300 DHCP_STATE_DISCOVER = 0,
301 /** Sending DHCPREQUESTs, waiting for DHCPACK */
303 /** Sending ProxyDHCPREQUESTs, waiting for ProxyDHCPACK */
304 DHCP_STATE_PROXYREQUEST,
305 /** Sending BootServerDHCPREQUESTs, waiting for BootServerDHCPACK */
306 DHCP_STATE_BSREQUEST,
310 * Name a DHCP session state
312 * @v state DHCP session state
313 * @ret string DHCP session state name
315 static inline const char * dhcp_state_name ( enum dhcp_session_state state ) {
317 case DHCP_STATE_DISCOVER: return "DHCPDISCOVER";
318 case DHCP_STATE_REQUEST: return "DHCPREQUEST";
319 case DHCP_STATE_PROXYREQUEST: return "ProxyDHCPREQUEST";
320 case DHCP_STATE_BSREQUEST: return "BootServerREQUEST";
321 default: return "<invalid>";
325 /** A DHCP session */
326 struct dhcp_session {
327 /** Reference counter */
328 struct refcnt refcnt;
329 /** Job control interface */
330 struct job_interface job;
331 /** Data transfer interface */
332 struct xfer_interface xfer;
334 /** Network device being configured */
335 struct net_device *netdev;
337 /** State of the session
339 * This is a value for the @c DHCP_MESSAGE_TYPE option
340 * (e.g. @c DHCPDISCOVER).
342 enum dhcp_session_state state;
343 /** DHCPOFFER obtained during DHCPDISCOVER */
344 struct dhcp_settings *dhcpoffer;
345 /** ProxyDHCPOFFER obtained during DHCPDISCOVER */
346 struct dhcp_settings *proxydhcpoffer;
347 /** DHCPACK obtained during DHCPREQUEST */
348 struct dhcp_settings *dhcpack;
349 /** ProxyDHCPACK obtained during ProxyDHCPREQUEST */
350 struct dhcp_settings *proxydhcpack;
351 /** BootServerDHCPACK obtained during BootServerDHCPREQUEST */
352 struct dhcp_settings *bsdhcpack;
353 /** Retransmission timer */
354 struct retry_timer timer;
355 /** Start time of the current state (in ticks) */
362 * @v refcnt Reference counter
364 static void dhcp_free ( struct refcnt *refcnt ) {
365 struct dhcp_session *dhcp =
366 container_of ( refcnt, struct dhcp_session, refcnt );
368 netdev_put ( dhcp->netdev );
369 dhcpset_put ( dhcp->dhcpoffer );
370 dhcpset_put ( dhcp->proxydhcpoffer );
371 dhcpset_put ( dhcp->dhcpack );
372 dhcpset_put ( dhcp->proxydhcpack );
373 dhcpset_put ( dhcp->bsdhcpack );
378 * Mark DHCP session as complete
380 * @v dhcp DHCP session
381 * @v rc Return status code
383 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
385 /* Block futher incoming messages */
386 job_nullify ( &dhcp->job );
387 xfer_nullify ( &dhcp->xfer );
389 /* Stop retry timer */
390 stop_timer ( &dhcp->timer );
392 /* Free resources and close interfaces */
393 xfer_close ( &dhcp->xfer, rc );
394 job_done ( &dhcp->job, rc );
397 /****************************************************************************
399 * Data transfer interface
404 * Create a DHCP packet
406 * @v dhcppkt DHCP packet structure to fill in
407 * @v netdev Network device
408 * @v msgtype DHCP message type
409 * @v options Initial options to include (or NULL)
410 * @v data Buffer for DHCP packet
411 * @v max_len Size of DHCP packet buffer
412 * @ret rc Return status code
414 * Creates a DHCP packet in the specified buffer, and fills out a @c
415 * dhcp_packet structure.
417 int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
418 struct net_device *netdev, uint8_t msgtype,
419 struct dhcp_options *options,
420 void *data, size_t max_len ) {
421 struct dhcphdr *dhcphdr = data;
427 options_len = ( options ? options->len : 0 );
428 if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
431 /* Initialise DHCP packet content */
432 memset ( dhcphdr, 0, max_len );
433 dhcphdr->xid = dhcp_xid ( netdev );
434 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
435 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
436 dhcphdr->op = dhcp_op[msgtype];
437 /* If hardware length exceeds the chaddr field length, don't
438 * use the chaddr field. This is as per RFC4390.
440 hlen = netdev->ll_protocol->ll_addr_len;
441 if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
443 dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
445 dhcphdr->hlen = hlen;
446 memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
447 memcpy ( dhcphdr->options, options->data, options_len );
449 /* Initialise DHCP packet structure */
450 memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
451 dhcppkt_init ( dhcppkt, data, max_len );
453 /* Set DHCP_MESSAGE_TYPE option */
454 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
455 &msgtype, sizeof ( msgtype ) ) ) != 0 )
462 * Create DHCP request packet
464 * @v dhcppkt DHCP packet structure to fill in
465 * @v netdev Network device
466 * @v msgtype DHCP message type
467 * @v ciaddr Client IP address, if applicable
468 * @v server Server identifier, if applicable
469 * @v requested_ip Requested address, if applicable
470 * @v menu_item PXE menu item, if applicable
471 * @v data Buffer for DHCP packet
472 * @v max_len Size of DHCP packet buffer
473 * @ret rc Return status code
475 int dhcp_create_request ( struct dhcp_packet *dhcppkt,
476 struct net_device *netdev, unsigned int msgtype,
477 struct in_addr ciaddr, struct in_addr server,
478 struct in_addr requested_ip,
479 struct dhcp_pxe_boot_menu_item *menu_item,
480 void *data, size_t max_len ) {
481 struct device_description *desc = &netdev->dev->desc;
482 struct dhcp_netdev_desc dhcp_desc;
483 struct dhcp_client_id client_id;
484 struct dhcp_client_uuid client_uuid;
485 size_t dhcp_features_len;
489 /* Create DHCP packet */
490 if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
491 &dhcp_request_options, data,
493 DBG ( "DHCP could not create DHCP packet: %s\n",
498 /* Set client IP address */
499 dhcppkt->dhcphdr->ciaddr = ciaddr;
501 /* Set server ID, if present */
502 if ( server.s_addr &&
503 ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
504 &server, sizeof ( server ) ) ) != 0 ) ) {
505 DBG ( "DHCP could not set server ID: %s\n",
510 /* Set requested IP address, if present */
511 if ( requested_ip.s_addr &&
512 ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
514 sizeof ( requested_ip ) ) ) != 0 ) ) {
515 DBG ( "DHCP could not set requested address: %s\n",
520 /* Add options to identify the feature list */
521 dhcp_features_len = ( dhcp_features_end - dhcp_features );
522 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
523 dhcp_features_len ) ) != 0 ) {
524 DBG ( "DHCP could not set features list option: %s\n",
529 /* Add options to identify the network device */
530 dhcp_desc.type = desc->bus_type;
531 dhcp_desc.vendor = htons ( desc->vendor );
532 dhcp_desc.device = htons ( desc->device );
533 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
534 sizeof ( dhcp_desc ) ) ) != 0 ) {
535 DBG ( "DHCP could not set bus ID option: %s\n",
540 /* Add DHCP client identifier. Required for Infiniband, and
541 * doesn't hurt other link layers.
543 client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
544 ll_addr_len = netdev->ll_protocol->ll_addr_len;
545 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
546 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
547 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
548 ( ll_addr_len + 1 ) ) ) != 0 ) {
549 DBG ( "DHCP could not set client ID: %s\n",
554 /* Add client UUID, if we have one. Required for PXE. */
555 client_uuid.type = DHCP_CLIENT_UUID_TYPE;
556 if ( ( rc = fetch_uuid_setting ( NULL, &uuid_setting,
557 &client_uuid.uuid ) ) >= 0 ) {
558 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
560 sizeof ( client_uuid ) ) ) != 0 ) {
561 DBG ( "DHCP could not set client UUID: %s\n",
567 /* Set PXE boot menu item, if present */
568 if ( menu_item && menu_item->type &&
569 ( ( rc = dhcppkt_store ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
571 sizeof ( *menu_item ) ) ) != 0 ) ) {
572 DBG ( "DHCP could not set PXE menu item: %s\n",
581 * Transmit DHCP request
583 * @v dhcp DHCP session
584 * @ret rc Return status code
586 static int dhcp_tx ( struct dhcp_session *dhcp ) {
587 static struct sockaddr_in dest = {
588 .sin_family = AF_INET,
589 .sin_port = htons ( PROXYDHCP_PORT ),
591 static struct sockaddr_in src = {
592 .sin_family = AF_INET,
593 .sin_port = htons ( BOOTPC_PORT ),
595 struct xfer_metadata meta = {
596 .netdev = dhcp->netdev,
598 struct io_buffer *iobuf;
599 struct dhcp_packet dhcppkt;
600 struct in_addr ciaddr = { 0 };
601 struct in_addr server = { 0 };
602 struct in_addr requested_ip = { 0 };
603 struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
604 unsigned int msgtype;
607 /* Start retry timer. Do this first so that failures to
608 * transmit will be retried.
610 start_timer ( &dhcp->timer );
612 /* Determine packet contents based on current state */
613 switch ( dhcp->state ) {
614 case DHCP_STATE_DISCOVER:
615 msgtype = DHCPDISCOVER;
617 case DHCP_STATE_REQUEST:
618 assert ( dhcp->dhcpoffer );
619 msgtype = DHCPREQUEST;
620 dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt,
621 DHCP_SERVER_IDENTIFIER, &server,
623 requested_ip = dhcp->dhcpoffer->dhcppkt.dhcphdr->yiaddr;
625 case DHCP_STATE_PROXYREQUEST:
626 assert ( dhcp->dhcpoffer );
627 assert ( dhcp->proxydhcpoffer );
628 assert ( dhcp->dhcpack );
629 msgtype = DHCPREQUEST;
630 ciaddr = dhcp->dhcpoffer->dhcppkt.dhcphdr->yiaddr;
631 dhcppkt_fetch ( &dhcp->proxydhcpoffer->dhcppkt,
632 DHCP_SERVER_IDENTIFIER, &dest.sin_addr,
633 sizeof ( dest.sin_addr ) );
634 meta.dest = ( struct sockaddr * ) &dest;
635 server = dest.sin_addr;
636 assert ( dest.sin_addr.s_addr );
637 assert ( ciaddr.s_addr );
639 case DHCP_STATE_BSREQUEST:
640 assert ( dhcp->dhcpoffer );
641 assert ( dhcp->proxydhcpoffer );
642 assert ( dhcp->dhcpack );
643 assert ( dhcp->proxydhcpack );
644 msgtype = DHCPREQUEST;
645 ciaddr = dhcp->dhcpoffer->dhcppkt.dhcphdr->yiaddr;
646 dhcppkt_fetch ( &dhcp->proxydhcpack->dhcppkt,
647 DHCP_PXE_BOOT_SERVER_MCAST,
648 &dest.sin_addr, sizeof ( dest.sin_addr ) );
649 meta.dest = ( struct sockaddr * ) &dest;
650 dhcppkt_fetch ( &dhcp->proxydhcpack->dhcppkt,
651 DHCP_PXE_BOOT_MENU, &menu_item.type,
652 sizeof ( menu_item.type ) );
653 assert ( dest.sin_addr.s_addr );
654 assert ( menu_item.type );
655 assert ( ciaddr.s_addr );
662 DBGC ( dhcp, "DHCP %p %s", dhcp, dhcp_msgtype_name ( msgtype ) );
664 DBGC ( dhcp, " to %s", inet_ntoa ( server ) );
666 if ( dest.sin_addr.s_addr == server.s_addr ) {
667 DBGC ( dhcp, ":%d (unicast)",
668 ntohs ( dest.sin_port ) );
670 DBGC ( dhcp, " via %s:%d", inet_ntoa ( dest.sin_addr ),
671 ntohs ( dest.sin_port ) );
674 DBGC ( dhcp, " (broadcast)" );
676 if ( requested_ip.s_addr )
677 DBGC ( dhcp, " for %s", inet_ntoa ( requested_ip ) );
678 if ( menu_item.type )
679 DBGC ( dhcp, " for item %04x", ntohs ( menu_item.type ) );
682 /* Allocate buffer for packet */
683 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
687 /* Create DHCP packet in temporary buffer */
688 if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev, msgtype,
689 ciaddr, server, requested_ip,
690 &menu_item, iobuf->data,
691 iob_tailroom ( iobuf ) ) ) != 0 ) {
692 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
693 dhcp, strerror ( rc ) );
697 /* Explicitly specify source address, if available. */
698 if ( ciaddr.s_addr ) {
699 src.sin_addr = ciaddr;
700 meta.src = ( struct sockaddr * ) &src;
703 /* Transmit the packet */
704 iob_put ( iobuf, dhcppkt.len );
705 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
708 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
709 dhcp, strerror ( rc ) );
719 * Transition to new DHCP session state
721 * @v dhcp DHCP session
722 * @v state New session state
724 static void dhcp_set_state ( struct dhcp_session *dhcp,
725 enum dhcp_session_state state ) {
726 DBGC ( dhcp, "DHCP %p entering %s state\n",
727 dhcp, dhcp_state_name ( state ) );
729 dhcp->start = currticks();
730 dhcp->timer.min_timeout = 0;
731 start_timer_nodelay ( &dhcp->timer );
735 * Transition to next DHCP state
737 * @v dhcp DHCP session
739 static void dhcp_next_state ( struct dhcp_session *dhcp ) {
740 struct in_addr bs_mcast = { 0 };
742 switch ( dhcp->state ) {
743 case DHCP_STATE_DISCOVER:
744 dhcp_set_state ( dhcp, DHCP_STATE_REQUEST );
746 case DHCP_STATE_REQUEST:
747 if ( dhcp->proxydhcpoffer ) {
748 /* Store DHCPACK as ProxyDHCPACK. This
749 * handles the case in which the DHCP server
750 * itself responds with "PXEClient" and PXE
751 * options but there is no actual ProxyDHCP
752 * server resident on the machine.
754 dhcp->proxydhcpack = dhcpset_get ( dhcp->dhcpack );
755 dhcp_set_state ( dhcp, DHCP_STATE_PROXYREQUEST );
759 case DHCP_STATE_PROXYREQUEST:
760 if ( dhcp->proxydhcpack ) {
761 dhcppkt_fetch ( &dhcp->proxydhcpack->dhcppkt,
762 DHCP_PXE_BOOT_SERVER_MCAST,
763 &bs_mcast, sizeof ( bs_mcast ) );
764 if ( bs_mcast.s_addr ) {
765 dhcp_set_state ( dhcp, DHCP_STATE_BSREQUEST );
770 case DHCP_STATE_BSREQUEST:
771 dhcp_finished ( dhcp, 0 );
781 * Store received DHCPOFFER
783 * @v dhcp DHCP session
784 * @v dhcpoffer Received DHCPOFFER
785 * @v stored_dhcpoffer Location to store DHCPOFFER
787 * The DHCPOFFER will be stored in place of the existing stored
788 * DHCPOFFER if its priority is equal to or greater than the stored
791 static void dhcp_store_dhcpoffer ( struct dhcp_session *dhcp,
792 struct dhcp_settings *dhcpoffer,
793 struct dhcp_settings **stored_dhcpoffer ) {
794 uint8_t stored_priority = 0;
795 uint8_t priority = 0;
797 /* Get priorities of the two DHCPOFFERs */
798 if ( *stored_dhcpoffer ) {
799 dhcppkt_fetch ( &(*stored_dhcpoffer)->dhcppkt,
800 DHCP_EB_PRIORITY, &stored_priority,
801 sizeof ( stored_priority ) );
803 dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_EB_PRIORITY, &priority,
804 sizeof ( priority ) );
806 /* Replace stored offer only if priority is equal or greater */
807 if ( priority >= stored_priority ) {
808 if ( *stored_dhcpoffer ) {
809 DBGC ( dhcp, "DHCP %p stored DHCPOFFER %p discarded\n",
810 dhcp, *stored_dhcpoffer );
812 DBGC ( dhcp, "DHCP %p DHCPOFFER %p stored\n",
814 dhcpset_put ( *stored_dhcpoffer );
815 *stored_dhcpoffer = dhcpset_get ( dhcpoffer );
820 * Handle received DHCPOFFER
822 * @v dhcp DHCP session
823 * @v dhcpoffer Received DHCPOFFER
825 static void dhcp_rx_dhcpoffer ( struct dhcp_session *dhcp,
826 struct dhcp_settings *dhcpoffer ) {
827 struct in_addr server_id = { 0 };
828 char vci[9]; /* "PXEClient" */
830 uint8_t ignore_proxy = 0;
831 unsigned long elapsed;
833 /* Check for presence of DHCP server ID */
834 if ( dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_SERVER_IDENTIFIER,
835 &server_id, sizeof ( server_id ) )
836 != sizeof ( server_id ) ) {
837 DBGC ( dhcp, "DHCP %p DHCPOFFER %p missing server ID\n",
839 /* Could be a valid BOOTP offer; do not abort processing */
842 /* If there is an IP address, it's a normal DHCPOFFER */
843 if ( dhcpoffer->dhcppkt.dhcphdr->yiaddr.s_addr != 0 ) {
844 DBGC ( dhcp, "DHCP %p DHCPOFFER %p from %s",
845 dhcp, dhcpoffer, inet_ntoa ( server_id ) );
846 DBGC ( dhcp, " has IP %s\n",
847 inet_ntoa ( dhcpoffer->dhcppkt.dhcphdr->yiaddr ) );
848 dhcp_store_dhcpoffer ( dhcp, dhcpoffer, &dhcp->dhcpoffer );
851 /* If there is a "PXEClient" vendor class ID, it's a
852 * ProxyDHCPOFFER. Note that it could be both a normal
853 * DHCPOFFER and a ProxyDHCPOFFER.
855 len = dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_VENDOR_CLASS_ID,
856 vci, sizeof ( vci ) );
857 if ( ( server_id.s_addr != 0 ) &&
858 ( len >= ( int ) sizeof ( vci ) ) &&
859 ( strncmp ( "PXEClient", vci, sizeof ( vci ) ) == 0 ) ) {
860 DBGC ( dhcp, "DHCP %p DHCPOFFER %p from %s is a "
862 dhcp, dhcpoffer, inet_ntoa ( server_id ) );
863 dhcp_store_dhcpoffer ( dhcp, dhcpoffer,
864 &dhcp->proxydhcpoffer );
867 /* We can transition to making the DHCPREQUEST when we have a
868 * valid DHCPOFFER, and either:
870 * o The DHCPOFFER instructs us to not wait for ProxyDHCP, or
871 * o We have a valid ProxyDHCPOFFER, or
872 * o We have allowed sufficient time for ProxyDHCPOFFERs.
875 /* If we don't yet have a DHCPOFFER, do nothing */
876 if ( ! dhcp->dhcpoffer )
879 /* If the DHCPOFFER instructs us to ignore ProxyDHCP, discard
882 dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt, DHCP_EB_NO_PROXYDHCP,
883 &ignore_proxy, sizeof ( ignore_proxy ) );
884 if ( ignore_proxy && dhcp->proxydhcpoffer ) {
885 DBGC ( dhcp, "DHCP %p discarding ProxyDHCPOFFER\n", dhcp );
886 dhcpset_put ( dhcp->proxydhcpoffer );
887 dhcp->proxydhcpoffer = NULL;
890 /* If we can't yet transition to DHCPREQUEST, do nothing */
891 elapsed = ( currticks() - dhcp->start );
892 if ( ! ( ignore_proxy || dhcp->proxydhcpoffer ||
893 ( elapsed > PROXYDHCP_WAIT_TIME ) ) )
896 /* Transition to DHCPREQUEST */
897 dhcp_next_state ( dhcp );
901 * Store received DHCPACK
903 * @v dhcp DHCP session
904 * @v dhcpack Received DHCPACK
906 * The DHCPACK will be registered as a settings block.
908 static int dhcp_store_dhcpack ( struct dhcp_session *dhcp,
909 struct dhcp_settings *dhcpack,
910 struct settings *parent ) {
911 struct settings *settings = &dhcpack->settings;
912 struct settings *old_settings;
915 /* Unregister any old settings obtained via DHCP */
916 if ( ( old_settings = find_child_settings ( parent, settings->name ) ))
917 unregister_settings ( old_settings );
919 /* Register new settings */
920 if ( ( rc = register_settings ( settings, parent ) ) != 0 ) {
921 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
922 dhcp, strerror ( rc ) );
923 dhcp_finished ( dhcp, rc ); /* This is a fatal error */
931 * Handle received DHCPACK
933 * @v dhcp DHCP session
934 * @v dhcpack Received DHCPACK
936 static void dhcp_rx_dhcpack ( struct dhcp_session *dhcp,
937 struct dhcp_settings *dhcpack ) {
938 struct settings *parent;
939 struct in_addr offer_server_id = { 0 };
940 struct in_addr ack_server_id = { 0 };
943 /* Verify server ID matches */
944 assert ( dhcp->dhcpoffer != NULL );
945 dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt, DHCP_SERVER_IDENTIFIER,
946 &offer_server_id, sizeof ( offer_server_id ) );
947 dhcppkt_fetch ( &dhcpack->dhcppkt, DHCP_SERVER_IDENTIFIER,
948 &ack_server_id, sizeof ( ack_server_id ) );
949 if ( offer_server_id.s_addr != ack_server_id.s_addr ) {
950 DBGC ( dhcp, "DHCP %p ignoring DHCPACK with wrong server ID "
951 "%s\n", dhcp, inet_ntoa ( ack_server_id ) );
956 assert ( dhcp->dhcpack == NULL );
957 dhcp->dhcpack = dhcpset_get ( dhcpack );
959 /* Register settings */
960 parent = netdev_settings ( dhcp->netdev );
961 if ( ( rc = dhcp_store_dhcpack ( dhcp, dhcpack, parent ) ) != 0 )
964 /* Transition to next state */
965 dhcp_next_state ( dhcp );
969 * Handle received ProxyDHCPACK
971 * @v dhcp DHCP session
972 * @v proxydhcpack Received ProxyDHCPACK
974 static void dhcp_rx_proxydhcpack ( struct dhcp_session *dhcp,
975 struct dhcp_settings *proxydhcpack ) {
976 struct in_addr offer_server_id = { 0 };
977 struct in_addr ack_server_id = { 0 };
980 /* Verify server ID matches, if present */
981 assert ( dhcp->proxydhcpoffer != NULL );
982 if ( ( rc = dhcppkt_fetch ( &proxydhcpack->dhcppkt,
983 DHCP_SERVER_IDENTIFIER, &ack_server_id,
984 sizeof ( ack_server_id ) ) ) > 0 ) {
985 dhcppkt_fetch ( &dhcp->proxydhcpoffer->dhcppkt,
986 DHCP_SERVER_IDENTIFIER, &offer_server_id,
987 sizeof ( offer_server_id ) );
988 if ( offer_server_id.s_addr != ack_server_id.s_addr ) {
989 DBGC ( dhcp, "DHCP %p ignoring ProxyDHCPACK with "
990 "wrong server ID %s\n",
991 dhcp, inet_ntoa ( ack_server_id ) );
996 /* Rename settings */
997 proxydhcpack->settings.name = PROXYDHCP_SETTINGS_NAME;
999 /* Record ProxyDHCPACK */
1000 dhcpset_put ( dhcp->proxydhcpack );
1001 dhcp->proxydhcpack = dhcpset_get ( proxydhcpack );
1003 /* Register settings */
1004 if ( ( rc = dhcp_store_dhcpack ( dhcp, proxydhcpack, NULL ) ) != 0 )
1007 /* Transition to next state */
1008 dhcp_next_state ( dhcp );
1012 * Handle received BootServerDHCPACK
1014 * @v dhcp DHCP session
1015 * @v bsdhcpack Received BootServerDHCPACK
1017 static void dhcp_rx_bsdhcpack ( struct dhcp_session *dhcp,
1018 struct dhcp_settings *bsdhcpack ) {
1021 /* Rename settings */
1022 bsdhcpack->settings.name = BSDHCP_SETTINGS_NAME;
1024 /* Record ProxyDHCPACK */
1025 assert ( dhcp->bsdhcpack == NULL );
1026 dhcp->bsdhcpack = dhcpset_get ( bsdhcpack );
1028 /* Register settings */
1029 if ( ( rc = dhcp_store_dhcpack ( dhcp, bsdhcpack, NULL ) ) != 0 )
1032 /* Transition to next state */
1033 dhcp_next_state ( dhcp );
1039 * @v xfer Data transfer interface
1040 * @v iobuf I/O buffer
1041 * @v meta Transfer metadata
1042 * @ret rc Return status code
1044 static int dhcp_deliver_iob ( struct xfer_interface *xfer,
1045 struct io_buffer *iobuf,
1046 struct xfer_metadata *meta ) {
1047 struct dhcp_session *dhcp =
1048 container_of ( xfer, struct dhcp_session, xfer );
1049 struct sockaddr_in *sin_src;
1050 unsigned int src_port;
1051 struct dhcp_settings *dhcpset;
1052 struct dhcphdr *dhcphdr;
1053 uint8_t msgtype = 0;
1058 DBGC ( dhcp, "DHCP %p received packet without metadata\n",
1063 if ( ! meta->src ) {
1064 DBGC ( dhcp, "DHCP %p received packet without source port\n",
1069 sin_src = ( struct sockaddr_in * ) meta->src;
1070 src_port = sin_src->sin_port;
1072 /* Convert packet into a DHCP settings block */
1073 dhcpset = dhcpset_create ( iobuf->data, iob_len ( iobuf ) );
1075 DBGC ( dhcp, "DHCP %p could not store DHCP packet\n", dhcp );
1077 goto err_dhcpset_create;
1079 dhcphdr = dhcpset->dhcppkt.dhcphdr;
1081 /* Identify message type */
1082 dhcppkt_fetch ( &dhcpset->dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
1083 sizeof ( msgtype ) );
1084 DBGC ( dhcp, "DHCP %p %s %p from %s:%d\n", dhcp,
1085 dhcp_msgtype_name ( msgtype ), dhcpset,
1086 inet_ntoa ( sin_src->sin_addr ), ntohs ( src_port ) );
1088 /* Check for matching transaction ID */
1089 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
1090 DBGC ( dhcp, "DHCP %p %s %p has bad transaction ID\n",
1091 dhcp, dhcp_msgtype_name ( msgtype ), dhcpset );
1096 /* Handle packet based on current state */
1097 switch ( dhcp->state ) {
1098 case DHCP_STATE_DISCOVER:
1099 if ( ( ( msgtype == DHCPOFFER ) || ( msgtype == DHCPNONE ) ) &&
1100 ( src_port == htons ( BOOTPS_PORT ) ) )
1101 dhcp_rx_dhcpoffer ( dhcp, dhcpset );
1103 case DHCP_STATE_REQUEST:
1104 if ( ( ( msgtype == DHCPACK ) || ( msgtype == DHCPNONE ) ) &&
1105 ( src_port == htons ( BOOTPS_PORT ) ) )
1106 dhcp_rx_dhcpack ( dhcp, dhcpset );
1108 case DHCP_STATE_PROXYREQUEST:
1109 if ( ( msgtype == DHCPACK ) &&
1110 ( src_port == htons ( PROXYDHCP_PORT ) ) )
1111 dhcp_rx_proxydhcpack ( dhcp, dhcpset );
1113 case DHCP_STATE_BSREQUEST:
1114 if ( ( msgtype == DHCPACK ) &&
1115 ( src_port == htons ( PROXYDHCP_PORT ) ) )
1116 dhcp_rx_bsdhcpack ( dhcp, dhcpset );
1124 dhcpset_put ( dhcpset );
1132 /** DHCP data transfer interface operations */
1133 static struct xfer_interface_operations dhcp_xfer_operations = {
1134 .close = ignore_xfer_close,
1135 .vredirect = xfer_vopen,
1136 .window = unlimited_xfer_window,
1137 .alloc_iob = default_xfer_alloc_iob,
1138 .deliver_iob = dhcp_deliver_iob,
1139 .deliver_raw = xfer_deliver_as_iob,
1143 * Handle DHCP retry timer expiry
1145 * @v timer DHCP retry timer
1146 * @v fail Failure indicator
1148 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
1149 struct dhcp_session *dhcp =
1150 container_of ( timer, struct dhcp_session, timer );
1151 unsigned long elapsed = ( currticks() - dhcp->start );
1153 /* If we have failed, terminate DHCP */
1155 dhcp_finished ( dhcp, -ETIMEDOUT );
1159 /* Give up waiting for ProxyDHCP before we reach the failure point */
1160 if ( dhcp->dhcpoffer && ( elapsed > PROXYDHCP_WAIT_TIME ) ) {
1161 dhcp_next_state ( dhcp );
1165 /* Otherwise, retransmit current packet */
1169 /****************************************************************************
1171 * Job control interface
1176 * Handle kill() event received via job control interface
1178 * @v job DHCP job control interface
1180 static void dhcp_job_kill ( struct job_interface *job ) {
1181 struct dhcp_session *dhcp =
1182 container_of ( job, struct dhcp_session, job );
1184 /* Terminate DHCP session */
1185 dhcp_finished ( dhcp, -ECANCELED );
1188 /** DHCP job control interface operations */
1189 static struct job_interface_operations dhcp_job_operations = {
1190 .done = ignore_job_done,
1191 .kill = dhcp_job_kill,
1192 .progress = ignore_job_progress,
1195 /****************************************************************************
1202 * Start DHCP on a network device
1204 * @v job Job control interface
1205 * @v netdev Network device
1206 * @v register_options DHCP option block registration routine
1207 * @ret rc Return status code
1209 * Starts DHCP on the specified network device. If successful, the @c
1210 * register_options() routine will be called with the acquired
1213 int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
1214 static struct sockaddr_in server = {
1215 .sin_family = AF_INET,
1216 .sin_addr.s_addr = INADDR_BROADCAST,
1217 .sin_port = htons ( BOOTPS_PORT ),
1219 static struct sockaddr_in client = {
1220 .sin_family = AF_INET,
1221 .sin_port = htons ( BOOTPC_PORT ),
1223 struct dhcp_session *dhcp;
1226 /* Allocate and initialise structure */
1227 dhcp = zalloc ( sizeof ( *dhcp ) );
1230 dhcp->refcnt.free = dhcp_free;
1231 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1232 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1233 dhcp->netdev = netdev_get ( netdev );
1234 dhcp->timer.expired = dhcp_timer_expired;
1235 dhcp->timer.min_timeout = DHCP_MIN_TIMEOUT;
1236 dhcp->timer.max_timeout = DHCP_MAX_TIMEOUT;
1237 dhcp->start = currticks();
1239 /* Instantiate child objects and attach to our interfaces */
1240 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
1241 ( struct sockaddr * ) &server,
1242 ( struct sockaddr * ) &client ) ) != 0 )
1245 /* Start timer to initiate initial DHCPREQUEST */
1246 start_timer_nodelay ( &dhcp->timer );
1248 /* Attach parent interface, mortalise self, and return */
1249 job_plug_plug ( &dhcp->job, job );
1250 ref_put ( &dhcp->refcnt );
1254 dhcp_finished ( dhcp, rc );
1255 ref_put ( &dhcp->refcnt );