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.
26 #include <gpxe/if_ether.h>
27 #include <gpxe/netdevice.h>
28 #include <gpxe/device.h>
29 #include <gpxe/xfer.h>
30 #include <gpxe/open.h>
32 #include <gpxe/retry.h>
33 #include <gpxe/tcpip.h>
35 #include <gpxe/uuid.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 static int dhcp_tx ( struct dhcp_session *dhcp );
53 * DHCP operation types
55 * This table maps from DHCP message types (i.e. values of the @c
56 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
59 static const uint8_t dhcp_op[] = {
60 [DHCPDISCOVER] = BOOTP_REQUEST,
61 [DHCPOFFER] = BOOTP_REPLY,
62 [DHCPREQUEST] = BOOTP_REQUEST,
63 [DHCPDECLINE] = BOOTP_REQUEST,
64 [DHCPACK] = BOOTP_REPLY,
65 [DHCPNAK] = BOOTP_REPLY,
66 [DHCPRELEASE] = BOOTP_REQUEST,
67 [DHCPINFORM] = BOOTP_REQUEST,
70 /** Raw option data for options common to all DHCP requests */
71 static uint8_t dhcp_request_options_data[] = {
72 DHCP_MAX_MESSAGE_SIZE,
73 DHCP_WORD ( ETH_MAX_MTU - 20 /* IP header */ - 8 /* UDP header */ ),
74 DHCP_CLIENT_ARCHITECTURE, DHCP_WORD ( 0 ),
75 DHCP_CLIENT_NDI, DHCP_OPTION ( 1 /* UNDI */ , 2, 1 /* v2.1 */ ),
77 DHCP_STRING ( 'P', 'X', 'E', 'C', 'l', 'i', 'e', 'n', 't', ':',
78 'A', 'r', 'c', 'h', ':', '0', '0', '0', '0', '0', ':',
79 'U', 'N', 'D', 'I', ':', '0', '0', '2', '0', '0', '1' ),
81 DHCP_STRING ( 'g', 'P', 'X', 'E' ),
82 DHCP_PARAMETER_REQUEST_LIST,
83 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
84 DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
85 DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
86 DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
87 DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
91 /** Version number feature */
92 FEATURE_VERSION ( VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH );
94 /** DHCP server address setting */
95 struct setting dhcp_server_setting __setting = {
96 .name = "dhcp-server",
97 .description = "DHCP server address",
98 .tag = DHCP_SERVER_IDENTIFIER,
99 .type = &setting_type_ipv4,
102 /** DHCP user class setting */
103 struct setting user_class_setting __setting = {
104 .name = "user-class",
105 .description = "User class identifier",
106 .tag = DHCP_USER_CLASS_ID,
107 .type = &setting_type_string,
111 * Name a DHCP packet type
113 * @v msgtype DHCP message type
114 * @ret string DHCP mesasge type name
116 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
118 case DHCPNONE: return "BOOTP"; /* Non-DHCP packet */
119 case DHCPDISCOVER: return "DHCPDISCOVER";
120 case DHCPOFFER: return "DHCPOFFER";
121 case DHCPREQUEST: return "DHCPREQUEST";
122 case DHCPDECLINE: return "DHCPDECLINE";
123 case DHCPACK: return "DHCPACK";
124 case DHCPNAK: return "DHCPNAK";
125 case DHCPRELEASE: return "DHCPRELEASE";
126 case DHCPINFORM: return "DHCPINFORM";
127 default: return "DHCP<invalid>";
132 * Calculate DHCP transaction ID for a network device
134 * @v netdev Network device
137 * Extract the least significant bits of the hardware address for use
138 * as the transaction ID.
140 static uint32_t dhcp_xid ( struct net_device *netdev ) {
143 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
144 - sizeof ( xid ) ), sizeof ( xid ) );
148 /****************************************************************************
156 /** DHCP session state operations */
157 struct dhcp_session_state {
161 * Construct transmitted packet
163 * @v dhcp DHCP session
164 * @v dhcppkt DHCP packet
165 * @v peer Destination address
167 int ( * tx ) ( struct dhcp_session *dhcp,
168 struct dhcp_packet *dhcppkt,
169 struct sockaddr_in *peer );
170 /** Handle received packet
172 * @v dhcp DHCP session
173 * @v dhcppkt DHCP packet
174 * @v peer DHCP server address
175 * @v msgtype DHCP message type
176 * @v server_id DHCP server ID
178 void ( * rx ) ( struct dhcp_session *dhcp,
179 struct dhcp_packet *dhcppkt,
180 struct sockaddr_in *peer,
181 uint8_t msgtype, struct in_addr server_id );
182 /** Handle timer expiry
184 * @v dhcp DHCP session
186 void ( * expired ) ( struct dhcp_session *dhcp );
187 /** Transmitted message type */
189 /** Apply minimum timeout */
190 uint8_t apply_min_timeout;
193 static struct dhcp_session_state dhcp_state_discover;
194 static struct dhcp_session_state dhcp_state_request;
195 static struct dhcp_session_state dhcp_state_proxy;
196 static struct dhcp_session_state dhcp_state_pxebs;
198 /** A DHCP session */
199 struct dhcp_session {
200 /** Reference counter */
201 struct refcnt refcnt;
202 /** Job control interface */
203 struct job_interface job;
204 /** Data transfer interface */
205 struct xfer_interface xfer;
207 /** Network device being configured */
208 struct net_device *netdev;
209 /** Local socket address */
210 struct sockaddr_in local;
211 /** State of the session */
212 struct dhcp_session_state *state;
214 /** Offered IP address */
215 struct in_addr offer;
217 struct in_addr server;
218 /** DHCP offer priority */
221 /** ProxyDHCP protocol extensions should be ignored */
223 /** ProxyDHCP server */
224 struct in_addr proxy_server;
225 /** ProxyDHCP server priority */
228 /** PXE Boot Server type */
230 /** List of PXE Boot Servers to attempt */
231 struct in_addr *pxe_attempt;
232 /** List of PXE Boot Servers to accept */
233 struct in_addr *pxe_accept;
235 /** Retransmission timer */
236 struct retry_timer timer;
237 /** Start time of the current state (in ticks) */
244 * @v refcnt Reference counter
246 static void dhcp_free ( struct refcnt *refcnt ) {
247 struct dhcp_session *dhcp =
248 container_of ( refcnt, struct dhcp_session, refcnt );
250 netdev_put ( dhcp->netdev );
255 * Mark DHCP session as complete
257 * @v dhcp DHCP session
258 * @v rc Return status code
260 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
262 /* Block futher incoming messages */
263 job_nullify ( &dhcp->job );
264 xfer_nullify ( &dhcp->xfer );
266 /* Stop retry timer */
267 stop_timer ( &dhcp->timer );
269 /* Free resources and close interfaces */
270 xfer_close ( &dhcp->xfer, rc );
271 job_done ( &dhcp->job, rc );
275 * Transition to new DHCP session state
277 * @v dhcp DHCP session
278 * @v state New session state
280 static void dhcp_set_state ( struct dhcp_session *dhcp,
281 struct dhcp_session_state *state ) {
283 DBGC ( dhcp, "DHCP %p entering %s state\n", dhcp, state->name );
285 dhcp->start = currticks();
286 stop_timer ( &dhcp->timer );
287 dhcp->timer.min_timeout =
288 ( state->apply_min_timeout ? DHCP_MIN_TIMEOUT : 0 );
289 dhcp->timer.max_timeout = DHCP_MAX_TIMEOUT;
290 start_timer_nodelay ( &dhcp->timer );
293 /****************************************************************************
300 * Construct transmitted packet for DHCP discovery
302 * @v dhcp DHCP session
303 * @v dhcppkt DHCP packet
304 * @v peer Destination address
306 static int dhcp_discovery_tx ( struct dhcp_session *dhcp,
307 struct dhcp_packet *dhcppkt __unused,
308 struct sockaddr_in *peer ) {
310 DBGC ( dhcp, "DHCP %p DHCPDISCOVER\n", dhcp );
312 /* Set server address */
313 peer->sin_addr.s_addr = INADDR_BROADCAST;
314 peer->sin_port = htons ( BOOTPS_PORT );
320 * Handle received packet during DHCP discovery
322 * @v dhcp DHCP session
323 * @v dhcppkt DHCP packet
324 * @v peer DHCP server address
325 * @v msgtype DHCP message type
326 * @v server_id DHCP server ID
328 static void dhcp_discovery_rx ( struct dhcp_session *dhcp,
329 struct dhcp_packet *dhcppkt,
330 struct sockaddr_in *peer, uint8_t msgtype,
331 struct in_addr server_id ) {
333 char vci[9]; /* "PXEClient" */
337 uint8_t no_pxedhcp = 0;
338 unsigned long elapsed;
340 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
341 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
342 ntohs ( peer->sin_port ) );
343 if ( server_id.s_addr != peer->sin_addr.s_addr )
344 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
346 /* Identify offered IP address */
347 ip = dhcppkt->dhcphdr->yiaddr;
349 DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
351 /* Identify "PXEClient" vendor class */
352 vci_len = dhcppkt_fetch ( dhcppkt, DHCP_VENDOR_CLASS_ID,
353 vci, sizeof ( vci ) );
354 has_pxeclient = ( ( vci_len >= ( int ) sizeof ( vci ) ) &&
355 ( strncmp ( "PXEClient", vci, sizeof (vci) ) == 0 ));
357 DBGC ( dhcp, " pxe" );
359 /* Identify priority */
360 dhcppkt_fetch ( dhcppkt, DHCP_EB_PRIORITY, &priority,
361 sizeof ( priority ) );
363 DBGC ( dhcp, " pri %d", priority );
365 /* Identify ignore-PXE flag */
366 dhcppkt_fetch ( dhcppkt, DHCP_EB_NO_PXEDHCP, &no_pxedhcp,
367 sizeof ( no_pxedhcp ) );
369 DBGC ( dhcp, " nopxe" );
372 /* Select as DHCP offer, if applicable */
373 if ( ip.s_addr && ( peer->sin_port == htons ( BOOTPS_PORT ) ) &&
374 ( ( msgtype == DHCPOFFER ) || ( ! msgtype /* BOOTP */ ) ) &&
375 ( priority >= dhcp->priority ) ) {
377 dhcp->server = server_id;
378 dhcp->priority = priority;
379 dhcp->no_pxedhcp = no_pxedhcp;
382 /* Select as ProxyDHCP offer, if applicable */
383 if ( has_pxeclient && ( msgtype == DHCPOFFER ) &&
384 ( priority >= dhcp->proxy_priority ) ) {
385 dhcp->proxy_server = server_id;
386 dhcp->proxy_priority = priority;
389 /* We can exit the discovery state when we have a valid
390 * DHCPOFFER, and either:
392 * o The DHCPOFFER instructs us to ignore ProxyDHCPOFFERs, or
393 * o We have a valid ProxyDHCPOFFER, or
394 * o We have allowed sufficient time for ProxyDHCPOFFERs.
397 /* If we don't yet have a DHCPOFFER, do nothing */
398 if ( ! dhcp->offer.s_addr )
401 /* If we can't yet transition to DHCPREQUEST, do nothing */
402 elapsed = ( currticks() - dhcp->start );
403 if ( ! ( dhcp->no_pxedhcp || dhcp->proxy_server.s_addr ||
404 ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) )
407 /* Transition to DHCPREQUEST */
408 dhcp_set_state ( dhcp, &dhcp_state_request );
412 * Handle timer expiry during DHCP discovery
414 * @v dhcp DHCP session
416 static void dhcp_discovery_expired ( struct dhcp_session *dhcp ) {
417 unsigned long elapsed = ( currticks() - dhcp->start );
419 /* Give up waiting for ProxyDHCP before we reach the failure point */
420 if ( dhcp->offer.s_addr && ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) {
421 dhcp_set_state ( dhcp, &dhcp_state_request );
425 /* Otherwise, retransmit current packet */
429 /** DHCP discovery state operations */
430 static struct dhcp_session_state dhcp_state_discover = {
432 .tx = dhcp_discovery_tx,
433 .rx = dhcp_discovery_rx,
434 .expired = dhcp_discovery_expired,
435 .tx_msgtype = DHCPDISCOVER,
436 .apply_min_timeout = 1,
440 * Construct transmitted packet for DHCP request
442 * @v dhcp DHCP session
443 * @v dhcppkt DHCP packet
444 * @v peer Destination address
446 static int dhcp_request_tx ( struct dhcp_session *dhcp,
447 struct dhcp_packet *dhcppkt,
448 struct sockaddr_in *peer ) {
451 DBGC ( dhcp, "DHCP %p DHCPREQUEST to %s:%d",
452 dhcp, inet_ntoa ( dhcp->server ), BOOTPS_PORT );
453 DBGC ( dhcp, " for %s\n", inet_ntoa ( dhcp->offer ) );
456 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
458 sizeof ( dhcp->server ) ) ) != 0 )
461 /* Set requested IP address */
462 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
464 sizeof ( dhcp->offer ) ) ) != 0 )
467 /* Set server address */
468 peer->sin_addr.s_addr = INADDR_BROADCAST;
469 peer->sin_port = htons ( BOOTPS_PORT );
475 * Handle received packet during DHCP request
477 * @v dhcp DHCP session
478 * @v dhcppkt DHCP packet
479 * @v peer DHCP server address
480 * @v msgtype DHCP message type
481 * @v server_id DHCP server ID
483 static void dhcp_request_rx ( struct dhcp_session *dhcp,
484 struct dhcp_packet *dhcppkt,
485 struct sockaddr_in *peer, uint8_t msgtype,
486 struct in_addr server_id ) {
488 struct settings *parent;
491 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
492 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
493 ntohs ( peer->sin_port ) );
494 if ( server_id.s_addr != peer->sin_addr.s_addr )
495 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
497 /* Identify leased IP address */
498 ip = dhcppkt->dhcphdr->yiaddr;
500 DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
503 /* Filter out unacceptable responses */
504 if ( peer->sin_port != htons ( BOOTPS_PORT ) )
506 if ( msgtype /* BOOTP */ && ( msgtype != DHCPACK ) )
508 if ( server_id.s_addr != dhcp->server.s_addr )
511 /* Record assigned address */
512 dhcp->local.sin_addr = ip;
514 /* Register settings */
515 parent = netdev_settings ( dhcp->netdev );
516 if ( ( rc = register_settings ( &dhcppkt->settings, parent ) ) != 0 ){
517 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
518 dhcp, strerror ( rc ) );
519 dhcp_finished ( dhcp, rc );
523 /* Start ProxyDHCPREQUEST if applicable */
524 if ( dhcp->proxy_server.s_addr && ( ! dhcp->no_pxedhcp ) ) {
525 dhcp_set_state ( dhcp, &dhcp_state_proxy );
530 dhcp_finished ( dhcp, 0 );
534 * Handle timer expiry during DHCP discovery
536 * @v dhcp DHCP session
538 static void dhcp_request_expired ( struct dhcp_session *dhcp ) {
540 /* Retransmit current packet */
544 /** DHCP request state operations */
545 static struct dhcp_session_state dhcp_state_request = {
547 .tx = dhcp_request_tx,
548 .rx = dhcp_request_rx,
549 .expired = dhcp_request_expired,
550 .tx_msgtype = DHCPREQUEST,
551 .apply_min_timeout = 0,
555 * Construct transmitted packet for ProxyDHCP request
557 * @v dhcp DHCP session
558 * @v dhcppkt DHCP packet
559 * @v peer Destination address
561 static int dhcp_proxy_tx ( struct dhcp_session *dhcp,
562 struct dhcp_packet *dhcppkt,
563 struct sockaddr_in *peer ) {
566 DBGC ( dhcp, "DHCP %p ProxyDHCP REQUEST to %s:%d\n",
567 dhcp, inet_ntoa ( dhcp->proxy_server ), PXE_PORT );
570 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
572 sizeof ( dhcp->proxy_server ) ) ) != 0 )
575 /* Set server address */
576 peer->sin_addr = dhcp->proxy_server;
577 peer->sin_port = htons ( PXE_PORT );
583 * Handle received packet during ProxyDHCP request
585 * @v dhcp DHCP session
586 * @v dhcppkt DHCP packet
587 * @v peer DHCP server address
588 * @v msgtype DHCP message type
589 * @v server_id DHCP server ID
591 static void dhcp_proxy_rx ( struct dhcp_session *dhcp,
592 struct dhcp_packet *dhcppkt,
593 struct sockaddr_in *peer, uint8_t msgtype,
594 struct in_addr server_id ) {
597 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
598 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
599 ntohs ( peer->sin_port ) );
600 if ( server_id.s_addr != peer->sin_addr.s_addr )
601 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
604 /* Filter out unacceptable responses */
605 if ( peer->sin_port != htons ( PXE_PORT ) )
607 if ( msgtype != DHCPACK )
609 if ( server_id.s_addr /* Linux PXE server omits server ID */ &&
610 ( server_id.s_addr != dhcp->proxy_server.s_addr ) )
613 /* Register settings */
614 dhcppkt->settings.name = PROXYDHCP_SETTINGS_NAME;
615 if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
616 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
617 dhcp, strerror ( rc ) );
618 dhcp_finished ( dhcp, rc );
623 dhcp_finished ( dhcp, 0 );
627 * Handle timer expiry during ProxyDHCP request
629 * @v dhcp DHCP session
631 static void dhcp_proxy_expired ( struct dhcp_session *dhcp ) {
632 unsigned long elapsed = ( currticks() - dhcp->start );
634 /* Give up waiting for ProxyDHCP before we reach the failure point */
635 if ( elapsed > PROXYDHCP_MAX_TIMEOUT ) {
636 dhcp_finished ( dhcp, 0 );
640 /* Retransmit current packet */
644 /** ProxyDHCP request state operations */
645 static struct dhcp_session_state dhcp_state_proxy = {
649 .expired = dhcp_proxy_expired,
650 .tx_msgtype = DHCPREQUEST,
651 .apply_min_timeout = 0,
655 * Construct transmitted packet for PXE Boot Server Discovery
657 * @v dhcp DHCP session
658 * @v dhcppkt DHCP packet
659 * @v peer Destination address
661 static int dhcp_pxebs_tx ( struct dhcp_session *dhcp,
662 struct dhcp_packet *dhcppkt,
663 struct sockaddr_in *peer ) {
664 struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
667 DBGC ( dhcp, "DHCP %p PXEBS REQUEST to %s:%d for type %d\n",
668 dhcp, inet_ntoa ( *(dhcp->pxe_attempt) ), PXE_PORT,
669 ntohs ( dhcp->pxe_type ) );
671 /* Set boot menu item */
672 menu_item.type = dhcp->pxe_type;
673 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
674 &menu_item, sizeof ( menu_item ) ) ) != 0 )
677 /* Set server address */
678 peer->sin_addr = *(dhcp->pxe_attempt);
679 peer->sin_port = htons ( PXE_PORT );
685 * Check to see if PXE Boot Server address is acceptable
687 * @v dhcp DHCP session
688 * @v bs Boot Server address
689 * @ret accept Boot Server is acceptable
691 static int dhcp_pxebs_accept ( struct dhcp_session *dhcp,
692 struct in_addr bs ) {
693 struct in_addr *accept;
695 /* Accept if we have no acceptance filter */
696 if ( ! dhcp->pxe_accept )
699 /* Scan through acceptance list */
700 for ( accept = dhcp->pxe_accept ; accept->s_addr ; accept++ ) {
701 if ( accept->s_addr == bs.s_addr )
705 DBGC ( dhcp, "DHCP %p rejecting server %s\n",
706 dhcp, inet_ntoa ( bs ) );
711 * Handle received packet during PXE Boot Server Discovery
713 * @v dhcp DHCP session
714 * @v dhcppkt DHCP packet
715 * @v peer DHCP server address
716 * @v msgtype DHCP message type
717 * @v server_id DHCP server ID
719 static void dhcp_pxebs_rx ( struct dhcp_session *dhcp,
720 struct dhcp_packet *dhcppkt,
721 struct sockaddr_in *peer, uint8_t msgtype,
722 struct in_addr server_id ) {
723 struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
726 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
727 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
728 ntohs ( peer->sin_port ) );
729 if ( server_id.s_addr != peer->sin_addr.s_addr )
730 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
732 /* Identify boot menu item */
733 dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
734 &menu_item, sizeof ( menu_item ) );
735 if ( menu_item.type )
736 DBGC ( dhcp, " for type %d", ntohs ( menu_item.type ) );
739 /* Filter out unacceptable responses */
740 if ( peer->sin_port != htons ( PXE_PORT ) )
742 if ( msgtype != DHCPACK )
744 if ( menu_item.type != dhcp->pxe_type )
746 if ( ! dhcp_pxebs_accept ( dhcp, ( server_id.s_addr ?
747 server_id : peer->sin_addr ) ) )
750 /* Register settings */
751 dhcppkt->settings.name = PXEBS_SETTINGS_NAME;
752 if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
753 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
754 dhcp, strerror ( rc ) );
755 dhcp_finished ( dhcp, rc );
760 dhcp_finished ( dhcp, 0 );
764 * Handle timer expiry during PXE Boot Server Discovery
766 * @v dhcp DHCP session
768 static void dhcp_pxebs_expired ( struct dhcp_session *dhcp ) {
769 unsigned long elapsed = ( currticks() - dhcp->start );
771 /* Give up waiting before we reach the failure point, and fail
772 * over to the next server in the attempt list
774 if ( elapsed > PXEBS_MAX_TIMEOUT ) {
776 if ( dhcp->pxe_attempt->s_addr ) {
777 dhcp_set_state ( dhcp, &dhcp_state_pxebs );
780 dhcp_finished ( dhcp, -ETIMEDOUT );
785 /* Retransmit current packet */
789 /** PXE Boot Server Discovery state operations */
790 static struct dhcp_session_state dhcp_state_pxebs = {
794 .expired = dhcp_pxebs_expired,
795 .tx_msgtype = DHCPREQUEST,
796 .apply_min_timeout = 1,
799 /****************************************************************************
801 * Packet construction
806 * Create a DHCP packet
808 * @v dhcppkt DHCP packet structure to fill in
809 * @v netdev Network device
810 * @v msgtype DHCP message type
811 * @v options Initial options to include (or NULL)
812 * @v options_len Length of initial options
813 * @v data Buffer for DHCP packet
814 * @v max_len Size of DHCP packet buffer
815 * @ret rc Return status code
817 * Creates a DHCP packet in the specified buffer, and initialise a
818 * DHCP packet structure.
820 int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
821 struct net_device *netdev, uint8_t msgtype,
822 const void *options, size_t options_len,
823 void *data, size_t max_len ) {
824 struct dhcphdr *dhcphdr = data;
829 if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
832 /* Initialise DHCP packet content */
833 memset ( dhcphdr, 0, max_len );
834 dhcphdr->xid = dhcp_xid ( netdev );
835 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
836 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
837 dhcphdr->op = dhcp_op[msgtype];
838 /* If hardware length exceeds the chaddr field length, don't
839 * use the chaddr field. This is as per RFC4390.
841 hlen = netdev->ll_protocol->ll_addr_len;
842 if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
844 dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
846 dhcphdr->hlen = hlen;
847 memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
848 memcpy ( dhcphdr->options, options, options_len );
850 /* Initialise DHCP packet structure */
851 memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
852 dhcppkt_init ( dhcppkt, data, max_len );
854 /* Set DHCP_MESSAGE_TYPE option */
855 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
856 &msgtype, sizeof ( msgtype ) ) ) != 0 )
863 * Create DHCP request packet
865 * @v dhcppkt DHCP packet structure to fill in
866 * @v netdev Network device
867 * @v msgtype DHCP message type
868 * @v ciaddr Client IP address
869 * @v data Buffer for DHCP packet
870 * @v max_len Size of DHCP packet buffer
871 * @ret rc Return status code
873 * Creates a DHCP request packet in the specified buffer, and
874 * initialise a DHCP packet structure.
876 int dhcp_create_request ( struct dhcp_packet *dhcppkt,
877 struct net_device *netdev, unsigned int msgtype,
878 struct in_addr ciaddr, void *data, size_t max_len ) {
879 struct device_description *desc = &netdev->dev->desc;
880 struct dhcp_netdev_desc dhcp_desc;
881 struct dhcp_client_id client_id;
882 struct dhcp_client_uuid client_uuid;
883 uint8_t *dhcp_features;
884 size_t dhcp_features_len;
889 /* Create DHCP packet */
890 if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
891 dhcp_request_options_data,
892 sizeof ( dhcp_request_options_data ),
893 data, max_len ) ) != 0 ) {
894 DBG ( "DHCP could not create DHCP packet: %s\n",
899 /* Set client IP address */
900 dhcppkt->dhcphdr->ciaddr = ciaddr;
902 /* Add options to identify the feature list */
903 dhcp_features = table_start ( DHCP_FEATURES );
904 dhcp_features_len = table_num_entries ( DHCP_FEATURES );
905 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
906 dhcp_features_len ) ) != 0 ) {
907 DBG ( "DHCP could not set features list option: %s\n",
912 /* Add options to identify the network device */
913 dhcp_desc.type = desc->bus_type;
914 dhcp_desc.vendor = htons ( desc->vendor );
915 dhcp_desc.device = htons ( desc->device );
916 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
917 sizeof ( dhcp_desc ) ) ) != 0 ) {
918 DBG ( "DHCP could not set bus ID option: %s\n",
923 /* Add DHCP client identifier. Required for Infiniband, and
924 * doesn't hurt other link layers.
926 client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
927 ll_addr_len = netdev->ll_protocol->ll_addr_len;
928 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
929 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
930 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
931 ( ll_addr_len + 1 ) ) ) != 0 ) {
932 DBG ( "DHCP could not set client ID: %s\n",
937 /* Add client UUID, if we have one. Required for PXE. */
938 client_uuid.type = DHCP_CLIENT_UUID_TYPE;
939 if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting,
940 &client_uuid.uuid ) ) >= 0 ) {
941 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
943 sizeof ( client_uuid ) ) ) != 0 ) {
944 DBG ( "DHCP could not set client UUID: %s\n",
950 /* Add user class, if we have one. */
951 if ( ( len = fetch_setting_len ( NULL, &user_class_setting ) ) >= 0 ) {
952 char user_class[len];
953 fetch_setting ( NULL, &user_class_setting, user_class,
954 sizeof ( user_class ) );
955 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_USER_CLASS_ID,
957 sizeof ( user_class ) ) ) != 0 ) {
958 DBG ( "DHCP could not set user class: %s\n",
967 /****************************************************************************
969 * Data transfer interface
974 * Transmit DHCP request
976 * @v dhcp DHCP session
977 * @ret rc Return status code
979 static int dhcp_tx ( struct dhcp_session *dhcp ) {
980 static struct sockaddr_in peer = {
981 .sin_family = AF_INET,
983 struct xfer_metadata meta = {
984 .netdev = dhcp->netdev,
985 .src = ( struct sockaddr * ) &dhcp->local,
986 .dest = ( struct sockaddr * ) &peer,
988 struct io_buffer *iobuf;
989 uint8_t msgtype = dhcp->state->tx_msgtype;
990 struct dhcp_packet dhcppkt;
993 /* Start retry timer. Do this first so that failures to
994 * transmit will be retried.
996 start_timer ( &dhcp->timer );
998 /* Allocate buffer for packet */
999 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
1003 /* Create basic DHCP packet in temporary buffer */
1004 if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev, msgtype,
1005 dhcp->local.sin_addr, iobuf->data,
1006 iob_tailroom ( iobuf ) ) ) != 0 ) {
1007 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
1008 dhcp, strerror ( rc ) );
1012 /* Fill in packet based on current state */
1013 if ( ( rc = dhcp->state->tx ( dhcp, &dhcppkt, &peer ) ) != 0 ) {
1014 DBGC ( dhcp, "DHCP %p could not fill DHCP request: %s\n",
1015 dhcp, strerror ( rc ) );
1019 /* Transmit the packet */
1020 iob_put ( iobuf, dhcppkt.len );
1021 if ( ( rc = xfer_deliver_iob_meta ( &dhcp->xfer, iob_disown ( iobuf ),
1023 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
1024 dhcp, strerror ( rc ) );
1036 * @v xfer Data transfer interface
1037 * @v iobuf I/O buffer
1038 * @v meta Transfer metadata
1039 * @ret rc Return status code
1041 static int dhcp_deliver_iob ( struct xfer_interface *xfer,
1042 struct io_buffer *iobuf,
1043 struct xfer_metadata *meta ) {
1044 struct dhcp_session *dhcp =
1045 container_of ( xfer, struct dhcp_session, xfer );
1046 struct sockaddr_in *peer;
1048 struct dhcp_packet *dhcppkt;
1049 struct dhcphdr *dhcphdr;
1050 uint8_t msgtype = 0;
1051 struct in_addr server_id = { 0 };
1055 if ( ! meta->src ) {
1056 DBGC ( dhcp, "DHCP %p received packet without source port\n",
1061 peer = ( struct sockaddr_in * ) meta->src;
1063 /* Create a DHCP packet containing the I/O buffer contents.
1064 * Whilst we could just use the original buffer in situ, that
1065 * would waste the unused space in the packet buffer, and also
1066 * waste a relatively scarce fully-aligned I/O buffer.
1068 data_len = iob_len ( iobuf );
1069 dhcppkt = zalloc ( sizeof ( *dhcppkt ) + data_len );
1072 goto err_alloc_dhcppkt;
1074 dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
1075 memcpy ( dhcphdr, iobuf->data, data_len );
1076 dhcppkt_init ( dhcppkt, dhcphdr, data_len );
1078 /* Identify message type */
1079 dhcppkt_fetch ( dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
1080 sizeof ( msgtype ) );
1082 /* Identify server ID */
1083 dhcppkt_fetch ( dhcppkt, DHCP_SERVER_IDENTIFIER,
1084 &server_id, sizeof ( server_id ) );
1086 /* Check for matching transaction ID */
1087 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
1088 DBGC ( dhcp, "DHCP %p %s from %s:%d has bad transaction "
1089 "ID\n", dhcp, dhcp_msgtype_name ( msgtype ),
1090 inet_ntoa ( peer->sin_addr ),
1091 ntohs ( peer->sin_port ) );
1096 /* Handle packet based on current state */
1097 dhcp->state->rx ( dhcp, dhcppkt, peer, msgtype, server_id );
1100 dhcppkt_put ( dhcppkt );
1107 /** DHCP data transfer interface operations */
1108 static struct xfer_interface_operations dhcp_xfer_operations = {
1109 .close = ignore_xfer_close,
1110 .vredirect = xfer_vopen,
1111 .window = unlimited_xfer_window,
1112 .alloc_iob = default_xfer_alloc_iob,
1113 .deliver_iob = dhcp_deliver_iob,
1114 .deliver_raw = xfer_deliver_as_iob,
1118 * Handle DHCP retry timer expiry
1120 * @v timer DHCP retry timer
1121 * @v fail Failure indicator
1123 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
1124 struct dhcp_session *dhcp =
1125 container_of ( timer, struct dhcp_session, timer );
1127 /* If we have failed, terminate DHCP */
1129 dhcp_finished ( dhcp, -ETIMEDOUT );
1133 /* Handle timer expiry based on current state */
1134 dhcp->state->expired ( dhcp );
1137 /****************************************************************************
1139 * Job control interface
1144 * Handle kill() event received via job control interface
1146 * @v job DHCP job control interface
1148 static void dhcp_job_kill ( struct job_interface *job ) {
1149 struct dhcp_session *dhcp =
1150 container_of ( job, struct dhcp_session, job );
1152 /* Terminate DHCP session */
1153 dhcp_finished ( dhcp, -ECANCELED );
1156 /** DHCP job control interface operations */
1157 static struct job_interface_operations dhcp_job_operations = {
1158 .done = ignore_job_done,
1159 .kill = dhcp_job_kill,
1160 .progress = ignore_job_progress,
1163 /****************************************************************************
1170 * DHCP peer address for socket opening
1172 * This is a dummy address; the only useful portion is the socket
1173 * family (so that we get a UDP connection). The DHCP client will set
1174 * the IP address and source port explicitly on each transmission.
1176 static struct sockaddr dhcp_peer = {
1177 .sa_family = AF_INET,
1181 * Start DHCP state machine on a network device
1183 * @v job Job control interface
1184 * @v netdev Network device
1185 * @ret rc Return status code
1187 * Starts DHCP on the specified network device. If successful, the
1188 * DHCPACK (and ProxyDHCPACK, if applicable) will be registered as
1191 int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
1192 struct dhcp_session *dhcp;
1195 /* Allocate and initialise structure */
1196 dhcp = zalloc ( sizeof ( *dhcp ) );
1199 dhcp->refcnt.free = dhcp_free;
1200 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1201 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1202 dhcp->netdev = netdev_get ( netdev );
1203 dhcp->local.sin_family = AF_INET;
1204 dhcp->local.sin_port = htons ( BOOTPC_PORT );
1205 dhcp->timer.expired = dhcp_timer_expired;
1207 /* Instantiate child objects and attach to our interfaces */
1208 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
1209 ( struct sockaddr * ) &dhcp->local ) ) != 0 )
1212 /* Enter DHCPDISCOVER state */
1213 dhcp_set_state ( dhcp, &dhcp_state_discover );
1215 /* Attach parent interface, mortalise self, and return */
1216 job_plug_plug ( &dhcp->job, job );
1217 ref_put ( &dhcp->refcnt );
1221 dhcp_finished ( dhcp, rc );
1222 ref_put ( &dhcp->refcnt );
1227 * Retrieve list of PXE boot servers for a given server type
1229 * @v dhcp DHCP session
1230 * @v raw DHCP PXE boot server list
1231 * @v raw_len Length of DHCP PXE boot server list
1232 * @v ip IP address list to fill in
1234 * The caller must ensure that the IP address list has sufficient
1237 static void pxebs_list ( struct dhcp_session *dhcp, void *raw,
1238 size_t raw_len, struct in_addr *ip ) {
1239 struct dhcp_pxe_boot_server *server = raw;
1244 if ( raw_len < sizeof ( *server ) ) {
1245 DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
1249 server_len = offsetof ( typeof ( *server ),
1250 ip[ server->num_ip ] );
1251 if ( raw_len < server_len ) {
1252 DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
1256 if ( server->type == dhcp->pxe_type ) {
1257 for ( i = 0 ; i < server->num_ip ; i++ )
1258 *(ip++) = server->ip[i];
1260 server = ( ( ( void * ) server ) + server_len );
1261 raw_len -= server_len;
1266 * Start PXE Boot Server Discovery on a network device
1268 * @v job Job control interface
1269 * @v netdev Network device
1270 * @v pxe_type PXE server type
1271 * @ret rc Return status code
1273 * Starts PXE Boot Server Discovery on the specified network device.
1274 * If successful, the Boot Server ACK will be registered as an option
1277 int start_pxebs ( struct job_interface *job, struct net_device *netdev,
1278 unsigned int pxe_type ) {
1279 struct setting pxe_discovery_control_setting =
1280 { .tag = DHCP_PXE_DISCOVERY_CONTROL };
1281 struct setting pxe_boot_servers_setting =
1282 { .tag = DHCP_PXE_BOOT_SERVERS };
1283 struct setting pxe_boot_server_mcast_setting =
1284 { .tag = DHCP_PXE_BOOT_SERVER_MCAST };
1285 ssize_t pxebs_list_len;
1286 struct dhcp_session *dhcp;
1288 unsigned int pxe_discovery_control;
1291 /* Get upper bound for PXE boot server IP address list */
1292 pxebs_list_len = fetch_setting_len ( NULL, &pxe_boot_servers_setting );
1293 if ( pxebs_list_len < 0 )
1296 /* Allocate and initialise structure */
1297 dhcp = zalloc ( sizeof ( *dhcp ) + sizeof ( *ip ) /* mcast */ +
1298 sizeof ( *ip ) /* bcast */ + pxebs_list_len +
1299 sizeof ( *ip ) /* terminator */ );
1302 dhcp->refcnt.free = dhcp_free;
1303 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1304 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1305 dhcp->netdev = netdev_get ( netdev );
1306 dhcp->local.sin_family = AF_INET;
1307 fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
1308 &dhcp->local.sin_addr );
1309 dhcp->local.sin_port = htons ( BOOTPC_PORT );
1310 dhcp->pxe_type = htons ( pxe_type );
1311 dhcp->timer.expired = dhcp_timer_expired;
1313 /* Construct PXE boot server IP address lists */
1314 pxe_discovery_control =
1315 fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
1316 ip = ( ( ( void * ) dhcp ) + sizeof ( *dhcp ) );
1317 dhcp->pxe_attempt = ip;
1318 if ( ! ( pxe_discovery_control & PXEBS_NO_MULTICAST ) ) {
1319 fetch_ipv4_setting ( NULL, &pxe_boot_server_mcast_setting, ip);
1323 if ( ! ( pxe_discovery_control & PXEBS_NO_BROADCAST ) )
1324 (ip++)->s_addr = INADDR_BROADCAST;
1325 if ( pxe_discovery_control & PXEBS_NO_UNKNOWN_SERVERS )
1326 dhcp->pxe_accept = ip;
1327 if ( pxebs_list_len ) {
1328 uint8_t buf[pxebs_list_len];
1330 fetch_setting ( NULL, &pxe_boot_servers_setting,
1331 buf, sizeof ( buf ) );
1332 pxebs_list ( dhcp, buf, sizeof ( buf ), ip );
1334 if ( ! dhcp->pxe_attempt->s_addr ) {
1335 DBGC ( dhcp, "DHCP %p has no PXE boot servers for type %04x\n",
1341 /* Dump out PXE server lists */
1342 DBGC ( dhcp, "DHCP %p attempting", dhcp );
1343 for ( ip = dhcp->pxe_attempt ; ip->s_addr ; ip++ )
1344 DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
1345 DBGC ( dhcp, "\n" );
1346 if ( dhcp->pxe_accept ) {
1347 DBGC ( dhcp, "DHCP %p accepting", dhcp );
1348 for ( ip = dhcp->pxe_accept ; ip->s_addr ; ip++ )
1349 DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
1350 DBGC ( dhcp, "\n" );
1353 /* Instantiate child objects and attach to our interfaces */
1354 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
1355 ( struct sockaddr * ) &dhcp->local ) ) != 0 )
1358 /* Enter PXEBS state */
1359 dhcp_set_state ( dhcp, &dhcp_state_pxebs );
1361 /* Attach parent interface, mortalise self, and return */
1362 job_plug_plug ( &dhcp->job, job );
1363 ref_put ( &dhcp->refcnt );
1367 dhcp_finished ( dhcp, rc );
1368 ref_put ( &dhcp->refcnt );