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>
44 * Dynamic Host Configuration Protocol
49 * DHCP operation types
51 * This table maps from DHCP message types (i.e. values of the @c
52 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
55 static const uint8_t dhcp_op[] = {
56 [DHCPDISCOVER] = BOOTP_REQUEST,
57 [DHCPOFFER] = BOOTP_REPLY,
58 [DHCPREQUEST] = BOOTP_REQUEST,
59 [DHCPDECLINE] = BOOTP_REQUEST,
60 [DHCPACK] = BOOTP_REPLY,
61 [DHCPNAK] = BOOTP_REPLY,
62 [DHCPRELEASE] = BOOTP_REQUEST,
63 [DHCPINFORM] = BOOTP_REQUEST,
66 /** Raw option data for options common to all DHCP requests */
67 static uint8_t dhcp_request_options_data[] = {
68 DHCP_MAX_MESSAGE_SIZE,
69 DHCP_WORD ( ETH_MAX_MTU - 20 /* IP header */ - 8 /* UDP header */ ),
70 DHCP_CLIENT_ARCHITECTURE, DHCP_WORD ( 0 ),
71 DHCP_CLIENT_NDI, DHCP_OPTION ( 1 /* UNDI */ , 2, 1 /* v2.1 */ ),
73 DHCP_STRING ( 'P', 'X', 'E', 'C', 'l', 'i', 'e', 'n', 't', ':',
74 'A', 'r', 'c', 'h', ':', '0', '0', '0', '0', '0', ':',
75 'U', 'N', 'D', 'I', ':', '0', '0', '2', '0', '0', '1' ),
76 DHCP_PARAMETER_REQUEST_LIST,
77 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
78 DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
79 DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
80 DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
81 DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
85 /** Options common to all DHCP requests */
86 static struct dhcp_options dhcp_request_options = {
87 .data = dhcp_request_options_data,
88 .max_len = sizeof ( dhcp_request_options_data ),
89 .len = sizeof ( dhcp_request_options_data ),
92 /** DHCP feature codes */
93 static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
94 static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
96 /** DHCP network device descriptor */
97 struct dhcp_netdev_desc {
104 } __attribute__ (( packed ));
106 /** DHCP client identifier */
107 struct dhcp_client_id {
108 /** Link-layer protocol */
110 /** Link-layer address */
111 uint8_t ll_addr[MAX_LL_ADDR_LEN];
112 } __attribute__ (( packed ));
114 /** DHCP client UUID */
115 struct dhcp_client_uuid {
116 /** Identifier type */
120 } __attribute__ (( packed ));
122 #define DHCP_CLIENT_UUID_TYPE 0
125 * Name a DHCP packet type
127 * @v msgtype DHCP message type
128 * @ret string DHCP mesasge type name
130 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
132 case DHCPNONE: return "BOOTP"; /* Non-DHCP packet */
133 case DHCPDISCOVER: return "DHCPDISCOVER";
134 case DHCPOFFER: return "DHCPOFFER";
135 case DHCPREQUEST: return "DHCPREQUEST";
136 case DHCPDECLINE: return "DHCPDECLINE";
137 case DHCPACK: return "DHCPACK";
138 case DHCPNAK: return "DHCPNAK";
139 case DHCPRELEASE: return "DHCPRELEASE";
140 case DHCPINFORM: return "DHCPINFORM";
141 default: return "DHCP<invalid>";
146 * Calculate DHCP transaction ID for a network device
148 * @v netdev Network device
151 * Extract the least significant bits of the hardware address for use
152 * as the transaction ID.
154 static uint32_t dhcp_xid ( struct net_device *netdev ) {
157 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
158 - sizeof ( xid ) ), sizeof ( xid ) );
162 /****************************************************************************
168 /** A DHCP settings block */
169 struct dhcp_settings {
170 /** Reference counter */
171 struct refcnt refcnt;
173 struct dhcp_packet dhcppkt;
174 /** Setting interface */
175 struct settings settings;
179 * Increment reference count on DHCP settings block
181 * @v dhcpset DHCP settings block
182 * @ret dhcpset DHCP settings block
184 static inline __attribute__ (( always_inline )) struct dhcp_settings *
185 dhcpset_get ( struct dhcp_settings *dhcpset ) {
186 ref_get ( &dhcpset->refcnt );
191 * Decrement reference count on DHCP settings block
193 * @v dhcpset DHCP settings block
195 static inline __attribute__ (( always_inline )) void
196 dhcpset_put ( struct dhcp_settings *dhcpset ) {
197 ref_put ( &dhcpset->refcnt );
201 * Store value of DHCP setting
203 * @v settings Settings block
204 * @v setting Setting to store
205 * @v data Setting data, or NULL to clear setting
206 * @v len Length of setting data
207 * @ret rc Return status code
209 static int dhcpset_store ( struct settings *settings, struct setting *setting,
210 const void *data, size_t len ) {
211 struct dhcp_settings *dhcpset =
212 container_of ( settings, struct dhcp_settings, settings );
214 return dhcppkt_store ( &dhcpset->dhcppkt, setting->tag, data, len );
218 * Fetch value of DHCP setting
220 * @v settings Settings block, or NULL to search all blocks
221 * @v setting Setting to fetch
222 * @v data Buffer to fill with setting data
223 * @v len Length of buffer
224 * @ret len Length of setting data, or negative error
226 static int dhcpset_fetch ( struct settings *settings, struct setting *setting,
227 void *data, size_t len ) {
228 struct dhcp_settings *dhcpset =
229 container_of ( settings, struct dhcp_settings, settings );
231 return dhcppkt_fetch ( &dhcpset->dhcppkt, setting->tag, data, len );
234 /** DHCP settings operations */
235 static struct settings_operations dhcpset_settings_operations = {
236 .store = dhcpset_store,
237 .fetch = dhcpset_fetch,
241 * Create DHCP setting block
243 * @v dhcphdr DHCP packet
244 * @v len Length of DHCP packet
245 * @ret dhcpset DHCP settings block
247 static struct dhcp_settings * dhcpset_create ( const struct dhcphdr *dhcphdr,
249 struct dhcp_settings *dhcpset;
252 dhcpset = zalloc ( sizeof ( *dhcpset ) + len );
254 data = ( ( ( void * ) dhcpset ) + sizeof ( *dhcpset ) );
255 memcpy ( data, dhcphdr, len );
256 dhcppkt_init ( &dhcpset->dhcppkt, data, len );
257 settings_init ( &dhcpset->settings,
258 &dhcpset_settings_operations, &dhcpset->refcnt,
259 DHCP_SETTINGS_NAME, 0 );
264 /** DHCP server address setting */
265 struct setting dhcp_server_setting __setting = {
266 .name = "dhcp-server",
267 .description = "DHCP server address",
268 .tag = DHCP_SERVER_IDENTIFIER,
269 .type = &setting_type_ipv4,
272 /****************************************************************************
278 /** DHCP session states */
279 enum dhcp_session_state {
280 /** Sending DHCPDISCOVERs, collecting DHCPOFFERs and ProxyDHCPOFFERs */
281 DHCP_STATE_DISCOVER = 0,
282 /** Sending DHCPREQUESTs, waiting for DHCPACK */
284 /** Sending ProxyDHCPREQUESTs, waiting for ProxyDHCPACK */
285 DHCP_STATE_PROXYREQUEST,
289 * Name a DHCP session state
291 * @v state DHCP session state
292 * @ret string DHCP session state name
294 static inline const char * dhcp_state_name ( enum dhcp_session_state state ) {
296 case DHCP_STATE_DISCOVER: return "DHCPDISCOVER";
297 case DHCP_STATE_REQUEST: return "DHCPREQUEST";
298 case DHCP_STATE_PROXYREQUEST: return "ProxyDHCPREQUEST";
299 default: return "<invalid>";
303 /** A DHCP session */
304 struct dhcp_session {
305 /** Reference counter */
306 struct refcnt refcnt;
307 /** Job control interface */
308 struct job_interface job;
309 /** Data transfer interface */
310 struct xfer_interface xfer;
312 /** Network device being configured */
313 struct net_device *netdev;
315 /** State of the session
317 * This is a value for the @c DHCP_MESSAGE_TYPE option
318 * (e.g. @c DHCPDISCOVER).
320 enum dhcp_session_state state;
321 /** DHCPOFFER obtained during DHCPDISCOVER */
322 struct dhcp_settings *dhcpoffer;
323 /** ProxyDHCPOFFER obtained during DHCPDISCOVER */
324 struct dhcp_settings *proxydhcpoffer;
325 /** Retransmission timer */
326 struct retry_timer timer;
327 /** Start time of the current state (in ticks) */
334 * @v refcnt Reference counter
336 static void dhcp_free ( struct refcnt *refcnt ) {
337 struct dhcp_session *dhcp =
338 container_of ( refcnt, struct dhcp_session, refcnt );
340 netdev_put ( dhcp->netdev );
341 dhcpset_put ( dhcp->dhcpoffer );
342 dhcpset_put ( dhcp->proxydhcpoffer );
347 * Mark DHCP session as complete
349 * @v dhcp DHCP session
350 * @v rc Return status code
352 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
354 /* Block futher incoming messages */
355 job_nullify ( &dhcp->job );
356 xfer_nullify ( &dhcp->xfer );
358 /* Stop retry timer */
359 stop_timer ( &dhcp->timer );
361 /* Free resources and close interfaces */
362 xfer_close ( &dhcp->xfer, rc );
363 job_done ( &dhcp->job, rc );
366 /****************************************************************************
368 * Data transfer interface
373 * Create a DHCP packet
375 * @v dhcppkt DHCP packet structure to fill in
376 * @v netdev Network device
377 * @v msgtype DHCP message type
378 * @v options Initial options to include (or NULL)
379 * @v data Buffer for DHCP packet
380 * @v max_len Size of DHCP packet buffer
381 * @ret rc Return status code
383 * Creates a DHCP packet in the specified buffer, and fills out a @c
384 * dhcp_packet structure.
386 int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
387 struct net_device *netdev, uint8_t msgtype,
388 struct dhcp_options *options,
389 void *data, size_t max_len ) {
390 struct dhcphdr *dhcphdr = data;
396 options_len = ( options ? options->len : 0 );
397 if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
400 /* Initialise DHCP packet content */
401 memset ( dhcphdr, 0, max_len );
402 dhcphdr->xid = dhcp_xid ( netdev );
403 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
404 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
405 dhcphdr->op = dhcp_op[msgtype];
406 /* If hardware length exceeds the chaddr field length, don't
407 * use the chaddr field. This is as per RFC4390.
409 hlen = netdev->ll_protocol->ll_addr_len;
410 if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
412 dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
414 dhcphdr->hlen = hlen;
415 memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
416 memcpy ( dhcphdr->options, options->data, options_len );
418 /* Initialise DHCP packet structure */
419 memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
420 dhcppkt_init ( dhcppkt, data, max_len );
422 /* Set DHCP_MESSAGE_TYPE option */
423 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
424 &msgtype, sizeof ( msgtype ) ) ) != 0 )
431 * Create DHCP request packet
433 * @v dhcppkt DHCP packet structure to fill in
434 * @v netdev Network device
435 * @v ciaddr Client IP address
436 * @v offer DHCP offer, if applicable
437 * @v data Buffer for DHCP packet
438 * @v max_len Size of DHCP packet buffer
439 * @ret rc Return status code
441 int dhcp_create_request ( struct dhcp_packet *dhcppkt,
442 struct net_device *netdev, struct in_addr ciaddr,
443 struct dhcp_packet *offer,
444 void *data, size_t max_len ) {
445 struct device_description *desc = &netdev->dev->desc;
446 struct dhcp_netdev_desc dhcp_desc;
447 struct dhcp_client_id client_id;
448 struct dhcp_client_uuid client_uuid;
449 unsigned int msgtype;
450 size_t dhcp_features_len;
454 /* Create DHCP packet */
455 msgtype = ( offer ? DHCPREQUEST : DHCPDISCOVER );
456 if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
457 &dhcp_request_options, data,
459 DBG ( "DHCP could not create DHCP packet: %s\n",
464 /* Set client IP address */
465 dhcppkt->dhcphdr->ciaddr = ciaddr;
467 /* Copy any required options from previous server repsonse */
469 struct in_addr server = { 0 };
470 struct in_addr *ip = &offer->dhcphdr->yiaddr;
472 /* Copy server identifier, if present */
473 if ( ( dhcppkt_fetch ( offer, DHCP_SERVER_IDENTIFIER, &server,
474 sizeof ( server ) ) >= 0 ) &&
475 ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
477 sizeof ( server ) ) ) != 0 ) ) {
478 DBG ( "DHCP could not set server ID: %s\n",
483 /* Copy requested IP address, if present */
484 if ( ( ip->s_addr != 0 ) &&
485 ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
486 ip, sizeof ( *ip ) ) ) != 0 ) ) {
487 DBG ( "DHCP could not set requested address: %s\n",
493 /* Add options to identify the feature list */
494 dhcp_features_len = ( dhcp_features_end - dhcp_features );
495 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
496 dhcp_features_len ) ) != 0 ) {
497 DBG ( "DHCP could not set features list option: %s\n",
502 /* Add options to identify the network device */
503 dhcp_desc.type = desc->bus_type;
504 dhcp_desc.vendor = htons ( desc->vendor );
505 dhcp_desc.device = htons ( desc->device );
506 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
507 sizeof ( dhcp_desc ) ) ) != 0 ) {
508 DBG ( "DHCP could not set bus ID option: %s\n",
513 /* Add DHCP client identifier. Required for Infiniband, and
514 * doesn't hurt other link layers.
516 client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
517 ll_addr_len = netdev->ll_protocol->ll_addr_len;
518 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
519 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
520 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
521 ( ll_addr_len + 1 ) ) ) != 0 ) {
522 DBG ( "DHCP could not set client ID: %s\n",
527 /* Add client UUID, if we have one. Required for PXE. */
528 client_uuid.type = DHCP_CLIENT_UUID_TYPE;
529 if ( ( rc = fetch_uuid_setting ( NULL, &uuid_setting,
530 &client_uuid.uuid ) ) >= 0 ) {
531 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
533 sizeof ( client_uuid ) ) ) != 0 ) {
534 DBG ( "DHCP could not set client UUID: %s\n",
544 * Transmit DHCP request
546 * @v dhcp DHCP session
547 * @ret rc Return status code
549 static int dhcp_tx ( struct dhcp_session *dhcp ) {
550 static struct sockaddr_in proxydhcp_server = {
551 .sin_family = AF_INET,
552 .sin_port = htons ( PROXYDHCP_PORT ),
554 struct xfer_metadata meta = {
555 .netdev = dhcp->netdev,
557 struct io_buffer *iobuf;
558 struct dhcp_packet dhcppkt;
559 struct dhcp_packet *offer = NULL;
560 struct in_addr ciaddr = { 0 };
564 /* Start retry timer. Do this first so that failures to
565 * transmit will be retried.
567 start_timer ( &dhcp->timer );
569 /* Determine packet contents based on current state */
570 switch ( dhcp->state ) {
571 case DHCP_STATE_DISCOVER:
572 DBGC ( dhcp, "DHCP %p transmitting DHCPDISCOVER\n", dhcp );
574 case DHCP_STATE_REQUEST:
575 DBGC ( dhcp, "DHCP %p transmitting DHCPREQUEST\n", dhcp );
576 assert ( dhcp->dhcpoffer );
577 offer = &dhcp->dhcpoffer->dhcppkt;
579 case DHCP_STATE_PROXYREQUEST:
580 DBGC ( dhcp, "DHCP %p transmitting ProxyDHCPREQUEST\n", dhcp );
581 assert ( dhcp->dhcpoffer );
582 assert ( dhcp->proxydhcpoffer );
583 offer = &dhcp->proxydhcpoffer->dhcppkt;
584 ciaddr = dhcp->dhcpoffer->dhcppkt.dhcphdr->yiaddr;
585 check_len = dhcppkt_fetch ( offer, DHCP_SERVER_IDENTIFIER,
586 &proxydhcp_server.sin_addr,
587 sizeof(proxydhcp_server.sin_addr));
588 meta.dest = ( struct sockaddr * ) &proxydhcp_server;
589 assert ( ciaddr.s_addr != 0 );
590 assert ( proxydhcp_server.sin_addr.s_addr != 0 );
591 assert ( check_len == sizeof ( proxydhcp_server.sin_addr ) );
598 /* Allocate buffer for packet */
599 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
603 /* Create DHCP packet in temporary buffer */
604 if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev,
605 ciaddr, offer, iobuf->data,
606 iob_tailroom ( iobuf ) ) ) != 0 ) {
607 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
608 dhcp, strerror ( rc ) );
612 /* Transmit the packet */
613 iob_put ( iobuf, dhcppkt.len );
614 rc = xfer_deliver_iob_meta ( &dhcp->xfer, iobuf, &meta );
617 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
618 dhcp, strerror ( rc ) );
628 * Transition to new DHCP session state
630 * @v dhcp DHCP session
631 * @v state New session state
633 static void dhcp_set_state ( struct dhcp_session *dhcp,
634 enum dhcp_session_state state ) {
635 DBGC ( dhcp, "DHCP %p entering %s state\n",
636 dhcp, dhcp_state_name ( state ) );
638 dhcp->start = currticks();
639 start_timer_nodelay ( &dhcp->timer );
643 * Store received DHCPOFFER
645 * @v dhcp DHCP session
646 * @v dhcpoffer Received DHCPOFFER
647 * @v stored_dhcpoffer Location to store DHCPOFFER
649 * The DHCPOFFER will be stored in place of the existing stored
650 * DHCPOFFER if its priority is equal to or greater than the stored
653 static void dhcp_store_dhcpoffer ( struct dhcp_session *dhcp,
654 struct dhcp_settings *dhcpoffer,
655 struct dhcp_settings **stored_dhcpoffer ) {
656 uint8_t stored_priority = 0;
657 uint8_t priority = 0;
659 /* Get priorities of the two DHCPOFFERs */
660 if ( *stored_dhcpoffer ) {
661 dhcppkt_fetch ( &(*stored_dhcpoffer)->dhcppkt,
662 DHCP_EB_PRIORITY, &stored_priority,
663 sizeof ( stored_priority ) );
665 dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_EB_PRIORITY, &priority,
666 sizeof ( priority ) );
668 /* Replace stored offer only if priority is equal or greater */
669 if ( priority >= stored_priority ) {
670 if ( *stored_dhcpoffer ) {
671 DBGC ( dhcp, "DHCP %p stored DHCPOFFER %p discarded\n",
672 dhcp, *stored_dhcpoffer );
674 DBGC ( dhcp, "DHCP %p received DHCPOFFER %p stored\n",
676 dhcpset_put ( *stored_dhcpoffer );
677 *stored_dhcpoffer = dhcpset_get ( dhcpoffer );
682 * Handle received DHCPOFFER
684 * @v dhcp DHCP session
685 * @v dhcpoffer Received DHCPOFFER
687 static void dhcp_rx_dhcpoffer ( struct dhcp_session *dhcp,
688 struct dhcp_settings *dhcpoffer ) {
689 struct in_addr server_id = { 0 };
690 char vci[9]; /* "PXEClient" */
692 uint8_t ignore_proxy = 0;
693 unsigned long elapsed;
695 /* Check for presence of DHCP server ID */
696 if ( dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_SERVER_IDENTIFIER,
697 &server_id, sizeof ( server_id ) )
698 != sizeof ( server_id ) ) {
699 DBGC ( dhcp, "DHCP %p received DHCPOFFER %p missing server "
700 "identifier\n", dhcp, dhcpoffer );
701 /* Could be a valid BOOTP offer; do not abort processing */
704 /* If there is an IP address, it's a normal DHCPOFFER */
705 if ( dhcpoffer->dhcppkt.dhcphdr->yiaddr.s_addr != 0 ) {
706 DBGC ( dhcp, "DHCP %p received DHCPOFFER %p from %s has IP "
708 dhcp, dhcpoffer, inet_ntoa ( server_id ) );
709 dhcp_store_dhcpoffer ( dhcp, dhcpoffer, &dhcp->dhcpoffer );
712 /* If there is a "PXEClient" vendor class ID, it's a
713 * ProxyDHCPOFFER. Note that it could be both a normal
714 * DHCPOFFER and a ProxyDHCPOFFER.
716 len = dhcppkt_fetch ( &dhcpoffer->dhcppkt, DHCP_VENDOR_CLASS_ID,
717 vci, sizeof ( vci ) );
718 if ( ( server_id.s_addr != 0 ) &&
719 ( len >= ( int ) sizeof ( vci ) ) &&
720 ( strncmp ( "PXEClient", vci, sizeof ( vci ) ) == 0 ) ) {
721 DBGC ( dhcp, "DHCP %p received DHCPOFFER %p from %s is a "
723 dhcp, dhcpoffer, inet_ntoa ( server_id ) );
724 dhcp_store_dhcpoffer ( dhcp, dhcpoffer,
725 &dhcp->proxydhcpoffer );
728 /* We can transition to making the DHCPREQUEST when we have a
729 * valid DHCPOFFER, and either:
731 * o The DHCPOFFER instructs us to not wait for ProxyDHCP, or
732 * o We have a valid ProxyDHCPOFFER, or
733 * o We have allowed sufficient time for ProxyDHCPOFFERs.
736 /* If we don't yet have a DHCPOFFER, do nothing */
737 if ( ! dhcp->dhcpoffer )
740 /* If the DHCPOFFER instructs us to ignore ProxyDHCP, discard
743 dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt, DHCP_EB_NO_PROXYDHCP,
744 &ignore_proxy, sizeof ( ignore_proxy ) );
745 if ( ignore_proxy && dhcp->proxydhcpoffer ) {
746 DBGC ( dhcp, "DHCP %p discarding ProxyDHCPOFFER\n", dhcp );
747 dhcpset_put ( dhcp->proxydhcpoffer );
748 dhcp->proxydhcpoffer = NULL;
751 /* If we can't yet transition to DHCPREQUEST, do nothing */
752 elapsed = ( currticks() - dhcp->start );
753 if ( ! ( ignore_proxy || dhcp->proxydhcpoffer ||
754 ( elapsed > PROXYDHCP_WAIT_TIME ) ) )
757 /* Transition to DHCPREQUEST */
758 dhcp_set_state ( dhcp, DHCP_STATE_REQUEST );
762 * Store received DHCPACK
764 * @v dhcp DHCP session
765 * @v dhcpack Received DHCPACK
767 * The DHCPACK will be registered as a settings block.
769 static int dhcp_store_dhcpack ( struct dhcp_session *dhcp,
770 struct dhcp_settings *dhcpack,
771 struct settings *parent ) {
772 struct settings *settings = &dhcpack->settings;
773 struct settings *old_settings;
776 /* Unregister any old settings obtained via DHCP */
777 if ( ( old_settings = find_child_settings ( parent, settings->name ) ))
778 unregister_settings ( old_settings );
780 /* Register new settings */
781 if ( ( rc = register_settings ( settings, parent ) ) != 0 ) {
782 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
783 dhcp, strerror ( rc ) );
784 dhcp_finished ( dhcp, rc ); /* This is a fatal error */
792 * Handle received DHCPACK
794 * @v dhcp DHCP session
795 * @v dhcpack Received DHCPACK
797 static void dhcp_rx_dhcpack ( struct dhcp_session *dhcp,
798 struct dhcp_settings *dhcpack ) {
799 struct settings *parent;
800 struct in_addr offer_server_id = { 0 };
801 struct in_addr ack_server_id = { 0 };
804 /* Verify server ID matches */
805 assert ( dhcp->dhcpoffer != NULL );
806 dhcppkt_fetch ( &dhcp->dhcpoffer->dhcppkt, DHCP_SERVER_IDENTIFIER,
807 &offer_server_id, sizeof ( offer_server_id ) );
808 dhcppkt_fetch ( &dhcpack->dhcppkt, DHCP_SERVER_IDENTIFIER,
809 &ack_server_id, sizeof ( ack_server_id ) );
810 if ( offer_server_id.s_addr != ack_server_id.s_addr ) {
811 DBGC ( dhcp, "DHCP %p ignoring DHCPACK with wrong server ID "
812 "%s\n", dhcp, inet_ntoa ( ack_server_id ) );
816 /* Register settings */
817 parent = netdev_settings ( dhcp->netdev );
818 if ( ( rc = dhcp_store_dhcpack ( dhcp, dhcpack, parent ) ) !=0 )
821 /* If we have a ProxyDHCPOFFER, transition to PROXYDHCPREQUEST */
822 if ( dhcp->proxydhcpoffer ) {
823 dhcp->timer.min_timeout = 0;
824 dhcp_set_state ( dhcp, DHCP_STATE_PROXYREQUEST );
829 dhcp_finished ( dhcp, 0 );
833 * Handle received ProxyDHCPACK
835 * @v dhcp DHCP session
836 * @v proxydhcpack Received ProxyDHCPACK
838 static void dhcp_rx_proxydhcpack ( struct dhcp_session *dhcp,
839 struct dhcp_settings *proxydhcpack ) {
840 struct in_addr offer_server_id = { 0 };
841 struct in_addr ack_server_id = { 0 };
844 /* Verify server ID matches */
845 assert ( dhcp->proxydhcpoffer != NULL );
846 dhcppkt_fetch ( &dhcp->proxydhcpoffer->dhcppkt, DHCP_SERVER_IDENTIFIER,
847 &offer_server_id, sizeof ( offer_server_id ) );
848 dhcppkt_fetch ( &proxydhcpack->dhcppkt, DHCP_SERVER_IDENTIFIER,
849 &ack_server_id, sizeof ( ack_server_id ) );
850 if ( offer_server_id.s_addr != ack_server_id.s_addr ) {
851 DBGC ( dhcp, "DHCP %p ignoring ProxyDHCPACK with wrong server "
852 "ID %s\n", dhcp, inet_ntoa ( ack_server_id ) );
856 /* Rename settings */
857 proxydhcpack->settings.name = PROXYDHCP_SETTINGS_NAME;
859 /* Register settings */
860 if ( ( rc = dhcp_store_dhcpack ( dhcp, proxydhcpack, NULL ) ) != 0 )
864 dhcp_finished ( dhcp, 0 );
870 * @v xfer Data transfer interface
871 * @v iobuf I/O buffer
872 * @v meta Transfer metadata
873 * @ret rc Return status code
875 static int dhcp_deliver_iob ( struct xfer_interface *xfer,
876 struct io_buffer *iobuf,
877 struct xfer_metadata *meta ) {
878 struct dhcp_session *dhcp =
879 container_of ( xfer, struct dhcp_session, xfer );
880 struct sockaddr_tcpip *st_src;
881 unsigned int src_port;
882 struct dhcp_settings *dhcpset;
883 struct dhcphdr *dhcphdr;
889 DBGC ( dhcp, "DHCP %p received packet without metadata\n",
895 DBGC ( dhcp, "DHCP %p received packet without source port\n",
900 st_src = ( struct sockaddr_tcpip * ) meta->src;
901 src_port = st_src->st_port;
903 /* Convert packet into a DHCP settings block */
904 dhcpset = dhcpset_create ( iobuf->data, iob_len ( iobuf ) );
906 DBGC ( dhcp, "DHCP %p could not store DHCP packet\n", dhcp );
908 goto err_dhcpset_create;
910 dhcphdr = dhcpset->dhcppkt.dhcphdr;
912 /* Identify message type */
913 dhcppkt_fetch ( &dhcpset->dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
914 sizeof ( msgtype ) );
915 DBGC ( dhcp, "DHCP %p received %s %p from port %d\n", dhcp,
916 dhcp_msgtype_name ( msgtype ), dhcpset, ntohs ( src_port ) );
918 /* Check for matching transaction ID */
919 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
920 DBGC ( dhcp, "DHCP %p received %s %p has bad transaction ID\n",
921 dhcp, dhcp_msgtype_name ( msgtype ), dhcpset );
926 /* Handle packet based on current state */
927 switch ( dhcp->state ) {
928 case DHCP_STATE_DISCOVER:
929 if ( ( ( msgtype == DHCPOFFER ) || ( msgtype == DHCPNONE ) ) &&
930 ( src_port == htons ( BOOTPS_PORT ) ) )
931 dhcp_rx_dhcpoffer ( dhcp, dhcpset );
933 case DHCP_STATE_REQUEST:
934 if ( ( ( msgtype == DHCPACK ) || ( msgtype == DHCPNONE ) ) &&
935 ( src_port == htons ( BOOTPS_PORT ) ) )
936 dhcp_rx_dhcpack ( dhcp, dhcpset );
938 case DHCP_STATE_PROXYREQUEST:
939 if ( ( msgtype == DHCPACK ) &&
940 ( src_port == htons ( PROXYDHCP_PORT ) ) )
941 dhcp_rx_proxydhcpack ( dhcp, dhcpset );
949 dhcpset_put ( dhcpset );
957 /** DHCP data transfer interface operations */
958 static struct xfer_interface_operations dhcp_xfer_operations = {
959 .close = ignore_xfer_close,
960 .vredirect = xfer_vopen,
961 .window = unlimited_xfer_window,
962 .alloc_iob = default_xfer_alloc_iob,
963 .deliver_iob = dhcp_deliver_iob,
964 .deliver_raw = xfer_deliver_as_iob,
968 * Handle DHCP retry timer expiry
970 * @v timer DHCP retry timer
971 * @v fail Failure indicator
973 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
974 struct dhcp_session *dhcp =
975 container_of ( timer, struct dhcp_session, timer );
976 unsigned long elapsed = ( currticks() - dhcp->start );
978 /* If we have failed, terminate DHCP */
980 dhcp_finished ( dhcp, -ETIMEDOUT );
984 /* Give up waiting for ProxyDHCP before we reach the failure point */
985 if ( dhcp->dhcpoffer && ( elapsed > PROXYDHCP_WAIT_TIME ) ) {
986 if ( dhcp->state == DHCP_STATE_DISCOVER ) {
987 dhcp_set_state ( dhcp, DHCP_STATE_REQUEST );
989 } else if ( dhcp->state == DHCP_STATE_PROXYREQUEST ) {
990 dhcp_finished ( dhcp, 0 );
995 /* Otherwise, retransmit current packet */
999 /****************************************************************************
1001 * Job control interface
1006 * Handle kill() event received via job control interface
1008 * @v job DHCP job control interface
1010 static void dhcp_job_kill ( struct job_interface *job ) {
1011 struct dhcp_session *dhcp =
1012 container_of ( job, struct dhcp_session, job );
1014 /* Terminate DHCP session */
1015 dhcp_finished ( dhcp, -ECANCELED );
1018 /** DHCP job control interface operations */
1019 static struct job_interface_operations dhcp_job_operations = {
1020 .done = ignore_job_done,
1021 .kill = dhcp_job_kill,
1022 .progress = ignore_job_progress,
1025 /****************************************************************************
1032 * Start DHCP on a network device
1034 * @v job Job control interface
1035 * @v netdev Network device
1036 * @v register_options DHCP option block registration routine
1037 * @ret rc Return status code
1039 * Starts DHCP on the specified network device. If successful, the @c
1040 * register_options() routine will be called with the acquired
1043 int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
1044 static struct sockaddr_in server = {
1045 .sin_family = AF_INET,
1046 .sin_addr.s_addr = INADDR_BROADCAST,
1047 .sin_port = htons ( BOOTPS_PORT ),
1049 static struct sockaddr_in client = {
1050 .sin_family = AF_INET,
1051 .sin_port = htons ( BOOTPC_PORT ),
1053 struct dhcp_session *dhcp;
1056 /* Allocate and initialise structure */
1057 dhcp = zalloc ( sizeof ( *dhcp ) );
1060 dhcp->refcnt.free = dhcp_free;
1061 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1062 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1063 dhcp->netdev = netdev_get ( netdev );
1064 dhcp->timer.expired = dhcp_timer_expired;
1065 dhcp->timer.min_timeout = DHCP_MIN_TIMEOUT;
1066 dhcp->timer.max_timeout = DHCP_MAX_TIMEOUT;
1067 dhcp->start = currticks();
1069 /* Instantiate child objects and attach to our interfaces */
1070 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM,
1071 ( struct sockaddr * ) &server,
1072 ( struct sockaddr * ) &client ) ) != 0 )
1075 /* Start timer to initiate initial DHCPREQUEST */
1076 start_timer_nodelay ( &dhcp->timer );
1078 /* Attach parent interface, mortalise self, and return */
1079 job_plug_plug ( &dhcp->job, job );
1080 ref_put ( &dhcp->refcnt );
1084 dhcp_finished ( dhcp, rc );
1085 ref_put ( &dhcp->refcnt );