2 Dhcp6 support functions implementation.
4 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "Dhcp6Impl.h"
20 Generate client Duid in the format of Duid-llt.
22 @param[in] Mode The pointer to the mode of SNP.
24 @retval NULL If it failed to generate a client Id.
25 @retval others The pointer to the new client id.
29 Dhcp6GenerateClientId (
30 IN EFI_SIMPLE_NETWORK_MODE
*Mode
41 // Attempt to get client Id from variable to keep it constant.
42 // See details in section-9 of rfc-3315.
44 GetVariable2 (L
"ClientId", &gEfiDhcp6ServiceBindingProtocolGuid
, (VOID
**)&Duid
, NULL
);
50 // The format of client identifier option:
52 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
53 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 // | OPTION_CLIENTID | option-len |
55 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 // . (variable length) .
60 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 // If System UUID is found from SMBIOS Table, use DUID-UUID type.
66 if (!EFI_ERROR (NetLibGetSystemGuid (&Uuid
))) {
69 // The format of DUID-UUID:
71 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
72 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 // | DUID-Type (4) | UUID (128 bits) |
74 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
77 // | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
79 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
82 // sizeof (option-len + Duid-type + UUID-size) = 20 bytes
84 Duid
= AllocateZeroPool (2 + 2 + sizeof (EFI_GUID
));
90 // sizeof (Duid-type + UUID-size) = 18 bytes
92 Duid
->Length
= (UINT16
) (18);
95 // Set the Duid-type and copy UUID.
97 WriteUnaligned16 ((UINT16
*) (Duid
->Duid
), HTONS (Dhcp6DuidTypeUuid
));
99 CopyMem (Duid
->Duid
+ 2, &Uuid
, sizeof(EFI_GUID
));
105 // The format of DUID-LLT:
107 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
108 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 // | Duid type (1) | hardware type (16 bits) |
110 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111 // | time (32 bits) |
112 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114 // . link-layer address (variable length) .
116 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120 // Generate a time stamp of the seconds from 2000/1/1, assume 30day/month.
122 gRT
->GetTime (&Time
, NULL
);
125 (((((Time
.Year
- 2000) * 360 + (Time
.Month
- 1)) * 30 + (Time
.Day
- 1)) * 24 + Time
.Hour
) * 60 + Time
.Minute
) *
131 // sizeof (option-len + Duid-type + hardware-type + time) = 10 bytes
133 Duid
= AllocateZeroPool (10 + Mode
->HwAddressSize
);
139 // sizeof (Duid-type + hardware-type + time) = 8 bytes
141 Duid
->Length
= (UINT16
) (Mode
->HwAddressSize
+ 8);
144 // Set the Duid-type, hardware-type, time and copy the hardware address.
146 WriteUnaligned16 ((UINT16
*) (Duid
->Duid
), HTONS (Dhcp6DuidTypeLlt
));
147 WriteUnaligned16 ((UINT16
*) (Duid
->Duid
+ 2), HTONS (NET_IFTYPE_ETHERNET
));
148 WriteUnaligned32 ((UINT32
*) (Duid
->Duid
+ 4), HTONL (Stamp
));
150 CopyMem (Duid
->Duid
+ 8, &Mode
->CurrentAddress
, Mode
->HwAddressSize
);
153 Status
= gRT
->SetVariable (
155 &gEfiDhcp6ServiceBindingProtocolGuid
,
156 (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
),
160 ASSERT_EFI_ERROR (Status
);
167 Copy the Dhcp6 configure data.
169 @param[in] DstCfg The pointer to the destination configure data.
170 @param[in] SorCfg The pointer to the source configure data.
172 @retval EFI_SUCCESS Copy the content from SorCfg from DstCfg successfully.
173 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
177 Dhcp6CopyConfigData (
178 IN EFI_DHCP6_CONFIG_DATA
*DstCfg
,
179 IN EFI_DHCP6_CONFIG_DATA
*SorCfg
183 UINTN OptionListSize
;
186 CopyMem (DstCfg
, SorCfg
, sizeof (EFI_DHCP6_CONFIG_DATA
));
189 // Allocate another buffer for solicitretransmission, and copy it.
191 if (SorCfg
->SolicitRetransmission
!= NULL
) {
193 DstCfg
->SolicitRetransmission
= AllocateZeroPool (sizeof (EFI_DHCP6_RETRANSMISSION
));
195 if (DstCfg
->SolicitRetransmission
== NULL
) {
197 // Error will be handled out of this function.
199 return EFI_OUT_OF_RESOURCES
;
203 DstCfg
->SolicitRetransmission
,
204 SorCfg
->SolicitRetransmission
,
205 sizeof (EFI_DHCP6_RETRANSMISSION
)
209 if (SorCfg
->OptionList
!= NULL
&& SorCfg
->OptionCount
!= 0) {
211 OptionListSize
= SorCfg
->OptionCount
* sizeof (EFI_DHCP6_PACKET_OPTION
*);
212 DstCfg
->OptionList
= AllocateZeroPool (OptionListSize
);
214 if (DstCfg
->OptionList
== NULL
) {
216 // Error will be handled out of this function.
218 return EFI_OUT_OF_RESOURCES
;
221 for (Index
= 0; Index
< SorCfg
->OptionCount
; Index
++) {
223 OptionSize
= NTOHS (SorCfg
->OptionList
[Index
]->OpLen
) + 4;
224 DstCfg
->OptionList
[Index
] = AllocateZeroPool (OptionSize
);
226 if (DstCfg
->OptionList
[Index
] == NULL
) {
228 // Error will be handled out of this function.
230 return EFI_OUT_OF_RESOURCES
;
234 DstCfg
->OptionList
[Index
],
235 SorCfg
->OptionList
[Index
],
246 Clean up the configure data.
248 @param[in, out] CfgData The pointer to the configure data.
252 Dhcp6CleanupConfigData (
253 IN OUT EFI_DHCP6_CONFIG_DATA
*CfgData
258 ASSERT (CfgData
!= NULL
);
260 // Clean up all fields in config data including the reference buffers, but do
261 // not free the config data buffer itself.
263 if (CfgData
->OptionList
!= NULL
) {
264 for (Index
= 0; Index
< CfgData
->OptionCount
; Index
++) {
265 if (CfgData
->OptionList
[Index
] != NULL
) {
266 FreePool (CfgData
->OptionList
[Index
]);
269 FreePool (CfgData
->OptionList
);
272 if (CfgData
->SolicitRetransmission
!= NULL
) {
273 FreePool (CfgData
->SolicitRetransmission
);
276 ZeroMem (CfgData
, sizeof (EFI_DHCP6_CONFIG_DATA
));
281 Clean up the mode data.
283 @param[in, out] ModeData The pointer to the mode data.
287 Dhcp6CleanupModeData (
288 IN OUT EFI_DHCP6_MODE_DATA
*ModeData
291 ASSERT (ModeData
!= NULL
);
293 // Clean up all fields in mode data including the reference buffers, but do
294 // not free the mode data buffer itself.
296 if (ModeData
->ClientId
!= NULL
) {
297 FreePool (ModeData
->ClientId
);
300 if (ModeData
->Ia
!= NULL
) {
302 if (ModeData
->Ia
->ReplyPacket
!= NULL
) {
303 FreePool (ModeData
->Ia
->ReplyPacket
);
305 FreePool (ModeData
->Ia
);
308 ZeroMem (ModeData
, sizeof (EFI_DHCP6_MODE_DATA
));
313 Calculate the expire time by the algorithm defined in rfc.
315 @param[in] Base The base value of the time.
316 @param[in] IsFirstRt If TRUE, it is the first time to calculate expire time.
317 @param[in] NeedSigned If TRUE, the the signed factor is needed.
319 @return Expire The calculated result for the new expire time.
323 Dhcp6CalculateExpireTime (
325 IN BOOLEAN IsFirstRt
,
326 IN BOOLEAN NeedSigned
335 // Take the 10bits of microsecond in system time as a uniform distribution.
336 // Take the 10th bit as a flag to determine it's signed or not.
338 gRT
->GetTime (&Time
, NULL
);
339 Seed
= ((Time
.Nanosecond
>> 10) & DHCP6_10_BIT_MASK
);
340 Signed
= (BOOLEAN
) ((((Time
.Nanosecond
>> 9) & 0x01) != 0) ? TRUE
: FALSE
);
341 Signed
= (BOOLEAN
) (NeedSigned
? Signed
: FALSE
);
344 // Calculate expire by the following algo:
345 // 1. base + base * (-0.1 ~ 0) for the first solicit
346 // 2. base + base * (-0.1 ~ 0.1) for the first other messages
347 // 3. 2 * base + base * (-0.1 ~ 0.1) for the subsequent all messages
348 // 4. base + base * (-0.1 ~ 0) for the more than mrt timeout
350 // The (Seed / 0x3ff / 10) is used to a random range (0, 0.1).
352 if (IsFirstRt
&& Signed
) {
354 Expire
= Base
- (UINT32
) (Base
* Seed
/ DHCP6_10_BIT_MASK
/ 10);
356 } else if (IsFirstRt
&& !Signed
) {
358 Expire
= Base
+ (UINT32
) (Base
* Seed
/ DHCP6_10_BIT_MASK
/ 10);
360 } else if (!IsFirstRt
&& Signed
) {
362 Expire
= 2 * Base
- (UINT32
) (Base
* Seed
/ DHCP6_10_BIT_MASK
/ 10);
366 Expire
= 2 * Base
+ (UINT32
) (Base
* Seed
/ DHCP6_10_BIT_MASK
/ 10);
369 Expire
= (Expire
!= 0) ? Expire
: 1;
376 Calculate the lease time by the algorithm defined in rfc.
378 @param[in] IaCb The pointer to the Ia control block.
382 Dhcp6CalculateLeaseTime (
386 EFI_DHCP6_IA_ADDRESS
*IaAddr
;
391 ASSERT (IaCb
->Ia
->IaAddressCount
> 0);
393 MinLt
= (UINT32
) (-1);
397 // Calculate minlt as min of all valid life time, and maxlt as max of all
400 for (Index
= 0; Index
< IaCb
->Ia
->IaAddressCount
; Index
++) {
401 IaAddr
= IaCb
->Ia
->IaAddress
+ Index
* sizeof (EFI_DHCP6_IA_ADDRESS
);
402 MinLt
= MIN (MinLt
, IaAddr
->ValidLifetime
);
403 MaxLt
= MAX (MinLt
, IaAddr
->ValidLifetime
);
407 // Take 50% minlt as t1, and 80% maxlt as t2 if Dhcp6 server doesn't offer
410 IaCb
->T1
= (IaCb
->T1
!= 0) ? IaCb
->T1
: (UINT32
)(MinLt
* 5 / 10);
411 IaCb
->T2
= (IaCb
->T2
!= 0) ? IaCb
->T2
: (UINT32
)(MinLt
* 8 / 10);
412 IaCb
->AllExpireTime
= MaxLt
;
418 Check whether the addresses are all included by the configured Ia.
420 @param[in] Ia The pointer to the Ia.
421 @param[in] AddressCount The number of addresses.
422 @param[in] Addresses The pointer to the addresses buffer.
424 @retval EFI_SUCCESS The addresses are all included by the configured IA.
425 @retval EFI_NOT_FOUND The addresses are not included by the configured IA.
431 IN UINT32 AddressCount
,
432 IN EFI_IPv6_ADDRESS
*Addresses
440 // Check whether the addresses are all included by the configured IA. And it
441 // will return success if address count is zero, which means all addresses.
443 for (Index1
= 0; Index1
< AddressCount
; Index1
++) {
447 for (Index2
= 0; Index2
< Ia
->IaAddressCount
; Index2
++) {
451 &Ia
->IaAddress
[Index2
],
452 sizeof (EFI_IPv6_ADDRESS
)
461 return EFI_NOT_FOUND
;
470 Deprive the addresses from current Ia, and generate another eliminated Ia.
472 @param[in] Ia The pointer to the Ia.
473 @param[in] AddressCount The number of addresses.
474 @param[in] Addresses The pointer to the addresses buffer.
476 @retval NULL If it failed to generate the deprived Ia.
477 @retval others The pointer to the deprived Ia.
481 Dhcp6DepriveAddress (
483 IN UINT32 AddressCount
,
484 IN EFI_IPv6_ADDRESS
*Addresses
487 EFI_DHCP6_IA
*IaCopy
;
493 if (AddressCount
== 0) {
495 // It means release all Ia addresses if address count is zero.
497 AddressCount
= Ia
->IaAddressCount
;
500 ASSERT (AddressCount
!= 0);
502 IaCopySize
= sizeof (EFI_DHCP6_IA
) + (AddressCount
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
503 IaCopy
= AllocateZeroPool (IaCopySize
);
505 if (IaCopy
== NULL
) {
509 if (AddressCount
== Ia
->IaAddressCount
) {
511 // If release all Ia addresses, just copy the configured Ia and then set
512 // its address count as zero.
513 // We may decline/release part of addresses at the begining. So it's a
514 // forwarding step to update address infor for decline/release, while the
515 // other infor such as Ia state will be updated when receiving reply.
517 CopyMem (IaCopy
, Ia
, IaCopySize
);
518 Ia
->IaAddressCount
= 0;
522 CopyMem (IaCopy
, Ia
, sizeof (EFI_DHCP6_IA
));
525 // Move the addresses from the Ia of instance to the deprived Ia.
527 for (Index1
= 0; Index1
< AddressCount
; Index1
++) {
531 for (Index2
= 0; Index2
< Ia
->IaAddressCount
; Index2
++) {
535 &Ia
->IaAddress
[Index2
],
536 sizeof (EFI_IPv6_ADDRESS
)
539 // Copy the deprived address to the copy of Ia
542 &IaCopy
->IaAddress
[Index1
],
543 &Ia
->IaAddress
[Index2
],
544 sizeof (EFI_DHCP6_IA_ADDRESS
)
547 // Delete the deprived address from the instance Ia
549 if (Index2
+ 1 < Ia
->IaAddressCount
) {
551 &Ia
->IaAddress
[Index2
],
552 &Ia
->IaAddress
[Index2
+ 1],
553 (Ia
->IaAddressCount
- Index2
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
)
560 ASSERT (Found
== TRUE
);
563 Ia
->IaAddressCount
-= AddressCount
;
564 IaCopy
->IaAddressCount
= AddressCount
;
571 The dummy ext buffer free callback routine.
573 @param[in] Arg The pointer to the parameter.
586 The callback routine once message transmitted.
588 @param[in] Wrap The pointer to the received net buffer.
589 @param[in] EndPoint The pointer to the udp end point.
590 @param[in] IoStatus The return status from udp io.
591 @param[in] Context The opaque parameter to the function.
598 IN UDP_END_POINT
*EndPoint
,
599 IN EFI_STATUS IoStatus
,
608 Append the option to Buf, and move Buf to the end.
610 @param[in, out] Buf The pointer to the buffer.
611 @param[in] OptType The option type.
612 @param[in] OptLen The length of option contents.
613 @param[in] Data The pointer to the option content.
615 @return Buf The position to append the next option.
627 // The format of Dhcp6 option:
629 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
630 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
631 // | option-code | option-len (option data) |
632 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
634 // | (option-len octets) |
635 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
638 ASSERT (OptLen
!= 0);
640 WriteUnaligned16 ((UINT16
*) Buf
, OptType
);
642 WriteUnaligned16 ((UINT16
*) Buf
, OptLen
);
644 CopyMem (Buf
, Data
, NTOHS (OptLen
));
645 Buf
+= NTOHS (OptLen
);
652 Append the appointed Ia option to Buf, and move Buf to the end.
654 @param[in, out] Buf The pointer to the position to append.
655 @param[in] Ia The pointer to the Ia.
656 @param[in] T1 The time of T1.
657 @param[in] T2 The time of T2.
659 @return Buf The position to append the next Ia option.
663 Dhcp6AppendIaOption (
676 // The format of IA_NA and IA_TA option:
678 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
679 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
680 // | OPTION_IA_NA | option-len |
681 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
682 // | IAID (4 octets) |
683 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
684 // | T1 (only for IA_NA) |
685 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
686 // | T2 (only for IA_NA) |
687 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
689 // . IA_NA-options/IA_TA-options .
691 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
695 // Fill the value of Ia option type
697 WriteUnaligned16 ((UINT16
*) Buf
, HTONS (Ia
->Descriptor
.Type
));
701 // Fill the len of Ia option later, keep the pointer first
703 Len
= (UINT16
*) Buf
;
707 // Fill the value of iaid
709 WriteUnaligned32 ((UINT32
*) Buf
, HTONL (Ia
->Descriptor
.IaId
));
713 // Fill the value of t1 and t2 if iana, keep it 0xffffffff if no specified.
715 if (Ia
->Descriptor
.Type
== Dhcp6OptIana
) {
716 WriteUnaligned32 ((UINT32
*) Buf
, ((T1
!= 0) ? T1
: 0xffffffff));
718 WriteUnaligned32 ((UINT32
*) Buf
, ((T2
!= 0) ? T2
: 0xffffffff));
723 // Fill all the addresses belong to the Ia
725 for (Index
= 0; Index
< Ia
->IaAddressCount
; Index
++) {
727 AddrOpt
= (UINT8
*) Ia
->IaAddress
+ Index
* sizeof (EFI_DHCP6_IA_ADDRESS
);
728 Length
= HTONS ((UINT16
) sizeof (EFI_DHCP6_IA_ADDRESS
));
729 Buf
= Dhcp6AppendOption (
731 HTONS (Dhcp6OptIaAddr
),
738 // Fill the value of Ia option length
740 *Len
= HTONS ((UINT16
) (Buf
- (UINT8
*) Len
- 2));
746 Append the appointed Elapsed time option to Buf, and move Buf to the end.
748 @param[in, out] Buf The pointer to the position to append.
749 @param[in] Instance The pointer to the Dhcp6 instance.
750 @param[out] Elapsed The pointer to the elapsed time value in
751 the generated packet.
753 @return Buf The position to append the next Ia option.
757 Dhcp6AppendETOption (
759 IN DHCP6_INSTANCE
*Instance
,
764 // The format of elapsed time option:
766 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
767 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
768 // | OPTION_ELAPSED_TIME | option-len |
769 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
771 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
775 // Fill the value of elapsed-time option type.
777 WriteUnaligned16 ((UINT16
*) Buf
, HTONS (Dhcp6OptElapsedTime
));
781 // Fill the len of elapsed-time option, which is fixed.
783 WriteUnaligned16 ((UINT16
*) Buf
, HTONS(2));
787 // Fill in elapsed time value with 0 value for now. The actual value is
788 // filled in later just before the packet is transmitted.
790 WriteUnaligned16 ((UINT16
*) Buf
, HTONS(0));
791 *Elapsed
= (UINT16
*) Buf
;
798 Set the elapsed time based on the given instance and the pointer to the
801 @param[in] Elapsed The pointer to the position to append.
802 @param[in] Instance The pointer to the Dhcp6 instance.
808 IN DHCP6_INSTANCE
*Instance
813 UINT64 ElapsedTimeValue
;
816 // Generate a time stamp of the centiseconds from 2000/1/1, assume 30day/month.
818 gRT
->GetTime (&Time
, NULL
);
819 CurrentStamp
= (UINT64
)
821 ((((((Time
.Year
- 2000) * 360 +
822 (Time
.Month
- 1)) * 30 +
823 (Time
.Day
- 1)) * 24 + Time
.Hour
) * 60 +
824 Time
.Minute
) * 60 + Time
.Second
) * 100
825 + DivU64x32(Time
.Nanosecond
, 10000000)
829 // Sentinel value of 0 means that this is the first DHCP packet that we are
830 // sending and that we need to initialize the value. First DHCP Solicit
831 // gets 0 elapsed-time. Otherwise, calculate based on StartTime.
833 if (Instance
->StartTime
== 0) {
834 ElapsedTimeValue
= 0;
835 Instance
->StartTime
= CurrentStamp
;
837 ElapsedTimeValue
= CurrentStamp
- Instance
->StartTime
;
840 // If elapsed time cannot fit in two bytes, set it to 0xffff.
842 if (ElapsedTimeValue
> 0xffff) {
843 ElapsedTimeValue
= 0xffff;
846 WriteUnaligned16 (Elapsed
, HTONS((UINT16
) ElapsedTimeValue
));
851 Seek the address of the first byte of the option header.
853 @param[in] Buf The pointer to the buffer.
854 @param[in] SeekLen The length to seek.
855 @param[in] OptType The option type.
857 @retval NULL If it failed to seek the option.
858 @retval others The position to the option.
877 // The format of Dhcp6 option refers to Dhcp6AppendOption().
879 while (Cursor
< Buf
+ SeekLen
) {
880 OpCode
= ReadUnaligned16 ((UINT16
*) Cursor
);
881 if (OpCode
== HTONS (OptType
)) {
885 DataLen
= NTOHS (ReadUnaligned16 ((UINT16
*) (Cursor
+ 2)));
886 Cursor
+= (DataLen
+ 4);
894 Seek the address of the first byte of the Ia option header.
896 @param[in] Buf The pointer to the buffer.
897 @param[in] SeekLen The length to seek.
898 @param[in] IaDesc The pointer to the Ia descriptor.
900 @retval NULL If it failed to seek the Ia option.
901 @retval others The position to the Ia option.
908 IN EFI_DHCP6_IA_DESCRIPTOR
*IaDesc
918 // The format of IA_NA and IA_TA option refers to Dhcp6AppendIaOption().
923 while (Cursor
< Buf
+ SeekLen
) {
924 OpCode
= ReadUnaligned16 ((UINT16
*) Cursor
);
925 IaId
= ReadUnaligned32 ((UINT32
*) (Cursor
+ 4));
926 if (OpCode
== HTONS (IaDesc
->Type
) && IaId
== HTONL (IaDesc
->IaId
)) {
930 DataLen
= NTOHS (ReadUnaligned16 ((UINT16
*) (Cursor
+ 2)));
931 Cursor
+= (DataLen
+ 4);
939 Parse the address option and update the address infomation.
941 @param[in] IaInnerOpt The pointer to the buffer.
942 @param[in] IaInnerLen The length to parse.
943 @param[out] AddrNum The number of addresses.
944 @param[in, out] AddrBuf The pointer to the address buffer.
948 Dhcp6ParseAddrOption (
949 IN UINT8
*IaInnerOpt
,
950 IN UINT16 IaInnerLen
,
952 IN OUT EFI_DHCP6_IA_ADDRESS
*AddrBuf
961 // The format of the IA Address option:
963 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
964 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
965 // | OPTION_IAADDR | option-len |
966 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
971 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
972 // | preferred-lifetime |
973 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
974 // | valid-lifetime |
975 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
977 // . IAaddr-options .
979 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
985 // 1. Pass addrbuf == null, to get the addrnum over the Ia inner options.
986 // 2. Pass addrbuf != null, to resolve the addresses over the Ia inner
987 // options to the addrbuf.
993 while (Cursor
< IaInnerOpt
+ IaInnerLen
) {
995 // Count the Ia address option with non-0 valid time.
997 OpCode
= ReadUnaligned16 ((UINT16
*) Cursor
);
998 ValidLt
= ReadUnaligned32 ((UINT32
*) (Cursor
+ 24));
999 if (OpCode
== HTONS (Dhcp6OptIaAddr
) && ValidLt
!= 0) {
1001 if (AddrBuf
!= NULL
) {
1002 CopyMem (AddrBuf
, Cursor
+ 4, sizeof (EFI_DHCP6_IA_ADDRESS
));
1003 AddrBuf
->PreferredLifetime
= NTOHL (AddrBuf
->PreferredLifetime
);
1004 AddrBuf
->ValidLifetime
= NTOHL (AddrBuf
->ValidLifetime
);
1005 AddrBuf
= (EFI_DHCP6_IA_ADDRESS
*) ((UINT8
*) AddrBuf
+ sizeof (EFI_DHCP6_IA_ADDRESS
));
1010 DataLen
= NTOHS (ReadUnaligned16 ((UINT16
*) (Cursor
+ 2)));
1011 Cursor
+= (DataLen
+ 4);
1017 Create a control blcok for the Ia according to the corresponding options.
1019 @param[in] Instance The pointer to DHCP6 Instance.
1020 @param[in] IaInnerOpt The pointer to the inner options in the Ia option.
1021 @param[in] IaInnerLen The length of all the inner options in the Ia option.
1022 @param[in] T1 T1 time in the Ia option.
1023 @param[in] T2 T2 time in the Ia option.
1025 @retval EFI_NOT_FOUND No valid IA option is found.
1026 @retval EFI_SUCCESS Create an IA control block successfully.
1027 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
1032 IN DHCP6_INSTANCE
*Instance
,
1033 IN UINT8
*IaInnerOpt
,
1034 IN UINT16 IaInnerLen
,
1043 if (Instance
->IaCb
.Ia
== NULL
) {
1044 return EFI_NOT_FOUND
;
1048 // Calculate the number of addresses for this Ia, excluding the addresses with
1049 // the value 0 of valid lifetime.
1051 Dhcp6ParseAddrOption (IaInnerOpt
, IaInnerLen
, &AddrNum
, NULL
);
1054 return EFI_NOT_FOUND
;
1058 // Allocate for new IA.
1060 IaSize
= sizeof (EFI_DHCP6_IA
) + (AddrNum
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1061 Ia
= AllocateZeroPool (IaSize
);
1064 return EFI_OUT_OF_RESOURCES
;
1068 // Fill up this new IA fields.
1070 Ia
->State
= Instance
->IaCb
.Ia
->State
;
1071 Ia
->IaAddressCount
= AddrNum
;
1072 CopyMem (&Ia
->Descriptor
, &Instance
->Config
->IaDescriptor
, sizeof (EFI_DHCP6_IA_DESCRIPTOR
));
1073 Dhcp6ParseAddrOption (IaInnerOpt
, IaInnerLen
, &AddrNum
, Ia
->IaAddress
);
1076 // Free original IA resource.
1078 if (Instance
->IaCb
.Ia
->ReplyPacket
!= NULL
) {
1079 FreePool (Instance
->IaCb
.Ia
->ReplyPacket
);
1081 FreePool (Instance
->IaCb
.Ia
);
1084 ZeroMem (&Instance
->IaCb
, sizeof (DHCP6_IA_CB
));
1087 // Update IaCb to use new IA.
1089 Instance
->IaCb
.Ia
= Ia
;
1093 // Fill in IaCb fields. Such as T1, T2, AllExpireTime and LeaseTime.
1095 Instance
->IaCb
.T1
= T1
;
1096 Instance
->IaCb
.T2
= T2
;
1097 Dhcp6CalculateLeaseTime (&Instance
->IaCb
);
1104 Cache the current IA configuration information.
1106 @param[in] Instance The pointer to DHCP6 Instance.
1108 @retval EFI_SUCCESS Cache the current IA successfully.
1109 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
1114 IN DHCP6_INSTANCE
*Instance
1120 Ia
= Instance
->IaCb
.Ia
;
1122 if ((Instance
->CacheIa
== NULL
) && (Ia
!= NULL
)) {
1124 // Cache the current IA.
1126 IaSize
= sizeof (EFI_DHCP6_IA
) + (Ia
->IaAddressCount
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1128 Instance
->CacheIa
= AllocateZeroPool (IaSize
);
1129 if (Instance
->CacheIa
== NULL
) {
1130 return EFI_OUT_OF_RESOURCES
;
1132 CopyMem (Instance
->CacheIa
, Ia
, IaSize
);
1138 Append CacheIa to the currrent IA. Meanwhile, clear CacheIa.ValidLifetime to 0.
1140 @param[in] Instance The pointer to DHCP6 instance.
1144 Dhcp6AppendCacheIa (
1145 IN DHCP6_INSTANCE
*Instance
1153 EFI_DHCP6_IA
*NewIa
;
1154 EFI_DHCP6_IA
*CacheIa
;
1156 Ia
= Instance
->IaCb
.Ia
;
1157 CacheIa
= Instance
->CacheIa
;
1159 if ((CacheIa
!= NULL
) && (CacheIa
->IaAddressCount
!= 0)) {
1161 // There are old addresses existing. Merge with current addresses.
1163 NewIaSize
= sizeof (EFI_DHCP6_IA
) + (Ia
->IaAddressCount
+ CacheIa
->IaAddressCount
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1164 NewIa
= AllocateZeroPool (NewIaSize
);
1165 if (NewIa
== NULL
) {
1169 IaSize
= sizeof (EFI_DHCP6_IA
) + (Ia
->IaAddressCount
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1170 CopyMem (NewIa
, Ia
, IaSize
);
1173 // Clear old address.ValidLifetime
1175 for (Index
= 0; Index
< CacheIa
->IaAddressCount
; Index
++) {
1176 CacheIa
->IaAddress
[Index
].ValidLifetime
= 0;
1179 NewIa
->IaAddressCount
+= CacheIa
->IaAddressCount
;
1180 Ptr
= (UINT8
*)&NewIa
->IaAddress
[Ia
->IaAddressCount
];
1181 CopyMem (Ptr
, CacheIa
->IaAddress
, CacheIa
->IaAddressCount
* sizeof (EFI_DHCP6_IA_ADDRESS
));
1184 // Migrate to the NewIa and free previous.
1186 FreePool (Instance
->CacheIa
);
1187 FreePool (Instance
->IaCb
.Ia
);
1188 Instance
->CacheIa
= NULL
;
1189 Instance
->IaCb
.Ia
= NewIa
;
1194 Calculate the Dhcp6 get mapping timeout by adding additinal delay to the IP6 DAD transmits count.
1196 @param[in] Ip6Cfg The pointer to Ip6 config protocol.
1197 @param[out] TimeOut The time out value in 100ns units.
1199 @retval EFI_INVALID_PARAMETER Input parameters are invalid.
1200 @retval EFI_SUCCESS Calculate the time out value successfully.
1203 Dhcp6GetMappingTimeOut (
1204 IN EFI_IP6_CONFIG_PROTOCOL
*Ip6Cfg
,
1210 EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS DadXmits
;
1212 if (Ip6Cfg
== NULL
|| TimeOut
== NULL
) {
1213 return EFI_INVALID_PARAMETER
;
1216 DataSize
= sizeof (EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS
);
1217 Status
= Ip6Cfg
->GetData (
1219 Ip6ConfigDataTypeDupAddrDetectTransmits
,
1223 if (EFI_ERROR (Status
)) {
1227 *TimeOut
= TICKS_PER_SECOND
* DadXmits
.DupAddrDetectTransmits
+ DHCP6_DAD_ADDITIONAL_DELAY
;