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
);
651 Append the appointed IA Address option to Buf, and move Buf to the end.
653 @param[in, out] Buf The pointer to the position to append.
654 @param[in] IaAddr The pointer to the IA Address.
655 @param[in] MessageType Message type of DHCP6 package.
657 @return Buf The position to append the next option.
661 Dhcp6AppendIaAddrOption (
663 IN EFI_DHCP6_IA_ADDRESS
*IaAddr
,
664 IN UINT32 MessageType
668 // The format of the IA Address option is:
671 // 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
672 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
673 // | OPTION_IAADDR | option-len |
674 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
679 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
680 // | preferred-lifetime |
681 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
682 // | valid-lifetime |
683 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
685 // . IAaddr-options .
687 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
690 // Fill the value of Ia Address option type
692 WriteUnaligned16 ((UINT16
*) Buf
, HTONS (Dhcp6OptIaAddr
));
695 WriteUnaligned16 ((UINT16
*) Buf
, HTONS (sizeof (EFI_DHCP6_IA_ADDRESS
)));
698 CopyMem (Buf
, &IaAddr
->IpAddress
, sizeof(EFI_IPv6_ADDRESS
));
699 Buf
+= sizeof(EFI_IPv6_ADDRESS
);
702 // Fill the value of preferred-lifetime and valid-lifetime.
703 // According to RFC3315 Chapter 18.1.2, the preferred-lifetime and valid-lifetime fields
704 // should set to 0 when initiate a Confirm message.
706 if (MessageType
!= Dhcp6MsgConfirm
) {
707 WriteUnaligned32 ((UINT32
*) Buf
, HTONL (IaAddr
->PreferredLifetime
));
711 if (MessageType
!= Dhcp6MsgConfirm
) {
712 WriteUnaligned32 ((UINT32
*) Buf
, HTONL (IaAddr
->ValidLifetime
));
721 Append the appointed Ia option to Buf, and move Buf to the end.
723 @param[in, out] Buf The pointer to the position to append.
724 @param[in] Ia The pointer to the Ia.
725 @param[in] T1 The time of T1.
726 @param[in] T2 The time of T2.
727 @param[in] MessageType Message type of DHCP6 package.
729 @return Buf The position to append the next Ia option.
733 Dhcp6AppendIaOption (
738 IN UINT32 MessageType
746 // The format of IA_NA and IA_TA option:
748 // 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
749 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
750 // | OPTION_IA_NA | option-len |
751 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
752 // | IAID (4 octets) |
753 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
754 // | T1 (only for IA_NA) |
755 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
756 // | T2 (only for IA_NA) |
757 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
759 // . IA_NA-options/IA_TA-options .
761 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
765 // Fill the value of Ia option type
767 WriteUnaligned16 ((UINT16
*) Buf
, HTONS (Ia
->Descriptor
.Type
));
771 // Fill the len of Ia option later, keep the pointer first
773 Len
= (UINT16
*) Buf
;
777 // Fill the value of iaid
779 WriteUnaligned32 ((UINT32
*) Buf
, HTONL (Ia
->Descriptor
.IaId
));
783 // Fill the value of t1 and t2 if iana, keep it 0xffffffff if no specified.
785 if (Ia
->Descriptor
.Type
== Dhcp6OptIana
) {
786 WriteUnaligned32 ((UINT32
*) Buf
, HTONL ((T1
!= 0) ? T1
: 0xffffffff));
788 WriteUnaligned32 ((UINT32
*) Buf
, HTONL ((T2
!= 0) ? T2
: 0xffffffff));
793 // Fill all the addresses belong to the Ia
795 for (Index
= 0; Index
< Ia
->IaAddressCount
; Index
++) {
796 AddrOpt
= (UINT8
*) Ia
->IaAddress
+ Index
* sizeof (EFI_DHCP6_IA_ADDRESS
);
797 Buf
= Dhcp6AppendIaAddrOption (Buf
, (EFI_DHCP6_IA_ADDRESS
*) AddrOpt
, MessageType
);
801 // Fill the value of Ia option length
803 *Len
= HTONS ((UINT16
) (Buf
- (UINT8
*) Len
- 2));
809 Append the appointed Elapsed time option to Buf, and move Buf to the end.
811 @param[in, out] Buf The pointer to the position to append.
812 @param[in] Instance The pointer to the Dhcp6 instance.
813 @param[out] Elapsed The pointer to the elapsed time value in
814 the generated packet.
816 @return Buf The position to append the next Ia option.
820 Dhcp6AppendETOption (
822 IN DHCP6_INSTANCE
*Instance
,
827 // The format of elapsed time option:
829 // 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
830 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
831 // | OPTION_ELAPSED_TIME | option-len |
832 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
834 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
838 // Fill the value of elapsed-time option type.
840 WriteUnaligned16 ((UINT16
*) Buf
, HTONS (Dhcp6OptElapsedTime
));
844 // Fill the len of elapsed-time option, which is fixed.
846 WriteUnaligned16 ((UINT16
*) Buf
, HTONS(2));
850 // Fill in elapsed time value with 0 value for now. The actual value is
851 // filled in later just before the packet is transmitted.
853 WriteUnaligned16 ((UINT16
*) Buf
, HTONS(0));
854 *Elapsed
= (UINT16
*) Buf
;
861 Set the elapsed time based on the given instance and the pointer to the
864 @param[in] Elapsed The pointer to the position to append.
865 @param[in] Instance The pointer to the Dhcp6 instance.
871 IN DHCP6_INSTANCE
*Instance
876 UINT64 ElapsedTimeValue
;
879 // Generate a time stamp of the centiseconds from 2000/1/1, assume 30day/month.
881 gRT
->GetTime (&Time
, NULL
);
882 CurrentStamp
= (UINT64
)
884 ((((((Time
.Year
- 2000) * 360 +
885 (Time
.Month
- 1)) * 30 +
886 (Time
.Day
- 1)) * 24 + Time
.Hour
) * 60 +
887 Time
.Minute
) * 60 + Time
.Second
) * 100
888 + DivU64x32(Time
.Nanosecond
, 10000000)
892 // Sentinel value of 0 means that this is the first DHCP packet that we are
893 // sending and that we need to initialize the value. First DHCP message
894 // gets 0 elapsed-time. Otherwise, calculate based on StartTime.
896 if (Instance
->StartTime
== 0) {
897 ElapsedTimeValue
= 0;
898 Instance
->StartTime
= CurrentStamp
;
900 ElapsedTimeValue
= CurrentStamp
- Instance
->StartTime
;
903 // If elapsed time cannot fit in two bytes, set it to 0xffff.
905 if (ElapsedTimeValue
> 0xffff) {
906 ElapsedTimeValue
= 0xffff;
909 WriteUnaligned16 (Elapsed
, HTONS((UINT16
) ElapsedTimeValue
));
914 Seek the address of the first byte of the option header.
916 @param[in] Buf The pointer to the buffer.
917 @param[in] SeekLen The length to seek.
918 @param[in] OptType The option type.
920 @retval NULL If it failed to seek the option.
921 @retval others The position to the option.
940 // The format of Dhcp6 option refers to Dhcp6AppendOption().
942 while (Cursor
< Buf
+ SeekLen
) {
943 OpCode
= ReadUnaligned16 ((UINT16
*) Cursor
);
944 if (OpCode
== HTONS (OptType
)) {
948 DataLen
= NTOHS (ReadUnaligned16 ((UINT16
*) (Cursor
+ 2)));
949 Cursor
+= (DataLen
+ 4);
957 Seek the address of the first byte of the Ia option header.
959 @param[in] Buf The pointer to the buffer.
960 @param[in] SeekLen The length to seek.
961 @param[in] IaDesc The pointer to the Ia descriptor.
963 @retval NULL If it failed to seek the Ia option.
964 @retval others The position to the Ia option.
971 IN EFI_DHCP6_IA_DESCRIPTOR
*IaDesc
981 // The format of IA_NA and IA_TA option refers to Dhcp6AppendIaOption().
986 while (Cursor
< Buf
+ SeekLen
) {
987 OpCode
= ReadUnaligned16 ((UINT16
*) Cursor
);
988 IaId
= ReadUnaligned32 ((UINT32
*) (Cursor
+ 4));
989 if (OpCode
== HTONS (IaDesc
->Type
) && IaId
== HTONL (IaDesc
->IaId
)) {
993 DataLen
= NTOHS (ReadUnaligned16 ((UINT16
*) (Cursor
+ 2)));
994 Cursor
+= (DataLen
+ 4);
1001 Check whether the incoming IPv6 address in IaAddr is one of the maintained
1002 addresses in the IA control blcok.
1004 @param[in] IaAddr The pointer to the IA Address to be checked.
1005 @param[in] CurrentIa The pointer to the IA in IA control block.
1007 @retval TRUE Yes, this Address is already in IA control block.
1008 @retval FALSE No, this Address is NOT in IA control block.
1012 Dhcp6AddrIsInCurrentIa (
1013 IN EFI_DHCP6_IA_ADDRESS
*IaAddr
,
1014 IN EFI_DHCP6_IA
*CurrentIa
1019 ASSERT (IaAddr
!= NULL
&& CurrentIa
!= NULL
);
1021 for (Index
= 0; Index
< CurrentIa
->IaAddressCount
; Index
++) {
1022 if (EFI_IP6_EQUAL(&IaAddr
->IpAddress
, &CurrentIa
->IaAddress
[Index
].IpAddress
)) {
1030 Parse the address option and update the address infomation.
1032 @param[in] CurrentIa The pointer to the Ia Address in control blcok.
1033 @param[in] IaInnerOpt The pointer to the buffer.
1034 @param[in] IaInnerLen The length to parse.
1035 @param[out] AddrNum The number of addresses.
1036 @param[in, out] AddrBuf The pointer to the address buffer.
1040 Dhcp6ParseAddrOption (
1041 IN EFI_DHCP6_IA
*CurrentIa
,
1042 IN UINT8
*IaInnerOpt
,
1043 IN UINT16 IaInnerLen
,
1044 OUT UINT32
*AddrNum
,
1045 IN OUT EFI_DHCP6_IA_ADDRESS
*AddrBuf
1053 EFI_DHCP6_IA_ADDRESS
*IaAddr
;
1056 // The format of the IA Address option:
1058 // 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
1059 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1060 // | OPTION_IAADDR | option-len |
1061 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1066 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1067 // | preferred-lifetime |
1068 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1069 // | valid-lifetime |
1070 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1072 // . IAaddr-options .
1074 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1080 // 1. Pass addrbuf == null, to get the addrnum over the Ia inner options.
1081 // 2. Pass addrbuf != null, to resolve the addresses over the Ia inner
1082 // options to the addrbuf.
1085 Cursor
= IaInnerOpt
;
1088 while (Cursor
< IaInnerOpt
+ IaInnerLen
) {
1090 // Refer to RFC3315 Chapter 18.1.8, we need to update lifetimes for any addresses in the IA option
1091 // that the client already has recorded in the IA, and discard the Ia address option with 0 valid time.
1093 OpCode
= ReadUnaligned16 ((UINT16
*) Cursor
);
1094 PreferredLt
= NTOHL (ReadUnaligned32 ((UINT32
*) (Cursor
+ 20)));
1095 ValidLt
= NTOHL (ReadUnaligned32 ((UINT32
*) (Cursor
+ 24)));
1096 IaAddr
= (EFI_DHCP6_IA_ADDRESS
*) (Cursor
+ 4);
1097 if (OpCode
== HTONS (Dhcp6OptIaAddr
) && ValidLt
>= PreferredLt
&&
1098 (Dhcp6AddrIsInCurrentIa(IaAddr
, CurrentIa
) || ValidLt
!=0)) {
1099 if (AddrBuf
!= NULL
) {
1100 CopyMem (AddrBuf
, IaAddr
, sizeof (EFI_DHCP6_IA_ADDRESS
));
1101 AddrBuf
->PreferredLifetime
= PreferredLt
;
1102 AddrBuf
->ValidLifetime
= ValidLt
;
1103 AddrBuf
= (EFI_DHCP6_IA_ADDRESS
*) ((UINT8
*) AddrBuf
+ sizeof (EFI_DHCP6_IA_ADDRESS
));
1107 DataLen
= NTOHS (ReadUnaligned16 ((UINT16
*) (Cursor
+ 2)));
1108 Cursor
+= (DataLen
+ 4);
1114 Create a control blcok for the Ia according to the corresponding options.
1116 @param[in] Instance The pointer to DHCP6 Instance.
1117 @param[in] IaInnerOpt The pointer to the inner options in the Ia option.
1118 @param[in] IaInnerLen The length of all the inner options in the Ia option.
1119 @param[in] T1 T1 time in the Ia option.
1120 @param[in] T2 T2 time in the Ia option.
1122 @retval EFI_NOT_FOUND No valid IA option is found.
1123 @retval EFI_SUCCESS Create an IA control block successfully.
1124 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
1125 @retval EFI_DEVICE_ERROR An unexpected error.
1130 IN DHCP6_INSTANCE
*Instance
,
1131 IN UINT8
*IaInnerOpt
,
1132 IN UINT16 IaInnerLen
,
1141 if (Instance
->IaCb
.Ia
== NULL
) {
1142 return EFI_DEVICE_ERROR
;
1146 // Calculate the number of addresses for this Ia, excluding the addresses with
1147 // the value 0 of valid lifetime.
1149 Dhcp6ParseAddrOption (Instance
->IaCb
.Ia
, IaInnerOpt
, IaInnerLen
, &AddrNum
, NULL
);
1152 return EFI_NOT_FOUND
;
1156 // Allocate for new IA.
1158 IaSize
= sizeof (EFI_DHCP6_IA
) + (AddrNum
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1159 Ia
= AllocateZeroPool (IaSize
);
1162 return EFI_OUT_OF_RESOURCES
;
1166 // Fill up this new IA fields.
1168 Ia
->State
= Instance
->IaCb
.Ia
->State
;
1169 Ia
->IaAddressCount
= AddrNum
;
1170 CopyMem (&Ia
->Descriptor
, &Instance
->Config
->IaDescriptor
, sizeof (EFI_DHCP6_IA_DESCRIPTOR
));
1171 Dhcp6ParseAddrOption (Instance
->IaCb
.Ia
, IaInnerOpt
, IaInnerLen
, &AddrNum
, Ia
->IaAddress
);
1174 // Free original IA resource.
1176 if (Instance
->IaCb
.Ia
->ReplyPacket
!= NULL
) {
1177 FreePool (Instance
->IaCb
.Ia
->ReplyPacket
);
1179 FreePool (Instance
->IaCb
.Ia
);
1182 ZeroMem (&Instance
->IaCb
, sizeof (DHCP6_IA_CB
));
1185 // Update IaCb to use new IA.
1187 Instance
->IaCb
.Ia
= Ia
;
1191 // Fill in IaCb fields. Such as T1, T2, AllExpireTime and LeaseTime.
1193 Instance
->IaCb
.T1
= T1
;
1194 Instance
->IaCb
.T2
= T2
;
1195 Dhcp6CalculateLeaseTime (&Instance
->IaCb
);
1202 Cache the current IA configuration information.
1204 @param[in] Instance The pointer to DHCP6 Instance.
1206 @retval EFI_SUCCESS Cache the current IA successfully.
1207 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
1212 IN DHCP6_INSTANCE
*Instance
1218 Ia
= Instance
->IaCb
.Ia
;
1220 if ((Instance
->CacheIa
== NULL
) && (Ia
!= NULL
)) {
1222 // Cache the current IA.
1224 IaSize
= sizeof (EFI_DHCP6_IA
) + (Ia
->IaAddressCount
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1226 Instance
->CacheIa
= AllocateZeroPool (IaSize
);
1227 if (Instance
->CacheIa
== NULL
) {
1228 return EFI_OUT_OF_RESOURCES
;
1230 CopyMem (Instance
->CacheIa
, Ia
, IaSize
);
1236 Append CacheIa to the currrent IA. Meanwhile, clear CacheIa.ValidLifetime to 0.
1238 @param[in] Instance The pointer to DHCP6 instance.
1242 Dhcp6AppendCacheIa (
1243 IN DHCP6_INSTANCE
*Instance
1251 EFI_DHCP6_IA
*NewIa
;
1252 EFI_DHCP6_IA
*CacheIa
;
1254 Ia
= Instance
->IaCb
.Ia
;
1255 CacheIa
= Instance
->CacheIa
;
1257 if ((CacheIa
!= NULL
) && (CacheIa
->IaAddressCount
!= 0)) {
1259 // There are old addresses existing. Merge with current addresses.
1261 NewIaSize
= sizeof (EFI_DHCP6_IA
) + (Ia
->IaAddressCount
+ CacheIa
->IaAddressCount
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1262 NewIa
= AllocateZeroPool (NewIaSize
);
1263 if (NewIa
== NULL
) {
1267 IaSize
= sizeof (EFI_DHCP6_IA
) + (Ia
->IaAddressCount
- 1) * sizeof (EFI_DHCP6_IA_ADDRESS
);
1268 CopyMem (NewIa
, Ia
, IaSize
);
1271 // Clear old address.ValidLifetime
1273 for (Index
= 0; Index
< CacheIa
->IaAddressCount
; Index
++) {
1274 CacheIa
->IaAddress
[Index
].ValidLifetime
= 0;
1277 NewIa
->IaAddressCount
+= CacheIa
->IaAddressCount
;
1278 Ptr
= (UINT8
*)&NewIa
->IaAddress
[Ia
->IaAddressCount
];
1279 CopyMem (Ptr
, CacheIa
->IaAddress
, CacheIa
->IaAddressCount
* sizeof (EFI_DHCP6_IA_ADDRESS
));
1282 // Migrate to the NewIa and free previous.
1284 FreePool (Instance
->CacheIa
);
1285 FreePool (Instance
->IaCb
.Ia
);
1286 Instance
->CacheIa
= NULL
;
1287 Instance
->IaCb
.Ia
= NewIa
;
1292 Calculate the Dhcp6 get mapping timeout by adding additinal delay to the IP6 DAD transmits count.
1294 @param[in] Ip6Cfg The pointer to Ip6 config protocol.
1295 @param[out] TimeOut The time out value in 100ns units.
1297 @retval EFI_INVALID_PARAMETER Input parameters are invalid.
1298 @retval EFI_SUCCESS Calculate the time out value successfully.
1301 Dhcp6GetMappingTimeOut (
1302 IN EFI_IP6_CONFIG_PROTOCOL
*Ip6Cfg
,
1308 EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS DadXmits
;
1310 if (Ip6Cfg
== NULL
|| TimeOut
== NULL
) {
1311 return EFI_INVALID_PARAMETER
;
1314 DataSize
= sizeof (EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS
);
1315 Status
= Ip6Cfg
->GetData (
1317 Ip6ConfigDataTypeDupAddrDetectTransmits
,
1321 if (EFI_ERROR (Status
)) {
1325 *TimeOut
= TICKS_PER_SECOND
* DadXmits
.DupAddrDetectTransmits
+ DHCP6_DAD_ADDITIONAL_DELAY
;