2 Dhcp6 support functions implementation.
4 Copyright (c) 2009 - 2014, 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
*) ((UINT8
*) Duid
+ OFFSET_OF (EFI_DHCP6_DUID
, Duid
)), HTONS (Dhcp6DuidTypeLlt
));
147 WriteUnaligned16 ((UINT16
*) ((UINT8
*) Duid
+ OFFSET_OF (EFI_DHCP6_DUID
, Duid
) + 2), HTONS (NET_IFTYPE_ETHERNET
));
148 WriteUnaligned32 ((UINT32
*) ((UINT8
*) Duid
+ OFFSET_OF (EFI_DHCP6_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 if (EFI_ERROR (Status
)) {
170 Copy the Dhcp6 configure data.
172 @param[in] DstCfg The pointer to the destination configure data.
173 @param[in] SorCfg The pointer to the source configure data.
175 @retval EFI_SUCCESS Copy the content from SorCfg from DstCfg successfully.
176 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
180 Dhcp6CopyConfigData (
181 IN EFI_DHCP6_CONFIG_DATA
*DstCfg
,
182 IN EFI_DHCP6_CONFIG_DATA
*SorCfg
186 UINTN OptionListSize
;
189 CopyMem (DstCfg
, SorCfg
, sizeof (EFI_DHCP6_CONFIG_DATA
));
192 // Allocate another buffer for solicitretransmission, and copy it.
194 if (SorCfg
->SolicitRetransmission
!= NULL
) {
196 DstCfg
->SolicitRetransmission
= AllocateZeroPool (sizeof (EFI_DHCP6_RETRANSMISSION
));
198 if (DstCfg
->SolicitRetransmission
== NULL
) {
200 // Error will be handled out of this function.
202 return EFI_OUT_OF_RESOURCES
;
206 DstCfg
->SolicitRetransmission
,
207 SorCfg
->SolicitRetransmission
,
208 sizeof (EFI_DHCP6_RETRANSMISSION
)
212 if (SorCfg
->OptionList
!= NULL
&& SorCfg
->OptionCount
!= 0) {
214 OptionListSize
= SorCfg
->OptionCount
* sizeof (EFI_DHCP6_PACKET_OPTION
*);
215 DstCfg
->OptionList
= AllocateZeroPool (OptionListSize
);
217 if (DstCfg
->OptionList
== NULL
) {
219 // Error will be handled out of this function.
221 return EFI_OUT_OF_RESOURCES
;
224 for (Index
= 0; Index
< SorCfg
->OptionCount
; Index
++) {
226 OptionSize
= NTOHS (SorCfg
->OptionList
[Index
]->OpLen
) + 4;
227 DstCfg
->OptionList
[Index
] = AllocateZeroPool (OptionSize
);
229 if (DstCfg
->OptionList
[Index
] == NULL
) {
231 // Error will be handled out of this function.
233 return EFI_OUT_OF_RESOURCES
;
237 DstCfg
->OptionList
[Index
],
238 SorCfg
->OptionList
[Index
],
249 Clean up the configure data.
251 @param[in, out] CfgData The pointer to the configure data.
255 Dhcp6CleanupConfigData (
256 IN OUT EFI_DHCP6_CONFIG_DATA
*CfgData
261 ASSERT (CfgData
!= NULL
);
263 // Clean up all fields in config data including the reference buffers, but do
264 // not free the config data buffer itself.
266 if (CfgData
->OptionList
!= NULL
) {
267 for (Index
= 0; Index
< CfgData
->OptionCount
; Index
++) {
268 if (CfgData
->OptionList
[Index
] != NULL
) {
269 FreePool (CfgData
->OptionList
[Index
]);
272 FreePool (CfgData
->OptionList
);
275 if (CfgData
->SolicitRetransmission
!= NULL
) {
276 FreePool (CfgData
->SolicitRetransmission
);
279 ZeroMem (CfgData
, sizeof (EFI_DHCP6_CONFIG_DATA
));
284 Clean up the mode data.
286 @param[in, out] ModeData The pointer to the mode data.
290 Dhcp6CleanupModeData (
291 IN OUT EFI_DHCP6_MODE_DATA
*ModeData
294 ASSERT (ModeData
!= NULL
);
296 // Clean up all fields in mode data including the reference buffers, but do
297 // not free the mode data buffer itself.
299 if (ModeData
->ClientId
!= NULL
) {
300 FreePool (ModeData
->ClientId
);
303 if (ModeData
->Ia
!= NULL
) {
305 if (ModeData
->Ia
->ReplyPacket
!= NULL
) {
306 FreePool (ModeData
->Ia
->ReplyPacket
);
308 FreePool (ModeData
->Ia
);
311 ZeroMem (ModeData
, sizeof (EFI_DHCP6_MODE_DATA
));
316 Calculate the expire time by the algorithm defined in rfc.
318 @param[in] Base The base value of the time.
319 @param[in] IsFirstRt If TRUE, it is the first time to calculate expire time.
320 @param[in] NeedSigned If TRUE, the the signed factor is needed.
322 @return Expire The calculated result for the new expire time.
326 Dhcp6CalculateExpireTime (
328 IN BOOLEAN IsFirstRt
,
329 IN BOOLEAN NeedSigned
338 // Take the 10bits of microsecond in system time as a uniform distribution.
339 // Take the 10th bit as a flag to determine it's signed or not.
341 gRT
->GetTime (&Time
, NULL
);
342 Seed
= ((Time
.Nanosecond
>> 10) & DHCP6_10_BIT_MASK
);
343 Signed
= (BOOLEAN
) ((((Time
.Nanosecond
>> 9) & 0x01) != 0) ? TRUE
: FALSE
);
344 Signed
= (BOOLEAN
) (NeedSigned
? Signed
: FALSE
);
347 // Calculate expire by the following algo:
348 // 1. base + base * (-0.1 ~ 0) for the first solicit
349 // 2. base + base * (-0.1 ~ 0.1) for the first other messages
350 // 3. 2 * base + base * (-0.1 ~ 0.1) for the subsequent all messages
351 // 4. base + base * (-0.1 ~ 0) for the more than mrt timeout
353 // The (Seed / 0x3ff / 10) is used to a random range (0, 0.1).
355 if (IsFirstRt
&& Signed
) {
357 Expire
= Base
- (UINT32
) (Base
* Seed
/ DHCP6_10_BIT_MASK
/ 10);
359 } else if (IsFirstRt
&& !Signed
) {
361 Expire
= Base
+ (UINT32
) (Base
* Seed
/ DHCP6_10_BIT_MASK
/ 10);
363 } else if (!IsFirstRt
&& Signed
) {
365 Expire
= 2 * Base
- (UINT32
) (Base
* Seed
/ DHCP6_10_BIT_MASK
/ 10);
369 Expire
= 2 * Base
+ (UINT32
) (Base
* Seed
/ DHCP6_10_BIT_MASK
/ 10);
372 Expire
= (Expire
!= 0) ? Expire
: 1;
379 Calculate the lease time by the algorithm defined in rfc.
381 @param[in] IaCb The pointer to the Ia control block.
385 Dhcp6CalculateLeaseTime (
393 ASSERT (IaCb
->Ia
->IaAddressCount
> 0);
395 MinLt
= (UINT32
) (-1);
399 // Calculate minlt as min of all valid life time, and maxlt as max of all
402 for (Index
= 0; Index
< IaCb
->Ia
->IaAddressCount
; Index
++) {
403 MinLt
= MIN (MinLt
, IaCb
->Ia
->IaAddress
[Index
].ValidLifetime
);
404 MaxLt
= MAX (MinLt
, IaCb
->Ia
->IaAddress
[Index
].ValidLifetime
);
408 // Take 50% minlt as t1, and 80% maxlt as t2 if Dhcp6 server doesn't offer
411 IaCb
->T1
= (IaCb
->T1
!= 0) ? IaCb
->T1
: (UINT32
)(MinLt
* 5 / 10);
412 IaCb
->T2
= (IaCb
->T2
!= 0) ? IaCb
->T2
: (UINT32
)(MinLt
* 8 / 10);
413 IaCb
->AllExpireTime
= MaxLt
;
419 Check whether the addresses are all included by the configured Ia.
421 @param[in] Ia The pointer to the Ia.
422 @param[in] AddressCount The number of addresses.
423 @param[in] Addresses The pointer to the addresses buffer.
425 @retval EFI_SUCCESS The addresses are all included by the configured IA.
426 @retval EFI_NOT_FOUND The addresses are not included by the configured IA.
432 IN UINT32 AddressCount
,
433 IN EFI_IPv6_ADDRESS
*Addresses
441 // Check whether the addresses are all included by the configured IA. And it
442 // will return success if address count is zero, which means all addresses.
444 for (Index1
= 0; Index1
< AddressCount
; Index1
++) {
448 for (Index2
= 0; Index2
< Ia
->IaAddressCount
; Index2
++) {
452 &Ia
->IaAddress
[Index2
],
453 sizeof (EFI_IPv6_ADDRESS
)
462 return EFI_NOT_FOUND
;
471 Deprive the addresses from current Ia, and generate another eliminated Ia.
473 @param[in] Ia The pointer to the Ia.
474 @param[in] AddressCount The number of addresses.
475 @param[in] Addresses The pointer to the addresses buffer.
477 @retval NULL If it failed to generate the deprived Ia.
478 @retval others The pointer to the deprived Ia.
482 Dhcp6DepriveAddress (
484 IN UINT32 AddressCount
,
485 IN EFI_IPv6_ADDRESS
*Addresses
488 EFI_DHCP6_IA
*IaCopy
;
494 if (AddressCount
== 0) {
496 // It means release all Ia addresses if address count is zero.
498 AddressCount
= Ia
->IaAddressCount
;
501 ASSERT (AddressCount
!= 0);
503 IaCopySize
= sizeof (EFI_DHCP6_IA
) + (AddressCount
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
504 IaCopy
= AllocateZeroPool (IaCopySize
);
506 if (IaCopy
== NULL
) {
510 if (AddressCount
== Ia
->IaAddressCount
) {
512 // If release all Ia addresses, just copy the configured Ia and then set
513 // its address count as zero.
514 // We may decline/release part of addresses at the begining. So it's a
515 // forwarding step to update address infor for decline/release, while the
516 // other infor such as Ia state will be updated when receiving reply.
518 CopyMem (IaCopy
, Ia
, IaCopySize
);
519 Ia
->IaAddressCount
= 0;
523 CopyMem (IaCopy
, Ia
, sizeof (EFI_DHCP6_IA
));
526 // Move the addresses from the Ia of instance to the deprived Ia.
528 for (Index1
= 0; Index1
< AddressCount
; Index1
++) {
532 for (Index2
= 0; Index2
< Ia
->IaAddressCount
; Index2
++) {
536 &Ia
->IaAddress
[Index2
],
537 sizeof (EFI_IPv6_ADDRESS
)
540 // Copy the deprived address to the copy of Ia
543 &IaCopy
->IaAddress
[Index1
],
544 &Ia
->IaAddress
[Index2
],
545 sizeof (EFI_DHCP6_IA_ADDRESS
)
548 // Delete the deprived address from the instance Ia
550 if (Index2
+ 1 < Ia
->IaAddressCount
) {
552 &Ia
->IaAddress
[Index2
],
553 &Ia
->IaAddress
[Index2
+ 1],
554 (Ia
->IaAddressCount
- Index2
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
)
561 ASSERT (Found
== TRUE
);
564 Ia
->IaAddressCount
-= AddressCount
;
565 IaCopy
->IaAddressCount
= AddressCount
;
572 The dummy ext buffer free callback routine.
574 @param[in] Arg The pointer to the parameter.
587 The callback routine once message transmitted.
589 @param[in] Wrap The pointer to the received net buffer.
590 @param[in] EndPoint The pointer to the udp end point.
591 @param[in] IoStatus The return status from udp io.
592 @param[in] Context The opaque parameter to the function.
599 IN UDP_END_POINT
*EndPoint
,
600 IN EFI_STATUS IoStatus
,
609 Append the option to Buf, and move Buf to the end.
611 @param[in, out] Buf The pointer to the buffer.
612 @param[in] OptType The option type.
613 @param[in] OptLen The length of option contents.
614 @param[in] Data The pointer to the option content.
616 @return Buf The position to append the next option.
628 // The format of Dhcp6 option:
630 // 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
631 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
632 // | option-code | option-len (option data) |
633 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
635 // | (option-len octets) |
636 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
639 ASSERT (OptLen
!= 0);
641 WriteUnaligned16 ((UINT16
*) Buf
, OptType
);
643 WriteUnaligned16 ((UINT16
*) Buf
, OptLen
);
645 CopyMem (Buf
, Data
, NTOHS (OptLen
));
646 Buf
+= NTOHS (OptLen
);
652 Append the appointed IA Address option to Buf, and move Buf to the end.
654 @param[in, out] Buf The pointer to the position to append.
655 @param[in] IaAddr The pointer to the IA Address.
656 @param[in] MessageType Message type of DHCP6 package.
658 @return Buf The position to append the next option.
662 Dhcp6AppendIaAddrOption (
664 IN EFI_DHCP6_IA_ADDRESS
*IaAddr
,
665 IN UINT32 MessageType
669 // The format of the IA Address option is:
672 // 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
673 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
674 // | OPTION_IAADDR | option-len |
675 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
680 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
681 // | preferred-lifetime |
682 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
683 // | valid-lifetime |
684 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
686 // . IAaddr-options .
688 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
691 // Fill the value of Ia Address option type
693 WriteUnaligned16 ((UINT16
*) Buf
, HTONS (Dhcp6OptIaAddr
));
696 WriteUnaligned16 ((UINT16
*) Buf
, HTONS (sizeof (EFI_DHCP6_IA_ADDRESS
)));
699 CopyMem (Buf
, &IaAddr
->IpAddress
, sizeof(EFI_IPv6_ADDRESS
));
700 Buf
+= sizeof(EFI_IPv6_ADDRESS
);
703 // Fill the value of preferred-lifetime and valid-lifetime.
704 // According to RFC3315 Chapter 18.1.2, the preferred-lifetime and valid-lifetime fields
705 // should set to 0 when initiate a Confirm message.
707 if (MessageType
!= Dhcp6MsgConfirm
) {
708 WriteUnaligned32 ((UINT32
*) Buf
, HTONL (IaAddr
->PreferredLifetime
));
712 if (MessageType
!= Dhcp6MsgConfirm
) {
713 WriteUnaligned32 ((UINT32
*) Buf
, HTONL (IaAddr
->ValidLifetime
));
722 Append the appointed Ia option to Buf, and move Buf to the end.
724 @param[in, out] Buf The pointer to the position to append.
725 @param[in] Ia The pointer to the Ia.
726 @param[in] T1 The time of T1.
727 @param[in] T2 The time of T2.
728 @param[in] MessageType Message type of DHCP6 package.
730 @return Buf The position to append the next Ia option.
734 Dhcp6AppendIaOption (
739 IN UINT32 MessageType
747 // The format of IA_NA and IA_TA option:
749 // 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
750 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
751 // | OPTION_IA_NA | option-len |
752 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
753 // | IAID (4 octets) |
754 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
755 // | T1 (only for IA_NA) |
756 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
757 // | T2 (only for IA_NA) |
758 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
760 // . IA_NA-options/IA_TA-options .
762 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
766 // Fill the value of Ia option type
768 WriteUnaligned16 ((UINT16
*) Buf
, HTONS (Ia
->Descriptor
.Type
));
772 // Fill the len of Ia option later, keep the pointer first
774 Len
= (UINT16
*) Buf
;
778 // Fill the value of iaid
780 WriteUnaligned32 ((UINT32
*) Buf
, HTONL (Ia
->Descriptor
.IaId
));
784 // Fill the value of t1 and t2 if iana, keep it 0xffffffff if no specified.
786 if (Ia
->Descriptor
.Type
== Dhcp6OptIana
) {
787 WriteUnaligned32 ((UINT32
*) Buf
, HTONL ((T1
!= 0) ? T1
: 0xffffffff));
789 WriteUnaligned32 ((UINT32
*) Buf
, HTONL ((T2
!= 0) ? T2
: 0xffffffff));
794 // Fill all the addresses belong to the Ia
796 for (Index
= 0; Index
< Ia
->IaAddressCount
; Index
++) {
797 AddrOpt
= (UINT8
*) Ia
->IaAddress
+ Index
* sizeof (EFI_DHCP6_IA_ADDRESS
);
798 Buf
= Dhcp6AppendIaAddrOption (Buf
, (EFI_DHCP6_IA_ADDRESS
*) AddrOpt
, MessageType
);
802 // Fill the value of Ia option length
804 *Len
= HTONS ((UINT16
) (Buf
- (UINT8
*) Len
- 2));
810 Append the appointed Elapsed time option to Buf, and move Buf to the end.
812 @param[in, out] Buf The pointer to the position to append.
813 @param[in] Instance The pointer to the Dhcp6 instance.
814 @param[out] Elapsed The pointer to the elapsed time value in
815 the generated packet.
817 @return Buf The position to append the next Ia option.
821 Dhcp6AppendETOption (
823 IN DHCP6_INSTANCE
*Instance
,
828 // The format of elapsed time option:
830 // 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
831 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
832 // | OPTION_ELAPSED_TIME | option-len |
833 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
835 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
839 // Fill the value of elapsed-time option type.
841 WriteUnaligned16 ((UINT16
*) Buf
, HTONS (Dhcp6OptElapsedTime
));
845 // Fill the len of elapsed-time option, which is fixed.
847 WriteUnaligned16 ((UINT16
*) Buf
, HTONS(2));
851 // Fill in elapsed time value with 0 value for now. The actual value is
852 // filled in later just before the packet is transmitted.
854 WriteUnaligned16 ((UINT16
*) Buf
, HTONS(0));
855 *Elapsed
= (UINT16
*) Buf
;
862 Set the elapsed time based on the given instance and the pointer to the
865 @param[in] Elapsed The pointer to the position to append.
866 @param[in] Instance The pointer to the Dhcp6 instance.
872 IN DHCP6_INSTANCE
*Instance
877 UINT64 ElapsedTimeValue
;
880 // Generate a time stamp of the centiseconds from 2000/1/1, assume 30day/month.
882 gRT
->GetTime (&Time
, NULL
);
883 CurrentStamp
= (UINT64
)
885 ((((((Time
.Year
- 2000) * 360 +
886 (Time
.Month
- 1)) * 30 +
887 (Time
.Day
- 1)) * 24 + Time
.Hour
) * 60 +
888 Time
.Minute
) * 60 + Time
.Second
) * 100
889 + DivU64x32(Time
.Nanosecond
, 10000000)
893 // Sentinel value of 0 means that this is the first DHCP packet that we are
894 // sending and that we need to initialize the value. First DHCP message
895 // gets 0 elapsed-time. Otherwise, calculate based on StartTime.
897 if (Instance
->StartTime
== 0) {
898 ElapsedTimeValue
= 0;
899 Instance
->StartTime
= CurrentStamp
;
901 ElapsedTimeValue
= CurrentStamp
- Instance
->StartTime
;
904 // If elapsed time cannot fit in two bytes, set it to 0xffff.
906 if (ElapsedTimeValue
> 0xffff) {
907 ElapsedTimeValue
= 0xffff;
910 WriteUnaligned16 (Elapsed
, HTONS((UINT16
) ElapsedTimeValue
));
915 Seek the address of the first byte of the option header.
917 @param[in] Buf The pointer to the buffer.
918 @param[in] SeekLen The length to seek.
919 @param[in] OptType The option type.
921 @retval NULL If it failed to seek the option.
922 @retval others The position to the option.
941 // The format of Dhcp6 option refers to Dhcp6AppendOption().
943 while (Cursor
< Buf
+ SeekLen
) {
944 OpCode
= ReadUnaligned16 ((UINT16
*) Cursor
);
945 if (OpCode
== HTONS (OptType
)) {
949 DataLen
= NTOHS (ReadUnaligned16 ((UINT16
*) (Cursor
+ 2)));
950 Cursor
+= (DataLen
+ 4);
958 Seek the address of the first byte of the Ia option header.
960 @param[in] Buf The pointer to the buffer.
961 @param[in] SeekLen The length to seek.
962 @param[in] IaDesc The pointer to the Ia descriptor.
964 @retval NULL If it failed to seek the Ia option.
965 @retval others The position to the Ia option.
972 IN EFI_DHCP6_IA_DESCRIPTOR
*IaDesc
982 // The format of IA_NA and IA_TA option refers to Dhcp6AppendIaOption().
987 while (Cursor
< Buf
+ SeekLen
) {
988 OpCode
= ReadUnaligned16 ((UINT16
*) Cursor
);
989 IaId
= ReadUnaligned32 ((UINT32
*) (Cursor
+ 4));
990 if (OpCode
== HTONS (IaDesc
->Type
) && IaId
== HTONL (IaDesc
->IaId
)) {
994 DataLen
= NTOHS (ReadUnaligned16 ((UINT16
*) (Cursor
+ 2)));
995 Cursor
+= (DataLen
+ 4);
1002 Check whether the incoming IPv6 address in IaAddr is one of the maintained
1003 addresses in the IA control blcok.
1005 @param[in] IaAddr The pointer to the IA Address to be checked.
1006 @param[in] CurrentIa The pointer to the IA in IA control block.
1008 @retval TRUE Yes, this Address is already in IA control block.
1009 @retval FALSE No, this Address is NOT in IA control block.
1013 Dhcp6AddrIsInCurrentIa (
1014 IN EFI_DHCP6_IA_ADDRESS
*IaAddr
,
1015 IN EFI_DHCP6_IA
*CurrentIa
1020 ASSERT (IaAddr
!= NULL
&& CurrentIa
!= NULL
);
1022 for (Index
= 0; Index
< CurrentIa
->IaAddressCount
; Index
++) {
1023 if (EFI_IP6_EQUAL(&IaAddr
->IpAddress
, &CurrentIa
->IaAddress
[Index
].IpAddress
)) {
1031 Parse the address option and update the address infomation.
1033 @param[in] CurrentIa The pointer to the Ia Address in control blcok.
1034 @param[in] IaInnerOpt The pointer to the buffer.
1035 @param[in] IaInnerLen The length to parse.
1036 @param[out] AddrNum The number of addresses.
1037 @param[in, out] AddrBuf The pointer to the address buffer.
1041 Dhcp6ParseAddrOption (
1042 IN EFI_DHCP6_IA
*CurrentIa
,
1043 IN UINT8
*IaInnerOpt
,
1044 IN UINT16 IaInnerLen
,
1045 OUT UINT32
*AddrNum
,
1046 IN OUT EFI_DHCP6_IA_ADDRESS
*AddrBuf
1054 EFI_DHCP6_IA_ADDRESS
*IaAddr
;
1057 // The format of the IA Address option:
1059 // 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
1060 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1061 // | OPTION_IAADDR | option-len |
1062 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1067 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1068 // | preferred-lifetime |
1069 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1070 // | valid-lifetime |
1071 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1073 // . IAaddr-options .
1075 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1081 // 1. Pass addrbuf == null, to get the addrnum over the Ia inner options.
1082 // 2. Pass addrbuf != null, to resolve the addresses over the Ia inner
1083 // options to the addrbuf.
1086 Cursor
= IaInnerOpt
;
1089 while (Cursor
< IaInnerOpt
+ IaInnerLen
) {
1091 // Refer to RFC3315 Chapter 18.1.8, we need to update lifetimes for any addresses in the IA option
1092 // that the client already has recorded in the IA, and discard the Ia address option with 0 valid time.
1094 OpCode
= ReadUnaligned16 ((UINT16
*) Cursor
);
1095 PreferredLt
= NTOHL (ReadUnaligned32 ((UINT32
*) (Cursor
+ 20)));
1096 ValidLt
= NTOHL (ReadUnaligned32 ((UINT32
*) (Cursor
+ 24)));
1097 IaAddr
= (EFI_DHCP6_IA_ADDRESS
*) (Cursor
+ 4);
1098 if (OpCode
== HTONS (Dhcp6OptIaAddr
) && ValidLt
>= PreferredLt
&&
1099 (Dhcp6AddrIsInCurrentIa(IaAddr
, CurrentIa
) || ValidLt
!=0)) {
1100 if (AddrBuf
!= NULL
) {
1101 CopyMem (AddrBuf
, IaAddr
, sizeof (EFI_DHCP6_IA_ADDRESS
));
1102 AddrBuf
->PreferredLifetime
= PreferredLt
;
1103 AddrBuf
->ValidLifetime
= ValidLt
;
1104 AddrBuf
= (EFI_DHCP6_IA_ADDRESS
*) ((UINT8
*) AddrBuf
+ sizeof (EFI_DHCP6_IA_ADDRESS
));
1108 DataLen
= NTOHS (ReadUnaligned16 ((UINT16
*) (Cursor
+ 2)));
1109 Cursor
+= (DataLen
+ 4);
1115 Create a control blcok for the Ia according to the corresponding options.
1117 @param[in] Instance The pointer to DHCP6 Instance.
1118 @param[in] IaInnerOpt The pointer to the inner options in the Ia option.
1119 @param[in] IaInnerLen The length of all the inner options in the Ia option.
1120 @param[in] T1 T1 time in the Ia option.
1121 @param[in] T2 T2 time in the Ia option.
1123 @retval EFI_NOT_FOUND No valid IA option is found.
1124 @retval EFI_SUCCESS Create an IA control block successfully.
1125 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
1126 @retval EFI_DEVICE_ERROR An unexpected error.
1131 IN DHCP6_INSTANCE
*Instance
,
1132 IN UINT8
*IaInnerOpt
,
1133 IN UINT16 IaInnerLen
,
1142 if (Instance
->IaCb
.Ia
== NULL
) {
1143 return EFI_DEVICE_ERROR
;
1147 // Calculate the number of addresses for this Ia, excluding the addresses with
1148 // the value 0 of valid lifetime.
1150 Dhcp6ParseAddrOption (Instance
->IaCb
.Ia
, IaInnerOpt
, IaInnerLen
, &AddrNum
, NULL
);
1153 return EFI_NOT_FOUND
;
1157 // Allocate for new IA.
1159 IaSize
= sizeof (EFI_DHCP6_IA
) + (AddrNum
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1160 Ia
= AllocateZeroPool (IaSize
);
1163 return EFI_OUT_OF_RESOURCES
;
1167 // Fill up this new IA fields.
1169 Ia
->State
= Instance
->IaCb
.Ia
->State
;
1170 Ia
->IaAddressCount
= AddrNum
;
1171 CopyMem (&Ia
->Descriptor
, &Instance
->Config
->IaDescriptor
, sizeof (EFI_DHCP6_IA_DESCRIPTOR
));
1172 Dhcp6ParseAddrOption (Instance
->IaCb
.Ia
, IaInnerOpt
, IaInnerLen
, &AddrNum
, Ia
->IaAddress
);
1175 // Free original IA resource.
1177 if (Instance
->IaCb
.Ia
->ReplyPacket
!= NULL
) {
1178 FreePool (Instance
->IaCb
.Ia
->ReplyPacket
);
1180 FreePool (Instance
->IaCb
.Ia
);
1183 ZeroMem (&Instance
->IaCb
, sizeof (DHCP6_IA_CB
));
1186 // Update IaCb to use new IA.
1188 Instance
->IaCb
.Ia
= Ia
;
1192 // Fill in IaCb fields. Such as T1, T2, AllExpireTime and LeaseTime.
1194 Instance
->IaCb
.T1
= T1
;
1195 Instance
->IaCb
.T2
= T2
;
1196 Dhcp6CalculateLeaseTime (&Instance
->IaCb
);
1203 Cache the current IA configuration information.
1205 @param[in] Instance The pointer to DHCP6 Instance.
1207 @retval EFI_SUCCESS Cache the current IA successfully.
1208 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
1213 IN DHCP6_INSTANCE
*Instance
1219 Ia
= Instance
->IaCb
.Ia
;
1221 if ((Instance
->CacheIa
== NULL
) && (Ia
!= NULL
)) {
1223 // Cache the current IA.
1225 IaSize
= sizeof (EFI_DHCP6_IA
) + (Ia
->IaAddressCount
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1227 Instance
->CacheIa
= AllocateZeroPool (IaSize
);
1228 if (Instance
->CacheIa
== NULL
) {
1229 return EFI_OUT_OF_RESOURCES
;
1231 CopyMem (Instance
->CacheIa
, Ia
, IaSize
);
1237 Append CacheIa to the currrent IA. Meanwhile, clear CacheIa.ValidLifetime to 0.
1239 @param[in] Instance The pointer to DHCP6 instance.
1243 Dhcp6AppendCacheIa (
1244 IN DHCP6_INSTANCE
*Instance
1252 EFI_DHCP6_IA
*NewIa
;
1253 EFI_DHCP6_IA
*CacheIa
;
1255 Ia
= Instance
->IaCb
.Ia
;
1256 CacheIa
= Instance
->CacheIa
;
1258 if ((CacheIa
!= NULL
) && (CacheIa
->IaAddressCount
!= 0)) {
1260 // There are old addresses existing. Merge with current addresses.
1262 NewIaSize
= sizeof (EFI_DHCP6_IA
) + (Ia
->IaAddressCount
+ CacheIa
->IaAddressCount
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1263 NewIa
= AllocateZeroPool (NewIaSize
);
1264 if (NewIa
== NULL
) {
1268 IaSize
= sizeof (EFI_DHCP6_IA
) + (Ia
->IaAddressCount
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1269 CopyMem (NewIa
, Ia
, IaSize
);
1272 // Clear old address.ValidLifetime
1274 for (Index
= 0; Index
< CacheIa
->IaAddressCount
; Index
++) {
1275 CacheIa
->IaAddress
[Index
].ValidLifetime
= 0;
1278 NewIa
->IaAddressCount
+= CacheIa
->IaAddressCount
;
1279 Ptr
= (UINT8
*)&NewIa
->IaAddress
[Ia
->IaAddressCount
];
1280 CopyMem (Ptr
, CacheIa
->IaAddress
, CacheIa
->IaAddressCount
* sizeof (EFI_DHCP6_IA_ADDRESS
));
1283 // Migrate to the NewIa and free previous.
1285 FreePool (Instance
->CacheIa
);
1286 FreePool (Instance
->IaCb
.Ia
);
1287 Instance
->CacheIa
= NULL
;
1288 Instance
->IaCb
.Ia
= NewIa
;
1293 Calculate the Dhcp6 get mapping timeout by adding additinal delay to the IP6 DAD transmits count.
1295 @param[in] Ip6Cfg The pointer to Ip6 config protocol.
1296 @param[out] TimeOut The time out value in 100ns units.
1298 @retval EFI_INVALID_PARAMETER Input parameters are invalid.
1299 @retval EFI_SUCCESS Calculate the time out value successfully.
1302 Dhcp6GetMappingTimeOut (
1303 IN EFI_IP6_CONFIG_PROTOCOL
*Ip6Cfg
,
1309 EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS DadXmits
;
1311 if (Ip6Cfg
== NULL
|| TimeOut
== NULL
) {
1312 return EFI_INVALID_PARAMETER
;
1315 DataSize
= sizeof (EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS
);
1316 Status
= Ip6Cfg
->GetData (
1318 Ip6ConfigDataTypeDupAddrDetectTransmits
,
1322 if (EFI_ERROR (Status
)) {
1326 *TimeOut
= TICKS_PER_SECOND
* DadXmits
.DupAddrDetectTransmits
+ DHCP6_DAD_ADDITIONAL_DELAY
;