2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER );
28 #include <gpxe/if_ether.h>
29 #include <gpxe/netdevice.h>
30 #include <gpxe/device.h>
31 #include <gpxe/xfer.h>
32 #include <gpxe/open.h>
34 #include <gpxe/retry.h>
35 #include <gpxe/tcpip.h>
37 #include <gpxe/uuid.h>
38 #include <gpxe/timer.h>
39 #include <gpxe/settings.h>
40 #include <gpxe/dhcp.h>
41 #include <gpxe/dhcpopts.h>
42 #include <gpxe/dhcppkt.h>
43 #include <gpxe/features.h>
47 * Dynamic Host Configuration Protocol
52 static int dhcp_tx ( struct dhcp_session *dhcp );
55 * DHCP operation types
57 * This table maps from DHCP message types (i.e. values of the @c
58 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
61 static const uint8_t dhcp_op[] = {
62 [DHCPDISCOVER] = BOOTP_REQUEST,
63 [DHCPOFFER] = BOOTP_REPLY,
64 [DHCPREQUEST] = BOOTP_REQUEST,
65 [DHCPDECLINE] = BOOTP_REQUEST,
66 [DHCPACK] = BOOTP_REPLY,
67 [DHCPNAK] = BOOTP_REPLY,
68 [DHCPRELEASE] = BOOTP_REQUEST,
69 [DHCPINFORM] = BOOTP_REQUEST,
72 /** Raw option data for options common to all DHCP requests */
73 static uint8_t dhcp_request_options_data[] = {
74 DHCP_MESSAGE_TYPE, DHCP_BYTE ( 0 ),
75 DHCP_MAX_MESSAGE_SIZE,
76 DHCP_WORD ( ETH_MAX_MTU - 20 /* IP header */ - 8 /* UDP header */ ),
77 DHCP_CLIENT_ARCHITECTURE, DHCP_WORD ( 0 ),
78 DHCP_CLIENT_NDI, DHCP_OPTION ( 1 /* UNDI */ , 2, 1 /* v2.1 */ ),
80 DHCP_STRING ( 'P', 'X', 'E', 'C', 'l', 'i', 'e', 'n', 't', ':',
81 'A', 'r', 'c', 'h', ':', '0', '0', '0', '0', '0', ':',
82 'U', 'N', 'D', 'I', ':', '0', '0', '2', '0', '0', '1' ),
84 DHCP_STRING ( 'g', 'P', 'X', 'E' ),
85 DHCP_PARAMETER_REQUEST_LIST,
86 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
87 DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
88 DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
89 DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
90 DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
94 /** Version number feature */
95 FEATURE_VERSION ( VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH );
97 /** DHCP server address setting */
98 struct setting dhcp_server_setting __setting = {
99 .name = "dhcp-server",
100 .description = "DHCP server address",
101 .tag = DHCP_SERVER_IDENTIFIER,
102 .type = &setting_type_ipv4,
105 /** DHCP user class setting */
106 struct setting user_class_setting __setting = {
107 .name = "user-class",
108 .description = "User class identifier",
109 .tag = DHCP_USER_CLASS_ID,
110 .type = &setting_type_string,
114 * Name a DHCP packet type
116 * @v msgtype DHCP message type
117 * @ret string DHCP mesasge type name
119 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
121 case DHCPNONE: return "BOOTP"; /* Non-DHCP packet */
122 case DHCPDISCOVER: return "DHCPDISCOVER";
123 case DHCPOFFER: return "DHCPOFFER";
124 case DHCPREQUEST: return "DHCPREQUEST";
125 case DHCPDECLINE: return "DHCPDECLINE";
126 case DHCPACK: return "DHCPACK";
127 case DHCPNAK: return "DHCPNAK";
128 case DHCPRELEASE: return "DHCPRELEASE";
129 case DHCPINFORM: return "DHCPINFORM";
130 default: return "DHCP<invalid>";
135 * Calculate DHCP transaction ID for a network device
137 * @v netdev Network device
140 * Extract the least significant bits of the hardware address for use
141 * as the transaction ID.
143 static uint32_t dhcp_xid ( struct net_device *netdev ) {
146 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
147 - sizeof ( xid ) ), sizeof ( xid ) );
151 /****************************************************************************
159 /** DHCP session state operations */
160 struct dhcp_session_state {
164 * Construct transmitted packet
166 * @v dhcp DHCP session
167 * @v dhcppkt DHCP packet
168 * @v peer Destination address
170 int ( * tx ) ( struct dhcp_session *dhcp,
171 struct dhcp_packet *dhcppkt,
172 struct sockaddr_in *peer );
173 /** Handle received packet
175 * @v dhcp DHCP session
176 * @v dhcppkt DHCP packet
177 * @v peer DHCP server address
178 * @v msgtype DHCP message type
179 * @v server_id DHCP server ID
181 void ( * rx ) ( struct dhcp_session *dhcp,
182 struct dhcp_packet *dhcppkt,
183 struct sockaddr_in *peer,
184 uint8_t msgtype, struct in_addr server_id );
185 /** Handle timer expiry
187 * @v dhcp DHCP session
189 void ( * expired ) ( struct dhcp_session *dhcp );
190 /** Transmitted message type */
192 /** Apply minimum timeout */
193 uint8_t apply_min_timeout;
196 static struct dhcp_session_state dhcp_state_discover;
197 static struct dhcp_session_state dhcp_state_request;
198 static struct dhcp_session_state dhcp_state_proxy;
199 static struct dhcp_session_state dhcp_state_pxebs;
201 /** A DHCP session */
202 struct dhcp_session {
203 /** Reference counter */
204 struct refcnt refcnt;
205 /** Job control interface */
206 struct job_interface job;
207 /** Data transfer interface */
208 struct xfer_interface xfer;
210 /** Network device being configured */
211 struct net_device *netdev;
212 /** Local socket address */
213 struct sockaddr_in local;
214 /** State of the session */
215 struct dhcp_session_state *state;
217 /** Offered IP address */
218 struct in_addr offer;
220 struct in_addr server;
221 /** DHCP offer priority */
224 /** ProxyDHCP protocol extensions should be ignored */
226 /** ProxyDHCP server */
227 struct in_addr proxy_server;
228 /** ProxyDHCP port */
230 /** ProxyDHCP server priority */
233 /** PXE Boot Server type */
235 /** List of PXE Boot Servers to attempt */
236 struct in_addr *pxe_attempt;
237 /** List of PXE Boot Servers to accept */
238 struct in_addr *pxe_accept;
240 /** Retransmission timer */
241 struct retry_timer timer;
242 /** Start time of the current state (in ticks) */
249 * @v refcnt Reference counter
251 static void dhcp_free ( struct refcnt *refcnt ) {
252 struct dhcp_session *dhcp =
253 container_of ( refcnt, struct dhcp_session, refcnt );
255 netdev_put ( dhcp->netdev );
260 * Mark DHCP session as complete
262 * @v dhcp DHCP session
263 * @v rc Return status code
265 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
267 /* Block futher incoming messages */
268 job_nullify ( &dhcp->job );
269 xfer_nullify ( &dhcp->xfer );
271 /* Stop retry timer */
272 stop_timer ( &dhcp->timer );
274 /* Free resources and close interfaces */
275 xfer_close ( &dhcp->xfer, rc );
276 job_done ( &dhcp->job, rc );
280 * Transition to new DHCP session state
282 * @v dhcp DHCP session
283 * @v state New session state
285 static void dhcp_set_state ( struct dhcp_session *dhcp,
286 struct dhcp_session_state *state ) {
288 DBGC ( dhcp, "DHCP %p entering %s state\n", dhcp, state->name );
290 dhcp->start = currticks();
291 stop_timer ( &dhcp->timer );
292 dhcp->timer.min_timeout =
293 ( state->apply_min_timeout ? DHCP_MIN_TIMEOUT : 0 );
294 dhcp->timer.max_timeout = DHCP_MAX_TIMEOUT;
295 start_timer_nodelay ( &dhcp->timer );
298 /****************************************************************************
305 * Construct transmitted packet for DHCP discovery
307 * @v dhcp DHCP session
308 * @v dhcppkt DHCP packet
309 * @v peer Destination address
311 static int dhcp_discovery_tx ( struct dhcp_session *dhcp,
312 struct dhcp_packet *dhcppkt __unused,
313 struct sockaddr_in *peer ) {
315 DBGC ( dhcp, "DHCP %p DHCPDISCOVER\n", dhcp );
317 /* Set server address */
318 peer->sin_addr.s_addr = INADDR_BROADCAST;
319 peer->sin_port = htons ( BOOTPS_PORT );
325 * Handle received packet during DHCP discovery
327 * @v dhcp DHCP session
328 * @v dhcppkt DHCP packet
329 * @v peer DHCP server address
330 * @v msgtype DHCP message type
331 * @v server_id DHCP server ID
333 static void dhcp_discovery_rx ( struct dhcp_session *dhcp,
334 struct dhcp_packet *dhcppkt,
335 struct sockaddr_in *peer, uint8_t msgtype,
336 struct in_addr server_id ) {
338 char vci[9]; /* "PXEClient" */
344 uint8_t no_pxedhcp = 0;
345 unsigned long elapsed;
347 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
348 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
349 ntohs ( peer->sin_port ) );
350 if ( server_id.s_addr != peer->sin_addr.s_addr )
351 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
353 /* Identify offered IP address */
354 ip = dhcppkt->dhcphdr->yiaddr;
356 DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
358 /* Identify "PXEClient" vendor class */
359 vci_len = dhcppkt_fetch ( dhcppkt, DHCP_VENDOR_CLASS_ID,
360 vci, sizeof ( vci ) );
361 has_pxeclient = ( ( vci_len >= ( int ) sizeof ( vci ) ) &&
362 ( strncmp ( "PXEClient", vci, sizeof (vci) ) == 0 ));
364 /* Identify presence of vendor-specific options */
365 pxeopts_len = dhcppkt_fetch ( dhcppkt, DHCP_VENDOR_ENCAP, NULL, 0 );
366 has_pxeopts = ( pxeopts_len >= 0 );
368 DBGC ( dhcp, "%s", ( has_pxeopts ? " pxe" : " proxy" ) );
370 /* Identify priority */
371 dhcppkt_fetch ( dhcppkt, DHCP_EB_PRIORITY, &priority,
372 sizeof ( priority ) );
374 DBGC ( dhcp, " pri %d", priority );
376 /* Identify ignore-PXE flag */
377 dhcppkt_fetch ( dhcppkt, DHCP_EB_NO_PXEDHCP, &no_pxedhcp,
378 sizeof ( no_pxedhcp ) );
380 DBGC ( dhcp, " nopxe" );
383 /* Select as DHCP offer, if applicable */
384 if ( ip.s_addr && ( peer->sin_port == htons ( BOOTPS_PORT ) ) &&
385 ( ( msgtype == DHCPOFFER ) || ( ! msgtype /* BOOTP */ ) ) &&
386 ( priority >= dhcp->priority ) ) {
388 dhcp->server = server_id;
389 dhcp->priority = priority;
390 dhcp->no_pxedhcp = no_pxedhcp;
393 /* Select as ProxyDHCP offer, if applicable */
394 if ( has_pxeclient && ( msgtype == DHCPOFFER ) &&
395 ( priority >= dhcp->proxy_priority ) ) {
396 /* If the offer already includes the PXE options, then
397 * assume that we can send the ProxyDHCPREQUEST to
398 * port 67 (since the DHCPDISCOVER that triggered this
399 * ProxyDHCPOFFER was sent to port 67). Otherwise,
400 * send the ProxyDHCPREQUEST to port 4011.
402 dhcp->proxy_server = server_id;
403 dhcp->proxy_port = ( has_pxeopts ? htons ( BOOTPS_PORT )
404 : htons ( PXE_PORT ) );
405 dhcp->proxy_priority = priority;
408 /* We can exit the discovery state when we have a valid
409 * DHCPOFFER, and either:
411 * o The DHCPOFFER instructs us to ignore ProxyDHCPOFFERs, or
412 * o We have a valid ProxyDHCPOFFER, or
413 * o We have allowed sufficient time for ProxyDHCPOFFERs.
416 /* If we don't yet have a DHCPOFFER, do nothing */
417 if ( ! dhcp->offer.s_addr )
420 /* If we can't yet transition to DHCPREQUEST, do nothing */
421 elapsed = ( currticks() - dhcp->start );
422 if ( ! ( dhcp->no_pxedhcp || dhcp->proxy_server.s_addr ||
423 ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) )
426 /* Transition to DHCPREQUEST */
427 dhcp_set_state ( dhcp, &dhcp_state_request );
431 * Handle timer expiry during DHCP discovery
433 * @v dhcp DHCP session
435 static void dhcp_discovery_expired ( struct dhcp_session *dhcp ) {
436 unsigned long elapsed = ( currticks() - dhcp->start );
438 /* Give up waiting for ProxyDHCP before we reach the failure point */
439 if ( dhcp->offer.s_addr && ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) {
440 dhcp_set_state ( dhcp, &dhcp_state_request );
444 /* Otherwise, retransmit current packet */
448 /** DHCP discovery state operations */
449 static struct dhcp_session_state dhcp_state_discover = {
451 .tx = dhcp_discovery_tx,
452 .rx = dhcp_discovery_rx,
453 .expired = dhcp_discovery_expired,
454 .tx_msgtype = DHCPDISCOVER,
455 .apply_min_timeout = 1,
459 * Construct transmitted packet for DHCP request
461 * @v dhcp DHCP session
462 * @v dhcppkt DHCP packet
463 * @v peer Destination address
465 static int dhcp_request_tx ( struct dhcp_session *dhcp,
466 struct dhcp_packet *dhcppkt,
467 struct sockaddr_in *peer ) {
470 DBGC ( dhcp, "DHCP %p DHCPREQUEST to %s:%d",
471 dhcp, inet_ntoa ( dhcp->server ), BOOTPS_PORT );
472 DBGC ( dhcp, " for %s\n", inet_ntoa ( dhcp->offer ) );
475 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
477 sizeof ( dhcp->server ) ) ) != 0 )
480 /* Set requested IP address */
481 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
483 sizeof ( dhcp->offer ) ) ) != 0 )
486 /* Set server address */
487 peer->sin_addr.s_addr = INADDR_BROADCAST;
488 peer->sin_port = htons ( BOOTPS_PORT );
494 * Handle received packet during DHCP request
496 * @v dhcp DHCP session
497 * @v dhcppkt DHCP packet
498 * @v peer DHCP server address
499 * @v msgtype DHCP message type
500 * @v server_id DHCP server ID
502 static void dhcp_request_rx ( struct dhcp_session *dhcp,
503 struct dhcp_packet *dhcppkt,
504 struct sockaddr_in *peer, uint8_t msgtype,
505 struct in_addr server_id ) {
507 struct settings *parent;
510 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
511 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
512 ntohs ( peer->sin_port ) );
513 if ( server_id.s_addr != peer->sin_addr.s_addr )
514 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
516 /* Identify leased IP address */
517 ip = dhcppkt->dhcphdr->yiaddr;
519 DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
522 /* Filter out unacceptable responses */
523 if ( peer->sin_port != htons ( BOOTPS_PORT ) )
525 if ( msgtype /* BOOTP */ && ( msgtype != DHCPACK ) )
527 if ( server_id.s_addr != dhcp->server.s_addr )
530 /* Record assigned address */
531 dhcp->local.sin_addr = ip;
533 /* Register settings */
534 parent = netdev_settings ( dhcp->netdev );
535 if ( ( rc = register_settings ( &dhcppkt->settings, parent ) ) != 0 ){
536 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
537 dhcp, strerror ( rc ) );
538 dhcp_finished ( dhcp, rc );
542 /* Start ProxyDHCPREQUEST if applicable */
543 if ( dhcp->proxy_server.s_addr /* Have ProxyDHCP server */ &&
544 ( ! dhcp->no_pxedhcp ) /* ProxyDHCP not disabled */ &&
545 ( /* ProxyDHCP server is not just the DHCP server itself */
546 ( dhcp->proxy_server.s_addr != dhcp->server.s_addr ) ||
547 ( dhcp->proxy_port != htons ( BOOTPS_PORT ) ) ) ) {
548 dhcp_set_state ( dhcp, &dhcp_state_proxy );
553 dhcp_finished ( dhcp, 0 );
557 * Handle timer expiry during DHCP discovery
559 * @v dhcp DHCP session
561 static void dhcp_request_expired ( struct dhcp_session *dhcp ) {
563 /* Retransmit current packet */
567 /** DHCP request state operations */
568 static struct dhcp_session_state dhcp_state_request = {
570 .tx = dhcp_request_tx,
571 .rx = dhcp_request_rx,
572 .expired = dhcp_request_expired,
573 .tx_msgtype = DHCPREQUEST,
574 .apply_min_timeout = 0,
578 * Construct transmitted packet for ProxyDHCP request
580 * @v dhcp DHCP session
581 * @v dhcppkt DHCP packet
582 * @v peer Destination address
584 static int dhcp_proxy_tx ( struct dhcp_session *dhcp,
585 struct dhcp_packet *dhcppkt,
586 struct sockaddr_in *peer ) {
589 DBGC ( dhcp, "DHCP %p ProxyDHCP REQUEST to %s:%d\n", dhcp,
590 inet_ntoa ( dhcp->proxy_server ), ntohs ( dhcp->proxy_port ) );
593 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
595 sizeof ( dhcp->proxy_server ) ) ) != 0 )
598 /* Set server address */
599 peer->sin_addr = dhcp->proxy_server;
600 peer->sin_port = dhcp->proxy_port;
606 * Handle received packet during ProxyDHCP request
608 * @v dhcp DHCP session
609 * @v dhcppkt DHCP packet
610 * @v peer DHCP server address
611 * @v msgtype DHCP message type
612 * @v server_id DHCP server ID
614 static void dhcp_proxy_rx ( struct dhcp_session *dhcp,
615 struct dhcp_packet *dhcppkt,
616 struct sockaddr_in *peer, uint8_t msgtype,
617 struct in_addr server_id ) {
620 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
621 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
622 ntohs ( peer->sin_port ) );
623 if ( server_id.s_addr != peer->sin_addr.s_addr )
624 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
627 /* Filter out unacceptable responses */
628 if ( peer->sin_port != dhcp->proxy_port )
630 if ( ( msgtype != DHCPOFFER ) && ( msgtype != DHCPACK ) )
632 if ( server_id.s_addr /* Linux PXE server omits server ID */ &&
633 ( server_id.s_addr != dhcp->proxy_server.s_addr ) )
636 /* Register settings */
637 dhcppkt->settings.name = PROXYDHCP_SETTINGS_NAME;
638 if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
639 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
640 dhcp, strerror ( rc ) );
641 dhcp_finished ( dhcp, rc );
646 dhcp_finished ( dhcp, 0 );
650 * Handle timer expiry during ProxyDHCP request
652 * @v dhcp DHCP session
654 static void dhcp_proxy_expired ( struct dhcp_session *dhcp ) {
655 unsigned long elapsed = ( currticks() - dhcp->start );
657 /* Give up waiting for ProxyDHCP before we reach the failure point */
658 if ( elapsed > PROXYDHCP_MAX_TIMEOUT ) {
659 dhcp_finished ( dhcp, 0 );
663 /* Retransmit current packet */
667 /** ProxyDHCP request state operations */
668 static struct dhcp_session_state dhcp_state_proxy = {
672 .expired = dhcp_proxy_expired,
673 .tx_msgtype = DHCPREQUEST,
674 .apply_min_timeout = 0,
678 * Construct transmitted packet for PXE Boot Server Discovery
680 * @v dhcp DHCP session
681 * @v dhcppkt DHCP packet
682 * @v peer Destination address
684 static int dhcp_pxebs_tx ( struct dhcp_session *dhcp,
685 struct dhcp_packet *dhcppkt,
686 struct sockaddr_in *peer ) {
687 struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
690 /* Set server address */
691 peer->sin_addr = *(dhcp->pxe_attempt);
692 peer->sin_port = ( ( peer->sin_addr.s_addr == INADDR_BROADCAST ) ?
693 htons ( BOOTPS_PORT ) : htons ( PXE_PORT ) );
695 DBGC ( dhcp, "DHCP %p PXEBS REQUEST to %s:%d for type %d\n",
696 dhcp, inet_ntoa ( peer->sin_addr ), ntohs ( peer->sin_port ),
697 le16_to_cpu ( dhcp->pxe_type ) );
699 /* Set boot menu item */
700 menu_item.type = dhcp->pxe_type;
701 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
702 &menu_item, sizeof ( menu_item ) ) ) != 0 )
709 * Check to see if PXE Boot Server address is acceptable
711 * @v dhcp DHCP session
712 * @v bs Boot Server address
713 * @ret accept Boot Server is acceptable
715 static int dhcp_pxebs_accept ( struct dhcp_session *dhcp,
716 struct in_addr bs ) {
717 struct in_addr *accept;
719 /* Accept if we have no acceptance filter */
720 if ( ! dhcp->pxe_accept )
723 /* Scan through acceptance list */
724 for ( accept = dhcp->pxe_accept ; accept->s_addr ; accept++ ) {
725 if ( accept->s_addr == bs.s_addr )
729 DBGC ( dhcp, "DHCP %p rejecting server %s\n",
730 dhcp, inet_ntoa ( bs ) );
735 * Handle received packet during PXE Boot Server Discovery
737 * @v dhcp DHCP session
738 * @v dhcppkt DHCP packet
739 * @v peer DHCP server address
740 * @v msgtype DHCP message type
741 * @v server_id DHCP server ID
743 static void dhcp_pxebs_rx ( struct dhcp_session *dhcp,
744 struct dhcp_packet *dhcppkt,
745 struct sockaddr_in *peer, uint8_t msgtype,
746 struct in_addr server_id ) {
747 struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
750 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
751 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
752 ntohs ( peer->sin_port ) );
753 if ( server_id.s_addr != peer->sin_addr.s_addr )
754 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
756 /* Identify boot menu item */
757 dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
758 &menu_item, sizeof ( menu_item ) );
759 if ( menu_item.type )
760 DBGC ( dhcp, " for type %d", ntohs ( menu_item.type ) );
763 /* Filter out unacceptable responses */
764 if ( ( peer->sin_port != htons ( BOOTPS_PORT ) ) &&
765 ( peer->sin_port != htons ( PXE_PORT ) ) )
767 if ( msgtype != DHCPACK )
769 if ( menu_item.type != dhcp->pxe_type )
771 if ( ! dhcp_pxebs_accept ( dhcp, ( server_id.s_addr ?
772 server_id : peer->sin_addr ) ) )
775 /* Register settings */
776 dhcppkt->settings.name = PXEBS_SETTINGS_NAME;
777 if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
778 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
779 dhcp, strerror ( rc ) );
780 dhcp_finished ( dhcp, rc );
785 dhcp_finished ( dhcp, 0 );
789 * Handle timer expiry during PXE Boot Server Discovery
791 * @v dhcp DHCP session
793 static void dhcp_pxebs_expired ( struct dhcp_session *dhcp ) {
794 unsigned long elapsed = ( currticks() - dhcp->start );
796 /* Give up waiting before we reach the failure point, and fail
797 * over to the next server in the attempt list
799 if ( elapsed > PXEBS_MAX_TIMEOUT ) {
801 if ( dhcp->pxe_attempt->s_addr ) {
802 dhcp_set_state ( dhcp, &dhcp_state_pxebs );
805 dhcp_finished ( dhcp, -ETIMEDOUT );
810 /* Retransmit current packet */
814 /** PXE Boot Server Discovery state operations */
815 static struct dhcp_session_state dhcp_state_pxebs = {
819 .expired = dhcp_pxebs_expired,
820 .tx_msgtype = DHCPREQUEST,
821 .apply_min_timeout = 1,
824 /****************************************************************************
826 * Packet construction
831 * Construct DHCP client hardware address field and broadcast flag
833 * @v netdev Network device
834 * @v hlen DHCP hardware address length to fill in
835 * @v flags DHCP flags to fill in
836 * @ret chaddr DHCP client hardware address
838 void * dhcp_chaddr ( struct net_device *netdev, uint8_t *hlen,
840 struct ll_protocol *ll_protocol = netdev->ll_protocol;
841 typeof ( ( ( struct dhcphdr * ) NULL )->chaddr ) chaddr;
843 /* If the link-layer address cannot fit into the chaddr field
844 * (as is the case for IPoIB) then try using the hardware
845 * address instead. If we do this, set the broadcast flag,
846 * since chaddr then does not represent a valid link-layer
847 * address for the return path.
849 * If even the hardware address is too large, use an empty
850 * chaddr field and set the broadcast flag.
852 * This goes against RFC4390, but RFC4390 mandates that we use
853 * a DHCP client identifier that conforms with RFC4361, which
854 * we cannot do without either persistent (NIC-independent)
855 * storage, or by eliminating the hardware address completely
856 * from the DHCP packet, which seems unfriendly to users.
858 if ( ( *hlen = ll_protocol->ll_addr_len ) <= sizeof ( chaddr ) ) {
859 return netdev->ll_addr;
861 *flags = htons ( BOOTP_FL_BROADCAST );
862 if ( ( *hlen = ll_protocol->hw_addr_len ) <= sizeof ( chaddr ) ) {
863 return netdev->hw_addr;
871 * Create a DHCP packet
873 * @v dhcppkt DHCP packet structure to fill in
874 * @v netdev Network device
875 * @v msgtype DHCP message type
876 * @v options Initial options to include (or NULL)
877 * @v options_len Length of initial options
878 * @v data Buffer for DHCP packet
879 * @v max_len Size of DHCP packet buffer
880 * @ret rc Return status code
882 * Creates a DHCP packet in the specified buffer, and initialise a
883 * DHCP packet structure.
885 int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
886 struct net_device *netdev, uint8_t msgtype,
887 const void *options, size_t options_len,
888 void *data, size_t max_len ) {
889 struct dhcphdr *dhcphdr = data;
894 if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
897 /* Initialise DHCP packet content */
898 memset ( dhcphdr, 0, max_len );
899 dhcphdr->xid = dhcp_xid ( netdev );
900 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
901 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
902 dhcphdr->op = dhcp_op[msgtype];
903 chaddr = dhcp_chaddr ( netdev, &dhcphdr->hlen, &dhcphdr->flags );
904 memcpy ( dhcphdr->chaddr, chaddr, dhcphdr->hlen );
905 memcpy ( dhcphdr->options, options, options_len );
907 /* Initialise DHCP packet structure */
908 memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
909 dhcppkt_init ( dhcppkt, data, max_len );
911 /* Set DHCP_MESSAGE_TYPE option */
912 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
913 &msgtype, sizeof ( msgtype ) ) ) != 0 )
920 * Create DHCP request packet
922 * @v dhcppkt DHCP packet structure to fill in
923 * @v netdev Network device
924 * @v msgtype DHCP message type
925 * @v ciaddr Client IP address
926 * @v data Buffer for DHCP packet
927 * @v max_len Size of DHCP packet buffer
928 * @ret rc Return status code
930 * Creates a DHCP request packet in the specified buffer, and
931 * initialise a DHCP packet structure.
933 int dhcp_create_request ( struct dhcp_packet *dhcppkt,
934 struct net_device *netdev, unsigned int msgtype,
935 struct in_addr ciaddr, void *data, size_t max_len ) {
936 struct dhcp_netdev_desc dhcp_desc;
937 struct dhcp_client_id client_id;
938 struct dhcp_client_uuid client_uuid;
939 uint8_t *dhcp_features;
940 size_t dhcp_features_len;
945 /* Create DHCP packet */
946 if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
947 dhcp_request_options_data,
948 sizeof ( dhcp_request_options_data ),
949 data, max_len ) ) != 0 ) {
950 DBG ( "DHCP could not create DHCP packet: %s\n",
955 /* Set client IP address */
956 dhcppkt->dhcphdr->ciaddr = ciaddr;
958 /* Add options to identify the feature list */
959 dhcp_features = table_start ( DHCP_FEATURES );
960 dhcp_features_len = table_num_entries ( DHCP_FEATURES );
961 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
962 dhcp_features_len ) ) != 0 ) {
963 DBG ( "DHCP could not set features list option: %s\n",
968 /* Add options to identify the network device */
969 fetch_setting ( &netdev->settings.settings, &busid_setting, &dhcp_desc,
970 sizeof ( dhcp_desc ) );
971 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
972 sizeof ( dhcp_desc ) ) ) != 0 ) {
973 DBG ( "DHCP could not set bus ID option: %s\n",
978 /* Add DHCP client identifier. Required for Infiniband, and
979 * doesn't hurt other link layers.
981 client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
982 ll_addr_len = netdev->ll_protocol->ll_addr_len;
983 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
984 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
985 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
986 ( ll_addr_len + 1 ) ) ) != 0 ) {
987 DBG ( "DHCP could not set client ID: %s\n",
992 /* Add client UUID, if we have one. Required for PXE. */
993 client_uuid.type = DHCP_CLIENT_UUID_TYPE;
994 if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting,
995 &client_uuid.uuid ) ) >= 0 ) {
996 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
998 sizeof ( client_uuid ) ) ) != 0 ) {
999 DBG ( "DHCP could not set client UUID: %s\n",
1005 /* Add user class, if we have one. */
1006 if ( ( len = fetch_setting_len ( NULL, &user_class_setting ) ) >= 0 ) {
1007 char user_class[len];
1008 fetch_setting ( NULL, &user_class_setting, user_class,
1009 sizeof ( user_class ) );
1010 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_USER_CLASS_ID,
1012 sizeof ( user_class ) ) ) != 0 ) {
1013 DBG ( "DHCP could not set user class: %s\n",
1022 /****************************************************************************
1024 * Data transfer interface
1029 * Transmit DHCP request
1031 * @v dhcp DHCP session
1032 * @ret rc Return status code
1034 static int dhcp_tx ( struct dhcp_session *dhcp ) {
1035 static struct sockaddr_in peer = {
1036 .sin_family = AF_INET,
1038 struct xfer_metadata meta = {
1039 .netdev = dhcp->netdev,
1040 .src = ( struct sockaddr * ) &dhcp->local,
1041 .dest = ( struct sockaddr * ) &peer,
1043 struct io_buffer *iobuf;
1044 uint8_t msgtype = dhcp->state->tx_msgtype;
1045 struct dhcp_packet dhcppkt;
1048 /* Start retry timer. Do this first so that failures to
1049 * transmit will be retried.
1051 start_timer ( &dhcp->timer );
1053 /* Allocate buffer for packet */
1054 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
1058 /* Create basic DHCP packet in temporary buffer */
1059 if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev, msgtype,
1060 dhcp->local.sin_addr, iobuf->data,
1061 iob_tailroom ( iobuf ) ) ) != 0 ) {
1062 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
1063 dhcp, strerror ( rc ) );
1067 /* Fill in packet based on current state */
1068 if ( ( rc = dhcp->state->tx ( dhcp, &dhcppkt, &peer ) ) != 0 ) {
1069 DBGC ( dhcp, "DHCP %p could not fill DHCP request: %s\n",
1070 dhcp, strerror ( rc ) );
1074 /* Transmit the packet */
1075 iob_put ( iobuf, dhcppkt.len );
1076 if ( ( rc = xfer_deliver_iob_meta ( &dhcp->xfer, iob_disown ( iobuf ),
1078 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
1079 dhcp, strerror ( rc ) );
1091 * @v xfer Data transfer interface
1092 * @v iobuf I/O buffer
1093 * @v meta Transfer metadata
1094 * @ret rc Return status code
1096 static int dhcp_deliver_iob ( struct xfer_interface *xfer,
1097 struct io_buffer *iobuf,
1098 struct xfer_metadata *meta ) {
1099 struct dhcp_session *dhcp =
1100 container_of ( xfer, struct dhcp_session, xfer );
1101 struct sockaddr_in *peer;
1103 struct dhcp_packet *dhcppkt;
1104 struct dhcphdr *dhcphdr;
1105 uint8_t msgtype = 0;
1106 struct in_addr server_id = { 0 };
1110 if ( ! meta->src ) {
1111 DBGC ( dhcp, "DHCP %p received packet without source port\n",
1116 peer = ( struct sockaddr_in * ) meta->src;
1118 /* Create a DHCP packet containing the I/O buffer contents.
1119 * Whilst we could just use the original buffer in situ, that
1120 * would waste the unused space in the packet buffer, and also
1121 * waste a relatively scarce fully-aligned I/O buffer.
1123 data_len = iob_len ( iobuf );
1124 dhcppkt = zalloc ( sizeof ( *dhcppkt ) + data_len );
1127 goto err_alloc_dhcppkt;
1129 dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
1130 memcpy ( dhcphdr, iobuf->data, data_len );
1131 dhcppkt_init ( dhcppkt, dhcphdr, data_len );
1133 /* Identify message type */
1134 dhcppkt_fetch ( dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
1135 sizeof ( msgtype ) );
1137 /* Identify server ID */
1138 dhcppkt_fetch ( dhcppkt, DHCP_SERVER_IDENTIFIER,
1139 &server_id, sizeof ( server_id ) );
1141 /* Check for matching transaction ID */
1142 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
1143 DBGC ( dhcp, "DHCP %p %s from %s:%d has bad transaction "
1144 "ID\n", dhcp, dhcp_msgtype_name ( msgtype ),
1145 inet_ntoa ( peer->sin_addr ),
1146 ntohs ( peer->sin_port ) );
1151 /* Handle packet based on current state */
1152 dhcp->state->rx ( dhcp, dhcppkt, peer, msgtype, server_id );
1155 dhcppkt_put ( dhcppkt );
1162 /** DHCP data transfer interface operations */
1163 static struct xfer_interface_operations dhcp_xfer_operations = {
1164 .close = ignore_xfer_close,
1165 .vredirect = xfer_vreopen,
1166 .window = unlimited_xfer_window,
1167 .alloc_iob = default_xfer_alloc_iob,
1168 .deliver_iob = dhcp_deliver_iob,
1169 .deliver_raw = xfer_deliver_as_iob,
1173 * Handle DHCP retry timer expiry
1175 * @v timer DHCP retry timer
1176 * @v fail Failure indicator
1178 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
1179 struct dhcp_session *dhcp =
1180 container_of ( timer, struct dhcp_session, timer );
1182 /* If we have failed, terminate DHCP */
1184 dhcp_finished ( dhcp, -ETIMEDOUT );
1188 /* Handle timer expiry based on current state */
1189 dhcp->state->expired ( dhcp );
1192 /****************************************************************************
1194 * Job control interface
1199 * Handle kill() event received via job control interface
1201 * @v job DHCP job control interface
1203 static void dhcp_job_kill ( struct job_interface *job ) {
1204 struct dhcp_session *dhcp =
1205 container_of ( job, struct dhcp_session, job );
1207 /* Terminate DHCP session */
1208 dhcp_finished ( dhcp, -ECANCELED );
1211 /** DHCP job control interface operations */
1212 static struct job_interface_operations dhcp_job_operations = {
1213 .done = ignore_job_done,
1214 .kill = dhcp_job_kill,
1215 .progress = ignore_job_progress,
1218 /****************************************************************************
1225 * DHCP peer address for socket opening
1227 * This is a dummy address; the only useful portion is the socket
1228 * family (so that we get a UDP connection). The DHCP client will set
1229 * the IP address and source port explicitly on each transmission.
1231 static struct sockaddr dhcp_peer = {
1232 .sa_family = AF_INET,
1236 * Start DHCP state machine on a network device
1238 * @v job Job control interface
1239 * @v netdev Network device
1240 * @ret rc Return status code
1242 * Starts DHCP on the specified network device. If successful, the
1243 * DHCPACK (and ProxyDHCPACK, if applicable) will be registered as
1246 int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
1247 struct dhcp_session *dhcp;
1250 /* Allocate and initialise structure */
1251 dhcp = zalloc ( sizeof ( *dhcp ) );
1254 dhcp->refcnt.free = dhcp_free;
1255 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1256 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1257 dhcp->netdev = netdev_get ( netdev );
1258 dhcp->local.sin_family = AF_INET;
1259 dhcp->local.sin_port = htons ( BOOTPC_PORT );
1260 dhcp->timer.expired = dhcp_timer_expired;
1262 /* Instantiate child objects and attach to our interfaces */
1263 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
1264 ( struct sockaddr * ) &dhcp->local ) ) != 0 )
1267 /* Enter DHCPDISCOVER state */
1268 dhcp_set_state ( dhcp, &dhcp_state_discover );
1270 /* Attach parent interface, mortalise self, and return */
1271 job_plug_plug ( &dhcp->job, job );
1272 ref_put ( &dhcp->refcnt );
1276 dhcp_finished ( dhcp, rc );
1277 ref_put ( &dhcp->refcnt );
1282 * Retrieve list of PXE boot servers for a given server type
1284 * @v dhcp DHCP session
1285 * @v raw DHCP PXE boot server list
1286 * @v raw_len Length of DHCP PXE boot server list
1287 * @v ip IP address list to fill in
1289 * The caller must ensure that the IP address list has sufficient
1292 static void pxebs_list ( struct dhcp_session *dhcp, void *raw,
1293 size_t raw_len, struct in_addr *ip ) {
1294 struct dhcp_pxe_boot_server *server = raw;
1299 if ( raw_len < sizeof ( *server ) ) {
1300 DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
1304 server_len = offsetof ( typeof ( *server ),
1305 ip[ server->num_ip ] );
1306 if ( raw_len < server_len ) {
1307 DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
1311 if ( server->type == dhcp->pxe_type ) {
1312 for ( i = 0 ; i < server->num_ip ; i++ )
1313 *(ip++) = server->ip[i];
1315 server = ( ( ( void * ) server ) + server_len );
1316 raw_len -= server_len;
1321 * Start PXE Boot Server Discovery on a network device
1323 * @v job Job control interface
1324 * @v netdev Network device
1325 * @v pxe_type PXE server type
1326 * @ret rc Return status code
1328 * Starts PXE Boot Server Discovery on the specified network device.
1329 * If successful, the Boot Server ACK will be registered as an option
1332 int start_pxebs ( struct job_interface *job, struct net_device *netdev,
1333 unsigned int pxe_type ) {
1334 struct setting pxe_discovery_control_setting =
1335 { .tag = DHCP_PXE_DISCOVERY_CONTROL };
1336 struct setting pxe_boot_servers_setting =
1337 { .tag = DHCP_PXE_BOOT_SERVERS };
1338 struct setting pxe_boot_server_mcast_setting =
1339 { .tag = DHCP_PXE_BOOT_SERVER_MCAST };
1340 ssize_t pxebs_list_len;
1341 struct dhcp_session *dhcp;
1343 unsigned int pxe_discovery_control;
1346 /* Get upper bound for PXE boot server IP address list */
1347 pxebs_list_len = fetch_setting_len ( NULL, &pxe_boot_servers_setting );
1348 if ( pxebs_list_len < 0 )
1351 /* Allocate and initialise structure */
1352 dhcp = zalloc ( sizeof ( *dhcp ) + sizeof ( *ip ) /* mcast */ +
1353 sizeof ( *ip ) /* bcast */ + pxebs_list_len +
1354 sizeof ( *ip ) /* terminator */ );
1357 dhcp->refcnt.free = dhcp_free;
1358 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1359 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1360 dhcp->netdev = netdev_get ( netdev );
1361 dhcp->local.sin_family = AF_INET;
1362 fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
1363 &dhcp->local.sin_addr );
1364 dhcp->local.sin_port = htons ( BOOTPC_PORT );
1365 dhcp->pxe_type = cpu_to_le16 ( pxe_type );
1366 dhcp->timer.expired = dhcp_timer_expired;
1368 /* Construct PXE boot server IP address lists */
1369 pxe_discovery_control =
1370 fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
1371 ip = ( ( ( void * ) dhcp ) + sizeof ( *dhcp ) );
1372 dhcp->pxe_attempt = ip;
1373 if ( ! ( pxe_discovery_control & PXEBS_NO_MULTICAST ) ) {
1374 fetch_ipv4_setting ( NULL, &pxe_boot_server_mcast_setting, ip);
1378 if ( ! ( pxe_discovery_control & PXEBS_NO_BROADCAST ) )
1379 (ip++)->s_addr = INADDR_BROADCAST;
1380 if ( pxe_discovery_control & PXEBS_NO_UNKNOWN_SERVERS )
1381 dhcp->pxe_accept = ip;
1382 if ( pxebs_list_len ) {
1383 uint8_t buf[pxebs_list_len];
1385 fetch_setting ( NULL, &pxe_boot_servers_setting,
1386 buf, sizeof ( buf ) );
1387 pxebs_list ( dhcp, buf, sizeof ( buf ), ip );
1389 if ( ! dhcp->pxe_attempt->s_addr ) {
1390 DBGC ( dhcp, "DHCP %p has no PXE boot servers for type %04x\n",
1396 /* Dump out PXE server lists */
1397 DBGC ( dhcp, "DHCP %p attempting", dhcp );
1398 for ( ip = dhcp->pxe_attempt ; ip->s_addr ; ip++ )
1399 DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
1400 DBGC ( dhcp, "\n" );
1401 if ( dhcp->pxe_accept ) {
1402 DBGC ( dhcp, "DHCP %p accepting", dhcp );
1403 for ( ip = dhcp->pxe_accept ; ip->s_addr ; ip++ )
1404 DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
1405 DBGC ( dhcp, "\n" );
1408 /* Instantiate child objects and attach to our interfaces */
1409 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
1410 ( struct sockaddr * ) &dhcp->local ) ) != 0 )
1413 /* Enter PXEBS state */
1414 dhcp_set_state ( dhcp, &dhcp_state_pxebs );
1416 /* Attach parent interface, mortalise self, and return */
1417 job_plug_plug ( &dhcp->job, job );
1418 ref_put ( &dhcp->refcnt );
1422 dhcp_finished ( dhcp, rc );
1423 ref_put ( &dhcp->refcnt );