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.
24 // #define DebugPrint(x) Aprint x
26 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
30 IN PXE_DHCP4_PRIVATE_DATA
*Private
,
31 IN DHCP4_PACKET
*tx_pkt
,
32 IN DHCP4_PACKET
*rx_pkt
,
36 -2 = ignore, stop waiting
37 -1 = ignore, keep waiting
38 0 = accept, keep waiting
39 1 = accept, stop waiting
44 DHCP4_OP
*msg_type_op
;
49 // Verify parameters. Touch unused parameters to keep
55 if (Private
== NULL
|| rx_pkt
== NULL
) {
60 rx_pkt_size
= rx_pkt_size
;
63 // This may be a BOOTP Reply or DHCP Offer packet.
64 // If there is no DHCP magik number, assume that
65 // this is a BOOTP Reply packet.
67 magik
= htonl (DHCP4_MAGIK_NUMBER
);
69 while (!CompareMem (&rx_pkt
->dhcp4
.magik
, &magik
, 4)) {
71 // If there is no DHCP message type option, assume
72 // this is a BOOTP reply packet and cache it.
74 EfiStatus
= find_opt (rx_pkt
, DHCP4_MESSAGE_TYPE
, 0, &msg_type_op
);
76 if (EFI_ERROR (EfiStatus
)) {
80 // If there is a DHCP message type option, it must be a
83 if (msg_type_op
->len
!= 1) {
87 if (msg_type_op
->data
[0] != DHCP4_MESSAGE_TYPE_OFFER
) {
91 // There must be a server identifier option.
93 EfiStatus
= find_opt (
95 DHCP4_SERVER_IDENTIFIER
,
100 if (EFI_ERROR (EfiStatus
)) {
104 if (srvid_op
->len
!= 4) {
108 // Good DHCP offer packet.
113 // Good DHCP (or BOOTP) packet. Cache it!
115 EfiStatus
= gBS
->AllocatePool (
117 (Private
->offers
+ 1) * sizeof (DHCP4_PACKET
),
121 if (EFI_ERROR (EfiStatus
)) {
127 if (Private
->offers
!= 0) {
131 Private
->offers
* sizeof (DHCP4_PACKET
)
134 gBS
->FreePool (Private
->offer_list
);
137 CopyMem (&tmp
[Private
->offers
++], rx_pkt
, sizeof (DHCP4_PACKET
));
139 Private
->offer_list
= tmp
;
144 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
148 IN PXE_DHCP4_PRIVATE_DATA
*Private
,
149 IN DHCP4_PACKET
*tx_pkt
,
150 IN DHCP4_PACKET
*rx_pkt
,
154 -2 = ignore, stop waiting
155 -1 = ignore, keep waiting
156 0 = accept, keep waiting
157 1 = accept, stop waiting
160 EFI_STATUS EfiStatus
;
161 DHCP4_OP
*msg_type_op
;
165 DHCP4_OP
*lease_time_op
;
169 // Verify parameters. Touch unused parameters to
170 // keep compiler happy.
175 if (Private
== NULL
|| rx_pkt
== NULL
) {
180 rx_pkt_size
= rx_pkt_size
;
183 // This must be a DHCP Ack message.
185 magik
= htonl (DHCP4_MAGIK_NUMBER
);
187 if (CompareMem (&rx_pkt
->dhcp4
.magik
, &magik
, 4)) {
191 EfiStatus
= find_opt (rx_pkt
, DHCP4_MESSAGE_TYPE
, 0, &msg_type_op
);
193 if (EFI_ERROR (EfiStatus
)) {
197 if (msg_type_op
->len
!= 1) {
201 if (msg_type_op
->data
[0] != DHCP4_MESSAGE_TYPE_ACK
) {
205 // There must be a server identifier.
207 EfiStatus
= find_opt (rx_pkt
, DHCP4_SERVER_IDENTIFIER
, 0, &srvid_op
);
209 if (EFI_ERROR (EfiStatus
)) {
213 if (srvid_op
->len
!= 4) {
217 // There should be a renewal time.
218 // If there is not, we will default to the 7/8 of the rebinding time.
220 EfiStatus
= find_opt (rx_pkt
, DHCP4_RENEWAL_TIME
, 0, &renew_op
);
222 if (EFI_ERROR (EfiStatus
)) {
224 } else if (renew_op
->len
!= 4) {
228 // There should be a rebinding time.
229 // If there is not, we will default to 7/8 of the lease time.
231 EfiStatus
= find_opt (rx_pkt
, DHCP4_REBINDING_TIME
, 0, &rebind_op
);
233 if (EFI_ERROR (EfiStatus
)) {
235 } else if (rebind_op
->len
!= 4) {
239 // There should be a lease time.
240 // If there is not, we will default to one week.
242 EfiStatus
= find_opt (rx_pkt
, DHCP4_LEASE_TIME
, 0, &lease_time_op
);
244 if (EFI_ERROR (EfiStatus
)) {
245 lease_time_op
= NULL
;
246 } else if (lease_time_op
->len
!= 4) {
247 lease_time_op
= NULL
;
250 // Packet looks good. Double check the renew, rebind and lease times.
252 CopyMem (&Private
->ServerIp
, srvid_op
->data
, 4);
254 if (renew_op
!= NULL
) {
255 CopyMem (&Private
->RenewTime
, renew_op
->data
, 4);
256 Private
->RenewTime
= htonl (Private
->RenewTime
);
258 Private
->RenewTime
= 0;
261 if (rebind_op
!= NULL
) {
262 CopyMem (&Private
->RebindTime
, rebind_op
->data
, 4);
263 Private
->RebindTime
= htonl (Private
->RebindTime
);
265 Private
->RebindTime
= 0;
268 if (lease_time_op
!= NULL
) {
269 CopyMem (&Private
->LeaseTime
, lease_time_op
->data
, 4);
270 Private
->LeaseTime
= htonl (Private
->LeaseTime
);
272 Private
->LeaseTime
= 0;
275 if (Private
->LeaseTime
< 60) {
276 Private
->LeaseTime
= 7 * 86400;
279 if (Private
->RebindTime
< 52 || Private
->RebindTime
>= Private
->LeaseTime
) {
280 Private
->RebindTime
= Private
->LeaseTime
/ 2 + Private
->LeaseTime
/ 4 + Private
->LeaseTime
/ 8;
283 if (Private
->RenewTime
< 45 || Private
->RenewTime
>= Private
->RebindTime
) {
284 Private
->RenewTime
= Private
->RebindTime
/ 2 + Private
->RebindTime
/ 4 + Private
->RebindTime
/ 8;
290 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
294 IN EFI_PXE_DHCP4_PROTOCOL
*This
,
295 IN UINTN seconds_timeout
,
297 OUT DHCP4_PACKET
**OfferList
300 PXE_DHCP4_PRIVATE_DATA
*Private
;
302 EFI_IP_ADDRESS bcast_ip
;
303 EFI_STATUS EfiStatus
;
306 // Verify parameters and protocol state.
309 seconds_timeout
< DHCP4_MIN_SECONDS
||
310 seconds_timeout
> DHCP4_MAX_SECONDS
||
315 // Return parameters are not initialized when
316 // parameters are invalid!
318 return EFI_INVALID_PARAMETER
;
325 // Check protocol state.
327 if (This
->Data
== NULL
) {
328 return EFI_NOT_STARTED
;
331 if (!This
->Data
->SetupCompleted
) {
332 return EFI_NOT_READY
;
336 // Get pointer to our instance data.
338 Private
= PXE_DHCP4_PRIVATE_DATA_FROM_THIS (This
);
340 if (Private
== NULL
) {
341 return EFI_INVALID_PARAMETER
;
344 if (Private
->PxeBc
== NULL
) {
345 return EFI_DEVICE_ERROR
;
348 // Setup variables...
351 Private
->offer_list
= NULL
;
353 EfiStatus
= gBS
->HandleProtocol (
355 &gEfiPxeDhcp4CallbackProtocolGuid
,
356 (VOID
*) &Private
->callback
359 if (EFI_ERROR (EfiStatus
)) {
360 Private
->callback
= NULL
;
363 Private
->function
= EFI_PXE_DHCP4_FUNCTION_INIT
;
366 // Increment the transaction ID.
371 CopyMem (&xid
, &This
->Data
->Discover
.dhcp4
.xid
, sizeof (UINT32
));
373 xid
= htonl (htonl (xid
) + 1);
375 CopyMem (&This
->Data
->Discover
.dhcp4
.xid
, &xid
, sizeof (UINT32
));
378 // Transmit discover and wait for offers...
380 SetMem (&bcast_ip
, sizeof (EFI_IP_ADDRESS
), 0xFF);
382 EfiStatus
= tx_rx_udp (
388 &This
->Data
->Discover
,
394 if (EFI_ERROR (EfiStatus
)) {
395 if (Private
->offer_list
) {
396 gBS
->FreePool (Private
->offer_list
);
400 Private
->offer_list
= NULL
;
401 Private
->callback
= NULL
;
403 DebugPrint (("%a:%d:%r\n", __FILE__
, __LINE__
, EfiStatus
));
407 *Offers
= Private
->offers
;
408 *OfferList
= Private
->offer_list
;
411 Private
->offer_list
= NULL
;
412 Private
->callback
= NULL
;
414 This
->Data
->InitCompleted
= TRUE
;
415 This
->Data
->SelectCompleted
= FALSE
;
416 This
->Data
->IsBootp
= FALSE
;
417 This
->Data
->IsAck
= FALSE
;
422 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
426 IN EFI_PXE_DHCP4_PROTOCOL
*This
,
427 IN UINTN seconds_timeout
,
428 IN DHCP4_PACKET
*Offer
431 PXE_DHCP4_PRIVATE_DATA
*Private
;
432 EFI_STATUS EfiStatus
;
433 DHCP4_PACKET request
;
435 EFI_IP_ADDRESS bcast_ip
;
436 EFI_IP_ADDRESS zero_ip
;
437 EFI_IP_ADDRESS local_ip
;
445 // Verify parameters.
447 if (This
== NULL
|| seconds_timeout
< DHCP4_MIN_SECONDS
|| seconds_timeout
> DHCP4_MAX_SECONDS
|| Offer
== NULL
) {
448 return EFI_INVALID_PARAMETER
;
451 // Check protocol state.
453 if (This
->Data
== NULL
) {
454 return EFI_NOT_STARTED
;
457 if (!This
->Data
->SetupCompleted
) {
458 return EFI_NOT_READY
;
461 // Get pointer to instance data.
463 Private
= PXE_DHCP4_PRIVATE_DATA_FROM_THIS (This
);
465 if (Private
== NULL
) {
466 return EFI_INVALID_PARAMETER
;
469 if (Private
->PxeBc
== NULL
) {
470 return EFI_DEVICE_ERROR
;
474 // Setup useful variables...
476 SetMem (&bcast_ip
, sizeof (EFI_IP_ADDRESS
), 0xFF);
478 ZeroMem (&zero_ip
, sizeof (EFI_IP_ADDRESS
));
480 ZeroMem (&local_ip
, sizeof (EFI_IP_ADDRESS
));
481 local_ip
.v4
.Addr
[0] = 127;
482 local_ip
.v4
.Addr
[3] = 1;
484 This
->Data
->SelectCompleted
= FALSE
;
485 This
->Data
->IsBootp
= FALSE
;
486 This
->Data
->IsAck
= FALSE
;
488 EfiStatus
= gBS
->HandleProtocol (
490 &gEfiPxeDhcp4CallbackProtocolGuid
,
491 (VOID
*) &Private
->callback
494 if (EFI_ERROR (EfiStatus
)) {
495 Private
->callback
= NULL
;
498 Private
->function
= EFI_PXE_DHCP4_FUNCTION_SELECT
;
501 // Verify offer packet fields.
503 if (Offer
->dhcp4
.op
!= BOOTP_REPLY
) {
504 Private
->callback
= NULL
;
505 return EFI_INVALID_PARAMETER
;
508 if (Offer
->dhcp4
.htype
!= This
->Data
->Discover
.dhcp4
.htype
) {
509 Private
->callback
= NULL
;
510 return EFI_INVALID_PARAMETER
;
513 if (Offer
->dhcp4
.hlen
!= This
->Data
->Discover
.dhcp4
.hlen
) {
514 Private
->callback
= NULL
;
515 return EFI_INVALID_PARAMETER
;
518 if (CompareMem (&Offer
->dhcp4
.xid
, &This
->Data
->Discover
.dhcp4
.xid
, 4)) {
519 Private
->callback
= NULL
;
520 return EFI_INVALID_PARAMETER
;
523 if (!CompareMem (&Offer
->dhcp4
.yiaddr
, &bcast_ip
, 4)) {
524 Private
->callback
= NULL
;
525 return EFI_INVALID_PARAMETER
;
528 if (!CompareMem (&Offer
->dhcp4
.yiaddr
, &zero_ip
, 4)) {
529 Private
->callback
= NULL
;
530 return EFI_INVALID_PARAMETER
;
533 if (!CompareMem (&Offer
->dhcp4
.yiaddr
, &local_ip
, 4)) {
534 Private
->callback
= NULL
;
535 return EFI_INVALID_PARAMETER
;
539 &Offer
->dhcp4
.chaddr
,
540 &This
->Data
->Discover
.dhcp4
.chaddr
,
543 Private
->callback
= NULL
;
544 return EFI_INVALID_PARAMETER
;
547 // DHCP option checks
549 dhcp4_magik
= htonl (DHCP4_MAGIK_NUMBER
);
552 if (!CompareMem (&Offer
->dhcp4
.magik
, &dhcp4_magik
, 4)) {
554 // If present, DHCP message type must be offer.
556 EfiStatus
= find_opt (Offer
, DHCP4_MESSAGE_TYPE
, 0, &op
);
558 if (!EFI_ERROR (EfiStatus
)) {
559 if (op
->len
!= 1 || op
->data
[0] != DHCP4_MESSAGE_TYPE_OFFER
) {
560 Private
->callback
= NULL
;
561 return EFI_INVALID_PARAMETER
;
567 // If present, DHCP max message size must be valid.
569 EfiStatus
= find_opt (Offer
, DHCP4_MAX_MESSAGE_SIZE
, 0, &op
);
571 if (!EFI_ERROR (EfiStatus
)) {
572 if (op
->len
!= 2 || ((op
->data
[0] << 8) | op
->data
[1]) < DHCP4_DEFAULT_MAX_MESSAGE_SIZE
) {
573 Private
->callback
= NULL
;
574 return EFI_INVALID_PARAMETER
;
578 // If present, DHCP server identifier must be valid.
580 EfiStatus
= find_opt (Offer
, DHCP4_SERVER_IDENTIFIER
, 0, &op
);
582 if (!EFI_ERROR (EfiStatus
)) {
583 if (op
->len
!= 4 || !CompareMem (op
->data
, &bcast_ip
, 4) || !CompareMem (op
->data
, &zero_ip
, 4)) {
584 Private
->callback
= NULL
;
585 return EFI_INVALID_PARAMETER
;
589 // If present, DHCP subnet mask must be valid.
591 EfiStatus
= find_opt (
598 if (!EFI_ERROR (EfiStatus
)) {
600 Private
->callback
= NULL
;
601 return EFI_INVALID_PARAMETER
;
606 // Early out for BOOTP.
608 This
->Data
->IsBootp
= is_bootp
;
611 // Copy offer packet to instance data.
613 CopyMem (&This
->Data
->Offer
, Offer
, sizeof (DHCP4_PACKET
));
616 // Copy discover to request and offer to acknak.
619 &This
->Data
->Request
,
620 &This
->Data
->Discover
,
621 sizeof (DHCP4_PACKET
)
627 sizeof (DHCP4_PACKET
)
633 This
->Data
->SelectCompleted
= TRUE
;
634 This
->Data
->IsAck
= TRUE
;
636 Private
->callback
= NULL
;
640 // Copy discover packet contents to request packet.
642 CopyMem (&request
, &This
->Data
->Discover
, sizeof (DHCP4_PACKET
));
644 This
->Data
->IsAck
= FALSE
;
647 // Change DHCP message type from discover to request.
649 EfiStatus
= find_opt (&request
, DHCP4_MESSAGE_TYPE
, 0, &op
);
651 if (EFI_ERROR (EfiStatus
) && EfiStatus
!= EFI_NOT_FOUND
) {
652 Private
->callback
= NULL
;
653 return EFI_INVALID_PARAMETER
;
656 if (EfiStatus
== EFI_NOT_FOUND
) {
657 EfiStatus
= find_opt (&request
, DHCP4_END
, 0, &op
);
659 if (EFI_ERROR (EfiStatus
)) {
660 Private
->callback
= NULL
;
661 return EFI_INVALID_PARAMETER
;
664 op
->op
= DHCP4_MESSAGE_TYPE
;
667 op
->data
[1] = DHCP4_END
;
670 op
->data
[0] = DHCP4_MESSAGE_TYPE_REQUEST
;
673 // Copy server identifier option from offer to request.
675 EfiStatus
= find_opt (Offer
, DHCP4_SERVER_IDENTIFIER
, 0, &srvid
);
677 if (EFI_ERROR (EfiStatus
)) {
678 Private
->callback
= NULL
;
679 return EFI_INVALID_PARAMETER
;
682 if (srvid
->len
!= 4) {
683 Private
->callback
= NULL
;
684 return EFI_INVALID_PARAMETER
;
687 EfiStatus
= add_opt (&request
, srvid
);
689 if (EFI_ERROR (EfiStatus
)) {
690 DebugPrint (("%a:%d:%r\n", __FILE__
, __LINE__
, EfiStatus
));
691 Private
->callback
= NULL
;
695 // Add requested IP address option to request packet.
697 op
= (DHCP4_OP
*) buf
;
698 op
->op
= DHCP4_REQUESTED_IP_ADDRESS
;
700 CopyMem (op
->data
, &Offer
->dhcp4
.yiaddr
, 4);
702 EfiStatus
= add_opt (&request
, op
);
704 if (EFI_ERROR (EfiStatus
)) {
705 DebugPrint (("%a:%d:%r\n", __FILE__
, __LINE__
, EfiStatus
));
706 Private
->callback
= NULL
;
710 // Transimit DHCP request and wait for DHCP ack...
712 SetMem (&bcast_ip
, sizeof (EFI_IP_ADDRESS
), 0xFF);
714 EfiStatus
= tx_rx_udp (
726 if (EFI_ERROR (EfiStatus
)) {
727 DebugPrint (("%a:%d:%r\n", __FILE__
, __LINE__
, EfiStatus
));
728 Private
->callback
= NULL
;
732 // Set Data->IsAck and return.
734 EfiStatus
= find_opt (&acknak
, DHCP4_MESSAGE_TYPE
, 0, &op
);
736 if (EFI_ERROR (EfiStatus
)) {
737 Private
->callback
= NULL
;
738 return EFI_DEVICE_ERROR
;
742 Private
->callback
= NULL
;
743 return EFI_DEVICE_ERROR
;
746 switch (op
->data
[0]) {
747 case DHCP4_MESSAGE_TYPE_ACK
:
748 This
->Data
->IsAck
= TRUE
;
751 case DHCP4_MESSAGE_TYPE_NAK
:
752 This
->Data
->IsAck
= FALSE
;
756 Private
->callback
= NULL
;
757 return EFI_DEVICE_ERROR
;
760 // Copy packets into instance data...
762 CopyMem (&This
->Data
->Offer
, Offer
, sizeof (DHCP4_PACKET
));
763 CopyMem (&This
->Data
->Request
, &request
, sizeof (DHCP4_PACKET
));
764 CopyMem (&This
->Data
->AckNak
, &acknak
, sizeof (DHCP4_PACKET
));
766 This
->Data
->SelectCompleted
= TRUE
;
768 Private
->callback
= NULL
;
772 /* eof - PxeDhcp4InitSelect.c */