2 This file implement the EFI_DHCP4_PROTOCOL interface.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 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"
19 Returns the current operating mode and cached data packet for the EFI DHCPv4 Protocol driver.
21 The GetModeData() function returns the current operating mode and cached data
22 packet for the EFI DHCPv4 Protocol driver.
24 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
25 @param[out] Dhcp4ModeData Pointer to storage for the EFI_DHCP4_MODE_DATA structure.
27 @retval EFI_SUCCESS The mode data was returned.
28 @retval EFI_INVALID_PARAMETER This is NULL.
34 IN EFI_DHCP4_PROTOCOL
*This
,
35 OUT EFI_DHCP4_MODE_DATA
*Dhcp4ModeData
39 Initializes, changes, or resets the operational settings for the EFI DHCPv4 Protocol driver.
41 The Configure() function is used to initialize, change, or reset the operational
42 settings of the EFI DHCPv4 Protocol driver for the communication device on which
43 the EFI DHCPv4 Service Binding Protocol is installed. This function can be
44 successfully called only if both of the following are true:
45 * This instance of the EFI DHCPv4 Protocol driver is in the Dhcp4Stopped, Dhcp4Init,
46 Dhcp4InitReboot, or Dhcp4Bound states.
47 * No other EFI DHCPv4 Protocol driver instance that is controlled by this EFI
48 DHCPv4 Service Binding Protocol driver instance has configured this EFI DHCPv4
50 When this driver is in the Dhcp4Stopped state, it can transfer into one of the
51 following two possible initial states:
54 The driver can transfer into these states by calling Configure() with a non-NULL
55 Dhcp4CfgData. The driver will transfer into the appropriate state based on the
56 supplied client network address in the ClientAddress parameter and DHCP options
57 in the OptionList parameter as described in RFC 2131.
58 When Configure() is called successfully while Dhcp4CfgData is set to NULL, the
59 default configuring data will be reset in the EFI DHCPv4 Protocol driver and
60 the state of the EFI DHCPv4 Protocol driver will not be changed. If one instance
61 wants to make it possible for another instance to configure the EFI DHCPv4 Protocol
62 driver, it must call this function with Dhcp4CfgData set to NULL.
64 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
65 @param[in] Dhcp4CfgData Pointer to the EFI_DHCP4_CONFIG_DATA.
67 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init or
68 Dhcp4InitReboot state, if the original state of this driver
69 was Dhcp4Stopped and the value of Dhcp4CfgData was
70 not NULL. Otherwise, the state was left unchanged.
71 @retval EFI_ACCESS_DENIED This instance of the EFI DHCPv4 Protocol driver was not in the
72 Dhcp4Stopped, Dhcp4Init, Dhcp4InitReboot, or Dhcp4Bound state;
73 Or onother instance of this EFI DHCPv4 Protocol driver is already
74 in a valid configured state.
75 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
76 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
77 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
83 IN EFI_DHCP4_PROTOCOL
*This
,
84 IN EFI_DHCP4_CONFIG_DATA
*Dhcp4CfgData OPTIONAL
88 Starts the DHCP configuration process.
90 The Start() function starts the DHCP configuration process. This function can
91 be called only when the EFI DHCPv4 Protocol driver is in the Dhcp4Init or
92 Dhcp4InitReboot state.
93 If the DHCP process completes successfully, the state of the EFI DHCPv4 Protocol
94 driver will be transferred through Dhcp4Selecting and Dhcp4Requesting to the
95 Dhcp4Bound state. The CompletionEvent will then be signaled if it is not NULL.
96 If the process aborts, either by the user or by some unexpected network error,
97 the state is restored to the Dhcp4Init state. The Start() function can be called
98 again to restart the process.
99 Refer to RFC 2131 for precise state transitions during this process. At the
100 time when each event occurs in this process, the callback function that was set
101 by EFI_DHCP4_PROTOCOL.Configure() will be called and the user can take this
102 opportunity to control the process.
104 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
105 @param[in] CompletionEvent If not NULL, indicates the event that will be signaled when the
106 EFI DHCPv4 Protocol driver is transferred into the
107 Dhcp4Bound state or when the DHCP process is aborted.
108 EFI_DHCP4_PROTOCOL.GetModeData() can be called to
109 check the completion status. If NULL,
110 EFI_DHCP4_PROTOCOL.Start() will wait until the driver
111 is transferred into the Dhcp4Bound state or the process fails.
113 @retval EFI_SUCCESS The DHCP configuration process has started, or it has completed
114 when CompletionEvent is NULL.
115 @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped
116 state. EFI_DHCP4_PROTOCOL. Configure() needs to be called.
117 @retval EFI_INVALID_PARAMETER This is NULL.
118 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
119 @retval EFI_TIMEOUT The DHCP configuration process failed because no response was
120 received from the server within the specified timeout value.
121 @retval EFI_ABORTED The user aborted the DHCP process.
122 @retval EFI_ALREADY_STARTED Some other EFI DHCPv4 Protocol instance already started the
124 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
130 IN EFI_DHCP4_PROTOCOL
*This
,
131 IN EFI_EVENT CompletionEvent OPTIONAL
135 Extends the lease time by sending a request packet.
137 The RenewRebind() function is used to manually extend the lease time when the
138 EFI DHCPv4 Protocol driver is in the Dhcp4Bound state and the lease time has
139 not expired yet. This function will send a request packet to the previously
140 found server (or to any server when RebindRequest is TRUE) and transfer the
141 state into the Dhcp4Renewing state (or Dhcp4Rebinding when RebindingRequest is
142 TRUE). When a response is received, the state is returned to Dhcp4Bound.
143 If no response is received before the try count is exceeded (the RequestTryCount
144 field that is specified in EFI_DHCP4_CONFIG_DATA) but before the lease time that
145 was issued by the previous server expires, the driver will return to the Dhcp4Bound
146 state and the previous configuration is restored. The outgoing and incoming packets
147 can be captured by the EFI_DHCP4_CALLBACK function.
149 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
150 @param[in] RebindRequest If TRUE, this function broadcasts the request packets and enters
151 the Dhcp4Rebinding state. Otherwise, it sends a unicast
152 request packet and enters the Dhcp4Renewing state.
153 @param[in] CompletionEvent If not NULL, this event is signaled when the renew/rebind phase
154 completes or some error occurs.
155 EFI_DHCP4_PROTOCOL.GetModeData() can be called to
156 check the completion status. If NULL,
157 EFI_DHCP4_PROTOCOL.RenewRebind() will busy-wait
158 until the DHCP process finishes.
160 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the
161 Dhcp4Renewing state or is back to the Dhcp4Bound state.
162 @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped
163 state. EFI_DHCP4_PROTOCOL.Configure() needs to
165 @retval EFI_INVALID_PARAMETER This is NULL.
166 @retval EFI_TIMEOUT There was no response from the server when the try count was
168 @retval EFI_ACCESS_DENIED The driver is not in the Dhcp4Bound state.
169 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
174 EfiDhcp4RenewRebind (
175 IN EFI_DHCP4_PROTOCOL
*This
,
176 IN BOOLEAN RebindRequest
,
177 IN EFI_EVENT CompletionEvent OPTIONAL
181 Releases the current address configuration.
183 The Release() function releases the current configured IP address by doing either
185 * Sending a DHCPRELEASE packet when the EFI DHCPv4 Protocol driver is in the
187 * Setting the previously assigned IP address that was provided with the
188 EFI_DHCP4_PROTOCOL.Configure() function to 0.0.0.0 when the driver is in
189 Dhcp4InitReboot state
190 After a successful call to this function, the EFI DHCPv4 Protocol driver returns
191 to the Dhcp4Init state and any subsequent incoming packets will be discarded silently.
193 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
195 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init phase.
196 @retval EFI_INVALID_PARAMETER This is NULL.
197 @retval EFI_ACCESS_DENIED The EFI DHCPv4 Protocol driver is not Dhcp4InitReboot state.
198 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
204 IN EFI_DHCP4_PROTOCOL
*This
208 Stops the current address configuration.
210 The Stop() function is used to stop the DHCP configuration process. After this
211 function is called successfully, the EFI DHCPv4 Protocol driver is transferred
212 into the Dhcp4Stopped state. EFI_DHCP4_PROTOCOL.Configure() needs to be called
213 before DHCP configuration process can be started again. This function can be
214 called when the EFI DHCPv4 Protocol driver is in any state.
216 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
218 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Stopped phase.
219 @retval EFI_INVALID_PARAMETER This is NULL.
225 IN EFI_DHCP4_PROTOCOL
*This
229 Builds a DHCP packet, given the options to be appended or deleted or replaced.
231 The Build() function is used to assemble a new packet from the original packet
232 by replacing or deleting existing options or appending new options. This function
233 does not change any state of the EFI DHCPv4 Protocol driver and can be used at
236 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
237 @param[in] SeedPacket Initial packet to be used as a base for building new packet.
238 @param[in] DeleteCount Number of opcodes in the DeleteList.
239 @param[in] DeleteList List of opcodes to be deleted from the seed packet.
240 Ignored if DeleteCount is zero.
241 @param[in] AppendCount Number of entries in the OptionList.
242 @param[in] AppendList Pointer to a DHCP option list to be appended to SeedPacket.
243 If SeedPacket also contains options in this list, they are
244 replaced by new options (except pad option). Ignored if
245 AppendCount is zero. Type EFI_DHCP4_PACKET_OPTION
246 @param[out] NewPacket Pointer to storage for the pointer to the new allocated packet.
247 Use the EFI Boot Service FreePool() on the resulting pointer
248 when done with the packet.
250 @retval EFI_SUCCESS The new packet was built.
251 @retval EFI_OUT_OF_RESOURCES Storage for the new packet could not be allocated.
252 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
258 IN EFI_DHCP4_PROTOCOL
*This
,
259 IN EFI_DHCP4_PACKET
*SeedPacket
,
260 IN UINT32 DeleteCount
,
261 IN UINT8
*DeleteList OPTIONAL
,
262 IN UINT32 AppendCount
,
263 IN EFI_DHCP4_PACKET_OPTION
*AppendList
[] OPTIONAL
,
264 OUT EFI_DHCP4_PACKET
**NewPacket
268 Transmits a DHCP formatted packet and optionally waits for responses.
270 The TransmitReceive() function is used to transmit a DHCP packet and optionally
271 wait for the response from servers. This function does not change the state of
272 the EFI DHCPv4 Protocol driver and thus can be used at any time.
274 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
275 @param[in] Token Pointer to the EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN structure.
277 @retval EFI_SUCCESS The packet was successfully queued for transmission.
278 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
279 @retval EFI_NOT_READY The previous call to this function has not finished yet. Try to call
280 this function after collection process completes.
281 @retval EFI_NO_MAPPING The default station address is not available yet.
282 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
283 @retval Others Some other unexpected error occurred.
288 EfiDhcp4TransmitReceive (
289 IN EFI_DHCP4_PROTOCOL
*This
,
290 IN EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN
*Token
294 Parses the packed DHCP option data.
296 The Parse() function is used to retrieve the option list from a DHCP packet.
297 If *OptionCount isn't zero, and there is enough space for all the DHCP options
298 in the Packet, each element of PacketOptionList is set to point to somewhere in
299 the Packet->Dhcp4.Option where a new DHCP option begins. If RFC3396 is supported,
300 the caller should reassemble the parsed DHCP options to get the finial result.
301 If *OptionCount is zero or there isn't enough space for all of them, the number
302 of DHCP options in the Packet is returned in OptionCount.
304 @param This Pointer to the EFI_DHCP4_PROTOCOL instance.
305 @param Packet Pointer to packet to be parsed.
306 @param OptionCount On input, the number of entries in the PacketOptionList.
307 On output, the number of entries that were written into the
309 @param PacketOptionList List of packet option entries to be filled in. End option or pad
310 options are not included.
312 @retval EFI_SUCCESS The packet was successfully parsed.
313 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
314 @retval EFI_BUFFER_TOO_SMALL One or more of the following conditions is TRUE:
315 1) *OptionCount is smaller than the number of options that
316 were found in the Packet.
317 2) PacketOptionList is NULL.
323 IN EFI_DHCP4_PROTOCOL
*This
,
324 IN EFI_DHCP4_PACKET
*Packet
,
325 IN OUT UINT32
*OptionCount
,
326 OUT EFI_DHCP4_PACKET_OPTION
*PacketOptionList
[] OPTIONAL
329 EFI_DHCP4_PROTOCOL mDhcp4ProtocolTemplate
= {
337 EfiDhcp4TransmitReceive
,
342 Returns the current operating mode and cached data packet for the EFI DHCPv4 Protocol driver.
344 The GetModeData() function returns the current operating mode and cached data
345 packet for the EFI DHCPv4 Protocol driver.
347 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
348 @param[out] Dhcp4ModeData Pointer to storage for the EFI_DHCP4_MODE_DATA structure.
350 @retval EFI_SUCCESS The mode data was returned.
351 @retval EFI_INVALID_PARAMETER This is NULL.
356 EfiDhcp4GetModeData (
357 IN EFI_DHCP4_PROTOCOL
*This
,
358 OUT EFI_DHCP4_MODE_DATA
*Dhcp4ModeData
361 DHCP_PROTOCOL
*Instance
;
362 DHCP_SERVICE
*DhcpSb
;
363 DHCP_PARAMETER
*Para
;
368 // First validate the parameters.
370 if ((This
== NULL
) || (Dhcp4ModeData
== NULL
)) {
371 return EFI_INVALID_PARAMETER
;
374 Instance
= DHCP_INSTANCE_FROM_THIS (This
);
376 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
377 DhcpSb
= Instance
->Service
;
380 // Caller can use GetModeData to retrieve current DHCP states
381 // no matter whether it is the active child or not.
383 Dhcp4ModeData
->State
= (EFI_DHCP4_STATE
) DhcpSb
->DhcpState
;
384 CopyMem (&Dhcp4ModeData
->ConfigData
, &DhcpSb
->ActiveConfig
, sizeof (Dhcp4ModeData
->ConfigData
));
385 CopyMem (&Dhcp4ModeData
->ClientMacAddress
, &DhcpSb
->Mac
, sizeof (Dhcp4ModeData
->ClientMacAddress
));
387 Ip
= HTONL (DhcpSb
->ClientAddr
);
388 CopyMem (&Dhcp4ModeData
->ClientAddress
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
390 Ip
= HTONL (DhcpSb
->Netmask
);
391 CopyMem (&Dhcp4ModeData
->SubnetMask
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
393 Ip
= HTONL (DhcpSb
->ServerAddr
);
394 CopyMem (&Dhcp4ModeData
->ServerAddress
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
399 Ip
= HTONL (Para
->Router
);
400 CopyMem (&Dhcp4ModeData
->RouterAddress
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
401 Dhcp4ModeData
->LeaseTime
= Para
->Lease
;
403 ZeroMem (&Dhcp4ModeData
->RouterAddress
, sizeof (EFI_IPv4_ADDRESS
));
404 Dhcp4ModeData
->LeaseTime
= 0xffffffff;
407 Dhcp4ModeData
->ReplyPacket
= DhcpSb
->Selected
;
409 gBS
->RestoreTPL (OldTpl
);
415 Free the resource related to the configure parameters.
416 DHCP driver will make a copy of the user's configure
417 such as the time out value.
419 @param Config The DHCP configure data
424 IN OUT EFI_DHCP4_CONFIG_DATA
*Config
429 if (Config
->DiscoverTimeout
!= NULL
) {
430 FreePool (Config
->DiscoverTimeout
);
433 if (Config
->RequestTimeout
!= NULL
) {
434 FreePool (Config
->RequestTimeout
);
437 if (Config
->OptionList
!= NULL
) {
438 for (Index
= 0; Index
< Config
->OptionCount
; Index
++) {
439 if (Config
->OptionList
[Index
] != NULL
) {
440 FreePool (Config
->OptionList
[Index
]);
444 FreePool (Config
->OptionList
);
447 ZeroMem (Config
, sizeof (EFI_DHCP4_CONFIG_DATA
));
452 Allocate memory for configure parameter such as timeout value for Dst,
453 then copy the configure parameter from Src to Dst.
455 @param[out] Dst The destination DHCP configure data.
456 @param[in] Src The source DHCP configure data.
458 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
459 @retval EFI_SUCCESS The configure is copied.
464 OUT EFI_DHCP4_CONFIG_DATA
*Dst
,
465 IN EFI_DHCP4_CONFIG_DATA
*Src
468 EFI_DHCP4_PACKET_OPTION
**DstOptions
;
469 EFI_DHCP4_PACKET_OPTION
**SrcOptions
;
473 CopyMem (Dst
, Src
, sizeof (*Dst
));
474 Dst
->DiscoverTimeout
= NULL
;
475 Dst
->RequestTimeout
= NULL
;
476 Dst
->OptionList
= NULL
;
479 // Allocate a memory then copy DiscoverTimeout to it
481 if (Src
->DiscoverTimeout
!= NULL
) {
482 Len
= Src
->DiscoverTryCount
* sizeof (UINT32
);
483 Dst
->DiscoverTimeout
= AllocatePool (Len
);
485 if (Dst
->DiscoverTimeout
== NULL
) {
486 return EFI_OUT_OF_RESOURCES
;
489 for (Index
= 0; Index
< Src
->DiscoverTryCount
; Index
++) {
490 Dst
->DiscoverTimeout
[Index
] = MAX (Src
->DiscoverTimeout
[Index
], 1);
495 // Allocate a memory then copy RequestTimeout to it
497 if (Src
->RequestTimeout
!= NULL
) {
498 Len
= Src
->RequestTryCount
* sizeof (UINT32
);
499 Dst
->RequestTimeout
= AllocatePool (Len
);
501 if (Dst
->RequestTimeout
== NULL
) {
505 for (Index
= 0; Index
< Src
->RequestTryCount
; Index
++) {
506 Dst
->RequestTimeout
[Index
] = MAX (Src
->RequestTimeout
[Index
], 1);
511 // Allocate an array of dhcp option point, then allocate memory
512 // for each option and copy the source option to it
514 if (Src
->OptionList
!= NULL
) {
515 Len
= Src
->OptionCount
* sizeof (EFI_DHCP4_PACKET_OPTION
*);
516 Dst
->OptionList
= AllocateZeroPool (Len
);
518 if (Dst
->OptionList
== NULL
) {
522 DstOptions
= Dst
->OptionList
;
523 SrcOptions
= Src
->OptionList
;
525 for (Index
= 0; Index
< Src
->OptionCount
; Index
++) {
526 Len
= sizeof (EFI_DHCP4_PACKET_OPTION
) + MAX (SrcOptions
[Index
]->Length
- 1, 0);
528 DstOptions
[Index
] = AllocatePool (Len
);
530 if (DstOptions
[Index
] == NULL
) {
534 CopyMem (DstOptions
[Index
], SrcOptions
[Index
], Len
);
541 DhcpCleanConfigure (Dst
);
542 return EFI_OUT_OF_RESOURCES
;
547 Give up the control of the DHCP service to let other child
548 resume. Don't change the service's DHCP state and the Client
549 address and option list configure as required by RFC2131.
551 @param DhcpSb The DHCP service instance.
556 IN DHCP_SERVICE
*DhcpSb
559 EFI_DHCP4_CONFIG_DATA
*Config
;
561 Config
= &DhcpSb
->ActiveConfig
;
563 DhcpSb
->ServiceState
= DHCP_UNCONFIGED
;
564 DhcpSb
->ActiveChild
= NULL
;
566 if (Config
->DiscoverTimeout
!= NULL
) {
567 FreePool (Config
->DiscoverTimeout
);
569 Config
->DiscoverTryCount
= 0;
570 Config
->DiscoverTimeout
= NULL
;
573 if (Config
->RequestTimeout
!= NULL
) {
574 FreePool (Config
->RequestTimeout
);
576 Config
->RequestTryCount
= 0;
577 Config
->RequestTimeout
= NULL
;
580 Config
->Dhcp4Callback
= NULL
;
581 Config
->CallbackContext
= NULL
;
586 Initializes, changes, or resets the operational settings for the EFI DHCPv4 Protocol driver.
588 The Configure() function is used to initialize, change, or reset the operational
589 settings of the EFI DHCPv4 Protocol driver for the communication device on which
590 the EFI DHCPv4 Service Binding Protocol is installed. This function can be
591 successfully called only if both of the following are true:
592 * This instance of the EFI DHCPv4 Protocol driver is in the Dhcp4Stopped, Dhcp4Init,
593 Dhcp4InitReboot, or Dhcp4Bound states.
594 * No other EFI DHCPv4 Protocol driver instance that is controlled by this EFI
595 DHCPv4 Service Binding Protocol driver instance has configured this EFI DHCPv4
597 When this driver is in the Dhcp4Stopped state, it can transfer into one of the
598 following two possible initial states:
601 The driver can transfer into these states by calling Configure() with a non-NULL
602 Dhcp4CfgData. The driver will transfer into the appropriate state based on the
603 supplied client network address in the ClientAddress parameter and DHCP options
604 in the OptionList parameter as described in RFC 2131.
605 When Configure() is called successfully while Dhcp4CfgData is set to NULL, the
606 default configuring data will be reset in the EFI DHCPv4 Protocol driver and
607 the state of the EFI DHCPv4 Protocol driver will not be changed. If one instance
608 wants to make it possible for another instance to configure the EFI DHCPv4 Protocol
609 driver, it must call this function with Dhcp4CfgData set to NULL.
611 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
612 @param[in] Dhcp4CfgData Pointer to the EFI_DHCP4_CONFIG_DATA.
614 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init or
615 Dhcp4InitReboot state, if the original state of this driver
616 was Dhcp4Stopped and the value of Dhcp4CfgData was
617 not NULL. Otherwise, the state was left unchanged.
618 @retval EFI_ACCESS_DENIED This instance of the EFI DHCPv4 Protocol driver was not in the
619 Dhcp4Stopped, Dhcp4Init, Dhcp4InitReboot, or Dhcp4Bound state;
620 Or onother instance of this EFI DHCPv4 Protocol driver is already
621 in a valid configured state.
622 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
623 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
624 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
630 IN EFI_DHCP4_PROTOCOL
*This
,
631 IN EFI_DHCP4_CONFIG_DATA
*Dhcp4CfgData OPTIONAL
634 EFI_DHCP4_CONFIG_DATA
*Config
;
635 DHCP_PROTOCOL
*Instance
;
636 DHCP_SERVICE
*DhcpSb
;
643 // First validate the parameters
646 return EFI_INVALID_PARAMETER
;
649 if (Dhcp4CfgData
!= NULL
) {
650 if ((Dhcp4CfgData
->DiscoverTryCount
!= 0) && (Dhcp4CfgData
->DiscoverTimeout
== NULL
)) {
651 return EFI_INVALID_PARAMETER
;
654 if ((Dhcp4CfgData
->RequestTryCount
!= 0) && (Dhcp4CfgData
->RequestTimeout
== NULL
)) {
655 return EFI_INVALID_PARAMETER
;
658 if ((Dhcp4CfgData
->OptionCount
!= 0) && (Dhcp4CfgData
->OptionList
== NULL
)) {
659 return EFI_INVALID_PARAMETER
;
662 CopyMem (&Ip
, &Dhcp4CfgData
->ClientAddress
, sizeof (IP4_ADDR
));
663 if (IP4_IS_LOCAL_BROADCAST(NTOHL (Ip
))) {
664 return EFI_INVALID_PARAMETER
;
668 Instance
= DHCP_INSTANCE_FROM_THIS (This
);
670 if (Instance
->Signature
!= DHCP_PROTOCOL_SIGNATURE
) {
671 return EFI_INVALID_PARAMETER
;
674 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
676 DhcpSb
= Instance
->Service
;
677 Config
= &DhcpSb
->ActiveConfig
;
679 Status
= EFI_ACCESS_DENIED
;
681 if ((DhcpSb
->DhcpState
!= Dhcp4Stopped
) &&
682 (DhcpSb
->DhcpState
!= Dhcp4Init
) &&
683 (DhcpSb
->DhcpState
!= Dhcp4InitReboot
) &&
684 (DhcpSb
->DhcpState
!= Dhcp4Bound
)) {
689 if ((DhcpSb
->ActiveChild
!= NULL
) && (DhcpSb
->ActiveChild
!= Instance
)) {
693 if (Dhcp4CfgData
!= NULL
) {
694 Status
= EFI_OUT_OF_RESOURCES
;
695 DhcpCleanConfigure (Config
);
697 if (EFI_ERROR (DhcpCopyConfigure (Config
, Dhcp4CfgData
))) {
701 DhcpSb
->UserOptionLen
= 0;
703 for (Index
= 0; Index
< Dhcp4CfgData
->OptionCount
; Index
++) {
704 DhcpSb
->UserOptionLen
+= Dhcp4CfgData
->OptionList
[Index
]->Length
+ 2;
707 DhcpSb
->ActiveChild
= Instance
;
709 if (DhcpSb
->DhcpState
== Dhcp4Stopped
) {
710 DhcpSb
->ClientAddr
= EFI_NTOHL (Dhcp4CfgData
->ClientAddress
);
712 if (DhcpSb
->ClientAddr
!= 0) {
713 DhcpSb
->DhcpState
= Dhcp4InitReboot
;
715 DhcpSb
->DhcpState
= Dhcp4Init
;
719 DhcpSb
->ServiceState
= DHCP_CONFIGED
;
720 Status
= EFI_SUCCESS
;
722 } else if (DhcpSb
->ActiveChild
== Instance
) {
723 Status
= EFI_SUCCESS
;
724 DhcpYieldControl (DhcpSb
);
728 gBS
->RestoreTPL (OldTpl
);
734 Starts the DHCP configuration process.
736 The Start() function starts the DHCP configuration process. This function can
737 be called only when the EFI DHCPv4 Protocol driver is in the Dhcp4Init or
738 Dhcp4InitReboot state.
739 If the DHCP process completes successfully, the state of the EFI DHCPv4 Protocol
740 driver will be transferred through Dhcp4Selecting and Dhcp4Requesting to the
741 Dhcp4Bound state. The CompletionEvent will then be signaled if it is not NULL.
742 If the process aborts, either by the user or by some unexpected network error,
743 the state is restored to the Dhcp4Init state. The Start() function can be called
744 again to restart the process.
745 Refer to RFC 2131 for precise state transitions during this process. At the
746 time when each event occurs in this process, the callback function that was set
747 by EFI_DHCP4_PROTOCOL.Configure() will be called and the user can take this
748 opportunity to control the process.
750 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
751 @param[in] CompletionEvent If not NULL, indicates the event that will be signaled when the
752 EFI DHCPv4 Protocol driver is transferred into the
753 Dhcp4Bound state or when the DHCP process is aborted.
754 EFI_DHCP4_PROTOCOL.GetModeData() can be called to
755 check the completion status. If NULL,
756 EFI_DHCP4_PROTOCOL.Start() will wait until the driver
757 is transferred into the Dhcp4Bound state or the process fails.
759 @retval EFI_SUCCESS The DHCP configuration process has started, or it has completed
760 when CompletionEvent is NULL.
761 @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped
762 state. EFI_DHCP4_PROTOCOL. Configure() needs to be called.
763 @retval EFI_INVALID_PARAMETER This is NULL.
764 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
765 @retval EFI_TIMEOUT The DHCP configuration process failed because no response was
766 received from the server within the specified timeout value.
767 @retval EFI_ABORTED The user aborted the DHCP process.
768 @retval EFI_ALREADY_STARTED Some other EFI DHCPv4 Protocol instance already started the
770 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
771 @retval EFI_NO_MEDIA There was a media error.
777 IN EFI_DHCP4_PROTOCOL
*This
,
778 IN EFI_EVENT CompletionEvent OPTIONAL
781 DHCP_PROTOCOL
*Instance
;
782 DHCP_SERVICE
*DhcpSb
;
785 EFI_STATUS MediaStatus
;
788 // First validate the parameters
791 return EFI_INVALID_PARAMETER
;
794 Instance
= DHCP_INSTANCE_FROM_THIS (This
);
796 if (Instance
->Signature
!= DHCP_PROTOCOL_SIGNATURE
) {
797 return EFI_INVALID_PARAMETER
;
800 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
801 DhcpSb
= Instance
->Service
;
803 if (DhcpSb
->DhcpState
== Dhcp4Stopped
) {
804 Status
= EFI_NOT_STARTED
;
808 if ((DhcpSb
->DhcpState
!= Dhcp4Init
) && (DhcpSb
->DhcpState
!= Dhcp4InitReboot
)) {
809 Status
= EFI_ALREADY_STARTED
;
814 // Check Media Satus.
816 MediaStatus
= EFI_SUCCESS
;
817 NetLibDetectMediaWaitTimeout (DhcpSb
->Controller
, DHCP_CHECK_MEDIA_WAITING_TIME
, &MediaStatus
);
818 if (MediaStatus
!= EFI_SUCCESS
) {
819 Status
= EFI_NO_MEDIA
;
823 DhcpSb
->IoStatus
= EFI_ALREADY_STARTED
;
825 if (EFI_ERROR (Status
= DhcpInitRequest (DhcpSb
))) {
830 Instance
->CompletionEvent
= CompletionEvent
;
833 // Restore the TPL now, don't call poll function at TPL_CALLBACK.
835 gBS
->RestoreTPL (OldTpl
);
837 if (CompletionEvent
== NULL
) {
838 while (DhcpSb
->IoStatus
== EFI_ALREADY_STARTED
) {
839 DhcpSb
->UdpIo
->Protocol
.Udp4
->Poll (DhcpSb
->UdpIo
->Protocol
.Udp4
);
842 return DhcpSb
->IoStatus
;
848 gBS
->RestoreTPL (OldTpl
);
854 Extends the lease time by sending a request packet.
856 The RenewRebind() function is used to manually extend the lease time when the
857 EFI DHCPv4 Protocol driver is in the Dhcp4Bound state and the lease time has
858 not expired yet. This function will send a request packet to the previously
859 found server (or to any server when RebindRequest is TRUE) and transfer the
860 state into the Dhcp4Renewing state (or Dhcp4Rebinding when RebindingRequest is
861 TRUE). When a response is received, the state is returned to Dhcp4Bound.
862 If no response is received before the try count is exceeded (the RequestTryCount
863 field that is specified in EFI_DHCP4_CONFIG_DATA) but before the lease time that
864 was issued by the previous server expires, the driver will return to the Dhcp4Bound
865 state and the previous configuration is restored. The outgoing and incoming packets
866 can be captured by the EFI_DHCP4_CALLBACK function.
868 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
869 @param[in] RebindRequest If TRUE, this function broadcasts the request packets and enters
870 the Dhcp4Rebinding state. Otherwise, it sends a unicast
871 request packet and enters the Dhcp4Renewing state.
872 @param[in] CompletionEvent If not NULL, this event is signaled when the renew/rebind phase
873 completes or some error occurs.
874 EFI_DHCP4_PROTOCOL.GetModeData() can be called to
875 check the completion status. If NULL,
876 EFI_DHCP4_PROTOCOL.RenewRebind() will busy-wait
877 until the DHCP process finishes.
879 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the
880 Dhcp4Renewing state or is back to the Dhcp4Bound state.
881 @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped
882 state. EFI_DHCP4_PROTOCOL.Configure() needs to
884 @retval EFI_INVALID_PARAMETER This is NULL.
885 @retval EFI_TIMEOUT There was no response from the server when the try count was
887 @retval EFI_ACCESS_DENIED The driver is not in the Dhcp4Bound state.
888 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
893 EfiDhcp4RenewRebind (
894 IN EFI_DHCP4_PROTOCOL
*This
,
895 IN BOOLEAN RebindRequest
,
896 IN EFI_EVENT CompletionEvent OPTIONAL
899 DHCP_PROTOCOL
*Instance
;
900 DHCP_SERVICE
*DhcpSb
;
905 // First validate the parameters
908 return EFI_INVALID_PARAMETER
;
911 Instance
= DHCP_INSTANCE_FROM_THIS (This
);
913 if (Instance
->Signature
!= DHCP_PROTOCOL_SIGNATURE
) {
914 return EFI_INVALID_PARAMETER
;
917 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
918 DhcpSb
= Instance
->Service
;
920 if (DhcpSb
->DhcpState
== Dhcp4Stopped
) {
921 Status
= EFI_NOT_STARTED
;
925 if (DhcpSb
->DhcpState
!= Dhcp4Bound
) {
926 Status
= EFI_ACCESS_DENIED
;
930 if (DHCP_IS_BOOTP (DhcpSb
->Para
)) {
931 Status
= EFI_SUCCESS
;
936 // Transit the states then send a extra DHCP request
938 if (!RebindRequest
) {
939 DhcpSetState (DhcpSb
, Dhcp4Renewing
, FALSE
);
941 DhcpSetState (DhcpSb
, Dhcp4Rebinding
, FALSE
);
945 // Clear initial time to make sure that elapsed-time
946 // is set to 0 for first REQUEST in renewal process.
948 Instance
->ElaspedTime
= 0;
950 Status
= DhcpSendMessage (
955 (UINT8
*) "Extra renew/rebind by the application"
958 if (EFI_ERROR (Status
)) {
959 DhcpSetState (DhcpSb
, Dhcp4Bound
, FALSE
);
963 DhcpSb
->ExtraRefresh
= TRUE
;
964 DhcpSb
->IoStatus
= EFI_ALREADY_STARTED
;
965 Instance
->RenewRebindEvent
= CompletionEvent
;
967 gBS
->RestoreTPL (OldTpl
);
969 if (CompletionEvent
== NULL
) {
970 while (DhcpSb
->IoStatus
== EFI_ALREADY_STARTED
) {
971 DhcpSb
->UdpIo
->Protocol
.Udp4
->Poll (DhcpSb
->UdpIo
->Protocol
.Udp4
);
975 return DhcpSb
->IoStatus
;
981 gBS
->RestoreTPL (OldTpl
);
987 Releases the current address configuration.
989 The Release() function releases the current configured IP address by doing either
991 * Sending a DHCPRELEASE packet when the EFI DHCPv4 Protocol driver is in the
993 * Setting the previously assigned IP address that was provided with the
994 EFI_DHCP4_PROTOCOL.Configure() function to 0.0.0.0 when the driver is in
995 Dhcp4InitReboot state
996 After a successful call to this function, the EFI DHCPv4 Protocol driver returns
997 to the Dhcp4Init state and any subsequent incoming packets will be discarded silently.
999 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
1001 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init phase.
1002 @retval EFI_INVALID_PARAMETER This is NULL.
1003 @retval EFI_ACCESS_DENIED The EFI DHCPv4 Protocol driver is not Dhcp4InitReboot state.
1004 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1010 IN EFI_DHCP4_PROTOCOL
*This
1013 DHCP_PROTOCOL
*Instance
;
1014 DHCP_SERVICE
*DhcpSb
;
1019 // First validate the parameters
1022 return EFI_INVALID_PARAMETER
;
1025 Instance
= DHCP_INSTANCE_FROM_THIS (This
);
1027 if (Instance
->Signature
!= DHCP_PROTOCOL_SIGNATURE
) {
1028 return EFI_INVALID_PARAMETER
;
1031 Status
= EFI_SUCCESS
;
1032 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1033 DhcpSb
= Instance
->Service
;
1035 if ((DhcpSb
->DhcpState
!= Dhcp4InitReboot
) && (DhcpSb
->DhcpState
!= Dhcp4Bound
)) {
1036 Status
= EFI_ACCESS_DENIED
;
1040 if (!DHCP_IS_BOOTP (DhcpSb
->Para
) && (DhcpSb
->DhcpState
== Dhcp4Bound
)) {
1041 Status
= DhcpSendMessage (
1049 if (EFI_ERROR (Status
)) {
1050 Status
= EFI_DEVICE_ERROR
;
1055 DhcpCleanLease (DhcpSb
);
1058 gBS
->RestoreTPL (OldTpl
);
1064 Stops the current address configuration.
1066 The Stop() function is used to stop the DHCP configuration process. After this
1067 function is called successfully, the EFI DHCPv4 Protocol driver is transferred
1068 into the Dhcp4Stopped state. EFI_DHCP4_PROTOCOL.Configure() needs to be called
1069 before DHCP configuration process can be started again. This function can be
1070 called when the EFI DHCPv4 Protocol driver is in any state.
1072 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
1074 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Stopped phase.
1075 @retval EFI_INVALID_PARAMETER This is NULL.
1081 IN EFI_DHCP4_PROTOCOL
*This
1084 DHCP_PROTOCOL
*Instance
;
1085 DHCP_SERVICE
*DhcpSb
;
1089 // First validate the parameters
1092 return EFI_INVALID_PARAMETER
;
1095 Instance
= DHCP_INSTANCE_FROM_THIS (This
);
1097 if (Instance
->Signature
!= DHCP_PROTOCOL_SIGNATURE
) {
1098 return EFI_INVALID_PARAMETER
;
1101 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1102 DhcpSb
= Instance
->Service
;
1104 DhcpCleanLease (DhcpSb
);
1106 DhcpSb
->DhcpState
= Dhcp4Stopped
;
1107 DhcpSb
->ServiceState
= DHCP_UNCONFIGED
;
1109 gBS
->RestoreTPL (OldTpl
);
1115 Builds a DHCP packet, given the options to be appended or deleted or replaced.
1117 The Build() function is used to assemble a new packet from the original packet
1118 by replacing or deleting existing options or appending new options. This function
1119 does not change any state of the EFI DHCPv4 Protocol driver and can be used at
1122 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
1123 @param[in] SeedPacket Initial packet to be used as a base for building new packet.
1124 @param[in] DeleteCount Number of opcodes in the DeleteList.
1125 @param[in] DeleteList List of opcodes to be deleted from the seed packet.
1126 Ignored if DeleteCount is zero.
1127 @param[in] AppendCount Number of entries in the OptionList.
1128 @param[in] AppendList Pointer to a DHCP option list to be appended to SeedPacket.
1129 If SeedPacket also contains options in this list, they are
1130 replaced by new options (except pad option). Ignored if
1131 AppendCount is zero. Type EFI_DHCP4_PACKET_OPTION
1132 @param[out] NewPacket Pointer to storage for the pointer to the new allocated packet.
1133 Use the EFI Boot Service FreePool() on the resulting pointer
1134 when done with the packet.
1136 @retval EFI_SUCCESS The new packet was built.
1137 @retval EFI_OUT_OF_RESOURCES Storage for the new packet could not be allocated.
1138 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
1144 IN EFI_DHCP4_PROTOCOL
*This
,
1145 IN EFI_DHCP4_PACKET
*SeedPacket
,
1146 IN UINT32 DeleteCount
,
1147 IN UINT8
*DeleteList OPTIONAL
,
1148 IN UINT32 AppendCount
,
1149 IN EFI_DHCP4_PACKET_OPTION
*AppendList
[] OPTIONAL
,
1150 OUT EFI_DHCP4_PACKET
**NewPacket
1154 // First validate the parameters
1156 if ((This
== NULL
) || (NewPacket
== NULL
)) {
1157 return EFI_INVALID_PARAMETER
;
1160 if ((SeedPacket
== NULL
) || (SeedPacket
->Dhcp4
.Magik
!= DHCP_OPTION_MAGIC
) ||
1161 EFI_ERROR (DhcpValidateOptions (SeedPacket
, NULL
))) {
1163 return EFI_INVALID_PARAMETER
;
1166 if (((DeleteCount
== 0) && (AppendCount
== 0)) ||
1167 ((DeleteCount
!= 0) && (DeleteList
== NULL
)) ||
1168 ((AppendCount
!= 0) && (AppendList
== NULL
))) {
1170 return EFI_INVALID_PARAMETER
;
1184 Callback by UdpIoCreatePort() when creating UdpIo for this Dhcp4 instance.
1186 @param[in] UdpIo The UdpIo being created.
1187 @param[in] Context Dhcp4 instance.
1189 @retval EFI_SUCCESS UdpIo is configured successfully.
1190 @retval EFI_INVALID_PARAMETER Class E IP address is not supported or other parameters
1192 @retval other Other error occurs.
1196 Dhcp4InstanceConfigUdpIo (
1201 DHCP_PROTOCOL
*Instance
;
1202 DHCP_SERVICE
*DhcpSb
;
1203 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN
*Token
;
1204 EFI_UDP4_CONFIG_DATA UdpConfigData
;
1205 IP4_ADDR ClientAddr
;
1208 IP4_ADDR SubnetMask
;
1210 Instance
= (DHCP_PROTOCOL
*) Context
;
1211 DhcpSb
= Instance
->Service
;
1212 Token
= Instance
->Token
;
1214 ZeroMem (&UdpConfigData
, sizeof (EFI_UDP4_CONFIG_DATA
));
1216 UdpConfigData
.AcceptBroadcast
= TRUE
;
1217 UdpConfigData
.AllowDuplicatePort
= TRUE
;
1218 UdpConfigData
.TimeToLive
= 64;
1219 UdpConfigData
.DoNotFragment
= TRUE
;
1221 ClientAddr
= EFI_NTOHL (Token
->Packet
->Dhcp4
.Header
.ClientAddr
);
1222 Ip
= HTONL (ClientAddr
);
1223 CopyMem (&UdpConfigData
.StationAddress
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
1225 if (DhcpSb
->Netmask
== 0) {
1227 // The Dhcp4.TransmitReceive() API should be able to used at any time according to
1228 // UEFI spec, while in classless addressing network, the netmask must be explicitly
1229 // provided together with the station address.
1230 // If the DHCP instance haven't be configured with a valid netmask, we could only
1231 // compute it according to the classful addressing rule.
1233 Class
= NetGetIpClass (ClientAddr
);
1235 // Class E IP address is not supported here!
1237 ASSERT (Class
< IP4_ADDR_CLASSE
);
1238 if (Class
>= IP4_ADDR_CLASSE
) {
1239 return EFI_INVALID_PARAMETER
;
1242 SubnetMask
= gIp4AllMasks
[Class
<< 3];
1244 SubnetMask
= DhcpSb
->Netmask
;
1247 Ip
= HTONL (SubnetMask
);
1248 CopyMem (&UdpConfigData
.SubnetMask
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
1250 if ((Token
->ListenPointCount
== 0) || (Token
->ListenPoints
[0].ListenPort
== 0)) {
1251 UdpConfigData
.StationPort
= DHCP_CLIENT_PORT
;
1253 UdpConfigData
.StationPort
= Token
->ListenPoints
[0].ListenPort
;
1256 return UdpIo
->Protocol
.Udp4
->Configure (UdpIo
->Protocol
.Udp4
, &UdpConfigData
);
1260 Create UdpIo for this Dhcp4 instance.
1262 @param Instance The Dhcp4 instance.
1264 @retval EFI_SUCCESS UdpIo is created successfully.
1265 @retval EFI_OUT_OF_RESOURCES Fails to create UdpIo because of limited
1266 resources or configuration failure.
1269 Dhcp4InstanceCreateUdpIo (
1270 IN OUT DHCP_PROTOCOL
*Instance
1273 DHCP_SERVICE
*DhcpSb
;
1277 ASSERT (Instance
->Token
!= NULL
);
1279 DhcpSb
= Instance
->Service
;
1280 Instance
->UdpIo
= UdpIoCreateIo (
1283 Dhcp4InstanceConfigUdpIo
,
1284 UDP_IO_UDP4_VERSION
,
1287 if (Instance
->UdpIo
== NULL
) {
1288 return EFI_OUT_OF_RESOURCES
;
1290 Status
= gBS
->OpenProtocol (
1291 Instance
->UdpIo
->UdpHandle
,
1292 &gEfiUdp4ProtocolGuid
,
1294 Instance
->Service
->Image
,
1296 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1298 if (EFI_ERROR (Status
)) {
1299 UdpIoFreeIo (Instance
->UdpIo
);
1300 Instance
->UdpIo
= NULL
;
1307 Callback of Dhcp packet. Does nothing.
1309 @param Arg The context.
1321 Callback of UdpIoRecvDatagram() that handles a Dhcp4 packet.
1323 Only BOOTP responses will be handled that correspond to the Xid of the request
1324 sent out. The packet will be queued to the response queue.
1326 @param UdpPacket The Dhcp4 packet.
1327 @param EndPoint Udp4 address pair.
1328 @param IoStatus Status of the input.
1329 @param Context Extra info for the input.
1336 UDP_END_POINT
*EndPoint
,
1337 EFI_STATUS IoStatus
,
1341 DHCP_PROTOCOL
*Instance
;
1342 EFI_DHCP4_HEADER
*Head
;
1344 EFI_DHCP4_PACKET
*Packet
;
1345 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN
*Token
;
1350 Instance
= (DHCP_PROTOCOL
*) Context
;
1351 Token
= Instance
->Token
;
1354 // Don't restart receive if error occurs or DHCP is destroyed.
1356 if (EFI_ERROR (IoStatus
)) {
1360 ASSERT (UdpPacket
!= NULL
);
1363 // Validate the packet received
1365 if (UdpPacket
->TotalSize
< sizeof (EFI_DHCP4_HEADER
)) {
1370 // Copy the DHCP message to a continuous memory block, make the buffer size
1371 // of the EFI_DHCP4_PACKET a multiple of 4-byte.
1373 Len
= NET_ROUNDUP (sizeof (EFI_DHCP4_PACKET
) + UdpPacket
->TotalSize
- sizeof (EFI_DHCP4_HEADER
), 4);
1374 Wrap
= NetbufAlloc (Len
);
1379 Packet
= (EFI_DHCP4_PACKET
*) NetbufAllocSpace (Wrap
, Len
, NET_BUF_TAIL
);
1380 ASSERT (Packet
!= NULL
);
1383 Head
= &Packet
->Dhcp4
.Header
;
1384 Packet
->Length
= NetbufCopy (UdpPacket
, 0, UdpPacket
->TotalSize
, (UINT8
*) Head
);
1386 if (Packet
->Length
!= UdpPacket
->TotalSize
) {
1391 // Is this packet the answer to our packet?
1393 if ((Head
->OpCode
!= BOOTP_REPLY
) ||
1394 (Head
->Xid
!= Token
->Packet
->Dhcp4
.Header
.Xid
) ||
1395 (CompareMem (&Token
->Packet
->Dhcp4
.Header
.ClientHwAddr
[0], Head
->ClientHwAddr
, Head
->HwAddrLen
) != 0)) {
1400 // Validate the options and retrieve the interested options
1402 if ((Packet
->Length
> sizeof (EFI_DHCP4_HEADER
) + sizeof (UINT32
)) &&
1403 (Packet
->Dhcp4
.Magik
== DHCP_OPTION_MAGIC
) &&
1404 EFI_ERROR (DhcpValidateOptions (Packet
, NULL
))) {
1410 // Keep this packet in the ResponseQueue.
1413 NetbufQueAppend (&Instance
->ResponseQueue
, Wrap
);
1417 NetbufFree (UdpPacket
);
1423 Status
= UdpIoRecvDatagram (Instance
->UdpIo
, PxeDhcpInput
, Instance
, 0);
1424 if (EFI_ERROR (Status
)) {
1425 PxeDhcpDone (Instance
);
1430 Complete a Dhcp4 transaction and signal the upper layer.
1432 @param Instance Dhcp4 instance.
1437 IN DHCP_PROTOCOL
*Instance
1440 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN
*Token
;
1442 Token
= Instance
->Token
;
1444 Token
->ResponseCount
= Instance
->ResponseQueue
.BufNum
;
1445 if (Token
->ResponseCount
!= 0) {
1446 Token
->ResponseList
= (EFI_DHCP4_PACKET
*) AllocatePool (Instance
->ResponseQueue
.BufSize
);
1447 if (Token
->ResponseList
== NULL
) {
1448 Token
->Status
= EFI_OUT_OF_RESOURCES
;
1453 // Copy the received DHCP responses.
1455 NetbufQueCopy (&Instance
->ResponseQueue
, 0, Instance
->ResponseQueue
.BufSize
, (UINT8
*) Token
->ResponseList
);
1456 Token
->Status
= EFI_SUCCESS
;
1458 Token
->ResponseList
= NULL
;
1459 Token
->Status
= EFI_TIMEOUT
;
1464 // Clean up the resources dedicated for this transmit receive transaction.
1466 NetbufQueFlush (&Instance
->ResponseQueue
);
1467 UdpIoCleanIo (Instance
->UdpIo
);
1468 gBS
->CloseProtocol (
1469 Instance
->UdpIo
->UdpHandle
,
1470 &gEfiUdp4ProtocolGuid
,
1471 Instance
->Service
->Image
,
1474 UdpIoFreeIo (Instance
->UdpIo
);
1475 Instance
->UdpIo
= NULL
;
1476 Instance
->Token
= NULL
;
1478 if (Token
->CompletionEvent
!= NULL
) {
1479 gBS
->SignalEvent (Token
->CompletionEvent
);
1485 Transmits a DHCP formatted packet and optionally waits for responses.
1487 The TransmitReceive() function is used to transmit a DHCP packet and optionally
1488 wait for the response from servers. This function does not change the state of
1489 the EFI DHCPv4 Protocol driver and thus can be used at any time.
1491 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
1492 @param[in] Token Pointer to the EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN structure.
1494 @retval EFI_SUCCESS The packet was successfully queued for transmission.
1495 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
1496 @retval EFI_NOT_READY The previous call to this function has not finished yet. Try to call
1497 this function after collection process completes.
1498 @retval EFI_NO_MAPPING The default station address is not available yet.
1499 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
1500 @retval Others Some other unexpected error occurred.
1505 EfiDhcp4TransmitReceive (
1506 IN EFI_DHCP4_PROTOCOL
*This
,
1507 IN EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN
*Token
1510 DHCP_PROTOCOL
*Instance
;
1515 UDP_END_POINT EndPoint
;
1517 DHCP_SERVICE
*DhcpSb
;
1518 EFI_IP_ADDRESS Gateway
;
1519 IP4_ADDR ClientAddr
;
1521 if ((This
== NULL
) || (Token
== NULL
) || (Token
->Packet
== NULL
)) {
1522 return EFI_INVALID_PARAMETER
;
1525 Instance
= DHCP_INSTANCE_FROM_THIS (This
);
1526 DhcpSb
= Instance
->Service
;
1528 if (Instance
->Token
!= NULL
) {
1530 // The previous call to TransmitReceive is not finished.
1532 return EFI_NOT_READY
;
1535 if ((Token
->Packet
->Dhcp4
.Magik
!= DHCP_OPTION_MAGIC
) ||
1536 (NTOHL (Token
->Packet
->Dhcp4
.Header
.Xid
) == Instance
->Service
->Xid
) ||
1537 (Token
->TimeoutValue
== 0) ||
1538 ((Token
->ListenPointCount
!= 0) && (Token
->ListenPoints
== NULL
)) ||
1539 EFI_ERROR (DhcpValidateOptions (Token
->Packet
, NULL
)) ||
1540 EFI_IP4_EQUAL (&Token
->RemoteAddress
, &mZeroIp4Addr
)
1543 // The DHCP packet isn't well-formed, the Transaction ID is already used,
1544 // the timeout value is zero, the ListenPoint is invalid, or the
1545 // RemoteAddress is zero.
1547 return EFI_INVALID_PARAMETER
;
1550 ClientAddr
= EFI_NTOHL (Token
->Packet
->Dhcp4
.Header
.ClientAddr
);
1552 if (ClientAddr
== 0) {
1553 return EFI_NO_MAPPING
;
1556 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1559 // Save the token and the timeout value.
1561 Instance
->Token
= Token
;
1562 Instance
->Timeout
= Token
->TimeoutValue
;
1565 // Create a UDP IO for this transmit receive transaction.
1567 Status
= Dhcp4InstanceCreateUdpIo (Instance
);
1568 if (EFI_ERROR (Status
)) {
1573 // Save the Client Address is sent out
1576 &DhcpSb
->ClientAddressSendOut
[0],
1577 &Token
->Packet
->Dhcp4
.Header
.ClientHwAddr
[0],
1578 Token
->Packet
->Dhcp4
.Header
.HwAddrLen
1582 // Wrap the DHCP packet into a net buffer.
1584 Frag
.Bulk
= (UINT8
*) &Token
->Packet
->Dhcp4
;
1585 Frag
.Len
= Token
->Packet
->Length
;
1586 Wrap
= NetbufFromExt (&Frag
, 1, 0, 0, DhcpDummyExtFree
, NULL
);
1588 Status
= EFI_OUT_OF_RESOURCES
;
1593 // Set the local address and local port to ZERO.
1595 ZeroMem (&EndPoint
, sizeof (UDP_END_POINT
));
1598 // Set the destination address and destination port.
1600 CopyMem (&Ip
, &Token
->RemoteAddress
, sizeof (EFI_IPv4_ADDRESS
));
1601 EndPoint
.RemoteAddr
.Addr
[0] = NTOHL (Ip
);
1603 if (Token
->RemotePort
== 0) {
1604 EndPoint
.RemotePort
= DHCP_SERVER_PORT
;
1606 EndPoint
.RemotePort
= Token
->RemotePort
;
1612 ZeroMem (&Gateway
, sizeof (Gateway
));
1613 if (!IP4_NET_EQUAL (ClientAddr
, EndPoint
.RemoteAddr
.Addr
[0], DhcpSb
->Netmask
)) {
1614 CopyMem (&Gateway
.v4
, &Token
->GatewayAddress
, sizeof (EFI_IPv4_ADDRESS
));
1615 Gateway
.Addr
[0] = NTOHL (Gateway
.Addr
[0]);
1619 // Transmit the DHCP packet.
1621 Status
= UdpIoSendDatagram (Instance
->UdpIo
, Wrap
, &EndPoint
, &Gateway
, DhcpOnPacketSent
, NULL
);
1622 if (EFI_ERROR (Status
)) {
1628 // Start to receive the DHCP response.
1630 Status
= UdpIoRecvDatagram (Instance
->UdpIo
, PxeDhcpInput
, Instance
, 0);
1631 if (EFI_ERROR (Status
)) {
1637 if (EFI_ERROR (Status
) && (Instance
->UdpIo
!= NULL
)) {
1638 UdpIoCleanIo (Instance
->UdpIo
);
1639 gBS
->CloseProtocol (
1640 Instance
->UdpIo
->UdpHandle
,
1641 &gEfiUdp4ProtocolGuid
,
1642 Instance
->Service
->Image
,
1645 UdpIoFreeIo (Instance
->UdpIo
);
1646 Instance
->UdpIo
= NULL
;
1647 Instance
->Token
= NULL
;
1650 gBS
->RestoreTPL (OldTpl
);
1652 if (!EFI_ERROR (Status
) && (Token
->CompletionEvent
== NULL
)) {
1654 // Keep polling until timeout if no error happens and the CompletionEvent
1658 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1660 // Raise TPL to protect the UDPIO in instance, in case that DhcpOnTimerTick
1661 // free it when timeout.
1663 if (Instance
->Timeout
> 0) {
1664 Instance
->UdpIo
->Protocol
.Udp4
->Poll (Instance
->UdpIo
->Protocol
.Udp4
);
1665 gBS
->RestoreTPL (OldTpl
);
1667 gBS
->RestoreTPL (OldTpl
);
1678 Callback function for DhcpIterateOptions. This callback sets the
1679 EFI_DHCP4_PACKET_OPTION array in the DHCP_PARSE_CONTEXT to point
1680 the individual DHCP option in the packet.
1682 @param[in] Tag The DHCP option type
1683 @param[in] Len Length of the DHCP option data
1684 @param[in] Data The DHCP option data
1685 @param[in] Context The context, to pass several parameters in.
1687 @retval EFI_SUCCESS It always returns EFI_SUCCESS
1691 Dhcp4ParseCheckOption (
1698 DHCP_PARSE_CONTEXT
*Parse
;
1700 Parse
= (DHCP_PARSE_CONTEXT
*) Context
;
1703 if (Parse
->Index
<= Parse
->OptionCount
) {
1705 // Use BASE_CR to get the memory position of EFI_DHCP4_PACKET_OPTION for
1706 // the EFI_DHCP4_PACKET_OPTION->Data because DhcpIterateOptions only
1707 // pass in the point to option data.
1709 Parse
->Option
[Parse
->Index
- 1] = BASE_CR (Data
, EFI_DHCP4_PACKET_OPTION
, Data
);
1717 Parses the packed DHCP option data.
1719 The Parse() function is used to retrieve the option list from a DHCP packet.
1720 If *OptionCount isn't zero, and there is enough space for all the DHCP options
1721 in the Packet, each element of PacketOptionList is set to point to somewhere in
1722 the Packet->Dhcp4.Option where a new DHCP option begins. If RFC3396 is supported,
1723 the caller should reassemble the parsed DHCP options to get the finial result.
1724 If *OptionCount is zero or there isn't enough space for all of them, the number
1725 of DHCP options in the Packet is returned in OptionCount.
1727 @param This Pointer to the EFI_DHCP4_PROTOCOL instance.
1728 @param Packet Pointer to packet to be parsed.
1729 @param OptionCount On input, the number of entries in the PacketOptionList.
1730 On output, the number of entries that were written into the
1732 @param PacketOptionList List of packet option entries to be filled in. End option or pad
1733 options are not included.
1735 @retval EFI_SUCCESS The packet was successfully parsed.
1736 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
1737 @retval EFI_BUFFER_TOO_SMALL One or more of the following conditions is TRUE:
1738 1) *OptionCount is smaller than the number of options that
1739 were found in the Packet.
1740 2) PacketOptionList is NULL.
1746 IN EFI_DHCP4_PROTOCOL
*This
,
1747 IN EFI_DHCP4_PACKET
*Packet
,
1748 IN OUT UINT32
*OptionCount
,
1749 OUT EFI_DHCP4_PACKET_OPTION
*PacketOptionList
[] OPTIONAL
1752 DHCP_PARSE_CONTEXT Context
;
1756 // First validate the parameters
1758 if ((This
== NULL
) || (Packet
== NULL
) || (OptionCount
== NULL
)) {
1759 return EFI_INVALID_PARAMETER
;
1762 if ((Packet
->Size
< Packet
->Length
+ 2 * sizeof (UINT32
)) ||
1763 (Packet
->Dhcp4
.Magik
!= DHCP_OPTION_MAGIC
) ||
1764 EFI_ERROR (DhcpValidateOptions (Packet
, NULL
))) {
1766 return EFI_INVALID_PARAMETER
;
1769 if ((*OptionCount
!= 0) && (PacketOptionList
== NULL
)) {
1770 return EFI_BUFFER_TOO_SMALL
;
1773 ZeroMem (PacketOptionList
, *OptionCount
* sizeof (EFI_DHCP4_PACKET_OPTION
*));
1775 Context
.Option
= PacketOptionList
;
1776 Context
.OptionCount
= *OptionCount
;
1779 Status
= DhcpIterateOptions (Packet
, Dhcp4ParseCheckOption
, &Context
);
1781 if (EFI_ERROR (Status
)) {
1785 *OptionCount
= Context
.Index
;
1787 if (Context
.Index
> Context
.OptionCount
) {
1788 return EFI_BUFFER_TOO_SMALL
;
1795 Set the elapsed time based on the given instance and the pointer to the
1796 elapsed time option.
1798 @param[in] Elapsed The pointer to the position to append.
1799 @param[in] Instance The pointer to the Dhcp4 instance.
1804 IN DHCP_PROTOCOL
*Instance
1807 WriteUnaligned16 (Elapsed
, HTONS(Instance
->ElaspedTime
));