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
129 * Name a DHCP packet type
131 * @v msgtype DHCP message type
132 * @ret string DHCP mesasge type name
134 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
136 case DHCPNONE: return "BOOTP"; /* Non-DHCP packet */
137 case DHCPDISCOVER: return "DHCPDISCOVER";
138 case DHCPOFFER: return "DHCPOFFER";
139 case DHCPREQUEST: return "DHCPREQUEST";
140 case DHCPDECLINE: return "DHCPDECLINE";
141 case DHCPACK: return "DHCPACK";
142 case DHCPNAK: return "DHCPNAK";
143 case DHCPRELEASE: return "DHCPRELEASE";
144 case DHCPINFORM: return "DHCPINFORM";
145 default: return "DHCP<invalid>";
150 * Calculate DHCP transaction ID for a network device
152 * @v netdev Network device
155 * Extract the least significant bits of the hardware address for use
156 * as the transaction ID.
158 static uint32_t dhcp_xid ( struct net_device *netdev ) {
161 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
162 - sizeof ( xid ) ), sizeof ( xid ) );
166 /****************************************************************************
172 /** A DHCP settings block */
173 struct dhcp_settings {
174 /** Reference counter */
175 struct refcnt refcnt;
177 struct dhcp_packet dhcppkt;
178 /** Setting interface */
179 struct settings settings;
183 * Increment reference count on DHCP settings block
185 * @v dhcpset DHCP settings block
186 * @ret dhcpset DHCP settings block
188 static inline __attribute__ (( always_inline )) struct dhcp_settings *
189 dhcpset_get ( struct dhcp_settings *dhcpset ) {
190 ref_get ( &dhcpset->refcnt );
195 * Decrement reference count on DHCP settings block
197 * @v dhcpset DHCP settings block
199 static inline __attribute__ (( always_inline )) void
200 dhcpset_put ( struct dhcp_settings *dhcpset ) {
201 ref_put ( &dhcpset->refcnt );
205 * Store value of DHCP setting
207 * @v settings Settings block
208 * @v setting Setting to store
209 * @v data Setting data, or NULL to clear setting
210 * @v len Length of setting data
211 * @ret rc Return status code
213 static int dhcpset_store ( struct settings *settings, struct setting *setting,
214 const void *data, size_t len ) {
215 struct dhcp_settings *dhcpset =
216 container_of ( settings, struct dhcp_settings, settings );
218 return dhcppkt_store ( &dhcpset->dhcppkt, setting->tag, data, len );
222 * Fetch value of DHCP setting
224 * @v settings Settings block, or NULL to search all blocks
225 * @v setting Setting to fetch
226 * @v data Buffer to fill with setting data
227 * @v len Length of buffer
228 * @ret len Length of setting data, or negative error
230 static int dhcpset_fetch ( struct settings *settings, struct setting *setting,
231 void *data, size_t len ) {
232 struct dhcp_settings *dhcpset =
233 container_of ( settings, struct dhcp_settings, settings );
235 return dhcppkt_fetch ( &dhcpset->dhcppkt, setting->tag, data, len );
238 /** DHCP settings operations */
239 static struct settings_operations dhcpset_settings_operations = {
240 .store = dhcpset_store,
241 .fetch = dhcpset_fetch,
245 * Create DHCP setting block
247 * @v dhcphdr DHCP packet
248 * @v len Length of DHCP packet
249 * @ret dhcpset DHCP settings block
251 static struct dhcp_settings * dhcpset_create ( const struct dhcphdr *dhcphdr,
253 struct dhcp_settings *dhcpset;
256 dhcpset = zalloc ( sizeof ( *dhcpset ) + len );
258 data = ( ( ( void * ) dhcpset ) + sizeof ( *dhcpset ) );
259 memcpy ( data, dhcphdr, len );
260 dhcppkt_init ( &dhcpset->dhcppkt, data, len );
261 settings_init ( &dhcpset->settings,
262 &dhcpset_settings_operations, &dhcpset->refcnt,
263 DHCP_SETTINGS_NAME, 0 );
268 /** DHCP server address setting */
269 struct setting dhcp_server_setting __setting = {
270 .name = "dhcp-server",
271 .description = "DHCP server address",
272 .tag = DHCP_SERVER_IDENTIFIER,
273 .type = &setting_type_ipv4,
276 /****************************************************************************
282 /** DHCP session states */
283 enum dhcp_session_state {
284 /** Sending DHCPDISCOVERs, collecting DHCPOFFERs and ProxyDHCPOFFERs */
285 DHCP_STATE_DISCOVER = 0,
286 /** Sending DHCPREQUESTs, waiting for DHCPACK */
288 /** Sending ProxyDHCPREQUESTs, waiting for ProxyDHCPACK */
289 DHCP_STATE_PROXYREQUEST,
290 /** Sending BootServerDHCPREQUESTs, waiting for BootServerDHCPACK */
291 DHCP_STATE_BSREQUEST,
295 * Name a DHCP session state
297 * @v state DHCP session state
298 * @ret string DHCP session state name
300 static inline const char * dhcp_state_name ( enum dhcp_session_state state ) {
302 case DHCP_STATE_DISCOVER: return "DHCPDISCOVER";
303 case DHCP_STATE_REQUEST: return "DHCPREQUEST";
304 case DHCP_STATE_PROXYREQUEST: return "ProxyDHCPREQUEST";
305 case DHCP_STATE_BSREQUEST: return "BootServerREQUEST";
306 default: return "<invalid>";
310 /** A DHCP session */
311 struct dhcp_session {
312 /** Reference counter */
313 struct refcnt refcnt;
314 /** Job control interface */
315 struct job_interface job;
316 /** Data transfer interface */
317 struct xfer_interface xfer;
319 /** Network device being configured */
320 struct net_device *netdev;
322 /** State of the session
324 * This is a value for the @c DHCP_MESSAGE_TYPE option
325 * (e.g. @c DHCPDISCOVER).
327 enum dhcp_session_state state;
328 /** DHCPOFFER obtained during DHCPDISCOVER */
329 struct dhcp_settings *dhcpoffer;
330 /** ProxyDHCPOFFER obtained during DHCPDISCOVER */
331 struct dhcp_settings *proxydhcpoffer;
332 /** DHCPACK obtained during DHCPREQUEST */
333 struct dhcp_settings *dhcpack;
334 /** ProxyDHCPACK obtained during ProxyDHCPREQUEST */
335 struct dhcp_settings *proxydhcpack;
336 /** BootServerDHCPACK obtained during BootServerDHCPREQUEST */
337 struct dhcp_settings *bsdhcpack;
338 /** Retransmission timer */
339 struct retry_timer timer;
340 /** Start time of the current state (in ticks) */
347 * @v refcnt Reference counter
349 static void dhcp_free ( struct refcnt *refcnt ) {
350 struct dhcp_session *dhcp =
351 container_of ( refcnt, struct dhcp_session, refcnt );
353 netdev_put ( dhcp->netdev );
354 dhcpset_put ( dhcp->dhcpoffer );
355 dhcpset_put ( dhcp->proxydhcpoffer );
356 dhcpset_put ( dhcp->dhcpack );
357 dhcpset_put ( dhcp->proxydhcpack );
358 dhcpset_put ( dhcp->bsdhcpack );
363 * Mark DHCP session as complete
365 * @v dhcp DHCP session
366 * @v rc Return status code
368 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
370 /* Block futher incoming messages */
371 job_nullify ( &dhcp->job );
372 xfer_nullify ( &dhcp->xfer );
374 /* Stop retry timer */
375 stop_timer ( &dhcp->timer );
377 /* Free resources and close interfaces */
378 xfer_close ( &dhcp->xfer, rc );
379 job_done ( &dhcp->job, rc );
382 /****************************************************************************
384 * Data transfer interface
389 * Create a DHCP packet
391 * @v dhcppkt DHCP packet structure to fill in
392 * @v netdev Network device
393 * @v msgtype DHCP message type
394 * @v options Initial options to include (or NULL)
395 * @v data Buffer for DHCP packet
396 * @v max_len Size of DHCP packet buffer
397 * @ret rc Return status code
399 * Creates a DHCP packet in the specified buffer, and fills out a @c
400 * dhcp_packet structure.
402 int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
403 struct net_device *netdev, uint8_t msgtype,
404 struct dhcp_options *options,
405 void *data, size_t max_len ) {
406 struct dhcphdr *dhcphdr = data;
412 options_len = ( options ? options->len : 0 );
413 if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
416 /* Initialise DHCP packet content */
417 memset ( dhcphdr, 0, max_len );
418 dhcphdr->xid = dhcp_xid ( netdev );
419 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
420 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
421 dhcphdr->op = dhcp_op[msgtype];
422 /* If hardware length exceeds the chaddr field length, don't
423 * use the chaddr field. This is as per RFC4390.
425 hlen = netdev->ll_protocol->ll_addr_len;
426 if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
428 dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
430 dhcphdr->hlen = hlen;
431 memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
432 memcpy ( dhcphdr->options, options->data, options_len );
434 /* Initialise DHCP packet structure */
435 memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
436 dhcppkt_init ( dhcppkt, data, max_len );
438 /* Set DHCP_MESSAGE_TYPE option */
439 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
440 &msgtype, sizeof ( msgtype ) ) ) != 0 )
447 * Create DHCP request packet
449 * @v dhcppkt DHCP packet structure to fill in
450 * @v netdev Network device
451 * @v ciaddr Client IP address
452 * @v offer DHCP offer, if applicable
453 * @v data Buffer for DHCP packet
454 * @v max_len Size of DHCP packet buffer
455 * @ret rc Return status code
457 int dhcp_create_request ( struct dhcp_packet *dhcppkt,
458 struct net_device *netdev, struct in_addr ciaddr,
459 struct dhcp_packet *offer,
460 void *data, size_t max_len ) {
461 struct device_description *desc = &netdev->dev->desc;
462 struct dhcp_netdev_desc dhcp_desc;
463 struct dhcp_client_id client_id;
464 struct dhcp_client_uuid client_uuid;
465 unsigned int msgtype;
466 size_t dhcp_features_len;
470 /* Create DHCP packet */
471 msgtype = ( offer ? DHCPREQUEST : DHCPDISCOVER );
472 if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
473 &dhcp_request_options, data,
475 DBG ( "DHCP could not create DHCP packet: %s\n",
480 /* Set client IP address */
481 dhcppkt->dhcphdr->ciaddr = ciaddr;
483 /* Copy any required options from previous server repsonse */
485 struct in_addr server = { 0 };
486 struct in_addr *ip = &offer->dhcphdr->yiaddr;
488 /* Copy server identifier, if present */
489 if ( ( dhcppkt_fetch ( offer, DHCP_SERVER_IDENTIFIER, &server,
490 sizeof ( server ) ) >= 0 ) &&
491 ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
493 sizeof ( server ) ) ) != 0 ) ) {
494 DBG ( "DHCP could not set server ID: %s\n",
499 /* Copy requested IP address, if present */
500 if ( ( ip->s_addr != 0 ) &&
501 ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
502 ip, sizeof ( *ip ) ) ) != 0 ) ) {
503 DBG ( "DHCP could not set requested address: %s\n",
509 /* Add options to identify the feature list */
510 dhcp_features_len = ( dhcp_features_end - dhcp_features );
511 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
512 dhcp_features_len ) ) != 0 ) {
513 DBG ( "DHCP could not set features list option: %s\n",
518 /* Add options to identify the network device */
519 dhcp_desc.type = desc->bus_type;
520 dhcp_desc.vendor = htons ( desc->vendor );
521 dhcp_desc.device = htons ( desc->device );
522 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
523 sizeof ( dhcp_desc ) ) ) != 0 ) {
524 DBG ( "DHCP could not set bus ID option: %s\n",
529 /* Add DHCP client identifier. Required for Infiniband, and
530 * doesn't hurt other link layers.
532 client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
533 ll_addr_len = netdev->ll_protocol->ll_addr_len;
534 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
535 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
536 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
537 ( ll_addr_len + 1 ) ) ) != 0 ) {
538 DBG ( "DHCP could not set client ID: %s\n",
543 /* Add client UUID, if we have one. Required for PXE. */
544 client_uuid.type = DHCP_CLIENT_UUID_TYPE;
545 if ( ( rc = fetch_uuid_setting ( NULL, &uuid_setting,
546 &client_uuid.uuid ) ) >= 0 ) {
547 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
549 sizeof ( client_uuid ) ) ) != 0 ) {
550 DBG ( "DHCP could not set client UUID: %s\n",
560 * Transmit DHCP request
562 * @v dhcp DHCP session
563 * @ret rc Return status code
565 static int dhcp_tx ( struct dhcp_session *dhcp ) {
566 static struct sockaddr_in proxydhcp_server = {
567 .sin_family = AF_INET,
568 .sin_port = htons ( PROXYDHCP_PORT ),
570 static struct sockaddr_in client = {
571 .sin_family = AF_INET,
572 .sin_port = htons ( BOOTPC_PORT ),
574 struct xfer_metadata meta = {
575 .netdev = dhcp->netdev,
577 struct io_buffer *iobuf;
578 struct dhcp_packet dhcppkt;
579 struct dhcp_packet *offer = NULL;
580 struct in_addr ciaddr = { 0 };
584 /* Start retry timer. Do this first so that failures to
585 * transmit will be retried.
587 start_timer ( &dhcp->timer );
589 /* Determine packet contents based on current state */
590 switch ( dhcp->state ) {
591 case DHCP_STATE_DISCOVER:
592 DBGC ( dhcp, "DHCP %p transmitting DHCPDISCOVER\n", dhcp );
594 case DHCP_STATE_REQUEST:
595 DBGC ( dhcp, "DHCP %p transmitting DHCPREQUEST\n", dhcp );
596 assert ( dhcp->dhcpoffer );
597 offer = &dhcp->dhcpoffer->dhcppkt;
599 case DHCP_STATE_PROXYREQUEST:
600 DBGC ( dhcp, "DHCP %p transmitting ProxyDHCPREQUEST\n", dhcp );
601 assert ( dhcp->dhcpoffer );
602 assert ( dhcp->proxydhcpoffer );
603 assert ( dhcp->dhcpack );
604 offer = &dhcp->proxydhcpoffer->dhcppkt;
605 ciaddr = dhcp->dhcpoffer->dhcppkt.dhcphdr->yiaddr;
606 check_len = dhcppkt_fetch ( offer, DHCP_SERVER_IDENTIFIER,
607 &proxydhcp_server.sin_addr,
608 sizeof(proxydhcp_server.sin_addr));
609 meta.dest = ( struct sockaddr * ) &proxydhcp_server;
610 assert ( ciaddr.s_addr != 0 );
611 assert ( proxydhcp_server.sin_addr.s_addr != 0 );
612 assert ( check_len == sizeof ( proxydhcp_server.sin_addr ) );
614 case DHCP_STATE_BSREQUEST:
615 DBGC ( dhcp, "DHCP %p transmitting BootServerREQUEST\n",
617 assert ( dhcp->dhcpoffer );
618 assert ( dhcp->proxydhcpoffer );
619 assert ( dhcp->dhcpack );
620 assert ( dhcp->proxydhcpack );
621 offer = &dhcp->proxydhcpoffer->dhcppkt;
622 ciaddr = dhcp->dhcpoffer->dhcppkt.dhcphdr->yiaddr;
623 check_len = dhcppkt_fetch ( &dhcp->proxydhcpack->dhcppkt,
624 DHCP_PXE_BOOT_SERVER_MCAST,
625 &proxydhcp_server.sin_addr,
626 sizeof(proxydhcp_server.sin_addr));
627 meta.dest = ( struct sockaddr * ) &proxydhcp_server;
628 assert ( ciaddr.s_addr != 0 );
629 assert ( proxydhcp_server.sin_addr.s_addr != 0 );
630 assert ( check_len == sizeof ( proxydhcp_server.sin_addr ) );
637 /* Allocate buffer for packet */
638 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
642 /* Create DHCP packet in temporary buffer */
643 if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev,
644 ciaddr, offer, iobuf->data,
645 iob_tailroom ( iobuf ) ) ) != 0 ) {
646 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
647 dhcp, strerror ( rc ) );
651 /* Explicitly specify source address, if available. */
652 if ( ciaddr.s_addr ) {
653 client.sin_addr = ciaddr;
654 meta.src = ( struct sockaddr * ) &client;
657 /* Transmit the packet */
658 iob_put ( iobuf, dhcppkt.len );
659 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
662 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
663 dhcp, strerror ( rc ) );
673 * Transition to new DHCP session state
675 * @v dhcp DHCP session
676 * @v state New session state
678 static void dhcp_set_state ( struct dhcp_session *dhcp,
679 enum dhcp_session_state state ) {
680 DBGC ( dhcp, "DHCP %p entering %s state\n",
681 dhcp, dhcp_state_name ( state ) );
683 dhcp->start = currticks();
684 dhcp->timer.min_timeout = 0;
685 start_timer_nodelay ( &dhcp->timer );
689 * Transition to next DHCP state
691 * @v dhcp DHCP session
693 static void dhcp_next_state ( struct dhcp_session *dhcp ) {
694 struct in_addr bs_mcast = { 0 };
696 switch ( dhcp->state ) {
697 case DHCP_STATE_DISCOVER:
698 dhcp_set_state ( dhcp, DHCP_STATE_REQUEST );
700 case DHCP_STATE_REQUEST:
701 if ( dhcp->proxydhcpoffer ) {
702 dhcp_set_state ( dhcp, DHCP_STATE_PROXYREQUEST );
706 case DHCP_STATE_PROXYREQUEST:
707 if ( dhcp->proxydhcpack ) {
708 dhcppkt_fetch ( &dhcp->proxydhcpack->dhcppkt,
709 DHCP_PXE_BOOT_SERVER_MCAST,
710 &bs_mcast, sizeof ( bs_mcast ) );
711 if ( bs_mcast.s_addr ) {
712 dhcp_set_state ( dhcp, DHCP_STATE_BSREQUEST );
717 case DHCP_STATE_BSREQUEST:
718 dhcp_finished ( dhcp, 0 );
728 * Store received DHCPOFFER
730 * @v dhcp DHCP session
731 * @v dhcpoffer Received DHCPOFFER
732 * @v stored_dhcpoffer Location to store DHCPOFFER
734 * The DHCPOFFER will be stored in place of the existing stored
735 * DHCPOFFER if its priority is equal to or greater than the stored
738 static void dhcp_store_dhcpoffer ( struct dhcp_session *dhcp,
739 struct dhcp_settings *dhcpoffer,
740 struct dhcp_settings **stored_dhcpoffer ) {
741 uint8_t stored_priority = 0;
742 uint8_t priority = 0;
744 /* Get priorities of the two DHCPOFFERs */
745 if ( *stored_dhcpoffer ) {
746 dhcppkt_fetch ( &(*stored_dhcpoffer)->dhcppkt,
747 DHCP_EB_PRIORITY, &stored_priority,
748 sizeof ( stored_priority ) );
750 dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_EB_PRIORITY, &priority,
751 sizeof ( priority ) );
753 /* Replace stored offer only if priority is equal or greater */
754 if ( priority >= stored_priority ) {
755 if ( *stored_dhcpoffer ) {
756 DBGC ( dhcp, "DHCP %p stored DHCPOFFER %p discarded\n",
757 dhcp, *stored_dhcpoffer );
759 DBGC ( dhcp, "DHCP %p received DHCPOFFER %p stored\n",
761 dhcpset_put ( *stored_dhcpoffer );
762 *stored_dhcpoffer = dhcpset_get ( dhcpoffer );
767 * Handle received DHCPOFFER
769 * @v dhcp DHCP session
770 * @v dhcpoffer Received DHCPOFFER
772 static void dhcp_rx_dhcpoffer ( struct dhcp_session *dhcp,
773 struct dhcp_settings *dhcpoffer ) {
774 struct in_addr server_id = { 0 };
775 char vci[9]; /* "PXEClient" */
777 uint8_t ignore_proxy = 0;
778 unsigned long elapsed;
780 /* Check for presence of DHCP server ID */
781 if ( dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_SERVER_IDENTIFIER,
782 &server_id, sizeof ( server_id ) )
783 != sizeof ( server_id ) ) {
784 DBGC ( dhcp, "DHCP %p received DHCPOFFER %p missing server "
785 "identifier\n", dhcp, dhcpoffer );
786 /* Could be a valid BOOTP offer; do not abort processing */
789 /* If there is an IP address, it's a normal DHCPOFFER */
790 if ( dhcpoffer->dhcppkt.dhcphdr->yiaddr.s_addr != 0 ) {
791 DBGC ( dhcp, "DHCP %p received DHCPOFFER %p from %s has IP "
793 dhcp, dhcpoffer, inet_ntoa ( server_id ) );
794 dhcp_store_dhcpoffer ( dhcp, dhcpoffer, &dhcp->dhcpoffer );
797 /* If there is a "PXEClient" vendor class ID, it's a
798 * ProxyDHCPOFFER. Note that it could be both a normal
799 * DHCPOFFER and a ProxyDHCPOFFER.
801 len = dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_VENDOR_CLASS_ID,
802 vci, sizeof ( vci ) );
803 if ( ( server_id.s_addr != 0 ) &&
804 ( len >= ( int ) sizeof ( vci ) ) &&
805 ( strncmp ( "PXEClient", vci, sizeof ( vci ) ) == 0 ) ) {
806 DBGC ( dhcp, "DHCP %p received DHCPOFFER %p from %s is a "
808 dhcp, dhcpoffer, inet_ntoa ( server_id ) );
809 dhcp_store_dhcpoffer ( dhcp, dhcpoffer,
810 &dhcp->proxydhcpoffer );
813 /* We can transition to making the DHCPREQUEST when we have a
814 * valid DHCPOFFER, and either:
816 * o The DHCPOFFER instructs us to not wait for ProxyDHCP, or
817 * o We have a valid ProxyDHCPOFFER, or
818 * o We have allowed sufficient time for ProxyDHCPOFFERs.
821 /* If we don't yet have a DHCPOFFER, do nothing */
822 if ( ! dhcp->dhcpoffer )
825 /* If the DHCPOFFER instructs us to ignore ProxyDHCP, discard
828 dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt, DHCP_EB_NO_PROXYDHCP,
829 &ignore_proxy, sizeof ( ignore_proxy ) );
830 if ( ignore_proxy && dhcp->proxydhcpoffer ) {
831 DBGC ( dhcp, "DHCP %p discarding ProxyDHCPOFFER\n", dhcp );
832 dhcpset_put ( dhcp->proxydhcpoffer );
833 dhcp->proxydhcpoffer = NULL;
836 /* If we can't yet transition to DHCPREQUEST, do nothing */
837 elapsed = ( currticks() - dhcp->start );
838 if ( ! ( ignore_proxy || dhcp->proxydhcpoffer ||
839 ( elapsed > PROXYDHCP_WAIT_TIME ) ) )
842 /* Transition to DHCPREQUEST */
843 dhcp_next_state ( dhcp );
847 * Store received DHCPACK
849 * @v dhcp DHCP session
850 * @v dhcpack Received DHCPACK
852 * The DHCPACK will be registered as a settings block.
854 static int dhcp_store_dhcpack ( struct dhcp_session *dhcp,
855 struct dhcp_settings *dhcpack,
856 struct settings *parent ) {
857 struct settings *settings = &dhcpack->settings;
858 struct settings *old_settings;
861 /* Unregister any old settings obtained via DHCP */
862 if ( ( old_settings = find_child_settings ( parent, settings->name ) ))
863 unregister_settings ( old_settings );
865 /* Register new settings */
866 if ( ( rc = register_settings ( settings, parent ) ) != 0 ) {
867 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
868 dhcp, strerror ( rc ) );
869 dhcp_finished ( dhcp, rc ); /* This is a fatal error */
877 * Handle received DHCPACK
879 * @v dhcp DHCP session
880 * @v dhcpack Received DHCPACK
882 static void dhcp_rx_dhcpack ( struct dhcp_session *dhcp,
883 struct dhcp_settings *dhcpack ) {
884 struct settings *parent;
885 struct in_addr offer_server_id = { 0 };
886 struct in_addr ack_server_id = { 0 };
889 /* Verify server ID matches */
890 assert ( dhcp->dhcpoffer != NULL );
891 dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt, DHCP_SERVER_IDENTIFIER,
892 &offer_server_id, sizeof ( offer_server_id ) );
893 dhcppkt_fetch ( &dhcpack->dhcppkt, DHCP_SERVER_IDENTIFIER,
894 &ack_server_id, sizeof ( ack_server_id ) );
895 if ( offer_server_id.s_addr != ack_server_id.s_addr ) {
896 DBGC ( dhcp, "DHCP %p ignoring DHCPACK with wrong server ID "
897 "%s\n", dhcp, inet_ntoa ( ack_server_id ) );
902 assert ( dhcp->dhcpack == NULL );
903 dhcp->dhcpack = dhcpset_get ( dhcpack );
905 /* Register settings */
906 parent = netdev_settings ( dhcp->netdev );
907 if ( ( rc = dhcp_store_dhcpack ( dhcp, dhcpack, parent ) ) != 0 )
910 /* Transition to next state */
911 dhcp_next_state ( dhcp );
915 * Handle received ProxyDHCPACK
917 * @v dhcp DHCP session
918 * @v proxydhcpack Received ProxyDHCPACK
920 static void dhcp_rx_proxydhcpack ( struct dhcp_session *dhcp,
921 struct dhcp_settings *proxydhcpack ) {
922 struct in_addr offer_server_id = { 0 };
923 struct in_addr ack_server_id = { 0 };
926 /* Verify server ID matches, if present */
927 assert ( dhcp->proxydhcpoffer != NULL );
928 if ( ( rc = dhcppkt_fetch ( &proxydhcpack->dhcppkt,
929 DHCP_SERVER_IDENTIFIER, &ack_server_id,
930 sizeof ( ack_server_id ) ) ) > 0 ) {
931 dhcppkt_fetch ( &dhcp->proxydhcpoffer->dhcppkt,
932 DHCP_SERVER_IDENTIFIER, &offer_server_id,
933 sizeof ( offer_server_id ) );
934 if ( offer_server_id.s_addr != ack_server_id.s_addr ) {
935 DBGC ( dhcp, "DHCP %p ignoring ProxyDHCPACK with "
936 "wrong server ID %s\n",
937 dhcp, inet_ntoa ( ack_server_id ) );
942 /* Rename settings */
943 proxydhcpack->settings.name = PROXYDHCP_SETTINGS_NAME;
945 /* Record ProxyDHCPACK */
946 assert ( dhcp->proxydhcpack == NULL );
947 dhcp->proxydhcpack = dhcpset_get ( proxydhcpack );
949 /* Register settings */
950 if ( ( rc = dhcp_store_dhcpack ( dhcp, proxydhcpack, NULL ) ) != 0 )
953 /* Transition to next state */
954 dhcp_next_state ( dhcp );
958 * Handle received BootServerDHCPACK
960 * @v dhcp DHCP session
961 * @v bsdhcpack Received BootServerDHCPACK
963 static void dhcp_rx_bsdhcpack ( struct dhcp_session *dhcp,
964 struct dhcp_settings *bsdhcpack ) {
967 /* Rename settings */
968 bsdhcpack->settings.name = BSDHCP_SETTINGS_NAME;
970 /* Record ProxyDHCPACK */
971 assert ( dhcp->bsdhcpack == NULL );
972 dhcp->bsdhcpack = dhcpset_get ( bsdhcpack );
974 /* Register settings */
975 if ( ( rc = dhcp_store_dhcpack ( dhcp, bsdhcpack, NULL ) ) != 0 )
978 /* Transition to next state */
979 dhcp_next_state ( dhcp );
985 * @v xfer Data transfer interface
986 * @v iobuf I/O buffer
987 * @v meta Transfer metadata
988 * @ret rc Return status code
990 static int dhcp_deliver_iob ( struct xfer_interface *xfer,
991 struct io_buffer *iobuf,
992 struct xfer_metadata *meta ) {
993 struct dhcp_session *dhcp =
994 container_of ( xfer, struct dhcp_session, xfer );
995 struct sockaddr_tcpip *st_src;
996 unsigned int src_port;
997 struct dhcp_settings *dhcpset;
998 struct dhcphdr *dhcphdr;
1004 DBGC ( dhcp, "DHCP %p received packet without metadata\n",
1009 if ( ! meta->src ) {
1010 DBGC ( dhcp, "DHCP %p received packet without source port\n",
1015 st_src = ( struct sockaddr_tcpip * ) meta->src;
1016 src_port = st_src->st_port;
1018 /* Convert packet into a DHCP settings block */
1019 dhcpset = dhcpset_create ( iobuf->data, iob_len ( iobuf ) );
1021 DBGC ( dhcp, "DHCP %p could not store DHCP packet\n", dhcp );
1023 goto err_dhcpset_create;
1025 dhcphdr = dhcpset->dhcppkt.dhcphdr;
1027 /* Identify message type */
1028 dhcppkt_fetch ( &dhcpset->dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
1029 sizeof ( msgtype ) );
1030 DBGC ( dhcp, "DHCP %p received %s %p from port %d\n", dhcp,
1031 dhcp_msgtype_name ( msgtype ), dhcpset, ntohs ( src_port ) );
1033 /* Check for matching transaction ID */
1034 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
1035 DBGC ( dhcp, "DHCP %p received %s %p has bad transaction ID\n",
1036 dhcp, dhcp_msgtype_name ( msgtype ), dhcpset );
1041 /* Handle packet based on current state */
1042 switch ( dhcp->state ) {
1043 case DHCP_STATE_DISCOVER:
1044 if ( ( ( msgtype == DHCPOFFER ) || ( msgtype == DHCPNONE ) ) &&
1045 ( src_port == htons ( BOOTPS_PORT ) ) )
1046 dhcp_rx_dhcpoffer ( dhcp, dhcpset );
1048 case DHCP_STATE_REQUEST:
1049 if ( ( ( msgtype == DHCPACK ) || ( msgtype == DHCPNONE ) ) &&
1050 ( src_port == htons ( BOOTPS_PORT ) ) )
1051 dhcp_rx_dhcpack ( dhcp, dhcpset );
1053 case DHCP_STATE_PROXYREQUEST:
1054 if ( ( msgtype == DHCPACK ) &&
1055 ( src_port == htons ( PROXYDHCP_PORT ) ) )
1056 dhcp_rx_proxydhcpack ( dhcp, dhcpset );
1058 case DHCP_STATE_BSREQUEST:
1059 if ( ( msgtype == DHCPACK ) &&
1060 ( src_port == htons ( PROXYDHCP_PORT ) ) )
1061 dhcp_rx_bsdhcpack ( dhcp, dhcpset );
1069 dhcpset_put ( dhcpset );
1077 /** DHCP data transfer interface operations */
1078 static struct xfer_interface_operations dhcp_xfer_operations = {
1079 .close = ignore_xfer_close,
1080 .vredirect = xfer_vopen,
1081 .window = unlimited_xfer_window,
1082 .alloc_iob = default_xfer_alloc_iob,
1083 .deliver_iob = dhcp_deliver_iob,
1084 .deliver_raw = xfer_deliver_as_iob,
1088 * Handle DHCP retry timer expiry
1090 * @v timer DHCP retry timer
1091 * @v fail Failure indicator
1093 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
1094 struct dhcp_session *dhcp =
1095 container_of ( timer, struct dhcp_session, timer );
1096 unsigned long elapsed = ( currticks() - dhcp->start );
1098 /* If we have failed, terminate DHCP */
1100 dhcp_finished ( dhcp, -ETIMEDOUT );
1104 /* Give up waiting for ProxyDHCP before we reach the failure point */
1105 if ( dhcp->dhcpoffer && ( elapsed > PROXYDHCP_WAIT_TIME ) ) {
1106 dhcp_next_state ( dhcp );
1110 /* Otherwise, retransmit current packet */
1114 /****************************************************************************
1116 * Job control interface
1121 * Handle kill() event received via job control interface
1123 * @v job DHCP job control interface
1125 static void dhcp_job_kill ( struct job_interface *job ) {
1126 struct dhcp_session *dhcp =
1127 container_of ( job, struct dhcp_session, job );
1129 /* Terminate DHCP session */
1130 dhcp_finished ( dhcp, -ECANCELED );
1133 /** DHCP job control interface operations */
1134 static struct job_interface_operations dhcp_job_operations = {
1135 .done = ignore_job_done,
1136 .kill = dhcp_job_kill,
1137 .progress = ignore_job_progress,
1140 /****************************************************************************
1147 * Start DHCP on a network device
1149 * @v job Job control interface
1150 * @v netdev Network device
1151 * @v register_options DHCP option block registration routine
1152 * @ret rc Return status code
1154 * Starts DHCP on the specified network device. If successful, the @c
1155 * register_options() routine will be called with the acquired
1158 int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
1159 static struct sockaddr_in server = {
1160 .sin_family = AF_INET,
1161 .sin_addr.s_addr = INADDR_BROADCAST,
1162 .sin_port = htons ( BOOTPS_PORT ),
1164 static struct sockaddr_in client = {
1165 .sin_family = AF_INET,
1166 .sin_port = htons ( BOOTPC_PORT ),
1168 struct dhcp_session *dhcp;
1171 /* Allocate and initialise structure */
1172 dhcp = zalloc ( sizeof ( *dhcp ) );
1175 dhcp->refcnt.free = dhcp_free;
1176 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1177 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1178 dhcp->netdev = netdev_get ( netdev );
1179 dhcp->timer.expired = dhcp_timer_expired;
1180 dhcp->timer.min_timeout = DHCP_MIN_TIMEOUT;
1181 dhcp->timer.max_timeout = DHCP_MAX_TIMEOUT;
1182 dhcp->start = currticks();
1184 /* Instantiate child objects and attach to our interfaces */
1185 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
1186 ( struct sockaddr * ) &server,
1187 ( struct sockaddr * ) &client ) ) != 0 )
1190 /* Start timer to initiate initial DHCPREQUEST */
1191 start_timer_nodelay ( &dhcp->timer );
1193 /* Attach parent interface, mortalise self, and return */
1194 job_plug_plug ( &dhcp->job, job );
1195 ref_put ( &dhcp->refcnt );
1199 dhcp_finished ( dhcp, rc );
1200 ref_put ( &dhcp->refcnt );