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,
293 * Name a DHCP session state
295 * @v state DHCP session state
296 * @ret string DHCP session state name
298 static inline const char * dhcp_state_name ( enum dhcp_session_state state ) {
300 case DHCP_STATE_DISCOVER: return "DHCPDISCOVER";
301 case DHCP_STATE_REQUEST: return "DHCPREQUEST";
302 case DHCP_STATE_PROXYREQUEST: return "ProxyDHCPREQUEST";
303 default: return "<invalid>";
307 /** A DHCP session */
308 struct dhcp_session {
309 /** Reference counter */
310 struct refcnt refcnt;
311 /** Job control interface */
312 struct job_interface job;
313 /** Data transfer interface */
314 struct xfer_interface xfer;
316 /** Network device being configured */
317 struct net_device *netdev;
319 /** State of the session
321 * This is a value for the @c DHCP_MESSAGE_TYPE option
322 * (e.g. @c DHCPDISCOVER).
324 enum dhcp_session_state state;
325 /** DHCPOFFER obtained during DHCPDISCOVER */
326 struct dhcp_settings *dhcpoffer;
327 /** ProxyDHCPOFFER obtained during DHCPDISCOVER */
328 struct dhcp_settings *proxydhcpoffer;
329 /** Retransmission timer */
330 struct retry_timer timer;
331 /** Start time of the current state (in ticks) */
338 * @v refcnt Reference counter
340 static void dhcp_free ( struct refcnt *refcnt ) {
341 struct dhcp_session *dhcp =
342 container_of ( refcnt, struct dhcp_session, refcnt );
344 netdev_put ( dhcp->netdev );
345 dhcpset_put ( dhcp->dhcpoffer );
346 dhcpset_put ( dhcp->proxydhcpoffer );
351 * Mark DHCP session as complete
353 * @v dhcp DHCP session
354 * @v rc Return status code
356 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
358 /* Block futher incoming messages */
359 job_nullify ( &dhcp->job );
360 xfer_nullify ( &dhcp->xfer );
362 /* Stop retry timer */
363 stop_timer ( &dhcp->timer );
365 /* Free resources and close interfaces */
366 xfer_close ( &dhcp->xfer, rc );
367 job_done ( &dhcp->job, rc );
370 /****************************************************************************
372 * Data transfer interface
377 * Create a DHCP packet
379 * @v dhcppkt DHCP packet structure to fill in
380 * @v netdev Network device
381 * @v msgtype DHCP message type
382 * @v options Initial options to include (or NULL)
383 * @v data Buffer for DHCP packet
384 * @v max_len Size of DHCP packet buffer
385 * @ret rc Return status code
387 * Creates a DHCP packet in the specified buffer, and fills out a @c
388 * dhcp_packet structure.
390 int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
391 struct net_device *netdev, uint8_t msgtype,
392 struct dhcp_options *options,
393 void *data, size_t max_len ) {
394 struct dhcphdr *dhcphdr = data;
400 options_len = ( options ? options->len : 0 );
401 if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
404 /* Initialise DHCP packet content */
405 memset ( dhcphdr, 0, max_len );
406 dhcphdr->xid = dhcp_xid ( netdev );
407 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
408 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
409 dhcphdr->op = dhcp_op[msgtype];
410 /* If hardware length exceeds the chaddr field length, don't
411 * use the chaddr field. This is as per RFC4390.
413 hlen = netdev->ll_protocol->ll_addr_len;
414 if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
416 dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
418 dhcphdr->hlen = hlen;
419 memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
420 memcpy ( dhcphdr->options, options->data, options_len );
422 /* Initialise DHCP packet structure */
423 memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
424 dhcppkt_init ( dhcppkt, data, max_len );
426 /* Set DHCP_MESSAGE_TYPE option */
427 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
428 &msgtype, sizeof ( msgtype ) ) ) != 0 )
435 * Create DHCP request packet
437 * @v dhcppkt DHCP packet structure to fill in
438 * @v netdev Network device
439 * @v ciaddr Client IP address
440 * @v offer DHCP offer, if applicable
441 * @v data Buffer for DHCP packet
442 * @v max_len Size of DHCP packet buffer
443 * @ret rc Return status code
445 int dhcp_create_request ( struct dhcp_packet *dhcppkt,
446 struct net_device *netdev, struct in_addr ciaddr,
447 struct dhcp_packet *offer,
448 void *data, size_t max_len ) {
449 struct device_description *desc = &netdev->dev->desc;
450 struct dhcp_netdev_desc dhcp_desc;
451 struct dhcp_client_id client_id;
452 struct dhcp_client_uuid client_uuid;
453 unsigned int msgtype;
454 size_t dhcp_features_len;
458 /* Create DHCP packet */
459 msgtype = ( offer ? DHCPREQUEST : DHCPDISCOVER );
460 if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
461 &dhcp_request_options, data,
463 DBG ( "DHCP could not create DHCP packet: %s\n",
468 /* Set client IP address */
469 dhcppkt->dhcphdr->ciaddr = ciaddr;
471 /* Copy any required options from previous server repsonse */
473 struct in_addr server = { 0 };
474 struct in_addr *ip = &offer->dhcphdr->yiaddr;
476 /* Copy server identifier, if present */
477 if ( ( dhcppkt_fetch ( offer, DHCP_SERVER_IDENTIFIER, &server,
478 sizeof ( server ) ) >= 0 ) &&
479 ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
481 sizeof ( server ) ) ) != 0 ) ) {
482 DBG ( "DHCP could not set server ID: %s\n",
487 /* Copy requested IP address, if present */
488 if ( ( ip->s_addr != 0 ) &&
489 ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
490 ip, sizeof ( *ip ) ) ) != 0 ) ) {
491 DBG ( "DHCP could not set requested address: %s\n",
497 /* Add options to identify the feature list */
498 dhcp_features_len = ( dhcp_features_end - dhcp_features );
499 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
500 dhcp_features_len ) ) != 0 ) {
501 DBG ( "DHCP could not set features list option: %s\n",
506 /* Add options to identify the network device */
507 dhcp_desc.type = desc->bus_type;
508 dhcp_desc.vendor = htons ( desc->vendor );
509 dhcp_desc.device = htons ( desc->device );
510 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
511 sizeof ( dhcp_desc ) ) ) != 0 ) {
512 DBG ( "DHCP could not set bus ID option: %s\n",
517 /* Add DHCP client identifier. Required for Infiniband, and
518 * doesn't hurt other link layers.
520 client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
521 ll_addr_len = netdev->ll_protocol->ll_addr_len;
522 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
523 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
524 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
525 ( ll_addr_len + 1 ) ) ) != 0 ) {
526 DBG ( "DHCP could not set client ID: %s\n",
531 /* Add client UUID, if we have one. Required for PXE. */
532 client_uuid.type = DHCP_CLIENT_UUID_TYPE;
533 if ( ( rc = fetch_uuid_setting ( NULL, &uuid_setting,
534 &client_uuid.uuid ) ) >= 0 ) {
535 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
537 sizeof ( client_uuid ) ) ) != 0 ) {
538 DBG ( "DHCP could not set client UUID: %s\n",
548 * Transmit DHCP request
550 * @v dhcp DHCP session
551 * @ret rc Return status code
553 static int dhcp_tx ( struct dhcp_session *dhcp ) {
554 static struct sockaddr_in proxydhcp_server = {
555 .sin_family = AF_INET,
556 .sin_port = htons ( PROXYDHCP_PORT ),
558 struct xfer_metadata meta = {
559 .netdev = dhcp->netdev,
561 struct io_buffer *iobuf;
562 struct dhcp_packet dhcppkt;
563 struct dhcp_packet *offer = NULL;
564 struct in_addr ciaddr = { 0 };
568 /* Start retry timer. Do this first so that failures to
569 * transmit will be retried.
571 start_timer ( &dhcp->timer );
573 /* Determine packet contents based on current state */
574 switch ( dhcp->state ) {
575 case DHCP_STATE_DISCOVER:
576 DBGC ( dhcp, "DHCP %p transmitting DHCPDISCOVER\n", dhcp );
578 case DHCP_STATE_REQUEST:
579 DBGC ( dhcp, "DHCP %p transmitting DHCPREQUEST\n", dhcp );
580 assert ( dhcp->dhcpoffer );
581 offer = &dhcp->dhcpoffer->dhcppkt;
583 case DHCP_STATE_PROXYREQUEST:
584 DBGC ( dhcp, "DHCP %p transmitting ProxyDHCPREQUEST\n", dhcp );
585 assert ( dhcp->dhcpoffer );
586 assert ( dhcp->proxydhcpoffer );
587 offer = &dhcp->proxydhcpoffer->dhcppkt;
588 ciaddr = dhcp->dhcpoffer->dhcppkt.dhcphdr->yiaddr;
589 check_len = dhcppkt_fetch ( offer, DHCP_SERVER_IDENTIFIER,
590 &proxydhcp_server.sin_addr,
591 sizeof(proxydhcp_server.sin_addr));
592 meta.dest = ( struct sockaddr * ) &proxydhcp_server;
593 assert ( ciaddr.s_addr != 0 );
594 assert ( proxydhcp_server.sin_addr.s_addr != 0 );
595 assert ( check_len == sizeof ( proxydhcp_server.sin_addr ) );
602 /* Allocate buffer for packet */
603 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
607 /* Create DHCP packet in temporary buffer */
608 if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev,
609 ciaddr, offer, iobuf->data,
610 iob_tailroom ( iobuf ) ) ) != 0 ) {
611 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
612 dhcp, strerror ( rc ) );
616 /* Transmit the packet */
617 iob_put ( iobuf, dhcppkt.len );
618 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
621 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
622 dhcp, strerror ( rc ) );
632 * Transition to new DHCP session state
634 * @v dhcp DHCP session
635 * @v state New session state
637 static void dhcp_set_state ( struct dhcp_session *dhcp,
638 enum dhcp_session_state state ) {
639 DBGC ( dhcp, "DHCP %p entering %s state\n",
640 dhcp, dhcp_state_name ( state ) );
642 dhcp->start = currticks();
643 dhcp->timer.min_timeout = 0;
644 start_timer_nodelay ( &dhcp->timer );
648 * Transition to next DHCP state
650 * @v dhcp DHCP session
652 static void dhcp_next_state ( struct dhcp_session *dhcp ) {
654 switch ( dhcp->state ) {
655 case DHCP_STATE_DISCOVER:
656 dhcp_set_state ( dhcp, DHCP_STATE_REQUEST );
658 case DHCP_STATE_REQUEST:
659 if ( dhcp->proxydhcpoffer ) {
660 dhcp_set_state ( dhcp, DHCP_STATE_PROXYREQUEST );
664 case DHCP_STATE_PROXYREQUEST:
665 dhcp_finished ( dhcp, 0 );
675 * Store received DHCPOFFER
677 * @v dhcp DHCP session
678 * @v dhcpoffer Received DHCPOFFER
679 * @v stored_dhcpoffer Location to store DHCPOFFER
681 * The DHCPOFFER will be stored in place of the existing stored
682 * DHCPOFFER if its priority is equal to or greater than the stored
685 static void dhcp_store_dhcpoffer ( struct dhcp_session *dhcp,
686 struct dhcp_settings *dhcpoffer,
687 struct dhcp_settings **stored_dhcpoffer ) {
688 uint8_t stored_priority = 0;
689 uint8_t priority = 0;
691 /* Get priorities of the two DHCPOFFERs */
692 if ( *stored_dhcpoffer ) {
693 dhcppkt_fetch ( &(*stored_dhcpoffer)->dhcppkt,
694 DHCP_EB_PRIORITY, &stored_priority,
695 sizeof ( stored_priority ) );
697 dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_EB_PRIORITY, &priority,
698 sizeof ( priority ) );
700 /* Replace stored offer only if priority is equal or greater */
701 if ( priority >= stored_priority ) {
702 if ( *stored_dhcpoffer ) {
703 DBGC ( dhcp, "DHCP %p stored DHCPOFFER %p discarded\n",
704 dhcp, *stored_dhcpoffer );
706 DBGC ( dhcp, "DHCP %p received DHCPOFFER %p stored\n",
708 dhcpset_put ( *stored_dhcpoffer );
709 *stored_dhcpoffer = dhcpset_get ( dhcpoffer );
714 * Handle received DHCPOFFER
716 * @v dhcp DHCP session
717 * @v dhcpoffer Received DHCPOFFER
719 static void dhcp_rx_dhcpoffer ( struct dhcp_session *dhcp,
720 struct dhcp_settings *dhcpoffer ) {
721 struct in_addr server_id = { 0 };
722 char vci[9]; /* "PXEClient" */
724 uint8_t ignore_proxy = 0;
725 unsigned long elapsed;
727 /* Check for presence of DHCP server ID */
728 if ( dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_SERVER_IDENTIFIER,
729 &server_id, sizeof ( server_id ) )
730 != sizeof ( server_id ) ) {
731 DBGC ( dhcp, "DHCP %p received DHCPOFFER %p missing server "
732 "identifier\n", dhcp, dhcpoffer );
733 /* Could be a valid BOOTP offer; do not abort processing */
736 /* If there is an IP address, it's a normal DHCPOFFER */
737 if ( dhcpoffer->dhcppkt.dhcphdr->yiaddr.s_addr != 0 ) {
738 DBGC ( dhcp, "DHCP %p received DHCPOFFER %p from %s has IP "
740 dhcp, dhcpoffer, inet_ntoa ( server_id ) );
741 dhcp_store_dhcpoffer ( dhcp, dhcpoffer, &dhcp->dhcpoffer );
744 /* If there is a "PXEClient" vendor class ID, it's a
745 * ProxyDHCPOFFER. Note that it could be both a normal
746 * DHCPOFFER and a ProxyDHCPOFFER.
748 len = dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_VENDOR_CLASS_ID,
749 vci, sizeof ( vci ) );
750 if ( ( server_id.s_addr != 0 ) &&
751 ( len >= ( int ) sizeof ( vci ) ) &&
752 ( strncmp ( "PXEClient", vci, sizeof ( vci ) ) == 0 ) ) {
753 DBGC ( dhcp, "DHCP %p received DHCPOFFER %p from %s is a "
755 dhcp, dhcpoffer, inet_ntoa ( server_id ) );
756 dhcp_store_dhcpoffer ( dhcp, dhcpoffer,
757 &dhcp->proxydhcpoffer );
760 /* We can transition to making the DHCPREQUEST when we have a
761 * valid DHCPOFFER, and either:
763 * o The DHCPOFFER instructs us to not wait for ProxyDHCP, or
764 * o We have a valid ProxyDHCPOFFER, or
765 * o We have allowed sufficient time for ProxyDHCPOFFERs.
768 /* If we don't yet have a DHCPOFFER, do nothing */
769 if ( ! dhcp->dhcpoffer )
772 /* If the DHCPOFFER instructs us to ignore ProxyDHCP, discard
775 dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt, DHCP_EB_NO_PROXYDHCP,
776 &ignore_proxy, sizeof ( ignore_proxy ) );
777 if ( ignore_proxy && dhcp->proxydhcpoffer ) {
778 DBGC ( dhcp, "DHCP %p discarding ProxyDHCPOFFER\n", dhcp );
779 dhcpset_put ( dhcp->proxydhcpoffer );
780 dhcp->proxydhcpoffer = NULL;
783 /* If we can't yet transition to DHCPREQUEST, do nothing */
784 elapsed = ( currticks() - dhcp->start );
785 if ( ! ( ignore_proxy || dhcp->proxydhcpoffer ||
786 ( elapsed > PROXYDHCP_WAIT_TIME ) ) )
789 /* Transition to DHCPREQUEST */
790 dhcp_next_state ( dhcp );
794 * Store received DHCPACK
796 * @v dhcp DHCP session
797 * @v dhcpack Received DHCPACK
799 * The DHCPACK will be registered as a settings block.
801 static int dhcp_store_dhcpack ( struct dhcp_session *dhcp,
802 struct dhcp_settings *dhcpack,
803 struct settings *parent ) {
804 struct settings *settings = &dhcpack->settings;
805 struct settings *old_settings;
808 /* Unregister any old settings obtained via DHCP */
809 if ( ( old_settings = find_child_settings ( parent, settings->name ) ))
810 unregister_settings ( old_settings );
812 /* Register new settings */
813 if ( ( rc = register_settings ( settings, parent ) ) != 0 ) {
814 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
815 dhcp, strerror ( rc ) );
816 dhcp_finished ( dhcp, rc ); /* This is a fatal error */
824 * Handle received DHCPACK
826 * @v dhcp DHCP session
827 * @v dhcpack Received DHCPACK
829 static void dhcp_rx_dhcpack ( struct dhcp_session *dhcp,
830 struct dhcp_settings *dhcpack ) {
831 struct settings *parent;
832 struct in_addr offer_server_id = { 0 };
833 struct in_addr ack_server_id = { 0 };
836 /* Verify server ID matches */
837 assert ( dhcp->dhcpoffer != NULL );
838 dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt, DHCP_SERVER_IDENTIFIER,
839 &offer_server_id, sizeof ( offer_server_id ) );
840 dhcppkt_fetch ( &dhcpack->dhcppkt, DHCP_SERVER_IDENTIFIER,
841 &ack_server_id, sizeof ( ack_server_id ) );
842 if ( offer_server_id.s_addr != ack_server_id.s_addr ) {
843 DBGC ( dhcp, "DHCP %p ignoring DHCPACK with wrong server ID "
844 "%s\n", dhcp, inet_ntoa ( ack_server_id ) );
848 /* Register settings */
849 parent = netdev_settings ( dhcp->netdev );
850 if ( ( rc = dhcp_store_dhcpack ( dhcp, dhcpack, parent ) ) !=0 )
853 /* Transition to next state */
854 dhcp_next_state ( dhcp );
858 * Handle received ProxyDHCPACK
860 * @v dhcp DHCP session
861 * @v proxydhcpack Received ProxyDHCPACK
863 static void dhcp_rx_proxydhcpack ( struct dhcp_session *dhcp,
864 struct dhcp_settings *proxydhcpack ) {
865 struct in_addr offer_server_id = { 0 };
866 struct in_addr ack_server_id = { 0 };
869 /* Verify server ID matches, if present */
870 assert ( dhcp->proxydhcpoffer != NULL );
871 if ( ( rc = dhcppkt_fetch ( &proxydhcpack->dhcppkt,
872 DHCP_SERVER_IDENTIFIER, &ack_server_id,
873 sizeof ( ack_server_id ) ) ) > 0 ) {
874 dhcppkt_fetch ( &dhcp->proxydhcpoffer->dhcppkt,
875 DHCP_SERVER_IDENTIFIER, &offer_server_id,
876 sizeof ( offer_server_id ) );
877 if ( offer_server_id.s_addr != ack_server_id.s_addr ) {
878 DBGC ( dhcp, "DHCP %p ignoring ProxyDHCPACK with "
879 "wrong server ID %s\n",
880 dhcp, inet_ntoa ( ack_server_id ) );
885 /* Rename settings */
886 proxydhcpack->settings.name = PROXYDHCP_SETTINGS_NAME;
888 /* Register settings */
889 if ( ( rc = dhcp_store_dhcpack ( dhcp, proxydhcpack, NULL ) ) != 0 )
892 /* Transition to next state */
893 dhcp_next_state ( dhcp );
899 * @v xfer Data transfer interface
900 * @v iobuf I/O buffer
901 * @v meta Transfer metadata
902 * @ret rc Return status code
904 static int dhcp_deliver_iob ( struct xfer_interface *xfer,
905 struct io_buffer *iobuf,
906 struct xfer_metadata *meta ) {
907 struct dhcp_session *dhcp =
908 container_of ( xfer, struct dhcp_session, xfer );
909 struct sockaddr_tcpip *st_src;
910 unsigned int src_port;
911 struct dhcp_settings *dhcpset;
912 struct dhcphdr *dhcphdr;
918 DBGC ( dhcp, "DHCP %p received packet without metadata\n",
924 DBGC ( dhcp, "DHCP %p received packet without source port\n",
929 st_src = ( struct sockaddr_tcpip * ) meta->src;
930 src_port = st_src->st_port;
932 /* Convert packet into a DHCP settings block */
933 dhcpset = dhcpset_create ( iobuf->data, iob_len ( iobuf ) );
935 DBGC ( dhcp, "DHCP %p could not store DHCP packet\n", dhcp );
937 goto err_dhcpset_create;
939 dhcphdr = dhcpset->dhcppkt.dhcphdr;
941 /* Identify message type */
942 dhcppkt_fetch ( &dhcpset->dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
943 sizeof ( msgtype ) );
944 DBGC ( dhcp, "DHCP %p received %s %p from port %d\n", dhcp,
945 dhcp_msgtype_name ( msgtype ), dhcpset, ntohs ( src_port ) );
947 /* Check for matching transaction ID */
948 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
949 DBGC ( dhcp, "DHCP %p received %s %p has bad transaction ID\n",
950 dhcp, dhcp_msgtype_name ( msgtype ), dhcpset );
955 /* Handle packet based on current state */
956 switch ( dhcp->state ) {
957 case DHCP_STATE_DISCOVER:
958 if ( ( ( msgtype == DHCPOFFER ) || ( msgtype == DHCPNONE ) ) &&
959 ( src_port == htons ( BOOTPS_PORT ) ) )
960 dhcp_rx_dhcpoffer ( dhcp, dhcpset );
962 case DHCP_STATE_REQUEST:
963 if ( ( ( msgtype == DHCPACK ) || ( msgtype == DHCPNONE ) ) &&
964 ( src_port == htons ( BOOTPS_PORT ) ) )
965 dhcp_rx_dhcpack ( dhcp, dhcpset );
967 case DHCP_STATE_PROXYREQUEST:
968 if ( ( msgtype == DHCPACK ) &&
969 ( src_port == htons ( PROXYDHCP_PORT ) ) )
970 dhcp_rx_proxydhcpack ( dhcp, dhcpset );
978 dhcpset_put ( dhcpset );
986 /** DHCP data transfer interface operations */
987 static struct xfer_interface_operations dhcp_xfer_operations = {
988 .close = ignore_xfer_close,
989 .vredirect = xfer_vopen,
990 .window = unlimited_xfer_window,
991 .alloc_iob = default_xfer_alloc_iob,
992 .deliver_iob = dhcp_deliver_iob,
993 .deliver_raw = xfer_deliver_as_iob,
997 * Handle DHCP retry timer expiry
999 * @v timer DHCP retry timer
1000 * @v fail Failure indicator
1002 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
1003 struct dhcp_session *dhcp =
1004 container_of ( timer, struct dhcp_session, timer );
1005 unsigned long elapsed = ( currticks() - dhcp->start );
1007 /* If we have failed, terminate DHCP */
1009 dhcp_finished ( dhcp, -ETIMEDOUT );
1013 /* Give up waiting for ProxyDHCP before we reach the failure point */
1014 if ( dhcp->dhcpoffer && ( elapsed > PROXYDHCP_WAIT_TIME ) ) {
1015 dhcp_next_state ( dhcp );
1019 /* Otherwise, retransmit current packet */
1023 /****************************************************************************
1025 * Job control interface
1030 * Handle kill() event received via job control interface
1032 * @v job DHCP job control interface
1034 static void dhcp_job_kill ( struct job_interface *job ) {
1035 struct dhcp_session *dhcp =
1036 container_of ( job, struct dhcp_session, job );
1038 /* Terminate DHCP session */
1039 dhcp_finished ( dhcp, -ECANCELED );
1042 /** DHCP job control interface operations */
1043 static struct job_interface_operations dhcp_job_operations = {
1044 .done = ignore_job_done,
1045 .kill = dhcp_job_kill,
1046 .progress = ignore_job_progress,
1049 /****************************************************************************
1056 * Start DHCP on a network device
1058 * @v job Job control interface
1059 * @v netdev Network device
1060 * @v register_options DHCP option block registration routine
1061 * @ret rc Return status code
1063 * Starts DHCP on the specified network device. If successful, the @c
1064 * register_options() routine will be called with the acquired
1067 int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
1068 static struct sockaddr_in server = {
1069 .sin_family = AF_INET,
1070 .sin_addr.s_addr = INADDR_BROADCAST,
1071 .sin_port = htons ( BOOTPS_PORT ),
1073 static struct sockaddr_in client = {
1074 .sin_family = AF_INET,
1075 .sin_port = htons ( BOOTPC_PORT ),
1077 struct dhcp_session *dhcp;
1080 /* Allocate and initialise structure */
1081 dhcp = zalloc ( sizeof ( *dhcp ) );
1084 dhcp->refcnt.free = dhcp_free;
1085 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1086 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1087 dhcp->netdev = netdev_get ( netdev );
1088 dhcp->timer.expired = dhcp_timer_expired;
1089 dhcp->timer.min_timeout = DHCP_MIN_TIMEOUT;
1090 dhcp->timer.max_timeout = DHCP_MAX_TIMEOUT;
1091 dhcp->start = currticks();
1093 /* Instantiate child objects and attach to our interfaces */
1094 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
1095 ( struct sockaddr * ) &server,
1096 ( struct sockaddr * ) &client ) ) != 0 )
1099 /* Start timer to initiate initial DHCPREQUEST */
1100 start_timer_nodelay ( &dhcp->timer );
1102 /* Attach parent interface, mortalise self, and return */
1103 job_plug_plug ( &dhcp->job, job );
1104 ref_put ( &dhcp->refcnt );
1108 dhcp_finished ( dhcp, rc );
1109 ref_put ( &dhcp->refcnt );