X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FLibrary%2FDxeNetLib%2FDxeNetLib.c;h=b8544b89ab8d3f5bee7b5e94c1eb12f615b170c2;hp=4bd84c4de7342f0a5db8129e67b69fd704dcb701;hb=29788f178e48fa5ffe7d3262d73c9548e9285d2d;hpb=79bcf0554bd91975095b3c4acf9e0c6a8f447588 diff --git a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c index 4bd84c4de7..b8544b89ab 100644 --- a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c +++ b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c @@ -1,7 +1,8 @@ /** @file Network library. -Copyright (c) 2005 - 2010, Intel Corporation. All rights reserved.
+Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.
+(C) Copyright 2015 Hewlett Packard Enterprise Development LP
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -13,16 +14,17 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include +#include + #include #include #include #include -#include +#include #include #include -#include -#include +#include #include #include @@ -32,11 +34,11 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include #include -#include #include #include #define NIC_ITEM_CONFIG_SIZE sizeof (NIC_IP4_CONFIG_INFO) + sizeof (EFI_IP4_ROUTE_TABLE) * MAX_IP4_CONFIG_IN_VARIABLE +#define DEFAULT_ZERO_START ((UINTN) ~0) // // All the supported IP4 maskes in host byte order. @@ -185,11 +187,7 @@ SyslogLocateSnp ( Snp = NULL; } - // - // Handles is allocated by gBS AllocatePool() service. - // So, gBS FreePool() service is used to free Handles. - // - gBS->FreePool (Handles); + FreePool (Handles); return Snp; } @@ -303,7 +301,7 @@ ON_EXIT: Build a syslog packet, including the Ethernet/Ip/Udp headers and user's message. - @param[in] Level Syslog servity level + @param[in] Level Syslog severity level @param[in] Module The module that generates the log @param[in] File The file that contains the current log @param[in] Line The line of code in the File that contains the current log @@ -477,7 +475,7 @@ NetDebugASPrint ( This function will locate a instance of SNP then send the message through it. Because it isn't open the SNP BY_DRIVER, apply caution when using it. - @param Level The servity level of the message. + @param Level The severity level of the message. @param Module The Moudle that generates the log. @param File The file that contains the log. @param Line The exact line that contains the log. @@ -567,7 +565,7 @@ NetGetMaskLength ( { INTN Index; - for (Index = 0; Index < IP4_MASK_NUM; Index++) { + for (Index = 0; Index <= IP4_MASK_MAX; Index++) { if (NetMask == gIp4AllMasks[Index]) { break; } @@ -582,6 +580,11 @@ NetGetMaskLength ( Return the class of the IP address, such as class A, B, C. Addr is in host byte order. + [ATTENTION] + Classful addressing (IP class A/B/C) has been deprecated according to RFC4632. + Caller of this function could only check the returned value against + IP4_ADDR_CLASSD (multicast) or IP4_ADDR_CLASSE (reserved) now. + The address of class A starts with 0. If the address belong to class A, return IP4_ADDR_CLASSA. The address of class B starts with 10. @@ -630,12 +633,13 @@ NetGetIpClass ( /** Check whether the IP is a valid unicast address according to - the netmask. If NetMask is zero, use the IP address's class to get the default mask. + the netmask. - If Ip is 0, IP is not a valid unicast address. - Class D address is used for multicasting and class E address is reserved for future. If Ip - belongs to class D or class E, IP is not a valid unicast address. - If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address. + ASSERT if NetMask is zero. + + If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address, + except when the originator is one of the endpoints of a point-to-point link with a 31-bit + mask (RFC3021). @param[in] Ip The IP to check against. @param[in] NetMask The mask of the IP. @@ -650,20 +654,18 @@ NetIp4IsUnicast ( IN IP4_ADDR NetMask ) { - INTN Class; - - Class = NetGetIpClass (Ip); - - if ((Ip == 0) || (Class >= IP4_ADDR_CLASSD)) { + ASSERT (NetMask != 0); + + if (Ip == 0 || IP4_IS_LOCAL_BROADCAST (Ip)) { return FALSE; } - if (NetMask == 0) { - NetMask = gIp4AllMasks[Class << 3]; - } - - if (((Ip &~NetMask) == ~NetMask) || ((Ip &~NetMask) == 0)) { - return FALSE; + if (NetGetMaskLength (NetMask) != 31) { + if (((Ip &~NetMask) == ~NetMask) || ((Ip &~NetMask) == 0)) { + return FALSE; + } + } else { + return TRUE; } return TRUE; @@ -796,7 +798,7 @@ NetIp6IsNetEqual ( UINT8 Bit; UINT8 Mask; - ASSERT ((Ip1 != NULL) && (Ip2 != NULL) && (PrefixLength < IP6_PREFIX_NUM)); + ASSERT ((Ip1 != NULL) && (Ip2 != NULL) && (PrefixLength <= IP6_PREFIX_MAX)); if (PrefixLength == 0) { return TRUE; @@ -856,11 +858,11 @@ Ip6Swap128 ( } /** - Initialize a random seed using current time. + Initialize a random seed using current time and monotonic count. - Get current time first. Then initialize a random seed based on some basic - mathematics operation on the hour, day, minute, second, nanosecond and year - of the current time. + Get current time and monotonic count first. Then initialize a random seed + based on some basic mathematics operation on the hour, day, minute, second, + nanosecond and year of the current time and the monotonic count value. @return The random seed initialized with current time. @@ -873,12 +875,16 @@ NetRandomInitSeed ( { EFI_TIME Time; UINT32 Seed; + UINT64 MonotonicCount; gRT->GetTime (&Time, NULL); - Seed = (~Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second); + Seed = (Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second); Seed ^= Time.Nanosecond; Seed ^= Time.Year << 7; + gBS->GetNextMonotonicCount (&MonotonicCount); + Seed += (UINT32) MonotonicCount; + return Seed; } @@ -1066,6 +1072,116 @@ NetListInsertBefore ( PostEntry->BackLink = NewEntry; } +/** + Safe destroy nodes in a linked list, and return the length of the list after all possible operations finished. + + Destroy network child instance list by list traversals is not safe due to graph dependencies between nodes. + This function performs a safe traversal to destroy these nodes by checking to see if the node being destroyed + has been removed from the list or not. + If it has been removed, then restart the traversal from the head. + If it hasn't been removed, then continue with the next node directly. + This function will end the iterate and return the CallBack's last return value if error happens, + or retrun EFI_SUCCESS if 2 complete passes are made with no changes in the number of children in the list. + + @param[in] List The head of the list. + @param[in] CallBack Pointer to the callback function to destroy one node in the list. + @param[in] Context Pointer to the callback function's context: corresponds to the + parameter Context in NET_DESTROY_LINK_LIST_CALLBACK. + @param[out] ListLength The length of the link list if the function returns successfully. + + @retval EFI_SUCCESS Two complete passes are made with no changes in the number of children. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + @retval Others Return the CallBack's last return value. + +**/ +EFI_STATUS +EFIAPI +NetDestroyLinkList ( + IN LIST_ENTRY *List, + IN NET_DESTROY_LINK_LIST_CALLBACK CallBack, + IN VOID *Context, OPTIONAL + OUT UINTN *ListLength OPTIONAL + ) +{ + UINTN PreviousLength; + LIST_ENTRY *Entry; + LIST_ENTRY *Ptr; + UINTN Length; + EFI_STATUS Status; + + if (List == NULL || CallBack == NULL) { + return EFI_INVALID_PARAMETER; + } + + Length = 0; + do { + PreviousLength = Length; + Entry = GetFirstNode (List); + while (!IsNull (List, Entry)) { + Status = CallBack (Entry, Context); + if (EFI_ERROR (Status)) { + return Status; + } + // + // Walk through the list to see whether the Entry has been removed or not. + // If the Entry still exists, just try to destroy the next one. + // If not, go back to the start point to iterate the list again. + // + for (Ptr = List->ForwardLink; Ptr != List; Ptr = Ptr->ForwardLink) { + if (Ptr == Entry) { + break; + } + } + if (Ptr == Entry) { + Entry = GetNextNode (List, Entry); + } else { + Entry = GetFirstNode (List); + } + } + for (Length = 0, Ptr = List->ForwardLink; Ptr != List; Length++, Ptr = Ptr->ForwardLink); + } while (Length != PreviousLength); + + if (ListLength != NULL) { + *ListLength = Length; + } + return EFI_SUCCESS; +} + +/** + This function checks the input Handle to see if it's one of these handles in ChildHandleBuffer. + + @param[in] Handle Handle to be checked. + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval TRUE Found the input Handle in ChildHandleBuffer. + @retval FALSE Can't find the input Handle in ChildHandleBuffer. + +**/ +BOOLEAN +EFIAPI +NetIsInHandleBuffer ( + IN EFI_HANDLE Handle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + UINTN Index; + + if (NumberOfChildren == 0 || ChildHandleBuffer == NULL) { + return FALSE; + } + + for (Index = 0; Index < NumberOfChildren; Index++) { + if (Handle == ChildHandleBuffer[Index]) { + return TRUE; + } + } + + return FALSE; +} + /** Initialize the netmap. Netmap is a reposity to keep the pairs. @@ -1524,7 +1640,7 @@ NetMapRemoveTail ( /** Iterate through the netmap and call CallBack for each item. - It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break + It will continue the traverse if CallBack returns EFI_SUCCESS, otherwise, break from the loop. It returns the CallBack's last return value. This function is delete safe for the current item. @@ -1576,94 +1692,6 @@ NetMapIterate ( } -/** - Internal function to get the child handle of the NIC handle. - - @param[in] Controller NIC controller handle. - @param[out] ChildHandle Returned child handle. - - @retval EFI_SUCCESS Successfully to get child handle. - @retval Others Failed to get child handle. - -**/ -EFI_STATUS -NetGetChildHandle ( - IN EFI_HANDLE Controller, - OUT EFI_HANDLE *ChildHandle - ) -{ - EFI_STATUS Status; - EFI_HANDLE *Handles; - UINTN HandleCount; - UINTN Index; - EFI_DEVICE_PATH_PROTOCOL *ChildDeviceDevicePath; - VENDOR_DEVICE_PATH *VendorDeviceNode; - - // - // Locate all EFI Hii Config Access protocols - // - Status = gBS->LocateHandleBuffer ( - ByProtocol, - &gEfiHiiConfigAccessProtocolGuid, - NULL, - &HandleCount, - &Handles - ); - if (EFI_ERROR (Status) || (HandleCount == 0)) { - return Status; - } - - Status = EFI_NOT_FOUND; - - for (Index = 0; Index < HandleCount; Index++) { - - Status = EfiTestChildHandle (Controller, Handles[Index], &gEfiManagedNetworkServiceBindingProtocolGuid); - if (!EFI_ERROR (Status)) { - // - // Get device path on the child handle - // - Status = gBS->HandleProtocol ( - Handles[Index], - &gEfiDevicePathProtocolGuid, - (VOID **) &ChildDeviceDevicePath - ); - - if (!EFI_ERROR (Status)) { - while (!IsDevicePathEnd (ChildDeviceDevicePath)) { - ChildDeviceDevicePath = NextDevicePathNode (ChildDeviceDevicePath); - // - // Parse one instance - // - if (ChildDeviceDevicePath->Type == HARDWARE_DEVICE_PATH && - ChildDeviceDevicePath->SubType == HW_VENDOR_DP) { - VendorDeviceNode = (VENDOR_DEVICE_PATH *) ChildDeviceDevicePath; - if (CompareMem (&VendorDeviceNode->Guid, &gEfiNicIp4ConfigVariableGuid, sizeof (EFI_GUID)) == 0) { - // - // Found item matched gEfiNicIp4ConfigVariableGuid - // - *ChildHandle = Handles[Index]; - // - // Handles is allocated by gBS AllocatePool() service. - // So, gBS FreePool() service is used to free Handles. - // - gBS->FreePool (Handles); - return EFI_SUCCESS; - } - } - } - } - } - } - - // - // Handles is allocated by gBS AllocatePool() service. - // So, gBS FreePool() service is used to free Handles. - // - gBS->FreePool (Handles); - return Status; -} - - /** This is the default unload handle for all the network drivers. @@ -1686,6 +1714,7 @@ NetLibDefaultUnload ( EFI_HANDLE *DeviceHandleBuffer; UINTN DeviceHandleCount; UINTN Index; + UINTN Index2; EFI_DRIVER_BINDING_PROTOCOL *DriverBinding; EFI_COMPONENT_NAME_PROTOCOL *ComponentName; EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2; @@ -1707,28 +1736,12 @@ NetLibDefaultUnload ( return Status; } - // - // Disconnect the driver specified by ImageHandle from all - // the devices in the handle database. - // - for (Index = 0; Index < DeviceHandleCount; Index++) { - Status = gBS->DisconnectController ( - DeviceHandleBuffer[Index], - ImageHandle, - NULL - ); - } - - // - // Uninstall all the protocols installed in the driver entry point - // for (Index = 0; Index < DeviceHandleCount; Index++) { Status = gBS->HandleProtocol ( DeviceHandleBuffer[Index], &gEfiDriverBindingProtocolGuid, (VOID **) &DriverBinding ); - if (EFI_ERROR (Status)) { continue; } @@ -1736,12 +1749,28 @@ NetLibDefaultUnload ( if (DriverBinding->ImageHandle != ImageHandle) { continue; } - + + // + // Disconnect the driver specified by ImageHandle from all + // the devices in the handle database. + // + for (Index2 = 0; Index2 < DeviceHandleCount; Index2++) { + Status = gBS->DisconnectController ( + DeviceHandleBuffer[Index2], + DriverBinding->DriverBindingHandle, + NULL + ); + } + + // + // Uninstall all the protocols installed in the driver entry point + // gBS->UninstallProtocolInterface ( - ImageHandle, + DriverBinding->DriverBindingHandle, &gEfiDriverBindingProtocolGuid, DriverBinding ); + Status = gBS->HandleProtocol ( DeviceHandleBuffer[Index], &gEfiComponentNameProtocolGuid, @@ -1749,7 +1778,7 @@ NetLibDefaultUnload ( ); if (!EFI_ERROR (Status)) { gBS->UninstallProtocolInterface ( - ImageHandle, + DriverBinding->DriverBindingHandle, &gEfiComponentNameProtocolGuid, ComponentName ); @@ -1762,7 +1791,7 @@ NetLibDefaultUnload ( ); if (!EFI_ERROR (Status)) { gBS->UninstallProtocolInterface ( - ImageHandle, + DriverBinding->DriverBindingHandle, &gEfiComponentName2ProtocolGuid, ComponentName2 ); @@ -1838,7 +1867,7 @@ NetLibCreateServiceChild ( /** - Destory a child of the service that is identified by ServiceBindingGuid. + Destroy a child of the service that is identified by ServiceBindingGuid. Get the ServiceBinding Protocol first, then use it to destroy a child. @@ -1847,10 +1876,10 @@ NetLibCreateServiceChild ( @param[in] Controller The controller which has the service installed. @param[in] Image The image handle used to open service. @param[in] ServiceBindingGuid The service's Guid. - @param[in] ChildHandle The child to destory. + @param[in] ChildHandle The child to destroy. - @retval EFI_SUCCESS The child is successfully destoried. - @retval Others Failed to destory the child. + @retval EFI_SUCCESS The child is successfully destroyed. + @retval Others Failed to destroy the child. **/ EFI_STATUS @@ -1884,7 +1913,7 @@ NetLibDestroyServiceChild ( } // - // destory the child + // destroy the child // Status = Service->DestroyChild (Service, ChildHandle); return Status; @@ -2139,6 +2168,7 @@ NetLibGetMacAddress ( (VOID **) &Mnp ); if (EFI_ERROR (Status)) { + MnpSb->DestroyChild (MnpSb, MnpChildHandle); return Status; } @@ -2146,7 +2176,8 @@ NetLibGetMacAddress ( // Try to get SNP mode from MNP // Status = Mnp->GetModeData (Mnp, NULL, &SnpModeData); - if (EFI_ERROR (Status)) { + if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) { + MnpSb->DestroyChild (MnpSb, MnpChildHandle); return Status; } SnpMode = &SnpModeData; @@ -2175,7 +2206,8 @@ NetLibGetMacAddress ( @param[in] ServiceHandle The handle where network service binding protocol is installed on. @param[in] ImageHandle The image handle used to act as the agent handle to - get the simple network protocol. + get the simple network protocol. This parameter is + optional and may be NULL. @param[out] MacString The pointer to store the address of the string representation of the mac address. @@ -2188,7 +2220,7 @@ EFI_STATUS EFIAPI NetLibGetMacString ( IN EFI_HANDLE ServiceHandle, - IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ImageHandle, OPTIONAL OUT CHAR16 **MacString ) { @@ -2199,6 +2231,7 @@ NetLibGetMacString ( UINT16 VlanId; CHAR16 *String; UINTN Index; + UINTN BufferSize; ASSERT (MacString != NULL); @@ -2215,7 +2248,8 @@ NetLibGetMacString ( // If VLAN is configured, it will need extra 5 characters like "\0005". // Plus one unicode character for the null-terminator. // - String = AllocateZeroPool ((2 * HwAddressSize + 5 + 1) * sizeof (CHAR16)); + BufferSize = (2 * HwAddressSize + 5 + 1) * sizeof (CHAR16); + String = AllocateZeroPool (BufferSize); if (String == NULL) { return EFI_OUT_OF_RESOURCES; } @@ -2226,7 +2260,14 @@ NetLibGetMacString ( // HwAddress = &MacAddress.Addr[0]; for (Index = 0; Index < HwAddressSize; Index++) { - String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, *(HwAddress++), 2); + UnicodeValueToStringS ( + String, + BufferSize - ((UINTN)String - (UINTN)*MacString), + PREFIX_ZERO | RADIX_HEX, + *(HwAddress++), + 2 + ); + String += StrnLenS (String, (BufferSize - ((UINTN)String - (UINTN)*MacString)) / sizeof (CHAR16)); } // @@ -2235,7 +2276,14 @@ NetLibGetMacString ( VlanId = NetLibGetVlanId (ServiceHandle); if (VlanId != 0) { *String++ = L'\\'; - String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, VlanId, 4); + UnicodeValueToStringS ( + String, + BufferSize - ((UINTN)String - (UINTN)*MacString), + PREFIX_ZERO | RADIX_HEX, + VlanId, + 4 + ); + String += StrnLenS (String, (BufferSize - ((UINTN)String - (UINTN)*MacString)) / sizeof (CHAR16)); } // @@ -2458,12 +2506,11 @@ Exit: Check the default address used by the IPv4 driver is static or dynamic (acquired from DHCP). - If the controller handle does not have the NIC Ip4 Config Protocol installed, the - default address is static. If the EFI variable to save the configuration is not found, - the default address is static. Otherwise, get the result from the EFI variable which - saving the configuration. + If the controller handle does not have the EFI_IP4_CONFIG2_PROTOCOL installed, the + default address is static. If failed to get the policy from Ip4 Config2 Protocol, + the default address is static. Otherwise, get the result from Ip4 Config2 Protocol. - @param[in] Controller The controller handle which has the NIC Ip4 Config Protocol + @param[in] Controller The controller handle which has the EFI_IP4_CONFIG2_PROTOCOL relative with the default address to judge. @retval TRUE If the default address is static. @@ -2476,107 +2523,34 @@ NetLibDefaultAddressIsStatic ( ) { EFI_STATUS Status; - EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting; - UINTN Len; - NIC_IP4_CONFIG_INFO *ConfigInfo; + EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2; + UINTN DataSize; + EFI_IP4_CONFIG2_POLICY Policy; BOOLEAN IsStatic; - EFI_STRING ConfigHdr; - EFI_STRING ConfigResp; - EFI_STRING AccessProgress; - EFI_STRING AccessResults; - EFI_STRING String; - EFI_HANDLE ChildHandle; - - ConfigInfo = NULL; - ConfigHdr = NULL; - ConfigResp = NULL; - AccessProgress = NULL; - AccessResults = NULL; - IsStatic = TRUE; - - Status = gBS->LocateProtocol ( - &gEfiHiiConfigRoutingProtocolGuid, - NULL, - (VOID **) &HiiConfigRouting - ); - if (EFI_ERROR (Status)) { - return TRUE; - } - Status = NetGetChildHandle (Controller, &ChildHandle); - if (EFI_ERROR (Status)) { - return TRUE; - } + Ip4Config2 = NULL; + + DataSize = sizeof (EFI_IP4_CONFIG2_POLICY); + + IsStatic = TRUE; // - // Construct config request string header + // Get Ip4Config2 policy. // - ConfigHdr = HiiConstructConfigHdr (&gEfiNicIp4ConfigVariableGuid, EFI_NIC_IP4_CONFIG_VARIABLE, ChildHandle); - if (ConfigHdr == NULL) { - return TRUE; - } - - Len = StrLen (ConfigHdr); - ConfigResp = AllocateZeroPool ((Len + NIC_ITEM_CONFIG_SIZE * 2 + 100) * sizeof (CHAR16)); - if (ConfigResp == NULL) { - goto ON_EXIT; - } - StrCpy (ConfigResp, ConfigHdr); - - String = ConfigResp + Len; - UnicodeSPrint ( - String, - (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16), - L"&OFFSET=%04X&WIDTH=%04X", - OFFSET_OF (NIC_IP4_CONFIG_INFO, Source), - sizeof (UINT32) - ); - - Status = HiiConfigRouting->ExtractConfig ( - HiiConfigRouting, - ConfigResp, - &AccessProgress, - &AccessResults - ); + Status = gBS->HandleProtocol (Controller, &gEfiIp4Config2ProtocolGuid, (VOID **) &Ip4Config2); if (EFI_ERROR (Status)) { goto ON_EXIT; } - ConfigInfo = AllocateZeroPool (NIC_ITEM_CONFIG_SIZE); - if (ConfigInfo == NULL) { - goto ON_EXIT; - } - - ConfigInfo->Source = IP4_CONFIG_SOURCE_STATIC; - Len = NIC_ITEM_CONFIG_SIZE; - Status = HiiConfigRouting->ConfigToBlock ( - HiiConfigRouting, - AccessResults, - (UINT8 *) ConfigInfo, - &Len, - &AccessProgress - ); + Status = Ip4Config2->GetData (Ip4Config2, Ip4Config2DataTypePolicy, &DataSize, &Policy); if (EFI_ERROR (Status)) { goto ON_EXIT; } - - IsStatic = (BOOLEAN) (ConfigInfo->Source == IP4_CONFIG_SOURCE_STATIC); + + IsStatic = (BOOLEAN) (Policy == Ip4Config2PolicyStatic); ON_EXIT: - - if (AccessResults != NULL) { - FreePool (AccessResults); - } - if (ConfigInfo != NULL) { - FreePool (ConfigInfo); - } - if (ConfigResp != NULL) { - FreePool (ConfigResp); - } - if (ConfigHdr != NULL) { - FreePool (ConfigHdr); - } - + return IsStatic; } @@ -2585,7 +2559,6 @@ ON_EXIT: The header type of IPv4 device path node is MESSAGING_DEVICE_PATH. The header subtype of IPv4 device path node is MSG_IPv4_DP. - The length of the IPv4 device path node in bytes is 19. Get other info from parameters to make up the whole IPv4 device path node. @param[in, out] Node Pointer to the IPv4 device path node. @@ -2613,7 +2586,7 @@ NetLibCreateIPv4DPathNode ( { Node->Header.Type = MESSAGING_DEVICE_PATH; Node->Header.SubType = MSG_IPv4_DP; - SetDevicePathNodeLength (&Node->Header, 19); + SetDevicePathNodeLength (&Node->Header, sizeof (IPv4_DEVICE_PATH)); CopyMem (&Node->LocalIpAddress, &LocalIp, sizeof (EFI_IPv4_ADDRESS)); CopyMem (&Node->RemoteIpAddress, &RemoteIp, sizeof (EFI_IPv4_ADDRESS)); @@ -2628,6 +2601,14 @@ NetLibCreateIPv4DPathNode ( } else { Node->StaticIpAddress = NetLibDefaultAddressIsStatic (Controller); } + + // + // Set the Gateway IP address to default value 0:0:0:0. + // Set the Subnet mask to default value 255:255:255:0. + // + ZeroMem (&Node->GatewayIpAddress, sizeof (EFI_IPv4_ADDRESS)); + SetMem (&Node->SubnetMask, sizeof (EFI_IPv4_ADDRESS), 0xff); + Node->SubnetMask.Addr[3] = 0; } /** @@ -2669,7 +2650,14 @@ NetLibCreateIPv6DPathNode ( Node->RemotePort = RemotePort; Node->Protocol = Protocol; - Node->StaticIpAddress = FALSE; + + // + // Set default value to IPAddressOrigin, PrefixLength. + // Set the Gateway IP address to unspecified address. + // + Node->IpAddressOrigin = 0; + Node->PrefixLength = IP6_PREFIX_LENGTH; + ZeroMem (&Node->GatewayIpAddress, sizeof (EFI_IPv6_ADDRESS)); } /** @@ -2743,58 +2731,21 @@ NetLibAsciiStrToIp4 ( OUT EFI_IPv4_ADDRESS *Ip4Address ) { - UINT8 Index; - CHAR8 *Ip4Str; - CHAR8 *TempStr; - UINTN NodeVal; + RETURN_STATUS Status; + CHAR8 *EndPointer; - if ((String == NULL) || (Ip4Address == NULL)) { + Status = AsciiStrToIpv4Address (String, &EndPointer, Ip4Address, NULL); + if (RETURN_ERROR (Status) || (*EndPointer != '\0')) { return EFI_INVALID_PARAMETER; + } else { + return EFI_SUCCESS; } - - Ip4Str = (CHAR8 *) String; - - for (Index = 0; Index < 4; Index++) { - TempStr = Ip4Str; - - while ((*Ip4Str != '\0') && (*Ip4Str != '.')) { - Ip4Str++; - } - - // - // The IPv4 address is X.X.X.X - // - if (*Ip4Str == '.') { - if (Index == 3) { - return EFI_INVALID_PARAMETER; - } - } else { - if (Index != 3) { - return EFI_INVALID_PARAMETER; - } - } - - // - // Convert the string to IPv4 address. AsciiStrDecimalToUintn stops at the - // first character that is not a valid decimal character, '.' or '\0' here. - // - NodeVal = AsciiStrDecimalToUintn (TempStr); - if (NodeVal > 0xFF) { - return EFI_INVALID_PARAMETER; - } - - Ip4Address->Addr[Index] = (UINT8) NodeVal; - - Ip4Str++; - } - - return EFI_SUCCESS; } /** Convert one Null-terminated ASCII string to EFI_IPv6_ADDRESS. The format of the - string is defined in RFC 4291 - Text Pepresentation of Addresses. + string is defined in RFC 4291 - Text Representation of Addresses. @param[in] String The pointer to the Ascii string. @param[out] Ip6Address The pointer to the converted IPv6 address. @@ -2810,129 +2761,15 @@ NetLibAsciiStrToIp6 ( OUT EFI_IPv6_ADDRESS *Ip6Address ) { - UINT8 Index; - CHAR8 *Ip6Str; - CHAR8 *TempStr; - CHAR8 *TempStr2; - UINT8 NodeCnt; - UINT8 TailNodeCnt; - UINT8 AllowedCnt; - UINTN NodeVal; - BOOLEAN Short; - BOOLEAN Update; - - if ((String == NULL) || (Ip6Address == NULL)) { - return EFI_INVALID_PARAMETER; - } - - Ip6Str = (CHAR8 *) String; - AllowedCnt = 6; - - // - // An IPv6 address leading with : looks strange. - // - if (*Ip6Str == ':') { - if (*(Ip6Str + 1) != ':') { - return EFI_INVALID_PARAMETER; - } else { - AllowedCnt = 7; - } - } - - ZeroMem (Ip6Address, sizeof (EFI_IPv6_ADDRESS)); - - NodeCnt = 0; - TailNodeCnt = 0; - Short = FALSE; - Update = FALSE; - - for (Index = 0; Index < 15; Index = (UINT8) (Index + 2)) { - TempStr = Ip6Str; - - while ((*Ip6Str != '\0') && (*Ip6Str != ':')) { - Ip6Str++; - } - - if ((*Ip6Str == '\0') && (Index != 14)) { - return EFI_INVALID_PARAMETER; - } - - if (*Ip6Str == ':') { - if (*(Ip6Str + 1) == ':') { - if ((*(Ip6Str + 2) == '0') || (NodeCnt > 6)) { - // - // ::0 looks strange. report error to user. - // - return EFI_INVALID_PARAMETER; - } - - // - // Skip the abbreviation part of IPv6 address. - // - TempStr2 = Ip6Str + 2; - while ((*TempStr2 != '\0')) { - if (*TempStr2 == ':') { - if (*(TempStr2 + 1) == ':') { - // - // :: can only appear once in IPv6 address. - // - return EFI_INVALID_PARAMETER; - } - - TailNodeCnt++; - if (TailNodeCnt >= (AllowedCnt - NodeCnt)) { - // - // :: indicates one or more groups of 16 bits of zeros. - // - return EFI_INVALID_PARAMETER; - } - } - - TempStr2++; - } - - Short = TRUE; - Update = TRUE; - - Ip6Str = Ip6Str + 2; - } else { - Ip6Str++; - NodeCnt++; - if ((Short && (NodeCnt > 6)) || (!Short && (NodeCnt > 7))) { - // - // There are more than 8 groups of 16 bits of zeros. - // - return EFI_INVALID_PARAMETER; - } - } - } - - // - // Convert the string to IPv6 address. AsciiStrHexToUintn stops at the first - // character that is not a valid hexadecimal character, ':' or '\0' here. - // - NodeVal = AsciiStrHexToUintn (TempStr); - if ((NodeVal > 0xFFFF) || (Index > 14)) { - return EFI_INVALID_PARAMETER; - } + RETURN_STATUS Status; + CHAR8 *EndPointer; - Ip6Address->Addr[Index] = (UINT8) (NodeVal >> 8); - Ip6Address->Addr[Index + 1] = (UINT8) (NodeVal & 0xFF); - - // - // Skip the groups of zeros by :: - // - if (Short && Update) { - Index = (UINT8) (16 - (TailNodeCnt + 2) * 2); - Update = FALSE; - } - } - - if ((!Short && Index != 16) || (*Ip6Str != '\0')) { + Status = AsciiStrToIpv6Address (String, &EndPointer, Ip6Address, NULL); + if (RETURN_ERROR (Status) || (*EndPointer != '\0')) { return EFI_INVALID_PARAMETER; + } else { + return EFI_SUCCESS; } - - return EFI_SUCCESS; } @@ -2944,7 +2781,6 @@ NetLibAsciiStrToIp6 ( @retval EFI_SUCCESS Convert to IPv4 address successfully. @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL. - @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource. **/ EFI_STATUS @@ -2954,38 +2790,27 @@ NetLibStrToIp4 ( OUT EFI_IPv4_ADDRESS *Ip4Address ) { - CHAR8 *Ip4Str; - EFI_STATUS Status; + RETURN_STATUS Status; + CHAR16 *EndPointer; - if ((String == NULL) || (Ip4Address == NULL)) { + Status = StrToIpv4Address (String, &EndPointer, Ip4Address, NULL); + if (RETURN_ERROR (Status) || (*EndPointer != L'\0')) { return EFI_INVALID_PARAMETER; + } else { + return EFI_SUCCESS; } - - Ip4Str = (CHAR8 *) AllocatePool ((StrLen (String) + 1) * sizeof (CHAR8)); - if (Ip4Str == NULL) { - return EFI_OUT_OF_RESOURCES; - } - - UnicodeStrToAsciiStr (String, Ip4Str); - - Status = NetLibAsciiStrToIp4 (Ip4Str, Ip4Address); - - FreePool (Ip4Str); - - return Status; } /** Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS. The format of - the string is defined in RFC 4291 - Text Pepresentation of Addresses. + the string is defined in RFC 4291 - Text Representation of Addresses. @param[in] String The pointer to the Ascii string. @param[out] Ip6Address The pointer to the converted IPv6 address. @retval EFI_SUCCESS Convert to IPv6 address successfully. @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL. - @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource. **/ EFI_STATUS @@ -2995,30 +2820,20 @@ NetLibStrToIp6 ( OUT EFI_IPv6_ADDRESS *Ip6Address ) { - CHAR8 *Ip6Str; - EFI_STATUS Status; + RETURN_STATUS Status; + CHAR16 *EndPointer; - if ((String == NULL) || (Ip6Address == NULL)) { + Status = StrToIpv6Address (String, &EndPointer, Ip6Address, NULL); + if (RETURN_ERROR (Status) || (*EndPointer != L'\0')) { return EFI_INVALID_PARAMETER; + } else { + return EFI_SUCCESS; } - - Ip6Str = (CHAR8 *) AllocatePool ((StrLen (String) + 1) * sizeof (CHAR8)); - if (Ip6Str == NULL) { - return EFI_OUT_OF_RESOURCES; - } - - UnicodeStrToAsciiStr (String, Ip6Str); - - Status = NetLibAsciiStrToIp6 (Ip6Str, Ip6Address); - - FreePool (Ip6Str); - - return Status; } /** Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS and prefix length. - The format of the string is defined in RFC 4291 - Text Pepresentation of Addresses + The format of the string is defined in RFC 4291 - Text Representation of Addresses Prefixes: ipv6-address/prefix-length. @param[in] String The pointer to the Ascii string. @@ -3027,7 +2842,6 @@ NetLibStrToIp6 ( @retval EFI_SUCCESS Convert to IPv6 address successfully. @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL. - @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource. **/ EFI_STATUS @@ -3038,79 +2852,272 @@ NetLibStrToIp6andPrefix ( OUT UINT8 *PrefixLength ) { - CHAR8 *Ip6Str; - CHAR8 *PrefixStr; - CHAR8 *TempStr; - EFI_STATUS Status; - UINT8 Length; + RETURN_STATUS Status; + CHAR16 *EndPointer; - if ((String == NULL) || (Ip6Address == NULL) || (PrefixLength == NULL)) { + Status = StrToIpv6Address (String, &EndPointer, Ip6Address, PrefixLength); + if (RETURN_ERROR (Status) || (*EndPointer != L'\0')) { return EFI_INVALID_PARAMETER; + } else { + return EFI_SUCCESS; } +} - Ip6Str = (CHAR8 *) AllocatePool ((StrLen (String) + 1) * sizeof (CHAR8)); - if (Ip6Str == NULL) { - return EFI_OUT_OF_RESOURCES; +/** + + Convert one EFI_IPv6_ADDRESS to Null-terminated Unicode string. + The text representation of address is defined in RFC 4291. + + @param[in] Ip6Address The pointer to the IPv6 address. + @param[out] String The buffer to return the converted string. + @param[in] StringSize The length in bytes of the input String. + + @retval EFI_SUCCESS Convert to string successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small for the result. BufferSize has been + updated with the size needed to complete the request. +**/ +EFI_STATUS +EFIAPI +NetLibIp6ToStr ( + IN EFI_IPv6_ADDRESS *Ip6Address, + OUT CHAR16 *String, + IN UINTN StringSize + ) +{ + UINT16 Ip6Addr[8]; + UINTN Index; + UINTN LongestZerosStart; + UINTN LongestZerosLength; + UINTN CurrentZerosStart; + UINTN CurrentZerosLength; + CHAR16 Buffer[sizeof"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"]; + CHAR16 *Ptr; + + if (Ip6Address == NULL || String == NULL || StringSize == 0) { + return EFI_INVALID_PARAMETER; } - UnicodeStrToAsciiStr (String, Ip6Str); + // + // Convert the UINT8 array to an UINT16 array for easy handling. + // + ZeroMem (Ip6Addr, sizeof (Ip6Addr)); + for (Index = 0; Index < 16; Index++) { + Ip6Addr[Index / 2] |= (Ip6Address->Addr[Index] << ((1 - (Index % 2)) << 3)); + } // - // Get the sub string describing prefix length. + // Find the longest zeros and mark it. // - TempStr = Ip6Str; - while (*TempStr != '\0' && (*TempStr != '/')) { - TempStr++; + CurrentZerosStart = DEFAULT_ZERO_START; + CurrentZerosLength = 0; + LongestZerosStart = DEFAULT_ZERO_START; + LongestZerosLength = 0; + for (Index = 0; Index < 8; Index++) { + if (Ip6Addr[Index] == 0) { + if (CurrentZerosStart == DEFAULT_ZERO_START) { + CurrentZerosStart = Index; + CurrentZerosLength = 1; + } else { + CurrentZerosLength++; + } + } else { + if (CurrentZerosStart != DEFAULT_ZERO_START) { + if (CurrentZerosLength > 2 && (LongestZerosStart == (DEFAULT_ZERO_START) || CurrentZerosLength > LongestZerosLength)) { + LongestZerosStart = CurrentZerosStart; + LongestZerosLength = CurrentZerosLength; + } + CurrentZerosStart = DEFAULT_ZERO_START; + CurrentZerosLength = 0; + } + } } - - if (*TempStr == '/') { - PrefixStr = TempStr + 1; - } else { - PrefixStr = NULL; + + if (CurrentZerosStart != DEFAULT_ZERO_START && CurrentZerosLength > 2) { + if (LongestZerosStart == DEFAULT_ZERO_START || LongestZerosLength < CurrentZerosLength) { + LongestZerosStart = CurrentZerosStart; + LongestZerosLength = CurrentZerosLength; + } } - // - // Get the sub string describing IPv6 address and convert it. - // - *TempStr = '\0'; + Ptr = Buffer; + for (Index = 0; Index < 8; Index++) { + if (LongestZerosStart != DEFAULT_ZERO_START && Index >= LongestZerosStart && Index < LongestZerosStart + LongestZerosLength) { + if (Index == LongestZerosStart) { + *Ptr++ = L':'; + } + continue; + } + if (Index != 0) { + *Ptr++ = L':'; + } + Ptr += UnicodeSPrint(Ptr, 10, L"%x", Ip6Addr[Index]); + } + + if (LongestZerosStart != DEFAULT_ZERO_START && LongestZerosStart + LongestZerosLength == 8) { + *Ptr++ = L':'; + } + *Ptr = L'\0'; - Status = NetLibAsciiStrToIp6 (Ip6Str, Ip6Address); - if (EFI_ERROR (Status)) { - goto Exit; + if ((UINTN)Ptr - (UINTN)Buffer > StringSize) { + return EFI_BUFFER_TOO_SMALL; } - // - // If input string doesn't indicate the prefix length, return 0xff. - // - Length = 0xFF; + StrCpyS (String, StringSize / sizeof (CHAR16), Buffer); - // - // Convert the string to prefix length - // - if (PrefixStr != NULL) { + return EFI_SUCCESS; +} - Status = EFI_INVALID_PARAMETER; - Length = 0; - while (*PrefixStr != '\0') { - if (NET_IS_DIGIT (*PrefixStr)) { - Length = (UINT8) (Length * 10 + (*PrefixStr - '0')); - if (Length >= IP6_PREFIX_NUM) { - goto Exit; - } - } else { - goto Exit; - } +/** + This function obtains the system guid from the smbios table. - PrefixStr++; + @param[out] SystemGuid The pointer of the returned system guid. + + @retval EFI_SUCCESS Successfully obtained the system guid. + @retval EFI_NOT_FOUND Did not find the SMBIOS table. + +**/ +EFI_STATUS +EFIAPI +NetLibGetSystemGuid ( + OUT EFI_GUID *SystemGuid + ) +{ + EFI_STATUS Status; + SMBIOS_TABLE_ENTRY_POINT *SmbiosTable; + SMBIOS_TABLE_3_0_ENTRY_POINT *Smbios30Table; + SMBIOS_STRUCTURE_POINTER Smbios; + SMBIOS_STRUCTURE_POINTER SmbiosEnd; + CHAR8 *String; + + SmbiosTable = NULL; + Status = EfiGetSystemConfigurationTable (&gEfiSmbios3TableGuid, (VOID **) &Smbios30Table); + if (!(EFI_ERROR (Status) || Smbios30Table == NULL)) { + Smbios.Hdr = (SMBIOS_STRUCTURE *) (UINTN) Smbios30Table->TableAddress; + SmbiosEnd.Raw = (UINT8 *) (UINTN) (Smbios30Table->TableAddress + Smbios30Table->TableMaximumSize); + } else { + Status = EfiGetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID **) &SmbiosTable); + if (EFI_ERROR (Status) || SmbiosTable == NULL) { + return EFI_NOT_FOUND; } + Smbios.Hdr = (SMBIOS_STRUCTURE *) (UINTN) SmbiosTable->TableAddress; + SmbiosEnd.Raw = (UINT8 *) ((UINTN) SmbiosTable->TableAddress + SmbiosTable->TableLength); } - *PrefixLength = Length; - Status = EFI_SUCCESS; + do { + if (Smbios.Hdr->Type == 1) { + if (Smbios.Hdr->Length < 0x19) { + // + // Older version did not support UUID. + // + return EFI_NOT_FOUND; + } + + // + // SMBIOS tables are byte packed so we need to do a byte copy to + // prevend alignment faults on Itanium-based platform. + // + CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof (EFI_GUID)); + return EFI_SUCCESS; + } -Exit: + // + // Go to the next SMBIOS structure. Each SMBIOS structure may include 2 parts: + // 1. Formatted section; 2. Unformatted string section. So, 2 steps are needed + // to skip one SMBIOS structure. + // + + // + // Step 1: Skip over formatted section. + // + String = (CHAR8 *) (Smbios.Raw + Smbios.Hdr->Length); + + // + // Step 2: Skip over unformated string section. + // + do { + // + // Each string is terminated with a NULL(00h) BYTE and the sets of strings + // is terminated with an additional NULL(00h) BYTE. + // + for ( ; *String != 0; String++) { + } - FreePool (Ip6Str); - return Status; + if (*(UINT8*)++String == 0) { + // + // Pointer to the next SMBIOS structure. + // + Smbios.Raw = (UINT8 *)++String; + break; + } + } while (TRUE); + } while (Smbios.Raw < SmbiosEnd.Raw); + return EFI_NOT_FOUND; } +/** + Create Dns QName according the queried domain name. + QName is a domain name represented as a sequence of labels, + where each label consists of a length octet followed by that + number of octets. The QName terminates with the zero + length octet for the null label of the root. Caller should + take responsibility to free the buffer in returned pointer. + + @param DomainName The pointer to the queried domain name string. + + @retval NULL Failed to fill QName. + @return QName filled successfully. + +**/ +CHAR8 * +EFIAPI +NetLibCreateDnsQName ( + IN CHAR16 *DomainName + ) +{ + CHAR8 *QueryName; + UINTN QueryNameSize; + CHAR8 *Header; + CHAR8 *Tail; + UINTN Len; + UINTN Index; + + QueryName = NULL; + QueryNameSize = 0; + Header = NULL; + Tail = NULL; + + // + // One byte for first label length, one byte for terminated length zero. + // + QueryNameSize = StrLen (DomainName) + 2; + + if (QueryNameSize > DNS_MAX_NAME_SIZE) { + return NULL; + } + + QueryName = AllocateZeroPool (QueryNameSize); + if (QueryName == NULL) { + return NULL; + } + + Header = QueryName; + Tail = Header + 1; + Len = 0; + for (Index = 0; DomainName[Index] != 0; Index++) { + *Tail = (CHAR8) DomainName[Index]; + if (*Tail == '.') { + *Header = (CHAR8) Len; + Header = Tail; + Tail ++; + Len = 0; + } else { + Tail++; + Len++; + } + } + *Header = (CHAR8) Len; + *Tail = 0; + + return QueryName; +}