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 (
389 EFI_DHCP6_IA_ADDRESS
*IaAddr
;
394 ASSERT (IaCb
->Ia
->IaAddressCount
> 0);
396 MinLt
= (UINT32
) (-1);
400 // Calculate minlt as min of all valid life time, and maxlt as max of all
403 for (Index
= 0; Index
< IaCb
->Ia
->IaAddressCount
; Index
++) {
404 IaAddr
= IaCb
->Ia
->IaAddress
+ Index
* sizeof (EFI_DHCP6_IA_ADDRESS
);
405 MinLt
= MIN (MinLt
, IaAddr
->ValidLifetime
);
406 MaxLt
= MAX (MinLt
, IaAddr
->ValidLifetime
);
410 // Take 50% minlt as t1, and 80% maxlt as t2 if Dhcp6 server doesn't offer
413 IaCb
->T1
= (IaCb
->T1
!= 0) ? IaCb
->T1
: (UINT32
)(MinLt
* 5 / 10);
414 IaCb
->T2
= (IaCb
->T2
!= 0) ? IaCb
->T2
: (UINT32
)(MinLt
* 8 / 10);
415 IaCb
->AllExpireTime
= MaxLt
;
421 Check whether the addresses are all included by the configured Ia.
423 @param[in] Ia The pointer to the Ia.
424 @param[in] AddressCount The number of addresses.
425 @param[in] Addresses The pointer to the addresses buffer.
427 @retval EFI_SUCCESS The addresses are all included by the configured IA.
428 @retval EFI_NOT_FOUND The addresses are not included by the configured IA.
434 IN UINT32 AddressCount
,
435 IN EFI_IPv6_ADDRESS
*Addresses
443 // Check whether the addresses are all included by the configured IA. And it
444 // will return success if address count is zero, which means all addresses.
446 for (Index1
= 0; Index1
< AddressCount
; Index1
++) {
450 for (Index2
= 0; Index2
< Ia
->IaAddressCount
; Index2
++) {
454 &Ia
->IaAddress
[Index2
],
455 sizeof (EFI_IPv6_ADDRESS
)
464 return EFI_NOT_FOUND
;
473 Deprive the addresses from current Ia, and generate another eliminated Ia.
475 @param[in] Ia The pointer to the Ia.
476 @param[in] AddressCount The number of addresses.
477 @param[in] Addresses The pointer to the addresses buffer.
479 @retval NULL If it failed to generate the deprived Ia.
480 @retval others The pointer to the deprived Ia.
484 Dhcp6DepriveAddress (
486 IN UINT32 AddressCount
,
487 IN EFI_IPv6_ADDRESS
*Addresses
490 EFI_DHCP6_IA
*IaCopy
;
496 if (AddressCount
== 0) {
498 // It means release all Ia addresses if address count is zero.
500 AddressCount
= Ia
->IaAddressCount
;
503 ASSERT (AddressCount
!= 0);
505 IaCopySize
= sizeof (EFI_DHCP6_IA
) + (AddressCount
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
506 IaCopy
= AllocateZeroPool (IaCopySize
);
508 if (IaCopy
== NULL
) {
512 if (AddressCount
== Ia
->IaAddressCount
) {
514 // If release all Ia addresses, just copy the configured Ia and then set
515 // its address count as zero.
516 // We may decline/release part of addresses at the begining. So it's a
517 // forwarding step to update address infor for decline/release, while the
518 // other infor such as Ia state will be updated when receiving reply.
520 CopyMem (IaCopy
, Ia
, IaCopySize
);
521 Ia
->IaAddressCount
= 0;
525 CopyMem (IaCopy
, Ia
, sizeof (EFI_DHCP6_IA
));
528 // Move the addresses from the Ia of instance to the deprived Ia.
530 for (Index1
= 0; Index1
< AddressCount
; Index1
++) {
534 for (Index2
= 0; Index2
< Ia
->IaAddressCount
; Index2
++) {
538 &Ia
->IaAddress
[Index2
],
539 sizeof (EFI_IPv6_ADDRESS
)
542 // Copy the deprived address to the copy of Ia
545 &IaCopy
->IaAddress
[Index1
],
546 &Ia
->IaAddress
[Index2
],
547 sizeof (EFI_DHCP6_IA_ADDRESS
)
550 // Delete the deprived address from the instance Ia
552 if (Index2
+ 1 < Ia
->IaAddressCount
) {
554 &Ia
->IaAddress
[Index2
],
555 &Ia
->IaAddress
[Index2
+ 1],
556 (Ia
->IaAddressCount
- Index2
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
)
563 ASSERT (Found
== TRUE
);
566 Ia
->IaAddressCount
-= AddressCount
;
567 IaCopy
->IaAddressCount
= AddressCount
;
574 The dummy ext buffer free callback routine.
576 @param[in] Arg The pointer to the parameter.
589 The callback routine once message transmitted.
591 @param[in] Wrap The pointer to the received net buffer.
592 @param[in] EndPoint The pointer to the udp end point.
593 @param[in] IoStatus The return status from udp io.
594 @param[in] Context The opaque parameter to the function.
601 IN UDP_END_POINT
*EndPoint
,
602 IN EFI_STATUS IoStatus
,
611 Append the option to Buf, and move Buf to the end.
613 @param[in, out] Buf The pointer to the buffer.
614 @param[in] OptType The option type.
615 @param[in] OptLen The length of option contents.
616 @param[in] Data The pointer to the option content.
618 @return Buf The position to append the next option.
630 // The format of Dhcp6 option:
632 // 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
633 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
634 // | option-code | option-len (option data) |
635 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
637 // | (option-len octets) |
638 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
641 ASSERT (OptLen
!= 0);
643 WriteUnaligned16 ((UINT16
*) Buf
, OptType
);
645 WriteUnaligned16 ((UINT16
*) Buf
, OptLen
);
647 CopyMem (Buf
, Data
, NTOHS (OptLen
));
648 Buf
+= NTOHS (OptLen
);
654 Append the appointed IA Address option to Buf, and move Buf to the end.
656 @param[in, out] Buf The pointer to the position to append.
657 @param[in] IaAddr The pointer to the IA Address.
658 @param[in] MessageType Message type of DHCP6 package.
660 @return Buf The position to append the next option.
664 Dhcp6AppendIaAddrOption (
666 IN EFI_DHCP6_IA_ADDRESS
*IaAddr
,
667 IN UINT32 MessageType
671 // The format of the IA Address option is:
674 // 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
675 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
676 // | OPTION_IAADDR | option-len |
677 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
682 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
683 // | preferred-lifetime |
684 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
685 // | valid-lifetime |
686 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
688 // . IAaddr-options .
690 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
693 // Fill the value of Ia Address option type
695 WriteUnaligned16 ((UINT16
*) Buf
, HTONS (Dhcp6OptIaAddr
));
698 WriteUnaligned16 ((UINT16
*) Buf
, HTONS (sizeof (EFI_DHCP6_IA_ADDRESS
)));
701 CopyMem (Buf
, &IaAddr
->IpAddress
, sizeof(EFI_IPv6_ADDRESS
));
702 Buf
+= sizeof(EFI_IPv6_ADDRESS
);
705 // Fill the value of preferred-lifetime and valid-lifetime.
706 // According to RFC3315 Chapter 18.1.2, the preferred-lifetime and valid-lifetime fields
707 // should set to 0 when initiate a Confirm message.
709 if (MessageType
!= Dhcp6MsgConfirm
) {
710 WriteUnaligned32 ((UINT32
*) Buf
, HTONL (IaAddr
->PreferredLifetime
));
714 if (MessageType
!= Dhcp6MsgConfirm
) {
715 WriteUnaligned32 ((UINT32
*) Buf
, HTONL (IaAddr
->ValidLifetime
));
724 Append the appointed Ia option to Buf, and move Buf to the end.
726 @param[in, out] Buf The pointer to the position to append.
727 @param[in] Ia The pointer to the Ia.
728 @param[in] T1 The time of T1.
729 @param[in] T2 The time of T2.
730 @param[in] MessageType Message type of DHCP6 package.
732 @return Buf The position to append the next Ia option.
736 Dhcp6AppendIaOption (
741 IN UINT32 MessageType
749 // The format of IA_NA and IA_TA option:
751 // 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
752 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
753 // | OPTION_IA_NA | option-len |
754 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
755 // | IAID (4 octets) |
756 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
757 // | T1 (only for IA_NA) |
758 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
759 // | T2 (only for IA_NA) |
760 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
762 // . IA_NA-options/IA_TA-options .
764 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
768 // Fill the value of Ia option type
770 WriteUnaligned16 ((UINT16
*) Buf
, HTONS (Ia
->Descriptor
.Type
));
774 // Fill the len of Ia option later, keep the pointer first
776 Len
= (UINT16
*) Buf
;
780 // Fill the value of iaid
782 WriteUnaligned32 ((UINT32
*) Buf
, HTONL (Ia
->Descriptor
.IaId
));
786 // Fill the value of t1 and t2 if iana, keep it 0xffffffff if no specified.
788 if (Ia
->Descriptor
.Type
== Dhcp6OptIana
) {
789 WriteUnaligned32 ((UINT32
*) Buf
, HTONL ((T1
!= 0) ? T1
: 0xffffffff));
791 WriteUnaligned32 ((UINT32
*) Buf
, HTONL ((T2
!= 0) ? T2
: 0xffffffff));
796 // Fill all the addresses belong to the Ia
798 for (Index
= 0; Index
< Ia
->IaAddressCount
; Index
++) {
799 AddrOpt
= (UINT8
*) Ia
->IaAddress
+ Index
* sizeof (EFI_DHCP6_IA_ADDRESS
);
800 Buf
= Dhcp6AppendIaAddrOption (Buf
, (EFI_DHCP6_IA_ADDRESS
*) AddrOpt
, MessageType
);
804 // Fill the value of Ia option length
806 *Len
= HTONS ((UINT16
) (Buf
- (UINT8
*) Len
- 2));
812 Append the appointed Elapsed time option to Buf, and move Buf to the end.
814 @param[in, out] Buf The pointer to the position to append.
815 @param[in] Instance The pointer to the Dhcp6 instance.
816 @param[out] Elapsed The pointer to the elapsed time value in
817 the generated packet.
819 @return Buf The position to append the next Ia option.
823 Dhcp6AppendETOption (
825 IN DHCP6_INSTANCE
*Instance
,
830 // The format of elapsed time option:
832 // 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
833 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
834 // | OPTION_ELAPSED_TIME | option-len |
835 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
837 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
841 // Fill the value of elapsed-time option type.
843 WriteUnaligned16 ((UINT16
*) Buf
, HTONS (Dhcp6OptElapsedTime
));
847 // Fill the len of elapsed-time option, which is fixed.
849 WriteUnaligned16 ((UINT16
*) Buf
, HTONS(2));
853 // Fill in elapsed time value with 0 value for now. The actual value is
854 // filled in later just before the packet is transmitted.
856 WriteUnaligned16 ((UINT16
*) Buf
, HTONS(0));
857 *Elapsed
= (UINT16
*) Buf
;
864 Set the elapsed time based on the given instance and the pointer to the
867 @param[in] Elapsed The pointer to the position to append.
868 @param[in] Instance The pointer to the Dhcp6 instance.
874 IN DHCP6_INSTANCE
*Instance
879 UINT64 ElapsedTimeValue
;
882 // Generate a time stamp of the centiseconds from 2000/1/1, assume 30day/month.
884 gRT
->GetTime (&Time
, NULL
);
885 CurrentStamp
= (UINT64
)
887 ((((((Time
.Year
- 2000) * 360 +
888 (Time
.Month
- 1)) * 30 +
889 (Time
.Day
- 1)) * 24 + Time
.Hour
) * 60 +
890 Time
.Minute
) * 60 + Time
.Second
) * 100
891 + DivU64x32(Time
.Nanosecond
, 10000000)
895 // Sentinel value of 0 means that this is the first DHCP packet that we are
896 // sending and that we need to initialize the value. First DHCP message
897 // gets 0 elapsed-time. Otherwise, calculate based on StartTime.
899 if (Instance
->StartTime
== 0) {
900 ElapsedTimeValue
= 0;
901 Instance
->StartTime
= CurrentStamp
;
903 ElapsedTimeValue
= CurrentStamp
- Instance
->StartTime
;
906 // If elapsed time cannot fit in two bytes, set it to 0xffff.
908 if (ElapsedTimeValue
> 0xffff) {
909 ElapsedTimeValue
= 0xffff;
912 WriteUnaligned16 (Elapsed
, HTONS((UINT16
) ElapsedTimeValue
));
917 Seek the address of the first byte of the option header.
919 @param[in] Buf The pointer to the buffer.
920 @param[in] SeekLen The length to seek.
921 @param[in] OptType The option type.
923 @retval NULL If it failed to seek the option.
924 @retval others The position to the option.
943 // The format of Dhcp6 option refers to Dhcp6AppendOption().
945 while (Cursor
< Buf
+ SeekLen
) {
946 OpCode
= ReadUnaligned16 ((UINT16
*) Cursor
);
947 if (OpCode
== HTONS (OptType
)) {
951 DataLen
= NTOHS (ReadUnaligned16 ((UINT16
*) (Cursor
+ 2)));
952 Cursor
+= (DataLen
+ 4);
960 Seek the address of the first byte of the Ia option header.
962 @param[in] Buf The pointer to the buffer.
963 @param[in] SeekLen The length to seek.
964 @param[in] IaDesc The pointer to the Ia descriptor.
966 @retval NULL If it failed to seek the Ia option.
967 @retval others The position to the Ia option.
974 IN EFI_DHCP6_IA_DESCRIPTOR
*IaDesc
984 // The format of IA_NA and IA_TA option refers to Dhcp6AppendIaOption().
989 while (Cursor
< Buf
+ SeekLen
) {
990 OpCode
= ReadUnaligned16 ((UINT16
*) Cursor
);
991 IaId
= ReadUnaligned32 ((UINT32
*) (Cursor
+ 4));
992 if (OpCode
== HTONS (IaDesc
->Type
) && IaId
== HTONL (IaDesc
->IaId
)) {
996 DataLen
= NTOHS (ReadUnaligned16 ((UINT16
*) (Cursor
+ 2)));
997 Cursor
+= (DataLen
+ 4);
1004 Check whether the incoming IPv6 address in IaAddr is one of the maintained
1005 addresses in the IA control blcok.
1007 @param[in] IaAddr The pointer to the IA Address to be checked.
1008 @param[in] CurrentIa The pointer to the IA in IA control block.
1010 @retval TRUE Yes, this Address is already in IA control block.
1011 @retval FALSE No, this Address is NOT in IA control block.
1015 Dhcp6AddrIsInCurrentIa (
1016 IN EFI_DHCP6_IA_ADDRESS
*IaAddr
,
1017 IN EFI_DHCP6_IA
*CurrentIa
1022 ASSERT (IaAddr
!= NULL
&& CurrentIa
!= NULL
);
1024 for (Index
= 0; Index
< CurrentIa
->IaAddressCount
; Index
++) {
1025 if (EFI_IP6_EQUAL(&IaAddr
->IpAddress
, &CurrentIa
->IaAddress
[Index
].IpAddress
)) {
1033 Parse the address option and update the address infomation.
1035 @param[in] CurrentIa The pointer to the Ia Address in control blcok.
1036 @param[in] IaInnerOpt The pointer to the buffer.
1037 @param[in] IaInnerLen The length to parse.
1038 @param[out] AddrNum The number of addresses.
1039 @param[in, out] AddrBuf The pointer to the address buffer.
1043 Dhcp6ParseAddrOption (
1044 IN EFI_DHCP6_IA
*CurrentIa
,
1045 IN UINT8
*IaInnerOpt
,
1046 IN UINT16 IaInnerLen
,
1047 OUT UINT32
*AddrNum
,
1048 IN OUT EFI_DHCP6_IA_ADDRESS
*AddrBuf
1056 EFI_DHCP6_IA_ADDRESS
*IaAddr
;
1059 // The format of the IA Address option:
1061 // 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
1062 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1063 // | OPTION_IAADDR | option-len |
1064 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1069 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1070 // | preferred-lifetime |
1071 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1072 // | valid-lifetime |
1073 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1075 // . IAaddr-options .
1077 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1083 // 1. Pass addrbuf == null, to get the addrnum over the Ia inner options.
1084 // 2. Pass addrbuf != null, to resolve the addresses over the Ia inner
1085 // options to the addrbuf.
1088 Cursor
= IaInnerOpt
;
1091 while (Cursor
< IaInnerOpt
+ IaInnerLen
) {
1093 // Refer to RFC3315 Chapter 18.1.8, we need to update lifetimes for any addresses in the IA option
1094 // that the client already has recorded in the IA, and discard the Ia address option with 0 valid time.
1096 OpCode
= ReadUnaligned16 ((UINT16
*) Cursor
);
1097 PreferredLt
= NTOHL (ReadUnaligned32 ((UINT32
*) (Cursor
+ 20)));
1098 ValidLt
= NTOHL (ReadUnaligned32 ((UINT32
*) (Cursor
+ 24)));
1099 IaAddr
= (EFI_DHCP6_IA_ADDRESS
*) (Cursor
+ 4);
1100 if (OpCode
== HTONS (Dhcp6OptIaAddr
) && ValidLt
>= PreferredLt
&&
1101 (Dhcp6AddrIsInCurrentIa(IaAddr
, CurrentIa
) || ValidLt
!=0)) {
1102 if (AddrBuf
!= NULL
) {
1103 CopyMem (AddrBuf
, IaAddr
, sizeof (EFI_DHCP6_IA_ADDRESS
));
1104 AddrBuf
->PreferredLifetime
= PreferredLt
;
1105 AddrBuf
->ValidLifetime
= ValidLt
;
1106 AddrBuf
= (EFI_DHCP6_IA_ADDRESS
*) ((UINT8
*) AddrBuf
+ sizeof (EFI_DHCP6_IA_ADDRESS
));
1110 DataLen
= NTOHS (ReadUnaligned16 ((UINT16
*) (Cursor
+ 2)));
1111 Cursor
+= (DataLen
+ 4);
1117 Create a control blcok for the Ia according to the corresponding options.
1119 @param[in] Instance The pointer to DHCP6 Instance.
1120 @param[in] IaInnerOpt The pointer to the inner options in the Ia option.
1121 @param[in] IaInnerLen The length of all the inner options in the Ia option.
1122 @param[in] T1 T1 time in the Ia option.
1123 @param[in] T2 T2 time in the Ia option.
1125 @retval EFI_NOT_FOUND No valid IA option is found.
1126 @retval EFI_SUCCESS Create an IA control block successfully.
1127 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
1128 @retval EFI_DEVICE_ERROR An unexpected error.
1133 IN DHCP6_INSTANCE
*Instance
,
1134 IN UINT8
*IaInnerOpt
,
1135 IN UINT16 IaInnerLen
,
1144 if (Instance
->IaCb
.Ia
== NULL
) {
1145 return EFI_DEVICE_ERROR
;
1149 // Calculate the number of addresses for this Ia, excluding the addresses with
1150 // the value 0 of valid lifetime.
1152 Dhcp6ParseAddrOption (Instance
->IaCb
.Ia
, IaInnerOpt
, IaInnerLen
, &AddrNum
, NULL
);
1155 return EFI_NOT_FOUND
;
1159 // Allocate for new IA.
1161 IaSize
= sizeof (EFI_DHCP6_IA
) + (AddrNum
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1162 Ia
= AllocateZeroPool (IaSize
);
1165 return EFI_OUT_OF_RESOURCES
;
1169 // Fill up this new IA fields.
1171 Ia
->State
= Instance
->IaCb
.Ia
->State
;
1172 Ia
->IaAddressCount
= AddrNum
;
1173 CopyMem (&Ia
->Descriptor
, &Instance
->Config
->IaDescriptor
, sizeof (EFI_DHCP6_IA_DESCRIPTOR
));
1174 Dhcp6ParseAddrOption (Instance
->IaCb
.Ia
, IaInnerOpt
, IaInnerLen
, &AddrNum
, Ia
->IaAddress
);
1177 // Free original IA resource.
1179 if (Instance
->IaCb
.Ia
->ReplyPacket
!= NULL
) {
1180 FreePool (Instance
->IaCb
.Ia
->ReplyPacket
);
1182 FreePool (Instance
->IaCb
.Ia
);
1185 ZeroMem (&Instance
->IaCb
, sizeof (DHCP6_IA_CB
));
1188 // Update IaCb to use new IA.
1190 Instance
->IaCb
.Ia
= Ia
;
1194 // Fill in IaCb fields. Such as T1, T2, AllExpireTime and LeaseTime.
1196 Instance
->IaCb
.T1
= T1
;
1197 Instance
->IaCb
.T2
= T2
;
1198 Dhcp6CalculateLeaseTime (&Instance
->IaCb
);
1205 Cache the current IA configuration information.
1207 @param[in] Instance The pointer to DHCP6 Instance.
1209 @retval EFI_SUCCESS Cache the current IA successfully.
1210 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
1215 IN DHCP6_INSTANCE
*Instance
1221 Ia
= Instance
->IaCb
.Ia
;
1223 if ((Instance
->CacheIa
== NULL
) && (Ia
!= NULL
)) {
1225 // Cache the current IA.
1227 IaSize
= sizeof (EFI_DHCP6_IA
) + (Ia
->IaAddressCount
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1229 Instance
->CacheIa
= AllocateZeroPool (IaSize
);
1230 if (Instance
->CacheIa
== NULL
) {
1231 return EFI_OUT_OF_RESOURCES
;
1233 CopyMem (Instance
->CacheIa
, Ia
, IaSize
);
1239 Append CacheIa to the currrent IA. Meanwhile, clear CacheIa.ValidLifetime to 0.
1241 @param[in] Instance The pointer to DHCP6 instance.
1245 Dhcp6AppendCacheIa (
1246 IN DHCP6_INSTANCE
*Instance
1254 EFI_DHCP6_IA
*NewIa
;
1255 EFI_DHCP6_IA
*CacheIa
;
1257 Ia
= Instance
->IaCb
.Ia
;
1258 CacheIa
= Instance
->CacheIa
;
1260 if ((CacheIa
!= NULL
) && (CacheIa
->IaAddressCount
!= 0)) {
1262 // There are old addresses existing. Merge with current addresses.
1264 NewIaSize
= sizeof (EFI_DHCP6_IA
) + (Ia
->IaAddressCount
+ CacheIa
->IaAddressCount
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1265 NewIa
= AllocateZeroPool (NewIaSize
);
1266 if (NewIa
== NULL
) {
1270 IaSize
= sizeof (EFI_DHCP6_IA
) + (Ia
->IaAddressCount
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1271 CopyMem (NewIa
, Ia
, IaSize
);
1274 // Clear old address.ValidLifetime
1276 for (Index
= 0; Index
< CacheIa
->IaAddressCount
; Index
++) {
1277 CacheIa
->IaAddress
[Index
].ValidLifetime
= 0;
1280 NewIa
->IaAddressCount
+= CacheIa
->IaAddressCount
;
1281 Ptr
= (UINT8
*)&NewIa
->IaAddress
[Ia
->IaAddressCount
];
1282 CopyMem (Ptr
, CacheIa
->IaAddress
, CacheIa
->IaAddressCount
* sizeof (EFI_DHCP6_IA_ADDRESS
));
1285 // Migrate to the NewIa and free previous.
1287 FreePool (Instance
->CacheIa
);
1288 FreePool (Instance
->IaCb
.Ia
);
1289 Instance
->CacheIa
= NULL
;
1290 Instance
->IaCb
.Ia
= NewIa
;
1295 Calculate the Dhcp6 get mapping timeout by adding additinal delay to the IP6 DAD transmits count.
1297 @param[in] Ip6Cfg The pointer to Ip6 config protocol.
1298 @param[out] TimeOut The time out value in 100ns units.
1300 @retval EFI_INVALID_PARAMETER Input parameters are invalid.
1301 @retval EFI_SUCCESS Calculate the time out value successfully.
1304 Dhcp6GetMappingTimeOut (
1305 IN EFI_IP6_CONFIG_PROTOCOL
*Ip6Cfg
,
1311 EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS DadXmits
;
1313 if (Ip6Cfg
== NULL
|| TimeOut
== NULL
) {
1314 return EFI_INVALID_PARAMETER
;
1317 DataSize
= sizeof (EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS
);
1318 Status
= Ip6Cfg
->GetData (
1320 Ip6ConfigDataTypeDupAddrDetectTransmits
,
1324 if (EFI_ERROR (Status
)) {
1328 *TimeOut
= TICKS_PER_SECOND
* DadXmits
.DupAddrDetectTransmits
+ DHCP6_DAD_ADDITIONAL_DELAY
;