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' ),
80 DHCP_PARAMETER_REQUEST_LIST,
81 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
82 DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
83 DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
84 DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
85 DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
89 /** DHCP feature codes */
90 static uint8_t dhcp_features[0] __table_start ( uint8_t, dhcp_features );
91 static uint8_t dhcp_features_end[0] __table_end ( uint8_t, dhcp_features );
93 /** Version number feature */
94 FEATURE_VERSION ( VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH );
96 /** DHCP server address setting */
97 struct setting dhcp_server_setting __setting = {
98 .name = "dhcp-server",
99 .description = "DHCP server address",
100 .tag = DHCP_SERVER_IDENTIFIER,
101 .type = &setting_type_ipv4,
104 /** DHCP user class setting */
105 struct setting user_class_setting __setting = {
106 .name = "user-class",
107 .description = "User class identifier",
108 .tag = DHCP_USER_CLASS_ID,
109 .type = &setting_type_string,
113 * Name a DHCP packet type
115 * @v msgtype DHCP message type
116 * @ret string DHCP mesasge type name
118 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
120 case DHCPNONE: return "BOOTP"; /* Non-DHCP packet */
121 case DHCPDISCOVER: return "DHCPDISCOVER";
122 case DHCPOFFER: return "DHCPOFFER";
123 case DHCPREQUEST: return "DHCPREQUEST";
124 case DHCPDECLINE: return "DHCPDECLINE";
125 case DHCPACK: return "DHCPACK";
126 case DHCPNAK: return "DHCPNAK";
127 case DHCPRELEASE: return "DHCPRELEASE";
128 case DHCPINFORM: return "DHCPINFORM";
129 default: return "DHCP<invalid>";
134 * Calculate DHCP transaction ID for a network device
136 * @v netdev Network device
139 * Extract the least significant bits of the hardware address for use
140 * as the transaction ID.
142 static uint32_t dhcp_xid ( struct net_device *netdev ) {
145 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
146 - sizeof ( xid ) ), sizeof ( xid ) );
150 /****************************************************************************
158 /** DHCP session state operations */
159 struct dhcp_session_state {
163 * Construct transmitted packet
165 * @v dhcp DHCP session
166 * @v dhcppkt DHCP packet
167 * @v peer Destination address
169 int ( * tx ) ( struct dhcp_session *dhcp,
170 struct dhcp_packet *dhcppkt,
171 struct sockaddr_in *peer );
172 /** Handle received packet
174 * @v dhcp DHCP session
175 * @v dhcppkt DHCP packet
176 * @v peer DHCP server address
177 * @v msgtype DHCP message type
178 * @v server_id DHCP server ID
180 void ( * rx ) ( struct dhcp_session *dhcp,
181 struct dhcp_packet *dhcppkt,
182 struct sockaddr_in *peer,
183 uint8_t msgtype, struct in_addr server_id );
184 /** Handle timer expiry
186 * @v dhcp DHCP session
188 void ( * expired ) ( struct dhcp_session *dhcp );
189 /** Transmitted message type */
191 /** Apply minimum timeout */
192 uint8_t apply_min_timeout;
195 static struct dhcp_session_state dhcp_state_discover;
196 static struct dhcp_session_state dhcp_state_request;
197 static struct dhcp_session_state dhcp_state_proxy;
198 static struct dhcp_session_state dhcp_state_pxebs;
200 /** A DHCP session */
201 struct dhcp_session {
202 /** Reference counter */
203 struct refcnt refcnt;
204 /** Job control interface */
205 struct job_interface job;
206 /** Data transfer interface */
207 struct xfer_interface xfer;
209 /** Network device being configured */
210 struct net_device *netdev;
211 /** Local socket address */
212 struct sockaddr_in local;
213 /** State of the session */
214 struct dhcp_session_state *state;
216 /** Offered IP address */
217 struct in_addr offer;
219 struct in_addr server;
220 /** DHCP offer priority */
223 /** ProxyDHCP protocol extensions should be ignored */
225 /** ProxyDHCP server */
226 struct in_addr proxy_server;
227 /** ProxyDHCP server priority */
230 /** PXE Boot Server type */
232 /** List of PXE Boot Servers to attempt */
233 struct in_addr *pxe_attempt;
234 /** List of PXE Boot Servers to accept */
235 struct in_addr *pxe_accept;
237 /** Retransmission timer */
238 struct retry_timer timer;
239 /** Start time of the current state (in ticks) */
246 * @v refcnt Reference counter
248 static void dhcp_free ( struct refcnt *refcnt ) {
249 struct dhcp_session *dhcp =
250 container_of ( refcnt, struct dhcp_session, refcnt );
252 netdev_put ( dhcp->netdev );
257 * Mark DHCP session as complete
259 * @v dhcp DHCP session
260 * @v rc Return status code
262 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
264 /* Block futher incoming messages */
265 job_nullify ( &dhcp->job );
266 xfer_nullify ( &dhcp->xfer );
268 /* Stop retry timer */
269 stop_timer ( &dhcp->timer );
271 /* Free resources and close interfaces */
272 xfer_close ( &dhcp->xfer, rc );
273 job_done ( &dhcp->job, rc );
277 * Transition to new DHCP session state
279 * @v dhcp DHCP session
280 * @v state New session state
282 static void dhcp_set_state ( struct dhcp_session *dhcp,
283 struct dhcp_session_state *state ) {
285 DBGC ( dhcp, "DHCP %p entering %s state\n", dhcp, state->name );
287 dhcp->start = currticks();
288 stop_timer ( &dhcp->timer );
289 dhcp->timer.min_timeout =
290 ( state->apply_min_timeout ? DHCP_MIN_TIMEOUT : 0 );
291 dhcp->timer.max_timeout = DHCP_MAX_TIMEOUT;
292 start_timer_nodelay ( &dhcp->timer );
295 /****************************************************************************
302 * Construct transmitted packet for DHCP discovery
304 * @v dhcp DHCP session
305 * @v dhcppkt DHCP packet
306 * @v peer Destination address
308 static int dhcp_discovery_tx ( struct dhcp_session *dhcp,
309 struct dhcp_packet *dhcppkt __unused,
310 struct sockaddr_in *peer ) {
312 DBGC ( dhcp, "DHCP %p DHCPDISCOVER\n", dhcp );
314 /* Set server address */
315 peer->sin_addr.s_addr = INADDR_BROADCAST;
316 peer->sin_port = htons ( BOOTPS_PORT );
322 * Handle received packet during DHCP discovery
324 * @v dhcp DHCP session
325 * @v dhcppkt DHCP packet
326 * @v peer DHCP server address
327 * @v msgtype DHCP message type
328 * @v server_id DHCP server ID
330 static void dhcp_discovery_rx ( struct dhcp_session *dhcp,
331 struct dhcp_packet *dhcppkt,
332 struct sockaddr_in *peer, uint8_t msgtype,
333 struct in_addr server_id ) {
335 char vci[9]; /* "PXEClient" */
339 uint8_t no_pxedhcp = 0;
340 unsigned long elapsed;
342 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
343 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
344 ntohs ( peer->sin_port ) );
345 if ( server_id.s_addr != peer->sin_addr.s_addr )
346 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
348 /* Identify offered IP address */
349 ip = dhcppkt->dhcphdr->yiaddr;
351 DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
353 /* Identify "PXEClient" vendor class */
354 vci_len = dhcppkt_fetch ( dhcppkt, DHCP_VENDOR_CLASS_ID,
355 vci, sizeof ( vci ) );
356 has_pxeclient = ( ( vci_len >= ( int ) sizeof ( vci ) ) &&
357 ( strncmp ( "PXEClient", vci, sizeof (vci) ) == 0 ));
359 DBGC ( dhcp, " pxe" );
361 /* Identify priority */
362 dhcppkt_fetch ( dhcppkt, DHCP_EB_PRIORITY, &priority,
363 sizeof ( priority ) );
365 DBGC ( dhcp, " pri %d", priority );
367 /* Identify ignore-PXE flag */
368 dhcppkt_fetch ( dhcppkt, DHCP_EB_NO_PXEDHCP, &no_pxedhcp,
369 sizeof ( no_pxedhcp ) );
371 DBGC ( dhcp, " nopxe" );
374 /* Select as DHCP offer, if applicable */
375 if ( ip.s_addr && ( peer->sin_port == htons ( BOOTPS_PORT ) ) &&
376 ( ( msgtype == DHCPOFFER ) || ( ! msgtype /* BOOTP */ ) ) &&
377 ( priority >= dhcp->priority ) ) {
379 dhcp->server = server_id;
380 dhcp->priority = priority;
381 dhcp->no_pxedhcp = no_pxedhcp;
384 /* Select as ProxyDHCP offer, if applicable */
385 if ( has_pxeclient && ( msgtype == DHCPOFFER ) &&
386 ( priority >= dhcp->proxy_priority ) ) {
387 dhcp->proxy_server = server_id;
388 dhcp->proxy_priority = priority;
391 /* We can exit the discovery state when we have a valid
392 * DHCPOFFER, and either:
394 * o The DHCPOFFER instructs us to ignore ProxyDHCPOFFERs, or
395 * o We have a valid ProxyDHCPOFFER, or
396 * o We have allowed sufficient time for ProxyDHCPOFFERs.
399 /* If we don't yet have a DHCPOFFER, do nothing */
400 if ( ! dhcp->offer.s_addr )
403 /* If we can't yet transition to DHCPREQUEST, do nothing */
404 elapsed = ( currticks() - dhcp->start );
405 if ( ! ( dhcp->no_pxedhcp || dhcp->proxy_server.s_addr ||
406 ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) )
409 /* Transition to DHCPREQUEST */
410 dhcp_set_state ( dhcp, &dhcp_state_request );
414 * Handle timer expiry during DHCP discovery
416 * @v dhcp DHCP session
418 static void dhcp_discovery_expired ( struct dhcp_session *dhcp ) {
419 unsigned long elapsed = ( currticks() - dhcp->start );
421 /* Give up waiting for ProxyDHCP before we reach the failure point */
422 if ( dhcp->offer.s_addr && ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) {
423 dhcp_set_state ( dhcp, &dhcp_state_request );
427 /* Otherwise, retransmit current packet */
431 /** DHCP discovery state operations */
432 static struct dhcp_session_state dhcp_state_discover = {
434 .tx = dhcp_discovery_tx,
435 .rx = dhcp_discovery_rx,
436 .expired = dhcp_discovery_expired,
437 .tx_msgtype = DHCPDISCOVER,
438 .apply_min_timeout = 1,
442 * Construct transmitted packet for DHCP request
444 * @v dhcp DHCP session
445 * @v dhcppkt DHCP packet
446 * @v peer Destination address
448 static int dhcp_request_tx ( struct dhcp_session *dhcp,
449 struct dhcp_packet *dhcppkt,
450 struct sockaddr_in *peer ) {
453 DBGC ( dhcp, "DHCP %p DHCPREQUEST to %s:%d",
454 dhcp, inet_ntoa ( dhcp->server ), BOOTPS_PORT );
455 DBGC ( dhcp, " for %s\n", inet_ntoa ( dhcp->offer ) );
458 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
460 sizeof ( dhcp->server ) ) ) != 0 )
463 /* Set requested IP address */
464 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
466 sizeof ( dhcp->offer ) ) ) != 0 )
469 /* Set server address */
470 peer->sin_addr.s_addr = INADDR_BROADCAST;
471 peer->sin_port = htons ( BOOTPS_PORT );
477 * Handle received packet during DHCP request
479 * @v dhcp DHCP session
480 * @v dhcppkt DHCP packet
481 * @v peer DHCP server address
482 * @v msgtype DHCP message type
483 * @v server_id DHCP server ID
485 static void dhcp_request_rx ( struct dhcp_session *dhcp,
486 struct dhcp_packet *dhcppkt,
487 struct sockaddr_in *peer, uint8_t msgtype,
488 struct in_addr server_id ) {
490 struct settings *parent;
493 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
494 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
495 ntohs ( peer->sin_port ) );
496 if ( server_id.s_addr != peer->sin_addr.s_addr )
497 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
499 /* Identify leased IP address */
500 ip = dhcppkt->dhcphdr->yiaddr;
502 DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
505 /* Filter out unacceptable responses */
506 if ( peer->sin_port != htons ( BOOTPS_PORT ) )
508 if ( msgtype /* BOOTP */ && ( msgtype != DHCPACK ) )
510 if ( server_id.s_addr != dhcp->server.s_addr )
513 /* Record assigned address */
514 dhcp->local.sin_addr = ip;
516 /* Register settings */
517 parent = netdev_settings ( dhcp->netdev );
518 if ( ( rc = register_settings ( &dhcppkt->settings, parent ) ) != 0 ){
519 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
520 dhcp, strerror ( rc ) );
521 dhcp_finished ( dhcp, rc );
525 /* Start ProxyDHCPREQUEST if applicable */
526 if ( dhcp->proxy_server.s_addr && ( ! dhcp->no_pxedhcp ) ) {
527 dhcp_set_state ( dhcp, &dhcp_state_proxy );
532 dhcp_finished ( dhcp, 0 );
536 * Handle timer expiry during DHCP discovery
538 * @v dhcp DHCP session
540 static void dhcp_request_expired ( struct dhcp_session *dhcp ) {
542 /* Retransmit current packet */
546 /** DHCP request state operations */
547 static struct dhcp_session_state dhcp_state_request = {
549 .tx = dhcp_request_tx,
550 .rx = dhcp_request_rx,
551 .expired = dhcp_request_expired,
552 .tx_msgtype = DHCPREQUEST,
553 .apply_min_timeout = 0,
557 * Construct transmitted packet for ProxyDHCP request
559 * @v dhcp DHCP session
560 * @v dhcppkt DHCP packet
561 * @v peer Destination address
563 static int dhcp_proxy_tx ( struct dhcp_session *dhcp,
564 struct dhcp_packet *dhcppkt,
565 struct sockaddr_in *peer ) {
568 DBGC ( dhcp, "DHCP %p ProxyDHCP REQUEST to %s:%d\n",
569 dhcp, inet_ntoa ( dhcp->proxy_server ), PXE_PORT );
572 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
574 sizeof ( dhcp->proxy_server ) ) ) != 0 )
577 /* Set server address */
578 peer->sin_addr = dhcp->proxy_server;
579 peer->sin_port = htons ( PXE_PORT );
585 * Handle received packet during ProxyDHCP request
587 * @v dhcp DHCP session
588 * @v dhcppkt DHCP packet
589 * @v peer DHCP server address
590 * @v msgtype DHCP message type
591 * @v server_id DHCP server ID
593 static void dhcp_proxy_rx ( struct dhcp_session *dhcp,
594 struct dhcp_packet *dhcppkt,
595 struct sockaddr_in *peer, uint8_t msgtype,
596 struct in_addr server_id ) {
599 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
600 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
601 ntohs ( peer->sin_port ) );
602 if ( server_id.s_addr != peer->sin_addr.s_addr )
603 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
606 /* Filter out unacceptable responses */
607 if ( peer->sin_port != htons ( PXE_PORT ) )
609 if ( msgtype != DHCPACK )
611 if ( server_id.s_addr /* Linux PXE server omits server ID */ &&
612 ( server_id.s_addr != dhcp->proxy_server.s_addr ) )
615 /* Register settings */
616 dhcppkt->settings.name = PROXYDHCP_SETTINGS_NAME;
617 if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
618 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
619 dhcp, strerror ( rc ) );
620 dhcp_finished ( dhcp, rc );
625 dhcp_finished ( dhcp, 0 );
629 * Handle timer expiry during ProxyDHCP request
631 * @v dhcp DHCP session
633 static void dhcp_proxy_expired ( struct dhcp_session *dhcp ) {
634 unsigned long elapsed = ( currticks() - dhcp->start );
636 /* Give up waiting for ProxyDHCP before we reach the failure point */
637 if ( elapsed > PROXYDHCP_MAX_TIMEOUT ) {
638 dhcp_finished ( dhcp, 0 );
642 /* Retransmit current packet */
646 /** ProxyDHCP request state operations */
647 static struct dhcp_session_state dhcp_state_proxy = {
651 .expired = dhcp_proxy_expired,
652 .tx_msgtype = DHCPREQUEST,
653 .apply_min_timeout = 0,
657 * Construct transmitted packet for PXE Boot Server Discovery
659 * @v dhcp DHCP session
660 * @v dhcppkt DHCP packet
661 * @v peer Destination address
663 static int dhcp_pxebs_tx ( struct dhcp_session *dhcp,
664 struct dhcp_packet *dhcppkt,
665 struct sockaddr_in *peer ) {
666 struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
669 DBGC ( dhcp, "DHCP %p PXEBS REQUEST to %s:%d for type %d\n",
670 dhcp, inet_ntoa ( *(dhcp->pxe_attempt) ), PXE_PORT,
671 ntohs ( dhcp->pxe_type ) );
673 /* Set boot menu item */
674 menu_item.type = dhcp->pxe_type;
675 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
676 &menu_item, sizeof ( menu_item ) ) ) != 0 )
679 /* Set server address */
680 peer->sin_addr = *(dhcp->pxe_attempt);
681 peer->sin_port = htons ( PXE_PORT );
687 * Check to see if PXE Boot Server address is acceptable
689 * @v dhcp DHCP session
690 * @v bs Boot Server address
691 * @ret accept Boot Server is acceptable
693 static int dhcp_pxebs_accept ( struct dhcp_session *dhcp,
694 struct in_addr bs ) {
695 struct in_addr *accept;
697 /* Accept if we have no acceptance filter */
698 if ( ! dhcp->pxe_accept )
701 /* Scan through acceptance list */
702 for ( accept = dhcp->pxe_accept ; accept->s_addr ; accept++ ) {
703 if ( accept->s_addr == bs.s_addr )
707 DBGC ( dhcp, "DHCP %p rejecting server %s\n",
708 dhcp, inet_ntoa ( bs ) );
713 * Handle received packet during PXE Boot Server Discovery
715 * @v dhcp DHCP session
716 * @v dhcppkt DHCP packet
717 * @v peer DHCP server address
718 * @v msgtype DHCP message type
719 * @v server_id DHCP server ID
721 static void dhcp_pxebs_rx ( struct dhcp_session *dhcp,
722 struct dhcp_packet *dhcppkt,
723 struct sockaddr_in *peer, uint8_t msgtype,
724 struct in_addr server_id ) {
725 struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
728 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
729 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
730 ntohs ( peer->sin_port ) );
731 if ( server_id.s_addr != peer->sin_addr.s_addr )
732 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
734 /* Identify boot menu item */
735 dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
736 &menu_item, sizeof ( menu_item ) );
737 if ( menu_item.type )
738 DBGC ( dhcp, " for type %d", ntohs ( menu_item.type ) );
741 /* Filter out unacceptable responses */
742 if ( peer->sin_port != htons ( PXE_PORT ) )
744 if ( msgtype != DHCPACK )
746 if ( menu_item.type != dhcp->pxe_type )
748 if ( ! dhcp_pxebs_accept ( dhcp, ( server_id.s_addr ?
749 server_id : peer->sin_addr ) ) )
752 /* Register settings */
753 dhcppkt->settings.name = PXEBS_SETTINGS_NAME;
754 if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
755 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
756 dhcp, strerror ( rc ) );
757 dhcp_finished ( dhcp, rc );
762 dhcp_finished ( dhcp, 0 );
766 * Handle timer expiry during PXE Boot Server Discovery
768 * @v dhcp DHCP session
770 static void dhcp_pxebs_expired ( struct dhcp_session *dhcp ) {
771 unsigned long elapsed = ( currticks() - dhcp->start );
773 /* Give up waiting before we reach the failure point, and fail
774 * over to the next server in the attempt list
776 if ( elapsed > PXEBS_MAX_TIMEOUT ) {
778 if ( dhcp->pxe_attempt->s_addr ) {
779 dhcp_set_state ( dhcp, &dhcp_state_pxebs );
782 dhcp_finished ( dhcp, -ETIMEDOUT );
787 /* Retransmit current packet */
791 /** PXE Boot Server Discovery state operations */
792 static struct dhcp_session_state dhcp_state_pxebs = {
796 .expired = dhcp_pxebs_expired,
797 .tx_msgtype = DHCPREQUEST,
798 .apply_min_timeout = 1,
801 /****************************************************************************
803 * Packet construction
808 * Create a DHCP packet
810 * @v dhcppkt DHCP packet structure to fill in
811 * @v netdev Network device
812 * @v msgtype DHCP message type
813 * @v options Initial options to include (or NULL)
814 * @v options_len Length of initial options
815 * @v data Buffer for DHCP packet
816 * @v max_len Size of DHCP packet buffer
817 * @ret rc Return status code
819 * Creates a DHCP packet in the specified buffer, and initialise a
820 * DHCP packet structure.
822 int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
823 struct net_device *netdev, uint8_t msgtype,
824 const void *options, size_t options_len,
825 void *data, size_t max_len ) {
826 struct dhcphdr *dhcphdr = data;
831 if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
834 /* Initialise DHCP packet content */
835 memset ( dhcphdr, 0, max_len );
836 dhcphdr->xid = dhcp_xid ( netdev );
837 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
838 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
839 dhcphdr->op = dhcp_op[msgtype];
840 /* If hardware length exceeds the chaddr field length, don't
841 * use the chaddr field. This is as per RFC4390.
843 hlen = netdev->ll_protocol->ll_addr_len;
844 if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
846 dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
848 dhcphdr->hlen = hlen;
849 memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
850 memcpy ( dhcphdr->options, options, options_len );
852 /* Initialise DHCP packet structure */
853 memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
854 dhcppkt_init ( dhcppkt, data, max_len );
856 /* Set DHCP_MESSAGE_TYPE option */
857 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
858 &msgtype, sizeof ( msgtype ) ) ) != 0 )
865 * Create DHCP request packet
867 * @v dhcppkt DHCP packet structure to fill in
868 * @v netdev Network device
869 * @v msgtype DHCP message type
870 * @v ciaddr Client IP address
871 * @v data Buffer for DHCP packet
872 * @v max_len Size of DHCP packet buffer
873 * @ret rc Return status code
875 * Creates a DHCP request packet in the specified buffer, and
876 * initialise a DHCP packet structure.
878 int dhcp_create_request ( struct dhcp_packet *dhcppkt,
879 struct net_device *netdev, unsigned int msgtype,
880 struct in_addr ciaddr, void *data, size_t max_len ) {
881 struct device_description *desc = &netdev->dev->desc;
882 struct dhcp_netdev_desc dhcp_desc;
883 struct dhcp_client_id client_id;
884 struct dhcp_client_uuid client_uuid;
885 size_t dhcp_features_len;
890 /* Create DHCP packet */
891 if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
892 dhcp_request_options_data,
893 sizeof ( dhcp_request_options_data ),
894 data, max_len ) ) != 0 ) {
895 DBG ( "DHCP could not create DHCP packet: %s\n",
900 /* Set client IP address */
901 dhcppkt->dhcphdr->ciaddr = ciaddr;
903 /* Add options to identify the feature list */
904 dhcp_features_len = ( dhcp_features_end - 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 };
1056 DBGC ( dhcp, "DHCP %p received packet without metadata\n",
1061 if ( ! meta->src ) {
1062 DBGC ( dhcp, "DHCP %p received packet without source port\n",
1067 peer = ( struct sockaddr_in * ) meta->src;
1069 /* Create a DHCP packet containing the I/O buffer contents.
1070 * Whilst we could just use the original buffer in situ, that
1071 * would waste the unused space in the packet buffer, and also
1072 * waste a relatively scarce fully-aligned I/O buffer.
1074 data_len = iob_len ( iobuf );
1075 dhcppkt = zalloc ( sizeof ( *dhcppkt ) + data_len );
1078 goto err_alloc_dhcppkt;
1080 dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
1081 memcpy ( dhcphdr, iobuf->data, data_len );
1082 dhcppkt_init ( dhcppkt, dhcphdr, data_len );
1084 /* Identify message type */
1085 dhcppkt_fetch ( dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
1086 sizeof ( msgtype ) );
1088 /* Identify server ID */
1089 dhcppkt_fetch ( dhcppkt, DHCP_SERVER_IDENTIFIER,
1090 &server_id, sizeof ( server_id ) );
1092 /* Check for matching transaction ID */
1093 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
1094 DBGC ( dhcp, "DHCP %p %s from %s:%d has bad transaction "
1095 "ID\n", dhcp, dhcp_msgtype_name ( msgtype ),
1096 inet_ntoa ( peer->sin_addr ),
1097 ntohs ( peer->sin_port ) );
1102 /* Handle packet based on current state */
1103 dhcp->state->rx ( dhcp, dhcppkt, peer, msgtype, server_id );
1106 dhcppkt_put ( dhcppkt );
1114 /** DHCP data transfer interface operations */
1115 static struct xfer_interface_operations dhcp_xfer_operations = {
1116 .close = ignore_xfer_close,
1117 .vredirect = xfer_vopen,
1118 .window = unlimited_xfer_window,
1119 .alloc_iob = default_xfer_alloc_iob,
1120 .deliver_iob = dhcp_deliver_iob,
1121 .deliver_raw = xfer_deliver_as_iob,
1125 * Handle DHCP retry timer expiry
1127 * @v timer DHCP retry timer
1128 * @v fail Failure indicator
1130 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
1131 struct dhcp_session *dhcp =
1132 container_of ( timer, struct dhcp_session, timer );
1134 /* If we have failed, terminate DHCP */
1136 dhcp_finished ( dhcp, -ETIMEDOUT );
1140 /* Handle timer expiry based on current state */
1141 dhcp->state->expired ( dhcp );
1144 /****************************************************************************
1146 * Job control interface
1151 * Handle kill() event received via job control interface
1153 * @v job DHCP job control interface
1155 static void dhcp_job_kill ( struct job_interface *job ) {
1156 struct dhcp_session *dhcp =
1157 container_of ( job, struct dhcp_session, job );
1159 /* Terminate DHCP session */
1160 dhcp_finished ( dhcp, -ECANCELED );
1163 /** DHCP job control interface operations */
1164 static struct job_interface_operations dhcp_job_operations = {
1165 .done = ignore_job_done,
1166 .kill = dhcp_job_kill,
1167 .progress = ignore_job_progress,
1170 /****************************************************************************
1177 * DHCP peer address for socket opening
1179 * This is a dummy address; the only useful portion is the socket
1180 * family (so that we get a UDP connection). The DHCP client will set
1181 * the IP address and source port explicitly on each transmission.
1183 static struct sockaddr dhcp_peer = {
1184 .sa_family = AF_INET,
1188 * Start DHCP state machine on a network device
1190 * @v job Job control interface
1191 * @v netdev Network device
1192 * @ret rc Return status code
1194 * Starts DHCP on the specified network device. If successful, the
1195 * DHCPACK (and ProxyDHCPACK, if applicable) will be registered as
1198 int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
1199 struct dhcp_session *dhcp;
1202 /* Allocate and initialise structure */
1203 dhcp = zalloc ( sizeof ( *dhcp ) );
1206 dhcp->refcnt.free = dhcp_free;
1207 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1208 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1209 dhcp->netdev = netdev_get ( netdev );
1210 dhcp->local.sin_family = AF_INET;
1211 dhcp->local.sin_port = htons ( BOOTPC_PORT );
1212 dhcp->timer.expired = dhcp_timer_expired;
1214 /* Instantiate child objects and attach to our interfaces */
1215 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
1216 ( struct sockaddr * ) &dhcp->local ) ) != 0 )
1219 /* Enter DHCPDISCOVER state */
1220 dhcp_set_state ( dhcp, &dhcp_state_discover );
1222 /* Attach parent interface, mortalise self, and return */
1223 job_plug_plug ( &dhcp->job, job );
1224 ref_put ( &dhcp->refcnt );
1228 dhcp_finished ( dhcp, rc );
1229 ref_put ( &dhcp->refcnt );
1234 * Retrieve list of PXE boot servers for a given server type
1236 * @v dhcp DHCP session
1237 * @v raw DHCP PXE boot server list
1238 * @v raw_len Length of DHCP PXE boot server list
1239 * @v ip IP address list to fill in
1241 * The caller must ensure that the IP address list has sufficient
1244 static void pxebs_list ( struct dhcp_session *dhcp, void *raw,
1245 size_t raw_len, struct in_addr *ip ) {
1246 struct dhcp_pxe_boot_server *server = raw;
1251 if ( raw_len < sizeof ( *server ) ) {
1252 DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
1256 server_len = offsetof ( typeof ( *server ),
1257 ip[ server->num_ip ] );
1258 if ( raw_len < server_len ) {
1259 DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
1263 if ( server->type == dhcp->pxe_type ) {
1264 for ( i = 0 ; i < server->num_ip ; i++ )
1265 *(ip++) = server->ip[i];
1267 server = ( ( ( void * ) server ) + server_len );
1268 raw_len -= server_len;
1273 * Start PXE Boot Server Discovery on a network device
1275 * @v job Job control interface
1276 * @v netdev Network device
1277 * @v pxe_type PXE server type
1278 * @ret rc Return status code
1280 * Starts PXE Boot Server Discovery on the specified network device.
1281 * If successful, the Boot Server ACK will be registered as an option
1284 int start_pxebs ( struct job_interface *job, struct net_device *netdev,
1285 unsigned int pxe_type ) {
1286 struct setting pxe_discovery_control_setting =
1287 { .tag = DHCP_PXE_DISCOVERY_CONTROL };
1288 struct setting pxe_boot_servers_setting =
1289 { .tag = DHCP_PXE_BOOT_SERVERS };
1290 struct setting pxe_boot_server_mcast_setting =
1291 { .tag = DHCP_PXE_BOOT_SERVER_MCAST };
1292 ssize_t pxebs_list_len;
1293 struct dhcp_session *dhcp;
1295 unsigned int pxe_discovery_control;
1298 /* Get upper bound for PXE boot server IP address list */
1299 pxebs_list_len = fetch_setting_len ( NULL, &pxe_boot_servers_setting );
1300 if ( pxebs_list_len < 0 )
1303 /* Allocate and initialise structure */
1304 dhcp = zalloc ( sizeof ( *dhcp ) + sizeof ( *ip ) /* mcast */ +
1305 sizeof ( *ip ) /* bcast */ + pxebs_list_len +
1306 sizeof ( *ip ) /* terminator */ );
1309 dhcp->refcnt.free = dhcp_free;
1310 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1311 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1312 dhcp->netdev = netdev_get ( netdev );
1313 dhcp->local.sin_family = AF_INET;
1314 fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
1315 &dhcp->local.sin_addr );
1316 dhcp->local.sin_port = htons ( BOOTPC_PORT );
1317 dhcp->pxe_type = htons ( pxe_type );
1318 dhcp->timer.expired = dhcp_timer_expired;
1320 /* Construct PXE boot server IP address lists */
1321 pxe_discovery_control =
1322 fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
1323 ip = ( ( ( void * ) dhcp ) + sizeof ( *dhcp ) );
1324 dhcp->pxe_attempt = ip;
1325 if ( ! ( pxe_discovery_control & PXEBS_NO_MULTICAST ) ) {
1326 fetch_ipv4_setting ( NULL, &pxe_boot_server_mcast_setting, ip);
1330 if ( ! ( pxe_discovery_control & PXEBS_NO_BROADCAST ) )
1331 (ip++)->s_addr = INADDR_BROADCAST;
1332 if ( pxe_discovery_control & PXEBS_NO_UNKNOWN_SERVERS )
1333 dhcp->pxe_accept = ip;
1334 if ( pxebs_list_len ) {
1335 uint8_t buf[pxebs_list_len];
1337 fetch_setting ( NULL, &pxe_boot_servers_setting,
1338 buf, sizeof ( buf ) );
1339 pxebs_list ( dhcp, buf, sizeof ( buf ), ip );
1341 if ( ! dhcp->pxe_attempt->s_addr ) {
1342 DBGC ( dhcp, "DHCP %p has no PXE boot servers for type %04x\n",
1348 /* Dump out PXE server lists */
1349 DBGC ( dhcp, "DHCP %p attempting", dhcp );
1350 for ( ip = dhcp->pxe_attempt ; ip->s_addr ; ip++ )
1351 DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
1352 DBGC ( dhcp, "\n" );
1353 if ( dhcp->pxe_accept ) {
1354 DBGC ( dhcp, "DHCP %p accepting", dhcp );
1355 for ( ip = dhcp->pxe_accept ; ip->s_addr ; ip++ )
1356 DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
1357 DBGC ( dhcp, "\n" );
1360 /* Instantiate child objects and attach to our interfaces */
1361 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
1362 ( struct sockaddr * ) &dhcp->local ) ) != 0 )
1365 /* Enter PXEBS state */
1366 dhcp_set_state ( dhcp, &dhcp_state_pxebs );
1368 /* Attach parent interface, mortalise self, and return */
1369 job_plug_plug ( &dhcp->job, job );
1370 ref_put ( &dhcp->refcnt );
1374 dhcp_finished ( dhcp, rc );
1375 ref_put ( &dhcp->refcnt );