2 This file implement the EFI_DHCP4_PROTOCOL interface.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "Dhcp4Impl.h"
13 Returns the current operating mode and cached data packet for the EFI DHCPv4 Protocol driver.
15 The GetModeData() function returns the current operating mode and cached data
16 packet for the EFI DHCPv4 Protocol driver.
18 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
19 @param[out] Dhcp4ModeData Pointer to storage for the EFI_DHCP4_MODE_DATA structure.
21 @retval EFI_SUCCESS The mode data was returned.
22 @retval EFI_INVALID_PARAMETER This is NULL.
28 IN EFI_DHCP4_PROTOCOL
*This
,
29 OUT EFI_DHCP4_MODE_DATA
*Dhcp4ModeData
33 Initializes, changes, or resets the operational settings for the EFI DHCPv4 Protocol driver.
35 The Configure() function is used to initialize, change, or reset the operational
36 settings of the EFI DHCPv4 Protocol driver for the communication device on which
37 the EFI DHCPv4 Service Binding Protocol is installed. This function can be
38 successfully called only if both of the following are true:
39 * This instance of the EFI DHCPv4 Protocol driver is in the Dhcp4Stopped, Dhcp4Init,
40 Dhcp4InitReboot, or Dhcp4Bound states.
41 * No other EFI DHCPv4 Protocol driver instance that is controlled by this EFI
42 DHCPv4 Service Binding Protocol driver instance has configured this EFI DHCPv4
44 When this driver is in the Dhcp4Stopped state, it can transfer into one of the
45 following two possible initial states:
48 The driver can transfer into these states by calling Configure() with a non-NULL
49 Dhcp4CfgData. The driver will transfer into the appropriate state based on the
50 supplied client network address in the ClientAddress parameter and DHCP options
51 in the OptionList parameter as described in RFC 2131.
52 When Configure() is called successfully while Dhcp4CfgData is set to NULL, the
53 default configuring data will be reset in the EFI DHCPv4 Protocol driver and
54 the state of the EFI DHCPv4 Protocol driver will not be changed. If one instance
55 wants to make it possible for another instance to configure the EFI DHCPv4 Protocol
56 driver, it must call this function with Dhcp4CfgData set to NULL.
58 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
59 @param[in] Dhcp4CfgData Pointer to the EFI_DHCP4_CONFIG_DATA.
61 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init or
62 Dhcp4InitReboot state, if the original state of this driver
63 was Dhcp4Stopped and the value of Dhcp4CfgData was
64 not NULL. Otherwise, the state was left unchanged.
65 @retval EFI_ACCESS_DENIED This instance of the EFI DHCPv4 Protocol driver was not in the
66 Dhcp4Stopped, Dhcp4Init, Dhcp4InitReboot, or Dhcp4Bound state;
67 Or another instance of this EFI DHCPv4 Protocol driver is already
68 in a valid configured state.
69 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
70 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
71 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
77 IN EFI_DHCP4_PROTOCOL
*This
,
78 IN EFI_DHCP4_CONFIG_DATA
*Dhcp4CfgData OPTIONAL
82 Starts the DHCP configuration process.
84 The Start() function starts the DHCP configuration process. This function can
85 be called only when the EFI DHCPv4 Protocol driver is in the Dhcp4Init or
86 Dhcp4InitReboot state.
87 If the DHCP process completes successfully, the state of the EFI DHCPv4 Protocol
88 driver will be transferred through Dhcp4Selecting and Dhcp4Requesting to the
89 Dhcp4Bound state. The CompletionEvent will then be signaled if it is not NULL.
90 If the process aborts, either by the user or by some unexpected network error,
91 the state is restored to the Dhcp4Init state. The Start() function can be called
92 again to restart the process.
93 Refer to RFC 2131 for precise state transitions during this process. At the
94 time when each event occurs in this process, the callback function that was set
95 by EFI_DHCP4_PROTOCOL.Configure() will be called and the user can take this
96 opportunity to control the process.
98 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
99 @param[in] CompletionEvent If not NULL, indicates the event that will be signaled when the
100 EFI DHCPv4 Protocol driver is transferred into the
101 Dhcp4Bound state or when the DHCP process is aborted.
102 EFI_DHCP4_PROTOCOL.GetModeData() can be called to
103 check the completion status. If NULL,
104 EFI_DHCP4_PROTOCOL.Start() will wait until the driver
105 is transferred into the Dhcp4Bound state or the process fails.
107 @retval EFI_SUCCESS The DHCP configuration process has started, or it has completed
108 when CompletionEvent is NULL.
109 @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped
110 state. EFI_DHCP4_PROTOCOL. Configure() needs to be called.
111 @retval EFI_INVALID_PARAMETER This is NULL.
112 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
113 @retval EFI_TIMEOUT The DHCP configuration process failed because no response was
114 received from the server within the specified timeout value.
115 @retval EFI_ABORTED The user aborted the DHCP process.
116 @retval EFI_ALREADY_STARTED Some other EFI DHCPv4 Protocol instance already started the
118 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
124 IN EFI_DHCP4_PROTOCOL
*This
,
125 IN EFI_EVENT CompletionEvent OPTIONAL
129 Extends the lease time by sending a request packet.
131 The RenewRebind() function is used to manually extend the lease time when the
132 EFI DHCPv4 Protocol driver is in the Dhcp4Bound state and the lease time has
133 not expired yet. This function will send a request packet to the previously
134 found server (or to any server when RebindRequest is TRUE) and transfer the
135 state into the Dhcp4Renewing state (or Dhcp4Rebinding when RebindingRequest is
136 TRUE). When a response is received, the state is returned to Dhcp4Bound.
137 If no response is received before the try count is exceeded (the RequestTryCount
138 field that is specified in EFI_DHCP4_CONFIG_DATA) but before the lease time that
139 was issued by the previous server expires, the driver will return to the Dhcp4Bound
140 state and the previous configuration is restored. The outgoing and incoming packets
141 can be captured by the EFI_DHCP4_CALLBACK function.
143 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
144 @param[in] RebindRequest If TRUE, this function broadcasts the request packets and enters
145 the Dhcp4Rebinding state. Otherwise, it sends a unicast
146 request packet and enters the Dhcp4Renewing state.
147 @param[in] CompletionEvent If not NULL, this event is signaled when the renew/rebind phase
148 completes or some error occurs.
149 EFI_DHCP4_PROTOCOL.GetModeData() can be called to
150 check the completion status. If NULL,
151 EFI_DHCP4_PROTOCOL.RenewRebind() will busy-wait
152 until the DHCP process finishes.
154 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the
155 Dhcp4Renewing state or is back to the Dhcp4Bound state.
156 @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped
157 state. EFI_DHCP4_PROTOCOL.Configure() needs to
159 @retval EFI_INVALID_PARAMETER This is NULL.
160 @retval EFI_TIMEOUT There was no response from the server when the try count was
162 @retval EFI_ACCESS_DENIED The driver is not in the Dhcp4Bound state.
163 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
168 EfiDhcp4RenewRebind (
169 IN EFI_DHCP4_PROTOCOL
*This
,
170 IN BOOLEAN RebindRequest
,
171 IN EFI_EVENT CompletionEvent OPTIONAL
175 Releases the current address configuration.
177 The Release() function releases the current configured IP address by doing either
179 * Sending a DHCPRELEASE packet when the EFI DHCPv4 Protocol driver is in the
181 * Setting the previously assigned IP address that was provided with the
182 EFI_DHCP4_PROTOCOL.Configure() function to 0.0.0.0 when the driver is in
183 Dhcp4InitReboot state
184 After a successful call to this function, the EFI DHCPv4 Protocol driver returns
185 to the Dhcp4Init state and any subsequent incoming packets will be discarded silently.
187 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
189 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init phase.
190 @retval EFI_INVALID_PARAMETER This is NULL.
191 @retval EFI_ACCESS_DENIED The EFI DHCPv4 Protocol driver is not Dhcp4InitReboot state.
192 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
198 IN EFI_DHCP4_PROTOCOL
*This
202 Stops the current address configuration.
204 The Stop() function is used to stop the DHCP configuration process. After this
205 function is called successfully, the EFI DHCPv4 Protocol driver is transferred
206 into the Dhcp4Stopped state. EFI_DHCP4_PROTOCOL.Configure() needs to be called
207 before DHCP configuration process can be started again. This function can be
208 called when the EFI DHCPv4 Protocol driver is in any state.
210 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
212 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Stopped phase.
213 @retval EFI_INVALID_PARAMETER This is NULL.
219 IN EFI_DHCP4_PROTOCOL
*This
223 Builds a DHCP packet, given the options to be appended or deleted or replaced.
225 The Build() function is used to assemble a new packet from the original packet
226 by replacing or deleting existing options or appending new options. This function
227 does not change any state of the EFI DHCPv4 Protocol driver and can be used at
230 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
231 @param[in] SeedPacket Initial packet to be used as a base for building new packet.
232 @param[in] DeleteCount Number of opcodes in the DeleteList.
233 @param[in] DeleteList List of opcodes to be deleted from the seed packet.
234 Ignored if DeleteCount is zero.
235 @param[in] AppendCount Number of entries in the OptionList.
236 @param[in] AppendList Pointer to a DHCP option list to be appended to SeedPacket.
237 If SeedPacket also contains options in this list, they are
238 replaced by new options (except pad option). Ignored if
239 AppendCount is zero. Type EFI_DHCP4_PACKET_OPTION
240 @param[out] NewPacket Pointer to storage for the pointer to the new allocated packet.
241 Use the EFI Boot Service FreePool() on the resulting pointer
242 when done with the packet.
244 @retval EFI_SUCCESS The new packet was built.
245 @retval EFI_OUT_OF_RESOURCES Storage for the new packet could not be allocated.
246 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
252 IN EFI_DHCP4_PROTOCOL
*This
,
253 IN EFI_DHCP4_PACKET
*SeedPacket
,
254 IN UINT32 DeleteCount
,
255 IN UINT8
*DeleteList OPTIONAL
,
256 IN UINT32 AppendCount
,
257 IN EFI_DHCP4_PACKET_OPTION
*AppendList
[] OPTIONAL
,
258 OUT EFI_DHCP4_PACKET
**NewPacket
262 Transmits a DHCP formatted packet and optionally waits for responses.
264 The TransmitReceive() function is used to transmit a DHCP packet and optionally
265 wait for the response from servers. This function does not change the state of
266 the EFI DHCPv4 Protocol driver and thus can be used at any time.
268 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
269 @param[in] Token Pointer to the EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN structure.
271 @retval EFI_SUCCESS The packet was successfully queued for transmission.
272 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
273 @retval EFI_NOT_READY The previous call to this function has not finished yet. Try to call
274 this function after collection process completes.
275 @retval EFI_NO_MAPPING The default station address is not available yet.
276 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
277 @retval Others Some other unexpected error occurred.
282 EfiDhcp4TransmitReceive (
283 IN EFI_DHCP4_PROTOCOL
*This
,
284 IN EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN
*Token
288 Parses the packed DHCP option data.
290 The Parse() function is used to retrieve the option list from a DHCP packet.
291 If *OptionCount isn't zero, and there is enough space for all the DHCP options
292 in the Packet, each element of PacketOptionList is set to point to somewhere in
293 the Packet->Dhcp4.Option where a new DHCP option begins. If RFC3396 is supported,
294 the caller should reassemble the parsed DHCP options to get the finial result.
295 If *OptionCount is zero or there isn't enough space for all of them, the number
296 of DHCP options in the Packet is returned in OptionCount.
298 @param This Pointer to the EFI_DHCP4_PROTOCOL instance.
299 @param Packet Pointer to packet to be parsed.
300 @param OptionCount On input, the number of entries in the PacketOptionList.
301 On output, the number of entries that were written into the
303 @param PacketOptionList List of packet option entries to be filled in. End option or pad
304 options are not included.
306 @retval EFI_SUCCESS The packet was successfully parsed.
307 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
308 @retval EFI_BUFFER_TOO_SMALL One or more of the following conditions is TRUE:
309 1) *OptionCount is smaller than the number of options that
310 were found in the Packet.
311 2) PacketOptionList is NULL.
317 IN EFI_DHCP4_PROTOCOL
*This
,
318 IN EFI_DHCP4_PACKET
*Packet
,
319 IN OUT UINT32
*OptionCount
,
320 OUT EFI_DHCP4_PACKET_OPTION
*PacketOptionList
[] OPTIONAL
323 EFI_DHCP4_PROTOCOL mDhcp4ProtocolTemplate
= {
331 EfiDhcp4TransmitReceive
,
336 Returns the current operating mode and cached data packet for the EFI DHCPv4 Protocol driver.
338 The GetModeData() function returns the current operating mode and cached data
339 packet for the EFI DHCPv4 Protocol driver.
341 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
342 @param[out] Dhcp4ModeData Pointer to storage for the EFI_DHCP4_MODE_DATA structure.
344 @retval EFI_SUCCESS The mode data was returned.
345 @retval EFI_INVALID_PARAMETER This is NULL.
350 EfiDhcp4GetModeData (
351 IN EFI_DHCP4_PROTOCOL
*This
,
352 OUT EFI_DHCP4_MODE_DATA
*Dhcp4ModeData
355 DHCP_PROTOCOL
*Instance
;
356 DHCP_SERVICE
*DhcpSb
;
357 DHCP_PARAMETER
*Para
;
362 // First validate the parameters.
364 if ((This
== NULL
) || (Dhcp4ModeData
== NULL
)) {
365 return EFI_INVALID_PARAMETER
;
368 Instance
= DHCP_INSTANCE_FROM_THIS (This
);
370 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
371 DhcpSb
= Instance
->Service
;
374 // Caller can use GetModeData to retrieve current DHCP states
375 // no matter whether it is the active child or not.
377 Dhcp4ModeData
->State
= (EFI_DHCP4_STATE
) DhcpSb
->DhcpState
;
378 CopyMem (&Dhcp4ModeData
->ConfigData
, &DhcpSb
->ActiveConfig
, sizeof (Dhcp4ModeData
->ConfigData
));
379 CopyMem (&Dhcp4ModeData
->ClientMacAddress
, &DhcpSb
->Mac
, sizeof (Dhcp4ModeData
->ClientMacAddress
));
381 Ip
= HTONL (DhcpSb
->ClientAddr
);
382 CopyMem (&Dhcp4ModeData
->ClientAddress
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
384 Ip
= HTONL (DhcpSb
->Netmask
);
385 CopyMem (&Dhcp4ModeData
->SubnetMask
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
387 Ip
= HTONL (DhcpSb
->ServerAddr
);
388 CopyMem (&Dhcp4ModeData
->ServerAddress
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
393 Ip
= HTONL (Para
->Router
);
394 CopyMem (&Dhcp4ModeData
->RouterAddress
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
395 Dhcp4ModeData
->LeaseTime
= Para
->Lease
;
397 ZeroMem (&Dhcp4ModeData
->RouterAddress
, sizeof (EFI_IPv4_ADDRESS
));
398 Dhcp4ModeData
->LeaseTime
= 0xffffffff;
401 Dhcp4ModeData
->ReplyPacket
= DhcpSb
->Selected
;
403 gBS
->RestoreTPL (OldTpl
);
409 Free the resource related to the configure parameters.
410 DHCP driver will make a copy of the user's configure
411 such as the time out value.
413 @param Config The DHCP configure data
418 IN OUT EFI_DHCP4_CONFIG_DATA
*Config
423 if (Config
->DiscoverTimeout
!= NULL
) {
424 FreePool (Config
->DiscoverTimeout
);
427 if (Config
->RequestTimeout
!= NULL
) {
428 FreePool (Config
->RequestTimeout
);
431 if (Config
->OptionList
!= NULL
) {
432 for (Index
= 0; Index
< Config
->OptionCount
; Index
++) {
433 if (Config
->OptionList
[Index
] != NULL
) {
434 FreePool (Config
->OptionList
[Index
]);
438 FreePool (Config
->OptionList
);
441 ZeroMem (Config
, sizeof (EFI_DHCP4_CONFIG_DATA
));
446 Allocate memory for configure parameter such as timeout value for Dst,
447 then copy the configure parameter from Src to Dst.
449 @param[out] Dst The destination DHCP configure data.
450 @param[in] Src The source DHCP configure data.
452 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
453 @retval EFI_SUCCESS The configure is copied.
458 OUT EFI_DHCP4_CONFIG_DATA
*Dst
,
459 IN EFI_DHCP4_CONFIG_DATA
*Src
462 EFI_DHCP4_PACKET_OPTION
**DstOptions
;
463 EFI_DHCP4_PACKET_OPTION
**SrcOptions
;
467 CopyMem (Dst
, Src
, sizeof (*Dst
));
468 Dst
->DiscoverTimeout
= NULL
;
469 Dst
->RequestTimeout
= NULL
;
470 Dst
->OptionList
= NULL
;
473 // Allocate a memory then copy DiscoverTimeout to it
475 if (Src
->DiscoverTimeout
!= NULL
) {
476 Len
= Src
->DiscoverTryCount
* sizeof (UINT32
);
477 Dst
->DiscoverTimeout
= AllocatePool (Len
);
479 if (Dst
->DiscoverTimeout
== NULL
) {
480 return EFI_OUT_OF_RESOURCES
;
483 for (Index
= 0; Index
< Src
->DiscoverTryCount
; Index
++) {
484 Dst
->DiscoverTimeout
[Index
] = MAX (Src
->DiscoverTimeout
[Index
], 1);
489 // Allocate a memory then copy RequestTimeout to it
491 if (Src
->RequestTimeout
!= NULL
) {
492 Len
= Src
->RequestTryCount
* sizeof (UINT32
);
493 Dst
->RequestTimeout
= AllocatePool (Len
);
495 if (Dst
->RequestTimeout
== NULL
) {
499 for (Index
= 0; Index
< Src
->RequestTryCount
; Index
++) {
500 Dst
->RequestTimeout
[Index
] = MAX (Src
->RequestTimeout
[Index
], 1);
505 // Allocate an array of dhcp option point, then allocate memory
506 // for each option and copy the source option to it
508 if (Src
->OptionList
!= NULL
) {
509 Len
= Src
->OptionCount
* sizeof (EFI_DHCP4_PACKET_OPTION
*);
510 Dst
->OptionList
= AllocateZeroPool (Len
);
512 if (Dst
->OptionList
== NULL
) {
516 DstOptions
= Dst
->OptionList
;
517 SrcOptions
= Src
->OptionList
;
519 for (Index
= 0; Index
< Src
->OptionCount
; Index
++) {
520 Len
= sizeof (EFI_DHCP4_PACKET_OPTION
) + MAX (SrcOptions
[Index
]->Length
- 1, 0);
522 DstOptions
[Index
] = AllocatePool (Len
);
524 if (DstOptions
[Index
] == NULL
) {
528 CopyMem (DstOptions
[Index
], SrcOptions
[Index
], Len
);
535 DhcpCleanConfigure (Dst
);
536 return EFI_OUT_OF_RESOURCES
;
541 Give up the control of the DHCP service to let other child
542 resume. Don't change the service's DHCP state and the Client
543 address and option list configure as required by RFC2131.
545 @param DhcpSb The DHCP service instance.
550 IN DHCP_SERVICE
*DhcpSb
553 EFI_DHCP4_CONFIG_DATA
*Config
;
555 Config
= &DhcpSb
->ActiveConfig
;
557 DhcpSb
->ServiceState
= DHCP_UNCONFIGED
;
558 DhcpSb
->ActiveChild
= NULL
;
560 if (Config
->DiscoverTimeout
!= NULL
) {
561 FreePool (Config
->DiscoverTimeout
);
563 Config
->DiscoverTryCount
= 0;
564 Config
->DiscoverTimeout
= NULL
;
567 if (Config
->RequestTimeout
!= NULL
) {
568 FreePool (Config
->RequestTimeout
);
570 Config
->RequestTryCount
= 0;
571 Config
->RequestTimeout
= NULL
;
574 Config
->Dhcp4Callback
= NULL
;
575 Config
->CallbackContext
= NULL
;
580 Initializes, changes, or resets the operational settings for the EFI DHCPv4 Protocol driver.
582 The Configure() function is used to initialize, change, or reset the operational
583 settings of the EFI DHCPv4 Protocol driver for the communication device on which
584 the EFI DHCPv4 Service Binding Protocol is installed. This function can be
585 successfully called only if both of the following are true:
586 * This instance of the EFI DHCPv4 Protocol driver is in the Dhcp4Stopped, Dhcp4Init,
587 Dhcp4InitReboot, or Dhcp4Bound states.
588 * No other EFI DHCPv4 Protocol driver instance that is controlled by this EFI
589 DHCPv4 Service Binding Protocol driver instance has configured this EFI DHCPv4
591 When this driver is in the Dhcp4Stopped state, it can transfer into one of the
592 following two possible initial states:
595 The driver can transfer into these states by calling Configure() with a non-NULL
596 Dhcp4CfgData. The driver will transfer into the appropriate state based on the
597 supplied client network address in the ClientAddress parameter and DHCP options
598 in the OptionList parameter as described in RFC 2131.
599 When Configure() is called successfully while Dhcp4CfgData is set to NULL, the
600 default configuring data will be reset in the EFI DHCPv4 Protocol driver and
601 the state of the EFI DHCPv4 Protocol driver will not be changed. If one instance
602 wants to make it possible for another instance to configure the EFI DHCPv4 Protocol
603 driver, it must call this function with Dhcp4CfgData set to NULL.
605 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
606 @param[in] Dhcp4CfgData Pointer to the EFI_DHCP4_CONFIG_DATA.
608 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init or
609 Dhcp4InitReboot state, if the original state of this driver
610 was Dhcp4Stopped and the value of Dhcp4CfgData was
611 not NULL. Otherwise, the state was left unchanged.
612 @retval EFI_ACCESS_DENIED This instance of the EFI DHCPv4 Protocol driver was not in the
613 Dhcp4Stopped, Dhcp4Init, Dhcp4InitReboot, or Dhcp4Bound state;
614 Or another instance of this EFI DHCPv4 Protocol driver is already
615 in a valid configured state.
616 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
617 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
618 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
624 IN EFI_DHCP4_PROTOCOL
*This
,
625 IN EFI_DHCP4_CONFIG_DATA
*Dhcp4CfgData OPTIONAL
628 EFI_DHCP4_CONFIG_DATA
*Config
;
629 DHCP_PROTOCOL
*Instance
;
630 DHCP_SERVICE
*DhcpSb
;
637 // First validate the parameters
640 return EFI_INVALID_PARAMETER
;
643 if (Dhcp4CfgData
!= NULL
) {
644 if ((Dhcp4CfgData
->DiscoverTryCount
!= 0) && (Dhcp4CfgData
->DiscoverTimeout
== NULL
)) {
645 return EFI_INVALID_PARAMETER
;
648 if ((Dhcp4CfgData
->RequestTryCount
!= 0) && (Dhcp4CfgData
->RequestTimeout
== NULL
)) {
649 return EFI_INVALID_PARAMETER
;
652 if ((Dhcp4CfgData
->OptionCount
!= 0) && (Dhcp4CfgData
->OptionList
== NULL
)) {
653 return EFI_INVALID_PARAMETER
;
656 CopyMem (&Ip
, &Dhcp4CfgData
->ClientAddress
, sizeof (IP4_ADDR
));
657 if (IP4_IS_LOCAL_BROADCAST(NTOHL (Ip
))) {
658 return EFI_INVALID_PARAMETER
;
662 Instance
= DHCP_INSTANCE_FROM_THIS (This
);
664 if (Instance
->Signature
!= DHCP_PROTOCOL_SIGNATURE
) {
665 return EFI_INVALID_PARAMETER
;
668 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
670 DhcpSb
= Instance
->Service
;
671 Config
= &DhcpSb
->ActiveConfig
;
673 Status
= EFI_ACCESS_DENIED
;
675 if ((DhcpSb
->DhcpState
!= Dhcp4Stopped
) &&
676 (DhcpSb
->DhcpState
!= Dhcp4Init
) &&
677 (DhcpSb
->DhcpState
!= Dhcp4InitReboot
) &&
678 (DhcpSb
->DhcpState
!= Dhcp4Bound
)) {
683 if ((DhcpSb
->ActiveChild
!= NULL
) && (DhcpSb
->ActiveChild
!= Instance
)) {
687 if (Dhcp4CfgData
!= NULL
) {
688 Status
= EFI_OUT_OF_RESOURCES
;
689 DhcpCleanConfigure (Config
);
691 if (EFI_ERROR (DhcpCopyConfigure (Config
, Dhcp4CfgData
))) {
695 DhcpSb
->UserOptionLen
= 0;
697 for (Index
= 0; Index
< Dhcp4CfgData
->OptionCount
; Index
++) {
698 DhcpSb
->UserOptionLen
+= Dhcp4CfgData
->OptionList
[Index
]->Length
+ 2;
701 DhcpSb
->ActiveChild
= Instance
;
703 if (DhcpSb
->DhcpState
== Dhcp4Stopped
) {
704 DhcpSb
->ClientAddr
= EFI_NTOHL (Dhcp4CfgData
->ClientAddress
);
706 if (DhcpSb
->ClientAddr
!= 0) {
707 DhcpSb
->DhcpState
= Dhcp4InitReboot
;
709 DhcpSb
->DhcpState
= Dhcp4Init
;
713 DhcpSb
->ServiceState
= DHCP_CONFIGED
;
714 Status
= EFI_SUCCESS
;
716 } else if (DhcpSb
->ActiveChild
== Instance
) {
717 Status
= EFI_SUCCESS
;
718 DhcpYieldControl (DhcpSb
);
722 gBS
->RestoreTPL (OldTpl
);
728 Starts the DHCP configuration process.
730 The Start() function starts the DHCP configuration process. This function can
731 be called only when the EFI DHCPv4 Protocol driver is in the Dhcp4Init or
732 Dhcp4InitReboot state.
733 If the DHCP process completes successfully, the state of the EFI DHCPv4 Protocol
734 driver will be transferred through Dhcp4Selecting and Dhcp4Requesting to the
735 Dhcp4Bound state. The CompletionEvent will then be signaled if it is not NULL.
736 If the process aborts, either by the user or by some unexpected network error,
737 the state is restored to the Dhcp4Init state. The Start() function can be called
738 again to restart the process.
739 Refer to RFC 2131 for precise state transitions during this process. At the
740 time when each event occurs in this process, the callback function that was set
741 by EFI_DHCP4_PROTOCOL.Configure() will be called and the user can take this
742 opportunity to control the process.
744 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
745 @param[in] CompletionEvent If not NULL, indicates the event that will be signaled when the
746 EFI DHCPv4 Protocol driver is transferred into the
747 Dhcp4Bound state or when the DHCP process is aborted.
748 EFI_DHCP4_PROTOCOL.GetModeData() can be called to
749 check the completion status. If NULL,
750 EFI_DHCP4_PROTOCOL.Start() will wait until the driver
751 is transferred into the Dhcp4Bound state or the process fails.
753 @retval EFI_SUCCESS The DHCP configuration process has started, or it has completed
754 when CompletionEvent is NULL.
755 @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped
756 state. EFI_DHCP4_PROTOCOL. Configure() needs to be called.
757 @retval EFI_INVALID_PARAMETER This is NULL.
758 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
759 @retval EFI_TIMEOUT The DHCP configuration process failed because no response was
760 received from the server within the specified timeout value.
761 @retval EFI_ABORTED The user aborted the DHCP process.
762 @retval EFI_ALREADY_STARTED Some other EFI DHCPv4 Protocol instance already started the
764 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
765 @retval EFI_NO_MEDIA There was a media error.
771 IN EFI_DHCP4_PROTOCOL
*This
,
772 IN EFI_EVENT CompletionEvent OPTIONAL
775 DHCP_PROTOCOL
*Instance
;
776 DHCP_SERVICE
*DhcpSb
;
779 EFI_STATUS MediaStatus
;
782 // First validate the parameters
785 return EFI_INVALID_PARAMETER
;
788 Instance
= DHCP_INSTANCE_FROM_THIS (This
);
790 if (Instance
->Signature
!= DHCP_PROTOCOL_SIGNATURE
) {
791 return EFI_INVALID_PARAMETER
;
794 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
795 DhcpSb
= Instance
->Service
;
797 if (DhcpSb
->DhcpState
== Dhcp4Stopped
) {
798 Status
= EFI_NOT_STARTED
;
802 if ((DhcpSb
->DhcpState
!= Dhcp4Init
) && (DhcpSb
->DhcpState
!= Dhcp4InitReboot
)) {
803 Status
= EFI_ALREADY_STARTED
;
808 // Check Media Status.
810 MediaStatus
= EFI_SUCCESS
;
811 NetLibDetectMediaWaitTimeout (DhcpSb
->Controller
, DHCP_CHECK_MEDIA_WAITING_TIME
, &MediaStatus
);
812 if (MediaStatus
!= EFI_SUCCESS
) {
813 Status
= EFI_NO_MEDIA
;
817 DhcpSb
->IoStatus
= EFI_ALREADY_STARTED
;
819 if (EFI_ERROR (Status
= DhcpInitRequest (DhcpSb
))) {
824 Instance
->CompletionEvent
= CompletionEvent
;
827 // Restore the TPL now, don't call poll function at TPL_CALLBACK.
829 gBS
->RestoreTPL (OldTpl
);
831 if (CompletionEvent
== NULL
) {
832 while (DhcpSb
->IoStatus
== EFI_ALREADY_STARTED
) {
833 DhcpSb
->UdpIo
->Protocol
.Udp4
->Poll (DhcpSb
->UdpIo
->Protocol
.Udp4
);
836 return DhcpSb
->IoStatus
;
842 gBS
->RestoreTPL (OldTpl
);
848 Extends the lease time by sending a request packet.
850 The RenewRebind() function is used to manually extend the lease time when the
851 EFI DHCPv4 Protocol driver is in the Dhcp4Bound state and the lease time has
852 not expired yet. This function will send a request packet to the previously
853 found server (or to any server when RebindRequest is TRUE) and transfer the
854 state into the Dhcp4Renewing state (or Dhcp4Rebinding when RebindingRequest is
855 TRUE). When a response is received, the state is returned to Dhcp4Bound.
856 If no response is received before the try count is exceeded (the RequestTryCount
857 field that is specified in EFI_DHCP4_CONFIG_DATA) but before the lease time that
858 was issued by the previous server expires, the driver will return to the Dhcp4Bound
859 state and the previous configuration is restored. The outgoing and incoming packets
860 can be captured by the EFI_DHCP4_CALLBACK function.
862 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
863 @param[in] RebindRequest If TRUE, this function broadcasts the request packets and enters
864 the Dhcp4Rebinding state. Otherwise, it sends a unicast
865 request packet and enters the Dhcp4Renewing state.
866 @param[in] CompletionEvent If not NULL, this event is signaled when the renew/rebind phase
867 completes or some error occurs.
868 EFI_DHCP4_PROTOCOL.GetModeData() can be called to
869 check the completion status. If NULL,
870 EFI_DHCP4_PROTOCOL.RenewRebind() will busy-wait
871 until the DHCP process finishes.
873 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the
874 Dhcp4Renewing state or is back to the Dhcp4Bound state.
875 @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped
876 state. EFI_DHCP4_PROTOCOL.Configure() needs to
878 @retval EFI_INVALID_PARAMETER This is NULL.
879 @retval EFI_TIMEOUT There was no response from the server when the try count was
881 @retval EFI_ACCESS_DENIED The driver is not in the Dhcp4Bound state.
882 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
887 EfiDhcp4RenewRebind (
888 IN EFI_DHCP4_PROTOCOL
*This
,
889 IN BOOLEAN RebindRequest
,
890 IN EFI_EVENT CompletionEvent OPTIONAL
893 DHCP_PROTOCOL
*Instance
;
894 DHCP_SERVICE
*DhcpSb
;
899 // First validate the parameters
902 return EFI_INVALID_PARAMETER
;
905 Instance
= DHCP_INSTANCE_FROM_THIS (This
);
907 if (Instance
->Signature
!= DHCP_PROTOCOL_SIGNATURE
) {
908 return EFI_INVALID_PARAMETER
;
911 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
912 DhcpSb
= Instance
->Service
;
914 if (DhcpSb
->DhcpState
== Dhcp4Stopped
) {
915 Status
= EFI_NOT_STARTED
;
919 if (DhcpSb
->DhcpState
!= Dhcp4Bound
) {
920 Status
= EFI_ACCESS_DENIED
;
924 if (DHCP_IS_BOOTP (DhcpSb
->Para
)) {
925 Status
= EFI_SUCCESS
;
930 // Transit the states then send a extra DHCP request
932 if (!RebindRequest
) {
933 DhcpSetState (DhcpSb
, Dhcp4Renewing
, FALSE
);
935 DhcpSetState (DhcpSb
, Dhcp4Rebinding
, FALSE
);
939 // Clear initial time to make sure that elapsed-time
940 // is set to 0 for first REQUEST in renewal process.
942 Instance
->ElaspedTime
= 0;
944 Status
= DhcpSendMessage (
949 (UINT8
*) "Extra renew/rebind by the application"
952 if (EFI_ERROR (Status
)) {
953 DhcpSetState (DhcpSb
, Dhcp4Bound
, FALSE
);
957 DhcpSb
->ExtraRefresh
= TRUE
;
958 DhcpSb
->IoStatus
= EFI_ALREADY_STARTED
;
959 Instance
->RenewRebindEvent
= CompletionEvent
;
961 gBS
->RestoreTPL (OldTpl
);
963 if (CompletionEvent
== NULL
) {
964 while (DhcpSb
->IoStatus
== EFI_ALREADY_STARTED
) {
965 DhcpSb
->UdpIo
->Protocol
.Udp4
->Poll (DhcpSb
->UdpIo
->Protocol
.Udp4
);
969 return DhcpSb
->IoStatus
;
975 gBS
->RestoreTPL (OldTpl
);
981 Releases the current address configuration.
983 The Release() function releases the current configured IP address by doing either
985 * Sending a DHCPRELEASE packet when the EFI DHCPv4 Protocol driver is in the
987 * Setting the previously assigned IP address that was provided with the
988 EFI_DHCP4_PROTOCOL.Configure() function to 0.0.0.0 when the driver is in
989 Dhcp4InitReboot state
990 After a successful call to this function, the EFI DHCPv4 Protocol driver returns
991 to the Dhcp4Init state and any subsequent incoming packets will be discarded silently.
993 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
995 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init phase.
996 @retval EFI_INVALID_PARAMETER This is NULL.
997 @retval EFI_ACCESS_DENIED The EFI DHCPv4 Protocol driver is not Dhcp4InitReboot state.
998 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1004 IN EFI_DHCP4_PROTOCOL
*This
1007 DHCP_PROTOCOL
*Instance
;
1008 DHCP_SERVICE
*DhcpSb
;
1013 // First validate the parameters
1016 return EFI_INVALID_PARAMETER
;
1019 Instance
= DHCP_INSTANCE_FROM_THIS (This
);
1021 if (Instance
->Signature
!= DHCP_PROTOCOL_SIGNATURE
) {
1022 return EFI_INVALID_PARAMETER
;
1025 Status
= EFI_SUCCESS
;
1026 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1027 DhcpSb
= Instance
->Service
;
1029 if ((DhcpSb
->DhcpState
!= Dhcp4InitReboot
) && (DhcpSb
->DhcpState
!= Dhcp4Bound
)) {
1030 Status
= EFI_ACCESS_DENIED
;
1034 if (!DHCP_IS_BOOTP (DhcpSb
->Para
) && (DhcpSb
->DhcpState
== Dhcp4Bound
)) {
1035 Status
= DhcpSendMessage (
1043 if (EFI_ERROR (Status
)) {
1044 Status
= EFI_DEVICE_ERROR
;
1049 DhcpCleanLease (DhcpSb
);
1052 gBS
->RestoreTPL (OldTpl
);
1058 Stops the current address configuration.
1060 The Stop() function is used to stop the DHCP configuration process. After this
1061 function is called successfully, the EFI DHCPv4 Protocol driver is transferred
1062 into the Dhcp4Stopped state. EFI_DHCP4_PROTOCOL.Configure() needs to be called
1063 before DHCP configuration process can be started again. This function can be
1064 called when the EFI DHCPv4 Protocol driver is in any state.
1066 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
1068 @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Stopped phase.
1069 @retval EFI_INVALID_PARAMETER This is NULL.
1075 IN EFI_DHCP4_PROTOCOL
*This
1078 DHCP_PROTOCOL
*Instance
;
1079 DHCP_SERVICE
*DhcpSb
;
1083 // First validate the parameters
1086 return EFI_INVALID_PARAMETER
;
1089 Instance
= DHCP_INSTANCE_FROM_THIS (This
);
1091 if (Instance
->Signature
!= DHCP_PROTOCOL_SIGNATURE
) {
1092 return EFI_INVALID_PARAMETER
;
1095 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1096 DhcpSb
= Instance
->Service
;
1098 DhcpCleanLease (DhcpSb
);
1100 DhcpSb
->DhcpState
= Dhcp4Stopped
;
1101 DhcpSb
->ServiceState
= DHCP_UNCONFIGED
;
1103 gBS
->RestoreTPL (OldTpl
);
1109 Builds a DHCP packet, given the options to be appended or deleted or replaced.
1111 The Build() function is used to assemble a new packet from the original packet
1112 by replacing or deleting existing options or appending new options. This function
1113 does not change any state of the EFI DHCPv4 Protocol driver and can be used at
1116 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
1117 @param[in] SeedPacket Initial packet to be used as a base for building new packet.
1118 @param[in] DeleteCount Number of opcodes in the DeleteList.
1119 @param[in] DeleteList List of opcodes to be deleted from the seed packet.
1120 Ignored if DeleteCount is zero.
1121 @param[in] AppendCount Number of entries in the OptionList.
1122 @param[in] AppendList Pointer to a DHCP option list to be appended to SeedPacket.
1123 If SeedPacket also contains options in this list, they are
1124 replaced by new options (except pad option). Ignored if
1125 AppendCount is zero. Type EFI_DHCP4_PACKET_OPTION
1126 @param[out] NewPacket Pointer to storage for the pointer to the new allocated packet.
1127 Use the EFI Boot Service FreePool() on the resulting pointer
1128 when done with the packet.
1130 @retval EFI_SUCCESS The new packet was built.
1131 @retval EFI_OUT_OF_RESOURCES Storage for the new packet could not be allocated.
1132 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
1138 IN EFI_DHCP4_PROTOCOL
*This
,
1139 IN EFI_DHCP4_PACKET
*SeedPacket
,
1140 IN UINT32 DeleteCount
,
1141 IN UINT8
*DeleteList OPTIONAL
,
1142 IN UINT32 AppendCount
,
1143 IN EFI_DHCP4_PACKET_OPTION
*AppendList
[] OPTIONAL
,
1144 OUT EFI_DHCP4_PACKET
**NewPacket
1148 // First validate the parameters
1150 if ((This
== NULL
) || (NewPacket
== NULL
)) {
1151 return EFI_INVALID_PARAMETER
;
1154 if ((SeedPacket
== NULL
) || (SeedPacket
->Dhcp4
.Magik
!= DHCP_OPTION_MAGIC
) ||
1155 EFI_ERROR (DhcpValidateOptions (SeedPacket
, NULL
))) {
1157 return EFI_INVALID_PARAMETER
;
1160 if (((DeleteCount
== 0) && (AppendCount
== 0)) ||
1161 ((DeleteCount
!= 0) && (DeleteList
== NULL
)) ||
1162 ((AppendCount
!= 0) && (AppendList
== NULL
))) {
1164 return EFI_INVALID_PARAMETER
;
1178 Callback by UdpIoCreatePort() when creating UdpIo for this Dhcp4 instance.
1180 @param[in] UdpIo The UdpIo being created.
1181 @param[in] Context Dhcp4 instance.
1183 @retval EFI_SUCCESS UdpIo is configured successfully.
1184 @retval EFI_INVALID_PARAMETER Class E IP address is not supported or other parameters
1186 @retval other Other error occurs.
1190 Dhcp4InstanceConfigUdpIo (
1195 DHCP_PROTOCOL
*Instance
;
1196 DHCP_SERVICE
*DhcpSb
;
1197 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN
*Token
;
1198 EFI_UDP4_CONFIG_DATA UdpConfigData
;
1199 IP4_ADDR ClientAddr
;
1202 IP4_ADDR SubnetMask
;
1204 Instance
= (DHCP_PROTOCOL
*) Context
;
1205 DhcpSb
= Instance
->Service
;
1206 Token
= Instance
->Token
;
1208 ZeroMem (&UdpConfigData
, sizeof (EFI_UDP4_CONFIG_DATA
));
1210 UdpConfigData
.AcceptBroadcast
= TRUE
;
1211 UdpConfigData
.AllowDuplicatePort
= TRUE
;
1212 UdpConfigData
.TimeToLive
= 64;
1213 UdpConfigData
.DoNotFragment
= TRUE
;
1215 ClientAddr
= EFI_NTOHL (Token
->Packet
->Dhcp4
.Header
.ClientAddr
);
1216 Ip
= HTONL (ClientAddr
);
1217 CopyMem (&UdpConfigData
.StationAddress
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
1219 if (DhcpSb
->Netmask
== 0) {
1221 // The Dhcp4.TransmitReceive() API should be able to used at any time according to
1222 // UEFI spec, while in classless addressing network, the netmask must be explicitly
1223 // provided together with the station address.
1224 // If the DHCP instance haven't be configured with a valid netmask, we could only
1225 // compute it according to the classful addressing rule.
1227 Class
= NetGetIpClass (ClientAddr
);
1229 // Class E IP address is not supported here!
1231 ASSERT (Class
< IP4_ADDR_CLASSE
);
1232 if (Class
>= IP4_ADDR_CLASSE
) {
1233 return EFI_INVALID_PARAMETER
;
1236 SubnetMask
= gIp4AllMasks
[Class
<< 3];
1238 SubnetMask
= DhcpSb
->Netmask
;
1241 Ip
= HTONL (SubnetMask
);
1242 CopyMem (&UdpConfigData
.SubnetMask
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
1244 if ((Token
->ListenPointCount
== 0) || (Token
->ListenPoints
[0].ListenPort
== 0)) {
1245 UdpConfigData
.StationPort
= DHCP_CLIENT_PORT
;
1247 UdpConfigData
.StationPort
= Token
->ListenPoints
[0].ListenPort
;
1250 return UdpIo
->Protocol
.Udp4
->Configure (UdpIo
->Protocol
.Udp4
, &UdpConfigData
);
1254 Create UdpIo for this Dhcp4 instance.
1256 @param Instance The Dhcp4 instance.
1258 @retval EFI_SUCCESS UdpIo is created successfully.
1259 @retval EFI_OUT_OF_RESOURCES Fails to create UdpIo because of limited
1260 resources or configuration failure.
1263 Dhcp4InstanceCreateUdpIo (
1264 IN OUT DHCP_PROTOCOL
*Instance
1267 DHCP_SERVICE
*DhcpSb
;
1271 ASSERT (Instance
->Token
!= NULL
);
1273 DhcpSb
= Instance
->Service
;
1274 Instance
->UdpIo
= UdpIoCreateIo (
1277 Dhcp4InstanceConfigUdpIo
,
1278 UDP_IO_UDP4_VERSION
,
1281 if (Instance
->UdpIo
== NULL
) {
1282 return EFI_OUT_OF_RESOURCES
;
1284 Status
= gBS
->OpenProtocol (
1285 Instance
->UdpIo
->UdpHandle
,
1286 &gEfiUdp4ProtocolGuid
,
1288 Instance
->Service
->Image
,
1290 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1292 if (EFI_ERROR (Status
)) {
1293 UdpIoFreeIo (Instance
->UdpIo
);
1294 Instance
->UdpIo
= NULL
;
1301 Callback of Dhcp packet. Does nothing.
1303 @param Arg The context.
1315 Callback of UdpIoRecvDatagram() that handles a Dhcp4 packet.
1317 Only BOOTP responses will be handled that correspond to the Xid of the request
1318 sent out. The packet will be queued to the response queue.
1320 @param UdpPacket The Dhcp4 packet.
1321 @param EndPoint Udp4 address pair.
1322 @param IoStatus Status of the input.
1323 @param Context Extra info for the input.
1330 UDP_END_POINT
*EndPoint
,
1331 EFI_STATUS IoStatus
,
1335 DHCP_PROTOCOL
*Instance
;
1336 EFI_DHCP4_HEADER
*Head
;
1338 EFI_DHCP4_PACKET
*Packet
;
1339 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN
*Token
;
1344 Instance
= (DHCP_PROTOCOL
*) Context
;
1345 Token
= Instance
->Token
;
1348 // Don't restart receive if error occurs or DHCP is destroyed.
1350 if (EFI_ERROR (IoStatus
)) {
1354 ASSERT (UdpPacket
!= NULL
);
1357 // Validate the packet received
1359 if (UdpPacket
->TotalSize
< sizeof (EFI_DHCP4_HEADER
)) {
1364 // Copy the DHCP message to a continuous memory block, make the buffer size
1365 // of the EFI_DHCP4_PACKET a multiple of 4-byte.
1367 Len
= NET_ROUNDUP (sizeof (EFI_DHCP4_PACKET
) + UdpPacket
->TotalSize
- sizeof (EFI_DHCP4_HEADER
), 4);
1368 Wrap
= NetbufAlloc (Len
);
1373 Packet
= (EFI_DHCP4_PACKET
*) NetbufAllocSpace (Wrap
, Len
, NET_BUF_TAIL
);
1374 ASSERT (Packet
!= NULL
);
1377 Head
= &Packet
->Dhcp4
.Header
;
1378 Packet
->Length
= NetbufCopy (UdpPacket
, 0, UdpPacket
->TotalSize
, (UINT8
*) Head
);
1380 if (Packet
->Length
!= UdpPacket
->TotalSize
) {
1385 // Is this packet the answer to our packet?
1387 if ((Head
->OpCode
!= BOOTP_REPLY
) ||
1388 (Head
->Xid
!= Token
->Packet
->Dhcp4
.Header
.Xid
) ||
1389 (CompareMem (&Token
->Packet
->Dhcp4
.Header
.ClientHwAddr
[0], Head
->ClientHwAddr
, Head
->HwAddrLen
) != 0)) {
1394 // Validate the options and retrieve the interested options
1396 if ((Packet
->Length
> sizeof (EFI_DHCP4_HEADER
) + sizeof (UINT32
)) &&
1397 (Packet
->Dhcp4
.Magik
== DHCP_OPTION_MAGIC
) &&
1398 EFI_ERROR (DhcpValidateOptions (Packet
, NULL
))) {
1404 // Keep this packet in the ResponseQueue.
1407 NetbufQueAppend (&Instance
->ResponseQueue
, Wrap
);
1411 NetbufFree (UdpPacket
);
1417 Status
= UdpIoRecvDatagram (Instance
->UdpIo
, PxeDhcpInput
, Instance
, 0);
1418 if (EFI_ERROR (Status
)) {
1419 PxeDhcpDone (Instance
);
1424 Complete a Dhcp4 transaction and signal the upper layer.
1426 @param Instance Dhcp4 instance.
1431 IN DHCP_PROTOCOL
*Instance
1434 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN
*Token
;
1436 Token
= Instance
->Token
;
1438 Token
->ResponseCount
= Instance
->ResponseQueue
.BufNum
;
1439 if (Token
->ResponseCount
!= 0) {
1440 Token
->ResponseList
= (EFI_DHCP4_PACKET
*) AllocatePool (Instance
->ResponseQueue
.BufSize
);
1441 if (Token
->ResponseList
== NULL
) {
1442 Token
->Status
= EFI_OUT_OF_RESOURCES
;
1447 // Copy the received DHCP responses.
1449 NetbufQueCopy (&Instance
->ResponseQueue
, 0, Instance
->ResponseQueue
.BufSize
, (UINT8
*) Token
->ResponseList
);
1450 Token
->Status
= EFI_SUCCESS
;
1452 Token
->ResponseList
= NULL
;
1453 Token
->Status
= EFI_TIMEOUT
;
1458 // Clean up the resources dedicated for this transmit receive transaction.
1460 NetbufQueFlush (&Instance
->ResponseQueue
);
1461 UdpIoCleanIo (Instance
->UdpIo
);
1462 gBS
->CloseProtocol (
1463 Instance
->UdpIo
->UdpHandle
,
1464 &gEfiUdp4ProtocolGuid
,
1465 Instance
->Service
->Image
,
1468 UdpIoFreeIo (Instance
->UdpIo
);
1469 Instance
->UdpIo
= NULL
;
1470 Instance
->Token
= NULL
;
1472 if (Token
->CompletionEvent
!= NULL
) {
1473 gBS
->SignalEvent (Token
->CompletionEvent
);
1479 Transmits a DHCP formatted packet and optionally waits for responses.
1481 The TransmitReceive() function is used to transmit a DHCP packet and optionally
1482 wait for the response from servers. This function does not change the state of
1483 the EFI DHCPv4 Protocol driver and thus can be used at any time.
1485 @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
1486 @param[in] Token Pointer to the EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN structure.
1488 @retval EFI_SUCCESS The packet was successfully queued for transmission.
1489 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
1490 @retval EFI_NOT_READY The previous call to this function has not finished yet. Try to call
1491 this function after collection process completes.
1492 @retval EFI_NO_MAPPING The default station address is not available yet.
1493 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
1494 @retval Others Some other unexpected error occurred.
1499 EfiDhcp4TransmitReceive (
1500 IN EFI_DHCP4_PROTOCOL
*This
,
1501 IN EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN
*Token
1504 DHCP_PROTOCOL
*Instance
;
1509 UDP_END_POINT EndPoint
;
1511 DHCP_SERVICE
*DhcpSb
;
1512 EFI_IP_ADDRESS Gateway
;
1513 IP4_ADDR ClientAddr
;
1515 if ((This
== NULL
) || (Token
== NULL
) || (Token
->Packet
== NULL
)) {
1516 return EFI_INVALID_PARAMETER
;
1519 Instance
= DHCP_INSTANCE_FROM_THIS (This
);
1520 DhcpSb
= Instance
->Service
;
1522 if (Instance
->Token
!= NULL
) {
1524 // The previous call to TransmitReceive is not finished.
1526 return EFI_NOT_READY
;
1529 if ((Token
->Packet
->Dhcp4
.Magik
!= DHCP_OPTION_MAGIC
) ||
1530 (NTOHL (Token
->Packet
->Dhcp4
.Header
.Xid
) == Instance
->Service
->Xid
) ||
1531 (Token
->TimeoutValue
== 0) ||
1532 ((Token
->ListenPointCount
!= 0) && (Token
->ListenPoints
== NULL
)) ||
1533 EFI_ERROR (DhcpValidateOptions (Token
->Packet
, NULL
)) ||
1534 EFI_IP4_EQUAL (&Token
->RemoteAddress
, &mZeroIp4Addr
)
1537 // The DHCP packet isn't well-formed, the Transaction ID is already used,
1538 // the timeout value is zero, the ListenPoint is invalid, or the
1539 // RemoteAddress is zero.
1541 return EFI_INVALID_PARAMETER
;
1544 ClientAddr
= EFI_NTOHL (Token
->Packet
->Dhcp4
.Header
.ClientAddr
);
1546 if (ClientAddr
== 0) {
1547 return EFI_NO_MAPPING
;
1550 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1553 // Save the token and the timeout value.
1555 Instance
->Token
= Token
;
1556 Instance
->Timeout
= Token
->TimeoutValue
;
1559 // Create a UDP IO for this transmit receive transaction.
1561 Status
= Dhcp4InstanceCreateUdpIo (Instance
);
1562 if (EFI_ERROR (Status
)) {
1567 // Save the Client Address is sent out
1570 &DhcpSb
->ClientAddressSendOut
[0],
1571 &Token
->Packet
->Dhcp4
.Header
.ClientHwAddr
[0],
1572 Token
->Packet
->Dhcp4
.Header
.HwAddrLen
1576 // Wrap the DHCP packet into a net buffer.
1578 Frag
.Bulk
= (UINT8
*) &Token
->Packet
->Dhcp4
;
1579 Frag
.Len
= Token
->Packet
->Length
;
1580 Wrap
= NetbufFromExt (&Frag
, 1, 0, 0, DhcpDummyExtFree
, NULL
);
1582 Status
= EFI_OUT_OF_RESOURCES
;
1587 // Set the local address and local port to ZERO.
1589 ZeroMem (&EndPoint
, sizeof (UDP_END_POINT
));
1592 // Set the destination address and destination port.
1594 CopyMem (&Ip
, &Token
->RemoteAddress
, sizeof (EFI_IPv4_ADDRESS
));
1595 EndPoint
.RemoteAddr
.Addr
[0] = NTOHL (Ip
);
1597 if (Token
->RemotePort
== 0) {
1598 EndPoint
.RemotePort
= DHCP_SERVER_PORT
;
1600 EndPoint
.RemotePort
= Token
->RemotePort
;
1606 ZeroMem (&Gateway
, sizeof (Gateway
));
1607 if (!IP4_NET_EQUAL (ClientAddr
, EndPoint
.RemoteAddr
.Addr
[0], DhcpSb
->Netmask
)) {
1608 CopyMem (&Gateway
.v4
, &Token
->GatewayAddress
, sizeof (EFI_IPv4_ADDRESS
));
1609 Gateway
.Addr
[0] = NTOHL (Gateway
.Addr
[0]);
1613 // Transmit the DHCP packet.
1615 Status
= UdpIoSendDatagram (Instance
->UdpIo
, Wrap
, &EndPoint
, &Gateway
, DhcpOnPacketSent
, NULL
);
1616 if (EFI_ERROR (Status
)) {
1622 // Start to receive the DHCP response.
1624 Status
= UdpIoRecvDatagram (Instance
->UdpIo
, PxeDhcpInput
, Instance
, 0);
1625 if (EFI_ERROR (Status
)) {
1631 if (EFI_ERROR (Status
) && (Instance
->UdpIo
!= NULL
)) {
1632 UdpIoCleanIo (Instance
->UdpIo
);
1633 gBS
->CloseProtocol (
1634 Instance
->UdpIo
->UdpHandle
,
1635 &gEfiUdp4ProtocolGuid
,
1636 Instance
->Service
->Image
,
1639 UdpIoFreeIo (Instance
->UdpIo
);
1640 Instance
->UdpIo
= NULL
;
1641 Instance
->Token
= NULL
;
1644 gBS
->RestoreTPL (OldTpl
);
1646 if (!EFI_ERROR (Status
) && (Token
->CompletionEvent
== NULL
)) {
1648 // Keep polling until timeout if no error happens and the CompletionEvent
1652 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1654 // Raise TPL to protect the UDPIO in instance, in case that DhcpOnTimerTick
1655 // free it when timeout.
1657 if (Instance
->Timeout
> 0) {
1658 Instance
->UdpIo
->Protocol
.Udp4
->Poll (Instance
->UdpIo
->Protocol
.Udp4
);
1659 gBS
->RestoreTPL (OldTpl
);
1661 gBS
->RestoreTPL (OldTpl
);
1672 Callback function for DhcpIterateOptions. This callback sets the
1673 EFI_DHCP4_PACKET_OPTION array in the DHCP_PARSE_CONTEXT to point
1674 the individual DHCP option in the packet.
1676 @param[in] Tag The DHCP option type
1677 @param[in] Len Length of the DHCP option data
1678 @param[in] Data The DHCP option data
1679 @param[in] Context The context, to pass several parameters in.
1681 @retval EFI_SUCCESS It always returns EFI_SUCCESS
1685 Dhcp4ParseCheckOption (
1692 DHCP_PARSE_CONTEXT
*Parse
;
1694 Parse
= (DHCP_PARSE_CONTEXT
*) Context
;
1697 if (Parse
->Index
<= Parse
->OptionCount
) {
1699 // Use BASE_CR to get the memory position of EFI_DHCP4_PACKET_OPTION for
1700 // the EFI_DHCP4_PACKET_OPTION->Data because DhcpIterateOptions only
1701 // pass in the point to option data.
1703 Parse
->Option
[Parse
->Index
- 1] = BASE_CR (Data
, EFI_DHCP4_PACKET_OPTION
, Data
);
1711 Parses the packed DHCP option data.
1713 The Parse() function is used to retrieve the option list from a DHCP packet.
1714 If *OptionCount isn't zero, and there is enough space for all the DHCP options
1715 in the Packet, each element of PacketOptionList is set to point to somewhere in
1716 the Packet->Dhcp4.Option where a new DHCP option begins. If RFC3396 is supported,
1717 the caller should reassemble the parsed DHCP options to get the finial result.
1718 If *OptionCount is zero or there isn't enough space for all of them, the number
1719 of DHCP options in the Packet is returned in OptionCount.
1721 @param This Pointer to the EFI_DHCP4_PROTOCOL instance.
1722 @param Packet Pointer to packet to be parsed.
1723 @param OptionCount On input, the number of entries in the PacketOptionList.
1724 On output, the number of entries that were written into the
1726 @param PacketOptionList List of packet option entries to be filled in. End option or pad
1727 options are not included.
1729 @retval EFI_SUCCESS The packet was successfully parsed.
1730 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
1731 @retval EFI_BUFFER_TOO_SMALL One or more of the following conditions is TRUE:
1732 1) *OptionCount is smaller than the number of options that
1733 were found in the Packet.
1734 2) PacketOptionList is NULL.
1740 IN EFI_DHCP4_PROTOCOL
*This
,
1741 IN EFI_DHCP4_PACKET
*Packet
,
1742 IN OUT UINT32
*OptionCount
,
1743 OUT EFI_DHCP4_PACKET_OPTION
*PacketOptionList
[] OPTIONAL
1746 DHCP_PARSE_CONTEXT Context
;
1750 // First validate the parameters
1752 if ((This
== NULL
) || (Packet
== NULL
) || (OptionCount
== NULL
)) {
1753 return EFI_INVALID_PARAMETER
;
1756 if ((Packet
->Size
< Packet
->Length
+ 2 * sizeof (UINT32
)) ||
1757 (Packet
->Dhcp4
.Magik
!= DHCP_OPTION_MAGIC
) ||
1758 EFI_ERROR (DhcpValidateOptions (Packet
, NULL
))) {
1760 return EFI_INVALID_PARAMETER
;
1763 if ((*OptionCount
!= 0) && (PacketOptionList
== NULL
)) {
1764 return EFI_BUFFER_TOO_SMALL
;
1767 ZeroMem (PacketOptionList
, *OptionCount
* sizeof (EFI_DHCP4_PACKET_OPTION
*));
1769 Context
.Option
= PacketOptionList
;
1770 Context
.OptionCount
= *OptionCount
;
1773 Status
= DhcpIterateOptions (Packet
, Dhcp4ParseCheckOption
, &Context
);
1775 if (EFI_ERROR (Status
)) {
1779 *OptionCount
= Context
.Index
;
1781 if (Context
.Index
> Context
.OptionCount
) {
1782 return EFI_BUFFER_TOO_SMALL
;
1789 Set the elapsed time based on the given instance and the pointer to the
1790 elapsed time option.
1792 @param[in] Elapsed The pointer to the position to append.
1793 @param[in] Instance The pointer to the Dhcp4 instance.
1798 IN DHCP_PROTOCOL
*Instance
1801 WriteUnaligned16 (Elapsed
, HTONS(Instance
->ElaspedTime
));