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.
19 FILE_LICENCE ( GPL2_OR_LATER );
28 #include <gpxe/if_ether.h>
29 #include <gpxe/netdevice.h>
30 #include <gpxe/device.h>
31 #include <gpxe/xfer.h>
32 #include <gpxe/open.h>
34 #include <gpxe/retry.h>
35 #include <gpxe/tcpip.h>
37 #include <gpxe/uuid.h>
38 #include <gpxe/timer.h>
39 #include <gpxe/settings.h>
40 #include <gpxe/dhcp.h>
41 #include <gpxe/dhcpopts.h>
42 #include <gpxe/dhcppkt.h>
43 #include <gpxe/features.h>
47 * Dynamic Host Configuration Protocol
52 static int dhcp_tx ( struct dhcp_session *dhcp );
55 * DHCP operation types
57 * This table maps from DHCP message types (i.e. values of the @c
58 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
61 static const uint8_t dhcp_op[] = {
62 [DHCPDISCOVER] = BOOTP_REQUEST,
63 [DHCPOFFER] = BOOTP_REPLY,
64 [DHCPREQUEST] = BOOTP_REQUEST,
65 [DHCPDECLINE] = BOOTP_REQUEST,
66 [DHCPACK] = BOOTP_REPLY,
67 [DHCPNAK] = BOOTP_REPLY,
68 [DHCPRELEASE] = BOOTP_REQUEST,
69 [DHCPINFORM] = BOOTP_REQUEST,
72 /** Raw option data for options common to all DHCP requests */
73 static uint8_t dhcp_request_options_data[] = {
74 DHCP_MESSAGE_TYPE, DHCP_BYTE ( 0 ),
75 DHCP_MAX_MESSAGE_SIZE,
76 DHCP_WORD ( ETH_MAX_MTU - 20 /* IP header */ - 8 /* UDP header */ ),
77 DHCP_CLIENT_ARCHITECTURE, DHCP_WORD ( 0 ),
78 DHCP_CLIENT_NDI, DHCP_OPTION ( 1 /* UNDI */ , 2, 1 /* v2.1 */ ),
80 DHCP_STRING ( 'P', 'X', 'E', 'C', 'l', 'i', 'e', 'n', 't', ':',
81 'A', 'r', 'c', 'h', ':', '0', '0', '0', '0', '0', ':',
82 'U', 'N', 'D', 'I', ':', '0', '0', '2', '0', '0', '1' ),
84 DHCP_STRING ( 'g', 'P', 'X', 'E' ),
85 DHCP_PARAMETER_REQUEST_LIST,
86 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
87 DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
88 DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
89 DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
90 DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
94 /** Version number feature */
95 FEATURE_VERSION ( VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH );
97 /** DHCP server address setting */
98 struct setting dhcp_server_setting __setting = {
99 .name = "dhcp-server",
100 .description = "DHCP server address",
101 .tag = DHCP_SERVER_IDENTIFIER,
102 .type = &setting_type_ipv4,
105 /** DHCP user class setting */
106 struct setting user_class_setting __setting = {
107 .name = "user-class",
108 .description = "User class identifier",
109 .tag = DHCP_USER_CLASS_ID,
110 .type = &setting_type_string,
113 /** Use cached network settings */
114 struct setting use_cached_setting __setting = {
115 .name = "use-cached",
116 .description = "Use cached network settings",
117 .tag = DHCP_EB_USE_CACHED,
118 .type = &setting_type_uint8,
122 * Name a DHCP packet type
124 * @v msgtype DHCP message type
125 * @ret string DHCP mesasge type name
127 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
129 case DHCPNONE: return "BOOTP"; /* Non-DHCP packet */
130 case DHCPDISCOVER: return "DHCPDISCOVER";
131 case DHCPOFFER: return "DHCPOFFER";
132 case DHCPREQUEST: return "DHCPREQUEST";
133 case DHCPDECLINE: return "DHCPDECLINE";
134 case DHCPACK: return "DHCPACK";
135 case DHCPNAK: return "DHCPNAK";
136 case DHCPRELEASE: return "DHCPRELEASE";
137 case DHCPINFORM: return "DHCPINFORM";
138 default: return "DHCP<invalid>";
143 * Calculate DHCP transaction ID for a network device
145 * @v netdev Network device
148 * Extract the least significant bits of the hardware address for use
149 * as the transaction ID.
151 static uint32_t dhcp_xid ( struct net_device *netdev ) {
154 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
155 - sizeof ( xid ) ), sizeof ( xid ) );
159 /****************************************************************************
167 /** DHCP session state operations */
168 struct dhcp_session_state {
172 * Construct transmitted packet
174 * @v dhcp DHCP session
175 * @v dhcppkt DHCP packet
176 * @v peer Destination address
178 int ( * tx ) ( struct dhcp_session *dhcp,
179 struct dhcp_packet *dhcppkt,
180 struct sockaddr_in *peer );
181 /** Handle received packet
183 * @v dhcp DHCP session
184 * @v dhcppkt DHCP packet
185 * @v peer DHCP server address
186 * @v msgtype DHCP message type
187 * @v server_id DHCP server ID
189 void ( * rx ) ( struct dhcp_session *dhcp,
190 struct dhcp_packet *dhcppkt,
191 struct sockaddr_in *peer,
192 uint8_t msgtype, struct in_addr server_id );
193 /** Handle timer expiry
195 * @v dhcp DHCP session
197 void ( * expired ) ( struct dhcp_session *dhcp );
198 /** Transmitted message type */
200 /** Apply minimum timeout */
201 uint8_t apply_min_timeout;
204 static struct dhcp_session_state dhcp_state_discover;
205 static struct dhcp_session_state dhcp_state_request;
206 static struct dhcp_session_state dhcp_state_proxy;
207 static struct dhcp_session_state dhcp_state_pxebs;
209 /** A DHCP session */
210 struct dhcp_session {
211 /** Reference counter */
212 struct refcnt refcnt;
213 /** Job control interface */
214 struct job_interface job;
215 /** Data transfer interface */
216 struct xfer_interface xfer;
218 /** Network device being configured */
219 struct net_device *netdev;
220 /** Local socket address */
221 struct sockaddr_in local;
222 /** State of the session */
223 struct dhcp_session_state *state;
225 /** Offered IP address */
226 struct in_addr offer;
228 struct in_addr server;
229 /** DHCP offer priority */
232 /** ProxyDHCP protocol extensions should be ignored */
234 /** ProxyDHCP server */
235 struct in_addr proxy_server;
236 /** ProxyDHCP port */
238 /** ProxyDHCP server priority */
241 /** PXE Boot Server type */
243 /** List of PXE Boot Servers to attempt */
244 struct in_addr *pxe_attempt;
245 /** List of PXE Boot Servers to accept */
246 struct in_addr *pxe_accept;
248 /** Retransmission timer */
249 struct retry_timer timer;
250 /** Start time of the current state (in ticks) */
257 * @v refcnt Reference counter
259 static void dhcp_free ( struct refcnt *refcnt ) {
260 struct dhcp_session *dhcp =
261 container_of ( refcnt, struct dhcp_session, refcnt );
263 netdev_put ( dhcp->netdev );
268 * Mark DHCP session as complete
270 * @v dhcp DHCP session
271 * @v rc Return status code
273 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
275 /* Block futher incoming messages */
276 job_nullify ( &dhcp->job );
277 xfer_nullify ( &dhcp->xfer );
279 /* Stop retry timer */
280 stop_timer ( &dhcp->timer );
282 /* Free resources and close interfaces */
283 xfer_close ( &dhcp->xfer, rc );
284 job_done ( &dhcp->job, rc );
288 * Transition to new DHCP session state
290 * @v dhcp DHCP session
291 * @v state New session state
293 static void dhcp_set_state ( struct dhcp_session *dhcp,
294 struct dhcp_session_state *state ) {
296 DBGC ( dhcp, "DHCP %p entering %s state\n", dhcp, state->name );
298 dhcp->start = currticks();
299 stop_timer ( &dhcp->timer );
300 dhcp->timer.min_timeout =
301 ( state->apply_min_timeout ? DHCP_MIN_TIMEOUT : 0 );
302 dhcp->timer.max_timeout = DHCP_MAX_TIMEOUT;
303 start_timer_nodelay ( &dhcp->timer );
306 /****************************************************************************
313 * Construct transmitted packet for DHCP discovery
315 * @v dhcp DHCP session
316 * @v dhcppkt DHCP packet
317 * @v peer Destination address
319 static int dhcp_discovery_tx ( struct dhcp_session *dhcp,
320 struct dhcp_packet *dhcppkt __unused,
321 struct sockaddr_in *peer ) {
323 DBGC ( dhcp, "DHCP %p DHCPDISCOVER\n", dhcp );
325 /* Set server address */
326 peer->sin_addr.s_addr = INADDR_BROADCAST;
327 peer->sin_port = htons ( BOOTPS_PORT );
333 * Handle received packet during DHCP discovery
335 * @v dhcp DHCP session
336 * @v dhcppkt DHCP packet
337 * @v peer DHCP server address
338 * @v msgtype DHCP message type
339 * @v server_id DHCP server ID
341 static void dhcp_discovery_rx ( struct dhcp_session *dhcp,
342 struct dhcp_packet *dhcppkt,
343 struct sockaddr_in *peer, uint8_t msgtype,
344 struct in_addr server_id ) {
346 char vci[9]; /* "PXEClient" */
352 uint8_t no_pxedhcp = 0;
353 unsigned long elapsed;
355 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
356 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
357 ntohs ( peer->sin_port ) );
358 if ( server_id.s_addr != peer->sin_addr.s_addr )
359 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
361 /* Identify offered IP address */
362 ip = dhcppkt->dhcphdr->yiaddr;
364 DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
366 /* Identify "PXEClient" vendor class */
367 vci_len = dhcppkt_fetch ( dhcppkt, DHCP_VENDOR_CLASS_ID,
368 vci, sizeof ( vci ) );
369 has_pxeclient = ( ( vci_len >= ( int ) sizeof ( vci ) ) &&
370 ( strncmp ( "PXEClient", vci, sizeof (vci) ) == 0 ));
372 /* Identify presence of PXE-specific options */
373 pxeopts_len = dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU, NULL, 0 );
374 has_pxeopts = ( pxeopts_len >= 0 );
376 DBGC ( dhcp, "%s", ( has_pxeopts ? " pxe" : " proxy" ) );
378 /* Identify priority */
379 dhcppkt_fetch ( dhcppkt, DHCP_EB_PRIORITY, &priority,
380 sizeof ( priority ) );
382 DBGC ( dhcp, " pri %d", priority );
384 /* Identify ignore-PXE flag */
385 dhcppkt_fetch ( dhcppkt, DHCP_EB_NO_PXEDHCP, &no_pxedhcp,
386 sizeof ( no_pxedhcp ) );
388 DBGC ( dhcp, " nopxe" );
391 /* Select as DHCP offer, if applicable */
392 if ( ip.s_addr && ( peer->sin_port == htons ( BOOTPS_PORT ) ) &&
393 ( ( msgtype == DHCPOFFER ) || ( ! msgtype /* BOOTP */ ) ) &&
394 ( priority >= dhcp->priority ) ) {
396 dhcp->server = server_id;
397 dhcp->priority = priority;
398 dhcp->no_pxedhcp = no_pxedhcp;
401 /* Select as ProxyDHCP offer, if applicable */
402 if ( has_pxeclient && ( msgtype == DHCPOFFER ) &&
403 ( priority >= dhcp->proxy_priority ) ) {
404 /* If the offer already includes the PXE options, then
405 * assume that we can send the ProxyDHCPREQUEST to
406 * port 67 (since the DHCPDISCOVER that triggered this
407 * ProxyDHCPOFFER was sent to port 67). Otherwise,
408 * send the ProxyDHCPREQUEST to port 4011.
410 dhcp->proxy_server = server_id;
411 dhcp->proxy_port = ( has_pxeopts ? htons ( BOOTPS_PORT )
412 : htons ( PXE_PORT ) );
413 dhcp->proxy_priority = priority;
416 /* We can exit the discovery state when we have a valid
417 * DHCPOFFER, and either:
419 * o The DHCPOFFER instructs us to ignore ProxyDHCPOFFERs, or
420 * o We have a valid ProxyDHCPOFFER, or
421 * o We have allowed sufficient time for ProxyDHCPOFFERs.
424 /* If we don't yet have a DHCPOFFER, do nothing */
425 if ( ! dhcp->offer.s_addr )
428 /* If we can't yet transition to DHCPREQUEST, do nothing */
429 elapsed = ( currticks() - dhcp->start );
430 if ( ! ( dhcp->no_pxedhcp || dhcp->proxy_server.s_addr ||
431 ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) )
434 /* Transition to DHCPREQUEST */
435 dhcp_set_state ( dhcp, &dhcp_state_request );
439 * Handle timer expiry during DHCP discovery
441 * @v dhcp DHCP session
443 static void dhcp_discovery_expired ( struct dhcp_session *dhcp ) {
444 unsigned long elapsed = ( currticks() - dhcp->start );
446 /* Give up waiting for ProxyDHCP before we reach the failure point */
447 if ( dhcp->offer.s_addr && ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) {
448 dhcp_set_state ( dhcp, &dhcp_state_request );
452 /* Otherwise, retransmit current packet */
456 /** DHCP discovery state operations */
457 static struct dhcp_session_state dhcp_state_discover = {
459 .tx = dhcp_discovery_tx,
460 .rx = dhcp_discovery_rx,
461 .expired = dhcp_discovery_expired,
462 .tx_msgtype = DHCPDISCOVER,
463 .apply_min_timeout = 1,
467 * Construct transmitted packet for DHCP request
469 * @v dhcp DHCP session
470 * @v dhcppkt DHCP packet
471 * @v peer Destination address
473 static int dhcp_request_tx ( struct dhcp_session *dhcp,
474 struct dhcp_packet *dhcppkt,
475 struct sockaddr_in *peer ) {
478 DBGC ( dhcp, "DHCP %p DHCPREQUEST to %s:%d",
479 dhcp, inet_ntoa ( dhcp->server ), BOOTPS_PORT );
480 DBGC ( dhcp, " for %s\n", inet_ntoa ( dhcp->offer ) );
483 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
485 sizeof ( dhcp->server ) ) ) != 0 )
488 /* Set requested IP address */
489 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
491 sizeof ( dhcp->offer ) ) ) != 0 )
494 /* Set server address */
495 peer->sin_addr.s_addr = INADDR_BROADCAST;
496 peer->sin_port = htons ( BOOTPS_PORT );
502 * Handle received packet during DHCP request
504 * @v dhcp DHCP session
505 * @v dhcppkt DHCP packet
506 * @v peer DHCP server address
507 * @v msgtype DHCP message type
508 * @v server_id DHCP server ID
510 static void dhcp_request_rx ( struct dhcp_session *dhcp,
511 struct dhcp_packet *dhcppkt,
512 struct sockaddr_in *peer, uint8_t msgtype,
513 struct in_addr server_id ) {
515 struct settings *parent;
518 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
519 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
520 ntohs ( peer->sin_port ) );
521 if ( server_id.s_addr != peer->sin_addr.s_addr )
522 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
524 /* Identify leased IP address */
525 ip = dhcppkt->dhcphdr->yiaddr;
527 DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
530 /* Filter out unacceptable responses */
531 if ( peer->sin_port != htons ( BOOTPS_PORT ) )
533 if ( msgtype /* BOOTP */ && ( msgtype != DHCPACK ) )
535 if ( server_id.s_addr != dhcp->server.s_addr )
538 /* Record assigned address */
539 dhcp->local.sin_addr = ip;
541 /* Register settings */
542 parent = netdev_settings ( dhcp->netdev );
543 if ( ( rc = register_settings ( &dhcppkt->settings, parent ) ) != 0 ){
544 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
545 dhcp, strerror ( rc ) );
546 dhcp_finished ( dhcp, rc );
550 /* Start ProxyDHCPREQUEST if applicable */
551 if ( dhcp->proxy_server.s_addr /* Have ProxyDHCP server */ &&
552 ( ! dhcp->no_pxedhcp ) /* ProxyDHCP not disabled */ &&
553 ( /* ProxyDHCP server is not just the DHCP server itself */
554 ( dhcp->proxy_server.s_addr != dhcp->server.s_addr ) ||
555 ( dhcp->proxy_port != htons ( BOOTPS_PORT ) ) ) ) {
556 dhcp_set_state ( dhcp, &dhcp_state_proxy );
561 dhcp_finished ( dhcp, 0 );
565 * Handle timer expiry during DHCP discovery
567 * @v dhcp DHCP session
569 static void dhcp_request_expired ( struct dhcp_session *dhcp ) {
571 /* Retransmit current packet */
575 /** DHCP request state operations */
576 static struct dhcp_session_state dhcp_state_request = {
578 .tx = dhcp_request_tx,
579 .rx = dhcp_request_rx,
580 .expired = dhcp_request_expired,
581 .tx_msgtype = DHCPREQUEST,
582 .apply_min_timeout = 0,
586 * Construct transmitted packet for ProxyDHCP request
588 * @v dhcp DHCP session
589 * @v dhcppkt DHCP packet
590 * @v peer Destination address
592 static int dhcp_proxy_tx ( struct dhcp_session *dhcp,
593 struct dhcp_packet *dhcppkt,
594 struct sockaddr_in *peer ) {
597 DBGC ( dhcp, "DHCP %p ProxyDHCP REQUEST to %s:%d\n", dhcp,
598 inet_ntoa ( dhcp->proxy_server ), ntohs ( dhcp->proxy_port ) );
601 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
603 sizeof ( dhcp->proxy_server ) ) ) != 0 )
606 /* Set server address */
607 peer->sin_addr = dhcp->proxy_server;
608 peer->sin_port = dhcp->proxy_port;
614 * Handle received packet during ProxyDHCP request
616 * @v dhcp DHCP session
617 * @v dhcppkt DHCP packet
618 * @v peer DHCP server address
619 * @v msgtype DHCP message type
620 * @v server_id DHCP server ID
622 static void dhcp_proxy_rx ( struct dhcp_session *dhcp,
623 struct dhcp_packet *dhcppkt,
624 struct sockaddr_in *peer, uint8_t msgtype,
625 struct in_addr server_id ) {
628 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
629 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
630 ntohs ( peer->sin_port ) );
631 if ( server_id.s_addr != peer->sin_addr.s_addr )
632 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
635 /* Filter out unacceptable responses */
636 if ( peer->sin_port != dhcp->proxy_port )
638 if ( ( msgtype != DHCPOFFER ) && ( msgtype != DHCPACK ) )
640 if ( server_id.s_addr /* Linux PXE server omits server ID */ &&
641 ( server_id.s_addr != dhcp->proxy_server.s_addr ) )
644 /* Register settings */
645 dhcppkt->settings.name = PROXYDHCP_SETTINGS_NAME;
646 if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
647 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
648 dhcp, strerror ( rc ) );
649 dhcp_finished ( dhcp, rc );
654 dhcp_finished ( dhcp, 0 );
658 * Handle timer expiry during ProxyDHCP request
660 * @v dhcp DHCP session
662 static void dhcp_proxy_expired ( struct dhcp_session *dhcp ) {
663 unsigned long elapsed = ( currticks() - dhcp->start );
665 /* Give up waiting for ProxyDHCP before we reach the failure point */
666 if ( elapsed > PROXYDHCP_MAX_TIMEOUT ) {
667 dhcp_finished ( dhcp, 0 );
671 /* Retransmit current packet */
675 /** ProxyDHCP request state operations */
676 static struct dhcp_session_state dhcp_state_proxy = {
680 .expired = dhcp_proxy_expired,
681 .tx_msgtype = DHCPREQUEST,
682 .apply_min_timeout = 0,
686 * Construct transmitted packet for PXE Boot Server Discovery
688 * @v dhcp DHCP session
689 * @v dhcppkt DHCP packet
690 * @v peer Destination address
692 static int dhcp_pxebs_tx ( struct dhcp_session *dhcp,
693 struct dhcp_packet *dhcppkt,
694 struct sockaddr_in *peer ) {
695 struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
698 /* Set server address */
699 peer->sin_addr = *(dhcp->pxe_attempt);
700 peer->sin_port = ( ( peer->sin_addr.s_addr == INADDR_BROADCAST ) ?
701 htons ( BOOTPS_PORT ) : htons ( PXE_PORT ) );
703 DBGC ( dhcp, "DHCP %p PXEBS REQUEST to %s:%d for type %d\n",
704 dhcp, inet_ntoa ( peer->sin_addr ), ntohs ( peer->sin_port ),
705 le16_to_cpu ( dhcp->pxe_type ) );
707 /* Set boot menu item */
708 menu_item.type = dhcp->pxe_type;
709 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
710 &menu_item, sizeof ( menu_item ) ) ) != 0 )
717 * Check to see if PXE Boot Server address is acceptable
719 * @v dhcp DHCP session
720 * @v bs Boot Server address
721 * @ret accept Boot Server is acceptable
723 static int dhcp_pxebs_accept ( struct dhcp_session *dhcp,
724 struct in_addr bs ) {
725 struct in_addr *accept;
727 /* Accept if we have no acceptance filter */
728 if ( ! dhcp->pxe_accept )
731 /* Scan through acceptance list */
732 for ( accept = dhcp->pxe_accept ; accept->s_addr ; accept++ ) {
733 if ( accept->s_addr == bs.s_addr )
737 DBGC ( dhcp, "DHCP %p rejecting server %s\n",
738 dhcp, inet_ntoa ( bs ) );
743 * Handle received packet during PXE Boot Server Discovery
745 * @v dhcp DHCP session
746 * @v dhcppkt DHCP packet
747 * @v peer DHCP server address
748 * @v msgtype DHCP message type
749 * @v server_id DHCP server ID
751 static void dhcp_pxebs_rx ( struct dhcp_session *dhcp,
752 struct dhcp_packet *dhcppkt,
753 struct sockaddr_in *peer, uint8_t msgtype,
754 struct in_addr server_id ) {
755 struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
758 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
759 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
760 ntohs ( peer->sin_port ) );
761 if ( server_id.s_addr != peer->sin_addr.s_addr )
762 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
764 /* Identify boot menu item */
765 dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
766 &menu_item, sizeof ( menu_item ) );
767 if ( menu_item.type )
768 DBGC ( dhcp, " for type %d", ntohs ( menu_item.type ) );
771 /* Filter out unacceptable responses */
772 if ( ( peer->sin_port != htons ( BOOTPS_PORT ) ) &&
773 ( peer->sin_port != htons ( PXE_PORT ) ) )
775 if ( msgtype != DHCPACK )
777 if ( menu_item.type != dhcp->pxe_type )
779 if ( ! dhcp_pxebs_accept ( dhcp, ( server_id.s_addr ?
780 server_id : peer->sin_addr ) ) )
783 /* Register settings */
784 dhcppkt->settings.name = PXEBS_SETTINGS_NAME;
785 if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
786 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
787 dhcp, strerror ( rc ) );
788 dhcp_finished ( dhcp, rc );
793 dhcp_finished ( dhcp, 0 );
797 * Handle timer expiry during PXE Boot Server Discovery
799 * @v dhcp DHCP session
801 static void dhcp_pxebs_expired ( struct dhcp_session *dhcp ) {
802 unsigned long elapsed = ( currticks() - dhcp->start );
804 /* Give up waiting before we reach the failure point, and fail
805 * over to the next server in the attempt list
807 if ( elapsed > PXEBS_MAX_TIMEOUT ) {
809 if ( dhcp->pxe_attempt->s_addr ) {
810 dhcp_set_state ( dhcp, &dhcp_state_pxebs );
813 dhcp_finished ( dhcp, -ETIMEDOUT );
818 /* Retransmit current packet */
822 /** PXE Boot Server Discovery state operations */
823 static struct dhcp_session_state dhcp_state_pxebs = {
827 .expired = dhcp_pxebs_expired,
828 .tx_msgtype = DHCPREQUEST,
829 .apply_min_timeout = 1,
832 /****************************************************************************
834 * Packet construction
839 * Construct DHCP client hardware address field and broadcast flag
841 * @v netdev Network device
842 * @v hlen DHCP hardware address length to fill in
843 * @v flags DHCP flags to fill in
844 * @ret chaddr DHCP client hardware address
846 void * dhcp_chaddr ( struct net_device *netdev, uint8_t *hlen,
848 struct ll_protocol *ll_protocol = netdev->ll_protocol;
849 typeof ( ( ( struct dhcphdr * ) NULL )->chaddr ) chaddr;
851 /* If the link-layer address cannot fit into the chaddr field
852 * (as is the case for IPoIB) then try using the hardware
853 * address instead. If we do this, set the broadcast flag,
854 * since chaddr then does not represent a valid link-layer
855 * address for the return path.
857 * If even the hardware address is too large, use an empty
858 * chaddr field and set the broadcast flag.
860 * This goes against RFC4390, but RFC4390 mandates that we use
861 * a DHCP client identifier that conforms with RFC4361, which
862 * we cannot do without either persistent (NIC-independent)
863 * storage, or by eliminating the hardware address completely
864 * from the DHCP packet, which seems unfriendly to users.
866 if ( ( *hlen = ll_protocol->ll_addr_len ) <= sizeof ( chaddr ) ) {
867 return netdev->ll_addr;
869 *flags = htons ( BOOTP_FL_BROADCAST );
870 if ( ( *hlen = ll_protocol->hw_addr_len ) <= sizeof ( chaddr ) ) {
871 return netdev->hw_addr;
879 * Create a DHCP packet
881 * @v dhcppkt DHCP packet structure to fill in
882 * @v netdev Network device
883 * @v msgtype DHCP message type
884 * @v options Initial options to include (or NULL)
885 * @v options_len Length of initial options
886 * @v data Buffer for DHCP packet
887 * @v max_len Size of DHCP packet buffer
888 * @ret rc Return status code
890 * Creates a DHCP packet in the specified buffer, and initialise a
891 * DHCP packet structure.
893 int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
894 struct net_device *netdev, uint8_t msgtype,
895 const void *options, size_t options_len,
896 void *data, size_t max_len ) {
897 struct dhcphdr *dhcphdr = data;
902 if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
905 /* Initialise DHCP packet content */
906 memset ( dhcphdr, 0, max_len );
907 dhcphdr->xid = dhcp_xid ( netdev );
908 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
909 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
910 dhcphdr->op = dhcp_op[msgtype];
911 chaddr = dhcp_chaddr ( netdev, &dhcphdr->hlen, &dhcphdr->flags );
912 memcpy ( dhcphdr->chaddr, chaddr, dhcphdr->hlen );
913 memcpy ( dhcphdr->options, options, options_len );
915 /* Initialise DHCP packet structure */
916 memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
917 dhcppkt_init ( dhcppkt, data, max_len );
919 /* Set DHCP_MESSAGE_TYPE option */
920 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
921 &msgtype, sizeof ( msgtype ) ) ) != 0 )
928 * Create DHCP request packet
930 * @v dhcppkt DHCP packet structure to fill in
931 * @v netdev Network device
932 * @v msgtype DHCP message type
933 * @v ciaddr Client IP address
934 * @v data Buffer for DHCP packet
935 * @v max_len Size of DHCP packet buffer
936 * @ret rc Return status code
938 * Creates a DHCP request packet in the specified buffer, and
939 * initialise a DHCP packet structure.
941 int dhcp_create_request ( struct dhcp_packet *dhcppkt,
942 struct net_device *netdev, unsigned int msgtype,
943 struct in_addr ciaddr, void *data, size_t max_len ) {
944 struct dhcp_netdev_desc dhcp_desc;
945 struct dhcp_client_id client_id;
946 struct dhcp_client_uuid client_uuid;
947 uint8_t *dhcp_features;
948 size_t dhcp_features_len;
953 /* Create DHCP packet */
954 if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
955 dhcp_request_options_data,
956 sizeof ( dhcp_request_options_data ),
957 data, max_len ) ) != 0 ) {
958 DBG ( "DHCP could not create DHCP packet: %s\n",
963 /* Set client IP address */
964 dhcppkt->dhcphdr->ciaddr = ciaddr;
966 /* Add options to identify the feature list */
967 dhcp_features = table_start ( DHCP_FEATURES );
968 dhcp_features_len = table_num_entries ( DHCP_FEATURES );
969 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
970 dhcp_features_len ) ) != 0 ) {
971 DBG ( "DHCP could not set features list option: %s\n",
976 /* Add options to identify the network device */
977 fetch_setting ( &netdev->settings.settings, &busid_setting, &dhcp_desc,
978 sizeof ( dhcp_desc ) );
979 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
980 sizeof ( dhcp_desc ) ) ) != 0 ) {
981 DBG ( "DHCP could not set bus ID option: %s\n",
986 /* Add DHCP client identifier. Required for Infiniband, and
987 * doesn't hurt other link layers.
989 client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
990 ll_addr_len = netdev->ll_protocol->ll_addr_len;
991 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
992 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
993 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
994 ( ll_addr_len + 1 ) ) ) != 0 ) {
995 DBG ( "DHCP could not set client ID: %s\n",
1000 /* Add client UUID, if we have one. Required for PXE. */
1001 client_uuid.type = DHCP_CLIENT_UUID_TYPE;
1002 if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting,
1003 &client_uuid.uuid ) ) >= 0 ) {
1004 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
1006 sizeof ( client_uuid ) ) ) != 0 ) {
1007 DBG ( "DHCP could not set client UUID: %s\n",
1013 /* Add user class, if we have one. */
1014 if ( ( len = fetch_setting_len ( NULL, &user_class_setting ) ) >= 0 ) {
1015 char user_class[len];
1016 fetch_setting ( NULL, &user_class_setting, user_class,
1017 sizeof ( user_class ) );
1018 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_USER_CLASS_ID,
1020 sizeof ( user_class ) ) ) != 0 ) {
1021 DBG ( "DHCP could not set user class: %s\n",
1030 /****************************************************************************
1032 * Data transfer interface
1037 * Transmit DHCP request
1039 * @v dhcp DHCP session
1040 * @ret rc Return status code
1042 static int dhcp_tx ( struct dhcp_session *dhcp ) {
1043 static struct sockaddr_in peer = {
1044 .sin_family = AF_INET,
1046 struct xfer_metadata meta = {
1047 .netdev = dhcp->netdev,
1048 .src = ( struct sockaddr * ) &dhcp->local,
1049 .dest = ( struct sockaddr * ) &peer,
1051 struct io_buffer *iobuf;
1052 uint8_t msgtype = dhcp->state->tx_msgtype;
1053 struct dhcp_packet dhcppkt;
1056 /* Start retry timer. Do this first so that failures to
1057 * transmit will be retried.
1059 start_timer ( &dhcp->timer );
1061 /* Allocate buffer for packet */
1062 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
1066 /* Create basic DHCP packet in temporary buffer */
1067 if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev, msgtype,
1068 dhcp->local.sin_addr, iobuf->data,
1069 iob_tailroom ( iobuf ) ) ) != 0 ) {
1070 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
1071 dhcp, strerror ( rc ) );
1075 /* Fill in packet based on current state */
1076 if ( ( rc = dhcp->state->tx ( dhcp, &dhcppkt, &peer ) ) != 0 ) {
1077 DBGC ( dhcp, "DHCP %p could not fill DHCP request: %s\n",
1078 dhcp, strerror ( rc ) );
1082 /* Transmit the packet */
1083 iob_put ( iobuf, dhcppkt.len );
1084 if ( ( rc = xfer_deliver_iob_meta ( &dhcp->xfer, iob_disown ( iobuf ),
1086 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
1087 dhcp, strerror ( rc ) );
1099 * @v xfer Data transfer interface
1100 * @v iobuf I/O buffer
1101 * @v meta Transfer metadata
1102 * @ret rc Return status code
1104 static int dhcp_deliver_iob ( struct xfer_interface *xfer,
1105 struct io_buffer *iobuf,
1106 struct xfer_metadata *meta ) {
1107 struct dhcp_session *dhcp =
1108 container_of ( xfer, struct dhcp_session, xfer );
1109 struct sockaddr_in *peer;
1111 struct dhcp_packet *dhcppkt;
1112 struct dhcphdr *dhcphdr;
1113 uint8_t msgtype = 0;
1114 struct in_addr server_id = { 0 };
1118 if ( ! meta->src ) {
1119 DBGC ( dhcp, "DHCP %p received packet without source port\n",
1124 peer = ( struct sockaddr_in * ) meta->src;
1126 /* Create a DHCP packet containing the I/O buffer contents.
1127 * Whilst we could just use the original buffer in situ, that
1128 * would waste the unused space in the packet buffer, and also
1129 * waste a relatively scarce fully-aligned I/O buffer.
1131 data_len = iob_len ( iobuf );
1132 dhcppkt = zalloc ( sizeof ( *dhcppkt ) + data_len );
1135 goto err_alloc_dhcppkt;
1137 dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
1138 memcpy ( dhcphdr, iobuf->data, data_len );
1139 dhcppkt_init ( dhcppkt, dhcphdr, data_len );
1141 /* Identify message type */
1142 dhcppkt_fetch ( dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
1143 sizeof ( msgtype ) );
1145 /* Identify server ID */
1146 dhcppkt_fetch ( dhcppkt, DHCP_SERVER_IDENTIFIER,
1147 &server_id, sizeof ( server_id ) );
1149 /* Check for matching transaction ID */
1150 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
1151 DBGC ( dhcp, "DHCP %p %s from %s:%d has bad transaction "
1152 "ID\n", dhcp, dhcp_msgtype_name ( msgtype ),
1153 inet_ntoa ( peer->sin_addr ),
1154 ntohs ( peer->sin_port ) );
1159 /* Handle packet based on current state */
1160 dhcp->state->rx ( dhcp, dhcppkt, peer, msgtype, server_id );
1163 dhcppkt_put ( dhcppkt );
1170 /** DHCP data transfer interface operations */
1171 static struct xfer_interface_operations dhcp_xfer_operations = {
1172 .close = ignore_xfer_close,
1173 .vredirect = xfer_vreopen,
1174 .window = unlimited_xfer_window,
1175 .alloc_iob = default_xfer_alloc_iob,
1176 .deliver_iob = dhcp_deliver_iob,
1177 .deliver_raw = xfer_deliver_as_iob,
1181 * Handle DHCP retry timer expiry
1183 * @v timer DHCP retry timer
1184 * @v fail Failure indicator
1186 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
1187 struct dhcp_session *dhcp =
1188 container_of ( timer, struct dhcp_session, timer );
1190 /* If we have failed, terminate DHCP */
1192 dhcp_finished ( dhcp, -ETIMEDOUT );
1196 /* Handle timer expiry based on current state */
1197 dhcp->state->expired ( dhcp );
1200 /****************************************************************************
1202 * Job control interface
1207 * Handle kill() event received via job control interface
1209 * @v job DHCP job control interface
1211 static void dhcp_job_kill ( struct job_interface *job ) {
1212 struct dhcp_session *dhcp =
1213 container_of ( job, struct dhcp_session, job );
1215 /* Terminate DHCP session */
1216 dhcp_finished ( dhcp, -ECANCELED );
1219 /** DHCP job control interface operations */
1220 static struct job_interface_operations dhcp_job_operations = {
1221 .done = ignore_job_done,
1222 .kill = dhcp_job_kill,
1223 .progress = ignore_job_progress,
1226 /****************************************************************************
1233 * DHCP peer address for socket opening
1235 * This is a dummy address; the only useful portion is the socket
1236 * family (so that we get a UDP connection). The DHCP client will set
1237 * the IP address and source port explicitly on each transmission.
1239 static struct sockaddr dhcp_peer = {
1240 .sa_family = AF_INET,
1244 * Start DHCP state machine on a network device
1246 * @v job Job control interface
1247 * @v netdev Network device
1248 * @ret rc Return status code, or positive if cached
1250 * Starts DHCP on the specified network device. If successful, the
1251 * DHCPACK (and ProxyDHCPACK, if applicable) will be registered as
1254 * On a return of 0, a background job has been started to perform the
1255 * DHCP request. Any nonzero return means the job has not been
1256 * started; a positive return value indicates the success condition of
1257 * having fetched the appropriate data from cached information.
1259 int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
1260 struct dhcp_session *dhcp;
1263 /* Check for cached DHCP information */
1264 get_cached_dhcpack();
1265 if ( fetch_uintz_setting ( NULL, &use_cached_setting ) ) {
1266 DBG ( "DHCP using cached network settings\n" );
1270 /* Allocate and initialise structure */
1271 dhcp = zalloc ( sizeof ( *dhcp ) );
1274 dhcp->refcnt.free = dhcp_free;
1275 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1276 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1277 dhcp->netdev = netdev_get ( netdev );
1278 dhcp->local.sin_family = AF_INET;
1279 dhcp->local.sin_port = htons ( BOOTPC_PORT );
1280 dhcp->timer.expired = dhcp_timer_expired;
1282 /* Instantiate child objects and attach to our interfaces */
1283 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
1284 ( struct sockaddr * ) &dhcp->local ) ) != 0 )
1287 /* Enter DHCPDISCOVER state */
1288 dhcp_set_state ( dhcp, &dhcp_state_discover );
1290 /* Attach parent interface, mortalise self, and return */
1291 job_plug_plug ( &dhcp->job, job );
1292 ref_put ( &dhcp->refcnt );
1296 dhcp_finished ( dhcp, rc );
1297 ref_put ( &dhcp->refcnt );
1302 * Retrieve list of PXE boot servers for a given server type
1304 * @v dhcp DHCP session
1305 * @v raw DHCP PXE boot server list
1306 * @v raw_len Length of DHCP PXE boot server list
1307 * @v ip IP address list to fill in
1309 * The caller must ensure that the IP address list has sufficient
1312 static void pxebs_list ( struct dhcp_session *dhcp, void *raw,
1313 size_t raw_len, struct in_addr *ip ) {
1314 struct dhcp_pxe_boot_server *server = raw;
1319 if ( raw_len < sizeof ( *server ) ) {
1320 DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
1324 server_len = offsetof ( typeof ( *server ),
1325 ip[ server->num_ip ] );
1326 if ( raw_len < server_len ) {
1327 DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
1331 if ( server->type == dhcp->pxe_type ) {
1332 for ( i = 0 ; i < server->num_ip ; i++ )
1333 *(ip++) = server->ip[i];
1335 server = ( ( ( void * ) server ) + server_len );
1336 raw_len -= server_len;
1341 * Start PXE Boot Server Discovery on a network device
1343 * @v job Job control interface
1344 * @v netdev Network device
1345 * @v pxe_type PXE server type
1346 * @ret rc Return status code
1348 * Starts PXE Boot Server Discovery on the specified network device.
1349 * If successful, the Boot Server ACK will be registered as an option
1352 int start_pxebs ( struct job_interface *job, struct net_device *netdev,
1353 unsigned int pxe_type ) {
1354 struct setting pxe_discovery_control_setting =
1355 { .tag = DHCP_PXE_DISCOVERY_CONTROL };
1356 struct setting pxe_boot_servers_setting =
1357 { .tag = DHCP_PXE_BOOT_SERVERS };
1358 struct setting pxe_boot_server_mcast_setting =
1359 { .tag = DHCP_PXE_BOOT_SERVER_MCAST };
1360 ssize_t pxebs_list_len;
1361 struct dhcp_session *dhcp;
1363 unsigned int pxe_discovery_control;
1366 /* Get upper bound for PXE boot server IP address list */
1367 pxebs_list_len = fetch_setting_len ( NULL, &pxe_boot_servers_setting );
1368 if ( pxebs_list_len < 0 )
1371 /* Allocate and initialise structure */
1372 dhcp = zalloc ( sizeof ( *dhcp ) + sizeof ( *ip ) /* mcast */ +
1373 sizeof ( *ip ) /* bcast */ + pxebs_list_len +
1374 sizeof ( *ip ) /* terminator */ );
1377 dhcp->refcnt.free = dhcp_free;
1378 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1379 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1380 dhcp->netdev = netdev_get ( netdev );
1381 dhcp->local.sin_family = AF_INET;
1382 fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
1383 &dhcp->local.sin_addr );
1384 dhcp->local.sin_port = htons ( BOOTPC_PORT );
1385 dhcp->pxe_type = cpu_to_le16 ( pxe_type );
1386 dhcp->timer.expired = dhcp_timer_expired;
1388 /* Construct PXE boot server IP address lists */
1389 pxe_discovery_control =
1390 fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
1391 ip = ( ( ( void * ) dhcp ) + sizeof ( *dhcp ) );
1392 dhcp->pxe_attempt = ip;
1393 if ( ! ( pxe_discovery_control & PXEBS_NO_MULTICAST ) ) {
1394 fetch_ipv4_setting ( NULL, &pxe_boot_server_mcast_setting, ip);
1398 if ( ! ( pxe_discovery_control & PXEBS_NO_BROADCAST ) )
1399 (ip++)->s_addr = INADDR_BROADCAST;
1400 if ( pxe_discovery_control & PXEBS_NO_UNKNOWN_SERVERS )
1401 dhcp->pxe_accept = ip;
1402 if ( pxebs_list_len ) {
1403 uint8_t buf[pxebs_list_len];
1405 fetch_setting ( NULL, &pxe_boot_servers_setting,
1406 buf, sizeof ( buf ) );
1407 pxebs_list ( dhcp, buf, sizeof ( buf ), ip );
1409 if ( ! dhcp->pxe_attempt->s_addr ) {
1410 DBGC ( dhcp, "DHCP %p has no PXE boot servers for type %04x\n",
1416 /* Dump out PXE server lists */
1417 DBGC ( dhcp, "DHCP %p attempting", dhcp );
1418 for ( ip = dhcp->pxe_attempt ; ip->s_addr ; ip++ )
1419 DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
1420 DBGC ( dhcp, "\n" );
1421 if ( dhcp->pxe_accept ) {
1422 DBGC ( dhcp, "DHCP %p accepting", dhcp );
1423 for ( ip = dhcp->pxe_accept ; ip->s_addr ; ip++ )
1424 DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
1425 DBGC ( dhcp, "\n" );
1428 /* Instantiate child objects and attach to our interfaces */
1429 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
1430 ( struct sockaddr * ) &dhcp->local ) ) != 0 )
1433 /* Enter PXEBS state */
1434 dhcp_set_state ( dhcp, &dhcp_state_pxebs );
1436 /* Attach parent interface, mortalise self, and return */
1437 job_plug_plug ( &dhcp->job, job );
1438 ref_put ( &dhcp->refcnt );
1442 dhcp_finished ( dhcp, rc );
1443 ref_put ( &dhcp->refcnt );