3 Copyright (c) 2004, 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 // #define DebugPrint(x) Aprint x
28 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36 IN PXE_DHCP4_PRIVATE_DATA
*Private
,
37 IN DHCP4_PACKET
*tx_pkt
,
38 IN DHCP4_PACKET
*rx_pkt
,
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 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
152 IN PXE_DHCP4_PRIVATE_DATA
*Private
,
153 IN DHCP4_PACKET
*tx_pkt
,
154 IN DHCP4_PACKET
*rx_pkt
,
158 EFI_STATUS EfiStatus
;
159 DHCP4_OP
*msg_type_op
;
163 DHCP4_OP
*lease_time_op
;
167 // Verify parameters. Touch unused parameters to
168 // keep compiler happy.
173 if (Private
== NULL
|| rx_pkt
== NULL
) {
178 rx_pkt_size
= rx_pkt_size
;
181 // This must be a DHCP Ack message.
183 magik
= htonl (DHCP4_MAGIK_NUMBER
);
185 if (CompareMem (&rx_pkt
->dhcp4
.magik
, &magik
, 4)) {
189 EfiStatus
= find_opt (rx_pkt
, DHCP4_MESSAGE_TYPE
, 0, &msg_type_op
);
191 if (EFI_ERROR (EfiStatus
)) {
195 if (msg_type_op
->len
!= 1) {
199 if (msg_type_op
->data
[0] != DHCP4_MESSAGE_TYPE_ACK
) {
203 // There must be a server identifier.
205 EfiStatus
= find_opt (rx_pkt
, DHCP4_SERVER_IDENTIFIER
, 0, &srvid_op
);
207 if (EFI_ERROR (EfiStatus
)) {
211 if (srvid_op
->len
!= 4) {
215 // There should be a renewal time.
216 // If there is not, we will default to the 7/8 of the rebinding time.
218 EfiStatus
= find_opt (rx_pkt
, DHCP4_RENEWAL_TIME
, 0, &renew_op
);
220 if (EFI_ERROR (EfiStatus
)) {
222 } else if (renew_op
->len
!= 4) {
226 // There should be a rebinding time.
227 // If there is not, we will default to 7/8 of the lease time.
229 EfiStatus
= find_opt (rx_pkt
, DHCP4_REBINDING_TIME
, 0, &rebind_op
);
231 if (EFI_ERROR (EfiStatus
)) {
233 } else if (rebind_op
->len
!= 4) {
237 // There should be a lease time.
238 // If there is not, we will default to one week.
240 EfiStatus
= find_opt (rx_pkt
, DHCP4_LEASE_TIME
, 0, &lease_time_op
);
242 if (EFI_ERROR (EfiStatus
)) {
243 lease_time_op
= NULL
;
244 } else if (lease_time_op
->len
!= 4) {
245 lease_time_op
= NULL
;
248 // Packet looks good. Double check the renew, rebind and lease times.
250 CopyMem (&Private
->ServerIp
, srvid_op
->data
, 4);
252 if (renew_op
!= NULL
) {
253 CopyMem (&Private
->RenewTime
, renew_op
->data
, 4);
254 Private
->RenewTime
= htonl (Private
->RenewTime
);
256 Private
->RenewTime
= 0;
259 if (rebind_op
!= NULL
) {
260 CopyMem (&Private
->RebindTime
, rebind_op
->data
, 4);
261 Private
->RebindTime
= htonl (Private
->RebindTime
);
263 Private
->RebindTime
= 0;
266 if (lease_time_op
!= NULL
) {
267 CopyMem (&Private
->LeaseTime
, lease_time_op
->data
, 4);
268 Private
->LeaseTime
= htonl (Private
->LeaseTime
);
270 Private
->LeaseTime
= 0;
273 if (Private
->LeaseTime
< 60) {
274 Private
->LeaseTime
= 7 * 86400;
277 if (Private
->RebindTime
< 52 || Private
->RebindTime
>= Private
->LeaseTime
) {
278 Private
->RebindTime
= Private
->LeaseTime
/ 2 + Private
->LeaseTime
/ 4 + Private
->LeaseTime
/ 8;
281 if (Private
->RenewTime
< 45 || Private
->RenewTime
>= Private
->RebindTime
) {
282 Private
->RenewTime
= Private
->RebindTime
/ 2 + Private
->RebindTime
/ 4 + Private
->RebindTime
/ 8;
288 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
292 IN EFI_PXE_DHCP4_PROTOCOL
*This
,
293 IN UINTN seconds_timeout
,
295 OUT DHCP4_PACKET
**OfferList
298 PXE_DHCP4_PRIVATE_DATA
*Private
;
300 EFI_IP_ADDRESS bcast_ip
;
301 EFI_STATUS EfiStatus
;
304 // Verify parameters and protocol state.
307 seconds_timeout
< DHCP4_MIN_SECONDS
||
308 seconds_timeout
> DHCP4_MAX_SECONDS
||
313 // Return parameters are not initialized when
314 // parameters are invalid!
316 return EFI_INVALID_PARAMETER
;
323 // Check protocol state.
325 if (This
->Data
== NULL
) {
326 return EFI_NOT_STARTED
;
329 if (!This
->Data
->SetupCompleted
) {
330 return EFI_NOT_READY
;
334 // Get pointer to our instance data.
336 Private
= PXE_DHCP4_PRIVATE_DATA_FROM_THIS (This
);
338 if (Private
== NULL
) {
339 return EFI_INVALID_PARAMETER
;
342 if (Private
->PxeBc
== NULL
) {
343 return EFI_DEVICE_ERROR
;
346 // Setup variables...
349 Private
->offer_list
= NULL
;
351 EfiStatus
= gBS
->HandleProtocol (
353 &gEfiPxeDhcp4CallbackProtocolGuid
,
354 (VOID
*) &Private
->callback
357 if (EFI_ERROR (EfiStatus
)) {
358 Private
->callback
= NULL
;
361 Private
->function
= EFI_PXE_DHCP4_FUNCTION_INIT
;
364 // Increment the transaction ID.
369 CopyMem (&xid
, &This
->Data
->Discover
.dhcp4
.xid
, sizeof (UINT32
));
371 xid
= htonl (htonl (xid
) + 1);
373 CopyMem (&This
->Data
->Discover
.dhcp4
.xid
, &xid
, sizeof (UINT32
));
376 // Transmit discover and wait for offers...
378 SetMem (&bcast_ip
, sizeof (EFI_IP_ADDRESS
), 0xFF);
380 EfiStatus
= tx_rx_udp (
386 &This
->Data
->Discover
,
392 if (EFI_ERROR (EfiStatus
)) {
393 if (Private
->offer_list
) {
394 gBS
->FreePool (Private
->offer_list
);
398 Private
->offer_list
= NULL
;
399 Private
->callback
= NULL
;
401 DebugPrint (("%a:%d:%r\n", __FILE__
, __LINE__
, EfiStatus
));
405 *Offers
= Private
->offers
;
406 *OfferList
= Private
->offer_list
;
409 Private
->offer_list
= NULL
;
410 Private
->callback
= NULL
;
412 This
->Data
->InitCompleted
= TRUE
;
413 This
->Data
->SelectCompleted
= FALSE
;
414 This
->Data
->IsBootp
= FALSE
;
415 This
->Data
->IsAck
= FALSE
;
420 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
424 IN EFI_PXE_DHCP4_PROTOCOL
*This
,
425 IN UINTN seconds_timeout
,
426 IN DHCP4_PACKET
*Offer
429 PXE_DHCP4_PRIVATE_DATA
*Private
;
430 EFI_STATUS EfiStatus
;
431 DHCP4_PACKET request
;
433 EFI_IP_ADDRESS bcast_ip
;
434 EFI_IP_ADDRESS zero_ip
;
435 EFI_IP_ADDRESS local_ip
;
443 // Verify parameters.
445 if (This
== NULL
|| seconds_timeout
< DHCP4_MIN_SECONDS
|| seconds_timeout
> DHCP4_MAX_SECONDS
|| Offer
== NULL
) {
446 return EFI_INVALID_PARAMETER
;
449 // Check protocol state.
451 if (This
->Data
== NULL
) {
452 return EFI_NOT_STARTED
;
455 if (!This
->Data
->SetupCompleted
) {
456 return EFI_NOT_READY
;
459 // Get pointer to instance data.
461 Private
= PXE_DHCP4_PRIVATE_DATA_FROM_THIS (This
);
463 if (Private
== NULL
) {
464 return EFI_INVALID_PARAMETER
;
467 if (Private
->PxeBc
== NULL
) {
468 return EFI_DEVICE_ERROR
;
472 // Setup useful variables...
474 SetMem (&bcast_ip
, sizeof (EFI_IP_ADDRESS
), 0xFF);
476 ZeroMem (&zero_ip
, sizeof (EFI_IP_ADDRESS
));
478 ZeroMem (&local_ip
, sizeof (EFI_IP_ADDRESS
));
479 local_ip
.v4
.Addr
[0] = 127;
480 local_ip
.v4
.Addr
[3] = 1;
482 This
->Data
->SelectCompleted
= FALSE
;
483 This
->Data
->IsBootp
= FALSE
;
484 This
->Data
->IsAck
= FALSE
;
486 EfiStatus
= gBS
->HandleProtocol (
488 &gEfiPxeDhcp4CallbackProtocolGuid
,
489 (VOID
*) &Private
->callback
492 if (EFI_ERROR (EfiStatus
)) {
493 Private
->callback
= NULL
;
496 Private
->function
= EFI_PXE_DHCP4_FUNCTION_SELECT
;
499 // Verify offer packet fields.
501 if (Offer
->dhcp4
.op
!= BOOTP_REPLY
) {
502 Private
->callback
= NULL
;
503 return EFI_INVALID_PARAMETER
;
506 if (Offer
->dhcp4
.htype
!= This
->Data
->Discover
.dhcp4
.htype
) {
507 Private
->callback
= NULL
;
508 return EFI_INVALID_PARAMETER
;
511 if (Offer
->dhcp4
.hlen
!= This
->Data
->Discover
.dhcp4
.hlen
) {
512 Private
->callback
= NULL
;
513 return EFI_INVALID_PARAMETER
;
516 if (CompareMem (&Offer
->dhcp4
.xid
, &This
->Data
->Discover
.dhcp4
.xid
, 4)) {
517 Private
->callback
= NULL
;
518 return EFI_INVALID_PARAMETER
;
521 if (!CompareMem (&Offer
->dhcp4
.yiaddr
, &bcast_ip
, 4)) {
522 Private
->callback
= NULL
;
523 return EFI_INVALID_PARAMETER
;
526 if (!CompareMem (&Offer
->dhcp4
.yiaddr
, &zero_ip
, 4)) {
527 Private
->callback
= NULL
;
528 return EFI_INVALID_PARAMETER
;
531 if (!CompareMem (&Offer
->dhcp4
.yiaddr
, &local_ip
, 4)) {
532 Private
->callback
= NULL
;
533 return EFI_INVALID_PARAMETER
;
537 &Offer
->dhcp4
.chaddr
,
538 &This
->Data
->Discover
.dhcp4
.chaddr
,
541 Private
->callback
= NULL
;
542 return EFI_INVALID_PARAMETER
;
545 // DHCP option checks
547 dhcp4_magik
= htonl (DHCP4_MAGIK_NUMBER
);
550 if (!CompareMem (&Offer
->dhcp4
.magik
, &dhcp4_magik
, 4)) {
552 // If present, DHCP message type must be offer.
554 EfiStatus
= find_opt (Offer
, DHCP4_MESSAGE_TYPE
, 0, &op
);
556 if (!EFI_ERROR (EfiStatus
)) {
557 if (op
->len
!= 1 || op
->data
[0] != DHCP4_MESSAGE_TYPE_OFFER
) {
558 Private
->callback
= NULL
;
559 return EFI_INVALID_PARAMETER
;
565 // If present, DHCP max message size must be valid.
567 EfiStatus
= find_opt (Offer
, DHCP4_MAX_MESSAGE_SIZE
, 0, &op
);
569 if (!EFI_ERROR (EfiStatus
)) {
570 if (op
->len
!= 2 || ((op
->data
[0] << 8) | op
->data
[1]) < DHCP4_DEFAULT_MAX_MESSAGE_SIZE
) {
571 Private
->callback
= NULL
;
572 return EFI_INVALID_PARAMETER
;
576 // If present, DHCP server identifier must be valid.
578 EfiStatus
= find_opt (Offer
, DHCP4_SERVER_IDENTIFIER
, 0, &op
);
580 if (!EFI_ERROR (EfiStatus
)) {
581 if (op
->len
!= 4 || !CompareMem (op
->data
, &bcast_ip
, 4) || !CompareMem (op
->data
, &zero_ip
, 4)) {
582 Private
->callback
= NULL
;
583 return EFI_INVALID_PARAMETER
;
587 // If present, DHCP subnet mask must be valid.
589 EfiStatus
= find_opt (
596 if (!EFI_ERROR (EfiStatus
)) {
598 Private
->callback
= NULL
;
599 return EFI_INVALID_PARAMETER
;
604 // Early out for BOOTP.
606 This
->Data
->IsBootp
= is_bootp
;
609 // Copy offer packet to instance data.
611 CopyMem (&This
->Data
->Offer
, Offer
, sizeof (DHCP4_PACKET
));
614 // Copy discover to request and offer to acknak.
617 &This
->Data
->Request
,
618 &This
->Data
->Discover
,
619 sizeof (DHCP4_PACKET
)
625 sizeof (DHCP4_PACKET
)
631 This
->Data
->SelectCompleted
= TRUE
;
632 This
->Data
->IsAck
= TRUE
;
634 Private
->callback
= NULL
;
638 // Copy discover packet contents to request packet.
640 CopyMem (&request
, &This
->Data
->Discover
, sizeof (DHCP4_PACKET
));
642 This
->Data
->IsAck
= FALSE
;
645 // Change DHCP message type from discover to request.
647 EfiStatus
= find_opt (&request
, DHCP4_MESSAGE_TYPE
, 0, &op
);
649 if (EFI_ERROR (EfiStatus
) && EfiStatus
!= EFI_NOT_FOUND
) {
650 Private
->callback
= NULL
;
651 return EFI_INVALID_PARAMETER
;
654 if (EfiStatus
== EFI_NOT_FOUND
) {
655 EfiStatus
= find_opt (&request
, DHCP4_END
, 0, &op
);
657 if (EFI_ERROR (EfiStatus
)) {
658 Private
->callback
= NULL
;
659 return EFI_INVALID_PARAMETER
;
662 op
->op
= DHCP4_MESSAGE_TYPE
;
665 op
->data
[1] = DHCP4_END
;
668 op
->data
[0] = DHCP4_MESSAGE_TYPE_REQUEST
;
671 // Copy server identifier option from offer to request.
673 EfiStatus
= find_opt (Offer
, DHCP4_SERVER_IDENTIFIER
, 0, &srvid
);
675 if (EFI_ERROR (EfiStatus
)) {
676 Private
->callback
= NULL
;
677 return EFI_INVALID_PARAMETER
;
680 if (srvid
->len
!= 4) {
681 Private
->callback
= NULL
;
682 return EFI_INVALID_PARAMETER
;
685 EfiStatus
= add_opt (&request
, srvid
);
687 if (EFI_ERROR (EfiStatus
)) {
688 DebugPrint (("%a:%d:%r\n", __FILE__
, __LINE__
, EfiStatus
));
689 Private
->callback
= NULL
;
693 // Add requested IP address option to request packet.
695 op
= (DHCP4_OP
*) buf
;
696 op
->op
= DHCP4_REQUESTED_IP_ADDRESS
;
698 CopyMem (op
->data
, &Offer
->dhcp4
.yiaddr
, 4);
700 EfiStatus
= add_opt (&request
, op
);
702 if (EFI_ERROR (EfiStatus
)) {
703 DebugPrint (("%a:%d:%r\n", __FILE__
, __LINE__
, EfiStatus
));
704 Private
->callback
= NULL
;
708 // Transimit DHCP request and wait for DHCP ack...
710 SetMem (&bcast_ip
, sizeof (EFI_IP_ADDRESS
), 0xFF);
712 EfiStatus
= tx_rx_udp (
724 if (EFI_ERROR (EfiStatus
)) {
725 DebugPrint (("%a:%d:%r\n", __FILE__
, __LINE__
, EfiStatus
));
726 Private
->callback
= NULL
;
730 // Set Data->IsAck and return.
732 EfiStatus
= find_opt (&acknak
, DHCP4_MESSAGE_TYPE
, 0, &op
);
734 if (EFI_ERROR (EfiStatus
)) {
735 Private
->callback
= NULL
;
736 return EFI_DEVICE_ERROR
;
740 Private
->callback
= NULL
;
741 return EFI_DEVICE_ERROR
;
744 switch (op
->data
[0]) {
745 case DHCP4_MESSAGE_TYPE_ACK
:
746 This
->Data
->IsAck
= TRUE
;
749 case DHCP4_MESSAGE_TYPE_NAK
:
750 This
->Data
->IsAck
= FALSE
;
754 Private
->callback
= NULL
;
755 return EFI_DEVICE_ERROR
;
758 // Copy packets into instance data...
760 CopyMem (&This
->Data
->Offer
, Offer
, sizeof (DHCP4_PACKET
));
761 CopyMem (&This
->Data
->Request
, &request
, sizeof (DHCP4_PACKET
));
762 CopyMem (&This
->Data
->AckNak
, &acknak
, sizeof (DHCP4_PACKET
));
764 This
->Data
->SelectCompleted
= TRUE
;
766 Private
->callback
= NULL
;
770 /* eof - PxeDhcp4InitSelect.c */