2 The implementation of common functions shared by IP6 driver.
4 Copyright (c) 2009 - 2010, 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.
19 Build a array of EFI_IP6_ADDRESS_INFO to be returned to the caller. The number
20 of EFI_IP6_ADDRESS_INFO is also returned. If AddressList is NULL,
21 only the address count is returned.
23 @param[in] IpSb The IP6 service binding instance.
24 @param[out] AddressCount The number of returned addresses.
25 @param[out] AddressList The pointer to the array of EFI_IP6_ADDRESS_INFO.
26 This is an optional parameter.
29 @retval EFI_SUCCESS The address array successfully built.
30 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the address info.
31 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
35 Ip6BuildEfiAddressList (
37 OUT UINT32
*AddressCount
,
38 OUT EFI_IP6_ADDRESS_INFO
**AddressList OPTIONAL
43 EFI_IP6_ADDRESS_INFO
*EfiAddrInfo
;
44 IP6_ADDRESS_INFO
*AddrInfo
;
46 if (AddressCount
== NULL
) {
47 return EFI_INVALID_PARAMETER
;
50 if (IpSb
->LinkLocalOk
) {
51 Count
= 1 + IpSb
->DefaultInterface
->AddressCount
;
56 *AddressCount
= Count
;
58 if ((AddressList
== NULL
) || (Count
== 0)) {
62 if (*AddressList
== NULL
) {
63 *AddressList
= AllocatePool (sizeof (EFI_IP6_ADDRESS_INFO
) * Count
);
64 if (*AddressList
== NULL
) {
65 return EFI_OUT_OF_RESOURCES
;
69 EfiAddrInfo
= *AddressList
;
71 IP6_COPY_ADDRESS (&EfiAddrInfo
->Address
, &IpSb
->LinkLocalAddr
);
72 EfiAddrInfo
->PrefixLength
= IP6_LINK_LOCAL_PREFIX_LENGTH
;
77 NET_LIST_FOR_EACH (Entry
, &IpSb
->DefaultInterface
->AddressList
) {
78 AddrInfo
= NET_LIST_USER_STRUCT_S (Entry
, IP6_ADDRESS_INFO
, Link
, IP6_ADDR_INFO_SIGNATURE
);
80 IP6_COPY_ADDRESS (&EfiAddrInfo
->Address
, &AddrInfo
->Address
);
81 EfiAddrInfo
->PrefixLength
= AddrInfo
->PrefixLength
;
87 ASSERT (Count
== *AddressCount
);
93 Generate the multicast addresses identify the group of all IPv6 nodes or IPv6
94 routers defined in RFC4291.
96 All Nodes Addresses: FF01::1, FF02::1.
97 All Router Addresses: FF01::2, FF02::2, FF05::2.
99 @param[in] Router If TRUE, generate all routers addresses,
100 else generate all node addresses.
101 @param[in] Scope interface-local(1), link-local(2), or site-local(5)
102 @param[out] Ip6Addr The generated multicast address.
104 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
105 @retval EFI_SUCCESS The address is generated.
109 Ip6SetToAllNodeMulticast (
112 OUT EFI_IPv6_ADDRESS
*Ip6Addr
115 if (Ip6Addr
== NULL
) {
116 return EFI_INVALID_PARAMETER
;
119 if (!Router
&& Scope
== IP6_SITE_LOCAL_SCOPE
) {
120 return EFI_INVALID_PARAMETER
;
123 ZeroMem (Ip6Addr
, sizeof (EFI_IPv6_ADDRESS
));
124 Ip6Addr
->Addr
[0] = 0xFF;
125 Ip6Addr
->Addr
[1] = Scope
;
128 Ip6Addr
->Addr
[15] = 0x1;
130 Ip6Addr
->Addr
[15] = 0x2;
137 This function converts MAC address to 64 bits interface ID according to RFC4291
138 and returns the interface ID. Currently only 48-bit MAC address is supported by
141 @param[in, out] IpSb The IP6 service binding instance.
143 @retval NULL The operation fails.
144 @return Pointer to the generated interface ID.
148 Ip6CreateInterfaceID (
149 IN OUT IP6_SERVICE
*IpSb
152 UINT8 InterfaceId
[8];
154 EFI_MAC_ADDRESS
*MacAddr
;
157 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
159 AddrLen
= IpSb
->SnpMode
.HwAddressSize
;
162 // Currently only IEEE 802 48-bit MACs are supported to create link local address.
164 if (AddrLen
!= IP6_MAC_LEN
|| IpSb
->InterfaceIdLen
!= IP6_IF_ID_LEN
) {
168 MacAddr
= &IpSb
->SnpMode
.CurrentAddress
;
171 // Convert MAC address to 64 bits interface ID according to Appendix A of RFC4291:
172 // 1. Insert 0xFFFE to the middle
173 // 2. Invert the universal/local bit - bit 6 in network order
175 CopyMem (InterfaceId
, MacAddr
, 3);
176 InterfaceId
[3] = 0xFF;
177 InterfaceId
[4] = 0xFE;
178 CopyMem (&InterfaceId
[5], &MacAddr
->Addr
[3], 3);
180 Byte
= (UINT8
) (InterfaceId
[0] & IP6_U_BIT
);
181 if (Byte
== IP6_U_BIT
) {
182 InterfaceId
[0] &= ~IP6_U_BIT
;
184 InterfaceId
[0] |= IP6_U_BIT
;
188 // Return the interface ID.
190 return AllocateCopyPool (IpSb
->InterfaceIdLen
, InterfaceId
);
194 This function creates link-local address from interface identifier. The
195 interface identifier is normally created from MAC address. It might be manually
196 configured by administrator if the link-local address created from MAC address
197 is a duplicate address.
199 @param[in, out] IpSb The IP6 service binding instance.
201 @retval NULL If the operation fails.
202 @return The generated Link Local address, in network order.
206 Ip6CreateLinkLocalAddr (
207 IN OUT IP6_SERVICE
*IpSb
210 EFI_IPv6_ADDRESS
*Ip6Addr
;
211 EFI_IP6_CONFIG_PROTOCOL
*Ip6Config
;
213 EFI_IP6_CONFIG_INTERFACE_ID InterfaceId
;
216 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
218 if (IpSb
->InterfaceId
!= NULL
) {
219 FreePool (IpSb
->InterfaceId
);
223 // Get the interface id if it is manully configured.
225 Ip6Config
= &IpSb
->Ip6ConfigInstance
.Ip6Config
;
226 DataSize
= sizeof (EFI_IP6_CONFIG_INTERFACE_ID
);
227 ZeroMem (&InterfaceId
, DataSize
);
229 Status
= Ip6Config
->GetData (
231 Ip6ConfigDataTypeAltInterfaceId
,
235 if (Status
== EFI_NOT_FOUND
) {
237 // Since the interface id is not configured, generate the interface id from
240 IpSb
->InterfaceId
= Ip6CreateInterfaceID (IpSb
);
241 if (IpSb
->InterfaceId
== NULL
) {
245 CopyMem (&InterfaceId
, IpSb
->InterfaceId
, IpSb
->InterfaceIdLen
);
247 // Record the interface id.
249 Status
= Ip6Config
->SetData (
251 Ip6ConfigDataTypeAltInterfaceId
,
255 if (EFI_ERROR (Status
)) {
256 FreePool (IpSb
->InterfaceId
);
257 IpSb
->InterfaceId
= NULL
;
260 } else if (!EFI_ERROR (Status
)) {
261 IpSb
->InterfaceId
= AllocateCopyPool (DataSize
, &InterfaceId
);
262 if (IpSb
->InterfaceId
== NULL
) {
270 // Append FE80::/64 to the left of IPv6 address then return.
272 Ip6Addr
= AllocateZeroPool (sizeof (EFI_IPv6_ADDRESS
));
273 if (Ip6Addr
== NULL
) {
274 FreePool (IpSb
->InterfaceId
);
275 IpSb
->InterfaceId
= NULL
;
279 CopyMem (&Ip6Addr
->Addr
[8], IpSb
->InterfaceId
, IpSb
->InterfaceIdLen
);
280 Ip6Addr
->Addr
[1] = 0x80;
281 Ip6Addr
->Addr
[0] = 0xFE;
287 Compute the solicited-node multicast address for an unicast or anycast address,
288 by taking the low-order 24 bits of this address, and appending those bits to
289 the prefix FF02:0:0:0:0:1:FF00::/104.
291 @param[in] Ip6Addr The unicast or anycast address, in network order.
292 @param[out] MulticastAddr The generated solicited-node multicast address,
297 Ip6CreateSNMulticastAddr (
298 IN EFI_IPv6_ADDRESS
*Ip6Addr
,
299 OUT EFI_IPv6_ADDRESS
*MulticastAddr
302 ASSERT (Ip6Addr
!= NULL
&& MulticastAddr
!= NULL
);
304 ZeroMem (MulticastAddr
, sizeof (EFI_IPv6_ADDRESS
));
306 MulticastAddr
->Addr
[0] = 0xFF;
307 MulticastAddr
->Addr
[1] = 0x02;
308 MulticastAddr
->Addr
[11] = 0x1;
309 MulticastAddr
->Addr
[12] = 0xFF;
311 CopyMem (&MulticastAddr
->Addr
[13], &Ip6Addr
->Addr
[13], 3);
315 Insert a node IP6_ADDRESS_INFO to an IP6 interface.
317 @param[in, out] IpIf Points to an IP6 interface.
318 @param[in] AddrInfo Points to IP6_ADDRESS_INFO
323 IN OUT IP6_INTERFACE
*IpIf
,
324 IN IP6_ADDRESS_INFO
*AddrInfo
327 InsertHeadList (&IpIf
->AddressList
, &AddrInfo
->Link
);
328 IpIf
->AddressCount
++;
332 Destroy the IP instance if its StationAddress is removed. It is the help function
335 @param[in, out] IpSb Points to an IP6 service binding instance.
336 @param[in] Address The to be removed address
340 Ip6DestroyInstanceByAddress (
341 IN OUT IP6_SERVICE
*IpSb
,
342 IN EFI_IPv6_ADDRESS
*Address
345 BOOLEAN OneDestroyed
;
346 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
348 IP6_PROTOCOL
*Instance
;
350 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
352 ServiceBinding
= &IpSb
->ServiceBinding
;
355 // Upper layer IP protocol consumers may have tight relationship between several
356 // IP protocol instances, in other words, calling ServiceBinding->DestroyChild to
357 // destroy one IP child may cause other related IP children destroyed too. This
358 // will probably leave hole in the children list when we iterate it. So everytime
359 // we just destroy one child then back to the start point to iterate the list.
362 OneDestroyed
= FALSE
;
364 NET_LIST_FOR_EACH (Entry
, &IpSb
->Children
) {
365 Instance
= NET_LIST_USER_STRUCT_S (Entry
, IP6_PROTOCOL
, Link
, IP6_PROTOCOL_SIGNATURE
);
367 if ((Instance
->State
== IP6_STATE_CONFIGED
) && EFI_IP6_EQUAL (&Instance
->ConfigData
.StationAddress
, Address
)) {
368 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
373 } while (OneDestroyed
);
377 Remove the IPv6 address from the address list node points to IP6_ADDRESS_INFO.
379 This function removes the matching IPv6 addresses from the address list and
380 adjusts the address count of the address list. If IpSb is not NULL, this function
381 calls Ip6LeaveGroup to see whether it should call Mnp->Groups() to remove the
382 its solicited-node multicast MAC address from the filter list and sends out
383 a Multicast Listener Done. If Prefix is NULL, all address in the address list
384 will be removed. If Prefix is not NULL, the address that matching the Prefix
385 with PrefixLength in the address list will be removed.
387 @param[in] IpSb NULL or points to IP6 service binding instance.
388 @param[in, out] AddressList Address list array.
389 @param[in, out] AddressCount The count of addresses in address list array.
390 @param[in] Prefix NULL or an IPv6 address prefix.
391 @param[in] PrefixLength The length of Prefix.
393 @retval EFI_SUCCESS The operation completed successfully.
394 @retval EFI_NOT_FOUND The address matching the Prefix with PrefixLength
395 cannot be found in the address list.
396 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
401 IN IP6_SERVICE
*IpSb OPTIONAL
,
402 IN OUT LIST_ENTRY
*AddressList
,
403 IN OUT UINT32
*AddressCount
,
404 IN EFI_IPv6_ADDRESS
*Prefix OPTIONAL
,
405 IN UINT8 PrefixLength
411 IP6_ADDRESS_INFO
*AddrInfo
;
412 EFI_IPv6_ADDRESS SnMCastAddr
;
414 if (IsListEmpty (AddressList
) || *AddressCount
< 1 || PrefixLength
> IP6_PREFIX_NUM
) {
415 return EFI_INVALID_PARAMETER
;
418 Status
= EFI_NOT_FOUND
;
420 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, AddressList
) {
421 AddrInfo
= NET_LIST_USER_STRUCT_S (Entry
, IP6_ADDRESS_INFO
, Link
, IP6_ADDR_INFO_SIGNATURE
);
423 if (Prefix
== NULL
||
424 (PrefixLength
== 128 && EFI_IP6_EQUAL (Prefix
, &AddrInfo
->Address
)) ||
425 (PrefixLength
== AddrInfo
->PrefixLength
&& NetIp6IsNetEqual (Prefix
, &AddrInfo
->Address
, PrefixLength
))
428 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
429 Ip6CreateSNMulticastAddr (&AddrInfo
->Address
, &SnMCastAddr
);
430 Ip6LeaveGroup (IpSb
, &SnMCastAddr
);
433 // Destroy any instance who is using the dying address as the source address.
435 Ip6DestroyInstanceByAddress (IpSb
, &AddrInfo
->Address
);
438 RemoveEntryList (Entry
);
442 Status
= EFI_SUCCESS
;
450 Check whether the incoming Ipv6 address is a solicited-node multicast address.
452 @param[in] Ip6 Ip6 address, in network order.
454 @retval TRUE Yes, solicited-node multicast address
459 Ip6IsSNMulticastAddr (
460 IN EFI_IPv6_ADDRESS
*Ip6
466 Ip6CreateSNMulticastAddr (Ip6
, &Sn
);
469 if (CompareMem (Sn
.Addr
, Ip6
->Addr
, 13) == 0) {
477 Check whether the incoming IPv6 address is one of the maintained addresses in
478 the IP6 service binding instance.
480 @param[in] IpSb Points to a IP6 service binding instance.
481 @param[in] Address The IP6 address to be checked.
482 @param[out] Interface If not NULL, output the IP6 interface which
483 maintains the Address.
484 @param[out] AddressInfo If not NULL, output the IP6 address information
487 @retval TRUE Yes, it is one of the maintained address.
488 @retval FALSE No, it is not one of the maintained address.
492 Ip6IsOneOfSetAddress (
493 IN IP6_SERVICE
*IpSb
,
494 IN EFI_IPv6_ADDRESS
*Address
,
495 OUT IP6_INTERFACE
**Interface OPTIONAL
,
496 OUT IP6_ADDRESS_INFO
**AddressInfo OPTIONAL
502 IP6_ADDRESS_INFO
*TmpAddressInfo
;
505 // Check link-local address first
507 if (IpSb
->LinkLocalOk
&& EFI_IP6_EQUAL (&IpSb
->LinkLocalAddr
, Address
)) {
508 if (Interface
!= NULL
) {
509 *Interface
= IpSb
->DefaultInterface
;
512 if (AddressInfo
!= NULL
) {
519 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
520 IpIf
= NET_LIST_USER_STRUCT_S (Entry
, IP6_INTERFACE
, Link
, IP6_INTERFACE_SIGNATURE
);
522 NET_LIST_FOR_EACH (Entry2
, &IpIf
->AddressList
) {
523 TmpAddressInfo
= NET_LIST_USER_STRUCT_S (Entry2
, IP6_ADDRESS_INFO
, Link
, IP6_ADDR_INFO_SIGNATURE
);
525 if (EFI_IP6_EQUAL (&TmpAddressInfo
->Address
, Address
)) {
526 if (Interface
!= NULL
) {
530 if (AddressInfo
!= NULL
) {
531 *AddressInfo
= TmpAddressInfo
;
543 Check whether the incoming MAC address is valid.
545 @param[in] IpSb Points to a IP6 service binding instance.
546 @param[in] LinkAddress The MAC address.
548 @retval TRUE Yes, it is valid.
549 @retval FALSE No, it is not valid.
553 Ip6IsValidLinkAddress (
554 IN IP6_SERVICE
*IpSb
,
555 IN EFI_MAC_ADDRESS
*LinkAddress
561 // TODO: might be updated later to be more acceptable.
563 for (Index
= IpSb
->SnpMode
.HwAddressSize
; Index
< sizeof (EFI_MAC_ADDRESS
); Index
++) {
564 if (LinkAddress
->Addr
[Index
] != 0) {
573 Copy the PrefixLength bits from Src to Dest.
575 @param[out] Dest A pointer to the buffer to copy to.
576 @param[in] Src A pointer to the buffer to copy from.
577 @param[in] PrefixLength The number of bits to copy.
581 Ip6CopyAddressByPrefix (
582 OUT EFI_IPv6_ADDRESS
*Dest
,
583 IN EFI_IPv6_ADDRESS
*Src
,
584 IN UINT8 PrefixLength
591 ASSERT (Dest
!= NULL
&& Src
!= NULL
);
592 ASSERT (PrefixLength
< IP6_PREFIX_NUM
);
594 Byte
= (UINT8
) (PrefixLength
/ 8);
595 Bit
= (UINT8
) (PrefixLength
% 8);
597 ZeroMem (Dest
, sizeof (EFI_IPv6_ADDRESS
));
599 CopyMem (Dest
, Src
, Byte
);
602 Mask
= (UINT8
) (0xFF << (8 - Bit
));
604 Dest
->Addr
[Byte
] = (UINT8
) (Src
->Addr
[Byte
] & Mask
);
609 Get the MAC address for a multicast IP address. Call
610 Mnp's McastIpToMac to find the MAC address instead of
611 hard-coding the NIC to be Ethernet.
613 @param[in] Mnp The Mnp instance to get the MAC address.
614 @param[in] Multicast The multicast IP address to translate.
615 @param[out] Mac The buffer to hold the translated address.
617 @retval EFI_SUCCESS The multicast IP successfully
618 translated to a multicast MAC address.
619 @retval Other The address is not converted because an error occurred.
624 IN EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
,
625 IN EFI_IPv6_ADDRESS
*Multicast
,
626 OUT EFI_MAC_ADDRESS
*Mac
629 EFI_IP_ADDRESS EfiIp
;
631 IP6_COPY_ADDRESS (&EfiIp
.v6
, Multicast
);
633 return Mnp
->McastIpToMac (Mnp
, TRUE
, &EfiIp
, Mac
);
637 Set the Ip6 variable data.
639 @param[in] IpSb Points to an IP6 service binding instance.
641 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the variable.
642 @retval other Set variable failed.
650 UINT32 NumConfiguredInstance
;
652 UINTN VariableDataSize
;
653 EFI_IP6_VARIABLE_DATA
*Ip6VariableData
;
654 EFI_IP6_ADDRESS_PAIR
*Ip6AddressPair
;
655 IP6_PROTOCOL
*IpInstance
;
656 CHAR16
*NewMacString
;
659 NumConfiguredInstance
= 0;
662 // Go through the children list to count the configured children.
664 NET_LIST_FOR_EACH (Entry
, &IpSb
->Children
) {
665 IpInstance
= NET_LIST_USER_STRUCT_S (Entry
, IP6_PROTOCOL
, Link
, IP6_PROTOCOL_SIGNATURE
);
667 if (IpInstance
->State
== IP6_STATE_CONFIGED
) {
668 NumConfiguredInstance
++;
673 // Calculate the size of the Ip6VariableData. As there may be no IP child,
674 // we should add extra buffer for the address paris only if the number of configured
675 // children is more than 1.
677 VariableDataSize
= sizeof (EFI_IP6_VARIABLE_DATA
);
679 if (NumConfiguredInstance
> 1) {
680 VariableDataSize
+= sizeof (EFI_IP6_ADDRESS_PAIR
) * (NumConfiguredInstance
- 1);
683 Ip6VariableData
= AllocatePool (VariableDataSize
);
684 if (Ip6VariableData
== NULL
) {
685 return EFI_OUT_OF_RESOURCES
;
688 Ip6VariableData
->DriverHandle
= IpSb
->Image
;
689 Ip6VariableData
->AddressCount
= NumConfiguredInstance
;
691 Ip6AddressPair
= &Ip6VariableData
->AddressPairs
[0];
694 // Go through the children list to fill the configured children's address pairs.
696 NET_LIST_FOR_EACH (Entry
, &IpSb
->Children
) {
697 IpInstance
= NET_LIST_USER_STRUCT_S (Entry
, IP6_PROTOCOL
, Link
, IP6_PROTOCOL_SIGNATURE
);
699 if (IpInstance
->State
== IP6_STATE_CONFIGED
) {
700 Ip6AddressPair
->InstanceHandle
= IpInstance
->Handle
;
701 Ip6AddressPair
->PrefixLength
= IpInstance
->PrefixLength
;
702 IP6_COPY_ADDRESS (&Ip6AddressPair
->Ip6Address
, &IpInstance
->ConfigData
.StationAddress
);
709 // Get the mac string.
711 Status
= NetLibGetMacString (IpSb
->Controller
, IpSb
->Image
, &NewMacString
);
712 if (EFI_ERROR (Status
)) {
716 if (IpSb
->MacString
!= NULL
) {
718 // The variable is set already, we're going to update it.
720 if (StrCmp (IpSb
->MacString
, NewMacString
) != 0) {
722 // The mac address is changed, delete the previous variable first.
726 &gEfiIp6ServiceBindingProtocolGuid
,
727 EFI_VARIABLE_BOOTSERVICE_ACCESS
,
733 FreePool (IpSb
->MacString
);
736 IpSb
->MacString
= NewMacString
;
738 Status
= gRT
->SetVariable (
740 &gEfiIp6ServiceBindingProtocolGuid
,
741 EFI_VARIABLE_BOOTSERVICE_ACCESS
,
743 (VOID
*) Ip6VariableData
747 FreePool (Ip6VariableData
);
752 Clear the variable and free the resource.
754 @param[in] IpSb Ip6 service binding instance.
758 Ip6ClearVariableData (
762 ASSERT (IpSb
->MacString
!= NULL
);
766 &gEfiIp6ServiceBindingProtocolGuid
,
767 EFI_VARIABLE_BOOTSERVICE_ACCESS
,
772 FreePool (IpSb
->MacString
);
773 IpSb
->MacString
= NULL
;
777 Convert the multibyte field in IP header's byter order.
778 In spite of its name, it can also be used to convert from
779 host to network byte order.
781 @param[in, out] Head The IP head to convert.
783 @return Point to the converted IP head.
788 IN OUT EFI_IP6_HEADER
*Head
791 Head
->FlowLabelL
= NTOHS (Head
->FlowLabelL
);
792 Head
->PayloadLength
= NTOHS (Head
->PayloadLength
);