2 EFI DHCP protocol implementation.
4 Copyright (c) 2006 - 2009, Intel Corporation.<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "Dhcp4Impl.h"
18 UINT32 mDhcp4DefaultTimeout
[4] = { 4, 8, 16, 32 };
22 Send an initial DISCOVER or REQUEST message according to the
23 DHCP service's current state.
25 @param[in] DhcpSb The DHCP service instance
27 @retval EFI_SUCCESS The request has been sent
28 @retval other Some error occurs when sending the request.
33 IN DHCP_SERVICE
*DhcpSb
38 ASSERT ((DhcpSb
->DhcpState
== Dhcp4Init
) || (DhcpSb
->DhcpState
== Dhcp4InitReboot
));
40 if (DhcpSb
->DhcpState
== Dhcp4Init
) {
41 DhcpSetState (DhcpSb
, Dhcp4Selecting
, FALSE
);
42 Status
= DhcpSendMessage (DhcpSb
, NULL
, NULL
, DHCP_MSG_DISCOVER
, NULL
);
44 if (EFI_ERROR (Status
)) {
45 DhcpSb
->DhcpState
= Dhcp4Init
;
49 DhcpSetState (DhcpSb
, Dhcp4Rebooting
, FALSE
);
50 Status
= DhcpSendMessage (DhcpSb
, NULL
, NULL
, DHCP_MSG_REQUEST
, NULL
);
52 if (EFI_ERROR (Status
)) {
53 DhcpSb
->DhcpState
= Dhcp4InitReboot
;
63 Call user provided callback function, and return the value the
64 function returns. If the user doesn't provide a callback, a
65 proper return value is selected to let the caller continue the
68 @param[in] DhcpSb The DHCP service instance
69 @param[in] Event The event as defined in the spec
70 @param[in] Packet The current packet trigger the event
71 @param[out] NewPacket The user's return new packet
73 @retval EFI_NOT_READY Direct the caller to continue collecting the offer.
74 @retval EFI_SUCCESS The user function returns success.
75 @retval EFI_ABORTED The user function ask it to abort.
80 IN DHCP_SERVICE
*DhcpSb
,
81 IN EFI_DHCP4_EVENT Event
,
82 IN EFI_DHCP4_PACKET
*Packet
, OPTIONAL
83 OUT EFI_DHCP4_PACKET
**NewPacket OPTIONAL
86 EFI_DHCP4_CONFIG_DATA
*Config
;
89 if (NewPacket
!= NULL
) {
94 // If user doesn't provide the call back function, return the value
95 // that directs the client to continue the normal process.
96 // In Dhcp4Selecting EFI_SUCCESS tells the client to stop collecting
97 // the offers and select a offer, EFI_NOT_READY tells the client to
98 // collect more offers.
100 Config
= &DhcpSb
->ActiveConfig
;
102 if (Config
->Dhcp4Callback
== NULL
) {
103 if (Event
== Dhcp4RcvdOffer
) {
104 return EFI_NOT_READY
;
110 Status
= Config
->Dhcp4Callback (
111 &DhcpSb
->ActiveChild
->Dhcp4Protocol
,
112 Config
->CallbackContext
,
113 (EFI_DHCP4_STATE
) DhcpSb
->DhcpState
,
120 // User's callback should only return EFI_SUCCESS, EFI_NOT_READY,
121 // and EFI_ABORTED. If it returns values other than those, assume
122 // it to be EFI_ABORTED.
124 if ((Status
== EFI_SUCCESS
) || (Status
== EFI_NOT_READY
)) {
133 Notify the user about the operation result.
135 @param DhcpSb DHCP service instance
136 @param Which Which notify function to signal
141 IN DHCP_SERVICE
*DhcpSb
,
145 DHCP_PROTOCOL
*Child
;
147 if ((Child
= DhcpSb
->ActiveChild
) == NULL
) {
151 if ((Child
->CompletionEvent
!= NULL
) &&
152 ((Which
== DHCP_NOTIFY_COMPLETION
) || (Which
== DHCP_NOTIFY_ALL
))
155 gBS
->SignalEvent (Child
->CompletionEvent
);
156 Child
->CompletionEvent
= NULL
;
159 if ((Child
->RenewRebindEvent
!= NULL
) &&
160 ((Which
== DHCP_NOTIFY_RENEWREBIND
) || (Which
== DHCP_NOTIFY_ALL
))
163 gBS
->SignalEvent (Child
->RenewRebindEvent
);
164 Child
->RenewRebindEvent
= NULL
;
171 Set the DHCP state. If CallUser is true, it will try to notify
172 the user before change the state by DhcpNotifyUser. It returns
173 EFI_ABORTED if the user return EFI_ABORTED, otherwise, it returns
174 EFI_SUCCESS. If CallUser is FALSE, it isn't necessary to test
175 the return value of this function.
177 @param DhcpSb The DHCP service instance
178 @param State The new DHCP state to change to
179 @param CallUser Whether we need to call user
181 @retval EFI_SUCCESS The state is changed
182 @retval EFI_ABORTED The user asks to abort the DHCP process.
187 IN OUT DHCP_SERVICE
*DhcpSb
,
195 Status
= EFI_SUCCESS
;
197 if (State
== Dhcp4Renewing
) {
198 Status
= DhcpCallUser (DhcpSb
, Dhcp4EnterRenewing
, NULL
, NULL
);
200 } else if (State
== Dhcp4Rebinding
) {
201 Status
= DhcpCallUser (DhcpSb
, Dhcp4EnterRebinding
, NULL
, NULL
);
203 } else if (State
== Dhcp4Bound
) {
204 Status
= DhcpCallUser (DhcpSb
, Dhcp4BoundCompleted
, NULL
, NULL
);
208 if (EFI_ERROR (Status
)) {
214 // Update the retransmission timer during the state transition.
215 // This will clear the retry count. This is also why the rule
216 // first transit the state, then send packets.
218 if (State
== Dhcp4Selecting
) {
219 DhcpSb
->MaxRetries
= DhcpSb
->ActiveConfig
.DiscoverTryCount
;
221 DhcpSb
->MaxRetries
= DhcpSb
->ActiveConfig
.RequestTryCount
;
224 if (DhcpSb
->MaxRetries
== 0) {
225 DhcpSb
->MaxRetries
= 4;
228 DhcpSb
->CurRetry
= 0;
229 DhcpSb
->PacketToLive
= 0;
230 DhcpSb
->LastTimeout
= 0;
231 DhcpSb
->DhcpState
= State
;
237 Set the retransmit timer for the packet. It will select from either
238 the discover timeouts/request timeouts or the default timeout values.
240 @param DhcpSb The DHCP service instance.
244 DhcpSetTransmitTimer (
245 IN OUT DHCP_SERVICE
*DhcpSb
250 ASSERT (DhcpSb
->MaxRetries
> DhcpSb
->CurRetry
);
252 if (DhcpSb
->DhcpState
== Dhcp4Selecting
) {
253 Times
= DhcpSb
->ActiveConfig
.DiscoverTimeout
;
255 Times
= DhcpSb
->ActiveConfig
.RequestTimeout
;
259 Times
= mDhcp4DefaultTimeout
;
262 DhcpSb
->PacketToLive
= Times
[DhcpSb
->CurRetry
];
263 DhcpSb
->LastTimeout
= DhcpSb
->PacketToLive
;
269 Compute the lease. If the server grants a permanent lease, just
270 process it as a normal timeout value since the lease will last
273 @param DhcpSb The DHCP service instance
274 @param Para The DHCP parameter extracted from the server's
279 IN OUT DHCP_SERVICE
*DhcpSb
,
280 IN DHCP_PARAMETER
*Para
283 ASSERT (Para
!= NULL
);
285 DhcpSb
->Lease
= Para
->Lease
;
286 DhcpSb
->T2
= Para
->T2
;
287 DhcpSb
->T1
= Para
->T1
;
289 if (DhcpSb
->Lease
== 0) {
290 DhcpSb
->Lease
= DHCP_DEFAULT_LEASE
;
293 if ((DhcpSb
->T2
== 0) || (DhcpSb
->T2
>= Para
->Lease
)) {
294 DhcpSb
->T2
= Para
->Lease
- (Para
->Lease
>> 3);
297 if ((DhcpSb
->T1
== 0) || (DhcpSb
->T1
>= Para
->T2
)) {
298 DhcpSb
->T1
= DhcpSb
->Lease
>> 1;
304 Configure a UDP IO port to use the acquired lease address.
305 DHCP driver needs this port to unicast packet to the server
306 such as DHCP release.
308 @param[in] UdpIo The UDP IO port to configure
309 @param[in] Context Dhcp service instance.
311 @retval EFI_SUCCESS The UDP IO port is successfully configured.
312 @retval Others It failed to configure the port.
316 DhcpConfigLeaseIoPort (
317 IN UDP_IO_PORT
*UdpIo
,
321 EFI_UDP4_CONFIG_DATA UdpConfigData
;
322 EFI_IPv4_ADDRESS Subnet
;
323 EFI_IPv4_ADDRESS Gateway
;
324 DHCP_SERVICE
*DhcpSb
;
328 DhcpSb
= (DHCP_SERVICE
*) Context
;
330 UdpConfigData
.AcceptBroadcast
= FALSE
;
331 UdpConfigData
.AcceptPromiscuous
= FALSE
;
332 UdpConfigData
.AcceptAnyPort
= FALSE
;
333 UdpConfigData
.AllowDuplicatePort
= TRUE
;
334 UdpConfigData
.TypeOfService
= 0;
335 UdpConfigData
.TimeToLive
= 64;
336 UdpConfigData
.DoNotFragment
= FALSE
;
337 UdpConfigData
.ReceiveTimeout
= 1;
338 UdpConfigData
.TransmitTimeout
= 0;
340 UdpConfigData
.UseDefaultAddress
= FALSE
;
341 UdpConfigData
.StationPort
= DHCP_CLIENT_PORT
;
342 UdpConfigData
.RemotePort
= DHCP_SERVER_PORT
;
344 Ip
= HTONL (DhcpSb
->ClientAddr
);
345 CopyMem (&UdpConfigData
.StationAddress
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
347 Ip
= HTONL (DhcpSb
->Netmask
);
348 CopyMem (&UdpConfigData
.SubnetMask
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
350 ZeroMem (&UdpConfigData
.RemoteAddress
, sizeof (EFI_IPv4_ADDRESS
));
352 Status
= UdpIo
->Udp
->Configure (UdpIo
->Udp
, &UdpConfigData
);
354 if (EFI_ERROR (Status
)) {
359 // Add a default route if received from the server.
361 if ((DhcpSb
->Para
!= NULL
) && (DhcpSb
->Para
->Router
!= 0)) {
362 ZeroMem (&Subnet
, sizeof (EFI_IPv4_ADDRESS
));
364 Ip
= HTONL (DhcpSb
->Para
->Router
);
365 CopyMem (&Gateway
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
367 UdpIo
->Udp
->Routes (UdpIo
->Udp
, FALSE
, &Subnet
, &Subnet
, &Gateway
);
375 Update the lease states when a new lease is acquired. It will not only
376 save the acquired the address and lease time, it will also create a UDP
377 child to provide address resolution for the address.
379 @param DhcpSb The DHCP service instance
381 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
382 @retval EFI_SUCCESS The lease is recorded.
387 IN OUT DHCP_SERVICE
*DhcpSb
392 DhcpSb
->ClientAddr
= EFI_NTOHL (DhcpSb
->Selected
->Dhcp4
.Header
.YourAddr
);
394 if (DhcpSb
->Para
!= NULL
) {
395 DhcpSb
->Netmask
= DhcpSb
->Para
->NetMask
;
396 DhcpSb
->ServerAddr
= DhcpSb
->Para
->ServerId
;
399 if (DhcpSb
->Netmask
== 0) {
400 Class
= NetGetIpClass (DhcpSb
->ClientAddr
);
401 DhcpSb
->Netmask
= gIp4AllMasks
[Class
<< 3];
404 if (DhcpSb
->LeaseIoPort
!= NULL
) {
405 UdpIoFreePort (DhcpSb
->LeaseIoPort
);
409 // Create a UDP/IP child to provide ARP service for the Leased IP,
410 // and transmit unicast packet with it as source address. Don't
411 // start receive on this port, the queued packet will be timeout.
413 DhcpSb
->LeaseIoPort
= UdpIoCreatePort (
416 DhcpConfigLeaseIoPort
,
420 if (DhcpSb
->LeaseIoPort
== NULL
) {
421 return EFI_OUT_OF_RESOURCES
;
424 if (!DHCP_IS_BOOTP (DhcpSb
->Para
)) {
425 DhcpComputeLease (DhcpSb
, DhcpSb
->Para
);
428 return DhcpSetState (DhcpSb
, Dhcp4Bound
, TRUE
);
433 Clean up the DHCP related states, IoStatus isn't reset.
435 @param DhcpSb The DHCP instance service.
440 IN DHCP_SERVICE
*DhcpSb
443 DhcpSb
->DhcpState
= Dhcp4Init
;
444 DhcpSb
->Xid
= DhcpSb
->Xid
+ 1;
445 DhcpSb
->ClientAddr
= 0;
446 DhcpSb
->ServerAddr
= 0;
448 if (DhcpSb
->LastOffer
!= NULL
) {
449 FreePool (DhcpSb
->LastOffer
);
450 DhcpSb
->LastOffer
= NULL
;
453 if (DhcpSb
->Selected
!= NULL
) {
454 FreePool (DhcpSb
->Selected
);
455 DhcpSb
->Selected
= NULL
;
458 if (DhcpSb
->Para
!= NULL
) {
459 FreePool (DhcpSb
->Para
);
466 DhcpSb
->ExtraRefresh
= FALSE
;
468 if (DhcpSb
->LeaseIoPort
!= NULL
) {
469 UdpIoFreePort (DhcpSb
->LeaseIoPort
);
470 DhcpSb
->LeaseIoPort
= NULL
;
473 if (DhcpSb
->LastPacket
!= NULL
) {
474 FreePool (DhcpSb
->LastPacket
);
475 DhcpSb
->LastPacket
= NULL
;
478 DhcpSb
->PacketToLive
= 0;
479 DhcpSb
->LastTimeout
= 0;
480 DhcpSb
->CurRetry
= 0;
481 DhcpSb
->MaxRetries
= 0;
482 DhcpSb
->LeaseLife
= 0;
487 Select a offer among all the offers collected. If the offer selected is
488 of BOOTP, the lease is recorded and user notified. If the offer is of
489 DHCP, it will request the offer from the server.
491 @param[in] DhcpSb The DHCP service instance.
493 @retval EFI_SUCCESS One of the offer is selected.
498 IN DHCP_SERVICE
*DhcpSb
501 EFI_DHCP4_PACKET
*Selected
;
502 EFI_DHCP4_PACKET
*NewPacket
;
503 EFI_DHCP4_PACKET
*TempPacket
;
506 ASSERT (DhcpSb
->LastOffer
!= NULL
);
509 // User will cache previous offers if he wants to select
510 // from multiple offers. If user provides an invalid packet,
511 // use the last offer, otherwise use the provided packet.
514 Status
= DhcpCallUser (DhcpSb
, Dhcp4SelectOffer
, DhcpSb
->LastOffer
, &NewPacket
);
516 if (EFI_ERROR (Status
)) {
520 Selected
= DhcpSb
->LastOffer
;
522 if ((NewPacket
!= NULL
) && !EFI_ERROR (DhcpValidateOptions (NewPacket
, NULL
))) {
523 TempPacket
= (EFI_DHCP4_PACKET
*) AllocatePool (NewPacket
->Size
);
524 if (TempPacket
!= NULL
) {
525 CopyMem (TempPacket
, NewPacket
, NewPacket
->Size
);
526 gBS
->FreePool (Selected
);
527 Selected
= TempPacket
;
531 DhcpSb
->Selected
= Selected
;
532 DhcpSb
->LastOffer
= NULL
;
534 DhcpValidateOptions (Selected
, &DhcpSb
->Para
);
537 // A bootp offer has been selected, save the lease status,
538 // enter bound state then notify the user.
540 if (DHCP_IS_BOOTP (DhcpSb
->Para
)) {
541 Status
= DhcpLeaseAcquired (DhcpSb
);
543 if (EFI_ERROR (Status
)) {
547 DhcpSb
->IoStatus
= EFI_SUCCESS
;
548 DhcpNotifyUser (DhcpSb
, DHCP_NOTIFY_ALL
);
553 // Send a DHCP requests
555 Status
= DhcpSetState (DhcpSb
, Dhcp4Requesting
, TRUE
);
557 if (EFI_ERROR (Status
)) {
561 return DhcpSendMessage (DhcpSb
, Selected
, DhcpSb
->Para
, DHCP_MSG_REQUEST
, NULL
);
566 Terminate the current address acquire. All the allocated resources
567 are released. Be careful when calling this function. A rule related
568 to this is: only call DhcpEndSession at the highest level, such as
569 DhcpInput, DhcpOnTimerTick...At the other level, just return error.
571 @param[in] DhcpSb The DHCP service instance
572 @param[in] Status The result of the DHCP process.
577 IN DHCP_SERVICE
*DhcpSb
,
581 if (DHCP_CONNECTED (DhcpSb
->DhcpState
)) {
582 DhcpCallUser (DhcpSb
, Dhcp4AddressLost
, NULL
, NULL
);
584 DhcpCallUser (DhcpSb
, Dhcp4Fail
, NULL
, NULL
);
587 DhcpCleanLease (DhcpSb
);
589 DhcpSb
->IoStatus
= Status
;
590 DhcpNotifyUser (DhcpSb
, DHCP_NOTIFY_ALL
);
595 Handle packets in DHCP select state.
597 @param[in] DhcpSb The DHCP service instance
598 @param[in] Packet The DHCP packet received
599 @param[in] Para The DHCP parameter extracted from the packet. That
600 is, all the option value that we care.
602 @retval EFI_SUCCESS The packet is successfully processed.
603 @retval Others Some error occured.
608 IN DHCP_SERVICE
*DhcpSb
,
609 IN EFI_DHCP4_PACKET
*Packet
,
610 IN DHCP_PARAMETER
*Para
615 Status
= EFI_SUCCESS
;
618 // First validate the message:
619 // 1. the offer is a unicast
620 // 2. if it is a DHCP message, it must contains a server ID.
621 // Don't return a error for these two case otherwise the session is ended.
623 if (!DHCP_IS_BOOTP (Para
) &&
624 ((Para
->DhcpType
!= DHCP_MSG_OFFER
) || (Para
->ServerId
== 0))
630 // Call the user's callback. The action according to the return is as:
631 // 1. EFI_SUCESS: stop waiting for more offers, select the offer now
632 // 2. EFI_NOT_READY: wait for more offers
633 // 3. EFI_ABORTED: abort the address acquiring.
635 Status
= DhcpCallUser (DhcpSb
, Dhcp4RcvdOffer
, Packet
, NULL
);
637 if (Status
== EFI_SUCCESS
) {
638 if (DhcpSb
->LastOffer
!= NULL
) {
639 FreePool (DhcpSb
->LastOffer
);
642 DhcpSb
->LastOffer
= Packet
;
644 return DhcpChooseOffer (DhcpSb
);
646 } else if (Status
== EFI_NOT_READY
) {
647 if (DhcpSb
->LastOffer
!= NULL
) {
648 FreePool (DhcpSb
->LastOffer
);
651 DhcpSb
->LastOffer
= Packet
;
653 } else if (Status
== EFI_ABORTED
) {
655 // DhcpInput will end the session upon error return. Remember
656 // only to call DhcpEndSession at the top level call.
664 gBS
->FreePool (Packet
);
670 Handle packets in DHCP request state.
672 @param[in] DhcpSb The DHCP service instance
673 @param[in] Packet The DHCP packet received
674 @param[in] Para The DHCP parameter extracted from the packet. That
675 is, all the option value that we care.
677 @retval EFI_SUCCESS The packet is successfully processed.
678 @retval Others Some error occured.
683 IN DHCP_SERVICE
*DhcpSb
,
684 IN EFI_DHCP4_PACKET
*Packet
,
685 IN DHCP_PARAMETER
*Para
688 EFI_DHCP4_HEADER
*Head
;
689 EFI_DHCP4_HEADER
*Selected
;
693 ASSERT (!DHCP_IS_BOOTP (DhcpSb
->Para
));
695 Head
= &Packet
->Dhcp4
.Header
;
696 Selected
= &DhcpSb
->Selected
->Dhcp4
.Header
;
699 // Ignore the BOOTP message and DHCP messages other than DHCP ACK/NACK.
701 if (DHCP_IS_BOOTP (Para
) ||
702 (Para
->ServerId
!= DhcpSb
->Para
->ServerId
) ||
703 ((Para
->DhcpType
!= DHCP_MSG_ACK
) && (Para
->DhcpType
!= DHCP_MSG_NAK
))
706 Status
= EFI_SUCCESS
;
711 // Received a NAK, end the session no matter what the user returns
713 Status
= EFI_DEVICE_ERROR
;
715 if (Para
->DhcpType
== DHCP_MSG_NAK
) {
716 DhcpCallUser (DhcpSb
, Dhcp4RcvdNak
, Packet
, NULL
);
721 // Check whether the ACK matches the selected offer
725 if (!EFI_IP4_EQUAL (&Head
->YourAddr
, &Selected
->YourAddr
)) {
726 Message
= (UINT8
*) "Lease confirmed isn't the same as that in the offer";
730 Status
= DhcpCallUser (DhcpSb
, Dhcp4RcvdAck
, Packet
, NULL
);
732 if (EFI_ERROR (Status
)) {
733 Message
= (UINT8
*) "Lease is denied upon received ACK";
738 // Record the lease, transit to BOUND state, then notify the user
740 Status
= DhcpLeaseAcquired (DhcpSb
);
742 if (EFI_ERROR (Status
)) {
743 Message
= (UINT8
*) "Lease is denied upon entering bound";
747 DhcpSb
->IoStatus
= EFI_SUCCESS
;
748 DhcpNotifyUser (DhcpSb
, DHCP_NOTIFY_COMPLETION
);
750 gBS
->FreePool (Packet
);
754 DhcpSendMessage (DhcpSb
, DhcpSb
->Selected
, DhcpSb
->Para
, DHCP_MSG_DECLINE
, Message
);
757 gBS
->FreePool (Packet
);
763 Handle packets in DHCP renew/rebound state.
765 @param[in] DhcpSb The DHCP service instance
766 @param[in] Packet The DHCP packet received
767 @param[in] Para The DHCP parameter extracted from the packet. That
768 is, all the option value that we care.
770 @retval EFI_SUCCESS The packet is successfully processed.
771 @retval Others Some error occured.
775 DhcpHandleRenewRebind (
776 IN DHCP_SERVICE
*DhcpSb
,
777 IN EFI_DHCP4_PACKET
*Packet
,
778 IN DHCP_PARAMETER
*Para
781 EFI_DHCP4_HEADER
*Head
;
782 EFI_DHCP4_HEADER
*Selected
;
785 ASSERT (!DHCP_IS_BOOTP (DhcpSb
->Para
));
787 Head
= &Packet
->Dhcp4
.Header
;
788 Selected
= &DhcpSb
->Selected
->Dhcp4
.Header
;
791 // Ignore the BOOTP message and DHCP messages other than DHCP ACK/NACK
793 if (DHCP_IS_BOOTP (Para
) ||
794 (Para
->ServerId
!= DhcpSb
->Para
->ServerId
) ||
795 ((Para
->DhcpType
!= DHCP_MSG_ACK
) && (Para
->DhcpType
!= DHCP_MSG_NAK
))
798 Status
= EFI_SUCCESS
;
803 // Received a NAK, ignore the user's return then terminate the process
805 Status
= EFI_DEVICE_ERROR
;
807 if (Para
->DhcpType
== DHCP_MSG_NAK
) {
808 DhcpCallUser (DhcpSb
, Dhcp4RcvdNak
, Packet
, NULL
);
813 // The lease is different from the selected. Don't send a DECLINE
814 // since it isn't existed in the client's FSM.
816 if (!EFI_IP4_EQUAL (&Head
->YourAddr
, &Selected
->YourAddr
)) {
820 Status
= DhcpCallUser (DhcpSb
, Dhcp4RcvdAck
, Packet
, NULL
);
822 if (EFI_ERROR (Status
)) {
827 // Record the lease, start timer for T1 and T2,
829 DhcpComputeLease (DhcpSb
, Para
);
830 DhcpSb
->LeaseLife
= 0;
831 DhcpSetState (DhcpSb
, Dhcp4Bound
, TRUE
);
833 if (DhcpSb
->ExtraRefresh
!= 0) {
834 DhcpSb
->ExtraRefresh
= FALSE
;
836 DhcpSb
->IoStatus
= EFI_SUCCESS
;
837 DhcpNotifyUser (DhcpSb
, DHCP_NOTIFY_RENEWREBIND
);
841 gBS
->FreePool (Packet
);
847 Handle packets in DHCP reboot state.
849 @param[in] DhcpSb The DHCP service instance
850 @param[in] Packet The DHCP packet received
851 @param[in] Para The DHCP parameter extracted from the packet. That
852 is, all the option value that we care.
854 @retval EFI_SUCCESS The packet is successfully processed.
855 @retval Others Some error occured.
860 IN DHCP_SERVICE
*DhcpSb
,
861 IN EFI_DHCP4_PACKET
*Packet
,
862 IN DHCP_PARAMETER
*Para
865 EFI_DHCP4_HEADER
*Head
;
868 Head
= &Packet
->Dhcp4
.Header
;
871 // Ignore the BOOTP message and DHCP messages other than DHCP ACK/NACK
873 if (DHCP_IS_BOOTP (Para
) ||
874 ((Para
->DhcpType
!= DHCP_MSG_ACK
) && (Para
->DhcpType
!= DHCP_MSG_NAK
))
877 Status
= EFI_SUCCESS
;
882 // If a NAK is received, transit to INIT and try again.
884 if (Para
->DhcpType
== DHCP_MSG_NAK
) {
885 DhcpCallUser (DhcpSb
, Dhcp4RcvdNak
, Packet
, NULL
);
887 DhcpSb
->ClientAddr
= 0;
888 DhcpSb
->DhcpState
= Dhcp4Init
;
890 Status
= DhcpInitRequest (DhcpSb
);
895 // Check whether the ACK matches the selected offer
897 if (EFI_NTOHL (Head
->YourAddr
) != DhcpSb
->ClientAddr
) {
898 Status
= EFI_DEVICE_ERROR
;
902 Status
= DhcpCallUser (DhcpSb
, Dhcp4RcvdAck
, Packet
, NULL
);
903 if (EFI_ERROR (Status
)) {
908 // OK, get the parameter from server, record the lease
910 DhcpSb
->Para
= AllocateCopyPool (sizeof (DHCP_PARAMETER
), Para
);
911 if (DhcpSb
->Para
== NULL
) {
912 Status
= EFI_OUT_OF_RESOURCES
;
916 DhcpSb
->Selected
= Packet
;
917 Status
= DhcpLeaseAcquired (DhcpSb
);
918 if (EFI_ERROR (Status
)) {
922 DhcpSb
->IoStatus
= EFI_SUCCESS
;
923 DhcpNotifyUser (DhcpSb
, DHCP_NOTIFY_COMPLETION
);
927 gBS
->FreePool (Packet
);
933 Handle the received DHCP packets. This function drives the DHCP
936 @param UdpPacket The UDP packets received.
937 @param Points The local/remote UDP access points
938 @param IoStatus The status of the UDP receive
939 @param Context The opaque parameter to the function.
950 DHCP_SERVICE
*DhcpSb
;
951 EFI_DHCP4_HEADER
*Head
;
952 EFI_DHCP4_PACKET
*Packet
;
953 DHCP_PARAMETER
*Para
;
958 DhcpSb
= (DHCP_SERVICE
*) Context
;
961 // Don't restart receive if error occurs or DHCP is destoried.
963 if (EFI_ERROR (IoStatus
)) {
965 } else if (DhcpSb
->ServiceState
== DHCP_DESTORY
) {
966 NetbufFree (UdpPacket
);
970 ASSERT (UdpPacket
!= NULL
);
972 if (DhcpSb
->DhcpState
== Dhcp4Stopped
) {
977 // Validate the packet received
979 if (UdpPacket
->TotalSize
< sizeof (EFI_DHCP4_HEADER
)) {
984 // Copy the DHCP message to a continuous memory block
986 Len
= sizeof (EFI_DHCP4_PACKET
) + UdpPacket
->TotalSize
- sizeof (EFI_DHCP4_HEADER
);
987 Packet
= (EFI_DHCP4_PACKET
*) AllocatePool (Len
);
989 if (Packet
== NULL
) {
994 Head
= &Packet
->Dhcp4
.Header
;
995 Packet
->Length
= NetbufCopy (UdpPacket
, 0, UdpPacket
->TotalSize
, (UINT8
*) Head
);
997 if (Packet
->Length
!= UdpPacket
->TotalSize
) {
1002 // Is this packet the answer to our packet?
1004 if ((Head
->OpCode
!= BOOTP_REPLY
) ||
1005 (NTOHL (Head
->Xid
) != DhcpSb
->Xid
) ||
1006 (CompareMem (DhcpSb
->ClientAddressSendOut
, Head
->ClientHwAddr
, Head
->HwAddrLen
) != 0)) {
1011 // Validate the options and retrieve the interested options
1014 if ((Packet
->Length
> sizeof (EFI_DHCP4_HEADER
) + sizeof (UINT32
)) &&
1015 (Packet
->Dhcp4
.Magik
== DHCP_OPTION_MAGIC
) &&
1016 EFI_ERROR (DhcpValidateOptions (Packet
, &Para
))) {
1022 // Call the handler for each state. The handler should return
1023 // EFI_SUCCESS if the process can go on no matter whether the
1024 // packet is ignored or not. If the return is EFI_ERROR, the
1025 // session will be terminated. Packet's ownership is handled
1026 // over to the handlers. If operation succeeds, the handler
1027 // must notify the user. It isn't necessary to do if EFI_ERROR
1028 // is returned because the DhcpEndSession will notify the user.
1030 Status
= EFI_SUCCESS
;
1032 switch (DhcpSb
->DhcpState
) {
1033 case Dhcp4Selecting
:
1034 Status
= DhcpHandleSelect (DhcpSb
, Packet
, Para
);
1037 case Dhcp4Requesting
:
1038 Status
= DhcpHandleRequest (DhcpSb
, Packet
, Para
);
1041 case Dhcp4InitReboot
:
1045 // Ignore the packet in INITREBOOT, INIT and BOUND states
1047 gBS
->FreePool (Packet
);
1048 Status
= EFI_SUCCESS
;
1052 case Dhcp4Rebinding
:
1053 Status
= DhcpHandleRenewRebind (DhcpSb
, Packet
, Para
);
1056 case Dhcp4Rebooting
:
1057 Status
= DhcpHandleReboot (DhcpSb
, Packet
, Para
);
1067 if (EFI_ERROR (Status
)) {
1068 NetbufFree (UdpPacket
);
1069 DhcpEndSession (DhcpSb
, Status
);
1074 NetbufFree (UdpPacket
);
1076 if (Packet
!= NULL
) {
1077 gBS
->FreePool (Packet
);
1080 Status
= UdpIoRecvDatagram (DhcpSb
->UdpIo
, DhcpInput
, DhcpSb
, 0);
1082 if (EFI_ERROR (Status
)) {
1083 DhcpEndSession (DhcpSb
, EFI_DEVICE_ERROR
);
1091 @param[in] Arg The packet to release
1099 gBS
->FreePool (Arg
);
1104 Release the net buffer when packet is sent.
1106 @param UdpPacket The UDP packets received.
1107 @param Points The local/remote UDP access points
1108 @param IoStatus The status of the UDP receive
1109 @param Context The opaque parameter to the function.
1116 EFI_STATUS IoStatus
,
1120 NetbufFree (Packet
);
1126 Build and transmit a DHCP message according to the current states.
1127 This function implement the Table 5. of RFC 2131. Always transits
1128 the state (as defined in Figure 5. of the same RFC) before sending
1129 a DHCP message. The table is adjusted accordingly.
1131 @param[in] DhcpSb The DHCP service instance
1132 @param[in] Seed The seed packet which the new packet is based on
1133 @param[in] Para The DHCP parameter of the Seed packet
1134 @param[in] Type The message type to send
1135 @param[in] Msg The human readable message to include in the packet
1138 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources for the packet
1139 @retval EFI_ACCESS_DENIED Failed to transmit the packet through UDP
1140 @retval EFI_SUCCESS The message is sent
1141 @retval other Other error occurs
1146 IN DHCP_SERVICE
*DhcpSb
,
1147 IN EFI_DHCP4_PACKET
*Seed
,
1148 IN DHCP_PARAMETER
*Para
,
1153 EFI_DHCP4_CONFIG_DATA
*Config
;
1154 EFI_DHCP4_PACKET
*Packet
;
1155 EFI_DHCP4_PACKET
*NewPacket
;
1156 EFI_DHCP4_HEADER
*Head
;
1157 EFI_DHCP4_HEADER
*SeedHead
;
1159 UDP_POINTS EndPoint
;
1170 // Allocate a big enough memory block to hold the DHCP packet
1172 Len
= sizeof (EFI_DHCP4_PACKET
) + 128 + DhcpSb
->UserOptionLen
;
1175 Len
+= (UINT32
)AsciiStrLen ((CHAR8
*) Msg
);
1178 Packet
= AllocatePool (Len
);
1180 if (Packet
== NULL
) {
1181 return EFI_OUT_OF_RESOURCES
;
1185 Packet
->Length
= sizeof (EFI_DHCP4_HEADER
) + sizeof (UINT32
);
1188 // Fill in the DHCP header fields
1190 Config
= &DhcpSb
->ActiveConfig
;
1194 SeedHead
= &Seed
->Dhcp4
.Header
;
1197 Head
= &Packet
->Dhcp4
.Header
;
1198 ZeroMem (Head
, sizeof (EFI_DHCP4_HEADER
));
1200 Head
->OpCode
= BOOTP_REQUEST
;
1201 Head
->HwType
= DhcpSb
->HwType
;
1202 Head
->HwAddrLen
= DhcpSb
->HwLen
;
1203 Head
->Xid
= HTONL (DhcpSb
->Xid
);
1204 Head
->Reserved
= HTONS (0x8000); //Server, broadcast the message please.
1206 EFI_IP4 (Head
->ClientAddr
) = HTONL (DhcpSb
->ClientAddr
);
1207 CopyMem (Head
->ClientHwAddr
, DhcpSb
->Mac
.Addr
, DhcpSb
->HwLen
);
1210 // Append the DHCP message type
1212 Packet
->Dhcp4
.Magik
= DHCP_OPTION_MAGIC
;
1213 Buf
= Packet
->Dhcp4
.Option
;
1214 Buf
= DhcpAppendOption (Buf
, DHCP_TAG_TYPE
, 1, &Type
);
1217 // Append the serverid option if necessary:
1218 // 1. DHCP decline message
1219 // 2. DHCP release message
1220 // 3. DHCP request to confirm one lease.
1222 if ((Type
== DHCP_MSG_DECLINE
) || (Type
== DHCP_MSG_RELEASE
) ||
1223 ((Type
== DHCP_MSG_REQUEST
) && (DhcpSb
->DhcpState
== Dhcp4Requesting
))
1226 ASSERT ((Para
!= NULL
) && (Para
->ServerId
!= 0));
1228 IpAddr
= HTONL (Para
->ServerId
);
1229 Buf
= DhcpAppendOption (Buf
, DHCP_TAG_SERVER_ID
, 4, (UINT8
*) &IpAddr
);
1233 // Append the requested IP option if necessary:
1234 // 1. DHCP request to use the previously allocated address
1235 // 2. DHCP request to confirm one lease
1236 // 3. DHCP decline to decline one lease
1240 if (Type
== DHCP_MSG_REQUEST
) {
1241 if (DhcpSb
->DhcpState
== Dhcp4Rebooting
) {
1242 IpAddr
= EFI_IP4 (Config
->ClientAddress
);
1244 } else if (DhcpSb
->DhcpState
== Dhcp4Requesting
) {
1245 ASSERT (SeedHead
!= NULL
);
1246 IpAddr
= EFI_IP4 (SeedHead
->YourAddr
);
1249 } else if (Type
== DHCP_MSG_DECLINE
) {
1250 ASSERT (SeedHead
!= NULL
);
1251 IpAddr
= EFI_IP4 (SeedHead
->YourAddr
);
1255 Buf
= DhcpAppendOption (Buf
, DHCP_TAG_REQUEST_IP
, 4, (UINT8
*) &IpAddr
);
1259 // Append the Max Message Length option if it isn't a DECLINE
1260 // or RELEASE to direct the server use large messages instead of
1261 // override the BOOTFILE and SERVER fields in the message head.
1263 if ((Type
!= DHCP_MSG_DECLINE
) && (Type
!= DHCP_MSG_RELEASE
)) {
1264 MaxMsg
= HTONS (0xFF00);
1265 Buf
= DhcpAppendOption (Buf
, DHCP_TAG_MAXMSG
, 2, (UINT8
*) &MaxMsg
);
1269 // Append the user's message if it isn't NULL
1272 Len
= MIN ((UINT32
) AsciiStrLen ((CHAR8
*) Msg
), 255);
1273 Buf
= DhcpAppendOption (Buf
, DHCP_TAG_MESSAGE
, (UINT16
) Len
, Msg
);
1277 // Append the user configured options
1279 if (DhcpSb
->UserOptionLen
!= 0) {
1280 for (Index
= 0; Index
< Config
->OptionCount
; Index
++) {
1282 // We can't use any option other than the client ID from user
1283 // if it is a DHCP decline or DHCP release .
1285 if (((Type
== DHCP_MSG_DECLINE
) || (Type
== DHCP_MSG_RELEASE
)) &&
1286 (Config
->OptionList
[Index
]->OpCode
!= DHCP_TAG_CLIENT_ID
)) {
1290 Buf
= DhcpAppendOption (
1292 Config
->OptionList
[Index
]->OpCode
,
1293 Config
->OptionList
[Index
]->Length
,
1294 Config
->OptionList
[Index
]->Data
1299 *(Buf
++) = DHCP_TAG_EOP
;
1300 Packet
->Length
+= (UINT32
) (Buf
- Packet
->Dhcp4
.Option
);
1303 // OK, the message is built, call the user to override it.
1305 Status
= EFI_SUCCESS
;
1308 if (Type
== DHCP_MSG_DISCOVER
) {
1309 Status
= DhcpCallUser (DhcpSb
, Dhcp4SendDiscover
, Packet
, &NewPacket
);
1311 } else if (Type
== DHCP_MSG_REQUEST
) {
1312 Status
= DhcpCallUser (DhcpSb
, Dhcp4SendRequest
, Packet
, &NewPacket
);
1314 } else if (Type
== DHCP_MSG_DECLINE
) {
1315 Status
= DhcpCallUser (DhcpSb
, Dhcp4SendDecline
, Packet
, &NewPacket
);
1318 if (EFI_ERROR (Status
)) {
1319 gBS
->FreePool (Packet
);
1323 if (NewPacket
!= NULL
) {
1324 gBS
->FreePool (Packet
);
1329 // Save the Client Address will be sent out
1332 &DhcpSb
->ClientAddressSendOut
[0],
1333 &Packet
->Dhcp4
.Header
.ClientHwAddr
[0],
1334 Packet
->Dhcp4
.Header
.HwAddrLen
1339 // Wrap it into a netbuf then send it.
1341 Frag
.Bulk
= (UINT8
*) &Packet
->Dhcp4
.Header
;
1342 Frag
.Len
= Packet
->Length
;
1343 Wrap
= NetbufFromExt (&Frag
, 1, 0, 0, DhcpReleasePacket
, Packet
);
1346 gBS
->FreePool (Packet
);
1347 return EFI_OUT_OF_RESOURCES
;
1351 // Save it as the last sent packet for retransmission
1353 if (DhcpSb
->LastPacket
!= NULL
) {
1354 FreePool (DhcpSb
->LastPacket
);
1357 DhcpSb
->LastPacket
= Packet
;
1358 DhcpSetTransmitTimer (DhcpSb
);
1361 // Broadcast the message, unless we know the server address.
1362 // Use the lease UdpIo port to send the unicast packet.
1364 EndPoint
.RemoteAddr
= 0xffffffff;
1365 EndPoint
.LocalAddr
= 0;
1366 EndPoint
.RemotePort
= DHCP_SERVER_PORT
;
1367 EndPoint
.LocalPort
= DHCP_CLIENT_PORT
;
1368 UdpIo
= DhcpSb
->UdpIo
;
1370 if ((DhcpSb
->DhcpState
== Dhcp4Renewing
) || (Type
== DHCP_MSG_RELEASE
)) {
1371 EndPoint
.RemoteAddr
= DhcpSb
->ServerAddr
;
1372 EndPoint
.LocalAddr
= DhcpSb
->ClientAddr
;
1373 UdpIo
= DhcpSb
->LeaseIoPort
;
1376 ASSERT (UdpIo
!= NULL
);
1379 Status
= UdpIoSendDatagram (
1388 if (EFI_ERROR (Status
)) {
1390 return EFI_ACCESS_DENIED
;
1398 Retransmit a saved packet. Only DISCOVER and REQUEST messages
1399 will be retransmitted.
1401 @param[in] DhcpSb The DHCP service instance
1403 @retval EFI_ACCESS_DENIED Failed to transmit packet through UDP port
1404 @retval EFI_SUCCESS The packet is retransmitted.
1409 IN DHCP_SERVICE
*DhcpSb
1413 UDP_POINTS EndPoint
;
1418 ASSERT (DhcpSb
->LastPacket
!= NULL
);
1420 DhcpSb
->LastPacket
->Dhcp4
.Header
.Seconds
= HTONS (*(UINT16
*)(&DhcpSb
->LastTimeout
));
1423 // Wrap it into a netbuf then send it.
1425 Frag
.Bulk
= (UINT8
*) &DhcpSb
->LastPacket
->Dhcp4
.Header
;
1426 Frag
.Len
= DhcpSb
->LastPacket
->Length
;
1427 Wrap
= NetbufFromExt (&Frag
, 1, 0, 0, DhcpReleasePacket
, DhcpSb
->LastPacket
);
1430 return EFI_OUT_OF_RESOURCES
;
1434 // Broadcast the message, unless we know the server address.
1436 EndPoint
.RemotePort
= DHCP_SERVER_PORT
;
1437 EndPoint
.LocalPort
= DHCP_CLIENT_PORT
;
1438 EndPoint
.RemoteAddr
= 0xffffffff;
1439 EndPoint
.LocalAddr
= 0;
1440 UdpIo
= DhcpSb
->UdpIo
;
1442 if (DhcpSb
->DhcpState
== Dhcp4Renewing
) {
1443 EndPoint
.RemoteAddr
= DhcpSb
->ServerAddr
;
1444 EndPoint
.LocalAddr
= DhcpSb
->ClientAddr
;
1445 UdpIo
= DhcpSb
->LeaseIoPort
;
1448 ASSERT (UdpIo
!= NULL
);
1451 Status
= UdpIoSendDatagram (
1460 if (EFI_ERROR (Status
)) {
1462 return EFI_ACCESS_DENIED
;
1470 Each DHCP service has three timer. Two of them are count down timer.
1471 One for the packet retransmission. The other is to collect the offers.
1472 The third timer increaments the lease life which is compared to T1, T2,
1473 and lease to determine the time to renew and rebind the lease.
1474 DhcpOnTimerTick will be called once every second.
1476 @param[in] Event The timer event
1477 @param[in] Context The context, which is the DHCP service instance.
1487 DHCP_SERVICE
*DhcpSb
;
1488 DHCP_PROTOCOL
*Instance
;
1491 DhcpSb
= (DHCP_SERVICE
*) Context
;
1492 Instance
= DhcpSb
->ActiveChild
;
1495 // Check the retransmit timer
1497 if ((DhcpSb
->PacketToLive
> 0) && (--DhcpSb
->PacketToLive
== 0)) {
1500 // Select offer at each timeout if any offer received.
1502 if (DhcpSb
->DhcpState
== Dhcp4Selecting
&& DhcpSb
->LastOffer
!= NULL
) {
1504 Status
= DhcpChooseOffer (DhcpSb
);
1506 if (EFI_ERROR(Status
)) {
1507 FreePool (DhcpSb
->LastOffer
);
1508 DhcpSb
->LastOffer
= NULL
;
1514 if (++DhcpSb
->CurRetry
< DhcpSb
->MaxRetries
) {
1516 // Still has another try
1518 DhcpRetransmit (DhcpSb
);
1519 DhcpSetTransmitTimer (DhcpSb
);
1521 } else if (DHCP_CONNECTED (DhcpSb
->DhcpState
)) {
1524 // Retransmission failed, if the DHCP request is initiated by
1525 // user, adjust the current state according to the lease life.
1526 // Otherwise do nothing to wait the lease to timeout
1528 if (DhcpSb
->ExtraRefresh
!= 0) {
1529 Status
= EFI_SUCCESS
;
1531 if (DhcpSb
->LeaseLife
< DhcpSb
->T1
) {
1532 Status
= DhcpSetState (DhcpSb
, Dhcp4Bound
, FALSE
);
1534 } else if (DhcpSb
->LeaseLife
< DhcpSb
->T2
) {
1535 Status
= DhcpSetState (DhcpSb
, Dhcp4Renewing
, FALSE
);
1537 } else if (DhcpSb
->LeaseLife
< DhcpSb
->Lease
) {
1538 Status
= DhcpSetState (DhcpSb
, Dhcp4Rebinding
, FALSE
);
1545 DhcpSb
->IoStatus
= EFI_TIMEOUT
;
1546 DhcpNotifyUser (DhcpSb
, DHCP_NOTIFY_RENEWREBIND
);
1554 // If an address has been acquired, check whether need to
1555 // refresh or whether it has expired.
1557 if (DHCP_CONNECTED (DhcpSb
->DhcpState
)) {
1558 DhcpSb
->LeaseLife
++;
1561 // Don't timeout the lease, only count the life if user is
1562 // requesting extra renew/rebind. Adjust the state after that.
1564 if (DhcpSb
->ExtraRefresh
!= 0) {
1568 if (DhcpSb
->LeaseLife
== DhcpSb
->Lease
) {
1570 // Lease expires, end the session
1574 } else if (DhcpSb
->LeaseLife
== DhcpSb
->T2
) {
1576 // T2 expires, transit to rebinding then send a REQUEST to any server
1578 if (EFI_ERROR (DhcpSetState (DhcpSb
, Dhcp4Rebinding
, TRUE
))) {
1582 Status
= DhcpSendMessage (
1590 if (EFI_ERROR (Status
)) {
1594 } else if (DhcpSb
->LeaseLife
== DhcpSb
->T1
) {
1596 // T1 expires, transit to renewing, then send a REQUEST to the server
1598 if (EFI_ERROR (DhcpSetState (DhcpSb
, Dhcp4Renewing
, TRUE
))) {
1602 Status
= DhcpSendMessage (
1610 if (EFI_ERROR (Status
)) {
1617 if ((Instance
!= NULL
) && (Instance
->Token
!= NULL
)) {
1618 Instance
->Timeout
--;
1619 if (Instance
->Timeout
== 0) {
1620 PxeDhcpDone (Instance
);
1627 DhcpEndSession (DhcpSb
, EFI_TIMEOUT
);