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 containing IP address */
344 struct dhcp_settings *dhcpoffer;
345 /** DHCPOFFER obtained during DHCPDISCOVER containing "PXEClient" */
346 struct dhcp_settings *pxedhcpoffer;
347 /** DHCPACK obtained during DHCPREQUEST containing IP address */
348 struct dhcp_settings *dhcpack;
349 /** DHCPACK obtained during DHCPREQUEST or ProxyDHCPREQUEST
350 * containing "PXEClient"
352 struct dhcp_settings *pxedhcpack;
353 /** BootServerDHCPACK obtained during BootServerDHCPREQUEST */
354 struct dhcp_settings *bsdhcpack;
355 /** Retransmission timer */
356 struct retry_timer timer;
357 /** Start time of the current state (in ticks) */
364 * @v refcnt Reference counter
366 static void dhcp_free ( struct refcnt *refcnt ) {
367 struct dhcp_session *dhcp =
368 container_of ( refcnt, struct dhcp_session, refcnt );
370 netdev_put ( dhcp->netdev );
371 dhcpset_put ( dhcp->dhcpoffer );
372 dhcpset_put ( dhcp->pxedhcpoffer );
373 dhcpset_put ( dhcp->dhcpack );
374 dhcpset_put ( dhcp->pxedhcpack );
375 dhcpset_put ( dhcp->bsdhcpack );
380 * Mark DHCP session as complete
382 * @v dhcp DHCP session
383 * @v rc Return status code
385 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
387 /* Block futher incoming messages */
388 job_nullify ( &dhcp->job );
389 xfer_nullify ( &dhcp->xfer );
391 /* Stop retry timer */
392 stop_timer ( &dhcp->timer );
394 /* Free resources and close interfaces */
395 xfer_close ( &dhcp->xfer, rc );
396 job_done ( &dhcp->job, rc );
399 /****************************************************************************
401 * Data transfer interface
406 * Create a DHCP packet
408 * @v dhcppkt DHCP packet structure to fill in
409 * @v netdev Network device
410 * @v msgtype DHCP message type
411 * @v options Initial options to include (or NULL)
412 * @v data Buffer for DHCP packet
413 * @v max_len Size of DHCP packet buffer
414 * @ret rc Return status code
416 * Creates a DHCP packet in the specified buffer, and fills out a @c
417 * dhcp_packet structure.
419 int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
420 struct net_device *netdev, uint8_t msgtype,
421 struct dhcp_options *options,
422 void *data, size_t max_len ) {
423 struct dhcphdr *dhcphdr = data;
429 options_len = ( options ? options->len : 0 );
430 if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
433 /* Initialise DHCP packet content */
434 memset ( dhcphdr, 0, max_len );
435 dhcphdr->xid = dhcp_xid ( netdev );
436 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
437 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
438 dhcphdr->op = dhcp_op[msgtype];
439 /* If hardware length exceeds the chaddr field length, don't
440 * use the chaddr field. This is as per RFC4390.
442 hlen = netdev->ll_protocol->ll_addr_len;
443 if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
445 dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
447 dhcphdr->hlen = hlen;
448 memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
449 memcpy ( dhcphdr->options, options->data, options_len );
451 /* Initialise DHCP packet structure */
452 memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
453 dhcppkt_init ( dhcppkt, data, max_len );
455 /* Set DHCP_MESSAGE_TYPE option */
456 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
457 &msgtype, sizeof ( msgtype ) ) ) != 0 )
464 * Create DHCP request packet
466 * @v dhcppkt DHCP packet structure to fill in
467 * @v netdev Network device
468 * @v msgtype DHCP message type
469 * @v ciaddr Client IP address, if applicable
470 * @v server Server identifier, if applicable
471 * @v requested_ip Requested address, if applicable
472 * @v menu_item PXE menu item, if applicable
473 * @v data Buffer for DHCP packet
474 * @v max_len Size of DHCP packet buffer
475 * @ret rc Return status code
477 int dhcp_create_request ( struct dhcp_packet *dhcppkt,
478 struct net_device *netdev, unsigned int msgtype,
479 struct in_addr ciaddr, struct in_addr server,
480 struct in_addr requested_ip,
481 struct dhcp_pxe_boot_menu_item *menu_item,
482 void *data, size_t max_len ) {
483 struct device_description *desc = &netdev->dev->desc;
484 struct dhcp_netdev_desc dhcp_desc;
485 struct dhcp_client_id client_id;
486 struct dhcp_client_uuid client_uuid;
487 size_t dhcp_features_len;
491 /* Create DHCP packet */
492 if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
493 &dhcp_request_options, data,
495 DBG ( "DHCP could not create DHCP packet: %s\n",
500 /* Set client IP address */
501 dhcppkt->dhcphdr->ciaddr = ciaddr;
503 /* Set server ID, if present */
504 if ( server.s_addr &&
505 ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
506 &server, sizeof ( server ) ) ) != 0 ) ) {
507 DBG ( "DHCP could not set server ID: %s\n",
512 /* Set requested IP address, if present */
513 if ( requested_ip.s_addr &&
514 ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
516 sizeof ( requested_ip ) ) ) != 0 ) ) {
517 DBG ( "DHCP could not set requested address: %s\n",
522 /* Add options to identify the feature list */
523 dhcp_features_len = ( dhcp_features_end - dhcp_features );
524 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
525 dhcp_features_len ) ) != 0 ) {
526 DBG ( "DHCP could not set features list option: %s\n",
531 /* Add options to identify the network device */
532 dhcp_desc.type = desc->bus_type;
533 dhcp_desc.vendor = htons ( desc->vendor );
534 dhcp_desc.device = htons ( desc->device );
535 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
536 sizeof ( dhcp_desc ) ) ) != 0 ) {
537 DBG ( "DHCP could not set bus ID option: %s\n",
542 /* Add DHCP client identifier. Required for Infiniband, and
543 * doesn't hurt other link layers.
545 client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
546 ll_addr_len = netdev->ll_protocol->ll_addr_len;
547 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
548 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
549 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
550 ( ll_addr_len + 1 ) ) ) != 0 ) {
551 DBG ( "DHCP could not set client ID: %s\n",
556 /* Add client UUID, if we have one. Required for PXE. */
557 client_uuid.type = DHCP_CLIENT_UUID_TYPE;
558 if ( ( rc = fetch_uuid_setting ( NULL, &uuid_setting,
559 &client_uuid.uuid ) ) >= 0 ) {
560 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
562 sizeof ( client_uuid ) ) ) != 0 ) {
563 DBG ( "DHCP could not set client UUID: %s\n",
569 /* Set PXE boot menu item, if present */
570 if ( menu_item && menu_item->type &&
571 ( ( rc = dhcppkt_store ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
573 sizeof ( *menu_item ) ) ) != 0 ) ) {
574 DBG ( "DHCP could not set PXE menu item: %s\n",
583 * Transmit DHCP request
585 * @v dhcp DHCP session
586 * @ret rc Return status code
588 static int dhcp_tx ( struct dhcp_session *dhcp ) {
589 static struct sockaddr_in dest = {
590 .sin_family = AF_INET,
591 .sin_port = htons ( PXE_PORT ),
593 static struct sockaddr_in src = {
594 .sin_family = AF_INET,
595 .sin_port = htons ( BOOTPC_PORT ),
597 struct xfer_metadata meta = {
598 .netdev = dhcp->netdev,
600 struct io_buffer *iobuf;
601 struct dhcp_packet dhcppkt;
602 struct in_addr ciaddr = { 0 };
603 struct in_addr server = { 0 };
604 struct in_addr requested_ip = { 0 };
605 struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
606 unsigned int msgtype;
609 /* Start retry timer. Do this first so that failures to
610 * transmit will be retried.
612 start_timer ( &dhcp->timer );
614 /* Determine packet contents based on current state */
615 switch ( dhcp->state ) {
616 case DHCP_STATE_DISCOVER:
617 msgtype = DHCPDISCOVER;
619 case DHCP_STATE_REQUEST:
620 assert ( dhcp->dhcpoffer );
621 msgtype = DHCPREQUEST;
622 dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt,
623 DHCP_SERVER_IDENTIFIER, &server,
625 requested_ip = dhcp->dhcpoffer->dhcppkt.dhcphdr->yiaddr;
627 case DHCP_STATE_PROXYREQUEST:
628 assert ( dhcp->dhcpoffer );
629 assert ( dhcp->pxedhcpoffer );
630 assert ( dhcp->dhcpack );
631 msgtype = DHCPREQUEST;
632 ciaddr = dhcp->dhcpoffer->dhcppkt.dhcphdr->yiaddr;
633 dhcppkt_fetch ( &dhcp->pxedhcpoffer->dhcppkt,
634 DHCP_SERVER_IDENTIFIER, &dest.sin_addr,
635 sizeof ( dest.sin_addr ) );
636 meta.dest = ( struct sockaddr * ) &dest;
637 server = dest.sin_addr;
638 assert ( dest.sin_addr.s_addr );
639 assert ( ciaddr.s_addr );
641 case DHCP_STATE_BSREQUEST:
642 assert ( dhcp->dhcpoffer );
643 assert ( dhcp->pxedhcpoffer );
644 assert ( dhcp->dhcpack );
645 assert ( dhcp->pxedhcpack );
646 msgtype = DHCPREQUEST;
647 ciaddr = dhcp->dhcpoffer->dhcppkt.dhcphdr->yiaddr;
648 dhcppkt_fetch ( &dhcp->pxedhcpack->dhcppkt,
649 DHCP_PXE_BOOT_SERVER_MCAST,
650 &dest.sin_addr, sizeof ( dest.sin_addr ) );
651 meta.dest = ( struct sockaddr * ) &dest;
652 dhcppkt_fetch ( &dhcp->pxedhcpack->dhcppkt,
653 DHCP_PXE_BOOT_MENU, &menu_item.type,
654 sizeof ( menu_item.type ) );
655 assert ( dest.sin_addr.s_addr );
656 assert ( menu_item.type );
657 assert ( ciaddr.s_addr );
664 DBGC ( dhcp, "DHCP %p %s", dhcp, dhcp_msgtype_name ( msgtype ) );
666 DBGC ( dhcp, " to %s", inet_ntoa ( server ) );
668 if ( dest.sin_addr.s_addr == server.s_addr ) {
669 DBGC ( dhcp, ":%d (unicast)",
670 ntohs ( dest.sin_port ) );
672 DBGC ( dhcp, " via %s:%d", inet_ntoa ( dest.sin_addr ),
673 ntohs ( dest.sin_port ) );
676 DBGC ( dhcp, " (broadcast)" );
678 if ( requested_ip.s_addr )
679 DBGC ( dhcp, " for %s", inet_ntoa ( requested_ip ) );
680 if ( menu_item.type )
681 DBGC ( dhcp, " for item %04x", ntohs ( menu_item.type ) );
684 /* Allocate buffer for packet */
685 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
689 /* Create DHCP packet in temporary buffer */
690 if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev, msgtype,
691 ciaddr, server, requested_ip,
692 &menu_item, iobuf->data,
693 iob_tailroom ( iobuf ) ) ) != 0 ) {
694 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
695 dhcp, strerror ( rc ) );
699 /* Explicitly specify source address, if available. */
700 if ( ciaddr.s_addr ) {
701 src.sin_addr = ciaddr;
702 meta.src = ( struct sockaddr * ) &src;
705 /* Transmit the packet */
706 iob_put ( iobuf, dhcppkt.len );
707 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
710 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
711 dhcp, strerror ( rc ) );
721 * Transition to new DHCP session state
723 * @v dhcp DHCP session
724 * @v state New session state
726 static void dhcp_set_state ( struct dhcp_session *dhcp,
727 enum dhcp_session_state state ) {
728 DBGC ( dhcp, "DHCP %p entering %s state\n",
729 dhcp, dhcp_state_name ( state ) );
731 dhcp->start = currticks();
732 dhcp->timer.min_timeout = 0;
733 start_timer_nodelay ( &dhcp->timer );
737 * Transition to next DHCP state
739 * @v dhcp DHCP session
741 static void dhcp_next_state ( struct dhcp_session *dhcp ) {
742 struct in_addr bs_mcast = { 0 };
744 switch ( dhcp->state ) {
745 case DHCP_STATE_DISCOVER:
746 dhcp_set_state ( dhcp, DHCP_STATE_REQUEST );
748 case DHCP_STATE_REQUEST:
749 if ( dhcp->pxedhcpoffer ) {
750 /* Store DHCPACK as PXEDHCPACK. This handles
751 * the case in which the DHCP server itself
752 * responds with "PXEClient" and PXE options
753 * but there is no ProxyDHCP server resident
756 dhcp->pxedhcpack = dhcpset_get ( dhcp->dhcpack );
757 dhcp_set_state ( dhcp, DHCP_STATE_PROXYREQUEST );
761 case DHCP_STATE_PROXYREQUEST:
762 if ( dhcp->pxedhcpack ) {
763 dhcppkt_fetch ( &dhcp->pxedhcpack->dhcppkt,
764 DHCP_PXE_BOOT_SERVER_MCAST,
765 &bs_mcast, sizeof ( bs_mcast ) );
766 if ( bs_mcast.s_addr ) {
767 dhcp_set_state ( dhcp, DHCP_STATE_BSREQUEST );
772 case DHCP_STATE_BSREQUEST:
773 dhcp_finished ( dhcp, 0 );
783 * Store received DHCPOFFER
785 * @v dhcp DHCP session
786 * @v dhcpoffer Received DHCPOFFER
787 * @v stored_dhcpoffer Location to store DHCPOFFER
789 * The DHCPOFFER will be stored in place of the existing stored
790 * DHCPOFFER if its priority is equal to or greater than the stored
793 static void dhcp_store_dhcpoffer ( struct dhcp_session *dhcp,
794 struct dhcp_settings *dhcpoffer,
795 struct dhcp_settings **stored_dhcpoffer ) {
796 uint8_t stored_priority = 0;
797 uint8_t priority = 0;
799 /* Get priorities of the two DHCPOFFERs */
800 if ( *stored_dhcpoffer ) {
801 dhcppkt_fetch ( &(*stored_dhcpoffer)->dhcppkt,
802 DHCP_EB_PRIORITY, &stored_priority,
803 sizeof ( stored_priority ) );
805 dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_EB_PRIORITY, &priority,
806 sizeof ( priority ) );
808 /* Replace stored offer only if priority is equal or greater */
809 if ( priority >= stored_priority ) {
810 if ( *stored_dhcpoffer ) {
811 DBGC ( dhcp, "DHCP %p stored DHCPOFFER %p discarded\n",
812 dhcp, *stored_dhcpoffer );
814 DBGC ( dhcp, "DHCP %p DHCPOFFER %p stored\n",
816 dhcpset_put ( *stored_dhcpoffer );
817 *stored_dhcpoffer = dhcpset_get ( dhcpoffer );
822 * Handle received DHCPOFFER
824 * @v dhcp DHCP session
825 * @v dhcpoffer Received DHCPOFFER
827 static void dhcp_rx_dhcpoffer ( struct dhcp_session *dhcp,
828 struct dhcp_settings *dhcpoffer ) {
829 struct in_addr server_id = { 0 };
830 char vci[9]; /* "PXEClient" */
832 uint8_t ignore_pxe = 0;
833 unsigned long elapsed;
835 /* Check for presence of DHCP server ID */
836 if ( dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_SERVER_IDENTIFIER,
837 &server_id, sizeof ( server_id ) )
838 != sizeof ( server_id ) ) {
839 DBGC ( dhcp, "DHCP %p DHCPOFFER %p missing server ID\n",
841 /* Could be a valid BOOTP offer; do not abort processing */
844 /* If there is an IP address, it's a normal DHCPOFFER */
845 if ( dhcpoffer->dhcppkt.dhcphdr->yiaddr.s_addr != 0 ) {
846 DBGC ( dhcp, "DHCP %p DHCPOFFER %p from %s",
847 dhcp, dhcpoffer, inet_ntoa ( server_id ) );
848 DBGC ( dhcp, " has IP %s\n",
849 inet_ntoa ( dhcpoffer->dhcppkt.dhcphdr->yiaddr ) );
850 dhcp_store_dhcpoffer ( dhcp, dhcpoffer, &dhcp->dhcpoffer );
853 /* If there is a "PXEClient" vendor class ID, it's a
854 * PXEDHCPOFFER. Note that it could be both a normal
855 * DHCPOFFER and a PXEDHCPOFFER.
857 len = dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_VENDOR_CLASS_ID,
858 vci, sizeof ( vci ) );
859 if ( ( server_id.s_addr != 0 ) &&
860 ( len >= ( int ) sizeof ( vci ) ) &&
861 ( strncmp ( "PXEClient", vci, sizeof ( vci ) ) == 0 ) ) {
862 DBGC ( dhcp, "DHCP %p DHCPOFFER %p from %s has PXE options\n",
863 dhcp, dhcpoffer, inet_ntoa ( server_id ) );
864 dhcp_store_dhcpoffer ( dhcp, dhcpoffer,
865 &dhcp->pxedhcpoffer );
868 /* We can transition to making the DHCPREQUEST when we have a
869 * valid DHCPOFFER, and either:
871 * o The DHCPOFFER instructs us to ignore PXEDHCPOFFERs, or
872 * o We have a valid PXEDHCPOFFER, or
873 * o We have allowed sufficient time for ProxyDHCPOFFERs.
876 /* If we don't yet have a DHCPOFFER, do nothing */
877 if ( ! dhcp->dhcpoffer )
880 /* If the DHCPOFFER instructs us to ignore PXEDHCP, discard
883 dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt, DHCP_EB_NO_PXEDHCP,
884 &ignore_pxe, sizeof ( ignore_pxe ) );
885 if ( ignore_pxe && dhcp->pxedhcpoffer ) {
886 DBGC ( dhcp, "DHCP %p discarding PXEDHCPOFFER\n", dhcp );
887 dhcpset_put ( dhcp->pxedhcpoffer );
888 dhcp->pxedhcpoffer = NULL;
891 /* If we can't yet transition to DHCPREQUEST, do nothing */
892 elapsed = ( currticks() - dhcp->start );
893 if ( ! ( ignore_pxe || dhcp->pxedhcpoffer ||
894 ( elapsed > PROXYDHCP_WAIT_TIME ) ) )
897 /* Transition to DHCPREQUEST */
898 dhcp_next_state ( dhcp );
902 * Store received DHCPACK
904 * @v dhcp DHCP session
905 * @v dhcpack Received DHCPACK
907 * The DHCPACK will be registered as a settings block.
909 static int dhcp_store_dhcpack ( struct dhcp_session *dhcp,
910 struct dhcp_settings *dhcpack,
911 struct settings *parent ) {
912 struct settings *settings = &dhcpack->settings;
913 struct settings *old_settings;
916 /* Unregister any old settings obtained via DHCP */
917 if ( ( old_settings = find_child_settings ( parent, settings->name ) ))
918 unregister_settings ( old_settings );
920 /* Register new settings */
921 if ( ( rc = register_settings ( settings, parent ) ) != 0 ) {
922 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
923 dhcp, strerror ( rc ) );
924 dhcp_finished ( dhcp, rc ); /* This is a fatal error */
932 * Handle received DHCPACK
934 * @v dhcp DHCP session
935 * @v dhcpack Received DHCPACK
937 static void dhcp_rx_dhcpack ( struct dhcp_session *dhcp,
938 struct dhcp_settings *dhcpack ) {
939 struct settings *parent;
940 struct in_addr offer_server_id = { 0 };
941 struct in_addr ack_server_id = { 0 };
944 /* Verify server ID matches */
945 assert ( dhcp->dhcpoffer != NULL );
946 dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt, DHCP_SERVER_IDENTIFIER,
947 &offer_server_id, sizeof ( offer_server_id ) );
948 dhcppkt_fetch ( &dhcpack->dhcppkt, DHCP_SERVER_IDENTIFIER,
949 &ack_server_id, sizeof ( ack_server_id ) );
950 if ( offer_server_id.s_addr != ack_server_id.s_addr ) {
951 DBGC ( dhcp, "DHCP %p ignoring DHCPACK with wrong server ID "
952 "%s\n", dhcp, inet_ntoa ( ack_server_id ) );
957 assert ( dhcp->dhcpack == NULL );
958 dhcp->dhcpack = dhcpset_get ( dhcpack );
960 /* Register settings */
961 parent = netdev_settings ( dhcp->netdev );
962 if ( ( rc = dhcp_store_dhcpack ( dhcp, dhcpack, parent ) ) != 0 )
965 /* Transition to next state */
966 dhcp_next_state ( dhcp );
970 * Handle received ProxyDHCPACK
972 * @v dhcp DHCP session
973 * @v proxydhcpack Received ProxyDHCPACK
975 static void dhcp_rx_proxydhcpack ( struct dhcp_session *dhcp,
976 struct dhcp_settings *proxydhcpack ) {
977 struct in_addr offer_server_id = { 0 };
978 struct in_addr ack_server_id = { 0 };
981 /* Verify server ID matches, if present */
982 assert ( dhcp->pxedhcpoffer != NULL );
983 if ( ( rc = dhcppkt_fetch ( &proxydhcpack->dhcppkt,
984 DHCP_SERVER_IDENTIFIER, &ack_server_id,
985 sizeof ( ack_server_id ) ) ) > 0 ) {
986 dhcppkt_fetch ( &dhcp->pxedhcpoffer->dhcppkt,
987 DHCP_SERVER_IDENTIFIER, &offer_server_id,
988 sizeof ( offer_server_id ) );
989 if ( offer_server_id.s_addr != ack_server_id.s_addr ) {
990 DBGC ( dhcp, "DHCP %p ignoring ProxyDHCPACK with "
991 "wrong server ID %s\n",
992 dhcp, inet_ntoa ( ack_server_id ) );
997 /* Rename settings */
998 proxydhcpack->settings.name = PROXYDHCP_SETTINGS_NAME;
1000 /* Record ProxyDHCPACK as PXEDHCPACK */
1001 dhcpset_put ( dhcp->pxedhcpack );
1002 dhcp->pxedhcpack = dhcpset_get ( proxydhcpack );
1004 /* Register settings */
1005 if ( ( rc = dhcp_store_dhcpack ( dhcp, proxydhcpack, NULL ) ) != 0 )
1008 /* Transition to next state */
1009 dhcp_next_state ( dhcp );
1013 * Handle received BootServerDHCPACK
1015 * @v dhcp DHCP session
1016 * @v bsdhcpack Received BootServerDHCPACK
1018 static void dhcp_rx_bsdhcpack ( struct dhcp_session *dhcp,
1019 struct dhcp_settings *bsdhcpack ) {
1022 /* Rename settings */
1023 bsdhcpack->settings.name = BSDHCP_SETTINGS_NAME;
1025 /* Record BootServerDHCPACK */
1026 assert ( dhcp->bsdhcpack == NULL );
1027 dhcp->bsdhcpack = dhcpset_get ( bsdhcpack );
1029 /* Register settings */
1030 if ( ( rc = dhcp_store_dhcpack ( dhcp, bsdhcpack, NULL ) ) != 0 )
1033 /* Transition to next state */
1034 dhcp_next_state ( dhcp );
1040 * @v xfer Data transfer interface
1041 * @v iobuf I/O buffer
1042 * @v meta Transfer metadata
1043 * @ret rc Return status code
1045 static int dhcp_deliver_iob ( struct xfer_interface *xfer,
1046 struct io_buffer *iobuf,
1047 struct xfer_metadata *meta ) {
1048 struct dhcp_session *dhcp =
1049 container_of ( xfer, struct dhcp_session, xfer );
1050 struct sockaddr_in *sin_src;
1051 unsigned int src_port;
1052 struct dhcp_settings *dhcpset;
1053 struct dhcphdr *dhcphdr;
1054 uint8_t msgtype = 0;
1059 DBGC ( dhcp, "DHCP %p received packet without metadata\n",
1064 if ( ! meta->src ) {
1065 DBGC ( dhcp, "DHCP %p received packet without source port\n",
1070 sin_src = ( struct sockaddr_in * ) meta->src;
1071 src_port = sin_src->sin_port;
1073 /* Convert packet into a DHCP settings block */
1074 dhcpset = dhcpset_create ( iobuf->data, iob_len ( iobuf ) );
1076 DBGC ( dhcp, "DHCP %p could not store DHCP packet\n", dhcp );
1078 goto err_dhcpset_create;
1080 dhcphdr = dhcpset->dhcppkt.dhcphdr;
1082 /* Identify message type */
1083 dhcppkt_fetch ( &dhcpset->dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
1084 sizeof ( msgtype ) );
1085 DBGC ( dhcp, "DHCP %p %s %p from %s:%d\n", dhcp,
1086 dhcp_msgtype_name ( msgtype ), dhcpset,
1087 inet_ntoa ( sin_src->sin_addr ), ntohs ( src_port ) );
1089 /* Check for matching transaction ID */
1090 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
1091 DBGC ( dhcp, "DHCP %p %s %p has bad transaction ID\n",
1092 dhcp, dhcp_msgtype_name ( msgtype ), dhcpset );
1097 /* Handle packet based on current state */
1098 switch ( dhcp->state ) {
1099 case DHCP_STATE_DISCOVER:
1100 if ( ( ( msgtype == DHCPOFFER ) || ( msgtype == DHCPNONE ) ) &&
1101 ( src_port == htons ( BOOTPS_PORT ) ) )
1102 dhcp_rx_dhcpoffer ( dhcp, dhcpset );
1104 case DHCP_STATE_REQUEST:
1105 if ( ( ( msgtype == DHCPACK ) || ( msgtype == DHCPNONE ) ) &&
1106 ( src_port == htons ( BOOTPS_PORT ) ) )
1107 dhcp_rx_dhcpack ( dhcp, dhcpset );
1109 case DHCP_STATE_PROXYREQUEST:
1110 if ( ( msgtype == DHCPACK ) &&
1111 ( src_port == htons ( PXE_PORT ) ) )
1112 dhcp_rx_proxydhcpack ( dhcp, dhcpset );
1114 case DHCP_STATE_BSREQUEST:
1115 if ( ( msgtype == DHCPACK ) &&
1116 ( src_port == htons ( PXE_PORT ) ) )
1117 dhcp_rx_bsdhcpack ( dhcp, dhcpset );
1125 dhcpset_put ( dhcpset );
1133 /** DHCP data transfer interface operations */
1134 static struct xfer_interface_operations dhcp_xfer_operations = {
1135 .close = ignore_xfer_close,
1136 .vredirect = xfer_vopen,
1137 .window = unlimited_xfer_window,
1138 .alloc_iob = default_xfer_alloc_iob,
1139 .deliver_iob = dhcp_deliver_iob,
1140 .deliver_raw = xfer_deliver_as_iob,
1144 * Handle DHCP retry timer expiry
1146 * @v timer DHCP retry timer
1147 * @v fail Failure indicator
1149 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
1150 struct dhcp_session *dhcp =
1151 container_of ( timer, struct dhcp_session, timer );
1152 unsigned long elapsed = ( currticks() - dhcp->start );
1154 /* If we have failed, terminate DHCP */
1156 dhcp_finished ( dhcp, -ETIMEDOUT );
1160 /* Give up waiting for ProxyDHCP before we reach the failure point */
1161 if ( dhcp->dhcpoffer && ( elapsed > PROXYDHCP_WAIT_TIME ) ) {
1162 dhcp_next_state ( dhcp );
1166 /* Otherwise, retransmit current packet */
1170 /****************************************************************************
1172 * Job control interface
1177 * Handle kill() event received via job control interface
1179 * @v job DHCP job control interface
1181 static void dhcp_job_kill ( struct job_interface *job ) {
1182 struct dhcp_session *dhcp =
1183 container_of ( job, struct dhcp_session, job );
1185 /* Terminate DHCP session */
1186 dhcp_finished ( dhcp, -ECANCELED );
1189 /** DHCP job control interface operations */
1190 static struct job_interface_operations dhcp_job_operations = {
1191 .done = ignore_job_done,
1192 .kill = dhcp_job_kill,
1193 .progress = ignore_job_progress,
1196 /****************************************************************************
1203 * Start DHCP on a network device
1205 * @v job Job control interface
1206 * @v netdev Network device
1207 * @v register_options DHCP option block registration routine
1208 * @ret rc Return status code
1210 * Starts DHCP on the specified network device. If successful, the @c
1211 * register_options() routine will be called with the acquired
1214 int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
1215 static struct sockaddr_in server = {
1216 .sin_family = AF_INET,
1217 .sin_addr.s_addr = INADDR_BROADCAST,
1218 .sin_port = htons ( BOOTPS_PORT ),
1220 static struct sockaddr_in client = {
1221 .sin_family = AF_INET,
1222 .sin_port = htons ( BOOTPC_PORT ),
1224 struct dhcp_session *dhcp;
1227 /* Allocate and initialise structure */
1228 dhcp = zalloc ( sizeof ( *dhcp ) );
1231 dhcp->refcnt.free = dhcp_free;
1232 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1233 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1234 dhcp->netdev = netdev_get ( netdev );
1235 dhcp->timer.expired = dhcp_timer_expired;
1236 dhcp->timer.min_timeout = DHCP_MIN_TIMEOUT;
1237 dhcp->timer.max_timeout = DHCP_MAX_TIMEOUT;
1238 dhcp->start = currticks();
1240 /* Instantiate child objects and attach to our interfaces */
1241 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
1242 ( struct sockaddr * ) &server,
1243 ( struct sockaddr * ) &client ) ) != 0 )
1246 /* Start timer to initiate initial DHCPREQUEST */
1247 start_timer_nodelay ( &dhcp->timer );
1249 /* Attach parent interface, mortalise self, and return */
1250 job_plug_plug ( &dhcp->job, job );
1251 ref_put ( &dhcp->refcnt );
1255 dhcp_finished ( dhcp, rc );
1256 ref_put ( &dhcp->refcnt );