3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
25 IN PXE_DHCP4_PRIVATE_DATA
*Private
,
26 IN DHCP4_PACKET
*tx_pkt
,
27 IN DHCP4_PACKET
*rx_pkt
,
31 -2 = ignore, stop waiting
32 -1 = ignore, keep waiting
33 0 = accept, keep waiting
34 1 = accept, stop waiting
39 DHCP4_OP
*msg_type_op
;
44 // Verify parameters. Touch unused parameters to keep
50 if (Private
== NULL
|| rx_pkt
== NULL
) {
55 rx_pkt_size
= rx_pkt_size
;
58 // This may be a BOOTP Reply or DHCP Offer packet.
59 // If there is no DHCP magik number, assume that
60 // this is a BOOTP Reply packet.
62 magik
= htonl (DHCP4_MAGIK_NUMBER
);
64 while (!CompareMem (&rx_pkt
->dhcp4
.magik
, &magik
, 4)) {
66 // If there is no DHCP message type option, assume
67 // this is a BOOTP reply packet and cache it.
69 EfiStatus
= find_opt (rx_pkt
, DHCP4_MESSAGE_TYPE
, 0, &msg_type_op
);
71 if (EFI_ERROR (EfiStatus
)) {
75 // If there is a DHCP message type option, it must be a
78 if (msg_type_op
->len
!= 1) {
82 if (msg_type_op
->data
[0] != DHCP4_MESSAGE_TYPE_OFFER
) {
86 // There must be a server identifier option.
88 EfiStatus
= find_opt (
90 DHCP4_SERVER_IDENTIFIER
,
95 if (EFI_ERROR (EfiStatus
)) {
99 if (srvid_op
->len
!= 4) {
103 // Good DHCP offer packet.
108 // Good DHCP (or BOOTP) packet. Cache it!
110 tmp
= AllocatePool ((Private
->offers
+ 1) * sizeof (DHCP4_PACKET
));
117 if (Private
->offers
!= 0) {
121 Private
->offers
* sizeof (DHCP4_PACKET
)
124 FreePool (Private
->offer_list
);
127 CopyMem (&tmp
[Private
->offers
++], rx_pkt
, sizeof (DHCP4_PACKET
));
129 Private
->offer_list
= tmp
;
134 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
138 IN PXE_DHCP4_PRIVATE_DATA
*Private
,
139 IN DHCP4_PACKET
*tx_pkt
,
140 IN DHCP4_PACKET
*rx_pkt
,
144 -2 = ignore, stop waiting
145 -1 = ignore, keep waiting
146 0 = accept, keep waiting
147 1 = accept, stop waiting
150 EFI_STATUS EfiStatus
;
151 DHCP4_OP
*msg_type_op
;
155 DHCP4_OP
*lease_time_op
;
159 // Verify parameters. Touch unused parameters to
160 // keep compiler happy.
165 if (Private
== NULL
|| rx_pkt
== NULL
) {
170 rx_pkt_size
= rx_pkt_size
;
173 // This must be a DHCP Ack message.
175 magik
= htonl (DHCP4_MAGIK_NUMBER
);
177 if (CompareMem (&rx_pkt
->dhcp4
.magik
, &magik
, 4)) {
181 EfiStatus
= find_opt (rx_pkt
, DHCP4_MESSAGE_TYPE
, 0, &msg_type_op
);
183 if (EFI_ERROR (EfiStatus
)) {
187 if (msg_type_op
->len
!= 1) {
191 if (msg_type_op
->data
[0] != DHCP4_MESSAGE_TYPE_ACK
) {
195 // There must be a server identifier.
197 EfiStatus
= find_opt (rx_pkt
, DHCP4_SERVER_IDENTIFIER
, 0, &srvid_op
);
199 if (EFI_ERROR (EfiStatus
)) {
203 if (srvid_op
->len
!= 4) {
207 // There should be a renewal time.
208 // If there is not, we will default to the 7/8 of the rebinding time.
210 EfiStatus
= find_opt (rx_pkt
, DHCP4_RENEWAL_TIME
, 0, &renew_op
);
212 if (EFI_ERROR (EfiStatus
)) {
214 } else if (renew_op
->len
!= 4) {
218 // There should be a rebinding time.
219 // If there is not, we will default to 7/8 of the lease time.
221 EfiStatus
= find_opt (rx_pkt
, DHCP4_REBINDING_TIME
, 0, &rebind_op
);
223 if (EFI_ERROR (EfiStatus
)) {
225 } else if (rebind_op
->len
!= 4) {
229 // There should be a lease time.
230 // If there is not, we will default to one week.
232 EfiStatus
= find_opt (rx_pkt
, DHCP4_LEASE_TIME
, 0, &lease_time_op
);
234 if (EFI_ERROR (EfiStatus
)) {
235 lease_time_op
= NULL
;
236 } else if (lease_time_op
->len
!= 4) {
237 lease_time_op
= NULL
;
240 // Packet looks good. Double check the renew, rebind and lease times.
242 CopyMem (&Private
->ServerIp
, srvid_op
->data
, 4);
244 if (renew_op
!= NULL
) {
245 CopyMem (&Private
->RenewTime
, renew_op
->data
, 4);
246 Private
->RenewTime
= htonl (Private
->RenewTime
);
248 Private
->RenewTime
= 0;
251 if (rebind_op
!= NULL
) {
252 CopyMem (&Private
->RebindTime
, rebind_op
->data
, 4);
253 Private
->RebindTime
= htonl (Private
->RebindTime
);
255 Private
->RebindTime
= 0;
258 if (lease_time_op
!= NULL
) {
259 CopyMem (&Private
->LeaseTime
, lease_time_op
->data
, 4);
260 Private
->LeaseTime
= htonl (Private
->LeaseTime
);
262 Private
->LeaseTime
= 0;
265 if (Private
->LeaseTime
< 60) {
266 Private
->LeaseTime
= 7 * 86400;
269 if (Private
->RebindTime
< 52 || Private
->RebindTime
>= Private
->LeaseTime
) {
270 Private
->RebindTime
= Private
->LeaseTime
/ 2 + Private
->LeaseTime
/ 4 + Private
->LeaseTime
/ 8;
273 if (Private
->RenewTime
< 45 || Private
->RenewTime
>= Private
->RebindTime
) {
274 Private
->RenewTime
= Private
->RebindTime
/ 2 + Private
->RebindTime
/ 4 + Private
->RebindTime
/ 8;
280 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
284 IN EFI_PXE_DHCP4_PROTOCOL
*This
,
285 IN UINTN seconds_timeout
,
287 OUT DHCP4_PACKET
**OfferList
290 PXE_DHCP4_PRIVATE_DATA
*Private
;
292 EFI_IP_ADDRESS bcast_ip
;
293 EFI_STATUS EfiStatus
;
296 // Verify parameters and protocol state.
299 seconds_timeout
< DHCP4_MIN_SECONDS
||
300 seconds_timeout
> DHCP4_MAX_SECONDS
||
305 // Return parameters are not initialized when
306 // parameters are invalid!
308 return EFI_INVALID_PARAMETER
;
315 // Check protocol state.
317 if (This
->Data
== NULL
) {
318 return EFI_NOT_STARTED
;
321 if (!This
->Data
->SetupCompleted
) {
322 return EFI_NOT_READY
;
326 // Get pointer to our instance data.
328 Private
= PXE_DHCP4_PRIVATE_DATA_FROM_THIS (This
);
330 if (Private
== NULL
) {
331 return EFI_INVALID_PARAMETER
;
334 if (Private
->PxeBc
== NULL
) {
335 return EFI_DEVICE_ERROR
;
338 // Setup variables...
341 Private
->offer_list
= NULL
;
343 EfiStatus
= gBS
->HandleProtocol (
345 &gEfiPxeDhcp4CallbackProtocolGuid
,
346 (VOID
*) &Private
->callback
349 if (EFI_ERROR (EfiStatus
)) {
350 Private
->callback
= NULL
;
353 Private
->function
= EFI_PXE_DHCP4_FUNCTION_INIT
;
356 // Increment the transaction ID.
361 CopyMem (&xid
, &This
->Data
->Discover
.dhcp4
.xid
, sizeof (UINT32
));
363 xid
= htonl (htonl (xid
) + 1);
365 CopyMem (&This
->Data
->Discover
.dhcp4
.xid
, &xid
, sizeof (UINT32
));
368 // Transmit discover and wait for offers...
370 SetMem (&bcast_ip
, sizeof (EFI_IP_ADDRESS
), 0xFF);
372 EfiStatus
= tx_rx_udp (
378 &This
->Data
->Discover
,
384 if (EFI_ERROR (EfiStatus
)) {
385 if (Private
->offer_list
) {
386 FreePool (Private
->offer_list
);
390 Private
->offer_list
= NULL
;
391 Private
->callback
= NULL
;
393 DEBUG ((EFI_D_ERROR
, "%r\n", EfiStatus
));
397 *Offers
= Private
->offers
;
398 *OfferList
= Private
->offer_list
;
401 Private
->offer_list
= NULL
;
402 Private
->callback
= NULL
;
404 This
->Data
->InitCompleted
= TRUE
;
405 This
->Data
->SelectCompleted
= FALSE
;
406 This
->Data
->IsBootp
= FALSE
;
407 This
->Data
->IsAck
= FALSE
;
412 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
416 IN EFI_PXE_DHCP4_PROTOCOL
*This
,
417 IN UINTN seconds_timeout
,
418 IN DHCP4_PACKET
*Offer
421 PXE_DHCP4_PRIVATE_DATA
*Private
;
422 EFI_STATUS EfiStatus
;
423 DHCP4_PACKET request
;
425 EFI_IP_ADDRESS bcast_ip
;
426 EFI_IP_ADDRESS zero_ip
;
427 EFI_IP_ADDRESS local_ip
;
435 // Verify parameters.
437 if (This
== NULL
|| seconds_timeout
< DHCP4_MIN_SECONDS
|| seconds_timeout
> DHCP4_MAX_SECONDS
|| Offer
== NULL
) {
438 return EFI_INVALID_PARAMETER
;
441 // Check protocol state.
443 if (This
->Data
== NULL
) {
444 return EFI_NOT_STARTED
;
447 if (!This
->Data
->SetupCompleted
) {
448 return EFI_NOT_READY
;
451 // Get pointer to instance data.
453 Private
= PXE_DHCP4_PRIVATE_DATA_FROM_THIS (This
);
455 if (Private
== NULL
) {
456 return EFI_INVALID_PARAMETER
;
459 if (Private
->PxeBc
== NULL
) {
460 return EFI_DEVICE_ERROR
;
464 // Setup useful variables...
466 SetMem (&bcast_ip
, sizeof (EFI_IP_ADDRESS
), 0xFF);
468 ZeroMem (&zero_ip
, sizeof (EFI_IP_ADDRESS
));
470 ZeroMem (&local_ip
, sizeof (EFI_IP_ADDRESS
));
471 local_ip
.v4
.Addr
[0] = 127;
472 local_ip
.v4
.Addr
[3] = 1;
474 This
->Data
->SelectCompleted
= FALSE
;
475 This
->Data
->IsBootp
= FALSE
;
476 This
->Data
->IsAck
= FALSE
;
478 EfiStatus
= gBS
->HandleProtocol (
480 &gEfiPxeDhcp4CallbackProtocolGuid
,
481 (VOID
*) &Private
->callback
484 if (EFI_ERROR (EfiStatus
)) {
485 Private
->callback
= NULL
;
488 Private
->function
= EFI_PXE_DHCP4_FUNCTION_SELECT
;
491 // Verify offer packet fields.
493 if (Offer
->dhcp4
.op
!= BOOTP_REPLY
) {
494 Private
->callback
= NULL
;
495 return EFI_INVALID_PARAMETER
;
498 if (Offer
->dhcp4
.htype
!= This
->Data
->Discover
.dhcp4
.htype
) {
499 Private
->callback
= NULL
;
500 return EFI_INVALID_PARAMETER
;
503 if (Offer
->dhcp4
.hlen
!= This
->Data
->Discover
.dhcp4
.hlen
) {
504 Private
->callback
= NULL
;
505 return EFI_INVALID_PARAMETER
;
508 if (CompareMem (&Offer
->dhcp4
.xid
, &This
->Data
->Discover
.dhcp4
.xid
, 4)) {
509 Private
->callback
= NULL
;
510 return EFI_INVALID_PARAMETER
;
513 if (!CompareMem (&Offer
->dhcp4
.yiaddr
, &bcast_ip
, 4)) {
514 Private
->callback
= NULL
;
515 return EFI_INVALID_PARAMETER
;
518 if (!CompareMem (&Offer
->dhcp4
.yiaddr
, &zero_ip
, 4)) {
519 Private
->callback
= NULL
;
520 return EFI_INVALID_PARAMETER
;
523 if (!CompareMem (&Offer
->dhcp4
.yiaddr
, &local_ip
, 4)) {
524 Private
->callback
= NULL
;
525 return EFI_INVALID_PARAMETER
;
529 &Offer
->dhcp4
.chaddr
,
530 &This
->Data
->Discover
.dhcp4
.chaddr
,
533 Private
->callback
= NULL
;
534 return EFI_INVALID_PARAMETER
;
537 // DHCP option checks
539 dhcp4_magik
= htonl (DHCP4_MAGIK_NUMBER
);
542 if (!CompareMem (&Offer
->dhcp4
.magik
, &dhcp4_magik
, 4)) {
544 // If present, DHCP message type must be offer.
546 EfiStatus
= find_opt (Offer
, DHCP4_MESSAGE_TYPE
, 0, &op
);
548 if (!EFI_ERROR (EfiStatus
)) {
549 if (op
->len
!= 1 || op
->data
[0] != DHCP4_MESSAGE_TYPE_OFFER
) {
550 Private
->callback
= NULL
;
551 return EFI_INVALID_PARAMETER
;
557 // If present, DHCP max message size must be valid.
559 EfiStatus
= find_opt (Offer
, DHCP4_MAX_MESSAGE_SIZE
, 0, &op
);
561 if (!EFI_ERROR (EfiStatus
)) {
562 if (op
->len
!= 2 || ((op
->data
[0] << 8) | op
->data
[1]) < DHCP4_DEFAULT_MAX_MESSAGE_SIZE
) {
563 Private
->callback
= NULL
;
564 return EFI_INVALID_PARAMETER
;
568 // If present, DHCP server identifier must be valid.
570 EfiStatus
= find_opt (Offer
, DHCP4_SERVER_IDENTIFIER
, 0, &op
);
572 if (!EFI_ERROR (EfiStatus
)) {
573 if (op
->len
!= 4 || !CompareMem (op
->data
, &bcast_ip
, 4) || !CompareMem (op
->data
, &zero_ip
, 4)) {
574 Private
->callback
= NULL
;
575 return EFI_INVALID_PARAMETER
;
579 // If present, DHCP subnet mask must be valid.
581 EfiStatus
= find_opt (
588 if (!EFI_ERROR (EfiStatus
)) {
590 Private
->callback
= NULL
;
591 return EFI_INVALID_PARAMETER
;
596 // Early out for BOOTP.
598 This
->Data
->IsBootp
= is_bootp
;
601 // Copy offer packet to instance data.
603 CopyMem (&This
->Data
->Offer
, Offer
, sizeof (DHCP4_PACKET
));
606 // Copy discover to request and offer to acknak.
609 &This
->Data
->Request
,
610 &This
->Data
->Discover
,
611 sizeof (DHCP4_PACKET
)
617 sizeof (DHCP4_PACKET
)
623 This
->Data
->SelectCompleted
= TRUE
;
624 This
->Data
->IsAck
= TRUE
;
626 Private
->callback
= NULL
;
630 // Copy discover packet contents to request packet.
632 CopyMem (&request
, &This
->Data
->Discover
, sizeof (DHCP4_PACKET
));
634 This
->Data
->IsAck
= FALSE
;
637 // Change DHCP message type from discover to request.
639 EfiStatus
= find_opt (&request
, DHCP4_MESSAGE_TYPE
, 0, &op
);
641 if (EFI_ERROR (EfiStatus
) && EfiStatus
!= EFI_NOT_FOUND
) {
642 Private
->callback
= NULL
;
643 return EFI_INVALID_PARAMETER
;
646 if (EfiStatus
== EFI_NOT_FOUND
) {
647 EfiStatus
= find_opt (&request
, DHCP4_END
, 0, &op
);
649 if (EFI_ERROR (EfiStatus
)) {
650 Private
->callback
= NULL
;
651 return EFI_INVALID_PARAMETER
;
654 op
->op
= DHCP4_MESSAGE_TYPE
;
657 op
->data
[1] = DHCP4_END
;
660 op
->data
[0] = DHCP4_MESSAGE_TYPE_REQUEST
;
663 // Copy server identifier option from offer to request.
665 EfiStatus
= find_opt (Offer
, DHCP4_SERVER_IDENTIFIER
, 0, &srvid
);
667 if (EFI_ERROR (EfiStatus
)) {
668 Private
->callback
= NULL
;
669 return EFI_INVALID_PARAMETER
;
672 if (srvid
->len
!= 4) {
673 Private
->callback
= NULL
;
674 return EFI_INVALID_PARAMETER
;
677 EfiStatus
= add_opt (&request
, srvid
);
679 if (EFI_ERROR (EfiStatus
)) {
680 DEBUG ((EFI_D_ERROR
, "%r\n", EfiStatus
));
681 Private
->callback
= NULL
;
685 // Add requested IP address option to request packet.
687 op
= (DHCP4_OP
*) buf
;
688 op
->op
= DHCP4_REQUESTED_IP_ADDRESS
;
690 CopyMem (op
->data
, &Offer
->dhcp4
.yiaddr
, 4);
692 EfiStatus
= add_opt (&request
, op
);
694 if (EFI_ERROR (EfiStatus
)) {
695 DEBUG ((EFI_D_ERROR
, "%r\n", EfiStatus
));
696 Private
->callback
= NULL
;
700 // Transimit DHCP request and wait for DHCP ack...
702 SetMem (&bcast_ip
, sizeof (EFI_IP_ADDRESS
), 0xFF);
704 EfiStatus
= tx_rx_udp (
716 if (EFI_ERROR (EfiStatus
)) {
717 DEBUG ((EFI_D_ERROR
, "%r\n", EfiStatus
));
718 Private
->callback
= NULL
;
722 // Set Data->IsAck and return.
724 EfiStatus
= find_opt (&acknak
, DHCP4_MESSAGE_TYPE
, 0, &op
);
726 if (EFI_ERROR (EfiStatus
)) {
727 Private
->callback
= NULL
;
728 return EFI_DEVICE_ERROR
;
732 Private
->callback
= NULL
;
733 return EFI_DEVICE_ERROR
;
736 switch (op
->data
[0]) {
737 case DHCP4_MESSAGE_TYPE_ACK
:
738 This
->Data
->IsAck
= TRUE
;
741 case DHCP4_MESSAGE_TYPE_NAK
:
742 This
->Data
->IsAck
= FALSE
;
746 Private
->callback
= NULL
;
747 return EFI_DEVICE_ERROR
;
750 // Copy packets into instance data...
752 CopyMem (&This
->Data
->Offer
, Offer
, sizeof (DHCP4_PACKET
));
753 CopyMem (&This
->Data
->Request
, &request
, sizeof (DHCP4_PACKET
));
754 CopyMem (&This
->Data
->AckNak
, &acknak
, sizeof (DHCP4_PACKET
));
756 This
->Data
->SelectCompleted
= TRUE
;
758 Private
->callback
= NULL
;
762 /* eof - PxeDhcp4InitSelect.c */