2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
28 #include <gpxe/if_ether.h>
29 #include <gpxe/netdevice.h>
30 #include <gpxe/device.h>
31 #include <gpxe/xfer.h>
32 #include <gpxe/open.h>
34 #include <gpxe/retry.h>
35 #include <gpxe/tcpip.h>
37 #include <gpxe/uuid.h>
38 #include <gpxe/timer.h>
39 #include <gpxe/settings.h>
40 #include <gpxe/dhcp.h>
41 #include <gpxe/dhcpopts.h>
42 #include <gpxe/dhcppkt.h>
43 #include <gpxe/features.h>
47 * Dynamic Host Configuration Protocol
52 static int dhcp_tx ( struct dhcp_session
*dhcp
);
55 * DHCP operation types
57 * This table maps from DHCP message types (i.e. values of the @c
58 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
61 static const uint8_t dhcp_op
[] = {
62 [DHCPDISCOVER
] = BOOTP_REQUEST
,
63 [DHCPOFFER
] = BOOTP_REPLY
,
64 [DHCPREQUEST
] = BOOTP_REQUEST
,
65 [DHCPDECLINE
] = BOOTP_REQUEST
,
66 [DHCPACK
] = BOOTP_REPLY
,
67 [DHCPNAK
] = BOOTP_REPLY
,
68 [DHCPRELEASE
] = BOOTP_REQUEST
,
69 [DHCPINFORM
] = BOOTP_REQUEST
,
72 /** Raw option data for options common to all DHCP requests */
73 static uint8_t dhcp_request_options_data
[] = {
74 DHCP_MESSAGE_TYPE
, DHCP_BYTE ( 0 ),
75 DHCP_MAX_MESSAGE_SIZE
,
76 DHCP_WORD ( ETH_MAX_MTU
- 20 /* IP header */ - 8 /* UDP header */ ),
77 DHCP_CLIENT_ARCHITECTURE
, DHCP_WORD ( 0 ),
78 DHCP_CLIENT_NDI
, DHCP_OPTION ( 1 /* UNDI */ , 2, 1 /* v2.1 */ ),
80 DHCP_STRING ( 'P', 'X', 'E', 'C', 'l', 'i', 'e', 'n', 't', ':',
81 'A', 'r', 'c', 'h', ':', '0', '0', '0', '0', '0', ':',
82 'U', 'N', 'D', 'I', ':', '0', '0', '2', '0', '0', '1' ),
84 DHCP_STRING ( 'g', 'P', 'X', 'E' ),
85 DHCP_PARAMETER_REQUEST_LIST
,
86 DHCP_OPTION ( DHCP_SUBNET_MASK
, DHCP_ROUTERS
, DHCP_DNS_SERVERS
,
87 DHCP_LOG_SERVERS
, DHCP_HOST_NAME
, DHCP_DOMAIN_NAME
,
88 DHCP_ROOT_PATH
, DHCP_VENDOR_ENCAP
, DHCP_VENDOR_CLASS_ID
,
89 DHCP_TFTP_SERVER_NAME
, DHCP_BOOTFILE_NAME
,
90 DHCP_EB_ENCAP
, DHCP_ISCSI_INITIATOR_IQN
),
94 /** Version number feature */
95 FEATURE_VERSION ( VERSION_MAJOR
, VERSION_MINOR
, VERSION_PATCH
);
97 /** DHCP server address setting */
98 struct setting dhcp_server_setting __setting
= {
99 .name
= "dhcp-server",
100 .description
= "DHCP server address",
101 .tag
= DHCP_SERVER_IDENTIFIER
,
102 .type
= &setting_type_ipv4
,
105 /** DHCP user class setting */
106 struct setting user_class_setting __setting
= {
107 .name
= "user-class",
108 .description
= "User class identifier",
109 .tag
= DHCP_USER_CLASS_ID
,
110 .type
= &setting_type_string
,
113 /** Use cached network settings */
114 struct setting use_cached_setting __setting
= {
115 .name
= "use-cached",
116 .description
= "Use cached network settings",
117 .tag
= DHCP_EB_USE_CACHED
,
118 .type
= &setting_type_uint8
,
122 * Name a DHCP packet type
124 * @v msgtype DHCP message type
125 * @ret string DHCP mesasge type name
127 static inline const char * dhcp_msgtype_name ( unsigned int msgtype
) {
129 case DHCPNONE
: return "BOOTP"; /* Non-DHCP packet */
130 case DHCPDISCOVER
: return "DHCPDISCOVER";
131 case DHCPOFFER
: return "DHCPOFFER";
132 case DHCPREQUEST
: return "DHCPREQUEST";
133 case DHCPDECLINE
: return "DHCPDECLINE";
134 case DHCPACK
: return "DHCPACK";
135 case DHCPNAK
: return "DHCPNAK";
136 case DHCPRELEASE
: return "DHCPRELEASE";
137 case DHCPINFORM
: return "DHCPINFORM";
138 default: return "DHCP<invalid>";
143 * Calculate DHCP transaction ID for a network device
145 * @v netdev Network device
148 * Extract the least significant bits of the hardware address for use
149 * as the transaction ID.
151 static uint32_t dhcp_xid ( struct net_device
*netdev
) {
154 memcpy ( &xid
, ( netdev
->ll_addr
+ netdev
->ll_protocol
->ll_addr_len
155 - sizeof ( xid
) ), sizeof ( xid
) );
159 /****************************************************************************
167 /** DHCP session state operations */
168 struct dhcp_session_state
{
172 * Construct transmitted packet
174 * @v dhcp DHCP session
175 * @v dhcppkt DHCP packet
176 * @v peer Destination address
178 int ( * tx
) ( struct dhcp_session
*dhcp
,
179 struct dhcp_packet
*dhcppkt
,
180 struct sockaddr_in
*peer
);
181 /** Handle received packet
183 * @v dhcp DHCP session
184 * @v dhcppkt DHCP packet
185 * @v peer DHCP server address
186 * @v msgtype DHCP message type
187 * @v server_id DHCP server ID
189 void ( * rx
) ( struct dhcp_session
*dhcp
,
190 struct dhcp_packet
*dhcppkt
,
191 struct sockaddr_in
*peer
,
192 uint8_t msgtype
, struct in_addr server_id
);
193 /** Handle timer expiry
195 * @v dhcp DHCP session
197 void ( * expired
) ( struct dhcp_session
*dhcp
);
198 /** Transmitted message type */
200 /** Apply minimum timeout */
201 uint8_t apply_min_timeout
;
204 static struct dhcp_session_state dhcp_state_discover
;
205 static struct dhcp_session_state dhcp_state_request
;
206 static struct dhcp_session_state dhcp_state_proxy
;
207 static struct dhcp_session_state dhcp_state_pxebs
;
209 /** DHCP offer is valid for IP lease */
210 #define DHCP_OFFER_IP 1
212 /** DHCP offer is valid for PXE options */
213 #define DHCP_OFFER_PXE 2
217 /** IP address of server granting offer */
218 struct in_addr server
;
220 /** IP address being offered, or 0.0.0.0 for a pure proxy */
223 /** DHCP packet containing PXE options; NULL if missing or proxied */
224 struct dhcp_packet
*pxe
;
226 /** Valid uses for this offer, a combination of DHCP_OFFER bits */
229 /** Priority of this offer */
232 /** Whether to ignore PXE DHCP extensions */
236 /** Maximum number of DHCP offers to queue */
237 #define DHCP_MAX_OFFERS 6
239 /** A DHCP session */
240 struct dhcp_session
{
241 /** Reference counter */
242 struct refcnt refcnt
;
243 /** Job control interface */
244 struct job_interface job
;
245 /** Data transfer interface */
246 struct xfer_interface xfer
;
248 /** Network device being configured */
249 struct net_device
*netdev
;
250 /** Local socket address */
251 struct sockaddr_in local
;
252 /** State of the session */
253 struct dhcp_session_state
*state
;
255 /** PXE Boot Server type */
257 /** List of PXE Boot Servers to attempt */
258 struct in_addr
*pxe_attempt
;
259 /** List of PXE Boot Servers to accept */
260 struct in_addr
*pxe_accept
;
262 /** Retransmission timer */
263 struct retry_timer timer
;
264 /** Start time of the current state (in ticks) */
267 /** DHCP offer just requested */
268 struct dhcp_offer
*current_offer
;
269 /** List of DHCP offers received */
270 struct dhcp_offer offers
[DHCP_MAX_OFFERS
];
276 * @v refcnt Reference counter
278 static void dhcp_free ( struct refcnt
*refcnt
) {
279 struct dhcp_session
*dhcp
=
280 container_of ( refcnt
, struct dhcp_session
, refcnt
);
283 for ( i
= 0 ; i
< DHCP_MAX_OFFERS
; i
++ ) {
284 if ( dhcp
->offers
[i
].pxe
)
285 dhcppkt_put ( dhcp
->offers
[i
].pxe
);
288 netdev_put ( dhcp
->netdev
);
293 * Mark DHCP session as complete
295 * @v dhcp DHCP session
296 * @v rc Return status code
298 static void dhcp_finished ( struct dhcp_session
*dhcp
, int rc
) {
300 /* Block futher incoming messages */
301 job_nullify ( &dhcp
->job
);
302 xfer_nullify ( &dhcp
->xfer
);
304 /* Stop retry timer */
305 stop_timer ( &dhcp
->timer
);
307 /* Free resources and close interfaces */
308 xfer_close ( &dhcp
->xfer
, rc
);
309 job_done ( &dhcp
->job
, rc
);
313 * Transition to new DHCP session state
315 * @v dhcp DHCP session
316 * @v state New session state
318 static void dhcp_set_state ( struct dhcp_session
*dhcp
,
319 struct dhcp_session_state
*state
) {
321 DBGC ( dhcp
, "DHCP %p entering %s state\n", dhcp
, state
->name
);
323 dhcp
->start
= currticks();
324 stop_timer ( &dhcp
->timer
);
325 dhcp
->timer
.min_timeout
=
326 ( state
->apply_min_timeout
? DHCP_MIN_TIMEOUT
: 0 );
327 dhcp
->timer
.max_timeout
= DHCP_MAX_TIMEOUT
;
328 start_timer_nodelay ( &dhcp
->timer
);
332 * Determine next DHCP offer to try
334 * @v dhcp DHCP session
335 * @v type DHCP offer type
336 * @ret offer Next DHCP offer to try
338 * Offers are ranked by priority, then by completeness (combined
339 * IP+PXE are tried before @a type alone), then by order of receipt.
341 static struct dhcp_offer
* dhcp_next_offer ( struct dhcp_session
*dhcp
,
344 struct dhcp_offer
*offer
;
345 struct dhcp_offer
*best
= NULL
;
347 for ( offer
= dhcp
->offers
; offer
< dhcp
->offers
+ DHCP_MAX_OFFERS
;
349 if ( ( offer
->valid
& type
) &&
350 ( ( best
== NULL
) ||
351 ( offer
->priority
> best
->priority
) ||
352 ( ( offer
->priority
== best
->priority
) &&
353 ( offer
->valid
& ~best
->valid
) ) ) )
360 /****************************************************************************
367 * Construct transmitted packet for DHCP discovery
369 * @v dhcp DHCP session
370 * @v dhcppkt DHCP packet
371 * @v peer Destination address
373 static int dhcp_discovery_tx ( struct dhcp_session
*dhcp
,
374 struct dhcp_packet
*dhcppkt __unused
,
375 struct sockaddr_in
*peer
) {
377 DBGC ( dhcp
, "DHCP %p DHCPDISCOVER\n", dhcp
);
379 /* Set server address */
380 peer
->sin_addr
.s_addr
= INADDR_BROADCAST
;
381 peer
->sin_port
= htons ( BOOTPS_PORT
);
387 * Handle received DHCPOFFER during any state
389 * @v dhcp DHCP session
390 * @v dhcppkt DHCP packet
391 * @v peer DHCP server address
392 * @v msgtype DHCP message type
393 * @v server_id DHCP server ID
395 static void dhcp_rx_offer ( struct dhcp_session
*dhcp
,
396 struct dhcp_packet
*dhcppkt
,
397 struct sockaddr_in
*peer
, uint8_t msgtype
,
398 struct in_addr server_id
) {
399 char vci
[9]; /* "PXEClient" */
404 struct dhcp_offer
*offer
;
407 DBGC ( dhcp
, "DHCP %p %s from %s:%d", dhcp
,
408 dhcp_msgtype_name ( msgtype
), inet_ntoa ( peer
->sin_addr
),
409 ntohs ( peer
->sin_port
) );
410 if ( server_id
.s_addr
!= peer
->sin_addr
.s_addr
)
411 DBGC ( dhcp
, " (%s)", inet_ntoa ( server_id
) );
413 /* Identify offered IP address */
414 if ( dhcppkt
->dhcphdr
->yiaddr
.s_addr
)
415 DBGC ( dhcp
, " for %s", inet_ntoa ( dhcppkt
->dhcphdr
->yiaddr
));
417 /* Enqueue an offer to be filled in */
418 for ( i
= 0 ; i
< DHCP_MAX_OFFERS
; i
++ ) {
419 if ( dhcp
->offers
[i
].server
.s_addr
== server_id
.s_addr
) {
420 DBGC ( dhcp
, " dup\n" );
424 if ( ! dhcp
->offers
[i
].valid
)
427 if ( i
== DHCP_MAX_OFFERS
) {
428 DBGC ( dhcp
, " dropped\n" );
432 offer
= &dhcp
->offers
[i
];
433 offer
->server
= server_id
;
434 offer
->ip
= dhcppkt
->dhcphdr
->yiaddr
;
436 /* Identify "PXEClient" vendor class */
437 vci_len
= dhcppkt_fetch ( dhcppkt
, DHCP_VENDOR_CLASS_ID
,
438 vci
, sizeof ( vci
) );
439 has_pxeclient
= ( ( vci_len
>= ( int ) sizeof ( vci
) ) &&
440 ( strncmp ( "PXEClient", vci
, sizeof (vci
) ) == 0 ));
442 /* Identify presence of PXE-specific options */
443 pxeopts_len
= dhcppkt_fetch ( dhcppkt
, DHCP_PXE_BOOT_MENU
, NULL
, 0 );
444 has_pxeopts
= ( pxeopts_len
>= 0 );
446 DBGC ( dhcp
, "%s", ( has_pxeopts
? " pxe" : " proxy" ) );
448 if ( has_pxeclient
&& has_pxeopts
) {
449 /* Save reference to packet for future use */
451 dhcppkt_put ( offer
->pxe
);
452 offer
->pxe
= dhcppkt_get ( dhcppkt
);
455 /* Identify priority */
456 dhcppkt_fetch ( dhcppkt
, DHCP_EB_PRIORITY
, &offer
->priority
,
457 sizeof ( offer
->priority
) );
458 if ( offer
->priority
)
459 DBGC ( dhcp
, " pri %d", offer
->priority
);
461 /* Identify ignore-PXE flag */
462 dhcppkt_fetch ( dhcppkt
, DHCP_EB_NO_PXEDHCP
, &offer
->no_pxedhcp
,
463 sizeof ( offer
->no_pxedhcp
) );
464 if ( offer
->no_pxedhcp
)
465 DBGC ( dhcp
, " nopxe" );
468 /* Determine roles this offer can fill */
469 if ( offer
->ip
.s_addr
&&
470 ( peer
->sin_port
== htons ( BOOTPS_PORT
) ) &&
471 ( ( msgtype
== DHCPOFFER
) || ( ! msgtype
/* BOOTP */ ) ) )
472 offer
->valid
|= DHCP_OFFER_IP
;
474 if ( has_pxeclient
&& ( msgtype
== DHCPOFFER
) )
475 offer
->valid
|= DHCP_OFFER_PXE
;
479 * Handle received packet during DHCP discovery
481 * @v dhcp DHCP session
482 * @v dhcppkt DHCP packet
483 * @v peer DHCP server address
484 * @v msgtype DHCP message type
485 * @v server_id DHCP server ID
487 static void dhcp_discovery_rx ( struct dhcp_session
*dhcp
,
488 struct dhcp_packet
*dhcppkt
,
489 struct sockaddr_in
*peer
, uint8_t msgtype
,
490 struct in_addr server_id
) {
491 unsigned long elapsed
;
492 struct dhcp_offer
*ip_offer
;
494 dhcp_rx_offer ( dhcp
, dhcppkt
, peer
, msgtype
, server_id
);
496 /* We can exit the discovery state when we have a valid
497 * DHCPOFFER, and either:
499 * o The DHCPOFFER instructs us to ignore ProxyDHCPOFFERs, or
500 * o We have a valid ProxyDHCPOFFER, or
501 * o We have allowed sufficient time for ProxyDHCPOFFERs.
504 /* If we don't yet have a DHCPOFFER, do nothing */
505 ip_offer
= dhcp_next_offer ( dhcp
, DHCP_OFFER_IP
);
509 /* If we can't yet transition to DHCPREQUEST, do nothing */
510 elapsed
= ( currticks() - dhcp
->start
);
511 if ( ! ( ip_offer
->no_pxedhcp
||
512 dhcp_next_offer ( dhcp
, DHCP_OFFER_PXE
) ||
513 ( elapsed
> PROXYDHCP_MAX_TIMEOUT
) ) )
516 /* Transition to DHCPREQUEST */
517 dhcp_set_state ( dhcp
, &dhcp_state_request
);
521 * Handle timer expiry during DHCP discovery
523 * @v dhcp DHCP session
525 static void dhcp_discovery_expired ( struct dhcp_session
*dhcp
) {
526 unsigned long elapsed
= ( currticks() - dhcp
->start
);
528 /* Give up waiting for ProxyDHCP before we reach the failure point */
529 if ( dhcp_next_offer ( dhcp
, DHCP_OFFER_IP
) &&
530 ( elapsed
> PROXYDHCP_MAX_TIMEOUT
) ) {
531 dhcp_set_state ( dhcp
, &dhcp_state_request
);
535 /* Otherwise, retransmit current packet */
539 /** DHCP discovery state operations */
540 static struct dhcp_session_state dhcp_state_discover
= {
542 .tx
= dhcp_discovery_tx
,
543 .rx
= dhcp_discovery_rx
,
544 .expired
= dhcp_discovery_expired
,
545 .tx_msgtype
= DHCPDISCOVER
,
546 .apply_min_timeout
= 1,
550 * Construct transmitted packet for DHCP request
552 * @v dhcp DHCP session
553 * @v dhcppkt DHCP packet
554 * @v peer Destination address
556 static int dhcp_request_tx ( struct dhcp_session
*dhcp
,
557 struct dhcp_packet
*dhcppkt
,
558 struct sockaddr_in
*peer
) {
560 struct dhcp_offer
*offer
;
562 offer
= dhcp
->current_offer
= dhcp_next_offer ( dhcp
, DHCP_OFFER_IP
);
564 DBGC ( dhcp
, "DHCP %p DHCPREQUEST to %s:%d",
565 dhcp
, inet_ntoa ( offer
->server
), BOOTPS_PORT
);
566 DBGC ( dhcp
, " for %s\n", inet_ntoa ( offer
->ip
) );
569 if ( ( rc
= dhcppkt_store ( dhcppkt
, DHCP_SERVER_IDENTIFIER
,
571 sizeof ( offer
->server
) ) ) != 0 )
574 /* Set requested IP address */
575 if ( ( rc
= dhcppkt_store ( dhcppkt
, DHCP_REQUESTED_ADDRESS
,
576 &offer
->ip
, sizeof ( offer
->ip
) ) ) != 0 )
579 /* Set server address */
580 peer
->sin_addr
.s_addr
= INADDR_BROADCAST
;
581 peer
->sin_port
= htons ( BOOTPS_PORT
);
587 * Handle received packet during DHCP request
589 * @v dhcp DHCP session
590 * @v dhcppkt DHCP packet
591 * @v peer DHCP server address
592 * @v msgtype DHCP message type
593 * @v server_id DHCP server ID
595 static void dhcp_request_rx ( struct dhcp_session
*dhcp
,
596 struct dhcp_packet
*dhcppkt
,
597 struct sockaddr_in
*peer
, uint8_t msgtype
,
598 struct in_addr server_id
) {
600 struct settings
*parent
;
602 struct dhcp_offer
*pxe_offer
;
604 if ( msgtype
== DHCPOFFER
) {
605 dhcp_rx_offer ( dhcp
, dhcppkt
, peer
, msgtype
, server_id
);
606 if ( dhcp_next_offer ( dhcp
, DHCP_OFFER_IP
) !=
607 dhcp
->current_offer
) {
608 /* Restart due to higher-priority offer received */
609 DBGC ( dhcp
, "DHCP %p re-requesting\n", dhcp
);
610 dhcp_set_state ( dhcp
, &dhcp_state_request
);
615 DBGC ( dhcp
, "DHCP %p %s from %s:%d", dhcp
,
616 dhcp_msgtype_name ( msgtype
), inet_ntoa ( peer
->sin_addr
),
617 ntohs ( peer
->sin_port
) );
618 if ( server_id
.s_addr
!= peer
->sin_addr
.s_addr
)
619 DBGC ( dhcp
, " (%s)", inet_ntoa ( server_id
) );
621 /* Identify leased IP address */
622 ip
= dhcppkt
->dhcphdr
->yiaddr
;
624 DBGC ( dhcp
, " for %s", inet_ntoa ( ip
) );
627 /* Filter out unacceptable responses */
628 if ( peer
->sin_port
!= htons ( BOOTPS_PORT
) )
630 if ( msgtype
/* BOOTP */ && ( msgtype
!= DHCPACK
) )
632 if ( server_id
.s_addr
!= dhcp
->current_offer
->server
.s_addr
)
635 /* Record assigned address */
636 dhcp
->local
.sin_addr
= ip
;
638 /* Register settings */
639 parent
= netdev_settings ( dhcp
->netdev
);
640 if ( ( rc
= register_settings ( &dhcppkt
->settings
, parent
) ) != 0 ){
641 DBGC ( dhcp
, "DHCP %p could not register settings: %s\n",
642 dhcp
, strerror ( rc
) );
643 dhcp_finished ( dhcp
, rc
);
647 /* Locate best source of PXE settings */
648 pxe_offer
= dhcp_next_offer ( dhcp
, DHCP_OFFER_PXE
);
650 if ( ( ! pxe_offer
) || /* No PXE available */
651 /* IP offer instructs us to ignore PXE */
652 dhcp
->current_offer
->no_pxedhcp
||
653 /* PXE settings already registered with IP offer */
654 ( ( dhcp
->current_offer
== pxe_offer
) && ( pxe_offer
->pxe
) ) ) {
657 dhcp_finished ( dhcp
, 0 );
659 } else if ( pxe_offer
->pxe
) {
660 /* Register PXE settings and terminate DHCP */
661 pxe_offer
->pxe
->settings
.name
= PROXYDHCP_SETTINGS_NAME
;
662 if ( ( rc
= register_settings ( &pxe_offer
->pxe
->settings
,
664 DBGC ( dhcp
, "DHCP %p could not register settings: "
665 "%s\n", dhcp
, strerror ( rc
) );
667 dhcp_finished ( dhcp
, rc
);
669 /* Start ProxyDHCP */
670 dhcp_set_state ( dhcp
, &dhcp_state_proxy
);
675 * Handle timer expiry during DHCP discovery
677 * @v dhcp DHCP session
679 static void dhcp_request_expired ( struct dhcp_session
*dhcp
) {
681 /* Retransmit current packet */
685 /** DHCP request state operations */
686 static struct dhcp_session_state dhcp_state_request
= {
688 .tx
= dhcp_request_tx
,
689 .rx
= dhcp_request_rx
,
690 .expired
= dhcp_request_expired
,
691 .tx_msgtype
= DHCPREQUEST
,
692 .apply_min_timeout
= 0,
696 * Construct transmitted packet for ProxyDHCP request
698 * @v dhcp DHCP session
699 * @v dhcppkt DHCP packet
700 * @v peer Destination address
702 static int dhcp_proxy_tx ( struct dhcp_session
*dhcp
,
703 struct dhcp_packet
*dhcppkt
,
704 struct sockaddr_in
*peer
) {
706 struct dhcp_offer
*offer
;
708 offer
= dhcp
->current_offer
= dhcp_next_offer ( dhcp
, DHCP_OFFER_PXE
);
710 DBGC ( dhcp
, "DHCP %p ProxyDHCP REQUEST to %s:%d\n", dhcp
,
711 inet_ntoa ( offer
->server
), PXE_PORT
);
714 if ( ( rc
= dhcppkt_store ( dhcppkt
, DHCP_SERVER_IDENTIFIER
,
716 sizeof ( offer
->server
) ) ) != 0 )
719 /* Set server address */
720 peer
->sin_addr
= offer
->server
;
721 peer
->sin_port
= htons ( PXE_PORT
);
727 * Handle received packet during ProxyDHCP request
729 * @v dhcp DHCP session
730 * @v dhcppkt DHCP packet
731 * @v peer DHCP server address
732 * @v msgtype DHCP message type
733 * @v server_id DHCP server ID
735 static void dhcp_proxy_rx ( struct dhcp_session
*dhcp
,
736 struct dhcp_packet
*dhcppkt
,
737 struct sockaddr_in
*peer
, uint8_t msgtype
,
738 struct in_addr server_id
) {
741 /* Enqueue last-minute DHCPOFFERs for use in case of failure */
742 if ( peer
->sin_port
== htons ( BOOTPS_PORT
) &&
743 msgtype
== DHCPOFFER
) {
744 dhcp_rx_offer ( dhcp
, dhcppkt
, peer
, msgtype
, server_id
);
748 DBGC ( dhcp
, "DHCP %p %s from %s:%d", dhcp
,
749 dhcp_msgtype_name ( msgtype
), inet_ntoa ( peer
->sin_addr
),
750 ntohs ( peer
->sin_port
) );
751 if ( server_id
.s_addr
!= peer
->sin_addr
.s_addr
)
752 DBGC ( dhcp
, " (%s)", inet_ntoa ( server_id
) );
755 /* Filter out unacceptable responses */
756 if ( peer
->sin_port
!= htons ( PXE_PORT
) )
758 if ( msgtype
!= DHCPACK
&& msgtype
!= DHCPOFFER
)
760 if ( server_id
.s_addr
/* Linux PXE server omits server ID */ &&
761 ( server_id
.s_addr
!= dhcp
->current_offer
->server
.s_addr
) )
764 /* Register settings */
765 dhcppkt
->settings
.name
= PROXYDHCP_SETTINGS_NAME
;
766 if ( ( rc
= register_settings ( &dhcppkt
->settings
, NULL
) ) != 0 ) {
767 DBGC ( dhcp
, "DHCP %p could not register settings: %s\n",
768 dhcp
, strerror ( rc
) );
769 dhcp_finished ( dhcp
, rc
);
774 dhcp_finished ( dhcp
, 0 );
778 * Handle timer expiry during ProxyDHCP request
780 * @v dhcp DHCP session
782 static void dhcp_proxy_expired ( struct dhcp_session
*dhcp
) {
783 unsigned long elapsed
= ( currticks() - dhcp
->start
);
785 /* Give up waiting for ProxyDHCP before we reach the failure point */
786 if ( elapsed
> PROXYDHCP_MAX_TIMEOUT
) {
788 /* Mark failed offer as unsuitable for ProxyDHCP */
789 dhcp
->current_offer
->valid
&= ~DHCP_OFFER_PXE
;
791 /* Prefer not to use only half of a PXE+IP offer if we
792 * have other offers available
794 dhcp
->current_offer
->priority
= -1;
796 /* If we have any other PXE offers we can try, go back
797 * to DHCPREQUEST (since they might not be proxied
798 * offers, or might be coupled to a new IP address).
799 * We should probably DHCPRELEASE our old IP, but the
800 * standard does not require it.
802 if ( dhcp_next_offer ( dhcp
, DHCP_OFFER_PXE
) ) {
803 dhcp
->local
.sin_addr
.s_addr
= 0;
804 dhcp_set_state ( dhcp
, &dhcp_state_request
);
808 /* No possibilities left; finish without PXE options */
809 dhcp_finished ( dhcp
, 0 );
813 /* Retransmit current packet */
817 /** ProxyDHCP request state operations */
818 static struct dhcp_session_state dhcp_state_proxy
= {
822 .expired
= dhcp_proxy_expired
,
823 .tx_msgtype
= DHCPREQUEST
,
824 .apply_min_timeout
= 0,
828 * Construct transmitted packet for PXE Boot Server Discovery
830 * @v dhcp DHCP session
831 * @v dhcppkt DHCP packet
832 * @v peer Destination address
834 static int dhcp_pxebs_tx ( struct dhcp_session
*dhcp
,
835 struct dhcp_packet
*dhcppkt
,
836 struct sockaddr_in
*peer
) {
837 struct dhcp_pxe_boot_menu_item menu_item
= { 0, 0 };
840 /* Set server address */
841 peer
->sin_addr
= *(dhcp
->pxe_attempt
);
842 peer
->sin_port
= ( ( peer
->sin_addr
.s_addr
== INADDR_BROADCAST
) ?
843 htons ( BOOTPS_PORT
) : htons ( PXE_PORT
) );
845 DBGC ( dhcp
, "DHCP %p PXEBS REQUEST to %s:%d for type %d\n",
846 dhcp
, inet_ntoa ( peer
->sin_addr
), ntohs ( peer
->sin_port
),
847 le16_to_cpu ( dhcp
->pxe_type
) );
849 /* Set boot menu item */
850 menu_item
.type
= dhcp
->pxe_type
;
851 if ( ( rc
= dhcppkt_store ( dhcppkt
, DHCP_PXE_BOOT_MENU_ITEM
,
852 &menu_item
, sizeof ( menu_item
) ) ) != 0 )
859 * Check to see if PXE Boot Server address is acceptable
861 * @v dhcp DHCP session
862 * @v bs Boot Server address
863 * @ret accept Boot Server is acceptable
865 static int dhcp_pxebs_accept ( struct dhcp_session
*dhcp
,
866 struct in_addr bs
) {
867 struct in_addr
*accept
;
869 /* Accept if we have no acceptance filter */
870 if ( ! dhcp
->pxe_accept
)
873 /* Scan through acceptance list */
874 for ( accept
= dhcp
->pxe_accept
; accept
->s_addr
; accept
++ ) {
875 if ( accept
->s_addr
== bs
.s_addr
)
879 DBGC ( dhcp
, "DHCP %p rejecting server %s\n",
880 dhcp
, inet_ntoa ( bs
) );
885 * Handle received packet during PXE Boot Server Discovery
887 * @v dhcp DHCP session
888 * @v dhcppkt DHCP packet
889 * @v peer DHCP server address
890 * @v msgtype DHCP message type
891 * @v server_id DHCP server ID
893 static void dhcp_pxebs_rx ( struct dhcp_session
*dhcp
,
894 struct dhcp_packet
*dhcppkt
,
895 struct sockaddr_in
*peer
, uint8_t msgtype
,
896 struct in_addr server_id
) {
897 struct dhcp_pxe_boot_menu_item menu_item
= { 0, 0 };
900 DBGC ( dhcp
, "DHCP %p %s from %s:%d", dhcp
,
901 dhcp_msgtype_name ( msgtype
), inet_ntoa ( peer
->sin_addr
),
902 ntohs ( peer
->sin_port
) );
903 if ( server_id
.s_addr
!= peer
->sin_addr
.s_addr
)
904 DBGC ( dhcp
, " (%s)", inet_ntoa ( server_id
) );
906 /* Identify boot menu item */
907 dhcppkt_fetch ( dhcppkt
, DHCP_PXE_BOOT_MENU_ITEM
,
908 &menu_item
, sizeof ( menu_item
) );
909 if ( menu_item
.type
)
910 DBGC ( dhcp
, " for type %d", ntohs ( menu_item
.type
) );
913 /* Filter out unacceptable responses */
914 if ( ( peer
->sin_port
!= htons ( BOOTPS_PORT
) ) &&
915 ( peer
->sin_port
!= htons ( PXE_PORT
) ) )
917 if ( msgtype
!= DHCPACK
)
919 if ( menu_item
.type
!= dhcp
->pxe_type
)
921 if ( ! dhcp_pxebs_accept ( dhcp
, ( server_id
.s_addr
?
922 server_id
: peer
->sin_addr
) ) )
925 /* Register settings */
926 dhcppkt
->settings
.name
= PXEBS_SETTINGS_NAME
;
927 if ( ( rc
= register_settings ( &dhcppkt
->settings
, NULL
) ) != 0 ) {
928 DBGC ( dhcp
, "DHCP %p could not register settings: %s\n",
929 dhcp
, strerror ( rc
) );
930 dhcp_finished ( dhcp
, rc
);
935 dhcp_finished ( dhcp
, 0 );
939 * Handle timer expiry during PXE Boot Server Discovery
941 * @v dhcp DHCP session
943 static void dhcp_pxebs_expired ( struct dhcp_session
*dhcp
) {
944 unsigned long elapsed
= ( currticks() - dhcp
->start
);
946 /* Give up waiting before we reach the failure point, and fail
947 * over to the next server in the attempt list
949 if ( elapsed
> PXEBS_MAX_TIMEOUT
) {
951 if ( dhcp
->pxe_attempt
->s_addr
) {
952 dhcp_set_state ( dhcp
, &dhcp_state_pxebs
);
955 dhcp_finished ( dhcp
, -ETIMEDOUT
);
960 /* Retransmit current packet */
964 /** PXE Boot Server Discovery state operations */
965 static struct dhcp_session_state dhcp_state_pxebs
= {
969 .expired
= dhcp_pxebs_expired
,
970 .tx_msgtype
= DHCPREQUEST
,
971 .apply_min_timeout
= 1,
974 /****************************************************************************
976 * Packet construction
981 * Construct DHCP client hardware address field and broadcast flag
983 * @v netdev Network device
984 * @v hlen DHCP hardware address length to fill in
985 * @v flags DHCP flags to fill in
986 * @ret chaddr DHCP client hardware address
988 void * dhcp_chaddr ( struct net_device
*netdev
, uint8_t *hlen
,
990 struct ll_protocol
*ll_protocol
= netdev
->ll_protocol
;
991 typeof ( ( ( struct dhcphdr
* ) NULL
)->chaddr
) chaddr
;
993 /* If the link-layer address cannot fit into the chaddr field
994 * (as is the case for IPoIB) then try using the hardware
995 * address instead. If we do this, set the broadcast flag,
996 * since chaddr then does not represent a valid link-layer
997 * address for the return path.
999 * If even the hardware address is too large, use an empty
1000 * chaddr field and set the broadcast flag.
1002 * This goes against RFC4390, but RFC4390 mandates that we use
1003 * a DHCP client identifier that conforms with RFC4361, which
1004 * we cannot do without either persistent (NIC-independent)
1005 * storage, or by eliminating the hardware address completely
1006 * from the DHCP packet, which seems unfriendly to users.
1008 if ( ( *hlen
= ll_protocol
->ll_addr_len
) <= sizeof ( chaddr
) ) {
1009 return netdev
->ll_addr
;
1011 *flags
= htons ( BOOTP_FL_BROADCAST
);
1012 if ( ( *hlen
= ll_protocol
->hw_addr_len
) <= sizeof ( chaddr
) ) {
1013 return netdev
->hw_addr
;
1021 * Create a DHCP packet
1023 * @v dhcppkt DHCP packet structure to fill in
1024 * @v netdev Network device
1025 * @v msgtype DHCP message type
1026 * @v options Initial options to include (or NULL)
1027 * @v options_len Length of initial options
1028 * @v data Buffer for DHCP packet
1029 * @v max_len Size of DHCP packet buffer
1030 * @ret rc Return status code
1032 * Creates a DHCP packet in the specified buffer, and initialise a
1033 * DHCP packet structure.
1035 int dhcp_create_packet ( struct dhcp_packet
*dhcppkt
,
1036 struct net_device
*netdev
, uint8_t msgtype
,
1037 const void *options
, size_t options_len
,
1038 void *data
, size_t max_len
) {
1039 struct dhcphdr
*dhcphdr
= data
;
1044 if ( max_len
< ( sizeof ( *dhcphdr
) + options_len
) )
1047 /* Initialise DHCP packet content */
1048 memset ( dhcphdr
, 0, max_len
);
1049 dhcphdr
->xid
= dhcp_xid ( netdev
);
1050 dhcphdr
->magic
= htonl ( DHCP_MAGIC_COOKIE
);
1051 dhcphdr
->htype
= ntohs ( netdev
->ll_protocol
->ll_proto
);
1052 dhcphdr
->op
= dhcp_op
[msgtype
];
1053 chaddr
= dhcp_chaddr ( netdev
, &dhcphdr
->hlen
, &dhcphdr
->flags
);
1054 memcpy ( dhcphdr
->chaddr
, chaddr
, dhcphdr
->hlen
);
1055 memcpy ( dhcphdr
->options
, options
, options_len
);
1057 /* Initialise DHCP packet structure */
1058 memset ( dhcppkt
, 0, sizeof ( *dhcppkt
) );
1059 dhcppkt_init ( dhcppkt
, data
, max_len
);
1061 /* Set DHCP_MESSAGE_TYPE option */
1062 if ( ( rc
= dhcppkt_store ( dhcppkt
, DHCP_MESSAGE_TYPE
,
1063 &msgtype
, sizeof ( msgtype
) ) ) != 0 )
1070 * Create DHCP request packet
1072 * @v dhcppkt DHCP packet structure to fill in
1073 * @v netdev Network device
1074 * @v msgtype DHCP message type
1075 * @v ciaddr Client IP address
1076 * @v data Buffer for DHCP packet
1077 * @v max_len Size of DHCP packet buffer
1078 * @ret rc Return status code
1080 * Creates a DHCP request packet in the specified buffer, and
1081 * initialise a DHCP packet structure.
1083 int dhcp_create_request ( struct dhcp_packet
*dhcppkt
,
1084 struct net_device
*netdev
, unsigned int msgtype
,
1085 struct in_addr ciaddr
, void *data
, size_t max_len
) {
1086 struct dhcp_netdev_desc dhcp_desc
;
1087 struct dhcp_client_id client_id
;
1088 struct dhcp_client_uuid client_uuid
;
1089 uint8_t *dhcp_features
;
1090 size_t dhcp_features_len
;
1095 /* Create DHCP packet */
1096 if ( ( rc
= dhcp_create_packet ( dhcppkt
, netdev
, msgtype
,
1097 dhcp_request_options_data
,
1098 sizeof ( dhcp_request_options_data
),
1099 data
, max_len
) ) != 0 ) {
1100 DBG ( "DHCP could not create DHCP packet: %s\n",
1105 /* Set client IP address */
1106 dhcppkt
->dhcphdr
->ciaddr
= ciaddr
;
1108 /* Add options to identify the feature list */
1109 dhcp_features
= table_start ( DHCP_FEATURES
);
1110 dhcp_features_len
= table_num_entries ( DHCP_FEATURES
);
1111 if ( ( rc
= dhcppkt_store ( dhcppkt
, DHCP_EB_ENCAP
, dhcp_features
,
1112 dhcp_features_len
) ) != 0 ) {
1113 DBG ( "DHCP could not set features list option: %s\n",
1118 /* Add options to identify the network device */
1119 fetch_setting ( &netdev
->settings
.settings
, &busid_setting
, &dhcp_desc
,
1120 sizeof ( dhcp_desc
) );
1121 if ( ( rc
= dhcppkt_store ( dhcppkt
, DHCP_EB_BUS_ID
, &dhcp_desc
,
1122 sizeof ( dhcp_desc
) ) ) != 0 ) {
1123 DBG ( "DHCP could not set bus ID option: %s\n",
1128 /* Add DHCP client identifier. Required for Infiniband, and
1129 * doesn't hurt other link layers.
1131 client_id
.ll_proto
= ntohs ( netdev
->ll_protocol
->ll_proto
);
1132 ll_addr_len
= netdev
->ll_protocol
->ll_addr_len
;
1133 assert ( ll_addr_len
<= sizeof ( client_id
.ll_addr
) );
1134 memcpy ( client_id
.ll_addr
, netdev
->ll_addr
, ll_addr_len
);
1135 if ( ( rc
= dhcppkt_store ( dhcppkt
, DHCP_CLIENT_ID
, &client_id
,
1136 ( ll_addr_len
+ 1 ) ) ) != 0 ) {
1137 DBG ( "DHCP could not set client ID: %s\n",
1142 /* Add client UUID, if we have one. Required for PXE. */
1143 client_uuid
.type
= DHCP_CLIENT_UUID_TYPE
;
1144 if ( ( len
= fetch_uuid_setting ( NULL
, &uuid_setting
,
1145 &client_uuid
.uuid
) ) >= 0 ) {
1146 if ( ( rc
= dhcppkt_store ( dhcppkt
, DHCP_CLIENT_UUID
,
1148 sizeof ( client_uuid
) ) ) != 0 ) {
1149 DBG ( "DHCP could not set client UUID: %s\n",
1155 /* Add user class, if we have one. */
1156 if ( ( len
= fetch_setting_len ( NULL
, &user_class_setting
) ) >= 0 ) {
1157 char user_class
[len
];
1158 fetch_setting ( NULL
, &user_class_setting
, user_class
,
1159 sizeof ( user_class
) );
1160 if ( ( rc
= dhcppkt_store ( dhcppkt
, DHCP_USER_CLASS_ID
,
1162 sizeof ( user_class
) ) ) != 0 ) {
1163 DBG ( "DHCP could not set user class: %s\n",
1172 /****************************************************************************
1174 * Data transfer interface
1179 * Transmit DHCP request
1181 * @v dhcp DHCP session
1182 * @ret rc Return status code
1184 static int dhcp_tx ( struct dhcp_session
*dhcp
) {
1185 static struct sockaddr_in peer
= {
1186 .sin_family
= AF_INET
,
1188 struct xfer_metadata meta
= {
1189 .netdev
= dhcp
->netdev
,
1190 .src
= ( struct sockaddr
* ) &dhcp
->local
,
1191 .dest
= ( struct sockaddr
* ) &peer
,
1193 struct io_buffer
*iobuf
;
1194 uint8_t msgtype
= dhcp
->state
->tx_msgtype
;
1195 struct dhcp_packet dhcppkt
;
1198 /* Start retry timer. Do this first so that failures to
1199 * transmit will be retried.
1201 start_timer ( &dhcp
->timer
);
1203 /* Allocate buffer for packet */
1204 iobuf
= xfer_alloc_iob ( &dhcp
->xfer
, DHCP_MIN_LEN
);
1208 /* Create basic DHCP packet in temporary buffer */
1209 if ( ( rc
= dhcp_create_request ( &dhcppkt
, dhcp
->netdev
, msgtype
,
1210 dhcp
->local
.sin_addr
, iobuf
->data
,
1211 iob_tailroom ( iobuf
) ) ) != 0 ) {
1212 DBGC ( dhcp
, "DHCP %p could not construct DHCP request: %s\n",
1213 dhcp
, strerror ( rc
) );
1217 /* Fill in packet based on current state */
1218 if ( ( rc
= dhcp
->state
->tx ( dhcp
, &dhcppkt
, &peer
) ) != 0 ) {
1219 DBGC ( dhcp
, "DHCP %p could not fill DHCP request: %s\n",
1220 dhcp
, strerror ( rc
) );
1224 /* Transmit the packet */
1225 iob_put ( iobuf
, dhcppkt
.len
);
1226 if ( ( rc
= xfer_deliver_iob_meta ( &dhcp
->xfer
, iob_disown ( iobuf
),
1228 DBGC ( dhcp
, "DHCP %p could not transmit UDP packet: %s\n",
1229 dhcp
, strerror ( rc
) );
1241 * @v xfer Data transfer interface
1242 * @v iobuf I/O buffer
1243 * @v meta Transfer metadata
1244 * @ret rc Return status code
1246 static int dhcp_deliver_iob ( struct xfer_interface
*xfer
,
1247 struct io_buffer
*iobuf
,
1248 struct xfer_metadata
*meta
) {
1249 struct dhcp_session
*dhcp
=
1250 container_of ( xfer
, struct dhcp_session
, xfer
);
1251 struct sockaddr_in
*peer
;
1253 struct dhcp_packet
*dhcppkt
;
1254 struct dhcphdr
*dhcphdr
;
1255 uint8_t msgtype
= 0;
1256 struct in_addr server_id
= { 0 };
1260 if ( ! meta
->src
) {
1261 DBGC ( dhcp
, "DHCP %p received packet without source port\n",
1266 peer
= ( struct sockaddr_in
* ) meta
->src
;
1268 /* Create a DHCP packet containing the I/O buffer contents.
1269 * Whilst we could just use the original buffer in situ, that
1270 * would waste the unused space in the packet buffer, and also
1271 * waste a relatively scarce fully-aligned I/O buffer.
1273 data_len
= iob_len ( iobuf
);
1274 dhcppkt
= zalloc ( sizeof ( *dhcppkt
) + data_len
);
1277 goto err_alloc_dhcppkt
;
1279 dhcphdr
= ( ( ( void * ) dhcppkt
) + sizeof ( *dhcppkt
) );
1280 memcpy ( dhcphdr
, iobuf
->data
, data_len
);
1281 dhcppkt_init ( dhcppkt
, dhcphdr
, data_len
);
1283 /* Identify message type */
1284 dhcppkt_fetch ( dhcppkt
, DHCP_MESSAGE_TYPE
, &msgtype
,
1285 sizeof ( msgtype
) );
1287 /* Identify server ID */
1288 dhcppkt_fetch ( dhcppkt
, DHCP_SERVER_IDENTIFIER
,
1289 &server_id
, sizeof ( server_id
) );
1291 /* Check for matching transaction ID */
1292 if ( dhcphdr
->xid
!= dhcp_xid ( dhcp
->netdev
) ) {
1293 DBGC ( dhcp
, "DHCP %p %s from %s:%d has bad transaction "
1294 "ID\n", dhcp
, dhcp_msgtype_name ( msgtype
),
1295 inet_ntoa ( peer
->sin_addr
),
1296 ntohs ( peer
->sin_port
) );
1301 /* Handle packet based on current state */
1302 dhcp
->state
->rx ( dhcp
, dhcppkt
, peer
, msgtype
, server_id
);
1305 dhcppkt_put ( dhcppkt
);
1312 /** DHCP data transfer interface operations */
1313 static struct xfer_interface_operations dhcp_xfer_operations
= {
1314 .close
= ignore_xfer_close
,
1315 .vredirect
= xfer_vreopen
,
1316 .window
= unlimited_xfer_window
,
1317 .alloc_iob
= default_xfer_alloc_iob
,
1318 .deliver_iob
= dhcp_deliver_iob
,
1319 .deliver_raw
= xfer_deliver_as_iob
,
1323 * Handle DHCP retry timer expiry
1325 * @v timer DHCP retry timer
1326 * @v fail Failure indicator
1328 static void dhcp_timer_expired ( struct retry_timer
*timer
, int fail
) {
1329 struct dhcp_session
*dhcp
=
1330 container_of ( timer
, struct dhcp_session
, timer
);
1332 /* If we have failed, terminate DHCP */
1334 dhcp_finished ( dhcp
, -ETIMEDOUT
);
1338 /* Handle timer expiry based on current state */
1339 dhcp
->state
->expired ( dhcp
);
1342 /****************************************************************************
1344 * Job control interface
1349 * Handle kill() event received via job control interface
1351 * @v job DHCP job control interface
1353 static void dhcp_job_kill ( struct job_interface
*job
) {
1354 struct dhcp_session
*dhcp
=
1355 container_of ( job
, struct dhcp_session
, job
);
1357 /* Terminate DHCP session */
1358 dhcp_finished ( dhcp
, -ECANCELED
);
1361 /** DHCP job control interface operations */
1362 static struct job_interface_operations dhcp_job_operations
= {
1363 .done
= ignore_job_done
,
1364 .kill
= dhcp_job_kill
,
1365 .progress
= ignore_job_progress
,
1368 /****************************************************************************
1375 * DHCP peer address for socket opening
1377 * This is a dummy address; the only useful portion is the socket
1378 * family (so that we get a UDP connection). The DHCP client will set
1379 * the IP address and source port explicitly on each transmission.
1381 static struct sockaddr dhcp_peer
= {
1382 .sa_family
= AF_INET
,
1386 * Start DHCP state machine on a network device
1388 * @v job Job control interface
1389 * @v netdev Network device
1390 * @ret rc Return status code, or positive if cached
1392 * Starts DHCP on the specified network device. If successful, the
1393 * DHCPACK (and ProxyDHCPACK, if applicable) will be registered as
1396 * On a return of 0, a background job has been started to perform the
1397 * DHCP request. Any nonzero return means the job has not been
1398 * started; a positive return value indicates the success condition of
1399 * having fetched the appropriate data from cached information.
1401 int start_dhcp ( struct job_interface
*job
, struct net_device
*netdev
) {
1402 struct dhcp_session
*dhcp
;
1405 /* Check for cached DHCP information */
1406 get_cached_dhcpack();
1407 if ( fetch_uintz_setting ( NULL
, &use_cached_setting
) ) {
1408 DBG ( "DHCP using cached network settings\n" );
1412 /* Allocate and initialise structure */
1413 dhcp
= zalloc ( sizeof ( *dhcp
) );
1416 dhcp
->refcnt
.free
= dhcp_free
;
1417 job_init ( &dhcp
->job
, &dhcp_job_operations
, &dhcp
->refcnt
);
1418 xfer_init ( &dhcp
->xfer
, &dhcp_xfer_operations
, &dhcp
->refcnt
);
1419 dhcp
->netdev
= netdev_get ( netdev
);
1420 dhcp
->local
.sin_family
= AF_INET
;
1421 dhcp
->local
.sin_port
= htons ( BOOTPC_PORT
);
1422 dhcp
->timer
.expired
= dhcp_timer_expired
;
1424 /* Instantiate child objects and attach to our interfaces */
1425 if ( ( rc
= xfer_open_socket ( &dhcp
->xfer
, SOCK_DGRAM
, &dhcp_peer
,
1426 ( struct sockaddr
* ) &dhcp
->local
) ) != 0 )
1429 /* Enter DHCPDISCOVER state */
1430 dhcp_set_state ( dhcp
, &dhcp_state_discover
);
1432 /* Attach parent interface, mortalise self, and return */
1433 job_plug_plug ( &dhcp
->job
, job
);
1434 ref_put ( &dhcp
->refcnt
);
1438 dhcp_finished ( dhcp
, rc
);
1439 ref_put ( &dhcp
->refcnt
);
1444 * Retrieve list of PXE boot servers for a given server type
1446 * @v dhcp DHCP session
1447 * @v raw DHCP PXE boot server list
1448 * @v raw_len Length of DHCP PXE boot server list
1449 * @v ip IP address list to fill in
1451 * The caller must ensure that the IP address list has sufficient
1454 static void pxebs_list ( struct dhcp_session
*dhcp
, void *raw
,
1455 size_t raw_len
, struct in_addr
*ip
) {
1456 struct dhcp_pxe_boot_server
*server
= raw
;
1461 if ( raw_len
< sizeof ( *server
) ) {
1462 DBGC ( dhcp
, "DHCP %p malformed PXE server list\n",
1466 server_len
= offsetof ( typeof ( *server
),
1467 ip
[ server
->num_ip
] );
1468 if ( raw_len
< server_len
) {
1469 DBGC ( dhcp
, "DHCP %p malformed PXE server list\n",
1473 if ( server
->type
== dhcp
->pxe_type
) {
1474 for ( i
= 0 ; i
< server
->num_ip
; i
++ )
1475 *(ip
++) = server
->ip
[i
];
1477 server
= ( ( ( void * ) server
) + server_len
);
1478 raw_len
-= server_len
;
1483 * Start PXE Boot Server Discovery on a network device
1485 * @v job Job control interface
1486 * @v netdev Network device
1487 * @v pxe_type PXE server type
1488 * @ret rc Return status code
1490 * Starts PXE Boot Server Discovery on the specified network device.
1491 * If successful, the Boot Server ACK will be registered as an option
1494 int start_pxebs ( struct job_interface
*job
, struct net_device
*netdev
,
1495 unsigned int pxe_type
) {
1496 struct setting pxe_discovery_control_setting
=
1497 { .tag
= DHCP_PXE_DISCOVERY_CONTROL
};
1498 struct setting pxe_boot_servers_setting
=
1499 { .tag
= DHCP_PXE_BOOT_SERVERS
};
1500 struct setting pxe_boot_server_mcast_setting
=
1501 { .tag
= DHCP_PXE_BOOT_SERVER_MCAST
};
1502 ssize_t pxebs_list_len
;
1503 struct dhcp_session
*dhcp
;
1505 unsigned int pxe_discovery_control
;
1508 /* Get upper bound for PXE boot server IP address list */
1509 pxebs_list_len
= fetch_setting_len ( NULL
, &pxe_boot_servers_setting
);
1510 if ( pxebs_list_len
< 0 )
1513 /* Allocate and initialise structure */
1514 dhcp
= zalloc ( sizeof ( *dhcp
) + sizeof ( *ip
) /* mcast */ +
1515 sizeof ( *ip
) /* bcast */ + pxebs_list_len
+
1516 sizeof ( *ip
) /* terminator */ );
1519 dhcp
->refcnt
.free
= dhcp_free
;
1520 job_init ( &dhcp
->job
, &dhcp_job_operations
, &dhcp
->refcnt
);
1521 xfer_init ( &dhcp
->xfer
, &dhcp_xfer_operations
, &dhcp
->refcnt
);
1522 dhcp
->netdev
= netdev_get ( netdev
);
1523 dhcp
->local
.sin_family
= AF_INET
;
1524 fetch_ipv4_setting ( netdev_settings ( netdev
), &ip_setting
,
1525 &dhcp
->local
.sin_addr
);
1526 dhcp
->local
.sin_port
= htons ( BOOTPC_PORT
);
1527 dhcp
->pxe_type
= cpu_to_le16 ( pxe_type
);
1528 dhcp
->timer
.expired
= dhcp_timer_expired
;
1530 /* Construct PXE boot server IP address lists */
1531 pxe_discovery_control
=
1532 fetch_uintz_setting ( NULL
, &pxe_discovery_control_setting
);
1533 ip
= ( ( ( void * ) dhcp
) + sizeof ( *dhcp
) );
1534 dhcp
->pxe_attempt
= ip
;
1535 if ( ! ( pxe_discovery_control
& PXEBS_NO_MULTICAST
) ) {
1536 fetch_ipv4_setting ( NULL
, &pxe_boot_server_mcast_setting
, ip
);
1540 if ( ! ( pxe_discovery_control
& PXEBS_NO_BROADCAST
) )
1541 (ip
++)->s_addr
= INADDR_BROADCAST
;
1542 if ( pxe_discovery_control
& PXEBS_NO_UNKNOWN_SERVERS
)
1543 dhcp
->pxe_accept
= ip
;
1544 if ( pxebs_list_len
) {
1545 uint8_t buf
[pxebs_list_len
];
1547 fetch_setting ( NULL
, &pxe_boot_servers_setting
,
1548 buf
, sizeof ( buf
) );
1549 pxebs_list ( dhcp
, buf
, sizeof ( buf
), ip
);
1551 if ( ! dhcp
->pxe_attempt
->s_addr
) {
1552 DBGC ( dhcp
, "DHCP %p has no PXE boot servers for type %04x\n",
1558 /* Dump out PXE server lists */
1559 DBGC ( dhcp
, "DHCP %p attempting", dhcp
);
1560 for ( ip
= dhcp
->pxe_attempt
; ip
->s_addr
; ip
++ )
1561 DBGC ( dhcp
, " %s", inet_ntoa ( *ip
) );
1562 DBGC ( dhcp
, "\n" );
1563 if ( dhcp
->pxe_accept
) {
1564 DBGC ( dhcp
, "DHCP %p accepting", dhcp
);
1565 for ( ip
= dhcp
->pxe_accept
; ip
->s_addr
; ip
++ )
1566 DBGC ( dhcp
, " %s", inet_ntoa ( *ip
) );
1567 DBGC ( dhcp
, "\n" );
1570 /* Instantiate child objects and attach to our interfaces */
1571 if ( ( rc
= xfer_open_socket ( &dhcp
->xfer
, SOCK_DGRAM
, &dhcp_peer
,
1572 ( struct sockaddr
* ) &dhcp
->local
) ) != 0 )
1575 /* Enter PXEBS state */
1576 dhcp_set_state ( dhcp
, &dhcp_state_pxebs
);
1578 /* Attach parent interface, mortalise self, and return */
1579 job_plug_plug ( &dhcp
->job
, job
);
1580 ref_put ( &dhcp
->refcnt
);
1584 dhcp_finished ( dhcp
, rc
);
1585 ref_put ( &dhcp
->refcnt
);