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_MAX_MESSAGE_SIZE,
75 DHCP_WORD ( ETH_MAX_MTU - 20 /* IP header */ - 8 /* UDP header */ ),
76 DHCP_CLIENT_ARCHITECTURE, DHCP_WORD ( 0 ),
77 DHCP_CLIENT_NDI, DHCP_OPTION ( 1 /* UNDI */ , 2, 1 /* v2.1 */ ),
79 DHCP_STRING ( 'P', 'X', 'E', 'C', 'l', 'i', 'e', 'n', 't', ':',
80 'A', 'r', 'c', 'h', ':', '0', '0', '0', '0', '0', ':',
81 'U', 'N', 'D', 'I', ':', '0', '0', '2', '0', '0', '1' ),
83 DHCP_STRING ( 'g', 'P', 'X', 'E' ),
84 DHCP_PARAMETER_REQUEST_LIST,
85 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
86 DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
87 DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
88 DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
89 DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
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" */
341 uint8_t no_pxedhcp = 0;
342 unsigned long elapsed;
344 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
345 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
346 ntohs ( peer->sin_port ) );
347 if ( server_id.s_addr != peer->sin_addr.s_addr )
348 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
350 /* Identify offered IP address */
351 ip = dhcppkt->dhcphdr->yiaddr;
353 DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
355 /* Identify "PXEClient" vendor class */
356 vci_len = dhcppkt_fetch ( dhcppkt, DHCP_VENDOR_CLASS_ID,
357 vci, sizeof ( vci ) );
358 has_pxeclient = ( ( vci_len >= ( int ) sizeof ( vci ) ) &&
359 ( strncmp ( "PXEClient", vci, sizeof (vci) ) == 0 ));
360 pxeopts_len = dhcppkt_fetch ( dhcppkt, DHCP_VENDOR_ENCAP, NULL, 0 );
361 has_pxeopts = ( pxeopts_len >= 0 );
363 DBGC ( dhcp, "%s", ( has_pxeopts ? " pxe" : " proxy" ) );
365 /* Identify priority */
366 dhcppkt_fetch ( dhcppkt, DHCP_EB_PRIORITY, &priority,
367 sizeof ( priority ) );
369 DBGC ( dhcp, " pri %d", priority );
371 /* Identify ignore-PXE flag */
372 dhcppkt_fetch ( dhcppkt, DHCP_EB_NO_PXEDHCP, &no_pxedhcp,
373 sizeof ( no_pxedhcp ) );
375 DBGC ( dhcp, " nopxe" );
378 /* Select as DHCP offer, if applicable */
379 if ( ip.s_addr && ( peer->sin_port == htons ( BOOTPS_PORT ) ) &&
380 ( ( msgtype == DHCPOFFER ) || ( ! msgtype /* BOOTP */ ) ) &&
381 ( priority >= dhcp->priority ) ) {
383 dhcp->server = server_id;
384 dhcp->priority = priority;
385 dhcp->no_pxedhcp = no_pxedhcp;
388 /* Select as ProxyDHCP offer, if applicable */
389 if ( has_pxeclient && ( ! has_pxeopts ) && ( msgtype == DHCPOFFER ) &&
390 ( priority >= dhcp->proxy_priority ) ) {
391 dhcp->proxy_server = server_id;
392 dhcp->proxy_priority = priority;
395 /* We can exit the discovery state when we have a valid
396 * DHCPOFFER, and either:
398 * o The DHCPOFFER instructs us to ignore ProxyDHCPOFFERs, or
399 * o We have a valid ProxyDHCPOFFER, or
400 * o We have allowed sufficient time for ProxyDHCPOFFERs.
403 /* If we don't yet have a DHCPOFFER, do nothing */
404 if ( ! dhcp->offer.s_addr )
407 /* If we can't yet transition to DHCPREQUEST, do nothing */
408 elapsed = ( currticks() - dhcp->start );
409 if ( ! ( dhcp->no_pxedhcp || dhcp->proxy_server.s_addr ||
410 ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) )
413 /* Transition to DHCPREQUEST */
414 dhcp_set_state ( dhcp, &dhcp_state_request );
418 * Handle timer expiry during DHCP discovery
420 * @v dhcp DHCP session
422 static void dhcp_discovery_expired ( struct dhcp_session *dhcp ) {
423 unsigned long elapsed = ( currticks() - dhcp->start );
425 /* Give up waiting for ProxyDHCP before we reach the failure point */
426 if ( dhcp->offer.s_addr && ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) {
427 dhcp_set_state ( dhcp, &dhcp_state_request );
431 /* Otherwise, retransmit current packet */
435 /** DHCP discovery state operations */
436 static struct dhcp_session_state dhcp_state_discover = {
438 .tx = dhcp_discovery_tx,
439 .rx = dhcp_discovery_rx,
440 .expired = dhcp_discovery_expired,
441 .tx_msgtype = DHCPDISCOVER,
442 .apply_min_timeout = 1,
446 * Construct transmitted packet for DHCP request
448 * @v dhcp DHCP session
449 * @v dhcppkt DHCP packet
450 * @v peer Destination address
452 static int dhcp_request_tx ( struct dhcp_session *dhcp,
453 struct dhcp_packet *dhcppkt,
454 struct sockaddr_in *peer ) {
457 DBGC ( dhcp, "DHCP %p DHCPREQUEST to %s:%d",
458 dhcp, inet_ntoa ( dhcp->server ), BOOTPS_PORT );
459 DBGC ( dhcp, " for %s\n", inet_ntoa ( dhcp->offer ) );
462 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
464 sizeof ( dhcp->server ) ) ) != 0 )
467 /* Set requested IP address */
468 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
470 sizeof ( dhcp->offer ) ) ) != 0 )
473 /* Set server address */
474 peer->sin_addr.s_addr = INADDR_BROADCAST;
475 peer->sin_port = htons ( BOOTPS_PORT );
481 * Handle received packet during DHCP request
483 * @v dhcp DHCP session
484 * @v dhcppkt DHCP packet
485 * @v peer DHCP server address
486 * @v msgtype DHCP message type
487 * @v server_id DHCP server ID
489 static void dhcp_request_rx ( struct dhcp_session *dhcp,
490 struct dhcp_packet *dhcppkt,
491 struct sockaddr_in *peer, uint8_t msgtype,
492 struct in_addr server_id ) {
494 struct settings *parent;
497 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
498 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
499 ntohs ( peer->sin_port ) );
500 if ( server_id.s_addr != peer->sin_addr.s_addr )
501 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
503 /* Identify leased IP address */
504 ip = dhcppkt->dhcphdr->yiaddr;
506 DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
509 /* Filter out unacceptable responses */
510 if ( peer->sin_port != htons ( BOOTPS_PORT ) )
512 if ( msgtype /* BOOTP */ && ( msgtype != DHCPACK ) )
514 if ( server_id.s_addr != dhcp->server.s_addr )
517 /* Record assigned address */
518 dhcp->local.sin_addr = ip;
520 /* Register settings */
521 parent = netdev_settings ( dhcp->netdev );
522 if ( ( rc = register_settings ( &dhcppkt->settings, parent ) ) != 0 ){
523 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
524 dhcp, strerror ( rc ) );
525 dhcp_finished ( dhcp, rc );
529 /* Start ProxyDHCPREQUEST if applicable */
530 if ( dhcp->proxy_server.s_addr && ( ! dhcp->no_pxedhcp ) ) {
531 dhcp_set_state ( dhcp, &dhcp_state_proxy );
536 dhcp_finished ( dhcp, 0 );
540 * Handle timer expiry during DHCP discovery
542 * @v dhcp DHCP session
544 static void dhcp_request_expired ( struct dhcp_session *dhcp ) {
546 /* Retransmit current packet */
550 /** DHCP request state operations */
551 static struct dhcp_session_state dhcp_state_request = {
553 .tx = dhcp_request_tx,
554 .rx = dhcp_request_rx,
555 .expired = dhcp_request_expired,
556 .tx_msgtype = DHCPREQUEST,
557 .apply_min_timeout = 0,
561 * Construct transmitted packet for ProxyDHCP request
563 * @v dhcp DHCP session
564 * @v dhcppkt DHCP packet
565 * @v peer Destination address
567 static int dhcp_proxy_tx ( struct dhcp_session *dhcp,
568 struct dhcp_packet *dhcppkt,
569 struct sockaddr_in *peer ) {
572 DBGC ( dhcp, "DHCP %p ProxyDHCP REQUEST to %s:%d\n",
573 dhcp, inet_ntoa ( dhcp->proxy_server ), PXE_PORT );
576 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
578 sizeof ( dhcp->proxy_server ) ) ) != 0 )
581 /* Set server address */
582 peer->sin_addr = dhcp->proxy_server;
583 peer->sin_port = htons ( PXE_PORT );
589 * Handle received packet during ProxyDHCP request
591 * @v dhcp DHCP session
592 * @v dhcppkt DHCP packet
593 * @v peer DHCP server address
594 * @v msgtype DHCP message type
595 * @v server_id DHCP server ID
597 static void dhcp_proxy_rx ( struct dhcp_session *dhcp,
598 struct dhcp_packet *dhcppkt,
599 struct sockaddr_in *peer, uint8_t msgtype,
600 struct in_addr server_id ) {
603 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
604 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
605 ntohs ( peer->sin_port ) );
606 if ( server_id.s_addr != peer->sin_addr.s_addr )
607 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
610 /* Filter out unacceptable responses */
611 if ( peer->sin_port != htons ( PXE_PORT ) )
613 if ( msgtype != DHCPACK )
615 if ( server_id.s_addr /* Linux PXE server omits server ID */ &&
616 ( server_id.s_addr != dhcp->proxy_server.s_addr ) )
619 /* Register settings */
620 dhcppkt->settings.name = PROXYDHCP_SETTINGS_NAME;
621 if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
622 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
623 dhcp, strerror ( rc ) );
624 dhcp_finished ( dhcp, rc );
629 dhcp_finished ( dhcp, 0 );
633 * Handle timer expiry during ProxyDHCP request
635 * @v dhcp DHCP session
637 static void dhcp_proxy_expired ( struct dhcp_session *dhcp ) {
638 unsigned long elapsed = ( currticks() - dhcp->start );
640 /* Give up waiting for ProxyDHCP before we reach the failure point */
641 if ( elapsed > PROXYDHCP_MAX_TIMEOUT ) {
642 dhcp_finished ( dhcp, 0 );
646 /* Retransmit current packet */
650 /** ProxyDHCP request state operations */
651 static struct dhcp_session_state dhcp_state_proxy = {
655 .expired = dhcp_proxy_expired,
656 .tx_msgtype = DHCPREQUEST,
657 .apply_min_timeout = 0,
661 * Construct transmitted packet for PXE Boot Server Discovery
663 * @v dhcp DHCP session
664 * @v dhcppkt DHCP packet
665 * @v peer Destination address
667 static int dhcp_pxebs_tx ( struct dhcp_session *dhcp,
668 struct dhcp_packet *dhcppkt,
669 struct sockaddr_in *peer ) {
670 struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
673 /* Set server address */
674 peer->sin_addr = *(dhcp->pxe_attempt);
675 peer->sin_port = ( ( peer->sin_addr.s_addr == INADDR_BROADCAST ) ?
676 htons ( BOOTPS_PORT ) : htons ( PXE_PORT ) );
678 DBGC ( dhcp, "DHCP %p PXEBS REQUEST to %s:%d for type %d\n",
679 dhcp, inet_ntoa ( peer->sin_addr ), ntohs ( peer->sin_port ),
680 ntohs ( dhcp->pxe_type ) );
682 /* Set boot menu item */
683 menu_item.type = dhcp->pxe_type;
684 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
685 &menu_item, sizeof ( menu_item ) ) ) != 0 )
692 * Check to see if PXE Boot Server address is acceptable
694 * @v dhcp DHCP session
695 * @v bs Boot Server address
696 * @ret accept Boot Server is acceptable
698 static int dhcp_pxebs_accept ( struct dhcp_session *dhcp,
699 struct in_addr bs ) {
700 struct in_addr *accept;
702 /* Accept if we have no acceptance filter */
703 if ( ! dhcp->pxe_accept )
706 /* Scan through acceptance list */
707 for ( accept = dhcp->pxe_accept ; accept->s_addr ; accept++ ) {
708 if ( accept->s_addr == bs.s_addr )
712 DBGC ( dhcp, "DHCP %p rejecting server %s\n",
713 dhcp, inet_ntoa ( bs ) );
718 * Handle received packet during PXE Boot Server Discovery
720 * @v dhcp DHCP session
721 * @v dhcppkt DHCP packet
722 * @v peer DHCP server address
723 * @v msgtype DHCP message type
724 * @v server_id DHCP server ID
726 static void dhcp_pxebs_rx ( struct dhcp_session *dhcp,
727 struct dhcp_packet *dhcppkt,
728 struct sockaddr_in *peer, uint8_t msgtype,
729 struct in_addr server_id ) {
730 struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
733 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
734 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
735 ntohs ( peer->sin_port ) );
736 if ( server_id.s_addr != peer->sin_addr.s_addr )
737 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
739 /* Identify boot menu item */
740 dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
741 &menu_item, sizeof ( menu_item ) );
742 if ( menu_item.type )
743 DBGC ( dhcp, " for type %d", ntohs ( menu_item.type ) );
746 /* Filter out unacceptable responses */
747 if ( ( peer->sin_port != htons ( BOOTPS_PORT ) ) &&
748 ( peer->sin_port != htons ( PXE_PORT ) ) )
750 if ( msgtype != DHCPACK )
752 if ( menu_item.type != dhcp->pxe_type )
754 if ( ! dhcp_pxebs_accept ( dhcp, ( server_id.s_addr ?
755 server_id : peer->sin_addr ) ) )
758 /* Register settings */
759 dhcppkt->settings.name = PXEBS_SETTINGS_NAME;
760 if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
761 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
762 dhcp, strerror ( rc ) );
763 dhcp_finished ( dhcp, rc );
768 dhcp_finished ( dhcp, 0 );
772 * Handle timer expiry during PXE Boot Server Discovery
774 * @v dhcp DHCP session
776 static void dhcp_pxebs_expired ( struct dhcp_session *dhcp ) {
777 unsigned long elapsed = ( currticks() - dhcp->start );
779 /* Give up waiting before we reach the failure point, and fail
780 * over to the next server in the attempt list
782 if ( elapsed > PXEBS_MAX_TIMEOUT ) {
784 if ( dhcp->pxe_attempt->s_addr ) {
785 dhcp_set_state ( dhcp, &dhcp_state_pxebs );
788 dhcp_finished ( dhcp, -ETIMEDOUT );
793 /* Retransmit current packet */
797 /** PXE Boot Server Discovery state operations */
798 static struct dhcp_session_state dhcp_state_pxebs = {
802 .expired = dhcp_pxebs_expired,
803 .tx_msgtype = DHCPREQUEST,
804 .apply_min_timeout = 1,
807 /****************************************************************************
809 * Packet construction
814 * Create a DHCP packet
816 * @v dhcppkt DHCP packet structure to fill in
817 * @v netdev Network device
818 * @v msgtype DHCP message type
819 * @v options Initial options to include (or NULL)
820 * @v options_len Length of initial options
821 * @v data Buffer for DHCP packet
822 * @v max_len Size of DHCP packet buffer
823 * @ret rc Return status code
825 * Creates a DHCP packet in the specified buffer, and initialise a
826 * DHCP packet structure.
828 int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
829 struct net_device *netdev, uint8_t msgtype,
830 const void *options, size_t options_len,
831 void *data, size_t max_len ) {
832 struct dhcphdr *dhcphdr = data;
837 if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
840 /* Initialise DHCP packet content */
841 memset ( dhcphdr, 0, max_len );
842 dhcphdr->xid = dhcp_xid ( netdev );
843 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
844 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
845 dhcphdr->op = dhcp_op[msgtype];
846 /* If hardware length exceeds the chaddr field length, don't
847 * use the chaddr field. This is as per RFC4390.
849 hlen = netdev->ll_protocol->ll_addr_len;
850 if ( hlen > sizeof ( dhcphdr->chaddr ) ) {
852 dhcphdr->flags = htons ( BOOTP_FL_BROADCAST );
854 dhcphdr->hlen = hlen;
855 memcpy ( dhcphdr->chaddr, netdev->ll_addr, hlen );
856 memcpy ( dhcphdr->options, options, options_len );
858 /* Initialise DHCP packet structure */
859 memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
860 dhcppkt_init ( dhcppkt, data, max_len );
862 /* Set DHCP_MESSAGE_TYPE option */
863 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
864 &msgtype, sizeof ( msgtype ) ) ) != 0 )
871 * Create DHCP request packet
873 * @v dhcppkt DHCP packet structure to fill in
874 * @v netdev Network device
875 * @v msgtype DHCP message type
876 * @v ciaddr Client IP address
877 * @v data Buffer for DHCP packet
878 * @v max_len Size of DHCP packet buffer
879 * @ret rc Return status code
881 * Creates a DHCP request packet in the specified buffer, and
882 * initialise a DHCP packet structure.
884 int dhcp_create_request ( struct dhcp_packet *dhcppkt,
885 struct net_device *netdev, unsigned int msgtype,
886 struct in_addr ciaddr, void *data, size_t max_len ) {
887 struct device_description *desc = &netdev->dev->desc;
888 struct dhcp_netdev_desc dhcp_desc;
889 struct dhcp_client_id client_id;
890 struct dhcp_client_uuid client_uuid;
891 uint8_t *dhcp_features;
892 size_t dhcp_features_len;
897 /* Create DHCP packet */
898 if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
899 dhcp_request_options_data,
900 sizeof ( dhcp_request_options_data ),
901 data, max_len ) ) != 0 ) {
902 DBG ( "DHCP could not create DHCP packet: %s\n",
907 /* Set client IP address */
908 dhcppkt->dhcphdr->ciaddr = ciaddr;
910 /* Add options to identify the feature list */
911 dhcp_features = table_start ( DHCP_FEATURES );
912 dhcp_features_len = table_num_entries ( DHCP_FEATURES );
913 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
914 dhcp_features_len ) ) != 0 ) {
915 DBG ( "DHCP could not set features list option: %s\n",
920 /* Add options to identify the network device */
921 dhcp_desc.type = desc->bus_type;
922 dhcp_desc.vendor = htons ( desc->vendor );
923 dhcp_desc.device = htons ( desc->device );
924 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
925 sizeof ( dhcp_desc ) ) ) != 0 ) {
926 DBG ( "DHCP could not set bus ID option: %s\n",
931 /* Add DHCP client identifier. Required for Infiniband, and
932 * doesn't hurt other link layers.
934 client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
935 ll_addr_len = netdev->ll_protocol->ll_addr_len;
936 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
937 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
938 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
939 ( ll_addr_len + 1 ) ) ) != 0 ) {
940 DBG ( "DHCP could not set client ID: %s\n",
945 /* Add client UUID, if we have one. Required for PXE. */
946 client_uuid.type = DHCP_CLIENT_UUID_TYPE;
947 if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting,
948 &client_uuid.uuid ) ) >= 0 ) {
949 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
951 sizeof ( client_uuid ) ) ) != 0 ) {
952 DBG ( "DHCP could not set client UUID: %s\n",
958 /* Add user class, if we have one. */
959 if ( ( len = fetch_setting_len ( NULL, &user_class_setting ) ) >= 0 ) {
960 char user_class[len];
961 fetch_setting ( NULL, &user_class_setting, user_class,
962 sizeof ( user_class ) );
963 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_USER_CLASS_ID,
965 sizeof ( user_class ) ) ) != 0 ) {
966 DBG ( "DHCP could not set user class: %s\n",
975 /****************************************************************************
977 * Data transfer interface
982 * Transmit DHCP request
984 * @v dhcp DHCP session
985 * @ret rc Return status code
987 static int dhcp_tx ( struct dhcp_session *dhcp ) {
988 static struct sockaddr_in peer = {
989 .sin_family = AF_INET,
991 struct xfer_metadata meta = {
992 .netdev = dhcp->netdev,
993 .src = ( struct sockaddr * ) &dhcp->local,
994 .dest = ( struct sockaddr * ) &peer,
996 struct io_buffer *iobuf;
997 uint8_t msgtype = dhcp->state->tx_msgtype;
998 struct dhcp_packet dhcppkt;
1001 /* Start retry timer. Do this first so that failures to
1002 * transmit will be retried.
1004 start_timer ( &dhcp->timer );
1006 /* Allocate buffer for packet */
1007 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
1011 /* Create basic DHCP packet in temporary buffer */
1012 if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev, msgtype,
1013 dhcp->local.sin_addr, iobuf->data,
1014 iob_tailroom ( iobuf ) ) ) != 0 ) {
1015 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
1016 dhcp, strerror ( rc ) );
1020 /* Fill in packet based on current state */
1021 if ( ( rc = dhcp->state->tx ( dhcp, &dhcppkt, &peer ) ) != 0 ) {
1022 DBGC ( dhcp, "DHCP %p could not fill DHCP request: %s\n",
1023 dhcp, strerror ( rc ) );
1027 /* Transmit the packet */
1028 iob_put ( iobuf, dhcppkt.len );
1029 if ( ( rc = xfer_deliver_iob_meta ( &dhcp->xfer, iob_disown ( iobuf ),
1031 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
1032 dhcp, strerror ( rc ) );
1044 * @v xfer Data transfer interface
1045 * @v iobuf I/O buffer
1046 * @v meta Transfer metadata
1047 * @ret rc Return status code
1049 static int dhcp_deliver_iob ( struct xfer_interface *xfer,
1050 struct io_buffer *iobuf,
1051 struct xfer_metadata *meta ) {
1052 struct dhcp_session *dhcp =
1053 container_of ( xfer, struct dhcp_session, xfer );
1054 struct sockaddr_in *peer;
1056 struct dhcp_packet *dhcppkt;
1057 struct dhcphdr *dhcphdr;
1058 uint8_t msgtype = 0;
1059 struct in_addr server_id = { 0 };
1063 if ( ! meta->src ) {
1064 DBGC ( dhcp, "DHCP %p received packet without source port\n",
1069 peer = ( struct sockaddr_in * ) meta->src;
1071 /* Create a DHCP packet containing the I/O buffer contents.
1072 * Whilst we could just use the original buffer in situ, that
1073 * would waste the unused space in the packet buffer, and also
1074 * waste a relatively scarce fully-aligned I/O buffer.
1076 data_len = iob_len ( iobuf );
1077 dhcppkt = zalloc ( sizeof ( *dhcppkt ) + data_len );
1080 goto err_alloc_dhcppkt;
1082 dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
1083 memcpy ( dhcphdr, iobuf->data, data_len );
1084 dhcppkt_init ( dhcppkt, dhcphdr, data_len );
1086 /* Identify message type */
1087 dhcppkt_fetch ( dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
1088 sizeof ( msgtype ) );
1090 /* Identify server ID */
1091 dhcppkt_fetch ( dhcppkt, DHCP_SERVER_IDENTIFIER,
1092 &server_id, sizeof ( server_id ) );
1094 /* Check for matching transaction ID */
1095 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
1096 DBGC ( dhcp, "DHCP %p %s from %s:%d has bad transaction "
1097 "ID\n", dhcp, dhcp_msgtype_name ( msgtype ),
1098 inet_ntoa ( peer->sin_addr ),
1099 ntohs ( peer->sin_port ) );
1104 /* Handle packet based on current state */
1105 dhcp->state->rx ( dhcp, dhcppkt, peer, msgtype, server_id );
1108 dhcppkt_put ( dhcppkt );
1115 /** DHCP data transfer interface operations */
1116 static struct xfer_interface_operations dhcp_xfer_operations = {
1117 .close = ignore_xfer_close,
1118 .vredirect = xfer_vreopen,
1119 .window = unlimited_xfer_window,
1120 .alloc_iob = default_xfer_alloc_iob,
1121 .deliver_iob = dhcp_deliver_iob,
1122 .deliver_raw = xfer_deliver_as_iob,
1126 * Handle DHCP retry timer expiry
1128 * @v timer DHCP retry timer
1129 * @v fail Failure indicator
1131 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
1132 struct dhcp_session *dhcp =
1133 container_of ( timer, struct dhcp_session, timer );
1135 /* If we have failed, terminate DHCP */
1137 dhcp_finished ( dhcp, -ETIMEDOUT );
1141 /* Handle timer expiry based on current state */
1142 dhcp->state->expired ( dhcp );
1145 /****************************************************************************
1147 * Job control interface
1152 * Handle kill() event received via job control interface
1154 * @v job DHCP job control interface
1156 static void dhcp_job_kill ( struct job_interface *job ) {
1157 struct dhcp_session *dhcp =
1158 container_of ( job, struct dhcp_session, job );
1160 /* Terminate DHCP session */
1161 dhcp_finished ( dhcp, -ECANCELED );
1164 /** DHCP job control interface operations */
1165 static struct job_interface_operations dhcp_job_operations = {
1166 .done = ignore_job_done,
1167 .kill = dhcp_job_kill,
1168 .progress = ignore_job_progress,
1171 /****************************************************************************
1178 * DHCP peer address for socket opening
1180 * This is a dummy address; the only useful portion is the socket
1181 * family (so that we get a UDP connection). The DHCP client will set
1182 * the IP address and source port explicitly on each transmission.
1184 static struct sockaddr dhcp_peer = {
1185 .sa_family = AF_INET,
1189 * Start DHCP state machine on a network device
1191 * @v job Job control interface
1192 * @v netdev Network device
1193 * @ret rc Return status code
1195 * Starts DHCP on the specified network device. If successful, the
1196 * DHCPACK (and ProxyDHCPACK, if applicable) will be registered as
1199 int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
1200 struct dhcp_session *dhcp;
1203 /* Allocate and initialise structure */
1204 dhcp = zalloc ( sizeof ( *dhcp ) );
1207 dhcp->refcnt.free = dhcp_free;
1208 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1209 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1210 dhcp->netdev = netdev_get ( netdev );
1211 dhcp->local.sin_family = AF_INET;
1212 dhcp->local.sin_port = htons ( BOOTPC_PORT );
1213 dhcp->timer.expired = dhcp_timer_expired;
1215 /* Instantiate child objects and attach to our interfaces */
1216 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
1217 ( struct sockaddr * ) &dhcp->local ) ) != 0 )
1220 /* Enter DHCPDISCOVER state */
1221 dhcp_set_state ( dhcp, &dhcp_state_discover );
1223 /* Attach parent interface, mortalise self, and return */
1224 job_plug_plug ( &dhcp->job, job );
1225 ref_put ( &dhcp->refcnt );
1229 dhcp_finished ( dhcp, rc );
1230 ref_put ( &dhcp->refcnt );
1235 * Retrieve list of PXE boot servers for a given server type
1237 * @v dhcp DHCP session
1238 * @v raw DHCP PXE boot server list
1239 * @v raw_len Length of DHCP PXE boot server list
1240 * @v ip IP address list to fill in
1242 * The caller must ensure that the IP address list has sufficient
1245 static void pxebs_list ( struct dhcp_session *dhcp, void *raw,
1246 size_t raw_len, struct in_addr *ip ) {
1247 struct dhcp_pxe_boot_server *server = raw;
1252 if ( raw_len < sizeof ( *server ) ) {
1253 DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
1257 server_len = offsetof ( typeof ( *server ),
1258 ip[ server->num_ip ] );
1259 if ( raw_len < server_len ) {
1260 DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
1264 if ( server->type == dhcp->pxe_type ) {
1265 for ( i = 0 ; i < server->num_ip ; i++ )
1266 *(ip++) = server->ip[i];
1268 server = ( ( ( void * ) server ) + server_len );
1269 raw_len -= server_len;
1274 * Start PXE Boot Server Discovery on a network device
1276 * @v job Job control interface
1277 * @v netdev Network device
1278 * @v pxe_type PXE server type
1279 * @ret rc Return status code
1281 * Starts PXE Boot Server Discovery on the specified network device.
1282 * If successful, the Boot Server ACK will be registered as an option
1285 int start_pxebs ( struct job_interface *job, struct net_device *netdev,
1286 unsigned int pxe_type ) {
1287 struct setting pxe_discovery_control_setting =
1288 { .tag = DHCP_PXE_DISCOVERY_CONTROL };
1289 struct setting pxe_boot_servers_setting =
1290 { .tag = DHCP_PXE_BOOT_SERVERS };
1291 struct setting pxe_boot_server_mcast_setting =
1292 { .tag = DHCP_PXE_BOOT_SERVER_MCAST };
1293 ssize_t pxebs_list_len;
1294 struct dhcp_session *dhcp;
1296 unsigned int pxe_discovery_control;
1299 /* Get upper bound for PXE boot server IP address list */
1300 pxebs_list_len = fetch_setting_len ( NULL, &pxe_boot_servers_setting );
1301 if ( pxebs_list_len < 0 )
1304 /* Allocate and initialise structure */
1305 dhcp = zalloc ( sizeof ( *dhcp ) + sizeof ( *ip ) /* mcast */ +
1306 sizeof ( *ip ) /* bcast */ + pxebs_list_len +
1307 sizeof ( *ip ) /* terminator */ );
1310 dhcp->refcnt.free = dhcp_free;
1311 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1312 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1313 dhcp->netdev = netdev_get ( netdev );
1314 dhcp->local.sin_family = AF_INET;
1315 fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
1316 &dhcp->local.sin_addr );
1317 dhcp->local.sin_port = htons ( BOOTPC_PORT );
1318 dhcp->pxe_type = htons ( pxe_type );
1319 dhcp->timer.expired = dhcp_timer_expired;
1321 /* Construct PXE boot server IP address lists */
1322 pxe_discovery_control =
1323 fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
1324 ip = ( ( ( void * ) dhcp ) + sizeof ( *dhcp ) );
1325 dhcp->pxe_attempt = ip;
1326 if ( ! ( pxe_discovery_control & PXEBS_NO_MULTICAST ) ) {
1327 fetch_ipv4_setting ( NULL, &pxe_boot_server_mcast_setting, ip);
1331 if ( ! ( pxe_discovery_control & PXEBS_NO_BROADCAST ) )
1332 (ip++)->s_addr = INADDR_BROADCAST;
1333 if ( pxe_discovery_control & PXEBS_NO_UNKNOWN_SERVERS )
1334 dhcp->pxe_accept = ip;
1335 if ( pxebs_list_len ) {
1336 uint8_t buf[pxebs_list_len];
1338 fetch_setting ( NULL, &pxe_boot_servers_setting,
1339 buf, sizeof ( buf ) );
1340 pxebs_list ( dhcp, buf, sizeof ( buf ), ip );
1342 if ( ! dhcp->pxe_attempt->s_addr ) {
1343 DBGC ( dhcp, "DHCP %p has no PXE boot servers for type %04x\n",
1349 /* Dump out PXE server lists */
1350 DBGC ( dhcp, "DHCP %p attempting", dhcp );
1351 for ( ip = dhcp->pxe_attempt ; ip->s_addr ; ip++ )
1352 DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
1353 DBGC ( dhcp, "\n" );
1354 if ( dhcp->pxe_accept ) {
1355 DBGC ( dhcp, "DHCP %p accepting", dhcp );
1356 for ( ip = dhcp->pxe_accept ; ip->s_addr ; ip++ )
1357 DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
1358 DBGC ( dhcp, "\n" );
1361 /* Instantiate child objects and attach to our interfaces */
1362 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
1363 ( struct sockaddr * ) &dhcp->local ) ) != 0 )
1366 /* Enter PXEBS state */
1367 dhcp_set_state ( dhcp, &dhcp_state_pxebs );
1369 /* Attach parent interface, mortalise self, and return */
1370 job_plug_plug ( &dhcp->job, job );
1371 ref_put ( &dhcp->refcnt );
1375 dhcp_finished ( dhcp, rc );
1376 ref_put ( &dhcp->refcnt );