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" */
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 uint8_t *dhcp_features;
886 size_t dhcp_features_len;
891 /* Create DHCP packet */
892 if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
893 dhcp_request_options_data,
894 sizeof ( dhcp_request_options_data ),
895 data, max_len ) ) != 0 ) {
896 DBG ( "DHCP could not create DHCP packet: %s\n",
901 /* Set client IP address */
902 dhcppkt->dhcphdr->ciaddr = ciaddr;
904 /* Add options to identify the feature list */
905 dhcp_features = table_start ( DHCP_FEATURES );
906 dhcp_features_len = table_num_entries ( DHCP_FEATURES );
907 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
908 dhcp_features_len ) ) != 0 ) {
909 DBG ( "DHCP could not set features list option: %s\n",
914 /* Add options to identify the network device */
915 dhcp_desc.type = desc->bus_type;
916 dhcp_desc.vendor = htons ( desc->vendor );
917 dhcp_desc.device = htons ( desc->device );
918 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
919 sizeof ( dhcp_desc ) ) ) != 0 ) {
920 DBG ( "DHCP could not set bus ID option: %s\n",
925 /* Add DHCP client identifier. Required for Infiniband, and
926 * doesn't hurt other link layers.
928 client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
929 ll_addr_len = netdev->ll_protocol->ll_addr_len;
930 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
931 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
932 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
933 ( ll_addr_len + 1 ) ) ) != 0 ) {
934 DBG ( "DHCP could not set client ID: %s\n",
939 /* Add client UUID, if we have one. Required for PXE. */
940 client_uuid.type = DHCP_CLIENT_UUID_TYPE;
941 if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting,
942 &client_uuid.uuid ) ) >= 0 ) {
943 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
945 sizeof ( client_uuid ) ) ) != 0 ) {
946 DBG ( "DHCP could not set client UUID: %s\n",
952 /* Add user class, if we have one. */
953 if ( ( len = fetch_setting_len ( NULL, &user_class_setting ) ) >= 0 ) {
954 char user_class[len];
955 fetch_setting ( NULL, &user_class_setting, user_class,
956 sizeof ( user_class ) );
957 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_USER_CLASS_ID,
959 sizeof ( user_class ) ) ) != 0 ) {
960 DBG ( "DHCP could not set user class: %s\n",
969 /****************************************************************************
971 * Data transfer interface
976 * Transmit DHCP request
978 * @v dhcp DHCP session
979 * @ret rc Return status code
981 static int dhcp_tx ( struct dhcp_session *dhcp ) {
982 static struct sockaddr_in peer = {
983 .sin_family = AF_INET,
985 struct xfer_metadata meta = {
986 .netdev = dhcp->netdev,
987 .src = ( struct sockaddr * ) &dhcp->local,
988 .dest = ( struct sockaddr * ) &peer,
990 struct io_buffer *iobuf;
991 uint8_t msgtype = dhcp->state->tx_msgtype;
992 struct dhcp_packet dhcppkt;
995 /* Start retry timer. Do this first so that failures to
996 * transmit will be retried.
998 start_timer ( &dhcp->timer );
1000 /* Allocate buffer for packet */
1001 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
1005 /* Create basic DHCP packet in temporary buffer */
1006 if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev, msgtype,
1007 dhcp->local.sin_addr, iobuf->data,
1008 iob_tailroom ( iobuf ) ) ) != 0 ) {
1009 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
1010 dhcp, strerror ( rc ) );
1014 /* Fill in packet based on current state */
1015 if ( ( rc = dhcp->state->tx ( dhcp, &dhcppkt, &peer ) ) != 0 ) {
1016 DBGC ( dhcp, "DHCP %p could not fill DHCP request: %s\n",
1017 dhcp, strerror ( rc ) );
1021 /* Transmit the packet */
1022 iob_put ( iobuf, dhcppkt.len );
1023 if ( ( rc = xfer_deliver_iob_meta ( &dhcp->xfer, iob_disown ( iobuf ),
1025 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
1026 dhcp, strerror ( rc ) );
1038 * @v xfer Data transfer interface
1039 * @v iobuf I/O buffer
1040 * @v meta Transfer metadata
1041 * @ret rc Return status code
1043 static int dhcp_deliver_iob ( struct xfer_interface *xfer,
1044 struct io_buffer *iobuf,
1045 struct xfer_metadata *meta ) {
1046 struct dhcp_session *dhcp =
1047 container_of ( xfer, struct dhcp_session, xfer );
1048 struct sockaddr_in *peer;
1050 struct dhcp_packet *dhcppkt;
1051 struct dhcphdr *dhcphdr;
1052 uint8_t msgtype = 0;
1053 struct in_addr server_id = { 0 };
1057 if ( ! meta->src ) {
1058 DBGC ( dhcp, "DHCP %p received packet without source port\n",
1063 peer = ( struct sockaddr_in * ) meta->src;
1065 /* Create a DHCP packet containing the I/O buffer contents.
1066 * Whilst we could just use the original buffer in situ, that
1067 * would waste the unused space in the packet buffer, and also
1068 * waste a relatively scarce fully-aligned I/O buffer.
1070 data_len = iob_len ( iobuf );
1071 dhcppkt = zalloc ( sizeof ( *dhcppkt ) + data_len );
1074 goto err_alloc_dhcppkt;
1076 dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
1077 memcpy ( dhcphdr, iobuf->data, data_len );
1078 dhcppkt_init ( dhcppkt, dhcphdr, data_len );
1080 /* Identify message type */
1081 dhcppkt_fetch ( dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
1082 sizeof ( msgtype ) );
1084 /* Identify server ID */
1085 dhcppkt_fetch ( dhcppkt, DHCP_SERVER_IDENTIFIER,
1086 &server_id, sizeof ( server_id ) );
1088 /* Check for matching transaction ID */
1089 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
1090 DBGC ( dhcp, "DHCP %p %s from %s:%d has bad transaction "
1091 "ID\n", dhcp, dhcp_msgtype_name ( msgtype ),
1092 inet_ntoa ( peer->sin_addr ),
1093 ntohs ( peer->sin_port ) );
1098 /* Handle packet based on current state */
1099 dhcp->state->rx ( dhcp, dhcppkt, peer, msgtype, server_id );
1102 dhcppkt_put ( dhcppkt );
1109 /** DHCP data transfer interface operations */
1110 static struct xfer_interface_operations dhcp_xfer_operations = {
1111 .close = ignore_xfer_close,
1112 .vredirect = xfer_vreopen,
1113 .window = unlimited_xfer_window,
1114 .alloc_iob = default_xfer_alloc_iob,
1115 .deliver_iob = dhcp_deliver_iob,
1116 .deliver_raw = xfer_deliver_as_iob,
1120 * Handle DHCP retry timer expiry
1122 * @v timer DHCP retry timer
1123 * @v fail Failure indicator
1125 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
1126 struct dhcp_session *dhcp =
1127 container_of ( timer, struct dhcp_session, timer );
1129 /* If we have failed, terminate DHCP */
1131 dhcp_finished ( dhcp, -ETIMEDOUT );
1135 /* Handle timer expiry based on current state */
1136 dhcp->state->expired ( dhcp );
1139 /****************************************************************************
1141 * Job control interface
1146 * Handle kill() event received via job control interface
1148 * @v job DHCP job control interface
1150 static void dhcp_job_kill ( struct job_interface *job ) {
1151 struct dhcp_session *dhcp =
1152 container_of ( job, struct dhcp_session, job );
1154 /* Terminate DHCP session */
1155 dhcp_finished ( dhcp, -ECANCELED );
1158 /** DHCP job control interface operations */
1159 static struct job_interface_operations dhcp_job_operations = {
1160 .done = ignore_job_done,
1161 .kill = dhcp_job_kill,
1162 .progress = ignore_job_progress,
1165 /****************************************************************************
1172 * DHCP peer address for socket opening
1174 * This is a dummy address; the only useful portion is the socket
1175 * family (so that we get a UDP connection). The DHCP client will set
1176 * the IP address and source port explicitly on each transmission.
1178 static struct sockaddr dhcp_peer = {
1179 .sa_family = AF_INET,
1183 * Start DHCP state machine on a network device
1185 * @v job Job control interface
1186 * @v netdev Network device
1187 * @ret rc Return status code
1189 * Starts DHCP on the specified network device. If successful, the
1190 * DHCPACK (and ProxyDHCPACK, if applicable) will be registered as
1193 int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
1194 struct dhcp_session *dhcp;
1197 /* Allocate and initialise structure */
1198 dhcp = zalloc ( sizeof ( *dhcp ) );
1201 dhcp->refcnt.free = dhcp_free;
1202 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1203 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1204 dhcp->netdev = netdev_get ( netdev );
1205 dhcp->local.sin_family = AF_INET;
1206 dhcp->local.sin_port = htons ( BOOTPC_PORT );
1207 dhcp->timer.expired = dhcp_timer_expired;
1209 /* Instantiate child objects and attach to our interfaces */
1210 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
1211 ( struct sockaddr * ) &dhcp->local ) ) != 0 )
1214 /* Enter DHCPDISCOVER state */
1215 dhcp_set_state ( dhcp, &dhcp_state_discover );
1217 /* Attach parent interface, mortalise self, and return */
1218 job_plug_plug ( &dhcp->job, job );
1219 ref_put ( &dhcp->refcnt );
1223 dhcp_finished ( dhcp, rc );
1224 ref_put ( &dhcp->refcnt );
1229 * Retrieve list of PXE boot servers for a given server type
1231 * @v dhcp DHCP session
1232 * @v raw DHCP PXE boot server list
1233 * @v raw_len Length of DHCP PXE boot server list
1234 * @v ip IP address list to fill in
1236 * The caller must ensure that the IP address list has sufficient
1239 static void pxebs_list ( struct dhcp_session *dhcp, void *raw,
1240 size_t raw_len, struct in_addr *ip ) {
1241 struct dhcp_pxe_boot_server *server = raw;
1246 if ( raw_len < sizeof ( *server ) ) {
1247 DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
1251 server_len = offsetof ( typeof ( *server ),
1252 ip[ server->num_ip ] );
1253 if ( raw_len < server_len ) {
1254 DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
1258 if ( server->type == dhcp->pxe_type ) {
1259 for ( i = 0 ; i < server->num_ip ; i++ )
1260 *(ip++) = server->ip[i];
1262 server = ( ( ( void * ) server ) + server_len );
1263 raw_len -= server_len;
1268 * Start PXE Boot Server Discovery on a network device
1270 * @v job Job control interface
1271 * @v netdev Network device
1272 * @v pxe_type PXE server type
1273 * @ret rc Return status code
1275 * Starts PXE Boot Server Discovery on the specified network device.
1276 * If successful, the Boot Server ACK will be registered as an option
1279 int start_pxebs ( struct job_interface *job, struct net_device *netdev,
1280 unsigned int pxe_type ) {
1281 struct setting pxe_discovery_control_setting =
1282 { .tag = DHCP_PXE_DISCOVERY_CONTROL };
1283 struct setting pxe_boot_servers_setting =
1284 { .tag = DHCP_PXE_BOOT_SERVERS };
1285 struct setting pxe_boot_server_mcast_setting =
1286 { .tag = DHCP_PXE_BOOT_SERVER_MCAST };
1287 ssize_t pxebs_list_len;
1288 struct dhcp_session *dhcp;
1290 unsigned int pxe_discovery_control;
1293 /* Get upper bound for PXE boot server IP address list */
1294 pxebs_list_len = fetch_setting_len ( NULL, &pxe_boot_servers_setting );
1295 if ( pxebs_list_len < 0 )
1298 /* Allocate and initialise structure */
1299 dhcp = zalloc ( sizeof ( *dhcp ) + sizeof ( *ip ) /* mcast */ +
1300 sizeof ( *ip ) /* bcast */ + pxebs_list_len +
1301 sizeof ( *ip ) /* terminator */ );
1304 dhcp->refcnt.free = dhcp_free;
1305 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1306 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1307 dhcp->netdev = netdev_get ( netdev );
1308 dhcp->local.sin_family = AF_INET;
1309 fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
1310 &dhcp->local.sin_addr );
1311 dhcp->local.sin_port = htons ( BOOTPC_PORT );
1312 dhcp->pxe_type = htons ( pxe_type );
1313 dhcp->timer.expired = dhcp_timer_expired;
1315 /* Construct PXE boot server IP address lists */
1316 pxe_discovery_control =
1317 fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
1318 ip = ( ( ( void * ) dhcp ) + sizeof ( *dhcp ) );
1319 dhcp->pxe_attempt = ip;
1320 if ( ! ( pxe_discovery_control & PXEBS_NO_MULTICAST ) ) {
1321 fetch_ipv4_setting ( NULL, &pxe_boot_server_mcast_setting, ip);
1325 if ( ! ( pxe_discovery_control & PXEBS_NO_BROADCAST ) )
1326 (ip++)->s_addr = INADDR_BROADCAST;
1327 if ( pxe_discovery_control & PXEBS_NO_UNKNOWN_SERVERS )
1328 dhcp->pxe_accept = ip;
1329 if ( pxebs_list_len ) {
1330 uint8_t buf[pxebs_list_len];
1332 fetch_setting ( NULL, &pxe_boot_servers_setting,
1333 buf, sizeof ( buf ) );
1334 pxebs_list ( dhcp, buf, sizeof ( buf ), ip );
1336 if ( ! dhcp->pxe_attempt->s_addr ) {
1337 DBGC ( dhcp, "DHCP %p has no PXE boot servers for type %04x\n",
1343 /* Dump out PXE server lists */
1344 DBGC ( dhcp, "DHCP %p attempting", dhcp );
1345 for ( ip = dhcp->pxe_attempt ; ip->s_addr ; ip++ )
1346 DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
1347 DBGC ( dhcp, "\n" );
1348 if ( dhcp->pxe_accept ) {
1349 DBGC ( dhcp, "DHCP %p accepting", dhcp );
1350 for ( ip = dhcp->pxe_accept ; ip->s_addr ; ip++ )
1351 DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
1352 DBGC ( dhcp, "\n" );
1355 /* Instantiate child objects and attach to our interfaces */
1356 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
1357 ( struct sockaddr * ) &dhcp->local ) ) != 0 )
1360 /* Enter PXEBS state */
1361 dhcp_set_state ( dhcp, &dhcp_state_pxebs );
1363 /* Attach parent interface, mortalise self, and return */
1364 job_plug_plug ( &dhcp->job, job );
1365 ref_put ( &dhcp->refcnt );
1369 dhcp_finished ( dhcp, rc );
1370 ref_put ( &dhcp->refcnt );